__init__.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. r"""usb.backend - Backend interface.
  29. This module exports:
  30. IBackend - backend interface.
  31. Backends are Python objects which implement the IBackend interface.
  32. The easiest way to do so is inherinting from IBackend.
  33. PyUSB already provides backends for libusb versions 0.1 and 1.0,
  34. and OpenUSB library. Backends modules included with PyUSB are required to
  35. export the get_backend() function, which returns an instance of a backend
  36. object. You can provide your own customized backend if you
  37. want to. Bellow you find a skeleton of a backend implementation module:
  38. import usb.backend
  39. class MyBackend(usb.backend.IBackend):
  40. pass
  41. def get_backend():
  42. return MyBackend()
  43. You can use your customized backend by passing it as the backend parameter of the
  44. usb.core.find() function. For example:
  45. import custom_backend
  46. import usb.core
  47. myidVendor = 0xfffe
  48. myidProduct = 0x0001
  49. mybackend = custom_backend.get_backend()
  50. dev = usb.core.find(backend = mybackend, idProduct=myidProduct,
  51. idVendor=myidVendor)
  52. For custom backends, you are not required to supply the get_backend() function,
  53. since the application code will instantiate the backend.
  54. If you do not provide a backend to the find() function, it will use one of the
  55. defaults backend according to its internal rules. For details, consult the
  56. find() function documentation.
  57. """
  58. __author__ = 'Wander Lairson Costa'
  59. __all__ = ['IBackend', 'libusb01', 'libusb10', 'openusb']
  60. def _not_implemented(func):
  61. raise NotImplementedError(func.__name__)
  62. class IBackend(object):
  63. r"""Backend interface.
  64. IBackend is the basic interface for backend implementations. By default,
  65. the methods of the interface raise a NotImplementedError exception. A
  66. backend implementation should replace the methods to provide the funcionality
  67. necessary.
  68. As Python is a dynamic typed language, you are not obligated to inherit from
  69. IBackend: everything that bahaves like an IBackend is an IBackend. But you
  70. are strongly recommended to do so, inheriting from IBackend provides consistent
  71. default behavior.
  72. """
  73. def enumerate_devices(self):
  74. r"""This function is required to return an iterable object which
  75. yields an implementation defined device identification for each
  76. USB device found in the system.
  77. The device identification object is used as argument to other methods
  78. of the interface.
  79. """
  80. _not_implemented(self.enumerate_devices)
  81. def get_device_descriptor(self, dev):
  82. r"""Return the device descriptor of the given device.
  83. The object returned is required to have all the Device Descriptor
  84. fields accessible as member variables. They must be convertible (but
  85. not required to be equal) to the int type.
  86. dev is an object yielded by the iterator returned by the enumerate_devices()
  87. method.
  88. """
  89. _not_implemented(self.get_device_descriptor)
  90. def get_configuration_descriptor(self, dev, config):
  91. r"""Return a configuration descriptor of the given device.
  92. The object returned is required to have all the Configuration Descriptor
  93. fields acessible as member variables. They must be convertible (but
  94. not required to be equal) to the int type.
  95. The dev parameter is the already described device identification object.
  96. config is the logical index of the configuration (not the bConfigurationValue
  97. field). By "logical index" we mean the relative order of the configurations
  98. returned by the peripheral as a result of GET_DESCRIPTOR request.
  99. """
  100. _not_implemented(self.get_configuration_descriptor)
  101. def get_interface_descriptor(self, dev, intf, alt, config):
  102. r"""Return an interface descriptor of the given device.
  103. The object returned is required to have all the Interface Descriptor
  104. fields accessible as member variables. They must be convertible (but
  105. not required to be equal) to the int type.
  106. The dev parameter is the already described device identification object.
  107. The intf parameter is the interface logical index (not the bInterfaceNumber field)
  108. and alt is the alternate setting logical index (not the bAlternateSetting value).
  109. Not every interface has more than one alternate setting. In this case, the alt
  110. parameter should be zero. config is the configuration logical index (not the
  111. bConfigurationValue field).
  112. """
  113. _not_implemented(self.get_interface_descriptor)
  114. def get_endpoint_descriptor(self, dev, ep, intf, alt, config):
  115. r"""Return an endpoint descriptor of the given device.
  116. The object returned is required to have all the Endpoint Descriptor
  117. fields acessible as member variables. They must be convertible (but
  118. not required to be equal) to the int type.
  119. The ep parameter is the endpoint logical index (not the bEndpointAddress
  120. field) of the endpoint descriptor desired. intf, alt and config are the same
  121. values already described in the get_interface_descriptor() method.
  122. """
  123. _not_implemented(self.get_endpoint_descriptor)
  124. def open_device(self, dev):
  125. r"""Open the device for data exchange.
  126. This method opens the device identified by the dev parameter for communication.
  127. This method must be called before calling any communication related method, such
  128. as transfer methods.
  129. It returns a handle identifying the communication instance. This handle must be
  130. passed to the communication methods.
  131. """
  132. _not_implemented(self.open_device)
  133. def close_device(self, dev_handle):
  134. r"""Close the device handle.
  135. This method closes the device communication channel and releases any
  136. system resources related to it.
  137. """
  138. _not_implemented(self.close_device)
  139. def set_configuration(self, dev_handle, config_value):
  140. r"""Set the active device configuration.
  141. This method should be called to set the active configuration
  142. of the device. The dev_handle parameter is the value returned
  143. by the open_device() method and the config_value parameter is the
  144. bConfigurationValue field of the related configuration descriptor.
  145. """
  146. _not_implemented(self.set_configuration)
  147. def get_configuration(self, dev_handle):
  148. r"""Get the current active device configuration.
  149. This method returns the bConfigurationValue of the currently
  150. active configuration. Depending on the backend and the OS,
  151. either a cached value may be returned or a control request may
  152. be issued. The dev_handle parameter is the value returned by
  153. the open_device method.
  154. """
  155. _not_implemented(self.get_configuration)
  156. def set_interface_altsetting(self, dev_handle, intf, altsetting):
  157. r"""Set the interface alternate setting.
  158. This method should only be called when the interface has more than
  159. one alternate setting. The dev_handle is the value returned by the
  160. open_device() method. intf and altsetting are respectivelly the
  161. bInterfaceNumber and bAlternateSetting fields of the related interface.
  162. """
  163. _not_implemented(self.set_interface_altsetting)
  164. def claim_interface(self, dev_handle, intf):
  165. r"""Claim the given interface.
  166. Interface claiming is not related to USB spec itself, but it is
  167. generally an necessary call of the USB libraries. It requests exclusive
  168. access to the interface on the system. This method must be called
  169. before using one of the transfer methods.
  170. dev_handle is the value returned by the open_device() method and
  171. intf is the bInterfaceNumber field of the desired interface.
  172. """
  173. _not_implemented(self.claim_interface)
  174. def release_interface(self, dev_handle, intf):
  175. r"""Release the claimed interface.
  176. dev_handle and intf are the same parameters of the claim_interface
  177. method.
  178. """
  179. _not_implemented(self.release_interface)
  180. def bulk_write(self, dev_handle, ep, intf, data, timeout):
  181. r"""Perform a bulk write.
  182. dev_handle is the value returned by the open_device() method.
  183. The ep parameter is the bEndpointAddress field whose endpoint
  184. the data will be sent to. intf is the bInterfaceNumber field
  185. of the interface containing the endpoint. The data parameter
  186. is the data to be sent. It must be an instance of the array.array
  187. class. The timeout parameter specifies a time limit to the operation
  188. in miliseconds.
  189. The method returns the number of bytes written.
  190. """
  191. _not_implemented(self.bulk_write)
  192. def bulk_read(self, dev_handle, ep, intf, size, timeout):
  193. r"""Perform a bulk read.
  194. dev_handle is the value returned by the open_device() method.
  195. The ep parameter is the bEndpointAddress field whose endpoint
  196. the data will be received from. intf is the bInterfaceNumber field
  197. of the interface containing the endpoint. The size parameter
  198. is the number of bytes to be read. The timeout parameter specifies
  199. a time limit to the operation in miliseconds.
  200. The method returns an array.array object containing the data read.
  201. """
  202. _not_implemented(self.bulk_read)
  203. def intr_write(self, dev_handle, ep, intf, data, timeout):
  204. r"""Perform an interrupt write.
  205. dev_handle is the value returned by the open_device() method.
  206. The ep parameter is the bEndpointAddress field whose endpoint
  207. the data will be sent to. intf is the bInterfaceNumber field
  208. of the interface containing the endpoint. The data parameter
  209. is the data to be sent. It must be an instance of the array.array
  210. class. The timeout parameter specifies a time limit to the operation
  211. in miliseconds.
  212. The method returns the number of bytes written.
  213. """
  214. _not_implemented(self.intr_write)
  215. def intr_read(self, dev_handle, ep, intf, size, timeout):
  216. r"""Perform an interrut read.
  217. dev_handle is the value returned by the open_device() method.
  218. The ep parameter is the bEndpointAddress field whose endpoint
  219. the data will be received from. intf is the bInterfaceNumber field
  220. of the interface containing the endpoint. The size parameter
  221. is the number of bytes to be read. The timeout parameter specifies
  222. a time limit to the operation in miliseconds.
  223. The method returns an array.array object containing the data read.
  224. """
  225. _not_implemented(self.intr_read)
  226. def iso_write(self, dev_handle, ep, intf, data, timeout):
  227. r"""Perform an isochronous write.
  228. dev_handle is the value returned by the open_device() method.
  229. The ep parameter is the bEndpointAddress field whose endpoint
  230. the data will be sent to. intf is the bInterfaceNumber field
  231. of the interface containing the endpoint. The data parameter
  232. is the data to be sent.It must be an instance of the array.array
  233. class. The timeout parameter specifies a time limit to the operation
  234. in miliseconds.
  235. The method returns the number of bytes written.
  236. """
  237. _not_implemented(self.iso_write)
  238. def iso_read(self, dev_handle, ep, intf, size, timeout):
  239. r"""Perform an isochronous read.
  240. dev_handle is the value returned by the open_device() method.
  241. The ep parameter is the bEndpointAddress field whose endpoint
  242. the data will be received from. intf is the bInterfaceNumber field
  243. of the interface containing the endpoint. The size parameter
  244. is the number of bytes to be read. The timeout parameter specifies
  245. a time limit to the operation in miliseconds.
  246. The method returns an array.array object containing the data read.
  247. """
  248. _not_implemented(self.iso_read)
  249. def ctrl_transfer(self,
  250. dev_handle,
  251. bmRequestType,
  252. bRequest,
  253. wValue,
  254. wIndex,
  255. data_or_wLength,
  256. timeout):
  257. r"""Perform a control transfer on the endpoint 0.
  258. The direction of the transfer is inferred from the bmRequestType
  259. field of the setup packet.
  260. dev_handle is the value returned by the open_device() method.
  261. bmRequestType, bRequest, wValue and wIndex are the same fields
  262. of the setup packet. data_or_wLength is either the payload to be sent
  263. to the device, if any, as an array.array object (None there is no
  264. payload) for OUT requests in the data stage or the wLength field
  265. specifying the number of bytes to read for IN requests in the data
  266. stage. The timeout parameter specifies a time limit to the operation
  267. in miliseconds.
  268. Return the number of bytes written (for OUT transfers) or the data
  269. read (for IN transfers), as an array.array object.
  270. """
  271. _not_implemented(self.ctrl_transfer)
  272. def reset_device(self, dev_handle):
  273. r"""Reset the device."""
  274. _not_implemented(self.reset_device)
  275. def is_kernel_driver_active(self, dev_handle, intf):
  276. r"""Determine if a kernel driver is active on an interface.
  277. If a kernel driver is active, you cannot claim the interface,
  278. and the backend will be unable to perform I/O.
  279. """
  280. _not_implemented(self.is_kernel_driver_active)
  281. def detach_kernel_driver(self, dev_handle, intf):
  282. r"""Detach a kernel driver from an interface.
  283. If successful, you will then be able to claim the interface
  284. and perform I/O.
  285. """
  286. _not_implemented(self.detach_kernel_driver)
  287. def attach_kernel_driver(self, dev_handle, intf):
  288. r"""Re-attach an interface's kernel driver, which was previously
  289. detached using detach_kernel_driver()."""
  290. _not_implemented(self.attach_kernel_driver)