usb_storage.c 39 KB

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