| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042 |
- /* USB core: device enumeration, descriptor parsing, hub recursion, bus scans
- and the hotplug watcher. Controller-specific work goes through the
- usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c / xhci.c). */
- #include "io.h"
- #include "process.h"
- #include "sched.h"
- #include "serial.h"
- #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.
- USB_HCD_MAX_CTRL is the per-type limit, this is the total. */
- #define USB_MAX_CTRL 8
- struct usb_ctrl_ref {
- const struct usb_hcd_ops *ops;
- unsigned int local;
- };
- /* Standard request types. */
- #define RT_DEV2HOST_STD 0x80u
- #define RT_HOST2DEV_STD 0x00u
- #define RT_DEV2HOST_CLS 0xA0u
- #define RT_DEV2HOST_OTH 0xA3u
- #define RT_HOST2DEV_OTH 0x23u
- /* Standard requests. */
- #define REQ_GET_STATUS 0x00u
- #define REQ_CLEAR_FEATURE 0x01u
- #define REQ_SET_FEATURE 0x03u
- #define REQ_SET_ADDRESS 0x05u
- #define REQ_GET_DESCRIPTOR 0x06u
- #define REQ_SET_CONFIGURATION 0x09u
- /* Descriptor types. */
- #define DESC_DEVICE 0x01u
- #define DESC_CONFIG 0x02u
- #define DESC_INTERFACE 0x04u
- #define DESC_ENDPOINT 0x05u
- #define DESC_HUB 0x29u
- /* Hub feature selectors. */
- #define FEAT_PORT_RESET 4u
- #define FEAT_PORT_POWER 8u
- #define FEAT_C_PORT_CONNECTION 16u
- #define FEAT_C_PORT_RESET 20u
- /* Hub port status bits. */
- #define PSTAT_CONNECT 0x0001u
- #define PSTAT_ENABLE 0x0002u
- #define PSTAT_LOW_SPEED 0x0200u
- /* How much of a configuration descriptor we read/parse. */
- #define USB_CONFIG_MAX 128
- /* Hotplug watcher period. */
- #define HOTPLUG_MS 250
- /* Bring-up diagnostics to COM1 (also mirrored by the network console on
- boards without serial). Set to 0 to silence. */
- #define USB_DEBUG 0
- #if USB_DEBUG
- #include "serial.h"
- static void dbg(const char *s)
- {
- serial_puts(SERIAL_COM1, s);
- }
- static void dbg_dec(const char *s, unsigned int n)
- {
- serial_puts(SERIAL_COM1, s);
- serial_putdec(SERIAL_COM1, n);
- serial_putc(SERIAL_COM1, '\n');
- }
- static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
- unsigned int low)
- {
- serial_puts(SERIAL_COM1, "usb: ctrl ");
- serial_putdec(SERIAL_COM1, ctrl);
- serial_puts(SERIAL_COM1, " port ");
- serial_putdec(SERIAL_COM1, port);
- serial_puts(SERIAL_COM1, " connected ");
- serial_putdec(SERIAL_COM1, conn);
- serial_puts(SERIAL_COM1, " ls ");
- serial_putdec(SERIAL_COM1, low);
- serial_putc(SERIAL_COM1, '\n');
- }
- #else
- static void dbg(const char *s) { (void)s; }
- static void dbg_dec(const char *s, unsigned int n) { (void)s; (void)n; }
- static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
- unsigned int low)
- {
- (void)ctrl;
- (void)port;
- (void)conn;
- (void)low;
- }
- #endif
- static struct usb_ctrl_ref usb_ctrls[USB_MAX_CTRL];
- static unsigned int n_usb_ctrls;
- static int hcd_init_done;
- static unsigned int generation;
- static unsigned int next_addr[USB_MAX_CTRL];
- static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
- static usb_probe_fn current_probe;
- #define USB_MAX_DEVICE_PROBES 6
- 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; /* 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)
- {
- struct process *p = current_proc();
- return p ? p->pid : 0;
- }
- int usb_bus_claim(void)
- {
- 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 bus_depth && cur_pid() != bus_owner_pid;
- }
- /* 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 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));
- 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;
- }
- t0 = get_ticks_ms();
- while (get_ticks_ms() - t0 < 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];
- static unsigned char port_st[4];
- static unsigned char conf_hdr[9];
- static unsigned char conf_buf[USB_CONFIG_MAX];
- static void put_uint(unsigned int n)
- {
- char buf[12];
- char *p = buf + sizeof(buf) - 1;
- *p = '\0';
- if (n == 0) {
- io_putchar('0');
- return;
- }
- while (n) {
- *--p = (char)('0' + (n % 10));
- n /= 10;
- }
- while (*p)
- io_putchar(*p++);
- }
- static void put_hex(unsigned int n, unsigned int digits)
- {
- static const char hex[] = "0123456789ABCDEF";
- unsigned int i;
- for (i = 0; i < digits; i++)
- io_putchar(hex[(n >> ((digits - 1 - i) * 4)) & 0xF]);
- }
- static void msleep(unsigned int ms)
- {
- usb_delay_ms(ms);
- }
- static void indent(unsigned int depth)
- {
- unsigned int i;
- for (i = 0; i < depth; i++)
- io_putstr(" ");
- }
- static const char *class_name(unsigned int cls)
- {
- switch (cls) {
- case 0x00: return "Per-interface";
- case 0x01: return "Audio";
- case 0x02: return "Communications";
- case 0x03: return "Human Interface";
- case 0x05: return "Physical";
- case 0x06: return "Image";
- case 0x07: return "Printer";
- case 0x08: return "Mass Storage";
- case 0x09: return "Hub";
- case 0x0A: return "CDC Data";
- case 0x0B: return "Smart Card";
- case 0x0D: return "Content Security";
- case 0x0E: return "Video";
- case 0x0F: return "Personal Healthcare";
- case 0xE0: return "Wireless";
- case 0xEF: return "Miscellaneous";
- case 0xFE: return "Application";
- case 0xFF: return "Vendor-specific";
- default: return "Unknown";
- }
- }
- /* wPortStatus (little-endian) from the GET_PORT_STATUS buffer. */
- static unsigned int port_status_word(void)
- {
- return (unsigned int)port_st[0] | ((unsigned int)port_st[1] << 8);
- }
- int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
- int low_speed, unsigned char rt, unsigned char req,
- unsigned short value, unsigned short index,
- unsigned char *buf, unsigned int len)
- {
- const struct usb_hcd_ops *ops;
- unsigned char setup[8];
- if (ctrl >= n_usb_ctrls || usb_scan_foreign())
- return -1;
- ops = usb_ctrls[ctrl].ops;
- setup[0] = rt;
- setup[1] = req;
- setup[2] = (unsigned char)(value & 0xFF);
- setup[3] = (unsigned char)((value >> 8) & 0xFF);
- setup[4] = (unsigned char)(index & 0xFF);
- setup[5] = (unsigned char)((index >> 8) & 0xFF);
- setup[6] = (unsigned char)(len & 0xFF);
- setup[7] = (unsigned char)((len >> 8) & 0xFF);
- return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
- low_speed, setup, buf, len, 1);
- }
- int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
- int low_speed, unsigned char rt, unsigned char req,
- unsigned short value, unsigned short index)
- {
- const struct usb_hcd_ops *ops;
- unsigned char setup[8];
- if (ctrl >= n_usb_ctrls || usb_scan_foreign())
- return -1;
- ops = usb_ctrls[ctrl].ops;
- setup[0] = rt;
- setup[1] = req;
- setup[2] = (unsigned char)(value & 0xFF);
- setup[3] = (unsigned char)((value >> 8) & 0xFF);
- setup[4] = (unsigned char)(index & 0xFF);
- setup[5] = (unsigned char)((index >> 8) & 0xFF);
- setup[6] = 0;
- setup[7] = 0;
- return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
- low_speed, setup, 0, 0, 0);
- }
- int usb_control_out_data(unsigned int ctrl, unsigned int addr,
- unsigned int mps, int low_speed, unsigned char rt,
- unsigned char req, unsigned short value,
- unsigned short index, const unsigned char *buf,
- unsigned int len)
- {
- const struct usb_hcd_ops *ops;
- unsigned char setup[8];
- if (ctrl >= n_usb_ctrls || usb_scan_foreign())
- return -1;
- ops = usb_ctrls[ctrl].ops;
- setup[0] = rt;
- setup[1] = req;
- setup[2] = (unsigned char)(value & 0xFF);
- setup[3] = (unsigned char)((value >> 8) & 0xFF);
- setup[4] = (unsigned char)(index & 0xFF);
- setup[5] = (unsigned char)((index >> 8) & 0xFF);
- setup[6] = (unsigned char)(len & 0xFF);
- setup[7] = (unsigned char)((len >> 8) & 0xFF);
- return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
- low_speed, setup, (unsigned char *)buf,
- len, 0);
- }
- /* Always-on failure trail (COM1/netmon): a device that never enumerates
- must not be silent, since boot-time input may depend on it. */
- static void usb_fail(const char *what, unsigned int ctrl, unsigned int port,
- unsigned int addr)
- {
- serial_puts(SERIAL_COM1, "usb: ");
- serial_puts(SERIAL_COM1, what);
- serial_puts(SERIAL_COM1, " ctrl ");
- serial_putdec(SERIAL_COM1, ctrl);
- serial_puts(SERIAL_COM1, " port ");
- serial_putdec(SERIAL_COM1, port);
- serial_puts(SERIAL_COM1, " addr ");
- serial_putdec(SERIAL_COM1, addr);
- serial_putc(SERIAL_COM1, '\n');
- }
- /* Read the configuration descriptor and hand every interface with an
- interrupt-IN endpoint to the active probe. Returns 1 when the device
- answered and its configuration was parsed. */
- static int probe_config(unsigned int ctrl, unsigned int addr, unsigned int mps,
- int low, unsigned int port, unsigned int vendor,
- unsigned int product)
- {
- struct usb_iface_info info;
- unsigned int total, i;
- int have_iface = 0;
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_CONFIG << 8), 0,
- conf_hdr, 9) < 0) {
- usb_fail("config header failed", ctrl, port, addr);
- return 0;
- }
- total = (unsigned int)conf_hdr[2] | ((unsigned int)conf_hdr[3] << 8);
- if (total < 9)
- return 0;
- if (total > USB_CONFIG_MAX)
- total = USB_CONFIG_MAX;
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_CONFIG << 8), 0,
- conf_buf, total) < 0) {
- usb_fail("config read failed", ctrl, port, addr);
- return 0;
- }
- dbg_dec("usb: config descriptor len ", total);
- info.ctrl = ctrl;
- info.addr = addr;
- info.mps = mps;
- info.low_speed = low;
- info.port = port;
- info.vendor = vendor;
- info.product = product;
- info.config_value = conf_buf[5];
- info.cls = 0;
- info.subcls = 0;
- info.proto = 0;
- info.iface_num = 0;
- info.ep_addr = 0;
- info.ep_mps = 0;
- info.ep_interval = 0;
- i = 0;
- while (i + 2 <= total) {
- unsigned int blen = conf_buf[i];
- unsigned int btype = conf_buf[i + 1];
- if (blen < 2 || i + blen > total)
- break;
- if (btype == DESC_INTERFACE && blen >= 9) {
- if (have_iface && info.ep_addr) {
- dbg("usb: interface has interrupt-IN ep\n");
- if (current_probe)
- current_probe(&info);
- }
- info.cls = conf_buf[i + 5];
- info.subcls = conf_buf[i + 6];
- info.proto = conf_buf[i + 7];
- info.iface_num = conf_buf[i + 2];
- if (print_devices) {
- io_putstr(" Interface ");
- put_uint(info.iface_num);
- io_putstr(": ");
- put_hex(info.cls, 2);
- io_putchar('/');
- put_hex(info.subcls, 2);
- io_putchar('/');
- put_hex(info.proto, 2);
- io_putchar('\n');
- } else {
- dbg_dec("usb: iface class ", info.cls);
- dbg_dec("usb: iface subclass ", info.subcls);
- dbg_dec("usb: iface protocol ", info.proto);
- }
- info.ep_addr = 0;
- info.ep_mps = 0;
- info.ep_interval = 0;
- have_iface = 1;
- } else if (btype == DESC_ENDPOINT && blen >= 7 && have_iface) {
- unsigned int epa = conf_buf[i + 2];
- unsigned int attr = conf_buf[i + 3] & 3u;
- unsigned int mps = ((unsigned int)conf_buf[i + 4] |
- ((unsigned int)conf_buf[i + 5] << 8)) & 0x7FFu;
- if (print_devices) {
- io_putstr(" Endpoint 0x");
- put_hex(epa, 2);
- io_putstr(attr == 3u ? " interrupt" : " other");
- io_putstr(", mps ");
- put_uint(mps);
- io_putchar('\n');
- }
- if (!info.ep_addr && (epa & 0x80u) && attr == 3u) {
- info.ep_addr = epa;
- info.ep_mps = mps;
- info.ep_interval = conf_buf[i + 6];
- dbg_dec("usb: interrupt-IN endpoint ", epa & 0x0Fu);
- }
- }
- i += blen;
- }
- if (have_iface && info.ep_addr && current_probe)
- current_probe(&info);
- return 1;
- }
- static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
- int low, unsigned int port, unsigned int depth);
- static int enumerate_device(unsigned int ctrl, unsigned int addr,
- unsigned int mps, int low, unsigned int port,
- unsigned int depth);
- static int enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
- unsigned int depth);
- static int enumerate_device(unsigned int ctrl, unsigned int addr,
- unsigned int mps, int low, unsigned int port,
- unsigned int depth)
- {
- unsigned int vendor, product, cls, subcls, proto;
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_DEVICE << 8), 0,
- dev_desc, 18) < 0) {
- usb_fail("device descriptor failed", ctrl, port, addr);
- if (print_devices) {
- indent(depth);
- io_putstr("(device descriptor read failed)\n");
- }
- return 0;
- }
- vendor = dev_desc[8] | ((unsigned int)dev_desc[9] << 8);
- product = dev_desc[10] | ((unsigned int)dev_desc[11] << 8);
- cls = dev_desc[4];
- subcls = dev_desc[5];
- proto = dev_desc[6];
- {
- unsigned int i;
- for (i = 0; i < n_device_probes; i++)
- if (device_probes[i](ctrl, addr, mps, low, port,
- vendor, product))
- break;
- }
- if (print_devices) {
- indent(depth);
- io_putstr("Bus ");
- put_uint(ctrl + 1);
- io_putstr(" Device ");
- put_uint(addr);
- io_putstr(": ID ");
- put_hex(vendor, 4);
- io_putchar(':');
- put_hex(product, 4);
- io_putchar(' ');
- io_putstr(class_name(cls));
- io_putstr(" [");
- put_hex(cls, 2);
- io_putchar('/');
- put_hex(subcls, 2);
- io_putchar('/');
- put_hex(proto, 2);
- io_putstr("]\n");
- }
- if (cls == 0x09) {
- enumerate_hub(ctrl, addr, mps, low, port, depth + 1);
- return 1;
- }
- if (current_probe || print_devices)
- return probe_config(ctrl, addr, mps, low, port, vendor, product);
- return 1;
- }
- static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
- int low, unsigned int port, unsigned int depth)
- {
- unsigned char cfg[9];
- unsigned int nports, p, i, power_good;
- /* Put the hub in its configured state before issuing hub-class
- requests. The core does not configure non-HID devices itself, and
- some hubs (notably Intel PCH internal hubs) require this. */
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_CONFIG << 8), 0, cfg,
- sizeof(cfg)) >= 0 && cfg[5] != 0) {
- usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_STD,
- REQ_SET_CONFIGURATION, (unsigned short)cfg[5],
- 0);
- msleep(5);
- }
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_CLS,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_HUB << 8), 0, hub_desc,
- sizeof(hub_desc)) < 0)
- return;
- nports = hub_desc[2];
- if (nports > 8)
- nports = 8;
- /* Power every downstream port before looking at connect status.
- An unpowered port reports no connection, so the old order could
- miss devices behind a switched hub. */
- power_good = (unsigned int)hub_desc[5] * 2;
- if (power_good < 20)
- power_good = 20;
- if (power_good > 500)
- power_good = 500;
- for (p = 1; p <= nports; p++)
- usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
- REQ_SET_FEATURE, FEAT_PORT_POWER,
- (unsigned short)p);
- msleep(power_good);
- for (p = 1; p <= nports; p++) {
- unsigned int st = 0;
- int low2;
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
- REQ_GET_STATUS, 0, (unsigned short)p,
- port_st, 4) < 0)
- continue;
- st = port_status_word();
- if (print_devices) {
- unsigned int chg = (unsigned int)port_st[2] |
- ((unsigned int)port_st[3] << 8);
- indent(depth);
- io_putstr("Hub port ");
- put_uint(p);
- io_putstr(": status 0x");
- put_hex(st, 4);
- io_putstr(" change 0x");
- put_hex(chg, 4);
- io_putchar('\n');
- }
- if (!(st & PSTAT_CONNECT))
- continue;
- /* Reset the downstream port and wait for it to enable. */
- if (usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
- REQ_SET_FEATURE, FEAT_PORT_RESET,
- (unsigned short)p) < 0)
- continue;
- for (i = 0; i < 100; i++) {
- if (usb_control_in(ctrl, addr, mps, low,
- RT_DEV2HOST_OTH, REQ_GET_STATUS, 0,
- (unsigned short)p, port_st, 4) < 0)
- break;
- st = port_status_word();
- if (st & PSTAT_ENABLE)
- break;
- if (port_st[2] & 0x10u) /* C_PORT_RESET */
- break;
- msleep(5);
- }
- msleep(10);
- usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
- REQ_CLEAR_FEATURE, FEAT_C_PORT_RESET,
- (unsigned short)p);
- usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
- REQ_CLEAR_FEATURE, FEAT_C_PORT_CONNECTION,
- (unsigned short)p);
- /* Re-read status to learn the child's speed. */
- low2 = 0;
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
- REQ_GET_STATUS, 0, (unsigned short)p,
- port_st, 4) >= 0) {
- st = port_status_word();
- if (st & PSTAT_LOW_SPEED)
- low2 = 1;
- }
- if (!(st & PSTAT_ENABLE))
- continue;
- if (print_devices) {
- unsigned int chg = (unsigned int)port_st[2] |
- ((unsigned int)port_st[3] << 8);
- indent(depth);
- io_putstr("Hub port ");
- put_uint(p);
- io_putstr(" after reset: status 0x");
- put_hex(st, 4);
- io_putstr(" change 0x");
- put_hex(chg, 4);
- io_putchar('\n');
- }
- enumerate_new_device(ctrl, port, low2, depth);
- }
- }
- static int enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
- unsigned int depth)
- {
- unsigned int mps;
- unsigned int addr = next_addr[ctrl];
- if (addr > 127) {
- if (print_devices) {
- indent(depth);
- io_putstr("(too many devices)\n");
- }
- return 0;
- }
- /* Some low/full-speed devices need a little more settle time after
- * the port reset than the OHCI spec's 10 ms. Without this the 970A's
- * OHCI mouse was intermittently dropped before its first control
- * transfer. */
- msleep(20);
- /* Traditional ("old") enumeration scheme: assign the address before
- * reading the descriptor. The xHCI Default-state EP0 transfer is
- * unreliable on this hardware, while a slot in the Addressed state
- * works. The device's real max packet size is not known yet, so use
- * the default 8-byte pipe for the first SET_ADDRESS/GET_DESCRIPTOR.
- * A full-speed device may have an 8-byte EP0 (the 64-byte guess made
- * the 970A's OHCI drop a mouse during descriptor read), and a
- * high-speed device accepts the short transfer and reports its real
- * bMaxPacketSize0 in the descriptor. */
- mps = 8;
- if (usb_control_out(ctrl, 0, mps, low, RT_HOST2DEV_STD,
- REQ_SET_ADDRESS, (unsigned short)addr, 0) < 0) {
- usb_fail("SET_ADDRESS failed", ctrl, port, 0);
- if (print_devices) {
- indent(depth);
- io_putstr("(SET_ADDRESS failed)\n");
- }
- return 0;
- }
- next_addr[ctrl] = addr + 1;
- msleep(20);
- /* First 8 bytes at the assigned address (learn max packet size). */
- if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
- REQ_GET_DESCRIPTOR,
- (unsigned short)(DESC_DEVICE << 8), 0,
- desc8, 8) < 0) {
- usb_fail("descriptor8 failed", ctrl, port, addr);
- if (print_devices) {
- indent(depth);
- io_putstr("(device descriptor read failed)\n");
- }
- return 0;
- }
- mps = desc8[7];
- if (mps == 0)
- mps = 8;
- dbg_dec("usb: enumerated device, addr ", addr);
- return enumerate_device(ctrl, addr, mps, low, port, depth);
- }
- /* A port reset hands the device to its companion controller, and the first
- * control transfer after it is occasionally ignored by the SB950 OHCI
- * (every TD stays NotAccessed for the full 2 s timeout). A fresh port
- * reset and a short settle makes the retry work, so keep trying a couple
- * of times instead of leaving the port without a device until the next
- * connect-change. */
- static int enum_port_retry(unsigned int ctrl, unsigned int port,
- const struct usb_hcd_ops *ops, unsigned int local)
- {
- unsigned int try;
- for (try = 0;; try++) {
- if (enumerate_new_device(ctrl, port,
- ops->port_low_speed(local, port), 1))
- return 1;
- if (try >= 2)
- return 0;
- msleep(30);
- if (ops->port_reset(local, port) != 0)
- return 0;
- }
- }
- static void add_ctrl(const struct usb_hcd_ops *ops, unsigned int count)
- {
- unsigned int i;
- for (i = 0; i < count && n_usb_ctrls < USB_MAX_CTRL; i++) {
- usb_ctrls[n_usb_ctrls].ops = ops;
- usb_ctrls[n_usb_ctrls].local = i;
- n_usb_ctrls++;
- }
- }
- int usb_hcd_init(void)
- {
- unsigned int uhci, ohci, ehci, xhci;
- if (hcd_init_done)
- return n_usb_ctrls ? 0 : -1;
- hcd_init_done = 1;
- /* Companions first, so EHCI's CONFIGFLAG claim is the last thing
- that happens to the shared ports. */
- uhci = (unsigned int)usb_uhci_ops.init();
- ohci = (unsigned int)usb_ohci_ops.init();
- ehci = (unsigned int)usb_ehci_ops.init();
- xhci = (unsigned int)usb_xhci_ops.init();
- /* Scan EHCI first: resetting its ports hands low/full-speed devices
- back to the companions before those are scanned. */
- add_ctrl(&usb_ehci_ops, ehci);
- add_ctrl(&usb_uhci_ops, uhci);
- add_ctrl(&usb_ohci_ops, ohci);
- add_ctrl(&usb_xhci_ops, xhci);
- return n_usb_ctrls ? 0 : -1;
- }
- const char *usb_hcd_name(void)
- {
- if (!n_usb_ctrls)
- return "none";
- if (n_usb_ctrls == 1)
- return usb_ctrls[0].ops->name;
- return "USB";
- }
- const char *usb_hcd_name_ctrl(unsigned int ctrl)
- {
- if (ctrl >= n_usb_ctrls)
- return "none";
- return usb_ctrls[ctrl].ops->name;
- }
- unsigned int usb_generation(void)
- {
- return generation;
- }
- int usb_port_connected(unsigned int ctrl, unsigned int port)
- {
- if (ctrl >= n_usb_ctrls)
- return 0;
- if (usb_scan_foreign())
- return 1; /* do not detach anything mid-scan */
- return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
- port);
- }
- int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
- unsigned int ep, unsigned int mps, int low_speed)
- {
- const struct usb_hcd_ops *ops;
- int r;
- if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS ||
- usb_scan_foreign())
- return -1;
- ops = usb_ctrls[ctrl].ops;
- if (slot_ops[slot]) { /* stale owner from another HCD */
- slot_ops[slot]->int_in_stop(slot);
- slot_ops[slot] = 0;
- }
- r = ops->int_in_start(usb_ctrls[ctrl].local, slot, addr, ep, mps,
- low_speed);
- slot_ops[slot] = r == 0 ? ops : 0;
- return r;
- }
- int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
- {
- if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
- return 0;
- return slot_ops[slot]->int_in_poll(slot, buf, len);
- }
- void usb_int_in_stop(unsigned int slot)
- {
- if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
- return;
- slot_ops[slot]->int_in_stop(slot);
- slot_ops[slot] = 0;
- }
- void usb_register_device_probe(usb_device_probe_fn fn)
- {
- unsigned int i;
- if (!fn)
- return;
- for (i = 0; i < n_device_probes; i++)
- if (device_probes[i] == fn)
- return;
- if (n_device_probes < USB_MAX_DEVICE_PROBES)
- device_probes[n_device_probes++] = fn;
- }
- int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
- unsigned int mps, const unsigned char *buf,
- unsigned int len)
- {
- if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out ||
- usb_scan_foreign())
- return -1;
- return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
- mps, buf, len);
- }
- int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
- unsigned int mps, unsigned char *buf, unsigned int len)
- {
- if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in ||
- usb_scan_foreign())
- return -1;
- return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
- mps, buf, len);
- }
- int usb_bulk_supported(unsigned int ctrl)
- {
- if (ctrl >= n_usb_ctrls)
- return 0;
- return usb_ctrls[ctrl].ops->bulk_in != 0 &&
- usb_ctrls[ctrl].ops->bulk_out != 0;
- }
- int usb_scan(usb_probe_fn probe)
- {
- unsigned int c, p;
- unsigned int seen[USB_MAX_CTRL];
- if (scan_busy || usb_bus_claim() != 0)
- return -2;
- /* 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++;
- current_probe = probe;
- print_devices = (probe == 0);
- for (c = 0; c < USB_MAX_CTRL; c++)
- seen[c] = 0;
- for (c = 0; c < n_usb_ctrls; c++) {
- const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
- unsigned int local = usb_ctrls[c].local;
- next_addr[c] = 1;
- if (print_devices) {
- unsigned int bus, dev, func, base;
- int mmio;
- ops->info(local, &bus, &dev, &func, &base, &mmio);
- io_putstr(ops->name);
- io_putstr(" Host Controller (PCI ");
- put_hex(bus, 2);
- io_putchar(':');
- put_hex(dev, 2);
- io_putchar('.');
- put_hex(func, 1);
- io_putstr(mmio ? ", MMIO 0x" : ", I/O 0x");
- put_hex(base, mmio ? 8 : 4);
- io_putstr(")\n");
- if (ops->print_ports)
- ops->print_ports(local);
- }
- for (p = 0; p < ops->port_count(local); p++) {
- unsigned int conn =
- (unsigned int)ops->port_connected(local, p);
- dbg_port(c, p, conn,
- (unsigned int)ops->port_low_speed(local, p));
- if (!conn)
- continue;
- ops->port_take_change(local, p); /* clear stale CSC */
- if (ops->port_reset(local, p) != 0) {
- dbg("usb: port reset failed\n");
- continue;
- }
- dbg("usb: port reset ok\n");
- if (enum_port_retry(c, p, ops, local) && p < 32)
- seen[c] |= 1u << p;
- }
- if (print_devices && ops->print_ports) {
- io_putstr(" After scan:\n");
- ops->print_ports(local);
- }
- }
- /* EHCI is scanned first so it can hand low/full-speed devices back to
- * their OHCI companion. That handback can leave the companion port
- * briefly reporting "disconnected" exactly while the OHCI pass runs
- * (the 970A's mouse was missed that way). A delayed second OHCI-only
- * pass catches ports that stayed connected but produced no device. */
- msleep(60);
- for (c = 0; c < n_usb_ctrls; c++) {
- const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
- unsigned int local = usb_ctrls[c].local;
- if (ops != &usb_ohci_ops)
- continue;
- for (p = 0; p < ops->port_count(local) && p < 32; p++) {
- if (seen[c] & (1u << p))
- continue;
- if (!ops->port_connected(local, p))
- continue;
- ops->port_take_change(local, p);
- if (ops->port_reset(local, p) != 0)
- continue;
- dbg("usb: companion retry\n");
- enum_port_retry(c, p, ops, local);
- }
- }
- current_probe = 0;
- print_devices = 0;
- scan_busy = 0;
- usb_bus_release();
- return 0;
- }
- int usb_hotplug_poll(usb_probe_fn probe)
- {
- static unsigned int last;
- unsigned int now, c, p;
- int n = 0;
- if (!n_usb_ctrls || scan_busy || bus_depth)
- return 0;
- now = get_ticks_ms();
- if (now - last < HOTPLUG_MS)
- return 0;
- last = now;
- 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;
- unsigned int local = usb_ctrls[c].local;
- for (p = 0; p < ops->port_count(local); p++) {
- if (!ops->port_take_change(local, p))
- continue;
- if (!ops->port_connected(local, p))
- continue;
- if (ops->port_reset(local, p) != 0)
- continue;
- enum_port_retry(c, p, ops, local);
- n++;
- }
- }
- current_probe = 0;
- scan_busy = 0;
- usb_bus_release();
- return n;
- }
- void usb_enumerate_and_print(void)
- {
- int r = usb_scan(0);
- if (r == -1)
- io_putstr("lsusb: no USB host controller found\n");
- else if (r == -2)
- io_putstr("lsusb: a USB scan is already running\n");
- }
|