usbd_core.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* This file is the part of the LUS32 project
  2. *
  3. * Copyright ©2016 Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** \file usbd_core.h
  16. * \brief Core and hardware driver framework.
  17. * \author Dmitry Filimonchuk <dmitrystu[at]gmail[dot]com>
  18. * \version 1.0
  19. * \copyright Apache License, Version 2.0
  20. */
  21. #ifndef _USBD_CORE_H_
  22. #define _USBD_CORE_H_
  23. #if defined(__cplusplus)
  24. extern "C" {
  25. #endif
  26. /** \addtogroup USBD_CORE USB device core
  27. * \brief Contains core and hardware driver framework definitions
  28. * @{ */
  29. #define USB_EPTYPE_DBLBUF 0x04 /**< indicates a doublebuffered endpoint (bulk endpoint only) */
  30. /** \name bmRequestType bitmapped field
  31. * @{ */
  32. #define USB_REQ_DIRECTION (1 << 7)
  33. #define USB_REQ_HOSTTODEV (0 << 7)
  34. #define USB_REQ_DEVTOHOST (1 << 7)
  35. #define USB_REQ_TYPE (3 << 5)
  36. #define USB_REQ_STANDARD (0 << 5)
  37. #define USB_REQ_CLASS (1 << 5)
  38. #define USB_REQ_VENDOR (2 << 5)
  39. #define USB_REQ_RECIPIENT (3 << 0)
  40. #define USB_REQ_DEVICE (0 << 0)
  41. #define USB_REQ_INTERFACE (1 << 0)
  42. #define USB_REQ_ENDPOINT (2 << 0)
  43. #define USB_REQ_OTHER (3 << 0)
  44. /** @} */
  45. /** \name USB device events
  46. * @{ */
  47. #define usbd_evt_reset 0 /**< Reset */
  48. #define usbd_evt_sof 1 /**< Start of frame */
  49. #define usbd_evt_susp 2 /**< Suspend */
  50. #define usbd_evt_wkup 3 /**< Wakeup */
  51. #define usbd_evt_eptx 4 /**< Data packet transmitted*/
  52. #define usbd_evt_eprx 5 /**< Data packet received */
  53. #define usbd_evt_epsetup 6 /**< Setup packet received */
  54. #define usbd_evt_error 7 /**< Data error */
  55. #define usbd_evt_esof 8 /**< Missed SOF */
  56. #define usbd_evt_count 9
  57. /** @} */
  58. /**\anchor USB_LANE_STATUS
  59. * \name USB lanes connection states
  60. * @{ */
  61. #define usbd_lane_unk 0 /**< Unknown or proprietary charger */
  62. #define usbd_lane_dsc 1 /**< Lanes disconnected */
  63. #define usbd_lane_sdp 2 /**< Connected to standard downstream port */
  64. #define usbd_lane_cdp 3 /**< Connected to charging downstream port */
  65. #define usbd_lane_dcp 4 /**< Connected to dedicated charging port */
  66. /** @} */
  67. /** \name USB HW capabilities
  68. * @{ */
  69. #define USBD_HW_ADDRFST (1 << 0) /**< Set address before STATUS_OUT */
  70. #define USBD_HW_BC (1 << 1) /**< Battery charging detection supported */
  71. /** @} */
  72. #if !defined(__ASSEMBLER__)
  73. /** USB device machine states */
  74. enum usbd_machine_state {
  75. usbd_state_disabled,
  76. usbd_state_disconnected,
  77. usbd_state_default, /**< Default */
  78. usbd_state_addressed, /**< Addressed */
  79. usbd_state_configured, /**< Configured */
  80. };
  81. /** USB device control endpoint machine state */
  82. enum usbd_ctl_state {
  83. usbd_ctl_idle, /**< Idle. Awaiting for SETUP packet */
  84. usbd_ctl_rxdata, /**< RX. Receiving DATA-OUT payload */
  85. usbd_ctl_txdata, /**< TX. Transmitting DATA-IN payload */
  86. usbd_ctl_ztxdata, /**< TX. Transmitting DATA-IN payload. Zero length packet maybe required. */
  87. usbd_ctl_lastdata, /**< TX. Last DATA-IN packed passed to buffer. Awaiting for the TX completion */
  88. usbd_ctl_statusin, /**< STATUS-IN stage */
  89. usbd_ctl_statusout, /**< STATUS-OUT stage */
  90. };
  91. /** Asynchronous device control commands */
  92. enum usbd_commands {
  93. usbd_cmd_enable, /**< Enables device */
  94. usbd_cmd_disable, /**< Disables device */
  95. usbd_cmd_connect, /**< Connects device to host */
  96. usbd_cmd_disconnect, /**< Disconnects device from host */
  97. usbd_cmd_reset, /**< Resets device */
  98. };
  99. /** Reporting status results */
  100. typedef enum _usbd_respond {
  101. usbd_fail, /**< Function has an error, STALLPID will be issued */
  102. usbd_ack, /**< Function completes request accepted ZLP or data will be send */
  103. usbd_nak, /**< Function is busy. NAK handshake */
  104. } usbd_respond;
  105. typedef struct _usbd_device usbd_device;
  106. typedef struct _usbd_ctlreq usbd_ctlreq;
  107. typedef struct _usbd_status usbd_status;
  108. /** \addtogroup USB_CORE_API Core API functions
  109. * @{ */
  110. /** Generic USB device event callback for events and endpoints processing
  111. * \param[in] dev pointer to USB device
  112. * \param event \ref usbd_evt "USB event"
  113. * \param ep active endpoint number
  114. * \note endpoints with same indexes i.e. 0x01 and 0x81 shares same callback.
  115. */
  116. typedef void (*usbd_evt_callback)(usbd_device *dev, uint8_t event, uint8_t ep);
  117. /** USB control transfer completed callback function.
  118. * \param[in] dev pointer to USB device
  119. * \param[in] req pointer to usb request structure
  120. * \note When this callback will be completed usbd_device#complete_callback will be reseted to NULL
  121. */
  122. typedef void (*usbd_rqc_callback)(usbd_device *dev, usbd_ctlreq *req);
  123. /** USB control callback function.
  124. * \details Uses for the control request processing.
  125. * Some requests will be handled by core if callback don't process it (returns FALSE). If request was not processed STALL PID will be issued.
  126. * - GET_CONFIGURATION
  127. * - SET_CONFIGURATION (passes to \ref usbd_cfg_callback)
  128. * - GET_DESCRIPTOR (passes to \ref usbd_dsc_callback)
  129. * - GET_STATUS
  130. * - SET_FEATURE, CLEAR_FEATURE (endpoints only)
  131. * - SET_ADDRESS
  132. * \param[in] dev points to USB device
  133. * \param[in] req points to usb control request
  134. * \param[out] *callback \ref usbd_rqc_callback "pointer to USB control transfer completed callback", default is NULL (no callback)
  135. * \return usbd_respond status.
  136. */
  137. typedef usbd_respond (*usbd_ctl_callback)(usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback);
  138. /** USB get descriptor callback function
  139. * \details Called when GET_DESCRIPTOR request issued
  140. * \param[in] req pointer to usb control request structure
  141. * \param[in,out] address pointer to the descriptor in memory. Points to req->data by default. You can use this buffer.
  142. * \param[in,out] dsize descriptor size. maximum buffer size by default.
  143. * \return usbd_ack if you passed the correct descriptor, usbd_fail otherwise.
  144. */
  145. typedef usbd_respond (*usbd_dsc_callback)(usbd_ctlreq *req, void **address, uint16_t *dsize);
  146. /** USB set configuration callback function
  147. * \details called when SET_CONFIGURATION request issued
  148. * \param[in] dev pointer to USB device
  149. * \param[in] cfg configuration number.
  150. * \note if config is 0 device endpoints should be de-configured
  151. * \return TRUE if success
  152. */
  153. typedef usbd_respond (*usbd_cfg_callback)(usbd_device *dev, uint8_t cfg);
  154. /** @} */
  155. /**\addtogroup USB_HW_API Hardware driver API functions
  156. * @{ */
  157. /** Enables or disables USB hardware
  158. * \param enable Enables USB when TRUE disables otherwise
  159. */
  160. typedef void (*usbd_hw_enable)(bool enable);
  161. /** Resets USB hardware */
  162. typedef void (*usbd_hw_reset)(void);
  163. /** Connects or disconnects USB hardware to/from usb host
  164. * \param connect Connects USB to host if TRUE, disconnects otherwise
  165. * \return lanes connection status. \ref USB_LANES_STATUS
  166. */
  167. typedef uint8_t (*usbd_hw_connect)(bool connect);
  168. /** Sets USB hardware address
  169. * \param address USB address
  170. */
  171. typedef void (*usbd_hw_setaddr)(uint8_t address);
  172. /** Configures endpoint
  173. * \param ep endpoint address. Use USB_EPDIR_ macros to set endpoint direction
  174. * \param eptype endpoint type. Use USB_EPTYPE_* macros.
  175. * \param epsize endpoint size in bytes
  176. * \return TRUE if success
  177. */
  178. typedef bool (*usbd_hw_ep_config)(uint8_t ep, uint8_t eptype, uint16_t epsize);
  179. /** De-configures, cleans and disables endpoint
  180. * \param ep endpoint index
  181. * \note if you have two one-direction single-buffered endpoints with same index (i.e. 0x02 and 0x82) both will be deconfigured.
  182. */
  183. typedef void (*usbd_hw_ep_deconfig)(uint8_t ep);
  184. /** Reads data from OUT or control endpoint
  185. * \param ep endpoint index, should belong to OUT or CONTROL endpoint.
  186. * \param buf pointer to read buffer
  187. * \param blen size of the read buffer in bytes
  188. * \return size of the actually received data, -1 on error.
  189. */
  190. typedef int32_t (*usbd_hw_ep_read)(uint8_t ep, void *buf, uint16_t blen);
  191. /** Writes data to IN or control endpoint
  192. * \param ep endpoint index, hould belong to IN or CONTROL endpoint
  193. * \param buf pointer to data buffer
  194. * \param blen size of data will be written
  195. * \return number of written bytes
  196. */
  197. typedef int32_t (*usbd_hw_ep_write)(uint8_t ep, void *buf, uint16_t blen);
  198. /** Stalls and unstalls endpoint
  199. * \param ep endpoint address
  200. * \param stall endpoint will be stalled if TRUE and unstalled otherwise.
  201. * \note Has no effect on inactive endpoints.
  202. */
  203. typedef void (*usbd_hw_ep_setstall)(uint8_t ep, bool stall);
  204. /** Checks endpoint for stalled state
  205. * \param ep endpoint address
  206. * \return TRUE if endpoint is stalled
  207. */
  208. typedef bool (*usbd_hw_ep_isstalled)(uint8_t ep);
  209. /** Polls USB hardware for the events
  210. * \param[in] dev pointer to usb device structure
  211. * \param drv_callback callback to event processing subroutine
  212. */
  213. typedef void (*usbd_hw_poll)(usbd_device *dev, usbd_evt_callback drv_callback);
  214. /** Gets frame number from usb hardware
  215. */
  216. typedef uint16_t (*usbd_hw_get_frameno)(void);
  217. /** Makes a string descriptor contains unique serial number from hardware ID's
  218. * \param[in] buffer pointer to buffer for the descriptor
  219. * \return of the descriptor in bytes
  220. */
  221. typedef uint16_t (*usbd_hw_get_serialno)(void *buffer);
  222. /** @} */
  223. /** Represents generic USB control request */
  224. struct _usbd_ctlreq {
  225. uint8_t bmRequestType; /**< This bitmapped field identifies the characteristics of the specific request. */
  226. uint8_t bRequest; /**< This field specifies the particular request. */
  227. uint16_t wValue; /**< It is used to pass a parameter to the device, specific to the request. */
  228. uint16_t wIndex; /**< It is used to pass a parameter to the device, specific to the request. */
  229. uint16_t wLength; /**< This field specifies the length of the data transferred during the second phase of the control transfer */
  230. uint8_t data[]; /**< Data payload */
  231. };
  232. /** USB device status data */
  233. struct _usbd_status {
  234. void *data_buf; /**< Pointer to data buffer used for control requests */
  235. void *data_ptr; /**< Pointer to current data for control request */
  236. uint16_t data_count; /**< Count remained data for control request */
  237. uint16_t data_maxsize; /**< Size of the data buffer for control requests */
  238. uint8_t ep0size; /**< Size of the control endpoint */
  239. uint8_t device_cfg; /**< Current device configuration number */
  240. uint8_t device_state; /**< Current \ref usbd_machine_state */
  241. uint8_t control_state; /**< Current \ref usbd_ctl_state */
  242. };
  243. /** Represents a hardware USB driver call table */
  244. struct usbd_driver {
  245. uint32_t caps; /**< HW capabilities **/
  246. usbd_hw_enable enable; /**< \copydoc usbd_hw_enable */
  247. usbd_hw_reset reset; /**< \copydoc usbd_hw_reset */
  248. usbd_hw_connect connect; /**< \copydoc usbd_hw_connect */
  249. usbd_hw_setaddr setaddr; /**< \copydoc usbd_hw_setaddr */
  250. usbd_hw_ep_config ep_config; /**< \copydoc usbd_hw_ep_config */
  251. usbd_hw_ep_deconfig ep_deconfig; /**< \copydoc usbd_hw_ep_deconfig */
  252. usbd_hw_ep_read ep_read; /**< \copydoc usbd_hw_ep_read */
  253. usbd_hw_ep_write ep_write; /**< \copydoc usbd_hw_ep_write */
  254. usbd_hw_ep_setstall ep_setstall; /**< \copydoc usbd_hw_ep_setstall */
  255. usbd_hw_ep_isstalled ep_isstalled; /**< \copydoc usbd_hw_ep_isstalled */
  256. usbd_hw_poll poll; /**< \copydoc usbd_hw_poll */
  257. usbd_hw_get_frameno frame_no; /**< \copydoc usbd_hw_get_frameno */
  258. usbd_hw_get_serialno get_serialno_desc; /**< \copydoc usbd_hw_get_serialno */
  259. };
  260. /** Represents a USB device data.
  261. */
  262. struct _usbd_device {
  263. const struct usbd_driver *driver;
  264. usbd_ctl_callback control_callback; /**< \copydoc usbd_ctl_callback */
  265. usbd_rqc_callback complete_callback; /**< \copydoc usbd_rqc_callback */
  266. usbd_cfg_callback config_callback; /**< \copydoc usbd_cfg_callback */
  267. usbd_dsc_callback descriptor_callback; /**< \copydoc usbd_dsc_callback */
  268. usbd_evt_callback events[usbd_evt_count]; /**< events callbacks array */
  269. usbd_evt_callback endpoint[8]; /**< endpoint callbacks array for tx, rx and setup events */
  270. usbd_status status; /**< \copydoc _usbd_status */
  271. };
  272. /** \addtogroup USB_CORE_API
  273. * @{ */
  274. /** Initializes device structure
  275. * \param dev USB device that will be initialized
  276. * \param drv Pointer to hardware driver
  277. * \param ep0size Control endpoint 0 size
  278. * \param buffer Pointer to control request data buffer (32-bit aligned)
  279. * \param bsize Size of the data buffer
  280. */
  281. inline static void usbd_init(usbd_device *dev, const struct usbd_driver *drv, const uint8_t ep0size, uint32_t *buffer, const uint16_t bsize) {
  282. dev->driver = drv;
  283. dev->status.ep0size = ep0size;
  284. dev->status.data_ptr = buffer;
  285. dev->status.data_buf = buffer;
  286. dev->status.data_maxsize = bsize - __builtin_offsetof(usbd_ctlreq, data);
  287. }
  288. /** Polls USB for events
  289. * \param dev Pointer to device structure
  290. * \note can be called as from main routine as from USB interrupt
  291. */
  292. void usbd_poll(usbd_device *dev);
  293. /** Asynchronous device control
  294. * \param dev USB device
  295. * \param cmd control command
  296. */
  297. void usbd_control(usbd_device *dev, enum usbd_commands cmd) __attribute__((deprecated));
  298. /** Register callback for all control requests
  299. * \param dev pointer to \ref usbd_device structure
  300. * \param cb pointer to user \ref usbd_ctl_callback
  301. */
  302. inline static void usbd_reg_control(usbd_device *dev, usbd_ctl_callback callback) {
  303. dev->control_callback = callback;
  304. }
  305. /** Register callback for SET_CONFIG control request
  306. * \param dev pointer to \ref usbd_device structure
  307. * \param cb pointer to user \ref usbd_cfg_callback
  308. */
  309. inline static void usbd_reg_config(usbd_device *dev, usbd_cfg_callback callback) {
  310. dev->config_callback = callback;
  311. }
  312. /** Register callback for GET_DESCRIPTOR control request
  313. * \param dev pointer to \ref usbd_device structure
  314. * \param cb pointer to user \ref usbd_ctl_callback
  315. */
  316. inline static void usbd_reg_descr(usbd_device *dev, usbd_dsc_callback callback) {
  317. dev->descriptor_callback = callback;
  318. }
  319. /** Configure endpoint
  320. * \param dev pointer to \ref usbd_device structure
  321. * \copydetails usbd_hw_ep_config
  322. */
  323. inline static bool usbd_ep_config(usbd_device *dev, uint8_t ep, uint8_t eptype, uint16_t epsize) {
  324. return dev->driver->ep_config(ep, eptype, epsize);
  325. }
  326. /** Deconfigure endpoint
  327. * \param dev pointer to \ref usbd_device structure
  328. * \copydetails usbd_hw_ep_deconfig
  329. */
  330. inline static void usbd_ep_deconfig(usbd_device *dev, uint8_t ep) {
  331. dev->driver->ep_deconfig(ep);
  332. }
  333. /** Register endpoint callback
  334. * \param dev pointer to \ref usbd_device structure
  335. * \param ep endpoint index
  336. * \param cb pointer to user \ref usbd_evt_callback callback for endpoint events
  337. */
  338. inline static void usbd_reg_endpoint(usbd_device *dev, uint8_t ep, usbd_evt_callback callback) {
  339. dev->endpoint[ep & 0x07] = callback;
  340. }
  341. /** Registers event callback
  342. * \param dev pointer to \ref usbd_device structure
  343. * \param evt device \ref usbd_evt "event" wants to be registered
  344. * \param cb pointer to user \ref usbd_evt_callback for this event
  345. */
  346. inline static void usbd_reg_event(usbd_device *dev, uint8_t evt, usbd_evt_callback callback) {
  347. dev->events[evt] = callback;
  348. }
  349. /** Write data to endpoint
  350. * \param dev pointer to \ref usbd_device structure
  351. * \copydetails usbd_hw_ep_write
  352. */
  353. inline static int32_t usbd_ep_write(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
  354. return dev->driver->ep_write(ep, buf, blen);
  355. }
  356. /** Read data from endpoint
  357. * \param dev pointer to \ref usbd_device structure
  358. * \copydetails usbd_hw_ep_read
  359. */
  360. inline static int32_t usbd_ep_read(usbd_device *dev, uint8_t ep, void *buf, uint16_t blen) {
  361. return dev->driver->ep_read(ep, buf, blen);
  362. }
  363. /** Stall endpoint
  364. * \param dev pointer to \ref usbd_device structure
  365. * \param ep endpoint address
  366. */
  367. inline static void usbd_ep_stall(usbd_device *dev, uint8_t ep) {
  368. dev->driver->ep_setstall(ep, 1);
  369. }
  370. /** Unstall endpoint
  371. * \param dev pointer to \ref usbd_device structure
  372. * \param ep endpoint address
  373. */
  374. inline static void usbd_ep_unstall(usbd_device *dev, uint8_t ep) {
  375. dev->driver->ep_setstall(ep, 0);
  376. }
  377. /** Enables or disables USB hardware
  378. * \param dev pointer to \ref usbd_device structure
  379. * \param enable Enables USB when TRUE disables otherwise
  380. */
  381. inline static void usbd_enable(usbd_device *dev, bool enable) {
  382. dev->driver->enable(enable);
  383. }
  384. /** Connects or disconnects USB hardware to/from usb host
  385. * \param dev pointer to \ref usbd_device structure
  386. * \param connect Connects USB to host if TRUE, disconnects otherwise
  387. * \return lanes connection status. \ref USB_LANES_STATUS
  388. */
  389. inline static uint8_t usbd_connect(usbd_device *dev, bool connect) {
  390. return dev->driver->connect(connect);
  391. }
  392. #endif //(__ASSEMBLER__)
  393. /** @} */
  394. /** @} */
  395. #if defined(__cplusplus)
  396. }
  397. #endif
  398. #endif //_USBD_STD_H_