control.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.control - USB standard control requests
  29. This module exports:
  30. get_status - get recipeint status
  31. clear_feature - clear a recipient feature
  32. set_feature - set a recipient feature
  33. get_descriptor - get a device descriptor
  34. set_descriptor - set a device descriptor
  35. get_configuration - get a device configuration
  36. set_configuration - set a device configuration
  37. get_interface - get a device interface
  38. set_interface - set a device interface
  39. """
  40. __author__ = 'Wander Lairson Costa'
  41. __all__ = ['get_status',
  42. 'clear_feature',
  43. 'set_feature',
  44. 'get_descriptor',
  45. 'set_descriptor',
  46. 'get_configuration',
  47. 'set_configuration',
  48. 'get_interface',
  49. 'set_interface',
  50. 'ENDPOINT_HALT',
  51. 'FUNCTION_SUSPEND',
  52. 'DEVICE_REMOTE_WAKEUP',
  53. 'U1_ENABLE',
  54. 'U2_ENABLE',
  55. 'LTM_ENABLE']
  56. import usb.util as util
  57. import usb.core as core
  58. def _parse_recipient(recipient, direction):
  59. if recipient is None:
  60. r = util.CTRL_RECIPIENT_DEVICE
  61. wIndex = 0
  62. elif isinstance(recipient, core.Interface):
  63. r = util.CTRL_RECIPIENT_INTERFACE
  64. wIndex = recipient.bInterfaceNumber
  65. elif isinstance(recipient, core.Endpoint):
  66. r = util.CTRL_RECIPIENT_ENDPOINT
  67. wIndex = recipient.bEndpointAddress
  68. else:
  69. raise ValueError('Invalid recipient.')
  70. bmRequestType = util.build_request_type(
  71. direction,
  72. util.CTRL_TYPE_STANDARD,
  73. r
  74. )
  75. return (bmRequestType, wIndex)
  76. # standard feature selectors from USB 2.0/3.0
  77. ENDPOINT_HALT = 0
  78. FUNCTION_SUSPEND = 0
  79. DEVICE_REMOTE_WAKEUP = 1
  80. U1_ENABLE = 48
  81. U2_ENABLE = 49
  82. LTM_ENABLE = 50
  83. def get_status(dev, recipient = None):
  84. r"""Return the status for the specified recipient.
  85. dev is the Device object to which the request will be
  86. sent to.
  87. The recipient can be None (on which the status will be queried
  88. on the device), an Interface or Endpoint descriptors.
  89. The status value is returned as an integer with the lower
  90. word being the two bytes status value.
  91. """
  92. bmRequestType, wIndex = _parse_recipient(recipient, util.CTRL_IN)
  93. ret = dev.ctrl_transfer(bmRequestType = bmRequestType,
  94. bRequest = 0x00,
  95. wIndex = wIndex,
  96. data_or_wLength = 2)
  97. return ret[0] | (ret[1] << 8)
  98. def clear_feature(dev, feature, recipient = None):
  99. r"""Clear/disable a specific feature.
  100. dev is the Device object to which the request will be
  101. sent to.
  102. feature is the feature you want to disable.
  103. The recipient can be None (on which the status will be queried
  104. on the device), an Interface or Endpoint descriptors.
  105. """
  106. bmRequestType, wIndex = _parse_recipient(recipient, util.CTRL_OUT)
  107. dev.ctrl_transfer(bmRequestType = bmRequestType,
  108. bRequest = 0x01,
  109. wIndex = wIndex,
  110. wValue = feature)
  111. def set_feature(dev, feature, recipient = None):
  112. r"""Set/enable a specific feature.
  113. dev is the Device object to which the request will be
  114. sent to.
  115. feature is the feature you want to enable.
  116. The recipient can be None (on which the status will be queried
  117. on the device), an Interface or Endpoint descriptors.
  118. """
  119. bmRequestType, wIndex = _parse_recipient(recipient, util.CTRL_OUT)
  120. dev.ctrl_transfer(bmRequestType = bmRequestType,
  121. bRequest = 0x03,
  122. wIndex = wIndex,
  123. wValue = feature)
  124. def get_descriptor(dev, desc_size, desc_type, desc_index, wIndex = 0):
  125. r"""Return the specified descriptor.
  126. dev is the Device object to which the request will be
  127. sent to.
  128. desc_size is the descriptor size.
  129. desc_type and desc_index are the descriptor type and index,
  130. respectively. wIndex index is used for string descriptors
  131. and represents the Language ID. For other types of descriptors,
  132. it is zero.
  133. """
  134. wValue = desc_index | (desc_type << 8)
  135. bmRequestType = util.build_request_type(
  136. util.CTRL_IN,
  137. util.CTRL_TYPE_STANDARD,
  138. util.CTRL_RECIPIENT_DEVICE
  139. )
  140. return dev.ctrl_transfer(
  141. bmRequestType = bmRequestType,
  142. bRequest = 0x06,
  143. wValue = wValue,
  144. wIndex = wIndex,
  145. data_or_wLength = desc_size
  146. )
  147. def set_descriptor(dev, desc, desc_type, desc_index, wIndex = None):
  148. r"""Update an existing descriptor or add a new one.
  149. dev is the Device object to which the request will be
  150. sent to.
  151. The desc parameter is the descriptor to be sent to the device.
  152. desc_type and desc_index are the descriptor type and index,
  153. respectively. wIndex index is used for string descriptors
  154. and represents the Language ID. For other types of descriptors,
  155. it is zero.
  156. """
  157. wValue = desc_index | (desc_type << 8)
  158. bmRequestType = util.build_request_type(
  159. util.CTRL_OUT,
  160. util.CTRL_TYPE_STANDARD,
  161. util.CTRL_RECIPIENT_DEVICE
  162. )
  163. dev.ctrl_transfer(
  164. bmRequestType = bmRequestType,
  165. bRequest = 0x07,
  166. wValue = wValue,
  167. wIndex = wIndex,
  168. data_or_wLength = desc
  169. )
  170. def get_configuration(dev):
  171. r"""Get the current active configuration of the device.
  172. dev is the Device object to which the request will be
  173. sent to.
  174. This function differs from the Device.get_active_configuration
  175. method because the later may use cached data, while this
  176. function always does a device request.
  177. """
  178. bmRequestType = util.build_request_type(
  179. util.CTRL_IN,
  180. util.CTRL_TYPE_STANDARD,
  181. util.CTRL_RECIPIENT_DEVICE
  182. )
  183. return dev.ctrl_transfer(
  184. bmRequestType,
  185. bRequest = 0x08,
  186. data_or_wLength = 1
  187. )[0]
  188. def set_configuration(dev, bConfigurationNumber):
  189. r"""Set the current device configuration.
  190. dev is the Device object to which the request will be
  191. sent to.
  192. """
  193. dev.set_configuration(bConfigurationNumber)
  194. def get_interface(dev, bInterfaceNumber):
  195. r"""Get the current alternate setting of the interface.
  196. dev is the Device object to which the request will be
  197. sent to.
  198. """
  199. bmRequestType = util.build_request_type(
  200. util.CTRL_IN,
  201. util.CTRL_TYPE_STANDARD,
  202. util.CTRL_RECIPIENT_INTERFACE
  203. )
  204. return dev.ctrl_transfer(
  205. bmRequestType = bmRequestType,
  206. bRequest = 0x0a,
  207. wIndex = bInterfaceNumber,
  208. data_or_wLength = 1
  209. )[0]
  210. def set_interface(dev, bInterfaceNumber, bAlternateSetting):
  211. r"""Set the alternate setting of the interface.
  212. dev is the Device object to which the request will be
  213. sent to.
  214. """
  215. dev.set_interface_altsetting(bInterfaceNumber, bAlternateSetting)