usbmsc.c 12 KB

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