usb_storage.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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 <blk.h>
  34. #include <command.h>
  35. #include <dm.h>
  36. #include <errno.h>
  37. #include <log.h>
  38. #include <mapmem.h>
  39. #include <memalign.h>
  40. #include <asm/byteorder.h>
  41. #include <asm/cache.h>
  42. #include <asm/processor.h>
  43. #include <dm/device-internal.h>
  44. #include <dm/lists.h>
  45. #include <linux/delay.h>
  46. #include <part.h>
  47. #include <usb.h>
  48. #undef BBB_COMDAT_TRACE
  49. #undef BBB_XPORT_TRACE
  50. #include <scsi.h>
  51. /* direction table -- this indicates the direction of the data
  52. * transfer for each command code -- a 1 indicates input
  53. */
  54. static const unsigned char us_direction[256/8] = {
  55. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  56. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  59. };
  60. #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
  61. static struct scsi_cmd usb_ccb __aligned(ARCH_DMA_MINALIGN);
  62. static __u32 CBWTag;
  63. static int usb_max_devs; /* number of highest available usb device */
  64. #if !CONFIG_IS_ENABLED(BLK)
  65. static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV];
  66. #endif
  67. struct us_data;
  68. typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
  69. typedef int (*trans_reset)(struct us_data *data);
  70. struct us_data {
  71. struct usb_device *pusb_dev; /* this usb_device */
  72. unsigned int flags; /* from filter initially */
  73. # define USB_READY (1 << 0)
  74. unsigned char ifnum; /* interface number */
  75. unsigned char ep_in; /* in endpoint */
  76. unsigned char ep_out; /* out ....... */
  77. unsigned char ep_int; /* interrupt . */
  78. unsigned char subclass; /* as in overview */
  79. unsigned char protocol; /* .............. */
  80. unsigned char attention_done; /* force attn on first cmd */
  81. unsigned short ip_data; /* interrupt data */
  82. int action; /* what to do */
  83. int ip_wanted; /* needed */
  84. int *irq_handle; /* for USB int requests */
  85. unsigned int irqpipe; /* pipe for release_irq */
  86. unsigned char irqmaxp; /* max packed for irq Pipe */
  87. unsigned char irqinterval; /* Intervall for IRQ Pipe */
  88. struct scsi_cmd *srb; /* current srb */
  89. trans_reset transport_reset; /* reset routine */
  90. trans_cmnd transport; /* transport routine */
  91. unsigned short max_xfer_blk; /* maximum transfer blocks */
  92. };
  93. #if !CONFIG_IS_ENABLED(BLK)
  94. static struct us_data usb_stor[USB_MAX_STOR_DEV];
  95. #endif
  96. #define USB_STOR_TRANSPORT_GOOD 0
  97. #define USB_STOR_TRANSPORT_FAILED -1
  98. #define USB_STOR_TRANSPORT_ERROR -2
  99. int usb_stor_get_info(struct usb_device *dev, struct us_data *us,
  100. struct blk_desc *dev_desc);
  101. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  102. struct us_data *ss);
  103. #if CONFIG_IS_ENABLED(BLK)
  104. static unsigned long usb_stor_read(struct udevice *dev, lbaint_t blknr,
  105. lbaint_t blkcnt, void *buffer);
  106. static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
  107. lbaint_t blkcnt, const void *buffer);
  108. #else
  109. static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
  110. lbaint_t blkcnt, void *buffer);
  111. static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
  112. lbaint_t blkcnt, const void *buffer);
  113. #endif
  114. void uhci_show_temp_int_td(void);
  115. static void usb_show_progress(void)
  116. {
  117. debug(".");
  118. }
  119. /*******************************************************************************
  120. * show info on storage devices; 'usb start/init' must be invoked earlier
  121. * as we only retrieve structures populated during devices initialization
  122. */
  123. int usb_stor_info(void)
  124. {
  125. int count = 0;
  126. #if CONFIG_IS_ENABLED(BLK)
  127. struct udevice *dev;
  128. for (blk_first_device(IF_TYPE_USB, &dev);
  129. dev;
  130. blk_next_device(&dev)) {
  131. struct blk_desc *desc = dev_get_uclass_platdata(dev);
  132. printf(" Device %d: ", desc->devnum);
  133. dev_print(desc);
  134. count++;
  135. }
  136. #else
  137. int i;
  138. if (usb_max_devs > 0) {
  139. for (i = 0; i < usb_max_devs; i++) {
  140. printf(" Device %d: ", i);
  141. dev_print(&usb_dev_desc[i]);
  142. }
  143. return 0;
  144. }
  145. #endif
  146. if (!count) {
  147. printf("No storage devices, perhaps not 'usb start'ed..?\n");
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. static unsigned int usb_get_max_lun(struct us_data *us)
  153. {
  154. int len;
  155. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
  156. len = usb_control_msg(us->pusb_dev,
  157. usb_rcvctrlpipe(us->pusb_dev, 0),
  158. US_BBB_GET_MAX_LUN,
  159. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  160. 0, us->ifnum,
  161. result, sizeof(char),
  162. USB_CNTL_TIMEOUT * 5);
  163. debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
  164. return (len > 0) ? *result : 0;
  165. }
  166. static int usb_stor_probe_device(struct usb_device *udev)
  167. {
  168. int lun, max_lun;
  169. #if CONFIG_IS_ENABLED(BLK)
  170. struct us_data *data;
  171. int ret;
  172. #else
  173. int start;
  174. if (udev == NULL)
  175. return -ENOENT; /* no more devices available */
  176. #endif
  177. debug("\n\nProbing for storage\n");
  178. #if CONFIG_IS_ENABLED(BLK)
  179. /*
  180. * We store the us_data in the mass storage device's platdata. It
  181. * is shared by all LUNs (block devices) attached to this mass storage
  182. * device.
  183. */
  184. data = dev_get_platdata(udev->dev);
  185. if (!usb_storage_probe(udev, 0, data))
  186. return 0;
  187. max_lun = usb_get_max_lun(data);
  188. for (lun = 0; lun <= max_lun; lun++) {
  189. struct blk_desc *blkdev;
  190. struct udevice *dev;
  191. char str[10];
  192. snprintf(str, sizeof(str), "lun%d", lun);
  193. ret = blk_create_devicef(udev->dev, "usb_storage_blk", str,
  194. IF_TYPE_USB, usb_max_devs, 512, 0,
  195. &dev);
  196. if (ret) {
  197. debug("Cannot bind driver\n");
  198. return ret;
  199. }
  200. blkdev = dev_get_uclass_platdata(dev);
  201. blkdev->target = 0xff;
  202. blkdev->lun = lun;
  203. ret = usb_stor_get_info(udev, data, blkdev);
  204. if (ret == 1) {
  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. #if !CONFIG_IS_ENABLED(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. usb_int_msg(us->pusb_dev, us->irqpipe,
  580. (void *)&us->ip_data, us->irqmaxp, us->irqinterval, false);
  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. /*
  855. * Limit the total size of a transfer to 120 KB.
  856. *
  857. * Some devices are known to choke with anything larger. It seems like
  858. * the problem stems from the fact that original IDE controllers had
  859. * only an 8-bit register to hold the number of sectors in one transfer
  860. * and even those couldn't handle a full 256 sectors.
  861. *
  862. * Because we want to make sure we interoperate with as many devices as
  863. * possible, we will maintain a 240 sector transfer size limit for USB
  864. * Mass Storage devices.
  865. *
  866. * Tests show that other operating have similar limits with Microsoft
  867. * Windows 7 limiting transfers to 128 sectors for both USB2 and USB3
  868. * and Apple Mac OS X 10.11 limiting transfers to 256 sectors for USB2
  869. * and 2048 for USB3 devices.
  870. */
  871. unsigned short blk = 240;
  872. #if CONFIG_IS_ENABLED(DM_USB)
  873. size_t size;
  874. int ret;
  875. ret = usb_get_max_xfer_size(udev, (size_t *)&size);
  876. if ((ret >= 0) && (size < blk * 512))
  877. blk = size / 512;
  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. #if CONFIG_IS_ENABLED(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. #if CONFIG_IS_ENABLED(BLK)
  1035. struct blk_desc *block_dev;
  1036. #endif
  1037. if (blkcnt == 0)
  1038. return 0;
  1039. /* Setup device */
  1040. #if CONFIG_IS_ENABLED(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. usb_lock_async(udev, 1);
  1055. srb->lun = block_dev->lun;
  1056. buf_addr = (uintptr_t)buffer;
  1057. start = blknr;
  1058. blks = blkcnt;
  1059. debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
  1060. block_dev->devnum, start, blks, buf_addr);
  1061. do {
  1062. /* XXX need some comment here */
  1063. retry = 2;
  1064. srb->pdata = (unsigned char *)buf_addr;
  1065. if (blks > ss->max_xfer_blk)
  1066. smallblks = ss->max_xfer_blk;
  1067. else
  1068. smallblks = (unsigned short) blks;
  1069. retry_it:
  1070. if (smallblks == ss->max_xfer_blk)
  1071. usb_show_progress();
  1072. srb->datalen = block_dev->blksz * smallblks;
  1073. srb->pdata = (unsigned char *)buf_addr;
  1074. if (usb_read_10(srb, ss, start, smallblks)) {
  1075. debug("Read ERROR\n");
  1076. ss->flags &= ~USB_READY;
  1077. usb_request_sense(srb, ss);
  1078. if (retry--)
  1079. goto retry_it;
  1080. blkcnt -= blks;
  1081. break;
  1082. }
  1083. start += smallblks;
  1084. blks -= smallblks;
  1085. buf_addr += srb->datalen;
  1086. } while (blks != 0);
  1087. debug("usb_read: end startblk " LBAF ", blccnt %x buffer %lx\n",
  1088. start, smallblks, buf_addr);
  1089. usb_lock_async(udev, 0);
  1090. usb_disable_asynch(0); /* asynch transfer allowed */
  1091. if (blkcnt >= ss->max_xfer_blk)
  1092. debug("\n");
  1093. return blkcnt;
  1094. }
  1095. #if CONFIG_IS_ENABLED(BLK)
  1096. static unsigned long usb_stor_write(struct udevice *dev, lbaint_t blknr,
  1097. lbaint_t blkcnt, const void *buffer)
  1098. #else
  1099. static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr,
  1100. lbaint_t blkcnt, const void *buffer)
  1101. #endif
  1102. {
  1103. lbaint_t start, blks;
  1104. uintptr_t buf_addr;
  1105. unsigned short smallblks;
  1106. struct usb_device *udev;
  1107. struct us_data *ss;
  1108. int retry;
  1109. struct scsi_cmd *srb = &usb_ccb;
  1110. #if CONFIG_IS_ENABLED(BLK)
  1111. struct blk_desc *block_dev;
  1112. #endif
  1113. if (blkcnt == 0)
  1114. return 0;
  1115. /* Setup device */
  1116. #if CONFIG_IS_ENABLED(BLK)
  1117. block_dev = dev_get_uclass_platdata(dev);
  1118. udev = dev_get_parent_priv(dev_get_parent(dev));
  1119. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1120. #else
  1121. debug("\nusb_read: udev %d\n", block_dev->devnum);
  1122. udev = usb_dev_desc[block_dev->devnum].priv;
  1123. if (!udev) {
  1124. debug("%s: No device\n", __func__);
  1125. return 0;
  1126. }
  1127. #endif
  1128. ss = (struct us_data *)udev->privptr;
  1129. usb_disable_asynch(1); /* asynch transfer not allowed */
  1130. usb_lock_async(udev, 1);
  1131. srb->lun = block_dev->lun;
  1132. buf_addr = (uintptr_t)buffer;
  1133. start = blknr;
  1134. blks = blkcnt;
  1135. debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
  1136. block_dev->devnum, start, blks, buf_addr);
  1137. do {
  1138. /* If write fails retry for max retry count else
  1139. * return with number of blocks written successfully.
  1140. */
  1141. retry = 2;
  1142. srb->pdata = (unsigned char *)buf_addr;
  1143. if (blks > ss->max_xfer_blk)
  1144. smallblks = ss->max_xfer_blk;
  1145. else
  1146. smallblks = (unsigned short) blks;
  1147. retry_it:
  1148. if (smallblks == ss->max_xfer_blk)
  1149. usb_show_progress();
  1150. srb->datalen = block_dev->blksz * smallblks;
  1151. srb->pdata = (unsigned char *)buf_addr;
  1152. if (usb_write_10(srb, ss, start, smallblks)) {
  1153. debug("Write ERROR\n");
  1154. ss->flags &= ~USB_READY;
  1155. usb_request_sense(srb, ss);
  1156. if (retry--)
  1157. goto retry_it;
  1158. blkcnt -= blks;
  1159. break;
  1160. }
  1161. start += smallblks;
  1162. blks -= smallblks;
  1163. buf_addr += srb->datalen;
  1164. } while (blks != 0);
  1165. debug("usb_write: end startblk " LBAF ", blccnt %x buffer %lx\n",
  1166. start, smallblks, buf_addr);
  1167. usb_lock_async(udev, 0);
  1168. usb_disable_asynch(0); /* asynch transfer allowed */
  1169. if (blkcnt >= ss->max_xfer_blk)
  1170. debug("\n");
  1171. return blkcnt;
  1172. }
  1173. /* Probe to see if a new device is actually a Storage device */
  1174. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  1175. struct us_data *ss)
  1176. {
  1177. struct usb_interface *iface;
  1178. int i;
  1179. struct usb_endpoint_descriptor *ep_desc;
  1180. unsigned int flags = 0;
  1181. /* let's examine the device now */
  1182. iface = &dev->config.if_desc[ifnum];
  1183. if (dev->descriptor.bDeviceClass != 0 ||
  1184. iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  1185. iface->desc.bInterfaceSubClass < US_SC_MIN ||
  1186. iface->desc.bInterfaceSubClass > US_SC_MAX) {
  1187. debug("Not mass storage\n");
  1188. /* if it's not a mass storage, we go no further */
  1189. return 0;
  1190. }
  1191. memset(ss, 0, sizeof(struct us_data));
  1192. /* At this point, we know we've got a live one */
  1193. debug("\n\nUSB Mass Storage device detected\n");
  1194. /* Initialize the us_data structure with some useful info */
  1195. ss->flags = flags;
  1196. ss->ifnum = ifnum;
  1197. ss->pusb_dev = dev;
  1198. ss->attention_done = 0;
  1199. ss->subclass = iface->desc.bInterfaceSubClass;
  1200. ss->protocol = iface->desc.bInterfaceProtocol;
  1201. /* set the handler pointers based on the protocol */
  1202. debug("Transport: ");
  1203. switch (ss->protocol) {
  1204. case US_PR_CB:
  1205. debug("Control/Bulk\n");
  1206. ss->transport = usb_stor_CB_transport;
  1207. ss->transport_reset = usb_stor_CB_reset;
  1208. break;
  1209. case US_PR_CBI:
  1210. debug("Control/Bulk/Interrupt\n");
  1211. ss->transport = usb_stor_CB_transport;
  1212. ss->transport_reset = usb_stor_CB_reset;
  1213. break;
  1214. case US_PR_BULK:
  1215. debug("Bulk/Bulk/Bulk\n");
  1216. ss->transport = usb_stor_BBB_transport;
  1217. ss->transport_reset = usb_stor_BBB_reset;
  1218. break;
  1219. default:
  1220. printf("USB Storage Transport unknown / not yet implemented\n");
  1221. return 0;
  1222. break;
  1223. }
  1224. /*
  1225. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1226. * An optional interrupt is OK (necessary for CBI protocol).
  1227. * We will ignore any others.
  1228. */
  1229. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1230. ep_desc = &iface->ep_desc[i];
  1231. /* is it an BULK endpoint? */
  1232. if ((ep_desc->bmAttributes &
  1233. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  1234. if (ep_desc->bEndpointAddress & USB_DIR_IN)
  1235. ss->ep_in = ep_desc->bEndpointAddress &
  1236. USB_ENDPOINT_NUMBER_MASK;
  1237. else
  1238. ss->ep_out =
  1239. ep_desc->bEndpointAddress &
  1240. USB_ENDPOINT_NUMBER_MASK;
  1241. }
  1242. /* is it an interrupt endpoint? */
  1243. if ((ep_desc->bmAttributes &
  1244. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  1245. ss->ep_int = ep_desc->bEndpointAddress &
  1246. USB_ENDPOINT_NUMBER_MASK;
  1247. ss->irqinterval = ep_desc->bInterval;
  1248. }
  1249. }
  1250. debug("Endpoints In %d Out %d Int %d\n",
  1251. ss->ep_in, ss->ep_out, ss->ep_int);
  1252. /* Do some basic sanity checks, and bail if we find a problem */
  1253. if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
  1254. !ss->ep_in || !ss->ep_out ||
  1255. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  1256. debug("Problems with device\n");
  1257. return 0;
  1258. }
  1259. /* set class specific stuff */
  1260. /* We only handle certain protocols. Currently, these are
  1261. * the only ones.
  1262. * The SFF8070 accepts the requests used in u-boot
  1263. */
  1264. if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
  1265. ss->subclass != US_SC_8070) {
  1266. printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
  1267. return 0;
  1268. }
  1269. if (ss->ep_int) {
  1270. /* we had found an interrupt endpoint, prepare irq pipe
  1271. * set up the IRQ pipe and handler
  1272. */
  1273. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  1274. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  1275. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  1276. dev->irq_handle = usb_stor_irq;
  1277. }
  1278. /* Set the maximum transfer size per host controller setting */
  1279. usb_stor_set_max_xfer_blk(dev, ss);
  1280. dev->privptr = (void *)ss;
  1281. return 1;
  1282. }
  1283. int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
  1284. struct blk_desc *dev_desc)
  1285. {
  1286. unsigned char perq, modi;
  1287. ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
  1288. ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
  1289. u32 capacity, blksz;
  1290. struct scsi_cmd *pccb = &usb_ccb;
  1291. pccb->pdata = usb_stor_buf;
  1292. dev_desc->target = dev->devnum;
  1293. pccb->lun = dev_desc->lun;
  1294. debug(" address %d\n", dev_desc->target);
  1295. if (usb_inquiry(pccb, ss)) {
  1296. debug("%s: usb_inquiry() failed\n", __func__);
  1297. return -1;
  1298. }
  1299. perq = usb_stor_buf[0];
  1300. modi = usb_stor_buf[1];
  1301. /*
  1302. * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
  1303. * they would not respond to test_unit_ready .
  1304. */
  1305. if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
  1306. debug("%s: unknown/unsupported device\n", __func__);
  1307. return 0;
  1308. }
  1309. if ((modi&0x80) == 0x80) {
  1310. /* drive is removable */
  1311. dev_desc->removable = 1;
  1312. }
  1313. memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
  1314. memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
  1315. memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
  1316. dev_desc->vendor[8] = 0;
  1317. dev_desc->product[16] = 0;
  1318. dev_desc->revision[4] = 0;
  1319. #ifdef CONFIG_USB_BIN_FIXUP
  1320. usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
  1321. (uchar *)dev_desc->product);
  1322. #endif /* CONFIG_USB_BIN_FIXUP */
  1323. debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
  1324. usb_stor_buf[3]);
  1325. if (usb_test_unit_ready(pccb, ss)) {
  1326. printf("Device NOT ready\n"
  1327. " Request Sense returned %02X %02X %02X\n",
  1328. pccb->sense_buf[2], pccb->sense_buf[12],
  1329. pccb->sense_buf[13]);
  1330. if (dev_desc->removable == 1)
  1331. dev_desc->type = perq;
  1332. return 0;
  1333. }
  1334. pccb->pdata = (unsigned char *)cap;
  1335. memset(pccb->pdata, 0, 8);
  1336. if (usb_read_capacity(pccb, ss) != 0) {
  1337. printf("READ_CAP ERROR\n");
  1338. ss->flags &= ~USB_READY;
  1339. cap[0] = 2880;
  1340. cap[1] = 0x200;
  1341. }
  1342. debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
  1343. #if 0
  1344. if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
  1345. cap[0] >>= 16;
  1346. cap[0] = cpu_to_be32(cap[0]);
  1347. cap[1] = cpu_to_be32(cap[1]);
  1348. #endif
  1349. capacity = be32_to_cpu(cap[0]) + 1;
  1350. blksz = be32_to_cpu(cap[1]);
  1351. debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz);
  1352. dev_desc->lba = capacity;
  1353. dev_desc->blksz = blksz;
  1354. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  1355. dev_desc->type = perq;
  1356. debug(" address %d\n", dev_desc->target);
  1357. return 1;
  1358. }
  1359. #if CONFIG_IS_ENABLED(DM_USB)
  1360. static int usb_mass_storage_probe(struct udevice *dev)
  1361. {
  1362. struct usb_device *udev = dev_get_parent_priv(dev);
  1363. int ret;
  1364. usb_disable_asynch(1); /* asynch transfer not allowed */
  1365. ret = usb_stor_probe_device(udev);
  1366. usb_disable_asynch(0); /* asynch transfer allowed */
  1367. return ret;
  1368. }
  1369. static const struct udevice_id usb_mass_storage_ids[] = {
  1370. { .compatible = "usb-mass-storage" },
  1371. { }
  1372. };
  1373. U_BOOT_DRIVER(usb_mass_storage) = {
  1374. .name = "usb_mass_storage",
  1375. .id = UCLASS_MASS_STORAGE,
  1376. .of_match = usb_mass_storage_ids,
  1377. .probe = usb_mass_storage_probe,
  1378. #if CONFIG_IS_ENABLED(BLK)
  1379. .platdata_auto_alloc_size = sizeof(struct us_data),
  1380. #endif
  1381. };
  1382. UCLASS_DRIVER(usb_mass_storage) = {
  1383. .id = UCLASS_MASS_STORAGE,
  1384. .name = "usb_mass_storage",
  1385. };
  1386. static const struct usb_device_id mass_storage_id_table[] = {
  1387. {
  1388. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1389. .bInterfaceClass = USB_CLASS_MASS_STORAGE
  1390. },
  1391. { } /* Terminating entry */
  1392. };
  1393. U_BOOT_USB_DEVICE(usb_mass_storage, mass_storage_id_table);
  1394. #endif
  1395. #if CONFIG_IS_ENABLED(BLK)
  1396. static const struct blk_ops usb_storage_ops = {
  1397. .read = usb_stor_read,
  1398. .write = usb_stor_write,
  1399. };
  1400. U_BOOT_DRIVER(usb_storage_blk) = {
  1401. .name = "usb_storage_blk",
  1402. .id = UCLASS_BLK,
  1403. .ops = &usb_storage_ops,
  1404. };
  1405. #else
  1406. U_BOOT_LEGACY_BLK(usb) = {
  1407. .if_typename = "usb",
  1408. .if_type = IF_TYPE_USB,
  1409. .max_devs = USB_MAX_STOR_DEV,
  1410. .desc = usb_dev_desc,
  1411. };
  1412. #endif