Skip to content

Commit

Permalink
dev: deduce product name from class if string descriptors are unavail…
Browse files Browse the repository at this point in the history
…able

JIRA: RTOS-937
  • Loading branch information
adamgreloch committed Jan 10, 2025
1 parent c291f77 commit 864d4f9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions libusb/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define CLASS_REQ_SET_CONTROL_LINE_STATE 0x22

/* class codes */
#define USB_CLASS_HID 0x3
#define USB_CLASS_MASS_STORAGE 0x8
#define USB_CLASS_HUB 0x9

Expand Down
65 changes: 61 additions & 4 deletions usb/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,61 @@ static int usb_getStringDesc(usb_dev_t *dev, char **buf, int index)
}


static void usb_guessProductString(usb_dev_t *dev)
{
switch (dev->desc.bDeviceClass) {
case USB_CLASS_HID:
dev->product = "USB HID";
break;
case USB_CLASS_HUB:
switch (dev->desc.bDeviceProtocol) {
case USB_HUB_PROTO_ROOT:
dev->product = "USB Root Hub";
break;
case USB_HUB_PROTO_SINGLE_TT:
dev->product = "USB Single TT Hub";
break;
default:
dev->product = "USB Hub";
break;
}
break;
case USB_CLASS_MASS_STORAGE:
dev->product = "USB Mass Storage";
break;
default:
dev->product = "Unknown USB Device";
break;
}
}


static void usb_guessManufacturerString(usb_dev_t *dev)
{
dev->manufacturer = "Generic";
}


static void usb_setSerialNumberString(usb_dev_t *dev)
{
dev->serialNumber = "Unknown";
}


static int usb_getAllStringDescs(usb_dev_t *dev)
{
usb_string_desc_t desc = { 0 };
int i;

/* Get an array of language ids */
/* String descriptors are optional. If a device omits all string descriptors,
* it must not return this array, so the following call is allowed to fail in
* that case */
if (usb_getDescriptor(dev, USB_DESC_STRING, 0, (char *)&desc, sizeof(desc)) < 0) {
USB_LOG("usb: Fail to get configuration descriptor\n");
return -1;
usb_guessManufacturerString(dev);
usb_guessProductString(dev);
usb_setSerialNumberString(dev);
return -ENOTSUP;
}

if (desc.bLength < 4)
Expand All @@ -392,16 +439,25 @@ static int usb_getAllStringDescs(usb_dev_t *dev)
if (usb_getStringDesc(dev, &dev->manufacturer, dev->desc.iManufacturer) != 0)
return -ENOMEM;
}
else {
usb_guessManufacturerString(dev);
}

if (dev->desc.iProduct != 0) {
if (usb_getStringDesc(dev, &dev->product, dev->desc.iProduct) != 0)
return -ENOMEM;
}
else {
usb_guessProductString(dev);
}

if (dev->desc.iSerialNumber != 0) {
if (usb_getStringDesc(dev, &dev->serialNumber, dev->desc.iSerialNumber) != 0)
return -ENOMEM;
}
else {
usb_setSerialNumberString(dev);
}

for (i = 0; i < dev->nifs; i++) {
if (dev->ifs[i].desc->iInterface == 0)
Expand Down Expand Up @@ -453,7 +509,8 @@ int usb_devEnumerate(usb_dev_t *dev)
return -1;
}

if (usb_getAllStringDescs(dev) < 0) {
ret = usb_getAllStringDescs(dev);
if (ret < 0 && ret != -ENOTSUP) {
USB_LOG("usb: Fail to get string descriptors\n");
return -1;
}
Expand All @@ -462,7 +519,7 @@ int usb_devEnumerate(usb_dev_t *dev)
usb_devSetChild(dev->hub, dev->port, dev);

USB_LOG("usb: New device addr: %d locationID: %08x %s, %s\n", dev->address, dev->locationID,
dev->manufacturer, dev->product);
dev->manufacturer, dev->product);

if (dev->desc.bDeviceClass == USB_CLASS_HUB) {
if (hub_conf(dev) != 0)
Expand Down

0 comments on commit 864d4f9

Please sign in to comment.