mouse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include "mouse.h"
  2. #include "bootinfo.h"
  3. #include "cirrus.h"
  4. #include "cursor_shape.h"
  5. #include "radeon.h"
  6. #include "timer.h"
  7. #include "usbhid.h"
  8. #include "vga.h"
  9. #define PS2_DATA 0x60
  10. #define PS2_STATUS 0x64
  11. #define PS2_CMD 0x64
  12. #define STATUS_OUT_FULL 0x01
  13. #define STATUS_IN_FULL 0x02
  14. #define STATUS_AUX_DATA 0x20
  15. #define MOUSE_ACK 0xFA
  16. #define MOUSE_RESEND 0xFE
  17. #define MOUSE_CUR_W CURSOR_SHAPE_WIDTH
  18. #define MOUSE_CUR_H CURSOR_SHAPE_HEIGHT
  19. static int mouse_present_flag;
  20. static int mouse_streaming_flag;
  21. static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
  22. static unsigned int mouse_id;
  23. static int mouse_cursor_flag;
  24. static unsigned int mouse_pkt_size;
  25. static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
  26. static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon hardware cursor */
  27. static unsigned int fbw, fbh;
  28. static unsigned int cur_x, cur_y;
  29. static unsigned int saved_x, saved_y, saved_w, saved_h;
  30. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  31. static unsigned char pkt[3];
  32. static unsigned int pkt_idx;
  33. static inline unsigned char inb(unsigned short port)
  34. {
  35. unsigned char v;
  36. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  37. return v;
  38. }
  39. static inline void outb(unsigned short port, unsigned char v)
  40. {
  41. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  42. }
  43. static void ps2_wait_input_empty(void)
  44. {
  45. unsigned spin = 100000;
  46. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  47. ;
  48. }
  49. static void enable_aux_port(void)
  50. {
  51. ps2_wait_input_empty();
  52. outb(PS2_CMD, 0xA8);
  53. }
  54. static void mouse_cmd_write(unsigned char val)
  55. {
  56. ps2_wait_input_empty();
  57. outb(PS2_CMD, 0xD4);
  58. ps2_wait_input_empty();
  59. outb(PS2_DATA, val);
  60. }
  61. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  62. {
  63. unsigned int t0 = get_ticks_ms();
  64. while (get_ticks_ms() - t0 < timeout_ticks) {
  65. unsigned char st = inb(PS2_STATUS);
  66. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  67. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  68. *out = inb(PS2_DATA);
  69. return 0;
  70. }
  71. }
  72. return -1;
  73. }
  74. static int mouse_expect_ack(void)
  75. {
  76. unsigned char b;
  77. if (mouse_read_aux(&b, 500) != 0)
  78. return -1;
  79. if (b == MOUSE_RESEND)
  80. return -2;
  81. if (b != MOUSE_ACK)
  82. return -1;
  83. return 0;
  84. }
  85. static int mouse_send_cmd_ack(unsigned char cmd)
  86. {
  87. int r;
  88. mouse_cmd_write(cmd);
  89. r = mouse_expect_ack();
  90. if (r == -2) {
  91. mouse_cmd_write(cmd);
  92. r = mouse_expect_ack();
  93. }
  94. return r;
  95. }
  96. enum {
  97. MOUSE_PIX_TRANSPARENT = 0,
  98. MOUSE_PIX_OUTLINE,
  99. MOUSE_PIX_FILL
  100. };
  101. static int mouse_pixel_at(int r, int c)
  102. {
  103. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  104. return MOUSE_PIX_TRANSPARENT;
  105. if (cursor_shape_fill[(unsigned int)r] & (0x8000u >> c))
  106. return MOUSE_PIX_FILL;
  107. if (cursor_shape_outline[(unsigned int)r] & (0x8000u >> c))
  108. return MOUSE_PIX_OUTLINE;
  109. return MOUSE_PIX_TRANSPARENT;
  110. }
  111. static void cursor_save_under(void)
  112. {
  113. if (!vga_shadow_is_active()) {
  114. /* Boot: the shadow buffer is not up yet, so there is no
  115. * background to save (and the saved buffer is uninitialized). */
  116. saved_w = saved_h = 0;
  117. return;
  118. }
  119. saved_x = cur_x;
  120. saved_y = cur_y;
  121. saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
  122. saved_h = (cur_y + MOUSE_CUR_H <= fbh) ? MOUSE_CUR_H : fbh - cur_y;
  123. if (saved_w && saved_h)
  124. vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  125. }
  126. static void cursor_restore_under(void)
  127. {
  128. if (saved_w && saved_h)
  129. vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  130. }
  131. /* Push the rectangle the cursor last touched to VRAM right away. The cursor
  132. * must not wait for the damage queue: over a large canvas (the browser) a
  133. * whole-window repaint can sit ahead of it for several flush iterations. */
  134. static void cursor_present(void)
  135. {
  136. if (saved_w && saved_h)
  137. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  138. }
  139. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  140. {
  141. unsigned int r, c;
  142. /* White arrow body with a black outline around it. */
  143. for (r = 0; r < MOUSE_CUR_H; r++) {
  144. for (c = 0; c < MOUSE_CUR_W; c++) {
  145. int px = mouse_pixel_at((int)r, (int)c);
  146. if (px == MOUSE_PIX_FILL)
  147. vga_fb_put_pixel(x0 + c, y0 + r, 255, 255, 255);
  148. else if (px == MOUSE_PIX_OUTLINE)
  149. vga_fb_put_pixel(x0 + c, y0 + r, 0, 0, 0);
  150. }
  151. }
  152. }
  153. static void mouse_clamp_ptr(long *nx, long *ny)
  154. {
  155. long maxx = (long)fbw - 1;
  156. long maxy = (long)fbh - 1;
  157. if (maxx < 0)
  158. maxx = 0;
  159. if (maxy < 0)
  160. maxy = 0;
  161. if (*nx < 0)
  162. *nx = 0;
  163. else if (*nx > maxx)
  164. *nx = maxx;
  165. if (*ny < 0)
  166. *ny = 0;
  167. else if (*ny > maxy)
  168. *ny = maxy;
  169. }
  170. static void mouse_cursor_setup(void)
  171. {
  172. mouse_cursor_flag = 0;
  173. if (!vga_has_vesa())
  174. return;
  175. fbw = vga_fb_width();
  176. fbh = vga_fb_height();
  177. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  178. return;
  179. /* Cirrus hardware cursor: the card overlays it; no save/restore.
  180. * UEFI GOP modes are not programmed through the legacy VGA sequencer,
  181. * so use the software cursor there. */
  182. if (!bootinfo_efi() && cirrus_available()) {
  183. mouse_hw_cursor = 1;
  184. cur_x = 100;
  185. cur_y = 100;
  186. cirrus_cursor_setup();
  187. cirrus_cursor_move(cur_x, cur_y);
  188. mouse_cursor_flag = 1;
  189. return;
  190. }
  191. /* Radeon (R200/RV280) hardware cursor. */
  192. if (!bootinfo_efi() && radeon_available()) {
  193. mouse_hw_cursor = 2;
  194. cur_x = 100;
  195. cur_y = 100;
  196. radeon_cursor_setup();
  197. radeon_cursor_move(cur_x, cur_y);
  198. mouse_cursor_flag = 1;
  199. return;
  200. }
  201. /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
  202. the display engine (see nv50.c); the software cursor is used. */
  203. mouse_hw_cursor = 0;
  204. cur_x = 100;
  205. cur_y = 100;
  206. {
  207. long nx = (long)cur_x;
  208. long ny = (long)cur_y;
  209. mouse_clamp_ptr(&nx, &ny);
  210. cur_x = (unsigned int)nx;
  211. cur_y = (unsigned int)ny;
  212. }
  213. cursor_save_under();
  214. mouse_draw_cursor(cur_x, cur_y);
  215. cursor_present();
  216. mouse_cursor_flag = 1;
  217. }
  218. static int mouse_intellimouse_init(void)
  219. {
  220. /* First three rates are the Intellimouse enable magic (200/100/80);
  221. the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
  222. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
  223. unsigned int i;
  224. for (i = 0; i < sizeof(rates); i++) {
  225. if (mouse_send_cmd_ack(0xF3) != 0)
  226. return -1;
  227. if (mouse_send_cmd_ack(rates[i]) != 0)
  228. return -1;
  229. }
  230. return 0;
  231. }
  232. void mouse_init(void)
  233. {
  234. unsigned char id = 0;
  235. mouse_present_flag = 0;
  236. mouse_streaming_flag = 0;
  237. mouse_id = 0;
  238. mouse_cursor_flag = 0;
  239. mouse_pkt_size = 3;
  240. pkt_idx = 0;
  241. enable_aux_port();
  242. if (mouse_send_cmd_ack(0xF5) != 0)
  243. return;
  244. mouse_intellimouse_init();
  245. mouse_cmd_write(0xF2);
  246. if (mouse_expect_ack() != 0)
  247. return;
  248. if (mouse_read_aux(&id, 500) != 0)
  249. return;
  250. mouse_present_flag = 1;
  251. mouse_id = id;
  252. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  253. mouse_cursor_setup();
  254. if (mouse_send_cmd_ack(0xF4) != 0)
  255. return;
  256. mouse_streaming_flag = 1;
  257. }
  258. /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
  259. +dy up), redrawing it with the current backend (hardware cursor or
  260. shadow-buffer save/restore). */
  261. static void mouse_move(int dx, int dy)
  262. {
  263. long nx, ny;
  264. if (!mouse_cursor_flag)
  265. return;
  266. nx = (long)cur_x + dx;
  267. ny = (long)cur_y - dy;
  268. mouse_clamp_ptr(&nx, &ny);
  269. if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
  270. return;
  271. if (mouse_hw_cursor) {
  272. cur_x = (unsigned int)nx;
  273. cur_y = (unsigned int)ny;
  274. if (mouse_hw_cursor == 2)
  275. radeon_cursor_move(cur_x, cur_y);
  276. else
  277. cirrus_cursor_move(cur_x, cur_y);
  278. } else if (!mouse_auto_draw) {
  279. /* A WM frame is between BEGIN and PRESENT: it erased the
  280. * software cursor and will redraw it at the new position. */
  281. cur_x = (unsigned int)nx;
  282. cur_y = (unsigned int)ny;
  283. } else {
  284. cursor_restore_under();
  285. cursor_present(); /* erase the old position in VRAM */
  286. cur_x = (unsigned int)nx;
  287. cur_y = (unsigned int)ny;
  288. cursor_save_under();
  289. mouse_draw_cursor(cur_x, cur_y);
  290. cursor_present(); /* draw the new position in VRAM */
  291. }
  292. }
  293. void mouse_poll_update(void)
  294. {
  295. unsigned int guard = 0;
  296. unsigned int usb_buttons = 0;
  297. int usb_dx = 0, usb_dy = 0;
  298. /* The cursor is set up for whichever backend shows up first (PS/2
  299. is probed at boot, USB may attach later). */
  300. if ((mouse_present_flag || usbhid_mouse_attached()) && !mouse_cursor_flag)
  301. mouse_cursor_setup();
  302. /* USB HID mouse: reports arrive through the armed interrupt
  303. endpoint; the backend also handles hotplug/detach. */
  304. if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy)) {
  305. mouse_buttons = usb_buttons;
  306. mouse_move(usb_dx, usb_dy);
  307. }
  308. if (!mouse_streaming_flag)
  309. return;
  310. for (;;) {
  311. unsigned char st;
  312. unsigned char b0;
  313. int dx, dy;
  314. /* Bound the drain: a stuck/emulated controller must not
  315. * wedge the WM loop; leftovers are read next iteration. */
  316. if (guard++ >= 64)
  317. return;
  318. st = inb(PS2_STATUS);
  319. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  320. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  321. return;
  322. pkt[pkt_idx++] = inb(PS2_DATA);
  323. if (pkt_idx < mouse_pkt_size)
  324. continue;
  325. pkt_idx = 0;
  326. b0 = pkt[0];
  327. mouse_buttons = b0 & 0x07;
  328. dx = (int)(signed char)pkt[1];
  329. dy = (int)(signed char)pkt[2];
  330. if (b0 & 0x10)
  331. dx |= ~0xFF;
  332. if (b0 & 0x20)
  333. dy |= ~0xFF;
  334. if ((b0 & 0xC0) != 0)
  335. continue;
  336. mouse_move(dx, dy);
  337. }
  338. }
  339. int mouse_is_present(void)
  340. {
  341. return mouse_present_flag || usbhid_mouse_attached();
  342. }
  343. int mouse_ps2_present(void)
  344. {
  345. return mouse_present_flag;
  346. }
  347. int mouse_streaming_enabled(void)
  348. {
  349. return mouse_streaming_flag;
  350. }
  351. unsigned int mouse_device_id(void)
  352. {
  353. return mouse_id;
  354. }
  355. int mouse_cursor_visible(void)
  356. {
  357. return mouse_cursor_flag;
  358. }
  359. int mouse_cursor_backend(void)
  360. {
  361. return mouse_hw_cursor;
  362. }
  363. const char *mouse_cursor_backend_name(void)
  364. {
  365. switch (mouse_hw_cursor) {
  366. case 1: return "cirrus hw";
  367. case 2: return "radeon hw";
  368. default: return "software";
  369. }
  370. }
  371. unsigned int mouse_packet_size(void)
  372. {
  373. return mouse_pkt_size;
  374. }
  375. unsigned int mouse_get_x(void)
  376. {
  377. return cur_x;
  378. }
  379. unsigned int mouse_get_y(void)
  380. {
  381. return cur_y;
  382. }
  383. unsigned int mouse_buttons_state(void)
  384. {
  385. return mouse_buttons;
  386. }
  387. void mouse_redraw_cursor(void)
  388. {
  389. if (!mouse_cursor_flag || mouse_hw_cursor)
  390. return;
  391. /* If the pointer moved while a WM frame owned the shadow, the saved
  392. * rectangle is still the old position and has been restored to the
  393. * background there. Erase it in VRAM before drawing the new one, so
  394. * a moving cursor does not leave a trail behind a slow frame. */
  395. if ((saved_x != cur_x || saved_y != cur_y) && saved_w && saved_h)
  396. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  397. cursor_save_under();
  398. mouse_draw_cursor(cur_x, cur_y);
  399. cursor_present();
  400. }
  401. void mouse_cursor_erase(void)
  402. {
  403. if (!mouse_cursor_flag || mouse_hw_cursor)
  404. return;
  405. cursor_restore_under();
  406. /* No direct present here: the caller is about to redraw the scene.
  407. * Keeping the old cursor on screen until that flush avoids a visible
  408. * blink at the start of every window frame. The restore already
  409. * marked the rectangle dirty, so the flush erases it. */
  410. }
  411. void mouse_set_auto_draw(int on)
  412. {
  413. mouse_auto_draw = on ? 1 : 0;
  414. }