usb_storage.c 39 KB

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