usbmsc.c 18 KB

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