usbhid.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /* keyboard report-diff state */
  41. unsigned char prev[6];
  42. unsigned char prev_mod;
  43. };
  44. static struct hid_dev devs[USB_HCD_INT_SLOTS];
  45. static int hcd_ok;
  46. static int need_rescan;
  47. static unsigned int gen;
  48. static unsigned int last_watch;
  49. /* --- keyboard report decoding ------------------------------------------ */
  50. /* HID keyboard usage -> PS/2 set-1 make code (0 = unmapped). */
  51. static const unsigned short hid_to_sc[256] = {
  52. [0x04] = 0x1E, [0x05] = 0x30, [0x06] = 0x2E, [0x07] = 0x20,
  53. [0x08] = 0x12, [0x09] = 0x21, [0x0A] = 0x22, [0x0B] = 0x23,
  54. [0x0C] = 0x17, [0x0D] = 0x24, [0x0E] = 0x25, [0x0F] = 0x26,
  55. [0x10] = 0x32, [0x11] = 0x31, [0x12] = 0x18, [0x13] = 0x19,
  56. [0x14] = 0x10, [0x15] = 0x13, [0x16] = 0x1F, [0x17] = 0x14,
  57. [0x18] = 0x16, [0x19] = 0x2F, [0x1A] = 0x11, [0x1B] = 0x2D,
  58. [0x1C] = 0x15, [0x1D] = 0x2C,
  59. [0x1E] = 0x02, [0x1F] = 0x03, [0x20] = 0x04, [0x21] = 0x05,
  60. [0x22] = 0x06, [0x23] = 0x07, [0x24] = 0x08, [0x25] = 0x09,
  61. [0x26] = 0x0A, [0x27] = 0x0B,
  62. [0x28] = 0x1C, [0x29] = 0x01, [0x2A] = 0x0E, [0x2B] = 0x0F,
  63. [0x2C] = 0x39, [0x2D] = 0x0C, [0x2E] = 0x0D, [0x2F] = 0x1A,
  64. [0x30] = 0x1B, [0x31] = 0x2B, [0x33] = 0x27, [0x34] = 0x28,
  65. [0x35] = 0x29, [0x36] = 0x33, [0x37] = 0x34, [0x38] = 0x35,
  66. [0x39] = 0x3A,
  67. [0x3A] = 0x3B, [0x3B] = 0x3C, [0x3C] = 0x3D, [0x3D] = 0x3E,
  68. [0x3E] = 0x3F, [0x3F] = 0x40, [0x40] = 0x41, [0x41] = 0x42,
  69. [0x42] = 0x43, [0x43] = 0x44, [0x44] = 0x57, [0x45] = 0x58,
  70. [0x49] = K_EXT | 0x52, [0x4A] = K_EXT | 0x47, [0x4B] = K_EXT | 0x49,
  71. [0x4C] = K_EXT | 0x53, [0x4D] = K_EXT | 0x4F, [0x4E] = K_EXT | 0x51,
  72. [0x4F] = K_EXT | 0x4D, [0x50] = K_EXT | 0x4B, [0x51] = K_EXT | 0x50,
  73. [0x52] = K_EXT | 0x48,
  74. };
  75. /* Modifier bit -> PS/2 make code: L/R ctrl, shift, alt, GUI. */
  76. static const unsigned short hid_mod_sc[8] = {
  77. 0x1D, K_EXT | 0x1D, /* L Ctrl, R Ctrl */
  78. 0x2A, 0x36, /* L Shift, R Shift */
  79. 0x38, K_EXT | 0x38, /* L Alt, R Alt */
  80. K_EXT | 0x5B, K_EXT | 0x5C, /* L GUI, R GUI */
  81. };
  82. static const unsigned char hid_mod_bit[8] = {
  83. 0x01, 0x10, 0x02, 0x20, 0x04, 0x40, 0x08, 0x80,
  84. };
  85. static void kbd_inject(unsigned int code, int down)
  86. {
  87. if (!code)
  88. return;
  89. if (code & K_EXT) {
  90. io_inject_scancode(0xE0);
  91. io_inject_scancode((unsigned char)((code & 0xFFu) |
  92. (down ? 0 : 0x80)));
  93. } else {
  94. io_inject_scancode((unsigned char)(code | (down ? 0 : 0x80)));
  95. }
  96. }
  97. /* Boot keyboard report: [modifiers][reserved][key0..key5]. Diff against
  98. the previous report to derive make/break scancodes. */
  99. static void kbd_decode(struct hid_dev *d, const unsigned char *r)
  100. {
  101. unsigned int i, j;
  102. for (i = 0; i < 8; i++) {
  103. int was = (d->prev_mod & hid_mod_bit[i]) != 0;
  104. int now = (r[0] & hid_mod_bit[i]) != 0;
  105. if (was != now)
  106. kbd_inject(hid_mod_sc[i], now);
  107. }
  108. d->prev_mod = r[0];
  109. /* Releases: in the previous report, gone from this one. */
  110. for (i = 0; i < 6; i++) {
  111. if (!d->prev[i])
  112. continue;
  113. for (j = 0; j < 6; j++) {
  114. if (r[2 + j] == d->prev[i])
  115. break;
  116. }
  117. if (j == 6)
  118. kbd_inject(hid_to_sc[d->prev[i]], 0);
  119. }
  120. /* Presses: new in this report. */
  121. for (i = 0; i < 6; i++) {
  122. if (!r[2 + i])
  123. continue;
  124. for (j = 0; j < 6; j++) {
  125. if (d->prev[j] == r[2 + i])
  126. break;
  127. }
  128. if (j == 6)
  129. kbd_inject(hid_to_sc[r[2 + i]], 1);
  130. }
  131. for (i = 0; i < 6; i++)
  132. d->prev[i] = r[2 + i];
  133. }
  134. /* --- report-descriptor classification ----------------------------------- */
  135. /* Minimal HID item walk: find a Generic Desktop (usage page 1) mouse or
  136. keyboard collection. Mouse detection accepts a Mouse/Pointer collection
  137. or a relative X/Y input item (so absolute tablets are not claimed). */
  138. static unsigned int hid_classify(const struct usb_iface_info *in)
  139. {
  140. static unsigned char rep[REPORT_MAX];
  141. int n, mouse = 0, kbd = 0;
  142. unsigned int i = 0, page = 0;
  143. int loc_mouse = 0, loc_kbd = 0, loc_xy = 0;
  144. n = usb_control_in(in->ctrl, in->addr, in->mps, in->low_speed,
  145. RT_DEV2HOST_CLS_IF, REQ_GET_DESCRIPTOR,
  146. (unsigned short)(0x22u << 8),
  147. (unsigned short)in->iface_num, rep, sizeof(rep));
  148. if (n <= 0)
  149. return KIND_FREE;
  150. while (i < (unsigned int)n) {
  151. unsigned int item = rep[i++];
  152. unsigned int size = item & 3u;
  153. unsigned int type = (item >> 2) & 3u;
  154. unsigned int tag = (item >> 4) & 0xFu;
  155. unsigned int data = 0, k;
  156. if (size == 3u)
  157. size = 4u;
  158. for (k = 0; k < size; k++) {
  159. if (i + k < (unsigned int)n)
  160. data |= (unsigned int)rep[i + k] << (8u * k);
  161. }
  162. i += size;
  163. if (type == 1u && tag == 0u) { /* Global Usage Page */
  164. page = data;
  165. } else if (type == 2u && tag == 0u && page == 0x01u) {
  166. if (data == 0x01u || data == 0x02u) /* Pointer/Mouse */
  167. loc_mouse = 1;
  168. else if (data == 0x06u) /* Keyboard */
  169. loc_kbd = 1;
  170. else if (data == 0x30u || data == 0x31u) /* X/Y */
  171. loc_xy = 1;
  172. } else if (type == 0u) { /* Main item */
  173. if (tag == 0xAu) { /* Collection */
  174. if (loc_mouse)
  175. mouse = 1;
  176. if (loc_kbd)
  177. kbd = 1;
  178. } else if (tag == 0x8u && loc_xy &&
  179. (data & 0x04u)) { /* Input, relative */
  180. mouse = 1;
  181. }
  182. loc_mouse = loc_kbd = loc_xy = 0;
  183. }
  184. }
  185. if (mouse)
  186. return KIND_MOUSE;
  187. if (kbd)
  188. return KIND_KBD;
  189. return KIND_FREE;
  190. }
  191. /* --- manager ------------------------------------------------------------ */
  192. static int hid_probe(const struct usb_iface_info *in)
  193. {
  194. struct hid_dev *d = 0;
  195. unsigned int kind, i;
  196. if (in->cls != 0x03u || !in->ep_addr || !(in->ep_addr & 0x80u))
  197. return 0;
  198. if (in->subcls == 0x01u && in->proto == 0x02u)
  199. kind = KIND_MOUSE;
  200. else if (in->subcls == 0x01u && in->proto == 0x01u)
  201. kind = KIND_KBD;
  202. else
  203. kind = hid_classify(in);
  204. if (kind == KIND_FREE)
  205. return 0;
  206. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  207. if (!devs[i].present && !devs[i].claimed) {
  208. d = &devs[i];
  209. break;
  210. }
  211. }
  212. if (!d)
  213. return 0; /* every slot is busy */
  214. /* The endpoint is unusable until the device is configured. */
  215. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  216. RT_HOST2DEV_STD, REQ_SET_CONFIGURATION,
  217. (unsigned short)(in->config_value & 0xFFu), 0);
  218. /* Boot protocol for boot-class devices; a stall is tolerated. */
  219. if (in->subcls == 0x01u)
  220. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  221. RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 0,
  222. in->iface_num);
  223. d->kind = kind;
  224. d->ctrl = in->ctrl;
  225. d->addr = in->addr;
  226. d->ep = in->ep_addr & 0x0Fu;
  227. d->mps = in->ep_mps ? in->ep_mps : 8;
  228. d->port = in->port;
  229. d->low = in->low_speed;
  230. d->claimed = 1;
  231. return 1;
  232. }
  233. static void dev_attach(struct hid_dev *d, unsigned int slot)
  234. {
  235. unsigned int i;
  236. if (usb_int_in_start(d->ctrl, slot, d->addr, d->ep, d->mps, d->low) != 0)
  237. return;
  238. d->err = 0;
  239. d->present = 1;
  240. d->prev_mod = 0;
  241. for (i = 0; i < 6; i++)
  242. d->prev[i] = 0;
  243. serial_puts(SERIAL_COM1, d->kind == KIND_MOUSE
  244. ? "usbhid: mouse attached ("
  245. : "usbhid: keyboard attached (");
  246. serial_puts(SERIAL_COM1, usb_hcd_name_ctrl(d->ctrl));
  247. serial_puts(SERIAL_COM1, ", addr ");
  248. serial_putdec(SERIAL_COM1, d->addr);
  249. serial_puts(SERIAL_COM1, " ep ");
  250. serial_putdec(SERIAL_COM1, 0x80u | d->ep);
  251. serial_puts(SERIAL_COM1, ")\n");
  252. }
  253. static void dev_detach(struct hid_dev *d, unsigned int slot)
  254. {
  255. usb_int_in_stop(slot);
  256. d->present = 0;
  257. d->claimed = 0;
  258. last_watch = get_ticks_ms() - WATCH_MS; /* watch again soon */
  259. serial_puts(SERIAL_COM1, d->kind == KIND_MOUSE
  260. ? "usbhid: mouse detached (addr "
  261. : "usbhid: keyboard detached (addr ");
  262. serial_putdec(SERIAL_COM1, d->addr);
  263. serial_puts(SERIAL_COM1, ")\n");
  264. d->kind = KIND_FREE;
  265. }
  266. static void arm_claimed(void)
  267. {
  268. unsigned int i;
  269. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  270. if (devs[i].claimed) {
  271. dev_attach(&devs[i], i);
  272. devs[i].claimed = 0;
  273. }
  274. }
  275. }
  276. static void hid_scan(void)
  277. {
  278. unsigned int i;
  279. /* A full scan resets every root port: existing handles are stale. */
  280. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  281. if (devs[i].kind != KIND_FREE) {
  282. usb_int_in_stop(i);
  283. devs[i].kind = KIND_FREE;
  284. devs[i].present = 0;
  285. devs[i].claimed = 0;
  286. }
  287. }
  288. if (usb_scan(hid_probe) != 0)
  289. return; /* busy: retried from the watcher */
  290. need_rescan = 0;
  291. gen = usb_generation();
  292. arm_claimed();
  293. }
  294. void usbhid_init(void)
  295. {
  296. if (usb_hcd_init() != 0) {
  297. serial_puts(SERIAL_COM1, "usbhid: no USB host controller\n");
  298. hcd_ok = 0;
  299. return;
  300. }
  301. hcd_ok = 1;
  302. if (usb_scan(hid_probe) != 0) {
  303. /* A scan is already running (unlikely at boot): finish from
  304. the watcher. */
  305. need_rescan = 1;
  306. gen = usb_generation();
  307. return;
  308. }
  309. gen = usb_generation();
  310. arm_claimed();
  311. if (!usbhid_mouse_attached() && !usbhid_keyboard_attached())
  312. serial_puts(SERIAL_COM1,
  313. "usbhid: no HID boot mouse/keyboard found\n");
  314. }
  315. /* Service one slot: detach on disconnect/errors, return report length. */
  316. static int dev_poll(struct hid_dev *d, unsigned int slot, unsigned char *buf,
  317. unsigned int len)
  318. {
  319. int n;
  320. if (!d->present)
  321. return 0;
  322. if (d->port != 0xFFu && !usb_port_connected(d->ctrl, d->port)) {
  323. dev_detach(d, slot);
  324. return 0;
  325. }
  326. n = usb_int_in_poll(slot, buf, len);
  327. if (n < 0) {
  328. if (++d->err >= 5)
  329. dev_detach(d, slot);
  330. return 0;
  331. }
  332. d->err = 0;
  333. return n;
  334. }
  335. int usbhid_poll(unsigned int *buttons, int *dx, int *dy)
  336. {
  337. unsigned char buf[8];
  338. unsigned int now, i, all_busy = 1;
  339. unsigned int b = 0;
  340. int n, x = 0, y = 0, got = 0;
  341. if (!hcd_ok)
  342. return 0;
  343. /* A full bus re-scan (`lsusb`) invalidates every handle. */
  344. if (usb_generation() != gen) {
  345. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  346. if (devs[i].present)
  347. dev_detach(&devs[i], i);
  348. }
  349. need_rescan = 1;
  350. }
  351. /* Throttled: finish a pending rescan, or watch for hotplug. */
  352. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  353. if (devs[i].kind == KIND_FREE)
  354. all_busy = 0;
  355. }
  356. now = get_ticks_ms();
  357. if (now - last_watch >= WATCH_MS) {
  358. last_watch = now;
  359. if (need_rescan) {
  360. hid_scan();
  361. } else if (!all_busy) {
  362. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  363. devs[i].claimed = 0;
  364. if (usb_hotplug_poll(hid_probe) > 0)
  365. arm_claimed();
  366. }
  367. }
  368. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  369. struct hid_dev *d = &devs[i];
  370. if (d->kind == KIND_FREE)
  371. continue;
  372. n = dev_poll(d, i, buf, sizeof(buf));
  373. if (n >= 3 && d->kind == KIND_MOUSE) {
  374. b |= buf[0] & 0x07u;
  375. x += (int)(signed char)buf[1];
  376. /* HID: +y is down; mouse.c expects +y = up. */
  377. y -= (int)(signed char)buf[2];
  378. got = 1;
  379. } else if (n >= 8 && d->kind == KIND_KBD) {
  380. kbd_decode(d, buf);
  381. }
  382. }
  383. if (got) {
  384. *buttons = b;
  385. *dx = x;
  386. *dy = y;
  387. }
  388. return got;
  389. }
  390. int usbhid_mouse_attached(void)
  391. {
  392. unsigned int i;
  393. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  394. if (devs[i].kind == KIND_MOUSE && devs[i].present)
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. int usbhid_keyboard_attached(void)
  400. {
  401. unsigned int i;
  402. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  403. if (devs[i].kind == KIND_KBD && devs[i].present)
  404. return 1;
  405. }
  406. return 0;
  407. }
  408. void usbhid_get_info(unsigned int *ctrl, unsigned int *addr,
  409. unsigned int *ep, unsigned int *mps, int *low)
  410. {
  411. unsigned int i;
  412. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  413. struct hid_dev *d = &devs[i];
  414. if (d->kind == KIND_MOUSE && d->present) {
  415. *ctrl = d->ctrl;
  416. *addr = d->addr;
  417. *ep = d->ep;
  418. *mps = d->mps;
  419. *low = d->low;
  420. return;
  421. }
  422. }
  423. *ctrl = 0;
  424. *addr = 0;
  425. *ep = 0;
  426. *mps = 0;
  427. *low = 0;
  428. }