usbmsc.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  1. /****************************************************************************
  2. * drivers/usbdev/usbmsc.c
  3. *
  4. * Copyright (C) 2008-2012, 2016-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Mass storage class device. Bulk-only with SCSI subclass.
  8. *
  9. * References:
  10. * "Universal Serial Bus Mass Storage Class, Specification Overview,"
  11. * Revision 1.2, USB Implementer's Forum, June 23, 2003.
  12. *
  13. * "Universal Serial Bus Mass Storage Class, Bulk-Only Transport,"
  14. * Revision 1.0, USB Implementer's Forum, September 31, 1999.
  15. *
  16. * "SCSI Primary Commands - 3 (SPC-3)," American National Standard
  17. * for Information Technology, May 4, 2005
  18. *
  19. * "SCSI Primary Commands - 4 (SPC-4)," American National Standard
  20. * for Information Technology, July 19, 2008
  21. *
  22. * "SCSI Block Commands -2 (SBC-2)," American National Standard
  23. * for Information Technology, November 13, 2004
  24. *
  25. * "SCSI Multimedia Commands - 3 (MMC-3)," American National Standard
  26. * for Information Technology, November 12, 2001
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. *
  32. * 1. Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in
  36. * the documentation and/or other materials provided with the
  37. * distribution.
  38. * 3. Neither the name NuttX nor the names of its contributors may be
  39. * used to endorse or promote products derived from this software
  40. * without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  43. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  45. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  46. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  47. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  48. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  49. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  50. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  52. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  53. * POSSIBILITY OF SUCH DAMAGE.
  54. *
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Included Files
  58. ****************************************************************************/
  59. #include <nuttx/config.h>
  60. #include <sys/types.h>
  61. #include <stdint.h>
  62. #include <stdbool.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <unistd.h>
  66. #include <string.h>
  67. #include <errno.h>
  68. #include <queue.h>
  69. #include <debug.h>
  70. #include <nuttx/irq.h>
  71. #include <nuttx/kmalloc.h>
  72. #include <nuttx/kthread.h>
  73. #include <nuttx/arch.h>
  74. #include <nuttx/semaphore.h>
  75. #include <nuttx/fs/fs.h>
  76. #include <nuttx/usb/usb.h>
  77. #include <nuttx/usb/storage.h>
  78. #include <nuttx/usb/usbdev.h>
  79. #include <nuttx/usb/usbdev_trace.h>
  80. #include "usbmsc.h"
  81. #ifdef CONFIG_USBMSC_COMPOSITE
  82. # include <nuttx/usb/composite.h>
  83. # include "composite.h"
  84. #endif
  85. /****************************************************************************
  86. * Pre-processor Definitions
  87. ****************************************************************************/
  88. /****************************************************************************
  89. * Private Types
  90. ****************************************************************************/
  91. /* The internal version of the class driver */
  92. struct usbmsc_driver_s
  93. {
  94. struct usbdevclass_driver_s drvr;
  95. FAR struct usbmsc_dev_s *dev;
  96. };
  97. /* This is what is allocated */
  98. struct usbmsc_alloc_s
  99. {
  100. struct usbmsc_dev_s dev;
  101. struct usbmsc_driver_s drvr;
  102. };
  103. /****************************************************************************
  104. * Private Function Prototypes
  105. ****************************************************************************/
  106. /* Class Driver Support *****************************************************/
  107. static void usbmsc_ep0incomplete(FAR struct usbdev_ep_s *ep,
  108. FAR struct usbdev_req_s *req);
  109. static struct usbdev_req_s *usbmsc_allocreq(FAR struct usbdev_ep_s *ep,
  110. uint16_t len);
  111. static void usbmsc_freereq(FAR struct usbdev_ep_s *ep,
  112. FAR struct usbdev_req_s *req);
  113. /* Class Driver Operations (most at interrupt level) ************************/
  114. static int usbmsc_bind(FAR struct usbdevclass_driver_s *driver,
  115. FAR struct usbdev_s *dev);
  116. static void usbmsc_unbind(FAR struct usbdevclass_driver_s *driver,
  117. FAR struct usbdev_s *dev);
  118. static int usbmsc_setup(FAR struct usbdevclass_driver_s *driver,
  119. FAR struct usbdev_s *dev,
  120. FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
  121. size_t outlen);
  122. static void usbmsc_disconnect(FAR struct usbdevclass_driver_s *driver,
  123. FAR struct usbdev_s *dev);
  124. /* Initialization/Uninitialization ******************************************/
  125. static void usbmsc_lununinitialize(struct usbmsc_lun_s *lun);
  126. #if !defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_USBMSC_COMPOSITE)
  127. static int usbmsc_exportluns(FAR void *handle);
  128. #endif
  129. /****************************************************************************
  130. * Private Data
  131. ****************************************************************************/
  132. /* Driver operations ********************************************************/
  133. static struct usbdevclass_driverops_s g_driverops =
  134. {
  135. usbmsc_bind, /* bind */
  136. usbmsc_unbind, /* unbind */
  137. usbmsc_setup, /* setup */
  138. usbmsc_disconnect, /* disconnect */
  139. NULL, /* suspend */
  140. NULL /* resume */
  141. };
  142. /* Used to hand-off the state structure when the SCSI worker thread is started */
  143. FAR struct usbmsc_dev_s *g_usbmsc_handoff;
  144. /****************************************************************************
  145. * Public Data
  146. ****************************************************************************/
  147. /****************************************************************************
  148. * Private Functions
  149. ****************************************************************************/
  150. /****************************************************************************
  151. * Class Driver Support
  152. ****************************************************************************/
  153. /****************************************************************************
  154. * Name: usbmsc_ep0incomplete
  155. *
  156. * Description:
  157. * Handle completion of EP0 control operations
  158. *
  159. ****************************************************************************/
  160. static void usbmsc_ep0incomplete(FAR struct usbdev_ep_s *ep,
  161. FAR struct usbdev_req_s *req)
  162. {
  163. if (req->result || req->xfrd != req->len)
  164. {
  165. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_REQRESULT),
  166. (uint16_t)-req->result);
  167. }
  168. }
  169. /****************************************************************************
  170. * Name: usbmsc_allocreq
  171. *
  172. * Description:
  173. * Allocate a request instance along with its buffer
  174. *
  175. ****************************************************************************/
  176. static struct usbdev_req_s *usbmsc_allocreq(FAR struct usbdev_ep_s *ep,
  177. uint16_t len)
  178. {
  179. FAR struct usbdev_req_s *req;
  180. req = EP_ALLOCREQ(ep);
  181. if (req != NULL)
  182. {
  183. req->len = len;
  184. req->buf = EP_ALLOCBUFFER(ep, len);
  185. if (!req->buf)
  186. {
  187. EP_FREEREQ(ep, req);
  188. req = NULL;
  189. }
  190. }
  191. return req;
  192. }
  193. /****************************************************************************
  194. * Name: usbmsc_freereq
  195. *
  196. * Description:
  197. * Free a request instance along with its buffer
  198. *
  199. ****************************************************************************/
  200. static void usbmsc_freereq(FAR struct usbdev_ep_s *ep, struct usbdev_req_s *req)
  201. {
  202. if (ep != NULL && req != NULL)
  203. {
  204. if (req->buf != NULL)
  205. {
  206. EP_FREEBUFFER(ep, req->buf);
  207. }
  208. EP_FREEREQ(ep, req);
  209. }
  210. }
  211. /****************************************************************************
  212. * Class Driver Interfaces
  213. ****************************************************************************/
  214. /****************************************************************************
  215. * Name: usbmsc_bind
  216. *
  217. * Description:
  218. * Invoked when the driver is bound to a USB device driver
  219. *
  220. ****************************************************************************/
  221. static int usbmsc_bind(FAR struct usbdevclass_driver_s *driver,
  222. FAR struct usbdev_s *dev)
  223. {
  224. FAR struct usbmsc_dev_s *priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
  225. FAR struct usbmsc_req_s *reqcontainer;
  226. irqstate_t flags;
  227. int ret = OK;
  228. int i;
  229. usbtrace(TRACE_CLASSBIND, 0);
  230. /* Bind the structures */
  231. priv->usbdev = dev;
  232. /* Save the reference to our private data structure in EP0 so that it
  233. * can be recovered in ep0 completion events (Unless we are part of
  234. * a composite device and, in that case, the composite device owns
  235. * EP0).
  236. */
  237. #ifndef CONFIG_USBMSC_COMPOSITE
  238. dev->ep0->priv = priv;
  239. #endif
  240. /* The configured EP0 size should match the reported EP0 size. We could
  241. * easily adapt to the reported EP0 size, but then we could not use the
  242. * const, canned descriptors.
  243. */
  244. DEBUGASSERT(CONFIG_USBMSC_EP0MAXPACKET == dev->ep0->maxpacket);
  245. /* Preallocate control request */
  246. priv->ctrlreq = usbmsc_allocreq(dev->ep0, USBMSC_MXDESCLEN);
  247. if (priv->ctrlreq == NULL)
  248. {
  249. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCCTRLREQ), 0);
  250. ret = -ENOMEM;
  251. goto errout;
  252. }
  253. priv->ctrlreq->callback = usbmsc_ep0incomplete;
  254. /* Pre-allocate all endpoints... the endpoints will not be functional
  255. * until the SET CONFIGURATION request is processed in usbmsc_setconfig.
  256. * This is done here because there may be calls to kmm_malloc and the SET
  257. * CONFIGURATION processing probably occurs within interrupt handling
  258. * logic where kmm_malloc calls will fail.
  259. */
  260. /* Pre-allocate the IN bulk endpoint */
  261. priv->epbulkin = DEV_ALLOCEP(dev, USBMSC_MKEPBULKIN(&priv->devinfo),
  262. true, USB_EP_ATTR_XFER_BULK);
  263. if (!priv->epbulkin)
  264. {
  265. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKINALLOCFAIL), 0);
  266. ret = -ENODEV;
  267. goto errout;
  268. }
  269. priv->epbulkin->priv = priv;
  270. /* Pre-allocate the OUT bulk endpoint */
  271. priv->epbulkout = DEV_ALLOCEP(dev, USBMSC_MKEPBULKOUT(&priv->devinfo),
  272. false, USB_EP_ATTR_XFER_BULK);
  273. if (!priv->epbulkout)
  274. {
  275. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKOUTALLOCFAIL), 0);
  276. ret = -ENODEV;
  277. goto errout;
  278. }
  279. priv->epbulkout->priv = priv;
  280. /* Pre-allocate read requests */
  281. for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
  282. {
  283. reqcontainer = &priv->rdreqs[i];
  284. reqcontainer->req = usbmsc_allocreq(priv->epbulkout,
  285. CONFIG_USBMSC_BULKOUTREQLEN);
  286. if (reqcontainer->req == NULL)
  287. {
  288. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDALLOCREQ),
  289. (uint16_t)-ret);
  290. ret = -ENOMEM;
  291. goto errout;
  292. }
  293. reqcontainer->req->priv = reqcontainer;
  294. reqcontainer->req->callback = usbmsc_rdcomplete;
  295. }
  296. /* Pre-allocate write request containers and put in a free list */
  297. for (i = 0; i < CONFIG_USBMSC_NWRREQS; i++)
  298. {
  299. reqcontainer = &priv->wrreqs[i];
  300. reqcontainer->req = usbmsc_allocreq(priv->epbulkin,
  301. CONFIG_USBMSC_BULKINREQLEN);
  302. if (reqcontainer->req == NULL)
  303. {
  304. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRALLOCREQ),
  305. (uint16_t)-ret);
  306. ret = -ENOMEM;
  307. goto errout;
  308. }
  309. reqcontainer->req->priv = reqcontainer;
  310. reqcontainer->req->callback = usbmsc_wrcomplete;
  311. flags = enter_critical_section();
  312. sq_addlast((FAR sq_entry_t *)reqcontainer, &priv->wrreqlist);
  313. leave_critical_section(flags);
  314. }
  315. /* Report if we are selfpowered (unless we are part of a composite device) */
  316. #ifndef CONFIG_USBMSC_COMPOSITE
  317. #ifdef CONFIG_USBDEV_SELFPOWERED
  318. DEV_SETSELFPOWERED(dev);
  319. #endif
  320. /* And pull-up the data line for the soft connect function (unless we are
  321. * part of a composite device)
  322. */
  323. DEV_CONNECT(dev);
  324. #endif
  325. return OK;
  326. errout:
  327. usbmsc_unbind(driver, dev);
  328. return ret;
  329. }
  330. /****************************************************************************
  331. * Name: usbmsc_unbind
  332. *
  333. * Description:
  334. * Invoked when the driver is unbound from a USB device driver
  335. *
  336. ****************************************************************************/
  337. static void usbmsc_unbind(FAR struct usbdevclass_driver_s *driver,
  338. FAR struct usbdev_s *dev)
  339. {
  340. FAR struct usbmsc_dev_s *priv;
  341. FAR struct usbmsc_req_s *reqcontainer;
  342. irqstate_t flags;
  343. int i;
  344. usbtrace(TRACE_CLASSUNBIND, 0);
  345. #ifdef CONFIG_DEBUG_FEATURES
  346. if (!driver || !dev || !dev->ep0)
  347. {
  348. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDINVALIDARGS), 0);
  349. return;
  350. }
  351. #endif
  352. /* Extract reference to private data */
  353. priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
  354. #ifdef CONFIG_DEBUG_FEATURES
  355. if (!priv)
  356. {
  357. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND1), 0);
  358. return;
  359. }
  360. #endif
  361. /* The worker thread should have already been stopped by the
  362. * driver un-initialize logic.
  363. */
  364. DEBUGASSERT(priv->thstate == USBMSC_STATE_TERMINATED || priv->thstate == USBMSC_STATE_NOTSTARTED);
  365. /* Make sure that we are not already unbound */
  366. if (priv != NULL)
  367. {
  368. /* Make sure that the endpoints have been unconfigured. If
  369. * we were terminated gracefully, then the configuration should
  370. * already have been reset. If not, then calling usbmsc_resetconfig
  371. * should cause the endpoints to immediately terminate all
  372. * transfers and return the requests to us (with result == -ESHUTDOWN)
  373. */
  374. usbmsc_resetconfig(priv);
  375. up_mdelay(50);
  376. /* Free the pre-allocated control request */
  377. if (priv->ctrlreq != NULL)
  378. {
  379. usbmsc_freereq(dev->ep0, priv->ctrlreq);
  380. priv->ctrlreq = NULL;
  381. }
  382. /* Free pre-allocated read requests (which should all have
  383. * been returned to the free list at this time -- we don't check)
  384. */
  385. for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
  386. {
  387. reqcontainer = &priv->rdreqs[i];
  388. if (reqcontainer->req)
  389. {
  390. usbmsc_freereq(priv->epbulkout, reqcontainer->req);
  391. reqcontainer->req = NULL;
  392. }
  393. }
  394. /* Free the bulk OUT endpoint */
  395. if (priv->epbulkout)
  396. {
  397. DEV_FREEEP(dev, priv->epbulkout);
  398. priv->epbulkout = NULL;
  399. }
  400. /* Free write requests that are not in use (which should be all
  401. * of them
  402. */
  403. flags = enter_critical_section();
  404. while (!sq_empty(&priv->wrreqlist))
  405. {
  406. reqcontainer = (struct usbmsc_req_s *)
  407. sq_remfirst(&priv->wrreqlist);
  408. if (reqcontainer->req != NULL)
  409. {
  410. usbmsc_freereq(priv->epbulkin, reqcontainer->req);
  411. }
  412. }
  413. /* Free the bulk IN endpoint */
  414. if (priv->epbulkin)
  415. {
  416. DEV_FREEEP(dev, priv->epbulkin);
  417. priv->epbulkin = NULL;
  418. }
  419. leave_critical_section(flags);
  420. }
  421. }
  422. /****************************************************************************
  423. * Name: usbmsc_setup
  424. *
  425. * Description:
  426. * Invoked for ep0 control requests. This function probably executes
  427. * in the context of an interrupt handler.
  428. *
  429. ****************************************************************************/
  430. static int usbmsc_setup(FAR struct usbdevclass_driver_s *driver,
  431. FAR struct usbdev_s *dev,
  432. FAR const struct usb_ctrlreq_s *ctrl,
  433. FAR uint8_t *dataout, size_t outlen)
  434. {
  435. FAR struct usbmsc_dev_s *priv;
  436. FAR struct usbdev_req_s *ctrlreq;
  437. uint16_t value;
  438. uint16_t index;
  439. uint16_t len;
  440. int ret = -EOPNOTSUPP;
  441. #ifdef CONFIG_DEBUG_FEATURES
  442. if (!driver || !dev || !dev->ep0 || !ctrl)
  443. {
  444. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SETUPINVALIDARGS), 0);
  445. return -EIO;
  446. }
  447. #endif
  448. /* Extract reference to private data */
  449. usbtrace(TRACE_CLASSSETUP, ctrl->req);
  450. priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
  451. #ifdef CONFIG_DEBUG_FEATURES
  452. if (!priv || !priv->ctrlreq)
  453. {
  454. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND2), 0);
  455. return -ENODEV;
  456. }
  457. #endif
  458. ctrlreq = priv->ctrlreq;
  459. /* Extract the little-endian 16-bit values to host order */
  460. value = GETUINT16(ctrl->value);
  461. index = GETUINT16(ctrl->index);
  462. len = GETUINT16(ctrl->len);
  463. uinfo("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
  464. ctrl->type, ctrl->req, value, index, len);
  465. if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
  466. {
  467. /**********************************************************************
  468. * Standard Requests
  469. **********************************************************************/
  470. switch (ctrl->req)
  471. {
  472. case USB_REQ_GETDESCRIPTOR:
  473. {
  474. /* The value field specifies the descriptor type in the MS byte and the
  475. * descriptor index in the LS byte (order is little endian)
  476. */
  477. switch (ctrl->value[1])
  478. {
  479. /* If the mass storage device is used in as part of a
  480. * composite device, then the device descriptor is is
  481. * provided by logic in the composite device implementation.
  482. */
  483. #ifndef CONFIG_USBMSC_COMPOSITE
  484. case USB_DESC_TYPE_DEVICE:
  485. {
  486. ret = USB_SIZEOF_DEVDESC;
  487. memcpy(ctrlreq->buf, usbmsc_getdevdesc(), ret);
  488. }
  489. break;
  490. #endif
  491. /* If the mass storage device is used in as part of a
  492. * composite device, then the device qualifier descriptor is
  493. * provided by logic in the composite device implementation.
  494. */
  495. #if !defined(CONFIG_USBMSC_COMPOSITE) && defined(CONFIG_USBDEV_DUALSPEED)
  496. case USB_DESC_TYPE_DEVICEQUALIFIER:
  497. {
  498. ret = USB_SIZEOF_QUALDESC;
  499. memcpy(ctrlreq->buf, usbmsc_getqualdesc(), ret);
  500. }
  501. break;
  502. case USB_DESC_TYPE_OTHERSPEEDCONFIG:
  503. #endif
  504. /* If the mass storage device is used in as part of a composite device,
  505. * then the configuration descriptor is provided by logic in the
  506. * composite device implementation.
  507. */
  508. #ifndef CONFIG_USBMSC_COMPOSITE
  509. case USB_DESC_TYPE_CONFIG:
  510. {
  511. #ifdef CONFIG_USBDEV_DUALSPEED
  512. ret = usbmsc_mkcfgdesc(ctrlreq->buf, &priv->devinfo,
  513. dev->speed, ctrl->value[1]);
  514. #else
  515. ret = usbmsc_mkcfgdesc(ctrlreq->buf, &priv->devinfo);
  516. #endif
  517. }
  518. break;
  519. #endif
  520. /* If the mass storage device is used in as part of a
  521. * composite device, then the language string descriptor is
  522. * provided by logic in the composite device implementation.
  523. */
  524. #ifndef CONFIG_USBMSC_COMPOSITE
  525. case USB_DESC_TYPE_STRING:
  526. {
  527. /* index == language code. */
  528. ret = usbmsc_mkstrdesc(ctrl->value[0],
  529. (struct usb_strdesc_s *)ctrlreq->buf);
  530. }
  531. break;
  532. #endif
  533. default:
  534. {
  535. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_GETUNKNOWNDESC), value);
  536. }
  537. break;
  538. }
  539. }
  540. break;
  541. case USB_REQ_SETCONFIGURATION:
  542. {
  543. if (ctrl->type == 0)
  544. {
  545. /* Signal the worker thread to instantiate the new
  546. * configuration.
  547. */
  548. priv->theventset |= USBMSC_EVENT_CFGCHANGE;
  549. priv->thvalue = value;
  550. usbmsc_scsi_signal(priv);
  551. /* Return here... the response will be provided later by the
  552. * worker thread.
  553. */
  554. return OK;
  555. }
  556. }
  557. break;
  558. /* If the mass storage device is used in as part of a composite
  559. * device, then the overall composite class configuration is
  560. * managed by logic in the composite device implementation.
  561. */
  562. #ifndef CONFIG_USBMSC_COMPOSITE
  563. case USB_REQ_GETCONFIGURATION:
  564. {
  565. if (ctrl->type == USB_DIR_IN)
  566. {
  567. ctrlreq->buf[0] = priv->config;
  568. ret = 1;
  569. }
  570. }
  571. break;
  572. #endif
  573. case USB_REQ_SETINTERFACE:
  574. {
  575. if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE)
  576. {
  577. if (priv->config == USBMSC_CONFIGID &&
  578. index == USBMSC_INTERFACEID &&
  579. value == USBMSC_ALTINTERFACEID)
  580. {
  581. /* Signal to instantiate the interface change */
  582. priv->theventset |= USBMSC_EVENT_IFCHANGE;
  583. usbmsc_scsi_signal(priv);
  584. /* Return here... the response will be provided later by
  585. * the worker thread.
  586. */
  587. return OK;
  588. }
  589. }
  590. }
  591. break;
  592. case USB_REQ_GETINTERFACE:
  593. {
  594. if (ctrl->type == (USB_DIR_IN | USB_REQ_RECIPIENT_INTERFACE) &&
  595. priv->config == USBMSC_CONFIGIDNONE)
  596. {
  597. if (index != USBMSC_INTERFACEID)
  598. {
  599. ret = -EDOM;
  600. }
  601. else
  602. {
  603. ctrlreq->buf[0] = USBMSC_ALTINTERFACEID;
  604. ret = 1;
  605. }
  606. }
  607. }
  608. break;
  609. default:
  610. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNSUPPORTEDSTDREQ), ctrl->req);
  611. break;
  612. }
  613. }
  614. else if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_CLASS)
  615. {
  616. /**********************************************************************
  617. * Bulk-Only Mass Storage Class Requests
  618. **********************************************************************/
  619. /* Verify that we are configured */
  620. if (!priv->config)
  621. {
  622. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_NOTCONFIGURED), 0);
  623. return ret;
  624. }
  625. switch (ctrl->req)
  626. {
  627. case USBMSC_REQ_MSRESET: /* Reset mass storage device and interface */
  628. {
  629. if (ctrl->type == USBMSC_TYPE_SETUPOUT && value == 0 && len == 0)
  630. {
  631. /* Only one interface is supported */
  632. if (index != USBMSC_INTERFACEID)
  633. {
  634. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_MSRESETNDX), index);
  635. ret = -EDOM;
  636. }
  637. else
  638. {
  639. /* Signal to stop the current operation and reinitialize
  640. * state.
  641. */
  642. priv->theventset |= USBMSC_EVENT_RESET;
  643. usbmsc_scsi_signal(priv);
  644. /* Return here... the response will be provided later by
  645. * the worker thread.
  646. */
  647. return OK;
  648. }
  649. }
  650. }
  651. break;
  652. case USBMSC_REQ_GETMAXLUN: /* Return number LUNs supported */
  653. {
  654. if (ctrl->type == USBMSC_TYPE_SETUPIN && value == 0 && len == 1)
  655. {
  656. /* Only one interface is supported */
  657. if (index != USBMSC_INTERFACEID)
  658. {
  659. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_GETMAXLUNNDX), index);
  660. ret = -EDOM;
  661. }
  662. else
  663. {
  664. ctrlreq->buf[0] = priv->nluns - 1;
  665. ret = 1;
  666. }
  667. }
  668. }
  669. break;
  670. default:
  671. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BADREQUEST), ctrl->req);
  672. break;
  673. }
  674. }
  675. else
  676. {
  677. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNSUPPORTEDTYPE), ctrl->type);
  678. }
  679. /* Respond to the setup command if data was returned. On an error return
  680. * value (ret < 0), the USB driver will stall EP0.
  681. */
  682. if (ret >= 0)
  683. {
  684. /* Configure the response */
  685. ctrlreq->len = MIN(len, ret);
  686. ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
  687. /* Send the response -- either directly to the USB controller or
  688. * indirectly in the case where this class is a member of a composite
  689. * device.
  690. */
  691. #ifndef CONFIG_USBMSC_COMPOSITE
  692. ret = EP_SUBMIT(dev->ep0, ctrlreq);
  693. #else
  694. ret = composite_ep0submit(driver, dev, ctrlreq);
  695. #endif
  696. if (ret < 0)
  697. {
  698. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPRESPQ), (uint16_t)-ret);
  699. #if 0 /* Not necessary */
  700. ctrlreq->result = OK;
  701. usbmsc_ep0incomplete(dev->ep0, ctrlreq);
  702. #endif
  703. }
  704. }
  705. return ret;
  706. }
  707. /****************************************************************************
  708. * Name: usbmsc_disconnect
  709. *
  710. * Description:
  711. * Invoked after all transfers have been stopped, when the host is
  712. * disconnected. This function is probably called from the context of an
  713. * interrupt handler.
  714. *
  715. ****************************************************************************/
  716. static void usbmsc_disconnect(FAR struct usbdevclass_driver_s *driver,
  717. FAR struct usbdev_s *dev)
  718. {
  719. struct usbmsc_dev_s *priv;
  720. irqstate_t flags;
  721. usbtrace(TRACE_CLASSDISCONNECT, 0);
  722. #ifdef CONFIG_DEBUG_FEATURES
  723. if (!driver || !dev || !dev->ep0)
  724. {
  725. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DISCONNECTINVALIDARGS), 0);
  726. return;
  727. }
  728. #endif
  729. /* Extract reference to private data */
  730. priv = ((FAR struct usbmsc_driver_s *)driver)->dev;
  731. #ifdef CONFIG_DEBUG_FEATURES
  732. if (!priv)
  733. {
  734. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EP0NOTBOUND3), 0);
  735. return;
  736. }
  737. #endif
  738. /* Reset the configuration */
  739. flags = enter_critical_section();
  740. usbmsc_resetconfig(priv);
  741. /* Signal the worker thread */
  742. priv->theventset |= USBMSC_EVENT_DISCONNECT;
  743. usbmsc_scsi_signal(priv);
  744. leave_critical_section(flags);
  745. /* Perform the soft connect function so that we will we can be
  746. * re-enumerated (unless we are part of a composite device)
  747. */
  748. #ifndef CONFIG_USBMSC_COMPOSITE
  749. DEV_CONNECT(dev);
  750. #endif
  751. }
  752. /****************************************************************************
  753. * Initialization/Un-Initialization
  754. ****************************************************************************/
  755. /****************************************************************************
  756. * Name: usbmsc_lununinitialize
  757. ****************************************************************************/
  758. static void usbmsc_lununinitialize(struct usbmsc_lun_s *lun)
  759. {
  760. /* Has a block driver has been bound to the LUN? */
  761. if (lun->inode)
  762. {
  763. /* Close the block driver */
  764. (void)close_blockdriver(lun->inode);
  765. }
  766. memset(lun, 0, sizeof(struct usbmsc_lun_s));
  767. }
  768. /****************************************************************************
  769. * Public Functions
  770. ****************************************************************************/
  771. /****************************************************************************
  772. * Internal Interfaces
  773. ****************************************************************************/
  774. /****************************************************************************
  775. * Name: usbmsc_setconfig
  776. *
  777. * Description:
  778. * Set the device configuration by allocating and configuring endpoints and
  779. * by allocating and queuing read and write requests.
  780. *
  781. ****************************************************************************/
  782. int usbmsc_setconfig(FAR struct usbmsc_dev_s *priv, uint8_t config)
  783. {
  784. FAR struct usbmsc_req_s *privreq;
  785. FAR struct usbdev_req_s *req;
  786. struct usb_epdesc_s epdesc;
  787. bool hispeed = false;
  788. int i;
  789. int ret = 0;
  790. #ifdef CONFIG_DEBUG_FEATURES
  791. if (priv == NULL)
  792. {
  793. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_SETCONFIGINVALIDARGS), 0);
  794. return -EIO;
  795. }
  796. #endif
  797. if (config == priv->config)
  798. {
  799. /* Already configured -- Do nothing */
  800. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALREADYCONFIGURED), 0);
  801. return OK;
  802. }
  803. #ifdef CONFIG_USBDEV_DUALSPEED
  804. hispeed = (priv->usbdev->speed == USB_SPEED_HIGH);
  805. #endif
  806. /* Discard the previous configuration data */
  807. usbmsc_resetconfig(priv);
  808. /* Was this a request to simply discard the current configuration? */
  809. if (config == USBMSC_CONFIGIDNONE)
  810. {
  811. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CONFIGNONE), 0);
  812. return OK;
  813. }
  814. /* We only accept one configuration */
  815. if (config != USBMSC_CONFIGID)
  816. {
  817. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CONFIGIDBAD), 0);
  818. return -EINVAL;
  819. }
  820. /* Configure the IN bulk endpoint */
  821. usbmsc_copy_epdesc(USBMSC_EPBULKIN, &epdesc, &priv->devinfo,
  822. hispeed);
  823. ret = EP_CONFIGURE(priv->epbulkin, &epdesc, false);
  824. if (ret < 0)
  825. {
  826. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKINCONFIGFAIL), 0);
  827. goto errout;
  828. }
  829. priv->epbulkin->priv = priv;
  830. /* Configure the OUT bulk endpoint */
  831. usbmsc_copy_epdesc(USBMSC_EPBULKOUT, &epdesc, &priv->devinfo,
  832. hispeed);
  833. ret = EP_CONFIGURE(priv->epbulkout, &epdesc, true);
  834. if (ret < 0)
  835. {
  836. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EPBULKOUTCONFIGFAIL), 0);
  837. goto errout;
  838. }
  839. priv->epbulkout->priv = priv;
  840. /* Queue read requests in the bulk OUT endpoint */
  841. for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++)
  842. {
  843. privreq = &priv->rdreqs[i];
  844. req = privreq->req;
  845. req->len = CONFIG_USBMSC_BULKOUTREQLEN;
  846. req->priv = privreq;
  847. req->callback = usbmsc_rdcomplete;
  848. ret = EP_SUBMIT(priv->epbulkout, req);
  849. if (ret < 0)
  850. {
  851. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDSUBMIT),
  852. (uint16_t)-ret);
  853. goto errout;
  854. }
  855. }
  856. priv->config = config;
  857. return OK;
  858. errout:
  859. usbmsc_resetconfig(priv);
  860. return ret;
  861. }
  862. /****************************************************************************
  863. * Name: usbmsc_resetconfig
  864. *
  865. * Description:
  866. * Mark the device as not configured and disable all endpoints.
  867. *
  868. ****************************************************************************/
  869. void usbmsc_resetconfig(FAR struct usbmsc_dev_s *priv)
  870. {
  871. /* Are we configured? */
  872. if (priv->config != USBMSC_CONFIGIDNONE)
  873. {
  874. /* Yes.. but not anymore */
  875. priv->config = USBMSC_CONFIGIDNONE;
  876. /* Disable endpoints. This should force completion of all pending
  877. * transfers.
  878. */
  879. EP_DISABLE(priv->epbulkin);
  880. EP_DISABLE(priv->epbulkout);
  881. }
  882. }
  883. /****************************************************************************
  884. * Name: usbmsc_wrcomplete
  885. *
  886. * Description:
  887. * Handle completion of write request. This function probably executes
  888. * in the context of an interrupt handler.
  889. *
  890. ****************************************************************************/
  891. void usbmsc_wrcomplete(FAR struct usbdev_ep_s *ep,
  892. FAR struct usbdev_req_s *req)
  893. {
  894. FAR struct usbmsc_dev_s *priv;
  895. FAR struct usbmsc_req_s *privreq;
  896. irqstate_t flags;
  897. /* Sanity check */
  898. #ifdef CONFIG_DEBUG_FEATURES
  899. if (!ep || !ep->priv || !req || !req->priv)
  900. {
  901. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRCOMPLETEINVALIDARGS), 0);
  902. return;
  903. }
  904. #endif
  905. /* Extract references to private data */
  906. priv = (FAR struct usbmsc_dev_s *)ep->priv;
  907. privreq = (FAR struct usbmsc_req_s *)req->priv;
  908. /* Return the write request to the free list */
  909. flags = enter_critical_section();
  910. sq_addlast((FAR sq_entry_t *)privreq, &priv->wrreqlist);
  911. leave_critical_section(flags);
  912. /* Process the received data unless this is some unusual condition */
  913. switch (req->result)
  914. {
  915. case OK: /* Normal completion */
  916. usbtrace(TRACE_CLASSWRCOMPLETE, req->xfrd);
  917. break;
  918. case -ESHUTDOWN: /* Disconnection */
  919. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRSHUTDOWN), 0);
  920. break;
  921. default: /* Some other error occurred */
  922. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_WRUNEXPECTED),
  923. (uint16_t)-req->result);
  924. break;
  925. };
  926. /* Inform the worker thread that a write request has been returned */
  927. priv->theventset |= USBMSC_EVENT_WRCOMPLETE;
  928. usbmsc_scsi_signal(priv);
  929. }
  930. /****************************************************************************
  931. * Name: usbmsc_rdcomplete
  932. *
  933. * Description:
  934. * Handle completion of read request on the bulk OUT endpoint. This
  935. * is handled like the receipt of serial data on the "UART"
  936. *
  937. ****************************************************************************/
  938. void usbmsc_rdcomplete(FAR struct usbdev_ep_s *ep,
  939. FAR struct usbdev_req_s *req)
  940. {
  941. FAR struct usbmsc_dev_s *priv;
  942. FAR struct usbmsc_req_s *privreq;
  943. irqstate_t flags;
  944. int ret;
  945. /* Sanity check */
  946. #ifdef CONFIG_DEBUG_FEATURES
  947. if (!ep || !ep->priv || !req || !req->priv)
  948. {
  949. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDCOMPLETEINVALIDARGS), 0);
  950. return;
  951. }
  952. #endif
  953. /* Extract references to private data */
  954. priv = (FAR struct usbmsc_dev_s *)ep->priv;
  955. privreq = (FAR struct usbmsc_req_s *)req->priv;
  956. /* Process the received data unless this is some unusual condition */
  957. switch (req->result)
  958. {
  959. case 0: /* Normal completion */
  960. {
  961. usbtrace(TRACE_CLASSRDCOMPLETE, req->xfrd);
  962. /* Add the filled read request from the rdreqlist */
  963. flags = enter_critical_section();
  964. sq_addlast((FAR sq_entry_t *)privreq, &priv->rdreqlist);
  965. leave_critical_section(flags);
  966. /* Signal the worker thread that there is received data to be processed */
  967. priv->theventset |= USBMSC_EVENT_RDCOMPLETE;
  968. usbmsc_scsi_signal(priv);
  969. }
  970. break;
  971. case -ESHUTDOWN: /* Disconnection */
  972. {
  973. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDSHUTDOWN), 0);
  974. /* Drop the read request... it will be cleaned up later */
  975. }
  976. break;
  977. default: /* Some other error occurred */
  978. {
  979. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDUNEXPECTED),
  980. (uint16_t)-req->result);
  981. /* Return the read request to the bulk out endpoint for re-filling */
  982. req = privreq->req;
  983. req->priv = privreq;
  984. req->callback = usbmsc_rdcomplete;
  985. ret = EP_SUBMIT(priv->epbulkout, req);
  986. if (ret != OK)
  987. {
  988. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_RDCOMPLETERDSUBMIT),
  989. (uint16_t)-ret);
  990. }
  991. }
  992. break;
  993. }
  994. }
  995. /****************************************************************************
  996. * Name: usbmsc_deferredresponse
  997. *
  998. * Description:
  999. * Some EP0 setup request cannot be responded to immediately because they
  1000. * require some asynchronous action from the SCSI worker thread. This
  1001. * function is provided for the SCSI thread to make that deferred response.
  1002. * The specific requests that require this deferred response are:
  1003. *
  1004. * 1. USB_REQ_SETCONFIGURATION,
  1005. * 2. USB_REQ_SETINTERFACE, or
  1006. * 3. USBMSC_REQ_MSRESET
  1007. *
  1008. * In all cases, the success reponse is a zero-length packet; the failure
  1009. * response is an EP0 stall.
  1010. *
  1011. * Input Parameters:
  1012. * priv - Private state structure for this USB storage instance
  1013. * stall - true is the action failed and a stall is required
  1014. *
  1015. ****************************************************************************/
  1016. void usbmsc_deferredresponse(FAR struct usbmsc_dev_s *priv, bool failed)
  1017. {
  1018. FAR struct usbdev_s *dev;
  1019. FAR struct usbdev_req_s *ctrlreq;
  1020. int ret;
  1021. #ifdef CONFIG_DEBUG_FEATURES
  1022. if (!priv || !priv->usbdev || !priv->ctrlreq)
  1023. {
  1024. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEFERREDRESPINVALIDARGS), 0);
  1025. return;
  1026. }
  1027. #endif
  1028. dev = priv->usbdev;
  1029. ctrlreq = priv->ctrlreq;
  1030. /* If no error occurs, respond to the deferred setup command with a null
  1031. * packet.
  1032. */
  1033. if (!failed)
  1034. {
  1035. ctrlreq->len = 0;
  1036. ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
  1037. ret = EP_SUBMIT(dev->ep0, ctrlreq);
  1038. if (ret < 0)
  1039. {
  1040. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEFERREDRESPSUBMIT),
  1041. (uint16_t)-ret);
  1042. #if 0 /* Not necessary */
  1043. ctrlreq->result = OK;
  1044. usbmsc_ep0incomplete(dev->ep0, ctrlreq);
  1045. #endif
  1046. }
  1047. }
  1048. else
  1049. {
  1050. /* On a failure, the USB driver will stall. */
  1051. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEFERREDRESPSTALLED), 0);
  1052. EP_STALL(dev->ep0);
  1053. }
  1054. }
  1055. /****************************************************************************
  1056. * Name: usbmsc_sync_wait
  1057. *
  1058. * Description:
  1059. * Wait for the worker thread to obtain the USB MSC state data
  1060. *
  1061. ****************************************************************************/
  1062. static inline void usbmsc_sync_wait(FAR struct usbmsc_dev_s *priv)
  1063. {
  1064. int ret;
  1065. do
  1066. {
  1067. ret = nxsem_wait(&priv->thsynch);
  1068. DEBUGASSERT(ret == OK || ret == -EINTR);
  1069. }
  1070. while (ret == -EINTR);
  1071. }
  1072. /****************************************************************************
  1073. * User Interfaces
  1074. ****************************************************************************/
  1075. /****************************************************************************
  1076. * Name: usbmsc_configure
  1077. *
  1078. * Description:
  1079. * One-time initialization of the USB storage driver. The initialization
  1080. * sequence is as follows:
  1081. *
  1082. * 1. Call usbmsc_configure to perform one-time initialization specifying
  1083. * the number of luns.
  1084. * 2. Call usbmsc_bindlun to configure each supported LUN
  1085. * 3. Call usbmsc_exportluns when all LUNs are configured
  1086. *
  1087. * Input Parameters:
  1088. * nluns - the number of LUNs that will be registered
  1089. * handle - Location to return a handle that is used in other API calls.
  1090. *
  1091. * Returned Value:
  1092. * 0 on success; a negated errno on failure
  1093. *
  1094. ****************************************************************************/
  1095. int usbmsc_configure(unsigned int nluns, void **handle)
  1096. {
  1097. FAR struct usbmsc_alloc_s *alloc;
  1098. FAR struct usbmsc_dev_s *priv;
  1099. FAR struct usbmsc_driver_s *drvr;
  1100. int ret;
  1101. #ifdef CONFIG_DEBUG_FEATURES
  1102. if (nluns > 15)
  1103. {
  1104. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_TOOMANYLUNS), 0);
  1105. return -EDOM;
  1106. }
  1107. #endif
  1108. /* Allocate the structures needed */
  1109. alloc = (FAR struct usbmsc_alloc_s *)kmm_malloc(sizeof(struct usbmsc_alloc_s));
  1110. if (!alloc)
  1111. {
  1112. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCDEVSTRUCT), 0);
  1113. return -ENOMEM;
  1114. }
  1115. /* Initialize the USB storage driver structure */
  1116. priv = &alloc->dev;
  1117. memset(priv, 0, sizeof(struct usbmsc_dev_s));
  1118. /* Initialize semaphores */
  1119. nxsem_init(&priv->thsynch, 0, 0);
  1120. nxsem_init(&priv->thlock, 0, 1);
  1121. nxsem_init(&priv->thwaitsem, 0, 0);
  1122. /* The thsynch and thwaitsem semaphores are used for signaling and, hence,
  1123. * should not have priority inheritance enabled.
  1124. */
  1125. nxsem_setprotocol(&priv->thsynch, SEM_PRIO_NONE);
  1126. nxsem_setprotocol(&priv->thwaitsem, SEM_PRIO_NONE);
  1127. sq_init(&priv->wrreqlist);
  1128. priv->nluns = nluns;
  1129. /* Allocate the LUN table */
  1130. priv->luntab = (FAR struct usbmsc_lun_s *)
  1131. kmm_malloc(priv->nluns*sizeof(struct usbmsc_lun_s));
  1132. if (!priv->luntab)
  1133. {
  1134. ret = -ENOMEM;
  1135. goto errout;
  1136. }
  1137. memset(priv->luntab, 0, priv->nluns * sizeof(struct usbmsc_lun_s));
  1138. /* Initialize the USB class driver structure */
  1139. drvr = &alloc->drvr;
  1140. #ifdef CONFIG_USBDEV_DUALSPEED
  1141. drvr->drvr.speed = USB_SPEED_HIGH;
  1142. #else
  1143. drvr->drvr.speed = USB_SPEED_FULL;
  1144. #endif
  1145. drvr->drvr.ops = &g_driverops;
  1146. drvr->dev = priv;
  1147. /* Initialize the device information if we are not part of a composite.
  1148. * If we are part of a composite, the device information will be
  1149. * initialized through coordinated actions of usbmsc_get_composite_devdesc()
  1150. * and board-specific logic.
  1151. */
  1152. #ifndef CONFIG_USBMSC_COMPOSITE
  1153. /* minor - not used */
  1154. /* Interfaces (ifnobase == 0) */
  1155. priv->devinfo.ninterfaces = USBMSC_NINTERFACES; /* Number of interfaces in the configuration */
  1156. /* Strings (strbase == 0) */
  1157. priv->devinfo.nstrings = USBMSC_NSTRIDS; /* Number of Strings */
  1158. /* Endpoints */
  1159. priv->devinfo.nendpoints = USBMSC_NENDPOINTS;
  1160. priv->devinfo.epno[USBMSC_EP_BULKIN_IDX] = CONFIG_USBMSC_EPBULKIN;
  1161. priv->devinfo.epno[USBMSC_EP_BULKOUT_IDX] = CONFIG_USBMSC_EPBULKOUT;
  1162. #endif
  1163. /* Return the handle and success */
  1164. *handle = (FAR void *)alloc;
  1165. return OK;
  1166. errout:
  1167. usbmsc_uninitialize(alloc);
  1168. return ret;
  1169. }
  1170. /****************************************************************************
  1171. * Name: usbmsc_bindlun
  1172. *
  1173. * Description:
  1174. * Bind the block driver specified by drvrpath to a USB storage LUN.
  1175. *
  1176. * Input Parameters:
  1177. * handle - The handle returned by a previous call to usbmsc_configure().
  1178. * drvrpath - the full path to the block driver
  1179. * startsector - A sector offset into the block driver to the start of the
  1180. * partition on drvrpath (0 if no partitions)
  1181. * nsectors - The number of sectors in the partition (if 0, all sectors
  1182. * to the end of the media will be exported).
  1183. * lunno - the LUN to bind to
  1184. *
  1185. * Returned Value:
  1186. * 0 on success; a negated errno on failure.
  1187. *
  1188. ****************************************************************************/
  1189. int usbmsc_bindlun(FAR void *handle, FAR const char *drvrpath,
  1190. unsigned int lunno, off_t startsector, size_t nsectors,
  1191. bool readonly)
  1192. {
  1193. FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
  1194. FAR struct usbmsc_dev_s *priv;
  1195. FAR struct usbmsc_lun_s *lun;
  1196. FAR struct inode *inode;
  1197. struct geometry geo;
  1198. int ret;
  1199. #ifdef CONFIG_DEBUG_FEATURES
  1200. if (!alloc || !drvrpath || startsector < 0)
  1201. {
  1202. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINLUNINVALIDARGS1), 0);
  1203. return -EINVAL;
  1204. }
  1205. #endif
  1206. priv = &alloc->dev;
  1207. #ifdef CONFIG_DEBUG_FEATURES
  1208. if (!priv->luntab)
  1209. {
  1210. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INTERNALCONFUSION1), 0);
  1211. return -EIO;
  1212. }
  1213. if (lunno > priv->nluns)
  1214. {
  1215. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS2), 0);
  1216. return -EINVAL;
  1217. }
  1218. #endif
  1219. lun = &priv->luntab[lunno];
  1220. #ifdef CONFIG_DEBUG_FEATURES
  1221. if (lun->inode != NULL)
  1222. {
  1223. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_LUNALREADYBOUND), 0);
  1224. return -EBUSY;
  1225. }
  1226. #endif
  1227. /* Open the block driver */
  1228. ret = open_blockdriver(drvrpath, 0, &inode);
  1229. if (ret < 0)
  1230. {
  1231. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BLKDRVEOPEN), 0);
  1232. return ret;
  1233. }
  1234. /* Get the drive geometry */
  1235. if (!inode || !inode->u.i_bops || !inode->u.i_bops->geometry ||
  1236. inode->u.i_bops->geometry(inode, &geo) != OK || !geo.geo_available)
  1237. {
  1238. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_NOGEOMETRY), 0);
  1239. return -ENODEV;
  1240. }
  1241. /* Verify that the partition parameters are valid */
  1242. if (startsector >= geo.geo_nsectors)
  1243. {
  1244. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS3), 0);
  1245. return -EDOM;
  1246. }
  1247. else if (nsectors == 0)
  1248. {
  1249. nsectors = geo.geo_nsectors - startsector;
  1250. }
  1251. else if (startsector + nsectors >= geo.geo_nsectors)
  1252. {
  1253. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_BINDLUNINVALIDARGS4), 0);
  1254. return -EDOM;
  1255. }
  1256. /* Initialize the LUN structure */
  1257. memset(lun, 0, sizeof(struct usbmsc_lun_s));
  1258. /* Allocate an I/O buffer big enough to hold one hardware sector. SCSI
  1259. * commands are processed one at a time so all LUNs may share a single I/O
  1260. * buffer. The I/O buffer will be allocated so that is it as large as the
  1261. * largest block device sector size
  1262. */
  1263. if (!priv->iobuffer)
  1264. {
  1265. priv->iobuffer = (FAR uint8_t *)kmm_malloc(geo.geo_sectorsize);
  1266. if (!priv->iobuffer)
  1267. {
  1268. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_ALLOCIOBUFFER), geo.geo_sectorsize);
  1269. return -ENOMEM;
  1270. }
  1271. priv->iosize = geo.geo_sectorsize;
  1272. }
  1273. else if (priv->iosize < geo.geo_sectorsize)
  1274. {
  1275. FAR void *tmp;
  1276. tmp = (FAR void *)kmm_realloc(priv->iobuffer, geo.geo_sectorsize);
  1277. if (!tmp)
  1278. {
  1279. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_REALLOCIOBUFFER), geo.geo_sectorsize);
  1280. return -ENOMEM;
  1281. }
  1282. priv->iobuffer = (FAR uint8_t *)tmp;
  1283. priv->iosize = geo.geo_sectorsize;
  1284. }
  1285. lun->inode = inode;
  1286. lun->startsector = startsector;
  1287. lun->nsectors = nsectors;
  1288. lun->sectorsize = geo.geo_sectorsize;
  1289. lun->readonly = readonly;
  1290. /* If the driver does not support the write method, then this is read-only */
  1291. if (!inode->u.i_bops->write)
  1292. {
  1293. lun->readonly = true;
  1294. }
  1295. return OK;
  1296. }
  1297. /****************************************************************************
  1298. * Name: usbmsc_unbindlun
  1299. *
  1300. * Description:
  1301. * Un-bind the block driver for the specified LUN
  1302. *
  1303. * Input Parameters:
  1304. * handle - The handle returned by a previous call to usbmsc_configure().
  1305. * lun - the LUN to unbind from
  1306. *
  1307. * Returned Value:
  1308. * 0 on success; a negated errno on failure.
  1309. *
  1310. ****************************************************************************/
  1311. int usbmsc_unbindlun(FAR void *handle, unsigned int lunno)
  1312. {
  1313. FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
  1314. FAR struct usbmsc_dev_s *priv;
  1315. FAR struct usbmsc_lun_s *lun;
  1316. int ret;
  1317. #ifdef CONFIG_DEBUG_FEATURES
  1318. if (!alloc)
  1319. {
  1320. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDLUNINVALIDARGS1), 0);
  1321. return -EINVAL;
  1322. }
  1323. #endif
  1324. priv = &alloc->dev;
  1325. #ifdef CONFIG_DEBUG_FEATURES
  1326. if (!priv->luntab)
  1327. {
  1328. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_INTERNALCONFUSION2), 0);
  1329. return -EIO;
  1330. }
  1331. if (lunno > priv->nluns)
  1332. {
  1333. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNBINDLUNINVALIDARGS2), 0);
  1334. return -EINVAL;
  1335. }
  1336. #endif
  1337. lun = &priv->luntab[lunno];
  1338. usbmsc_scsi_lock(priv);
  1339. #ifdef CONFIG_DEBUG_FEATURES
  1340. if (lun->inode == NULL)
  1341. {
  1342. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_LUNNOTBOUND), 0);
  1343. ret = -EBUSY;
  1344. }
  1345. else
  1346. #endif
  1347. {
  1348. /* Close the block driver */
  1349. usbmsc_lununinitialize(lun);
  1350. ret = OK;
  1351. }
  1352. usbmsc_scsi_unlock(priv);
  1353. return ret;
  1354. }
  1355. /****************************************************************************
  1356. * Name: usbmsc_exportluns
  1357. *
  1358. * Description:
  1359. * After all of the LUNs have been bound, this function may be called
  1360. * in order to export those LUNs in the USB storage device.
  1361. *
  1362. * Input Parameters:
  1363. * handle - The handle returned by a previous call to usbmsc_configure().
  1364. *
  1365. * Returned Value:
  1366. * 0 on success; a negated errno on failure
  1367. *
  1368. ****************************************************************************/
  1369. #if !defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_USBMSC_COMPOSITE)
  1370. static
  1371. #endif
  1372. int usbmsc_exportluns(FAR void *handle)
  1373. {
  1374. FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
  1375. FAR struct usbmsc_dev_s *priv;
  1376. #ifndef CONFIG_USBMSC_COMPOSITE
  1377. FAR struct usbmsc_driver_s *drvr;
  1378. #endif
  1379. irqstate_t flags;
  1380. int ret = OK;
  1381. #ifdef CONFIG_DEBUG_FEATURES
  1382. if (!alloc)
  1383. {
  1384. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_EXPORTLUNSINVALIDARGS), 0);
  1385. return -ENXIO;
  1386. }
  1387. #endif
  1388. priv = &alloc->dev;
  1389. #ifndef CONFIG_USBMSC_COMPOSITE
  1390. drvr = &alloc->drvr;
  1391. #endif
  1392. /* Start the worker thread
  1393. *
  1394. * REVISIT: g_usbmsc_handoff is a global and, hence, really requires
  1395. * some protection against re-entrant usage.
  1396. */
  1397. usbmsc_scsi_lock(priv);
  1398. priv->thstate = USBMSC_STATE_NOTSTARTED;
  1399. priv->theventset = USBMSC_EVENT_NOEVENTS;
  1400. g_usbmsc_handoff = priv;
  1401. uinfo("Starting SCSI worker thread\n");
  1402. priv->thpid = kthread_create("scsid", CONFIG_USBMSC_SCSI_PRIO,
  1403. CONFIG_USBMSC_SCSI_STACKSIZE,
  1404. usbmsc_scsi_main, NULL);
  1405. if (priv->thpid <= 0)
  1406. {
  1407. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_THREADCREATE),
  1408. (uint16_t)errno);
  1409. goto errout_with_lock;
  1410. }
  1411. /* Wait for the worker thread to run and initialize */
  1412. uinfo("Waiting for the SCSI worker thread\n");
  1413. usbmsc_sync_wait(priv);
  1414. DEBUGASSERT(g_usbmsc_handoff == NULL);
  1415. /* Register the USB storage class driver (unless we are part of a composite device) */
  1416. #ifndef CONFIG_USBMSC_COMPOSITE
  1417. ret = usbdev_register(&drvr->drvr);
  1418. if (ret != OK)
  1419. {
  1420. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_DEVREGISTER), (uint16_t)-ret);
  1421. goto errout_with_lock;
  1422. }
  1423. #endif
  1424. /* Signal to start the thread */
  1425. uinfo("Signalling for the SCSI worker thread\n");
  1426. flags = enter_critical_section();
  1427. priv->theventset |= USBMSC_EVENT_READY;
  1428. usbmsc_scsi_signal(priv);
  1429. leave_critical_section(flags);
  1430. errout_with_lock:
  1431. usbmsc_scsi_unlock(priv);
  1432. return ret;
  1433. }
  1434. /****************************************************************************
  1435. * Name: usbmsc_classobject
  1436. *
  1437. * Description:
  1438. * Register USB mass storage device and return the class object.
  1439. *
  1440. * Input Parameters:
  1441. * classdev - The location to return the CDC serial class' device
  1442. * instance.
  1443. *
  1444. * Returned Value:
  1445. * 0 on success; a negated errno on failure
  1446. *
  1447. ****************************************************************************/
  1448. #ifdef CONFIG_USBMSC_COMPOSITE
  1449. int usbmsc_classobject(FAR void *handle,
  1450. FAR struct usbdev_devinfo_s *devinfo,
  1451. FAR struct usbdevclass_driver_s **classdev)
  1452. {
  1453. FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
  1454. int ret;
  1455. DEBUGASSERT(handle != NULL && classdev != NULL);
  1456. /* Save the device description */
  1457. memcpy(&alloc->dev.devinfo, devinfo, sizeof(struct usbdev_devinfo_s));
  1458. /* Export the LUNs as with the "standalone" USB mass storage driver, but
  1459. * don't register the class instance with the USB device infrastructure.
  1460. */
  1461. ret = usbmsc_exportluns(handle);
  1462. if (ret == OK)
  1463. {
  1464. /* On success, return an (typed) instance of the class instance */
  1465. *classdev = &alloc->drvr.drvr;
  1466. }
  1467. return ret;
  1468. }
  1469. #endif
  1470. /****************************************************************************
  1471. * Name: usbmsc_uninitialize
  1472. *
  1473. * Description:
  1474. * Un-initialize the USB storage class driver
  1475. *
  1476. * Input Parameters:
  1477. * handle - The handle returned by a previous call to usbmsc_configure().
  1478. *
  1479. * Returned Value:
  1480. * None
  1481. *
  1482. ****************************************************************************/
  1483. void usbmsc_uninitialize(FAR void *handle)
  1484. {
  1485. FAR struct usbmsc_alloc_s *alloc = (FAR struct usbmsc_alloc_s *)handle;
  1486. FAR struct usbmsc_dev_s *priv;
  1487. irqstate_t flags;
  1488. int i;
  1489. #ifdef CONFIG_DEBUG_FEATURES
  1490. if (!handle)
  1491. {
  1492. usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_UNINITIALIZEINVALIDARGS), 0);
  1493. return;
  1494. }
  1495. #endif
  1496. priv = &alloc->dev;
  1497. #ifdef CONFIG_USBMSC_COMPOSITE
  1498. /* Check for pass 2 uninitialization. We did most of the work on the
  1499. * first pass uninitialization.
  1500. */
  1501. if (priv->thpid == 0)
  1502. {
  1503. /* In this second and final pass, all that remains to be done is to
  1504. * free the memory resources.
  1505. */
  1506. kmm_free(priv);
  1507. return;
  1508. }
  1509. #endif
  1510. /* If the thread hasn't already exitted, tell it to exit now */
  1511. if (priv->thstate != USBMSC_STATE_NOTSTARTED)
  1512. {
  1513. /* The thread was started.. Is it still running? */
  1514. usbmsc_scsi_lock(priv);
  1515. if (priv->thstate != USBMSC_STATE_TERMINATED)
  1516. {
  1517. /* Yes.. Ask the thread to stop */
  1518. flags = enter_critical_section();
  1519. priv->theventset |= USBMSC_EVENT_TERMINATEREQUEST;
  1520. usbmsc_scsi_signal(priv);
  1521. leave_critical_section(flags);
  1522. }
  1523. usbmsc_scsi_unlock(priv);
  1524. /* Wait for the thread to exit */
  1525. while ((priv->theventset & USBMSC_EVENT_TERMINATEREQUEST) != 0)
  1526. {
  1527. usbmsc_sync_wait(priv);
  1528. }
  1529. }
  1530. priv->thpid = 0;
  1531. /* Unregister the driver (unless we are a part of a composite device) */
  1532. #ifndef CONFIG_USBMSC_COMPOSITE
  1533. usbdev_unregister(&alloc->drvr.drvr);
  1534. #endif
  1535. /* Uninitialize and release the LUNs */
  1536. for (i = 0; i < priv->nluns; ++i)
  1537. {
  1538. usbmsc_lununinitialize(&priv->luntab[i]);
  1539. }
  1540. kmm_free(priv->luntab);
  1541. /* Release the I/O buffer */
  1542. if (priv->iobuffer)
  1543. {
  1544. kmm_free(priv->iobuffer);
  1545. }
  1546. /* Uninitialize and release the driver structure */
  1547. nxsem_destroy(&priv->thsynch);
  1548. nxsem_destroy(&priv->thlock);
  1549. nxsem_destroy(&priv->thwaitsem);
  1550. #ifndef CONFIG_USBMSC_COMPOSITE
  1551. /* For the case of the composite driver, there is a two pass
  1552. * uninitialization sequence. We cannot yet free the driver structure.
  1553. * We will do that on the second pass (and we will know that it is the
  1554. * second pass because of priv->thpid == 0)
  1555. */
  1556. kmm_free(priv);
  1557. #endif
  1558. }
  1559. /****************************************************************************
  1560. * Name: usbmsc_get_composite_devdesc
  1561. *
  1562. * Description:
  1563. * Helper function to fill in some constants into the composite
  1564. * configuration struct.
  1565. *
  1566. * Input Parameters:
  1567. * dev - Pointer to the configuration struct we should fill
  1568. *
  1569. * Returned Value:
  1570. * None
  1571. *
  1572. ****************************************************************************/
  1573. #if defined(CONFIG_USBDEV_COMPOSITE) && defined(CONFIG_USBMSC_COMPOSITE)
  1574. void usbmsc_get_composite_devdesc(FAR struct composite_devdesc_s *dev)
  1575. {
  1576. memset(dev, 0, sizeof(struct composite_devdesc_s));
  1577. /* The callback functions for the CDC/ACM class.
  1578. *
  1579. * classobject() and uninitialize() must be provided by board-specific
  1580. * logic
  1581. */
  1582. dev->mkconfdesc = usbmsc_mkcfgdesc;
  1583. dev->mkstrdesc = usbmsc_mkstrdesc;
  1584. dev->nconfigs = USBMSC_NCONFIGS; /* Number of configurations supported */
  1585. dev->configid = USBMSC_CONFIGID; /* The only supported configuration ID */
  1586. dev->cfgdescsize = SIZEOF_USBMSC_CFGDESC; /* The size of the config descriptor */
  1587. /* Board-specific logic must provide the device minor */
  1588. /* Interfaces.
  1589. *
  1590. * ifnobase must be provided by board-specific logic
  1591. */
  1592. dev->devinfo.ninterfaces = USBMSC_NINTERFACES; /* Number of interfaces in the configuration */
  1593. /* Strings.
  1594. *
  1595. * strbase must be provided by board-specific logic
  1596. */
  1597. dev->devinfo.nstrings = USBMSC_NSTRIDS; /* Number of Strings */
  1598. /* Endpoints.
  1599. *
  1600. * Endpoint numbers must be provided by board-specific logic.
  1601. */
  1602. dev->devinfo.nendpoints = USBMSC_NENDPOINTS;
  1603. }
  1604. #endif