@@ -1287,13 +1287,17 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
12871287 removing_hubs |= TU_BIT (dev_id - CFG_TUH_DEVICE_MAX );
12881288 } else {
12891289 // Invoke callback before closing driver (maybe call it later ?)
1290- if (tuh_umount_cb ) tuh_umount_cb (daddr );
1290+ if (tuh_umount_cb ) {
1291+ tuh_umount_cb (daddr );
1292+ }
12911293 }
12921294
12931295 // Close class driver
12941296 for (uint8_t drv_id = 0 ; drv_id < TOTAL_DRIVER_COUNT ; drv_id ++ ) {
12951297 usbh_class_driver_t const * driver = get_driver (drv_id );
1296- if (driver ) driver -> close (daddr );
1298+ if (driver ) {
1299+ driver -> close (daddr );
1300+ }
12971301 }
12981302
12991303 hcd_device_close (rhport , daddr );
@@ -1350,8 +1354,11 @@ enum {
13501354 ENUM_HUB_GET_STATUS_2 ,
13511355 ENUM_HUB_CLEAR_RESET_2 ,
13521356 ENUM_SET_ADDR ,
1353-
13541357 ENUM_GET_DEVICE_DESC ,
1358+ ENUM_GET_STRING_LANGUAGE_ID ,
1359+ ENUM_GET_STRING_MANUFACTURER ,
1360+ ENUM_GET_STRING_PRODUCT ,
1361+ ENUM_GET_STRING_SERIAL ,
13551362 ENUM_GET_9BYTE_CONFIG_DESC ,
13561363 ENUM_GET_FULL_CONFIG_DESC ,
13571364 ENUM_SET_CONFIG ,
@@ -1364,25 +1371,25 @@ static void enum_full_complete(void);
13641371
13651372// process device enumeration
13661373static void process_enumeration (tuh_xfer_t * xfer ) {
1367- // Retry a few times with transfers in enumeration since device can be unstable when starting up
1368- enum {
1369- ATTEMPT_COUNT_MAX = 3 ,
1370- ATTEMPT_DELAY_MS = 100
1371- };
1374+ // Retry a few times while enumerating since device can be unstable when starting up
13721375 static uint8_t failed_count = 0 ;
1376+ if (XFER_RESULT_FAILED == xfer -> result ) {
1377+ enum {
1378+ ATTEMPT_COUNT_MAX = 3 ,
1379+ ATTEMPT_DELAY_MS = 100
1380+ };
13731381
1374- if (XFER_RESULT_SUCCESS != xfer -> result ) {
13751382 // retry if not reaching max attempt
1383+ failed_count ++ ;
13761384 bool retry = _dev0 .enumerating && (failed_count < ATTEMPT_COUNT_MAX );
1377- if ( retry ) {
1378- failed_count ++ ;
1385+ if (retry ) {
13791386 tusb_time_delay_ms_api (ATTEMPT_DELAY_MS ); // delay a bit
1380- TU_LOG1 ("Enumeration attempt %u\r\n" , failed_count );
1387+ TU_LOG1 ("Enumeration attempt %u/%u \r\n" , failed_count + 1 , ATTEMPT_COUNT_MAX );
13811388 retry = tuh_control_xfer (xfer );
13821389 }
13831390
13841391 if (!retry ) {
1385- enum_full_complete ();
1392+ enum_full_complete (); // complete as failed
13861393 }
13871394
13881395 return ;
@@ -1391,6 +1398,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
13911398
13921399 uint8_t const daddr = xfer -> daddr ;
13931400 uintptr_t const state = xfer -> user_data ;
1401+ usbh_device_t * dev = get_device (daddr );
1402+ uint16_t langid = 0x0409 ; // default is English
13941403
13951404 switch (state ) {
13961405 #if CFG_TUH_HUB
@@ -1481,7 +1490,6 @@ static void process_enumeration(tuh_xfer_t* xfer) {
14811490 tusb_time_delay_ms_api (2 );
14821491
14831492 const uint8_t new_addr = (uint8_t ) tu_le16toh (xfer -> setup -> wValue );
1484-
14851493 usbh_device_t * new_dev = get_device (new_addr );
14861494 TU_ASSERT (new_dev ,);
14871495 new_dev -> addressed = 1 ;
@@ -1495,21 +1503,69 @@ static void process_enumeration(tuh_xfer_t* xfer) {
14951503 // Get full device descriptor
14961504 TU_LOG_USBH ("Get Device Descriptor\r\n" );
14971505 TU_ASSERT (tuh_descriptor_get_device (new_addr , _usbh_epbuf .ctrl , sizeof (tusb_desc_device_t ),
1498- process_enumeration , ENUM_GET_9BYTE_CONFIG_DESC ),);
1506+ process_enumeration , ENUM_GET_STRING_LANGUAGE_ID ),);
14991507 break ;
15001508 }
15011509
1502- case ENUM_GET_9BYTE_CONFIG_DESC : {
1503- tusb_desc_device_t const * desc_device = (tusb_desc_device_t const * ) _usbh_epbuf .ctrl ;
1504- usbh_device_t * dev = get_device (daddr );
1510+ case ENUM_GET_STRING_LANGUAGE_ID : {
1511+ // save the received device descriptor
15051512 TU_ASSERT (dev ,);
1506-
1513+ tusb_desc_device_t const * desc_device = ( tusb_desc_device_t const * ) _usbh_epbuf . ctrl ;
15071514 dev -> vid = desc_device -> idVendor ;
15081515 dev -> pid = desc_device -> idProduct ;
15091516 dev -> i_manufacturer = desc_device -> iManufacturer ;
15101517 dev -> i_product = desc_device -> iProduct ;
15111518 dev -> i_serial = desc_device -> iSerialNumber ;
15121519
1520+ tuh_descriptor_get_string_langid (daddr , _usbh_epbuf .ctrl , CFG_TUH_ENUMERATION_BUFSIZE ,
1521+ process_enumeration , ENUM_GET_STRING_MANUFACTURER );
1522+ break ;
1523+ }
1524+
1525+ case ENUM_GET_STRING_MANUFACTURER : {
1526+ TU_ASSERT (dev ,);
1527+ const tusb_desc_string_t * desc_langid = (tusb_desc_string_t const * ) _usbh_epbuf .ctrl ;
1528+ if (desc_langid -> bLength >= 4 ) {
1529+ langid = tu_le16toh (desc_langid -> utf16le [0 ]);
1530+ }
1531+ if (dev -> i_manufacturer != 0 ) {
1532+ tuh_descriptor_get_string (daddr , dev -> i_manufacturer , langid , _usbh_epbuf .ctrl , CFG_TUH_ENUMERATION_BUFSIZE ,
1533+ process_enumeration , ENUM_GET_STRING_PRODUCT );
1534+ break ;
1535+ } else {
1536+ TU_ATTR_FALLTHROUGH ;
1537+ }
1538+ }
1539+
1540+ case ENUM_GET_STRING_PRODUCT : {
1541+ TU_ASSERT (dev ,);
1542+ if (state == ENUM_GET_STRING_PRODUCT ) {
1543+ langid = tu_le16toh (xfer -> setup -> wIndex ); // if not fall through, get langid from previous setup packet
1544+ }
1545+ if (dev -> i_product != 0 ) {
1546+ tuh_descriptor_get_string (daddr , dev -> i_product , 0x0409 , _usbh_epbuf .ctrl , CFG_TUH_ENUMERATION_BUFSIZE ,
1547+ process_enumeration , ENUM_GET_STRING_SERIAL );
1548+ break ;
1549+ } else {
1550+ TU_ATTR_FALLTHROUGH ;
1551+ }
1552+ }
1553+
1554+ case ENUM_GET_STRING_SERIAL : {
1555+ TU_ASSERT (dev ,);
1556+ if (state == ENUM_GET_STRING_SERIAL ) {
1557+ langid = tu_le16toh (xfer -> setup -> wIndex ); // if not fall through, get langid from previous setup packet
1558+ }
1559+ if (dev -> i_serial != 0 ) {
1560+ tuh_descriptor_get_string (daddr , dev -> i_serial , langid , _usbh_epbuf .ctrl , CFG_TUH_ENUMERATION_BUFSIZE ,
1561+ process_enumeration , ENUM_GET_9BYTE_CONFIG_DESC );
1562+ break ;
1563+ } else {
1564+ TU_ATTR_FALLTHROUGH ;
1565+ }
1566+ }
1567+
1568+ case ENUM_GET_9BYTE_CONFIG_DESC : {
15131569 // Get 9-byte for total length
15141570 uint8_t const config_idx = CONFIG_NUM - 1 ;
15151571 TU_LOG_USBH ("Get Configuration[0] Descriptor (9 bytes)\r\n" );
@@ -1542,7 +1598,6 @@ static void process_enumeration(tuh_xfer_t* xfer) {
15421598
15431599 case ENUM_CONFIG_DRIVER : {
15441600 TU_LOG_USBH ("Device configured\r\n" );
1545- usbh_device_t * dev = get_device (daddr );
15461601 TU_ASSERT (dev ,);
15471602
15481603 dev -> configured = 1 ;
0 commit comments