mouse.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 mouse_buttons; /* bit0=left bit1=right bit2=middle */
  41. static unsigned int fbw, fbh;
  42. static unsigned int cur_x, cur_y;
  43. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  44. static unsigned char pkt[3];
  45. static unsigned int pkt_idx;
  46. static inline unsigned char inb(unsigned short port)
  47. {
  48. unsigned char v;
  49. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  50. return v;
  51. }
  52. static inline void outb(unsigned short port, unsigned char v)
  53. {
  54. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  55. }
  56. static void ps2_wait_input_empty(void)
  57. {
  58. unsigned spin = 100000;
  59. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  60. ;
  61. }
  62. static void enable_aux_port(void)
  63. {
  64. ps2_wait_input_empty();
  65. outb(PS2_CMD, 0xA8);
  66. }
  67. static void mouse_cmd_write(unsigned char val)
  68. {
  69. ps2_wait_input_empty();
  70. outb(PS2_CMD, 0xD4);
  71. ps2_wait_input_empty();
  72. outb(PS2_DATA, val);
  73. }
  74. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  75. {
  76. unsigned int t0 = get_ticks_ms();
  77. while (get_ticks_ms() - t0 < timeout_ticks) {
  78. unsigned char st = inb(PS2_STATUS);
  79. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  80. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  81. *out = inb(PS2_DATA);
  82. return 0;
  83. }
  84. }
  85. return -1;
  86. }
  87. static int mouse_expect_ack(void)
  88. {
  89. unsigned char b;
  90. if (mouse_read_aux(&b, 500) != 0)
  91. return -1;
  92. if (b == MOUSE_RESEND)
  93. return -2;
  94. if (b != MOUSE_ACK)
  95. return -1;
  96. return 0;
  97. }
  98. static int mouse_send_cmd_ack(unsigned char cmd)
  99. {
  100. int r;
  101. mouse_cmd_write(cmd);
  102. r = mouse_expect_ack();
  103. if (r == -2) {
  104. mouse_cmd_write(cmd);
  105. r = mouse_expect_ack();
  106. }
  107. return r;
  108. }
  109. static int mouse_bit_at(int r, int c)
  110. {
  111. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  112. return 0;
  113. return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
  114. }
  115. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  116. {
  117. unsigned int r, c;
  118. /* Draw only the cursor fill bitmap (no outline). */
  119. for (r = 0; r < MOUSE_CUR_H; r++) {
  120. for (c = 0; c < MOUSE_CUR_W; c++) {
  121. if (mouse_bit_at((int)r, (int)c))
  122. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  123. }
  124. }
  125. }
  126. static void mouse_clamp_ptr(long *nx, long *ny)
  127. {
  128. long maxx = (long)fbw - (long)MOUSE_CUR_W;
  129. long maxy = (long)fbh - (long)MOUSE_CUR_H;
  130. if (maxx < 0)
  131. maxx = 0;
  132. if (maxy < 0)
  133. maxy = 0;
  134. if (*nx < 0)
  135. *nx = 0;
  136. else if (*nx > maxx)
  137. *nx = maxx;
  138. if (*ny < 0)
  139. *ny = 0;
  140. else if (*ny > maxy)
  141. *ny = maxy;
  142. }
  143. static void mouse_cursor_setup(void)
  144. {
  145. mouse_cursor_flag = 0;
  146. if (!vga_has_vesa())
  147. return;
  148. fbw = vga_fb_width();
  149. fbh = vga_fb_height();
  150. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  151. return;
  152. /* Start cursor at a fixed visible spot (clamped to the framebuffer).
  153. Using a constant offset avoids any overlap with the terminal header/prompt. */
  154. cur_x = 100;
  155. cur_y = 100;
  156. {
  157. long nx = (long)cur_x;
  158. long ny = (long)cur_y;
  159. mouse_clamp_ptr(&nx, &ny);
  160. cur_x = (unsigned int)nx;
  161. cur_y = (unsigned int)ny;
  162. }
  163. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  164. mouse_draw_cursor(cur_x, cur_y);
  165. mouse_cursor_flag = 1;
  166. }
  167. static int mouse_intellimouse_init(void)
  168. {
  169. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0x32 };
  170. unsigned int i;
  171. for (i = 0; i < sizeof(rates); i++) {
  172. if (mouse_send_cmd_ack(0xF3) != 0)
  173. return -1;
  174. if (mouse_send_cmd_ack(rates[i]) != 0)
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. void mouse_init(void)
  180. {
  181. unsigned char id = 0;
  182. mouse_present_flag = 0;
  183. mouse_streaming_flag = 0;
  184. mouse_id = 0;
  185. mouse_cursor_flag = 0;
  186. mouse_pkt_size = 3;
  187. pkt_idx = 0;
  188. enable_aux_port();
  189. if (mouse_send_cmd_ack(0xF5) != 0)
  190. return;
  191. mouse_intellimouse_init();
  192. mouse_cmd_write(0xF2);
  193. if (mouse_expect_ack() != 0)
  194. return;
  195. if (mouse_read_aux(&id, 500) != 0)
  196. return;
  197. mouse_present_flag = 1;
  198. mouse_id = id;
  199. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  200. mouse_cursor_setup();
  201. if (mouse_send_cmd_ack(0xF4) != 0)
  202. return;
  203. mouse_streaming_flag = 1;
  204. }
  205. void mouse_poll_update(void)
  206. {
  207. if (mouse_present_flag && !mouse_cursor_flag)
  208. mouse_cursor_setup();
  209. if (!mouse_streaming_flag)
  210. return;
  211. for (;;) {
  212. unsigned char st = inb(PS2_STATUS);
  213. unsigned char b0;
  214. int dx, dy;
  215. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  216. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  217. return;
  218. pkt[pkt_idx++] = inb(PS2_DATA);
  219. if (pkt_idx < mouse_pkt_size)
  220. continue;
  221. pkt_idx = 0;
  222. b0 = pkt[0];
  223. mouse_buttons = b0 & 0x07;
  224. dx = (int)(signed char)pkt[1];
  225. dy = (int)(signed char)pkt[2];
  226. if (b0 & 0x10)
  227. dx |= ~0xFF;
  228. if (b0 & 0x20)
  229. dy |= ~0xFF;
  230. if ((b0 & 0xC0) != 0)
  231. continue;
  232. if (mouse_cursor_flag) {
  233. long nx = (long)cur_x + dx;
  234. long ny = (long)cur_y - dy;
  235. mouse_clamp_ptr(&nx, &ny);
  236. if ((unsigned int)nx != cur_x || (unsigned int)ny != cur_y) {
  237. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  238. cur_saved);
  239. cur_x = (unsigned int)nx;
  240. cur_y = (unsigned int)ny;
  241. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  242. cur_saved);
  243. mouse_draw_cursor(cur_x, cur_y);
  244. }
  245. }
  246. }
  247. }
  248. int mouse_is_present(void)
  249. {
  250. return mouse_present_flag;
  251. }
  252. int mouse_streaming_enabled(void)
  253. {
  254. return mouse_streaming_flag;
  255. }
  256. unsigned int mouse_device_id(void)
  257. {
  258. return mouse_id;
  259. }
  260. int mouse_cursor_visible(void)
  261. {
  262. return mouse_cursor_flag;
  263. }
  264. unsigned int mouse_packet_size(void)
  265. {
  266. return mouse_pkt_size;
  267. }
  268. unsigned int mouse_get_x(void)
  269. {
  270. return cur_x;
  271. }
  272. unsigned int mouse_get_y(void)
  273. {
  274. return cur_y;
  275. }
  276. unsigned int mouse_buttons_state(void)
  277. {
  278. return mouse_buttons;
  279. }
  280. void mouse_redraw_cursor(void)
  281. {
  282. if (!mouse_cursor_flag)
  283. return;
  284. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  285. mouse_draw_cursor(cur_x, cur_y);
  286. }
  287. void mouse_cursor_erase(void)
  288. {
  289. if (!mouse_cursor_flag)
  290. return;
  291. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  292. }