mouse.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. saved_x = cur_x;
  114. saved_y = cur_y;
  115. saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
  116. saved_h = (cur_y + MOUSE_CUR_H <= fbh) ? MOUSE_CUR_H : fbh - cur_y;
  117. if (saved_w && saved_h)
  118. vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  119. }
  120. static void cursor_restore_under(void)
  121. {
  122. if (saved_w && saved_h)
  123. vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  124. }
  125. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  126. {
  127. unsigned int r, c;
  128. /* White arrow body with a black outline around it. */
  129. for (r = 0; r < MOUSE_CUR_H; r++) {
  130. for (c = 0; c < MOUSE_CUR_W; c++) {
  131. int px = mouse_pixel_at((int)r, (int)c);
  132. if (px == MOUSE_PIX_FILL)
  133. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  134. else if (px == MOUSE_PIX_OUTLINE)
  135. vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
  136. }
  137. }
  138. }
  139. static void mouse_clamp_ptr(long *nx, long *ny)
  140. {
  141. long maxx = (long)fbw - 1;
  142. long maxy = (long)fbh - 1;
  143. if (maxx < 0)
  144. maxx = 0;
  145. if (maxy < 0)
  146. maxy = 0;
  147. if (*nx < 0)
  148. *nx = 0;
  149. else if (*nx > maxx)
  150. *nx = maxx;
  151. if (*ny < 0)
  152. *ny = 0;
  153. else if (*ny > maxy)
  154. *ny = maxy;
  155. }
  156. static void mouse_cursor_setup(void)
  157. {
  158. mouse_cursor_flag = 0;
  159. if (!vga_has_vesa())
  160. return;
  161. fbw = vga_fb_width();
  162. fbh = vga_fb_height();
  163. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  164. return;
  165. /* Cirrus hardware cursor: the card overlays it; no save/restore.
  166. * UEFI GOP modes are not programmed through the legacy VGA sequencer,
  167. * so use the software cursor there. */
  168. if (!bootinfo_efi() && cirrus_available()) {
  169. mouse_hw_cursor = 1;
  170. cur_x = 100;
  171. cur_y = 100;
  172. cirrus_cursor_setup();
  173. cirrus_cursor_move(cur_x, cur_y);
  174. mouse_cursor_flag = 1;
  175. return;
  176. }
  177. /* Radeon (R200/RV280) hardware cursor. */
  178. if (!bootinfo_efi() && radeon_available()) {
  179. mouse_hw_cursor = 2;
  180. cur_x = 100;
  181. cur_y = 100;
  182. radeon_cursor_setup();
  183. radeon_cursor_move(cur_x, cur_y);
  184. mouse_cursor_flag = 1;
  185. return;
  186. }
  187. /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
  188. the display engine (see nv50.c); the software cursor is used. */
  189. mouse_hw_cursor = 0;
  190. cur_x = 100;
  191. cur_y = 100;
  192. {
  193. long nx = (long)cur_x;
  194. long ny = (long)cur_y;
  195. mouse_clamp_ptr(&nx, &ny);
  196. cur_x = (unsigned int)nx;
  197. cur_y = (unsigned int)ny;
  198. }
  199. cursor_save_under();
  200. mouse_draw_cursor(cur_x, cur_y);
  201. mouse_cursor_flag = 1;
  202. }
  203. static int mouse_intellimouse_init(void)
  204. {
  205. /* First three rates are the Intellimouse enable magic (200/100/80);
  206. the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
  207. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
  208. unsigned int i;
  209. for (i = 0; i < sizeof(rates); i++) {
  210. if (mouse_send_cmd_ack(0xF3) != 0)
  211. return -1;
  212. if (mouse_send_cmd_ack(rates[i]) != 0)
  213. return -1;
  214. }
  215. return 0;
  216. }
  217. void mouse_init(void)
  218. {
  219. unsigned char id = 0;
  220. mouse_present_flag = 0;
  221. mouse_streaming_flag = 0;
  222. mouse_id = 0;
  223. mouse_cursor_flag = 0;
  224. mouse_pkt_size = 3;
  225. pkt_idx = 0;
  226. enable_aux_port();
  227. if (mouse_send_cmd_ack(0xF5) != 0)
  228. return;
  229. mouse_intellimouse_init();
  230. mouse_cmd_write(0xF2);
  231. if (mouse_expect_ack() != 0)
  232. return;
  233. if (mouse_read_aux(&id, 500) != 0)
  234. return;
  235. mouse_present_flag = 1;
  236. mouse_id = id;
  237. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  238. mouse_cursor_setup();
  239. if (mouse_send_cmd_ack(0xF4) != 0)
  240. return;
  241. mouse_streaming_flag = 1;
  242. }
  243. /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
  244. +dy up), redrawing it with the current backend (hardware cursor or
  245. shadow-buffer save/restore). */
  246. static void mouse_move(int dx, int dy)
  247. {
  248. long nx, ny;
  249. if (!mouse_cursor_flag)
  250. return;
  251. nx = (long)cur_x + dx;
  252. ny = (long)cur_y - dy;
  253. mouse_clamp_ptr(&nx, &ny);
  254. if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
  255. return;
  256. if (mouse_hw_cursor) {
  257. cur_x = (unsigned int)nx;
  258. cur_y = (unsigned int)ny;
  259. if (mouse_hw_cursor == 2)
  260. radeon_cursor_move(cur_x, cur_y);
  261. else
  262. cirrus_cursor_move(cur_x, cur_y);
  263. } else if (!mouse_auto_draw) {
  264. /* A WM frame is between BEGIN and PRESENT: it erased the
  265. * software cursor and will redraw it at the new position. */
  266. cur_x = (unsigned int)nx;
  267. cur_y = (unsigned int)ny;
  268. } else {
  269. cursor_restore_under();
  270. cur_x = (unsigned int)nx;
  271. cur_y = (unsigned int)ny;
  272. cursor_save_under();
  273. mouse_draw_cursor(cur_x, cur_y);
  274. }
  275. }
  276. void mouse_poll_update(void)
  277. {
  278. unsigned int guard = 0;
  279. unsigned int usb_buttons = 0;
  280. int usb_dx = 0, usb_dy = 0;
  281. /* The cursor is set up for whichever backend shows up first (PS/2
  282. is probed at boot, USB may attach later). */
  283. if ((mouse_present_flag || usbhid_mouse_attached()) && !mouse_cursor_flag)
  284. mouse_cursor_setup();
  285. /* USB HID mouse: reports arrive through the armed interrupt
  286. endpoint; the backend also handles hotplug/detach. */
  287. if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy)) {
  288. mouse_buttons = usb_buttons;
  289. mouse_move(usb_dx, usb_dy);
  290. }
  291. if (!mouse_streaming_flag)
  292. return;
  293. for (;;) {
  294. unsigned char st;
  295. unsigned char b0;
  296. int dx, dy;
  297. /* Bound the drain: a stuck/emulated controller must not
  298. * wedge the WM loop; leftovers are read next iteration. */
  299. if (guard++ >= 64)
  300. return;
  301. st = inb(PS2_STATUS);
  302. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  303. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  304. return;
  305. pkt[pkt_idx++] = inb(PS2_DATA);
  306. if (pkt_idx < mouse_pkt_size)
  307. continue;
  308. pkt_idx = 0;
  309. b0 = pkt[0];
  310. mouse_buttons = b0 & 0x07;
  311. dx = (int)(signed char)pkt[1];
  312. dy = (int)(signed char)pkt[2];
  313. if (b0 & 0x10)
  314. dx |= ~0xFF;
  315. if (b0 & 0x20)
  316. dy |= ~0xFF;
  317. if ((b0 & 0xC0) != 0)
  318. continue;
  319. mouse_move(dx, dy);
  320. }
  321. }
  322. int mouse_is_present(void)
  323. {
  324. return mouse_present_flag || usbhid_mouse_attached();
  325. }
  326. int mouse_ps2_present(void)
  327. {
  328. return mouse_present_flag;
  329. }
  330. int mouse_streaming_enabled(void)
  331. {
  332. return mouse_streaming_flag;
  333. }
  334. unsigned int mouse_device_id(void)
  335. {
  336. return mouse_id;
  337. }
  338. int mouse_cursor_visible(void)
  339. {
  340. return mouse_cursor_flag;
  341. }
  342. int mouse_cursor_backend(void)
  343. {
  344. return mouse_hw_cursor;
  345. }
  346. const char *mouse_cursor_backend_name(void)
  347. {
  348. switch (mouse_hw_cursor) {
  349. case 1: return "cirrus hw";
  350. case 2: return "radeon hw";
  351. default: return "software";
  352. }
  353. }
  354. unsigned int mouse_packet_size(void)
  355. {
  356. return mouse_pkt_size;
  357. }
  358. unsigned int mouse_get_x(void)
  359. {
  360. return cur_x;
  361. }
  362. unsigned int mouse_get_y(void)
  363. {
  364. return cur_y;
  365. }
  366. unsigned int mouse_buttons_state(void)
  367. {
  368. return mouse_buttons;
  369. }
  370. void mouse_redraw_cursor(void)
  371. {
  372. if (!mouse_cursor_flag || mouse_hw_cursor)
  373. return;
  374. cursor_save_under();
  375. mouse_draw_cursor(cur_x, cur_y);
  376. }
  377. void mouse_cursor_erase(void)
  378. {
  379. if (!mouse_cursor_flag || mouse_hw_cursor)
  380. return;
  381. cursor_restore_under();
  382. }
  383. void mouse_set_auto_draw(int on)
  384. {
  385. mouse_auto_draw = on ? 1 : 0;
  386. }