usb_common.h 14 KB

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