mouse.c 8.9 KB

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