usbdev.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /************************************************************************************
  2. * include/nuttx/usb/usbdev.h
  3. *
  4. * Copyright (C) 2008-2010, 2012-2013, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * NOTE: This interface was inspired by the Linux gadget interface by
  8. * David Brownell. That work was very helpful in determining a usable
  9. * partitioning of functionality between standard class drivers and various
  10. * implementations of USB controller drivers. This work, however, does
  11. * not derive directly from that work and is licensed differently.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * 3. Neither the name NuttX nor the names of its contributors may be
  24. * used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  34. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  35. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ************************************************************************************/
  41. #ifndef __INCLUDE_NUTTX_USB_USBDEV_H
  42. #define __INCLUDE_NUTTX_USB_USBDEV_H
  43. /************************************************************************************
  44. * Included Files
  45. ************************************************************************************/
  46. #include <nuttx/config.h>
  47. #include <sys/types.h>
  48. #include <stdint.h>
  49. #include <stdbool.h>
  50. #include <nuttx/usb/pl2303.h>
  51. #include <nuttx/usb/cdcacm.h>
  52. #include <nuttx/usb/usbmsc.h>
  53. #include <nuttx/usb/composite.h>
  54. /************************************************************************************
  55. * Pre-processor Definitions
  56. ************************************************************************************/
  57. /* Endpoint helpers *****************************************************************/
  58. /* Configure endpoint, making it usable. The class driver may deallocate or re-use
  59. * the 'desc' structure after returning:
  60. *
  61. * ep - the struct usbdev_ep_s instance obtained from allocep()
  62. * desc - A struct usb_epdesc_s instance describing the endpoint
  63. * last - true if this this last endpoint to be configured. Some hardware needs
  64. * to take special action when all of the endpoints have been configured.
  65. */
  66. #define EP_CONFIGURE(ep,desc,last) (ep)->ops->configure(ep,desc,last)
  67. /* The endpoint will no longer be used */
  68. #define EP_DISABLE(ep) (ep)->ops->disable(ep)
  69. /* Allocate/free I/O requests. Should not be called from interrupt processing! */
  70. #define EP_ALLOCREQ(ep) (ep)->ops->allocreq(ep)
  71. #define EP_FREEREQ(ep,req) (ep)->ops->freereq(ep,req)
  72. /* Allocate/free an I/O buffer. Should not be called from interrupt processing! */
  73. #ifdef CONFIG_USBDEV_DMA
  74. # define EP_ALLOCBUFFER(ep,nb) (ep)->ops->allocbuffer(ep,nb)
  75. # define EP_FREEBUFFER(ep,buf) (ep)->ops->freebuffer(ep,buf)
  76. #else
  77. # define EP_ALLOCBUFFER(ep,nb) malloc(nb)
  78. # define EP_FREEBUFFER(ep,buf) free(buf)
  79. #endif
  80. /* Submit an I/O request to the endpoint */
  81. #define EP_SUBMIT(ep,req) (ep)->ops->submit(ep,req)
  82. /* Cancel an I/O request previously sent to an endpoint */
  83. #define EP_CANCEL(ep,req) (ep)->ops->cancel(ep,req)
  84. /* Stall or resume an endpoint */
  85. #define EP_STALL(ep) (ep)->ops->stall(ep,false)
  86. #define EP_RESUME(ep) (ep)->ops->stall(ep,true)
  87. /* USB Device Driver Helpers ********************************************************/
  88. /* Allocate an endpoint:
  89. *
  90. * ep - 7-bit logical endpoint number (direction bit ignored). Zero means
  91. * that any endpoint matching the other requirements will suffice. The
  92. * assigned endpoint can be found in the eplog field.
  93. * in - true: IN (device-to-host) endpoint requested
  94. * eptype - Endpoint type. One of {USB_EP_ATTR_XFER_ISOC, USB_EP_ATTR_XFER_BULK,
  95. * USB_EP_ATTR_XFER_INT}
  96. */
  97. #define DEV_ALLOCEP(dev,ep,in,type) (dev)->ops->allocep(dev,ep,in,type)
  98. /* Release an endpoint */
  99. #define DEV_FREEEP(dev,ep) (dev)->ops->freeep(dev,ep)
  100. /* Returns the current frame number */
  101. #define DEV_GETFRAME(dev) (dev)->ops->getframe(dev)
  102. /* Tries to wake up the host connected to this device */
  103. #define DEV_WAKEUP(dev) (dev)->ops->wakeup(dev)
  104. /* Sets the device selfpowered feature */
  105. #define DEV_SETSELFPOWERED(dev) (dev)->ops->selfpowered(dev,true)
  106. /* Clears the device selfpowered feature */
  107. #define DEV_CLRSELFPOWERED(dev) (dev)->ops->selfpowered(dev, false)
  108. /* Software-controlled connect to USB host. All USB class drivers need to call
  109. * DEV_CONNECT() when they are ready to be enumerated. That is, (1) initially when
  110. * bound to the USB driver, and (2) after a USB reset.
  111. */
  112. #define DEV_CONNECT(dev) (dev)->ops->pullup ? (dev)->ops->pullup(dev,true) : -EOPNOTSUPP
  113. /* Software-controlled disconnect from USB host */
  114. #define DEV_DISCONNECT(dev) (dev)->ops->pullup ? (dev)->ops->pullup(dev,false) : -EOPNOTSUPP
  115. /* USB Class Driver Helpers *********************************************************/
  116. /* All may be called from interrupt handling logic except bind() and unbind() */
  117. /* Invoked when the driver is bound to a USB device driver. */
  118. #define CLASS_BIND(drvr,dev) (drvr)->ops->bind(drvr,dev)
  119. /* Invoked when the driver is unbound from a USB device driver */
  120. #define CLASS_UNBIND(drvr,dev) (drvr)->ops->unbind(drvr,dev)
  121. /* Invoked after all transfers have been stopped, when the host is disconnected. */
  122. #define CLASS_DISCONNECT(drvr,dev) (drvr)->ops->disconnect(drvr,dev)
  123. /* Invoked for ep0 control requests */
  124. #define CLASS_SETUP(drvr,dev,ctrl,dataout,outlen) \
  125. (drvr)->ops->setup(drvr,dev,ctrl,dataout,outlen)
  126. /* Invoked on USB suspend. */
  127. #define CLASS_SUSPEND(drvr,dev) \
  128. do { if ((drvr)->ops->suspend) (drvr)->ops->suspend(drvr,dev); } while (0)
  129. /* Invoked on USB resume */
  130. #define CLASS_RESUME(drvr,dev) \
  131. do { if ((drvr)->ops->resume) (drvr)->ops->resume(drvr,dev); } while (0)
  132. /* Maximum size of a request buffer */
  133. #define USBDEV_MAXREQUEUST UINT16_MAX
  134. /* Request flags */
  135. #define USBDEV_REQFLAGS_NULLPKT 1 /* Bit 0: Terminate w/short packet; null packet if necessary */
  136. /* Bits 1-7: Available */
  137. /************************************************************************************
  138. * Public Types
  139. ************************************************************************************/
  140. /* USB Controller Structures ********************************************************/
  141. /* usbdev_devinfo_s - describes the low level bindings of an usb device */
  142. struct usbdev_devinfo_s
  143. {
  144. int ninterfaces; /* Number of interfaces in the configuration */
  145. int ifnobase; /* Offset to Interface-IDs */
  146. int nstrings; /* Number of Strings */
  147. int strbase; /* Offset to String Numbers */
  148. int nendpoints; /* Number of Endpoints referenced in the following allay */
  149. int epno[5]; /* Array holding the endpoint configuration for this device */
  150. };
  151. #ifdef CONFIG_USBDEV_COMPOSITE
  152. struct composite_devdesc_s
  153. {
  154. #ifdef CONFIG_USBDEV_DUALSPEED
  155. CODE int16_t (*mkconfdesc)(FAR uint8_t *buf,
  156. FAR struct usbdev_devinfo_s *devinfo,
  157. uint8_t speed, uint8_t type);
  158. #else
  159. CODE int16_t (*mkconfdesc)(FAR uint8_t *buf,
  160. FAR struct usbdev_devinfo_s *devinfo);
  161. #endif
  162. CODE int (*mkstrdesc)(uint8_t id, FAR struct usb_strdesc_s *strdesc);
  163. CODE int (*classobject)(int minor,
  164. FAR struct usbdev_devinfo_s *devinfo,
  165. FAR struct usbdevclass_driver_s **classdev);
  166. CODE void (*uninitialize)(FAR struct usbdevclass_driver_s *classdev);
  167. int nconfigs; /* Number of configurations supported */
  168. int configid; /* The only supported configuration ID */
  169. int cfgdescsize; /* The size of the config descriptor */
  170. int minor;
  171. #ifdef CONFIG_COMPOSITE_MSFT_OS_DESCRIPTORS
  172. uint8_t msft_compatible_id[8];
  173. uint8_t msft_sub_id[8];
  174. #endif
  175. struct usbdev_devinfo_s devinfo;
  176. };
  177. #endif
  178. /* struct usbdev_req_s - describes one i/o request */
  179. struct usbdev_ep_s;
  180. struct usbdev_req_s
  181. {
  182. uint8_t *buf; /* Call: Buffer used for data; Return: Unchanged */
  183. uint8_t flags; /* See USBDEV_REQFLAGS_* definitions */
  184. uint16_t len; /* Call: Total length of data in buf; Return: Unchanged */
  185. uint16_t xfrd; /* Call: zero; Return: Bytes transferred so far */
  186. int16_t result; /* Call: zero; Return: Result of transfer (O or -errno) */
  187. /* Callback when the transfer completes */
  188. CODE void (*callback)(FAR struct usbdev_ep_s *ep, FAR struct usbdev_req_s *req);
  189. FAR void *priv; /* Used only by callee */
  190. };
  191. /* Endpoint-specific interface to USB controller hardware. */
  192. struct usbdev_epops_s
  193. {
  194. /* Configure/enable and disable endpoint */
  195. CODE int (*configure)(FAR struct usbdev_ep_s *ep,
  196. FAR const struct usb_epdesc_s *desc, bool last);
  197. CODE int (*disable)(FAR struct usbdev_ep_s *ep);
  198. /* Allocate and free I/O requests */
  199. CODE FAR struct usbdev_req_s *(*allocreq)(FAR struct usbdev_ep_s *ep);
  200. CODE void (*freereq)(FAR struct usbdev_ep_s *ep,
  201. FAR struct usbdev_req_s *req);
  202. /* Allocate and free I/O buffers */
  203. #ifdef CONFIG_USBDEV_DMA
  204. CODE FAR void *(*allocbuffer)(FAR struct usbdev_ep_s *ep, uint16_t nbytes);
  205. CODE void (*freebuffer)(FAR struct usbdev_ep_s *ep, FAR void *buf);
  206. #endif
  207. /* Submit and cancel I/O requests */
  208. CODE int (*submit)(FAR struct usbdev_ep_s *ep,
  209. FAR struct usbdev_req_s *req);
  210. CODE int (*cancel)(FAR struct usbdev_ep_s *ep,
  211. FAR struct usbdev_req_s *req);
  212. /* Stall or resume an endpoint */
  213. CODE int (*stall)(FAR struct usbdev_ep_s *ep, bool resume);
  214. };
  215. /* Representation of one USB endpoint */
  216. struct usbdev_ep_s
  217. {
  218. FAR const struct usbdev_epops_s *ops; /* Endpoint operations */
  219. uint8_t eplog; /* Logical endpoint address */
  220. uint16_t maxpacket; /* Maximum packet size for this endpoint */
  221. FAR void *priv; /* For use by class driver */
  222. };
  223. /* struct usbdev_s represents a usb device */
  224. struct usbdev_s;
  225. struct usbdev_ops_s
  226. {
  227. /* Allocate and free endpoints */
  228. CODE FAR struct usbdev_ep_s *(*allocep)(FAR struct usbdev_s *dev,
  229. uint8_t epphy, bool in, uint8_t eptype);
  230. CODE void (*freeep)(FAR struct usbdev_s *dev, FAR struct usbdev_ep_s *ep);
  231. /* Get the frame number from the last SOF */
  232. CODE int (*getframe)(FAR struct usbdev_s *dev);
  233. /* Hardware specific features */
  234. CODE int (*wakeup)(FAR struct usbdev_s *dev);
  235. CODE int (*selfpowered)(FAR struct usbdev_s *dev, bool selfpowered);
  236. CODE int (*pullup)(FAR struct usbdev_s *dev, bool enable);
  237. /* Device-specific I/O command support */
  238. CODE int (*ioctl)(FAR struct usbdev_s *dev, unsigned code,
  239. unsigned long param);
  240. };
  241. struct usbdev_s
  242. {
  243. FAR const struct usbdev_ops_s *ops; /* Access to hardware specific features */
  244. FAR struct usbdev_ep_s *ep0; /* Endpoint zero */
  245. uint8_t speed; /* Current speed of the host connection */
  246. uint8_t dualspeed:1; /* 1:supports high and full speed operation */
  247. };
  248. /* USB Device Class Implementations *************************************************/
  249. struct usbdevclass_driver_s;
  250. struct usbdevclass_driverops_s
  251. {
  252. CODE int (*bind)(FAR struct usbdevclass_driver_s *driver,
  253. FAR struct usbdev_s *dev);
  254. CODE void (*unbind)(FAR struct usbdevclass_driver_s *driver,
  255. FAR struct usbdev_s *dev);
  256. CODE int (*setup)(FAR struct usbdevclass_driver_s *driver,
  257. FAR struct usbdev_s *dev, FAR const struct usb_ctrlreq_s *ctrl,
  258. FAR uint8_t *dataout, size_t outlen);
  259. CODE void (*disconnect)(FAR struct usbdevclass_driver_s *driver,
  260. FAR struct usbdev_s *dev);
  261. CODE void (*suspend)(FAR struct usbdevclass_driver_s *driver,
  262. FAR struct usbdev_s *dev);
  263. CODE void (*resume)(FAR struct usbdevclass_driver_s *driver,
  264. FAR struct usbdev_s *dev);
  265. };
  266. struct usbdevclass_driver_s
  267. {
  268. FAR const struct usbdevclass_driverops_s *ops;
  269. uint8_t speed; /* Highest speed that the driver handles */
  270. };
  271. /************************************************************************************
  272. * Public Data
  273. ************************************************************************************/
  274. #undef EXTERN
  275. #if defined(__cplusplus)
  276. # define EXTERN extern "C"
  277. extern "C"
  278. {
  279. #else
  280. # define EXTERN extern
  281. #endif
  282. /************************************************************************************
  283. * Public Functions
  284. ************************************************************************************/
  285. /************************************************************************************
  286. * Name: usbdevclass_register
  287. *
  288. * Description:
  289. * Register a USB device class driver. The class driver's bind() method will be
  290. * called to bind it to a USB device driver.
  291. *
  292. ************************************************************************************/
  293. int usbdev_register(FAR struct usbdevclass_driver_s *driver);
  294. /************************************************************************************
  295. * Name: usbdev_unregister
  296. *
  297. * Description:
  298. * Un-register usbdev class driver.If the USB device is connected to a USB host,
  299. * it will first disconnect(). The driver is also requested to unbind() and clean
  300. * up any device state, before this procedure finally returns.
  301. *
  302. ************************************************************************************/
  303. int usbdev_unregister(FAR struct usbdevclass_driver_s *driver);
  304. /****************************************************************************
  305. * Name: usbdev_dma_alloc and usbdev_dma_free
  306. *
  307. * Description:
  308. * The USB class driver allocates packet I/O buffers for data transfer by
  309. * calling the driver allocbuffer() and freebuffer() methods. Those
  310. * methods are only available if CONFIG_USBDEV_DMA is defined in the
  311. * system configuration.
  312. *
  313. * If CONFIG_USBDEV_DMAMEMORY is also defined in the NuttX configuration,
  314. * then the driver implementations of the allocbuffer() and freebuffer()
  315. * methods may use board-specific usbdev_dma_alloc() and usbdev_dma_free().
  316. * If CONFIG_USBDEV_DMA and CONFIG_USBDEV_DMAMEMORY are both defined,
  317. * then the board-specific logic must provide the functions
  318. * usbdev_dma_alloc() and usbdev_dma_free() as prototyped below:
  319. * usbdev_dma_alloc() will allocate DMA-capable memory of the specified
  320. * size; usbdev_dma_free() is the corresponding function that will be
  321. * called to free the DMA-capable memory.
  322. *
  323. * This functions may be simple wrappers around gran_alloc() and
  324. * gran_free() (See nuttx/mm/gran.h). Note that the gran_free() function
  325. * does require the size of the allocation to be freed; that would need
  326. * to be managed in the board-specific logic.
  327. *
  328. ****************************************************************************/
  329. #if defined(CONFIG_USBDEV_DMA) && defined(CONFIG_USBDEV_DMAMEMORY)
  330. FAR void *usbdev_dma_alloc(size_t size);
  331. void usbdev_dma_free(FAR void *memory);
  332. #endif
  333. #undef EXTERN
  334. #if defined(__cplusplus)
  335. }
  336. #endif
  337. #endif /* __INCLUDE_NUTTX_USB_USBDEV_H */