1
1
/*
2
- * Copyright (c) 2009-2024 Vadim Mikhailov
2
+ * Copyright (c) 2009-2025 Vadim Mikhailov
3
3
*
4
4
* Utility to turn USB port power on/off
5
5
* for USB hubs that support per-port power switching.
30
30
#include <unistd.h>
31
31
#endif
32
32
33
- #if defined(__APPLE__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ ) || defined(_WIN32 )
34
33
#include <libusb.h>
35
- #else
36
- #include <libusb-1.0/libusb.h>
34
+
35
+ /* LIBUSBX_API_VERSION was first defined in libusb 1.0.13
36
+ and renamed to LIBUSB_API_VERSION since libusb 1.0.16 */
37
+ #if defined(LIBUSBX_API_VERSION ) && !defined(LIBUSB_API_VERSION )
38
+ #define LIBUSB_API_VERSION LIBUSBX_API_VERSION
37
39
#endif
38
40
39
- #if !defined(LIBUSB_API_VERSION ) || ( LIBUSB_API_VERSION <= 0x01000103 )
40
- #define LIBUSB_DT_SUPERSPEED_HUB 0x2a
41
+ #if !defined(LIBUSB_API_VERSION )
42
+ #error "libusb-1.0 is required!"
41
43
#endif
42
44
43
45
#if _POSIX_C_SOURCE >= 199309L
@@ -221,9 +223,9 @@ static int opt_exact = 0; /* exact location match - disable USB3 duality handl
221
223
static int opt_reset = 0 ; /* reset hub after operation(s) */
222
224
static int opt_force = 0 ; /* force operation even on unsupported hubs */
223
225
static int opt_nodesc = 0 ; /* skip querying device description */
224
- #if defined(__gnu_linux__ ) || defined( __linux__ )
226
+ #if defined(__linux__ )
225
227
static int opt_nosysfs = 0 ; /* don't use the Linux sysfs port disable interface, even if available */
226
- #if defined (LIBUSB_API_VERSION ) && ( LIBUSB_API_VERSION >= 0x01000107 )
228
+ #if (LIBUSB_API_VERSION >= 0x01000107 ) /* 1.0.23 */
227
229
static const char * opt_sysdev ;
228
230
#endif
229
231
#endif
@@ -234,11 +236,10 @@ static int is_rpi_5 = 0;
234
236
235
237
static const char short_options [] =
236
238
"l:L:n:a:p:d:r:w:s:hvefRN"
237
- #if defined(__gnu_linux__ ) || defined(__linux__ )
238
- #if defined(LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 )
239
- "Sy:"
240
- #else
239
+ #if defined(__linux__ )
241
240
"S"
241
+ #if (LIBUSB_API_VERSION >= 0x01000107 ) /* 1.0.23 */
242
+ "y:"
242
243
#endif
243
244
#endif
244
245
;
@@ -256,9 +257,9 @@ static const struct option long_options[] = {
256
257
{ "exact" , no_argument , NULL , 'e' },
257
258
{ "force" , no_argument , NULL , 'f' },
258
259
{ "nodesc" , no_argument , NULL , 'N' },
259
- #if defined(__gnu_linux__ ) || defined ( __linux__ )
260
+ #if defined(__linux__ )
260
261
{ "nosysfs" , no_argument , NULL , 'S' },
261
- #if defined( LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 )
262
+ #if (LIBUSB_API_VERSION >= 0x01000107 )
262
263
{ "sysdev" , required_argument , NULL , 'y' },
263
264
#endif
264
265
#endif
@@ -288,9 +289,9 @@ static int print_usage(void)
288
289
"--exact, -e - exact location (no USB3 duality handling).\n"
289
290
"--force, -f - force operation even on unsupported hubs.\n"
290
291
"--nodesc, -N - do not query device description (helpful for unresponsive devices).\n"
291
- #if defined(__gnu_linux__ ) || defined ( __linux__ )
292
+ #if defined(__linux__ )
292
293
"--nosysfs, -S - do not use the Linux sysfs port disable interface.\n"
293
- #if defined( LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 )
294
+ #if (LIBUSB_API_VERSION >= 0x01000107 )
294
295
"--sysdev, -y - open system device node instead of scanning.\n"
295
296
#endif
296
297
#endif
@@ -423,7 +424,7 @@ static int check_computer_model(char *target)
423
424
static int get_port_numbers (libusb_device * dev , uint8_t * buf , uint8_t bufsize )
424
425
{
425
426
int pcount ;
426
- #if defined( LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000102 )
427
+ #if (LIBUSB_API_VERSION >= 0x01000102 )
427
428
/*
428
429
* libusb_get_port_path is deprecated since libusb v1.0.16,
429
430
* therefore use libusb_get_port_numbers when supported
@@ -602,7 +603,7 @@ static int get_port_status(struct libusb_device_handle *devh, int port)
602
603
}
603
604
604
605
605
- #if defined(__gnu_linux__ ) || defined( __linux__ )
606
+ #if defined(__linux__ )
606
607
/*
607
608
* Try to use the Linux sysfs interface to power a port off/on.
608
609
* Returns 0 on success.
@@ -702,7 +703,7 @@ static int set_port_status_libusb(struct libusb_device_handle *devh, int port, i
702
703
703
704
static int set_port_status (struct libusb_device_handle * devh , struct hub_info * hub , int port , int on )
704
705
{
705
- #if defined(__gnu_linux__ ) || defined( __linux__ )
706
+ #if defined(__linux__ )
706
707
if (!opt_nosysfs ) {
707
708
if (set_port_status_linux (devh , hub , port , on ) == 0 ) {
708
709
return 0 ;
@@ -1093,7 +1094,7 @@ static int usb_find_hubs(void)
1093
1094
}
1094
1095
}
1095
1096
if (perm_ok == 0 && hub_phys_count == 0 ) {
1096
- #if defined(__gnu_linux__ ) || defined( __linux__ )
1097
+ #if defined(__linux__ )
1097
1098
if (geteuid () != 0 ) {
1098
1099
fprintf (stderr ,
1099
1100
"There were permission problems while accessing USB.\n"
@@ -1112,8 +1113,7 @@ int main(int argc, char *argv[])
1112
1113
int rc ;
1113
1114
int c = 0 ;
1114
1115
int option_index = 0 ;
1115
- #if defined(LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 ) && \
1116
- (defined(__gnu_linux__ ) || defined(__linux__ ))
1116
+ #if defined(__linux__ ) && (LIBUSB_API_VERSION >= 0x01000107 )
1117
1117
int sys_fd ;
1118
1118
libusb_device_handle * sys_devh = NULL ;
1119
1119
#endif
@@ -1181,11 +1181,11 @@ int main(int argc, char *argv[])
1181
1181
case 'N' :
1182
1182
opt_nodesc = 1 ;
1183
1183
break ;
1184
- #if defined(__gnu_linux__ ) || defined( __linux__ )
1184
+ #if defined(__linux__ )
1185
1185
case 'S' :
1186
1186
opt_nosysfs = 1 ;
1187
1187
break ;
1188
- #if defined( LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 )
1188
+ #if (LIBUSB_API_VERSION >= 0x01000107 )
1189
1189
case 'y' :
1190
1190
opt_sysdev = optarg ;
1191
1191
break ;
@@ -1232,8 +1232,7 @@ int main(int argc, char *argv[])
1232
1232
exit (1 );
1233
1233
}
1234
1234
1235
- #if defined(LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 ) && \
1236
- (defined(__gnu_linux__ ) || defined(__linux__ ))
1235
+ #if defined(__linux__ ) && (LIBUSB_API_VERSION >= 0x01000107 )
1237
1236
if (opt_sysdev ) {
1238
1237
sys_fd = open (opt_sysdev , O_RDWR );
1239
1238
if (sys_fd < 0 ) {
@@ -1367,8 +1366,7 @@ int main(int argc, char *argv[])
1367
1366
}
1368
1367
rc = 0 ;
1369
1368
cleanup :
1370
- #if defined(LIBUSB_API_VERSION ) && (LIBUSB_API_VERSION >= 0x01000107 ) && \
1371
- (defined(__gnu_linux__ ) || defined(__linux__ ))
1369
+ #if defined(__linux__ ) && (LIBUSB_API_VERSION >= 0x01000107 )
1372
1370
if (opt_sysdev && sys_fd >= 0 ) {
1373
1371
if (sys_devh )
1374
1372
libusb_close (sys_devh );
0 commit comments