mouse.c 9.8 KB

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