mouse.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. #include "mouse.h"
  2. #include "bootinfo.h"
  3. #include "cirrus.h"
  4. #include "cursor_shape.h"
  5. #include "intel.h"
  6. #include "radeon.h"
  7. #include "timer.h"
  8. #include "touchpad.h"
  9. #include "usbhid.h"
  10. #include "vga.h"
  11. #define PS2_DATA 0x60
  12. #define PS2_STATUS 0x64
  13. #define PS2_CMD 0x64
  14. #define STATUS_OUT_FULL 0x01
  15. #define STATUS_IN_FULL 0x02
  16. #define STATUS_AUX_DATA 0x20
  17. #define MOUSE_ACK 0xFA
  18. #define MOUSE_RESEND 0xFE
  19. #define MOUSE_CUR_W CURSOR_SHAPE_WIDTH
  20. #define MOUSE_CUR_H CURSOR_SHAPE_HEIGHT
  21. static int mouse_present_flag;
  22. static int mouse_streaming_flag;
  23. static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
  24. static unsigned int mouse_id;
  25. static int mouse_cursor_flag;
  26. static unsigned int mouse_pkt_size;
  27. static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
  28. static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon, 3 = Intel */
  29. static int mouse_irq_mode; /* PS/2 AUX IRQ12 owns packet input */
  30. static int mouse_aux_irq_ok; /* controller config accepted AUX IRQ */
  31. static unsigned int fbw, fbh;
  32. static unsigned int cur_x, cur_y;
  33. static unsigned int saved_x, saved_y, saved_w, saved_h;
  34. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  35. static unsigned char pkt[4];
  36. static unsigned int pkt_idx;
  37. static inline unsigned char inb(unsigned short port)
  38. {
  39. unsigned char v;
  40. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  41. return v;
  42. }
  43. static inline void outb(unsigned short port, unsigned char v)
  44. {
  45. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  46. }
  47. static void ps2_wait_input_empty(void)
  48. {
  49. unsigned spin = 100000;
  50. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  51. ;
  52. }
  53. static void enable_aux_port(void)
  54. {
  55. ps2_wait_input_empty();
  56. outb(PS2_CMD, 0xA8);
  57. }
  58. static inline unsigned int mouse_irq_save(void)
  59. {
  60. unsigned int flags;
  61. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  62. return flags;
  63. }
  64. static inline void mouse_irq_restore(unsigned int flags)
  65. {
  66. __asm__ volatile ("pushl %0; popfl" :: "r"(flags));
  67. }
  68. /* Set the controller's AUX-interrupt bit. The PIC line stays masked until
  69. * mouse_init() has a hardware cursor and streaming is enabled. */
  70. static int ps2_aux_irq_config(void)
  71. {
  72. unsigned int flags, spin = 100000;
  73. unsigned char cfg;
  74. flags = mouse_irq_save();
  75. ps2_wait_input_empty();
  76. outb(PS2_CMD, 0x20);
  77. while (spin-- && !(inb(PS2_STATUS) & STATUS_OUT_FULL))
  78. ;
  79. if (!(inb(PS2_STATUS) & STATUS_OUT_FULL)) {
  80. mouse_irq_restore(flags);
  81. return -1;
  82. }
  83. cfg = inb(PS2_DATA);
  84. cfg |= 0x02; /* AUX (mouse) interrupt */
  85. ps2_wait_input_empty();
  86. outb(PS2_CMD, 0x60);
  87. ps2_wait_input_empty();
  88. outb(PS2_DATA, cfg);
  89. mouse_irq_restore(flags);
  90. return 0;
  91. }
  92. static void ps2_aux_irq_unmask(void)
  93. {
  94. unsigned char mask;
  95. /* Master IRQ2 is the cascade, slave IRQ12 is the AUX line. */
  96. mask = inb(0x21);
  97. outb(0x21, (unsigned char)(mask & ~0x04));
  98. mask = inb(0xA1);
  99. outb(0xA1, (unsigned char)(mask & ~0x10));
  100. }
  101. static void mouse_cmd_write(unsigned char val)
  102. {
  103. ps2_wait_input_empty();
  104. outb(PS2_CMD, 0xD4);
  105. ps2_wait_input_empty();
  106. outb(PS2_DATA, val);
  107. }
  108. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  109. {
  110. unsigned int t0 = get_ticks_ms();
  111. while (get_ticks_ms() - t0 < timeout_ticks) {
  112. unsigned char st = inb(PS2_STATUS);
  113. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  114. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  115. *out = inb(PS2_DATA);
  116. return 0;
  117. }
  118. }
  119. return -1;
  120. }
  121. static int mouse_expect_ack(void)
  122. {
  123. unsigned char b;
  124. if (mouse_read_aux(&b, 500) != 0)
  125. return -1;
  126. if (b == MOUSE_RESEND)
  127. return -2;
  128. if (b != MOUSE_ACK)
  129. return -1;
  130. return 0;
  131. }
  132. static int mouse_send_cmd_ack(unsigned char cmd)
  133. {
  134. int r;
  135. mouse_cmd_write(cmd);
  136. r = mouse_expect_ack();
  137. if (r == -2) {
  138. mouse_cmd_write(cmd);
  139. r = mouse_expect_ack();
  140. }
  141. return r;
  142. }
  143. enum {
  144. MOUSE_PIX_TRANSPARENT = 0,
  145. MOUSE_PIX_OUTLINE,
  146. MOUSE_PIX_FILL
  147. };
  148. static int mouse_pixel_at(int r, int c)
  149. {
  150. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  151. return MOUSE_PIX_TRANSPARENT;
  152. if (cursor_shape_fill[(unsigned int)r] & (0x8000u >> c))
  153. return MOUSE_PIX_FILL;
  154. if (cursor_shape_outline[(unsigned int)r] & (0x8000u >> c))
  155. return MOUSE_PIX_OUTLINE;
  156. return MOUSE_PIX_TRANSPARENT;
  157. }
  158. static void cursor_save_under(void)
  159. {
  160. if (!vga_shadow_is_active()) {
  161. /* Boot: the shadow buffer is not up yet, so there is no
  162. * background to save (and the saved buffer is uninitialized). */
  163. saved_w = saved_h = 0;
  164. return;
  165. }
  166. saved_x = cur_x;
  167. saved_y = cur_y;
  168. saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
  169. saved_h = (cur_y + MOUSE_CUR_H <= fbh) ? MOUSE_CUR_H : fbh - cur_y;
  170. if (saved_w && saved_h)
  171. vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  172. }
  173. static void cursor_restore_under(void)
  174. {
  175. if (saved_w && saved_h)
  176. vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  177. }
  178. /* Push the rectangle the cursor last touched to VRAM right away. The cursor
  179. * must not wait for the damage queue: over a large canvas (the browser) a
  180. * whole-window repaint can sit ahead of it for several flush iterations. */
  181. static void cursor_present(void)
  182. {
  183. if (saved_w && saved_h)
  184. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  185. }
  186. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  187. {
  188. unsigned int r, c;
  189. /* White arrow body with a black outline around it. */
  190. for (r = 0; r < MOUSE_CUR_H; r++) {
  191. for (c = 0; c < MOUSE_CUR_W; c++) {
  192. int px = mouse_pixel_at((int)r, (int)c);
  193. if (px == MOUSE_PIX_FILL)
  194. vga_fb_put_pixel(x0 + c, y0 + r, 255, 255, 255);
  195. else if (px == MOUSE_PIX_OUTLINE)
  196. vga_fb_put_pixel(x0 + c, y0 + r, 0, 0, 0);
  197. }
  198. }
  199. }
  200. static void mouse_clamp_ptr(long *nx, long *ny)
  201. {
  202. long maxx = (long)fbw - 1;
  203. long maxy = (long)fbh - 1;
  204. if (maxx < 0)
  205. maxx = 0;
  206. if (maxy < 0)
  207. maxy = 0;
  208. if (*nx < 0)
  209. *nx = 0;
  210. else if (*nx > maxx)
  211. *nx = maxx;
  212. if (*ny < 0)
  213. *ny = 0;
  214. else if (*ny > maxy)
  215. *ny = maxy;
  216. }
  217. static void mouse_cursor_setup(void)
  218. {
  219. mouse_cursor_flag = 0;
  220. if (!vga_has_vesa())
  221. return;
  222. fbw = vga_fb_width();
  223. fbh = vga_fb_height();
  224. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  225. return;
  226. /* Cirrus hardware cursor: the card overlays it; no save/restore.
  227. * UEFI GOP modes are not programmed through the legacy VGA sequencer,
  228. * so use the software cursor there. */
  229. if (!bootinfo_efi() && cirrus_available()) {
  230. mouse_hw_cursor = 1;
  231. cur_x = 100;
  232. cur_y = 100;
  233. cirrus_cursor_setup();
  234. cirrus_cursor_move(cur_x, cur_y);
  235. mouse_cursor_flag = 1;
  236. return;
  237. }
  238. /* Radeon (R200/RV280) hardware cursor. */
  239. if (!bootinfo_efi() && radeon_available()) {
  240. mouse_hw_cursor = 2;
  241. cur_x = 100;
  242. cur_y = 100;
  243. radeon_cursor_setup();
  244. radeon_cursor_move(cur_x, cur_y);
  245. mouse_cursor_flag = 1;
  246. return;
  247. }
  248. /* Intel Ironlake (Arrandale/X201) cursor plane. The cursor image is
  249. * mapped through the GTT by intel_init(); only enable it once a mouse
  250. * is actually present. */
  251. if (!bootinfo_efi() && intel_available()) {
  252. long nx = 100;
  253. long ny = 100;
  254. mouse_clamp_ptr(&nx, &ny);
  255. cur_x = (unsigned int)nx;
  256. cur_y = (unsigned int)ny;
  257. if (intel_cursor_setup(cur_x, cur_y) == 0) {
  258. mouse_hw_cursor = 3;
  259. mouse_cursor_flag = 1;
  260. return;
  261. }
  262. mouse_hw_cursor = 0;
  263. }
  264. /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
  265. the display engine (see nv50.c); the software cursor is used. */
  266. mouse_hw_cursor = 0;
  267. cur_x = 100;
  268. cur_y = 100;
  269. {
  270. long nx = (long)cur_x;
  271. long ny = (long)cur_y;
  272. mouse_clamp_ptr(&nx, &ny);
  273. cur_x = (unsigned int)nx;
  274. cur_y = (unsigned int)ny;
  275. }
  276. cursor_save_under();
  277. mouse_draw_cursor(cur_x, cur_y);
  278. cursor_present();
  279. mouse_cursor_flag = 1;
  280. }
  281. static int mouse_intellimouse_init(void)
  282. {
  283. /* First three rates are the Intellimouse enable magic (200/100/80);
  284. the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
  285. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
  286. unsigned int i;
  287. for (i = 0; i < sizeof(rates); i++) {
  288. if (mouse_send_cmd_ack(0xF3) != 0)
  289. return -1;
  290. if (mouse_send_cmd_ack(rates[i]) != 0)
  291. return -1;
  292. }
  293. return 0;
  294. }
  295. void mouse_init(void)
  296. {
  297. unsigned char id = 0;
  298. mouse_present_flag = 0;
  299. mouse_streaming_flag = 0;
  300. mouse_id = 0;
  301. mouse_cursor_flag = 0;
  302. mouse_pkt_size = 3;
  303. mouse_irq_mode = 0;
  304. mouse_aux_irq_ok = 0;
  305. pkt_idx = 0;
  306. /* Built-in HID-over-I2C touchpad. The probe maps the LPSS I2C
  307. * window, so it must run at boot, before the kworker and command
  308. * processes clone the page directory. */
  309. touchpad_init();
  310. enable_aux_port();
  311. mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
  312. if (mouse_send_cmd_ack(0xF5) != 0)
  313. return;
  314. mouse_intellimouse_init();
  315. mouse_cmd_write(0xF2);
  316. if (mouse_expect_ack() != 0)
  317. return;
  318. if (mouse_read_aux(&id, 500) != 0)
  319. return;
  320. mouse_present_flag = 1;
  321. mouse_id = id;
  322. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  323. mouse_cursor_setup();
  324. if (mouse_send_cmd_ack(0xF4) != 0)
  325. return;
  326. mouse_streaming_flag = 1;
  327. /* A hardware cursor makes the IRQ12 path safe: the handler only has
  328. * to write the cursor registers, not touch the shadow buffer or the
  329. * damage list. Otherwise keep the kworker polling the controller. */
  330. if (mouse_hw_cursor && mouse_aux_irq_ok) {
  331. pkt_idx = 0;
  332. mouse_irq_mode = 1;
  333. ps2_aux_irq_unmask();
  334. }
  335. }
  336. /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
  337. +dy up), redrawing it with the current backend (hardware cursor or
  338. shadow-buffer save/restore). */
  339. static void mouse_move(int dx, int dy)
  340. {
  341. long nx, ny;
  342. if (!mouse_cursor_flag)
  343. return;
  344. nx = (long)cur_x + dx;
  345. ny = (long)cur_y - dy;
  346. mouse_clamp_ptr(&nx, &ny);
  347. if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
  348. return;
  349. if (mouse_hw_cursor) {
  350. /* The PS/2 AUX IRQ can preempt a USB-driven update in the
  351. * kworker; keep the position read/modify/write atomic. */
  352. unsigned int flags = mouse_irq_save();
  353. cur_x = (unsigned int)nx;
  354. cur_y = (unsigned int)ny;
  355. if (mouse_hw_cursor == 3)
  356. intel_cursor_move(cur_x, cur_y);
  357. else if (mouse_hw_cursor == 2)
  358. radeon_cursor_move(cur_x, cur_y);
  359. else
  360. cirrus_cursor_move(cur_x, cur_y);
  361. mouse_irq_restore(flags);
  362. } else if (!mouse_auto_draw) {
  363. /* A WM frame is between BEGIN and PRESENT: it erased the
  364. * software cursor and will redraw it at the new position. */
  365. cur_x = (unsigned int)nx;
  366. cur_y = (unsigned int)ny;
  367. } else {
  368. cursor_restore_under();
  369. cursor_present(); /* erase the old position in VRAM */
  370. cur_x = (unsigned int)nx;
  371. cur_y = (unsigned int)ny;
  372. cursor_save_under();
  373. mouse_draw_cursor(cur_x, cur_y);
  374. cursor_present(); /* draw the new position in VRAM */
  375. }
  376. }
  377. /* Decode one complete PS/2 packet from pkt[] (3 or 4 bytes). */
  378. static void mouse_handle_packet(void)
  379. {
  380. unsigned char b0 = pkt[0];
  381. int dx = (int)(signed char)pkt[1];
  382. int dy = (int)(signed char)pkt[2];
  383. mouse_buttons = b0 & 0x07;
  384. if (b0 & 0x10)
  385. dx |= ~0xFF;
  386. if (b0 & 0x20)
  387. dy |= ~0xFF;
  388. if ((b0 & 0xC0) != 0)
  389. return;
  390. mouse_move(dx, dy);
  391. }
  392. /* IRQ12: PS/2 AUX data. With a hardware cursor the position update is just
  393. * a few MMIO writes, so it is safe and fast in interrupt context. This path
  394. * is only enabled when mouse_irq_mode is set (hardware cursor present). */
  395. void mouse_irq(void)
  396. {
  397. unsigned int guard = 0;
  398. if (mouse_irq_mode) {
  399. for (;;) {
  400. unsigned char st;
  401. if (guard++ >= 16)
  402. break;
  403. st = inb(PS2_STATUS);
  404. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  405. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  406. break;
  407. pkt[pkt_idx++] = inb(PS2_DATA);
  408. if (pkt_idx < mouse_pkt_size)
  409. continue;
  410. pkt_idx = 0;
  411. mouse_handle_packet();
  412. }
  413. }
  414. /* Slave first, then the master cascade. */
  415. outb(0xA0, 0x20);
  416. outb(0x20, 0x20);
  417. }
  418. void mouse_poll_update(void)
  419. {
  420. unsigned int guard = 0;
  421. unsigned int usb_buttons = 0;
  422. int usb_dx = 0, usb_dy = 0;
  423. int tp_buttons = 0, tp_dx = 0, tp_dy = 0;
  424. /* The cursor is set up for whichever backend shows up first (PS/2
  425. is probed at boot, USB may attach later). */
  426. if ((mouse_present_flag || usbhid_mouse_attached() ||
  427. touchpad_present()) && !mouse_cursor_flag)
  428. mouse_cursor_setup();
  429. /* USB HID mouse: reports arrive through the armed interrupt
  430. endpoint; the backend also handles hotplug/detach. */
  431. if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy)) {
  432. mouse_buttons = usb_buttons;
  433. mouse_move(usb_dx, usb_dy);
  434. }
  435. /* Built-in touchpad: absolute positions converted to deltas. */
  436. if (touchpad_poll(&tp_buttons, &tp_dx, &tp_dy)) {
  437. mouse_buttons = tp_buttons;
  438. mouse_move(tp_dx, tp_dy);
  439. }
  440. if (mouse_irq_mode)
  441. return; /* IRQ12 owns the PS/2 packets */
  442. if (!mouse_streaming_flag)
  443. return;
  444. for (;;) {
  445. unsigned char st;
  446. /* Bound the drain: a stuck/emulated controller must not
  447. * wedge the WM loop; leftovers are read next iteration. */
  448. if (guard++ >= 64)
  449. return;
  450. st = inb(PS2_STATUS);
  451. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  452. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  453. return;
  454. pkt[pkt_idx++] = inb(PS2_DATA);
  455. if (pkt_idx < mouse_pkt_size)
  456. continue;
  457. pkt_idx = 0;
  458. mouse_handle_packet();
  459. }
  460. }
  461. int mouse_is_present(void)
  462. {
  463. return mouse_present_flag || usbhid_mouse_attached() ||
  464. touchpad_present();
  465. }
  466. int mouse_ps2_present(void)
  467. {
  468. return mouse_present_flag;
  469. }
  470. int mouse_streaming_enabled(void)
  471. {
  472. return mouse_streaming_flag;
  473. }
  474. unsigned int mouse_device_id(void)
  475. {
  476. return mouse_id;
  477. }
  478. int mouse_cursor_visible(void)
  479. {
  480. return mouse_cursor_flag;
  481. }
  482. int mouse_cursor_backend(void)
  483. {
  484. return mouse_hw_cursor;
  485. }
  486. const char *mouse_cursor_backend_name(void)
  487. {
  488. switch (mouse_hw_cursor) {
  489. case 1: return "cirrus hw";
  490. case 2: return "radeon hw";
  491. case 3: return "intel hw";
  492. default: return "software";
  493. }
  494. }
  495. unsigned int mouse_packet_size(void)
  496. {
  497. return mouse_pkt_size;
  498. }
  499. unsigned int mouse_get_x(void)
  500. {
  501. return cur_x;
  502. }
  503. unsigned int mouse_get_y(void)
  504. {
  505. return cur_y;
  506. }
  507. unsigned int mouse_buttons_state(void)
  508. {
  509. return mouse_buttons;
  510. }
  511. void mouse_redraw_cursor(void)
  512. {
  513. if (!mouse_cursor_flag || mouse_hw_cursor)
  514. return;
  515. /* If the pointer moved while a WM frame owned the shadow, the saved
  516. * rectangle is still the old position and has been restored to the
  517. * background there. Erase it in VRAM before drawing the new one, so
  518. * a moving cursor does not leave a trail behind a slow frame. */
  519. if ((saved_x != cur_x || saved_y != cur_y) && saved_w && saved_h)
  520. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  521. cursor_save_under();
  522. mouse_draw_cursor(cur_x, cur_y);
  523. cursor_present();
  524. }
  525. void mouse_cursor_erase(void)
  526. {
  527. if (!mouse_cursor_flag || mouse_hw_cursor)
  528. return;
  529. cursor_restore_under();
  530. /* No direct present here: the caller is about to redraw the scene.
  531. * Keeping the old cursor on screen until that flush avoids a visible
  532. * blink at the start of every window frame. The restore already
  533. * marked the rectangle dirty, so the flush erases it. */
  534. }
  535. void mouse_set_auto_draw(int on)
  536. {
  537. mouse_auto_draw = on ? 1 : 0;
  538. }