mouse.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "mouse.h"
  2. #include "cirrus.h"
  3. #include "radeon.h"
  4. #include "timer.h"
  5. #include "usbhid.h"
  6. #include "vga.h"
  7. #define PS2_DATA 0x60
  8. #define PS2_STATUS 0x64
  9. #define PS2_CMD 0x64
  10. #define STATUS_OUT_FULL 0x01
  11. #define STATUS_IN_FULL 0x02
  12. #define STATUS_AUX_DATA 0x20
  13. #define MOUSE_ACK 0xFA
  14. #define MOUSE_RESEND 0xFE
  15. #define MOUSE_CUR_W 16
  16. #define MOUSE_CUR_H 16
  17. /* Simple triangle cursor bitmap, 16×16.
  18. Hotspot is the top-left pixel of the 16×16 box (x0,y0).
  19. Each row is encoded as 16 bits; MSB (bit 15) maps to c=0. */
  20. static const unsigned short mouse_pointer_bits[MOUSE_CUR_H] = {
  21. 0x8000, /* 1....... ........ row 0: tip */
  22. 0xC000, /* 11...... ........ row 1 */
  23. 0xE000, /* 111..... ........ row 2 */
  24. 0xF000, /* 1111.... ........ row 3 */
  25. 0xF800, /* 11111... ........ row 4 */
  26. 0xFC00, /* 111111.. ........ row 5 */
  27. 0xFE00, /* 1111111. ........ row 6 */
  28. 0xFF00, /* 11111111 ........ row 7 */
  29. 0xFC00, /* 111111.. ........ row 8 */
  30. 0xE000, /* 111..... ........ row 9 */
  31. 0x8000, /* 1....... ........ row 10 */
  32. 0x0000, /* ........ ........ row 11 */
  33. 0x0000, /* ........ ........ row 12 */
  34. 0x0000, /* ........ ........ row 13 */
  35. 0x0000, /* ........ ........ row 14 */
  36. 0x0000 /* ........ ........ row 15 */
  37. };
  38. static int mouse_present_flag;
  39. static int mouse_streaming_flag;
  40. static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
  41. static unsigned int mouse_id;
  42. static int mouse_cursor_flag;
  43. static unsigned int mouse_pkt_size;
  44. static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
  45. static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon hardware cursor */
  46. static unsigned int fbw, fbh;
  47. static unsigned int cur_x, cur_y;
  48. static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
  49. static unsigned char pkt[3];
  50. static unsigned int pkt_idx;
  51. static inline unsigned char inb(unsigned short port)
  52. {
  53. unsigned char v;
  54. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  55. return v;
  56. }
  57. static inline void outb(unsigned short port, unsigned char v)
  58. {
  59. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  60. }
  61. static void ps2_wait_input_empty(void)
  62. {
  63. unsigned spin = 100000;
  64. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  65. ;
  66. }
  67. static void enable_aux_port(void)
  68. {
  69. ps2_wait_input_empty();
  70. outb(PS2_CMD, 0xA8);
  71. }
  72. static void mouse_cmd_write(unsigned char val)
  73. {
  74. ps2_wait_input_empty();
  75. outb(PS2_CMD, 0xD4);
  76. ps2_wait_input_empty();
  77. outb(PS2_DATA, val);
  78. }
  79. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  80. {
  81. unsigned int t0 = get_ticks_ms();
  82. while (get_ticks_ms() - t0 < timeout_ticks) {
  83. unsigned char st = inb(PS2_STATUS);
  84. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  85. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  86. *out = inb(PS2_DATA);
  87. return 0;
  88. }
  89. }
  90. return -1;
  91. }
  92. static int mouse_expect_ack(void)
  93. {
  94. unsigned char b;
  95. if (mouse_read_aux(&b, 500) != 0)
  96. return -1;
  97. if (b == MOUSE_RESEND)
  98. return -2;
  99. if (b != MOUSE_ACK)
  100. return -1;
  101. return 0;
  102. }
  103. static int mouse_send_cmd_ack(unsigned char cmd)
  104. {
  105. int r;
  106. mouse_cmd_write(cmd);
  107. r = mouse_expect_ack();
  108. if (r == -2) {
  109. mouse_cmd_write(cmd);
  110. r = mouse_expect_ack();
  111. }
  112. return r;
  113. }
  114. static int mouse_bit_at(int r, int c)
  115. {
  116. if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
  117. return 0;
  118. return (mouse_pointer_bits[(unsigned int)r] & (0x8000u >> c)) != 0;
  119. }
  120. static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
  121. {
  122. unsigned int r, c;
  123. /* Draw only the cursor fill bitmap (no outline). */
  124. for (r = 0; r < MOUSE_CUR_H; r++) {
  125. for (c = 0; c < MOUSE_CUR_W; c++) {
  126. if (mouse_bit_at((int)r, (int)c))
  127. vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
  128. }
  129. }
  130. }
  131. static void mouse_clamp_ptr(long *nx, long *ny)
  132. {
  133. long maxx = (long)fbw - (long)MOUSE_CUR_W;
  134. long maxy = (long)fbh - (long)MOUSE_CUR_H;
  135. if (maxx < 0)
  136. maxx = 0;
  137. if (maxy < 0)
  138. maxy = 0;
  139. if (*nx < 0)
  140. *nx = 0;
  141. else if (*nx > maxx)
  142. *nx = maxx;
  143. if (*ny < 0)
  144. *ny = 0;
  145. else if (*ny > maxy)
  146. *ny = maxy;
  147. }
  148. static void mouse_cursor_setup(void)
  149. {
  150. mouse_cursor_flag = 0;
  151. if (!vga_has_vesa())
  152. return;
  153. fbw = vga_fb_width();
  154. fbh = vga_fb_height();
  155. if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
  156. return;
  157. /* Cirrus hardware cursor: the card overlays it; no save/restore. */
  158. if (cirrus_available()) {
  159. mouse_hw_cursor = 1;
  160. cur_x = 100;
  161. cur_y = 100;
  162. cirrus_cursor_setup();
  163. cirrus_cursor_move(cur_x, cur_y);
  164. mouse_cursor_flag = 1;
  165. return;
  166. }
  167. /* Radeon (R200/RV280) hardware cursor. */
  168. if (radeon_available()) {
  169. mouse_hw_cursor = 2;
  170. cur_x = 100;
  171. cur_y = 100;
  172. radeon_cursor_setup();
  173. radeon_cursor_move(cur_x, cur_y);
  174. mouse_cursor_flag = 1;
  175. return;
  176. }
  177. /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
  178. the display engine (see nv50.c); the software cursor is used. */
  179. mouse_hw_cursor = 0;
  180. cur_x = 100;
  181. cur_y = 100;
  182. {
  183. long nx = (long)cur_x;
  184. long ny = (long)cur_y;
  185. mouse_clamp_ptr(&nx, &ny);
  186. cur_x = (unsigned int)nx;
  187. cur_y = (unsigned int)ny;
  188. }
  189. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  190. mouse_draw_cursor(cur_x, cur_y);
  191. mouse_cursor_flag = 1;
  192. }
  193. static int mouse_intellimouse_init(void)
  194. {
  195. /* First three rates are the Intellimouse enable magic (200/100/80);
  196. the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
  197. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
  198. unsigned int i;
  199. for (i = 0; i < sizeof(rates); i++) {
  200. if (mouse_send_cmd_ack(0xF3) != 0)
  201. return -1;
  202. if (mouse_send_cmd_ack(rates[i]) != 0)
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. void mouse_init(void)
  208. {
  209. unsigned char id = 0;
  210. mouse_present_flag = 0;
  211. mouse_streaming_flag = 0;
  212. mouse_id = 0;
  213. mouse_cursor_flag = 0;
  214. mouse_pkt_size = 3;
  215. pkt_idx = 0;
  216. enable_aux_port();
  217. if (mouse_send_cmd_ack(0xF5) != 0)
  218. return;
  219. mouse_intellimouse_init();
  220. mouse_cmd_write(0xF2);
  221. if (mouse_expect_ack() != 0)
  222. return;
  223. if (mouse_read_aux(&id, 500) != 0)
  224. return;
  225. mouse_present_flag = 1;
  226. mouse_id = id;
  227. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  228. mouse_cursor_setup();
  229. if (mouse_send_cmd_ack(0xF4) != 0)
  230. return;
  231. mouse_streaming_flag = 1;
  232. }
  233. /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
  234. +dy up), redrawing it with the current backend (hardware cursor or
  235. shadow-buffer save/restore). */
  236. static void mouse_move(int dx, int dy)
  237. {
  238. long nx, ny;
  239. if (!mouse_cursor_flag)
  240. return;
  241. nx = (long)cur_x + dx;
  242. ny = (long)cur_y - dy;
  243. mouse_clamp_ptr(&nx, &ny);
  244. if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
  245. return;
  246. if (mouse_hw_cursor) {
  247. cur_x = (unsigned int)nx;
  248. cur_y = (unsigned int)ny;
  249. if (mouse_hw_cursor == 2)
  250. radeon_cursor_move(cur_x, cur_y);
  251. else
  252. cirrus_cursor_move(cur_x, cur_y);
  253. } else if (!mouse_auto_draw) {
  254. /* A WM frame is between BEGIN and PRESENT: it erased the
  255. * software cursor and will redraw it at the new position. */
  256. cur_x = (unsigned int)nx;
  257. cur_y = (unsigned int)ny;
  258. } else {
  259. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  260. cur_saved);
  261. cur_x = (unsigned int)nx;
  262. cur_y = (unsigned int)ny;
  263. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H,
  264. cur_saved);
  265. mouse_draw_cursor(cur_x, cur_y);
  266. }
  267. }
  268. void mouse_poll_update(void)
  269. {
  270. unsigned int guard = 0;
  271. unsigned int usb_buttons = 0;
  272. int usb_dx = 0, usb_dy = 0;
  273. /* The cursor is set up for whichever backend shows up first (PS/2
  274. is probed at boot, USB may attach later). */
  275. if ((mouse_present_flag || usbhid_mouse_attached()) && !mouse_cursor_flag)
  276. mouse_cursor_setup();
  277. /* USB HID mouse: reports arrive through the armed interrupt
  278. endpoint; the backend also handles hotplug/detach. */
  279. if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy)) {
  280. mouse_buttons = usb_buttons;
  281. mouse_move(usb_dx, usb_dy);
  282. }
  283. if (!mouse_streaming_flag)
  284. return;
  285. for (;;) {
  286. unsigned char st;
  287. unsigned char b0;
  288. int dx, dy;
  289. /* Bound the drain: a stuck/emulated controller must not
  290. * wedge the WM loop; leftovers are read next iteration. */
  291. if (guard++ >= 64)
  292. return;
  293. st = inb(PS2_STATUS);
  294. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  295. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  296. return;
  297. pkt[pkt_idx++] = inb(PS2_DATA);
  298. if (pkt_idx < mouse_pkt_size)
  299. continue;
  300. pkt_idx = 0;
  301. b0 = pkt[0];
  302. mouse_buttons = b0 & 0x07;
  303. dx = (int)(signed char)pkt[1];
  304. dy = (int)(signed char)pkt[2];
  305. if (b0 & 0x10)
  306. dx |= ~0xFF;
  307. if (b0 & 0x20)
  308. dy |= ~0xFF;
  309. if ((b0 & 0xC0) != 0)
  310. continue;
  311. mouse_move(dx, dy);
  312. }
  313. }
  314. int mouse_is_present(void)
  315. {
  316. return mouse_present_flag || usbhid_mouse_attached();
  317. }
  318. int mouse_ps2_present(void)
  319. {
  320. return mouse_present_flag;
  321. }
  322. int mouse_streaming_enabled(void)
  323. {
  324. return mouse_streaming_flag;
  325. }
  326. unsigned int mouse_device_id(void)
  327. {
  328. return mouse_id;
  329. }
  330. int mouse_cursor_visible(void)
  331. {
  332. return mouse_cursor_flag;
  333. }
  334. int mouse_cursor_backend(void)
  335. {
  336. return mouse_hw_cursor;
  337. }
  338. const char *mouse_cursor_backend_name(void)
  339. {
  340. switch (mouse_hw_cursor) {
  341. case 1: return "cirrus hw";
  342. case 2: return "radeon hw";
  343. default: return "software";
  344. }
  345. }
  346. unsigned int mouse_packet_size(void)
  347. {
  348. return mouse_pkt_size;
  349. }
  350. unsigned int mouse_get_x(void)
  351. {
  352. return cur_x;
  353. }
  354. unsigned int mouse_get_y(void)
  355. {
  356. return cur_y;
  357. }
  358. unsigned int mouse_buttons_state(void)
  359. {
  360. return mouse_buttons;
  361. }
  362. void mouse_redraw_cursor(void)
  363. {
  364. if (!mouse_cursor_flag || mouse_hw_cursor)
  365. return;
  366. vga_fb_read_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  367. mouse_draw_cursor(cur_x, cur_y);
  368. }
  369. void mouse_cursor_erase(void)
  370. {
  371. if (!mouse_cursor_flag || mouse_hw_cursor)
  372. return;
  373. vga_fb_write_rect(cur_x, cur_y, MOUSE_CUR_W, MOUSE_CUR_H, cur_saved);
  374. }
  375. void mouse_set_auto_draw(int on)
  376. {
  377. mouse_auto_draw = on ? 1 : 0;
  378. }