def _find_devices(self): """ Find all attached USB devices that match the filter. """ self.error = False self.device_count = 0 self._usb_devices = list() # Get device list from libusb. dev_list = ct.POINTER(ct.POINTER(usb.device))() cnt = usb.get_device_list(None, ct.byref(dev_list)) if cnt < 0: usb_error(cnt, _('Failed to find attached USB devices.')) self.error = True return _logger.debug(_('Vendor filters: ') + ', '.join(self._filters)) _logger.debug(_('Searching for Ultimarc USB devices...')) for dev in dev_list: if not dev: # We must always look for the Null device and quit the loop. break dev_desc = usb.device_descriptor() ret = usb.get_device_descriptor(dev, ct.byref(dev_desc)) if ret != usb.LIBUSB_SUCCESS: usb_error(ret, _('failed to get USB device descriptor.')) continue if not self._filters or f'{dev_desc.idVendor:04x}' in self._filters: ud = USBDeviceInfo(dev, dev_desc) self._usb_devices.append(ud) self.device_count += 1 _logger.debug(_('Device search complete.')) usb.free_device_list(dev_list, 1)
def test_many_device_lists(tctx): # Tests that 100 concurrent device lists can be open at a time. LIST_COUNT = 100 ctx = ct.POINTER(usb.context)() r = usb.init(ct.byref(ctx)) if r != usb.LIBUSB_SUCCESS: testlib.logf(tctx, "Failed to init libusb: {:d}", r) return test_result.TEST_STATUS_FAILURE device_lists = (ct.POINTER(ct.POINTER(usb.device)) * LIST_COUNT)() # Create the 100 device lists. for i in range(LIST_COUNT): list_size = usb.get_device_list(ctx, ct.byref(device_lists[i])) if list_size < 0 or not device_lists[i]: testlib.logf(tctx, "Failed to get device list on iteration " "{:d}: {:d} ({:#x})", i, -list_size, device_lists[i]) return test_result.TEST_STATUS_FAILURE # Destroy the 100 device lists. for i in range(LIST_COUNT): if device_lists[i]: usb.free_device_list(device_lists[i], 1) device_lists[i] = None usb.exit(ctx) return test_result.TEST_STATUS_SUCCESS
def main(argv=sys.argv): global verbose if len(argv) > 1 and argv[1] == "-v": verbose = True r = usb.init(None) if r < 0: return r try: devs = ct.POINTER(ct.POINTER(usb.device))() cnt = usb.get_device_list(None, ct.byref(devs)) if cnt < 0: return cnt i = 0 while devs[i]: print_device(devs[i], 0) i += 1 usb.free_device_list(devs, 1) finally: usb.exit(None) return 0
def main(argv=sys.argv): r = usb.init(None) if r < 0: return r try: devs = ct.POINTER(ct.POINTER(usb.device))() cnt = usb.get_device_list(None, ct.byref(devs)) if cnt < 0: return cnt print_devs(devs) usb.free_device_list(devs, 1) finally: usb.exit(None) return 0
def test_get_device_list(tctx: test_ctx) -> int: """Tests that devices can be listed 1000 times.""" ctx = ct.POINTER(usb.context)() r = usb.init(ct.byref(ctx)) if r != usb.LIBUSB_SUCCESS: testlib.logf(tctx, "Failed to init libusb: {:d}", r) return test_result.TEST_STATUS_FAILURE for i in range(1000): device_list = ct.POINTER(ct.POINTER(usb.device))() list_size = usb.get_device_list(ctx, ct.byref(device_list)) if list_size < 0 or not device_list: testlib.logf(tctx, "Failed to get device list on iteration " "{:d}: {:d} ({:#x})", i, -list_size, device_list) return test_result.TEST_STATUS_FAILURE usb.free_device_list(device_list, 1) usb.exit(ctx) return test_result.TEST_STATUS_SUCCESS
def _close_device_list_handle(self): """ Close the device list handle if set. """ if self.__dev_list__: _logger.debug(_('Freeing USB device list.')) usb.free_device_list(self.__dev_list__, 1) self.__dev_list__ = None
def main(argv=sys.argv): global verbose FIRMWARE = 0 LOADER = 1 known_devices = FX_KNOWN_DEVICES paths = [None, None] # [const char*] device_id = None # const char* device_path = os.environ.get("DEVICE", None) target_type = None # const char* fx_names = FX_TYPE_NAMES img_names = IMG_TYPE_NAMES fx_type = FX_TYPE_UNDEFINED # int img_types = [0] * len(paths) # [int] #opt; # int #status; # int #ext; # const char* #i, j; # unsigned int vid = 0 # unsigned pid = 0 # unsigned busnum = 0 # unsigned devaddr = 0 # unsigned #_busnum; # unsigned #_devaddr; # unsigned dev = ct.POINTER(usb.device)() devs = ct.POINTER(ct.POINTER(usb.device))() device = ct.POINTER(usb.device_handle)() desc = usb.device_descriptor() try: opts, args = getopt.getopt(argv[1:], "qvV?hd:p:i:I:s:S:t:") except getopt.GetoptError: return print_usage(-1) for opt, optarg in opts: if opt == "-d": device_id = optarg if sscanf(device_id, "%x:%x", ct.byref(vid), ct.byref(pid)) != 2: print( "please specify VID & PID as \"vid:pid\" in hexadecimal format", file=sys.stderr) return -1 elif opt == "-p": device_path = optarg if sscanf(device_path, "%u,%u", ct.byref(busnum), ct.byref(devaddr)) != 2: print( "please specify bus number & device number as \"bus,dev\" in decimal format", file=sys.stderr) return -1 elif opt in ("-i", "-I"): paths[FIRMWARE] = optarg elif opt in ("-s", "-S"): paths[LOADER] = optarg elif opt == "-V": print(FXLOAD_VERSION) return 0 elif opt == "-t": target_type = optarg elif opt == "-v": verbose += 1 elif opt == "-q": verbose -= 1 elif opt in ("-?", "-h"): return print_usage(-1) else: return print_usage(-1) if paths[FIRMWARE] is None: logerror("no firmware specified!\n") return print_usage(-1) if device_id is not None and device_path is not None: logerror("only one of -d or -p can be specified\n") return print_usage(-1) # determine the target type if target_type is not None: for i in range(FX_TYPE_MAX): if fx_names[i] == target_type: fx_type = i break else: logerror("illegal microcontroller type: {}\n", target_type) return print_usage(-1) # open the device using libusb status = usb.init(None) if status < 0: logerror("usb.init() failed: {}\n", usb.error_name(status)) return -1 try: usb.set_option(None, usb.LIBUSB_OPTION_LOG_LEVEL, verbose) # try to pick up missing parameters from known devices if target_type is None or device_id is None or device_path is not None: if usb.get_device_list(None, ct.byref(devs)) < 0: logerror("libusb.get_device_list() failed: {}\n", usb.error_name(status)) return -1 i = 0 while True: dev = devs[i] if not dev: usb.free_device_list(devs, 1) logerror( "could not find a known device - please specify type and/or vid:pid and/or bus,dev\n" ) return print_usage(-1) _busnum = usb.get_bus_number(dev) _devaddr = usb.get_device_address(dev) if target_type is not None and device_path is not None: # if both a type and bus,addr were specified, we just need to find our match if (usb.get_bus_number(dev) == busnum and usb.get_device_address(dev) == devaddr): break else: status = usb.get_device_descriptor(dev, ct.byref(desc)) if status >= 0: if verbose >= 3: logerror("examining {:04x}:{:04x} ({},{})\n", desc.idVendor, desc.idProduct, _busnum, _devaddr) if_break = False for known_device in known_devices: if (desc.idVendor == known_device.vid and desc.idProduct == known_device.pid): if ( # nothing was specified (target_type is None and device_id is None and device_path is None) or # vid:pid was specified and we have a match (target_type is None and device_id is not None and vid == desc.idVendor and pid == desc.idProduct) or # bus,addr was specified and we have a match (target_type is None and device_path is not None and busnum == _busnum and devaddr == _devaddr) or # type was specified and we have a match (target_type is not None and device_id is None and device_path is None and fx_type == known_device.type)): fx_type = known_device.type vid = desc.idVendor pid = desc.idProduct busnum = _busnum devaddr = _devaddr if_break = True break if if_break: if verbose: logerror( "found device '{}' [{:04x}:{:04x}] ({},{})\n", known_device.designation, vid, pid, busnum, devaddr) break i += 1 status = usb.open(dev, ct.byref(device)) usb.free_device_list(devs, 1) if status < 0: logerror("usb.open() failed: {}\n", usb.error_name(status)) return -1 elif device_id is not None: device = usb.open_device_with_vid_pid(None, ct.c_uint16(vid), ct.c_uint16(pid)) if not device: logerror("usb.open() failed\n") return -1 # We need to claim the first interface usb.set_auto_detach_kernel_driver(device, 1) status = usb.claim_interface(device, 0) if status != usb.LIBUSB_SUCCESS: usb.close(device) logerror("libusb.claim_interface failed: {}\n", usb.error_name(status)) return -1 if verbose: logerror("microcontroller type: {}\n", fx_names[fx_type]) for i, path in enumerate(paths): if path is not None: ext = path[-4:] if ext.lower() == ".hex" or ext == ".ihx": img_types[i] = IMG_TYPE_HEX elif ext.lower() == ".iic": img_types[i] = IMG_TYPE_IIC elif ext.lower() == ".bix": img_types[i] = IMG_TYPE_BIX elif ext.lower() == ".img": img_types[i] = IMG_TYPE_IMG else: logerror("{} is not a recognized image type\n", path) return -1 if verbose and path is not None: logerror("{}: type {}\n", path, img_names[img_types[i]]) if paths[LOADER] is None: # single stage, put into internal memory if verbose > 1: logerror("single stage: load on-chip memory\n") status = ezusb_load_ram(device, paths[FIRMWARE], fx_type, img_types[FIRMWARE], 0) else: # two-stage, put loader into internal memory if verbose > 1: logerror("1st stage: load 2nd stage loader\n") status = ezusb_load_ram(device, paths[LOADER], fx_type, img_types[LOADER], 0) if status == 0: # two-stage, put firmware into internal memory if verbose > 1: logerror("2nd state: load on-chip memory\n") status = ezusb_load_ram(device, paths[FIRMWARE], fx_type, img_types[FIRMWARE], 1) usb.release_interface(device, 0) usb.close(device) finally: usb.exit(None) return status