mouse.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
  15. 0x0000,
  16. 0x8000, 0xC000, 0xE000, 0xF000, 0xF800, 0xFC00, 0xFE00, 0xFF00,
  17. 0xFF80, 0x9780, 0x0380, 0x0180, 0x0180, 0x0000, 0x0000
  18. };
  19. static int mouse_present_flag;
  20. static int mouse_streaming_flag;
  21. static unsigned int mouse_id;
  22. static int mouse_cursor_flag;
  23. static unsigned int fbw, fbh;
  24. static unsigned int cur_x, cur_y;
  25. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  26. static unsigned char pkt[3];
  27. static unsigned int pkt_idx;
  28. static inline unsigned char inb(unsigned short port)
  29. {
  30. unsigned char v;
  31. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  32. return v;
  33. }
  34. static inline void outb(unsigned short port, unsigned char v)
  35. {
  36. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  37. }
  38. static void ps2_wait_input_empty(void)
  39. {
  40. unsigned spin = 100000;
  41. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  42. ;
  43. }
  44. static void enable_aux_port(void)
  45. {
  46. ps2_wait_input_empty();
  47. outb(PS2_CMD, 0xA8);
  48. }
  49. static void mouse_cmd_write(unsigned char val)
  50. {
  51. ps2_wait_input_empty();
  52. outb(PS2_CMD, 0xD4);
  53. ps2_wait_input_empty();
  54. outb(PS2_DATA, val);
  55. }
  56. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  57. {
  58. unsigned int t0 = get_ticks_ms();
  59. while (get_ticks_ms() - t0 < timeout_ticks) {
  60. unsigned char st = inb(PS2_STATUS);
  61. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  62. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  63. *out = inb(PS2_DATA);
  64. return 0;
  65. }
  66. }
  67. return -1;
  68. }
  69. static int mouse_expect_ack(void)
  70. {
  71. unsigned char b;
  72. if (mouse_read_aux(&b, 500) != 0)
  73. return -1;
  74. if (b == MOUSE_RESEND)
  75. return -2;
  76. if (b != MOUSE_ACK)
  77. return -1;
  78. return 0;
  79. }
  80. static int mouse_send_cmd_ack(unsigned char cmd)
  81. {
  82. int r;
  83. mouse_cmd_write(cmd);
  84. r = mouse_expect_ack();
  85. if (r == -2) {
  86. mouse_cmd_write(cmd);
  87. r = mouse_expect_ack();
  88. }
  89. return r;
  90. }
  91. static int mouse_bit_at(int r, int c)
  92. {
  93. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  94. return 0;
  95. return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
  96. }
  97. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  98. {
  99. unsigned int r, c;
  100. for (r = 0; r < MOUSE_CUR_H; r++) {
  101. for (c = 0; c < MOUSE_CUR_W; c++) {
  102. if (mouse_bit_at((int)r, (int)c))
  103. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  104. }
  105. }
  106. for (r = 0; r < MOUSE_CUR_H; r++) {
  107. for (c = 0; c < MOUSE_CUR_W; c++) {
  108. if (!mouse_bit_at((int)r, (int)c)) {
  109. if (mouse_bit_at((int)r - 1, (int)c) ||
  110. mouse_bit_at((int)r + 1, (int)c) ||
  111. mouse_bit_at((int)r, (int)c - 1) ||
  112. mouse_bit_at((int)r, (int)c + 1))
  113. vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
  114. }
  115. }
  116. }
  117. }
  118. static void mouse_clamp_ptr(long *nx, long *ny)
  119. {
  120. long maxx = (long)fbw - (long)MOUSE_CUR_W;
  121. long maxy = (long)fbh - (long)MOUSE_CUR_H;
  122. if (maxx < 0)
  123. maxx = 0;
  124. if (maxy < 0)
  125. maxy = 0;
  126. if (*nx < 0)
  127. *nx = 0;
  128. else if (*nx > maxx)
  129. *nx = maxx;
  130. if (*ny < 0)
  131. *ny = 0;
  132. else if (*ny > maxy)
  133. *ny = maxy;
  134. }
  135. static void mouse_cursor_setup(void)
  136. {
  137. mouse_cursor_flag = 0;
  138. if (!vga_has_vesa())
  139. return;
  140. fbw = vga_fb_width();
  141. fbh = vga_fb_height();
  142. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  143. return;
  144. /* Start cursor at a fixed visible spot (clamped to the framebuffer).
  145. Using a constant offset avoids any overlap with the terminal header/prompt. */
  146. cur_x = 100;
  147. cur_y = 100;
  148. {
  149. long nx = (long)cur_x;
  150. long ny = (long)cur_y;
  151. mouse_clamp_ptr(&nx, &ny);
  152. cur_x = (unsigned int)nx;
  153. cur_y = (unsigned int)ny;
  154. }
  155. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  156. mouse_draw_cursor(cur_x, cur_y);
  157. mouse_cursor_flag = 1;
  158. }
  159. void mouse_init(void)
  160. {
  161. unsigned char id = 0;
  162. mouse_present_flag = 0;
  163. mouse_streaming_flag = 0;
  164. mouse_id = 0;
  165. mouse_cursor_flag = 0;
  166. pkt_idx = 0;
  167. enable_aux_port();
  168. if (mouse_send_cmd_ack(0xF5) != 0)
  169. return;
  170. mouse_cmd_write(0xF2);
  171. if (mouse_expect_ack() != 0)
  172. return;
  173. if (mouse_read_aux(&id, 500) != 0)
  174. return;
  175. mouse_present_flag = 1;
  176. mouse_id = id;
  177. mouse_cursor_setup();
  178. if (mouse_send_cmd_ack(0xF4) != 0)
  179. return;
  180. mouse_streaming_flag = 1;
  181. }
  182. void mouse_poll_update(void)
  183. {
  184. /* If video mode info wasn't ready at mouse_init time, try again. */
  185. if (mouse_present_flag && !mouse_cursor_flag)
  186. mouse_cursor_setup();
  187. if (!mouse_streaming_flag)
  188. return;
  189. for (;;) {
  190. unsigned char st = inb(PS2_STATUS);
  191. unsigned char b0;
  192. int dx, dy;
  193. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  194. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  195. return;
  196. pkt[pkt_idx++] = inb(PS2_DATA);
  197. if (pkt_idx < 3)
  198. continue;
  199. pkt_idx = 0;
  200. b0 = pkt[0];
  201. dx = (int)(signed char)pkt[1];
  202. dy = (int)(signed char)pkt[2];
  203. if (b0 & 0x10)
  204. dx |= ~0xFF;
  205. if (b0 & 0x20)
  206. dy |= ~0xFF;
  207. if ((b0 & 0xC0) != 0)
  208. continue;
  209. if (mouse_cursor_flag) {
  210. long nx = (long)cur_x + dx;
  211. long ny = (long)cur_y - dy;
  212. mouse_clamp_ptr(&nx, &ny);
  213. if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
  214. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  215. cur_saved);
  216. cur_x = (unsigned int)nx;
  217. cur_y = (unsigned int)ny;
  218. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  219. cur_saved);
  220. mouse_draw_cursor(cur_x, cur_y);
  221. }
  222. }
  223. }
  224. }
  225. int mouse_is_present(void)
  226. {
  227. return mouse_present_flag;
  228. }
  229. int mouse_streaming_enabled(void)
  230. {
  231. return mouse_streaming_flag;
  232. }
  233. unsigned int mouse_device_id(void)
  234. {
  235. return mouse_id;
  236. }
  237. int mouse_cursor_visible(void)
  238. {
  239. return mouse_cursor_flag;
  240. }