usb_storage.c 34 KB

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