usb_storage.c 39 KB

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