usb_storage.c 39 KB

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