usb_storage.c 42 KB

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