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

update hidapi, use hidraw on linux (instead of libusb) #43

Closed
wants to merge 5 commits into from

Conversation

holiman
Copy link
Collaborator

@holiman holiman commented Dec 20, 2023

This is work in progress.

  • Updates hidapi to latest
  • Uses linux-specific code for the linux-platform, instead of some generic mishmash. (requires libudev when building)

@holiman
Copy link
Collaborator Author

holiman commented Dec 20, 2023

With the latest code (in this PR), plus switching to linux-specifc code instead of generic, a Ledger Nano X is identified as follows:

[user@work usb]$ go run -a ./demo.go 
--------------------------------------------------------------------------------------------------------------------------------
HID #0
  OS Path:      /dev/hidraw0
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0xffa0
  Usage:        1
  Interface:    0
--------------------------------------------------------------------------------------------------------------------------------
HID #1
  OS Path:      /dev/hidraw1
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0xf1d0
  Usage:        1
  Interface:    1
================================================================================================================================
RAW #0
  OS Path:    2c97:4015:01
  Vendor ID:  0x2c97
  Product ID: 0x4015
  Interface:  2
--------------------------------------------------------------------------------------------------------------------------------

On master:

--------------------------------------------------------------------------------------------------------------------------------
HID #0
  OS Path:      0001:0008:00
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0
  Usage:        0
  Interface:    0
--------------------------------------------------------------------------------------------------------------------------------
HID #1
  OS Path:      0001:0008:01
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0
  Usage:        0
  Interface:    1
================================================================================================================================
RAW #0
  OS Path:    2c97:4015:01
  Vendor ID:  0x2c97
  Product ID: 0x4015
  Interface:  2
--------------------------------------------------------------------------------------------------------------------------------

@holiman holiman changed the title Hidupdate update hidapi, use linux-specific code on linux Dec 20, 2023
@holiman
Copy link
Collaborator Author

holiman commented Dec 20, 2023

Re "using linux-specific code on linux" -- it's not so simple. As stated in https://github.com/libusb/hidapi#hidapi-has-four-back-ends , what this PR does is switch from 3 to 2.

Is that preferrable? I am not 100% sure. Docs say "There are tradeoffs, and the functionality supported is slightly different. ", but doesn't go into more details. 🤷


HIDAPI has four back-ends:

  1. Windows (using hid.dll)
  2. Linux/hidraw (using the Kernel's hidraw driver)
  3. libusb (using libusb-1.0 - Linux/BSD/other UNIX-like systems)
  4. macOS (using IOHidManager)

On Linux, either the hidraw or the libusb back-end can be used. There are tradeoffs, and the functionality supported is slightly different. Both are built by default. It is up to the application linking to hidapi to choose the backend at link time by linking to either libhidapi-libusb or libhidapi-hidraw.

Note that you will need to install an udev rule file with your application for unprivileged users to be able to access HID devices with hidapi. Refer to the 69-hid.rules file in the udev directory for an example.

Linux/hidraw (linux/hid.c):

This back-end uses the hidraw interface in the Linux kernel, and supports both USB and Bluetooth HID devices. It requires kernel version at least 2.6.39 to build. In addition, it will only communicate with devices which have hidraw nodes associated with them. Keyboards, mice, and some other devices which are blacklisted from having hidraw nodes will not work. Fortunately, for nearly all the uses of hidraw, this is not a problem.

Linux/FreeBSD/libusb (libusb/hid.c):

This back-end uses libusb-1.0 to communicate directly to a USB device. This back-end will of course not work with Bluetooth devices.

@lightclient
Copy link

With this PR I'm seeing about the exact same output as you. Only difference appears to be how the OS Path is represented

$ go run -a ./demo.go
--------------------------------------------------------------------------------------------------------------------------------
HID #33
  OS Path:      DevSrvsID:4295599569
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0xffa0
  Usage:        1
  Interface:    0
--------------------------------------------------------------------------------------------------------------------------------
HID #34
  OS Path:      DevSrvsID:4295599571
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   0xf1d0
  Usage:        1
  Interface:    1
================================================================================================================================
RAW #0
  OS Path:    2c97:4015:02
  Vendor ID:  0x2c97
  Product ID: 0x4015
  Interface:  2
--------------------------------------------------------------------------------------------------------------------------------

master

$ go run -a ./demo.go
--------------------------------------------------------------------------------------------------------------------------------
HID #2
  OS Path:
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   65440
  Usage:        1
  Interface:    -1
--------------------------------------------------------------------------------------------------------------------------------
HID #13
  OS Path:
  Vendor ID:    0x2c97
  Product ID:   0x4015
  Release:      513
  Serial:       0001
  Manufacturer: Ledger
  Product:      Nano X
  Usage Page:   61904
  Usage:        1
  Interface:    -1
================================================================================================================================
RAW #0
  OS Path:    2c97:4015:02
  Vendor ID:  0x2c97
  Product ID: 0x4015
  Interface:  2
--------------------------------------------------------------------------------------------------------------------------------

@holiman holiman changed the title update hidapi, use linux-specific code on linux update hidapi, use hidraw on linux (instead of libusb) Dec 20, 2023
@holiman
Copy link
Collaborator Author

holiman commented Dec 20, 2023

I tested this in go-ethereum:

diff --git a/go.mod b/go.mod
index 8f99a00754..54bf9780b1 100644
--- a/go.mod
+++ b/go.mod
@@ -141,3 +141,4 @@ require (
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	rsc.io/tmplfunc v0.0.3 // indirect
 )
+replace github.com/karalabe/usb => /home/user/go/src/github.com/karalabe/usb

Ledger nano X discovery / derivation worked fine. It will match on the same device, 1, but it will match earlier, on the hub.UsageID(0xffa0) == device.UsagePage(0x0) clause.

@holiman
Copy link
Collaborator Author

holiman commented Dec 21, 2023

Note to self: need to add back the dummy.go files

@holiman
Copy link
Collaborator Author

holiman commented Feb 2, 2024

Closing this in favour of a pure-hid-update in either karalabe/hid#44 (and then use hid in go-ethereum), OR ethereum/go-ethereum#28901

@holiman holiman closed this Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants