usb_storage.c 36 KB

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