usb_storage.c 40 KB

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