/* USB mass-storage class driver: Bulk-Only Transport (BOT) + SCSI. * * The USB core's device probe sees every enumerated device; this driver * parses the configuration for an interface with class 8 / subclass 6 / * protocol 0x50 (SCSI transparent, bulk-only), claims its bulk IN/OUT * endpoints and exposes the disk as `usb0..` once usbmsc_blk_scan() runs. * * Only EHCI and xHCI implement the bulk-transfer ops; UHCI/OHCI devices are * left unclaimed (a full-speed stick on a companion controller cannot be * used). The block layer calls usbmsc_read/write/flush from blkdev.c. */ #include "usbmsc.h" #include "blkdev.h" #include "io.h" #include "serial.h" #include "timer.h" #include "usb.h" #define USB_MSC_MAX 4 /* Keep one SCSI data stage below EHCI's 0x7FFF-byte bulk-OUT limit: * 62 sectors = 31,744 bytes. */ #define MSC_CHUNK_SECTORS 62u #define MSC_BULK_CHUNK 2048u /* EHCI's fixed bulk-IN buffer */ #define MSC_TIMEOUT_MS 3000u /* The first media access after enumeration can take seconds while the card * or stick finishes its internal initialisation. It is better to wait on * the outstanding READ(10) than to reset the pipe and restart that work. */ #define MSC_FIRST_READ_MS 15000u /* Best-effort boot probe: one bounded attempt, then leave the device for a * later `storage` rescan. */ #define MSC_PROBE_MS 5000u #define MSC_CBW_SIG 0x43425355u /* "USBC" LE */ #define MSC_CSW_SIG 0x53425355u /* "USBS" LE */ struct usb_msc { int present; unsigned int ctrl, addr, mps, port; int low; unsigned int ep_in, ep_in_mps; /* endpoint numbers (1..15) */ unsigned int ep_out, ep_out_mps; unsigned int iface; unsigned int config; /* configuration value, for recovery */ unsigned int timeout_ms; /* bulk-IN deadline */ unsigned int first_read; /* media not read since the probe */ unsigned int sectors; unsigned int tag; unsigned int last_status, last_residue; unsigned int sense_key, sense_asc, sense_ascq; char model[41]; }; static struct usb_msc msc[USB_MSC_MAX]; static char msc_names[USB_MSC_MAX][6]; static unsigned int msc_generation; static int msc_probe_only; void usbmsc_set_probe_only(int on) { msc_probe_only = on; } void usbmsc_begin_full_scan(void) { unsigned int i; for (i = 0; i < USB_MSC_MAX; i++) if (msc[i].present) msc[i].first_read = 1; } static unsigned int rd32le(const unsigned char *p) { return (unsigned int)p[0] | ((unsigned int)p[1] << 8) | ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24); } static unsigned int rd32be(const unsigned char *p) { return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) | ((unsigned int)p[2] << 8) | (unsigned int)p[3]; } static void put32le(unsigned char *p, unsigned int v) { p[0] = (unsigned char)(v & 0xFF); p[1] = (unsigned char)((v >> 8) & 0xFF); p[2] = (unsigned char)((v >> 16) & 0xFF); p[3] = (unsigned char)((v >> 24) & 0xFF); } static void put16be(unsigned char *p, unsigned int v) { p[0] = (unsigned char)((v >> 8) & 0xFF); p[1] = (unsigned char)(v & 0xFF); } static void put32be(unsigned char *p, unsigned int v) { p[0] = (unsigned char)((v >> 24) & 0xFF); p[1] = (unsigned char)((v >> 16) & 0xFF); p[2] = (unsigned char)((v >> 8) & 0xFF); p[3] = (unsigned char)(v & 0xFF); } /* Sleeps inside a bus scan or a claimed storage-rescan command (see * usb_delay_ms); busy-waits everywhere else. */ static void msc_delay(unsigned int ms) { usb_delay_ms(ms); } /* Bulk IN with a deadline. Transfers larger than the host controller's * internal bulk-IN buffer are split; EHCI's is 2048 bytes. */ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len) { unsigned int got = 0, t0 = get_ticks_ms(); while (got < len) { unsigned int want = len - got; int n; if (want > MSC_BULK_CHUNK) want = MSC_BULK_CHUNK; n = usb_bulk_in(m->ctrl, m->addr, m->ep_in, m->ep_in_mps, buf + got, want); if (n < 0) return -1; if (n > 0) { /* A short packet is normal inside a BOT data stage: * the device may split it at any USB transfer * boundary. Keep reading until the Stage's known * byte count is reached. */ got += (unsigned int)n; t0 = get_ticks_ms(); } else if (get_ticks_ms() - t0 > m->timeout_ms) { /* A timed-out bulk-IN qTD stays armed in the HCD * schedule; cancel it or every later transfer sees * the endpoint as permanently busy. */ usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1); return -1; } else { usb_poll_relax(t0); } } return 0; } static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf, unsigned int len) { if (usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps, buf, len) != 0) { usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0); return -1; } return 0; } /* BOT reset + clear both bulk endpoint halts. */ static void msc_reset(struct usb_msc *m) { usb_control_out(m->ctrl, m->addr, m->mps, m->low, 0x21, 0xFF, 0, (unsigned short)m->iface); usb_control_out(m->ctrl, m->addr, m->mps, m->low, 0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in)); usb_control_out(m->ctrl, m->addr, m->mps, m->low, 0x02, 0x01, 0, (unsigned short)m->ep_out); /* CLEAR_FEATURE resets the device's endpoint toggle to DATA0; the * host side must do the same or the next CBW/data stage is sent with * the wrong toggle (real EHCI hardware rejects it, QEMU does not). */ usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1); usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0); m->tag++; } /* Last-resort recovery for a device whose BOT reset did not restore the * pipes: re-apply the configuration. That resets the device's endpoints * (and their data toggles) to DATA0 without a port reset, so the address * and the driver's endpoint numbers stay valid. */ static void msc_reconfigure(struct usb_msc *m) { if (!m->config) return; usb_control_out(m->ctrl, m->addr, m->mps, m->low, 0x00, 0x09, (unsigned short)m->config, 0); usb_bulk_reset(m->ctrl, m->addr, m->ep_in, 1); usb_bulk_reset(m->ctrl, m->addr, m->ep_out, 0); } static int bot_cmd(struct usb_msc *m, const unsigned char *cb, unsigned int cb_len, void *data, unsigned int data_len, int dir_in) { unsigned char cbw[31]; unsigned char csw[13]; unsigned int tag = ++m->tag; for (unsigned int i = 0; i < sizeof(cbw); i++) cbw[i] = 0; put32le(cbw, MSC_CBW_SIG); put32le(cbw + 4, tag); put32le(cbw + 8, data_len); cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00); cbw[13] = 0; /* LUN 0 */ cbw[14] = (unsigned char)cb_len; for (unsigned int i = 0; i < cb_len && i < 16; i++) cbw[15 + i] = cb[i]; if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0) return -1; if (data_len) { if (dir_in) { if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0) return -1; } else if (msc_bulk_out(m, (const unsigned char *)data, data_len) != 0) { return -1; } } if (msc_bulk_in(m, csw, sizeof(csw)) != 0) return -1; if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag) { m->last_status = 0xFF; return -1; } m->last_status = csw[12]; m->last_residue = rd32le(csw + 8); if (csw[12] != 0) { /* A command that completed with CHECK CONDITION (or another * SCSI status) has valid sense data; a transport failure does * not. -2 tells the recovery path to ask for the sense * before resetting the pipe. */ return -2; } if (m->last_residue != 0) return -1; return 0; } /* Fetch the sense data after a failed command so the next CBW does not keep * returning the same unit attention/medium error. */ static int msc_sense(struct usb_msc *m) { unsigned char cb[6] = { 0x03, 0, 0, 0, 18, 0 }; unsigned char data[18]; if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0) return -1; m->sense_key = data[2] & 0x0F; m->sense_asc = data[12]; m->sense_ascq = data[13]; return 0; } static int msc_test_unit_ready(struct usb_msc *m) { unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 }; return bot_cmd(m, cb, sizeof(cb), 0, 0, 0); } static int msc_inquiry(struct usb_msc *m, unsigned char data[36]) { unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 }; return bot_cmd(m, cb, sizeof(cb), data, 36, 1); } static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors) { unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; unsigned char data[8]; unsigned int last, block; if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0) return -1; last = rd32be(data); block = rd32be(data + 4); if (block != 512 || last == 0xFFFFFFFFu) return -1; *sectors = last + 1; return 0; } static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count, void *buf) { unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; put32be(cb + 2, lba); put16be(cb + 7, count); return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1); } static int msc_read10_retry(struct usb_msc *m, unsigned int lba, unsigned int count, void *buf) { unsigned int i, t0 = get_ticks_ms(); int probe_only = msc_probe_only; for (i = 0; i < 3; i++) { int r; if (i == 0 && (m->first_read || probe_only)) m->timeout_ms = probe_only ? MSC_PROBE_MS : MSC_FIRST_READ_MS; r = msc_read10(m, lba, count, buf); if (i == 0 && m->first_read) { unsigned int elapsed = get_ticks_ms() - t0; m->first_read = 0; m->timeout_ms = MSC_TIMEOUT_MS; if (r == 0 && elapsed > 1000) { serial_puts(SERIAL_COM1, "usbmsc: first read "); serial_putdec(SERIAL_COM1, elapsed); serial_puts(SERIAL_COM1, " ms\n"); } } if (r == 0) { if (probe_only) m->timeout_ms = MSC_TIMEOUT_MS; return 0; } if (probe_only) { /* Best-effort probe: do not spend the reset and retry * chain on a device that did not answer. `storage` * will try the full path. */ m->timeout_ms = MSC_TIMEOUT_MS; return -1; } /* Only a completed command with a non-zero CSW status has * sense data. After a timeout the endpoint is in an * unknown state, and a REQUEST SENSE would just add another * multi-second timeout before the reset. */ if (r == -2) msc_sense(m); msc_reset(m); /* If the BOT reset alone did not restore the pipes, reset the * device configuration once before the next retry. */ if (i == 0) msc_reconfigure(m); msc_delay(10); } if (probe_only) m->timeout_ms = MSC_TIMEOUT_MS; return -1; } static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count, const void *buf) { unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; put32be(cb + 2, lba); put16be(cb + 7, count); return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0); } static int msc_sync_cache(struct usb_msc *m) { unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; return bot_cmd(m, cb, sizeof(cb), 0, 0, 0); } /* Find and configure a bulk-only mass-storage interface. */ static int msc_config(struct usb_msc *m) { unsigned char hdr[9], cfg[256]; unsigned int total, i, config = 0; unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0; unsigned int iface = 0; int found = 0; if (usb_control_in(m->ctrl, m->addr, m->mps, m->low, 0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0) return -1; total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8); if (total < 9 || total > sizeof(cfg)) return -1; if (usb_control_in(m->ctrl, m->addr, m->mps, m->low, 0x80, 0x06, 0x0200, 0, cfg, total) != (int)total) return -1; config = cfg[5]; if (!config) return -1; i = 0; while (i + 2 <= total) { unsigned int blen = cfg[i]; unsigned int btype = cfg[i + 1]; if (blen < 2 || i + blen > total) break; if (btype == 0x04 && blen >= 9) { /* interface */ if (cfg[i + 5] == 8 && cfg[i + 6] == 6 && cfg[i + 7] == 0x50) { found = 1; iface = cfg[i + 2]; ep_in = ep_out = 0; ep_in_mps = ep_out_mps = 0; } else { found = 0; } } else if (btype == 0x05 && blen >= 7 && found) { unsigned int epa = cfg[i + 2]; unsigned int attr = cfg[i + 3] & 3u; unsigned int mps = ((unsigned int)cfg[i + 4] | ((unsigned int)cfg[i + 5] << 8)) & 0x7FFu; if (attr == 2) { if (epa & 0x80) { ep_in = epa & 0x0F; ep_in_mps = mps; } else { ep_out = epa & 0x0F; ep_out_mps = mps; } } } i += blen; } if (!found || !ep_in || !ep_out) return -1; if (usb_control_out(m->ctrl, m->addr, m->mps, m->low, 0x00, 0x09, (unsigned short)config, 0) < 0) return -1; m->iface = iface; m->config = config; m->ep_in = ep_in; m->ep_in_mps = ep_in_mps ? ep_in_mps : 64; m->ep_out = ep_out; m->ep_out_mps = ep_out_mps ? ep_out_mps : 64; return 0; } static void msc_set_model(struct usb_msc *m, const unsigned char data[36]) { unsigned int i, n = 0; for (i = 0; i < 8; i++) m->model[n++] = (char)data[8 + i]; m->model[n++] = ' '; for (i = 0; i < 16; i++) m->model[n++] = (char)data[16 + i]; while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0')) n--; m->model[n] = '\0'; } static int device_probe(unsigned int ctrl, unsigned int addr, unsigned int mps, int low_speed, unsigned int port, unsigned int vendor, unsigned int product) { struct usb_msc *m = 0; unsigned char inquiry[36]; unsigned int i, sectors; int slot = -1; (void)vendor; (void)product; /* A new bus scan re-enumerates every device with fresh addresses; * forget the old set on its first probe. */ { unsigned int gen = usb_generation(); if (gen != msc_generation) { for (i = 0; i < USB_MSC_MAX; i++) msc[i].present = 0; msc_generation = gen; } } for (i = 0; i < USB_MSC_MAX; i++) if (msc[i].present && msc[i].ctrl == ctrl && msc[i].addr == addr) { slot = (int)i; break; } if (slot < 0) for (i = 0; i < USB_MSC_MAX; i++) if (!msc[i].present) { slot = (int)i; break; } if (slot < 0) return 0; m = &msc[(unsigned int)slot]; m->present = 0; m->ctrl = ctrl; m->addr = addr; m->mps = mps; m->low = low_speed; m->port = port; m->tag = 0; m->iface = 0; m->config = 0; m->timeout_ms = MSC_TIMEOUT_MS; m->first_read = 0; if (msc_config(m) != 0) return 0; for (i = 0; i < 4; i++) { if (msc_test_unit_ready(m) == 0) break; msc_delay(50); msc_reset(m); } if (i == 4) { serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n"); return 0; } if (msc_inquiry(m, inquiry) != 0) { serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n"); msc_reset(m); if (msc_inquiry(m, inquiry) != 0) return 0; } if (msc_read_capacity(m, §ors) != 0) { serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n"); msc_reset(m); if (msc_read_capacity(m, §ors) != 0) return 0; } if (sectors == 0) return 0; msc_set_model(m, inquiry); m->sectors = sectors; m->timeout_ms = MSC_TIMEOUT_MS; m->first_read = 1; m->present = 1; serial_puts(SERIAL_COM1, "usbmsc: "); serial_puts(SERIAL_COM1, m->model); serial_puts(SERIAL_COM1, " "); serial_putdec(SERIAL_COM1, sectors); serial_puts(SERIAL_COM1, " sectors on ctrl "); serial_putdec(SERIAL_COM1, ctrl); serial_puts(SERIAL_COM1, " addr "); serial_putdec(SERIAL_COM1, addr); serial_puts(SERIAL_COM1, "\n"); return 1; } void usbmsc_register(void) { unsigned int i; for (i = 0; i < USB_MSC_MAX; i++) { msc_names[i][0] = 'u'; msc_names[i][1] = 's'; msc_names[i][2] = 'b'; msc_names[i][3] = (char)('0' + i); msc_names[i][4] = '\0'; } usb_register_device_probe(device_probe); } void usbmsc_blk_scan(void) { unsigned int i; for (i = 0; i < USB_MSC_MAX; i++) { blk_dev_t *d; unsigned int j; if (!msc[i].present || blk_dev_count >= BLKDEV_MAX) continue; d = &blk_devs[blk_dev_count++]; d->name = msc_names[i]; d->kind = BLK_USB; d->bus = 0; d->unit = i; d->present = 1; d->total_sectors = msc[i].sectors; for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++) d->model[j] = msc[i].model[j]; d->model[j] = '\0'; } } static int msc_get(unsigned int unit, struct usb_msc **out) { if (unit >= USB_MSC_MAX || !msc[unit].present) return -1; *out = &msc[unit]; return 0; } static int msc_read_claimed(unsigned int unit, unsigned int lba, void *buf, unsigned int count) { struct usb_msc *m; unsigned char *p = (unsigned char *)buf; if (msc_get(unit, &m) != 0) return -1; if (lba > m->sectors || count > m->sectors - lba) return -1; while (count) { unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count; if (msc_read10_retry(m, lba, n, p) != 0) { /* Some bridges/Sticks fail a large READ(10) but * accept the same range one sector at a time. This * also narrows a real medium error to one LBA. */ if (n > 1) { unsigned int i; io_putstr("usbmsc: lba "); io_puthex(lba); io_putstr(" retrying 1-sector reads\n"); for (i = 0; i < n; i++) if (msc_read10_retry(m, lba + i, 1, p + i * 512u) != 0) { io_putstr("usbmsc: sector "); io_puthex(lba + i); io_putstr(" failed, sense "); io_puthex((m->sense_key << 16) | (m->sense_asc << 8) | m->sense_ascq); io_putchar('\n'); return -1; } } else { io_putstr("usbmsc: lba "); io_puthex(lba); io_putstr(" failed, sense "); io_puthex((m->sense_key << 16) | (m->sense_asc << 8) | m->sense_ascq); io_putchar('\n'); return -1; } } lba += n; p += n * 512u; count -= n; } return 0; } static int msc_write_claimed(unsigned int unit, unsigned int lba, const void *buf, unsigned int count) { struct usb_msc *m; const unsigned char *p = (const unsigned char *)buf; if (msc_get(unit, &m) != 0) return -1; if (lba > m->sectors || count > m->sectors - lba) return -1; while (count) { unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count; if (msc_write10(m, lba, n, p) != 0) { msc_reset(m); if (msc_write10(m, lba, n, p) != 0) return -1; } lba += n; p += n * 512u; count -= n; } return 0; } static int msc_flush_claimed(unsigned int unit) { struct usb_msc *m; if (msc_get(unit, &m) != 0) return -1; if (msc_sync_cache(m) != 0) { msc_reset(m); if (msc_sync_cache(m) != 0) return -1; } return 0; } /* The block-layer entry points claim the bus for the whole command, so the * storage rescan may sleep in the HCD wait loops (dropping the BKL) while * no other context can start a transfer on the same controller. */ int usbmsc_read(unsigned int unit, unsigned int lba, void *buf, unsigned int count) { int r; if (usb_bus_claim() != 0) return -1; r = msc_read_claimed(unit, lba, buf, count); usb_bus_release(); return r; } int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf, unsigned int count) { int r; if (usb_bus_claim() != 0) return -1; r = msc_write_claimed(unit, lba, buf, count); usb_bus_release(); return r; } int usbmsc_flush(unsigned int unit) { int r; if (usb_bus_claim() != 0) return -1; r = msc_flush_claimed(unit); usb_bus_release(); return r; }