usbmsc.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /****************************************************************************
  2. * include/nuttx/usb/usbmsc.h
  3. *
  4. * Copyright (C) 2008-2010, 2012, 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_USBMSC_H
  42. #define __INCLUDE_NUTTX_USB_USBMSC_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. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /* Information about the device needed in usbdev_devinfo_s */
  54. #define USBMSC_CONFIGID (1) /* The only supported configuration ID */
  55. #define USBMSC_NENDPOINTS (2) /* Number of endpoints in the interface */
  56. #define USBMSC_EP_BULKIN_IDX (0)
  57. #define USBMSC_EP_BULKOUT_IDX (1)
  58. #define USBMSC_NCONFIGS (1) /* Number of configurations supported */
  59. #define USBMSC_NINTERFACES (1) /* Number of interfaces in the configuration */
  60. /****************************************************************************
  61. * Public Types
  62. ****************************************************************************/
  63. /****************************************************************************
  64. * Public Data
  65. ****************************************************************************/
  66. #undef EXTERN
  67. #if defined(__cplusplus)
  68. # define EXTERN extern "C"
  69. extern "C"
  70. {
  71. #else
  72. # define EXTERN extern
  73. #endif
  74. /****************************************************************************
  75. * Public Functions
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Name: usbmsc_configure
  79. *
  80. * Description:
  81. * One-time initialization of the USB storage driver. The initialization
  82. * sequence is as follows:
  83. *
  84. * 1. Call usbmsc_configure to perform one-time initialization specifying
  85. * the number of luns.
  86. * 2. Call usbmsc_bindlun to configure each supported LUN
  87. * 3. Call usbmsc_exportluns when all LUNs are configured
  88. *
  89. * Input Parameters:
  90. * nluns - the number of LUNs that will be registered
  91. * handle - Location to return a handle that is used in other API calls.
  92. *
  93. * Returned Value:
  94. * 0 on success; a negated errno on failure. The returned handle value is
  95. * an untyped equivalent to the usbmsc_classobject().
  96. *
  97. ****************************************************************************/
  98. int usbmsc_configure(unsigned int nluns, FAR void **handle);
  99. /****************************************************************************
  100. * Name: usbmsc_bindlun
  101. *
  102. * Description:
  103. * Bind the block driver specified by drvrpath to a USB storage LUN.
  104. *
  105. * Input Parameters:
  106. * handle - The handle returned by a previous call to
  107. * usbmsc_configure().
  108. * drvrpath - the full path to the block driver
  109. * startsector - A sector offset into the block driver to the start of the
  110. * partition on drvrpath (0 if no partitions)
  111. * nsectors - The number of sectors in the partition (if 0, all sectors
  112. * to the end of the media will be exported).
  113. * lunno - the LUN to bind to
  114. *
  115. * Returned Value:
  116. * 0 on success; a negated errno on failure.
  117. *
  118. ****************************************************************************/
  119. int usbmsc_bindlun(FAR void *handle, FAR const char *drvrpath, unsigned int lunno,
  120. off_t startsector, size_t nsectors, bool readonly);
  121. /****************************************************************************
  122. * Name: usbmsc_unbindlun
  123. *
  124. * Description:
  125. * Un-bind the block driver for the specified LUN
  126. *
  127. * Input Parameters:
  128. * handle - The handle returned by a previous call to usbmsc_configure().
  129. * lun - the LUN to unbind from
  130. *
  131. * Returned Value:
  132. * 0 on success; a negated errno on failure.
  133. *
  134. ****************************************************************************/
  135. int usbmsc_unbindlun(FAR void *handle, unsigned int lunno);
  136. /****************************************************************************
  137. * Name: usbmsc_exportluns
  138. *
  139. * Description:
  140. * After all of the LUNs have been bound, this function may be called in
  141. * order to export those LUNs in the USB storage device.
  142. *
  143. * Input Parameters:
  144. * handle - The handle returned by a previous call to usbmsc_configure().
  145. *
  146. * Returned Value:
  147. * 0 on success; a negated errno on failure
  148. *
  149. ****************************************************************************/
  150. int usbmsc_exportluns(FAR void *handle);
  151. /****************************************************************************
  152. * Name: usbmsc_classobject
  153. *
  154. * Description:
  155. * Register USB mass storage device and return the class object.
  156. *
  157. * Input Parameters:
  158. * classdev - The location to return the CDC serial class' device
  159. * instance.
  160. *
  161. * Returned Value:
  162. * 0 on success; a negated errno on failure
  163. *
  164. ****************************************************************************/
  165. #if defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_USBMSC_COMPOSITE)
  166. struct usbdevclass_driver_s;
  167. int usbmsc_classobject(FAR void *handle, FAR struct usbdev_devinfo_s *devinfo,
  168. FAR struct usbdevclass_driver_s **classdev);
  169. #endif
  170. /****************************************************************************
  171. * Name: usbmsc_uninitialize
  172. *
  173. * Description:
  174. * Un-initialize the USB storage class driver. The handle is the USB MSC
  175. * class' device object. This is the same value as returned by
  176. * usbmsc_classobject (typed) or by usbmsc_configure (untyped).
  177. *
  178. * Input Parameters:
  179. * handle - The handle returned by a previous call to usbmsc_configure()
  180. * (or usbmsc_classobject()).
  181. *
  182. * Returned Value:
  183. * None
  184. *
  185. ****************************************************************************/
  186. void usbmsc_uninitialize(FAR void *handle);
  187. /****************************************************************************
  188. * Name: usbmsc_get_composite_devdesc
  189. *
  190. * Description:
  191. * Helper function to fill in some constants into the composite configuration
  192. * structure.
  193. *
  194. * Input Parameters:
  195. * dev - Pointer to the configuration struct we should fill
  196. *
  197. * Returned Value:
  198. * None
  199. *
  200. ****************************************************************************/
  201. #if defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_USBMSC_COMPOSITE)
  202. struct composite_devdesc_s;
  203. void usbmsc_get_composite_devdesc(FAR struct composite_devdesc_s *dev);
  204. #endif
  205. #undef EXTERN
  206. #if defined(__cplusplus)
  207. }
  208. #endif
  209. #endif /* __INCLUDE_NUTTX_USB_USBMSC_H */