usb_storage.c 35 KB

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