mouse.c 7.9 KB

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