usbmsc_desc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /****************************************************************************
  2. * drivers/usbdev/usbmsc_desc.c
  3. *
  4. * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <nuttx/usb/usb.h>
  45. #include <nuttx/usb/usbdev_trace.h>
  46. #include "usbmsc.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Private Types
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Function Prototypes
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Data
  58. ****************************************************************************/
  59. /* Descriptors **************************************************************/
  60. /* Device descriptor. If the USB mass storage device is configured as part
  61. * of a composite device, then the device descriptor will be provided by the
  62. * composite device logic.
  63. */
  64. #ifndef CONFIG_USBMSC_COMPOSITE
  65. static const struct usb_devdesc_s g_devdesc =
  66. {
  67. USB_SIZEOF_DEVDESC, /* len */
  68. USB_DESC_TYPE_DEVICE, /* type */
  69. {LSBYTE(0x0200), MSBYTE(0x0200)}, /* usb */
  70. USB_CLASS_PER_INTERFACE, /* classid */
  71. 0, /* subclass */
  72. 0, /* protocol */
  73. CONFIG_USBMSC_EP0MAXPACKET, /* maxpacketsize */
  74. { /* vendor */
  75. LSBYTE(CONFIG_USBMSC_VENDORID),
  76. MSBYTE(CONFIG_USBMSC_VENDORID)
  77. },
  78. { /* product */
  79. LSBYTE(CONFIG_USBMSC_PRODUCTID),
  80. MSBYTE(CONFIG_USBMSC_PRODUCTID) },
  81. { /* device */
  82. LSBYTE(CONFIG_USBMSC_VERSIONNO),
  83. MSBYTE(CONFIG_USBMSC_VERSIONNO)
  84. },
  85. USBMSC_MANUFACTURERSTRID, /* imfgr */
  86. USBMSC_PRODUCTSTRID, /* iproduct */
  87. USBMSC_SERIALSTRID, /* serno */
  88. USBMSC_NCONFIGS /* nconfigs */
  89. };
  90. #endif
  91. /* Configuration descriptor If the USB mass storage device is configured as part
  92. * of a composite device, then the configuration descriptor will be provided by the
  93. * composite device logic.
  94. */
  95. #ifndef CONFIG_USBMSC_COMPOSITE
  96. static const struct usb_cfgdesc_s g_cfgdesc =
  97. {
  98. USB_SIZEOF_CFGDESC, /* len */
  99. USB_DESC_TYPE_CONFIG, /* type */
  100. { /* totallen */
  101. LSBYTE(SIZEOF_USBMSC_CFGDESC),
  102. MSBYTE(SIZEOF_USBMSC_CFGDESC)
  103. },
  104. USBMSC_NINTERFACES, /* ninterfaces */
  105. USBMSC_CONFIGID, /* cfgvalue */
  106. USBMSC_CONFIGSTRID, /* icfg */
  107. USB_CONFIG_ATTR_ONE|SELFPOWERED|REMOTEWAKEUP, /* attr */
  108. (CONFIG_USBDEV_MAXPOWER + 1) / 2 /* mxpower */
  109. };
  110. #endif
  111. /* Single interface descriptor */
  112. static const struct usb_ifdesc_s g_ifdesc =
  113. {
  114. USB_SIZEOF_IFDESC, /* len */
  115. USB_DESC_TYPE_INTERFACE, /* type */
  116. USBMSC_INTERFACEID, /* ifno */
  117. USBMSC_ALTINTERFACEID, /* alt */
  118. USBMSC_NENDPOINTS, /* neps */
  119. USB_CLASS_MASS_STORAGE, /* classid */
  120. USBMSC_SUBCLASS_SCSI, /* subclass */
  121. USBMSC_PROTO_BULKONLY, /* protocol */
  122. USBMSC_INTERFACESTRID /* iif */
  123. };
  124. /* Endpoint descriptors */
  125. static const struct usb_epdesc_s g_fsepbulkoutdesc =
  126. {
  127. USB_SIZEOF_EPDESC, /* len */
  128. USB_DESC_TYPE_ENDPOINT, /* type */
  129. USBMSC_EPOUTBULK_ADDR, /* addr */
  130. USBMSC_EPOUTBULK_ATTR, /* attr */
  131. { /* maxpacket */
  132. LSBYTE(USBMSC_FSBULKMAXPACKET),
  133. MSBYTE(USBMSC_FSBULKMAXPACKET)
  134. },
  135. 0 /* interval */
  136. };
  137. static const struct usb_epdesc_s g_fsepbulkindesc =
  138. {
  139. USB_SIZEOF_EPDESC, /* len */
  140. USB_DESC_TYPE_ENDPOINT, /* type */
  141. USBMSC_EPINBULK_ADDR, /* addr */
  142. USBMSC_EPINBULK_ATTR, /* attr */
  143. { /* maxpacket */
  144. LSBYTE(USBMSC_FSBULKMAXPACKET),
  145. MSBYTE(USBMSC_FSBULKMAXPACKET)
  146. },
  147. 0 /* interval */
  148. };
  149. #ifdef CONFIG_USBDEV_DUALSPEED
  150. #ifndef CONFIG_USBMSC_COMPOSITE
  151. static const struct usb_qualdesc_s g_qualdesc =
  152. {
  153. USB_SIZEOF_QUALDESC, /* len */
  154. USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
  155. { /* usb */
  156. LSBYTE(0x0200),
  157. MSBYTE(0x0200)
  158. },
  159. USB_CLASS_PER_INTERFACE, /* classid */
  160. 0, /* subclass */
  161. 0, /* protocol */
  162. CONFIG_USBMSC_EP0MAXPACKET, /* mxpacketsize */
  163. USBMSC_NCONFIGS, /* nconfigs */
  164. 0, /* reserved */
  165. };
  166. #endif
  167. static const struct usb_epdesc_s g_hsepbulkoutdesc =
  168. {
  169. USB_SIZEOF_EPDESC, /* len */
  170. USB_DESC_TYPE_ENDPOINT, /* type */
  171. USBMSC_EPOUTBULK_ADDR, /* addr */
  172. USBMSC_EPOUTBULK_ATTR, /* attr */
  173. { /* maxpacket */
  174. LSBYTE(USBMSC_HSBULKMAXPACKET),
  175. MSBYTE(USBMSC_HSBULKMAXPACKET)
  176. },
  177. 0 /* interval */
  178. };
  179. static const struct usb_epdesc_s g_hsepbulkindesc =
  180. {
  181. USB_SIZEOF_EPDESC, /* len */
  182. USB_DESC_TYPE_ENDPOINT, /* type */
  183. USBMSC_EPINBULK_ADDR, /* addr */
  184. USBMSC_EPINBULK_ATTR, /* attr */
  185. { /* maxpacket */
  186. LSBYTE(USBMSC_HSBULKMAXPACKET),
  187. MSBYTE(USBMSC_HSBULKMAXPACKET)
  188. },
  189. 0 /* interval */
  190. };
  191. #endif
  192. /****************************************************************************
  193. * Public Data
  194. ****************************************************************************/
  195. /* Strings ******************************************************************/
  196. #ifndef CONFIG_USBMSC_COMPOSITE
  197. const char g_mscvendorstr[] = CONFIG_USBMSC_VENDORSTR;
  198. const char g_mscproductstr[] = CONFIG_USBMSC_PRODUCTSTR;
  199. const char g_mscserialstr[] = CONFIG_USBMSC_SERIALSTR;
  200. #endif
  201. /****************************************************************************
  202. * Private Functions
  203. ****************************************************************************/
  204. /****************************************************************************
  205. * Public Functions
  206. ****************************************************************************/
  207. /****************************************************************************
  208. * Name: usbmsc_mkstrdesc
  209. *
  210. * Description:
  211. * Construct a string descriptor
  212. *
  213. ****************************************************************************/
  214. int usbmsc_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc)
  215. {
  216. const char *str;
  217. int len;
  218. int ndata;
  219. int i;
  220. switch (id)
  221. {
  222. #ifndef CONFIG_USBMSC_COMPOSITE
  223. case 0:
  224. {
  225. /* Descriptor 0 is the language id */
  226. strdesc->len = 4;
  227. strdesc->type = USB_DESC_TYPE_STRING;
  228. strdesc->data[0] = LSBYTE(USBMSC_STR_LANGUAGE);
  229. strdesc->data[1] = MSBYTE(USBMSC_STR_LANGUAGE);
  230. return 4;
  231. }
  232. case USBMSC_MANUFACTURERSTRID:
  233. str = g_mscvendorstr;
  234. break;
  235. case USBMSC_PRODUCTSTRID:
  236. str = g_mscproductstr;
  237. break;
  238. case USBMSC_SERIALSTRID:
  239. str = g_mscserialstr;
  240. break;
  241. #endif
  242. /* case USBMSC_CONFIGSTRID: */
  243. case USBMSC_INTERFACESTRID:
  244. str = CONFIG_USBMSC_CONFIGSTR;
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. /* The string is utf16-le. The poor man's utf-8 to utf16-le
  250. * conversion below will only handle 7-bit en-us ascii
  251. */
  252. len = strlen(str);
  253. for (i = 0, ndata = 0; i < len; i++, ndata += 2)
  254. {
  255. strdesc->data[ndata] = str[i];
  256. strdesc->data[ndata+1] = 0;
  257. }
  258. strdesc->len = ndata+2;
  259. strdesc->type = USB_DESC_TYPE_STRING;
  260. return strdesc->len;
  261. }
  262. /****************************************************************************
  263. * Name: usbmsc_getepdesc
  264. *
  265. * Description:
  266. * Return a pointer to the raw device descriptor
  267. *
  268. ****************************************************************************/
  269. #ifndef CONFIG_USBMSC_COMPOSITE
  270. FAR const struct usb_devdesc_s *usbmsc_getdevdesc(void)
  271. {
  272. return &g_devdesc;
  273. }
  274. #endif
  275. /****************************************************************************
  276. * Name: usbmsc_getepdesc
  277. *
  278. * Description:
  279. * Return a pointer to the raw endpoint descriptor (used for configuring
  280. * endpoints)
  281. *
  282. ****************************************************************************/
  283. FAR const struct usb_epdesc_s *usbmsc_getepdesc(enum usbmsc_epdesc_e epid)
  284. {
  285. switch (epid)
  286. {
  287. case USBMSC_EPFSBULKOUT: /* Full speed bulk OUT endpoint descriptor */
  288. return &g_fsepbulkoutdesc;
  289. case USBMSC_EPFSBULKIN: /* Full speed bulk IN endpoint descriptor */
  290. return &g_fsepbulkindesc;
  291. #ifdef CONFIG_USBDEV_DUALSPEED
  292. case USBMSC_EPHSBULKOUT: /* High speed bulk OUT endpoint descriptor */
  293. return &g_hsepbulkoutdesc;
  294. case USBMSC_EPHSBULKIN: /* High speed bulk IN endpoint descriptor */
  295. return &g_hsepbulkindesc;
  296. #endif
  297. default:
  298. return NULL;
  299. }
  300. };
  301. /****************************************************************************
  302. * Name: usbmsc_mkcfgdesc
  303. *
  304. * Description:
  305. * Construct the configuration descriptor
  306. *
  307. ****************************************************************************/
  308. #ifdef CONFIG_USBDEV_DUALSPEED
  309. int16_t usbmsc_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type)
  310. #else
  311. int16_t usbmsc_mkcfgdesc(uint8_t *buf)
  312. #endif
  313. {
  314. #ifdef CONFIG_USBDEV_DUALSPEED
  315. FAR const struct usb_epdesc_s *epdesc;
  316. bool hispeed = (speed == USB_SPEED_HIGH);
  317. uint16_t bulkmxpacket;
  318. #endif
  319. /* Configuration descriptor. If the USB mass storage device is
  320. * configured as part of a composite device, then the configuration
  321. * descriptor will be provided by the composite device logic.
  322. */
  323. #ifndef CONFIG_USBMSC_COMPOSITE
  324. memcpy(buf, &g_cfgdesc, USB_SIZEOF_CFGDESC);
  325. buf += USB_SIZEOF_CFGDESC;
  326. #endif
  327. /* Copy the canned interface descriptor */
  328. memcpy(buf, &g_ifdesc, USB_SIZEOF_IFDESC);
  329. buf += USB_SIZEOF_IFDESC;
  330. /* Make the two endpoint configurations */
  331. #ifdef CONFIG_USBDEV_DUALSPEED
  332. /* Check for switches between high and full speed */
  333. hispeed = (speed == USB_SPEED_HIGH);
  334. if (type == USB_DESC_TYPE_OTHERSPEEDCONFIG)
  335. {
  336. hispeed = !hispeed;
  337. }
  338. bulkmxpacket = USBMSC_BULKMAXPACKET(hispeed);
  339. epdesc = USBMSC_EPBULKINDESC(hispeed);
  340. memcpy(buf, epdesc, USB_SIZEOF_EPDESC);
  341. buf += USB_SIZEOF_EPDESC;
  342. epdesc = USBMSC_EPBULKOUTDESC(hispeed);
  343. memcpy(buf, epdesc, USB_SIZEOF_EPDESC);
  344. #else
  345. memcpy(buf, &g_fsepbulkoutdesc, USB_SIZEOF_EPDESC);
  346. buf += USB_SIZEOF_EPDESC;
  347. memcpy(buf, &g_fsepbulkindesc, USB_SIZEOF_EPDESC);
  348. #endif
  349. return SIZEOF_USBMSC_CFGDESC;
  350. }
  351. /****************************************************************************
  352. * Name: usbmsc_getqualdesc
  353. *
  354. * Description:
  355. * Return a pointer to the raw qual descriptor
  356. *
  357. ****************************************************************************/
  358. #if !defined(CONFIG_USBMSC_COMPOSITE) && defined(CONFIG_USBDEV_DUALSPEED)
  359. FAR const struct usb_qualdesc_s *usbmsc_getqualdesc(void)
  360. {
  361. return &g_qualdesc;
  362. }
  363. #endif