usb_storage.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * (C) Copyright 2001
  3. * Denis Peter, MPL AG Switzerland
  4. *
  5. * Most of this source has been derived from the Linux USB
  6. * project.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. *
  26. */
  27. /* Note:
  28. * Currently only the CBI transport protocoll has been implemented, and it
  29. * is only tested with a TEAC USB Floppy. Other Massstorages with CBI or CB
  30. * transport protocoll may work as well.
  31. */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <asm/processor.h>
  35. #if (CONFIG_COMMANDS & CFG_CMD_USB)
  36. #include <usb.h>
  37. #ifdef CONFIG_USB_STORAGE
  38. #undef USB_STOR_DEBUG
  39. #ifdef USB_STOR_DEBUG
  40. #define USB_STOR_PRINTF(fmt,args...) printf (fmt ,##args)
  41. #else
  42. #define USB_STOR_PRINTF(fmt,args...)
  43. #endif
  44. #include <scsi.h>
  45. /* direction table -- this indicates the direction of the data
  46. * transfer for each command code -- a 1 indicates input
  47. */
  48. unsigned char us_direction[256/8] = {
  49. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  50. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  53. };
  54. #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
  55. static unsigned char usb_stor_buf[512];
  56. static ccb usb_ccb;
  57. /*
  58. * CBI style
  59. */
  60. #define US_CBI_ADSC 0
  61. #define USB_MAX_STOR_DEV 5
  62. static int usb_max_devs; /* number of highest available usb device */
  63. static block_dev_desc_t usb_dev_desc[USB_MAX_STOR_DEV];
  64. struct us_data;
  65. typedef int (*trans_cmnd)(ccb*, struct us_data*);
  66. typedef int (*trans_reset)(struct us_data*);
  67. struct us_data {
  68. struct usb_device *pusb_dev; /* this usb_device */
  69. unsigned int flags; /* from filter initially */
  70. unsigned char ifnum; /* interface number */
  71. unsigned char ep_in; /* in endpoint */
  72. unsigned char ep_out; /* out ....... */
  73. unsigned char ep_int; /* interrupt . */
  74. unsigned char subclass; /* as in overview */
  75. unsigned char protocol; /* .............. */
  76. unsigned char attention_done; /* force attn on first cmd */
  77. unsigned short ip_data; /* interrupt data */
  78. int action; /* what to do */
  79. int ip_wanted; /* needed */
  80. int *irq_handle; /* for USB int requests */
  81. unsigned int irqpipe; /* pipe for release_irq */
  82. unsigned char irqmaxp; /* max packed for irq Pipe */
  83. unsigned char irqinterval; /* Intervall for IRQ Pipe */
  84. ccb *srb; /* current srb */
  85. trans_reset transport_reset; /* reset routine */
  86. trans_cmnd transport; /* transport routine */
  87. };
  88. static struct us_data usb_stor[USB_MAX_STOR_DEV];
  89. #define USB_STOR_TRANSPORT_GOOD 0
  90. #define USB_STOR_TRANSPORT_FAILED -1
  91. #define USB_STOR_TRANSPORT_ERROR -2
  92. int usb_stor_get_info(struct usb_device *dev, struct us_data *us, block_dev_desc_t *dev_desc);
  93. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss);
  94. unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer);
  95. struct usb_device * usb_get_dev_index(int index);
  96. void uhci_show_temp_int_td(void);
  97. block_dev_desc_t *usb_stor_get_dev(int index)
  98. {
  99. return &usb_dev_desc[index];
  100. }
  101. void usb_show_progress(void)
  102. {
  103. printf(".");
  104. }
  105. /*********************************************************************************
  106. * (re)-scan the usb and reports device info
  107. * to the user if mode = 1
  108. * returns current device or -1 if no
  109. */
  110. int usb_stor_scan(int mode)
  111. {
  112. unsigned char i;
  113. struct usb_device *dev;
  114. if(mode==1) {
  115. printf("scanning bus for storage devices...\n");
  116. }
  117. usb_disable_asynch(1); /* asynch transfer not allowed */
  118. for(i=0;i<USB_MAX_STOR_DEV;i++) {
  119. memset(&usb_dev_desc[i],0,sizeof(block_dev_desc_t));
  120. usb_dev_desc[i].target=0xff;
  121. usb_dev_desc[i].if_type=IF_TYPE_USB;
  122. usb_dev_desc[i].dev=i;
  123. usb_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
  124. usb_dev_desc[i].block_read=usb_stor_read;
  125. }
  126. usb_max_devs=0;
  127. for(i=0;i<USB_MAX_DEVICE;i++) {
  128. dev=usb_get_dev_index(i); /* get device */
  129. USB_STOR_PRINTF("i=%d\n",i);
  130. if(dev==NULL) {
  131. break; /* no more devices avaiable */
  132. }
  133. if(usb_storage_probe(dev,0,&usb_stor[usb_max_devs])) { /* ok, it is a storage devices */
  134. /* get info and fill it in */
  135. if(usb_stor_get_info(dev, &usb_stor[usb_max_devs], &usb_dev_desc[usb_max_devs])) {
  136. if(mode==1) {
  137. printf (" Device %d: ", usb_max_devs);
  138. dev_print(&usb_dev_desc[usb_max_devs]);
  139. } /* if mode */
  140. usb_max_devs++;
  141. } /* if get info ok */
  142. } /* if storage device */
  143. if(usb_max_devs==USB_MAX_STOR_DEV) {
  144. printf("max USB Storage Device reached: %d stopping\n",usb_max_devs);
  145. break;
  146. }
  147. } /* for */
  148. usb_disable_asynch(0); /* asynch transfer allowed */
  149. if(usb_max_devs>0)
  150. return 0;
  151. else
  152. return-1;
  153. }
  154. static int usb_stor_irq(struct usb_device *dev)
  155. {
  156. struct us_data *us;
  157. us=(struct us_data *)dev->privptr;
  158. if(us->ip_wanted) {
  159. us->ip_wanted=0;
  160. }
  161. return 0;
  162. }
  163. #ifdef USB_STOR_DEBUG
  164. static void usb_show_srb(ccb * pccb)
  165. {
  166. int i;
  167. printf("SRB: len %d datalen 0x%lX\n ",pccb->cmdlen,pccb->datalen);
  168. for(i=0;i<12;i++) {
  169. printf("%02X ",pccb->cmd[i]);
  170. }
  171. printf("\n");
  172. }
  173. static void display_int_status(unsigned long tmp)
  174. {
  175. printf("Status: %s %s %s %s %s %s %s\n",
  176. (tmp & USB_ST_ACTIVE) ? "Active" : "",
  177. (tmp & USB_ST_STALLED) ? "Stalled" : "",
  178. (tmp & USB_ST_BUF_ERR) ? "Buffer Error" : "",
  179. (tmp & USB_ST_BABBLE_DET) ? "Babble Det" : "",
  180. (tmp & USB_ST_NAK_REC) ? "NAKed" : "",
  181. (tmp & USB_ST_CRC_ERR) ? "CRC Error" : "",
  182. (tmp & USB_ST_BIT_ERR) ? "Bitstuff Error" : "");
  183. }
  184. #endif
  185. /***********************************************************************
  186. * Data transfer routines
  187. ***********************************************************************/
  188. static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
  189. {
  190. int max_size;
  191. int this_xfer;
  192. int result;
  193. int partial;
  194. int maxtry;
  195. int stat;
  196. /* determine the maximum packet size for these transfers */
  197. max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
  198. /* while we have data left to transfer */
  199. while (length) {
  200. /* calculate how long this will be -- maximum or a remainder */
  201. this_xfer = length > max_size ? max_size : length;
  202. length -= this_xfer;
  203. /* setup the retry counter */
  204. maxtry = 10;
  205. /* set up the transfer loop */
  206. do {
  207. /* transfer the data */
  208. USB_STOR_PRINTF("Bulk xfer 0x%x(%d) try #%d\n",
  209. (unsigned int)buf, this_xfer, 11 - maxtry);
  210. result = usb_bulk_msg(us->pusb_dev, pipe, buf,
  211. this_xfer, &partial, USB_CNTL_TIMEOUT*5);
  212. USB_STOR_PRINTF("bulk_msg returned %d xferred %d/%d\n",
  213. result, partial, this_xfer);
  214. if(us->pusb_dev->status!=0) {
  215. /* if we stall, we need to clear it before we go on */
  216. #ifdef USB_STOR_DEBUG
  217. display_int_status(us->pusb_dev->status);
  218. #endif
  219. if (us->pusb_dev->status & USB_ST_STALLED) {
  220. USB_STOR_PRINTF("stalled ->clearing endpoint halt for pipe 0x%x\n", pipe);
  221. stat = us->pusb_dev->status;
  222. usb_clear_halt(us->pusb_dev, pipe);
  223. us->pusb_dev->status=stat;
  224. if(this_xfer == partial) {
  225. USB_STOR_PRINTF("bulk transferred with error %X, but data ok\n",us->pusb_dev->status);
  226. return 0;
  227. }
  228. else
  229. return result;
  230. }
  231. if (us->pusb_dev->status & USB_ST_NAK_REC) {
  232. USB_STOR_PRINTF("Device NAKed bulk_msg\n");
  233. return result;
  234. }
  235. if(this_xfer == partial) {
  236. USB_STOR_PRINTF("bulk transferred with error %d, but data ok\n",us->pusb_dev->status);
  237. return 0;
  238. }
  239. /* if our try counter reaches 0, bail out */
  240. USB_STOR_PRINTF("bulk transferred with error %d, data %d\n",us->pusb_dev->status,partial);
  241. if (!maxtry--)
  242. return result;
  243. }
  244. /* update to show what data was transferred */
  245. this_xfer -= partial;
  246. buf += partial;
  247. /* continue until this transfer is done */
  248. } while ( this_xfer );
  249. }
  250. /* if we get here, we're done and successful */
  251. return 0;
  252. }
  253. /* FIXME: this reset function doesn't really reset the port, and it
  254. * should. Actually it should probably do what it's doing here, and
  255. * reset the port physically
  256. */
  257. static int usb_stor_CB_reset(struct us_data *us)
  258. {
  259. unsigned char cmd[12];
  260. int result;
  261. USB_STOR_PRINTF("CB_reset\n");
  262. memset(cmd, 0xFF, sizeof(cmd));
  263. cmd[0] = SCSI_SEND_DIAG;
  264. cmd[1] = 4;
  265. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
  266. US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  267. 0, us->ifnum, cmd, sizeof(cmd), USB_CNTL_TIMEOUT*5);
  268. /* long wait for reset */
  269. wait_ms(1500);
  270. USB_STOR_PRINTF("CB_reset result %d: status %X clearing endpoint halt\n",result,us->pusb_dev->status);
  271. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
  272. usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
  273. USB_STOR_PRINTF("CB_reset done\n");
  274. return 0;
  275. }
  276. /* FIXME: we also need a CBI_command which sets up the completion
  277. * interrupt, and waits for it
  278. */
  279. int usb_stor_CB_comdat(ccb *srb, struct us_data *us)
  280. {
  281. int result;
  282. int dir_in,retry;
  283. unsigned int pipe;
  284. unsigned long status;
  285. retry=5;
  286. dir_in=US_DIRECTION(srb->cmd[0]);
  287. if(dir_in)
  288. pipe=usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  289. else
  290. pipe=usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  291. while(retry--) {
  292. USB_STOR_PRINTF("CBI gets a command: Try %d\n",5-retry);
  293. #ifdef USB_STOR_DEBUG
  294. usb_show_srb(srb);
  295. #endif
  296. /* let's send the command via the control pipe */
  297. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0),
  298. US_CBI_ADSC, USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  299. 0, us->ifnum,
  300. srb->cmd, srb->cmdlen, USB_CNTL_TIMEOUT*5);
  301. USB_STOR_PRINTF("CB_transport: control msg returned %d, status %X\n",result,us->pusb_dev->status);
  302. /* check the return code for the command */
  303. if (result < 0) {
  304. if(us->pusb_dev->status & USB_ST_STALLED) {
  305. status=us->pusb_dev->status;
  306. USB_STOR_PRINTF(" stall during command found, clear pipe\n");
  307. usb_clear_halt(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev,0));
  308. us->pusb_dev->status=status;
  309. }
  310. USB_STOR_PRINTF(" error during command %02X Stat = %X\n",srb->cmd[0],us->pusb_dev->status);
  311. return result;
  312. }
  313. /* transfer the data payload for this command, if one exists*/
  314. USB_STOR_PRINTF("CB_transport: control msg returned %d, direction is %s to go 0x%lx\n",result,dir_in ? "IN" : "OUT",srb->datalen);
  315. if (srb->datalen) {
  316. result = us_one_transfer(us, pipe, srb->pdata,srb->datalen);
  317. 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);
  318. if(!(us->pusb_dev->status & USB_ST_NAK_REC))
  319. break;
  320. } /* if (srb->datalen) */
  321. else
  322. break;
  323. }
  324. /* return result */
  325. return result;
  326. }
  327. int usb_stor_CBI_get_status(ccb *srb, struct us_data *us)
  328. {
  329. int timeout;
  330. us->ip_wanted=1;
  331. submit_int_msg(us->pusb_dev,us->irqpipe,
  332. (void *)&us->ip_data,us->irqmaxp ,us->irqinterval);
  333. timeout=1000;
  334. while(timeout--) {
  335. if((volatile int *)us->ip_wanted==0)
  336. break;
  337. wait_ms(10);
  338. }
  339. if (us->ip_wanted) {
  340. printf(" Did not get interrupt on CBI\n");
  341. us->ip_wanted = 0;
  342. return USB_STOR_TRANSPORT_ERROR;
  343. }
  344. USB_STOR_PRINTF("Got interrupt data 0x%x, transfered %d status 0x%lX\n", us->ip_data,us->pusb_dev->irq_act_len,us->pusb_dev->irq_status);
  345. /* UFI gives us ASC and ASCQ, like a request sense */
  346. if (us->subclass == US_SC_UFI) {
  347. if (srb->cmd[0] == SCSI_REQ_SENSE ||
  348. srb->cmd[0] == SCSI_INQUIRY)
  349. return USB_STOR_TRANSPORT_GOOD; /* Good */
  350. else
  351. if (us->ip_data)
  352. return USB_STOR_TRANSPORT_FAILED;
  353. else
  354. return USB_STOR_TRANSPORT_GOOD;
  355. }
  356. /* otherwise, we interpret the data normally */
  357. switch (us->ip_data) {
  358. case 0x0001:
  359. return USB_STOR_TRANSPORT_GOOD;
  360. case 0x0002:
  361. return USB_STOR_TRANSPORT_FAILED;
  362. default:
  363. return USB_STOR_TRANSPORT_ERROR;
  364. } /* switch */
  365. return USB_STOR_TRANSPORT_ERROR;
  366. }
  367. #define USB_TRANSPORT_UNKNOWN_RETRY 5
  368. #define USB_TRANSPORT_NOT_READY_RETRY 10
  369. int usb_stor_CB_transport(ccb *srb, struct us_data *us)
  370. {
  371. int result,status;
  372. ccb *psrb;
  373. ccb reqsrb;
  374. int retry,notready;
  375. psrb=&reqsrb;
  376. status=USB_STOR_TRANSPORT_GOOD;
  377. retry=0;
  378. notready=0;
  379. /* issue the command */
  380. do_retry:
  381. result=usb_stor_CB_comdat(srb,us);
  382. USB_STOR_PRINTF("command / Data returned %d, status %X\n",result,us->pusb_dev->status);
  383. /* if this is an CBI Protocol, get IRQ */
  384. if(us->protocol==US_PR_CBI) {
  385. status=usb_stor_CBI_get_status(srb,us);
  386. /* if the status is error, report it */
  387. if(status==USB_STOR_TRANSPORT_ERROR) {
  388. USB_STOR_PRINTF(" USB CBI Command Error\n");
  389. return status;
  390. }
  391. srb->sense_buf[12]=(unsigned char)(us->ip_data>>8);
  392. srb->sense_buf[13]=(unsigned char)(us->ip_data&0xff);
  393. if(!us->ip_data) {
  394. /* if the status is good, report it */
  395. if(status==USB_STOR_TRANSPORT_GOOD) {
  396. USB_STOR_PRINTF(" USB CBI Command Good\n");
  397. return status;
  398. }
  399. }
  400. }
  401. /* do we have to issue an auto request? */
  402. /* HERE we have to check the result */
  403. if((result<0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
  404. USB_STOR_PRINTF("ERROR %X\n",us->pusb_dev->status);
  405. us->transport_reset(us);
  406. return USB_STOR_TRANSPORT_ERROR;
  407. }
  408. if((us->protocol==US_PR_CBI) &&
  409. ((srb->cmd[0]==SCSI_REQ_SENSE) ||
  410. (srb->cmd[0]==SCSI_INQUIRY))) { /* do not issue an autorequest after request sense */
  411. USB_STOR_PRINTF("No auto request and good\n");
  412. return USB_STOR_TRANSPORT_GOOD;
  413. }
  414. /* issue an request_sense */
  415. memset(&psrb->cmd[0],0,12);
  416. psrb->cmd[0]=SCSI_REQ_SENSE;
  417. psrb->cmd[1]=srb->lun<<5;
  418. psrb->cmd[4]=18;
  419. psrb->datalen=18;
  420. psrb->pdata=&srb->sense_buf[0];
  421. psrb->cmdlen=12;
  422. /* issue the command */
  423. result=usb_stor_CB_comdat(psrb,us);
  424. USB_STOR_PRINTF("auto request returned %d\n",result);
  425. /* if this is an CBI Protocol, get IRQ */
  426. if(us->protocol==US_PR_CBI) {
  427. status=usb_stor_CBI_get_status(psrb,us);
  428. }
  429. if((result<0)&&!(us->pusb_dev->status & USB_ST_STALLED)) {
  430. USB_STOR_PRINTF(" AUTO REQUEST ERROR %d\n",us->pusb_dev->status);
  431. return USB_STOR_TRANSPORT_ERROR;
  432. }
  433. 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]);
  434. /* Check the auto request result */
  435. if((srb->sense_buf[2]==0) &&
  436. (srb->sense_buf[12]==0) &&
  437. (srb->sense_buf[13]==0)) /* ok, no sense */
  438. return USB_STOR_TRANSPORT_GOOD;
  439. /* Check the auto request result */
  440. switch(srb->sense_buf[2]) {
  441. case 0x01: /* Recovered Error */
  442. return USB_STOR_TRANSPORT_GOOD;
  443. break;
  444. case 0x02: /* Not Ready */
  445. if(notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
  446. printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X (NOT READY)\n",
  447. srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
  448. return USB_STOR_TRANSPORT_FAILED;
  449. }
  450. else {
  451. wait_ms(100);
  452. goto do_retry;
  453. }
  454. break;
  455. default:
  456. if(retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
  457. printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
  458. srb->cmd[0],srb->sense_buf[0],srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
  459. return USB_STOR_TRANSPORT_FAILED;
  460. }
  461. else
  462. goto do_retry;
  463. break;
  464. }
  465. return USB_STOR_TRANSPORT_FAILED;
  466. }
  467. static int usb_inquiry(ccb *srb,struct us_data *ss)
  468. {
  469. int retry,i;
  470. retry=3;
  471. do {
  472. memset(&srb->cmd[0],0,12);
  473. srb->cmd[0]=SCSI_INQUIRY;
  474. srb->cmd[1]=srb->lun<<5;
  475. srb->cmd[4]=36;
  476. srb->datalen=36;
  477. srb->cmdlen=12;
  478. i=ss->transport(srb,ss);
  479. USB_STOR_PRINTF("inquiry returns %d\n",i);
  480. if(i==0)
  481. break;
  482. }while(retry--);
  483. if(!retry) {
  484. printf("error in inquiry\n");
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. static int usb_request_sense(ccb *srb,struct us_data *ss)
  490. {
  491. char *ptr;
  492. return 0;
  493. ptr=srb->pdata;
  494. memset(&srb->cmd[0],0,12);
  495. srb->cmd[0]=SCSI_REQ_SENSE;
  496. srb->cmd[1]=srb->lun<<5;
  497. srb->cmd[4]=18;
  498. srb->datalen=18;
  499. srb->pdata=&srb->sense_buf[0];
  500. srb->cmdlen=12;
  501. ss->transport(srb,ss);
  502. USB_STOR_PRINTF("Request Sense returned %02X %02X %02X\n",srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
  503. srb->pdata=ptr;
  504. return 0;
  505. }
  506. static int usb_test_unit_ready(ccb *srb,struct us_data *ss)
  507. {
  508. int retries=10;
  509. do {
  510. memset(&srb->cmd[0],0,12);
  511. srb->cmd[0]=SCSI_TST_U_RDY;
  512. srb->cmd[1]=srb->lun<<5;
  513. srb->datalen=0;
  514. srb->cmdlen=12;
  515. if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD)
  516. {
  517. return 0;
  518. }
  519. } while(retries--);
  520. return -1;
  521. }
  522. static int usb_read_capacity(ccb *srb,struct us_data *ss)
  523. {
  524. int retry;
  525. retry=2; /* retries */
  526. do {
  527. memset(&srb->cmd[0],0,12);
  528. srb->cmd[0]=SCSI_RD_CAPAC;
  529. srb->cmd[1]=srb->lun<<5;
  530. srb->datalen=8;
  531. srb->cmdlen=12;
  532. if(ss->transport(srb,ss)==USB_STOR_TRANSPORT_GOOD) {
  533. return 0;
  534. }
  535. }while(retry--);
  536. return -1;
  537. }
  538. static int usb_read_10(ccb *srb,struct us_data *ss, unsigned long start, unsigned short blocks)
  539. {
  540. memset(&srb->cmd[0],0,12);
  541. srb->cmd[0]=SCSI_READ10;
  542. srb->cmd[1]=srb->lun<<5;
  543. srb->cmd[2]=((unsigned char) (start>>24))&0xff;
  544. srb->cmd[3]=((unsigned char) (start>>16))&0xff;
  545. srb->cmd[4]=((unsigned char) (start>>8))&0xff;
  546. srb->cmd[5]=((unsigned char) (start))&0xff;
  547. srb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
  548. srb->cmd[8]=(unsigned char) blocks & 0xff;
  549. srb->cmdlen=12;
  550. USB_STOR_PRINTF("read10: start %lx blocks %x\n",start,blocks);
  551. return ss->transport(srb,ss);
  552. }
  553. #define USB_MAX_READ_BLK 20
  554. unsigned long usb_stor_read(int device, unsigned long blknr, unsigned long blkcnt, unsigned long *buffer)
  555. {
  556. unsigned long start,blks, buf_addr;
  557. unsigned short smallblks;
  558. struct usb_device *dev;
  559. int retry,i;
  560. ccb *srb=&usb_ccb;
  561. device&=0xff;
  562. /* Setup device
  563. */
  564. USB_STOR_PRINTF("\nusb_read: dev %d \n",device);
  565. dev=NULL;
  566. for(i=0;i<USB_MAX_DEVICE;i++) {
  567. dev=usb_get_dev_index(i);
  568. if(dev==NULL) {
  569. return 0;
  570. }
  571. if(dev->devnum==usb_dev_desc[device].target)
  572. break;
  573. }
  574. usb_disable_asynch(1); /* asynch transfer not allowed */
  575. srb->lun=usb_dev_desc[device].lun;
  576. buf_addr=(unsigned long)buffer;
  577. start=blknr;
  578. blks=blkcnt;
  579. if(usb_test_unit_ready(srb,(struct us_data *)dev->privptr)) {
  580. printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",
  581. srb->sense_buf[2],srb->sense_buf[12],srb->sense_buf[13]);
  582. return 0;
  583. }
  584. USB_STOR_PRINTF("\nusb_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks, buf_addr);
  585. do {
  586. retry=2;
  587. srb->pdata=(unsigned char *)buf_addr;
  588. if(blks>USB_MAX_READ_BLK) {
  589. smallblks=USB_MAX_READ_BLK;
  590. }
  591. else {
  592. smallblks=(unsigned short) blks;
  593. }
  594. retry_it:
  595. if(smallblks==USB_MAX_READ_BLK)
  596. usb_show_progress();
  597. srb->datalen=usb_dev_desc[device].blksz * smallblks;
  598. srb->pdata=(unsigned char *)buf_addr;
  599. if(usb_read_10(srb,(struct us_data *)dev->privptr, start, smallblks)) {
  600. USB_STOR_PRINTF("Read ERROR\n");
  601. usb_request_sense(srb,(struct us_data *)dev->privptr);
  602. if(retry--)
  603. goto retry_it;
  604. blkcnt-=blks;
  605. break;
  606. }
  607. start+=smallblks;
  608. blks-=smallblks;
  609. buf_addr+=srb->datalen;
  610. } while(blks!=0);
  611. USB_STOR_PRINTF("usb_read: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
  612. usb_disable_asynch(0); /* asynch transfer allowed */
  613. if(blkcnt>=USB_MAX_READ_BLK)
  614. printf("\n");
  615. return(blkcnt);
  616. }
  617. /* Probe to see if a new device is actually a Storage device */
  618. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,struct us_data *ss)
  619. {
  620. struct usb_interface_descriptor *iface;
  621. int i;
  622. unsigned int flags = 0;
  623. int protocol = 0;
  624. int subclass = 0;
  625. memset(ss, 0, sizeof(struct us_data));
  626. /* let's examine the device now */
  627. iface = &dev->config.if_desc[ifnum];
  628. #if 0
  629. /* this is the place to patch some storage devices */
  630. USB_STOR_PRINTF("iVendor %X iProduct %X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
  631. if ((dev->descriptor.idVendor) == 0x066b && (dev->descriptor.idProduct) == 0x0103) {
  632. USB_STOR_PRINTF("patched for E-USB\n");
  633. protocol = US_PR_CB;
  634. subclass = US_SC_UFI; /* an assumption */
  635. }
  636. #endif
  637. if (dev->descriptor.bDeviceClass != 0 ||
  638. iface->bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  639. iface->bInterfaceSubClass < US_SC_MIN ||
  640. iface->bInterfaceSubClass > US_SC_MAX) {
  641. /* if it's not a mass storage, we go no further */
  642. return 0;
  643. }
  644. /* At this point, we know we've got a live one */
  645. USB_STOR_PRINTF("\n\nUSB Mass Storage device detected\n");
  646. /* Initialize the us_data structure with some useful info */
  647. ss->flags = flags;
  648. ss->ifnum = ifnum;
  649. ss->pusb_dev = dev;
  650. ss->attention_done = 0;
  651. /* If the device has subclass and protocol, then use that. Otherwise,
  652. * take data from the specific interface.
  653. */
  654. if (subclass) {
  655. ss->subclass = subclass;
  656. ss->protocol = protocol;
  657. } else {
  658. ss->subclass = iface->bInterfaceSubClass;
  659. ss->protocol = iface->bInterfaceProtocol;
  660. }
  661. /* set the handler pointers based on the protocol */
  662. USB_STOR_PRINTF("Transport: ");
  663. switch (ss->protocol) {
  664. case US_PR_CB:
  665. USB_STOR_PRINTF("Control/Bulk\n");
  666. ss->transport = usb_stor_CB_transport;
  667. ss->transport_reset = usb_stor_CB_reset;
  668. break;
  669. case US_PR_CBI:
  670. USB_STOR_PRINTF("Control/Bulk/Interrupt\n");
  671. ss->transport = usb_stor_CB_transport;
  672. ss->transport_reset = usb_stor_CB_reset;
  673. break;
  674. default:
  675. printf("USB Starage Transport unknown / not yet implemented\n");
  676. return 0;
  677. break;
  678. }
  679. /*
  680. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  681. * An optional interrupt is OK (necessary for CBI protocol).
  682. * We will ignore any others.
  683. */
  684. for (i = 0; i < iface->bNumEndpoints; i++) {
  685. /* is it an BULK endpoint? */
  686. if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  687. == USB_ENDPOINT_XFER_BULK) {
  688. if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
  689. ss->ep_in = iface->ep_desc[i].bEndpointAddress &
  690. USB_ENDPOINT_NUMBER_MASK;
  691. else
  692. ss->ep_out = iface->ep_desc[i].bEndpointAddress &
  693. USB_ENDPOINT_NUMBER_MASK;
  694. }
  695. /* is it an interrupt endpoint? */
  696. if ((iface->ep_desc[i].bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  697. == USB_ENDPOINT_XFER_INT) {
  698. ss->ep_int = iface->ep_desc[i].bEndpointAddress &
  699. USB_ENDPOINT_NUMBER_MASK;
  700. ss->irqinterval = iface->ep_desc[i].bInterval;
  701. }
  702. }
  703. USB_STOR_PRINTF("Endpoints In %d Out %d Int %d\n",
  704. ss->ep_in, ss->ep_out, ss->ep_int);
  705. /* Do some basic sanity checks, and bail if we find a problem */
  706. if (usb_set_interface(dev, iface->bInterfaceNumber, 0) ||
  707. !ss->ep_in || !ss->ep_out ||
  708. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  709. USB_STOR_PRINTF("Problems with device\n");
  710. return 0;
  711. }
  712. /* set class specific stuff */
  713. /* We only handle certain protocols. Currently, this is
  714. * the only one.
  715. */
  716. if (ss->subclass != US_SC_UFI) {
  717. printf("Sorry, protocol %d not yet supported.\n",ss->subclass);
  718. return 0;
  719. }
  720. if(ss->ep_int) /* we had found an interrupt endpoint, prepare irq pipe */
  721. {
  722. /* set up the IRQ pipe and handler */
  723. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  724. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  725. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  726. dev->irq_handle=usb_stor_irq;
  727. dev->privptr=(void *)ss;
  728. }
  729. return 1;
  730. }
  731. int usb_stor_get_info(struct usb_device *dev,struct us_data *ss,block_dev_desc_t *dev_desc)
  732. {
  733. unsigned char perq,modi;
  734. unsigned long cap[2];
  735. unsigned long *capacity,*blksz;
  736. ccb *pccb=&usb_ccb;
  737. ss->transport_reset(ss);
  738. pccb->pdata=usb_stor_buf;
  739. dev_desc->target=dev->devnum;
  740. pccb->lun=dev_desc->lun;
  741. USB_STOR_PRINTF(" address %d\n",dev_desc->target);
  742. if(usb_inquiry(pccb,ss))
  743. return -1;
  744. perq=usb_stor_buf[0];
  745. modi=usb_stor_buf[1];
  746. if((perq & 0x1f)==0x1f) {
  747. return 0; /* skip unknown devices */
  748. }
  749. if((modi&0x80)==0x80) {/* drive is removable */
  750. dev_desc->removable=1;
  751. }
  752. memcpy(&dev_desc->vendor[0], &usb_stor_buf[8], 8);
  753. memcpy(&dev_desc->product[0], &usb_stor_buf[16], 16);
  754. memcpy(&dev_desc->revision[0], &usb_stor_buf[32], 4);
  755. dev_desc->vendor[8]=0;
  756. dev_desc->product[16]=0;
  757. dev_desc->revision[4]=0;
  758. USB_STOR_PRINTF("ISO Vers %X, Response Data %X\n",usb_stor_buf[2],usb_stor_buf[3]);
  759. if(usb_test_unit_ready(pccb,ss)) {
  760. printf("Device NOT ready\n Request Sense returned %02X %02X %02X\n",pccb->sense_buf[2],pccb->sense_buf[12],pccb->sense_buf[13]);
  761. if(dev_desc->removable==1) {
  762. dev_desc->type=perq;
  763. return 1;
  764. }
  765. else
  766. return 0;
  767. }
  768. pccb->pdata=(unsigned char *)&cap[0];
  769. memset(pccb->pdata,0,8);
  770. if(usb_read_capacity(pccb,ss)!=0) {
  771. printf("READ_CAP ERROR\n");
  772. cap[0]=2880;
  773. cap[1]=0x200;
  774. }
  775. USB_STOR_PRINTF("Read Capacity returns: 0x%lx, 0x%lx\n",cap[0],cap[1]);
  776. #if 0
  777. if(cap[0]>(0x200000 * 10)) /* greater than 10 GByte */
  778. cap[0]>>=16;
  779. #endif
  780. cap[0]+=1;
  781. capacity=&cap[0];
  782. blksz=&cap[1];
  783. USB_STOR_PRINTF("Capacity = 0x%lx, blocksz = 0x%lx\n",*capacity,*blksz);
  784. dev_desc->lba=*capacity;
  785. dev_desc->blksz=*blksz;
  786. dev_desc->type=perq;
  787. USB_STOR_PRINTF(" address %d\n",dev_desc->target);
  788. USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
  789. init_part(dev_desc);
  790. USB_STOR_PRINTF("partype: %d\n",dev_desc->part_type);
  791. return 1;
  792. }
  793. #endif
  794. #endif /* CONFIG_USB_STORAGE */