usb_storage.c 39 KB

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