From 185bf28c21e3b5e01fb81251e236c2e89f4a5008 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Wed, 16 Dec 2020 17:22:30 +0100 Subject: [PATCH 1/2] usb: Add configuration options for USB peripheral serial This adds compile-time options to configure the serial of an USB peripheral. The serial be autogenerated with a configured number of bytes. It is also possible to configure a fixed serial string for a device and disable the autogeneration of the serial. --- sys/include/usb.h | 26 ++++++++++++++++++++++++++ sys/usb/Kconfig | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/sys/include/usb.h b/sys/include/usb.h index de838e51d3..fd2b8660fd 100644 --- a/sys/include/usb.h +++ b/sys/include/usb.h @@ -96,6 +96,32 @@ extern "C" { #define CONFIG_USB_CONFIGURATION_STR "USB config" #endif +/** + * @brief USB peripheral serial string + * + * Compile-time value to override the serial string with. An LUID-based hex + * string is generated when this value is not used. + * + * This string does not have to be a number, but it must be unique between + * devices with identical VID:PID combination. + */ +#ifdef DOXYGEN +#define CONFIG_USB_SERIAL_STR "RIOT-12345" +#endif + +/** + * @brief USB peripheral serial string length + * + * Maximum value is 63. Sensible values are between 8 to 32 depending on the + * number of expected devices. + * + * The length here is in bytes. The generated hex string is twice as many chars + * in length due to conversion from bytes to hex chars. + */ +#if !defined(CONFIG_USB_SERIAL_STR) && !defined(CONFIG_USB_SERIAL_BYTE_LENGTH) +#define CONFIG_USB_SERIAL_BYTE_LENGTH 8 +#endif + /** * @brief USB peripheral device version * diff --git a/sys/usb/Kconfig b/sys/usb/Kconfig index c7d35e8e27..05fb319a4a 100644 --- a/sys/usb/Kconfig +++ b/sys/usb/Kconfig @@ -81,6 +81,32 @@ config USB_DEFAULT_LANGID help The default value is EN-US (0x0409). +config USB_CUSTOM_SERIAL_STR + bool "Configure a custom USB serial string" + +config USB_SERIAL_STR + string "Serial string" + depends on USB_CUSTOM_SERIAL_STR + help + USB peripheral serial string + + Compile-time value to override the serial string with. An LUID-based hex + string is generated when this value is not used. + + This string does not have to be a number, but it must be unique between + devices with identical VID:PID combination. + +config USB_SERIAL_BYTE_LENGTH + int "USB peripheral serial string length" + depends on !USB_CUSTOM_SERIAL_STR + range 0 63 + default 8 + help + The length here is in bytes. The generated hex string is twice as many chars + in length due to conversion from bytes to hex chars. + +comment "WARNING: The serial string is empty!" + depends on USB_SERIAL_STR = "" && USB_CUSTOM_SERIAL_STR rsource "usbus/Kconfig" endif # KCONFIG_USB From 9cd753228c0c563025d206071fb7760deb9e6e4d Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Wed, 16 Dec 2020 17:24:40 +0100 Subject: [PATCH 2/2] usbus: Add USB peripheral serial string support --- sys/Makefile.dep | 2 ++ sys/include/usb/usbus.h | 7 +++++++ sys/usb/usbus/usbus.c | 14 ++++++++++++++ sys/usb/usbus/usbus_fmt.c | 1 + 4 files changed, 24 insertions(+) diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 66ca09d648..b4365f5fc1 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -950,6 +950,8 @@ ifneq (,$(filter usbus,$(USEMODULE))) DEFAULT_MODULE += auto_init_usbus USEMODULE += core_thread_flags USEMODULE += event + USEMODULE += luid + USEMODULE += fmt ifeq (,$(filter usbdev_mock,$(USEMODULE))) FEATURES_REQUIRED += periph_usbdev endif diff --git a/sys/include/usb/usbus.h b/sys/include/usb/usbus.h index b17bb3f4b6..9af1c5a644 100644 --- a/sys/include/usb/usbus.h +++ b/sys/include/usb/usbus.h @@ -399,6 +399,7 @@ struct usbus { usbus_string_t manuf; /**< Manufacturer string */ usbus_string_t product; /**< Product string */ usbus_string_t config; /**< Configuration string */ + usbus_string_t serial; /**< serial string */ usbus_endpoint_t ep_out[USBDEV_NUM_ENDPOINTS]; /**< USBUS OUT endpoints */ usbus_endpoint_t ep_in[USBDEV_NUM_ENDPOINTS]; /**< USBUS IN endpoints */ event_queue_t queue; /**< Event queue */ @@ -415,6 +416,12 @@ struct usbus { usbus_state_t state; /**< Current state */ usbus_state_t pstate; /**< state to recover to from suspend */ uint8_t addr; /**< Address of the USB peripheral */ +#ifndef CONFIG_USB_SERIAL_STR + /** + * @brief Hex representation of the device serial number + */ + char serial_str[2 * CONFIG_USB_SERIAL_BYTE_LENGTH + 1]; +#endif }; /** diff --git a/sys/usb/usbus/usbus.c b/sys/usb/usbus/usbus.c index 8304134815..6f3d0f4bee 100644 --- a/sys/usb/usbus/usbus.c +++ b/sys/usb/usbus/usbus.c @@ -21,6 +21,8 @@ #include "kernel_defines.h" #include "bitarithm.h" #include "event.h" +#include "fmt.h" +#include "luid.h" #include "thread.h" #include "thread_flags.h" #include "periph/usbdev.h" @@ -254,6 +256,18 @@ static void *_usbus_thread(void *args) usbus_add_string_descriptor(usbus, &usbus->product, CONFIG_USB_PRODUCT_STR); usbus_add_string_descriptor(usbus, &usbus->manuf, CONFIG_USB_MANUF_STR); +#ifdef CONFIG_USB_SERIAL_STR + usbus_add_string_descriptor(usbus, &usbus->serial, CONFIG_USB_SERIAL_STR); +#else + static_assert(CONFIG_USB_SERIAL_BYTE_LENGTH <= UINT8_MAX/4, + "USB serial byte length must be at most 63 due to protocol " + "limitations"); + uint8_t luid_buf[CONFIG_USB_SERIAL_BYTE_LENGTH]; + luid_get(luid_buf, sizeof(luid_buf)); + fmt_bytes_hex(usbus->serial_str, luid_buf, sizeof(luid_buf)); + usbus_add_string_descriptor(usbus, &usbus->serial, usbus->serial_str); +#endif + usbus->state = USBUS_STATE_DISCONNECT; /* Initialize handlers */ diff --git a/sys/usb/usbus/usbus_fmt.c b/sys/usb/usbus/usbus_fmt.c index 5584de185f..cb3f097fa2 100644 --- a/sys/usb/usbus/usbus_fmt.c +++ b/sys/usb/usbus/usbus_fmt.c @@ -288,6 +288,7 @@ size_t usbus_fmt_descriptor_dev(usbus_t *usbus) desc.product_id = CONFIG_USB_PID; desc.manufacturer_idx = usbus->manuf.idx; desc.product_idx = usbus->product.idx; + desc.serial_idx = usbus->serial.idx; /* USBUS supports only a single config at the moment */ desc.num_configurations = 1; usbus_control_slicer_put_bytes(usbus, (uint8_t *)&desc,