usbmsc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* USB mass-storage class driver: Bulk-Only Transport (BOT) + SCSI.
  2. *
  3. * The USB core's device probe sees every enumerated device; this driver
  4. * parses the configuration for an interface with class 8 / subclass 6 /
  5. * protocol 0x50 (SCSI transparent, bulk-only), claims its bulk IN/OUT
  6. * endpoints and exposes the disk as `usb0..` once usbmsc_blk_scan() runs.
  7. *
  8. * Only EHCI and xHCI implement the bulk-transfer ops; UHCI/OHCI devices are
  9. * left unclaimed (a full-speed stick on a companion controller cannot be
  10. * used). The block layer calls usbmsc_read/write/flush from blkdev.c.
  11. */
  12. #include "usbmsc.h"
  13. #include "blkdev.h"
  14. #include "io.h"
  15. #include "serial.h"
  16. #include "timer.h"
  17. #include "usb.h"
  18. #define USB_MSC_MAX 4
  19. /* Keep one SCSI data stage below EHCI's 0x7FFF-byte bulk-OUT limit:
  20. * 62 sectors = 31,744 bytes. */
  21. #define MSC_CHUNK_SECTORS 62u
  22. #define MSC_BULK_CHUNK 2048u /* EHCI's fixed bulk-IN buffer */
  23. #define MSC_TIMEOUT_MS 3000u
  24. #define MSC_CBW_SIG 0x43425355u /* "USBC" LE */
  25. #define MSC_CSW_SIG 0x53425355u /* "USBS" LE */
  26. struct usb_msc {
  27. int present;
  28. unsigned int ctrl, addr, mps, port;
  29. int low;
  30. unsigned int ep_in, ep_in_mps; /* endpoint numbers (1..15) */
  31. unsigned int ep_out, ep_out_mps;
  32. unsigned int iface;
  33. unsigned int sectors;
  34. unsigned int tag;
  35. unsigned int last_status, last_residue;
  36. unsigned int sense_key, sense_asc, sense_ascq;
  37. char model[41];
  38. };
  39. static struct usb_msc msc[USB_MSC_MAX];
  40. static char msc_names[USB_MSC_MAX][6];
  41. static unsigned int msc_generation;
  42. static unsigned int rd32le(const unsigned char *p)
  43. {
  44. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  45. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  46. }
  47. static unsigned int rd32be(const unsigned char *p)
  48. {
  49. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  50. ((unsigned int)p[2] << 8) | (unsigned int)p[3];
  51. }
  52. static void put32le(unsigned char *p, unsigned int v)
  53. {
  54. p[0] = (unsigned char)(v & 0xFF);
  55. p[1] = (unsigned char)((v >> 8) & 0xFF);
  56. p[2] = (unsigned char)((v >> 16) & 0xFF);
  57. p[3] = (unsigned char)((v >> 24) & 0xFF);
  58. }
  59. static void put16be(unsigned char *p, unsigned int v)
  60. {
  61. p[0] = (unsigned char)((v >> 8) & 0xFF);
  62. p[1] = (unsigned char)(v & 0xFF);
  63. }
  64. static void put32be(unsigned char *p, unsigned int v)
  65. {
  66. p[0] = (unsigned char)((v >> 24) & 0xFF);
  67. p[1] = (unsigned char)((v >> 16) & 0xFF);
  68. p[2] = (unsigned char)((v >> 8) & 0xFF);
  69. p[3] = (unsigned char)(v & 0xFF);
  70. }
  71. static void msc_delay(unsigned int ms)
  72. {
  73. unsigned int t0 = get_ticks_ms();
  74. while (get_ticks_ms() - t0 < ms)
  75. ;
  76. }
  77. /* Bulk IN with a deadline. Transfers larger than the host controller's
  78. * internal bulk-IN buffer are split; EHCI's is 2048 bytes. */
  79. static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
  80. {
  81. unsigned int got = 0, t0 = get_ticks_ms();
  82. while (got < len) {
  83. unsigned int want = len - got;
  84. int n;
  85. if (want > MSC_BULK_CHUNK)
  86. want = MSC_BULK_CHUNK;
  87. n = usb_bulk_in(m->ctrl, m->addr, m->ep_in, m->ep_in_mps,
  88. buf + got, want);
  89. if (n < 0)
  90. return -1;
  91. if (n > 0) {
  92. /* A short packet is normal inside a BOT data stage:
  93. * the device may split it at any USB transfer
  94. * boundary. Keep reading until the Stage's known
  95. * byte count is reached. */
  96. got += (unsigned int)n;
  97. t0 = get_ticks_ms();
  98. } else if (get_ticks_ms() - t0 > MSC_TIMEOUT_MS) {
  99. return -1;
  100. }
  101. }
  102. return 0;
  103. }
  104. static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
  105. unsigned int len)
  106. {
  107. return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
  108. buf, len);
  109. }
  110. /* BOT reset + clear both bulk endpoint halts. */
  111. static void msc_reset(struct usb_msc *m)
  112. {
  113. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  114. 0x21, 0xFF, 0, (unsigned short)m->iface);
  115. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  116. 0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
  117. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  118. 0x02, 0x01, 0, (unsigned short)m->ep_out);
  119. m->tag++;
  120. }
  121. static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
  122. unsigned int cb_len, void *data, unsigned int data_len,
  123. int dir_in)
  124. {
  125. unsigned char cbw[31];
  126. unsigned char csw[13];
  127. unsigned int tag = ++m->tag;
  128. for (unsigned int i = 0; i < sizeof(cbw); i++)
  129. cbw[i] = 0;
  130. put32le(cbw, MSC_CBW_SIG);
  131. put32le(cbw + 4, tag);
  132. put32le(cbw + 8, data_len);
  133. cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00);
  134. cbw[13] = 0; /* LUN 0 */
  135. cbw[14] = (unsigned char)cb_len;
  136. for (unsigned int i = 0; i < cb_len && i < 16; i++)
  137. cbw[15 + i] = cb[i];
  138. if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0)
  139. return -1;
  140. if (data_len) {
  141. if (dir_in) {
  142. if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0)
  143. return -1;
  144. } else if (msc_bulk_out(m, (const unsigned char *)data,
  145. data_len) != 0) {
  146. return -1;
  147. }
  148. }
  149. if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
  150. return -1;
  151. if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag) {
  152. m->last_status = 0xFF;
  153. return -1;
  154. }
  155. m->last_status = csw[12];
  156. m->last_residue = rd32le(csw + 8);
  157. if (csw[12] != 0)
  158. return -1;
  159. if (m->last_residue != 0)
  160. return -1;
  161. return 0;
  162. }
  163. /* Fetch the sense data after a failed command so the next CBW does not keep
  164. * returning the same unit attention/medium error. */
  165. static int msc_sense(struct usb_msc *m)
  166. {
  167. unsigned char cb[6] = { 0x03, 0, 0, 0, 18, 0 };
  168. unsigned char data[18];
  169. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  170. return -1;
  171. m->sense_key = data[2] & 0x0F;
  172. m->sense_asc = data[12];
  173. m->sense_ascq = data[13];
  174. return 0;
  175. }
  176. static int msc_test_unit_ready(struct usb_msc *m)
  177. {
  178. unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 };
  179. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  180. }
  181. static int msc_inquiry(struct usb_msc *m, unsigned char data[36])
  182. {
  183. unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 };
  184. return bot_cmd(m, cb, sizeof(cb), data, 36, 1);
  185. }
  186. static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors)
  187. {
  188. unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  189. unsigned char data[8];
  190. unsigned int last, block;
  191. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  192. return -1;
  193. last = rd32be(data);
  194. block = rd32be(data + 4);
  195. if (block != 512 || last == 0xFFFFFFFFu)
  196. return -1;
  197. *sectors = last + 1;
  198. return 0;
  199. }
  200. static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
  201. void *buf)
  202. {
  203. unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  204. put32be(cb + 2, lba);
  205. put16be(cb + 7, count);
  206. return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
  207. }
  208. static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
  209. unsigned int count, void *buf)
  210. {
  211. unsigned int i;
  212. for (i = 0; i < 3; i++) {
  213. if (msc_read10(m, lba, count, buf) == 0)
  214. return 0;
  215. msc_sense(m);
  216. msc_reset(m);
  217. msc_delay(10);
  218. }
  219. return -1;
  220. }
  221. static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
  222. const void *buf)
  223. {
  224. unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  225. put32be(cb + 2, lba);
  226. put16be(cb + 7, count);
  227. return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
  228. }
  229. static int msc_sync_cache(struct usb_msc *m)
  230. {
  231. unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  232. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  233. }
  234. /* Find and configure a bulk-only mass-storage interface. */
  235. static int msc_config(struct usb_msc *m)
  236. {
  237. unsigned char hdr[9], cfg[256];
  238. unsigned int total, i, config = 0;
  239. unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
  240. unsigned int iface = 0;
  241. int found = 0;
  242. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  243. 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
  244. return -1;
  245. total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
  246. if (total < 9 || total > sizeof(cfg))
  247. return -1;
  248. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  249. 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
  250. return -1;
  251. config = cfg[5];
  252. if (!config)
  253. return -1;
  254. i = 0;
  255. while (i + 2 <= total) {
  256. unsigned int blen = cfg[i];
  257. unsigned int btype = cfg[i + 1];
  258. if (blen < 2 || i + blen > total)
  259. break;
  260. if (btype == 0x04 && blen >= 9) { /* interface */
  261. if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
  262. cfg[i + 7] == 0x50) {
  263. found = 1;
  264. iface = cfg[i + 2];
  265. ep_in = ep_out = 0;
  266. ep_in_mps = ep_out_mps = 0;
  267. } else {
  268. found = 0;
  269. }
  270. } else if (btype == 0x05 && blen >= 7 && found) {
  271. unsigned int epa = cfg[i + 2];
  272. unsigned int attr = cfg[i + 3] & 3u;
  273. unsigned int mps = ((unsigned int)cfg[i + 4] |
  274. ((unsigned int)cfg[i + 5] << 8)) &
  275. 0x7FFu;
  276. if (attr == 2) {
  277. if (epa & 0x80) {
  278. ep_in = epa & 0x0F;
  279. ep_in_mps = mps;
  280. } else {
  281. ep_out = epa & 0x0F;
  282. ep_out_mps = mps;
  283. }
  284. }
  285. }
  286. i += blen;
  287. }
  288. if (!found || !ep_in || !ep_out)
  289. return -1;
  290. if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  291. 0x00, 0x09, (unsigned short)config, 0) < 0)
  292. return -1;
  293. m->iface = iface;
  294. m->ep_in = ep_in;
  295. m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
  296. m->ep_out = ep_out;
  297. m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
  298. return 0;
  299. }
  300. static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
  301. {
  302. unsigned int i, n = 0;
  303. for (i = 0; i < 8; i++)
  304. m->model[n++] = (char)data[8 + i];
  305. m->model[n++] = ' ';
  306. for (i = 0; i < 16; i++)
  307. m->model[n++] = (char)data[16 + i];
  308. while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
  309. n--;
  310. m->model[n] = '\0';
  311. }
  312. static int device_probe(unsigned int ctrl, unsigned int addr,
  313. unsigned int mps, int low_speed, unsigned int port,
  314. unsigned int vendor, unsigned int product)
  315. {
  316. struct usb_msc *m = 0;
  317. unsigned char inquiry[36];
  318. unsigned int i, sectors;
  319. int slot = -1;
  320. (void)vendor;
  321. (void)product;
  322. /* A new bus scan re-enumerates every device with fresh addresses;
  323. * forget the old set on its first probe. */
  324. {
  325. unsigned int gen = usb_generation();
  326. if (gen != msc_generation) {
  327. for (i = 0; i < USB_MSC_MAX; i++)
  328. msc[i].present = 0;
  329. msc_generation = gen;
  330. }
  331. }
  332. for (i = 0; i < USB_MSC_MAX; i++)
  333. if (msc[i].present && msc[i].ctrl == ctrl &&
  334. msc[i].addr == addr) {
  335. slot = (int)i;
  336. break;
  337. }
  338. if (slot < 0)
  339. for (i = 0; i < USB_MSC_MAX; i++)
  340. if (!msc[i].present) {
  341. slot = (int)i;
  342. break;
  343. }
  344. if (slot < 0)
  345. return 0;
  346. m = &msc[(unsigned int)slot];
  347. m->present = 0;
  348. m->ctrl = ctrl;
  349. m->addr = addr;
  350. m->mps = mps;
  351. m->low = low_speed;
  352. m->port = port;
  353. m->tag = 0;
  354. m->iface = 0;
  355. if (msc_config(m) != 0)
  356. return 0;
  357. for (i = 0; i < 4; i++) {
  358. if (msc_test_unit_ready(m) == 0)
  359. break;
  360. msc_delay(50);
  361. msc_reset(m);
  362. }
  363. if (i == 4) {
  364. serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
  365. return 0;
  366. }
  367. if (msc_inquiry(m, inquiry) != 0) {
  368. serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
  369. msc_reset(m);
  370. if (msc_inquiry(m, inquiry) != 0)
  371. return 0;
  372. }
  373. if (msc_read_capacity(m, &sectors) != 0) {
  374. serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
  375. msc_reset(m);
  376. if (msc_read_capacity(m, &sectors) != 0)
  377. return 0;
  378. }
  379. if (sectors == 0)
  380. return 0;
  381. msc_set_model(m, inquiry);
  382. m->sectors = sectors;
  383. m->present = 1;
  384. serial_puts(SERIAL_COM1, "usbmsc: ");
  385. serial_puts(SERIAL_COM1, m->model);
  386. serial_puts(SERIAL_COM1, " ");
  387. serial_putdec(SERIAL_COM1, sectors);
  388. serial_puts(SERIAL_COM1, " sectors on ctrl ");
  389. serial_putdec(SERIAL_COM1, ctrl);
  390. serial_puts(SERIAL_COM1, " addr ");
  391. serial_putdec(SERIAL_COM1, addr);
  392. serial_puts(SERIAL_COM1, "\n");
  393. return 1;
  394. }
  395. void usbmsc_register(void)
  396. {
  397. unsigned int i;
  398. for (i = 0; i < USB_MSC_MAX; i++) {
  399. msc_names[i][0] = 'u';
  400. msc_names[i][1] = 's';
  401. msc_names[i][2] = 'b';
  402. msc_names[i][3] = (char)('0' + i);
  403. msc_names[i][4] = '\0';
  404. }
  405. usb_register_device_probe(device_probe);
  406. }
  407. void usbmsc_blk_scan(void)
  408. {
  409. unsigned int i;
  410. for (i = 0; i < USB_MSC_MAX; i++) {
  411. blk_dev_t *d;
  412. unsigned int j;
  413. if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
  414. continue;
  415. d = &blk_devs[blk_dev_count++];
  416. d->name = msc_names[i];
  417. d->kind = BLK_USB;
  418. d->bus = 0;
  419. d->unit = i;
  420. d->present = 1;
  421. d->total_sectors = msc[i].sectors;
  422. for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
  423. d->model[j] = msc[i].model[j];
  424. d->model[j] = '\0';
  425. }
  426. }
  427. static int msc_get(unsigned int unit, struct usb_msc **out)
  428. {
  429. if (unit >= USB_MSC_MAX || !msc[unit].present)
  430. return -1;
  431. *out = &msc[unit];
  432. return 0;
  433. }
  434. int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
  435. unsigned int count)
  436. {
  437. struct usb_msc *m;
  438. unsigned char *p = (unsigned char *)buf;
  439. if (msc_get(unit, &m) != 0)
  440. return -1;
  441. if (lba > m->sectors || count > m->sectors - lba)
  442. return -1;
  443. while (count) {
  444. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  445. if (msc_read10_retry(m, lba, n, p) != 0) {
  446. /* Some bridges/Sticks fail a large READ(10) but
  447. * accept the same range one sector at a time. This
  448. * also narrows a real medium error to one LBA. */
  449. if (n > 1) {
  450. unsigned int i;
  451. io_putstr("usbmsc: lba ");
  452. io_puthex(lba);
  453. io_putstr(" retrying 1-sector reads\n");
  454. for (i = 0; i < n; i++)
  455. if (msc_read10_retry(m, lba + i, 1,
  456. p + i * 512u) != 0) {
  457. io_putstr("usbmsc: sector ");
  458. io_puthex(lba + i);
  459. io_putstr(" failed, sense ");
  460. io_puthex((m->sense_key << 16) |
  461. (m->sense_asc << 8) |
  462. m->sense_ascq);
  463. io_putchar('\n');
  464. return -1;
  465. }
  466. } else {
  467. io_putstr("usbmsc: lba ");
  468. io_puthex(lba);
  469. io_putstr(" failed, sense ");
  470. io_puthex((m->sense_key << 16) |
  471. (m->sense_asc << 8) | m->sense_ascq);
  472. io_putchar('\n');
  473. return -1;
  474. }
  475. }
  476. lba += n;
  477. p += n * 512u;
  478. count -= n;
  479. }
  480. return 0;
  481. }
  482. int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
  483. unsigned int count)
  484. {
  485. struct usb_msc *m;
  486. const unsigned char *p = (const unsigned char *)buf;
  487. if (msc_get(unit, &m) != 0)
  488. return -1;
  489. if (lba > m->sectors || count > m->sectors - lba)
  490. return -1;
  491. while (count) {
  492. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  493. if (msc_write10(m, lba, n, p) != 0) {
  494. msc_reset(m);
  495. if (msc_write10(m, lba, n, p) != 0)
  496. return -1;
  497. }
  498. lba += n;
  499. p += n * 512u;
  500. count -= n;
  501. }
  502. return 0;
  503. }
  504. int usbmsc_flush(unsigned int unit)
  505. {
  506. struct usb_msc *m;
  507. if (msc_get(unit, &m) != 0)
  508. return -1;
  509. if (msc_sync_cache(m) != 0) {
  510. msc_reset(m);
  511. if (msc_sync_cache(m) != 0)
  512. return -1;
  513. }
  514. return 0;
  515. }