diff --git a/dev/include/dev/usb.h b/dev/include/dev/usb.h index 17fd4fcf..b78b11a7 100644 --- a/dev/include/dev/usb.h +++ b/dev/include/dev/usb.h @@ -64,18 +64,17 @@ status_t usb_setup(usb_config *config); uint8_t usb_get_current_iface_num_highspeed(void); uint8_t usb_get_current_iface_num_lowspeed(void); -/* apped new interface descriptors to the existing config if desired */ -status_t usb_append_interface_highspeed(const uint8_t *int_descr, size_t len); -status_t usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len); +/* Append new interface descriptors to the existing config.*/ +/* Returns interface number selected or error. */ +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); +/* add a new string to the existing config */ status_t usb_add_string(const char *string, uint8_t id); status_t usb_start(void); status_t usb_stop(void); -/* callback api the usbc driver uses */ -status_t usbc_callback(usb_callback_op_t op, const union usb_callback_args *args); - /* callback api that anyone can register for */ status_t usb_register_callback(usb_callback_t, void *cookie); diff --git a/dev/include/dev/usbc.h b/dev/include/dev/usbc.h index 479226d1..365ae2ba 100644 --- a/dev/include/dev/usbc.h +++ b/dev/include/dev/usbc.h @@ -12,6 +12,7 @@ #include #include #include +#include __BEGIN_CDECLS @@ -58,6 +59,9 @@ status_t usbc_flush_ep(ep_t ep); status_t usbc_set_active(bool active); void usbc_set_address(uint8_t address); +/* callback api the usbc driver uses */ +status_t usbc_callback(usb_callback_op_t op, const union usb_callback_args *args); + /* called back from within a callback to handle setup responses */ void usbc_ep0_ack(void); void usbc_ep0_stall(void); diff --git a/dev/usb/usb.c b/dev/usb/usb.c index e77fc122..0bb8ba4f 100644 --- a/dev/usb/usb.c +++ b/dev/usb/usb.c @@ -5,6 +5,8 @@ * license that can be found in the LICENSE file or at * https://opensource.org/licenses/MIT */ +#include + #include #include #include @@ -13,9 +15,16 @@ #include #include #include -#include #include +/* + * USB device side support. Handles configuration and main high level host requests. + * USB device controller must pump the stack via usbc_callback() and implement the + * usbc function interface. + */ + +/* TODO: make big endian safe */ + #define LOCAL_TRACE 0 #define MAX_STRINGS 8 @@ -38,8 +47,11 @@ typedef struct { static void usb_do_callbacks(usb_callback_op_t op, const union usb_callback_args *args); -static void append_desc_data(usb_descriptor *desc, const void *dat, size_t len) { +static status_t append_desc_data(usb_descriptor *desc, const void *dat, size_t len) { uint8_t *ptr = malloc(desc->len + len); + if (!ptr) { + return ERR_NO_MEMORY; + } memcpy(ptr, desc->desc, desc->len); memcpy(ptr + desc->len, dat, len); @@ -51,6 +63,8 @@ static void append_desc_data(usb_descriptor *desc, const void *dat, size_t len) desc->desc = ptr; desc->len += len; + + return NO_ERROR; } static uint8_t usb_get_current_iface_num(const usb_descriptor *desc) { @@ -67,16 +81,18 @@ uint8_t usb_get_current_iface_num_lowspeed(void) { return usb_get_current_iface_num(&usb.config->lowspeed.config); } -/* returns the interface number assigned */ +/* returns the interface number assigned or error */ static int usb_append_interface(usb_descriptor *desc, const uint8_t *int_descr, size_t len) { uint8_t *ptr = malloc(len); - int interface_num; + if (!ptr) { + return ERR_NO_MEMORY; + } // create a temporary copy of the interface memcpy(ptr, int_descr, len); // find the last interface used - interface_num = usb_get_current_iface_num(desc); // current interface + int interface_num = usb_get_current_iface_num(desc); // current interface // patch our interface descriptor with the new id ptr[2] = interface_num; @@ -90,6 +106,7 @@ static int usb_append_interface(usb_descriptor *desc, const uint8_t *int_descr, interface_num++; ((uint8_t *)desc->desc)[4] = interface_num; + DEBUG_ASSERT(interface_num > 0); return interface_num - 1; } @@ -101,26 +118,6 @@ int usb_append_interface_lowspeed(const uint8_t *int_descr, size_t len) { return usb_append_interface(&usb.config->lowspeed.config, int_descr, len); } -void usb_set_string_descriptor(usb_descriptor *desc, const char *string) { - int len = strlen(string); - ushort *data; - int datalen = len * 2 + 2; - - data = malloc(datalen); - - /* write length field */ - data[0] = 0x0300 + datalen; - - /* copy the string into the uint16_t based usb string */ - int i; - for (i = 0; i < len; i++) { - data[i + 1] = string[i]; - } - - desc->desc = (void *)data; - desc->len = datalen; -} - static void set_usb_id(uint16_t vendor, uint16_t product) { // patch the current configuration to with the vendor/product id ((uint16_t *)usb.config->lowspeed.device.desc)[4] = vendor; @@ -135,8 +132,9 @@ status_t usb_add_string(const char *string, uint8_t id) { size_t len = strlen(string); uint16_t *strbuf = malloc(len * 2 + 2); - if (!strbuf) + if (!strbuf) { return ERR_NO_MEMORY; + } /* build the usb string descriptor */ strbuf[0] = 0x300 | (len * 2 + 2); @@ -176,8 +174,9 @@ status_t usb_register_callback(usb_callback_t cb, void *cookie) { DEBUG_ASSERT(cb); usb_callback_container_t *c = malloc(sizeof(usb_callback_container_t)); - if (!c) + if (!c) { return ERR_NO_MEMORY; + } c->cb = cb; c->cookie = cookie;