usb_storage.c 39 KB

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