mouse.c 11 KB

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