usb_common.h 15 KB

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