usb_storage.c 40 KB

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