usbmsc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. /* Sleeps inside a bus scan or a claimed storage-rescan command (see
  72. * usb_delay_ms); busy-waits everywhere else. */
  73. static void msc_delay(unsigned int ms)
  74. {
  75. usb_delay_ms(ms);
  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. } else {
  101. usb_poll_relax(t0);
  102. }
  103. }
  104. return 0;
  105. }
  106. static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
  107. unsigned int len)
  108. {
  109. return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
  110. buf, len);
  111. }
  112. /* BOT reset + clear both bulk endpoint halts. */
  113. static void msc_reset(struct usb_msc *m)
  114. {
  115. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  116. 0x21, 0xFF, 0, (unsigned short)m->iface);
  117. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  118. 0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
  119. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  120. 0x02, 0x01, 0, (unsigned short)m->ep_out);
  121. m->tag++;
  122. }
  123. static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
  124. unsigned int cb_len, void *data, unsigned int data_len,
  125. int dir_in)
  126. {
  127. unsigned char cbw[31];
  128. unsigned char csw[13];
  129. unsigned int tag = ++m->tag;
  130. for (unsigned int i = 0; i < sizeof(cbw); i++)
  131. cbw[i] = 0;
  132. put32le(cbw, MSC_CBW_SIG);
  133. put32le(cbw + 4, tag);
  134. put32le(cbw + 8, data_len);
  135. cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00);
  136. cbw[13] = 0; /* LUN 0 */
  137. cbw[14] = (unsigned char)cb_len;
  138. for (unsigned int i = 0; i < cb_len && i < 16; i++)
  139. cbw[15 + i] = cb[i];
  140. if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0)
  141. return -1;
  142. if (data_len) {
  143. if (dir_in) {
  144. if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0)
  145. return -1;
  146. } else if (msc_bulk_out(m, (const unsigned char *)data,
  147. data_len) != 0) {
  148. return -1;
  149. }
  150. }
  151. if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
  152. return -1;
  153. if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag) {
  154. m->last_status = 0xFF;
  155. return -1;
  156. }
  157. m->last_status = csw[12];
  158. m->last_residue = rd32le(csw + 8);
  159. if (csw[12] != 0)
  160. return -1;
  161. if (m->last_residue != 0)
  162. return -1;
  163. return 0;
  164. }
  165. /* Fetch the sense data after a failed command so the next CBW does not keep
  166. * returning the same unit attention/medium error. */
  167. static int msc_sense(struct usb_msc *m)
  168. {
  169. unsigned char cb[6] = { 0x03, 0, 0, 0, 18, 0 };
  170. unsigned char data[18];
  171. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  172. return -1;
  173. m->sense_key = data[2] & 0x0F;
  174. m->sense_asc = data[12];
  175. m->sense_ascq = data[13];
  176. return 0;
  177. }
  178. static int msc_test_unit_ready(struct usb_msc *m)
  179. {
  180. unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 };
  181. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  182. }
  183. static int msc_inquiry(struct usb_msc *m, unsigned char data[36])
  184. {
  185. unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 };
  186. return bot_cmd(m, cb, sizeof(cb), data, 36, 1);
  187. }
  188. static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors)
  189. {
  190. unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  191. unsigned char data[8];
  192. unsigned int last, block;
  193. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  194. return -1;
  195. last = rd32be(data);
  196. block = rd32be(data + 4);
  197. if (block != 512 || last == 0xFFFFFFFFu)
  198. return -1;
  199. *sectors = last + 1;
  200. return 0;
  201. }
  202. static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
  203. void *buf)
  204. {
  205. unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  206. put32be(cb + 2, lba);
  207. put16be(cb + 7, count);
  208. return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
  209. }
  210. static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
  211. unsigned int count, void *buf)
  212. {
  213. unsigned int i;
  214. for (i = 0; i < 3; i++) {
  215. if (msc_read10(m, lba, count, buf) == 0)
  216. return 0;
  217. msc_sense(m);
  218. msc_reset(m);
  219. msc_delay(10);
  220. }
  221. return -1;
  222. }
  223. static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
  224. const void *buf)
  225. {
  226. unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  227. put32be(cb + 2, lba);
  228. put16be(cb + 7, count);
  229. return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
  230. }
  231. static int msc_sync_cache(struct usb_msc *m)
  232. {
  233. unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  234. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  235. }
  236. /* Find and configure a bulk-only mass-storage interface. */
  237. static int msc_config(struct usb_msc *m)
  238. {
  239. unsigned char hdr[9], cfg[256];
  240. unsigned int total, i, config = 0;
  241. unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
  242. unsigned int iface = 0;
  243. int found = 0;
  244. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  245. 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
  246. return -1;
  247. total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
  248. if (total < 9 || total > sizeof(cfg))
  249. return -1;
  250. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  251. 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
  252. return -1;
  253. config = cfg[5];
  254. if (!config)
  255. return -1;
  256. i = 0;
  257. while (i + 2 <= total) {
  258. unsigned int blen = cfg[i];
  259. unsigned int btype = cfg[i + 1];
  260. if (blen < 2 || i + blen > total)
  261. break;
  262. if (btype == 0x04 && blen >= 9) { /* interface */
  263. if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
  264. cfg[i + 7] == 0x50) {
  265. found = 1;
  266. iface = cfg[i + 2];
  267. ep_in = ep_out = 0;
  268. ep_in_mps = ep_out_mps = 0;
  269. } else {
  270. found = 0;
  271. }
  272. } else if (btype == 0x05 && blen >= 7 && found) {
  273. unsigned int epa = cfg[i + 2];
  274. unsigned int attr = cfg[i + 3] & 3u;
  275. unsigned int mps = ((unsigned int)cfg[i + 4] |
  276. ((unsigned int)cfg[i + 5] << 8)) &
  277. 0x7FFu;
  278. if (attr == 2) {
  279. if (epa & 0x80) {
  280. ep_in = epa & 0x0F;
  281. ep_in_mps = mps;
  282. } else {
  283. ep_out = epa & 0x0F;
  284. ep_out_mps = mps;
  285. }
  286. }
  287. }
  288. i += blen;
  289. }
  290. if (!found || !ep_in || !ep_out)
  291. return -1;
  292. if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  293. 0x00, 0x09, (unsigned short)config, 0) < 0)
  294. return -1;
  295. m->iface = iface;
  296. m->ep_in = ep_in;
  297. m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
  298. m->ep_out = ep_out;
  299. m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
  300. return 0;
  301. }
  302. static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
  303. {
  304. unsigned int i, n = 0;
  305. for (i = 0; i < 8; i++)
  306. m->model[n++] = (char)data[8 + i];
  307. m->model[n++] = ' ';
  308. for (i = 0; i < 16; i++)
  309. m->model[n++] = (char)data[16 + i];
  310. while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
  311. n--;
  312. m->model[n] = '\0';
  313. }
  314. static int device_probe(unsigned int ctrl, unsigned int addr,
  315. unsigned int mps, int low_speed, unsigned int port,
  316. unsigned int vendor, unsigned int product)
  317. {
  318. struct usb_msc *m = 0;
  319. unsigned char inquiry[36];
  320. unsigned int i, sectors;
  321. int slot = -1;
  322. (void)vendor;
  323. (void)product;
  324. /* A new bus scan re-enumerates every device with fresh addresses;
  325. * forget the old set on its first probe. */
  326. {
  327. unsigned int gen = usb_generation();
  328. if (gen != msc_generation) {
  329. for (i = 0; i < USB_MSC_MAX; i++)
  330. msc[i].present = 0;
  331. msc_generation = gen;
  332. }
  333. }
  334. for (i = 0; i < USB_MSC_MAX; i++)
  335. if (msc[i].present && msc[i].ctrl == ctrl &&
  336. msc[i].addr == addr) {
  337. slot = (int)i;
  338. break;
  339. }
  340. if (slot < 0)
  341. for (i = 0; i < USB_MSC_MAX; i++)
  342. if (!msc[i].present) {
  343. slot = (int)i;
  344. break;
  345. }
  346. if (slot < 0)
  347. return 0;
  348. m = &msc[(unsigned int)slot];
  349. m->present = 0;
  350. m->ctrl = ctrl;
  351. m->addr = addr;
  352. m->mps = mps;
  353. m->low = low_speed;
  354. m->port = port;
  355. m->tag = 0;
  356. m->iface = 0;
  357. if (msc_config(m) != 0)
  358. return 0;
  359. for (i = 0; i < 4; i++) {
  360. if (msc_test_unit_ready(m) == 0)
  361. break;
  362. msc_delay(50);
  363. msc_reset(m);
  364. }
  365. if (i == 4) {
  366. serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
  367. return 0;
  368. }
  369. if (msc_inquiry(m, inquiry) != 0) {
  370. serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
  371. msc_reset(m);
  372. if (msc_inquiry(m, inquiry) != 0)
  373. return 0;
  374. }
  375. if (msc_read_capacity(m, &sectors) != 0) {
  376. serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
  377. msc_reset(m);
  378. if (msc_read_capacity(m, &sectors) != 0)
  379. return 0;
  380. }
  381. if (sectors == 0)
  382. return 0;
  383. msc_set_model(m, inquiry);
  384. m->sectors = sectors;
  385. m->present = 1;
  386. serial_puts(SERIAL_COM1, "usbmsc: ");
  387. serial_puts(SERIAL_COM1, m->model);
  388. serial_puts(SERIAL_COM1, " ");
  389. serial_putdec(SERIAL_COM1, sectors);
  390. serial_puts(SERIAL_COM1, " sectors on ctrl ");
  391. serial_putdec(SERIAL_COM1, ctrl);
  392. serial_puts(SERIAL_COM1, " addr ");
  393. serial_putdec(SERIAL_COM1, addr);
  394. serial_puts(SERIAL_COM1, "\n");
  395. return 1;
  396. }
  397. void usbmsc_register(void)
  398. {
  399. unsigned int i;
  400. for (i = 0; i < USB_MSC_MAX; i++) {
  401. msc_names[i][0] = 'u';
  402. msc_names[i][1] = 's';
  403. msc_names[i][2] = 'b';
  404. msc_names[i][3] = (char)('0' + i);
  405. msc_names[i][4] = '\0';
  406. }
  407. usb_register_device_probe(device_probe);
  408. }
  409. void usbmsc_blk_scan(void)
  410. {
  411. unsigned int i;
  412. for (i = 0; i < USB_MSC_MAX; i++) {
  413. blk_dev_t *d;
  414. unsigned int j;
  415. if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
  416. continue;
  417. d = &blk_devs[blk_dev_count++];
  418. d->name = msc_names[i];
  419. d->kind = BLK_USB;
  420. d->bus = 0;
  421. d->unit = i;
  422. d->present = 1;
  423. d->total_sectors = msc[i].sectors;
  424. for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
  425. d->model[j] = msc[i].model[j];
  426. d->model[j] = '\0';
  427. }
  428. }
  429. static int msc_get(unsigned int unit, struct usb_msc **out)
  430. {
  431. if (unit >= USB_MSC_MAX || !msc[unit].present)
  432. return -1;
  433. *out = &msc[unit];
  434. return 0;
  435. }
  436. static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
  437. unsigned int count)
  438. {
  439. struct usb_msc *m;
  440. unsigned char *p = (unsigned char *)buf;
  441. if (msc_get(unit, &m) != 0)
  442. return -1;
  443. if (lba > m->sectors || count > m->sectors - lba)
  444. return -1;
  445. while (count) {
  446. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  447. if (msc_read10_retry(m, lba, n, p) != 0) {
  448. /* Some bridges/Sticks fail a large READ(10) but
  449. * accept the same range one sector at a time. This
  450. * also narrows a real medium error to one LBA. */
  451. if (n > 1) {
  452. unsigned int i;
  453. io_putstr("usbmsc: lba ");
  454. io_puthex(lba);
  455. io_putstr(" retrying 1-sector reads\n");
  456. for (i = 0; i < n; i++)
  457. if (msc_read10_retry(m, lba + i, 1,
  458. p + i * 512u) != 0) {
  459. io_putstr("usbmsc: sector ");
  460. io_puthex(lba + i);
  461. io_putstr(" failed, sense ");
  462. io_puthex((m->sense_key << 16) |
  463. (m->sense_asc << 8) |
  464. m->sense_ascq);
  465. io_putchar('\n');
  466. return -1;
  467. }
  468. } else {
  469. io_putstr("usbmsc: lba ");
  470. io_puthex(lba);
  471. io_putstr(" failed, sense ");
  472. io_puthex((m->sense_key << 16) |
  473. (m->sense_asc << 8) | m->sense_ascq);
  474. io_putchar('\n');
  475. return -1;
  476. }
  477. }
  478. lba += n;
  479. p += n * 512u;
  480. count -= n;
  481. }
  482. return 0;
  483. }
  484. static int msc_write_claimed(unsigned int unit, unsigned int lba,
  485. const void *buf, unsigned int count)
  486. {
  487. struct usb_msc *m;
  488. const unsigned char *p = (const unsigned char *)buf;
  489. if (msc_get(unit, &m) != 0)
  490. return -1;
  491. if (lba > m->sectors || count > m->sectors - lba)
  492. return -1;
  493. while (count) {
  494. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  495. if (msc_write10(m, lba, n, p) != 0) {
  496. msc_reset(m);
  497. if (msc_write10(m, lba, n, p) != 0)
  498. return -1;
  499. }
  500. lba += n;
  501. p += n * 512u;
  502. count -= n;
  503. }
  504. return 0;
  505. }
  506. static int msc_flush_claimed(unsigned int unit)
  507. {
  508. struct usb_msc *m;
  509. if (msc_get(unit, &m) != 0)
  510. return -1;
  511. if (msc_sync_cache(m) != 0) {
  512. msc_reset(m);
  513. if (msc_sync_cache(m) != 0)
  514. return -1;
  515. }
  516. return 0;
  517. }
  518. /* The block-layer entry points claim the bus for the whole command, so the
  519. * storage rescan may sleep in the HCD wait loops (dropping the BKL) while
  520. * no other context can start a transfer on the same controller. */
  521. int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
  522. unsigned int count)
  523. {
  524. int r;
  525. if (usb_bus_claim() != 0)
  526. return -1;
  527. r = msc_read_claimed(unit, lba, buf, count);
  528. usb_bus_release();
  529. return r;
  530. }
  531. int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
  532. unsigned int count)
  533. {
  534. int r;
  535. if (usb_bus_claim() != 0)
  536. return -1;
  537. r = msc_write_claimed(unit, lba, buf, count);
  538. usb_bus_release();
  539. return r;
  540. }
  541. int usbmsc_flush(unsigned int unit)
  542. {
  543. int r;
  544. if (usb_bus_claim() != 0)
  545. return -1;
  546. r = msc_flush_claimed(unit);
  547. usb_bus_release();
  548. return r;
  549. }