From 018372bf39d8bc994c9aec325fd71640c4de2f9e Mon Sep 17 00:00:00 2001 From: James Buren Date: Thu, 25 Mar 2021 13:10:50 -0500 Subject: usb: implement macro for initializing USB strings This uses the new unicode string literal feature that is available now to greatly simplify the initialization of these special string types. This makes them much more readable at a quick glance. Change-Id: Iad8b49aa763486608e3bb7e83fb8abfb48ce0a7b --- firmware/export/usb_drv.h | 7 +++++++ firmware/usbstack/usb_core.c | 29 ++++------------------------- utils/hwstub/stub/main.c | 26 ++++---------------------- utils/hwstub/stub/usb_drv.h | 7 +++++++ 4 files changed, 22 insertions(+), 47 deletions(-) diff --git a/firmware/export/usb_drv.h b/firmware/export/usb_drv.h index b5b5a7f065..7ef7c8b7ee 100644 --- a/firmware/export/usb_drv.h +++ b/firmware/export/usb_drv.h @@ -80,4 +80,11 @@ bool usb_drv_connected(void); int usb_drv_request_endpoint(int type, int dir); void usb_drv_release_endpoint(int ep); +/* USB_STRING_INITIALIZER(u"Example String") */ +#define USB_STRING_INITIALIZER(S) { \ + sizeof(struct usb_string_descriptor) + sizeof(S) - sizeof(*S), \ + USB_DT_STRING, \ + S \ +} + #endif /* _USB_DRV_H */ diff --git a/firmware/usbstack/usb_core.c b/firmware/usbstack/usb_core.c index 038aef241f..3d187c8cab 100644 --- a/firmware/usbstack/usb_core.c +++ b/firmware/usbstack/usb_core.c @@ -120,43 +120,22 @@ static const struct usb_qualifier_descriptor __attribute__((aligned(2))) static const struct usb_string_descriptor __attribute__((aligned(2))) usb_string_iManufacturer = -{ - 24, - USB_DT_STRING, - {'R', 'o', 'c', 'k', 'b', 'o', 'x', '.', 'o', 'r', 'g'} -}; +USB_STRING_INITIALIZER(u"Rockbox.org"); static const struct usb_string_descriptor __attribute__((aligned(2))) usb_string_iProduct = -{ - 42, - USB_DT_STRING, - {'R', 'o', 'c', 'k', 'b', 'o', 'x', ' ', - 'm', 'e', 'd', 'i', 'a', ' ', - 'p', 'l', 'a', 'y', 'e', 'r'} -}; +USB_STRING_INITIALIZER(u"Rockbox media player"); static struct usb_string_descriptor __attribute__((aligned(2))) usb_string_iSerial = -{ - 84, - USB_DT_STRING, - {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', - '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', - '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', - '0', '0', '0', '0', '0', '0', '0', '0'} -}; +USB_STRING_INITIALIZER(u"00000000000000000000000000000000000000000"); /* Generic for all targets */ /* this is stringid #0: languages supported */ static const struct usb_string_descriptor __attribute__((aligned(2))) lang_descriptor = -{ - 4, - USB_DT_STRING, - {0x0409} /* LANGID US English */ -}; +USB_STRING_INITIALIZER(u"\x0409"); /* LANGID US English */ static const struct usb_string_descriptor* const usb_strings[] = { diff --git a/utils/hwstub/stub/main.c b/utils/hwstub/stub/main.c index 7b6b02b83a..9e2e7116df 100644 --- a/utils/hwstub/stub/main.c +++ b/utils/hwstub/stub/main.c @@ -95,35 +95,17 @@ static struct usb_interface_descriptor interface_descriptor = }; static const struct usb_string_descriptor usb_string_iManufacturer = -{ - 24, - USB_DT_STRING, - {'R', 'o', 'c', 'k', 'b', 'o', 'x', '.', 'o', 'r', 'g'} -}; +USB_STRING_INITIALIZER(u"Rockbox.org"); static const struct usb_string_descriptor usb_string_iProduct = -{ - 44, - USB_DT_STRING, - {'R', 'o', 'c', 'k', 'b', 'o', 'x', ' ', - 'h', 'a', 'r', 'd', 'w', 'a', 'r', 'e', ' ', - 's', 't', 'u', 'b'} -}; +USB_STRING_INITIALIZER(u"Rockbox hardware stub"); static const struct usb_string_descriptor usb_string_iInterface = -{ - 14, - USB_DT_STRING, - {'H', 'W', 'S', 't', 'u', 'b'} -}; +USB_STRING_INITIALIZER(u"HWStub"); /* this is stringid #0: languages supported */ static const struct usb_string_descriptor lang_descriptor = -{ - 4, - USB_DT_STRING, - {0x0409} /* LANGID US English */ -}; +USB_STRING_INITIALIZER(u"\x0409"); /* LANGID US English */ static struct hwstub_version_desc_t version_descriptor = { diff --git a/utils/hwstub/stub/usb_drv.h b/utils/hwstub/stub/usb_drv.h index 00f22d8e1a..ef032f52a3 100644 --- a/utils/hwstub/stub/usb_drv.h +++ b/utils/hwstub/stub/usb_drv.h @@ -43,5 +43,12 @@ void usb_drv_set_address(int address); int usb_drv_port_speed(void); void usb_drv_configure_endpoint(int ep_num, int type); +/* USB_STRING_INITIALIZER(u"Example String") */ +#define USB_STRING_INITIALIZER(S) { \ + sizeof(struct usb_string_descriptor) + sizeof(S) - sizeof(*S), \ + USB_DT_STRING, \ + S \ +} + #endif /* _USB_DRV_H */ -- cgit v1.2.3