<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-e1-hardware/+/21775">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">icE1usb fw: USB control request handling<br><br>This introduces a number of vendor-specific control requests for<br>configuration of the icE1usb from the host software.<br><br>Closes: OS#4675<br>Change-Id: I9d28566ba21a2a78def5e4a0ba07ecbc4a583aa9<br>---<br>A firmware/ice40-riscv/icE1usb/ice1usb_proto.h<br>M firmware/ice40-riscv/icE1usb/usb_e1.c<br>2 files changed, 196 insertions(+), 1 deletion(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-e1-hardware refs/changes/75/21775/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/firmware/ice40-riscv/icE1usb/ice1usb_proto.h b/firmware/ice40-riscv/icE1usb/ice1usb_proto.h</span><br><span>new file mode 100644</span><br><span>index 0000000..b636ec6</span><br><span>--- /dev/null</span><br><span>+++ b/firmware/ice40-riscv/icE1usb/ice1usb_proto.h</span><br><span>@@ -0,0 +1,70 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Header file describing the USB protocol between the icE1usb firmware and the host</span><br><span style="color: hsl(120, 100%, 40%);">+ * software (currently really only osmo-e1d) */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/***********************************************************************</span><br><span style="color: hsl(120, 100%, 40%);">+ * Control Endpoint / Device Requests</span><br><span style="color: hsl(120, 100%, 40%);">+ ***********************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! returns a bit-mask of optional device capabilities (see enum e1usb_dev_capability) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_DEV_GET_CAPABILITIES      0x01</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_DEV_GET_FW_BUILD  0x02</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum e1usb_dev_capability {</span><br><span style="color: hsl(120, 100%, 40%);">+   /*! Does this board have a GPS-DO */</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_DEV_CAP_GPSDO,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* Interface Requests */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*! returns a bit-mask of optional device capabilities (see enum e1usb_intf_capability) */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_INTF_GET_CAPABILITIES   0x01</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_INTF_SET_TX_CFG           0x02    /*!< struct ice1usb_tx_config */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_INTF_GET_TX_CFG            0x03    /*!< struct ice1usb_tx_config */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_INTF_SET_RX_CFG            0x04    /*!< struct ice1usb_rx_config */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ICE1USB_INTF_GET_RX_CFG            0x05    /*!< struct ice1usb_rx_config */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+//enum e1usb_intf_capability { };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum ice1usb_tx_mode {</span><br><span style="color: hsl(120, 100%, 40%);">+    ICE1USB_TX_MODE_TRANSP          = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_MODE_TS0             = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_MODE_TS0_CRC4        = 2,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_MODE_TS0_CRC4_E      = 3,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum ice1usb_tx_timing {</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_TIME_SRC_LOCAL       = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_TIME_SRC_REMOTE      = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum ice1usb_tx_ext_loopback {</span><br><span style="color: hsl(120, 100%, 40%);">+    ICE1USB_TX_EXT_LOOPBACK_OFF     = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_EXT_LOOPBACK_SAME    = 1,</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_TX_EXT_LOOPBACK_CROSS   = 2,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* ICE1USB_INTF_{GET,SET}_TX_CFG */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ice1usb_tx_config {</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t mode;           /*!< enum ice1usb_tx_mode */</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t timing;         /*!< enum ice1usb_tx_timing */</span><br><span style="color: hsl(120, 100%, 40%);">+     uint8_t ext_loopback;   /*!< enum ice1usb_tx_ext_loopback */</span><br><span style="color: hsl(120, 100%, 40%);">+       uint8_t alarm;          /*!< 1 = transmit alarm; 0 = don't */</span><br><span style="color: hsl(120, 100%, 40%);">+} __attribute__((packed));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+enum ice1usb_rx_mode {</span><br><span style="color: hsl(120, 100%, 40%);">+  /*! transparent, unaligned bitstream */</span><br><span style="color: hsl(120, 100%, 40%);">+       ICE1USB_RX_MODE_TRANSP          = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+  /*! alignment to E1 frame */</span><br><span style="color: hsl(120, 100%, 40%);">+  ICE1USB_RX_MODE_FRAME           = 2,</span><br><span style="color: hsl(120, 100%, 40%);">+  /*! alignment to E1 multiframe */</span><br><span style="color: hsl(120, 100%, 40%);">+     ICE1USB_RX_MODE_MULTIFRAME      = 3,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* ICE1USB_INTF_{GET,SET}_RX_CFG */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ice1usb_rx_config {</span><br><span style="color: hsl(120, 100%, 40%);">+   uint8_t mode;           /*!< enum ice1usb_rx_mode */</span><br><span style="color: hsl(120, 100%, 40%);">+} __attribute__((packed));</span><br><span>diff --git a/firmware/ice40-riscv/icE1usb/usb_e1.c b/firmware/ice40-riscv/icE1usb/usb_e1.c</span><br><span>index e594a99..e910852 100644</span><br><span>--- a/firmware/ice40-riscv/icE1usb/usb_e1.c</span><br><span>+++ b/firmware/ice40-riscv/icE1usb/usb_e1.c</span><br><span>@@ -15,12 +15,28 @@</span><br><span> #include "console.h"</span><br><span> #include "misc.h"</span><br><span> #include "e1.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "e1_hw.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "ice1usb_proto.h"</span><br><span> </span><br><span> struct {</span><br><span>       bool running;           /* are we running (transceiving USB data)? */</span><br><span>        int out_bdi;            /* buffer descriptor index for OUT EP */</span><br><span>     int in_bdi;             /* buffer descriptor index for IN EP */</span><br><span style="color: hsl(0, 100%, 40%);">-} g_usb_e1;</span><br><span style="color: hsl(120, 100%, 40%);">+    struct ice1usb_tx_config tx_cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+      struct ice1usb_rx_config rx_cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+} g_usb_e1 = {</span><br><span style="color: hsl(120, 100%, 40%);">+      /* default configuration at power-up */</span><br><span style="color: hsl(120, 100%, 40%);">+       .tx_cfg = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .mode           = ICE1USB_TX_MODE_TS0_CRC4_E,</span><br><span style="color: hsl(120, 100%, 40%);">+         .timing         = ICE1USB_TX_TIME_SRC_REMOTE,</span><br><span style="color: hsl(120, 100%, 40%);">+         .ext_loopback   = ICE1USB_TX_EXT_LOOPBACK_OFF,</span><br><span style="color: hsl(120, 100%, 40%);">+                .alarm          = 0,</span><br><span style="color: hsl(120, 100%, 40%);">+  },</span><br><span style="color: hsl(120, 100%, 40%);">+    .rx_cfg = {</span><br><span style="color: hsl(120, 100%, 40%);">+           .mode           = ICE1USB_RX_MODE_MULTIFRAME,</span><br><span style="color: hsl(120, 100%, 40%);">+ },</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span> </span><br><span> </span><br><span> /* Hack */</span><br><span>@@ -253,10 +269,119 @@</span><br><span>        return USB_FND_SUCCESS;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static bool</span><br><span style="color: hsl(120, 100%, 40%);">+_set_tx_mode_done(struct usb_xfer *xfer)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    const struct ice1usb_tx_config *cfg = (const struct ice1usb_tx_config *) xfer->data;</span><br><span style="color: hsl(120, 100%, 40%);">+       printf("set_tx_mode %02x%02x%02x%02x\r\n",</span><br><span style="color: hsl(120, 100%, 40%);">+          xfer->data[0], xfer->data[1], xfer->data[2], xfer->data[3]);</span><br><span style="color: hsl(120, 100%, 40%);">+      g_usb_e1.tx_cfg = *cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+       e1_tx_config(E1_TX_CR_ENABLE |</span><br><span style="color: hsl(120, 100%, 40%);">+                        ((cfg->mode & 2) << 1) |</span><br><span style="color: hsl(120, 100%, 40%);">+                 ((cfg->timing & 1) << 3) |</span><br><span style="color: hsl(120, 100%, 40%);">+                       ((cfg->alarm & 1) << 4) |</span><br><span style="color: hsl(120, 100%, 40%);">+                        ((cfg->ext_loopback & 2) << 5) );</span><br><span style="color: hsl(120, 100%, 40%);">+        return true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static bool</span><br><span style="color: hsl(120, 100%, 40%);">+_set_rx_mode_done(struct usb_xfer *xfer)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+   const struct ice1usb_rx_config *cfg = (const struct ice1usb_rx_config *) xfer->data;</span><br><span style="color: hsl(120, 100%, 40%);">+       printf("set_rx_mode %02x\r\n", xfer->data[0]);</span><br><span style="color: hsl(120, 100%, 40%);">+   g_usb_e1.rx_cfg = *cfg;</span><br><span style="color: hsl(120, 100%, 40%);">+       e1_rx_config(E1_RX_CR_ENABLE | (cfg->mode << 1));</span><br><span style="color: hsl(120, 100%, 40%);">+    return true;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* per-interface requests */</span><br><span style="color: hsl(120, 100%, 40%);">+static enum usb_fnd_resp</span><br><span style="color: hsl(120, 100%, 40%);">+_e1_ctrl_req_intf(struct usb_ctrl_req *req, struct usb_xfer *xfer)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      unsigned int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     switch (req->bRequest) {</span><br><span style="color: hsl(120, 100%, 40%);">+   case ICE1USB_INTF_GET_CAPABILITIES:</span><br><span style="color: hsl(120, 100%, 40%);">+           /* no optional capabilities yet */</span><br><span style="color: hsl(120, 100%, 40%);">+            xfer->len = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ICE1USB_INTF_SET_TX_CFG:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (req->wLength < sizeof(struct ice1usb_tx_config))</span><br><span style="color: hsl(120, 100%, 40%);">+                    return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+         xfer->cb_done = _set_tx_mode_done;</span><br><span style="color: hsl(120, 100%, 40%);">+         xfer->cb_ctx = req;</span><br><span style="color: hsl(120, 100%, 40%);">+                xfer->len = sizeof(struct ice1usb_tx_config);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ICE1USB_INTF_GET_TX_CFG:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (req->wLength < sizeof(struct ice1usb_tx_config))</span><br><span style="color: hsl(120, 100%, 40%);">+                    return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+         memcpy(xfer->data, &g_usb_e1.tx_cfg, sizeof(struct ice1usb_tx_config));</span><br><span style="color: hsl(120, 100%, 40%);">+                xfer->len = sizeof(struct ice1usb_tx_config);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ICE1USB_INTF_SET_RX_CFG:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (req->wLength < sizeof(struct ice1usb_rx_config))</span><br><span style="color: hsl(120, 100%, 40%);">+                    return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+         xfer->cb_done = _set_rx_mode_done;</span><br><span style="color: hsl(120, 100%, 40%);">+         xfer->cb_ctx = req;</span><br><span style="color: hsl(120, 100%, 40%);">+                xfer->len = sizeof(struct ice1usb_rx_config);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        case ICE1USB_INTF_GET_RX_CFG:</span><br><span style="color: hsl(120, 100%, 40%);">+         if (req->wLength < sizeof(struct ice1usb_rx_config))</span><br><span style="color: hsl(120, 100%, 40%);">+                    return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+         memcpy(xfer->data, &g_usb_e1.rx_cfg, sizeof(struct ice1usb_rx_config));</span><br><span style="color: hsl(120, 100%, 40%);">+                xfer->len = sizeof(struct ice1usb_rx_config);</span><br><span style="color: hsl(120, 100%, 40%);">+              break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return USB_FND_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* device-global requests */</span><br><span style="color: hsl(120, 100%, 40%);">+static enum usb_fnd_resp</span><br><span style="color: hsl(120, 100%, 40%);">+_e1_ctrl_req_dev(struct usb_ctrl_req *req, struct usb_xfer *xfer)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    switch (req->bRequest) {</span><br><span style="color: hsl(120, 100%, 40%);">+   case ICE1USB_DEV_GET_CAPABILITIES:</span><br><span style="color: hsl(120, 100%, 40%);">+            xfer->data[0] = (1 << ICE1USB_DEV_CAP_GPSDO);</span><br><span style="color: hsl(120, 100%, 40%);">+                xfer->len = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+             break;</span><br><span style="color: hsl(120, 100%, 40%);">+        default:</span><br><span style="color: hsl(120, 100%, 40%);">+              return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   return USB_FND_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* USB host issues a control request to us */</span><br><span style="color: hsl(120, 100%, 40%);">+static enum usb_fnd_resp</span><br><span style="color: hsl(120, 100%, 40%);">+_e1_ctrl_req(struct usb_ctrl_req *req, struct usb_xfer *xfer)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+     if (USB_REQ_TYPE(req) != USB_REQ_TYPE_VENDOR)</span><br><span style="color: hsl(120, 100%, 40%);">+         return USB_FND_CONTINUE;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    switch (USB_REQ_RCPT(req)) {</span><br><span style="color: hsl(120, 100%, 40%);">+  case USB_REQ_RCPT_DEV:</span><br><span style="color: hsl(120, 100%, 40%);">+                return _e1_ctrl_req_dev(req, xfer);</span><br><span style="color: hsl(120, 100%, 40%);">+   case USB_REQ_RCPT_INTF:</span><br><span style="color: hsl(120, 100%, 40%);">+               if (req->wIndex != 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                      return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+         return _e1_ctrl_req_intf(req, xfer);</span><br><span style="color: hsl(120, 100%, 40%);">+  case USB_REQ_RCPT_EP:</span><br><span style="color: hsl(120, 100%, 40%);">+ case USB_REQ_RCPT_OTHER:</span><br><span style="color: hsl(120, 100%, 40%);">+      default:</span><br><span style="color: hsl(120, 100%, 40%);">+              return USB_FND_ERROR;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static struct usb_fn_drv _e1_drv = {</span><br><span>     .set_conf       = _e1_set_conf,</span><br><span>         .set_intf       = _e1_set_intf,</span><br><span>         .get_intf       = _e1_get_intf,</span><br><span style="color: hsl(120, 100%, 40%);">+ .ctrl_req       = _e1_ctrl_req,</span><br><span> };</span><br><span> </span><br><span> void</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-e1-hardware/+/21775">change 21775</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-e1-hardware/+/21775"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-e1-hardware </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I9d28566ba21a2a78def5e4a0ba07ecbc4a583aa9 </div>
<div style="display:none"> Gerrit-Change-Number: 21775 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>