8 Commits

Author SHA1 Message Date
Harald Welte
eb8fbc0296 fw/qmod: enable modem RESET by default at boot
When the uC firmware starts, assert the (high-active) _RESET GPIOs,
which will cause the transistors to drive the mPCIe slot !PERST
low.

There is of course a short instance of time between the uC reset
until it executes wwan_perst_init(). To avoid this, a hardware
pull-up would have to be re-worked onto the _RESET[1..4] signals
on the PCBA

Change-Id: I8742fefa4bf02e728e34d5d8cbc0fda771e78ffb
2021-06-06 11:46:50 +02:00
Harald Welte
9292d22726 WIP: firmware: sniffer: inbound USB handling
Change-Id: Ic953148600d82285281abcb573a07e2e9b8082a7
2021-06-06 11:46:50 +02:00
Harald Welte
d2bc858ddf firmware: More common infrastructure for USB handling
Let's move low-level handling of endpoint queue refill from the
individual apps into common/shared code.

Now the main simply has to call usb_process() for every interface,
and inbound messages will be dispatched to call-back functions from
there.

Change-Id: Ic6f9c6c1ffdbb0c9c3b284371ecc83b17e3be746
2021-06-06 11:46:49 +02:00
Harald Welte
29de264d6e HACK: Solve weird problem wih lost OUT transfer on ping-pong endpoint
This adds an unconditional endpoint reset procedure to every SET_FEATURE(UnHalt).

It doesn't really make sense that this is required, *particularly* as
we *MUST NOT* set bEndpoint->bank to 0 here.

Without this patch, I'm observing the following problem:

Every first OUT transfer after a SET_INTERFACE + UNHALT on a bulk endpoint
is lost. "lost" means that it completes successfully on the host, can
be seen completing successfully with an ACK on a USB bus analyzer,
but still doesn't show up in the firmware.  No Endpoint Interrupt
is generated.

This can be reproduced by calling libusb_set_interface_alt_setting()
from the host and then submitting a single OUT transfer.

Change-Id: I18ed530e617baddf76e8f9829512443ce2a76e0d
2021-06-06 11:46:49 +02:00
Kévin Redon
07fda1e706 card_emu: use edge-triggered VCC ADC logic
Before this patch, we used to st ci->vcc_active depending on the
instantaneous ADC reading of VCC.  Is it > .5v, we claim VCC is active,
and if it's below, VCC is inactive.

With this patch we move to an edge triggered approach: Only change
ci->vcc_active if the previous reading was different from the current
reading.

FIXME: why?

Change-Id: I71b703162219484e43638f1f2f692e9dd554ef55
2021-06-06 11:46:49 +02:00
Harald Welte
ed4ca25730 Introduce simtrace2-tool
The simtrace-tool isa command line tool which can be used to e.g.
manually request a modem reset.

Change-Id: I3a8896ac2b3caef7590b51118359e5caed820a40
2021-06-06 11:46:49 +02:00
Harald Welte
69c32f2e38 simtrace2-list: Use osmo_st2_compatible_dev_ids[]
we shouldn't use a local copy of the device id list, which is already
outdated now that OCTSIMTEST support has been added to libosmo-st2

Change-Id: I2231006b94c33fe3b28ce37b0d54c67206751058
2021-06-06 11:46:49 +02:00
Harald Welte
53dfaa0aab don't printf() directly from library code, go via libosmocore logging
This allows us to use different log levels, and permits the user to use
other log targets.

Change-Id: I08ef7cfa5d8734882746a11ccd5f059d757401ae
2021-06-06 11:46:49 +02:00
11 changed files with 496 additions and 82 deletions

View File

@@ -221,6 +221,7 @@ extern int main(void)
}
last_simtrace_config = simtrace_config;
} else {
//FIXME: usb_proces() for every interface in this configuration?
if (config_func_ptrs[simtrace_config].run) {
config_func_ptrs[simtrace_config].run();
}

View File

@@ -1672,6 +1672,10 @@ uint8_t USBD_HAL_Halt(uint8_t bEndpoint, uint8_t ctl)
UDP->UDP_RST_EP |= 1 << bEndpoint;
UDP->UDP_RST_EP &= ~(1 << bEndpoint);
}
/* This fixes a weird bug with regard to ping-pong OUT endpoints */
UDP->UDP_RST_EP |= 1 << bEndpoint;
UDP->UDP_RST_EP &= ~(1 << bEndpoint);
}
/* Return Halt status */

View File

@@ -91,8 +91,8 @@
#define PINS_WWAN_IN { PIN_WWAN1, PIN_WWAN2 }
/* outputs controlling RESET input of modems */
#define PIN_PERST1 {PIO_PA25, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_PULLUP}
#define PIN_PERST2 {PIO_PA26, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_PULLUP}
#define PIN_PERST1 {PIO_PA25, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_PULLUP}
#define PIN_PERST2 {PIO_PA26, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_PULLUP}
#define PINS_PERST { PIN_PERST1, PIN_PERST2 }
#define PIN_VERSION_DET {PIO_PA19, PIOA, ID_PIOA, PIO_PERIPH_D, PIO_DEFAULT}

View File

@@ -42,5 +42,15 @@ int usb_drain_queue(uint8_t ep);
void usb_buf_init(void);
struct usb_buffered_ep *usb_get_buf_ep(uint8_t ep);
int usb_refill_to_host(uint8_t ep);
int usb_refill_from_host(uint8_t ep);
struct usb_if {
uint8_t if_num; /* interface number */
uint8_t ep_out; /* OUT endpoint (0 if none) */
uint8_t ep_in; /* IN endpint (0 if none) */
uint8_t ep_int; /* INT endpoint (0 if none) */
void *data; /* opaque data, passed through */
struct {
/* call-back to be called for inclming messages on OUT EP */
void (*rx_out)(struct msgb *msg, const struct usb_if *usb_if);
} ops;
};
void usb_process(const struct usb_if *usb_if);

View File

@@ -57,7 +57,7 @@ static void usb_write_cb(uint8_t *arg, uint8_t status, uint32_t transferred,
}
/* check if the spcified IN endpoint is idle and submit the next buffer from queue */
int usb_refill_to_host(uint8_t ep)
static int usb_refill_to_host(uint8_t ep)
{
struct usb_buffered_ep *bep = usb_get_buf_ep(ep);
struct msgb *msg;
@@ -130,7 +130,7 @@ static void usb_read_cb(uint8_t *arg, uint8_t status, uint32_t transferred,
}
/* refill the read queue for data received from host PC on OUT EP, if needed */
int usb_refill_from_host(uint8_t ep)
static int usb_refill_from_host(uint8_t ep)
{
struct usb_buffered_ep *bep = usb_get_buf_ep(ep);
struct msgb *msg;
@@ -198,3 +198,45 @@ int usb_drain_queue(uint8_t ep)
return ret;
}
/* iterate over the queue of incoming USB commands and dispatch/execute
* them */
static void process_any_usb_commands(const struct usb_if *usb_if)
{
struct llist_head *queue = usb_get_queue(usb_if->ep_out);
struct llist_head *lh;
struct msgb *msg;
int i;
/* limit the number of iterations to 10, to ensure we don't get
* stuck here without returning to main loop processing */
for (i = 0; i < 10; i++) {
/* de-queue the list head in an irq-safe way */
lh = llist_head_dequeue_irqsafe(queue);
if (!lh)
break;
msg = llist_entry(lh, struct msgb, list);
usb_if->ops.rx_out(msg, usb_if);
}
}
/* perform any action related to USB processing (IRQ/INT/OUT EP refill, handling OUT) */
void usb_process(const struct usb_if *usb_if)
{
/* first try to send any pending messages on IRQ */
if (usb_if->ep_int)
usb_refill_to_host(usb_if->ep_int);
/* then try to send any pending messages on IN */
if (usb_if->ep_in)
usb_refill_to_host(usb_if->ep_in);
/* ensure we can handle incoming USB messages from the
* host */
if (usb_if->ep_out) {
usb_refill_from_host(usb_if->ep_out);
process_any_usb_commands(usb_if);
}
}

View File

@@ -34,6 +34,8 @@
#define TRACE_ENTRY() TRACE_DEBUG("%s entering\r\n", __func__)
static void dispatch_received_usb_msg(struct msgb *msg, const struct usb_if *usb_if);
#ifdef PINS_CARDSIM
static const Pin pins_cardsim[] = PINS_CARDSIM;
#endif
@@ -68,12 +70,11 @@ struct cardem_inst {
bool half_time_notified;
} wt;
int usb_pending_old;
uint8_t ep_out;
uint8_t ep_in;
uint8_t ep_int;
struct usb_if usb_if;
const Pin pin_insert;
#ifdef DETECT_VCC_BY_ADC
uint32_t vcc_uv;
uint32_t vcc_uv_last;
#endif
bool vcc_active;
bool vcc_active_last;
@@ -89,9 +90,16 @@ struct cardem_inst cardem_inst[] = {
.id = ID_USART1,
.state = USART_RCV
},
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM1_INT,
.usb_if = {
.if_num = 0,
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM1_INT,
.data = &cardem_inst[0],
.ops = {
.rx_out = dispatch_received_usb_msg,
},
},
#ifdef PIN_SET_USIM1_PRES
.pin_insert = PIN_SET_USIM1_PRES,
#endif
@@ -104,9 +112,16 @@ struct cardem_inst cardem_inst[] = {
.id = ID_USART0,
.state = USART_RCV
},
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM2_INT,
.usb_if = {
.if_num = 1,
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM2_INT,
.data = &cardem_inst[1],
.ops = {
.rx_out = dispatch_received_usb_msg,
},
},
#ifdef PIN_SET_USIM2_PRES
.pin_insert = PIN_SET_USIM2_PRES,
#endif
@@ -253,6 +268,7 @@ static uint16_t compute_next_timeout(struct cardem_inst *ci)
* \param[in] inst_num Instance number, range 0..1 (some boards only '0' permitted) */
static void usart_irq_rx(uint8_t inst_num)
{
OSMO_ASSERT(inst_num < ARRAY_SIZE(cardem_inst));
Usart *usart = get_usart_by_chan(inst_num);
struct cardem_inst *ci = &cardem_inst[inst_num];
uint32_t csr;
@@ -461,10 +477,14 @@ static int card_vcc_adc_init(void)
static void process_vcc_adc(struct cardem_inst *ci)
{
if (ci->vcc_uv >= VCC_UV_THRESH_3V)
if (ci->vcc_uv >= VCC_UV_THRESH_3V &&
ci->vcc_uv_last < VCC_UV_THRESH_3V) {
ci->vcc_active = true;
else
} else if (ci->vcc_uv < VCC_UV_THRESH_3V &&
ci->vcc_uv_last >= VCC_UV_THRESH_3V) {
ci->vcc_active = false;
}
ci->vcc_uv_last = ci->vcc_uv;
}
void ADC_IrqHandler(void)
@@ -781,8 +801,9 @@ static void dispatch_usb_command_modem(struct msgb *msg, struct cardem_inst *ci)
}
/* handle a single USB command as received from the USB host */
static void dispatch_usb_command(struct msgb *msg, struct cardem_inst *ci)
static void dispatch_usb_command(struct msgb *msg, const struct usb_if *usb_if)
{
struct cardem_inst *ci = usb_if->data;
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h;
if (msgb_length(msg) < sizeof(*sh)) {
@@ -811,7 +832,8 @@ static void dispatch_usb_command(struct msgb *msg, struct cardem_inst *ci)
}
}
static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
/* handle a single USB transfer as received from the USB host */
static void dispatch_received_usb_msg(struct msgb *msg, const struct usb_if *usb_if)
{
struct msgb *segm;
struct simtrace_msg_hdr *mh;
@@ -822,7 +844,7 @@ static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
mh = (struct simtrace_msg_hdr *) msg->data;
if (mh->msg_len == msgb_length(msg)) {
/* fast path: only one message in buffer */
dispatch_usb_command(msg, ci);
dispatch_usb_command(msg, usb_if);
return;
}
@@ -831,23 +853,23 @@ static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
while (1) {
mh = (struct simtrace_msg_hdr *) msg->data;
segm = usb_buf_alloc(ci->ep_out);
segm = usb_buf_alloc(usb_if->ep_out);
if (!segm) {
TRACE_ERROR("%u: ENOMEM during msg segmentation\r\n",
ci->num);
usb_if->if_num);
break;
}
if (mh->msg_len > msgb_length(msg)) {
TRACE_ERROR("%u: Unexpected large message (%u bytes)\r\n",
ci->num, mh->msg_len);
usb_if->if_num, mh->msg_len);
usb_buf_free(segm);
break;
} else {
uint8_t *cur = msgb_put(segm, mh->msg_len);
segm->l1h = segm->head;
memcpy(cur, mh, mh->msg_len);
dispatch_usb_command(segm, ci);
dispatch_usb_command(segm, usb_if);
}
/* pull this message */
msgb_pull(msg, mh->msg_len);
@@ -859,35 +881,14 @@ static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
usb_buf_free(msg);
}
/* iterate over the queue of incoming USB commands and dispatch/execute
* them */
static void process_any_usb_commands(struct llist_head *main_q,
struct cardem_inst *ci)
{
struct llist_head *lh;
struct msgb *msg;
int i;
/* limit the number of iterations to 10, to ensure we don't get
* stuck here without returning to main loop processing */
for (i = 0; i < 10; i++) {
/* de-queue the list head in an irq-safe way */
lh = llist_head_dequeue_irqsafe(main_q);
if (!lh)
break;
msg = llist_entry(lh, struct msgb, list);
dispatch_received_msg(msg, ci);
}
}
/* main loop function, called repeatedly */
void mode_cardemu_run(void)
{
struct llist_head *queue;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) {
struct cardem_inst *ci = &cardem_inst[i];
struct usb_if *usb_if = &ci->usb_if;
/* drain the ring buffer from UART into card_emu */
while (1) {
@@ -904,16 +905,6 @@ void mode_cardemu_run(void)
process_io_statechg(ci);
/* first try to send any pending messages on IRQ */
usb_refill_to_host(ci->ep_int);
/* then try to send any pending messages on IN */
usb_refill_to_host(ci->ep_in);
/* ensure we can handle incoming USB messages from the
* host */
usb_refill_from_host(ci->ep_out);
queue = usb_get_queue(ci->ep_out);
process_any_usb_commands(queue, ci);
usb_process(&ci->usb_if);
}
}

View File

@@ -975,20 +975,42 @@ static void usb_send_change(uint32_t flags)
usb_msg_upd_len_and_submit(usb_msg);
}
/* handle incoming message from USB OUT EP */
static void dispatch_usb_out(struct msgb *msg, const struct usb_if *usb_if)
{
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h;
if (msgb_length(msg) < sizeof(*sh)) {
usb_buf_free(msg);
return;
}
msg->l2h = msg->l1h + sizeof(*sh);
switch (sh->msg_class) {
case SIMTRACE_MSGC_GENERIC:
break;
default:
break;
}
usb_buf_free(msg);
}
static const struct usb_if sniffer_usb_if = {
.if_num = 0,
.ep_in = SIMTRACE_USB_EP_CARD_DATAIN,
.ep_int = SIMTRACE_USB_EP_CARD_INT,
.ep_out = SIMTRACE_USB_EP_CARD_DATAOUT,
.ops = {
.rx_out = dispatch_usb_out,
}
};
/* Main (idle/busy) loop of this USB configuration */
void Sniffer_run(void)
{
/* Handle USB queue */
/* first try to send any pending messages on INT */
usb_refill_to_host(SIMTRACE_USB_EP_CARD_INT);
/* then try to send any pending messages on IN */
usb_refill_to_host(SIMTRACE_USB_EP_CARD_DATAIN);
/* ensure we can handle incoming USB messages from the host */
/* currently we don't need any incoming data
usb_refill_from_host(SIMTRACE_USB_EP_CARD_DATAOUT);
struct llist_head *queue = usb_get_queue(SIMTRACE_USB_EP_CARD_DATAOUT);
process_any_usb_commands(queue);
*/
usb_process(&sniffer_usb_if);
/* WARNING: the signal data and flags are not synchronized. We have to hope
* the processing is fast enough to not land in the wrong state while data

View File

@@ -44,9 +44,13 @@
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/sim/class_tables.h>
#include <osmocom/sim/sim.h>
#define LOGSLOT(slot, lvl, fmt, args...) \
LOGP(DLINP, lvl, "[%u] " fmt, (slot)->slot_nr, ## args)
/***********************************************************************
* SIMTRACE core protocol
***********************************************************************/
@@ -141,7 +145,6 @@ int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
OSMO_ASSERT(transp);
st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
printf("SIMtrace <- %s\n", msgb_hexdump(msg));
if (transp->udp_fd < 0) {
if (transp->usb_async)
@@ -166,6 +169,8 @@ int osmo_st2_cardem_request_card_insert(struct osmo_st2_cardem_inst *ci, bool in
struct msgb *msg = st_msgb_alloc();
struct cardemu_usb_msg_cardinsert *cins;
LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(inserted=%d)\n", __func__, inserted);
cins = (struct cardemu_usb_msg_cardinsert *) msgb_put(msg, sizeof(*cins));
memset(cins, 0, sizeof(*cins));
if (inserted)
@@ -181,7 +186,7 @@ int osmo_st2_cardem_request_pb_and_rx(struct osmo_st2_cardem_inst *ci, uint8_t p
struct cardemu_usb_msg_tx_data *txd;
txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
printf("<= %s(%02x, %d)\n", __func__, pb, le);
LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(pb=%02x, le=%u)\n", __func__, pb, le);
memset(txd, 0, sizeof(*txd));
txd->data_len = 1;
@@ -202,7 +207,7 @@ int osmo_st2_cardem_request_pb_and_tx(struct osmo_st2_cardem_inst *ci, uint8_t p
txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
printf("<= %s(%02x, %s, %d)\n", __func__, pb,
LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(pb=%02x, tx=%s, len=%d)\n", __func__, pb,
osmo_hexdump(data, data_len_in), data_len_in);
memset(txd, 0, sizeof(*txd));
@@ -226,7 +231,7 @@ int osmo_st2_cardem_request_sw_tx(struct osmo_st2_cardem_inst *ci, const uint8_t
txd = (struct cardemu_usb_msg_tx_data *) msgb_put(msg, sizeof(*txd));
printf("<= %s(%02x %02x)\n", __func__, sw[0], sw[1]);
LOGSLOT(ci->slot, LOGL_DEBUG, "<= %s(sw=%02x%02x)\n", __func__, sw[0], sw[1]);
memset(txd, 0, sizeof(*txd));
txd->data_len = 2;
@@ -246,7 +251,7 @@ int osmo_st2_cardem_request_set_atr(struct osmo_st2_cardem_inst *ci, const uint8
satr = (struct cardemu_usb_msg_set_atr *) msgb_put(msg, sizeof(*satr));
printf("<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(%s)\n", __func__, osmo_hexdump(atr, atr_len));
memset(satr, 0, sizeof(*satr));
satr->atr_len = atr_len;
@@ -263,7 +268,7 @@ int osmo_st2_cardem_request_config(struct osmo_st2_cardem_inst *ci, uint32_t fea
cfg = (struct cardemu_usb_msg_config *) msgb_put(msg, sizeof(*cfg));
printf("<= %s(%08x)\n", __func__, features);
LOGSLOT(ci->slot, LOGL_NOTICE, "<= %s(features=%08x)\n", __func__, features);
memset(cfg, 0, sizeof(*cfg));
cfg->features = features;
@@ -280,6 +285,9 @@ static int _modem_reset(struct osmo_st2_slot *slot, uint8_t asserted, uint16_t p
struct msgb *msg = st_msgb_alloc();
struct st_modem_reset *sr ;
LOGSLOT(slot, LOGL_NOTICE, "<= %s(asserted=%u, pulse_ms=%u)\n", __func__,
asserted, pulse_ms);
sr = (struct st_modem_reset *) msgb_put(msg, sizeof(*sr));
sr->asserted = asserted;
sr->pulse_duration_msec = pulse_ms;
@@ -310,6 +318,8 @@ static int _modem_sim_select(struct osmo_st2_slot *slot, uint8_t remote_sim)
struct msgb *msg = st_msgb_alloc();
struct st_modem_sim_select *ss;
LOGSLOT(slot, LOGL_NOTICE, "<= %s(remote_sim=%u)\n", __func__, remote_sim);
ss = (struct st_modem_sim_select *) msgb_put(msg, sizeof(*ss));
ss->remote_sim = remote_sim;

View File

@@ -5,10 +5,12 @@ AM_LDFLAGS=$(COVERAGE_LDFLAGS)
LDADD= $(top_builddir)/lib/libosmo-simtrace2.la \
$(LIBOSMOCORE_LIBS) $(LIBOSMOSIM_LIBS) $(LIBOSMOUSB_LIBS) $(LIBUSB_LIBS)
bin_PROGRAMS = simtrace2-cardem-pcsc simtrace2-list simtrace2-sniff
bin_PROGRAMS = simtrace2-cardem-pcsc simtrace2-list simtrace2-sniff simtrace2-tool
simtrace2_cardem_pcsc_SOURCES = simtrace2-cardem-pcsc.c
simtrace2_list_SOURCES = simtrace2_usb.c
simtrace2_sniff_SOURCES = simtrace2-sniff.c
simtrace2_tool_SOURCES = simtrace2-tool.c

338
host/src/simtrace2-tool.c Normal file
View File

@@ -0,0 +1,338 @@
/* simtrace2-tool - main program for the host PC to provide a remote SIM
* using the SIMtrace 2 firmware in card emulation mode
*
* (C) 2019 by Harald Welte <hwelte@hmw-consulting.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <sys/types.h>
#include <libusb.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace2_api.h>
#include <osmocom/simtrace2/simtrace_prot.h>
#include <osmocom/simtrace2/gsmtap.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
/***********************************************************************
* Incoming Messages
***********************************************************************/
static void print_welcome(void)
{
printf("simtrace2-tool\n"
"(C) 2019 Harald Welte <laforge@gnumonks.org>\n");
}
static void print_help(void)
{
printf( "simtrace2-tool [OPTIONS] COMMAND\n\n");
printf( "Options:\n"
"\t-h\t--help\n"
"\t-V\t--usb-vendor\tVENDOR_ID\n"
"\t-P\t--usb-product\tPRODUCT_ID\n"
"\t-C\t--usb-config\tCONFIG_ID\n"
"\t-I\t--usb-interface\tINTERFACE_ID\n"
"\t-S\t--usb-altsetting ALTSETTING_ID\n"
"\t-A\t--usb-address\tADDRESS\n"
"\t-H\t--usb-path\tPATH\n"
"\n"
);
printf( "Commands:\n"
"\tmodem reset (enable|disable|cycle)\n"
"\tmodem sim-switch (local|remote)\n"
"\n");
}
static const struct option opts[] = {
{ "help", 0, 0, 'h' },
{ "usb-vendor", 1, 0, 'V' },
{ "usb-product", 1, 0, 'P' },
{ "usb-config", 1, 0, 'C' },
{ "usb-interface", 1, 0, 'I' },
{ "usb-altsetting", 1, 0, 'S' },
{ "usb-address", 1, 0, 'A' },
{ "usb-path", 1, 0, 'H' },
{ NULL, 0, 0, 0 }
};
static void run_mainloop(struct osmo_st2_cardem_inst *ci)
{
struct osmo_st2_transport *transp = ci->slot->transp;
uint8_t buf[16*265];
int xfer_len;
int rc;
while (1) {
/* read data from SIMtrace2 device */
rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
buf, sizeof(buf), &xfer_len, 100);
if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
rc != LIBUSB_ERROR_INTERRUPTED &&
rc != LIBUSB_ERROR_IO) {
fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
return;
}
/* break the loop if no new messages arrive within 100ms */
if (rc == LIBUSB_ERROR_TIMEOUT)
return;
}
}
static struct osmo_st2_transport _transp;
static struct osmo_st2_slot _slot = {
.transp = &_transp,
.slot_nr = 0,
};
struct osmo_st2_cardem_inst _ci = {
.slot = &_slot,
};
struct osmo_st2_cardem_inst *ci = &_ci;
/* perform a modem reset */
static int do_modem_reset(int argc, char **argv)
{
char *command;
if (argc < 1)
command = "cycle";
else {
command = argv[0];
argc--;
argv++;
}
if (!strcmp(command, "enable")) {
printf("Activating Modem RESET\n");
return osmo_st2_modem_reset_active(ci->slot);
} else if (!strcmp(command, "disable")) {
printf("Deactivating Modem RESET\n");
return osmo_st2_modem_reset_inactive(ci->slot);
} else if (!strcmp(command, "cycle")) {
printf("Pulsing Modem RESET (1s)\n");
return osmo_st2_modem_reset_pulse(ci->slot, 1000);
} else {
fprintf(stderr, "Unsupported modem reset command: '%s'\n", command);
return -EINVAL;
}
return 0;
}
/* switch between local and remote (emulated) SIM */
static int do_modem_sim_switch(int argc, char **argv)
{
char *command;
if (argc < 1)
return -EINVAL;
command = argv[0];
argc--;
argv++;
if (!strcmp(command, "local")) {
printf("Setting SIM=LOCAL; Modem reset recommended\n");
return osmo_st2_modem_sim_select_local(ci->slot);
} else if (!strcmp(command, "remote")) {
printf("Setting SIM=REMOTE; Modem reset recommended\n");
return osmo_st2_modem_sim_select_remote(ci->slot);
} else {
fprintf(stderr, "Unsupported modem sim-switch command: '%s'\n", command);
return -EINVAL;
}
return 0;
}
static int do_subsys_modem(int argc, char **argv)
{
char *command;
int rc;
if (argc < 1)
return -EINVAL;
command = argv[0];
argc--;
argv++;
if (!strcmp(command, "reset")) {
rc = do_modem_reset(argc, argv);
} else if (!strcmp(command, "sim-switch")) {
rc = do_modem_sim_switch(argc, argv);
} else {
fprintf(stderr, "Unsupported command for subsystem modem: '%s'\n", command);
return -EINVAL;
}
return rc;
}
static int do_command(int argc, char **argv)
{
char *subsys;
int rc;
if (argc < 1)
return -EINVAL;
subsys = argv[0];
argc--;
argv++;
if (!strcmp(subsys, "modem"))
rc = do_subsys_modem(argc, argv);
else {
fprintf(stderr, "Unsupported subsystem '%s'\n", subsys);
rc = -EINVAL;
}
return rc;
}
int main(int argc, char **argv)
{
struct osmo_st2_transport *transp = ci->slot->transp;
int rc;
int c, ret = 1;
int if_num = 0, vendor_id = -1, product_id = -1;
int config_id = -1, altsetting = 0, addr = -1;
char *path = NULL;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "hV:P:C:I:S:A:H:", opts, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
print_help();
exit(0);
break;
case 'V':
vendor_id = strtol(optarg, NULL, 16);
break;
case 'P':
product_id = strtol(optarg, NULL, 16);
break;
case 'C':
config_id = atoi(optarg);
break;
case 'I':
if_num = atoi(optarg);
break;
case 'S':
altsetting = atoi(optarg);
break;
case 'A':
addr = atoi(optarg);
break;
case 'H':
path = optarg;
break;
}
}
if ((vendor_id < 0 || product_id < 0)) {
fprintf(stderr, "You have to specify the vendor and product ID\n");
goto do_exit;
}
transp->udp_fd = -1;
rc = libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto do_exit;
}
print_welcome();
do {
if (transp->udp_fd < 0) {
struct usb_interface_match _ifm, *ifm = &_ifm;
ifm->vendor = vendor_id;
ifm->product = product_id;
ifm->configuration = config_id;
ifm->interface = if_num;
ifm->altsetting = altsetting;
ifm->addr = addr;
if (path)
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
if (!transp->usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
}
rc = libusb_claim_interface(transp->usb_devh, if_num);
if (rc < 0) {
fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
goto close_exit;
}
rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
&transp->usb_ep.in, &transp->usb_ep.irq_in);
if (rc < 0) {
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
goto close_exit;
}
}
if (argc - optind <= 0) {
fprintf(stderr, "You have to specify a command to execute\n");
exit(1);
}
rc = do_command(argc-optind, argv+optind);
switch (rc) {
case 0:
break;
case -EINVAL:
fprintf(stderr, "Error: Invalid command/syntax\n");
exit(1);
break;
default:
fprintf(stderr, "Error executing command: %d\n", rc);
exit(1);
break;
}
run_mainloop(ci);
ret = 0;
libusb_release_interface(transp->usb_devh, 0);
close_exit:
if (transp->usb_devh)
libusb_close(transp->usb_devh);
} while (0);
libusb_exit(NULL);
do_exit:
return ret;
}

View File

@@ -25,13 +25,7 @@
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace_usb.h>
static const struct dev_id compatible_dev_ids[] = {
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3 },
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_QMOD_SAM3 },
{ USB_VENDOR_OPENMOKO, USB_PRODUCT_SIMTRACE2 },
{ 0, 0 }
};
#include <osmocom/simtrace2/usb_util.h>
static int find_devices(void)
{
@@ -39,7 +33,7 @@ static int find_devices(void)
int rc, i, num_interfaces;
/* scan for USB devices matching SIMtrace USB ID with proprietary class */
rc = osmo_libusb_find_matching_interfaces(NULL, compatible_dev_ids,
rc = osmo_libusb_find_matching_interfaces(NULL, osmo_st2_compatible_dev_ids,
USB_CLASS_PROPRIETARY, -1, -1, ifm, ARRAY_SIZE(ifm));
printf("USB matches: %d\n", rc);
if (rc < 0)