mouse.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "mouse.h"
  2. #include "timer.h"
  3. #include "vga.h"
  4. #define PS2_DATA 0x60
  5. #define PS2_STATUS 0x64
  6. #define PS2_CMD 0x64
  7. #define STATUS_OUT_FULL 0x01
  8. #define STATUS_IN_FULL 0x02
  9. #define STATUS_AUX_DATA 0x20
  10. #define MOUSE_ACK 0xFA
  11. #define MOUSE_RESEND 0xFE
  12. #define MOUSE_CUR_W 16
  13. #define MOUSE_CUR_H 16
  14. /* Simple triangle cursor bitmap, 16×16.
  15. Hotspot is the top-left pixel of the 16×16 box (x0,y0).
  16. Each row is encoded as 16 bits; MSB (bit 15) maps to c=0. */
  17. static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
  18. 0x8000, /* 1....... ........ row 0: tip */
  19. 0xC000, /* 11...... ........ row 1 */
  20. 0xE000, /* 111..... ........ row 2 */
  21. 0xF000, /* 1111.... ........ row 3 */
  22. 0xF800, /* 11111... ........ row 4 */
  23. 0xFC00, /* 111111.. ........ row 5 */
  24. 0xFE00, /* 1111111. ........ row 6 */
  25. 0xFF00, /* 11111111 ........ row 7 */
  26. 0xFC00, /* 111111.. ........ row 8 */
  27. 0xE000, /* 111..... ........ row 9 */
  28. 0x8000, /* 1....... ........ row 10 */
  29. 0x0000, /* ........ ........ row 11 */
  30. 0x0000, /* ........ ........ row 12 */
  31. 0x0000, /* ........ ........ row 13 */
  32. 0x0000, /* ........ ........ row 14 */
  33. 0x0000 /* ........ ........ row 15 */
  34. };
  35. static int mouse_present_flag;
  36. static int mouse_streaming_flag;
  37. static unsigned int mouse_id;
  38. static int mouse_cursor_flag;
  39. static unsigned int mouse_pkt_size;
  40. static unsigned int fbw, fbh;
  41. static unsigned int cur_x, cur_y;
  42. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  43. static unsigned char pkt[3];
  44. static unsigned int pkt_idx;
  45. static inline unsigned char inb(unsigned short port)
  46. {
  47. unsigned char v;
  48. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  49. return v;
  50. }
  51. static inline void outb(unsigned short port, unsigned char v)
  52. {
  53. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  54. }
  55. static void ps2_wait_input_empty(void)
  56. {
  57. unsigned spin = 100000;
  58. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  59. ;
  60. }
  61. static void enable_aux_port(void)
  62. {
  63. ps2_wait_input_empty();
  64. outb(PS2_CMD, 0xA8);
  65. }
  66. static void mouse_cmd_write(unsigned char val)
  67. {
  68. ps2_wait_input_empty();
  69. outb(PS2_CMD, 0xD4);
  70. ps2_wait_input_empty();
  71. outb(PS2_DATA, val);
  72. }
  73. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  74. {
  75. unsigned int t0 = get_ticks_ms();
  76. while (get_ticks_ms() - t0 < timeout_ticks) {
  77. unsigned char st = inb(PS2_STATUS);
  78. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  79. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  80. *out = inb(PS2_DATA);
  81. return 0;
  82. }
  83. }
  84. return -1;
  85. }
  86. static int mouse_expect_ack(void)
  87. {
  88. unsigned char b;
  89. if (mouse_read_aux(&b, 500) != 0)
  90. return -1;
  91. if (b == MOUSE_RESEND)
  92. return -2;
  93. if (b != MOUSE_ACK)
  94. return -1;
  95. return 0;
  96. }
  97. static int mouse_send_cmd_ack(unsigned char cmd)
  98. {
  99. int r;
  100. mouse_cmd_write(cmd);
  101. r = mouse_expect_ack();
  102. if (r == -2) {
  103. mouse_cmd_write(cmd);
  104. r = mouse_expect_ack();
  105. }
  106. return r;
  107. }
  108. static int mouse_bit_at(int r, int c)
  109. {
  110. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  111. return 0;
  112. return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
  113. }
  114. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  115. {
  116. unsigned int r, c;
  117. /* Draw only the cursor fill bitmap (no outline). */
  118. for (r = 0; r < MOUSE_CUR_H; r++) {
  119. for (c = 0; c < MOUSE_CUR_W; c++) {
  120. if (mouse_bit_at((int)r, (int)c))
  121. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  122. }
  123. }
  124. }
  125. static void mouse_clamp_ptr(long *nx, long *ny)
  126. {
  127. long maxx = (long)fbw - (long)MOUSE_CUR_W;
  128. long maxy = (long)fbh - (long)MOUSE_CUR_H;
  129. if (maxx < 0)
  130. maxx = 0;
  131. if (maxy < 0)
  132. maxy = 0;
  133. if (*nx < 0)
  134. *nx = 0;
  135. else if (*nx > maxx)
  136. *nx = maxx;
  137. if (*ny < 0)
  138. *ny = 0;
  139. else if (*ny > maxy)
  140. *ny = maxy;
  141. }
  142. static void mouse_cursor_setup(void)
  143. {
  144. mouse_cursor_flag = 0;
  145. if (!vga_has_vesa())
  146. return;
  147. fbw = vga_fb_width();
  148. fbh = vga_fb_height();
  149. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  150. return;
  151. /* Start cursor at a fixed visible spot (clamped to the framebuffer).
  152. Using a constant offset avoids any overlap with the terminal header/prompt. */
  153. cur_x = 100;
  154. cur_y = 100;
  155. {
  156. long nx = (long)cur_x;
  157. long ny = (long)cur_y;
  158. mouse_clamp_ptr(&nx, &ny);
  159. cur_x = (unsigned int)nx;
  160. cur_y = (unsigned int)ny;
  161. }
  162. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  163. mouse_draw_cursor(cur_x, cur_y);
  164. mouse_cursor_flag = 1;
  165. }
  166. static int mouse_intellimouse_init(void)
  167. {
  168. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0x32 };
  169. unsigned int i;
  170. for (i = 0; i < sizeof(rates); i++) {
  171. if (mouse_send_cmd_ack(0xF3) != 0)
  172. return -1;
  173. if (mouse_send_cmd_ack(rates[i]) != 0)
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. void mouse_init(void)
  179. {
  180. unsigned char id = 0;
  181. mouse_present_flag = 0;
  182. mouse_streaming_flag = 0;
  183. mouse_id = 0;
  184. mouse_cursor_flag = 0;
  185. mouse_pkt_size = 3;
  186. pkt_idx = 0;
  187. enable_aux_port();
  188. if (mouse_send_cmd_ack(0xF5) != 0)
  189. return;
  190. mouse_intellimouse_init();
  191. mouse_cmd_write(0xF2);
  192. if (mouse_expect_ack() != 0)
  193. return;
  194. if (mouse_read_aux(&id, 500) != 0)
  195. return;
  196. mouse_present_flag = 1;
  197. mouse_id = id;
  198. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  199. mouse_cursor_setup();
  200. if (mouse_send_cmd_ack(0xF4) != 0)
  201. return;
  202. mouse_streaming_flag = 1;
  203. }
  204. void mouse_poll_update(void)
  205. {
  206. if (mouse_present_flag && !mouse_cursor_flag)
  207. mouse_cursor_setup();
  208. if (!mouse_streaming_flag)
  209. return;
  210. for (;;) {
  211. unsigned char st = inb(PS2_STATUS);
  212. unsigned char b0;
  213. int dx, dy;
  214. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  215. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  216. return;
  217. pkt[pkt_idx++] = inb(PS2_DATA);
  218. if (pkt_idx < mouse_pkt_size)
  219. continue;
  220. pkt_idx = 0;
  221. b0 = pkt[0];
  222. dx = (int)(signed char)pkt[1];
  223. dy = (int)(signed char)pkt[2];
  224. if (b0 & 0x10)
  225. dx |= ~0xFF;
  226. if (b0 & 0x20)
  227. dy |= ~0xFF;
  228. if ((b0 & 0xC0) != 0)
  229. continue;
  230. if (mouse_cursor_flag) {
  231. long nx = (long)cur_x + dx;
  232. long ny = (long)cur_y - dy;
  233. mouse_clamp_ptr(&nx, &ny);
  234. if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
  235. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  236. cur_saved);
  237. cur_x = (unsigned int)nx;
  238. cur_y = (unsigned int)ny;
  239. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  240. cur_saved);
  241. mouse_draw_cursor(cur_x, cur_y);
  242. }
  243. }
  244. }
  245. }
  246. int mouse_is_present(void)
  247. {
  248. return mouse_present_flag;
  249. }
  250. int mouse_streaming_enabled(void)
  251. {
  252. return mouse_streaming_flag;
  253. }
  254. unsigned int mouse_device_id(void)
  255. {
  256. return mouse_id;
  257. }
  258. int mouse_cursor_visible(void)
  259. {
  260. return mouse_cursor_flag;
  261. }
  262. unsigned int mouse_packet_size(void)
  263. {
  264. return mouse_pkt_size;
  265. }