usbmsc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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;
  268. for (i = 0; i < 3; i++) {
  269. int r;
  270. if (i == 0 && (m->first_read || probe_only))
  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. if (probe_only)
  287. m->timeout_ms = MSC_TIMEOUT_MS;
  288. return 0;
  289. }
  290. if (probe_only) {
  291. /* Best-effort probe: do not spend the reset and retry
  292. * chain on a device that did not answer. `storage`
  293. * will try the full path. */
  294. m->timeout_ms = MSC_TIMEOUT_MS;
  295. return -1;
  296. }
  297. /* Only a completed command with a non-zero CSW status has
  298. * sense data. After a timeout the endpoint is in an
  299. * unknown state, and a REQUEST SENSE would just add another
  300. * multi-second timeout before the reset. */
  301. if (r == -2)
  302. msc_sense(m);
  303. msc_reset(m);
  304. /* If the BOT reset alone did not restore the pipes, reset the
  305. * device configuration once before the next retry. */
  306. if (i == 0)
  307. msc_reconfigure(m);
  308. msc_delay(10);
  309. }
  310. if (probe_only)
  311. m->timeout_ms = MSC_TIMEOUT_MS;
  312. return -1;
  313. }
  314. static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
  315. const void *buf)
  316. {
  317. unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  318. put32be(cb + 2, lba);
  319. put16be(cb + 7, count);
  320. return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
  321. }
  322. static int msc_sync_cache(struct usb_msc *m)
  323. {
  324. unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  325. return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
  326. }
  327. /* Find and configure a bulk-only mass-storage interface. */
  328. static int msc_config(struct usb_msc *m)
  329. {
  330. unsigned char hdr[9], cfg[256];
  331. unsigned int total, i, config = 0;
  332. unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
  333. unsigned int iface = 0;
  334. int found = 0;
  335. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  336. 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
  337. return -1;
  338. total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
  339. if (total < 9 || total > sizeof(cfg))
  340. return -1;
  341. if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
  342. 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
  343. return -1;
  344. config = cfg[5];
  345. if (!config)
  346. return -1;
  347. i = 0;
  348. while (i + 2 <= total) {
  349. unsigned int blen = cfg[i];
  350. unsigned int btype = cfg[i + 1];
  351. if (blen < 2 || i + blen > total)
  352. break;
  353. if (btype == 0x04 && blen >= 9) { /* interface */
  354. if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
  355. cfg[i + 7] == 0x50) {
  356. found = 1;
  357. iface = cfg[i + 2];
  358. ep_in = ep_out = 0;
  359. ep_in_mps = ep_out_mps = 0;
  360. } else {
  361. found = 0;
  362. }
  363. } else if (btype == 0x05 && blen >= 7 && found) {
  364. unsigned int epa = cfg[i + 2];
  365. unsigned int attr = cfg[i + 3] & 3u;
  366. unsigned int mps = ((unsigned int)cfg[i + 4] |
  367. ((unsigned int)cfg[i + 5] << 8)) &
  368. 0x7FFu;
  369. if (attr == 2) {
  370. if (epa & 0x80) {
  371. ep_in = epa & 0x0F;
  372. ep_in_mps = mps;
  373. } else {
  374. ep_out = epa & 0x0F;
  375. ep_out_mps = mps;
  376. }
  377. }
  378. }
  379. i += blen;
  380. }
  381. if (!found || !ep_in || !ep_out)
  382. return -1;
  383. if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
  384. 0x00, 0x09, (unsigned short)config, 0) < 0)
  385. return -1;
  386. m->iface = iface;
  387. m->config = config;
  388. m->ep_in = ep_in;
  389. m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
  390. m->ep_out = ep_out;
  391. m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
  392. return 0;
  393. }
  394. static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
  395. {
  396. unsigned int i, n = 0;
  397. for (i = 0; i < 8; i++)
  398. m->model[n++] = (char)data[8 + i];
  399. m->model[n++] = ' ';
  400. for (i = 0; i < 16; i++)
  401. m->model[n++] = (char)data[16 + i];
  402. while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
  403. n--;
  404. m->model[n] = '\0';
  405. }
  406. static int device_probe(unsigned int ctrl, unsigned int addr,
  407. unsigned int mps, int low_speed, unsigned int port,
  408. unsigned int vendor, unsigned int product)
  409. {
  410. struct usb_msc *m = 0;
  411. unsigned char inquiry[36];
  412. unsigned int i, sectors;
  413. int slot = -1;
  414. (void)vendor;
  415. (void)product;
  416. /* A new bus scan re-enumerates every device with fresh addresses;
  417. * forget the old set on its first probe. */
  418. {
  419. unsigned int gen = usb_generation();
  420. if (gen != msc_generation) {
  421. for (i = 0; i < USB_MSC_MAX; i++)
  422. msc[i].present = 0;
  423. msc_generation = gen;
  424. }
  425. }
  426. for (i = 0; i < USB_MSC_MAX; i++)
  427. if (msc[i].present && msc[i].ctrl == ctrl &&
  428. msc[i].addr == addr) {
  429. slot = (int)i;
  430. break;
  431. }
  432. if (slot < 0)
  433. for (i = 0; i < USB_MSC_MAX; i++)
  434. if (!msc[i].present) {
  435. slot = (int)i;
  436. break;
  437. }
  438. if (slot < 0)
  439. return 0;
  440. m = &msc[(unsigned int)slot];
  441. m->present = 0;
  442. m->ctrl = ctrl;
  443. m->addr = addr;
  444. m->mps = mps;
  445. m->low = low_speed;
  446. m->port = port;
  447. m->tag = 0;
  448. m->iface = 0;
  449. m->config = 0;
  450. m->timeout_ms = MSC_TIMEOUT_MS;
  451. m->first_read = 0;
  452. if (msc_config(m) != 0)
  453. return 0;
  454. for (i = 0; i < 4; i++) {
  455. if (msc_test_unit_ready(m) == 0)
  456. break;
  457. msc_delay(50);
  458. msc_reset(m);
  459. }
  460. if (i == 4) {
  461. serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
  462. return 0;
  463. }
  464. if (msc_inquiry(m, inquiry) != 0) {
  465. serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
  466. msc_reset(m);
  467. if (msc_inquiry(m, inquiry) != 0)
  468. return 0;
  469. }
  470. if (msc_read_capacity(m, &sectors) != 0) {
  471. serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
  472. msc_reset(m);
  473. if (msc_read_capacity(m, &sectors) != 0)
  474. return 0;
  475. }
  476. if (sectors == 0)
  477. return 0;
  478. msc_set_model(m, inquiry);
  479. m->sectors = sectors;
  480. m->timeout_ms = MSC_TIMEOUT_MS;
  481. m->first_read = 1;
  482. m->present = 1;
  483. serial_puts(SERIAL_COM1, "usbmsc: ");
  484. serial_puts(SERIAL_COM1, m->model);
  485. serial_puts(SERIAL_COM1, " ");
  486. serial_putdec(SERIAL_COM1, sectors);
  487. serial_puts(SERIAL_COM1, " sectors on ctrl ");
  488. serial_putdec(SERIAL_COM1, ctrl);
  489. serial_puts(SERIAL_COM1, " addr ");
  490. serial_putdec(SERIAL_COM1, addr);
  491. serial_puts(SERIAL_COM1, "\n");
  492. return 1;
  493. }
  494. void usbmsc_register(void)
  495. {
  496. unsigned int i;
  497. for (i = 0; i < USB_MSC_MAX; i++) {
  498. msc_names[i][0] = 'u';
  499. msc_names[i][1] = 's';
  500. msc_names[i][2] = 'b';
  501. msc_names[i][3] = (char)('0' + i);
  502. msc_names[i][4] = '\0';
  503. }
  504. usb_register_device_probe(device_probe);
  505. }
  506. void usbmsc_blk_scan(void)
  507. {
  508. unsigned int i;
  509. for (i = 0; i < USB_MSC_MAX; i++) {
  510. blk_dev_t *d;
  511. unsigned int j;
  512. if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
  513. continue;
  514. d = &blk_devs[blk_dev_count++];
  515. d->name = msc_names[i];
  516. d->kind = BLK_USB;
  517. d->bus = 0;
  518. d->unit = i;
  519. d->present = 1;
  520. d->total_sectors = msc[i].sectors;
  521. for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
  522. d->model[j] = msc[i].model[j];
  523. d->model[j] = '\0';
  524. }
  525. }
  526. static int msc_get(unsigned int unit, struct usb_msc **out)
  527. {
  528. if (unit >= USB_MSC_MAX || !msc[unit].present)
  529. return -1;
  530. *out = &msc[unit];
  531. return 0;
  532. }
  533. static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf,
  534. unsigned int count)
  535. {
  536. struct usb_msc *m;
  537. unsigned char *p = (unsigned char *)buf;
  538. if (msc_get(unit, &m) != 0)
  539. return -1;
  540. if (lba > m->sectors || count > m->sectors - lba)
  541. return -1;
  542. while (count) {
  543. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  544. if (msc_read10_retry(m, lba, n, p) != 0) {
  545. /* Some bridges/Sticks fail a large READ(10) but
  546. * accept the same range one sector at a time. This
  547. * also narrows a real medium error to one LBA. */
  548. if (n > 1) {
  549. unsigned int i;
  550. io_putstr("usbmsc: lba ");
  551. io_puthex(lba);
  552. io_putstr(" retrying 1-sector reads\n");
  553. for (i = 0; i < n; i++)
  554. if (msc_read10_retry(m, lba + i, 1,
  555. p + i * 512u) != 0) {
  556. io_putstr("usbmsc: sector ");
  557. io_puthex(lba + i);
  558. io_putstr(" failed, sense ");
  559. io_puthex((m->sense_key << 16) |
  560. (m->sense_asc << 8) |
  561. m->sense_ascq);
  562. io_putchar('\n');
  563. return -1;
  564. }
  565. } else {
  566. io_putstr("usbmsc: lba ");
  567. io_puthex(lba);
  568. io_putstr(" failed, sense ");
  569. io_puthex((m->sense_key << 16) |
  570. (m->sense_asc << 8) | m->sense_ascq);
  571. io_putchar('\n');
  572. return -1;
  573. }
  574. }
  575. lba += n;
  576. p += n * 512u;
  577. count -= n;
  578. }
  579. return 0;
  580. }
  581. static int msc_write_claimed(unsigned int unit, unsigned int lba,
  582. const void *buf, unsigned int count)
  583. {
  584. struct usb_msc *m;
  585. const unsigned char *p = (const unsigned char *)buf;
  586. if (msc_get(unit, &m) != 0)
  587. return -1;
  588. if (lba > m->sectors || count > m->sectors - lba)
  589. return -1;
  590. while (count) {
  591. unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
  592. if (msc_write10(m, lba, n, p) != 0) {
  593. msc_reset(m);
  594. if (msc_write10(m, lba, n, p) != 0)
  595. return -1;
  596. }
  597. lba += n;
  598. p += n * 512u;
  599. count -= n;
  600. }
  601. return 0;
  602. }
  603. static int msc_flush_claimed(unsigned int unit)
  604. {
  605. struct usb_msc *m;
  606. if (msc_get(unit, &m) != 0)
  607. return -1;
  608. if (msc_sync_cache(m) != 0) {
  609. msc_reset(m);
  610. if (msc_sync_cache(m) != 0)
  611. return -1;
  612. }
  613. return 0;
  614. }
  615. /* The block-layer entry points claim the bus for the whole command, so the
  616. * storage rescan may sleep in the HCD wait loops (dropping the BKL) while
  617. * no other context can start a transfer on the same controller. */
  618. int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
  619. unsigned int count)
  620. {
  621. int r;
  622. if (usb_bus_claim() != 0)
  623. return -1;
  624. r = msc_read_claimed(unit, lba, buf, count);
  625. usb_bus_release();
  626. return r;
  627. }
  628. int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
  629. unsigned int count)
  630. {
  631. int r;
  632. if (usb_bus_claim() != 0)
  633. return -1;
  634. r = msc_write_claimed(unit, lba, buf, count);
  635. usb_bus_release();
  636. return r;
  637. }
  638. int usbmsc_flush(unsigned int unit)
  639. {
  640. int r;
  641. if (usb_bus_claim() != 0)
  642. return -1;
  643. r = msc_flush_claimed(unit);
  644. usb_bus_release();
  645. return r;
  646. }