usb_storage.c 42 KB

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