ch9.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  2. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  3. * the the People's Republic of China and other countries.
  4. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  5. * DISCLAIMER
  6. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  7. * IF YOU NEED TO INTEGRATE THIRD PART'S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  8. * IN ALLWINNER'SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  9. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  10. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  11. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  12. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PART'S TECHNOLOGY.
  13. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  14. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  15. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  16. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  17. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __USB_CH9_H__
  28. #define __USB_CH9_H__
  29. #include <stdint.h>
  30. /* CONTROL REQUEST SUPPORT */
  31. /*
  32. * USB directions
  33. *
  34. * This bit flag is used in endpoint descriptors' bEndpointAddress field.
  35. * It's also one of three fields in control requests bRequestType.
  36. */
  37. #define USB_DIR_OUT 0 /* to device */
  38. #define USB_DIR_IN 0x80 /* to host */
  39. /*
  40. * USB types, the second of three bRequestType fields
  41. */
  42. #define USB_TYPE_MASK (0x03 << 5)
  43. #define USB_TYPE_STANDARD (0x00 << 5)
  44. #define USB_TYPE_CLASS (0x01 << 5)
  45. #define USB_TYPE_VENDOR (0x02 << 5)
  46. #define USB_TYPE_RESERVED (0x03 << 5)
  47. /*
  48. * USB recipients, the third of three bRequestType fields
  49. */
  50. #define USB_RECIP_MASK 0x1f
  51. #define USB_RECIP_DEVICE 0x00
  52. #define USB_RECIP_INTERFACE 0x01
  53. #define USB_RECIP_ENDPOINT 0x02
  54. #define USB_RECIP_OTHER 0x03
  55. /* From Wireless USB 1.0 */
  56. #define USB_RECIP_PORT 0x04
  57. #define USB_RECIP_RPIPE 0x05
  58. /*
  59. * Standard requests, for the bRequest field of a SETUP packet.
  60. *
  61. * These are qualified by the bRequestType field, so that for example
  62. * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
  63. * by a GET_STATUS request.
  64. */
  65. #define USB_REQ_GET_STATUS 0x00
  66. #define USB_REQ_CLEAR_FEATURE 0x01
  67. #define USB_REQ_SET_FEATURE 0x03
  68. #define USB_REQ_SET_ADDRESS 0x05
  69. #define USB_REQ_GET_DESCRIPTOR 0x06
  70. #define USB_REQ_SET_DESCRIPTOR 0x07
  71. #define USB_REQ_GET_CONFIGURATION 0x08
  72. #define USB_REQ_SET_CONFIGURATION 0x09
  73. #define USB_REQ_GET_INTERFACE 0x0A
  74. #define USB_REQ_SET_INTERFACE 0x0B
  75. #define USB_REQ_SYNCH_FRAME 0x0C
  76. #define USB_REQ_SET_SEL 0x30
  77. #define USB_REQ_SET_ISOCH_DELAY 0x31
  78. #define USB_REQ_SET_ENCRYPTION 0x0D /* Wireless USB */
  79. #define USB_REQ_GET_ENCRYPTION 0x0E
  80. #define USB_REQ_RPIPE_ABORT 0x0E
  81. #define USB_REQ_SET_HANDSHAKE 0x0F
  82. #define USB_REQ_RPIPE_RESET 0x0F
  83. #define USB_REQ_GET_HANDSHAKE 0x10
  84. #define USB_REQ_SET_CONNECTION 0x11
  85. #define USB_REQ_SET_SECURITY_DATA 0x12
  86. #define USB_REQ_GET_SECURITY_DATA 0x13
  87. #define USB_REQ_SET_WUSB_DATA 0x14
  88. #define USB_REQ_LOOPBACK_DATA_WRITE 0x15
  89. #define USB_REQ_LOOPBACK_DATA_READ 0x16
  90. #define USB_REQ_SET_INTERFACE_DS 0x17
  91. /* specific requests for USB Power Delivery */
  92. #define USB_REQ_GET_PARTNER_PDO 20
  93. #define USB_REQ_GET_BATTERY_STATUS 21
  94. #define USB_REQ_SET_PDO 22
  95. #define USB_REQ_GET_VDM 23
  96. #define USB_REQ_SEND_VDM 24
  97. /* The Link Power Management (LPM) ECN defines USB_REQ_TEST_AND_SET command,
  98. * used by hubs to put ports into a new L1 suspend state, except that it
  99. * forgot to define its number ...
  100. */
  101. /*
  102. * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
  103. * are read as a bit array returned by USB_REQ_GET_STATUS. (So there
  104. * are at most sixteen features of each type.) Hubs may also support a
  105. * new USB_REQ_TEST_AND_SET_FEATURE to put ports into L1 suspend.
  106. */
  107. #define USB_DEVICE_SELF_POWERED 0 /* (read only) */
  108. #define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */
  109. #define USB_DEVICE_TEST_MODE 2 /* (wired high speed only) */
  110. #define USB_DEVICE_BATTERY 2 /* (wireless) */
  111. #define USB_DEVICE_B_HNP_ENABLE 3 /* (otg) dev may initiate HNP */
  112. #define USB_DEVICE_WUSB_DEVICE 3 /* (wireless)*/
  113. #define USB_DEVICE_A_HNP_SUPPORT 4 /* (otg) RH port supports HNP */
  114. #define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* (otg) other RH port does */
  115. #define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */
  116. /*
  117. * Test Mode Selectors
  118. * See USB 2.0 spec Table 9-7
  119. */
  120. #define TEST_J 1
  121. #define TEST_K 2
  122. #define TEST_SE0_NAK 3
  123. #define TEST_PACKET 4
  124. #define TEST_FORCE_EN 5
  125. /*
  126. * New Feature Selectors as added by USB 3.0
  127. * See USB 3.0 spec Table 9-7
  128. */
  129. #define USB_DEVICE_U1_ENABLE 48 /* dev may initiate U1 transition */
  130. #define USB_DEVICE_U2_ENABLE 49 /* dev may initiate U2 transition */
  131. #define USB_DEVICE_LTM_ENABLE 50 /* dev may send LTM */
  132. #define USB_INTRF_FUNC_SUSPEND 0 /* function suspend */
  133. #define USB_INTR_FUNC_SUSPEND_OPT_MASK 0xFF00
  134. /*
  135. * Suspend Options, Table 9-8 USB 3.0 spec
  136. */
  137. #define USB_INTRF_FUNC_SUSPEND_LP (1 << (8 + 0))
  138. #define USB_INTRF_FUNC_SUSPEND_RW (1 << (8 + 1))
  139. /*
  140. * Interface status, Figure 9-5 USB 3.0 spec
  141. */
  142. #define USB_INTRF_STAT_FUNC_RW_CAP 1
  143. #define USB_INTRF_STAT_FUNC_RW 2
  144. #define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */
  145. /* Bit array elements as returned by the USB_REQ_GET_STATUS request. */
  146. #define USB_DEV_STAT_U1_ENABLED 2 /* transition into U1 state */
  147. #define USB_DEV_STAT_U2_ENABLED 3 /* transition into U2 state */
  148. #define USB_DEV_STAT_LTM_ENABLED 4 /* Latency tolerance messages */
  149. /*
  150. * Feature selectors from Table 9-8 USB Power Delivery spec
  151. */
  152. #define USB_DEVICE_BATTERY_WAKE_MASK 40
  153. #define USB_DEVICE_OS_IS_PD_AWARE 41
  154. #define USB_DEVICE_POLICY_MODE 42
  155. #define USB_PORT_PR_SWAP 43
  156. #define USB_PORT_GOTO_MIN 44
  157. #define USB_PORT_RETURN_POWER 45
  158. #define USB_PORT_ACCEPT_PD_REQUEST 46
  159. #define USB_PORT_REJECT_PD_REQUEST 47
  160. #define USB_PORT_PORT_PD_RESET 48
  161. #define USB_PORT_C_PORT_PD_CHANGE 49
  162. #define USB_PORT_CABLE_PD_RESET 50
  163. #define USB_DEVICE_CHARGING_POLICY 54
  164. /**
  165. * struct usb_ctrlrequest - SETUP data for a USB device control request
  166. * @bRequestType: matches the USB bmRequestType field
  167. * @bRequest: matches the USB bRequest field
  168. * @wValue: matches the USB wValue field (le16 byte order)
  169. * @wIndex: matches the USB wIndex field (le16 byte order)
  170. * @wLength: matches the USB wLength field (le16 byte order)
  171. *
  172. * This structure is used to send control requests to a USB device. It matches
  173. * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See the
  174. * USB spec for a fuller description of the different fields, and what they are
  175. * used for.
  176. *
  177. * Note that the driver for any interface can issue control requests.
  178. * For most devices, interfaces don't coordinate with each other, so
  179. * such requests may be made at any time.
  180. */
  181. struct usb_ctrlrequest {
  182. uint8_t bRequestType;
  183. uint8_t bRequest;
  184. uint16_t wValue;
  185. uint16_t wIndex;
  186. uint16_t wLength;
  187. };
  188. /*-------------------------------------------------------------------------*/
  189. /*
  190. * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or
  191. * (rarely) accepted by SET_DESCRIPTOR.
  192. *
  193. * Note that all multi-byte values here are encoded in little endian
  194. * byte order "on the wire". Within the kernel and when exposed
  195. * through the Linux-USB APIs, they are not converted to cpu byte
  196. * order; it is the responsibility of the client code to do this.
  197. * The single exception is when device and configuration descriptors (but
  198. * not other descriptors) are read from usbfs (i.e. /proc/bus/usb/BBB/DDD);
  199. * in this case the fields are converted to host endianness by the kernel.
  200. */
  201. /*
  202. * Descriptor types ... USB 2.0 spec table 9.5
  203. */
  204. #define USB_DT_DEVICE 0x01
  205. #define USB_DT_CONFIG 0x02
  206. #define USB_DT_STRING 0x03
  207. #define USB_DT_INTERFACE 0x04
  208. #define USB_DT_ENDPOINT 0x05
  209. #define USB_DT_DEVICE_QUALIFIER 0x06
  210. #define USB_DT_OTHER_SPEED_CONFIG 0x07
  211. #define USB_DT_INTERFACE_POWER 0x08
  212. /* these are from a minor usb 2.0 revision (ECN) */
  213. #define USB_DT_OTG 0x09
  214. #define USB_DT_DEBUG 0x0a
  215. #define USB_DT_INTERFACE_ASSOCIATION 0x0b
  216. /* these are from the Wireless USB spec */
  217. #define USB_DT_SECURITY 0x0c
  218. #define USB_DT_KEY 0x0d
  219. #define USB_DT_ENCRYPTION_TYPE 0x0e
  220. #define USB_DT_BOS 0x0f
  221. #define USB_DT_DEVICE_CAPABILITY 0x10
  222. #define USB_DT_WIRELESS_ENDPOINT_COMP 0x11
  223. #define USB_DT_WIRE_ADAPTER 0x21
  224. #define USB_DT_RPIPE 0x22
  225. #define USB_DT_CS_RADIO_CONTROL 0x23
  226. /* From the T10 UAS specification */
  227. #define USB_DT_PIPE_USAGE 0x24
  228. /* From the USB 3.0 spec */
  229. #define USB_DT_SS_ENDPOINT_COMP 0x30
  230. /* From the USB 3.1 spec */
  231. #define USB_DT_SSP_ISOC_ENDPOINT_COMP 0x31
  232. /* Conventional codes for class-specific descriptors. The convention is
  233. * defined in the USB "Common Class" Spec (3.11). Individual class specs
  234. * are authoritative for their usage, not the "common class" writeup.
  235. */
  236. #define USB_DT_CS_DEVICE (USB_TYPE_CLASS | USB_DT_DEVICE)
  237. #define USB_DT_CS_CONFIG (USB_TYPE_CLASS | USB_DT_CONFIG)
  238. #define USB_DT_CS_STRING (USB_TYPE_CLASS | USB_DT_STRING)
  239. #define USB_DT_CS_INTERFACE (USB_TYPE_CLASS | USB_DT_INTERFACE)
  240. #define USB_DT_CS_ENDPOINT (USB_TYPE_CLASS | USB_DT_ENDPOINT)
  241. /* All standard descriptors have these 2 fields at the beginning */
  242. struct usb_descriptor_header {
  243. uint8_t bLength;
  244. uint8_t bDescriptorType;
  245. };
  246. /*-------------------------------------------------------------------------*/
  247. /* USB_DT_DEVICE: Device descriptor */
  248. struct usb_device_descriptor {
  249. uint8_t bLength;
  250. uint8_t bDescriptorType;
  251. uint16_t bcdUSB;
  252. uint8_t bDeviceClass;
  253. uint8_t bDeviceSubClass;
  254. uint8_t bDeviceProtocol;
  255. uint8_t bMaxPacketSize0;
  256. uint16_t idVendor;
  257. uint16_t idProduct;
  258. uint16_t bcdDevice;
  259. uint8_t iManufacturer;
  260. uint8_t iProduct;
  261. uint8_t iSerialNumber;
  262. uint8_t bNumConfigurations;
  263. };
  264. #define USB_DT_DEVICE_SIZE 18
  265. /*
  266. * Device and/or Interface Class codes
  267. * as found in bDeviceClass or bInterfaceClass
  268. * and defined by www.usb.org documents
  269. */
  270. #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
  271. #define USB_CLASS_AUDIO 1
  272. #define USB_CLASS_COMM 2
  273. #define USB_CLASS_HID 3
  274. #define USB_CLASS_PHYSICAL 5
  275. #define USB_CLASS_STILL_IMAGE 6
  276. #define USB_CLASS_PRINTER 7
  277. #define USB_CLASS_MASS_STORAGE 8
  278. #define USB_CLASS_HUB 9
  279. #define USB_CLASS_CDC_DATA 0x0a
  280. #define USB_CLASS_CSCID 0x0b /* chip+ smart card */
  281. #define USB_CLASS_CONTENT_SEC 0x0d /* content security */
  282. #define USB_CLASS_VIDEO 0x0e
  283. #define USB_CLASS_WIRELESS_CONTROLLER 0xe0
  284. #define USB_CLASS_MISC 0xef
  285. #define USB_CLASS_APP_SPEC 0xfe
  286. #define USB_CLASS_VENDOR_SPEC 0xff
  287. #define USB_SUBCLASS_VENDOR_SPEC 0xff
  288. /*-------------------------------------------------------------------------*/
  289. /* USB_DT_CONFIG: Configuration descriptor information.
  290. *
  291. * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
  292. * descriptor type is different. Highspeed-capable devices can look
  293. * different depending on what speed they're currently running. Only
  294. * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
  295. * descriptors.
  296. */
  297. struct usb_config_descriptor {
  298. uint8_t bLength;
  299. uint8_t bDescriptorType;
  300. uint16_t wTotalLength;
  301. uint8_t bNumInterfaces;
  302. uint8_t bConfigurationValue;
  303. uint8_t iConfiguration;
  304. uint8_t bmAttributes;
  305. uint8_t bMaxPower;
  306. };
  307. #define USB_DT_CONFIG_SIZE 9
  308. /* from config descriptor bmAttributes */
  309. #define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */
  310. #define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */
  311. #define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */
  312. #define USB_CONFIG_ATT_BATTERY (1 << 4) /* battery powered */
  313. /*-------------------------------------------------------------------------*/
  314. /* USB_DT_STRING: String descriptor */
  315. struct usb_string_descriptor {
  316. uint8_t bLength;
  317. uint8_t bDescriptorType;
  318. uint16_t wData[1]; /* UTF-16LE encoded */
  319. };
  320. /* note that "string" zero is special, it holds language codes that
  321. * the device supports, not Unicode characters.
  322. */
  323. /*-------------------------------------------------------------------------*/
  324. /* USB_DT_INTERFACE: Interface descriptor */
  325. struct usb_interface_descriptor {
  326. uint8_t bLength;
  327. uint8_t bDescriptorType;
  328. uint8_t bInterfaceNumber;
  329. uint8_t bAlternateSetting;
  330. uint8_t bNumEndpoints;
  331. uint8_t bInterfaceClass;
  332. uint8_t bInterfaceSubClass;
  333. uint8_t bInterfaceProtocol;
  334. uint8_t iInterface;
  335. };
  336. #define USB_DT_INTERFACE_SIZE 9
  337. /*-------------------------------------------------------------------------*/
  338. /* USB_DT_ENDPOINT: Endpoint descriptor */
  339. struct usb_endpoint_descriptor {
  340. uint8_t bLength;
  341. uint8_t bDescriptorType;
  342. uint8_t bEndpointAddress;
  343. uint8_t bmAttributes;
  344. uint16_t wMaxPacketSize;
  345. uint8_t bInterval;
  346. /* NOTE: these two are _only_ in audio endpoints. */
  347. /* use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof. */
  348. uint8_t bRefresh;
  349. uint8_t bSynchAddress;
  350. };
  351. #define USB_DT_ENDPOINT_SIZE 7
  352. #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
  353. /*
  354. * Endpoints
  355. */
  356. #define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
  357. #define USB_ENDPOINT_DIR_MASK 0x80
  358. #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
  359. #define USB_ENDPOINT_XFER_CONTROL 0
  360. #define USB_ENDPOINT_XFER_ISOC 1
  361. #define USB_ENDPOINT_XFER_BULK 2
  362. #define USB_ENDPOINT_XFER_INT 3
  363. #define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
  364. #define USB_EP_MAXP_MULT_SHIFT 11
  365. #define USB_EP_MAXP_MULT_MASK (3 << USB_EP_MAXP_MULT_SHIFT)
  366. #define USB_EP_MAXP_MULT(m) \
  367. (((m) & USB_EP_MAXP_MULT_MASK) >> USB_EP_MAXP_MULT_SHIFT)
  368. /* The USB 3.0 spec redefines bits 5:4 of bmAttributes as interrupt ep type. */
  369. #define USB_ENDPOINT_INTRTYPE 0x30
  370. #define USB_ENDPOINT_INTR_PERIODIC (0 << 4)
  371. #define USB_ENDPOINT_INTR_NOTIFICATION (1 << 4)
  372. #define USB_ENDPOINT_SYNCTYPE 0x0c
  373. #define USB_ENDPOINT_SYNC_NONE (0 << 2)
  374. #define USB_ENDPOINT_SYNC_ASYNC (1 << 2)
  375. #define USB_ENDPOINT_SYNC_ADAPTIVE (2 << 2)
  376. #define USB_ENDPOINT_SYNC_SYNC (3 << 2)
  377. #define USB_ENDPOINT_USAGE_MASK 0x30
  378. #define USB_ENDPOINT_USAGE_DATA 0x00
  379. #define USB_ENDPOINT_USAGE_FEEDBACK 0x10
  380. #define USB_ENDPOINT_USAGE_IMPLICIT_FB 0x20 /* Implicit feedback Data endpoint */
  381. #endif /*__USB_CH9_H__*/