usbhid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* USB HID boot devices: mice + keyboards.
  2. *
  3. * The probe claims every HID interface it understands: boot mouse/keyboard
  4. * (class 3 / subclass 1 / protocol 2 or 1) or report-protocol HID
  5. * interfaces whose report descriptor says "mouse"/"pointer" or "keyboard"
  6. * (class 3, other subclasses — many modern and composite devices). Each
  7. * claimed interface gets its own interrupt slot, so a composite device
  8. * (keyboard + mouse in one) and several devices can coexist.
  9. *
  10. * Mouse reports are handed back to mouse.c; keyboard reports are decoded
  11. * into PS/2 set-1 scancodes and injected into the regular input queue
  12. * (io_inject_scancode), so the terminal/WM see them exactly like a PS/2
  13. * keyboard. Root ports are watched so devices can be plugged in later. */
  14. #include "usbhid.h"
  15. #include "io.h"
  16. #include "usb.h"
  17. #include "serial.h"
  18. #include "timer.h"
  19. /* Request types/requests used here. */
  20. #define RT_HOST2DEV_STD 0x00u
  21. #define RT_HOST2DEV_CLS 0x21u
  22. #define RT_DEV2HOST_CLS_IF 0x81u /* class request to an interface, IN */
  23. #define REQ_GET_DESCRIPTOR 0x06u
  24. #define REQ_SET_CONFIGURATION 0x09u
  25. #define REQ_SET_PROTOCOL 0x0Bu
  26. /* Hotplug watcher period (must match usb.c's internal throttle). */
  27. #define WATCH_MS 250
  28. /* Extended (0xE0-prefixed) PS/2 make codes in the mapping table. */
  29. #define K_EXT 0xE000u
  30. /* How much of a report descriptor to read for classification. */
  31. #define REPORT_MAX 64
  32. enum { KIND_FREE = 0, KIND_MOUSE, KIND_KBD };
  33. struct hid_dev {
  34. unsigned int kind; /* KIND_*, KIND_FREE = slot unused */
  35. int present; /* endpoint armed */
  36. int claimed; /* claimed by the scan in progress */
  37. unsigned int ctrl, addr, ep, mps, port;
  38. int low;
  39. unsigned int err; /* consecutive transfer errors */
  40. unsigned int disc; /* consecutive "port disconnected" reads */
  41. /* keyboard report-diff state */
  42. unsigned char prev[6];
  43. unsigned char prev_mod;
  44. };
  45. static struct hid_dev devs[USB_HCD_INT_SLOTS];
  46. static int hcd_ok;
  47. static int need_rescan;
  48. static unsigned int gen;
  49. static unsigned int last_watch;
  50. /* --- keyboard report decoding ------------------------------------------ */
  51. /* HID keyboard usage -> PS/2 set-1 make code (0 = unmapped). */
  52. static const unsigned short hid_to_sc[256] = {
  53. [0x04] = 0x1E, [0x05] = 0x30, [0x06] = 0x2E, [0x07] = 0x20,
  54. [0x08] = 0x12, [0x09] = 0x21, [0x0A] = 0x22, [0x0B] = 0x23,
  55. [0x0C] = 0x17, [0x0D] = 0x24, [0x0E] = 0x25, [0x0F] = 0x26,
  56. [0x10] = 0x32, [0x11] = 0x31, [0x12] = 0x18, [0x13] = 0x19,
  57. [0x14] = 0x10, [0x15] = 0x13, [0x16] = 0x1F, [0x17] = 0x14,
  58. [0x18] = 0x16, [0x19] = 0x2F, [0x1A] = 0x11, [0x1B] = 0x2D,
  59. [0x1C] = 0x15, [0x1D] = 0x2C,
  60. [0x1E] = 0x02, [0x1F] = 0x03, [0x20] = 0x04, [0x21] = 0x05,
  61. [0x22] = 0x06, [0x23] = 0x07, [0x24] = 0x08, [0x25] = 0x09,
  62. [0x26] = 0x0A, [0x27] = 0x0B,
  63. [0x28] = 0x1C, [0x29] = 0x01, [0x2A] = 0x0E, [0x2B] = 0x0F,
  64. [0x2C] = 0x39, [0x2D] = 0x0C, [0x2E] = 0x0D, [0x2F] = 0x1A,
  65. [0x30] = 0x1B, [0x31] = 0x2B, [0x33] = 0x27, [0x34] = 0x28,
  66. [0x35] = 0x29, [0x36] = 0x33, [0x37] = 0x34, [0x38] = 0x35,
  67. [0x39] = 0x3A,
  68. [0x3A] = 0x3B, [0x3B] = 0x3C, [0x3C] = 0x3D, [0x3D] = 0x3E,
  69. [0x3E] = 0x3F, [0x3F] = 0x40, [0x40] = 0x41, [0x41] = 0x42,
  70. [0x42] = 0x43, [0x43] = 0x44, [0x44] = 0x57, [0x45] = 0x58,
  71. [0x49] = K_EXT | 0x52, [0x4A] = K_EXT | 0x47, [0x4B] = K_EXT | 0x49,
  72. [0x4C] = K_EXT | 0x53, [0x4D] = K_EXT | 0x4F, [0x4E] = K_EXT | 0x51,
  73. [0x4F] = K_EXT | 0x4D, [0x50] = K_EXT | 0x4B, [0x51] = K_EXT | 0x50,
  74. [0x52] = K_EXT | 0x48,
  75. };
  76. /* Modifier bit -> PS/2 make code: L/R ctrl, shift, alt, GUI. */
  77. static const unsigned short hid_mod_sc[8] = {
  78. 0x1D, K_EXT | 0x1D, /* L Ctrl, R Ctrl */
  79. 0x2A, 0x36, /* L Shift, R Shift */
  80. 0x38, K_EXT | 0x38, /* L Alt, R Alt */
  81. K_EXT | 0x5B, K_EXT | 0x5C, /* L GUI, R GUI */
  82. };
  83. static const unsigned char hid_mod_bit[8] = {
  84. 0x01, 0x10, 0x02, 0x20, 0x04, 0x40, 0x08, 0x80,
  85. };
  86. static void kbd_inject(unsigned int code, int down)
  87. {
  88. if (!code)
  89. return;
  90. if (code & K_EXT) {
  91. io_inject_scancode(0xE0);
  92. io_inject_scancode((unsigned char)((code & 0xFFu) |
  93. (down ? 0 : 0x80)));
  94. } else {
  95. io_inject_scancode((unsigned char)(code | (down ? 0 : 0x80)));
  96. }
  97. }
  98. /* Boot keyboard report: [modifiers][reserved][key0..key5]. Diff against
  99. the previous report to derive make/break scancodes. */
  100. static void kbd_decode(struct hid_dev *d, const unsigned char *r)
  101. {
  102. unsigned int i, j;
  103. for (i = 0; i < 8; i++) {
  104. int was = (d->prev_mod & hid_mod_bit[i]) != 0;
  105. int now = (r[0] & hid_mod_bit[i]) != 0;
  106. if (was != now)
  107. kbd_inject(hid_mod_sc[i], now);
  108. }
  109. d->prev_mod = r[0];
  110. /* Releases: in the previous report, gone from this one. */
  111. for (i = 0; i < 6; i++) {
  112. if (!d->prev[i])
  113. continue;
  114. for (j = 0; j < 6; j++) {
  115. if (r[2 + j] == d->prev[i])
  116. break;
  117. }
  118. if (j == 6)
  119. kbd_inject(hid_to_sc[d->prev[i]], 0);
  120. }
  121. /* Presses: new in this report. */
  122. for (i = 0; i < 6; i++) {
  123. if (!r[2 + i])
  124. continue;
  125. for (j = 0; j < 6; j++) {
  126. if (d->prev[j] == r[2 + i])
  127. break;
  128. }
  129. if (j == 6)
  130. kbd_inject(hid_to_sc[r[2 + i]], 1);
  131. }
  132. for (i = 0; i < 6; i++)
  133. d->prev[i] = r[2 + i];
  134. }
  135. /* --- report-descriptor classification ----------------------------------- */
  136. /* Minimal HID item walk: find a Generic Desktop (usage page 1) mouse or
  137. keyboard collection. Mouse detection accepts a Mouse/Pointer collection
  138. or a relative X/Y input item (so absolute tablets are not claimed). */
  139. static unsigned int hid_classify(const struct usb_iface_info *in)
  140. {
  141. static unsigned char rep[REPORT_MAX];
  142. int n, mouse = 0, kbd = 0;
  143. unsigned int i = 0, page = 0;
  144. int loc_mouse = 0, loc_kbd = 0, loc_xy = 0;
  145. n = usb_control_in(in->ctrl, in->addr, in->mps, in->low_speed,
  146. RT_DEV2HOST_CLS_IF, REQ_GET_DESCRIPTOR,
  147. (unsigned short)(0x22u << 8),
  148. (unsigned short)in->iface_num, rep, sizeof(rep));
  149. if (n <= 0)
  150. return KIND_FREE;
  151. while (i < (unsigned int)n) {
  152. unsigned int item = rep[i++];
  153. unsigned int size = item & 3u;
  154. unsigned int type = (item >> 2) & 3u;
  155. unsigned int tag = (item >> 4) & 0xFu;
  156. unsigned int data = 0, k;
  157. if (size == 3u)
  158. size = 4u;
  159. for (k = 0; k < size; k++) {
  160. if (i + k < (unsigned int)n)
  161. data |= (unsigned int)rep[i + k] << (8u * k);
  162. }
  163. i += size;
  164. if (type == 1u && tag == 0u) { /* Global Usage Page */
  165. page = data;
  166. } else if (type == 2u && tag == 0u && page == 0x01u) {
  167. if (data == 0x01u || data == 0x02u) /* Pointer/Mouse */
  168. loc_mouse = 1;
  169. else if (data == 0x06u) /* Keyboard */
  170. loc_kbd = 1;
  171. else if (data == 0x30u || data == 0x31u) /* X/Y */
  172. loc_xy = 1;
  173. } else if (type == 0u) { /* Main item */
  174. if (tag == 0xAu) { /* Collection */
  175. if (loc_mouse)
  176. mouse = 1;
  177. if (loc_kbd)
  178. kbd = 1;
  179. } else if (tag == 0x8u && loc_xy &&
  180. (data & 0x04u)) { /* Input, relative */
  181. mouse = 1;
  182. }
  183. loc_mouse = loc_kbd = loc_xy = 0;
  184. }
  185. }
  186. if (mouse)
  187. return KIND_MOUSE;
  188. if (kbd)
  189. return KIND_KBD;
  190. return KIND_FREE;
  191. }
  192. /* --- manager ------------------------------------------------------------ */
  193. static int hid_probe(const struct usb_iface_info *in)
  194. {
  195. struct hid_dev *d = 0;
  196. unsigned int kind, i;
  197. if (in->cls != 0x03u || !in->ep_addr || !(in->ep_addr & 0x80u))
  198. return 0;
  199. if (in->subcls == 0x01u && in->proto == 0x02u)
  200. kind = KIND_MOUSE;
  201. else if (in->subcls == 0x01u && in->proto == 0x01u)
  202. kind = KIND_KBD;
  203. else
  204. kind = hid_classify(in);
  205. if (kind == KIND_FREE)
  206. return 0;
  207. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  208. if (!devs[i].present && !devs[i].claimed) {
  209. d = &devs[i];
  210. break;
  211. }
  212. }
  213. if (!d)
  214. return 0; /* every slot is busy */
  215. /* The endpoint is unusable until the device is configured. */
  216. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  217. RT_HOST2DEV_STD, REQ_SET_CONFIGURATION,
  218. (unsigned short)(in->config_value & 0xFFu), 0);
  219. /* Boot protocol for boot-class devices; a stall is tolerated. */
  220. if (in->subcls == 0x01u)
  221. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  222. RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 0,
  223. in->iface_num);
  224. d->kind = kind;
  225. d->ctrl = in->ctrl;
  226. d->addr = in->addr;
  227. d->ep = in->ep_addr & 0x0Fu;
  228. d->mps = in->ep_mps ? in->ep_mps : 8;
  229. d->port = in->port;
  230. d->low = in->low_speed;
  231. d->claimed = 1;
  232. return 1;
  233. }
  234. static void dev_attach(struct hid_dev *d, unsigned int slot)
  235. {
  236. unsigned int i;
  237. if (usb_int_in_start(d->ctrl, slot, d->addr, d->ep, d->mps, d->low) != 0)
  238. return;
  239. d->err = 0;
  240. d->disc = 0;
  241. d->present = 1;
  242. d->prev_mod = 0;
  243. for (i = 0; i < 6; i++)
  244. d->prev[i] = 0;
  245. serial_puts(SERIAL_COM1, d->kind == KIND_MOUSE
  246. ? "usbhid: mouse attached ("
  247. : "usbhid: keyboard attached (");
  248. serial_puts(SERIAL_COM1, usb_hcd_name_ctrl(d->ctrl));
  249. serial_puts(SERIAL_COM1, ", addr ");
  250. serial_putdec(SERIAL_COM1, d->addr);
  251. serial_puts(SERIAL_COM1, " ep ");
  252. serial_putdec(SERIAL_COM1, 0x80u | d->ep);
  253. serial_puts(SERIAL_COM1, ")\n");
  254. }
  255. static void dev_detach(struct hid_dev *d, unsigned int slot)
  256. {
  257. usb_int_in_stop(slot);
  258. d->present = 0;
  259. d->claimed = 0;
  260. /* A false port-loss leaves the device attached but unreachable (no
  261. * CSC to wake the hotplug watcher). A clean full rescan re-arms
  262. * everything, and is the only reliable recovery on this SB950. */
  263. need_rescan = 1;
  264. last_watch = get_ticks_ms() - WATCH_MS; /* watch again soon */
  265. serial_puts(SERIAL_COM1, d->kind == KIND_MOUSE
  266. ? "usbhid: mouse detached (addr "
  267. : "usbhid: keyboard detached (addr ");
  268. serial_putdec(SERIAL_COM1, d->addr);
  269. serial_puts(SERIAL_COM1, ")\n");
  270. d->kind = KIND_FREE;
  271. }
  272. static void arm_claimed(void)
  273. {
  274. unsigned int i;
  275. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  276. if (devs[i].claimed) {
  277. dev_attach(&devs[i], i);
  278. devs[i].claimed = 0;
  279. }
  280. }
  281. }
  282. static void hid_scan(void)
  283. {
  284. unsigned int i;
  285. /* A full scan resets every root port: existing handles are stale. */
  286. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  287. if (devs[i].kind != KIND_FREE) {
  288. usb_int_in_stop(i);
  289. devs[i].kind = KIND_FREE;
  290. devs[i].present = 0;
  291. devs[i].claimed = 0;
  292. }
  293. }
  294. if (usb_scan(hid_probe) != 0)
  295. return; /* busy: retried from the watcher */
  296. need_rescan = 0;
  297. gen = usb_generation();
  298. arm_claimed();
  299. }
  300. void usbhid_init(void)
  301. {
  302. /* usb_scan() brings the controllers up itself, under the scan claim;
  303. * hcd_ok stays 0 until it returns, so usbhid_poll() keeps out. */
  304. int r = usb_scan(hid_probe);
  305. if (r == -1) {
  306. serial_puts(SERIAL_COM1, "usbhid: no USB host controller\n");
  307. hcd_ok = 0;
  308. return;
  309. }
  310. hcd_ok = 1;
  311. if (r != 0) {
  312. /* A scan is already running (unlikely at boot): finish from
  313. the watcher. */
  314. need_rescan = 1;
  315. gen = usb_generation();
  316. return;
  317. }
  318. gen = usb_generation();
  319. arm_claimed();
  320. if (!usbhid_mouse_attached() && !usbhid_keyboard_attached())
  321. serial_puts(SERIAL_COM1,
  322. "usbhid: no HID boot mouse/keyboard found\n");
  323. }
  324. /* Service one slot: detach on disconnect/errors, return report length. */
  325. static int dev_poll(struct hid_dev *d, unsigned int slot, unsigned char *buf,
  326. unsigned int len)
  327. {
  328. int n;
  329. if (!d->present)
  330. return 0;
  331. /* CCS can glitch for a frame on the SB950; a single read must not
  332. * detach a working device. Require three consecutive misses. */
  333. if (d->port != 0xFFu && !usb_port_connected(d->ctrl, d->port)) {
  334. if (++d->disc >= 3)
  335. dev_detach(d, slot);
  336. return 0;
  337. }
  338. d->disc = 0;
  339. n = usb_int_in_poll(slot, buf, len);
  340. if (n < 0) {
  341. /* The HCD re-arms on every error; a short burst is normal on
  342. * a marginal low-speed link and must not kill the device. */
  343. if (++d->err >= 50)
  344. dev_detach(d, slot);
  345. return 0;
  346. }
  347. d->err = 0;
  348. return n;
  349. }
  350. int usbhid_poll(unsigned int *buttons, int *dx, int *dy, int *scroll)
  351. {
  352. unsigned char buf[8];
  353. unsigned int now, i, all_busy = 1;
  354. unsigned int b = 0;
  355. int n, x = 0, y = 0, got = 0;
  356. if (scroll)
  357. *scroll = 0;
  358. if (!hcd_ok)
  359. return 0;
  360. /* Another context's scan is sleeping between port resets: the
  361. * controllers are its until it finishes. */
  362. if (usb_scan_foreign())
  363. return 0;
  364. /* A full bus re-scan (`lsusb`) invalidates every handle. */
  365. if (usb_generation() != gen) {
  366. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  367. if (devs[i].present)
  368. dev_detach(&devs[i], i);
  369. }
  370. need_rescan = 1;
  371. }
  372. /* Throttled: finish a pending rescan, or watch for hotplug. */
  373. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  374. if (devs[i].kind == KIND_FREE)
  375. all_busy = 0;
  376. }
  377. now = get_ticks_ms();
  378. if (now - last_watch >= WATCH_MS) {
  379. last_watch = now;
  380. if (need_rescan) {
  381. hid_scan();
  382. } else if (!all_busy) {
  383. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  384. devs[i].claimed = 0;
  385. if (usb_hotplug_poll(hid_probe) > 0)
  386. arm_claimed();
  387. }
  388. }
  389. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  390. struct hid_dev *d = &devs[i];
  391. if (d->kind == KIND_FREE)
  392. continue;
  393. n = dev_poll(d, i, buf, sizeof(buf));
  394. if (n >= 3 && d->kind == KIND_MOUSE) {
  395. b |= buf[0] & 0x07u;
  396. x += (int)(signed char)buf[1];
  397. /* HID: +y is down; mouse.c expects +y = up. */
  398. y -= (int)(signed char)buf[2];
  399. got = 1;
  400. } else if (n >= 8 && d->kind == KIND_KBD) {
  401. kbd_decode(d, buf);
  402. }
  403. }
  404. if (got) {
  405. *buttons = b;
  406. *dx = x;
  407. *dy = y;
  408. }
  409. return got;
  410. }
  411. int usbhid_mouse_attached(void)
  412. {
  413. unsigned int i;
  414. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  415. if (devs[i].kind == KIND_MOUSE && devs[i].present)
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. int usbhid_keyboard_attached(void)
  421. {
  422. unsigned int i;
  423. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  424. if (devs[i].kind == KIND_KBD && devs[i].present)
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. void usbhid_get_info(unsigned int *ctrl, unsigned int *addr,
  430. unsigned int *ep, unsigned int *mps, int *low)
  431. {
  432. unsigned int i;
  433. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  434. struct hid_dev *d = &devs[i];
  435. if (d->kind == KIND_MOUSE && d->present) {
  436. *ctrl = d->ctrl;
  437. *addr = d->addr;
  438. *ep = d->ep;
  439. *mps = d->mps;
  440. *low = d->low;
  441. return;
  442. }
  443. }
  444. *ctrl = 0;
  445. *addr = 0;
  446. *ep = 0;
  447. *mps = 0;
  448. *low = 0;
  449. }