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