Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libusb_get_device_address(), not libusb_get_port_number() #1819

Merged
merged 6 commits into from
Jan 22, 2023
7 changes: 4 additions & 3 deletions drivers/bcmxcp_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ static usb_dev_handle *open_powerware_usb(void)
ssize_t devcount = 0;
libusb_device_handle *udev;
struct libusb_device_descriptor dev_desc;
uint8_t bus;
uint8_t bus_num;
/* TODO: consider device_addr */
int i;

devcount = libusb_get_device_list(NULL, &devlist);
Expand All @@ -441,13 +442,13 @@ static usb_dev_handle *open_powerware_usb(void)

curDevice.VendorID = dev_desc.idVendor;
curDevice.ProductID = dev_desc.idProduct;
bus = libusb_get_bus_number(device);
bus_num = libusb_get_bus_number(device);
curDevice.Bus = (char *)malloc(4);
if (curDevice.Bus == NULL) {
libusb_free_device_list(devlist, 1);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
sprintf(curDevice.Bus, "%03d", bus);
sprintf(curDevice.Bus, "%03d", bus_num);

/* FIXME: we should also retrieve
* dev->descriptor.iManufacturer
Expand Down
26 changes: 13 additions & 13 deletions drivers/libusb1.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "nut_stdint.h"

#define USB_DRIVER_NAME "USB communication driver (libusb 1.0)"
#define USB_DRIVER_VERSION "0.44"
#define USB_DRIVER_VERSION "0.45"

/* driver description structure */
upsdrv_info_t comm_upsdrv_info = {
Expand Down Expand Up @@ -162,7 +162,7 @@ static int nut_libusb_open(libusb_device_handle **udevp,
struct libusb_config_descriptor *conf_desc = NULL;
const struct libusb_interface_descriptor *if_desc;
libusb_device_handle *udev;
uint8_t bus, port;
uint8_t bus_num, device_addr;
int ret, res;
unsigned char buf[20];
const unsigned char *p;
Expand Down Expand Up @@ -242,39 +242,39 @@ static int nut_libusb_open(libusb_device_handle **udevp,
/* Keep the list of items in sync with those matched by
* drivers/libusb0.c and tools/nut-scanner/scan_usb.c:
*/
bus = libusb_get_bus_number(device);
bus_num = libusb_get_bus_number(device);
curDevice->Bus = (char *)malloc(4);
if (curDevice->Bus == NULL) {
libusb_free_device_list(devlist, 1);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
if (bus > 0) {
sprintf(curDevice->Bus, "%03d", bus);
if (bus_num > 0) {
sprintf(curDevice->Bus, "%03d", bus_num);
} else {
upsdebugx(1, "%s: invalid libusb bus number %i",
__func__, bus);
__func__, bus_num);
}

port = libusb_get_port_number(device);
device_addr = libusb_get_device_address(device);
curDevice->Device = (char *)malloc(4);
if (curDevice->Device == NULL) {
libusb_free_device_list(devlist, 1);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
if (port > 0) {
if (device_addr > 0) {
/* 0 means not available, e.g. lack of platform support */
sprintf(curDevice->Device, "%03d", port);
sprintf(curDevice->Device, "%03d", device_addr);
} else {
if (devnum <= 999) {
/* Log visibly so users know their number discovered
* from `lsusb` or `dmesg` (if any) was ignored */
upsdebugx(0, "%s: invalid libusb port number %i, "
upsdebugx(0, "%s: invalid libusb device address %" PRIu8 ", "
"falling back to enumeration order counter %" PRIuSIZE,
__func__, port, devnum);
__func__, device_addr, devnum);
sprintf(curDevice->Device, "%03d", (int)devnum);
} else {
upsdebugx(1, "%s: invalid libusb port number %i",
__func__, port);
upsdebugx(1, "%s: invalid libusb device address %" PRIu8,
__func__, device_addr);
}
}

Expand Down
7 changes: 4 additions & 3 deletions drivers/nutdrv_atcl_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ static int usb_device_open(usb_dev_handle **handlep, USBDevice_t *device, USBDev
ssize_t devcount = 0;
libusb_device_handle *handle;
struct libusb_device_descriptor dev_desc;
uint8_t bus;
uint8_t bus_num;
/* TODO: consider device_addr */
int i;

devcount = libusb_get_device_list(NULL, &devlist);
Expand Down Expand Up @@ -341,13 +342,13 @@ static int usb_device_open(usb_dev_handle **handlep, USBDevice_t *device, USBDev
#if WITH_LIBUSB_1_0
device->VendorID = dev_desc.idVendor;
device->ProductID = dev_desc.idProduct;
bus = libusb_get_bus_number(dev);
bus_num = libusb_get_bus_number(dev);
device->Bus = (char *)malloc(4);
if (device->Bus == NULL) {
libusb_free_device_list(devlist, 1);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
sprintf(device->Bus, "%03d", bus);
sprintf(device->Bus, "%03d", bus_num);
iManufacturer = dev_desc.iManufacturer;
iProduct = dev_desc.iProduct;
iSerialNumber = dev_desc.iSerialNumber;
Expand Down
7 changes: 4 additions & 3 deletions drivers/richcomm_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ static int usb_device_open(usb_dev_handle **handlep, USBDevice_t *device, USBDev
ssize_t devcount = 0;
libusb_device_handle *handle;
struct libusb_device_descriptor dev_desc;
uint8_t bus;
uint8_t bus_num;
/* TODO: consider device_addr */
int i;

devcount = libusb_get_device_list(NULL, &devlist);
Expand Down Expand Up @@ -391,13 +392,13 @@ static int usb_device_open(usb_dev_handle **handlep, USBDevice_t *device, USBDev
#if WITH_LIBUSB_1_0
device->VendorID = dev_desc.idVendor;
device->ProductID = dev_desc.idProduct;
bus = libusb_get_bus_number(dev);
bus_num = libusb_get_bus_number(dev);
device->Bus = (char *)malloc(4);
if (device->Bus == NULL) {
libusb_free_device_list(devlist, 1);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
sprintf(device->Bus, "%03d", bus);
sprintf(device->Bus, "%03d", bus_num);
iManufacturer = dev_desc.iManufacturer;
iProduct = dev_desc.iProduct;
iSerialNumber = dev_desc.iSerialNumber;
Expand Down
23 changes: 13 additions & 10 deletions tools/nut-scanner/scan_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static int (*nut_usb_get_string_simple)(libusb_device_handle *dev, int index,
static ssize_t (*nut_usb_get_device_list)(libusb_context *ctx, libusb_device ***list);
static void (*nut_usb_free_device_list)(libusb_device **list, int unref_devices);
static uint8_t (*nut_usb_get_bus_number)(libusb_device *dev);
static uint8_t (*nut_usb_get_port_number)(libusb_device *dev);
static uint8_t (*nut_usb_get_device_address)(libusb_device *dev);
static int (*nut_usb_get_device_descriptor)(libusb_device *dev,
struct libusb_device_descriptor *desc);
#else /* => WITH_LIBUSB_0_1 */
Expand Down Expand Up @@ -151,8 +151,8 @@ int nutscan_load_usb_library(const char *libname_path)
* was a libusb_get_port_path() equivalent with different arguments, but
* not for too long (libusb-1.0.12...1.0.16) and now it is deprecated.
*/
*(void **) (&nut_usb_get_port_number) = lt_dlsym(dl_handle,
"libusb_get_port_number");
*(void **) (&nut_usb_get_device_address) = lt_dlsym(dl_handle,
"libusb_get_device_address");
if ((dl_error = lt_dlerror()) != NULL) {
goto err;
}
Expand Down Expand Up @@ -250,9 +250,12 @@ nutscan_device_t * nutscan_scan_usb()
* different consumers plugged into same socket should have
* the same port value. However in practice such functionality
* depends on platform and HW involved.
* In libusb1 API: libusb_get_port_numbers() earlier known
* In libusb1 API: first libusb_get_port_numbers() earlier known
* as libusb_get_port_path() for physical port number on the bus, see
* https://libusb.sourceforge.io/api-1.0/group__libusb__dev.html#ga14879a0ea7daccdcddb68852d86c00c4
* later changed to logical libusb_get_bus_number() (which
* often yields same numeric value, except on systems that
* can not see or tell about physical topology)
* In libusb0 API: "device filename"
*/
char *device_port = NULL;
Expand All @@ -261,7 +264,7 @@ nutscan_device_t * nutscan_scan_usb()
#if WITH_LIBUSB_1_0
libusb_device *dev;
libusb_device **devlist;
uint8_t bus;
uint8_t bus_num;
#else /* => WITH_LIBUSB_0_1 */
struct usb_device *dev;
struct usb_bus *bus;
Expand Down Expand Up @@ -310,25 +313,25 @@ nutscan_device_t * nutscan_scan_usb()
iManufacturer = dev_desc.iManufacturer;
iProduct = dev_desc.iProduct;
iSerialNumber = dev_desc.iSerialNumber;
bus = (*nut_usb_get_bus_number)(dev);
bus_num = (*nut_usb_get_bus_number)(dev);

busname = (char *)malloc(4);
if (busname == NULL) {
(*nut_usb_free_device_list)(devlist, 1);
(*nut_usb_exit)(NULL);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
}
snprintf(busname, 4, "%03d", bus);
snprintf(busname, 4, "%03d", bus_num);

device_port = (char *)malloc(4);
if (device_port == NULL) {
(*nut_usb_free_device_list)(devlist, 1);
(*nut_usb_exit)(NULL);
fatal_with_errno(EXIT_FAILURE, "Out of memory");
} else {
uint8_t port = (*nut_usb_get_port_number)(dev);
if (port > 0) {
snprintf(device_port, 4, "%03d", port);
uint8_t device_addr = (*nut_usb_get_device_address)(dev);
if (device_addr > 0) {
snprintf(device_port, 4, "%03d", device_addr);
} else {
snprintf(device_port, 4, ".*");
}
Expand Down