usb_storage.c 38 KB

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