usb_storage.c 39 KB

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