/* USB HID devices: mice, keyboards and the Logitech Unifying receiver. * * The probe claims every HID interface it understands: boot mouse/keyboard * (class 3 / subclass 1 / protocol 2 or 1), report-protocol HID interfaces * whose report descriptor says "mouse"/"pointer" or "keyboard" (class 3, * other subclasses — many modern and composite devices), and the vendor * DJ interface of a Logitech Unifying receiver. Each claimed interface * gets its own interrupt slot, so a composite device (keyboard + mouse in * one) and several devices can coexist. * * Mouse reports are handed back to mouse.c; keyboard reports are decoded * into PS/2 set-1 scancodes and injected into the regular input queue * (io_inject_scancode), so the terminal/WM see them exactly like a PS/2 * keyboard. Root ports are watched so devices can be plugged in later. */ #include "usbhid.h" #include "hid_report.h" #include "io.h" #include "mm.h" #include "usb.h" #include "serial.h" #include "timer.h" /* Request types/requests used here. */ #define RT_HOST2DEV_STD 0x00u #define RT_HOST2DEV_CLS 0x21u #define RT_DEV2HOST_CLS_IF 0x81u /* class request to an interface, IN */ #define REQ_GET_DESCRIPTOR 0x06u #define REQ_SET_CONFIGURATION 0x09u #define REQ_SET_PROTOCOL 0x0Bu /* Hotplug watcher period (must match usb.c's internal throttle). */ #define WATCH_MS 250 /* Extended (0xE0-prefixed) PS/2 make codes in the mapping table. */ #define K_EXT 0xE000u /* Report descriptors are read into one frame. 240 bytes is the largest * control-IN transfer UHCI can build at an 8-byte EP0 (30 TDs), and it * covers every ordinary mouse descriptor; a longer one is parsed from its * first part, and the boot decoder stays the fallback if the wheel lies * beyond it. */ #define REPORT_MAX 240 /* One PS/2/IntelliMouse wheel notch, shared with the DJ report path. */ #define USB_WHEEL_PX 48 #if HID_REPORT_MAX > USB_INT_MAX #error "HID mouse layout exceeds the interrupt-IN report buffer" #endif enum { KIND_FREE = 0, KIND_MOUSE, KIND_KBD, KIND_DJ }; struct hid_dev { unsigned int kind; /* KIND_*, KIND_FREE = slot unused */ int present; /* endpoint armed */ int claimed; /* claimed by the scan in progress */ unsigned int ctrl, addr, ep, mps, port; unsigned int xfer; /* report buffer size for int_in_start */ int low; unsigned int err; /* consecutive transfer errors */ unsigned int disc; /* consecutive "port disconnected" reads */ unsigned int report_mode; /* 1 = decode with layout, 0 = boot format */ struct hid_mouse_layout layout; /* keyboard report-diff state */ unsigned char prev[6]; unsigned char prev_mod; }; static struct hid_dev devs[USB_HCD_INT_SLOTS]; static int hcd_ok; static int need_rescan; static unsigned int gen; static unsigned int last_watch; /* --- keyboard report decoding ------------------------------------------ */ /* HID keyboard usage -> PS/2 set-1 make code (0 = unmapped). */ static const unsigned short hid_to_sc[256] = { [0x04] = 0x1E, [0x05] = 0x30, [0x06] = 0x2E, [0x07] = 0x20, [0x08] = 0x12, [0x09] = 0x21, [0x0A] = 0x22, [0x0B] = 0x23, [0x0C] = 0x17, [0x0D] = 0x24, [0x0E] = 0x25, [0x0F] = 0x26, [0x10] = 0x32, [0x11] = 0x31, [0x12] = 0x18, [0x13] = 0x19, [0x14] = 0x10, [0x15] = 0x13, [0x16] = 0x1F, [0x17] = 0x14, [0x18] = 0x16, [0x19] = 0x2F, [0x1A] = 0x11, [0x1B] = 0x2D, [0x1C] = 0x15, [0x1D] = 0x2C, [0x1E] = 0x02, [0x1F] = 0x03, [0x20] = 0x04, [0x21] = 0x05, [0x22] = 0x06, [0x23] = 0x07, [0x24] = 0x08, [0x25] = 0x09, [0x26] = 0x0A, [0x27] = 0x0B, [0x28] = 0x1C, [0x29] = 0x01, [0x2A] = 0x0E, [0x2B] = 0x0F, [0x2C] = 0x39, [0x2D] = 0x0C, [0x2E] = 0x0D, [0x2F] = 0x1A, [0x30] = 0x1B, [0x31] = 0x2B, [0x33] = 0x27, [0x34] = 0x28, [0x35] = 0x29, [0x36] = 0x33, [0x37] = 0x34, [0x38] = 0x35, [0x39] = 0x3A, [0x3A] = 0x3B, [0x3B] = 0x3C, [0x3C] = 0x3D, [0x3D] = 0x3E, [0x3E] = 0x3F, [0x3F] = 0x40, [0x40] = 0x41, [0x41] = 0x42, [0x42] = 0x43, [0x43] = 0x44, [0x44] = 0x57, [0x45] = 0x58, [0x49] = K_EXT | 0x52, [0x4A] = K_EXT | 0x47, [0x4B] = K_EXT | 0x49, [0x4C] = K_EXT | 0x53, [0x4D] = K_EXT | 0x4F, [0x4E] = K_EXT | 0x51, [0x4F] = K_EXT | 0x4D, [0x50] = K_EXT | 0x4B, [0x51] = K_EXT | 0x50, [0x52] = K_EXT | 0x48, }; /* Modifier bit -> PS/2 make code: L/R ctrl, shift, alt, GUI. */ static const unsigned short hid_mod_sc[8] = { 0x1D, K_EXT | 0x1D, /* L Ctrl, R Ctrl */ 0x2A, 0x36, /* L Shift, R Shift */ 0x38, K_EXT | 0x38, /* L Alt, R Alt */ K_EXT | 0x5B, K_EXT | 0x5C, /* L GUI, R GUI */ }; static const unsigned char hid_mod_bit[8] = { 0x01, 0x10, 0x02, 0x20, 0x04, 0x40, 0x08, 0x80, }; static void kbd_inject(unsigned int code, int down) { if (!code) return; if (code & K_EXT) { io_inject_scancode(0xE0); io_inject_scancode((unsigned char)((code & 0xFFu) | (down ? 0 : 0x80))); } else { io_inject_scancode((unsigned char)(code | (down ? 0 : 0x80))); } } /* Boot keyboard report: [modifiers][reserved][key0..key5]. Diff against the previous report to derive make/break scancodes. */ static void kbd_decode(struct hid_dev *d, const unsigned char *r) { unsigned int i, j; for (i = 0; i < 8; i++) { int was = (d->prev_mod & hid_mod_bit[i]) != 0; int now = (r[0] & hid_mod_bit[i]) != 0; if (was != now) kbd_inject(hid_mod_sc[i], now); } d->prev_mod = r[0]; /* Releases: in the previous report, gone from this one. */ for (i = 0; i < 6; i++) { if (!d->prev[i]) continue; for (j = 0; j < 6; j++) { if (r[2 + j] == d->prev[i]) break; } if (j == 6) kbd_inject(hid_to_sc[d->prev[i]], 0); } /* Presses: new in this report. */ for (i = 0; i < 6; i++) { if (!r[2 + i]) continue; for (j = 0; j < 6; j++) { if (d->prev[j] == r[2 + i]) break; } if (j == 6) kbd_inject(hid_to_sc[r[2 + i]], 1); } for (i = 0; i < 6; i++) d->prev[i] = r[2 + i]; } /* --- report-descriptor classification ----------------------------------- */ /* Minimal HID item walk: find a Generic Desktop (usage page 1) mouse or keyboard collection. Mouse detection accepts a Mouse/Pointer collection or a relative X/Y input item (so absolute tablets are not claimed). */ static unsigned int hid_classify(const unsigned char *rep, unsigned int n) { int mouse = 0, kbd = 0; unsigned int i = 0, page = 0; int loc_mouse = 0, loc_kbd = 0, loc_xy = 0; while (i < n) { unsigned int item = rep[i++]; unsigned int size = item & 3u; unsigned int type = (item >> 2) & 3u; unsigned int tag = (item >> 4) & 0xFu; unsigned int data = 0, k; if (size == 3u) size = 4u; for (k = 0; k < size; k++) { if (i + k < (unsigned int)n) data |= (unsigned int)rep[i + k] << (8u * k); } i += size; if (type == 1u && tag == 0u) { /* Global Usage Page */ page = data; } else if (type == 2u && tag == 0u && page == 0x01u) { if (data == 0x01u || data == 0x02u) /* Pointer/Mouse */ loc_mouse = 1; else if (data == 0x06u) /* Keyboard */ loc_kbd = 1; else if (data == 0x30u || data == 0x31u) /* X/Y */ loc_xy = 1; } else if (type == 0u) { /* Main item */ if (tag == 0xAu) { /* Collection */ if (loc_mouse) mouse = 1; if (loc_kbd) kbd = 1; } else if (tag == 0x8u && loc_xy && (data & 0x04u)) { /* Input, relative */ mouse = 1; } loc_mouse = loc_kbd = loc_xy = 0; } } if (mouse) return KIND_MOUSE; if (kbd) return KIND_KBD; return KIND_FREE; } /* --- manager ------------------------------------------------------------ */ /* Read up to `cap` bytes of the interface's HID report descriptor. The * request stays within UHCI's 30-TD control limit for an 8-byte EP0. */ static int read_report_desc(const struct usb_iface_info *in, unsigned char *buf, unsigned int cap) { return usb_control_in(in->ctrl, in->addr, in->mps, in->low_speed, RT_DEV2HOST_CLS_IF, REQ_GET_DESCRIPTOR, (unsigned short)(0x22u << 8), (unsigned short)in->iface_num, buf, cap); } static int hid_probe(const struct usb_iface_info *in) { struct hid_dev *d = 0; struct hid_mouse_layout layout; unsigned char *desc = 0; unsigned int desc_len = 0; unsigned int kind, i; int layout_ok = 0; if (in->cls != 0x03u || !in->ep_addr || !(in->ep_addr & 0x80u)) return 0; for (i = 0; i < sizeof(layout); i++) ((unsigned char *)&layout)[i] = 0; /* Logitech Unifying receiver: interface 2 is the HID++/DJ channel * that carries the paired device's reports. Its vendor report * descriptor is not a mouse collection, so claim it explicitly. */ if (in->vendor == 0x046du && in->product == 0xc52bu && in->iface_num == 2 && in->subcls == 0x00u && in->proto == 0x00u) kind = KIND_DJ; else if (in->subcls == 0x01u && in->proto == 0x01u) kind = KIND_KBD; else { int n; desc = (unsigned char *)mm_alloc_page(); if (desc) { n = read_report_desc(in, desc, REPORT_MAX); if (n > 0) desc_len = (unsigned int)n; } if (in->subcls == 0x01u && in->proto == 0x02u) kind = KIND_MOUSE; else kind = desc_len ? hid_classify(desc, desc_len) : KIND_FREE; if (kind == KIND_MOUSE && desc_len) layout_ok = hid_parse_mouse(desc, desc_len, &layout); } if (kind == KIND_FREE) { if (desc) mm_free_page((unsigned int)desc); return 0; } for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (!devs[i].present && !devs[i].claimed) { d = &devs[i]; break; } } if (!d) { if (desc) mm_free_page((unsigned int)desc); return 0; /* every slot is busy */ } d->report_mode = 0; d->layout.flags = 0; /* The endpoint is unusable until the device is configured. */ usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed, RT_HOST2DEV_STD, REQ_SET_CONFIGURATION, (unsigned short)(in->config_value & 0xFFu), 0); /* A wheel lives only in the report descriptor: boot protocol has no * field for it. Such a mouse runs in report protocol, and the fixed * boot layout stays the fallback for everything else (and for a * device that refuses the protocol switch). */ if (kind == KIND_MOUSE && layout_ok && layout.wheel_size) { if (in->subcls != 0x01u || usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed, RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 1, in->iface_num) == 0) d->report_mode = 1; else usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed, RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 0, in->iface_num); } else if (in->subcls == 0x01u) { /* Boot protocol for boot-class devices; a stall is tolerated. */ usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed, RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 0, in->iface_num); } if (desc) mm_free_page((unsigned int)desc); d->kind = kind; d->ctrl = in->ctrl; d->addr = in->addr; d->ep = in->ep_addr & 0x0Fu; d->mps = in->ep_mps ? in->ep_mps : 8; d->xfer = kind == KIND_DJ ? USB_INT_MAX : (in->ep_mps ? in->ep_mps : 8); if (d->xfer > USB_INT_MAX) d->xfer = USB_INT_MAX; d->port = in->port; d->low = in->low_speed; if (layout_ok) d->layout = layout; d->claimed = 1; return 1; } static void dev_attach(struct hid_dev *d, unsigned int slot) { unsigned int i; if (usb_int_in_start(d->ctrl, slot, d->addr, d->ep, d->mps, d->xfer, d->low) != 0) return; d->err = 0; d->disc = 0; d->present = 1; d->prev_mod = 0; for (i = 0; i < 6; i++) d->prev[i] = 0; if (d->kind == KIND_DJ) serial_puts(SERIAL_COM1, "usbhid: Logitech DJ receiver attached ("); else if (d->kind == KIND_MOUSE) serial_puts(SERIAL_COM1, "usbhid: mouse attached ("); else serial_puts(SERIAL_COM1, "usbhid: keyboard attached ("); serial_puts(SERIAL_COM1, usb_hcd_name_ctrl(d->ctrl)); serial_puts(SERIAL_COM1, " ctrl "); serial_putdec(SERIAL_COM1, d->ctrl); serial_puts(SERIAL_COM1, ", addr "); serial_putdec(SERIAL_COM1, d->addr); serial_puts(SERIAL_COM1, " ep "); serial_putdec(SERIAL_COM1, 0x80u | d->ep); if (d->report_mode) serial_puts(SERIAL_COM1, ", report protocol + wheel"); serial_puts(SERIAL_COM1, ")\n"); } static void dev_detach(struct hid_dev *d, unsigned int slot) { usb_int_in_stop(slot); d->present = 0; d->claimed = 0; /* A false port-loss leaves the device attached but unreachable (no * CSC to wake the hotplug watcher). A clean full rescan re-arms * everything, and is the only reliable recovery on this SB950. */ need_rescan = 1; last_watch = get_ticks_ms() - WATCH_MS; /* watch again soon */ serial_puts(SERIAL_COM1, d->kind == KIND_DJ ? "usbhid: Logitech DJ receiver detached (addr " : d->kind == KIND_MOUSE ? "usbhid: mouse detached (addr " : "usbhid: keyboard detached (addr "); serial_putdec(SERIAL_COM1, d->addr); serial_puts(SERIAL_COM1, ")\n"); d->kind = KIND_FREE; } static void arm_claimed(void) { unsigned int i; for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (devs[i].claimed) { dev_attach(&devs[i], i); devs[i].claimed = 0; } } } static void hid_scan(void) { unsigned int i; /* A full scan resets every root port: existing handles are stale. */ for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (devs[i].kind != KIND_FREE) { usb_int_in_stop(i); devs[i].kind = KIND_FREE; devs[i].present = 0; devs[i].claimed = 0; } } if (usb_scan(hid_probe) != 0) return; /* busy: retried from the watcher */ need_rescan = 0; gen = usb_generation(); arm_claimed(); } void usbhid_init(void) { /* usb_scan() brings the controllers up itself, under the scan claim; * hcd_ok stays 0 until it returns, so usbhid_poll() keeps out. */ int r = usb_scan(hid_probe); if (r == -1) { serial_puts(SERIAL_COM1, "usbhid: no USB host controller\n"); hcd_ok = 0; return; } hcd_ok = 1; if (r != 0) { /* A scan is already running (unlikely at boot): finish from the watcher. */ need_rescan = 1; gen = usb_generation(); return; } gen = usb_generation(); arm_claimed(); if (!usbhid_mouse_attached() && !usbhid_keyboard_attached()) serial_puts(SERIAL_COM1, "usbhid: no HID boot mouse/keyboard found\n"); } /* Service one slot: detach on disconnect/errors, return report length. */ static int dev_poll(struct hid_dev *d, unsigned int slot, unsigned char *buf, unsigned int len) { int n; if (!d->present) return 0; /* CCS can glitch for a frame on the SB950; a single read must not * detach a working device. Require three consecutive misses. */ if (d->port != 0xFFu && !usb_port_connected(d->ctrl, d->port)) { if (++d->disc >= 3) dev_detach(d, slot); return 0; } d->disc = 0; n = usb_int_in_poll(slot, buf, len); if (n < 0) { /* The HCD re-arms on every error; a short burst is normal on * a marginal low-speed link and must not kill the device. */ if (++d->err >= 50) dev_detach(d, slot); return 0; } d->err = 0; return n; } static int dj_seen; static int mouse_seen; static void dj_decode(const unsigned char *r, unsigned int n, unsigned int *buttons, int *dx, int *dy, int *scroll, int *got) { const unsigned char *p; unsigned int btn; int x, y; if (n < 10 || (r[0] != 0x20u && r[0] != 0x21u)) return; /* Device index 1..7, standard mouse report type. The receiver's * connection/notification reports (0x40/0x41/0x42) and other device * types are ignored. */ if (r[1] < 1 || r[1] > 7 || r[2] != 0x02u) return; p = r + 3; btn = p[0] | ((unsigned int)p[1] << 8); x = p[2] | ((p[3] & 0x0Fu) << 8); y = (p[3] >> 4) | (p[4] << 4); if (x & 0x800) x -= 0x1000; /* 12-bit signed relative X */ if (y & 0x800) y -= 0x1000; *buttons |= btn & 0x07u; *dx += x; *dy -= y; /* HID +y down; mouse.c expects +y up */ if (scroll) *scroll -= (int)(signed char)p[5] * USB_WHEEL_PX; *got = 1; if (!dj_seen) { dj_seen = 1; serial_puts(SERIAL_COM1, "usbhid: first DJ mouse report\n"); } } int usbhid_poll(unsigned int *buttons, int *dx, int *dy, int *scroll) { unsigned char buf[USB_INT_MAX]; unsigned int now, i, all_busy = 1; unsigned int b = 0; int n, x = 0, y = 0, got = 0; if (scroll) *scroll = 0; if (!hcd_ok) return 0; /* Another context's scan is sleeping between port resets: the * controllers are its until it finishes. */ if (usb_scan_foreign()) return 0; /* A full bus re-scan (`lsusb`) invalidates every handle. */ if (usb_generation() != gen) { for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (devs[i].present) dev_detach(&devs[i], i); } need_rescan = 1; } /* Throttled: finish a pending rescan, or watch for hotplug. */ for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (devs[i].kind == KIND_FREE) all_busy = 0; } now = get_ticks_ms(); if (now - last_watch >= WATCH_MS) { last_watch = now; if (need_rescan) { hid_scan(); } else if (!all_busy) { for (i = 0; i < USB_HCD_INT_SLOTS; i++) devs[i].claimed = 0; if (usb_hotplug_poll(hid_probe) > 0) arm_claimed(); } } for (i = 0; i < USB_HCD_INT_SLOTS; i++) { struct hid_dev *d = &devs[i]; if (d->kind == KIND_FREE) continue; n = dev_poll(d, i, buf, sizeof(buf)); if (n >= 3 && d->kind == KIND_MOUSE) { if (d->report_mode) { struct hid_mouse_frame f; if (hid_mouse_decode(&d->layout, buf, (unsigned int)n, &f)) { b |= f.buttons; x += f.dx; /* HID: +y is down; mouse.c expects * +y = up. */ y -= f.dy; if (scroll) *scroll -= f.wheel * USB_WHEEL_PX; got = 1; if (!mouse_seen) { mouse_seen = 1; serial_puts(SERIAL_COM1, "usbhid: first report-protocol mouse report\n"); } } } else { b |= buf[0] & 0x07u; x += (int)(signed char)buf[1]; /* HID: +y is down; mouse.c expects +y = up. */ y -= (int)(signed char)buf[2]; got = 1; if (!mouse_seen) { mouse_seen = 1; serial_puts(SERIAL_COM1, "usbhid: first boot mouse report\n"); } } } else if (d->kind == KIND_DJ && n >= 10) { dj_decode(buf, (unsigned int)n, &b, &x, &y, scroll, &got); } else if (n >= 8 && d->kind == KIND_KBD) { kbd_decode(d, buf); } } if (got) { *buttons = b; *dx = x; *dy = y; } return got; } int usbhid_mouse_attached(void) { unsigned int i; for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if ((devs[i].kind == KIND_MOUSE || devs[i].kind == KIND_DJ) && devs[i].present) return 1; } return 0; } int usbhid_keyboard_attached(void) { unsigned int i; for (i = 0; i < USB_HCD_INT_SLOTS; i++) { if (devs[i].kind == KIND_KBD && devs[i].present) return 1; } return 0; } void usbhid_get_info(unsigned int *ctrl, unsigned int *addr, unsigned int *ep, unsigned int *mps, int *low, int *report_mode, int *wheel) { unsigned int i; for (i = 0; i < USB_HCD_INT_SLOTS; i++) { struct hid_dev *d = &devs[i]; if (d->kind == KIND_MOUSE || d->kind == KIND_DJ) { if (!d->present) continue; *ctrl = d->ctrl; *addr = d->addr; *ep = d->ep; *mps = d->mps; *low = d->low; *report_mode = d->kind == KIND_DJ ? 2 : (int)d->report_mode; *wheel = d->kind == KIND_DJ || (d->report_mode && d->layout.wheel_size); return; } } *ctrl = 0; *addr = 0; *ep = 0; *mps = 0; *low = 0; *report_mode = 0; *wheel = 0; }