mouse.c 7.5 KB

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