uint8_t libusb_get_bus_number(libusb_device *dev) { if (dev == NULL) return (0); /* should not happen */ return (libusb20_dev_get_bus_number(dev->os_priv)); }
void dump_device_info(struct libusb20_device *pdev, uint8_t show_ifdrv) { char buf[128]; uint8_t n; unsigned int usage; usage = libusb20_dev_get_power_usage(pdev); printf("%s, cfg=%u md=%s spd=%s pwr=%s (%umA)\n", libusb20_dev_get_desc(pdev), libusb20_dev_get_config_index(pdev), dump_mode(libusb20_dev_get_mode(pdev)), dump_speed(libusb20_dev_get_speed(pdev)), dump_power_mode(libusb20_dev_get_power_mode(pdev)), usage); if (!show_ifdrv) return; for (n = 0; n != 255; n++) { if (libusb20_dev_get_iface_desc(pdev, n, buf, sizeof(buf))) break; if (buf[0] == 0) continue; printf("ugen%u.%u.%u: %s\n", libusb20_dev_get_bus_number(pdev), libusb20_dev_get_address(pdev), n, buf); } }
int usb_find_devices(void) { struct libusb20_device *pdev; struct usb_device *udev; struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; int devnum; int err; /* cleanup after last device search */ /* close all opened devices, if any */ while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) { udev = pdev->privLuData; libusb20_be_dequeue_device(usb_backend, pdev); libusb20_dev_free(pdev); if (udev != NULL) { LIST_DEL(usb_global_bus.devices, udev); free(udev); } } /* free old USB backend, if any */ libusb20_be_free(usb_backend); /* do a new backend device search */ usb_backend = libusb20_be_alloc_default(); if (usb_backend == NULL) { return (-1); } /* iterate all devices */ devnum = 1; pdev = NULL; while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) { udev = malloc(sizeof(*udev)); if (udev == NULL) break; memset(udev, 0, sizeof(*udev)); udev->bus = &usb_global_bus; snprintf(udev->filename, sizeof(udev->filename), "/dev/ugen%u.%u", libusb20_dev_get_bus_number(pdev), libusb20_dev_get_address(pdev)); ddesc = libusb20_dev_get_device_desc(pdev); udev->descriptor.bLength = sizeof(udev->descriptor); udev->descriptor.bDescriptorType = ddesc->bDescriptorType; udev->descriptor.bcdUSB = ddesc->bcdUSB; udev->descriptor.bDeviceClass = ddesc->bDeviceClass; udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass; udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol; udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0; udev->descriptor.idVendor = ddesc->idVendor; udev->descriptor.idProduct = ddesc->idProduct; udev->descriptor.bcdDevice = ddesc->bcdDevice; udev->descriptor.iManufacturer = ddesc->iManufacturer; udev->descriptor.iProduct = ddesc->iProduct; udev->descriptor.iSerialNumber = ddesc->iSerialNumber; udev->descriptor.bNumConfigurations = ddesc->bNumConfigurations; if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) { /* truncate number of configurations */ udev->descriptor.bNumConfigurations = USB_MAXCONFIG; } udev->devnum = devnum++; /* link together the two structures */ udev->dev = pdev; pdev->privLuData = udev; err = libusb20_dev_open(pdev, 0); if (err == 0) { /* XXX get all config descriptors by default */ usb_fetch_and_parse_descriptors((void *)pdev); libusb20_dev_close(pdev); } LIST_ADD(usb_global_bus.devices, udev); } return (devnum - 1); /* success */ }
void usb_control_ep_error_test(uint16_t vid, uint16_t pid) { struct LIBUSB20_CONTROL_SETUP_DECODED req; struct libusb20_device *pdev; uint8_t buffer[256]; int error; int fail = 0; int bus; int dev; int cfg; pdev = find_usb_device(vid, pid); if (pdev == NULL) { printf("USB device not found\n"); return; } error = libusb20_dev_open(pdev, 0); if (error) { printf("Could not open USB device\n"); libusb20_dev_free(pdev); return; } bus = libusb20_dev_get_bus_number(pdev); dev = libusb20_dev_get_address(pdev); for (cfg = 0; cfg != 255; cfg++) { LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); req.bmRequestType = 0x80; /* read */ req.bRequest = 0x06; /* descriptor */ req.wValue = 0x0200 | cfg; /* config descriptor */ req.wIndex = 0; req.wLength = 255; printf("Test #%d.1/3 ...\n", cfg); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Last configuration index is: %d\n", cfg - 1); break; } printf("Test #%d.2/3 ...\n", cfg); set_ctrl_ep_fail(bus,dev,1,1); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Cannot fetch descriptor (unexpected)\n"); fail++; } printf("Test #%d.3/3 ...\n", cfg); set_ctrl_ep_fail(bus,dev,0,1); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); set_ctrl_ep_fail(-1,-1,0,0); error = libusb20_dev_request_sync(pdev, &req, buffer, NULL, 1000, 0); if (error != 0) { printf("Cannot fetch descriptor (unexpected)\n"); fail++; } } libusb20_dev_close(pdev); libusb20_dev_free(pdev); printf("Test completed detecting %d failures\nDone\n\n", fail); }