Skip to content

Commit

Permalink
hidraw: fallback to HID_NAME
Browse files Browse the repository at this point in the history
if we can't get the vendor/product string of the USB attributes, fallback to the HID_NAME string collected from the uevent.
  • Loading branch information
jo-bitsch committed Jan 10, 2024
1 parent f03f4c4 commit 760050d
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/hid_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ is_fido(const char *path)

static int
parse_uevent(const char *uevent, int *bus, int16_t *vendor_id,
int16_t *product_id)
int16_t *product_id, char **hid_name)
{
char *cp;
char *p;
Expand All @@ -86,6 +86,7 @@ parse_uevent(const char *uevent, int *bus, int16_t *vendor_id,
short unsigned int x;
short unsigned int y;
short unsigned int z;
char name[80];

if ((s = cp = strdup(uevent)) == NULL)
return (-1);
Expand All @@ -97,8 +98,14 @@ parse_uevent(const char *uevent, int *bus, int16_t *vendor_id,
*vendor_id = (int16_t)y;
*product_id = (int16_t)z;
ok = 0;
break;
}
continue;
}
if (strncmp(p, "HID_NAME=", 9) == 0 && hid_name != NULL) {
if (sscanf(p + 9, "%79[^\n]s", name) == 1) {
*hid_name = strdup(name);
}
continue;
}
}

Expand Down Expand Up @@ -137,6 +144,7 @@ copy_info(fido_dev_info_t *di, struct udev *udev,
char *uevent = NULL;
struct udev_device *dev = NULL;
int bus = 0;
char *hid_name = NULL;
int ok = -1;

memset(di, 0, sizeof(*di));
Expand All @@ -148,7 +156,7 @@ copy_info(fido_dev_info_t *di, struct udev *udev,
goto fail;

if ((uevent = get_parent_attr(dev, "hid", NULL, "uevent")) == NULL ||
parse_uevent(uevent, &bus, &di->vendor_id, &di->product_id) < 0) {
parse_uevent(uevent, &bus, &di->vendor_id, &di->product_id, &hid_name) < 0) {
fido_log_debug("%s: uevent", __func__);
goto fail;
}
Expand All @@ -165,6 +173,24 @@ copy_info(fido_dev_info_t *di, struct udev *udev,
di->manufacturer = strdup("");
if ((di->product = get_usb_attr(dev, "product")) == NULL)
di->product = strdup("");
if (strnlen(di->manufacturer, 1) == 0 && strnlen(di->product, 1) == 0) {
char* first_space = strchr(hid_name, ' ');
if (first_space == NULL) {
free(di->manufacturer);
di->manufacturer = strdup(hid_name);
} else {
size_t manufacturer_length = (size_t) (first_space - hid_name);

free(di->manufacturer);
di->manufacturer = malloc(manufacturer_length + 1);
strncpy(di->manufacturer, hid_name, manufacturer_length);
di->manufacturer[manufacturer_length] = '\0';

free(di->product);
di->product = strdup(first_space+1);

}
}
if (di->path == NULL || di->manufacturer == NULL || di->product == NULL)
goto fail;

Expand All @@ -174,6 +200,7 @@ copy_info(fido_dev_info_t *di, struct udev *udev,
udev_device_unref(dev);

free(uevent);
free(hid_name);

if (ok < 0) {
free(di->path);
Expand Down

0 comments on commit 760050d

Please sign in to comment.