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

Linux Hotplug: connection-callback implementation for libusb backend #645

Merged
merged 10 commits into from
Apr 6, 2024

Conversation

k1-801
Copy link

@k1-801 k1-801 commented Nov 20, 2023

No description provided.

@k1-801 k1-801 mentioned this pull request Nov 21, 2023
@mcuee mcuee added the libusb Related to libusb backend label Nov 21, 2023
@k1-801 k1-801 changed the title WIP: RFC: connection callback support for libusb backend RFC: connection callback support for libusb backend Nov 21, 2023
@k1-801 k1-801 changed the title RFC: connection callback support for libusb backend RFC: Linux Hotplug - connection callback support for libusb backend Nov 21, 2023
@Youw
Copy link
Member

Youw commented Nov 21, 2023

Before looking into this - lets finish with #646 - probably some comments are applicable here as well

@k1-801
Copy link
Author

k1-801 commented Nov 21, 2023

Agreed. Going to squash all the commits into one so it's a little less messy, then will try to check for similar macro check related issues.

@k1-801
Copy link
Author

k1-801 commented Nov 21, 2023

For now, my biggest concern is the match_libusb_to_info function. It is the only way I found to match the devices, yet it itself relies on some features that may be unavailable for a device that has already left.

@k1-801 k1-801 force-pushed the connection-callback-libusb branch 2 times, most recently from 25014be to b3937ab Compare November 22, 2023 05:19
@k1-801
Copy link
Author

k1-801 commented Nov 22, 2023

Attempted to adjust the code-style to the current style of the library, it only ended up messing some other locations where indentation is done with both tabs AND spaces :(

@k1-801
Copy link
Author

k1-801 commented Nov 23, 2023

Added some changes to list management during device removal.

@mcuee mcuee added the enhancement New feature or request label Nov 28, 2023
@mcuee
Copy link
Member

mcuee commented Dec 5, 2023

Ubuntu-cmake and Arch Linux github action build are still failing.

1 similar comment
@mcuee
Copy link
Member

mcuee commented Dec 5, 2023

Ubuntu-cmake and Arch Linux github action build are still failing.

@k1-801
Copy link
Author

k1-801 commented Dec 5, 2023

Ubuntu-cmake and Arch Linux github action build are still failing.

Interesting. I will see to it right away.
UPD: It is just some warnings for unused parameters, treated as errors. Easy to fix.

@k1-801
Copy link
Author

k1-801 commented Dec 5, 2023

Ubuntu-cmake and Arch Linux github action build are still failing.

Addressed the issue, should be fine now

@mcuee
Copy link
Member

mcuee commented Mar 6, 2024

Not so sure how to re-run the github actions as there were build error previously.
However, previous Windows CMake error has nothing to do with this PR. So it should still be okay.

@mcuee
Copy link
Member

mcuee commented Mar 6, 2024

I will test this under Linux later.

@mcuee
Copy link
Member

mcuee commented Mar 8, 2024

This may be a bit tricky to test under Linux as we need to detach the kernel HID driver in order to use hidapi-libusb.

BTW, libusb currently only supports hotplug under Linux and macOS. There is a PR for Windows.

FreeBSD has its own libusb implementation and it supports hotplug. I will also test this PR under FreeBSD.
https://man.freebsd.org/cgi/man.cgi?query=libusb

@k1-801 k1-801 force-pushed the connection-callback-libusb branch from 16326c2 to 46f6784 Compare March 11, 2024 14:29
@k1-801
Copy link
Author

k1-801 commented Mar 11, 2024

I just rebased this onto the updated connection-callback branch. There were quite a few significant conflicts, there might even be compilation errors, will ensure it builds later today.

@k1-801 k1-801 marked this pull request as draft March 11, 2024 14:30
@mcuee
Copy link
Member

mcuee commented Mar 12, 2024

@k1-801
It does not seem to work under Ubuntu Linux 20.04.
No issues with PR #647 with the same system (Acer Laptop, dual boot Windows 11 and Ubuntu 20.04).

  1. Unplug and plug of Logitech USB Receiver

I have tried to detached kernel usbhid driver (same results if I do not detach the kernel driver).
https://lwn.net/Articles/143397/

root@UbuntuSwift3:/home/mcuee# tree /sys/bus/usb/drivers/usbhid/
/sys/bus/usb/drivers/usbhid/
├── 3-1:1.0 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0
├── 3-1:1.1 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.1
├── 3-2.1:1.3 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-2/3-2.1/3-2.1:1.3
├── 3-3:1.0 -> ../../../../devices/pci0000:00/0000:00:14.0/usb3/3-3/3-3:1.0
├── bind
├── module -> ../../../../module/usbhid
├── new_id
├── remove_id
├── uevent
└── unbind

root@UbuntuSwift3:/home/mcuee# echo -n "3-1:1.0" > /sys/bus/usb/drivers/usbhid/unbind
root@UbuntuSwift3:/home/mcuee# echo -n "3-1:1.1" > /sys/bus/usb/drivers/usbhid/unbind
root@UbuntuSwift3:/home/mcuee# lsusb -t
/:  Bus 001.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/1p, 480M
/:  Bus 002.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/4p, 10000M
    |__ Port 001: Dev 002, If 0, Class=Hub, Driver=hub/4p, 5000M
/:  Bus 003.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/12p, 480M
    |__ Port 001: Dev 017, If 0, Class=Human Interface Device, Driver=[none], 12M
    |__ Port 001: Dev 017, If 1, Class=Human Interface Device, Driver=[none], 12M
    |__ Port 002: Dev 003, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 001: Dev 018, If 0, Class=Audio, Driver=snd-usb-audio, 12M
        |__ Port 001: Dev 018, If 1, Class=Audio, Driver=snd-usb-audio, 12M
        |__ Port 001: Dev 018, If 2, Class=Audio, Driver=snd-usb-audio, 12M
        |__ Port 001: Dev 018, If 3, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 003: Dev 004, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 005: Dev 005, If 0, Class=Video, Driver=uvcvideo, 480M
    |__ Port 005: Dev 005, If 1, Class=Video, Driver=uvcvideo, 480M
    |__ Port 007: Dev 006, If 0, Class=Vendor Specific Class, Driver=[none], 480M
    |__ Port 010: Dev 007, 12M
/:  Bus 004.Port 001: Dev 001, Class=root_hub, Driver=xhci_hcd/4p, 10000M

hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 1ea7 0064
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: (null)
  Product:      2.4G Mouse
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: (105 bytes)
0x06, 0xb5, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x85, 0xb5, 0x09, 
0x02, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x07, 
0x81, 0x02, 0x09, 0x02, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 
0x08, 0x95, 0x07, 0x91, 0x02, 0xc0, 0x05, 0x01, 0x09, 0x02, 
0xa1, 0x01, 0x85, 0x02, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 
0x19, 0x01, 0x29, 0x08, 0x15, 0x00, 0x25, 0x01, 0x95, 0x08, 
0x75, 0x01, 0x81, 0x02, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 
0x16, 0x01, 0xf8, 0x26, 0xff, 0x07, 0x75, 0x0c, 0x95, 0x02, 
0x81, 0x06, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 
0x95, 0x01, 0x81, 0x06, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x95, 
0x01, 0x81, 0x06, 0xc0, 0xc0, 
Device Found
  type: 047f c056
  path: 3-2.1:1.3
  serial_number: D1CEC32927974D5F9BD6B2AEBF2EA8E3
  Manufacturer: Plantronics
  Product:      Plantronics Blackwire 3220 Series
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: (373 bytes)
0x05, 0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x01, 0x15, 0x00, 
0x25, 0x01, 0x09, 0xe9, 0x09, 0xea, 0x75, 0x01, 0x95, 0x02, 
0x81, 0x06, 0x95, 0x06, 0x81, 0x01, 0x85, 0x02, 0x05, 0x0c, 
0x09, 0x00, 0x95, 0x10, 0x81, 0x02, 0x85, 0x04, 0x09, 0x00, 
0x75, 0x08, 0x95, 0x24, 0x91, 0x02, 0x85, 0x05, 0x09, 0x00, 
0x95, 0x20, 0x81, 0x02, 0x85, 0x06, 0x09, 0x00, 0x95, 0x24, 
0x91, 0x02, 0x85, 0x07, 0x09, 0x00, 0x95, 0x20, 0x81, 0x02, 
0xc0, 0x05, 0x0b, 0x09, 0x05, 0xa1, 0x01, 0x85, 0x08, 0x15, 
0x00, 0x25, 0x01, 0x09, 0x2f, 0x75, 0x01, 0x95, 0x01, 0x81, 
0x06, 0x09, 0x20, 0x09, 0x21, 0x75, 0x01, 0x95, 0x02, 0x81, 
0x22, 0x95, 0x05, 0x81, 0x01, 0x05, 0x08, 0x85, 0x09, 0x09, 
0x09, 0x95, 0x01, 0x91, 0x22, 0x95, 0x07, 0x91, 0x01, 0x85, 
0x17, 0x09, 0x17, 0x95, 0x01, 0x91, 0x22, 0x95, 0x07, 0x91, 
0x01, 0x85, 0x18, 0x09, 0x18, 0x95, 0x01, 0x91, 0x22, 0x95, 
0x07, 0x91, 0x01, 0x85, 0x1e, 0x09, 0x1e, 0x95, 0x01, 0x91, 
0x22, 0x95, 0x07, 0x91, 0x01, 0x85, 0x20, 0x09, 0x20, 0x95, 
0x01, 0x91, 0x22, 0x95, 0x07, 0x91, 0x01, 0x85, 0x2a, 0x09, 
0x2a, 0x95, 0x01, 0x91, 0x22, 0x95, 0x07, 0x91, 0x01, 0xc0, 
0x06, 0xa0, 0xff, 0x09, 0x03, 0xa1, 0x01, 0x85, 0x03, 0x09, 
0x30, 0x75, 0x08, 0x95, 0x20, 0x91, 0x02, 0x85, 0x03, 0x09, 
0x30, 0x75, 0x08, 0x95, 0x20, 0x81, 0x02, 0x85, 0x14, 0x09, 
0xb1, 0x09, 0xb2, 0x09, 0xb5, 0x09, 0xb7, 0x09, 0xb3, 0x15, 
0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x05, 0x81, 0x06, 0x95, 
0x03, 0x81, 0x01, 0x85, 0x15, 0x09, 0x8c, 0x15, 0x00, 0x27, 
0xff, 0xff, 0x00, 0x00, 0x75, 0x10, 0x95, 0x01, 0x81, 0x22, 
0x85, 0x19, 0x09, 0x8d, 0x09, 0x8f, 0x09, 0x9e, 0x09, 0xdc, 
0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x04, 0x91, 0x22, 
0x09, 0xd2, 0x09, 0xd9, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 
0x95, 0x02, 0x91, 0x06, 0x95, 0x02, 0x91, 0x01, 0x85, 0x1a, 
0x09, 0xb5, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x01, 
0x91, 0x22, 0x95, 0x07, 0x91, 0x01, 0x85, 0x1b, 0x09, 0xcf, 
0x09, 0xb5, 0x75, 0x01, 0x95, 0x02, 0xb1, 0x22, 0x09, 0xde, 
0x75, 0x01, 0x95, 0x01, 0xb1, 0x23, 0x09, 0xd8, 0x95, 0x01, 
0xb1, 0x22, 0x95, 0x04, 0xb1, 0x01, 0x09, 0x09, 0x09, 0x17, 
0x09, 0x18, 0x09, 0x1e, 0x09, 0x20, 0x09, 0x2a, 0x75, 0x01, 
0x95, 0x06, 0xb1, 0x22, 0x95, 0x02, 0xb1, 0x01, 0x85, 0x1f, 
0x09, 0x9c, 0x75, 0x01, 0x95, 0x01, 0x81, 0x06, 0x95, 0x07, 
0x81, 0x01, 0xc0, 
Device Found
  type: 046d c534
  path: 3-1:1.0
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      2901
  Interface:    0
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: (59 bytes)
0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 
0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 
0x81, 0x02, 0x81, 0x03, 0x95, 0x05, 0x05, 0x08, 0x19, 0x01, 
0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 
0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x26, 0xa4, 0x00, 0x05, 
0x07, 0x19, 0x00, 0x2a, 0xa4, 0x00, 0x81, 0x00, 0xc0, 
Device Found
  type: 046d c534
  path: 3-1:1.1
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      2901
  Interface:    1
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: (177 bytes)
0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x02, 0x09, 0x01, 
0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x10, 0x15, 0x00, 
0x25, 0x01, 0x95, 0x10, 0x75, 0x01, 0x81, 0x02, 0x05, 0x01, 
0x16, 0x01, 0xf8, 0x26, 0xff, 0x07, 0x75, 0x0c, 0x95, 0x02, 
0x09, 0x30, 0x09, 0x31, 0x81, 0x06, 0x15, 0x81, 0x25, 0x7f, 
0x75, 0x08, 0x95, 0x01, 0x09, 0x38, 0x81, 0x06, 0x05, 0x0c, 
0x0a, 0x38, 0x02, 0x95, 0x01, 0x81, 0x06, 0xc0, 0xc0, 0x05, 
0x0c, 0x09, 0x01, 0xa1, 0x01, 0x85, 0x03, 0x75, 0x10, 0x95, 
0x02, 0x15, 0x01, 0x26, 0x8c, 0x02, 0x19, 0x01, 0x2a, 0x8c, 
0x02, 0x81, 0x00, 0xc0, 0x05, 0x01, 0x09, 0x80, 0xa1, 0x01, 
0x85, 0x04, 0x75, 0x02, 0x95, 0x01, 0x15, 0x01, 0x25, 0x03, 
0x09, 0x82, 0x09, 0x81, 0x09, 0x83, 0x81, 0x60, 0x75, 0x06, 
0x81, 0x03, 0xc0, 0x06, 0x00, 0xff, 0x09, 0x01, 0xa1, 0x01, 
0x85, 0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 0x00, 0x26, 0xff, 
0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 0x91, 0x00, 0xc0, 
0x06, 0x00, 0xff, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x11, 0x75, 
0x08, 0x95, 0x13, 0x15, 0x00, 0x26, 0xff, 0x00, 0x09, 0x02, 
0x81, 0x00, 0x09, 0x02, 0x91, 0x00, 0xc0, 
Handle 1: New device is connected: 3-3:1.0.
type: 1ea7 0064
  serial_number: (null)
  Manufacturer: (null)
  Product:      2.4G Mouse
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2.1:1.3.
type: 047f c056
  serial_number: D1CEC32927974D5F9BD6B2AEBF2EA8E3
  Manufacturer: Plantronics
  Product:      Plantronics Blackwire 3220 Series
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.0.
type: 046d c534
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      2901
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.1.
type: 046d c534
  serial_number: (null)
  Manufacturer: Logitech
  Product:      USB Receiver
  Release:      2901
  Interface:    1
  Usage (page): 0x0 (0x0)

@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

It does not seem to work under Ubuntu Linux 20.04

Okay, thank you for the info, I'll see what I can do. Keeping as a draft until I have tested it myself.

@mcuee
Copy link
Member

mcuee commented Mar 12, 2024

It does not seem to work under Ubuntu Linux 20.04

Okay, thank you for the info, I'll see what I can do. Keeping as a draft until I have tested it myself.

Thanks.

FYI: libusb hotplug works fine.

mcuee@UbuntuSwift3 ~/build/libusb/libusb/build (master)$ sudo ./examples/hotplugtest 
Device detached: 046d:c534
Device attached: 046d:c534
Device detached: 046d:c534
Device attached: 046d:c534
^C

@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

FYI: libusb hotplug works fine.

It took me some time to track the issue, but I found it. Turns out that, despite the documentation foe libusb NEVER explicitly stating so, the hotplug system actually does NOT start it's own background thread and requires a call to any variation libusb_handle_events this is actually described in the documentation, but the explanation is not super clear.

In turn, calling libusb_handle_events in a seprate thread, according to the documentation, may or may not cause issues with the read thread and it's own call to libusb_handle_events. I don't really understand how multiple read_thread's for multiple devices don't interfere with one another.
The way I see it, the best solution here would be to have a separate libusb context dedicated to the hotplug events. Will test and report my findings in 2 hours.

@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

I managed to get the events going (there is some debug output in the printout below that will not be present in the real tests), BUT, the device info is always empty. So far, it's definitely an impovement.

I don't know why the fields are empty or how to fix them, get_usb_string keeps returning NULL. But, as long as we have the events firing, as long as the paths are not empty and as long as we still get VID/PID pairs, I assume the patch does it's job.

Maybe it is related to the fact that I used a second libusb context, but the documentation states they should not interfere with one another, and the processing code never mentions the context at all.

UPD: every call to libusb_get_string_descriptor returns error -6, which is LIBUSB_ERROR_BUSY, and that's why no strings are returned.

UPD2: libusb/libusb#408 - it is impossible to retrieve any strings while in the libusb's hotplug callback. Will think of a workaround, but this is really becoming complicated. I can imagine it running two threads, one for libusb running it's event processing, in which the events would be put on a queue. The other would be actually processing the queue and calling the callbacks registered in hidapi.

Handle 1: New device is connected: 3-3:1.0. type: 04d9 fc38 serial_number: (null) Manufacturer: (null) Product: USB Gaming Mouse Release: 110 Interface: 0 Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-3:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-3:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

libusb callback fired! Event: out
Handle 1: Device was disconnected: 3-3:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-3:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-3:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

libusb callback fired! Event: in
Handle 1: New device is connected: 3-3:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-3:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-3:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

libusb callback fired! Event: out
Handle 1: Device was disconnected: 3-3:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-3:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-3:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

libusb callback fired! Event: in
Handle 1: New device is connected: 3-2:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: (null)
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

@k1-801 k1-801 marked this pull request as ready for review March 12, 2024 17:33
@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

Implemented a two-thread queue processing. Will push in a moment, needs some indentation cleanup.

Handle 1: New device is connected: 3-2:1.0. type: 04d9 fc38 serial_number: (null) Manufacturer: (null) Product: USB Gaming Mouse Release: 110 Interface: 0 Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-2:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-2:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-2:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.0.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 0
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.1.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 1
Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2:1.2.
type: 04d9 fc38
serial_number: (null)
Manufacturer: (null)
Product: USB Gaming Mouse
Release: 110
Interface: 2
Usage (page): 0x0 (0x0)

@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

@mcuee this monstrocity is now more or less ready to be tested, reviewed and insulted.

@k1-801
Copy link
Author

k1-801 commented Mar 12, 2024

Forgot a call to trigger the pthread_condition...
Does not affect the funcction, but the event is delayed for up to 5ms. Going to fix later. done.

@mcuee
Copy link
Member

mcuee commented Mar 12, 2024

Now this works fine. Thanks. And yes it is not requird to detach kernel driver for event.

Tested under Ubuntu Linux 20.04.

Unplug and plug of Platronics USB headset
Unplug and plug of a USB mouse receiver
Unplug and plug of another USB mouse receiver

mcuee@UbuntuSwift3 ~/build/hid/hidapi_hotplug_libusb (connection-callback-libusb)$ ./hidtest/hidtest-libusb 
hidapi test/example tool. Compiled with hidapi version 0.14.0, runtime version 0.14.0.
Compile-time version matches runtime version of hidapi.

Device Found
  type: 1ea7 0064
  path: 3-3:1.0
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: Unable to open device by path
Device Found
  type: 047f c056
  path: 3-2.1:1.3
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: Unable to open device by path
Device Found
  type: 1ea7 0066
  path: 3-1:1.0
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: Unable to open device by path
Device Found
  type: 1ea7 0066
  path: 3-1:1.1
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    1
  Usage (page): 0x0 (0x0)
  Bus type: 1 (USB)

  Report Descriptor: Unable to open device by path
Handle 1: New device is connected: 3-3:1.0.
type: 1ea7 0064
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2.1:1.3.
type: 047f c056
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.0.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.1.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    1
  Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-2.1:1.3.
type: 047f c056
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-2.1:1.3.
type: 047f c056
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      210
  Interface:    3
  Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-1:1.0.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-1:1.1.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    1
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.0.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-1:1.1.
type: 1ea7 0066
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    1
  Usage (page): 0x0 (0x0)

Handle 1: Device was disconnected: 3-3:1.0.
type: 1ea7 0064
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

Handle 1: New device is connected: 3-3:1.0.
type: 1ea7 0064
  serial_number: (null)
  Manufacturer: (null)
  Product:      (null)
  Release:      200
  Interface:    0
  Usage (page): 0x0 (0x0)

^C

@k1-801 k1-801 changed the title RFC: Linux Hotplug - connection callback support for libusb backend [Awaiting review] Linux Hotplug - connection callback support for libusb backend Mar 13, 2024
@k1-801 k1-801 changed the title [Awaiting review] Linux Hotplug - connection callback support for libusb backend Linux Hotplug - connection callback support for libusb backend Mar 13, 2024
@k1-801 k1-801 changed the title Linux Hotplug - connection callback support for libusb backend Linux Hotplug: connection-callback implementation for libusb backend Mar 13, 2024
libusb/hid.c Outdated Show resolved Hide resolved
libusb/hid.c Outdated Show resolved Hide resolved
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.

I've fixed some formatting concerns of mine, but still don't have enough time to go thru all of the code.
But I trust the community to review it properly and fix if anything needs to be fixed.
That said, I will not block the PR from getting into a feature branch.

@Youw Youw merged commit 60b40d9 into libusb:connection-callback Apr 6, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request libusb Related to libusb backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants