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" );
137138static 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
141143static 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+
529555static 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 ;
0 commit comments