usb_composite_device.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _USB__COMPOSITE_DEVICE_H_
  13. #define _USB__COMPOSITE_DEVICE_H_
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include <sys/queue.h>
  18. #include <usb/usb_device.h>
  19. #include <stdbool.h>
  20. #include "ql_api_common.h"
  21. struct composite_gadget_s;
  22. struct composite_func_s;
  23. /**
  24. * @brief Composite gadget driver
  25. */
  26. typedef struct composite_device_s cops_t;
  27. typedef struct
  28. {
  29. usbXfer_t *xfer;
  30. uint8_t *buffer;
  31. unsigned bufsize;
  32. } copsCtrl_t;
  33. typedef struct
  34. {
  35. uint32_t (*fill_desc)(struct composite_func_s *f, void *buf, uint32_t size);
  36. int (*bind)(struct composite_func_s *f, cops_t *cops, udc_t *udc);
  37. void (*unbind)(struct composite_func_s *f);
  38. void (*destroy)(struct composite_func_s *f);
  39. int (*setup)(struct composite_func_s *f, const usb_device_request_t *ctrl);
  40. int (*set_alt)(struct composite_func_s *f, uint8_t intf, uint8_t alt);
  41. void (*disable)(struct composite_func_s *f);
  42. void (*resume)(struct composite_func_s *f);
  43. void (*suspend)(struct composite_func_s *f);
  44. } copsFuncOpt_t;
  45. struct composite_func_s
  46. {
  47. uint32_t name;
  48. copsFuncOpt_t ops;
  49. TAILQ_ENTRY(composite_func_s) anchor;
  50. // will be set on function add to composite device
  51. cops_t *cops;
  52. udc_t *controller;
  53. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  54. uint8 port_mode;
  55. #endif
  56. };
  57. /**
  58. * @brief Usb function adapt composite driver framework
  59. */
  60. typedef struct composite_func_s copsFunc_t;
  61. #define CHECK_CF_OPS(f, o) ((f) && ((f)->ops.o))
  62. /**
  63. * @brief composite function fill its interface descriptors
  64. *
  65. * @param f the function
  66. * @param buf buffer to fill descriptors
  67. * @param size size of the buffer
  68. * @return
  69. * - (-1) fail
  70. * - otherwise the number of bytes actually filled
  71. */
  72. static inline uint32_t cfFillDesc(copsFunc_t *f, void *buf, uint32_t size)
  73. {
  74. if (size == 0 || buf == NULL)
  75. return 0;
  76. if (!CHECK_CF_OPS(f, fill_desc))
  77. return 0;
  78. return f->ops.fill_desc(f, buf, size);
  79. }
  80. /**
  81. * @brief composite function destroy
  82. *
  83. * @param f the composite function
  84. */
  85. static inline void cfDestroy(copsFunc_t *f)
  86. {
  87. if (CHECK_CF_OPS(f, destroy))
  88. return f->ops.destroy(f);
  89. }
  90. /**
  91. * @brief composite function process setup packet
  92. *
  93. * @param f the function
  94. * @param ctrl the setup packet
  95. * @return
  96. * - (-1) fail
  97. * - 0 success
  98. */
  99. static inline int cfSetup(copsFunc_t *f, const usb_device_request_t *ctrl)
  100. {
  101. if (CHECK_CF_OPS(f, setup) && ctrl != NULL)
  102. return f->ops.setup(f, ctrl);
  103. return -1;
  104. }
  105. /**
  106. * @brief composite function bind driver
  107. *
  108. * @param f the function
  109. * @param cops the composite device
  110. * @param udc the udc
  111. * @return
  112. * - (-1) fail
  113. * - 0 success
  114. */
  115. static inline int cfBind(copsFunc_t *f, cops_t *cops, udc_t *udc)
  116. {
  117. if (CHECK_CF_OPS(f, bind) && cops && udc)
  118. return f->ops.bind(f, cops, udc);
  119. return -1;
  120. }
  121. /**
  122. * @brief composite function unbind driver
  123. *
  124. * @param f the function
  125. */
  126. static inline void cfUnbind(copsFunc_t *f)
  127. {
  128. if (CHECK_CF_OPS(f, unbind))
  129. f->ops.unbind(f);
  130. }
  131. /**
  132. * @brief composite function set alternate
  133. *
  134. * @param f the function
  135. * @param intf the interface number
  136. * @param alt alternate setting number
  137. * @return
  138. * - (-1) fail
  139. * - 0 success
  140. */
  141. static inline int cfSetAlt(copsFunc_t *f, uint8_t intf, uint8_t alt)
  142. {
  143. if (CHECK_CF_OPS(f, set_alt))
  144. return f->ops.set_alt(f, intf, alt);
  145. return -1;
  146. }
  147. /**
  148. * @breif composite function disable
  149. *
  150. * @param f the function
  151. */
  152. static inline void cfDisable(copsFunc_t *f)
  153. {
  154. if (CHECK_CF_OPS(f, disable))
  155. f->ops.disable(f);
  156. }
  157. /**
  158. * @breif composite function suspend
  159. *
  160. * @param f the function
  161. */
  162. static inline void cfSuspend(copsFunc_t *f)
  163. {
  164. if (CHECK_CF_OPS(f, suspend))
  165. f->ops.suspend(f);
  166. }
  167. /**
  168. * @breif composite function resume
  169. *
  170. * @param f the function
  171. */
  172. static inline void cfResume(copsFunc_t *f)
  173. {
  174. if (CHECK_CF_OPS(f, resume))
  175. f->ops.resume(f);
  176. }
  177. /**
  178. * @brief Create a composite gadget driver
  179. *
  180. * @return
  181. * - NULL fail to create the driver
  182. * - other the composite driver
  183. */
  184. cops_t *copsCreate();
  185. /**
  186. * @brief Destroy a composite gadget driver
  187. *
  188. * @param c the composite driver
  189. */
  190. void copsDestroy(cops_t *c);
  191. /**
  192. * @brief Add an usb function to the composite driver
  193. *
  194. * @param c the composite driver
  195. * @param f the composite usb function
  196. * @return
  197. * - true success
  198. * - false fail
  199. */
  200. bool copsAddFunction(cops_t *c, copsFunc_t *f);
  201. /**
  202. * @brief Add an usb function to the composite driver
  203. *
  204. * @param c the composite driver
  205. * @param funcs the composite usb function list
  206. * @param count functions count
  207. * @return
  208. * - true success
  209. * - false fail
  210. */
  211. bool copsAddFunctions(cops_t *c, copsFunc_t **funcs, unsigned count);
  212. /**
  213. * @brief Remove all usb function from the composite driver
  214. *
  215. * @param c the composite driver
  216. */
  217. void copsRemoveAllFunction(cops_t *c);
  218. /**
  219. * @brief cops api for mtp notify vfs add file
  220. *
  221. * @param c the composite driver
  222. */
  223. void copsMtpNotify(cops_t *c);
  224. /**
  225. * @brief From composite driver get the gadget driver instance
  226. *
  227. * @param c the composite driver
  228. * @return
  229. * - NULL fail
  230. * - other the gadget driver instance
  231. */
  232. udevDrv_t *copsGetGadgetDriver(cops_t *c);
  233. /**
  234. * @brief Assign an interface number
  235. *
  236. * @param c the composite drvice
  237. * @param f the composite function
  238. * @return
  239. * - (-1) fail
  240. * - [0:15] the interface number
  241. */
  242. int copsAssignInterface(cops_t *c, copsFunc_t *f);
  243. /**
  244. * @brief Remove an interface
  245. *
  246. * @param c the composite device
  247. * @param index the interface number
  248. */
  249. void copsRemoveInterface(cops_t *c, uint8_t index);
  250. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  251. int copsAssignInterface_ex(cops_t *c, copsFunc_t *f);
  252. int quec_usb_assign_interface(uint8 intf, cops_t *c, copsFunc_t *f);
  253. #endif
  254. /**
  255. * @brief Allocate a string id
  256. * @note Caller must keep the strings room, composite device only store
  257. * the address.
  258. *
  259. * @param c the composite device
  260. * @param ustr the usb string (caller keep the memory)
  261. * @return the string id, 0 means no string
  262. */
  263. int copsAssignStringId(cops_t *c, usbString_t *ustr);
  264. /**
  265. * @brief Remove a string, giveback the id
  266. *
  267. * @param c the composite device
  268. * @prarm ustr the usb string (caller keep the memory)
  269. * @return
  270. * - 0 always return 0
  271. */
  272. int copsRemoveString(cops_t *c, usbString_t *ustr);
  273. /**
  274. * @brief Set vendor id for this composite device
  275. *
  276. * @param c the composite device
  277. * @param vid vid
  278. */
  279. void copsSetVendorId(cops_t *c, uint16_t vid);
  280. /**
  281. * @brief Set product id for this composite device
  282. *
  283. * @param c the composite device
  284. * @param pid pid
  285. */
  286. void copsSetProductId(cops_t *c, uint16_t pid);
  287. /**
  288. * @brief get ctrl buffer/xfer
  289. *
  290. * @param c the composite device
  291. * @return
  292. * - NonNull for cops ctrl data
  293. */
  294. copsCtrl_t *copsGetCtrl(cops_t *c);
  295. #ifdef __cplusplus
  296. }
  297. #endif
  298. #endif