84 Commits

Author SHA1 Message Date
Vadim Mikhailov
b3fa9e7e63 Improve computer model detection when device tree is not available
Closes #614.
2025-05-30 23:23:04 -07:00
Vadim Mikhailov
fbe812bd83 Add an option to search/limit hub by description
Adding new option to limit hub by description.
Existing -s option limits by attached device description,
and new feature -H limits by hub instead.
Thanks to @pendragonsound for submitting patch!

Closes #611.
2025-04-19 10:35:44 -07:00
Vadim Mikhailov
c74755be42 Fix FreeBSD build, update docs regarding pkg-config 2025-01-21 19:45:22 -08:00
Vadim Mikhailov
07abf4cbea Fix warnings when compiled in C++ mode
Fixed all warnings when compiling with `c++ -std=c++11 ...`:
* bzero() appears to be deprecated, replaced with memset()
* fixed few const warnings
* suppressed zero array warnings (emitted by libusb headers)
2025-01-21 19:25:50 -08:00
Vadim Mikhailov
a7df8f8367 Cleanup ifdef usage
* No need to check for both __gnu_linux__ and __linux__, latter is enough.
* Require libusb 1.0.13 to build - this makes LIBUSB_API_VERSION always defined.
* Include `<libusb.h>` not `<libusb-1.0/libusb.h>` - this will always work
  if `pkg-config` is detected. For backwards compatibility still try building
  without `pkg-config` present, but it is much less reliable and will not work on Mac.
* Bump copyright year.
2025-01-18 14:04:34 -08:00
Tormod Volden
a957b21815 Add --sysdev/-y option for direct device node access (#600)
* Add --sysdev/-u option for direct device node access

Only the device specified by the given device path will be accessed,
instead of scanning the USB bus.

Most useful if you use udev rules to create stable device node aliases for hubs,
independent of bus topology.

For instance, if your matching udev rules include SYMLINK+="MYSMARTHUB1"
you can call uhubctl with --sysdev /dev/MYSMARTHUB1 instead of using -l
and a non-stable bus location to specify it.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
2025-01-18 11:50:25 -08:00
mvp
302b3c0c77 always include <fcntl.h>
4a233824c4 fixed issue for Raspberry Pi on Linux, but broke compilation on Mac.
Fixing it by always including <fcntl.h>.
2024-10-13 13:10:22 -07:00
Vadim Mikhailov
e0d1c34eed Improve support for Raspberry Pi 4B and 5:
* Detect if we are running on Raspberry 4B or 5
* Add RPi 4B and 5 conditionals for RPi hacks
* Relax some conditions which may vary depending on RPi hardware and/or OS
  (in particular Ubuntu hub numbering differs from Raspbian).
* For RPi5, changed README to suggest using busses 2 and 4 instead of 1 and 3.
  This makes the same recipe work on both Raspbian and Ubuntu.

This fixes #587.
2024-09-16 20:23:33 -07:00
mvp
153c5da267 Fixed snprintf override which breaks on old MacOS
`snprintf` is available in Mac/FreeBSD headers only if _XOPEN_SOURCE is 600 or higher.
This closes #586.
2024-09-12 10:42:23 -07:00
mvp
169842ae97 Improve Linux detection
__gnu_linux__ is not always present on all Linux platforms -
add checking for __linux__ too.
2024-08-31 16:19:59 -07:00
Misha Bunte
83ce372810 Fix flag to turn on and off 2024-08-16 14:05:14 -07:00
Misha Bunte
f22541cf64 Add flash (inverted cycle) option - turn on and off (#580)
* Add flash (inverted cycle) option - to turn port on then off with a delay
2024-08-13 13:24:09 -07:00
Christian Svensson
2df4681826 Construct root USB device sysfs path
Fixes #578. See issue for discussion.

Signed-off-by: Christian Svensson <blue@cmd.nu>
2024-08-05 12:32:33 -07:00
Christian Svensson
1f920f6b15 Fix endian conversions to work for big-endian
Before:
```
Current status for hub 3 [6b1d:0200 Linux 6.1.98 xhci-hcd xHCI Host Controller 0000:01:00.0, USB 0.02, 2 ports, ppps]
  Port 1: 0305 power lowspeed suspend connect [9911:c068 Sierra Wireless, Incorporated MC7304-CP]
  Port 2: 0301 power lowspeed connect [0304:0160 FTDI FT232R USB UART A10OEDEW]
```

After:

```
Current status for hub 1 [1d6b:0002 Linux 6.1.98 ehci_hcd EHCI Host Controller 16f0000000000.ehci, USB 2.00, 2 ports, nops]
  Port 1: 0503 power highspeed enable connect [0424:2240 Generic Ultra Fast Media 000000225001]
  Port 2: 0100 power
Current status for hub 3 [1d6b:0002 Linux 6.1.98 xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 2 ports, ppps]
  Port 1: 0503 power highspeed enable connect [1199:68c0 Sierra Wireless, Incorporated MC7304-CP]
  Port 2: 0103 power enable connect [0403:6001 FTDI FT232R USB UART A10OEDEW]
```

Tested on both x86-64 and MIPS64.

Signed-off-by: Christian Svensson <blue@cmd.nu>
2024-08-04 19:59:56 -07:00
Vadim Mikhailov
3d804c1ab7 Add support for Raspberry Pi 5
Added hack to support Raspberry Pi 5.
Updated README with recipe how to control power on RPi5.

Closes #547.
2024-04-19 16:08:19 -07:00
Vadim Mikhailov
f70c2ced96 Fix build on Mac
Recent libusb seems to have removed extra symlink `libusb-1.0/libusb.h`,
which breaks build on Mac.

TODO: consider always using `<libusb.h>`, but that would
put hard dependency on `pkg-config` usage.
2024-04-19 15:49:19 -07:00
Vadim Mikhailov
4deae1731c Update README, bump copyright year
* Remove mention of MacOS bug only applicable to old MacOS 12.4 x86
* Simplify docs related to Linux kernel 6.0 fixes
* Bump copyright year
2024-02-20 22:40:28 -08:00
Vadim Mikhailov
33f1ecb2dd Add explicit void prototypes to make modern compilers happy 2023-03-31 14:44:54 -07:00
Vadim Mikhailov
41e1bacf41 Bump copyright year 2023-01-27 11:23:08 -08:00
Vadim Mikhailov
b6189d1409 Consistently use only C style comments 2022-10-05 12:04:56 -07:00
Vadim Mikhailov
732ad2d90e Fix crash on non-Linux if -S is used
Also move short options near long options table so it is easier to maintain.
2022-09-22 11:24:32 -07:00
Vadim Mikhailov
dd5ff8f79d Fix unused variable compilation warning on non Linux platforms 2022-09-22 11:09:24 -07:00
Leonard Göhrs
aa7fc0a126 Add support for Linux sysfs based power switching
Starting with Linux kernel 6.0[1] there will be a sysfs interface to power USB
ports off/on from userspace.
Try to use this interface before falling back to the usual libusb based
power switching (e.g. when running on a kernel <6.0 or if file permissions do
not allow using the sysfs interface).

The main benefit of using the sysfs interface is that the kernel does not get
confused about the state of a port, so retrying should no longer be required.

[1]: https://lore.kernel.org/all/20220607114522.3359148-1-m.grzeschik@pengutronix.de/

Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
2022-09-22 11:40:03 +02:00
Leonard Göhrs
9d66811175 Split out port status setting logic into separate function
Split out current libusb based power on/off logic into a separate function
in anticipation of a other power switching implementations.

Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
2022-09-02 12:28:04 +02:00
Tim Ruffing
88e5641fe1 Make sure option strings are null-terminated 2022-08-24 15:59:01 -07:00
Vadim Mikhailov
ee98e43a43 Update copyright year 2022-05-10 18:13:08 -07:00
Vadim Mikhailov
f8cb89f4be Add option --nodesc to skip querying device string descriptors
Sometimes target devices may become unresponsive for control messages to read string descriptors.
Option --nodesc (-N) allows to still reset such devices.

Also, as preventative measure, check if libusb_get_string_descriptor_ascii returns any error,
then stop querying any other string descriptor from the same device.
2021-10-29 11:49:59 -07:00
Vadim Mikhailov
9b5efa0ec0 Simplify code that powers on/off
* Simplified code that powers on/off.
* Fixed minor bug for repeat counter (always once to power off, and opt_repeat times to power on).
* Only warn about USB permissions if no compatible hubs are detected.
  Old behavior would always warn, even if permissions were missing for any hub (even unsupported ones).
2021-01-17 14:34:11 -08:00
comzine
b99a0805bb Added feature toggle mode (#305)
Added feature toggle mode

Co-authored-by: Tobias Weber <comzine@gmail.com>
2021-01-17 11:28:44 -08:00
Vadim Mikhailov
71a4274615 Print uhubctl version as last item in --help output 2020-12-13 13:08:32 -08:00
Vadim Mikhailov
77aeb7f30b Implement searching by device description
Add switch --search (-s) to limit hubs by attached device description.
It is recommended to specify search string that is as unique as possible, e.g. device serial number.
It should work for "off" and "cycle" actions, but not for "on" because device would not be visible on USB.

Closes #209.
2020-12-08 18:40:13 -08:00
Vadim Mikhailov
0cc09299fe Add a switch to allow operation on unsupported hubs
Show power switching type supported by a hub:

* ppps   - per-port power switching
* ganged - ganged power switching
* nops   - no power switching

Add --force (-f) switch to allow operation on non-ppps hubs.
Use `-f` at your own risk, this most likely will not work to actually switch power.

For ganged hubs, you may need to turn power off for all hub ports to get any effect.
For nops hubs, it is not likely to work at all, but might be useful for informational purposes.

Also, instead of printing recipe on how to fix USB permissions on Linux,
forward user to web page with detailed explanations.

Closes #260, #280.
2020-12-07 18:43:22 -08:00
Vadim Mikhailov
44f963d00f Improve USB2/3 duality matching
Use priority based duality matching.

* Support Raspberry Pi 4B better (as it has USB2 hub one level deeper than its USB3 counterpart).
* Support M1 Macs (as they seem to place all USB devices to bus 2).
* Support Apple mini-dock (it advertises 2 USB2 ports but only 1 USB3 port).
* Should support multiple identical hubs (with the same container id) on Linux.
2020-12-07 11:32:29 -08:00
Vadim Mikhailov
fae3da7e6f Bump copyright year 2020-01-20 12:14:48 -08:00
Vadim Mikhailov
4aae44ced0 Add support for Raspberry Pi 4
Hack to support Raspberry Pi 4:

* Override ganged power switching reported by USB2 hub as per-port. This is not necessarily true,
  but is consistent with per-port power switching returned by dual USB3 root hub.
  Proper fix is for Raspberry Pi 4 USB firmware to return consistent descriptor information.

* When root USB3 hub is detected on RPi4 without ContainerID, it is assumed to have
  ContainerID of dual USB2 hub, so USB2/3 duality is still working as expected.
  Proper fix would be for USB3 root hub to report correct ContainerID.
2020-01-05 13:43:14 -08:00
Karsten Hohmeier
3e10680f22 Fix compile warning about format truncation by increasing buffer size for description 2019-11-05 13:52:24 -08:00
Vadim Mikhailov
d227c8326f Don't complain about USB permission problems if we are already root 2019-10-23 22:52:44 -07:00
Vadim Mikhailov
743ecf226b Relax level check when looking for USB2/3 dual hubs
Some devices (notably Raspberry 4B) have USB3 hub that advertises
its USB2 compatibility partner at different depth level.
Raspberry 4B onboard hub doesn't yet support uhubctl directly
(but it should once https://github.com/raspberrypi/linux/issues/3079 is fixed),
but this breaks level check restriction for any external USB3 hub attached to RPi4B
- so this check has been removed.
2019-10-16 23:25:36 -07:00
Vadim Mikhailov
4818eddc03 FreeBSD is using bNumDeviceCapabilities instead of bNumDeviceCaps
FreeBSD libusb is different from mainline libusb, and needs hacks here and there :-(

This resolves issue #175.
2019-09-20 15:29:30 -07:00
Vadim Mikhailov
1856ae9d59 Use --location for long option name
Add --location as long option name - it is still backwards compatible with --loc.
2019-09-02 23:09:56 -07:00
Vadim Mikhailov
5db248771e Add support for NetBSD
This adds support to be able to build and use uhubctl on NetBSD by using pkg-config to get proper CFLAGS and LDFLAGS.
Perhaps we should use pkg-config universally across all platforms,
but I don't want to potentially break support for existing plaftorms
because pkg-config does not seem to be unconditionally available everywhere.
2019-07-26 18:00:49 -07:00
Vadim Mikhailov
5f8426105c USB2/3 duality: require serial numbers match only if both present
I have observed a hub which had serial number present for USB2 compatibility hub,
but not for USB3, which broke USB2/3 duality handling:

    Current status for hub 4-4 [0451:8140, USB 3.00, 4 ports]
      Port 1: 06a0 power Rx.Detect
      Port 2: 06a0 power Rx.Detect
      Port 3: 06a0 power Rx.Detect
      Port 4: 06a0 power Rx.Detect
    Current status for hub 3-13 [0451:8142 210C08692601, USB 2.10, 4 ports]
      Port 1: 0100 power
      Port 2: 0100 power
      Port 3: 0100 power
      Port 4: 0100 power

This fix will only compare serial numbers if they are both present.
2019-07-15 10:45:21 -07:00
Vadim Mikhailov
75023dc28c Stop searching if matching device is found
This is minor fix for issue #161.
2019-07-04 10:46:25 -07:00
Vadim Mikhailov
a510d091af Implemented reading ContainerID from BOS descriptor to detect USB2/3 dual devices
Per USB 3.0 spec, chapter 11.2:

> Within a USB 3.0 hub, both the SuperSpeed and USB 2.0 hub devices shall implement in the hub framework
> a common standardized ContainerID to enable software to identify the physical relationship of the hub devices.
> The ContainerID descriptor is a part of the BOS descriptor set.

* Read ContainerID from BOS descriptor.
* get_device_description() now returns struct with vendor, product, serial number as well as full description.
* Use ContainerID to improve finding dual hubs. This seems to work very well on any OS.
  However, this may still fail if two or more identical hubs use the same hardcoded ContainerID
  In this case, we still try to look for other clues like serial number match if possible.

This fixes issue #161.
2019-07-04 00:45:22 -07:00
Vadim Mikhailov
a9da51fb1c Fixed help text for level, some code style fixes 2019-05-17 14:38:17 -07:00
Wujun Zhou
ee0287077b fix %d is used by the printf 2019-05-17 15:47:16 -04:00
Wujun Zhou
798d2323a9 fix help text for level 2019-05-17 15:43:23 -04:00
Wujun Zhou
bd3e398f4e add comment for the same level check in dual 2019-05-17 15:40:39 -04:00
Wujun Zhou
74a5d721c5 use 0 as opt_level wildcard 2019-05-17 15:39:10 -04:00
Wujun Zhou
134decf02b c style change 2019-05-17 15:27:38 -04:00