usb_storage.c 42 KB

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