usb_storage.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Most of this source has been derived from the Linux USB
  4. * project:
  5. * (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
  6. * (c) 2000 David L. Brown, Jr. (usb-storage@davidb.org)
  7. * (c) 1999 Michael Gee (michael@linuxspecific.com)
  8. * (c) 2000 Yggdrasil Computing, Inc.
  9. *
  10. *
  11. * Adapted for U-Boot:
  12. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  13. * Driver model conversion:
  14. * (C) Copyright 2015 Google, Inc
  15. *
  16. * For BBB support (C) Copyright 2003
  17. * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
  18. *
  19. * BBB support based on /sys/dev/usb/umass.c from
  20. * FreeBSD.
  21. */
  22. /* Note:
  23. * Currently only the CBI transport protocoll has been implemented, and it
  24. * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
  25. * transport protocoll may work as well.
  26. */
  27. /*
  28. * New Note:
  29. * Support for USB Mass Storage Devices (BBB) has been added. It has
  30. * only been tested with USB memory sticks.
  31. */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <dm.h>
  35. #include <errno.h>
  36. #include <mapmem.h>
  37. #include <memalign.h>
  38. #include <asm/byteorder.h>
  39. #include <asm/processor.h>
  40. #include <dm/device-internal.h>
  41. #include <dm/lists.h>
  42. #include <part.h>
  43. #include <usb.h>
  44. #undef BBB_COMDAT_TRACE
  45. #undef BBB_XPORT_TRACE
  46. #include <scsi.h>
  47. /* direction table -- this indicates the direction of the data
  48. * transfer for each command code -- a 1 indicates input
  49. */
  50. static const unsigned char us_direction[256/8] = {
  51. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  52. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  55. };
  56. #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
  57. static struct scsi_cmd usb_ccb __aligned(ARCH_DMA_MINALIGN);
  58. static __u32 CBWTag;
  59. static int usb_max_devs; /* number of highest available usb device */
  60. #if !CONFIG_IS_ENABLED(BLK)
  61. static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV];
  62. #endif
  63. struct us_data;
  64. typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
  65. typedef int (*trans_reset)(struct us_data *data);
  66. struct us_data {
  67. struct usb_device *pusb_dev; /* this usb_device */
  68. unsigned int flags; /* from filter initially */
  69. # define USB_READY (1 << 0)
  70. unsigned char ifnum; /* interface number */
  71. unsigned char ep_in; /* in endpoint */
  72. unsigned char ep_out; /* out ....... */
  73. unsigned char ep_int; /* interrupt . */
  74. unsigned char subclass; /* as in overview */
  75. unsigned char protocol; /* .............. */
  76. unsigned char attention_done; /* force attn on first cmd */
  77. unsigned short ip_data; /* interrupt data */
  78. int action; /* what to do */
  79. int ip_wanted; /* needed */
  80. int *irq_handle; /* for USB int requests */
  81. unsigned int irqpipe; /* pipe for release_irq */
  82. unsigned char irqmaxp; /* max packed for irq Pipe */
  83. unsigned char irqinterval; /* Intervall for IRQ Pipe */
  84. struct scsi_cmd *srb; /* current srb */
  85. trans_reset transport_reset; /* reset routine */
  86. trans_cmnd transport; /* transport routine */
  87. unsigned short max_xfer_blk; /* maximum transfer blocks */
  88. };
  89. #if !CONFIG_IS_ENABLED(BLK)
  90. static struct us_data usb_stor[USB_MAX_STOR_DEV];
  91. #endif
  92. #define USB_STOR_TRANSPORT_GOOD 0
  93. #define USB_STOR_TRANSPORT_FAILED -1
  94. #define USB_STOR_TRANSPORT_ERROR -2
  95. int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
  96. struct blk_desc *dev_desc);
  97. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  98. struct us_data *ss);
  99. #if CONFIG_IS_ENABLED(BLK)
  100. static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
  101. lbaint_t blkcnt, void *buffer);
  102. static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
  103. lbaint_t blkcnt, const void *buffer);
  104. #else
  105. static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
  106. lbaint_t blkcnt, void *buffer);
  107. static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
  108. lbaint_t blkcnt, const void *buffer);
  109. #endif
  110. void uhci_show_temp_int_td(void);
  111. static void usb_show_progress(void)
  112. {
  113. debug(".");
  114. }
  115. /*******************************************************************************
  116. * show info on storage devices; 'usb start/init' must be invoked earlier
  117. * as we only retrieve structures populated during devices initialization
  118. */
  119. int usb_stor_info(void)
  120. {
  121. int count = 0;
  122. #if CONFIG_IS_ENABLED(BLK)
  123. struct udevice *dev;
  124. for (blk_first_device(IF_TYPE_USB, &dev);
  125. dev;
  126. blk_next_device(&dev)) {
  127. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  128. printf(" Device %d: ", desc->devnum);
  129. dev_print(desc);
  130. count++;
  131. }
  132. #else
  133. int i;
  134. if (usb_max_devs > 0) {
  135. for (i = 0; i < usb_max_devs; i++) {
  136. printf(" Device %d: ", i);
  137. dev_print(&usb_dev_desc[i]);
  138. }
  139. return 0;
  140. }
  141. #endif
  142. if (!count) {
  143. printf("No storage devices, perhaps not 'usb start'ed..?\n");
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. static unsigned int usb_get_max_lun(struct us_data *us)
  149. {
  150. int len;
  151. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
  152. len = usb_control_msg(us->pusb_dev,
  153. usb_rcvctrlpipe(us->pusb_dev, 0),
  154. US_BBB_GET_MAX_LUN,
  155. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  156. 0, us->ifnum,
  157. result, sizeof(char),
  158. USB_CNTL_TIMEOUT * 5);
  159. debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
  160. return (len > 0) ? *result : 0;
  161. }
  162. static int usb_stor_probe_device(struct usb_device *udev)
  163. {
  164. int lun, max_lun;
  165. #if CONFIG_IS_ENABLED(BLK)
  166. struct us_data *data;
  167. int ret;
  168. #else
  169. int start;
  170. if (udev == NULL)
  171. return -ENOENT; /* no more devices available */
  172. #endif
  173. debug("\n\nProbing for storage\n");
  174. #if CONFIG_IS_ENABLED(BLK)
  175. /*
  176. * We store the us_data in the mass storage device's platdata. It
  177. * is shared by all LUNs (block devices) attached to this mass storage
  178. * device.
  179. */
  180. data = dev_get_platdata(udev->dev);
  181. if (!usb_storage_probe(udev, 0, data))
  182. return 0;
  183. max_lun = usb_get_max_lun(data);
  184. for (lun = 0; lun <= max_lun; lun++) {
  185. struct blk_desc *blkdev;
  186. struct udevice *dev;
  187. char str[10];
  188. snprintf(str, sizeof(str), "lun%d", lun);
  189. ret = blk_create_devicef(udev->dev, "usb_storage_blk", str,
  190. IF_TYPE_USB, usb_max_devs, 512, 0,
  191. &dev);
  192. if (ret) {
  193. debug("Cannot bind driver\n");
  194. return ret;
  195. }
  196. blkdev = dev_get_uclass_platdata(dev);
  197. blkdev->target = 0xff;
  198. blkdev->lun = lun;
  199. ret = usb_stor_get_info(udev, data, blkdev);
  200. if (ret == 1) {
  201. usb_max_devs++;
  202. debug("%s: Found device %p\n", __func__, udev);
  203. } else {
  204. debug("usb_stor_get_info: Invalid device\n");
  205. ret = device_unbind(dev);
  206. if (ret)
  207. return ret;
  208. }
  209. }
  210. #else
  211. /* We don't have space to even probe if we hit the maximum */
  212. if (usb_max_devs == USB_MAX_STOR_DEV) {
  213. printf("max USB Storage Device reached: %d stopping\n",
  214. usb_max_devs);
  215. return -ENOSPC;
  216. }
  217. if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs]))
  218. return 0;
  219. /*
  220. * OK, it's a storage device. Iterate over its LUNs and populate
  221. * usb_dev_desc'
  222. */
  223. start = usb_max_devs;
  224. max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
  225. for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
  226. lun++) {
  227. struct blk_desc *blkdev;
  228. blkdev = &usb_dev_desc[usb_max_devs];
  229. memset(blkdev, '\0', sizeof(struct blk_desc));
  230. blkdev->if_type = IF_TYPE_USB;
  231. blkdev->devnum = usb_max_devs;
  232. blkdev->part_type = PART_TYPE_UNKNOWN;
  233. blkdev->target = 0xff;
  234. blkdev->type = DEV_TYPE_UNKNOWN;
  235. blkdev->block_read = usb_stor_read;
  236. blkdev->block_write = usb_stor_write;
  237. blkdev->lun = lun;
  238. blkdev->priv = udev;
  239. if (usb_stor_get_info(udev, &usb_stor[start],
  240. &usb_dev_desc[usb_max_devs]) == 1) {
  241. debug("partype: %d\n", blkdev->part_type);
  242. part_init(blkdev);
  243. debug("partype: %d\n", blkdev->part_type);
  244. usb_max_devs++;
  245. debug("%s: Found device %p\n", __func__, udev);
  246. }
  247. }
  248. #endif
  249. return 0;
  250. }
  251. void usb_stor_reset(void)
  252. {
  253. usb_max_devs = 0;
  254. }
  255. /*******************************************************************************
  256. * scan the usb and reports device info
  257. * to the user if mode = 1
  258. * returns current device or -1 if no
  259. */
  260. int usb_stor_scan(int mode)
  261. {
  262. if (mode == 1)
  263. printf(" scanning usb for storage devices... ");
  264. #if !CONFIG_IS_ENABLED(DM_USB)
  265. unsigned char i;
  266. usb_disable_asynch(1); /* asynch transfer not allowed */
  267. usb_stor_reset();
  268. for (i = 0; i < USB_MAX_DEVICE; i++) {
  269. struct usb_device *dev;
  270. dev = usb_get_dev_index(i); /* get device */
  271. debug("i=%d\n", i);
  272. if (usb_stor_probe_device(dev))
  273. break;
  274. } /* for */
  275. usb_disable_asynch(0); /* asynch transfer allowed */
  276. #endif
  277. printf("%d Storage Device(s) found\n", usb_max_devs);
  278. if (usb_max_devs > 0)
  279. return 0;
  280. return -1;
  281. }
  282. static int usb_stor_irq(struct usb_device *dev)
  283. {
  284. struct us_data *us;
  285. us = (struct us_data *)dev->privptr;
  286. if (us->ip_wanted)
  287. us->ip_wanted = 0;
  288. return 0;
  289. }
  290. #ifdef DEBUG
  291. static void usb_show_srb(struct scsi_cmd *pccb)
  292. {
  293. int i;
  294. printf("SRB: len %d datalen 0x%lX\n ", pccb->cmdlen, pccb->datalen);
  295. for (i = 0; i < 12; i++)
  296. printf("%02X ", pccb->cmd[i]);
  297. printf("\n");
  298. }
  299. static void display_int_status(unsigned long tmp)
  300. {
  301. printf("Status: %s %s %s %s %s %s %s\n",
  302. (tmp & USB_ST_ACTIVE) ? "Active" : "",
  303. (tmp & USB_ST_STALLED) ? "Stalled" : "",
  304. (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
  305. (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
  306. (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
  307. (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
  308. (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
  309. }
  310. #endif
  311. /***********************************************************************
  312. * Data transfer routines
  313. ***********************************************************************/
  314. static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
  315. {
  316. int max_size;
  317. int this_xfer;
  318. int result;
  319. int partial;
  320. int maxtry;
  321. int stat;
  322. /* determine the maximum packet size for these transfers */
  323. max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
  324. /* while we have data left to transfer */
  325. while (length) {
  326. /* calculate how long this will be -- maximum or a remainder */
  327. this_xfer = length > max_size ? max_size : length;
  328. length -= this_xfer;
  329. /* setup the retry counter */
  330. maxtry = 10;
  331. /* set up the transfer loop */
  332. do {
  333. /* transfer the data */
  334. debug("Bulk xfer 0x%lx(%d) try #%d\n",
  335. (ulong)map_to_sysmem(buf), this_xfer,
  336. 11 - maxtry);
  337. result = usb_bulk_msg(us->pusb_dev, pipe, buf,
  338. this_xfer, &partial,
  339. USB_CNTL_TIMEOUT * 5);
  340. debug("bulk_msg returned %d xferred %d/%d\n",
  341. result, partial, this_xfer);
  342. if (us->pusb_dev->status != 0) {
  343. /* if we stall, we need to clear it before
  344. * we go on
  345. */
  346. #ifdef DEBUG
  347. display_int_status(us->pusb_dev->status);
  348. #endif
  349. if (us->pusb_dev->status & USB_ST_STALLED) {
  350. debug("stalled ->clearing endpoint" \
  351. "halt for pipe 0x%x\n", pipe);
  352. stat = us->pusb_dev->status;
  353. usb_clear_halt(us->pusb_dev, pipe);
  354. us->pusb_dev->status = stat;
  355. if (this_xfer == partial) {
  356. debug("bulk transferred" \
  357. "with error %lX," \
  358. " but data ok\n",
  359. us->pusb_dev->status);
  360. return 0;
  361. }
  362. else
  363. return result;
  364. }
  365. if (us->pusb_dev->status & USB_ST_NAK_REC) {
  366. debug("Device NAKed bulk_msg\n");
  367. return result;
  368. }
  369. debug("bulk transferred with error");
  370. if (this_xfer == partial) {
  371. debug(" %ld, but data ok\n",
  372. us->pusb_dev->status);
  373. return 0;
  374. }
  375. /* if our try counter reaches 0, bail out */
  376. debug(" %ld, data %d\n",
  377. us->pusb_dev->status, partial);
  378. if (!maxtry--)
  379. return result;
  380. }
  381. /* update to show what data was transferred */
  382. this_xfer -= partial;
  383. buf += partial;
  384. /* continue until this transfer is done */
  385. } while (this_xfer);
  386. }
  387. /* if we get here, we're done and successful */
  388. return 0;
  389. }
  390. static int usb_stor_BBB_reset(struct us_data *us)
  391. {
  392. int result;
  393. unsigned int pipe;
  394. /*
  395. * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
  396. *
  397. * For Reset Recovery the host shall issue in the following order:
  398. * a) a Bulk-Only Mass Storage Reset
  399. * b) a Clear Feature HALT to the Bulk-In endpoint
  400. * c) a Clear Feature HALT to the Bulk-Out endpoint
  401. *
  402. * This is done in 3 steps.
  403. *
  404. * If the reset doesn't succeed, the device should be port reset.
  405. *
  406. * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
  407. */
  408. debug("BBB_reset\n");
  409. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  410. US_BBB_RESET,
  411. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  412. 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
  413. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  414. debug("RESET:stall\n");
  415. return -1;
  416. }
  417. /* long wait for reset */
  418. mdelay(150);
  419. debug("BBB_reset result %d: status %lX reset\n",
  420. result, us->pusb_dev->status);
  421. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  422. result = usb_clear_halt(us->pusb_dev, pipe);
  423. /* long wait for reset */
  424. mdelay(150);
  425. debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
  426. result, us->pusb_dev->status);
  427. /* long wait for reset */
  428. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  429. result = usb_clear_halt(us->pusb_dev, pipe);
  430. mdelay(150);
  431. debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
  432. result, us->pusb_dev->status);
  433. debug("BBB_reset done\n");
  434. return 0;
  435. }
  436. /* FIXME: this reset function doesn't really reset the port, and it
  437. * should. Actually it should probably do what it's doing here, and
  438. * reset the port physically
  439. */
  440. static int usb_stor_CB_reset(struct us_data *us)
  441. {
  442. unsigned char cmd[12];
  443. int result;
  444. debug("CB_reset\n");
  445. memset(cmd, 0xff, sizeof(cmd));
  446. cmd[0] = SCSI_SEND_DIAG;
  447. cmd[1] = 4;
  448. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  449. US_CBI_ADSC,
  450. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  451. 0, us->ifnum, cmd, sizeof(cmd),
  452. USB_CNTL_TIMEOUT * 5);
  453. /* long wait for reset */
  454. mdelay(1500);
  455. debug("CB_reset result %d: status %lX clearing endpoint halt\n",
  456. result, us->pusb_dev->status);
  457. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
  458. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
  459. debug("CB_reset done\n");
  460. return 0;
  461. }
  462. /*
  463. * Set up the command for a BBB device. Note that the actual SCSI
  464. * command is copied into cbw.CBWCDB.
  465. */
  466. static int usb_stor_BBB_comdat(struct scsi_cmd *srb, struct us_data *us)
  467. {
  468. int result;
  469. int actlen;
  470. int dir_in;
  471. unsigned int pipe;
  472. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1);
  473. dir_in = US_DIRECTION(srb->cmd[0]);
  474. #ifdef BBB_COMDAT_TRACE
  475. printf("dir %d lun %d cmdlen %d cmd %p datalen %lu pdata %p\n",
  476. dir_in, srb->lun, srb->cmdlen, srb->cmd, srb->datalen,
  477. srb->pdata);
  478. if (srb->cmdlen) {
  479. for (result = 0; result < srb->cmdlen; result++)
  480. printf("cmd[%d] %#x ", result, srb->cmd[result]);
  481. printf("\n");
  482. }
  483. #endif
  484. /* sanity checks */
  485. if (!(srb->cmdlen <= CBWCDBLENGTH)) {
  486. debug("usb_stor_BBB_comdat:cmdlen too large\n");
  487. return -1;
  488. }
  489. /* always OUT to the ep */
  490. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  491. cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
  492. cbw->dCBWTag = cpu_to_le32(CBWTag++);
  493. cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
  494. cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
  495. cbw->bCBWLUN = srb->lun;
  496. cbw->bCDBLength = srb->cmdlen;
  497. /* copy the command data into the CBW command data buffer */
  498. /* DST SRC LEN!!! */
  499. memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
  500. result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
  501. &actlen, USB_CNTL_TIMEOUT * 5);
  502. if (result < 0)
  503. debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
  504. return result;
  505. }
  506. /* FIXME: we also need a CBI_command which sets up the completion
  507. * interrupt, and waits for it
  508. */
  509. static int usb_stor_CB_comdat(struct scsi_cmd *srb, struct us_data *us)
  510. {
  511. int result = 0;
  512. int dir_in, retry;
  513. unsigned int pipe;
  514. unsigned long status;
  515. retry = 5;
  516. dir_in = US_DIRECTION(srb->cmd[0]);
  517. if (dir_in)
  518. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  519. else
  520. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  521. while (retry--) {
  522. debug("CBI gets a command: Try %d\n", 5 - retry);
  523. #ifdef DEBUG
  524. usb_show_srb(srb);
  525. #endif
  526. /* let's send the command via the control pipe */
  527. result = usb_control_msg(us->pusb_dev,
  528. usb_sndctrlpipe(us->pusb_dev , 0),
  529. US_CBI_ADSC,
  530. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  531. 0, us->ifnum,
  532. srb->cmd, srb->cmdlen,
  533. USB_CNTL_TIMEOUT * 5);
  534. debug("CB_transport: control msg returned %d, status %lX\n",
  535. result, us->pusb_dev->status);
  536. /* check the return code for the command */
  537. if (result < 0) {
  538. if (us->pusb_dev->status & USB_ST_STALLED) {
  539. status = us->pusb_dev->status;
  540. debug(" stall during command found," \
  541. " clear pipe\n");
  542. usb_clear_halt(us->pusb_dev,
  543. usb_sndctrlpipe(us->pusb_dev, 0));
  544. us->pusb_dev->status = status;
  545. }
  546. debug(" error during command %02X" \
  547. " Stat = %lX\n", srb->cmd[0],
  548. us->pusb_dev->status);
  549. return result;
  550. }
  551. /* transfer the data payload for this command, if one exists*/
  552. debug("CB_transport: control msg returned %d," \
  553. " direction is %s to go 0x%lx\n", result,
  554. dir_in ? "IN" : "OUT", srb->datalen);
  555. if (srb->datalen) {
  556. result = us_one_transfer(us, pipe, (char *)srb->pdata,
  557. srb->datalen);
  558. debug("CBI attempted to transfer data," \
  559. " result is %d status %lX, len %d\n",
  560. result, us->pusb_dev->status,
  561. us->pusb_dev->act_len);
  562. if (!(us->pusb_dev->status & USB_ST_NAK_REC))
  563. break;
  564. } /* if (srb->datalen) */
  565. else
  566. break;
  567. }
  568. /* return result */
  569. return result;
  570. }
  571. static int usb_stor_CBI_get_status(struct scsi_cmd *srb, struct us_data *us)
  572. {
  573. int timeout;
  574. us->ip_wanted = 1;
  575. usb_int_msg(us->pusb_dev, us->irqpipe,
  576. (void *)&us->ip_data, us->irqmaxp, us->irqinterval, false);
  577. timeout = 1000;
  578. while (timeout--) {
  579. if (us->ip_wanted == 0)
  580. break;
  581. mdelay(10);
  582. }
  583. if (us->ip_wanted) {
  584. printf(" Did not get interrupt on CBI\n");
  585. us->ip_wanted = 0;
  586. return USB_STOR_TRANSPORT_ERROR;
  587. }
  588. debug("Got interrupt data 0x%x, transferred %d status 0x%lX\n",
  589. us->ip_data, us->pusb_dev->irq_act_len,
  590. us->pusb_dev->irq_status);
  591. /* UFI gives us ASC and ASCQ, like a request sense */
  592. if (us->subclass == US_SC_UFI) {
  593. if (srb->cmd[0] == SCSI_REQ_SENSE ||
  594. srb->cmd[0] == SCSI_INQUIRY)
  595. return USB_STOR_TRANSPORT_GOOD; /* Good */
  596. else if (us->ip_data)
  597. return USB_STOR_TRANSPORT_FAILED;
  598. else
  599. return USB_STOR_TRANSPORT_GOOD;
  600. }
  601. /* otherwise, we interpret the data normally */
  602. switch (us->ip_data) {
  603. case 0x0001:
  604. return USB_STOR_TRANSPORT_GOOD;
  605. case 0x0002:
  606. return USB_STOR_TRANSPORT_FAILED;
  607. default:
  608. return USB_STOR_TRANSPORT_ERROR;
  609. } /* switch */
  610. return USB_STOR_TRANSPORT_ERROR;
  611. }
  612. #define USB_TRANSPORT_UNKNOWN_RETRY 5
  613. #define USB_TRANSPORT_NOT_READY_RETRY 10
  614. /* clear a stall on an endpoint - special for BBB devices */
  615. static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, __u8 endpt)
  616. {
  617. /* ENDPOINT_HALT = 0, so set value to 0 */
  618. return usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  619. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  620. endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
  621. }
  622. static int usb_stor_BBB_transport(struct scsi_cmd *srb, struct us_data *us)
  623. {
  624. int result, retry;
  625. int dir_in;
  626. int actlen, data_actlen;
  627. unsigned int pipe, pipein, pipeout;
  628. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1);
  629. #ifdef BBB_XPORT_TRACE
  630. unsigned char *ptr;
  631. int index;
  632. #endif
  633. dir_in = US_DIRECTION(srb->cmd[0]);
  634. /* COMMAND phase */
  635. debug("COMMAND phase\n");
  636. result = usb_stor_BBB_comdat(srb, us);
  637. if (result < 0) {
  638. debug("failed to send CBW status %ld\n",
  639. us->pusb_dev->status);
  640. usb_stor_BBB_reset(us);
  641. return USB_STOR_TRANSPORT_FAILED;
  642. }
  643. if (!(us->flags & USB_READY))
  644. mdelay(5);
  645. pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  646. pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  647. /* DATA phase + error handling */
  648. data_actlen = 0;
  649. /* no data, go immediately to the STATUS phase */
  650. if (srb->datalen == 0)
  651. goto st;
  652. debug("DATA phase\n");
  653. if (dir_in)
  654. pipe = pipein;
  655. else
  656. pipe = pipeout;
  657. result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
  658. &data_actlen, USB_CNTL_TIMEOUT * 5);
  659. /* special handling of STALL in DATA phase */
  660. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  661. debug("DATA:stall\n");
  662. /* clear the STALL on the endpoint */
  663. result = usb_stor_BBB_clear_endpt_stall(us,
  664. dir_in ? us->ep_in : us->ep_out);
  665. if (result >= 0)
  666. /* continue on to STATUS phase */
  667. goto st;
  668. }
  669. if (result < 0) {
  670. debug("usb_bulk_msg error status %ld\n",
  671. us->pusb_dev->status);
  672. usb_stor_BBB_reset(us);
  673. return USB_STOR_TRANSPORT_FAILED;
  674. }
  675. #ifdef BBB_XPORT_TRACE
  676. for (index = 0; index < data_actlen; index++)
  677. printf("pdata[%d] %#x ", index, srb->pdata[index]);
  678. printf("\n");
  679. #endif
  680. /* STATUS phase + error handling */
  681. st:
  682. retry = 0;
  683. again:
  684. debug("STATUS phase\n");
  685. result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
  686. &actlen, USB_CNTL_TIMEOUT*5);
  687. /* special handling of STALL in STATUS phase */
  688. if ((result < 0) && (retry < 1) &&
  689. (us->pusb_dev->status & USB_ST_STALLED)) {
  690. debug("STATUS:stall\n");
  691. /* clear the STALL on the endpoint */
  692. result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
  693. if (result >= 0 && (retry++ < 1))
  694. /* do a retry */
  695. goto again;
  696. }
  697. if (result < 0) {
  698. debug("usb_bulk_msg error status %ld\n",
  699. us->pusb_dev->status);
  700. usb_stor_BBB_reset(us);
  701. return USB_STOR_TRANSPORT_FAILED;
  702. }
  703. #ifdef BBB_XPORT_TRACE
  704. ptr = (unsigned char *)csw;
  705. for (index = 0; index < UMASS_BBB_CSW_SIZE; index++)
  706. printf("ptr[%d] %#x ", index, ptr[index]);
  707. printf("\n");
  708. #endif
  709. /* misuse pipe to get the residue */
  710. pipe = le32_to_cpu(csw->dCSWDataResidue);
  711. if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
  712. pipe = srb->datalen - data_actlen;
  713. if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
  714. debug("!CSWSIGNATURE\n");
  715. usb_stor_BBB_reset(us);
  716. return USB_STOR_TRANSPORT_FAILED;
  717. } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
  718. debug("!Tag\n");
  719. usb_stor_BBB_reset(us);
  720. return USB_STOR_TRANSPORT_FAILED;
  721. } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
  722. debug(">PHASE\n");
  723. usb_stor_BBB_reset(us);
  724. return USB_STOR_TRANSPORT_FAILED;
  725. } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
  726. debug("=PHASE\n");
  727. usb_stor_BBB_reset(us);
  728. return USB_STOR_TRANSPORT_FAILED;
  729. } else if (data_actlen > srb->datalen) {
  730. debug("transferred %dB instead of %ldB\n",
  731. data_actlen, srb->datalen);
  732. return USB_STOR_TRANSPORT_FAILED;
  733. } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
  734. debug("FAILED\n");
  735. return USB_STOR_TRANSPORT_FAILED;
  736. }
  737. return result;
  738. }
  739. static int usb_stor_CB_transport(struct scsi_cmd *srb, struct us_data *us)
  740. {
  741. int result, status;
  742. struct scsi_cmd *psrb;
  743. struct scsi_cmd reqsrb;
  744. int retry, notready;
  745. psrb = &reqsrb;
  746. status = USB_STOR_TRANSPORT_GOOD;
  747. retry = 0;
  748. notready = 0;
  749. /* issue the command */
  750. do_retry:
  751. result = usb_stor_CB_comdat(srb, us);
  752. debug("command / Data returned %d, status %lX\n",
  753. result, us->pusb_dev->status);
  754. /* if this is an CBI Protocol, get IRQ */
  755. if (us->protocol == US_PR_CBI) {
  756. status = usb_stor_CBI_get_status(srb, us);
  757. /* if the status is error, report it */
  758. if (status == USB_STOR_TRANSPORT_ERROR) {
  759. debug(" USB CBI Command Error\n");
  760. return status;
  761. }
  762. srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
  763. srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
  764. if (!us->ip_data) {
  765. /* if the status is good, report it */
  766. if (status == USB_STOR_TRANSPORT_GOOD) {
  767. debug(" USB CBI Command Good\n");
  768. return status;
  769. }
  770. }
  771. }
  772. /* do we have to issue an auto request? */
  773. /* HERE we have to check the result */
  774. if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
  775. debug("ERROR %lX\n", us->pusb_dev->status);
  776. us->transport_reset(us);
  777. return USB_STOR_TRANSPORT_ERROR;
  778. }
  779. if ((us->protocol == US_PR_CBI) &&
  780. ((srb->cmd[0] == SCSI_REQ_SENSE) ||
  781. (srb->cmd[0] == SCSI_INQUIRY))) {
  782. /* do not issue an autorequest after request sense */
  783. debug("No auto request and good\n");
  784. return USB_STOR_TRANSPORT_GOOD;
  785. }
  786. /* issue an request_sense */
  787. memset(&psrb->cmd[0], 0, 12);
  788. psrb->cmd[0] = SCSI_REQ_SENSE;
  789. psrb->cmd[1] = srb->lun << 5;
  790. psrb->cmd[4] = 18;
  791. psrb->datalen = 18;
  792. psrb->pdata = &srb->sense_buf[0];
  793. psrb->cmdlen = 12;
  794. /* issue the command */
  795. result = usb_stor_CB_comdat(psrb, us);
  796. debug("auto request returned %d\n", result);
  797. /* if this is an CBI Protocol, get IRQ */
  798. if (us->protocol == US_PR_CBI)
  799. status = usb_stor_CBI_get_status(psrb, us);
  800. if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
  801. debug(" AUTO REQUEST ERROR %ld\n",
  802. us->pusb_dev->status);
  803. return USB_STOR_TRANSPORT_ERROR;
  804. }
  805. debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
  806. srb->sense_buf[0], srb->sense_buf[2],
  807. srb->sense_buf[12], srb->sense_buf[13]);
  808. /* Check the auto request result */
  809. if ((srb->sense_buf[2] == 0) &&
  810. (srb->sense_buf[12] == 0) &&
  811. (srb->sense_buf[13] == 0)) {
  812. /* ok, no sense */
  813. return USB_STOR_TRANSPORT_GOOD;
  814. }
  815. /* Check the auto request result */
  816. switch (srb->sense_buf[2]) {
  817. case 0x01:
  818. /* Recovered Error */
  819. return USB_STOR_TRANSPORT_GOOD;
  820. break;
  821. case 0x02:
  822. /* Not Ready */
  823. if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
  824. printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
  825. " 0x%02X (NOT READY)\n", srb->cmd[0],
  826. srb->sense_buf[0], srb->sense_buf[2],
  827. srb->sense_buf[12], srb->sense_buf[13]);
  828. return USB_STOR_TRANSPORT_FAILED;
  829. } else {
  830. mdelay(100);
  831. goto do_retry;
  832. }
  833. break;
  834. default:
  835. if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
  836. printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
  837. " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
  838. srb->sense_buf[2], srb->sense_buf[12],
  839. srb->sense_buf[13]);
  840. return USB_STOR_TRANSPORT_FAILED;
  841. } else
  842. goto do_retry;
  843. break;
  844. }
  845. return USB_STOR_TRANSPORT_FAILED;
  846. }
  847. static void usb_stor_set_max_xfer_blk(struct usb_device *udev,
  848. struct us_data *us)
  849. {
  850. /*
  851. * Limit the total size of a transfer to 120 KB.
  852. *
  853. * Some devices are known to choke with anything larger. It seems like
  854. * the problem stems from the fact that original IDE controllers had
  855. * only an 8-bit register to hold the number of sectors in one transfer
  856. * and even those couldn't handle a full 256 sectors.
  857. *
  858. * Because we want to make sure we interoperate with as many devices as
  859. * possible, we will maintain a 240 sector transfer size limit for USB
  860. * Mass Storage devices.
  861. *
  862. * Tests show that other operating have similar limits with Microsoft
  863. * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
  864. * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
  865. * and 2048 for USB3 devices.
  866. */
  867. unsigned short blk = 240;
  868. #if CONFIG_IS_ENABLED(DM_USB)
  869. size_t size;
  870. int ret;
  871. ret = usb_get_max_xfer_size(udev, (size_t *)&size);
  872. if ((ret >= 0) && (size < blk * 512))
  873. blk = size / 512;
  874. #endif
  875. us->max_xfer_blk = blk;
  876. }
  877. static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss)
  878. {
  879. int retry, i;
  880. retry = 5;
  881. do {
  882. memset(&srb->cmd[0], 0, 12);
  883. srb->cmd[0] = SCSI_INQUIRY;
  884. srb->cmd[1] = srb->lun << 5;
  885. srb->cmd[4] = 36;
  886. srb->datalen = 36;
  887. srb->cmdlen = 12;
  888. i = ss->transport(srb, ss);
  889. debug("inquiry returns %d\n", i);
  890. if (i == 0)
  891. break;
  892. } while (--retry);
  893. if (!retry) {
  894. printf("error in inquiry\n");
  895. return -1;
  896. }
  897. return 0;
  898. }
  899. static int usb_request_sense(struct scsi_cmd *srb, struct us_data *ss)
  900. {
  901. char *ptr;
  902. ptr = (char *)srb->pdata;
  903. memset(&srb->cmd[0], 0, 12);
  904. srb->cmd[0] = SCSI_REQ_SENSE;
  905. srb->cmd[1] = srb->lun << 5;
  906. srb->cmd[4] = 18;
  907. srb->datalen = 18;
  908. srb->pdata = &srb->sense_buf[0];
  909. srb->cmdlen = 12;
  910. ss->transport(srb, ss);
  911. debug("Request Sense returned %02X %02X %02X\n",
  912. srb->sense_buf[2], srb->sense_buf[12],
  913. srb->sense_buf[13]);
  914. srb->pdata = (uchar *)ptr;
  915. return 0;
  916. }
  917. static int usb_test_unit_ready(struct scsi_cmd *srb, struct us_data *ss)
  918. {
  919. int retries = 10;
  920. do {
  921. memset(&srb->cmd[0], 0, 12);
  922. srb->cmd[0] = SCSI_TST_U_RDY;
  923. srb->cmd[1] = srb->lun << 5;
  924. srb->datalen = 0;
  925. srb->cmdlen = 12;
  926. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
  927. ss->flags |= USB_READY;
  928. return 0;
  929. }
  930. usb_request_sense(srb, ss);
  931. /*
  932. * Check the Key Code Qualifier, if it matches
  933. * "Not Ready - medium not present"
  934. * (the sense Key equals 0x2 and the ASC is 0x3a)
  935. * return immediately as the medium being absent won't change
  936. * unless there is a user action.
  937. */
  938. if ((srb->sense_buf[2] == 0x02) &&
  939. (srb->sense_buf[12] == 0x3a))
  940. return -1;
  941. mdelay(100);
  942. } while (retries--);
  943. return -1;
  944. }
  945. static int usb_read_capacity(struct scsi_cmd *srb, struct us_data *ss)
  946. {
  947. int retry;
  948. /* XXX retries */
  949. retry = 3;
  950. do {
  951. memset(&srb->cmd[0], 0, 12);
  952. srb->cmd[0] = SCSI_RD_CAPAC;
  953. srb->cmd[1] = srb->lun << 5;
  954. srb->datalen = 8;
  955. srb->cmdlen = 12;
  956. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
  957. return 0;
  958. } while (retry--);
  959. return -1;
  960. }
  961. static int usb_read_10(struct scsi_cmd *srb, struct us_data *ss,
  962. unsigned long start, unsigned short blocks)
  963. {
  964. memset(&srb->cmd[0], 0, 12);
  965. srb->cmd[0] = SCSI_READ10;
  966. srb->cmd[1] = srb->lun << 5;
  967. srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
  968. srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
  969. srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
  970. srb->cmd[5] = ((unsigned char) (start)) & 0xff;
  971. srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
  972. srb->cmd[8] = (unsigned char) blocks & 0xff;
  973. srb->cmdlen = 12;
  974. debug("read10: start %lx blocks %x\n", start, blocks);
  975. return ss->transport(srb, ss);
  976. }
  977. static int usb_write_10(struct scsi_cmd *srb, struct us_data *ss,
  978. unsigned long start, unsigned short blocks)
  979. {
  980. memset(&srb->cmd[0], 0, 12);
  981. srb->cmd[0] = SCSI_WRITE10;
  982. srb->cmd[1] = srb->lun << 5;
  983. srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
  984. srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
  985. srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
  986. srb->cmd[5] = ((unsigned char) (start)) & 0xff;
  987. srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
  988. srb->cmd[8] = (unsigned char) blocks & 0xff;
  989. srb->cmdlen = 12;
  990. debug("write10: start %lx blocks %x\n", start, blocks);
  991. return ss->transport(srb, ss);
  992. }
  993. #ifdef CONFIG_USB_BIN_FIXUP
  994. /*
  995. * Some USB storage devices queried for SCSI identification data respond with
  996. * binary strings, which if output to the console freeze the terminal. The
  997. * workaround is to modify the vendor and product strings read from such
  998. * device with proper values (as reported by 'usb info').
  999. *
  1000. * Vendor and product length limits are taken from the definition of
  1001. * struct blk_desc in include/part.h.
  1002. */
  1003. static void usb_bin_fixup(struct usb_device_descriptor descriptor,
  1004. unsigned char vendor[],
  1005. unsigned char product[]) {
  1006. const unsigned char max_vendor_len = 40;
  1007. const unsigned char max_product_len = 20;
  1008. if (descriptor.idVendor == 0x0424 && descriptor.idProduct == 0x223a) {
  1009. strncpy((char *)vendor, "SMSC", max_vendor_len);
  1010. strncpy((char *)product, "Flash Media Cntrller",
  1011. max_product_len);
  1012. }
  1013. }
  1014. #endif /* CONFIG_USB_BIN_FIXUP */
  1015. #if CONFIG_IS_ENABLED(BLK)
  1016. static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
  1017. lbaint_t blkcnt, void *buffer)
  1018. #else
  1019. static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
  1020. lbaint_t blkcnt, void *buffer)
  1021. #endif
  1022. {
  1023. lbaint_t start, blks;
  1024. uintptr_t buf_addr;
  1025. unsigned short smallblks;
  1026. struct usb_device *udev;
  1027. struct us_data *ss;
  1028. int retry;
  1029. struct scsi_cmd *srb = &usb_ccb;
  1030. #if CONFIG_IS_ENABLED(BLK)
  1031. struct blk_desc *block_dev;
  1032. #endif
  1033. if (blkcnt == 0)
  1034. return 0;
  1035. /* Setup device */
  1036. #if CONFIG_IS_ENABLED(BLK)
  1037. block_dev = dev_get_uclass_platdata(dev);
  1038. udev = dev_get_parent_priv(dev_get_parent(dev));
  1039. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1040. #else
  1041. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1042. udev = usb_dev_desc[block_dev->devnum].priv;
  1043. if (!udev) {
  1044. debug("%s: No device\n", __func__);
  1045. return 0;
  1046. }
  1047. #endif
  1048. ss = (struct us_data *)udev->privptr;
  1049. usb_disable_asynch(1); /* asynch transfer not allowed */
  1050. usb_lock_async(udev, 1);
  1051. srb->lun = block_dev->lun;
  1052. buf_addr = (uintptr_t)buffer;
  1053. start = blknr;
  1054. blks = blkcnt;
  1055. debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
  1056. block_dev->devnum, start, blks, buf_addr);
  1057. do {
  1058. /* XXX need some comment here */
  1059. retry = 2;
  1060. srb->pdata = (unsigned char *)buf_addr;
  1061. if (blks > ss->max_xfer_blk)
  1062. smallblks = ss->max_xfer_blk;
  1063. else
  1064. smallblks = (unsigned short) blks;
  1065. retry_it:
  1066. if (smallblks == ss->max_xfer_blk)
  1067. usb_show_progress();
  1068. srb->datalen = block_dev->blksz * smallblks;
  1069. srb->pdata = (unsigned char *)buf_addr;
  1070. if (usb_read_10(srb, ss, start, smallblks)) {
  1071. debug("Read ERROR\n");
  1072. ss->flags &= ~USB_READY;
  1073. usb_request_sense(srb, ss);
  1074. if (retry--)
  1075. goto retry_it;
  1076. blkcnt -= blks;
  1077. break;
  1078. }
  1079. start += smallblks;
  1080. blks -= smallblks;
  1081. buf_addr += srb->datalen;
  1082. } while (blks != 0);
  1083. debug("usb_read: end startblk " LBAF ", blccnt %x buffer %lx\n",
  1084. start, smallblks, buf_addr);
  1085. usb_lock_async(udev, 0);
  1086. usb_disable_asynch(0); /* asynch transfer allowed */
  1087. if (blkcnt >= ss->max_xfer_blk)
  1088. debug("\n");
  1089. return blkcnt;
  1090. }
  1091. #if CONFIG_IS_ENABLED(BLK)
  1092. static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
  1093. lbaint_t blkcnt, const void *buffer)
  1094. #else
  1095. static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
  1096. lbaint_t blkcnt, const void *buffer)
  1097. #endif
  1098. {
  1099. lbaint_t start, blks;
  1100. uintptr_t buf_addr;
  1101. unsigned short smallblks;
  1102. struct usb_device *udev;
  1103. struct us_data *ss;
  1104. int retry;
  1105. struct scsi_cmd *srb = &usb_ccb;
  1106. #if CONFIG_IS_ENABLED(BLK)
  1107. struct blk_desc *block_dev;
  1108. #endif
  1109. if (blkcnt == 0)
  1110. return 0;
  1111. /* Setup device */
  1112. #if CONFIG_IS_ENABLED(BLK)
  1113. block_dev = dev_get_uclass_platdata(dev);
  1114. udev = dev_get_parent_priv(dev_get_parent(dev));
  1115. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1116. #else
  1117. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1118. udev = usb_dev_desc[block_dev->devnum].priv;
  1119. if (!udev) {
  1120. debug("%s: No device\n", __func__);
  1121. return 0;
  1122. }
  1123. #endif
  1124. ss = (struct us_data *)udev->privptr;
  1125. usb_disable_asynch(1); /* asynch transfer not allowed */
  1126. usb_lock_async(udev, 1);
  1127. srb->lun = block_dev->lun;
  1128. buf_addr = (uintptr_t)buffer;
  1129. start = blknr;
  1130. blks = blkcnt;
  1131. debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
  1132. block_dev->devnum, start, blks, buf_addr);
  1133. do {
  1134. /* If write fails retry for max retry count else
  1135. * return with number of blocks written successfully.
  1136. */
  1137. retry = 2;
  1138. srb->pdata = (unsigned char *)buf_addr;
  1139. if (blks > ss->max_xfer_blk)
  1140. smallblks = ss->max_xfer_blk;
  1141. else
  1142. smallblks = (unsigned short) blks;
  1143. retry_it:
  1144. if (smallblks == ss->max_xfer_blk)
  1145. usb_show_progress();
  1146. srb->datalen = block_dev->blksz * smallblks;
  1147. srb->pdata = (unsigned char *)buf_addr;
  1148. if (usb_write_10(srb, ss, start, smallblks)) {
  1149. debug("Write ERROR\n");
  1150. ss->flags &= ~USB_READY;
  1151. usb_request_sense(srb, ss);
  1152. if (retry--)
  1153. goto retry_it;
  1154. blkcnt -= blks;
  1155. break;
  1156. }
  1157. start += smallblks;
  1158. blks -= smallblks;
  1159. buf_addr += srb->datalen;
  1160. } while (blks != 0);
  1161. debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
  1162. start, smallblks, buf_addr);
  1163. usb_lock_async(udev, 0);
  1164. usb_disable_asynch(0); /* asynch transfer allowed */
  1165. if (blkcnt >= ss->max_xfer_blk)
  1166. debug("\n");
  1167. return blkcnt;
  1168. }
  1169. /* Probe to see if a new device is actually a Storage device */
  1170. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  1171. struct us_data *ss)
  1172. {
  1173. struct usb_interface *iface;
  1174. int i;
  1175. struct usb_endpoint_descriptor *ep_desc;
  1176. unsigned int flags = 0;
  1177. /* let's examine the device now */
  1178. iface = &dev->config.if_desc[ifnum];
  1179. if (dev->descriptor.bDeviceClass != 0 ||
  1180. iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  1181. iface->desc.bInterfaceSubClass < US_SC_MIN ||
  1182. iface->desc.bInterfaceSubClass > US_SC_MAX) {
  1183. debug("Not mass storage\n");
  1184. /* if it's not a mass storage, we go no further */
  1185. return 0;
  1186. }
  1187. memset(ss, 0, sizeof(struct us_data));
  1188. /* At this point, we know we've got a live one */
  1189. debug("\n\nUSB Mass Storage device detected\n");
  1190. /* Initialize the us_data structure with some useful info */
  1191. ss->flags = flags;
  1192. ss->ifnum = ifnum;
  1193. ss->pusb_dev = dev;
  1194. ss->attention_done = 0;
  1195. ss->subclass = iface->desc.bInterfaceSubClass;
  1196. ss->protocol = iface->desc.bInterfaceProtocol;
  1197. /* set the handler pointers based on the protocol */
  1198. debug("Transport: ");
  1199. switch (ss->protocol) {
  1200. case US_PR_CB:
  1201. debug("Control/Bulk\n");
  1202. ss->transport = usb_stor_CB_transport;
  1203. ss->transport_reset = usb_stor_CB_reset;
  1204. break;
  1205. case US_PR_CBI:
  1206. debug("Control/Bulk/Interrupt\n");
  1207. ss->transport = usb_stor_CB_transport;
  1208. ss->transport_reset = usb_stor_CB_reset;
  1209. break;
  1210. case US_PR_BULK:
  1211. debug("Bulk/Bulk/Bulk\n");
  1212. ss->transport = usb_stor_BBB_transport;
  1213. ss->transport_reset = usb_stor_BBB_reset;
  1214. break;
  1215. default:
  1216. printf("USB Storage Transport unknown / not yet implemented\n");
  1217. return 0;
  1218. break;
  1219. }
  1220. /*
  1221. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1222. * An optional interrupt is OK (necessary for CBI protocol).
  1223. * We will ignore any others.
  1224. */
  1225. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1226. ep_desc = &iface->ep_desc[i];
  1227. /* is it an BULK endpoint? */
  1228. if ((ep_desc->bmAttributes &
  1229. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  1230. if (ep_desc->bEndpointAddress & USB_DIR_IN)
  1231. ss->ep_in = ep_desc->bEndpointAddress &
  1232. USB_ENDPOINT_NUMBER_MASK;
  1233. else
  1234. ss->ep_out =
  1235. ep_desc->bEndpointAddress &
  1236. USB_ENDPOINT_NUMBER_MASK;
  1237. }
  1238. /* is it an interrupt endpoint? */
  1239. if ((ep_desc->bmAttributes &
  1240. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  1241. ss->ep_int = ep_desc->bEndpointAddress &
  1242. USB_ENDPOINT_NUMBER_MASK;
  1243. ss->irqinterval = ep_desc->bInterval;
  1244. }
  1245. }
  1246. debug("Endpoints In %d Out %d Int %d\n",
  1247. ss->ep_in, ss->ep_out, ss->ep_int);
  1248. /* Do some basic sanity checks, and bail if we find a problem */
  1249. if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
  1250. !ss->ep_in || !ss->ep_out ||
  1251. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  1252. debug("Problems with device\n");
  1253. return 0;
  1254. }
  1255. /* set class specific stuff */
  1256. /* We only handle certain protocols. Currently, these are
  1257. * the only ones.
  1258. * The SFF8070 accepts the requests used in u-boot
  1259. */
  1260. if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
  1261. ss->subclass != US_SC_8070) {
  1262. printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
  1263. return 0;
  1264. }
  1265. if (ss->ep_int) {
  1266. /* we had found an interrupt endpoint, prepare irq pipe
  1267. * set up the IRQ pipe and handler
  1268. */
  1269. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  1270. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  1271. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  1272. dev->irq_handle = usb_stor_irq;
  1273. }
  1274. /* Set the maximum transfer size per host controller setting */
  1275. usb_stor_set_max_xfer_blk(dev, ss);
  1276. dev->privptr = (void *)ss;
  1277. return 1;
  1278. }
  1279. int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
  1280. struct blk_desc *dev_desc)
  1281. {
  1282. unsigned char perq, modi;
  1283. ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
  1284. ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
  1285. u32 capacity, blksz;
  1286. struct scsi_cmd *pccb = &usb_ccb;
  1287. pccb->pdata = usb_stor_buf;
  1288. dev_desc->target = dev->devnum;
  1289. pccb->lun = dev_desc->lun;
  1290. debug(" address %d\n", dev_desc->target);
  1291. if (usb_inquiry(pccb, ss)) {
  1292. debug("%s: usb_inquiry() failed\n", __func__);
  1293. return -1;
  1294. }
  1295. perq = usb_stor_buf[0];
  1296. modi = usb_stor_buf[1];
  1297. /*
  1298. * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
  1299. * they would not respond to test_unit_ready .
  1300. */
  1301. if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
  1302. debug("%s: unknown/unsupported device\n", __func__);
  1303. return 0;
  1304. }
  1305. if ((modi&0x80) == 0x80) {
  1306. /* drive is removable */
  1307. dev_desc->removable = 1;
  1308. }
  1309. memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
  1310. memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
  1311. memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
  1312. dev_desc->vendor[8] = 0;
  1313. dev_desc->product[16] = 0;
  1314. dev_desc->revision[4] = 0;
  1315. #ifdef CONFIG_USB_BIN_FIXUP
  1316. usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
  1317. (uchar *)dev_desc->product);
  1318. #endif /* CONFIG_USB_BIN_FIXUP */
  1319. debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
  1320. usb_stor_buf[3]);
  1321. if (usb_test_unit_ready(pccb, ss)) {
  1322. printf("Device NOT ready\n"
  1323. " Request Sense returned %02X %02X %02X\n",
  1324. pccb->sense_buf[2], pccb->sense_buf[12],
  1325. pccb->sense_buf[13]);
  1326. if (dev_desc->removable == 1)
  1327. dev_desc->type = perq;
  1328. return 0;
  1329. }
  1330. pccb->pdata = (unsigned char *)cap;
  1331. memset(pccb->pdata, 0, 8);
  1332. if (usb_read_capacity(pccb, ss) != 0) {
  1333. printf("READ_CAP ERROR\n");
  1334. ss->flags &= ~USB_READY;
  1335. cap[0] = 2880;
  1336. cap[1] = 0x200;
  1337. }
  1338. debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
  1339. #if 0
  1340. if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
  1341. cap[0] >>= 16;
  1342. cap[0] = cpu_to_be32(cap[0]);
  1343. cap[1] = cpu_to_be32(cap[1]);
  1344. #endif
  1345. capacity = be32_to_cpu(cap[0]) + 1;
  1346. blksz = be32_to_cpu(cap[1]);
  1347. debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz);
  1348. dev_desc->lba = capacity;
  1349. dev_desc->blksz = blksz;
  1350. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  1351. dev_desc->type = perq;
  1352. debug(" address %d\n", dev_desc->target);
  1353. return 1;
  1354. }
  1355. #if CONFIG_IS_ENABLED(DM_USB)
  1356. static int usb_mass_storage_probe(struct udevice *dev)
  1357. {
  1358. struct usb_device *udev = dev_get_parent_priv(dev);
  1359. int ret;
  1360. usb_disable_asynch(1); /* asynch transfer not allowed */
  1361. ret = usb_stor_probe_device(udev);
  1362. usb_disable_asynch(0); /* asynch transfer allowed */
  1363. return ret;
  1364. }
  1365. static const struct udevice_id usb_mass_storage_ids[] = {
  1366. { .compatible = "usb-mass-storage" },
  1367. { }
  1368. };
  1369. U_BOOT_DRIVER(usb_mass_storage) = {
  1370. .name = "usb_mass_storage",
  1371. .id = UCLASS_MASS_STORAGE,
  1372. .of_match = usb_mass_storage_ids,
  1373. .probe = usb_mass_storage_probe,
  1374. #if CONFIG_IS_ENABLED(BLK)
  1375. .platdata_auto_alloc_size = sizeof(struct us_data),
  1376. #endif
  1377. };
  1378. UCLASS_DRIVER(usb_mass_storage) = {
  1379. .id = UCLASS_MASS_STORAGE,
  1380. .name = "usb_mass_storage",
  1381. };
  1382. static const struct usb_device_id mass_storage_id_table[] = {
  1383. {
  1384. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1385. .bInterfaceClass = USB_CLASS_MASS_STORAGE
  1386. },
  1387. { } /* Terminating entry */
  1388. };
  1389. U_BOOT_USB_DEVICE(usb_mass_storage, mass_storage_id_table);
  1390. #endif
  1391. #if CONFIG_IS_ENABLED(BLK)
  1392. static const struct blk_ops usb_storage_ops = {
  1393. .read = usb_stor_read,
  1394. .write = usb_stor_write,
  1395. };
  1396. U_BOOT_DRIVER(usb_storage_blk) = {
  1397. .name = "usb_storage_blk",
  1398. .id = UCLASS_BLK,
  1399. .ops = &usb_storage_ops,
  1400. };
  1401. #else
  1402. U_BOOT_LEGACY_BLK(usb) = {
  1403. .if_typename = "usb",
  1404. .if_type = IF_TYPE_USB,
  1405. .max_devs = USB_MAX_STOR_DEV,
  1406. .desc = usb_dev_desc,
  1407. };
  1408. #endif