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

Windows: libusb Windows hotplug support #86

Open
mcuee opened this issue Aug 10, 2015 · 66 comments
Open

Windows: libusb Windows hotplug support #86

mcuee opened this issue Aug 10, 2015 · 66 comments
Labels

Comments

@mcuee
Copy link
Member

mcuee commented Aug 10, 2015

This is one of the long standing missing feature for Windows.

@mcuee mcuee added the enhancement New features label Aug 17, 2015
@mcuee
Copy link
Member Author

mcuee commented Nov 27, 2015

Last known tree back in the libusbx time.
https://github.com/dickens/libusbx-hp/tree/windows-hotplug-3

@mcuee mcuee changed the title libusb Windows hotplug support Windows: libusb Windows hotplug support Feb 25, 2016
@changyp6
Copy link

changyp6 commented Mar 2, 2016

libusb hotplug for Windows is really important.
I have a program running on Linux/Window/MacOS, but Windows doesn't have hotplug support, I have to write my own device detection code for Windows.
But Windows only reports that there is a device arrived or there's a device removed, you have to find which one is added or removed.
So when I receive device remove event, I usb libusb_get_device_list() to get current device present in the system, and compare this list with the old one to find out which one is removed, and I use the same method to find which one is added, after I find the difference, my program will handle the removed/added device.

But if I unplug the device and plug it back to the same USB port very quickly, and Windows's driver searching program is disabled(to make system recognize device faster), my program can get device remove and device add event correctly, but libusb_get_device_list() will get a usb device list the same as the previous saved one.

Here is the reason:
When my program receives device remove event, and start to list all the USB devices, because the unplug/plug action is so fast, libusb_get_device_list() will give a list after the device is plugged back. So this USB device list is the same as the list before unplug/plug action, which means my program cannot find out which device is removed and which is added, there will be 2 missing actions for my program.

Because my program is event driven, this causes 2 events missing.

I really hope that hotplug feature can be implemented for Windows.

@mcuee
Copy link
Member Author

mcuee commented Mar 3, 2016 via email

@changyp6
Copy link

changyp6 commented Mar 3, 2016

I tried that before, I even tried to merge the change into libusb 1.0.19, but I failed to merge it to newer code. I still need to use latest branch, because there are tons of bug/leak fixes for Windows platform.

Hope Windows hotplug support can be implemented soon.

@dickens
Copy link
Member

dickens commented Mar 3, 2016

Will be part of the next release. Unfortunately it is a complicated set of
changes.

Regards,
Chris

@changyp6
Copy link

changyp6 commented Mar 3, 2016

This is a great news!

@hjelmn hjelmn added the windows label Mar 5, 2016
@mcuee mcuee added this to the 1.0.22 milestone Mar 6, 2016
@chrisba77
Copy link

chrisba77 commented Sep 2, 2016

were there any changes regarding the hotplug issue since then? Currently having the problem with LibUsbDotNet that communication with an unplugged-replugged device is only possible again after reinstalling the driver using the Inf-Wizard OR restarting Windows. This device is a CardReader. On the other hand, i dont have issues hotplugging a USB Keyboard. Does anyone have suggestions?

@diabolo38
Copy link

@chrisba77 strange w/o any "hotplug" support you should be able to re-use the device without driver re-install reboot etc
if:
-release any claimed interface
-close the device
-call exit on the libsusb context

then run again lib usb init , device detect ,re-open re claim etc ..

That is what i do to get control again of unplugged and re-pluged device
not real nice but the simplest i did find.

@chrisba77
Copy link

@diabolo38 I do all this. And actually after plugging in the device again, communication is possible. BUT the read values coming from the device are not valid anymore.
After reading in a Tag / Card the device sends some HID reports with Number-Keys including a Return Hit. After unplug / replug, these values seem to be random. Checked with Port Sniffer that I read the right values coming from the device. But sent Key Hits are not valid anymore and change every time i read in the same Tag. Reconnecting or Restarting my Test-SW doesnt help. Only thing that helps is rerunning Inf-Wizard or a reboot.

@diabolo38
Copy link

@chrisba77 if device is hid using standard os hid device driver then the issue could be os driver or the device itlsef. I can't see such issue with non hid device at least.

@tony--
Copy link

tony-- commented Jan 5, 2017

It looks like this didn't make it into 1.0.21 as planned.
Any plans to include this in 1.0.22?
Thanks in advance!

Edit: I see now that it is included in the 1.0.22 milestone. I'll also take a look at the mailing list archives to see if there is recent discussion. Any additional info would still be welcome!

@Jam-Nibi
Copy link

Hi guys, what's the status of this issue ?

@leileigong
Copy link

Did the hotplug supported on windows?

@jazzy-crane
Copy link

Has this now been moved to 1.0.23? :(

@changyp6
Copy link

changyp6 commented Jan 2, 2018

I just checked the latest code, it seems that windows hotplug hasn't been implemented.
Windows libusb hotplug definitely is my first 2018 new year wish.

@njk42
Copy link

njk42 commented Jan 5, 2018

+1 for Windows hotplug support.

Without hotplug support, what are the standard ways of determining when a device connects or disconnects on Windows? Does one just repeatedly poll a device list and determine when there are changes detected?

@ryxuli
Copy link

ryxuli commented Jan 11, 2018

It's really inconvenient that libusb doesn't support hotplug capability on Windows. I finely turn to libusbK. It can be downloaded from https://sourceforge.net/projects/libusbk/, after setting up, you can download the whole src zip in https://sourceforge.net/projects/libusbk/files/libusbK-release/3.0.7.0/libusbK-3.0.7.0-src.zip/download which includes an example hot-plug-monitor.c shows the hotplug ability.

@rjgc2015
Copy link

Hi all, did it support on Windows now?

@mcuee
Copy link
Member Author

mcuee commented Mar 20, 2018 via email

@mcuee
Copy link
Member Author

mcuee commented Jan 22, 2022

Unfortunately Chris seems to be busy and has not been active in libusb development for a while. So I have to remove this from libusb 1.0.26 milestones unless he is back or a new Windows developer can join the project and look at this feature.

@mcuee
Copy link
Member Author

mcuee commented Jan 25, 2022

Ref: #1047 (comment)
From Tormod:

For something like the Windows hotplug support we must be sure there are developers around that will stay available to timely fix any issues that arises after it lands. Anyway, until there is at least a draft of a pull request for it, it remains just a wishlist item with no relevance for our release milestones.

@douglasg14b
Copy link

douglasg14b commented Feb 8, 2022

Is this why Talon is working for me?

Activating Microphone: "Microphone (MH670)"
Falling back to polling for USB hotplug: failed to register hotplug callback

Searching for this lead me here.

Edit: Nevermind, false thingie.

@mcuee
Copy link
Member Author

mcuee commented Feb 8, 2022

Is this why Talon is working for me?

Activating Microphone: "Microphone (MH670)"
Falling back to polling for USB hotplug: failed to register hotplug callback

Searching for this lead me here.

Please provide more details. What is your question? Do you have some issues with regard to Talon? In that case, then you have to get support from them.
Ref: https://github.com/talonvoice/talon/issues

If Talon is using libusb, then they have to fall back to polling for USB hotplug under Windows, since libusb Windows does not support hotplug yet.

@Aii-Wei
Copy link

Aii-Wei commented May 31, 2022

Polling for USB hotplug under Windows, Will it affect the device communication? I tested and found that the device change can be detected according to idVendor, idProduct, port from libusb_get_port_number, address from libusb_get_device_address.

@mcuee
Copy link
Member Author

mcuee commented Jun 2, 2022

Polling for USB hotplug under Windows, Will it affect the device communication? I tested and found that the device change can be detected according to idVendor, idProduct, port from libusb_get_port_number, address from libusb_get_device_address.

Basically you have to deal with hotplug by yourself since hotplug is not supported by Windows. You need to keep polling.

@xhwang-China
Copy link

unfortunately! 2023 is coming to an end and this feature is still not supported

@mcuee
Copy link
Member Author

mcuee commented Sep 14, 2023

unfortunately! 2023 is coming to an end and this feature is still not supported

This feature will not be supported, unless we get an active Windows developer for libusb project who can actively working on this feature.

You want to volunteer? PRs are welcome.

@whitequark
Copy link

It is really unfortunate that the payoff from having hotplug (not needing to do like a loop or something) is absolutely not worth the person-weeks of work of someone highly competent to get it in upstream libusb. I know I cannot justify it for any of my projects, and at least one would decently benefit from having hotplug. Just not enough.

@mcuee
Copy link
Member Author

mcuee commented Sep 17, 2023

Contributors are more than welcome for libusb projects now that we are short of developers and reviewers which makes 1.0.27 release not happening yet.

As for this paticular issue (Windows Hotplug support), it may be even more difficult as we do not have an active Windows developer for quite some time (last commit done by Chris Dickens was on 9-Feb-2021).

@xhwang-China
Copy link

unfortunately! 2023 is coming to an end and this feature is still not supported

This feature will not be supported, unless we get an active Windows developer for libusb project who can actively working on this feature.

You want to volunteer? PRs are welcome.

My current solution is to listen for WM_DEVICECHANGE events, but this solution is not a good idea for cross-platform library because it requires binding Windows and you have to create a virtual window to do this.

Finally, I added a usb_event_cb api to the libusb library to be called by the upper layer to handle device hotplug,Device monitoring is implemented by the upper layer

This is an approach that hopefully will help other developers, although it's not a perfect solution.

@Elemecca
Copy link

My current solution is to listen for WM_DEVICECHANGE events, but this solution is not a good idea for cross-platform library because it requires binding Windows and you have to create a virtual window to do this.

You might look at CM_Register_Notification - it gives you roughly the same set of events as WM_DEVICECHANGE without needing a window handle. It requires Windows 8 or newer, but that covers everything that's still supported.

@sonatique
Copy link
Contributor

Please see #1406 . Comments welcome.

@fabiensanglard
Copy link

fabiensanglard commented Jan 29, 2024

@mcuee while #1406 is being reviewed, is there an official recommended way to poll on system without hotplug?

I tried the method mentioned by @Aii-Wei above.

I tested and found that the device change can be detected according to idVendor, idProduct, port from libusb_get_port_number, address from libusb_get_device_address.

I can confirm that using <vid, pid, port, address> tuplet works on Windows and Linux (the address is increased every time the device is connected).

Note that it does not work on Macos (both port and address are constant).

@mcuee
Copy link
Member Author

mcuee commented Feb 6, 2024

@fabiensanglard

For Windows, I think we should focus on reviewing and testing PR #1406 to get it merged.

As for the work-around, let's see if @tormodvolden has some recommendations for platforms without hotplug support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests