usb_ch9.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * This file holds USB constants and structures that are needed for USB
  3. * device APIs. These are used by the USB device model, which is defined
  4. * in chapter 9 of the USB 2.0 specification. Linux has several APIs in C
  5. * that need these:
  6. *
  7. * - the master/host side Linux-USB kernel driver API;
  8. * - the "usbfs" user space API; and
  9. * - (eventually) a Linux "gadget" slave/device side driver API.
  10. *
  11. * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems
  12. * act either as a USB master/host or as a USB slave/device. That means
  13. * the master and slave side APIs will benefit from working well together.
  14. */
  15. #ifndef __LINUX_USB_CH9_H
  16. #define __LINUX_USB_CH9_H
  17. #include <asm/types.h> /* __u8 etc */
  18. /*-------------------------------------------------------------------------*/
  19. /* CONTROL REQUEST SUPPORT */
  20. /*
  21. * USB directions
  22. *
  23. * This bit flag is used in endpoint descriptors' bEndpointAddress field.
  24. * It's also one of three fields in control requests bRequestType.
  25. */
  26. #define USB_DIR_OUT 0 /* to device */
  27. #define USB_DIR_IN 0x80 /* to host */
  28. /*
  29. * USB types, the second of three bRequestType fields
  30. */
  31. #define USB_TYPE_MASK (0x03 << 5)
  32. #define USB_TYPE_STANDARD (0x00 << 5)
  33. #define USB_TYPE_CLASS (0x01 << 5)
  34. #define USB_TYPE_VENDOR (0x02 << 5)
  35. #define USB_TYPE_RESERVED (0x03 << 5)
  36. /*
  37. * USB recipients, the third of three bRequestType fields
  38. */
  39. #define USB_RECIP_MASK 0x1f
  40. #define USB_RECIP_DEVICE 0x00
  41. #define USB_RECIP_INTERFACE 0x01
  42. #define USB_RECIP_ENDPOINT 0x02
  43. #define USB_RECIP_OTHER 0x03
  44. /*
  45. * Standard requests, for the bRequest field of a SETUP packet.
  46. *
  47. * These are qualified by the bRequestType field, so that for example
  48. * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
  49. * by a GET_STATUS request.
  50. */
  51. #define USB_REQ_GET_STATUS 0x00
  52. #define USB_REQ_CLEAR_FEATURE 0x01
  53. #define USB_REQ_SET_FEATURE 0x03
  54. #define USB_REQ_SET_ADDRESS 0x05
  55. #define USB_REQ_GET_DESCRIPTOR 0x06
  56. #define USB_REQ_SET_DESCRIPTOR 0x07
  57. #define USB_REQ_GET_CONFIGURATION 0x08
  58. #define USB_REQ_SET_CONFIGURATION 0x09
  59. #define USB_REQ_GET_INTERFACE 0x0A
  60. #define USB_REQ_SET_INTERFACE 0x0B
  61. #define USB_REQ_SYNCH_FRAME 0x0C
  62. /*
  63. * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
  64. * are read as a bit array returned by USB_REQ_GET_STATUS. (So there
  65. * are at most sixteen features of each type.)
  66. */
  67. #define USB_DEVICE_SELF_POWERED 0 /* (read only) */
  68. #define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */
  69. #define USB_DEVICE_TEST_MODE 2 /* (high speed only) */
  70. #define USB_DEVICE_B_HNP_ENABLE 3 /* dev may initiate HNP */
  71. #define USB_DEVICE_A_HNP_SUPPORT 4 /* RH port supports HNP */
  72. #define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* other RH port does */
  73. #define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */
  74. #define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */
  75. /**
  76. * struct usb_ctrlrequest - SETUP data for a USB device control request
  77. * @bRequestType: matches the USB bmRequestType field
  78. * @bRequest: matches the USB bRequest field
  79. * @wValue: matches the USB wValue field (le16 byte order)
  80. * @wIndex: matches the USB wIndex field (le16 byte order)
  81. * @wLength: matches the USB wLength field (le16 byte order)
  82. *
  83. * This structure is used to send control requests to a USB device. It matches
  84. * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See the
  85. * USB spec for a fuller description of the different fields, and what they are
  86. * used for.
  87. *
  88. * Note that the driver for any interface can issue control requests.
  89. * For most devices, interfaces don't coordinate with each other, so
  90. * such requests may be made at any time.
  91. */
  92. struct usb_ctrlrequest {
  93. __u8 bRequestType;
  94. __u8 bRequest;
  95. __le16 wValue;
  96. __le16 wIndex;
  97. __le16 wLength;
  98. } __attribute__ ((packed));
  99. /*-------------------------------------------------------------------------*/
  100. /*
  101. * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or
  102. * (rarely) accepted by SET_DESCRIPTOR.
  103. *
  104. * Note that all multi-byte values here are encoded in little endian
  105. * byte order "on the wire". But when exposed through Linux-USB APIs,
  106. * they've been converted to cpu byte order.
  107. */
  108. /*
  109. * Descriptor types ... USB 2.0 spec table 9.5
  110. */
  111. #define USB_DT_DEVICE 0x01
  112. #define USB_DT_CONFIG 0x02
  113. #define USB_DT_STRING 0x03
  114. #define USB_DT_INTERFACE 0x04
  115. #define USB_DT_ENDPOINT 0x05
  116. #define USB_DT_DEVICE_QUALIFIER 0x06
  117. #define USB_DT_OTHER_SPEED_CONFIG 0x07
  118. #define USB_DT_INTERFACE_POWER 0x08
  119. /* these are from a minor usb 2.0 revision (ECN) */
  120. #define USB_DT_OTG 0x09
  121. #define USB_DT_DEBUG 0x0a
  122. #define USB_DT_INTERFACE_ASSOCIATION 0x0b
  123. /* conventional codes for class-specific descriptors */
  124. #define USB_DT_CS_DEVICE 0x21
  125. #define USB_DT_CS_CONFIG 0x22
  126. #define USB_DT_CS_STRING 0x23
  127. #define USB_DT_CS_INTERFACE 0x24
  128. #define USB_DT_CS_ENDPOINT 0x25
  129. /* All standard descriptors have these 2 fields at the beginning */
  130. struct usb_descriptor_header {
  131. __u8 bLength;
  132. __u8 bDescriptorType;
  133. } __attribute__ ((packed));
  134. /*-------------------------------------------------------------------------*/
  135. /* USB_DT_DEVICE: Device descriptor */
  136. struct usb_device_descriptor {
  137. __u8 bLength;
  138. __u8 bDescriptorType;
  139. __le16 bcdUSB;
  140. __u8 bDeviceClass;
  141. __u8 bDeviceSubClass;
  142. __u8 bDeviceProtocol;
  143. __u8 bMaxPacketSize0;
  144. __le16 idVendor;
  145. __le16 idProduct;
  146. __le16 bcdDevice;
  147. __u8 iManufacturer;
  148. __u8 iProduct;
  149. __u8 iSerialNumber;
  150. __u8 bNumConfigurations;
  151. } __attribute__ ((packed));
  152. #define USB_DT_DEVICE_SIZE 18
  153. /*
  154. * Device and/or Interface Class codes
  155. * as found in bDeviceClass or bInterfaceClass
  156. * and defined by www.usb.org documents
  157. */
  158. #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
  159. #define USB_CLASS_AUDIO 1
  160. #define USB_CLASS_COMM 2
  161. #define USB_CLASS_HID 3
  162. #define USB_CLASS_PHYSICAL 5
  163. #define USB_CLASS_STILL_IMAGE 6
  164. #define USB_CLASS_PRINTER 7
  165. #define USB_CLASS_MASS_STORAGE 8
  166. #define USB_CLASS_HUB 9
  167. #define USB_CLASS_CDC_DATA 0x0a
  168. #define USB_CLASS_CSCID 0x0b /* chip+ smart card */
  169. #define USB_CLASS_CONTENT_SEC 0x0d /* content security */
  170. #define USB_CLASS_VIDEO 0x0e
  171. #define USB_CLASS_APP_SPEC 0xfe
  172. #define USB_CLASS_VENDOR_SPEC 0xff
  173. /*-------------------------------------------------------------------------*/
  174. /* USB_DT_CONFIG: Configuration descriptor information.
  175. *
  176. * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
  177. * descriptor type is different. Highspeed-capable devices can look
  178. * different depending on what speed they're currently running. Only
  179. * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
  180. * descriptors.
  181. */
  182. struct usb_config_descriptor {
  183. __u8 bLength;
  184. __u8 bDescriptorType;
  185. __le16 wTotalLength;
  186. __u8 bNumInterfaces;
  187. __u8 bConfigurationValue;
  188. __u8 iConfiguration;
  189. __u8 bmAttributes;
  190. __u8 bMaxPower;
  191. } __attribute__ ((packed));
  192. #define USB_DT_CONFIG_SIZE 9
  193. /* from config descriptor bmAttributes */
  194. #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */
  195. #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */
  196. #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */
  197. /*-------------------------------------------------------------------------*/
  198. /* USB_DT_STRING: String descriptor */
  199. struct usb_string_descriptor {
  200. __u8 bLength;
  201. __u8 bDescriptorType;
  202. __le16 wData[1]; /* UTF-16LE encoded */
  203. } __attribute__ ((packed));
  204. /* note that "string" zero is special, it holds language codes that
  205. * the device supports, not Unicode characters.
  206. */
  207. /*-------------------------------------------------------------------------*/
  208. /* USB_DT_INTERFACE: Interface descriptor */
  209. struct usb_interface_descriptor {
  210. __u8 bLength;
  211. __u8 bDescriptorType;
  212. __u8 bInterfaceNumber;
  213. __u8 bAlternateSetting;
  214. __u8 bNumEndpoints;
  215. __u8 bInterfaceClass;
  216. __u8 bInterfaceSubClass;
  217. __u8 bInterfaceProtocol;
  218. __u8 iInterface;
  219. } __attribute__ ((packed));
  220. #define USB_DT_INTERFACE_SIZE 9
  221. /*-------------------------------------------------------------------------*/
  222. /* USB_DT_ENDPOINT: Endpoint descriptor */
  223. struct usb_endpoint_descriptor {
  224. __u8 bLength;
  225. __u8 bDescriptorType;
  226. __u8 bEndpointAddress;
  227. __u8 bmAttributes;
  228. __le16 wMaxPacketSize;
  229. __u8 bInterval;
  230. // NOTE: these two are _only_ in audio endpoints.
  231. // use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof.
  232. __u8 bRefresh;
  233. __u8 bSynchAddress;
  234. } __attribute__ ((packed));
  235. #define USB_DT_ENDPOINT_SIZE 7
  236. #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
  237. /*
  238. * Endpoints
  239. */
  240. #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
  241. #define USB_ENDPOINT_DIR_MASK 0x80
  242. #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
  243. #define USB_ENDPOINT_XFER_CONTROL 0
  244. #define USB_ENDPOINT_XFER_ISOC 1
  245. #define USB_ENDPOINT_XFER_BULK 2
  246. #define USB_ENDPOINT_XFER_INT 3
  247. /*-------------------------------------------------------------------------*/
  248. /* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */
  249. struct usb_qualifier_descriptor {
  250. __u8 bLength;
  251. __u8 bDescriptorType;
  252. __le16 bcdUSB;
  253. __u8 bDeviceClass;
  254. __u8 bDeviceSubClass;
  255. __u8 bDeviceProtocol;
  256. __u8 bMaxPacketSize0;
  257. __u8 bNumConfigurations;
  258. __u8 bRESERVED;
  259. } __attribute__ ((packed));
  260. /*-------------------------------------------------------------------------*/
  261. /* USB_DT_OTG (from OTG 1.0a supplement) */
  262. struct usb_otg_descriptor {
  263. __u8 bLength;
  264. __u8 bDescriptorType;
  265. __u8 bmAttributes; /* support for HNP, SRP, etc */
  266. } __attribute__ ((packed));
  267. /* from usb_otg_descriptor.bmAttributes */
  268. #define USB_OTG_SRP (1 << 0)
  269. #define USB_OTG_HNP (1 << 1) /* swap host/device roles */
  270. /*-------------------------------------------------------------------------*/
  271. /* USB_DT_DEBUG: for special highspeed devices, replacing serial console */
  272. struct usb_debug_descriptor {
  273. __u8 bLength;
  274. __u8 bDescriptorType;
  275. /* bulk endpoints with 8 byte maxpacket */
  276. __u8 bDebugInEndpoint;
  277. __u8 bDebugOutEndpoint;
  278. };
  279. /*-------------------------------------------------------------------------*/
  280. /* USB_DT_INTERFACE_ASSOCIATION: groups interfaces */
  281. struct usb_interface_assoc_descriptor {
  282. __u8 bLength;
  283. __u8 bDescriptorType;
  284. __u8 bFirstInterface;
  285. __u8 bInterfaceCount;
  286. __u8 bFunctionClass;
  287. __u8 bFunctionSubClass;
  288. __u8 bFunctionProtocol;
  289. __u8 iFunction;
  290. } __attribute__ ((packed));
  291. /*-------------------------------------------------------------------------*/
  292. /* USB 2.0 defines three speeds, here's how Linux identifies them */
  293. enum usb_device_speed {
  294. USB_SPEED_UNKNOWN = 0, /* enumerating */
  295. USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
  296. USB_SPEED_HIGH /* usb 2.0 */
  297. };
  298. enum usb_device_state {
  299. /* NOTATTACHED isn't in the USB spec, and this state acts
  300. * the same as ATTACHED ... but it's clearer this way.
  301. */
  302. USB_STATE_NOTATTACHED = 0,
  303. /* the chapter 9 device states */
  304. USB_STATE_ATTACHED,
  305. USB_STATE_POWERED,
  306. USB_STATE_DEFAULT, /* limited function */
  307. USB_STATE_ADDRESS,
  308. USB_STATE_CONFIGURED, /* most functions */
  309. USB_STATE_SUSPENDED
  310. /* NOTE: there are actually four different SUSPENDED
  311. * states, returning to POWERED, DEFAULT, ADDRESS, or
  312. * CONFIGURED respectively when SOF tokens flow again.
  313. */
  314. };
  315. #endif /* __LINUX_USB_CH9_H */