[usb] merge in some changes from another code base to make the usb
string manipulation a little simpler
diff --git a/dev/usb/usb.c b/dev/usb/usb.c
index b3ddabc..1b1661d 100644
--- a/dev/usb/usb.c
+++ b/dev/usb/usb.c
@@ -27,12 +27,10 @@
#include <dev/usbc.h>
#include <dev/usb.h>
-//#include <lib/pmux.h>
-//#include <lib/usbmass.h>
-//#include <lib/dfu.h>
-//#include <target/usbconfig.h>
+#define LOCAL_TRACE 0
-#define LOCAL_TRACE 1
+#define MAX_STRINGS 8
+static usb_string strings[MAX_STRINGS];
static usb_config *config;
@@ -118,6 +116,29 @@
((uint16_t *)config->highspeed.device.desc)[5] = product;
}
+void usb_add_string(const char *string, uint8_t id)
+{
+ uint i;
+ size_t len = strlen(string);
+ uint16_t *strbuf = malloc(len * 2 + 2);
+
+ /* build the usb string descriptor */
+ strbuf[0] = 0x300 | (len * 2 + 2);
+ for (i = 0; i < len; i++) {
+ strbuf[i + 1] = (uint16_t)string[i];
+ }
+
+ /* find a slot to put it */
+ for (i = 0; i < MAX_STRINGS; i++) {
+ if (strings[i].id == 0) {
+ strings[i].string.desc = strbuf;
+ strings[i].string.len = len * 2 + 2;
+ strings[i].id = id;
+ break;
+ }
+ }
+}
+
static int default_usb_callback(usbc_callback_op_t op, const union usb_callback_args *args)
{
LTRACEF("op %d, args %p\n", op, args);
@@ -170,29 +191,26 @@
usbc_ep0_send(config->langid.desc,
config->langid.len, setup->length);
break;
- case 0x301: /* Manufacturer string */
- LTRACEF("got GET_DESCRIPTOR, mfg string\n");
- usbc_ep0_send(config->mfg_string.desc,
- config->mfg_string.len,
- setup->length);
- break;
- case 0x302: /* Device string */
- LTRACEF("got GET_DESCRIPTOR, device string\n");
- usbc_ep0_send(config->device_string.desc,
- config->device_string.len,
- setup->length);
- break;
- case 0x303: /* Serial number string */
- LTRACEF("got GET_DESCRIPTOR, serial number string\n");
- if (config->serial_string.desc) {
- usbc_ep0_send(config->serial_string.desc,
- config->serial_string.len,
+ case (0x301)...(0x3ff): {
+ /* string descriptor, search our list for a match */
+ uint i;
+ bool found = false;
+ uint8_t id = setup->value & 0xff;
+ for (i = 0; i < MAX_STRINGS; i++) {
+ if (strings[i].id == id) {
+ usbc_ep0_send(strings[i].string.desc,
+ strings[i].string.len,
setup->length);
- } else {
- /* stall */
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ /* couldn't find one, stall */
usbc_ep0_stall();
}
break;
+ }
case 0x600: /* DEVICE QUALIFIER */
LTRACEF("got GET_DESCRIPTOR, device qualifier\n");
usbc_ep0_send(speed->device_qual.desc,
diff --git a/include/dev/usb.h b/include/dev/usb.h
index a35a819..e33bea4 100644
--- a/include/dev/usb.h
+++ b/include/dev/usb.h
@@ -32,6 +32,11 @@
size_t len;
} usb_descriptor __ALIGNED(2);
+typedef struct {
+ usb_descriptor string;
+ uint8_t id;
+} usb_string;
+
/* complete usb config struct, passed in to usb_setup() */
typedef struct {
struct usb_descriptor_speed {
@@ -39,9 +44,6 @@
usb_descriptor device_qual;
usb_descriptor config;
} lowspeed, highspeed;
- usb_descriptor device_string;
- usb_descriptor mfg_string;
- usb_descriptor serial_string;
usb_descriptor langid;
} usb_config;
@@ -54,6 +56,8 @@
int usb_append_interface_highspeed(const uint8_t *int_descr, size_t len);
int usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len);
+void usb_add_string(const char *string, uint8_t id);
+
void usb_start(void);
void usb_stop(void);
diff --git a/project/beagle-test/init.c b/project/beagle-test/init.c
index 12a0fbf..725ec5c 100644
--- a/project/beagle-test/init.c
+++ b/project/beagle-test/init.c
@@ -39,9 +39,6 @@
.device_qual = { devqual_descr, sizeof(devqual_descr) },
.config = { cfg_descr_highspeed, sizeof(cfg_descr_highspeed) },
},
- .device_string = { dstring, sizeof(dstring) },
- .mfg_string = { mstring, sizeof(mstring) },
- .serial_string = { NULL, 0 },
.langid = { langid, sizeof(langid) }
};
@@ -55,6 +52,8 @@
usb_init();
usb_setup(&config);
+ usb_add_string("lk, Inc.", 0x1); // manufacturer id
+ usb_add_string("lk", 0x2); // device string
usb_start();
console_start();
diff --git a/project/beagle-test/usb_descriptors.h b/project/beagle-test/usb_descriptors.h
index 1bfce4c..cfd499a 100644
--- a/project/beagle-test/usb_descriptors.h
+++ b/project/beagle-test/usb_descriptors.h
@@ -81,16 +81,6 @@
0, /* Power Consumption - none */
};
-static ushort dstring[] = {
- 0x0306, /* numchars * 2 + 2 */
- 'l', 'k'
-};
-
-static ushort mstring[] = {
- 0x0310, /* numchars * 2 + 2 */
- 'l', 'k', ',', 'i', 'n', 'c', '.'
-};
-
static uchar langid[] __ALIGNED(2) = { 0x04, 0x03, 0x09, 0x04 };
#endif