|
|
@@ -23,6 +23,13 @@
|
|
|
#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 */
|
|
|
|
|
|
@@ -33,6 +40,9 @@ struct usb_msc {
|
|
|
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;
|
|
|
@@ -43,6 +53,21 @@ struct usb_msc {
|
|
|
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)
|
|
|
{
|
|
|
@@ -108,7 +133,11 @@ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
|
|
|
* byte count is reached. */
|
|
|
got += (unsigned int)n;
|
|
|
t0 = get_ticks_ms();
|
|
|
- } else if (get_ticks_ms() - t0 > MSC_TIMEOUT_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);
|
|
|
@@ -120,8 +149,12 @@ static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
|
|
|
static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
|
|
|
unsigned int len)
|
|
|
{
|
|
|
- return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
|
|
|
- buf, 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. */
|
|
|
@@ -133,9 +166,28 @@ static void msc_reset(struct usb_msc *m)
|
|
|
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)
|
|
|
@@ -174,8 +226,13 @@ static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
|
|
|
}
|
|
|
m->last_status = csw[12];
|
|
|
m->last_residue = rd32le(csw + 8);
|
|
|
- if (csw[12] != 0)
|
|
|
- return -1;
|
|
|
+ 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;
|
|
|
@@ -239,13 +296,47 @@ static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
|
|
|
static int msc_read10_retry(struct usb_msc *m, unsigned int lba,
|
|
|
unsigned int count, void *buf)
|
|
|
{
|
|
|
- unsigned int i;
|
|
|
+ unsigned int i, t0 = get_ticks_ms();
|
|
|
+ int probe_only = msc_probe_only && m->first_read;
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
- if (msc_read10(m, lba, count, buf) == 0)
|
|
|
+ int r;
|
|
|
+
|
|
|
+ if (i == 0 && m->first_read)
|
|
|
+ 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)
|
|
|
return 0;
|
|
|
- msc_sense(m);
|
|
|
+ if (probe_only) {
|
|
|
+ /* Boot background scan: do not spend the reset and
|
|
|
+ * retry chain on a device that did not answer its
|
|
|
+ * first read. `storage` will try the full path. */
|
|
|
+ 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);
|
|
|
}
|
|
|
return -1;
|
|
|
@@ -333,6 +424,7 @@ static int msc_config(struct usb_msc *m)
|
|
|
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;
|
|
|
@@ -402,6 +494,9 @@ static int device_probe(unsigned int ctrl, unsigned int addr,
|
|
|
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;
|
|
|
@@ -434,6 +529,8 @@ static int device_probe(unsigned int ctrl, unsigned int addr,
|
|
|
|
|
|
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: ");
|