usb_storage.c 43 KB

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