libusb10.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. # Copyright (C) 2009-2011 Wander Lairson Costa
  2. #
  3. # The following terms apply to all files associated
  4. # with the software unless explicitly disclaimed in individual files.
  5. #
  6. # The authors hereby grant permission to use, copy, modify, distribute,
  7. # and license this software and its documentation for any purpose, provided
  8. # that existing copyright notices are retained in all copies and that this
  9. # notice is included verbatim in any distributions. No written agreement,
  10. # license, or royalty fee is required for any of the authorized uses.
  11. # Modifications to this software may be copyrighted by their authors
  12. # and need not follow the licensing terms described here, provided that
  13. # the new terms are clearly indicated on the first page of each file where
  14. # they apply.
  15. #
  16. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  17. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  18. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  19. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  20. # POSSIBILITY OF SUCH DAMAGE.
  21. #
  22. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  23. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
  25. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  26. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  27. # MODIFICATIONS.
  28. from ctypes import *
  29. import ctypes.util
  30. import usb.util
  31. import sys
  32. import logging
  33. from usb._debug import methodtrace
  34. import usb._interop as _interop
  35. __author__ = 'Wander Lairson Costa'
  36. __all__ = ['get_backend']
  37. _logger = logging.getLogger('usb.backend.libusb10')
  38. # libusb.h
  39. # return codes
  40. _LIBUSB_SUCCESS = 0
  41. _LIBUSB_ERROR_IO = -1
  42. _LIBUSB_ERROR_INVALID_PARAM = -2
  43. _LIBUSB_ERROR_ACCESS = -3
  44. _LIBUSB_ERROR_NO_DEVICE = -4
  45. _LIBUSB_ERROR_NOT_FOUND = -5
  46. _LIBUSB_ERROR_BUSY = -6
  47. _LIBUSB_ERROR_TIMEOUT = -7
  48. _LIBUSB_ERROR_OVERFLOW = -8
  49. _LIBUSB_ERROR_PIPE = -9
  50. _LIBUSB_ERROR_INTERRUPTED = -10
  51. _LIBUSB_ERROR_NO_MEM = -11
  52. _LIBUSB_ERROR_NOT_SUPPORTED = -12
  53. _LIBUSB_ERROR_OTHER = -99
  54. # map return codes to strings
  55. _str_error = {
  56. _LIBUSB_SUCCESS:'Success (no error)',
  57. _LIBUSB_ERROR_IO:'Input/output error',
  58. _LIBUSB_ERROR_INVALID_PARAM:'Invalid parameter',
  59. _LIBUSB_ERROR_ACCESS:'Access denied (insufficient permissions)',
  60. _LIBUSB_ERROR_NO_DEVICE:'No such device (it may have been disconnected)',
  61. _LIBUSB_ERROR_NOT_FOUND:'Entity not found',
  62. _LIBUSB_ERROR_BUSY:'Resource busy',
  63. _LIBUSB_ERROR_TIMEOUT:'Operation timed out',
  64. _LIBUSB_ERROR_OVERFLOW:'Overflow',
  65. _LIBUSB_ERROR_PIPE:'Pipe error',
  66. _LIBUSB_ERROR_INTERRUPTED:'System call interrupted (perhaps due to signal)',
  67. _LIBUSB_ERROR_NO_MEM:'Insufficient memory',
  68. _LIBUSB_ERROR_NOT_SUPPORTED:'Operation not supported or unimplemented on this platform',
  69. _LIBUSB_ERROR_OTHER:'Unknown error'
  70. }
  71. # Data structures
  72. class _libusb_endpoint_descriptor(Structure):
  73. _fields_ = [('bLength', c_uint8),
  74. ('bDescriptorType', c_uint8),
  75. ('bEndpointAddress', c_uint8),
  76. ('bmAttributes', c_uint8),
  77. ('wMaxPacketSize', c_uint16),
  78. ('bInterval', c_uint8),
  79. ('bRefresh', c_uint8),
  80. ('bSynchAddress', c_uint8),
  81. ('extra', POINTER(c_ubyte)),
  82. ('extra_length', c_int)]
  83. class _libusb_interface_descriptor(Structure):
  84. _fields_ = [('bLength', c_uint8),
  85. ('bDescriptorType', c_uint8),
  86. ('bInterfaceNumber', c_uint8),
  87. ('bAlternateSetting', c_uint8),
  88. ('bNumEndpoints', c_uint8),
  89. ('bInterfaceClass', c_uint8),
  90. ('bInterfaceSubClass', c_uint8),
  91. ('bInterfaceProtocol', c_uint8),
  92. ('iInterface', c_uint8),
  93. ('endpoint', POINTER(_libusb_endpoint_descriptor)),
  94. ('extra', POINTER(c_ubyte)),
  95. ('extra_length', c_int)]
  96. class _libusb_interface(Structure):
  97. _fields_ = [('altsetting', POINTER(_libusb_interface_descriptor)),
  98. ('num_altsetting', c_int)]
  99. class _libusb_config_descriptor(Structure):
  100. _fields_ = [('bLength', c_uint8),
  101. ('bDescriptorType', c_uint8),
  102. ('wTotalLength', c_uint16),
  103. ('bNumInterfaces', c_uint8),
  104. ('bConfigurationValue', c_uint8),
  105. ('iConfiguration', c_uint8),
  106. ('bmAttributes', c_uint8),
  107. ('bMaxPower', c_uint8),
  108. ('interface', POINTER(_libusb_interface)),
  109. ('extra', POINTER(c_ubyte)),
  110. ('extra_length', c_int)]
  111. class _libusb_device_descriptor(Structure):
  112. _fields_ = [('bLength', c_uint8),
  113. ('bDescriptorType', c_uint8),
  114. ('bcdUSB', c_uint16),
  115. ('bDeviceClass', c_uint8),
  116. ('bDeviceSubClass', c_uint8),
  117. ('bDeviceProtocol', c_uint8),
  118. ('bMaxPacketSize0', c_uint8),
  119. ('idVendor', c_uint16),
  120. ('idProduct', c_uint16),
  121. ('bcdDevice', c_uint16),
  122. ('iManufacturer', c_uint8),
  123. ('iProduct', c_uint8),
  124. ('iSerialNumber', c_uint8),
  125. ('bNumConfigurations', c_uint8)]
  126. _lib = None
  127. _init = None
  128. _libusb_device_handle = c_void_p
  129. def _load_library():
  130. candidates = ('usb-1.0', 'libusb-1.0', 'usb')
  131. for candidate in candidates:
  132. libname = ctypes.util.find_library(candidate)
  133. if libname is not None: break
  134. else:
  135. # corner cases
  136. # cygwin predefines library names with 'cyg' instead of 'lib'
  137. if sys.platform == 'cygwin':
  138. try:
  139. return CDLL('cygusb-1.0-0.dll')
  140. except Exception:
  141. _logger.error('Libusb 1.0 could not be loaded in cygwin', exc_info=True)
  142. raise OSError('USB library could not be found')
  143. # Windows backend uses stdcall calling convention
  144. if sys.platform == 'win32':
  145. l = WinDLL(libname)
  146. else:
  147. l = CDLL(libname)
  148. # On FreeBSD 8/9, libusb 1.0 and libusb 0.1 are in the same shared
  149. # object libusb.so, so if we found libusb library name, we must assure
  150. # it is 1.0 version. We just try to get some symbol from 1.0 version
  151. if not hasattr(l, 'libusb_init'):
  152. raise OSError('USB library could not be found')
  153. return l
  154. def _setup_prototypes(lib):
  155. # void libusb_set_debug (libusb_context *ctx, int level)
  156. lib.libusb_set_debug.argtypes = [c_void_p, c_int]
  157. # int libusb_init (libusb_context **context)
  158. lib.libusb_init.argtypes = [POINTER(c_void_p)]
  159. # void libusb_exit (struct libusb_context *ctx)
  160. lib.libusb_exit.argtypes = [c_void_p]
  161. # ssize_t libusb_get_device_list (libusb_context *ctx,
  162. # libusb_device ***list)
  163. lib.libusb_get_device_list.argtypes = [
  164. c_void_p,
  165. POINTER(POINTER(c_void_p))
  166. ]
  167. # void libusb_free_device_list (libusb_device **list,
  168. # int unref_devices)
  169. lib.libusb_free_device_list.argtypes = [
  170. POINTER(c_void_p),
  171. c_int
  172. ]
  173. # libusb_device *libusb_ref_device (libusb_device *dev)
  174. lib.libusb_ref_device.argtypes = [c_void_p]
  175. lib.libusb_ref_device.restype = c_void_p
  176. # void libusb_unref_device(libusb_device *dev)
  177. lib.libusb_unref_device.argtypes = [c_void_p]
  178. # int libusb_open(libusb_device *dev, libusb_device_handle **handle)
  179. lib.libusb_open.argtypes = [c_void_p, POINTER(_libusb_device_handle)]
  180. # void libusb_close(libusb_device_handle *dev_handle)
  181. lib.libusb_close.argtypes = [_libusb_device_handle]
  182. # int libusb_set_configuration(libusb_device_handle *dev,
  183. # int configuration)
  184. lib.libusb_set_configuration.argtypes = [_libusb_device_handle, c_int]
  185. # int libusb_get_configuration(libusb_device_handle *dev,
  186. # int *config)
  187. lib.libusb_get_configuration.argtypes = [_libusb_device_handle, POINTER(c_int)]
  188. # int libusb_claim_interface(libusb_device_handle *dev,
  189. # int interface_number)
  190. lib.libusb_claim_interface.argtypes = [_libusb_device_handle, c_int]
  191. # int libusb_release_interface(libusb_device_handle *dev,
  192. # int interface_number)
  193. lib.libusb_release_interface.argtypes = [_libusb_device_handle, c_int]
  194. # int libusb_set_interface_alt_setting(libusb_device_handle *dev,
  195. # int interface_number,
  196. # int alternate_setting)
  197. lib.libusb_set_interface_alt_setting.argtypes = [
  198. _libusb_device_handle,
  199. c_int,
  200. c_int
  201. ]
  202. # int libusb_reset_device (libusb_device_handle *dev)
  203. lib.libusb_reset_device.argtypes = [_libusb_device_handle]
  204. # int libusb_kernel_driver_active(libusb_device_handle *dev,
  205. # int interface)
  206. lib.libusb_kernel_driver_active.argtypes = [
  207. _libusb_device_handle,
  208. c_int
  209. ]
  210. # int libusb_detach_kernel_driver(libusb_device_handle *dev,
  211. # int interface)
  212. lib.libusb_detach_kernel_driver.argtypes = [
  213. _libusb_device_handle,
  214. c_int
  215. ]
  216. # int libusb_attach_kernel_driver(libusb_device_handle *dev,
  217. # int interface)
  218. lib.libusb_attach_kernel_driver.argtypes = [
  219. _libusb_device_handle,
  220. c_int
  221. ]
  222. # int libusb_get_device_descriptor(
  223. # libusb_device *dev,
  224. # struct libusb_device_descriptor *desc
  225. # )
  226. lib.libusb_get_device_descriptor.argtypes = [
  227. c_void_p,
  228. POINTER(_libusb_device_descriptor)
  229. ]
  230. # int libusb_get_config_descriptor(
  231. # libusb_device *dev,
  232. # uint8_t config_index,
  233. # struct libusb_config_descriptor **config
  234. # )
  235. lib.libusb_get_config_descriptor.argtypes = [
  236. c_void_p,
  237. c_uint8,
  238. POINTER(POINTER(_libusb_config_descriptor))
  239. ]
  240. # void libusb_free_config_descriptor(
  241. # struct libusb_config_descriptor *config
  242. # )
  243. lib.libusb_free_config_descriptor.argtypes = [
  244. POINTER(_libusb_config_descriptor)
  245. ]
  246. # int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
  247. # uint8_t desc_index,
  248. # unsigned char *data,
  249. # int length)
  250. lib.libusb_get_string_descriptor_ascii.argtypes = [
  251. _libusb_device_handle,
  252. c_uint8,
  253. POINTER(c_ubyte),
  254. c_int
  255. ]
  256. # int libusb_control_transfer(libusb_device_handle *dev_handle,
  257. # uint8_t bmRequestType,
  258. # uint8_t bRequest,
  259. # uint16_t wValue,
  260. # uint16_t wIndex,
  261. # unsigned char *data,
  262. # uint16_t wLength,
  263. # unsigned int timeout)
  264. lib.libusb_control_transfer.argtypes = [
  265. _libusb_device_handle,
  266. c_uint8,
  267. c_uint8,
  268. c_uint16,
  269. c_uint16,
  270. POINTER(c_ubyte),
  271. c_uint16,
  272. c_uint
  273. ]
  274. #int libusb_bulk_transfer(
  275. # struct libusb_device_handle *dev_handle,
  276. # unsigned char endpoint,
  277. # unsigned char *data,
  278. # int length,
  279. # int *transferred,
  280. # unsigned int timeout
  281. # )
  282. lib.libusb_bulk_transfer.argtypes = [
  283. _libusb_device_handle,
  284. c_ubyte,
  285. POINTER(c_ubyte),
  286. c_int,
  287. POINTER(c_int),
  288. c_uint
  289. ]
  290. # int libusb_interrupt_transfer(
  291. # libusb_device_handle *dev_handle,
  292. # unsigned char endpoint,
  293. # unsigned char *data,
  294. # int length,
  295. # int *actual_length,
  296. # unsigned int timeout
  297. # );
  298. lib.libusb_interrupt_transfer.argtypes = [
  299. _libusb_device_handle,
  300. c_ubyte,
  301. POINTER(c_ubyte),
  302. c_int,
  303. POINTER(c_int),
  304. c_uint
  305. ]
  306. # check a libusb function call
  307. def _check(retval):
  308. if isinstance(retval, int):
  309. retval = c_int(retval)
  310. if isinstance(retval, c_int):
  311. if retval.value < 0:
  312. from usb.core import USBError
  313. raise USBError(_str_error[retval.value])
  314. return retval
  315. # wrap a device
  316. class _Device(object):
  317. def __init__(self, devid):
  318. self.devid = _lib.libusb_ref_device(devid)
  319. def __del__(self):
  320. _lib.libusb_unref_device(self.devid)
  321. # wrap a descriptor and keep a reference to another object
  322. # Thanks to Thomas Reitmayr.
  323. class _WrapDescriptor(object):
  324. def __init__(self, desc, obj = None):
  325. self.obj = obj
  326. self.desc = desc
  327. def __getattr__(self, name):
  328. return getattr(self.desc, name)
  329. # wrap a configuration descriptor
  330. class _ConfigDescriptor(object):
  331. def __init__(self, desc):
  332. self.desc = desc
  333. def __del__(self):
  334. _lib.libusb_free_config_descriptor(self.desc)
  335. def __getattr__(self, name):
  336. return getattr(self.desc.contents, name)
  337. # initialize and finalize the library
  338. class _Initializer(object):
  339. def __init__(self):
  340. _check(_lib.libusb_init(None))
  341. def __del__(self):
  342. _lib.libusb_exit(None)
  343. # iterator for libusb devices
  344. class _DevIterator(object):
  345. def __init__(self):
  346. self.dev_list = POINTER(c_void_p)()
  347. self.num_devs = _check(_lib.libusb_get_device_list(
  348. None,
  349. byref(self.dev_list))
  350. ).value
  351. def __iter__(self):
  352. for i in range(self.num_devs):
  353. yield _Device(self.dev_list[i])
  354. def __del__(self):
  355. _lib.libusb_free_device_list(self.dev_list, 1)
  356. # implementation of libusb 1.0 backend
  357. class _LibUSB(usb.backend.IBackend):
  358. @methodtrace(_logger)
  359. def enumerate_devices(self):
  360. return _DevIterator()
  361. @methodtrace(_logger)
  362. def get_device_descriptor(self, dev):
  363. dev_desc = _libusb_device_descriptor()
  364. _check(_lib.libusb_get_device_descriptor(dev.devid, byref(dev_desc)))
  365. return dev_desc
  366. @methodtrace(_logger)
  367. def get_configuration_descriptor(self, dev, config):
  368. cfg = POINTER(_libusb_config_descriptor)()
  369. _check(_lib.libusb_get_config_descriptor(dev.devid,
  370. config, byref(cfg)))
  371. return _ConfigDescriptor(cfg)
  372. @methodtrace(_logger)
  373. def get_interface_descriptor(self, dev, intf, alt, config):
  374. cfg = self.get_configuration_descriptor(dev, config)
  375. if intf >= cfg.bNumInterfaces:
  376. raise IndexError('Invalid interface index ' + str(intf))
  377. i = cfg.interface[intf]
  378. if alt >= i.num_altsetting:
  379. raise IndexError('Invalid alternate setting index ' + str(alt))
  380. return _WrapDescriptor(i.altsetting[alt], cfg)
  381. @methodtrace(_logger)
  382. def get_endpoint_descriptor(self, dev, ep, intf, alt, config):
  383. i = self.get_interface_descriptor(dev, intf, alt, config)
  384. if ep > i.bNumEndpoints:
  385. raise IndexError('Invalid endpoint index ' + str(ep))
  386. return _WrapDescriptor(i.endpoint[ep], i)
  387. @methodtrace(_logger)
  388. def open_device(self, dev):
  389. handle = _libusb_device_handle()
  390. _check(_lib.libusb_open(dev.devid, byref(handle)))
  391. return handle
  392. @methodtrace(_logger)
  393. def close_device(self, dev_handle):
  394. _lib.libusb_close(dev_handle)
  395. @methodtrace(_logger)
  396. def set_configuration(self, dev_handle, config_value):
  397. _check(_lib.libusb_set_configuration(dev_handle, config_value))
  398. @methodtrace(_logger)
  399. def get_configuration(self, dev_handle):
  400. config = c_int()
  401. _check(_lib.libusb_get_configuration(dev_handle, byref(config)))
  402. return config.value
  403. @methodtrace(_logger)
  404. def set_interface_altsetting(self, dev_handle, intf, altsetting):
  405. _check(_lib.libusb_set_interface_alt_setting(dev_handle,
  406. intf,
  407. altsetting))
  408. @methodtrace(_logger)
  409. def claim_interface(self, dev_handle, intf):
  410. _check(_lib.libusb_claim_interface(dev_handle, intf))
  411. @methodtrace(_logger)
  412. def release_interface(self, dev_handle, intf):
  413. _check(_lib.libusb_release_interface(dev_handle, intf))
  414. @methodtrace(_logger)
  415. def bulk_write(self, dev_handle, ep, intf, data, timeout):
  416. return self.__write(_lib.libusb_bulk_transfer,
  417. dev_handle,
  418. ep,
  419. intf,
  420. data,
  421. timeout)
  422. @methodtrace(_logger)
  423. def bulk_read(self, dev_handle, ep, intf, size, timeout):
  424. return self.__read(_lib.libusb_bulk_transfer,
  425. dev_handle,
  426. ep,
  427. intf,
  428. size,
  429. timeout)
  430. @methodtrace(_logger)
  431. def intr_write(self, dev_handle, ep, intf, data, timeout):
  432. return self.__write(_lib.libusb_interrupt_transfer,
  433. dev_handle,
  434. ep,
  435. intf,
  436. data,
  437. timeout)
  438. @methodtrace(_logger)
  439. def intr_read(self, dev_handle, ep, intf, size, timeout):
  440. return self.__read(_lib.libusb_interrupt_transfer,
  441. dev_handle,
  442. ep,
  443. intf,
  444. size,
  445. timeout)
  446. # TODO: implement isochronous
  447. # @methodtrace(_logger)
  448. # def iso_write(self, dev_handle, ep, intf, data, timeout):
  449. # pass
  450. # @methodtrace(_logger)
  451. # def iso_read(self, dev_handle, ep, intf, size, timeout):
  452. # pass
  453. @methodtrace(_logger)
  454. def ctrl_transfer(self,
  455. dev_handle,
  456. bmRequestType,
  457. bRequest,
  458. wValue,
  459. wIndex,
  460. data_or_wLength,
  461. timeout):
  462. if usb.util.ctrl_direction(bmRequestType) == usb.util.CTRL_OUT:
  463. buff = data_or_wLength
  464. else:
  465. buff = _interop.as_array((0,) * data_or_wLength)
  466. addr, length = buff.buffer_info()
  467. length *= buff.itemsize
  468. ret = _check(_lib.libusb_control_transfer(dev_handle,
  469. bmRequestType,
  470. bRequest,
  471. wValue,
  472. wIndex,
  473. cast(addr,
  474. POINTER(c_ubyte)),
  475. length,
  476. timeout))
  477. if usb.util.ctrl_direction(bmRequestType) == usb.util.CTRL_OUT:
  478. return ret.value
  479. else:
  480. return buff[:ret.value]
  481. @methodtrace(_logger)
  482. def reset_device(self, dev_handle):
  483. _check(_lib.libusb_reset_device(dev_handle))
  484. @methodtrace(_logger)
  485. def is_kernel_driver_active(self, dev_handle, intf):
  486. return bool(_check(_lib.libusb_kernel_driver_active(dev_handle, intf)))
  487. @methodtrace(_logger)
  488. def detach_kernel_driver(self, dev_handle, intf):
  489. _check(_lib.libusb_detach_kernel_driver(dev_handle, intf))
  490. @methodtrace(_logger)
  491. def attach_kernel_driver(self, dev_handle, intf):
  492. _check(_lib.libusb_attach_kernel_driver(dev_handle, intf))
  493. def __write(self, fn, dev_handle, ep, intf, data, timeout):
  494. address, length = data.buffer_info()
  495. length *= data.itemsize
  496. transferred = c_int()
  497. _check(fn(dev_handle,
  498. ep,
  499. cast(address, POINTER(c_ubyte)),
  500. length,
  501. byref(transferred),
  502. timeout))
  503. return transferred.value
  504. def __read(self, fn, dev_handle, ep, intf, size, timeout):
  505. data = _interop.as_array((0,) * size)
  506. address, length = data.buffer_info()
  507. length *= data.itemsize
  508. transferred = c_int()
  509. _check(fn(dev_handle,
  510. ep,
  511. cast(address, POINTER(c_ubyte)),
  512. length,
  513. byref(transferred),
  514. timeout))
  515. return data[:transferred.value]
  516. def get_backend():
  517. global _lib, _init
  518. try:
  519. if _lib is None:
  520. _lib = _load_library()
  521. _setup_prototypes(_lib)
  522. _init = _Initializer()
  523. return _LibUSB()
  524. except Exception:
  525. _logger.error('Error loading libusb 1.0 backend', exc_info=True)
  526. return None