mouse.c 10.0 KB

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