-
Notifications
You must be signed in to change notification settings - Fork 426
libusb backend for Windows and macOS #319
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
Comments
Reference discussion for macOS: libusb/libusb#911 |
Quick hack for mac/hid.c
|
Quick hack for Makefile.macOS
|
Runlog for macOS with hidtest using the quick mod libusb backend.
|
Under Windows, here is the output, with a quick mod of the following file. I choose to use usbdk backend so that I do not need to replace the HID driver with WinUSB. And it may be able to find more devices like USB keyboard and mouse if they are present. modified files and Windows 64bit binaries are included in the following zip file.
|
Of course, it is also possible not to use usbdk, just comment out the following line in hid.c.
|
Instead of using "hacks" to make it functional I'd rather suggest to refactor libusb backend of HIDAPI and switch to C11 instead of using POSIX/pthread API. Thoughts? |
Yes, that will be great. Haha, my hack is just a quick demo to show that it is possible. |
In order for your USB HID device to be found by the libusb backend (hidapi_libusb2.zip), there are two possibilities. Take note this is only applicable to device listed under Windows Device Manager as "Human Interface Devices" --> "USB Input Device". Keyboard/mouse and Bluetooth/I2C/SPI HID devices are not supported.
https://github.com/libusb/libusb/wiki/Windows#known-restrictions
|
hidapi_libusb.zip uses the USBDK backend of the libusb, so it may be able to find more devices than hidapi_libusb2.zip. For example, it may be able to find some USB mice/keyboards. But it requires the installation of usbdk. libusb usbdk backend may be less stable than using WinUSB backend. |
64bit Windows binary with hidapitester here (not using usbdk). libusb-1.0.dll is a mod version of 1.0.24.11650 with pull request libusb/libusb#986 to fix the Windows HID backend issue. |
Since I do not know much about pthread and C++11 threading, and since ChatGPT is now popular, I asked ChatGPT to do the job. Unfortuantely ChatGPT is not able to finish the full conversion and always got stuck. (Edit: delete the code as it is not good). |
Loool :) |
Glad to hear that. Interestingly I ask ChatGPT to convert the simple libusb https://github.com/libusb/libusb/blob/master/tests/stress_mt.c However, if I ask it to convert into C11, the result codes do not even compile since MinGW does not support C11 |
That's a bummer :( Writing an entire backend in C++ has many proc and cons. One of the most important ones - dependency on C++ Standard library. But maybe, having an experimental (additional, not replacement) C++ port of libusb backend of HIDAPI - should be a "fun project" at least. |
FYI: Apple clang does not support C11 threading either. Ref: |
I think in the end, this is not an important work (not much real use in reality) and therefore I will close it for now. |
Re-open as a low priority item. |
From #706, comment by mandar1jn.
|
I'll add some context to this: I'm working with skylanders portals which are HID devices, but they recieve commands over control transfers, something you wouldn't normally do for an HID device, but what can you do about it. My current code uses hidapi with a workaround to get the device handle from hidapi and manually runs the required win32 functions, but I'd prefer to use libusb (I can still extract the libusb handle from what I saw) and then use that functionality to send control transfers, and maybe utilize the hotplug functionality |
Right now there is no official support of libusb backend for Windows or macOS, but there steps has been made towards that direction with the help of the community. |
Quick hack for hidapitester to use libusb backend under Windows.
|
PR #728 almost works under Windows but there is one extra byte written and read.
|
So you've replaced a standard HID driver with WinUSB one to perform this test? |
No, the above test was using libusb-1.0 Windows HID backend.
If I switch to WinUSB driver, it does not work, matching the testing result I had with PR #728 under Linux and FreeBSD.
|
But it will work if I test with the default fx2hid FW (two bytes input/output report loopback), if I switch the driver to WinUSB. So I will say the current libusb codes kind of work under Windows even though it will suffer the same issue as #274. You can see that libusb mod works in this case while unmodifed hidapitester will not work since it only works with Windows HID driver. Of course my mod is a quick hack -- needs proper changes to the code, auto-tools script and CMake files. And it may need further work to remove the dependanices to libiconv as well. I will also need to test under macOS.
BTW, I am not so sure where I introduce the dependancy to libwinpthread-1.dll. It would be good to remove the dependancy as well. The default Windows version (using native Windows HID API) does not have those dependancies.
My mod of hidapitester Makefiles and the binaries (MSYS2 uscrt64 build). |
Same if I use my mod of Jan Axelson's USB PIC Generic HID example, mentioned here. Both hidapi git and PR #728 work fine, basically with report IDs for input/output reports and feature reports. Using WinUSB driver and not native Windows HID driver in the following tests.
Switch back to Windows HID driver and use hidapi git. This is just to prove the FW is working.
|
As discussed in #61 , it may make sense to create a new libusb backend to remove some limitations of the native HID APIs under Windows. The users will need to use WinUSB driver to replace the native HID driver in order to make use of this (if using libusb HID backend, then the limitation still applies).
I am not so sure if there is any benefit to extend this to macOS, but it can be done as well with the latest kernel driver detaching function of libusb under macOS.
The text was updated successfully, but these errors were encountered: