Skip to content

Commit

Permalink
Merge pull request #1254 from networkupstools/libusb-1.0+0.1
Browse files Browse the repository at this point in the history
Merge refined libusb-1.0+0.1 branch into NUT master
  • Loading branch information
jimklimov authored Jan 11, 2022
2 parents c0ce6ce + 22aca84 commit 74d3f50
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 82 deletions.
17 changes: 14 additions & 3 deletions drivers/bcmxcp_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <unistd.h>

#define SUBDRIVER_NAME "USB communication subdriver"
#define SUBDRIVER_VERSION "0.23"
#define SUBDRIVER_VERSION "0.26"

/* communication driver description structure */
upsdrv_info_t comm_upsdrv_info = {
Expand Down Expand Up @@ -358,6 +358,11 @@ void upsdrv_cleanup(void)
{
upslogx(LOG_ERR, "CLOSING\n");
nutusb_close(upsdev, "USB");
free(curDevice.Vendor);
free(curDevice.Product);
free(curDevice.Serial);
free(curDevice.Bus);
free(curDevice.Device);
}

void upsdrv_reconnect(void)
Expand Down Expand Up @@ -414,7 +419,11 @@ static usb_dev_handle *open_powerware_usb(void)
curDevice.VendorID = dev_desc.idVendor;
curDevice.ProductID = dev_desc.idProduct;
bus = libusb_get_bus_number(device);
curDevice.Bus = (char *)xmalloc(4);
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);

/* FIXME: we should also retrieve
Expand All @@ -426,9 +435,11 @@ static usb_dev_handle *open_powerware_usb(void)

if (is_usb_device_supported(pw_usb_device_table, &curDevice) == SUPPORTED) {
libusb_open(device, &udev);
libusb_free_device_list(devlist, 1);
return udev;
}
}
libusb_free_device_list(devlist, 1);
#else /* not WITH_LIBUSB_1_0 */
struct usb_bus *busses = usb_get_busses();
struct usb_bus *bus;
Expand All @@ -445,7 +456,7 @@ static usb_dev_handle *open_powerware_usb(void)

curDevice.VendorID = dev->descriptor.idVendor;
curDevice.ProductID = dev->descriptor.idProduct;
curDevice.Bus = strdup(bus->dirname);
curDevice.Bus = xstrdup(bus->dirname);

/* FIXME: we should also retrieve
* dev->descriptor.iManufacturer
Expand Down
2 changes: 1 addition & 1 deletion drivers/libhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ int HIDGetEvents(hid_dev_handle_t udev, HIDData_t **event, int eventsize)
buflen = comm_driver->get_interrupt(
udev, (usb_ctrl_charbuf)buf,
(usb_ctrl_charbufsize)r,
250);
750);

if (buflen <= 0) {
return buflen; /* propagate "error" or "no event" code */
Expand Down
20 changes: 14 additions & 6 deletions drivers/libusb0.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "nut_libusb.h"

#define USB_DRIVER_NAME "USB communication driver (libusb 0.1)"
#define USB_DRIVER_VERSION "0.34"
#define USB_DRIVER_VERSION "0.35"

/* driver description structure */
upsdrv_info_t comm_upsdrv_info = {
Expand Down Expand Up @@ -200,6 +200,8 @@ static int libusb_open(usb_dev_handle **udevp,

for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
/* int if_claimed = 0; */

upsdebugx(2, "Checking device (%04X/%04X) (%s/%s)",
dev->descriptor.idVendor, dev->descriptor.idProduct,
bus->dirname, dev->filename);
Expand Down Expand Up @@ -232,31 +234,31 @@ static int libusb_open(usb_dev_handle **udevp,

curDevice->VendorID = dev->descriptor.idVendor;
curDevice->ProductID = dev->descriptor.idProduct;
curDevice->Bus = strdup(bus->dirname);
curDevice->Device = strdup(dev->filename);
curDevice->Bus = xstrdup(bus->dirname);
curDevice->Device = xstrdup(dev->filename);
curDevice->bcdDevice = dev->descriptor.bcdDevice;

if (dev->descriptor.iManufacturer) {
ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer,
string, sizeof(string));
if (ret > 0) {
curDevice->Vendor = strdup(string);
curDevice->Vendor = xstrdup(string);
}
}

if (dev->descriptor.iProduct) {
ret = usb_get_string_simple(udev, dev->descriptor.iProduct,
string, sizeof(string));
if (ret > 0) {
curDevice->Product = strdup(string);
curDevice->Product = xstrdup(string);
}
}

if (dev->descriptor.iSerialNumber) {
ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber,
string, sizeof(string));
if (ret > 0) {
curDevice->Serial = strdup(string);
curDevice->Serial = xstrdup(string);
}
}

Expand Down Expand Up @@ -339,6 +341,7 @@ static int libusb_open(usb_dev_handle **udevp,
usb_strerror());
}
#endif
/* if_claimed = 1; */

nut_usb_set_altinterface(udev);

Expand Down Expand Up @@ -490,6 +493,11 @@ static int libusb_open(usb_dev_handle **udevp,
return rdlen;

next_device:
/* usb_release_interface() sometimes blocks
* and goes into uninterruptible sleep.
* So don't do it. */
/* if (if_claimed)
usb_release_interface(udev, 0); */
usb_close(udev);
}
}
Expand Down
Loading

0 comments on commit 74d3f50

Please sign in to comment.