usb_storage.c 34 KB

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