Skip to content

Commit bdf54ba

Browse files
committed
usb_dwc3: Populate serial number with the actual serial number
This will be useful for CI and similar use cases, to distinguish different machines. Also fix spurious NUL-termination of the descriptor strings while I'm here. Signed-off-by: Hector Martin <[email protected]>
1 parent b57a917 commit bdf54ba

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/usb_dwc3.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../build/build_tag.h"
1111

1212
#include "usb_dwc3.h"
13+
#include "adt.h"
1314
#include "dart.h"
1415
#include "malloc.h"
1516
#include "memory.h"
@@ -136,7 +137,8 @@ static const struct usb_string_descriptor str_manufacturer =
136137
make_usb_string_descriptor("Asahi Linux");
137138
static const struct usb_string_descriptor str_product =
138139
make_usb_string_descriptor("m1n1 uartproxy " BUILD_TAG);
139-
static const struct usb_string_descriptor str_serial = make_usb_string_descriptor("P-0");
140+
static const struct usb_string_descriptor str_serial_dummy = make_usb_string_descriptor("P-0");
141+
static const struct usb_string_descriptor *str_serial;
140142

141143
static const struct usb_string_descriptor_languages str_langs = {
142144
.bLength = sizeof(str_langs) + 2,
@@ -526,6 +528,30 @@ static void usb_dwc3_ep_set_stall(dwc3_dev_t *dev, u8 ep, u8 stall)
526528
usb_dwc3_ep_command(dev, ep, DWC3_DEPCMD_CLEARSTALL, 0, 0, 0);
527529
}
528530

531+
static void usb_build_serial(void)
532+
{
533+
if (str_serial)
534+
return;
535+
536+
const char *serial = adt_getprop(adt, 0, "serial-number", NULL);
537+
if (!serial || !serial[0]) {
538+
str_serial = &str_serial_dummy;
539+
return;
540+
}
541+
542+
size_t len = strlen(serial);
543+
size_t size = sizeof(struct usb_string_descriptor) + 2 * len;
544+
545+
struct usb_string_descriptor *desc = malloc(size);
546+
memset(desc, 0, size);
547+
desc->bLength = size;
548+
desc->bDescriptorType = USB_STRING_DESCRIPTOR;
549+
for (size_t i = 0; i < len; i++)
550+
desc->bString[i] = serial[i];
551+
552+
str_serial = desc;
553+
}
554+
529555
static void usb_cdc_get_string_descriptor(u32 index, const void **descriptor, u16 *descriptor_len)
530556
{
531557
switch (index) {
@@ -542,8 +568,9 @@ static void usb_cdc_get_string_descriptor(u32 index, const void **descriptor, u1
542568
*descriptor_len = str_product.bLength;
543569
break;
544570
case STRING_DESCRIPTOR_SERIAL:
545-
*descriptor = &str_serial;
546-
*descriptor_len = str_serial.bLength;
571+
usb_build_serial();
572+
*descriptor = str_serial;
573+
*descriptor_len = str_serial->bLength;
547574
break;
548575
default:
549576
*descriptor = NULL;

src/usb_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ struct usb_device_qualifier_descriptor {
202202
*/
203203
#define make_usb_string_descriptor(str) \
204204
{ \
205-
.bLength = sizeof(struct usb_string_descriptor) + sizeof(u##str), \
205+
.bLength = sizeof(struct usb_string_descriptor) + sizeof(u##str) - 2, \
206206
.bDescriptorType = USB_STRING_DESCRIPTOR, .bString = u##str \
207207
}
208208

0 commit comments

Comments
 (0)