usb_storage.c 38 KB

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