|
| 1 | +libusb-compat-0.1 examples |
| 2 | +========================== |
| 3 | + |
| 4 | +Small programs that exercise the libusb-0.1 compatibility shim. They are |
| 5 | +built automatically by `make` but are not installed. |
| 6 | + |
| 7 | +lsusb |
| 8 | +----- |
| 9 | +Prints `idVendor:idProduct` for every USB device. The smallest possible |
| 10 | +demonstration of the libusb-0.1 enumeration API. |
| 11 | + |
| 12 | +testlibusb [-v] |
| 13 | +--------------- |
| 14 | +Walks every bus and device, opens each one, and prints descriptors. |
| 15 | +With `-v`, also prints each device's full configuration tree. |
| 16 | + |
| 17 | +hotplug_monitor [poll-ms] |
| 18 | +------------------------- |
| 19 | +Polls usb_find_busses() and usb_find_devices() in a loop and prints the |
| 20 | +current bus/device tree whenever either reports a change. Useful for |
| 21 | +exercising the bus-removal path in usb_find_busses() and for verifying |
| 22 | +that exit produces no "device X.Y still referenced" warnings from the |
| 23 | +underlying libusb (issue #25). |
| 24 | + |
| 25 | +Default polling interval is 500 ms; pass an integer argv[1] in |
| 26 | +milliseconds to override. Exit with Ctrl-C; the destructor runs and |
| 27 | +frees all retained device references. |
| 28 | + |
| 29 | +Triggering hot-plug events |
| 30 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 31 | + |
| 32 | +* Device add/remove: plug or unplug any USB device. |
| 33 | + |
| 34 | +* Bus add/remove (Linux, requires root): unbind and rebind the USB |
| 35 | + host controller PCI device. This is the only way to make a whole |
| 36 | + bus disappear from libusb's perspective on most systems. |
| 37 | + |
| 38 | + lspci -D | grep USB |
| 39 | + # pick the BDF of the controller you want to drop, e.g. |
| 40 | + echo 0000:00:14.0 | sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind |
| 41 | + echo 0000:00:14.0 | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind |
| 42 | + |
| 43 | + Replace `xhci_hcd` with `ehci-pci`, `ohci-pci`, or `uhci_hcd` as |
| 44 | + appropriate. Unbinding the controller you booted from will make |
| 45 | + attached input devices stop working until you rebind it; consider |
| 46 | + picking a controller that is not in use. |
| 47 | + |
| 48 | +Verifying reference accounting |
| 49 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 50 | + |
| 51 | +Run with the libusb-1.0 debug level cranked up and watch the exit |
| 52 | +output: |
| 53 | + |
| 54 | + LIBUSB_DEBUG=4 ./hotplug_monitor |
| 55 | + |
| 56 | +After Ctrl-C there should be no `device X.Y still referenced` warnings |
| 57 | +from `libusb_exit`. For an exhaustive memory check, run under valgrind: |
| 58 | + |
| 59 | + LIBUSB_DEBUG=4 valgrind --leak-check=full ./hotplug_monitor |
0 commit comments