diff --git a/dev/usb/usb.c b/dev/usb/usb.c index 6843d1a1..d1082ef8 100644 --- a/dev/usb/usb.c +++ b/dev/usb/usb.c @@ -46,7 +46,12 @@ static void append_desc_data(usb_descriptor *desc, const void *dat, size_t len) memcpy(ptr, desc->desc, desc->len); memcpy(ptr + desc->len, dat, len); - free(desc->desc); + + /* free the old buffer if it wasn't marked static */ + if ((desc->flags & USB_DESC_FLAG_STATIC) == 0) + free(desc->desc); + desc->flags &= ~USB_DESC_FLAG_STATIC; + desc->desc = ptr; desc->len += len; } @@ -144,6 +149,17 @@ status_t usb_add_string(const char *string, uint8_t id) return ERR_NO_MEMORY; } +static void usb_set_active_config(uint8_t config) +{ + if (config != active_config) { + active_config = config; + if (active_config != 0) + printf("usb online\n"); + else + printf("usb offline\n"); + } +} + status_t usb_callback(usbc_callback_op_t op, const union usb_callback_args *args) { LTRACEF("op %d, args %p\n", op, args); @@ -241,10 +257,8 @@ status_t usb_callback(usbc_callback_op_t op, const union usb_callback_args *args case SET_CONFIGURATION: LTRACEF("SET_CONFIGURATION %d\n", setup->value); - active_config = setup->value; - if (active_config != 0) - printf("usb online\n"); usbc_ep0_ack(); + usb_set_active_config(setup->value); break; case GET_CONFIGURATION: diff --git a/include/dev/usb.h b/include/dev/usb.h index 775cfd70..03c23c1b 100644 --- a/include/dev/usb.h +++ b/include/dev/usb.h @@ -30,9 +30,12 @@ typedef struct { void *desc; size_t len; + uint flags; } usb_descriptor __ALIGNED(2); -#define USB_DESC_STATIC(x) { .desc = (void *)(x), .len = sizeof(x) } +#define USB_DESC_FLAG_STATIC (0x1) + +#define USB_DESC_STATIC(x) { .desc = (void *)(x), .len = sizeof(x), .flags = USB_DESC_FLAG_STATIC } typedef struct { usb_descriptor string;