usb_storage.c 36 KB

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