usb_storage.c 35 KB

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