usbstack: Revise usb string descriptor table to use enum values for indices

This makes it possible for macros of conditionally included string
descriptors to get a correct index no matter what other usb drivers
are enabled or disabled due to the nature behavior of enums.

Change-Id: I8ccebbd316605bed0f5d90b6b73fab4a333c02fa
This commit is contained in:
James Buren 2021-10-06 16:40:54 -05:00 committed by Aidan MacDonald
parent 4be81c2385
commit c0a59b9a6a
2 changed files with 17 additions and 10 deletions

View File

@ -39,6 +39,14 @@
extern int usb_max_pkt_size;
enum {
USB_STRING_INDEX_LANGUAGE,
USB_STRING_INDEX_MANUFACTURER,
USB_STRING_INDEX_PRODUCT,
USB_STRING_INDEX_SERIAL,
USB_STRING_INDEX_MAX,
};
struct usb_class_driver;
void usb_core_init(void);

View File

@ -90,9 +90,9 @@ static struct usb_device_descriptor __attribute__((aligned(2)))
.idVendor = USB_VENDOR_ID,
.idProduct = USB_PRODUCT_ID,
.bcdDevice = 0x0100,
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.iManufacturer = USB_STRING_INDEX_MANUFACTURER,
.iProduct = USB_STRING_INDEX_PRODUCT,
.iSerialNumber = USB_STRING_INDEX_SERIAL,
.bNumConfigurations = 1
} ;
@ -141,12 +141,12 @@ static const struct usb_string_descriptor __attribute__((aligned(2)))
lang_descriptor =
USB_STRING_INITIALIZER(u"\x0409"); /* LANGID US English */
static const struct usb_string_descriptor* const usb_strings[] =
static const struct usb_string_descriptor* const usb_strings[USB_STRING_INDEX_MAX] =
{
&lang_descriptor,
&usb_string_iManufacturer,
&usb_string_iProduct,
&usb_string_iSerial
[USB_STRING_INDEX_LANGUAGE] = &lang_descriptor,
[USB_STRING_INDEX_MANUFACTURER] = &usb_string_iManufacturer,
[USB_STRING_INDEX_PRODUCT] = &usb_string_iProduct,
[USB_STRING_INDEX_SERIAL] = &usb_string_iSerial,
};
static int usb_address = 0;
@ -637,8 +637,7 @@ static void request_handler_device_get_descriptor(struct usb_ctrlrequest* req)
case USB_DT_STRING:
logf("STRING %d", index);
if((unsigned)index < (sizeof(usb_strings) /
sizeof(struct usb_string_descriptor*))) {
if((unsigned)index < USB_STRING_INDEX_MAX) {
size = usb_strings[index]->bLength;
ptr = usb_strings[index];
}