usb_storage.c 42 KB

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