usb_storage.c 42 KB

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