usb_storage.c 41 KB

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