usbmsc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. /* The first media access after enumeration can take seconds while the card
  25. * or stick finishes its internal initialisation. It is better to wait on
  26. * the outstanding READ(10) than to reset the pipe and restart that work. */
  27. #define MSC_FIRST_READ_MS 15000u
  28. /* Best-effort boot probe: one bounded attempt, then leave the device for a
  29. * later `storage` rescan. */
  30. #define MSC_PROBE_MS 5000u
  31. #define MSC_CBW_SIG 0x43425355u /* "USBC" LE */
  32. #define MSC_CSW_SIG 0x53425355u /* "USBS" LE */
  33. struct usb_msc {
  34. int present;
  35. unsigned int ctrl, addr, mps, port;
  36. int low;
  37. unsigned int ep_in, ep_in_mps; /* endpoint numbers (1..15) */
  38. unsigned int ep_out, ep_out_mps;
  39. unsigned int iface;
  40. unsigned int config; /* configuration value, for recovery */
  41. unsigned int timeout_ms; /* bulk-IN deadline */
  42. unsigned int first_read; /* media not read since the probe */
  43. int failed; /* last media read failed */
  44. unsigned int sectors;
  45. unsigned int tag;
  46. unsigned int last_status, last_residue;
  47. unsigned int sense_key, sense_asc, sense_ascq;
  48. char model[41];
  49. };
  50. static struct usb_msc msc[USB_MSC_MAX];
  51. static char msc_names[USB_MSC_MAX][6];
  52. static unsigned int msc_generation;
  53. static int msc_probe_only;
  54. void usbmsc_set_probe_only(int on)
  55. {
  56. msc_probe_only = on;
  57. }
  58. void usbmsc_begin_full_scan(void)
  59. {
  60. unsigned int i;
  61. for (i = 0; i < USB_MSC_MAX; i++)
  62. if (msc[i].present) {
  63. msc[i].first_read = 1;
  64. msc[i].failed = 0;
  65. }
  66. }
  67. int usbmsc_slot_failed(unsigned int unit)
  68. {
  69. return unit < USB_MSC_MAX && msc[unit].present && msc[unit].failed;
  70. }
  71. static unsigned int rd32le(const unsigned char *p)
  72. {
  73. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  74. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  75. }
  76. static unsigned int rd32be(const unsigned char *p)
  77. {
  78. return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
  79. ((unsigned int)p[2] << 8) | (unsigned int)p[3];
  80. }
  81. static void put32le(unsigned char *p, unsigned int v)
  82. {
  83. p[0] = (unsigned char)(v & 0xFF);
  84. p[1] = (unsigned char)((v >> 8) & 0xFF);
  85. p[2] = (unsigned char)((v >> 16) & 0xFF);
  86. p[3] = (unsigned char)((v >> 24) & 0xFF);
  87. }
  88. static void put16be(unsigned char *p, unsigned int v)
  89. {
  90. p[0] = (unsigned char)((v >> 8) & 0xFF);
  91. p[1] = (unsigned char)(v & 0xFF);
  92. }
  93. static void put32be(unsigned char *p, unsigned int v)
  94. {
  95. p[0] = (unsigned char)((v >> 24) & 0xFF);
  96. p[1] = (unsigned char)((v >> 16) & 0xFF);
  97. p[2] = (unsigned char)((v >> 8) & 0xFF);
  98. p[3] = (unsigned char)(v & 0xFF);
  99. }
  100. /* Sleeps inside a bus scan or a claimed storage-rescan command (see
  101. * usb_delay_ms); busy-waits everywhere else. */
  102. static void msc_delay(unsigned int ms)
  103. {
  104. usb_delay_ms(ms);
  105. }
  106. /* Bulk IN with a deadline. Transfers larger than the host controller's
  107. * internal bulk-IN buffer are split; EHCI's is 2048 bytes. */
  108. static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
  109. {
  110. unsigned int got = 0, t0 = get_ticks_ms();
  111. while (got < len) {
  112. unsigned int want = len - got;
  113. int n;
  114. if (want > MSC_BULK_CHUNK)
  115. want = MSC_BULK_CHUNK;
  116. n = usb_bulk_in(m->ctrl, m->addr, m->ep_in, m->ep_in_mps,
  117. buf + got, want);
  118. if (n < 0)
  119. return -1;
  120. if (n > 0) {
  121. /* A short packet is normal inside a BOT data stage:
  122. * the device may split it at any USB transfer
  123. * boundary. Keep reading until the Stage's known
  124. * byte count is reached. */
  125. got += (unsigned int)n;
  126. t0 = get_ticks_ms();
  127. } else if (get_ticks_ms() - t0 > m->timeout_ms) {
  128. /* A timed-out bulk-IN qTD stays armed in the HCD
  129. * schedule; cancel it or every later transfer sees
  130. * the endpoint as permanently busy. */
  131. usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
  132. return -1;
  133. } else {
  134. usb_poll_relax(t0);
  135. }
  136. }
  137. return 0;
  138. }
  139. static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
  140. unsigned int len)
  141. {
  142. if (usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
  143. buf, len) != 0) {
  144. usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. /* BOT reset + clear both bulk endpoint halts. */
  150. static void msc_reset(struct usb_msc *m)
  151. {
  152. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  153. 0x21, 0xFF, 0, (unsigned short)m->iface);
  154. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  155. 0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
  156. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  157. 0x02, 0x01, 0, (unsigned short)m->ep_out);
  158. /* CLEAR_FEATURE resets the device's endpoint toggle to DATA0; the
  159. * host side must do the same or the next CBW/data stage is sent with
  160. * the wrong toggle (real EHCI hardware rejects it, QEMU does not). */
  161. usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
  162. usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
  163. m->tag++;
  164. }
  165. /* Last-resort recovery for a device whose BOT reset did not restore the
  166. * pipes: re-apply the configuration. That resets the device's endpoints
  167. * (and their data toggles) to DATA0 without a port reset, so the address
  168. * and the driver's endpoint numbers stay valid. */
  169. static void msc_reconfigure(struct usb_msc *m)
  170. {
  171. if (!m->config)
  172. return;
  173. usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  174. 0x00, 0x09, (unsigned short)m->config, 0);
  175. usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1);
  176. usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0);
  177. }
  178. static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
  179. unsigned int cb_len, void *data, unsigned int data_len,
  180. int dir_in)
  181. {
  182. unsigned char cbw[31];
  183. unsigned char csw[13];
  184. unsigned int tag = ++m->tag;
  185. for (unsigned int i = 0; i < sizeof(cbw); i++)
  186. cbw[i] = 0;
  187. put32le(cbw, MSC_CBW_SIG);
  188. put32le(cbw + 4, tag);
  189. put32le(cbw + 8, data_len);
  190. cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00);
  191. cbw[13] = 0; /* LUN 0 */
  192. cbw[14] = (unsigned char)cb_len;
  193. for (unsigned int i = 0; i < cb_len && i < 16; i++)
  194. cbw[15 + i] = cb[i];
  195. if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0)
  196. return -1;
  197. if (data_len) {
  198. if (dir_in) {
  199. if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0)
  200. return -1;
  201. } else if (msc_bulk_out(m, (const unsigned char *)data,
  202. data_len) != 0) {
  203. return -1;
  204. }
  205. }
  206. if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
  207. return -1;
  208. if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag) {
  209. m->last_status = 0xFF;
  210. return -1;
  211. }
  212. m->last_status = csw[12];
  213. m->last_residue = rd32le(csw + 8);
  214. if (csw[12] != 0) {
  215. /* A command that completed with CHECK CONDITION (or another
  216. * SCSI status) has valid sense data; a transport failure does
  217. * not. -2 tells the recovery path to ask for the sense
  218. * before resetting the pipe. */
  219. return -2;
  220. }
  221. if (m->last_residue != 0)
  222. return -1;
  223. return 0;
  224. }
  225. /* Fetch the sense data after a failed command so the next CBW does not keep
  226. * returning the same unit attention/medium error. */
  227. static int msc_sense(struct usb_msc *m)
  228. {
  229. unsigned char cb[6] = { 0x03, 0, 0, 0, 18, 0 };
  230. unsigned char data[18];
  231. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  232. return -1;
  233. m->sense_key = data[2] & 0x0F;
  234. m->sense_asc = data[12];
  235. m->sense_ascq = data[13];
  236. return 0;
  237. }
  238. static int msc_test_unit_ready(struct usb_msc *m)
  239. {
  240. unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 };
  241. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  242. }
  243. static int msc_inquiry(struct usb_msc *m, unsigned char data[36])
  244. {
  245. unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 };
  246. return bot_cmd(m, cb, sizeof(cb), data, 36, 1);
  247. }
  248. static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors)
  249. {
  250. unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  251. unsigned char data[8];
  252. unsigned int last, block;
  253. if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
  254. return -1;
  255. last = rd32be(data);
  256. block = rd32be(data + 4);
  257. if (block != 512 || last == 0xFFFFFFFFu)
  258. return -1;
  259. *sectors = last + 1;
  260. return 0;
  261. }
  262. static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
  263. void *buf)
  264. {
  265. unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  266. put32be(cb + 2, lba);
  267. put16be(cb + 7, count);
  268. return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
  269. }
  270. static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
  271. unsigned int count, void *buf)
  272. {
  273. unsigned int i, t0 = get_ticks_ms();
  274. int probe_only = msc_probe_only;
  275. for (i = 0; i < 3; i++) {
  276. int r;
  277. if (i == 0 && (m->first_read || probe_only))
  278. m->timeout_ms = probe_only ? MSC_PROBE_MS :
  279. MSC_FIRST_READ_MS;
  280. r = msc_read10(m, lba, count, buf);
  281. if (i == 0 && m->first_read) {
  282. unsigned int elapsed = get_ticks_ms() - t0;
  283. m->first_read = 0;
  284. m->timeout_ms = MSC_TIMEOUT_MS;
  285. if (r == 0 && elapsed > 1000) {
  286. serial_puts(SERIAL_COM1,
  287. "usbmsc: first read ");
  288. serial_putdec(SERIAL_COM1, elapsed);
  289. serial_puts(SERIAL_COM1, " ms\n");
  290. }
  291. }
  292. if (r == 0) {
  293. if (probe_only)
  294. m->timeout_ms = MSC_TIMEOUT_MS;
  295. return 0;
  296. }
  297. if (probe_only) {
  298. /* Best-effort probe: do not spend the reset and retry
  299. * chain on a device that did not answer. `storage`
  300. * will try the full path. */
  301. m->timeout_ms = MSC_TIMEOUT_MS;
  302. return -1;
  303. }
  304. /* Only a completed command with a non-zero CSW status has
  305. * sense data. After a timeout the endpoint is in an
  306. * unknown state, and a REQUEST SENSE would just add another
  307. * multi-second timeout before the reset. */
  308. if (r == -2)
  309. msc_sense(m);
  310. msc_reset(m);
  311. /* If the BOT reset alone did not restore the pipes, reset the
  312. * device configuration once before the next retry. */
  313. if (i == 0)
  314. msc_reconfigure(m);
  315. msc_delay(10);
  316. }
  317. if (probe_only)
  318. m->timeout_ms = MSC_TIMEOUT_MS;
  319. return -1;
  320. }
  321. static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
  322. const void *buf)
  323. {
  324. unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  325. put32be(cb + 2, lba);
  326. put16be(cb + 7, count);
  327. return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
  328. }
  329. static int msc_sync_cache(struct usb_msc *m)
  330. {
  331. unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  332. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  333. }
  334. /* Find and configure a bulk-only mass-storage interface. */
  335. static int msc_config(struct usb_msc *m)
  336. {
  337. unsigned char hdr[9], cfg[256];
  338. unsigned int total, i, config = 0;
  339. unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
  340. unsigned int iface = 0;
  341. int found = 0;
  342. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  343. 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
  344. return -1;
  345. total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
  346. if (total < 9 || total > sizeof(cfg))
  347. return -1;
  348. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  349. 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
  350. return -1;
  351. config = cfg[5];
  352. if (!config)
  353. return -1;
  354. i = 0;
  355. while (i + 2 <= total) {
  356. unsigned int blen = cfg[i];
  357. unsigned int btype = cfg[i + 1];
  358. if (blen < 2 || i + blen > total)
  359. break;
  360. if (btype == 0x04 && blen >= 9) { /* interface */
  361. if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
  362. cfg[i + 7] == 0x50) {
  363. found = 1;
  364. iface = cfg[i + 2];
  365. ep_in = ep_out = 0;
  366. ep_in_mps = ep_out_mps = 0;
  367. } else {
  368. found = 0;
  369. }
  370. } else if (btype == 0x05 && blen >= 7 && found) {
  371. unsigned int epa = cfg[i + 2];
  372. unsigned int attr = cfg[i + 3] & 3u;
  373. unsigned int mps = ((unsigned int)cfg[i + 4] |
  374. ((unsigned int)cfg[i + 5] << 8)) &
  375. 0x7FFu;
  376. if (attr == 2) {
  377. if (epa & 0x80) {
  378. ep_in = epa & 0x0F;
  379. ep_in_mps = mps;
  380. } else {
  381. ep_out = epa & 0x0F;
  382. ep_out_mps = mps;
  383. }
  384. }
  385. }
  386. i += blen;
  387. }
  388. if (!found || !ep_in || !ep_out)
  389. return -1;
  390. if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  391. 0x00, 0x09, (unsigned short)config, 0) < 0)
  392. return -1;
  393. m->iface = iface;
  394. m->config = config;
  395. m->ep_in = ep_in;
  396. m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
  397. m->ep_out = ep_out;
  398. m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
  399. return 0;
  400. }
  401. static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
  402. {
  403. unsigned int i, n = 0;
  404. for (i = 0; i < 8; i++)
  405. m->model[n++] = (char)data[8 + i];
  406. m->model[n++] = ' ';
  407. for (i = 0; i < 16; i++)
  408. m->model[n++] = (char)data[16 + i];
  409. while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
  410. n--;
  411. m->model[n] = '\0';
  412. }
  413. static int device_probe(unsigned int ctrl, unsigned int addr,
  414. unsigned int mps, int low_speed, unsigned int port,
  415. unsigned int vendor, unsigned int product)
  416. {
  417. struct usb_msc *m = 0;
  418. unsigned char inquiry[36];
  419. unsigned int i, sectors;
  420. int slot = -1;
  421. (void)vendor;
  422. (void)product;
  423. /* A new bus scan re-enumerates every device with fresh addresses;
  424. * forget the old set on its first probe. */
  425. {
  426. unsigned int gen = usb_generation();
  427. if (gen != msc_generation) {
  428. for (i = 0; i < USB_MSC_MAX; i++)
  429. msc[i].present = 0;
  430. msc_generation = gen;
  431. }
  432. }
  433. for (i = 0; i < USB_MSC_MAX; i++)
  434. if (msc[i].present && msc[i].ctrl == ctrl &&
  435. msc[i].addr == addr) {
  436. slot = (int)i;
  437. break;
  438. }
  439. if (slot < 0)
  440. for (i = 0; i < USB_MSC_MAX; i++)
  441. if (!msc[i].present) {
  442. slot = (int)i;
  443. break;
  444. }
  445. if (slot < 0)
  446. return 0;
  447. m = &msc[(unsigned int)slot];
  448. m->present = 0;
  449. m->ctrl = ctrl;
  450. m->addr = addr;
  451. m->mps = mps;
  452. m->low = low_speed;
  453. m->port = port;
  454. m->tag = 0;
  455. m->iface = 0;
  456. m->config = 0;
  457. m->timeout_ms = MSC_TIMEOUT_MS;
  458. m->first_read = 0;
  459. m->failed = 0;
  460. if (msc_config(m) != 0)
  461. return 0;
  462. for (i = 0; i < 4; i++) {
  463. if (msc_test_unit_ready(m) == 0)
  464. break;
  465. msc_delay(50);
  466. msc_reset(m);
  467. }
  468. if (i == 4) {
  469. serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
  470. return 0;
  471. }
  472. if (msc_inquiry(m, inquiry) != 0) {
  473. serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
  474. msc_reset(m);
  475. if (msc_inquiry(m, inquiry) != 0)
  476. return 0;
  477. }
  478. if (msc_read_capacity(m, &sectors) != 0) {
  479. serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
  480. msc_reset(m);
  481. if (msc_read_capacity(m, &sectors) != 0)
  482. return 0;
  483. }
  484. if (sectors == 0)
  485. return 0;
  486. msc_set_model(m, inquiry);
  487. m->sectors = sectors;
  488. m->timeout_ms = MSC_TIMEOUT_MS;
  489. m->first_read = 1;
  490. m->present = 1;
  491. serial_puts(SERIAL_COM1, "usbmsc: ");
  492. serial_puts(SERIAL_COM1, m->model);
  493. serial_puts(SERIAL_COM1, " ");
  494. serial_putdec(SERIAL_COM1, sectors);
  495. serial_puts(SERIAL_COM1, " sectors on ctrl ");
  496. serial_putdec(SERIAL_COM1, ctrl);
  497. serial_puts(SERIAL_COM1, " addr ");
  498. serial_putdec(SERIAL_COM1, addr);
  499. serial_puts(SERIAL_COM1, "\n");
  500. return 1;
  501. }
  502. void usbmsc_register(void)
  503. {
  504. unsigned int i;
  505. for (i = 0; i < USB_MSC_MAX; i++) {
  506. msc_names[i][0] = 'u';
  507. msc_names[i][1] = 's';
  508. msc_names[i][2] = 'b';
  509. msc_names[i][3] = (char)('0' + i);
  510. msc_names[i][4] = '\0';
  511. }
  512. usb_register_device_probe(device_probe);
  513. }
  514. void usbmsc_blk_scan(void)
  515. {
  516. unsigned int i;
  517. for (i = 0; i < USB_MSC_MAX; i++) {
  518. blk_dev_t *d;
  519. unsigned int j;
  520. if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
  521. continue;
  522. d = &blk_devs[blk_dev_count++];
  523. d->name = msc_names[i];
  524. d->kind = BLK_USB;
  525. d->bus = 0;
  526. d->unit = i;
  527. d->present = 1;
  528. d->total_sectors = msc[i].sectors;
  529. for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
  530. d->model[j] = msc[i].model[j];
  531. d->model[j] = '\0';
  532. }
  533. }
  534. static int msc_get(unsigned int unit, struct usb_msc **out)
  535. {
  536. if (unit >= USB_MSC_MAX || !msc[unit].present)
  537. return -1;
  538. *out = &msc[unit];
  539. return 0;
  540. }
  541. static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
  542. unsigned int count)
  543. {
  544. struct usb_msc *m;
  545. unsigned char *p = (unsigned char *)buf;
  546. if (msc_get(unit, &m) != 0)
  547. return -1;
  548. if (lba > m->sectors || count > m->sectors - lba)
  549. return -1;
  550. while (count) {
  551. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  552. if (msc_read10_retry(m, lba, n, p) != 0) {
  553. /* Some bridges/Sticks fail a large READ(10) but
  554. * accept the same range one sector at a time. This
  555. * also narrows a real medium error to one LBA. */
  556. if (n > 1) {
  557. unsigned int i;
  558. io_putstr("usbmsc: lba ");
  559. io_puthex(lba);
  560. io_putstr(" retrying 1-sector reads\n");
  561. for (i = 0; i < n; i++)
  562. if (msc_read10_retry(m, lba + i, 1,
  563. p + i * 512u) != 0) {
  564. io_putstr("usbmsc: sector ");
  565. io_puthex(lba + i);
  566. io_putstr(" failed, sense ");
  567. io_puthex((m->sense_key << 16) |
  568. (m->sense_asc << 8) |
  569. m->sense_ascq);
  570. io_putchar('\n');
  571. m->failed = 1;
  572. return -1;
  573. }
  574. } else {
  575. io_putstr("usbmsc: lba ");
  576. io_puthex(lba);
  577. io_putstr(" failed, sense ");
  578. io_puthex((m->sense_key << 16) |
  579. (m->sense_asc << 8) | m->sense_ascq);
  580. io_putchar('\n');
  581. m->failed = 1;
  582. return -1;
  583. }
  584. }
  585. lba += n;
  586. p += n * 512u;
  587. count -= n;
  588. }
  589. m->failed = 0;
  590. return 0;
  591. }
  592. static int msc_write_claimed(unsigned int unit, unsigned int lba,
  593. const void *buf, unsigned int count)
  594. {
  595. struct usb_msc *m;
  596. const unsigned char *p = (const unsigned char *)buf;
  597. if (msc_get(unit, &m) != 0)
  598. return -1;
  599. if (lba > m->sectors || count > m->sectors - lba)
  600. return -1;
  601. while (count) {
  602. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  603. if (msc_write10(m, lba, n, p) != 0) {
  604. msc_reset(m);
  605. if (msc_write10(m, lba, n, p) != 0)
  606. return -1;
  607. }
  608. lba += n;
  609. p += n * 512u;
  610. count -= n;
  611. }
  612. return 0;
  613. }
  614. static int msc_flush_claimed(unsigned int unit)
  615. {
  616. struct usb_msc *m;
  617. if (msc_get(unit, &m) != 0)
  618. return -1;
  619. if (msc_sync_cache(m) != 0) {
  620. msc_reset(m);
  621. if (msc_sync_cache(m) != 0)
  622. return -1;
  623. }
  624. return 0;
  625. }
  626. /* The block-layer entry points claim the bus for the whole command, so the
  627. * storage rescan may sleep in the HCD wait loops (dropping the BKL) while
  628. * no other context can start a transfer on the same controller. */
  629. int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
  630. unsigned int count)
  631. {
  632. int r;
  633. if (usb_bus_claim() != 0)
  634. return -1;
  635. r = msc_read_claimed(unit, lba, buf, count);
  636. usb_bus_release();
  637. return r;
  638. }
  639. int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
  640. unsigned int count)
  641. {
  642. int r;
  643. if (usb_bus_claim() != 0)
  644. return -1;
  645. r = msc_write_claimed(unit, lba, buf, count);
  646. usb_bus_release();
  647. return r;
  648. }
  649. int usbmsc_flush(unsigned int unit)
  650. {
  651. int r;
  652. if (usb_bus_claim() != 0)
  653. return -1;
  654. r = msc_flush_claimed(unit);
  655. usb_bus_release();
  656. return r;
  657. }