|
|
@@ -8,6 +8,7 @@
|
|
|
#include "timer.h"
|
|
|
#include "usb.h"
|
|
|
#include "usb_hcd.h"
|
|
|
+#include "vfs.h"
|
|
|
|
|
|
/* One global controller index space. Each entry maps to one instance of
|
|
|
one controller type; a machine with EHCI + UHCI companions gets several.
|
|
|
@@ -111,8 +112,12 @@ static usb_probe_fn current_probe;
|
|
|
static usb_device_probe_fn device_probes[USB_MAX_DEVICE_PROBES];
|
|
|
static unsigned int n_device_probes;
|
|
|
static int print_devices;
|
|
|
-static int scan_busy;
|
|
|
-static unsigned int scan_owner_pid; /* pid that set scan_busy */
|
|
|
+static int scan_busy; /* a usb_scan/hotplug pass runs */
|
|
|
+/* Bus ownership: a scan, or a mass-storage command issued by the storage
|
|
|
+ * rescan, claims the controllers for one process. While the owner sleeps
|
|
|
+ * in a wait loop, the transfer wrappers refuse every other context. */
|
|
|
+static unsigned int bus_depth;
|
|
|
+static unsigned int bus_owner_pid;
|
|
|
|
|
|
static unsigned int cur_pid(void)
|
|
|
{
|
|
|
@@ -121,27 +126,53 @@ static unsigned int cur_pid(void)
|
|
|
return p ? p->pid : 0;
|
|
|
}
|
|
|
|
|
|
-static void scan_begin(void)
|
|
|
+int usb_bus_claim(void)
|
|
|
{
|
|
|
- scan_busy = 1;
|
|
|
- scan_owner_pid = cur_pid();
|
|
|
+ unsigned int pid = cur_pid();
|
|
|
+
|
|
|
+ if (bus_depth && pid != bus_owner_pid)
|
|
|
+ return -1;
|
|
|
+ if (!bus_depth++)
|
|
|
+ bus_owner_pid = pid;
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+void usb_bus_release(void)
|
|
|
+{
|
|
|
+ if (bus_depth && !--bus_depth)
|
|
|
+ bus_owner_pid = 0;
|
|
|
}
|
|
|
|
|
|
int usb_scan_foreign(void)
|
|
|
{
|
|
|
- return scan_busy && cur_pid() != scan_owner_pid;
|
|
|
+ return bus_depth && cur_pid() != bus_owner_pid;
|
|
|
}
|
|
|
|
|
|
-void usb_delay_ms(unsigned int ms)
|
|
|
+/* 1 when the caller may sleep inside a USB wait: it owns the bus for a scan
|
|
|
+ * (lsusb, the kworker's HID rescan/hotplug) or for a command issued by the
|
|
|
+ * storage rescan, and it is not the idle/boot context (pid 0), not inside a
|
|
|
+ * non-preemptible section and not in an interrupts-off syscall (SYS_WIFI
|
|
|
+ * runs with IF clear). Other callers (normal file I/O, the installer, NIC
|
|
|
+ * paths) keep busy-waiting exactly as before. */
|
|
|
+static int usb_can_sleep(void)
|
|
|
{
|
|
|
- unsigned int t0, flags;
|
|
|
+ unsigned int flags, pid = cur_pid();
|
|
|
|
|
|
+ if (!bus_depth || !bus_owner_pid || pid != bus_owner_pid)
|
|
|
+ return 0;
|
|
|
+ if (!scan_busy && !(vfs_scan_busy && vfs_scan_owner_pid == pid))
|
|
|
+ return 0;
|
|
|
+ if (this_cpu()->preempt_count)
|
|
|
+ return 0;
|
|
|
__asm__ volatile ("pushfl; popl %0" : "=r"(flags));
|
|
|
- /* Only the scan owner sleeps, never the idle/boot context (pid 0),
|
|
|
- * a non-preemptible section or an interrupts-off syscall (SYS_WIFI
|
|
|
- * runs with IF clear). */
|
|
|
- if (scan_busy && scan_owner_pid && cur_pid() == scan_owner_pid &&
|
|
|
- !this_cpu()->preempt_count && (flags & 0x200u)) {
|
|
|
+ return (flags & 0x200u) != 0;
|
|
|
+}
|
|
|
+
|
|
|
+void usb_delay_ms(unsigned int ms)
|
|
|
+{
|
|
|
+ unsigned int t0;
|
|
|
+
|
|
|
+ if (usb_can_sleep()) {
|
|
|
process_sleep_ms(ms ? ms : 1);
|
|
|
return;
|
|
|
}
|
|
|
@@ -150,6 +181,17 @@ void usb_delay_ms(unsigned int ms)
|
|
|
;
|
|
|
}
|
|
|
|
|
|
+/* A yield, not a sleep: it hands the BKL to a waiting process (the WM, a
|
|
|
+ * terminal) and returns at once when nothing else is runnable, so a
|
|
|
+ * completion that lands a few ms later is still seen promptly. A 1 ms
|
|
|
+ * sleep would wake on the 100 Hz idle tick and made every slow transfer
|
|
|
+ * cost ~10 ms. */
|
|
|
+void usb_poll_relax(unsigned int t0)
|
|
|
+{
|
|
|
+ if (get_ticks_ms() - t0 >= 2 && usb_can_sleep())
|
|
|
+ sched_yield();
|
|
|
+}
|
|
|
+
|
|
|
static unsigned char dev_desc[18];
|
|
|
static unsigned char desc8[64];
|
|
|
static unsigned char hub_desc[16];
|
|
|
@@ -795,15 +837,16 @@ int usb_scan(usb_probe_fn probe)
|
|
|
{
|
|
|
unsigned int c, p;
|
|
|
|
|
|
- if (scan_busy)
|
|
|
+ if (scan_busy || usb_bus_claim() != 0)
|
|
|
return -2;
|
|
|
- /* Claim the bus before the first controller bring-up: the boot scan
|
|
|
- * runs while the WM and the first terminal are already up, and their
|
|
|
- * USB calls must see the scan (usb_scan_foreign) rather than half-
|
|
|
- * initialized controllers. */
|
|
|
- scan_begin();
|
|
|
+ /* The bus is claimed before the first controller bring-up: the boot
|
|
|
+ * scan runs while the WM and the first terminal are already up, and
|
|
|
+ * their USB calls must see the scan (usb_scan_foreign) rather than
|
|
|
+ * half-initialized controllers. */
|
|
|
+ scan_busy = 1;
|
|
|
if (usb_hcd_init() != 0) {
|
|
|
scan_busy = 0;
|
|
|
+ usb_bus_release();
|
|
|
return -1;
|
|
|
}
|
|
|
generation++;
|
|
|
@@ -862,6 +905,7 @@ int usb_scan(usb_probe_fn probe)
|
|
|
current_probe = 0;
|
|
|
print_devices = 0;
|
|
|
scan_busy = 0;
|
|
|
+ usb_bus_release();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@@ -871,7 +915,7 @@ int usb_hotplug_poll(usb_probe_fn probe)
|
|
|
unsigned int now, c, p;
|
|
|
int n = 0;
|
|
|
|
|
|
- if (!n_usb_ctrls || scan_busy)
|
|
|
+ if (!n_usb_ctrls || scan_busy || bus_depth)
|
|
|
return 0;
|
|
|
|
|
|
now = get_ticks_ms();
|
|
|
@@ -879,7 +923,8 @@ int usb_hotplug_poll(usb_probe_fn probe)
|
|
|
return 0;
|
|
|
last = now;
|
|
|
|
|
|
- scan_begin();
|
|
|
+ usb_bus_claim();
|
|
|
+ scan_busy = 1;
|
|
|
current_probe = probe;
|
|
|
for (c = 0; c < n_usb_ctrls; c++) {
|
|
|
const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
|
|
|
@@ -899,6 +944,7 @@ int usb_hotplug_poll(usb_probe_fn probe)
|
|
|
}
|
|
|
current_probe = 0;
|
|
|
scan_busy = 0;
|
|
|
+ usb_bus_release();
|
|
|
return n;
|
|
|
}
|
|
|
|