fusb.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.h
  15. * Date: 2022-02-11 13:33:11
  16. * LastEditTime: 2022-02-18 09:22:25
  17. * Description:  This files is for definition of FUSB user interface
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 Zhugengyu 2022/2/7 init commit
  23. */
  24. #ifndef DRIVERS_FUSB_H
  25. #define DRIVERS_FUSB_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. /***************************** Include Files *********************************/
  31. #include "ftypes.h"
  32. #include "ferror_code.h"
  33. #include "fassert.h"
  34. #include "fusb_def.h"
  35. /************************** Constant Definitions *****************************/
  36. #define FUSB_SUCCESS FT_SUCCESS
  37. #define FUSB_ERR_WAIT_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x0)
  38. #define FUSB_ERR_INVALID_PARA FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x1)
  39. #define FUSB_ERR_NOT_SUPPORT FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x2)
  40. #define FUSB_ERR_NON_INSTANCE FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x3)
  41. #define FUSB_ERR_INVALID_DATA FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x4)
  42. #define FUSB_ERR_DESC_PARSE_ERR FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x5)
  43. #define FUSB_ERR_ALLOCATE_FAIL FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x6)
  44. #define FUSB_ERR_TRANS_FAIL FT_MAKE_ERRCODE(ErrModBsp, ErrUsb, 0x7)
  45. /* SetAddress() recovery interval (USB 2.0 specification 9.2.6.3 */
  46. #define FUSB_SET_ADDRESS_MDELAY 2
  47. /*
  48. * USB sets an upper limit of 5 seconds for any transfer to be completed.
  49. *
  50. * Data originally from FUSB_HC_EHCI driver:
  51. * Tested with some USB2.0 flash sticks:
  52. * TUR turn around took about 2.2s for the slowest (13fe:3800), maximum
  53. * of 250ms for the others.
  54. *
  55. * SET ADDRESS on xHCI controllers.
  56. * The USB specification indicates that devices must complete processing
  57. * of a SET ADDRESS request within 50 ms. However, some hubs were found
  58. * to take more than 100 ms to complete a SET ADDRESS request on a
  59. * downstream port.
  60. */
  61. #define FUSB_USB_MAX_PROCESSING_TIME_US (5 * 1000 * 1000)
  62. #define FUSB_FULL_LOW_SPEED_FRAME_US 1000
  63. #define FUSB_MAX_CTRL_NUM 1
  64. #define FUSB_MAX_DEV_TYPE_NUM 8
  65. #define FUSB_MAX_DEV_NUM 128
  66. #define FUSB_MAX_EP_NUM 32
  67. #define FUSB_MAX_SLOT_NUM FUSB_MAX_DEV_NUM
  68. #define FUSB_SLOT_ID_VALID(slot) ((0 <= (slot)) && (FUSB_MAX_SLOT_NUM > (slot)))
  69. #define FUSB_DEFAULT_ALIGN 1
  70. #define FUSB_NO_DEV_ADDR -1
  71. #define FUSB_NO_HUB -1
  72. #define FUSB_NO_PORT -1
  73. /**************************** Type Definitions *******************************/
  74. typedef struct _FUsbDev FUsbDev;
  75. typedef struct _FUsbHc FUsbHc;
  76. typedef struct _FUsb FUsb;
  77. /* Transfer complete code for USB */
  78. enum
  79. {
  80. FUSB_CC_ZERO_BYTES = 0,
  81. FUSB_CC_SUCCESS = 1
  82. /* for XHCI transfer complete code, please refer to e.g. FXHCI_CC_SUCCESS */
  83. /* be careful not define conflict CC code */
  84. };
  85. /* less than 0 means error (implemented by usb Hc, e.g. FXhciTransCode),
  86. greater or equal than 0 means bytes transfered */
  87. typedef int FUsbTransCode;
  88. typedef struct
  89. {
  90. FUsbDev *dev; /* device instance of this endpoint */
  91. int endpoint; /* endpoint address ep0 = 0, epn = n */
  92. FUsbDirection direction; /* type or direction of ep */
  93. int toggle; /* ep state for some device to toggle */
  94. int maxpacketsize; /* max packet size for ep transfer */
  95. FUsbEpType type; /* transfer type of ep, control, bulk or so on */
  96. int interval; /* expressed as binary logarithm of the number
  97. of microframes (i.e. t = 125us * 2^interval) */
  98. } FUsbEndpoint; /* encapsulates a single endpoint of an USB device */
  99. typedef struct
  100. {
  101. const FUsbDescriptor *buf;
  102. u32 buf_len;
  103. boolean is_valid;
  104. const FUsbDescriptor *end_pos;
  105. const FUsbDescriptor *next_pos;
  106. const FUsbDescriptor *cur_desc;
  107. const FUsbDescriptor *err_pos;
  108. } FUsbConfigParser; /* parser for configure descriptor */
  109. typedef struct
  110. {
  111. #define FUSB_USBSTR_MIN_LEN 4
  112. FUsbStringDescriptor *usb_str;
  113. #define FUSB_STRDESC_BUF_MAX 64
  114. char str_buf[FUSB_STRDESC_BUF_MAX];
  115. } FUsbStringParser; /* parser for string descriptor */
  116. typedef int FUsbDevAddr;
  117. typedef struct _FUsbDev
  118. {
  119. FUsbHc *controller; /* Hc instance where device attached */
  120. FUsbEndpoint endpoints[FUSB_MAX_EP_NUM]; /* all Ep instance of device */
  121. int num_endp; /* num of Ep in use */
  122. FUsbDevAddr address; /* USB address */
  123. FUsbDevClass class; /* USB device class, e.g hid */
  124. int hub; /* hub where device is attached to */
  125. int port; /* port where device is attached */
  126. FUsbSpeed speed; /* speed type of device */
  127. void *data; /* private data for specific type of device */
  128. FUsbDeviceDescriptor *descriptor; /* device descriptor ever get from device hw */
  129. FUsbConfigurationDescriptor *configuration; /* configure descriptor followed with interface descriptor ever get from device hw */
  130. FUsbConfigParser config_parser; /* parser for configure descriptor */
  131. FUsbStringParser string_parser; /* parser for string descriptor */
  132. void (*init)(FUsbDev *dev); /* device init function of specific device type for register */
  133. void (*destroy)(FUsbDev *dev); /* device deinit function of specific device type for register */
  134. void (*poll)(FUsbDev *dev); /* device poll function of specific device type for register */
  135. } FUsbDev; /* encapsulates a single USB device */
  136. typedef enum
  137. {
  138. FUSB_HC_OHCI = 0,
  139. FUSB_HC_UHCI = 1,
  140. FUSB_HC_EHCI = 2,
  141. FUSB_HC_XHCI = 3,
  142. FUSB_HC_DWC2 = 4
  143. } FUsbHcType;
  144. typedef struct _FUsbHc
  145. {
  146. uintptr reg_base; /* base address of Hc register */
  147. FUsb *usb; /* instance of USB system */
  148. FUsbHcType type; /* type of Hc, e.g XHCI */
  149. FUsbDev *devices[FUSB_MAX_DEV_NUM]; /* dev 0 is root hub, 127 is last addressable */
  150. /* start(): Resume operation. */
  151. void (*start)(FUsbHc *controller);
  152. /* stop(): Stop operation but keep controller initialized. */
  153. void (*stop)(FUsbHc *controller);
  154. /* reset(): Perform a controller reset. The controller needs to
  155. be (re)initialized afterwards to work (again). */
  156. FUsbTransCode(*reset)(FUsbHc *controller);
  157. /* init(): Initialize a (previously reset) controller
  158. to a working state. */
  159. void (*init)(FUsbHc *controller);
  160. /* shutdown(): Stop operation, detach host controller and shutdown
  161. this driver instance. After calling shutdown() any
  162. other usage of this hci_t* is invalid. */
  163. void (*shutdown)(FUsbHc *controller);
  164. FUsbTransCode(*bulk)(FUsbEndpoint *ep, int size, u8 *data, int finalize);
  165. FUsbTransCode(*control)(FUsbDev *dev, FUsbDirection pid, int dr_length,
  166. void *devreq, int data_length, u8 *data);
  167. void *(*create_intr_queue)(FUsbEndpoint *ep, int reqsize, int reqcount, int reqtiming);
  168. void (*destroy_intr_queue)(FUsbEndpoint *ep, void *queue);
  169. u8 *(*poll_intr_queue)(void *queue);
  170. void *instance; /* instance to specific Hc implementation, e.g XHCI */
  171. /* set_address(): Tell the USB device its address (xHCI
  172. controllers want to do this by
  173. themselves). Also, allocate the FUsbDev
  174. structure, initialize enpoint 0
  175. (including MPS) and return it. */
  176. FUsbDev *(*set_address)(FUsbHc *controller, FUsbSpeed speed,
  177. int hubport, int hubaddr);
  178. /* finish_device_config(): Another hook for xHCI, returns 0 on success. */
  179. int (*finish_device_config)(FUsbDev *dev);
  180. /* destroy_device(): Finally, destroy all structures that
  181. were allocated during set_address()
  182. and finish_device_config(). */
  183. void (*destroy_device)(FUsbHc *controller, int devaddr);
  184. } FUsbHc; /* encapsulates a single USB host */
  185. typedef struct
  186. {
  187. void *(*malloc_align)(size_t size, size_t align);
  188. void (*free)(void *mem);
  189. } FUsbMemAllocator; /* memory allocator used in USB system */
  190. typedef struct
  191. {
  192. u32 instance_id; /* id for this USB system */
  193. uintptr base_addr; /* base addr of Hc register, set as 0 for pci-usb */
  194. u32 irq_num;
  195. u32 irq_priority;
  196. FUsbMemAllocator allocator; /* memory allocator to support dynamic memory */
  197. } FUsbConfig; /* configure data of the USB system */
  198. typedef enum
  199. {
  200. FUSB_STANDARD_INTERFACE,
  201. FUSB_VENDOR_SPECIFIED
  202. } FUsbDevCategory;
  203. typedef struct
  204. {
  205. FUsbDevCategory category;
  206. FUsbDevClass class;
  207. u32 sub_class;
  208. u32 protocol;
  209. } FUsbDevIndex;
  210. typedef void (* FUsbDevInitHandler)(FUsbDev *dev);
  211. typedef struct
  212. {
  213. FUsbDevIndex index;
  214. FUsbDevInitHandler handler;
  215. } FUsbDevInitFunc;
  216. typedef struct _FUsb
  217. {
  218. FUsbConfig config; /* configuration of USB system */
  219. void *pcie_instance; /* NULL if unused */
  220. void *pcie_info[FUSB_MAX_CTRL_NUM]; /* NULL if unused */
  221. FUsbHc *hc; /* first hc, there might have multiple hc in pcie-mode */
  222. /* hook to set init function for specific device type */
  223. FUsbDevInitFunc dev_init[FUSB_MAX_DEV_TYPE_NUM];
  224. u32 dev_init_num; /* number of init function in used */
  225. u32 is_ready; /* indicator of system okay */
  226. } FUsb; /* instance of the USB system */
  227. /************************** Variable Definitions *****************************/
  228. /***************** Macros (Inline Functions) Definitions *********************/
  229. static FUsbDev *FUsbGetDevEntry(FUsbHc *controller, int dev_address)
  230. {
  231. FASSERT(controller && controller->devices);
  232. FUsbDev *result = NULL;
  233. u32 loop;
  234. for (loop = 0; loop < FUSB_MAX_DEV_NUM; loop++)
  235. {
  236. if ((NULL != controller->devices[loop]) && (dev_address == controller->devices[loop]->address))
  237. {
  238. result = controller->devices[loop];
  239. break;
  240. }
  241. }
  242. return result;
  243. }
  244. /*
  245. * returns the speed is above FUSB_SUPER_SPEED or not
  246. */
  247. static inline boolean FUsbIsSuperSpeed(FUsbSpeed speed)
  248. {
  249. return (speed == FUSB_SUPER_SPEED || speed == FUSB_SUPER_SPEED_PLUS);
  250. }
  251. static inline unsigned char FUsbGenerateReqType(FUsbReqDirection dir, FUsbReqType type, FUsbReqRecpient recp)
  252. {
  253. return (dir << 7) | (type << 5) | recp;
  254. }
  255. /************************** Function Prototypes ******************************/
  256. /* 获取USB的默认配置 */
  257. const FUsbConfig *FUsbLookupConfig(u32 instance_id);
  258. /* 初始化USB实例 */
  259. FError FUsbCfgInitialize(FUsb *instance, const FUsbConfig *config);
  260. /* 去初始化USB实例 */
  261. void FUsbDeInitialize(FUsb *instance);
  262. /* 轮询所有的USB控制器连接的所有设备, 更新设备拓扑 */
  263. void FUsbPoll(FUsb *instance);
  264. /* 关闭所有的USB控制器,移除所有连接的设备 */
  265. void FUsbExit(FUsb *instance);
  266. /* 从USB内存池分配一块内存,并清零分配的空间 */
  267. void *FUsbMempAllocate(FUsb *instance, size_t size, size_t align);
  268. /* 释放从USB内存池分配的空间 */
  269. void FUsbMempFree(FUsb *instance, void *ptr);
  270. /* 指定特定USB设备的初始化函数,供创建USB设备实例时使用 */
  271. FError FUsbAssignDevInitFunc(FUsb *instance, const FUsbDevIndex *index, FUsbDevInitHandler handler);
  272. /* 获取USB控制器上连接的所有USB设备实例 */
  273. size_t FUsbGetAllDevEntries(FUsbHc *controller, FUsbDev *devs[], size_t max_dev_num);
  274. /* 标准USB主机请求,使能设备/接口/端点的某个特性 */
  275. FUsbTransCode FUsbSetFeature(FUsbDev *dev, int endp, int feature, int rtype);
  276. /* 标准USB主机请求,获取设备/接口/端点的状态 */
  277. FUsbTransCode FUsbGetStatus(FUsbDev *dev, int endp, int rtype, int len, void *data);
  278. /* 标准USB主机请求,获取指定描述符 */
  279. FUsbTransCode FUsbGetDescriptor(FUsbDev *dev, int rtype, FUsbDescriptorType descType, int descIdx,
  280. void *data, size_t len);
  281. /* USB主机请求,获取字符串描述符 */
  282. FUsbTransCode FUsbGetStringDescriptor(FUsbDev *dev, int rtype, FUsbDescriptorType desc_type, int desc_idx, int lang_id,
  283. void *data, size_t len);
  284. /* 标准USB主机请求,设置配置值 */
  285. FUsbTransCode FUsbSetConfiguration(FUsbDev *dev);
  286. /* 标准USB主机请求,去使能设备/接口/端点的某个特性 */
  287. FUsbTransCode FUsbClearFeature(FUsbDev *dev, int endp, int feature, int rtype);
  288. /* 打印描述符信息 */
  289. void FUsbDumpAllDescriptors(FUsbDev *dev);
  290. /* 从USB主机移除指定USB设备(USB设备驱动使用) */
  291. void FUsbDetachDev(FUsbHc *controller, int devno);
  292. /* 向USB主机添加USB设备(USB设备驱动使用) */
  293. FUsbDevAddr FUsbAttachDev(FUsbHc *controller, int hubaddress, int port,
  294. FUsbSpeed speed);
  295. /**
  296. * To be implemented by application. It's called by the USB
  297. * stack just before iterating over known devices to poll them for
  298. * status change.
  299. */
  300. void __attribute__((weak)) FUsbPollPrepare(FUsb *instance);
  301. /**
  302. * To be implemented by application. It's called by the USB
  303. * stack just before exit known Hc.
  304. */
  305. void __attribute__((weak)) FUsbExitPrepare(FUsb *instance);
  306. /**
  307. * To be implemented by application. It's called by the USB stack
  308. * when a new USB device is found which isn't claimed by a built in driver,
  309. * so the client has the chance to know about it.
  310. *
  311. * @param dev descriptor for the USB device
  312. */
  313. void __attribute__((weak)) FUsbGenericCreate(FUsbDev *dev);
  314. /**
  315. * To be implemented by application. It's called by the USB stack
  316. * when it finds out that a USB device is removed which wasn't claimed by a
  317. * built in driver.
  318. *
  319. * @param dev descriptor for the USB device
  320. */
  321. void __attribute__((weak)) FUsbGenericRemove(FUsbDev *dev);
  322. /* 支持带TAG的内存分配,用于跟踪动态内存使用 */
  323. #ifdef FMEMP_TAG_DEBUG
  324. void *FUsbMempAllocateTag(FUsb *instance, size_t size, size_t align, const char *file, unsigned long line, const char *msg);
  325. void FUsbMempFreeTag(FUsb *instance, void *ptr);
  326. #define FUSB_ALLOCATE(instance, size, align) FUsbMempAllocateTag((instance), (size), (align), __FILE__, __LINE__, "")
  327. #define FUSB_FREE(instance, ptr) FUsbMempFreeTag((instance), (ptr))
  328. #else
  329. #define FUSB_ALLOCATE(instance, size, align) FUsbMempAllocate((instance), (size), (align))
  330. #define FUSB_FREE(instance, ptr) FUsbMempFree((instance), (ptr))
  331. #endif
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif