usb_storage.c 36 KB

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