Firmware for MNT ZZ9000 graphics and ARM coprocessor card for Amiga computers.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_storage.c 39KB

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