Skip to content

Commit a7df8f8

Browse files
committed
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.
1 parent a957b21 commit a7df8f8

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
uhubctl – USB hub per-port power control.
22

3-
Copyright (c) 2009-2024, Vadim Mikhailov
3+
Copyright (c) 2009-2025, Vadim Mikhailov
44

55
This program is free software; you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ ifneq (,$(shell which $(PKG_CONFIG)))
3131
CFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0)
3232
LDFLAGS += $(shell $(PKG_CONFIG) --libs libusb-1.0)
3333
else
34-
# But it should still build if pkg-config is not available (e.g. Linux or Mac homebrew)
34+
# But it should still build even if pkg-config is not available
35+
CFLAGS += -I/usr/include/libusb-1.0
3536
LDFLAGS += -lusb-1.0
3637
endif
3738

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ This may be fixed if `libusb` starts supporting different driver on Windows.
164164

165165
Note that it is highly recommended to have `pkg-config` installed (many platforms provide it by default).
166166

167-
First, you need to install library libusb-1.0 (version 1.0.12 or later, 1.0.16 or later is recommended):
167+
First, you need to install library libusb-1.0 (version 1.0.13 or later is required,
168+
1.0.23 or later is recommended):
168169

169170
* Ubuntu: `sudo apt-get install libusb-1.0-0-dev`
170171
* Redhat: `sudo yum install libusb1-devel`
@@ -485,7 +486,7 @@ Notable projects using uhubctl
485486
Copyright
486487
=========
487488

488-
Copyright (C) 2009-2024 Vadim Mikhailov
489+
Copyright (C) 2009-2025 Vadim Mikhailov
489490

490491
This file can be distributed under the terms and conditions of the
491492
GNU General Public License version 2.

udev/rules.d/52-usb.rules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# uhubctl – USB hub per-port power control https://github.com/mvp/uhubctl
22
#
3-
# Copyright (c) 2009-2024, Vadim Mikhailov
3+
# Copyright (c) 2009-2025, Vadim Mikhailov
44
#
55
# This file can be distributed under the terms and conditions of the
66
# GNU General Public License version 2.

uhubctl.c

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2024 Vadim Mikhailov
2+
* Copyright (c) 2009-2025 Vadim Mikhailov
33
*
44
* Utility to turn USB port power on/off
55
* for USB hubs that support per-port power switching.
@@ -30,14 +30,16 @@
3030
#include <unistd.h>
3131
#endif
3232

33-
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_WIN32)
3433
#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
3739
#endif
3840

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!"
4143
#endif
4244

4345
#if _POSIX_C_SOURCE >= 199309L
@@ -221,9 +223,9 @@ static int opt_exact = 0; /* exact location match - disable USB3 duality handl
221223
static int opt_reset = 0; /* reset hub after operation(s) */
222224
static int opt_force = 0; /* force operation even on unsupported hubs */
223225
static int opt_nodesc = 0; /* skip querying device description */
224-
#if defined(__gnu_linux__) || defined(__linux__)
226+
#if defined(__linux__)
225227
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 */
227229
static const char *opt_sysdev;
228230
#endif
229231
#endif
@@ -234,11 +236,10 @@ static int is_rpi_5 = 0;
234236

235237
static const char short_options[] =
236238
"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__)
241240
"S"
241+
#if (LIBUSB_API_VERSION >= 0x01000107) /* 1.0.23 */
242+
"y:"
242243
#endif
243244
#endif
244245
;
@@ -256,9 +257,9 @@ static const struct option long_options[] = {
256257
{ "exact", no_argument, NULL, 'e' },
257258
{ "force", no_argument, NULL, 'f' },
258259
{ "nodesc", no_argument, NULL, 'N' },
259-
#if defined(__gnu_linux__) || defined(__linux__)
260+
#if defined(__linux__)
260261
{ "nosysfs", no_argument, NULL, 'S' },
261-
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107)
262+
#if (LIBUSB_API_VERSION >= 0x01000107)
262263
{ "sysdev", required_argument, NULL, 'y' },
263264
#endif
264265
#endif
@@ -288,9 +289,9 @@ static int print_usage(void)
288289
"--exact, -e - exact location (no USB3 duality handling).\n"
289290
"--force, -f - force operation even on unsupported hubs.\n"
290291
"--nodesc, -N - do not query device description (helpful for unresponsive devices).\n"
291-
#if defined(__gnu_linux__) || defined(__linux__)
292+
#if defined(__linux__)
292293
"--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)
294295
"--sysdev, -y - open system device node instead of scanning.\n"
295296
#endif
296297
#endif
@@ -423,7 +424,7 @@ static int check_computer_model(char *target)
423424
static int get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
424425
{
425426
int pcount;
426-
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102)
427+
#if (LIBUSB_API_VERSION >= 0x01000102)
427428
/*
428429
* libusb_get_port_path is deprecated since libusb v1.0.16,
429430
* therefore use libusb_get_port_numbers when supported
@@ -602,7 +603,7 @@ static int get_port_status(struct libusb_device_handle *devh, int port)
602603
}
603604

604605

605-
#if defined(__gnu_linux__) || defined(__linux__)
606+
#if defined(__linux__)
606607
/*
607608
* Try to use the Linux sysfs interface to power a port off/on.
608609
* Returns 0 on success.
@@ -702,7 +703,7 @@ static int set_port_status_libusb(struct libusb_device_handle *devh, int port, i
702703

703704
static int set_port_status(struct libusb_device_handle *devh, struct hub_info *hub, int port, int on)
704705
{
705-
#if defined(__gnu_linux__) || defined(__linux__)
706+
#if defined(__linux__)
706707
if (!opt_nosysfs) {
707708
if (set_port_status_linux(devh, hub, port, on) == 0) {
708709
return 0;
@@ -1093,7 +1094,7 @@ static int usb_find_hubs(void)
10931094
}
10941095
}
10951096
if (perm_ok == 0 && hub_phys_count == 0) {
1096-
#if defined(__gnu_linux__) || defined(__linux__)
1097+
#if defined(__linux__)
10971098
if (geteuid() != 0) {
10981099
fprintf(stderr,
10991100
"There were permission problems while accessing USB.\n"
@@ -1112,8 +1113,7 @@ int main(int argc, char *argv[])
11121113
int rc;
11131114
int c = 0;
11141115
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)
11171117
int sys_fd;
11181118
libusb_device_handle *sys_devh = NULL;
11191119
#endif
@@ -1181,11 +1181,11 @@ int main(int argc, char *argv[])
11811181
case 'N':
11821182
opt_nodesc = 1;
11831183
break;
1184-
#if defined(__gnu_linux__) || defined(__linux__)
1184+
#if defined(__linux__)
11851185
case 'S':
11861186
opt_nosysfs = 1;
11871187
break;
1188-
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107)
1188+
#if (LIBUSB_API_VERSION >= 0x01000107)
11891189
case 'y':
11901190
opt_sysdev = optarg;
11911191
break;
@@ -1232,8 +1232,7 @@ int main(int argc, char *argv[])
12321232
exit(1);
12331233
}
12341234

1235-
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) && \
1236-
(defined(__gnu_linux__) || defined(__linux__))
1235+
#if defined(__linux__) && (LIBUSB_API_VERSION >= 0x01000107)
12371236
if (opt_sysdev) {
12381237
sys_fd = open(opt_sysdev, O_RDWR);
12391238
if (sys_fd < 0) {
@@ -1367,8 +1366,7 @@ int main(int argc, char *argv[])
13671366
}
13681367
rc = 0;
13691368
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)
13721370
if (opt_sysdev && sys_fd >= 0) {
13731371
if (sys_devh)
13741372
libusb_close(sys_devh);

0 commit comments

Comments
 (0)