fusb_def.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fusb_def.h
  15. * Date: 2022-02-11 13:33:11
  16. * LastEditTime: 2022-02-18 09:18:24
  17. * Description:  This files is for definition of spec defined USB data structure
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 Zhugengyu 2022/2/7 init commit
  23. */
  24. #ifndef DRIVERS_FUSB_DEF_H
  25. #define DRIVERS_FUSB_DEF_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. /***************************** Include Files *********************************/
  31. #include "ftypes.h"
  32. /************************** Constant Definitions *****************************/
  33. typedef enum
  34. {
  35. FUSB_UNKNOWN_SPEED = -1,
  36. FUSB_FULL_SPEED = 0,
  37. FUSB_LOW_SPEED = 1,
  38. FUSB_HIGH_SPEED = 2,
  39. FUSB_SUPER_SPEED = 3,
  40. FUSB_SUPER_SPEED_PLUS = 4,
  41. } FUsbSpeed;
  42. typedef enum
  43. {
  44. FUSB_REQ_HOST_TO_DEVICE = 0,
  45. FUSB_REQ_DEVICE_TO_HOST = 1
  46. } FUsbReqDirection;
  47. typedef enum
  48. {
  49. FUSB_REQ_TYPE_STANDARD = 0,
  50. FUSB_REQ_TYPE_CLASS = 1,
  51. FUSB_REQ_TYPE_VENDOR = 2,
  52. FUSB_REQ_TYPE_RESERVED = 3
  53. } FUsbReqType;
  54. typedef enum
  55. {
  56. FUSB_REQ_RECP_DEV = 0,
  57. FUSB_REQ_RECP_IF = 1,
  58. FUSB_REQ_RECP_EP = 2,
  59. FUSB_REQ_RECP_OTHER = 3
  60. } FUsbReqRecpient;
  61. /* refer to Table 9-5. Descriptor Types in USB spec. for details */
  62. typedef enum
  63. {
  64. FUSB_DESC_TYPE_NONE = 0,
  65. FUSB_DESC_TYPE_DEVICE = 1,
  66. FUSB_DESC_TYPE_CONFIG = 2,
  67. FUSB_DESC_TYPE_STRING = 3,
  68. FUSB_DESC_TYPE_INTERFACE = 4,
  69. FUSB_DESC_TYPE_ENDPOINT = 5,
  70. FUSB_DESC_TYPE_HUB = 41,
  71. FUSB_DESC_TYPE_SUPER_SPEED_HUB = 42
  72. } FUsbDescriptorType;
  73. typedef enum
  74. {
  75. FUSB_GET_STATUS = 0,
  76. FUSB_CLEAR_FEATURE = 1,
  77. FUSB_SET_FEATURE = 3,
  78. FUSB_SET_ADDRESS = 5,
  79. FUSB_GET_DESCRIPTOR = 6,
  80. FUSB_SET_DESCRIPTOR = 7,
  81. FUSB_GET_CONFIGURATION = 8,
  82. FUSB_SET_CONFIGURATION = 9,
  83. FUSB_GET_INTERFACE = 10,
  84. FUSB_SET_INTERFACE = 11,
  85. FUSB_SYNCH_FRAME = 12
  86. } FUsbRequestCode;
  87. typedef enum
  88. {
  89. FUSB_ENDPOINT_HALT = 0,
  90. FUSB_DEVICE_REMOTE_WAKEUP = 1,
  91. FUSB_TEST_MODE = 2
  92. } FUsbFeatureSelectors;
  93. typedef enum
  94. {
  95. FUSB_SETUP,
  96. FUSB_IN,
  97. FUSB_OUT
  98. } FUsbDirection;
  99. typedef enum
  100. {
  101. FUSB_CONTROL_EP = 0,
  102. FUSB_ISOCHRONOUS_EP = 1,
  103. FUSB_BULK_EP = 2,
  104. FUSB_INTERRUPT_EP = 3
  105. } FUsbEpType;
  106. typedef enum
  107. {
  108. FUSB_UNKOWN_DEVICE = 0x0,
  109. FUSB_AUDIO_DEVICE = 0x01,
  110. FUSB_COMM_DEVICE = 0x02,
  111. FUSB_HID_DEVICE = 0x03,
  112. FUSB_PHYSICAL_DEVICE = 0x05,
  113. FUSB_IMAGE_DEVICE = 0x06,
  114. FUSB_PRINTER_DEVICE = 0x07,
  115. FUSB_MASS_STORAGE_DEVICE = 0x08,
  116. FUSB_HUB_DEVICE = 0x09,
  117. } FUsbDevClass; /* definition of device class */
  118. /**************************** Type Definitions *******************************/
  119. /* following data structure is defined according to spec. name their member
  120. may not compliant with code convention */
  121. typedef struct
  122. {
  123. unsigned char bDescLength;
  124. unsigned char bDescriptorType;
  125. unsigned char bNbrPorts; /* Number of downstream facing ports supports */
  126. union
  127. {
  128. struct
  129. {
  130. unsigned long logicalPowerSwitchingMode: 2; /* BIT[1:0] */
  131. unsigned long isCompoundDevice: 1; /* BIT[2] */
  132. unsigned long overcurrentProtectionMode: 2; /* BIT[4:3] */
  133. unsigned long ttThinkTime: 2; /* BIT[6:5] */
  134. unsigned long arePortIndicatorsSupported: 1; /* BIT[7] */
  135. unsigned long: 8;
  136. } __attribute__((packed));
  137. unsigned short wHubCharacteristics;
  138. } __attribute__((packed));
  139. unsigned char bPowerOn2PwrGood; /* in 2 ms intervals */
  140. unsigned char bHubContrCurrent; /* max current requirements */
  141. char DeviceRemovable[]; /* indicates if a port has a removable device attached */
  142. } __attribute__((packed)) FUsbHubDescriptor;
  143. typedef struct
  144. {
  145. unsigned char bLength;
  146. unsigned char bDescriptorType;
  147. unsigned short bcdUSB;
  148. unsigned char bDeviceClass;
  149. unsigned char bDeviceSubClass;
  150. unsigned char bDeviceProtocol;
  151. unsigned char bMaxPacketSize0;
  152. unsigned short idVendor;
  153. unsigned short idProduct;
  154. unsigned short bcdDevice;
  155. unsigned char iManufacturer;
  156. unsigned char iProduct;
  157. unsigned char iSerialNumber;
  158. unsigned char bNumConfigurations;
  159. } __attribute__((packed)) FUsbDeviceDescriptor;
  160. typedef struct
  161. {
  162. unsigned char bLength;
  163. unsigned char bDescriptorType;
  164. unsigned short wTotalLength;
  165. unsigned char bNumInterfaces;
  166. unsigned char bConfigurationValue;
  167. unsigned char iConfiguration;
  168. unsigned char bmAttributes;
  169. #define FUSB_CONFIG_DESC_ATTR_REMOTE_WEAKUP BIT(5) /* 1: remote wakeup feature */
  170. #define FUSB_CONFIG_DESC_ATTR_SELF_POWER BIT(6) /* 1: self-powered 0: bus-powered */
  171. #define FUSB_CONFIG_DESC_ATTR_USB1_COMPATIABLE BIT(7) /* 1: compatibility with USB 1.0 */
  172. unsigned char bMaxPower;
  173. /* configuration descriptor may follow more buffers, need to allocate dynamic memory for all contents */
  174. } __attribute__((packed)) FUsbConfigurationDescriptor;
  175. typedef struct
  176. {
  177. unsigned char bLength;
  178. unsigned char bDescriptorType;
  179. unsigned char bInterfaceNumber;
  180. unsigned char bAlternateSetting;
  181. unsigned char bNumEndpoints;
  182. unsigned char bInterfaceClass;
  183. unsigned char bInterfaceSubClass;
  184. unsigned char bInterfaceProtocol;
  185. unsigned char iInterface;
  186. } __attribute__((packed)) FUsbInterfaceDescriptor;
  187. typedef struct
  188. {
  189. unsigned char bLength;
  190. unsigned char bDescriptorType;
  191. unsigned char bEndpointAddress;
  192. /* Low-speed devices can have a maximum of 3 endpoint, other devices can have 16 (0–15) */
  193. #define FUSB_EP_DESC_EP_NUM(x) (GENMASK(3, 0) & (x)) /* endpoint number */
  194. #define FUSB_EP_DESC_EP_DIR_IN BIT(7) /* direction, 0: OUT, 1: IN */
  195. unsigned char bmAttributes;
  196. #define FUSB_EP_DESC_TRANS_TYPE(x) (GENMASK(1, 0) & (x))
  197. #define FUSB_EP_DESC_TRANS_CTRL 0b00 /* control */
  198. #define FUSB_EP_DESC_TRANS_ISOC 0b01 /* isochronous */
  199. #define FUSB_EP_DESC_TRANS_BULK 0b10 /* bulk */
  200. #define FUSB_EP_DESC_TRANS_INTR 0b11 /* interrupt */
  201. unsigned short wMaxPacketSize;
  202. #define FUSB_EP_DESC_MAX_PACKET_SZ GENMASK(10, 0)
  203. unsigned char bInterval;
  204. } __attribute__((packed)) FUsbEndpointDescriptor;
  205. typedef union
  206. {
  207. struct
  208. {
  209. u8 len;
  210. u8 type;
  211. } header;
  212. #define FUSB_DESCRIPTOR_HEADER_SIZE 2
  213. FUsbConfigurationDescriptor configuration;
  214. FUsbInterfaceDescriptor interface;
  215. FUsbEndpointDescriptor endpoint;
  216. } __attribute__((packed)) FUsbDescriptor;
  217. typedef struct
  218. {
  219. unsigned char bLength;
  220. unsigned char bDescriptorType;
  221. unsigned short bcdHID;
  222. unsigned char bCountryCode;
  223. unsigned char bNumDescriptors;
  224. unsigned char bReportDescriptorType;
  225. unsigned short wReportDescriptorLength;
  226. } __attribute__((packed)) FUsbHidDescriptor;
  227. typedef struct
  228. {
  229. u8 len; /* Descriptor size in bytes (variable) */
  230. u8 type; /* The constant String (0x03 DESCRIPTOR_STRING) */
  231. u16 string[0]; /* Unicode UTF- 16LE string */
  232. } __attribute__((packed)) FUsbStringDescriptor;
  233. typedef struct
  234. {
  235. union
  236. {
  237. struct
  238. {
  239. FUsbReqRecpient req_recp: 5;
  240. FUsbReqType req_type: 2;
  241. FUsbReqDirection data_dir: 1;
  242. } __attribute__((packed));
  243. unsigned char bmRequestType;
  244. } __attribute__((packed));
  245. unsigned char bRequest;
  246. unsigned short wValue;
  247. unsigned short wIndex;
  248. unsigned short wLength;
  249. } __attribute__((packed)) FUsbDevReq;
  250. /************************** Variable Definitions *****************************/
  251. /***************** Macros (Inline Functions) Definitions *********************/
  252. #define FUSB_DEFAULT_LANG_ID 0x409 /* English */
  253. /************************** Function Prototypes ******************************/
  254. #ifdef __cplusplus
  255. }
  256. #endif
  257. #endif