usb_storage.c 43 KB

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