usb_storage.c 40 KB

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