usb_massstorage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #define CONFIG_USB_STORAGE
  6. #include "usb_os_adapter.h"
  7. #include "trace.h"
  8. #include <linux/usb/ch9.h>
  9. #include "scsi.h"
  10. #include "usb.h"
  11. #include "dwc2_compat.h"
  12. #include <linux/errno.h>
  13. #include "ark_dwc2.h"
  14. #include "usb_massstorage.h"
  15. #include "timer.h"
  16. #include "fs/ff.h"
  17. #include "fs/diskio.h"
  18. /* direction table -- this indicates the direction of the data
  19. * transfer for each command code -- a 1 indicates input
  20. */
  21. static const unsigned char us_direction[256/8] = {
  22. 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
  23. 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  26. };
  27. #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
  28. #pragma pack(ARCH_DMA_MINALIGN)
  29. static struct scsi_cmd usb_ccb;
  30. static u32 CBWTag;
  31. static int usb_max_devs; /* number of highest available usb device */
  32. struct blk_desc usb_dev_desc[6];
  33. struct us_data;
  34. typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
  35. typedef int (*trans_reset)(struct us_data *data);
  36. #define USB_READY (1 << 0)
  37. struct us_data {
  38. struct usb_device *pusb_dev; /* this usb_device */
  39. unsigned int flags; /* from filter initially */
  40. unsigned char ifnum; /* interface number */
  41. unsigned char ep_in; /* in endpoint */
  42. unsigned char ep_out; /* out ....... */
  43. unsigned char ep_int; /* interrupt . */
  44. unsigned char subclass; /* as in overview */
  45. unsigned char protocol; /* .............. */
  46. unsigned char attention_done; /* force attn on first cmd */
  47. unsigned short ip_data; /* interrupt data */
  48. int action; /* what to do */
  49. int ip_wanted; /* needed */
  50. int *irq_handle; /* for USB int requests */
  51. unsigned int irqpipe; /* pipe for release_irq */
  52. unsigned char irqmaxp; /* max packed for irq Pipe */
  53. unsigned char irqinterval; /* Intervall for IRQ Pipe */
  54. struct scsi_cmd *srb; /* current srb */
  55. trans_reset transport_reset; /* reset routine */
  56. trans_cmnd transport; /* transport routine */
  57. unsigned short max_xfer_blk; /* maximum transfer blocks */
  58. };
  59. static struct us_data usb_stor[USB_MAX_STOR_DEV];
  60. #define USB_STOR_TRANSPORT_GOOD 0
  61. #define USB_STOR_TRANSPORT_FAILED -1
  62. #define USB_STOR_TRANSPORT_ERROR -2
  63. static int usb_media_ready = 0;
  64. /***********************************************************************
  65. * Data transfer routines
  66. ***********************************************************************/
  67. static unsigned int usb_get_max_lun(struct us_data *us)
  68. {
  69. int len;
  70. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
  71. len = usb_control_msg(us->pusb_dev,
  72. usb_rcvctrlpipe(us->pusb_dev, 0),
  73. US_BBB_GET_MAX_LUN,
  74. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  75. 0, us->ifnum,
  76. result, sizeof(char),
  77. USB_CNTL_TIMEOUT * 5);
  78. //debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
  79. return (len > 0) ? *result : 0;
  80. }
  81. static int usb_stor_BBB_reset(struct us_data *us)
  82. {
  83. int result;
  84. unsigned int pipe;
  85. /*
  86. * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
  87. *
  88. * For Reset Recovery the host shall issue in the following order:
  89. * a) a Bulk-Only Mass Storage Reset
  90. * b) a Clear Feature HALT to the Bulk-In endpoint
  91. * c) a Clear Feature HALT to the Bulk-Out endpoint
  92. *
  93. * This is done in 3 steps.
  94. *
  95. * If the reset doesn't succeed, the device should be port reset.
  96. *
  97. * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
  98. */
  99. //debug("BBB_reset\n");
  100. result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  101. US_BBB_RESET,
  102. USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  103. 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
  104. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  105. //debug("RESET:stall\n");
  106. return -1;
  107. }
  108. if (usb_media_ready == 0)
  109. return -1;
  110. /* long wait for reset */
  111. mdelay(150);
  112. /* debug("BBB_reset result %d: status %lX reset\n",
  113. result, us->pusb_dev->status); */
  114. pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  115. result = usb_clear_halt(us->pusb_dev, pipe);
  116. /* long wait for reset */
  117. mdelay(150);
  118. /* debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
  119. result, us->pusb_dev->status); */
  120. /* long wait for reset */
  121. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  122. result = usb_clear_halt(us->pusb_dev, pipe);
  123. mdelay(150);
  124. /* debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
  125. result, us->pusb_dev->status);
  126. debug("BBB_reset done\n"); */
  127. return 0;
  128. }
  129. /*
  130. * Set up the command for a BBB device. Note that the actual SCSI
  131. * command is copied into cbw.CBWCDB.
  132. */
  133. static int usb_stor_BBB_comdat(struct scsi_cmd *srb, struct us_data *us)
  134. {
  135. int result;
  136. int actlen;
  137. int dir_in;
  138. unsigned int pipe;
  139. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1);
  140. dir_in = US_DIRECTION(srb->cmd[0]);
  141. /* sanity checks */
  142. if (!(srb->cmdlen <= CBWCDBLENGTH)) {
  143. //debug("usb_stor_BBB_comdat:cmdlen too large\n");
  144. return -1;
  145. }
  146. /* always OUT to the ep */
  147. pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  148. cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
  149. cbw->dCBWTag = cpu_to_le32(CBWTag++);
  150. cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
  151. cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
  152. cbw->bCBWLUN = srb->lun;
  153. cbw->bCDBLength = srb->cmdlen;
  154. /* copy the command data into the CBW command data buffer */
  155. /* DST SRC LEN!!! */
  156. memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
  157. result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
  158. &actlen, USB_CNTL_TIMEOUT * 5);
  159. if (result < 0)
  160. ;//debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
  161. return result;
  162. }
  163. #define USB_TRANSPORT_UNKNOWN_RETRY 5
  164. #define USB_TRANSPORT_NOT_READY_RETRY 10
  165. /* clear a stall on an endpoint - special for BBB devices */
  166. static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, u8 endpt)
  167. {
  168. /* ENDPOINT_HALT = 0, so set value to 0 */
  169. return usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
  170. USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
  171. endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
  172. }
  173. static int usb_stor_BBB_transport(struct scsi_cmd *srb, struct us_data *us)
  174. {
  175. int result, retry;
  176. int dir_in;
  177. int actlen, data_actlen;
  178. unsigned int pipe, pipein, pipeout;
  179. ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1);
  180. dir_in = US_DIRECTION(srb->cmd[0]);
  181. /* COMMAND phase */
  182. //debug("COMMAND phase\n");
  183. result = usb_stor_BBB_comdat(srb, us);
  184. if (result < 0) {
  185. /* debug("failed to send CBW status %ld\n",
  186. us->pusb_dev->status); */
  187. usb_stor_BBB_reset(us);
  188. return USB_STOR_TRANSPORT_FAILED;
  189. }
  190. if (!(us->flags & USB_READY))
  191. mdelay(5);
  192. pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
  193. pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
  194. /* DATA phase + error handling */
  195. data_actlen = 0;
  196. /* no data, go immediately to the STATUS phase */
  197. if (srb->datalen == 0)
  198. goto st;
  199. //debug("DATA phase\n");
  200. if (dir_in)
  201. pipe = pipein;
  202. else
  203. pipe = pipeout;
  204. result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
  205. &data_actlen, USB_CNTL_TIMEOUT * 5);
  206. /* special handling of STALL in DATA phase */
  207. if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
  208. //debug("DATA:stall\n");
  209. /* clear the STALL on the endpoint */
  210. result = usb_stor_BBB_clear_endpt_stall(us,
  211. dir_in ? us->ep_in : us->ep_out);
  212. if (result >= 0)
  213. /* continue on to STATUS phase */
  214. goto st;
  215. }
  216. if (result < 0) {
  217. /* debug("usb_bulk_msg error status %ld\n",
  218. us->pusb_dev->status); */
  219. usb_stor_BBB_reset(us);
  220. return USB_STOR_TRANSPORT_FAILED;
  221. }
  222. /* STATUS phase + error handling */
  223. char *tmpcsw;
  224. st:
  225. retry = 0;
  226. again:
  227. //debug("STATUS phase\n");
  228. tmpcsw = (char *)csw;
  229. tmpcsw[0] = 0x55;
  230. tmpcsw[1] = 0x53;
  231. tmpcsw[2] = 0x42;
  232. tmpcsw[3] = 0x43;
  233. tmpcsw[4] = 0x01;
  234. tmpcsw[5] = 0x00;
  235. tmpcsw[6] = 0x00;
  236. tmpcsw[7] = 0x00;
  237. tmpcsw[8] = 0x24;
  238. tmpcsw[9] = 0x00;
  239. tmpcsw[10] = 0x00;
  240. tmpcsw[11] = 0x00;
  241. tmpcsw[12] = 0x80;
  242. result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
  243. &actlen, USB_CNTL_TIMEOUT);
  244. /* special handling of STALL in STATUS phase */
  245. if ((result < 0) && (retry < 1) &&
  246. (us->pusb_dev->status & USB_ST_STALLED)) {
  247. //debug("STATUS:stall\n");
  248. /* clear the STALL on the endpoint */
  249. result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
  250. if (result >= 0 && (retry++ < 1))
  251. /* do a retry */
  252. goto again;
  253. }
  254. if (result < 0) {
  255. /* debug("usb_bulk_msg error status %ld\n",
  256. us->pusb_dev->status); */
  257. usb_stor_BBB_reset(us);
  258. return USB_STOR_TRANSPORT_FAILED;
  259. }
  260. /* misuse pipe to get the residue */
  261. pipe = le32_to_cpu(csw->dCSWDataResidue);
  262. if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
  263. pipe = srb->datalen - data_actlen;
  264. if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
  265. //debug("!CSWSIGNATURE\n");
  266. usb_stor_BBB_reset(us);
  267. return USB_STOR_TRANSPORT_FAILED;
  268. } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
  269. //debug("!Tag\n");
  270. usb_stor_BBB_reset(us);
  271. return USB_STOR_TRANSPORT_FAILED;
  272. } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
  273. //debug(">PHASE\n");
  274. usb_stor_BBB_reset(us);
  275. return USB_STOR_TRANSPORT_FAILED;
  276. } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
  277. //debug("=PHASE\n");
  278. usb_stor_BBB_reset(us);
  279. return USB_STOR_TRANSPORT_FAILED;
  280. } else if (data_actlen > srb->datalen) {
  281. /* debug("transferred %dB instead of %ldB\n",
  282. data_actlen, srb->datalen); */
  283. return USB_STOR_TRANSPORT_FAILED;
  284. } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
  285. //debug("FAILED\n");
  286. return USB_STOR_TRANSPORT_FAILED;
  287. }
  288. return result;
  289. }
  290. static int usb_stor_irq(struct usb_device *dev)
  291. {
  292. struct us_data *us;
  293. us = (struct us_data *)dev->privptr;
  294. if (us->ip_wanted)
  295. us->ip_wanted = 0;
  296. return 0;
  297. }
  298. static void usb_stor_set_max_xfer_blk(struct usb_device *udev,
  299. struct us_data *us)
  300. {
  301. unsigned short blk = 20;
  302. us->max_xfer_blk = blk;
  303. }
  304. static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss)
  305. {
  306. int retry, i;
  307. retry = 5;
  308. do {
  309. memset(&srb->cmd[0], 0, 12);
  310. srb->cmd[0] = SCSI_INQUIRY;
  311. srb->cmd[1] = srb->lun << 5;
  312. srb->cmd[4] = 36;
  313. srb->datalen = 36;
  314. srb->cmdlen = 12;
  315. i = ss->transport(srb, ss);
  316. //debug("inquiry returns %d\n", i);
  317. if (i == 0)
  318. break;
  319. } while (--retry);
  320. if (!retry) {
  321. //printf("error in inquiry\n");
  322. return -1;
  323. }
  324. return 0;
  325. }
  326. static int usb_request_sense(struct scsi_cmd *srb, struct us_data *ss)
  327. {
  328. char *ptr;
  329. int ret = -1;
  330. ptr = (char *)srb->pdata;
  331. memset(&srb->cmd[0], 0, 12);
  332. srb->cmd[0] = SCSI_REQ_SENSE;
  333. srb->cmd[1] = srb->lun << 5;
  334. srb->cmd[4] = 18;
  335. srb->datalen = 18;
  336. srb->pdata = &srb->sense_buf[0];
  337. srb->cmdlen = 12;
  338. ret = ss->transport(srb, ss);
  339. /* debug("Request Sense returned %02X %02X %02X\n",
  340. srb->sense_buf[2], srb->sense_buf[12],
  341. srb->sense_buf[13]); */
  342. srb->pdata = (unsigned char *)ptr;
  343. return ret;
  344. }
  345. static int usb_test_unit_ready(struct scsi_cmd *srb, struct us_data *ss)
  346. {
  347. int retries = 10;
  348. int ret = -1;
  349. do {
  350. memset(&srb->cmd[0], 0, 12);
  351. srb->cmd[0] = SCSI_TST_U_RDY;
  352. srb->cmd[1] = srb->lun << 5;
  353. srb->datalen = 0;
  354. srb->cmdlen = 12;
  355. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
  356. ss->flags |= USB_READY;
  357. return 0;
  358. }
  359. ret = usb_request_sense(srb, ss);
  360. if (ret != USB_STOR_TRANSPORT_GOOD)
  361. break;
  362. /*
  363. * Check the Key Code Qualifier, if it matches
  364. * "Not Ready - medium not present"
  365. * (the sense Key equals 0x2 and the ASC is 0x3a)
  366. * return immediately as the medium being absent won't change
  367. * unless there is a user action.
  368. */
  369. if ((srb->sense_buf[2] == 0x02) &&
  370. (srb->sense_buf[12] == 0x3a))
  371. return -1;
  372. if (usb_media_ready == 0)
  373. break;
  374. mdelay(100);
  375. } while (retries--);
  376. return -1;
  377. }
  378. static int usb_read_capacity(struct scsi_cmd *srb, struct us_data *ss)
  379. {
  380. int retry;
  381. /* XXX retries */
  382. retry = 3;
  383. do {
  384. memset(&srb->cmd[0], 0, 12);
  385. srb->cmd[0] = SCSI_RD_CAPAC;
  386. srb->cmd[1] = srb->lun << 5;
  387. srb->datalen = 8;
  388. srb->cmdlen = 12;
  389. if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
  390. return 0;
  391. } while (retry--);
  392. return -1;
  393. }
  394. static int usb_read_10(struct scsi_cmd *srb, struct us_data *ss,
  395. unsigned long start, unsigned short blocks)
  396. {
  397. memset(&srb->cmd[0], 0, 12);
  398. srb->cmd[0] = SCSI_READ10;
  399. srb->cmd[1] = srb->lun << 5;
  400. srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
  401. srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
  402. srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
  403. srb->cmd[5] = ((unsigned char) (start)) & 0xff;
  404. srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
  405. srb->cmd[8] = (unsigned char) blocks & 0xff;
  406. srb->cmdlen = 12;
  407. //debug("read10: start %lx blocks %x\n", start, blocks);
  408. return ss->transport(srb, ss);
  409. }
  410. /* Probe to see if a new device is actually a Storage device */
  411. int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
  412. struct us_data *ss)
  413. {
  414. struct usb_interface *iface;
  415. int i;
  416. struct usb_endpoint_descriptor *ep_desc;
  417. unsigned int flags = 0;
  418. /* let's examine the device now */
  419. iface = &dev->config.if_desc[ifnum];
  420. if (dev->descriptor.bDeviceClass != 0 ||
  421. iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
  422. iface->desc.bInterfaceSubClass < US_SC_MIN ||
  423. iface->desc.bInterfaceSubClass > US_SC_MAX) {
  424. //SendUartString("Not mass storage\r\n");
  425. /* if it's not a mass storage, we go no further */
  426. return 0;
  427. }
  428. memset(ss, 0, sizeof(struct us_data));
  429. usb_media_ready = 1;
  430. /* At this point, we know we've got a live one */
  431. //debug("\n\nUSB Mass Storage device detected\n");
  432. /* Initialize the us_data structure with some useful info */
  433. ss->flags = flags;
  434. ss->ifnum = ifnum;
  435. ss->pusb_dev = dev;
  436. ss->attention_done = 0;
  437. ss->subclass = iface->desc.bInterfaceSubClass;
  438. ss->protocol = iface->desc.bInterfaceProtocol;
  439. /* set the handler pointers based on the protocol */
  440. //debug("Transport: ");
  441. switch (ss->protocol) {
  442. case US_PR_BULK:
  443. //debug("Bulk/Bulk/Bulk\n");
  444. ss->transport = usb_stor_BBB_transport;
  445. ss->transport_reset = usb_stor_BBB_reset;
  446. break;
  447. default:
  448. //printf("USB Storage Transport unknown / not yet implemented\n");
  449. return 0;
  450. break;
  451. }
  452. /*
  453. * We are expecting a minimum of 2 endpoints - in and out (bulk).
  454. * An optional interrupt is OK (necessary for CBI protocol).
  455. * We will ignore any others.
  456. */
  457. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  458. ep_desc = &iface->ep_desc[i];
  459. /* is it an BULK endpoint? */
  460. if ((ep_desc->bmAttributes &
  461. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  462. if (ep_desc->bEndpointAddress & USB_DIR_IN)
  463. ss->ep_in = ep_desc->bEndpointAddress &
  464. USB_ENDPOINT_NUMBER_MASK;
  465. else
  466. ss->ep_out =
  467. ep_desc->bEndpointAddress &
  468. USB_ENDPOINT_NUMBER_MASK;
  469. }
  470. /* is it an interrupt endpoint? */
  471. if ((ep_desc->bmAttributes &
  472. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  473. ss->ep_int = ep_desc->bEndpointAddress &
  474. USB_ENDPOINT_NUMBER_MASK;
  475. ss->irqinterval = ep_desc->bInterval;
  476. }
  477. }
  478. /* debug("Endpoints In %d Out %d Int %d\n",
  479. ss->ep_in, ss->ep_out, ss->ep_int); */
  480. /* Do some basic sanity checks, and bail if we find a problem */
  481. if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
  482. !ss->ep_in || !ss->ep_out ||
  483. (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
  484. //debug("Problems with device\n");
  485. return 0;
  486. }
  487. /* set class specific stuff */
  488. /* We only handle certain protocols. Currently, these are
  489. * the only ones.
  490. * The SFF8070 accepts the requests used in u-boot
  491. */
  492. if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
  493. ss->subclass != US_SC_8070) {
  494. //printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
  495. return 0;
  496. }
  497. if (ss->ep_int) {
  498. /* we had found an interrupt endpoint, prepare irq pipe
  499. * set up the IRQ pipe and handler
  500. */
  501. ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
  502. ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
  503. ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
  504. dev->irq_handle = usb_stor_irq;
  505. }
  506. /* Set the maximum transfer size per host controller setting */
  507. usb_stor_set_max_xfer_blk(dev, ss);
  508. dev->privptr = (void *)ss;
  509. return 1;
  510. }
  511. int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
  512. struct blk_desc *dev_desc)
  513. {
  514. unsigned char perq, modi;
  515. ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
  516. ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
  517. u32 capacity, blksz;
  518. struct scsi_cmd *pccb = &usb_ccb;
  519. memset(usb_stor_buf, 0, 36);
  520. pccb->pdata = usb_stor_buf;
  521. dev_desc->target = dev->devnum;
  522. pccb->lun = dev_desc->lun;
  523. //debug(" address %d\n", dev_desc->target);
  524. if (usb_inquiry(pccb, ss)) {
  525. //debug("%s: usb_inquiry() failed\n", __func__);
  526. return -1;
  527. }
  528. perq = usb_stor_buf[0];
  529. modi = usb_stor_buf[1];
  530. /*
  531. * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
  532. * they would not respond to test_unit_ready .
  533. */
  534. if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
  535. //debug("%s: unknown/unsupported device\n", __func__);
  536. return 0;
  537. }
  538. if ((modi&0x80) == 0x80) {
  539. /* drive is removable */
  540. dev_desc->removable = 1;
  541. }
  542. memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
  543. memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
  544. memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
  545. dev_desc->vendor[8] = 0;
  546. dev_desc->product[16] = 0;
  547. dev_desc->revision[4] = 0;
  548. /* debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
  549. usb_stor_buf[3]); */
  550. if (usb_test_unit_ready(pccb, ss)) {
  551. /* printf("Device NOT ready\n"
  552. " Request Sense returned %02X %02X %02X\n",
  553. pccb->sense_buf[2], pccb->sense_buf[12],
  554. pccb->sense_buf[13]); */
  555. if (dev_desc->removable == 1)
  556. dev_desc->type = perq;
  557. return 0;
  558. }
  559. pccb->pdata = (unsigned char *)cap;
  560. memset(pccb->pdata, 0, 8);
  561. if (usb_read_capacity(pccb, ss) != 0) {
  562. //printf("READ_CAP ERROR\n");
  563. cap[0] = 2880;
  564. cap[1] = 0x200;
  565. }
  566. ss->flags &= ~USB_READY;
  567. //debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
  568. #if 0
  569. if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
  570. cap[0] >>= 16;
  571. cap[0] = cpu_to_be32(cap[0]);
  572. cap[1] = cpu_to_be32(cap[1]);
  573. #endif
  574. capacity = be32_to_cpu(cap[0]) + 1;
  575. blksz = be32_to_cpu(cap[1]);
  576. dev_desc->lba = capacity;
  577. dev_desc->blksz = blksz;
  578. dev_desc->log2blksz = LOG2(dev_desc->blksz);
  579. dev_desc->type = perq;
  580. return 1;
  581. }
  582. static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
  583. lbaint_t blkcnt, void *buffer)
  584. {
  585. lbaint_t start, blks;
  586. uintptr_t buf_addr;
  587. unsigned short smallblks;
  588. struct usb_device *udev;
  589. struct us_data *ss;
  590. int retry;
  591. struct scsi_cmd *srb = &usb_ccb;
  592. if (blkcnt == 0)
  593. return 0;
  594. /* Setup device */
  595. udev = usb_dev_desc[block_dev->devnum].priv;
  596. if (!udev) {
  597. return 0;
  598. }
  599. ss = (struct us_data *)udev->privptr;
  600. usb_disable_asynch(1); /* asynch transfer not allowed */
  601. srb->lun = block_dev->lun;
  602. buf_addr = (uintptr_t)buffer;
  603. start = blknr;
  604. blks = blkcnt;
  605. do {
  606. retry = 2;
  607. srb->pdata = (unsigned char *)buf_addr;
  608. if (blks > ss->max_xfer_blk)
  609. smallblks = ss->max_xfer_blk;
  610. else
  611. smallblks = (unsigned short) blks;
  612. retry_it:
  613. if (usb_media_ready == 0)
  614. break;
  615. /* if (smallblks == ss->max_xfer_blk)
  616. usb_show_progress(); */
  617. srb->datalen = block_dev->blksz * smallblks;
  618. srb->pdata = (unsigned char *)buf_addr;
  619. if (usb_read_10(srb, ss, start, smallblks)) {
  620. usb_request_sense(srb, ss);
  621. if (retry--)
  622. goto retry_it;
  623. blkcnt -= blks;
  624. break;
  625. }
  626. start += smallblks;
  627. blks -= smallblks;
  628. buf_addr += srb->datalen;
  629. } while (blks != 0);
  630. ss->flags &= ~USB_READY;
  631. usb_disable_asynch(0);
  632. return blkcnt;
  633. }
  634. static int usb_stor_probe_device(struct usb_device *udev)
  635. {
  636. int lun, max_lun;
  637. int start;
  638. if (udev == NULL)
  639. return -ENOENT; /* no more devices available */
  640. //debug("\n\nProbing for storage\n");
  641. /* We don't have space to even probe if we hit the maximum */
  642. if (usb_max_devs == USB_MAX_STOR_DEV) {
  643. /* printf("max USB Storage Device reached: %d stopping\n",
  644. usb_max_devs); */
  645. return -ENOSPC;
  646. }
  647. if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs]))
  648. return 0;
  649. /*
  650. * OK, it's a storage device. Iterate over its LUNs and populate
  651. * usb_dev_desc'
  652. */
  653. start = usb_max_devs;
  654. max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
  655. for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
  656. lun++) {
  657. struct blk_desc *blkdev;
  658. blkdev = &usb_dev_desc[usb_max_devs];
  659. memset(blkdev, '\0', sizeof(struct blk_desc));
  660. blkdev->if_type = IF_TYPE_USB;
  661. blkdev->devnum = usb_max_devs;
  662. blkdev->part_type = PART_TYPE_UNKNOWN;
  663. blkdev->target = 0xff;
  664. blkdev->type = DEV_TYPE_UNKNOWN;
  665. blkdev->block_read = usb_stor_read;
  666. //blkdev->block_write = usb_stor_write;
  667. blkdev->lun = lun;
  668. blkdev->priv = udev;
  669. if (usb_stor_get_info(udev, &usb_stor[start],
  670. &usb_dev_desc[usb_max_devs]) == 1) {
  671. //debug("partype: %d\n", blkdev->part_type);
  672. //part_init(blkdev);
  673. //debug("partype: %d\n", blkdev->part_type);
  674. usb_max_devs++;
  675. //debug("%s: Found device %p\n", __func__, udev);
  676. /* blkdev->disk_priv = (void *)FF_USBDiskInit("/usb");
  677. if (NULL == blkdev->disk_priv) {
  678. printf("FF_USBDiskInit failed\r\n");
  679. continue;
  680. }
  681. mdelay(200);
  682. usb_disk_file_test(); */
  683. }
  684. }
  685. return 0;
  686. }
  687. void usb_stor_reset(void)
  688. {
  689. usb_max_devs = 0;
  690. CBWTag = 0;
  691. }
  692. /*******************************************************************************
  693. * scan the usb and reports device info
  694. * to the user if mode = 1
  695. * returns current device or -1 if no
  696. */
  697. int usb_stor_scan(int mode)
  698. {
  699. unsigned char i;
  700. if (mode == 1)
  701. SendUartString("scanning usb for storage devices... ");
  702. usb_disable_asynch(1); /* asynch transfer not allowed */
  703. usb_stor_reset();
  704. for (i = 0; i < USB_MAX_DEVICE; i++) {
  705. struct usb_device *dev;
  706. dev = usb_get_dev_index(i); /* get device */
  707. //debug("i=%d\n", i);
  708. if (usb_stor_probe_device(dev))
  709. break;
  710. } /* for */
  711. usb_disable_asynch(0); /* asynch transfer allowed */
  712. if (usb_max_devs > 0) {
  713. SendUartString("Storage Device(s) found\r\n");
  714. return 0;
  715. }
  716. return -1;
  717. }
  718. void usb_stor_disconnect()
  719. {
  720. usb_media_ready = 0;
  721. }
  722. int USB_disk_initialize(void)
  723. {
  724. return 0;
  725. }
  726. int USB_disk_read(void *buff, DWORD sector, BYTE count)
  727. {
  728. struct blk_desc *dev_desc = &usb_dev_desc[0];
  729. return dev_desc->block_read(dev_desc, sector, count, buff);
  730. }
  731. int USB_disk_ioctl(BYTE ctrl, void *buff)
  732. {
  733. struct blk_desc *dev_desc = &usb_dev_desc[0];
  734. switch(ctrl)
  735. {
  736. case CTRL_SYNC:
  737. break;
  738. case GET_SECTOR_COUNT:
  739. *(DWORD*)buff = dev_desc->lba;
  740. break;
  741. case GET_BLOCK_SIZE:
  742. *(DWORD*)buff = 512;
  743. break;
  744. default:
  745. return -1;
  746. }
  747. return 0;
  748. }