usb_storage.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr,
  107. lbaint_t blkcnt, void *buffer);
  108. static unsigned long usb_stor_write(block_dev_desc_t *block_dev, 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. static unsigned long usb_stor_read(block_dev_desc_t *block_dev, lbaint_t blknr,
  931. lbaint_t blkcnt, void *buffer)
  932. {
  933. int device = block_dev->dev;
  934. lbaint_t start, blks;
  935. uintptr_t buf_addr;
  936. unsigned short smallblks;
  937. struct usb_device *dev;
  938. struct us_data *ss;
  939. int retry;
  940. ccb *srb = &usb_ccb;
  941. if (blkcnt == 0)
  942. return 0;
  943. device &= 0xff;
  944. /* Setup device */
  945. debug("\nusb_read: dev %d\n", device);
  946. dev = usb_dev_desc[device].priv;
  947. if (!dev) {
  948. debug("%s: No device\n", __func__);
  949. return 0;
  950. }
  951. ss = (struct us_data *)dev->privptr;
  952. usb_disable_asynch(1); /* asynch transfer not allowed */
  953. srb->lun = usb_dev_desc[device].lun;
  954. buf_addr = (uintptr_t)buffer;
  955. start = blknr;
  956. blks = blkcnt;
  957. debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF
  958. " buffer %" PRIxPTR "\n", device, start, blks, buf_addr);
  959. do {
  960. /* XXX need some comment here */
  961. retry = 2;
  962. srb->pdata = (unsigned char *)buf_addr;
  963. if (blks > USB_MAX_XFER_BLK)
  964. smallblks = USB_MAX_XFER_BLK;
  965. else
  966. smallblks = (unsigned short) blks;
  967. retry_it:
  968. if (smallblks == USB_MAX_XFER_BLK)
  969. usb_show_progress();
  970. srb->datalen = usb_dev_desc[device].blksz * smallblks;
  971. srb->pdata = (unsigned char *)buf_addr;
  972. if (usb_read_10(srb, ss, start, smallblks)) {
  973. debug("Read ERROR\n");
  974. usb_request_sense(srb, ss);
  975. if (retry--)
  976. goto retry_it;
  977. blkcnt -= blks;
  978. break;
  979. }
  980. start += smallblks;
  981. blks -= smallblks;
  982. buf_addr += srb->datalen;
  983. } while (blks != 0);
  984. ss->flags &= ~USB_READY;
  985. debug("usb_read: end startblk " LBAF
  986. ", blccnt %x buffer %" PRIxPTR "\n",
  987. start, smallblks, buf_addr);
  988. usb_disable_asynch(0); /* asynch transfer allowed */
  989. if (blkcnt >= USB_MAX_XFER_BLK)
  990. debug("\n");
  991. return blkcnt;
  992. }
  993. static unsigned long usb_stor_write(block_dev_desc_t *block_dev, lbaint_t blknr,
  994. lbaint_t blkcnt, const void *buffer)
  995. {
  996. int device = block_dev->dev;
  997. lbaint_t start, blks;
  998. uintptr_t buf_addr;
  999. unsigned short smallblks;
  1000. struct usb_device *dev;
  1001. struct us_data *ss;
  1002. int retry;
  1003. ccb *srb = &usb_ccb;
  1004. if (blkcnt == 0)
  1005. return 0;
  1006. device &= 0xff;
  1007. /* Setup device */
  1008. debug("\nusb_write: dev %d\n", device);
  1009. dev = usb_dev_desc[device].priv;
  1010. if (!dev)
  1011. return 0;
  1012. ss = (struct us_data *)dev->privptr;
  1013. usb_disable_asynch(1); /* asynch transfer not allowed */
  1014. srb->lun = usb_dev_desc[device].lun;
  1015. buf_addr = (uintptr_t)buffer;
  1016. start = blknr;
  1017. blks = blkcnt;
  1018. debug("\nusb_write: dev %d startblk " LBAF ", blccnt " LBAF
  1019. " buffer %" PRIxPTR "\n", device, start, blks, buf_addr);
  1020. do {
  1021. /* If write fails retry for max retry count else
  1022. * return with number of blocks written successfully.
  1023. */
  1024. retry = 2;
  1025. srb->pdata = (unsigned char *)buf_addr;
  1026. if (blks > USB_MAX_XFER_BLK)
  1027. smallblks = USB_MAX_XFER_BLK;
  1028. else
  1029. smallblks = (unsigned short) blks;
  1030. retry_it:
  1031. if (smallblks == USB_MAX_XFER_BLK)
  1032. usb_show_progress();
  1033. srb->datalen = usb_dev_desc[device].blksz * smallblks;
  1034. srb->pdata = (unsigned char *)buf_addr;
  1035. if (usb_write_10(srb, ss, start, smallblks)) {
  1036. debug("Write ERROR\n");
  1037. usb_request_sense(srb, ss);
  1038. if (retry--)
  1039. goto retry_it;
  1040. blkcnt -= blks;
  1041. break;
  1042. }
  1043. start += smallblks;
  1044. blks -= smallblks;
  1045. buf_addr += srb->datalen;
  1046. } while (blks != 0);
  1047. ss->flags &= ~USB_READY;
  1048. debug("usb_write: end startblk " LBAF ", blccnt %x buffer %"
  1049. PRIxPTR "\n", start, smallblks, buf_addr);
  1050. usb_disable_asynch(0); /* asynch transfer allowed */
  1051. if (blkcnt >= USB_MAX_XFER_BLK)
  1052. debug("\n");
  1053. return blkcnt;
  1054. }
  1055. /* Probe to see if a new device is actually a Storage device */
  1056. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  1057. struct us_data *ss)
  1058. {
  1059. struct usb_interface *iface;
  1060. int i;
  1061. struct usb_endpoint_descriptor *ep_desc;
  1062. unsigned int flags = 0;
  1063. /* let's examine the device now */
  1064. iface = &dev->config.if_desc[ifnum];
  1065. if (dev->descriptor.bDeviceClass != 0 ||
  1066. iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  1067. iface->desc.bInterfaceSubClass < US_SC_MIN ||
  1068. iface->desc.bInterfaceSubClass > US_SC_MAX) {
  1069. debug("Not mass storage\n");
  1070. /* if it's not a mass storage, we go no further */
  1071. return 0;
  1072. }
  1073. memset(ss, 0, sizeof(struct us_data));
  1074. /* At this point, we know we've got a live one */
  1075. debug("\n\nUSB Mass Storage device detected\n");
  1076. /* Initialize the us_data structure with some useful info */
  1077. ss->flags = flags;
  1078. ss->ifnum = ifnum;
  1079. ss->pusb_dev = dev;
  1080. ss->attention_done = 0;
  1081. ss->subclass = iface->desc.bInterfaceSubClass;
  1082. ss->protocol = iface->desc.bInterfaceProtocol;
  1083. /* set the handler pointers based on the protocol */
  1084. debug("Transport: ");
  1085. switch (ss->protocol) {
  1086. case US_PR_CB:
  1087. debug("Control/Bulk\n");
  1088. ss->transport = usb_stor_CB_transport;
  1089. ss->transport_reset = usb_stor_CB_reset;
  1090. break;
  1091. case US_PR_CBI:
  1092. debug("Control/Bulk/Interrupt\n");
  1093. ss->transport = usb_stor_CB_transport;
  1094. ss->transport_reset = usb_stor_CB_reset;
  1095. break;
  1096. case US_PR_BULK:
  1097. debug("Bulk/Bulk/Bulk\n");
  1098. ss->transport = usb_stor_BBB_transport;
  1099. ss->transport_reset = usb_stor_BBB_reset;
  1100. break;
  1101. default:
  1102. printf("USB Storage Transport unknown / not yet implemented\n");
  1103. return 0;
  1104. break;
  1105. }
  1106. /*
  1107. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  1108. * An optional interrupt is OK (necessary for CBI protocol).
  1109. * We will ignore any others.
  1110. */
  1111. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  1112. ep_desc = &iface->ep_desc[i];
  1113. /* is it an BULK endpoint? */
  1114. if ((ep_desc->bmAttributes &
  1115. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  1116. if (ep_desc->bEndpointAddress & USB_DIR_IN)
  1117. ss->ep_in = ep_desc->bEndpointAddress &
  1118. USB_ENDPOINT_NUMBER_MASK;
  1119. else
  1120. ss->ep_out =
  1121. ep_desc->bEndpointAddress &
  1122. USB_ENDPOINT_NUMBER_MASK;
  1123. }
  1124. /* is it an interrupt endpoint? */
  1125. if ((ep_desc->bmAttributes &
  1126. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  1127. ss->ep_int = ep_desc->bEndpointAddress &
  1128. USB_ENDPOINT_NUMBER_MASK;
  1129. ss->irqinterval = ep_desc->bInterval;
  1130. }
  1131. }
  1132. debug("Endpoints In %d Out %d Int %d\n",
  1133. ss->ep_in, ss->ep_out, ss->ep_int);
  1134. /* Do some basic sanity checks, and bail if we find a problem */
  1135. if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
  1136. !ss->ep_in || !ss->ep_out ||
  1137. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  1138. debug("Problems with device\n");
  1139. return 0;
  1140. }
  1141. /* set class specific stuff */
  1142. /* We only handle certain protocols. Currently, these are
  1143. * the only ones.
  1144. * The SFF8070 accepts the requests used in u-boot
  1145. */
  1146. if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
  1147. ss->subclass != US_SC_8070) {
  1148. printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
  1149. return 0;
  1150. }
  1151. if (ss->ep_int) {
  1152. /* we had found an interrupt endpoint, prepare irq pipe
  1153. * set up the IRQ pipe and handler
  1154. */
  1155. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  1156. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  1157. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  1158. dev->irq_handle = usb_stor_irq;
  1159. }
  1160. dev->privptr = (void *)ss;
  1161. return 1;
  1162. }
  1163. int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
  1164. block_dev_desc_t *dev_desc)
  1165. {
  1166. unsigned char perq, modi;
  1167. ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
  1168. ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
  1169. u32 capacity, blksz;
  1170. ccb *pccb = &usb_ccb;
  1171. pccb->pdata = usb_stor_buf;
  1172. dev_desc->target = dev->devnum;
  1173. pccb->lun = dev_desc->lun;
  1174. debug(" address %d\n", dev_desc->target);
  1175. if (usb_inquiry(pccb, ss)) {
  1176. debug("%s: usb_inquiry() failed\n", __func__);
  1177. return -1;
  1178. }
  1179. perq = usb_stor_buf[0];
  1180. modi = usb_stor_buf[1];
  1181. /*
  1182. * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
  1183. * they would not respond to test_unit_ready .
  1184. */
  1185. if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
  1186. debug("%s: unknown/unsupported device\n", __func__);
  1187. return 0;
  1188. }
  1189. if ((modi&0x80) == 0x80) {
  1190. /* drive is removable */
  1191. dev_desc->removable = 1;
  1192. }
  1193. memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
  1194. memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
  1195. memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
  1196. dev_desc->vendor[8] = 0;
  1197. dev_desc->product[16] = 0;
  1198. dev_desc->revision[4] = 0;
  1199. #ifdef CONFIG_USB_BIN_FIXUP
  1200. usb_bin_fixup(dev->descriptor, (uchar *)dev_desc->vendor,
  1201. (uchar *)dev_desc->product);
  1202. #endif /* CONFIG_USB_BIN_FIXUP */
  1203. debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
  1204. usb_stor_buf[3]);
  1205. if (usb_test_unit_ready(pccb, ss)) {
  1206. printf("Device NOT ready\n"
  1207. " Request Sense returned %02X %02X %02X\n",
  1208. pccb->sense_buf[2], pccb->sense_buf[12],
  1209. pccb->sense_buf[13]);
  1210. if (dev_desc->removable == 1) {
  1211. dev_desc->type = perq;
  1212. return 1;
  1213. }
  1214. return 0;
  1215. }
  1216. pccb->pdata = (unsigned char *)cap;
  1217. memset(pccb->pdata, 0, 8);
  1218. if (usb_read_capacity(pccb, ss) != 0) {
  1219. printf("READ_CAP ERROR\n");
  1220. cap[0] = 2880;
  1221. cap[1] = 0x200;
  1222. }
  1223. ss->flags &= ~USB_READY;
  1224. debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
  1225. #if 0
  1226. if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
  1227. cap[0] >>= 16;
  1228. cap[0] = cpu_to_be32(cap[0]);
  1229. cap[1] = cpu_to_be32(cap[1]);
  1230. #endif
  1231. capacity = be32_to_cpu(cap[0]) + 1;
  1232. blksz = be32_to_cpu(cap[1]);
  1233. debug("Capacity = 0x%08x, blocksz = 0x%08x\n", capacity, blksz);
  1234. dev_desc->lba = capacity;
  1235. dev_desc->blksz = blksz;
  1236. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  1237. dev_desc->type = perq;
  1238. debug(" address %d\n", dev_desc->target);
  1239. debug("partype: %d\n", dev_desc->part_type);
  1240. init_part(dev_desc);
  1241. debug("partype: %d\n", dev_desc->part_type);
  1242. return 1;
  1243. }
  1244. #ifdef CONFIG_DM_USB
  1245. static int usb_mass_storage_probe(struct udevice *dev)
  1246. {
  1247. struct usb_device *udev = dev_get_parent_priv(dev);
  1248. int ret;
  1249. usb_disable_asynch(1); /* asynch transfer not allowed */
  1250. ret = usb_stor_probe_device(udev);
  1251. usb_disable_asynch(0); /* asynch transfer allowed */
  1252. return ret;
  1253. }
  1254. static const struct udevice_id usb_mass_storage_ids[] = {
  1255. { .compatible = "usb-mass-storage" },
  1256. { }
  1257. };
  1258. U_BOOT_DRIVER(usb_mass_storage) = {
  1259. .name = "usb_mass_storage",
  1260. .id = UCLASS_MASS_STORAGE,
  1261. .of_match = usb_mass_storage_ids,
  1262. .probe = usb_mass_storage_probe,
  1263. };
  1264. UCLASS_DRIVER(usb_mass_storage) = {
  1265. .id = UCLASS_MASS_STORAGE,
  1266. .name = "usb_mass_storage",
  1267. };
  1268. static const struct usb_device_id mass_storage_id_table[] = {
  1269. {
  1270. .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
  1271. .bInterfaceClass = USB_CLASS_MASS_STORAGE
  1272. },
  1273. { } /* Terminating entry */
  1274. };
  1275. U_BOOT_USB_DEVICE(usb_mass_storage, mass_storage_id_table);
  1276. #endif