Skip to content

Commit ffb50af

Browse files
authored
hidraw: Add support for HID over I2C and uhid devices (#166)
- uhid USB, Bluetooth (standard) and I2C now enumerating * USB uhid support was added by removing the udev USB endpoint check; - HID over I2C was excluded before (likely because it didn't exist when hidraw was developed);
1 parent d9471d4 commit ffb50af

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

linux/hid.c

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -506,27 +506,8 @@ static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t
506506
&serial_number_utf8,
507507
&product_name_utf8);
508508

509-
if (bus_type == BUS_BLUETOOTH) {
510-
switch (key) {
511-
case DEVICE_STRING_MANUFACTURER:
512-
wcsncpy(string, L"", maxlen);
513-
ret = 0;
514-
break;
515-
case DEVICE_STRING_PRODUCT:
516-
retm = mbstowcs(string, product_name_utf8, maxlen);
517-
ret = (retm == (size_t)-1)? -1: 0;
518-
break;
519-
case DEVICE_STRING_SERIAL:
520-
retm = mbstowcs(string, serial_number_utf8, maxlen);
521-
ret = (retm == (size_t)-1)? -1: 0;
522-
break;
523-
case DEVICE_STRING_COUNT:
524-
default:
525-
ret = -1;
526-
break;
527-
}
528-
}
529-
else {
509+
/* Standard USB device */
510+
if (bus_type == BUS_USB) {
530511
/* This is a USB device. Find its parent USB Device node. */
531512
parent = udev_device_get_parent_with_subsystem_devtype(
532513
udev_dev,
@@ -548,10 +529,40 @@ static int get_device_string(hid_device *dev, enum device_string_id key, wchar_t
548529
/* Convert the string from UTF-8 to wchar_t */
549530
retm = mbstowcs(string, str, maxlen);
550531
ret = (retm == (size_t)-1)? -1: 0;
551-
goto end;
552532
}
533+
534+
/* USB information parsed */
535+
goto end;
536+
}
537+
else {
538+
/* Correctly handled below */
553539
}
554540
}
541+
542+
/* USB information not available (uhid) or another type of HID bus */
543+
switch (bus_type) {
544+
case BUS_BLUETOOTH:
545+
case BUS_I2C:
546+
case BUS_USB:
547+
switch (key) {
548+
case DEVICE_STRING_MANUFACTURER:
549+
wcsncpy(string, L"", maxlen);
550+
ret = 0;
551+
break;
552+
case DEVICE_STRING_PRODUCT:
553+
retm = mbstowcs(string, product_name_utf8, maxlen);
554+
ret = (retm == (size_t)-1)? -1: 0;
555+
break;
556+
case DEVICE_STRING_SERIAL:
557+
retm = mbstowcs(string, serial_number_utf8, maxlen);
558+
ret = (retm == (size_t)-1)? -1: 0;
559+
break;
560+
case DEVICE_STRING_COUNT:
561+
default:
562+
ret = -1;
563+
break;
564+
}
565+
}
555566
}
556567
}
557568

@@ -669,9 +680,15 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
669680
goto next;
670681
}
671682

672-
if (bus_type != BUS_USB && bus_type != BUS_BLUETOOTH) {
673-
/* We only know how to handle USB and BT devices. */
674-
goto next;
683+
/* Filter out unhandled devices right away */
684+
switch (bus_type) {
685+
case BUS_BLUETOOTH:
686+
case BUS_I2C:
687+
case BUS_USB:
688+
break;
689+
690+
default:
691+
goto next;
675692
}
676693

677694
/* Check the VID/PID against the arguments */
@@ -720,22 +737,14 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
720737
"usb",
721738
"usb_device");
722739

740+
/* uhid USB devices
741+
Since this is a virtual hid interface, no USB information will
742+
be available. */
723743
if (!usb_dev) {
724-
/* Free this device */
725-
free(cur_dev->serial_number);
726-
free(cur_dev->path);
727-
free(cur_dev);
728-
729-
/* Take it off the device list. */
730-
if (prev_dev) {
731-
prev_dev->next = NULL;
732-
cur_dev = prev_dev;
733-
}
734-
else {
735-
cur_dev = root = NULL;
736-
}
737-
738-
goto next;
744+
/* Manufacturer and Product strings */
745+
cur_dev->manufacturer_string = wcsdup(L"");
746+
cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);
747+
break;
739748
}
740749

741750
/* Manufacturer and Product strings */
@@ -759,6 +768,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
759768
break;
760769

761770
case BUS_BLUETOOTH:
771+
case BUS_I2C:
762772
/* Manufacturer and Product strings */
763773
cur_dev->manufacturer_string = wcsdup(L"");
764774
cur_dev->product_string = utf8_to_wchar_t(product_name_utf8);

0 commit comments

Comments
 (0)