mouse.c 14 KB

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