static void print_devs(libusb_device **devs) { libusb_device *dev; int i = 0, j = 0; uint8_t path[8]; while ((dev = devs[i++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { fprintf(stderr, "failed to get device descriptor"); return; } printf("%04x:%04x (bus %d, device %d)", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { printf(" path: %d", path[0]); for (j = 1; j < r; j++) printf(".%d", path[j]); } printf("\n"); } }
void IntersonManagerTest::printDevices(libusb_device ** deviceList) { libusb_device *dev; int i = 0, j = 0; uint8_t path[8]; uint8_t string_index[3]; // indexes of the string descriptors char string[128]; while ((dev = deviceList[i]) != NULL) { printf("\n"); struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { fprintf(stderr, "failed to get device descriptor"); return; } string_index[0] = desc.iManufacturer; string_index[1] = desc.iProduct; string_index[2] = desc.iSerialNumber; printf("%04x:%04x (bus %d, device %d)", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { printf(" path: %d", path[0]); for (j = 1; j < r; j++) printf(".%d", path[j]); } printf("\n"); libusb_device_handle *handle; r = libusb_open(dev, &handle); if(r == LIBUSB_ERROR_ACCESS) { std::cout<<" Insufficient permission"<<std::endl; } if (r == 0 && handle != NULL) { for (int k = 0; k < 3; k++) { if (string_index[k] == 0) { continue; } if (libusb_get_string_descriptor_ascii(handle, string_index[k], (unsigned char *) string, 128) >= 0) { printf("String Desc(0x%02X): \"%s\"\n", string_index[k], string); } } libusb_close(handle); } i++; } }
void printdev(libusb_device *dev) { libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { std::cout<<"failed to get device descriptor"<<std::endl; return; } std::cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<" "; std::cout<<"Device Class: "<<(int)desc.bDeviceClass<<" "; std::cout<<"VendorID: "<< desc.idVendor<<"(int) "; std::cout<<"ProductID: "<<desc.idProduct<<"(int)"<<std::endl; //std::cout<<"Address: "<<libusb_get_device_address(dev)<<std::endl; //std::cout<<"Bus number: "<<libusb_get_bus_number(dev)<<std::endl; std::cout << "as hex-> "<<std::hex << std::setfill('0') << std::setw(4)<< desc.idVendor<<":"; std::cout <<std::hex << std::setfill('0') << std::setw(4)<< std::hex <<desc.idProduct; printf("(bus %d, device %d)\n",libusb_get_bus_number(dev), libusb_get_device_address(dev)); uint8_t path[8]; r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { printf(" path: %d", path[0]); for (int j = 1; j < r; j++) printf(".%d", path[j]); } printf("\n"); /* libusb_config_descriptor *config; libusb_get_config_descriptor(dev, 0, &config); std::cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| "; const libusb_interface *inter; const libusb_interface_descriptor *interdesc; const libusb_endpoint_descriptor *epdesc; for(int i=0; i<(int)config->bNumInterfaces; i++) { inter = &config->interface[i]; std::cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | "; for(int j=0; j<inter->num_altsetting; j++) { interdesc = &inter->altsetting[j]; std::cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | "; std::cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | "; for(int k=0; k<(int)interdesc->bNumEndpoints; k++) { epdesc = &interdesc->endpoint[k]; std::cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | "; std::cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | "; } } } std::cout<<std::endl<<std::endl<<std::endl; libusb_free_config_descriptor(config); */ }
static bool device_location_equal(libusb_device *device, const char *location) { bool result = false; #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS char *loc = strdup(location); uint8_t port_path[7]; int path_step, path_len; uint8_t dev_bus = libusb_get_bus_number(device); char *ptr; path_len = libusb_get_port_numbers(device, port_path, 7); if (path_len == LIBUSB_ERROR_OVERFLOW) { LOG_ERROR("cannot determine path to usb device! (more than 7 ports in path)"); goto done; } LOG_DEBUG("device path has %i steps", path_len); ptr = strtok(loc, ":"); if (ptr == NULL) { LOG_DEBUG("no ':' in path"); goto done; } if (atoi(ptr) != dev_bus) { LOG_DEBUG("bus mismatch"); goto done; } path_step = 0; while (path_step < 7) { ptr = strtok(NULL, ","); if (ptr == NULL) { LOG_DEBUG("no more tokens in path at step %i", path_step); break; } if (path_step < path_len && atoi(ptr) != port_path[path_step]) { LOG_DEBUG("path mismatch at step %i", path_step); break; } path_step++; }; /* walked the full path, all elements match */ if (path_step == path_len) result = true; done: free(loc); #endif return result; }
unsigned int get_location_id(libusb_device *dev) { uint8_t port_numbers[7]; int j; int count = libusb_get_port_numbers(dev,port_numbers, 7); unsigned int location_id = 0; for (j = 0; j < count; j++) { location_id |= (port_numbers[j] & 0xf) << (20 - 4*j); } location_id |= (libusb_get_bus_number(dev) << 24); return location_id; }
char *get_path(libusb_device *dev) { uint8_t path[8]; int r,j; r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { sprintf(path_buf,"%d-%d",libusb_get_bus_number(dev),path[0]); for (j = 1; j < r; j++){ sprintf(path_buf+strlen(path_buf),".%d",path[j]); }; } return path_buf; }
bool TrackerDeviceEnumerator::recompute_current_device_validity() { dev_valid = false; if (cur_dev != nullptr) { USBDeviceInfo &dev_info = g_supported_tracker_infos[GET_DEVICE_TYPE_INDEX(m_deviceType)]; struct libusb_device_descriptor dev_desc; int libusb_result = libusb_get_device_descriptor(cur_dev, &dev_desc); if (libusb_result == 0 && dev_desc.idVendor == dev_info.vendor_id && dev_desc.idProduct == dev_info.product_id) { uint8_t port_numbers[MAX_USB_DEVICE_PORT_PATH]; memset(port_numbers, 0, sizeof(port_numbers)); int elements_filled= libusb_get_port_numbers(cur_dev, port_numbers, MAX_USB_DEVICE_PORT_PATH); if (elements_filled > 0) { // Make sure this device is actually different from the last device we looked at // (i.e. has a different device port path) if (memcmp(port_numbers, dev_port_numbers, sizeof(port_numbers)) != 0) { libusb_device_handle *devhandle; // Finally need to test that we can actually open the device // (or see that device is already open) libusb_result = libusb_open(cur_dev, &devhandle); if (libusb_result == LIBUSB_SUCCESS || libusb_result == LIBUSB_ERROR_ACCESS) { if (libusb_result == LIBUSB_SUCCESS) { libusb_close(devhandle); } // Cache the port number for the last valid device found memcpy(dev_port_numbers, port_numbers, sizeof(port_numbers)); dev_valid = true; } } } } } return dev_valid; }
static void print_devs(libusb_device **devs) { libusb_device *dev; struct libusb_config_descriptor *conf_desc; int nb_ifaces = 0; int i = 0, j = 0, n = 0; uint8_t path[8]; while ((dev = devs[n++]) != NULL) { struct libusb_device_descriptor desc; int r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { fprintf(stderr, "failed to get device descriptor"); return; } printf("%04x:%04x (bus %d, device %d)", desc.idVendor, desc.idProduct, libusb_get_bus_number(dev), libusb_get_device_address(dev)); r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { printf(" path: %d", path[0]); for (j = 1; j < r; j++) printf(".%d", path[j]); } printf("\n"); libusb_get_config_descriptor(dev, 0, &conf_desc); nb_ifaces = conf_desc->bNumInterfaces; fprintf(stdout, "nb_ifaces:::%d\n", nb_ifaces); for (i=0; i<nb_ifaces; i++) { printf(" interface[%d]: id = %d\n", i, conf_desc->interface[i].altsetting[0].bInterfaceNumber); for (j=0; j<conf_desc->interface[i].num_altsetting; j++) { printf("interface[%d].altsetting[%d]: num endpoints = %d\n", i, j, conf_desc->interface[i].altsetting[j].bNumEndpoints); printf(" Class.SubClass.Protocol: %02X.%02X.%02X\n", conf_desc->interface[i].altsetting[j].bInterfaceClass, conf_desc->interface[i].altsetting[j].bInterfaceSubClass, conf_desc->interface[i].altsetting[j].bInterfaceProtocol); } } } }
static std::string get_usb_port_id(libusb_device* usb_device) { auto usb_bus = std::to_string(libusb_get_bus_number(usb_device)); // As per the USB 3.0 specs, the current maximum limit for the depth is 7. const auto max_usb_depth = 8; uint8_t usb_ports[max_usb_depth] = {}; std::stringstream port_path; auto port_count = libusb_get_port_numbers(usb_device, usb_ports, max_usb_depth); auto usb_dev = std::to_string(libusb_get_device_address(usb_device)); for (size_t i = 0; i < port_count; ++i) { port_path << std::to_string(usb_ports[i]) << (((i+1) < port_count)?".":""); } return usb_bus + "-" + port_path.str() + "-" + usb_dev; }
std::string get_device_port(libusb_device *dev) { int bus = libusb_get_bus_number(dev); //int address = libusb_get_device_address(dev); std::ostringstream os; os << bus << "-"; uint8_t path[8]; int r = libusb_get_port_numbers(dev, path, sizeof(path)); if (r > 0) { //printf(" path: %d", path[0]); os << (int)path[0]; for (int j = 1; j < r; j++) os << "." << (int)path[j]; //printf(".%d", path[j]); } return os.str(); }
SR_PRIV int usb_get_port_path(libusb_device *dev, char *path, int path_len) { uint8_t port_numbers[8]; int i, n, len; /* * FreeBSD requires that devices prior to calling libusb_get_port_numbers() * have been opened with libusb_open(). * This apparently also applies to some Mac OS X versions. */ #if defined(__FreeBSD__) || defined(__APPLE__) struct libusb_device_handle *devh; if (libusb_open(dev, &devh) != 0) return SR_ERR; #endif n = libusb_get_port_numbers(dev, port_numbers, sizeof(port_numbers)); #if defined(__FreeBSD__) || defined(__APPLE__) libusb_close(devh); #endif /* Workaround FreeBSD / Mac OS X libusb_get_port_numbers() returning 0. */ #if defined(__FreeBSD__) || defined(__APPLE__) if (n == 0) { port_numbers[0] = libusb_get_device_address(dev); n = 1; } #endif if (n < 1) return SR_ERR; len = snprintf(path, path_len, "usb/%d-%d", libusb_get_bus_number(dev), port_numbers[0]); for (i = 1; i < n; i++) len += snprintf(path+len, path_len-len, ".%d", port_numbers[i]); return SR_OK; }
static int test_device(uint16_t vid, uint16_t pid) { libusb_device_handle *handle; libusb_device *dev; uint8_t bus, port_path[8]; struct libusb_bos_descriptor *bos_desc; struct libusb_config_descriptor *conf_desc; const struct libusb_endpoint_descriptor *endpoint; int i, j, k, r; int iface, nb_ifaces, first_iface = -1; struct libusb_device_descriptor dev_desc; const char* speed_name[5] = { "Unknown", "1.5 Mbit/s (USB LowSpeed)", "12 Mbit/s (USB FullSpeed)", "480 Mbit/s (USB HighSpeed)", "5000 Mbit/s (USB SuperSpeed)"}; char string[128]; uint8_t string_index[3]; // indexes of the string descriptors uint8_t endpoint_in = 0, endpoint_out = 0; // default IN and OUT endpoints printf("Opening device %04X:%04X...\n", vid, pid); handle = libusb_open_device_with_vid_pid(NULL, vid, pid); if (handle == NULL) { perr(" Failed.\n"); return -1; } dev = libusb_get_device(handle); bus = libusb_get_bus_number(dev); if (extra_info) { r = libusb_get_port_numbers(dev, port_path, sizeof(port_path)); if (r > 0) { printf("\nDevice properties:\n"); printf(" bus number: %d\n", bus); printf(" port path: %d", port_path[0]); for (i=1; i<r; i++) { printf("->%d", port_path[i]); } printf(" (from root hub)\n"); } r = libusb_get_device_speed(dev); if ((r<0) || (r>4)) r=0; printf(" speed: %s\n", speed_name[r]); } printf("\nReading device descriptor:\n"); CALL_CHECK(libusb_get_device_descriptor(dev, &dev_desc)); printf(" length: %d\n", dev_desc.bLength); printf(" device class: %d\n", dev_desc.bDeviceClass); printf(" S/N: %d\n", dev_desc.iSerialNumber); printf(" VID:PID: %04X:%04X\n", dev_desc.idVendor, dev_desc.idProduct); printf(" bcdDevice: %04X\n", dev_desc.bcdDevice); printf(" iMan:iProd:iSer: %d:%d:%d\n", dev_desc.iManufacturer, dev_desc.iProduct, dev_desc.iSerialNumber); printf(" nb confs: %d\n", dev_desc.bNumConfigurations); // Copy the string descriptors for easier parsing string_index[0] = dev_desc.iManufacturer; string_index[1] = dev_desc.iProduct; string_index[2] = dev_desc.iSerialNumber; printf("\nReading BOS descriptor: "); if (libusb_get_bos_descriptor(handle, &bos_desc) == LIBUSB_SUCCESS) { printf("%d caps\n", bos_desc->bNumDeviceCaps); for (i = 0; i < bos_desc->bNumDeviceCaps; i++) print_device_cap(bos_desc->dev_capability[i]); libusb_free_bos_descriptor(bos_desc); } else { printf("no descriptor\n"); } printf("\nReading first configuration descriptor:\n"); CALL_CHECK(libusb_get_config_descriptor(dev, 0, &conf_desc)); nb_ifaces = conf_desc->bNumInterfaces; printf(" nb interfaces: %d\n", nb_ifaces); if (nb_ifaces > 0) first_iface = conf_desc->usb_interface[0].altsetting[0].bInterfaceNumber; for (i=0; i<nb_ifaces; i++) { printf(" interface[%d]: id = %d\n", i, conf_desc->usb_interface[i].altsetting[0].bInterfaceNumber); for (j=0; j<conf_desc->usb_interface[i].num_altsetting; j++) { printf("interface[%d].altsetting[%d]: num endpoints = %d\n", i, j, conf_desc->usb_interface[i].altsetting[j].bNumEndpoints); printf(" Class.SubClass.Protocol: %02X.%02X.%02X\n", conf_desc->usb_interface[i].altsetting[j].bInterfaceClass, conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass, conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol); if ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceClass == LIBUSB_CLASS_MASS_STORAGE) && ( (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x01) || (conf_desc->usb_interface[i].altsetting[j].bInterfaceSubClass == 0x06) ) && (conf_desc->usb_interface[i].altsetting[j].bInterfaceProtocol == 0x50) ) { // Mass storage devices that can use basic SCSI commands test_mode = USE_SCSI; } for (k=0; k<conf_desc->usb_interface[i].altsetting[j].bNumEndpoints; k++) { struct libusb_ss_endpoint_companion_descriptor *ep_comp = NULL; endpoint = &conf_desc->usb_interface[i].altsetting[j].endpoint[k]; printf(" endpoint[%d].address: %02X\n", k, endpoint->bEndpointAddress); // Use the first interrupt or bulk IN/OUT endpoints as default for testing if ((endpoint->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) & (LIBUSB_TRANSFER_TYPE_BULK | LIBUSB_TRANSFER_TYPE_INTERRUPT)) { if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { if (!endpoint_in) endpoint_in = endpoint->bEndpointAddress; } else { if (!endpoint_out) endpoint_out = endpoint->bEndpointAddress; } } printf(" max packet size: %04X\n", endpoint->wMaxPacketSize); printf(" polling interval: %02X\n", endpoint->bInterval); libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp); if (ep_comp) { printf(" max burst: %02X (USB 3.0)\n", ep_comp->bMaxBurst); printf(" bytes per interval: %04X (USB 3.0)\n", ep_comp->wBytesPerInterval); libusb_free_ss_endpoint_companion_descriptor(ep_comp); } } } } libusb_free_config_descriptor(conf_desc); libusb_set_auto_detach_kernel_driver(handle, 1); for (iface = 0; iface < nb_ifaces; iface++) { printf("\nClaiming interface %d...\n", iface); r = libusb_claim_interface(handle, iface); if (r != LIBUSB_SUCCESS) { perr(" Failed.\n"); } } printf("\nReading string descriptors:\n"); for (i=0; i<3; i++) { if (string_index[i] == 0) { continue; } if (libusb_get_string_descriptor_ascii(handle, string_index[i], (unsigned char*)string, 128) >= 0) { printf(" String (0x%02X): \"%s\"\n", string_index[i], string); } } // Read the OS String Descriptor if (libusb_get_string_descriptor_ascii(handle, 0xEE, (unsigned char*)string, 128) >= 0) { printf(" String (0x%02X): \"%s\"\n", 0xEE, string); // If this is a Microsoft OS String Descriptor, // attempt to read the WinUSB extended Feature Descriptors if (strncmp(string, "MSFT100", 7) == 0) read_ms_winsub_feature_descriptors(handle, string[7], first_iface); } switch(test_mode) { case USE_PS3: CALL_CHECK(display_ps3_status(handle)); break; case USE_XBOX: CALL_CHECK(display_xbox_status(handle)); CALL_CHECK(set_xbox_actuators(handle, 128, 222)); msleep(2000); CALL_CHECK(set_xbox_actuators(handle, 0, 0)); break; case USE_HID: test_hid(handle, endpoint_in); break; case USE_SCSI: CALL_CHECK(test_mass_storage(handle, endpoint_in, endpoint_out)); case USE_GENERIC: break; } printf("\n"); for (iface = 0; iface<nb_ifaces; iface++) { printf("Releasing interface %d...\n", iface); libusb_release_interface(handle, iface); } printf("Closing device...\n"); libusb_close(handle); return 0; }