usb_storage.c 39 KB

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