usbhid.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /* USB HID devices: mice, keyboards and the Logitech Unifying receiver.
  2. *
  3. * The probe claims every HID interface it understands: boot mouse/keyboard
  4. * (class 3 / subclass 1 / protocol 2 or 1), report-protocol HID interfaces
  5. * whose report descriptor says "mouse"/"pointer" or "keyboard" (class 3,
  6. * other subclasses — many modern and composite devices), and the vendor
  7. * DJ interface of a Logitech Unifying receiver. Each claimed interface
  8. * gets its own interrupt slot, so a composite device (keyboard + mouse in
  9. * one) and several devices can coexist.
  10. *
  11. * Mouse reports are handed back to mouse.c; keyboard reports are decoded
  12. * into PS/2 set-1 scancodes and injected into the regular input queue
  13. * (io_inject_scancode), so the terminal/WM see them exactly like a PS/2
  14. * keyboard. Root ports are watched so devices can be plugged in later. */
  15. #include "usbhid.h"
  16. #include "hid_report.h"
  17. #include "io.h"
  18. #include "mm.h"
  19. #include "usb.h"
  20. #include "serial.h"
  21. #include "timer.h"
  22. /* Request types/requests used here. */
  23. #define RT_HOST2DEV_STD 0x00u
  24. #define RT_HOST2DEV_CLS 0x21u
  25. #define RT_DEV2HOST_CLS_IF 0x81u /* class request to an interface, IN */
  26. #define REQ_GET_DESCRIPTOR 0x06u
  27. #define REQ_SET_CONFIGURATION 0x09u
  28. #define REQ_SET_PROTOCOL 0x0Bu
  29. /* Hotplug watcher period (must match usb.c's internal throttle). */
  30. #define WATCH_MS 250
  31. /* Extended (0xE0-prefixed) PS/2 make codes in the mapping table. */
  32. #define K_EXT 0xE000u
  33. /* Report descriptors are read into one frame. 240 bytes is the largest
  34. * control-IN transfer UHCI can build at an 8-byte EP0 (30 TDs), and it
  35. * covers every ordinary mouse descriptor; a longer one is parsed from its
  36. * first part, and the boot decoder stays the fallback if the wheel lies
  37. * beyond it. */
  38. #define REPORT_MAX 240
  39. /* One PS/2/IntelliMouse wheel notch, shared with the DJ report path. */
  40. #define USB_WHEEL_PX 48
  41. #if HID_REPORT_MAX > USB_INT_MAX
  42. #error "HID mouse layout exceeds the interrupt-IN report buffer"
  43. #endif
  44. enum { KIND_FREE = 0, KIND_MOUSE, KIND_KBD, KIND_DJ };
  45. struct hid_dev {
  46. unsigned int kind; /* KIND_*, KIND_FREE = slot unused */
  47. int present; /* endpoint armed */
  48. int claimed; /* claimed by the scan in progress */
  49. unsigned int ctrl, addr, ep, mps, port;
  50. unsigned int xfer; /* report buffer size for int_in_start */
  51. int low;
  52. unsigned int err; /* consecutive transfer errors */
  53. unsigned int disc; /* consecutive "port disconnected" reads */
  54. unsigned int report_mode; /* 1 = decode with layout, 0 = boot format */
  55. struct hid_mouse_layout layout;
  56. /* keyboard report-diff state */
  57. unsigned char prev[6];
  58. unsigned char prev_mod;
  59. };
  60. static struct hid_dev devs[USB_HCD_INT_SLOTS];
  61. static int hcd_ok;
  62. static int need_rescan;
  63. static unsigned int gen;
  64. static unsigned int last_watch;
  65. /* --- keyboard report decoding ------------------------------------------ */
  66. /* HID keyboard usage -> PS/2 set-1 make code (0 = unmapped). */
  67. static const unsigned short hid_to_sc[256] = {
  68. [0x04] = 0x1E, [0x05] = 0x30, [0x06] = 0x2E, [0x07] = 0x20,
  69. [0x08] = 0x12, [0x09] = 0x21, [0x0A] = 0x22, [0x0B] = 0x23,
  70. [0x0C] = 0x17, [0x0D] = 0x24, [0x0E] = 0x25, [0x0F] = 0x26,
  71. [0x10] = 0x32, [0x11] = 0x31, [0x12] = 0x18, [0x13] = 0x19,
  72. [0x14] = 0x10, [0x15] = 0x13, [0x16] = 0x1F, [0x17] = 0x14,
  73. [0x18] = 0x16, [0x19] = 0x2F, [0x1A] = 0x11, [0x1B] = 0x2D,
  74. [0x1C] = 0x15, [0x1D] = 0x2C,
  75. [0x1E] = 0x02, [0x1F] = 0x03, [0x20] = 0x04, [0x21] = 0x05,
  76. [0x22] = 0x06, [0x23] = 0x07, [0x24] = 0x08, [0x25] = 0x09,
  77. [0x26] = 0x0A, [0x27] = 0x0B,
  78. [0x28] = 0x1C, [0x29] = 0x01, [0x2A] = 0x0E, [0x2B] = 0x0F,
  79. [0x2C] = 0x39, [0x2D] = 0x0C, [0x2E] = 0x0D, [0x2F] = 0x1A,
  80. [0x30] = 0x1B, [0x31] = 0x2B, [0x33] = 0x27, [0x34] = 0x28,
  81. [0x35] = 0x29, [0x36] = 0x33, [0x37] = 0x34, [0x38] = 0x35,
  82. [0x39] = 0x3A,
  83. [0x3A] = 0x3B, [0x3B] = 0x3C, [0x3C] = 0x3D, [0x3D] = 0x3E,
  84. [0x3E] = 0x3F, [0x3F] = 0x40, [0x40] = 0x41, [0x41] = 0x42,
  85. [0x42] = 0x43, [0x43] = 0x44, [0x44] = 0x57, [0x45] = 0x58,
  86. [0x49] = K_EXT | 0x52, [0x4A] = K_EXT | 0x47, [0x4B] = K_EXT | 0x49,
  87. [0x4C] = K_EXT | 0x53, [0x4D] = K_EXT | 0x4F, [0x4E] = K_EXT | 0x51,
  88. [0x4F] = K_EXT | 0x4D, [0x50] = K_EXT | 0x4B, [0x51] = K_EXT | 0x50,
  89. [0x52] = K_EXT | 0x48,
  90. };
  91. /* Modifier bit -> PS/2 make code: L/R ctrl, shift, alt, GUI. */
  92. static const unsigned short hid_mod_sc[8] = {
  93. 0x1D, K_EXT | 0x1D, /* L Ctrl, R Ctrl */
  94. 0x2A, 0x36, /* L Shift, R Shift */
  95. 0x38, K_EXT | 0x38, /* L Alt, R Alt */
  96. K_EXT | 0x5B, K_EXT | 0x5C, /* L GUI, R GUI */
  97. };
  98. static const unsigned char hid_mod_bit[8] = {
  99. 0x01, 0x10, 0x02, 0x20, 0x04, 0x40, 0x08, 0x80,
  100. };
  101. static void kbd_inject(unsigned int code, int down)
  102. {
  103. if (!code)
  104. return;
  105. if (code & K_EXT) {
  106. io_inject_scancode(0xE0);
  107. io_inject_scancode((unsigned char)((code & 0xFFu) |
  108. (down ? 0 : 0x80)));
  109. } else {
  110. io_inject_scancode((unsigned char)(code | (down ? 0 : 0x80)));
  111. }
  112. }
  113. /* Boot keyboard report: [modifiers][reserved][key0..key5]. Diff against
  114. the previous report to derive make/break scancodes. */
  115. static void kbd_decode(struct hid_dev *d, const unsigned char *r)
  116. {
  117. unsigned int i, j;
  118. for (i = 0; i < 8; i++) {
  119. int was = (d->prev_mod & hid_mod_bit[i]) != 0;
  120. int now = (r[0] & hid_mod_bit[i]) != 0;
  121. if (was != now)
  122. kbd_inject(hid_mod_sc[i], now);
  123. }
  124. d->prev_mod = r[0];
  125. /* Releases: in the previous report, gone from this one. */
  126. for (i = 0; i < 6; i++) {
  127. if (!d->prev[i])
  128. continue;
  129. for (j = 0; j < 6; j++) {
  130. if (r[2 + j] == d->prev[i])
  131. break;
  132. }
  133. if (j == 6)
  134. kbd_inject(hid_to_sc[d->prev[i]], 0);
  135. }
  136. /* Presses: new in this report. */
  137. for (i = 0; i < 6; i++) {
  138. if (!r[2 + i])
  139. continue;
  140. for (j = 0; j < 6; j++) {
  141. if (d->prev[j] == r[2 + i])
  142. break;
  143. }
  144. if (j == 6)
  145. kbd_inject(hid_to_sc[r[2 + i]], 1);
  146. }
  147. for (i = 0; i < 6; i++)
  148. d->prev[i] = r[2 + i];
  149. }
  150. /* --- report-descriptor classification ----------------------------------- */
  151. /* Minimal HID item walk: find a Generic Desktop (usage page 1) mouse or
  152. keyboard collection. Mouse detection accepts a Mouse/Pointer collection
  153. or a relative X/Y input item (so absolute tablets are not claimed). */
  154. static unsigned int hid_classify(const unsigned char *rep, unsigned int n)
  155. {
  156. int mouse = 0, kbd = 0;
  157. unsigned int i = 0, page = 0;
  158. int loc_mouse = 0, loc_kbd = 0, loc_xy = 0;
  159. while (i < n) {
  160. unsigned int item = rep[i++];
  161. unsigned int size = item & 3u;
  162. unsigned int type = (item >> 2) & 3u;
  163. unsigned int tag = (item >> 4) & 0xFu;
  164. unsigned int data = 0, k;
  165. if (size == 3u)
  166. size = 4u;
  167. for (k = 0; k < size; k++) {
  168. if (i + k < (unsigned int)n)
  169. data |= (unsigned int)rep[i + k] << (8u * k);
  170. }
  171. i += size;
  172. if (type == 1u && tag == 0u) { /* Global Usage Page */
  173. page = data;
  174. } else if (type == 2u && tag == 0u && page == 0x01u) {
  175. if (data == 0x01u || data == 0x02u) /* Pointer/Mouse */
  176. loc_mouse = 1;
  177. else if (data == 0x06u) /* Keyboard */
  178. loc_kbd = 1;
  179. else if (data == 0x30u || data == 0x31u) /* X/Y */
  180. loc_xy = 1;
  181. } else if (type == 0u) { /* Main item */
  182. if (tag == 0xAu) { /* Collection */
  183. if (loc_mouse)
  184. mouse = 1;
  185. if (loc_kbd)
  186. kbd = 1;
  187. } else if (tag == 0x8u && loc_xy &&
  188. (data & 0x04u)) { /* Input, relative */
  189. mouse = 1;
  190. }
  191. loc_mouse = loc_kbd = loc_xy = 0;
  192. }
  193. }
  194. if (mouse)
  195. return KIND_MOUSE;
  196. if (kbd)
  197. return KIND_KBD;
  198. return KIND_FREE;
  199. }
  200. /* --- manager ------------------------------------------------------------ */
  201. /* Read up to `cap` bytes of the interface's HID report descriptor. The
  202. * request stays within UHCI's 30-TD control limit for an 8-byte EP0. */
  203. static int read_report_desc(const struct usb_iface_info *in,
  204. unsigned char *buf, unsigned int cap)
  205. {
  206. return usb_control_in(in->ctrl, in->addr, in->mps, in->low_speed,
  207. RT_DEV2HOST_CLS_IF, REQ_GET_DESCRIPTOR,
  208. (unsigned short)(0x22u << 8),
  209. (unsigned short)in->iface_num, buf, cap);
  210. }
  211. static int hid_probe(const struct usb_iface_info *in)
  212. {
  213. struct hid_dev *d = 0;
  214. struct hid_mouse_layout layout;
  215. unsigned char *desc = 0;
  216. unsigned int desc_len = 0;
  217. unsigned int kind, i;
  218. int layout_ok = 0;
  219. if (in->cls != 0x03u || !in->ep_addr || !(in->ep_addr & 0x80u))
  220. return 0;
  221. for (i = 0; i < sizeof(layout); i++)
  222. ((unsigned char *)&layout)[i] = 0;
  223. /* Logitech Unifying receiver: interface 2 is the HID++/DJ channel
  224. * that carries the paired device's reports. Its vendor report
  225. * descriptor is not a mouse collection, so claim it explicitly. */
  226. if (in->vendor == 0x046du && in->product == 0xc52bu &&
  227. in->iface_num == 2 && in->subcls == 0x00u && in->proto == 0x00u)
  228. kind = KIND_DJ;
  229. else if (in->subcls == 0x01u && in->proto == 0x01u)
  230. kind = KIND_KBD;
  231. else {
  232. int n;
  233. desc = (unsigned char *)mm_alloc_page();
  234. if (desc) {
  235. n = read_report_desc(in, desc, REPORT_MAX);
  236. if (n > 0)
  237. desc_len = (unsigned int)n;
  238. }
  239. if (in->subcls == 0x01u && in->proto == 0x02u)
  240. kind = KIND_MOUSE;
  241. else
  242. kind = desc_len ? hid_classify(desc, desc_len) :
  243. KIND_FREE;
  244. if (kind == KIND_MOUSE && desc_len)
  245. layout_ok = hid_parse_mouse(desc, desc_len, &layout);
  246. }
  247. if (kind == KIND_FREE) {
  248. if (desc)
  249. mm_free_page((unsigned int)desc);
  250. return 0;
  251. }
  252. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  253. if (!devs[i].present && !devs[i].claimed) {
  254. d = &devs[i];
  255. break;
  256. }
  257. }
  258. if (!d) {
  259. if (desc)
  260. mm_free_page((unsigned int)desc);
  261. return 0; /* every slot is busy */
  262. }
  263. d->report_mode = 0;
  264. d->layout.flags = 0;
  265. /* The endpoint is unusable until the device is configured. */
  266. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  267. RT_HOST2DEV_STD, REQ_SET_CONFIGURATION,
  268. (unsigned short)(in->config_value & 0xFFu), 0);
  269. /* A wheel lives only in the report descriptor: boot protocol has no
  270. * field for it. Such a mouse runs in report protocol, and the fixed
  271. * boot layout stays the fallback for everything else (and for a
  272. * device that refuses the protocol switch). */
  273. if (kind == KIND_MOUSE && layout_ok && layout.wheel_size) {
  274. if (in->subcls != 0x01u ||
  275. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  276. RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 1,
  277. in->iface_num) == 0)
  278. d->report_mode = 1;
  279. else
  280. usb_control_out(in->ctrl, in->addr, in->mps,
  281. in->low_speed, RT_HOST2DEV_CLS,
  282. REQ_SET_PROTOCOL, 0, in->iface_num);
  283. } else if (in->subcls == 0x01u) {
  284. /* Boot protocol for boot-class devices; a stall is tolerated. */
  285. usb_control_out(in->ctrl, in->addr, in->mps, in->low_speed,
  286. RT_HOST2DEV_CLS, REQ_SET_PROTOCOL, 0,
  287. in->iface_num);
  288. }
  289. if (desc)
  290. mm_free_page((unsigned int)desc);
  291. d->kind = kind;
  292. d->ctrl = in->ctrl;
  293. d->addr = in->addr;
  294. d->ep = in->ep_addr & 0x0Fu;
  295. d->mps = in->ep_mps ? in->ep_mps : 8;
  296. d->xfer = kind == KIND_DJ ? USB_INT_MAX :
  297. (in->ep_mps ? in->ep_mps : 8);
  298. if (d->xfer > USB_INT_MAX)
  299. d->xfer = USB_INT_MAX;
  300. d->port = in->port;
  301. d->low = in->low_speed;
  302. if (layout_ok)
  303. d->layout = layout;
  304. d->claimed = 1;
  305. return 1;
  306. }
  307. static void dev_attach(struct hid_dev *d, unsigned int slot)
  308. {
  309. unsigned int i;
  310. if (usb_int_in_start(d->ctrl, slot, d->addr, d->ep, d->mps,
  311. d->xfer, d->low) != 0)
  312. return;
  313. d->err = 0;
  314. d->disc = 0;
  315. d->present = 1;
  316. d->prev_mod = 0;
  317. for (i = 0; i < 6; i++)
  318. d->prev[i] = 0;
  319. if (d->kind == KIND_DJ)
  320. serial_puts(SERIAL_COM1, "usbhid: Logitech DJ receiver attached (");
  321. else if (d->kind == KIND_MOUSE)
  322. serial_puts(SERIAL_COM1, "usbhid: mouse attached (");
  323. else
  324. serial_puts(SERIAL_COM1, "usbhid: keyboard attached (");
  325. serial_puts(SERIAL_COM1, usb_hcd_name_ctrl(d->ctrl));
  326. serial_puts(SERIAL_COM1, " ctrl ");
  327. serial_putdec(SERIAL_COM1, d->ctrl);
  328. serial_puts(SERIAL_COM1, ", addr ");
  329. serial_putdec(SERIAL_COM1, d->addr);
  330. serial_puts(SERIAL_COM1, " ep ");
  331. serial_putdec(SERIAL_COM1, 0x80u | d->ep);
  332. if (d->report_mode)
  333. serial_puts(SERIAL_COM1, ", report protocol + wheel");
  334. serial_puts(SERIAL_COM1, ")\n");
  335. }
  336. static void dev_detach(struct hid_dev *d, unsigned int slot)
  337. {
  338. usb_int_in_stop(slot);
  339. d->present = 0;
  340. d->claimed = 0;
  341. /* A false port-loss leaves the device attached but unreachable (no
  342. * CSC to wake the hotplug watcher). A clean full rescan re-arms
  343. * everything, and is the only reliable recovery on this SB950. */
  344. need_rescan = 1;
  345. last_watch = get_ticks_ms() - WATCH_MS; /* watch again soon */
  346. serial_puts(SERIAL_COM1, d->kind == KIND_DJ
  347. ? "usbhid: Logitech DJ receiver detached (addr "
  348. : d->kind == KIND_MOUSE
  349. ? "usbhid: mouse detached (addr "
  350. : "usbhid: keyboard detached (addr ");
  351. serial_putdec(SERIAL_COM1, d->addr);
  352. serial_puts(SERIAL_COM1, ")\n");
  353. d->kind = KIND_FREE;
  354. }
  355. static void arm_claimed(void)
  356. {
  357. unsigned int i;
  358. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  359. if (devs[i].claimed) {
  360. dev_attach(&devs[i], i);
  361. devs[i].claimed = 0;
  362. }
  363. }
  364. }
  365. static void hid_scan(void)
  366. {
  367. unsigned int i;
  368. /* A full scan resets every root port: existing handles are stale. */
  369. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  370. if (devs[i].kind != KIND_FREE) {
  371. usb_int_in_stop(i);
  372. devs[i].kind = KIND_FREE;
  373. devs[i].present = 0;
  374. devs[i].claimed = 0;
  375. }
  376. }
  377. if (usb_scan(hid_probe) != 0)
  378. return; /* busy: retried from the watcher */
  379. need_rescan = 0;
  380. gen = usb_generation();
  381. arm_claimed();
  382. }
  383. void usbhid_init(void)
  384. {
  385. /* usb_scan() brings the controllers up itself, under the scan claim;
  386. * hcd_ok stays 0 until it returns, so usbhid_poll() keeps out. */
  387. int r = usb_scan(hid_probe);
  388. if (r == -1) {
  389. serial_puts(SERIAL_COM1, "usbhid: no USB host controller\n");
  390. hcd_ok = 0;
  391. return;
  392. }
  393. hcd_ok = 1;
  394. if (r != 0) {
  395. /* A scan is already running (unlikely at boot): finish from
  396. the watcher. */
  397. need_rescan = 1;
  398. gen = usb_generation();
  399. return;
  400. }
  401. gen = usb_generation();
  402. arm_claimed();
  403. if (!usbhid_mouse_attached() && !usbhid_keyboard_attached())
  404. serial_puts(SERIAL_COM1,
  405. "usbhid: no HID boot mouse/keyboard found\n");
  406. }
  407. /* Service one slot: detach on disconnect/errors, return report length. */
  408. static int dev_poll(struct hid_dev *d, unsigned int slot, unsigned char *buf,
  409. unsigned int len)
  410. {
  411. int n;
  412. if (!d->present)
  413. return 0;
  414. /* CCS can glitch for a frame on the SB950; a single read must not
  415. * detach a working device. Require three consecutive misses. */
  416. if (d->port != 0xFFu && !usb_port_connected(d->ctrl, d->port)) {
  417. if (++d->disc >= 3)
  418. dev_detach(d, slot);
  419. return 0;
  420. }
  421. d->disc = 0;
  422. n = usb_int_in_poll(slot, buf, len);
  423. if (n < 0) {
  424. /* The HCD re-arms on every error; a short burst is normal on
  425. * a marginal low-speed link and must not kill the device. */
  426. if (++d->err >= 50)
  427. dev_detach(d, slot);
  428. return 0;
  429. }
  430. d->err = 0;
  431. return n;
  432. }
  433. static int dj_seen;
  434. static int mouse_seen;
  435. static void dj_decode(const unsigned char *r, unsigned int n,
  436. unsigned int *buttons, int *dx, int *dy,
  437. int *scroll, int *got)
  438. {
  439. const unsigned char *p;
  440. unsigned int btn;
  441. int x, y;
  442. if (n < 10 || (r[0] != 0x20u && r[0] != 0x21u))
  443. return;
  444. /* Device index 1..7, standard mouse report type. The receiver's
  445. * connection/notification reports (0x40/0x41/0x42) and other device
  446. * types are ignored. */
  447. if (r[1] < 1 || r[1] > 7 || r[2] != 0x02u)
  448. return;
  449. p = r + 3;
  450. btn = p[0] | ((unsigned int)p[1] << 8);
  451. x = p[2] | ((p[3] & 0x0Fu) << 8);
  452. y = (p[3] >> 4) | (p[4] << 4);
  453. if (x & 0x800)
  454. x -= 0x1000; /* 12-bit signed relative X */
  455. if (y & 0x800)
  456. y -= 0x1000;
  457. *buttons |= btn & 0x07u;
  458. *dx += x;
  459. *dy -= y; /* HID +y down; mouse.c expects +y up */
  460. if (scroll)
  461. *scroll -= (int)(signed char)p[5] * USB_WHEEL_PX;
  462. *got = 1;
  463. if (!dj_seen) {
  464. dj_seen = 1;
  465. serial_puts(SERIAL_COM1, "usbhid: first DJ mouse report\n");
  466. }
  467. }
  468. int usbhid_poll(unsigned int *buttons, int *dx, int *dy, int *scroll)
  469. {
  470. unsigned char buf[USB_INT_MAX];
  471. unsigned int now, i, all_busy = 1;
  472. unsigned int b = 0;
  473. int n, x = 0, y = 0, got = 0;
  474. if (scroll)
  475. *scroll = 0;
  476. if (!hcd_ok)
  477. return 0;
  478. /* Another context's scan is sleeping between port resets: the
  479. * controllers are its until it finishes. */
  480. if (usb_scan_foreign())
  481. return 0;
  482. /* A full bus re-scan (`lsusb`) invalidates every handle. */
  483. if (usb_generation() != gen) {
  484. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  485. if (devs[i].present)
  486. dev_detach(&devs[i], i);
  487. }
  488. need_rescan = 1;
  489. }
  490. /* Throttled: finish a pending rescan, or watch for hotplug. */
  491. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  492. if (devs[i].kind == KIND_FREE)
  493. all_busy = 0;
  494. }
  495. now = get_ticks_ms();
  496. if (now - last_watch >= WATCH_MS) {
  497. last_watch = now;
  498. if (need_rescan) {
  499. hid_scan();
  500. } else if (!all_busy) {
  501. for (i = 0; i < USB_HCD_INT_SLOTS; i++)
  502. devs[i].claimed = 0;
  503. if (usb_hotplug_poll(hid_probe) > 0)
  504. arm_claimed();
  505. }
  506. }
  507. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  508. struct hid_dev *d = &devs[i];
  509. if (d->kind == KIND_FREE)
  510. continue;
  511. n = dev_poll(d, i, buf, sizeof(buf));
  512. if (n >= 3 && d->kind == KIND_MOUSE) {
  513. if (d->report_mode) {
  514. struct hid_mouse_frame f;
  515. if (hid_mouse_decode(&d->layout, buf,
  516. (unsigned int)n, &f)) {
  517. b |= f.buttons;
  518. x += f.dx;
  519. /* HID: +y is down; mouse.c expects
  520. * +y = up. */
  521. y -= f.dy;
  522. if (scroll)
  523. *scroll -= f.wheel * USB_WHEEL_PX;
  524. got = 1;
  525. if (!mouse_seen) {
  526. mouse_seen = 1;
  527. serial_puts(SERIAL_COM1,
  528. "usbhid: first report-protocol mouse report\n");
  529. }
  530. }
  531. } else {
  532. b |= buf[0] & 0x07u;
  533. x += (int)(signed char)buf[1];
  534. /* HID: +y is down; mouse.c expects +y = up. */
  535. y -= (int)(signed char)buf[2];
  536. got = 1;
  537. if (!mouse_seen) {
  538. mouse_seen = 1;
  539. serial_puts(SERIAL_COM1,
  540. "usbhid: first boot mouse report\n");
  541. }
  542. }
  543. } else if (d->kind == KIND_DJ && n >= 10) {
  544. dj_decode(buf, (unsigned int)n, &b, &x, &y,
  545. scroll, &got);
  546. } else if (n >= 8 && d->kind == KIND_KBD) {
  547. kbd_decode(d, buf);
  548. }
  549. }
  550. if (got) {
  551. *buttons = b;
  552. *dx = x;
  553. *dy = y;
  554. }
  555. return got;
  556. }
  557. int usbhid_mouse_attached(void)
  558. {
  559. unsigned int i;
  560. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  561. if ((devs[i].kind == KIND_MOUSE || devs[i].kind == KIND_DJ) &&
  562. devs[i].present)
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. int usbhid_keyboard_attached(void)
  568. {
  569. unsigned int i;
  570. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  571. if (devs[i].kind == KIND_KBD && devs[i].present)
  572. return 1;
  573. }
  574. return 0;
  575. }
  576. void usbhid_get_info(unsigned int *ctrl, unsigned int *addr,
  577. unsigned int *ep, unsigned int *mps, int *low,
  578. int *report_mode, int *wheel)
  579. {
  580. unsigned int i;
  581. for (i = 0; i < USB_HCD_INT_SLOTS; i++) {
  582. struct hid_dev *d = &devs[i];
  583. if (d->kind == KIND_MOUSE || d->kind == KIND_DJ) {
  584. if (!d->present)
  585. continue;
  586. *ctrl = d->ctrl;
  587. *addr = d->addr;
  588. *ep = d->ep;
  589. *mps = d->mps;
  590. *low = d->low;
  591. *report_mode = d->kind == KIND_DJ ? 2 :
  592. (int)d->report_mode;
  593. *wheel = d->kind == KIND_DJ ||
  594. (d->report_mode && d->layout.wheel_size);
  595. return;
  596. }
  597. }
  598. *ctrl = 0;
  599. *addr = 0;
  600. *ep = 0;
  601. *mps = 0;
  602. *low = 0;
  603. *report_mode = 0;
  604. *wheel = 0;
  605. }