usb_storage.c 39 KB

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