Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hid_get_device_info #432

Merged
merged 28 commits into from
Aug 13, 2022
Merged

Conversation

Julusian
Copy link
Contributor

@Julusian Julusian commented Jun 20, 2022

Resolves: #431
Closes: #164
Closes: #163

I don't like the signature of get_device_info_for_device in linux, and Im unsure about fill_device_info_for_device in libusb.

The mac, linux and libusb implementations appear to work fine with hidtest.
I havent tested the other backends yet

@Julusian Julusian changed the title wip: add api Add hid_get_device_info Jun 20, 2022
@Youw
Copy link
Member

Youw commented Jun 20, 2022

lets have it against a feature branch for now: https://github.com/libusb/hidapi/tree/hid_get_device_info

@Julusian
Copy link
Contributor Author

This could replace #164, as it will be possible to get the path from the device_info struct.
I can see there you were wondering about lazily initialising the path. Would you want that to be done here? It doesnt look like it would be too hard to do for most backends. It looks like the only extra data some backends may need is the path. Nothing too bad to persist until the device_info is generated

@Julusian Julusian force-pushed the feat/hid_get_device_info branch from 9b09429 to 94aea4f Compare June 20, 2022 22:46
@Youw
Copy link
Member

Youw commented Jun 20, 2022

Would you want that to be done here?

Yes, lazy initialization of the device info should be beneficial, specially for those who never use it.

@Julusian
Copy link
Contributor Author

Would you want that to be done here?

Yes, lazy initialization of the device info should be beneficial, specially for those who never use it.

The calls to get the serial number and other strings are now all copying off the device_info, so I would guess it to be rare that it won't get loaded, but I have no problem making it lazy and caching it

hidapi/hidapi.h Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
@Youw
Copy link
Member

Youw commented Jun 21, 2022

Rebase to master, when you have a chance - got some conflicting changes in there.

Julusian and others added 4 commits June 23, 2022 19:32
Co-authored-by: Ihor Dutchak <ihor.youw@gmail.com>
Co-authored-by: Ihor Dutchak <ihor.youw@gmail.com>
@Julusian Julusian force-pushed the feat/hid_get_device_info branch from 53a1c91 to 5497de1 Compare June 23, 2022 18:35
@mcuee mcuee added the enhancement New feature or request label Jul 5, 2022
@mcuee
Copy link
Member

mcuee commented Jul 16, 2022

Mac Mini M1 (macOS 12.4) output. The output seems to be fine.


mcuee@mcuees-Mac-mini hidapi_pr432 % ./build_darwin/hidtest/hidtest       
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 0000 0000
  path: DevSrvsID:4294969138
  serial_number: 
  Manufacturer: Apple
  Product:      
  Release:      0
  Interface:    -1
  Usage (page): 0xff (0xff00)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969659
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x2 (0x1)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969659
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x1 (0x1)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969659
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x1 (0xc)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969659
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x80 (0x1)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969659
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    3
  Usage (page): 0x0 (0xff00)

Device Found
  type: 1915 1025
  path: DevSrvsID:4294969661
  serial_number: 
  Manufacturer: ZY.Ltd
  Product:      ZY Control Mic
  Release:      173
  Interface:    2
  Usage (page): 0x6 (0x1)

Device Found
  type: 045e 082f
  path: DevSrvsID:4294969867
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x2 (0x1)

Device Found
  type: 045e 082f
  path: DevSrvsID:4294969867
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x1 (0x1)

Device Found
  type: 045e 082f
  path: DevSrvsID:4294969867
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x6 (0x1)

Device Found
  type: 045e 082f
  path: DevSrvsID:4294969867
  serial_number: 734762612322
  Manufacturer: Microsoft
  Product:      Microsoft Bluetooth Mouse
  Release:      212
  Interface:    -1
  Usage (page): 0x212 (0xff07)

Device Found
  type: 046d b33d
  path: DevSrvsID:4294969883
  serial_number: f4-73-35-4b-c5-aa
  Manufacturer: Unknown
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x6 (0x1)

Device Found
  type: 046d b33d
  path: DevSrvsID:4294969883
  serial_number: f4-73-35-4b-c5-aa
  Manufacturer: Unknown
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x1 (0xc)

Device Found
  type: 046d b33d
  path: DevSrvsID:4294969883
  serial_number: f4-73-35-4b-c5-aa
  Manufacturer: Unknown
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x80 (0x1)

Device Found
  type: 046d b33d
  path: DevSrvsID:4294969883
  serial_number: f4-73-35-4b-c5-aa
  Manufacturer: Unknown
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x1 (0xff00)

Device Found
  type: 046d b33d
  path: DevSrvsID:4294969883
  serial_number: f4-73-35-4b-c5-aa
  Manufacturer: Unknown
  Product:      Keyboard K480
  Release:      2803
  Interface:    -1
  Usage (page): 0x2 (0xff00)

Device Found
  type: 0000 0000
  path: DevSrvsID:4294968515
  serial_number: 
  Manufacturer: Apple
  Product:      Headset
  Release:      0
  Interface:    -1
  Usage (page): 0x1 (0xc)

Device Found
  type: 0000 0000
  path: DevSrvsID:4294968878
  serial_number: 
  Manufacturer: APPL
  Product:      BTM
  Release:      0
  Interface:    -1
  Usage (page): 0x48 (0xff00)

unable to open device

@mcuee
Copy link
Member

mcuee commented Jul 16, 2022

Windows MSYS2 mingw64 output. Seems to be fine.

MINGW64 /c/work/libusb/hidapi_pr432
$ ./build_mingw64/hidtest/hidtest.exe
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 8087 0a1e
  path: \\?\HID#INTC816#3&d2322f2&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer:
  Product:
  Release:      200
  Interface:    -1
  Usage (page): 0xd (0x1)

Device Found
  type: 06cb cd40
  path: \\?\HID#SYNA7DAB&Col01#5&2f64dfea&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      501
  Interface:    -1
  Usage (page): 0x2 (0x1)

Device Found
  type: 06cb cd40
  path: \\?\HID#SYNA7DAB&Col02#5&2f64dfea&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      501
  Interface:    -1
  Usage (page): 0x5 (0xd)

Device Found
  type: 06cb cd40
  path: \\?\HID#SYNA7DAB&Col03#5&2f64dfea&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      501
  Interface:    -1
  Usage (page): 0xe (0xd)

Device Found
  type: 06cb cd40
  path: \\?\HID#SYNA7DAB&Col04#5&2f64dfea&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number: 9999
  Manufacturer: Microsoft
  Product:      HIDI2C Device
  Release:      501
  Interface:    -1
  Usage (page): 0x1 (0xff00)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_01&Col01#7&1119bfb4&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x2 (0x1)

Device Found
  type: 045e 0000
  path: \\?\HID#ConvertedDevice&Col02#5&32cf90e6&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x1 (0xc)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_01&Col02#7&1119bfb4&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x1 (0xc)

Device Found
  type: 045e 0000
  path: \\?\HID#ConvertedDevice&Col03#5&32cf90e6&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x80 (0x1)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_01&Col03#7&1119bfb4&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x80 (0x1)

Device Found
  type: deed feed
  path: \\?\HID#10251229#3&9d5d338&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Acer Inc
  Product:      Acer Airplane Mode Controller
  Release:      101
  Interface:    -1
  Usage (page): 0xc (0x1)

Device Found
  type: 045e 0000
  path: \\?\HID#ConvertedDevice&Col01#5&32cf90e6&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD
  serial_number:
  Manufacturer:
  Product:
  Release:      0
  Interface:    -1
  Usage (page): 0x6 (0x1)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_01&Col04#7&1119bfb4&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x88 (0xffbc)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_02&Col01#7&12bd7e0e&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x1 (0xff00)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_02&Col02#7&12bd7e0e&0&0001#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x2 (0xff00)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_02&Col03#7&12bd7e0e&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x4 (0xff00)

Device Found
  type: 046d c52b
  path: \\?\HID#VID_046D&PID_C52B&MI_00#7&34f0fd76&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}\KBD
  serial_number:
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x6 (0x1)

unable to open device

@Youw
Copy link
Member

Youw commented Jul 16, 2022

@Julusian do you consider this as still a draft?

@Julusian
Copy link
Contributor Author

Sorry, I got distracted on other things
Yeah I am happy with this. I havent made it lazy initialize yet though

@mcuee you haven't really tested my changes much there. You have for the refactoring, but the unable to open device at the end means that it didnt open a device and use the new method

@Julusian Julusian marked this pull request as ready for review July 17, 2022 11:23
@mcuee
Copy link
Member

mcuee commented Jul 17, 2022

Sorry, I got distracted on other things Yeah I am happy with this. I havent made it lazy initialize yet though

@mcuee you haven't really tested my changes much there. You have for the refactoring, but the unable to open device at the end means that it didnt open a device and use the new method

I see. Unfortunately the one used in hidtest is not a common device but rather a modified Microchip generic HID FW example from Alan Ott. So maybe I need to use other codes to test this pull request.

Will hidapitester be good enought?

@Julusian
Copy link
Contributor Author

I see. Unfortunately the one used in hidtest is not a common device but rather a modified Microchip generic HID FW example from Alan Ott. So maybe I need to use other programms to test this pull request.

I have been changing the ids to be for a device I have, then stopping the application when it starts on the read/write tests

@mcuee
Copy link
Member

mcuee commented Jul 17, 2022

I see. Unfortunately the one used in hidtest is not a common device but rather a modified Microchip generic HID FW example from Alan Ott. So maybe I need to use other programms to test this pull request.

I have been changing the ids to be for a device I have, then stopping the application when it starts on the read/write tests

This is the output from my Microchip Custom Demo (original Microchip FW). It is probably good enough for this testing.

...
PS C:\work\libusb\hidapi_pr432> .\build_mingw64\hidtest\hidtest.exe
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.
....
...
...
Device Found
  type: 04d8 003f
  path: \\?\HID#VID_04D8&PID_003F#7&aebbb81&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    -1
  Usage (page): 0x1 (0xff00)

Manufacturer String: Microchip Technology Inc.
Product String: Simple HID Device Demo
Serial Number String: (0)
Device Found
  type: 04d8 003f
  path: \\?\HID#VID_04D8&PID_003F#7&aebbb81&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
  serial_number:
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    -1
  Usage (page): 0x1 (0xff00)

Indexed String 1: Microchip Technology Inc.
Unable to send a feature report.
Unable to get a feature report.
Get Input/Feature Report DeviceIoControl: (0x00000001) Incorrect function.Data read:
   81 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Press any key to continue . . .

@Youw
Copy link
Member

Youw commented Aug 7, 2022

Another libusb update: sorted out the "get usage_page/usage". It has to be handled differently during the enumeration and when the device is fully opened.

@Youw
Copy link
Member

Youw commented Aug 7, 2022

Now I'm happy with libusb implementation.

Copy link
Member

@Youw Youw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated hidraw implementation too.

Dependency on libudev has been a major blocker for me, from using hidraw backend on Android, for a while. Today I made a tiny step to using sysfs directly instead of libudev (hid_enumerate/filter over VID/PID).

Second major improvement - hid_get_*_string functions: wcsncpy crashes of the source string is NULL. Add explicit check.


If anyone can additionally test libusb and hidraw backends - I'd highly appreciate it.

@Youw
Copy link
Member

Youw commented Aug 13, 2022

Since no one complains about my latest changes - either everything works as expecter or no one had a chance to look :)

@mcuee
Copy link
Member

mcuee commented Aug 13, 2022

@Youw
The hidraw backend seems to have some issues with my Microchip Custom Demo device.

No issue with the libusb backend under Ubuntu 20.04.

mcuee@UbuntuSwift3:~/build/hidapi_pr432/build_linux$ ./hidtest/hidtest_libusb
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 046d c52b
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x0 (0x0)

Device Found
  type: 046d c52b
  path: 3-3:1.1
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x0 (0x0)

Device Found
  type: 046d c52b
  path: 3-3:1.2
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x0 (0x0)

Device Found
  type: 04d8 003f
  path: 3-2.3:1.0
  serial_number: (null)
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x0 (0x0)

Device Found
  type: 16c0 05dc
  path: 3-1:1.1
  serial_number: 0001
  Manufacturer: www.fischl.de
  Product:      USBasp
  Release:      110
  Interface:    1
  Usage (page): 0x0 (0x0)

Manufacturer String: Microchip Technology Inc.
Product String: Simple HID Device Demo
Serial Number String: (1033) 
Device Found
  type: 04d8 003f
  path: 3-2.3:1.0
  serial_number: (null)
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x1 (0xff00)

Indexed String 1: Microchip Technology Inc.
Unable to send a feature report.
Unable to get a feature report.
hid_error is not implemented yetwaiting...
waiting...
waiting...
waiting...
waiting...
^C

But there is no way to open the device using the hidraw backend. It can not even find the device after the above. Maybe it does not work with hidraw driver.

mcuee@UbuntuSwift3:~/build/hidapi_pr432/build_linux$ ./hidtest/hidtest_hidraw
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 16c0 05dc
  path: /dev/hidraw1
  serial_number: 0001
  Manufacturer: www.fischl.de
  Product:      USBasp
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xff00)
...

unable to open device


@mcuee
Copy link
Member

mcuee commented Aug 13, 2022

@Youw
Sorry but the above is a false alarm. The libusb backend will detach the hidraw driver so that hidraw backend will not be able to find the device. There will be no issue running the hidtest-hidraw first.

mcuee@UbuntuSwift3:~/build/hidapi_pr432/build_linux$ sudo ./hidtest/hidtest_hidraw
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 16c0 05dc
  path: /dev/hidraw1
  serial_number: 0001
  Manufacturer: www.fischl.de
  Product:      USBasp
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xff00)

Device Found
  type: 04d8 003f
  path: /dev/hidraw4
  serial_number: 
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x1 (0xff00)
...

Manufacturer String: Microchip Technology Inc.
Product String: Simple HID Device Demo
Serial Number String: (0) 
Device Found
  type: 04d8 003f
  path: /dev/hidraw4
  serial_number: 
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x1 (0xff00)

Unable to read indexed string 1
Indexed String 1: 
Unable to send a feature report.
Unable to get a feature report.
ioctl (GFEATURE): Broken pipewaiting...
waiting...
waiting...
waiting...
waiting...
^C

@mcuee
Copy link
Member

mcuee commented Aug 13, 2022

Results of the Logitech USB Receiver under Linux.

mcuee@UbuntuSwift3:~/build/hidapi_pr432/build_linux$ sudo ./hidtest/hidtest_hidraw
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 16c0 05dc
  path: /dev/hidraw1
  serial_number: 0001
  Manufacturer: www.fischl.de
  Product:      USBasp
  Release:      110
  Interface:    1
  Usage (page): 0x1 (0xff00)

Device Found
  type: 04d8 003f
  path: /dev/hidraw4
  serial_number: 
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x1 (0xff00)
...

Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x1 (0xff00)

Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x2 (0xff00)

Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x4 (0xff00)

...

Manufacturer String: Logitech
Product String: USB Receiver
Serial Number String: (0) 
Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x1 (0xff00)

Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x2 (0xff00)

Device Found
  type: 046d c52b
  path: /dev/hidraw2
  serial_number: 
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x4 (0xff00)

Unable to read indexed string 1
Indexed String 1: 
Unable to send a feature report.
Unable to get a feature report.
ioctl (GFEATURE): Broken pipeUnable to write()
Error: Broken pipe
Unable to write() (2)
waiting...
waiting...
waiting...
waiting...
^C

mcuee@UbuntuSwift3:~/build/hidapi_pr432/build_linux$ ./hidtest/hidtest_libusb
hidapi test/example tool. Compiled with hidapi version 0.12.0, runtime version 0.12.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 046d c52b
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x0 (0x0)

Device Found
  type: 046d c52b
  path: 3-3:1.1
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    1
  Usage (page): 0x0 (0x0)

Device Found
  type: 046d c52b
  path: 3-3:1.2
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    2
  Usage (page): 0x0 (0x0)

Device Found
  type: 04d8 003f
  path: 3-2.3:1.0
  serial_number: (null)
  Manufacturer: Microchip Technology Inc.
  Product:      Simple HID Device Demo
  Release:      2
  Interface:    0
  Usage (page): 0x0 (0x0)

Device Found
  type: 16c0 05dc
  path: 3-1:1.1
  serial_number: 0001
  Manufacturer: www.fischl.de
  Product:      USBasp
  Release:      110
  Interface:    1
  Usage (page): 0x0 (0x0)

Manufacturer String: Logitech
Product String: USB Receiver
Serial Number String: (1033) 
Device Found
  type: 046d c52b
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x6 (0x1)

Indexed String 1: Logitech
Unable to send a feature report.
Unable to get a feature report.
hid_error is not implemented yetUnable to write()
Error: hid_error is not implemented yet
Unable to write() (2)
waiting...
waiting...
waiting...
waiting...
^C



@Youw
Copy link
Member

Youw commented Aug 13, 2022

The libusb backend will detach the hidraw driver so that hidraw backend will not be able to find the device.

That is actually unexpected. LIBUSB backed should have re-attach the kernel (hidraw) driver back, once the device is closed.
Let me check.

@Youw
Copy link
Member

Youw commented Aug 13, 2022

Let me check.

As far as I can tell - the driver was reattached (otherwise you wouldn't be able to enumerate it with hidraw after libusb).
Maybe there is adifferent issue - likely outside of this PR's scope.

@Youw
Copy link
Member

Youw commented Aug 13, 2022

There is an interesting side-effect/improvement in libusb backed:
Before this change, the only way to get (primary) usage_page/usage was to compile hidapi with INVASIVE_GET_USAGE.
Now - it is possible to to get it with hid_get_device_info with default compilation flags.
I.e. (default compilation flags):
hid_enumerate:

  type: 046d c52b
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x0 (0x0)

hid_get_device_info:

  type: 046d c52b
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      1203
  Interface:    0
  Usage (page): 0x6 (0x1)

But that works only if device supports GET LIBUSB_DT_REPORT - which is not always true. I have several devices that do not have this usb request implemented, even though HID Report Descriptor is still present among all USB descriptors.
UPD: see #438.

@mcuee
Copy link
Member

mcuee commented Aug 13, 2022

Let me check.

As far as I can tell - the driver was reattached (otherwise you wouldn't be able to enumerate it with hidraw after libusb). Maybe there is adifferent issue - likely outside of this PR's scope.

It can not enumerate with the hidraw after libusb. Therefore I think the re-attach does not work in this case, probably due to the use of CTRL-C to terminate the hidtest application. So I do not think this is a real issue.

@Youw
Copy link
Member

Youw commented Aug 13, 2022

use of CTRL-C to terminate the hidtest application

Ah, right.

I do not think this is a real issue

Agree.

@mcuee
Copy link
Member

mcuee commented Aug 15, 2022

There is an interesting side-effect/improvement in libusb backed: Before this change, the only way to get (primary) usage_page/usage was to compile hidapi with INVASIVE_GET_USAGE. Now - it is possible to to get it with hid_get_device_info with default compilation flags. I.e. (default compilation flags): hid_enumerate:

But that works only if device supports GET LIBUSB_DT_REPORT - which is not always true. I have several devices that do not have this usb request implemented, even though HID Report Descriptor is still present among all USB descriptors.

Thanks for the tip. I will try this on the Teensy HID device.

jonasmalacofilho added a commit to jonasmalacofilho/hidapi that referenced this pull request Jan 7, 2023
Commit 5c9f147 (libusb#432) replaced a call to strdup with an explicit
memcpy to a buffer on the stack.

However, it incorrectly used the buffer size, instead of the clamped
uevent length, as the argument to memcpy, resulting in reads past the
end of uevent:

$ valgrind -q hidtest/hidtest_hidraw 1>/dev/null
==51900== Invalid read of size 8
==51900==    at 0x48571D5: parse_uevent_info (hid.c:496)
==51900==    by 0x48574F2: create_device_info_for_device (hid.c:578)
==51900==    by 0x4857ED7: hid_enumerate (hid.c:876)
==51900==    by 0x1094CE: main (test.c:105)
==51900==  Address 0x4b6c1a0 is 7 bytes after a block of size 185 alloc'd
==51900==    at 0x4846CC3: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==51900==    by 0x4AA0E45: UnknownInlinedFun (fileio.c:534)
==51900==    by 0x4AA0E45: read_virtual_file_at.constprop.0 (fileio.c:572)
==51900==    by 0x4A9D37C: UnknownInlinedFun (fileio.h:74)
==51900==    by 0x4A9D37C: UnknownInlinedFun (fileio.h:77)
==51900==    by 0x4A9D37C: sd_device_get_sysattr_value (sd-device.c:2318)
==51900==    by 0x4A8B0D1: udev_device_get_sysattr_value (libudev-device.c:747)
==51900==    by 0x48574BE: create_device_info_for_device (hid.c:578)
==51900==    by 0x4857ED7: hid_enumerate (hid.c:876)
==51900==    by 0x1094CE: main (test.c:105)
==51900==

Fix this by using uevent_len as the argument to memcpy.

Calling strndupa was considered but abandoned, as it is not standard.

Fixes: 5c9f147 (libusb#432)
Fixes: 4779d63
Youw pushed a commit that referenced this pull request Jan 9, 2023
…497)

Commit 5c9f147 (#432) replaced a call to strdup with an explicit
memcpy to a buffer on the stack.

However, it incorrectly used the buffer size, instead of the clamped
uevent length, as the argument to memcpy, resulting in reads past the
end of uevent:

Fix this by using uevent_len as the argument to memcpy.

Calling strndupa was considered but abandoned, as it is not standard.

Fixes: 5c9f147 (#432)
Fixes: 4779d63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get vendorId & productId from hid_device or path Mapping from hid_device to hid_device_info
3 participants