usb_storage.c 40 KB

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