usb_storage.c 36 KB

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