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.
This commit is contained in:
Vadim Mikhailov
2025-01-18 13:46:55 -08:00
parent a957b21815
commit a7df8f8367
5 changed files with 33 additions and 33 deletions

View File

@@ -1,6 +1,6 @@
uhubctl USB hub per-port power control. uhubctl USB hub per-port power control.
Copyright (c) 2009-2024, Vadim Mikhailov Copyright (c) 2009-2025, Vadim Mikhailov
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by

View File

@@ -31,7 +31,8 @@ ifneq (,$(shell which $(PKG_CONFIG)))
CFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0) CFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0)
LDFLAGS += $(shell $(PKG_CONFIG) --libs libusb-1.0) LDFLAGS += $(shell $(PKG_CONFIG) --libs libusb-1.0)
else else
# But it should still build if pkg-config is not available (e.g. Linux or Mac homebrew) # But it should still build even if pkg-config is not available
CFLAGS += -I/usr/include/libusb-1.0
LDFLAGS += -lusb-1.0 LDFLAGS += -lusb-1.0
endif endif

View File

@@ -164,7 +164,8 @@ This may be fixed if `libusb` starts supporting different driver on Windows.
Note that it is highly recommended to have `pkg-config` installed (many platforms provide it by default). Note that it is highly recommended to have `pkg-config` installed (many platforms provide it by default).
First, you need to install library libusb-1.0 (version 1.0.12 or later, 1.0.16 or later is recommended): First, you need to install library libusb-1.0 (version 1.0.13 or later is required,
1.0.23 or later is recommended):
* Ubuntu: `sudo apt-get install libusb-1.0-0-dev` * Ubuntu: `sudo apt-get install libusb-1.0-0-dev`
* Redhat: `sudo yum install libusb1-devel` * Redhat: `sudo yum install libusb1-devel`
@@ -485,7 +486,7 @@ Notable projects using uhubctl
Copyright Copyright
========= =========
Copyright (C) 2009-2024 Vadim Mikhailov Copyright (C) 2009-2025 Vadim Mikhailov
This file can be distributed under the terms and conditions of the This file can be distributed under the terms and conditions of the
GNU General Public License version 2. GNU General Public License version 2.

View File

@@ -1,6 +1,6 @@
# uhubctl USB hub per-port power control https://github.com/mvp/uhubctl # uhubctl USB hub per-port power control https://github.com/mvp/uhubctl
# #
# Copyright (c) 2009-2024, Vadim Mikhailov # Copyright (c) 2009-2025, Vadim Mikhailov
# #
# This file can be distributed under the terms and conditions of the # This file can be distributed under the terms and conditions of the
# GNU General Public License version 2. # GNU General Public License version 2.

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009-2024 Vadim Mikhailov * Copyright (c) 2009-2025 Vadim Mikhailov
* *
* Utility to turn USB port power on/off * Utility to turn USB port power on/off
* for USB hubs that support per-port power switching. * for USB hubs that support per-port power switching.
@@ -30,14 +30,16 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_WIN32)
#include <libusb.h> #include <libusb.h>
#else
#include <libusb-1.0/libusb.h> /* LIBUSBX_API_VERSION was first defined in libusb 1.0.13
and renamed to LIBUSB_API_VERSION since libusb 1.0.16 */
#if defined(LIBUSBX_API_VERSION) && !defined(LIBUSB_API_VERSION)
#define LIBUSB_API_VERSION LIBUSBX_API_VERSION
#endif #endif
#if !defined(LIBUSB_API_VERSION) || (LIBUSB_API_VERSION <= 0x01000103) #if !defined(LIBUSB_API_VERSION)
#define LIBUSB_DT_SUPERSPEED_HUB 0x2a #error "libusb-1.0 is required!"
#endif #endif
#if _POSIX_C_SOURCE >= 199309L #if _POSIX_C_SOURCE >= 199309L
@@ -221,9 +223,9 @@ static int opt_exact = 0; /* exact location match - disable USB3 duality handl
static int opt_reset = 0; /* reset hub after operation(s) */ static int opt_reset = 0; /* reset hub after operation(s) */
static int opt_force = 0; /* force operation even on unsupported hubs */ static int opt_force = 0; /* force operation even on unsupported hubs */
static int opt_nodesc = 0; /* skip querying device description */ static int opt_nodesc = 0; /* skip querying device description */
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
static int opt_nosysfs = 0; /* don't use the Linux sysfs port disable interface, even if available */ static int opt_nosysfs = 0; /* don't use the Linux sysfs port disable interface, even if available */
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) #if (LIBUSB_API_VERSION >= 0x01000107) /* 1.0.23 */
static const char *opt_sysdev; static const char *opt_sysdev;
#endif #endif
#endif #endif
@@ -234,11 +236,10 @@ static int is_rpi_5 = 0;
static const char short_options[] = static const char short_options[] =
"l:L:n:a:p:d:r:w:s:hvefRN" "l:L:n:a:p:d:r:w:s:hvefRN"
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107)
"Sy:"
#else
"S" "S"
#if (LIBUSB_API_VERSION >= 0x01000107) /* 1.0.23 */
"y:"
#endif #endif
#endif #endif
; ;
@@ -256,9 +257,9 @@ static const struct option long_options[] = {
{ "exact", no_argument, NULL, 'e' }, { "exact", no_argument, NULL, 'e' },
{ "force", no_argument, NULL, 'f' }, { "force", no_argument, NULL, 'f' },
{ "nodesc", no_argument, NULL, 'N' }, { "nodesc", no_argument, NULL, 'N' },
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
{ "nosysfs", no_argument, NULL, 'S' }, { "nosysfs", no_argument, NULL, 'S' },
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) #if (LIBUSB_API_VERSION >= 0x01000107)
{ "sysdev", required_argument, NULL, 'y' }, { "sysdev", required_argument, NULL, 'y' },
#endif #endif
#endif #endif
@@ -288,9 +289,9 @@ static int print_usage(void)
"--exact, -e - exact location (no USB3 duality handling).\n" "--exact, -e - exact location (no USB3 duality handling).\n"
"--force, -f - force operation even on unsupported hubs.\n" "--force, -f - force operation even on unsupported hubs.\n"
"--nodesc, -N - do not query device description (helpful for unresponsive devices).\n" "--nodesc, -N - do not query device description (helpful for unresponsive devices).\n"
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
"--nosysfs, -S - do not use the Linux sysfs port disable interface.\n" "--nosysfs, -S - do not use the Linux sysfs port disable interface.\n"
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) #if (LIBUSB_API_VERSION >= 0x01000107)
"--sysdev, -y - open system device node instead of scanning.\n" "--sysdev, -y - open system device node instead of scanning.\n"
#endif #endif
#endif #endif
@@ -423,7 +424,7 @@ static int check_computer_model(char *target)
static int get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize) static int get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
{ {
int pcount; int pcount;
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000102) #if (LIBUSB_API_VERSION >= 0x01000102)
/* /*
* libusb_get_port_path is deprecated since libusb v1.0.16, * libusb_get_port_path is deprecated since libusb v1.0.16,
* therefore use libusb_get_port_numbers when supported * therefore use libusb_get_port_numbers when supported
@@ -602,7 +603,7 @@ static int get_port_status(struct libusb_device_handle *devh, int port)
} }
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
/* /*
* Try to use the Linux sysfs interface to power a port off/on. * Try to use the Linux sysfs interface to power a port off/on.
* Returns 0 on success. * Returns 0 on success.
@@ -702,7 +703,7 @@ static int set_port_status_libusb(struct libusb_device_handle *devh, int port, i
static int set_port_status(struct libusb_device_handle *devh, struct hub_info *hub, int port, int on) static int set_port_status(struct libusb_device_handle *devh, struct hub_info *hub, int port, int on)
{ {
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
if (!opt_nosysfs) { if (!opt_nosysfs) {
if (set_port_status_linux(devh, hub, port, on) == 0) { if (set_port_status_linux(devh, hub, port, on) == 0) {
return 0; return 0;
@@ -1093,7 +1094,7 @@ static int usb_find_hubs(void)
} }
} }
if (perm_ok == 0 && hub_phys_count == 0) { if (perm_ok == 0 && hub_phys_count == 0) {
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
if (geteuid() != 0) { if (geteuid() != 0) {
fprintf(stderr, fprintf(stderr,
"There were permission problems while accessing USB.\n" "There were permission problems while accessing USB.\n"
@@ -1112,8 +1113,7 @@ int main(int argc, char *argv[])
int rc; int rc;
int c = 0; int c = 0;
int option_index = 0; int option_index = 0;
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) && \ #if defined(__linux__) && (LIBUSB_API_VERSION >= 0x01000107)
(defined(__gnu_linux__) || defined(__linux__))
int sys_fd; int sys_fd;
libusb_device_handle *sys_devh = NULL; libusb_device_handle *sys_devh = NULL;
#endif #endif
@@ -1181,11 +1181,11 @@ int main(int argc, char *argv[])
case 'N': case 'N':
opt_nodesc = 1; opt_nodesc = 1;
break; break;
#if defined(__gnu_linux__) || defined(__linux__) #if defined(__linux__)
case 'S': case 'S':
opt_nosysfs = 1; opt_nosysfs = 1;
break; break;
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) #if (LIBUSB_API_VERSION >= 0x01000107)
case 'y': case 'y':
opt_sysdev = optarg; opt_sysdev = optarg;
break; break;
@@ -1232,8 +1232,7 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) && \ #if defined(__linux__) && (LIBUSB_API_VERSION >= 0x01000107)
(defined(__gnu_linux__) || defined(__linux__))
if (opt_sysdev) { if (opt_sysdev) {
sys_fd = open(opt_sysdev, O_RDWR); sys_fd = open(opt_sysdev, O_RDWR);
if (sys_fd < 0) { if (sys_fd < 0) {
@@ -1367,8 +1366,7 @@ int main(int argc, char *argv[])
} }
rc = 0; rc = 0;
cleanup: cleanup:
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000107) && \ #if defined(__linux__) && (LIBUSB_API_VERSION >= 0x01000107)
(defined(__gnu_linux__) || defined(__linux__))
if (opt_sysdev && sys_fd >= 0) { if (opt_sysdev && sys_fd >= 0) {
if (sys_devh) if (sys_devh)
libusb_close(sys_devh); libusb_close(sys_devh);