usb_common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * File : usb_common.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-10-01 Yi Qiu first version
  13. */
  14. #ifndef __RT_USB_COMMON_H__
  15. #define __RT_USB_COMMON_H__
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #include <rtthread.h>
  20. #define RT_DEBUG_USB 0x00
  21. #define USB_DYNAMIC 0x00
  22. #define USB_CLASS_DEVICE 0x00
  23. #define USB_CLASS_AUDIO 0x01
  24. #define USB_CLASS_CDC 0x02
  25. #define USB_CLASS_HID 0x03
  26. #define USB_CLASS_PHYSICAL 0x05
  27. #define USB_CLASS_IMAGE 0x06
  28. #define USB_CLASS_PRINTER 0x07
  29. #define USB_CLASS_MASS_STORAGE 0x08
  30. #define USB_CLASS_HUB 0x09
  31. #define USB_CLASS_CDC_DATA 0x0a
  32. #define USB_CLASS_SMART_CARD 0x0b
  33. #define USB_CLASS_SECURITY 0x0d
  34. #define USB_CLASS_VIDEO 0x0e
  35. #define USB_CLASS_HEALTHCARE 0x0f
  36. #define USB_CLASS_DIAG_DEVICE 0xdc
  37. #define USB_CLASS_WIRELESS 0xe0
  38. #define USB_CLASS_MISC 0xef
  39. #define USB_CLASS_APP_SPECIFIC 0xfe
  40. #define USB_CLASS_VEND_SPECIFIC 0xff
  41. #define USB_DESC_TYPE_DEVICE 0x01
  42. #define USB_DESC_TYPE_CONFIGURATION 0x02
  43. #define USB_DESC_TYPE_STRING 0x03
  44. #define USB_DESC_TYPE_INTERFACE 0x04
  45. #define USB_DESC_TYPE_ENDPOINT 0x05
  46. #define USB_DESC_TYPE_DEVICEQUALIFIER 0x06
  47. #define USB_DESC_TYPE_OTHERSPEED 0x07
  48. #define USB_DESC_TYPE_HID 0x21
  49. #define USB_DESC_TYPE_REPORT 0x22
  50. #define USB_DESC_TYPE_PHYSICAL 0x23
  51. #define USB_DESC_TYPE_HUB 0x29
  52. #define USB_DESC_LENGTH_DEVICE 0x12
  53. #define USB_DESC_LENGTH_CONFIG 0x9
  54. #define USB_DESC_LENGTH_STRING 0x4
  55. #define USB_DESC_LENGTH_INTERFACE 0x9
  56. #define USB_DESC_LENGTH_ENDPOINT 0x7
  57. #define USB_REQ_TYPE_STANDARD 0x00
  58. #define USB_REQ_TYPE_CLASS 0x20
  59. #define USB_REQ_TYPE_VENDOR 0x40
  60. #define USB_REQ_TYPE_MASK 0x60
  61. #define USB_REQ_TYPE_DIR_OUT 0x00
  62. #define USB_REQ_TYPE_DIR_IN 0x80
  63. #define USB_REQ_TYPE_DEVICE 0x00
  64. #define USB_REQ_TYPE_INTERFACE 0x01
  65. #define USB_REQ_TYPE_ENDPOINT 0x02
  66. #define USB_REQ_TYPE_OTHER 0x03
  67. #define USB_REQ_TYPE_RECIPIENT_MASK 0x1f
  68. #define USB_FEATURE_ENDPOINT_HALT 0x00
  69. #define USB_FEATURE_DEV_REMOTE_WAKEUP 0x01
  70. #define USB_FEATURE_TEST_MODE 0x02
  71. #define USB_REQ_GET_STATUS 0x00
  72. #define USB_REQ_CLEAR_FEATURE 0x01
  73. #define USB_REQ_SET_FEATURE 0x03
  74. #define USB_REQ_SET_ADDRESS 0x05
  75. #define USB_REQ_GET_DESCRIPTOR 0x06
  76. #define USB_REQ_SET_DESCRIPTOR 0x07
  77. #define USB_REQ_GET_CONFIGURATION 0x08
  78. #define USB_REQ_SET_CONFIGURATION 0x09
  79. #define USB_REQ_GET_INTERFACE 0x0A
  80. #define USB_REQ_SET_INTERFACE 0x0B
  81. #define USB_REQ_SYNCH_FRAME 0x0C
  82. #define USB_REQ_SET_ENCRYPTION 0x0D
  83. #define USB_REQ_GET_ENCRYPTION 0x0E
  84. #define USB_REQ_RPIPE_ABORT 0x0E
  85. #define USB_REQ_SET_HANDSHAKE 0x0F
  86. #define USB_REQ_RPIPE_RESET 0x0F
  87. #define USB_REQ_GET_HANDSHAKE 0x10
  88. #define USB_REQ_SET_CONNECTION 0x11
  89. #define USB_REQ_SET_SECURITY_DATA 0x12
  90. #define USB_REQ_GET_SECURITY_DATA 0x13
  91. #define USB_REQ_SET_WUSB_DATA 0x14
  92. #define USB_REQ_LOOPBACK_DATA_WRITE 0x15
  93. #define USB_REQ_LOOPBACK_DATA_READ 0x16
  94. #define USB_REQ_SET_INTERFACE_DS 0x17
  95. #define USB_STRING_LANGID_INDEX 0x00
  96. #define USB_STRING_MANU_INDEX 0x01
  97. #define USB_STRING_PRODUCT_INDEX 0x02
  98. #define USB_STRING_SERIAL_INDEX 0x03
  99. #define USB_STRING_CONFIG_INDEX 0x04
  100. #define USB_STRING_INTERFACE_INDEX 0x05
  101. #define USB_PID_OUT 0x01
  102. #define USB_PID_ACK 0x02
  103. #define USB_PID_DATA0 0x03
  104. #define USB_PID_SOF 0x05
  105. #define USB_PID_IN 0x09
  106. #define USB_PID_NACK 0x0A
  107. #define USB_PID_DATA1 0x0B
  108. #define USB_PID_PRE 0x0C
  109. #define USB_PID_SETUP 0x0D
  110. #define USB_PID_STALL 0x0E
  111. #define USB_EP_DESC_OUT 0x00
  112. #define USB_EP_DESC_IN 0x80
  113. #define USB_EP_DESC_NUM_MASK 0x0f
  114. #define USB_EP_ATTR_CONTROL 0x00
  115. #define USB_EP_ATTR_ISOC 0x01
  116. #define USB_EP_ATTR_BULK 0x02
  117. #define USB_EP_ATTR_INT 0x03
  118. #define USB_EP_ATTR_TYPE_MASK 0x03
  119. #define USB_EPNO_MASK 0x7f
  120. #define USB_DIR_OUT 0x00
  121. #define USB_DIR_IN 0x80
  122. #define USB_DIR_MASK 0x80
  123. #define RH_GET_PORT_STATUS 0
  124. #define RH_SET_PORT_STATUS 1
  125. #define RH_CLEAR_PORT_FEATURE 2
  126. #define RH_SET_PORT_FEATURE 3
  127. /*
  128. * Port feature numbers
  129. */
  130. #define PORT_FEAT_CONNECTION 0
  131. #define PORT_FEAT_ENABLE 1
  132. #define PORT_FEAT_SUSPEND 2
  133. #define PORT_FEAT_OVER_CURRENT 3
  134. #define PORT_FEAT_RESET 4
  135. #define PORT_FEAT_POWER 8
  136. #define PORT_FEAT_LOWSPEED 9
  137. #define PORT_FEAT_HIGHSPEED 10
  138. #define PORT_FEAT_C_CONNECTION 16
  139. #define PORT_FEAT_C_ENABLE 17
  140. #define PORT_FEAT_C_SUSPEND 18
  141. #define PORT_FEAT_C_OVER_CURRENT 19
  142. #define PORT_FEAT_C_RESET 20
  143. /*
  144. The HcRhPortStatus[1:NDP] register is used to control and report port events on a per-port
  145. basis. NumberDownstreamPorts represents the number of HcRhPortStatus registers that are
  146. implemented in hardware. The lower word is used to reflect the port status, whereas the upper
  147. word reflects the status change bits. Some status bits are implemented with special write behavior
  148. (see below). If a transaction (token through handshake) is in progress when a write to change
  149. port status occurs, the resulting port status change must be postponed until the transaction
  150. completes. Reserved bits should always be written '0'.
  151. */
  152. #define PORT_CCS 0x00000001UL /* R:CurrentConnectStatus - W:ClearPortEnable */
  153. #define PORT_PES 0x00000002UL /* R:PortEnableStatus - W:SetPortEnable */
  154. #define PORT_PSS 0x00000004UL /* R:PortSuspendStatus - W:SetPortSuspend */
  155. #define PORT_POCI 0x00000008UL /* R:PortOverCurrentIndicator - W:ClearSuspendStatus */
  156. #define PORT_PRS 0x00000010UL /* R:PortResetStatus - W: SetPortReset */
  157. #define PORT_PPS 0x00000100UL /* R:PortPowerStatus - W: SetPortPower */
  158. #define PORT_LSDA 0x00000200UL /* R:LowSpeedDeviceAttached - W:ClearPortPower */
  159. #define PORT_CCSC 0x00010000UL
  160. #define PORT_PESC 0x00020000UL
  161. #define PORT_PSSC 0x00040000UL
  162. #define PORT_POCIC 0x00080000UL
  163. #define PORT_PRSC 0x00100000UL
  164. /*
  165. *Hub Status & Hub Change bit masks
  166. */
  167. #define HUB_STATUS_LOCAL_POWER 0x0001
  168. #define HUB_STATUS_OVERCURRENT 0x0002
  169. #define HUB_CHANGE_LOCAL_POWER 0x0001
  170. #define HUB_CHANGE_OVERCURRENT 0x0002
  171. #define USB_EP_ATTR(attr) (attr & USB_EP_ATTR_TYPE_MASK)
  172. #define USB_EP_DESC_NUM(addr) (addr & USB_EP_DESC_NUM_MASK)
  173. #define uswap_32(x) \
  174. ((((x) & 0xff000000) >> 24) | \
  175. (((x) & 0x00ff0000) >> 8) | \
  176. (((x) & 0x0000ff00) << 8) | \
  177. (((x) & 0x000000ff) << 24))
  178. #define uswap_8(x) \
  179. (((rt_uint16_t)(*((rt_uint8_t *)(x)))) + \
  180. (((rt_uint16_t)(*(((rt_uint8_t *)(x)) + 1))) << 8))
  181. typedef void (*func_callback)(void *context);
  182. #pragma pack(1)
  183. struct usb_descriptor
  184. {
  185. rt_uint8_t bLength;
  186. rt_uint8_t type;
  187. };
  188. typedef struct usb_descriptor* udesc_t;
  189. struct udevice_descriptor
  190. {
  191. rt_uint8_t bLength;
  192. rt_uint8_t type;
  193. rt_uint16_t bcdUSB;
  194. rt_uint8_t bDeviceClass;
  195. rt_uint8_t bDeviceSubClass;
  196. rt_uint8_t bDeviceProtocol;
  197. rt_uint8_t bMaxPacketSize0;
  198. rt_uint16_t idVendor;
  199. rt_uint16_t idProduct;
  200. rt_uint16_t bcdDevice;
  201. rt_uint8_t iManufacturer;
  202. rt_uint8_t iProduct;
  203. rt_uint8_t iSerialNumber;
  204. rt_uint8_t bNumConfigurations;
  205. };
  206. typedef struct udevice_descriptor* udev_desc_t;
  207. struct uconfig_descriptor
  208. {
  209. rt_uint8_t bLength;
  210. rt_uint8_t type;
  211. rt_uint16_t wTotalLength;
  212. rt_uint8_t bNumInterfaces;
  213. rt_uint8_t bConfigurationValue;
  214. rt_uint8_t iConfiguration;
  215. rt_uint8_t bmAttributes;
  216. rt_uint8_t MaxPower;
  217. rt_uint8_t data[128];
  218. };
  219. typedef struct uconfig_descriptor* ucfg_desc_t;
  220. struct uinterface_descriptor
  221. {
  222. rt_uint8_t bLength;
  223. rt_uint8_t type;
  224. rt_uint8_t bInterfaceNumber;
  225. rt_uint8_t bAlternateSetting;
  226. rt_uint8_t bNumEndpoints;
  227. rt_uint8_t bInterfaceClass;
  228. rt_uint8_t bInterfaceSubClass;
  229. rt_uint8_t bInterfaceProtocol;
  230. rt_uint8_t iInterface;
  231. };
  232. typedef struct uinterface_descriptor* uintf_desc_t;
  233. /* Interface Association Descriptor (IAD) */
  234. struct uassco_descriptor
  235. {
  236. rt_uint8_t bLength;
  237. rt_uint8_t bDescriptorType;
  238. rt_uint8_t bFirstInterface;
  239. rt_uint8_t bInterfaceCount;
  240. rt_uint8_t bFunctionClass;
  241. rt_uint8_t bFunctionSubClass;
  242. rt_uint8_t bFunctionProtocol;
  243. rt_uint8_t iFunction;
  244. };
  245. typedef struct uassco_descriptor* uassco_desc_t;
  246. struct uendpoint_descriptor
  247. {
  248. rt_uint8_t bLength;
  249. rt_uint8_t type;
  250. rt_uint8_t bEndpointAddress;
  251. rt_uint8_t bmAttributes;
  252. rt_uint16_t wMaxPacketSize;
  253. rt_uint8_t bInterval;
  254. };
  255. typedef struct uendpoint_descriptor* uep_desc_t;
  256. struct ustring_descriptor
  257. {
  258. rt_uint8_t bLength;
  259. rt_uint8_t type;
  260. rt_uint8_t String[64];
  261. };
  262. typedef struct ustring_descriptor* ustr_desc_t;
  263. struct uhub_descriptor
  264. {
  265. rt_uint8_t length;
  266. rt_uint8_t type;
  267. rt_uint8_t num_ports;
  268. rt_uint16_t characteristics;
  269. rt_uint8_t pwron_to_good; /* power on to power good */
  270. rt_uint8_t current;
  271. rt_uint8_t removable[8];
  272. rt_uint8_t pwr_ctl[8];
  273. };
  274. typedef struct uhub_descriptor* uhub_desc_t;
  275. struct ureqest
  276. {
  277. rt_uint8_t request_type;
  278. rt_uint8_t request;
  279. rt_uint16_t value;
  280. rt_uint16_t index;
  281. rt_uint16_t length;
  282. };
  283. typedef struct ureqest* ureq_t;
  284. struct ustorage_cbw
  285. {
  286. rt_uint32_t signature;
  287. rt_uint32_t tag;
  288. rt_uint32_t xfer_len;
  289. rt_uint8_t dflags;
  290. rt_uint8_t lun;
  291. rt_uint8_t cb_len;
  292. rt_uint8_t cb[16];
  293. };
  294. typedef struct ustorage_cbw* ustorage_cbw_t;
  295. struct ustorage_csw
  296. {
  297. rt_uint32_t signature;
  298. rt_uint32_t tag;
  299. rt_uint32_t data_reside;
  300. rt_uint8_t status;
  301. };
  302. typedef struct ustorage_csw* ustorage_csw_t;
  303. #define SIZEOF_CSW 0x0d
  304. #define SIZEOF_CBW 0x1f
  305. #define CBWFLAGS_DIR_M 0x80
  306. #define CBWFLAGS_DIR_IN 0x80
  307. #define CBWFLAGS_DIR_OUT 0x00
  308. #define SCSI_TEST_UNIT_READY 0x00
  309. #define SCSI_REQUEST_SENSE 0x03
  310. #define SCSI_INQUIRY_CMD 0x12
  311. #define SCSI_ALLOW_MEDIUM_REMOVAL 0x1e
  312. #define SCSI_MODE_SENSE_6 0x1a
  313. #define SCSI_READ_CAPACITIES 0x23
  314. #define SCSI_READ_CAPACITY 0x25
  315. #define SCSI_READ_10 0x28
  316. #define SCSI_WRITE_10 0x2a
  317. #define SCSI_VERIFY_10 0x2f
  318. #define CBW_SIGNATURE 0x43425355
  319. #define CSW_SIGNATURE 0x53425355
  320. #define CBW_TAG_VALUE 0x12345678
  321. #define USBREQ_GET_MAX_LUN 0xfe
  322. #define USBREQ_MASS_STORAGE_RESET 0xff
  323. #pragma pack()
  324. #ifdef __cplusplus
  325. }
  326. #endif
  327. #endif