Change in simtrace2[master]: simtrace2-cardem-pcsc: Make it work again

2021-04-05 Thread laforge
laforge has submitted this change. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23634 )

Change subject: simtrace2-cardem-pcsc: Make it work again
..

simtrace2-cardem-pcsc: Make it work again

* support Interrupt STATUS notifications
* use osmocom libusb abstraction
* use asynchronous URBs for interrupt + bulk

Change-Id: Ib04798572295f25477719124530b6584780c5b75
---
M host/src/simtrace2-cardem-pcsc.c
1 file changed, 159 insertions(+), 23 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/host/src/simtrace2-cardem-pcsc.c b/host/src/simtrace2-cardem-pcsc.c
index f11330c..543780f 100644
--- a/host/src/simtrace2-cardem-pcsc.c
+++ b/host/src/simtrace2-cardem-pcsc.c
@@ -47,11 +47,13 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 

 #define ATR_MAX_LEN 33

+#define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args)

 /* reasonable ATR offering all protocols and voltages
  * smartphones might not care, but other readers do
@@ -180,6 +182,9 @@
case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
rc = process_do_rx_da(ci, buf, len);
break;
+   case SIMTRACE_MSGT_BD_CEMU_CONFIG:
+   /* firmware confirms configuration change; ignore */
+   break;
default:
printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
rc = -1;
@@ -189,6 +194,144 @@
return rc;
 }

+/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
+static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t 
*buf, int len)
+{
+   const struct cardemu_usb_msg_status *status = (struct 
cardemu_usb_msg_status *) buf;
+
+   LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, 
wi=%u wtime=%u\n",
+   status->flags, status->fi, status->di, status->wi,
+   status->waiting_time);
+
+   return 0;
+}
+
+static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t 
*buf, unsigned int len)
+{
+   struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
+   int rc;
+
+   LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
+
+   buf += sizeof(*sh);
+
+   switch (sh->msg_type) {
+   case SIMTRACE_MSGT_BD_CEMU_STATUS:
+   rc = process_irq_status(ci, buf, len);
+   break;
+   default:
+   LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", 
sh->msg_type);
+   rc = -1;
+   break;
+   }
+
+   return rc;
+}
+
+static void usb_in_xfer_cb(struct libusb_transfer *xfer)
+{
+   struct osmo_st2_cardem_inst *ci = xfer->user_data;
+   int rc;
+
+   switch (xfer->status) {
+   case LIBUSB_TRANSFER_COMPLETED:
+   /* hand the message up the stack */
+   process_usb_msg(ci, xfer->buffer, xfer->actual_length);
+   break;
+   case LIBUSB_TRANSFER_NO_DEVICE:
+   LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
+   exit(1);
+   break;
+   default:
+   LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", 
xfer->status);
+   exit(1);
+   break;
+   }
+
+   /* re-submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);
+}
+
+
+static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci)
+{
+   struct osmo_st2_transport *transp = ci->slot->transp;
+   struct libusb_transfer *xfer;
+   int rc;
+
+   xfer = libusb_alloc_transfer(0);
+   OSMO_ASSERT(xfer);
+   xfer->dev_handle = transp->usb_devh;
+   xfer->flags = 0;
+   xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
+   xfer->endpoint = transp->usb_ep.in;
+   xfer->timeout = 0;
+   xfer->user_data = ci;
+   xfer->length = 16*256;
+
+   xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
+   OSMO_ASSERT(xfer->buffer);
+   xfer->callback = usb_in_xfer_cb;
+
+   /* submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);
+}
+
+
+static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
+{
+   struct osmo_st2_cardem_inst *ci = xfer->user_data;
+   int rc;
+
+   switch (xfer->status) {
+   case LIBUSB_TRANSFER_COMPLETED:
+   process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
+   break;
+   case LIBUSB_TRANSFER_NO_DEVICE:
+   LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
+   exit(1);
+   break;
+   default:
+   LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", 
xfer->status);
+   exit(1);
+   break;
+   }
+
+   /* re-submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);
+}
+
+

Change in simtrace2[master]: simtrace2-cardem-pcsc: Make it work again

2021-04-05 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23634 )

Change subject: simtrace2-cardem-pcsc: Make it work again
..


Patch Set 1: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/c/simtrace2/+/23634
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-Change-Id: Ib04798572295f25477719124530b6584780c5b75
Gerrit-Change-Number: 23634
Gerrit-PatchSet: 1
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Comment-Date: Tue, 06 Apr 2021 00:38:50 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in simtrace2[master]: simtrace2-cardem-pcsc: Make it work again

2021-04-05 Thread laforge
laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/simtrace2/+/23634 )


Change subject: simtrace2-cardem-pcsc: Make it work again
..

simtrace2-cardem-pcsc: Make it work again

* support Interrupt STATUS notifications
* use osmocom libusb abstraction
* use asynchronous URBs for interrupt + bulk

Change-Id: Ib04798572295f25477719124530b6584780c5b75
---
M host/src/simtrace2-cardem-pcsc.c
1 file changed, 159 insertions(+), 23 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/34/23634/1

diff --git a/host/src/simtrace2-cardem-pcsc.c b/host/src/simtrace2-cardem-pcsc.c
index f11330c..543780f 100644
--- a/host/src/simtrace2-cardem-pcsc.c
+++ b/host/src/simtrace2-cardem-pcsc.c
@@ -47,11 +47,13 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 

 #define ATR_MAX_LEN 33

+#define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args)

 /* reasonable ATR offering all protocols and voltages
  * smartphones might not care, but other readers do
@@ -180,6 +182,9 @@
case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
rc = process_do_rx_da(ci, buf, len);
break;
+   case SIMTRACE_MSGT_BD_CEMU_CONFIG:
+   /* firmware confirms configuration change; ignore */
+   break;
default:
printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
rc = -1;
@@ -189,6 +194,144 @@
return rc;
 }

+/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
+static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t 
*buf, int len)
+{
+   const struct cardemu_usb_msg_status *status = (struct 
cardemu_usb_msg_status *) buf;
+
+   LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, 
wi=%u wtime=%u\n",
+   status->flags, status->fi, status->di, status->wi,
+   status->waiting_time);
+
+   return 0;
+}
+
+static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t 
*buf, unsigned int len)
+{
+   struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
+   int rc;
+
+   LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
+
+   buf += sizeof(*sh);
+
+   switch (sh->msg_type) {
+   case SIMTRACE_MSGT_BD_CEMU_STATUS:
+   rc = process_irq_status(ci, buf, len);
+   break;
+   default:
+   LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", 
sh->msg_type);
+   rc = -1;
+   break;
+   }
+
+   return rc;
+}
+
+static void usb_in_xfer_cb(struct libusb_transfer *xfer)
+{
+   struct osmo_st2_cardem_inst *ci = xfer->user_data;
+   int rc;
+
+   switch (xfer->status) {
+   case LIBUSB_TRANSFER_COMPLETED:
+   /* hand the message up the stack */
+   process_usb_msg(ci, xfer->buffer, xfer->actual_length);
+   break;
+   case LIBUSB_TRANSFER_NO_DEVICE:
+   LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
+   exit(1);
+   break;
+   default:
+   LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", 
xfer->status);
+   exit(1);
+   break;
+   }
+
+   /* re-submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);
+}
+
+
+static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci)
+{
+   struct osmo_st2_transport *transp = ci->slot->transp;
+   struct libusb_transfer *xfer;
+   int rc;
+
+   xfer = libusb_alloc_transfer(0);
+   OSMO_ASSERT(xfer);
+   xfer->dev_handle = transp->usb_devh;
+   xfer->flags = 0;
+   xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
+   xfer->endpoint = transp->usb_ep.in;
+   xfer->timeout = 0;
+   xfer->user_data = ci;
+   xfer->length = 16*256;
+
+   xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
+   OSMO_ASSERT(xfer->buffer);
+   xfer->callback = usb_in_xfer_cb;
+
+   /* submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);
+}
+
+
+static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
+{
+   struct osmo_st2_cardem_inst *ci = xfer->user_data;
+   int rc;
+
+   switch (xfer->status) {
+   case LIBUSB_TRANSFER_COMPLETED:
+   process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
+   break;
+   case LIBUSB_TRANSFER_NO_DEVICE:
+   LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
+   exit(1);
+   break;
+   default:
+   LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", 
xfer->status);
+   exit(1);
+   break;
+   }
+
+   /* re-submit the IN transfer */
+   rc = libusb_submit_transfer(xfer);
+   OSMO_ASSERT(rc == 0);