mouse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. #include "mouse.h"
  2. #include "bootinfo.h"
  3. #include "cirrus.h"
  4. #include "cursor.h"
  5. #include "intel.h"
  6. #include "radeon.h"
  7. #include "timer.h"
  8. #include "touchpad.h"
  9. #include "usbhid.h"
  10. #include "vga.h"
  11. #define PS2_DATA 0x60
  12. #define PS2_STATUS 0x64
  13. #define PS2_CMD 0x64
  14. #define STATUS_OUT_FULL 0x01
  15. #define STATUS_IN_FULL 0x02
  16. #define STATUS_AUX_DATA 0x20
  17. #define MOUSE_ACK 0xFA
  18. #define MOUSE_RESEND 0xFE
  19. /* One PS/2 wheel notch in scroll pixels; touchpads emit pixels directly. */
  20. #define MOUSE_WHEEL_PX 48
  21. #define MOUSE_SCROLL_MAX 100000
  22. static int mouse_present_flag;
  23. static int mouse_streaming_flag;
  24. static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
  25. static unsigned int mouse_id;
  26. static int mouse_cursor_flag;
  27. static unsigned int mouse_pkt_size;
  28. static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
  29. static int mouse_scroll_x, mouse_scroll_y;
  30. static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon, 3 = Intel */
  31. static int mouse_irq_mode; /* PS/2 AUX IRQ12 owns packet input */
  32. static int mouse_aux_irq_ok; /* controller config accepted AUX IRQ */
  33. static unsigned int fbw, fbh;
  34. static unsigned int cur_x, cur_y;
  35. static unsigned int saved_x, saved_y, saved_w, saved_h;
  36. static int saved_ox, saved_oy; /* unclipped image origin of the save */
  37. static unsigned char cur_saved[CURSOR_MAX_DIM * CURSOR_MAX_DIM * 4];
  38. static struct cursor_image cur_img; /* the frame on screen */
  39. static int cur_shape = CURSOR_DEFAULT;
  40. static unsigned int cur_frame, cur_frame_ms;
  41. static unsigned char pkt[4];
  42. static unsigned int pkt_idx;
  43. static inline unsigned char inb(unsigned short port)
  44. {
  45. unsigned char v;
  46. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  47. return v;
  48. }
  49. static inline void outb(unsigned short port, unsigned char v)
  50. {
  51. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  52. }
  53. static void ps2_wait_input_empty(void)
  54. {
  55. unsigned spin = 100000;
  56. while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
  57. ;
  58. }
  59. static void enable_aux_port(void)
  60. {
  61. ps2_wait_input_empty();
  62. outb(PS2_CMD, 0xA8);
  63. }
  64. static inline unsigned int mouse_irq_save(void)
  65. {
  66. unsigned int flags;
  67. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  68. return flags;
  69. }
  70. static inline void mouse_irq_restore(unsigned int flags)
  71. {
  72. __asm__ volatile ("pushl %0; popfl" :: "r"(flags));
  73. }
  74. /* Set the controller's AUX-interrupt bit. The PIC line stays masked until
  75. * mouse_init() has a hardware cursor and streaming is enabled. */
  76. static int ps2_aux_irq_config(void)
  77. {
  78. unsigned int flags, spin = 100000;
  79. unsigned char cfg;
  80. flags = mouse_irq_save();
  81. ps2_wait_input_empty();
  82. outb(PS2_CMD, 0x20);
  83. while (spin-- && !(inb(PS2_STATUS) & STATUS_OUT_FULL))
  84. ;
  85. if (!(inb(PS2_STATUS) & STATUS_OUT_FULL)) {
  86. mouse_irq_restore(flags);
  87. return -1;
  88. }
  89. cfg = inb(PS2_DATA);
  90. cfg |= 0x02; /* AUX (mouse) interrupt */
  91. ps2_wait_input_empty();
  92. outb(PS2_CMD, 0x60);
  93. ps2_wait_input_empty();
  94. outb(PS2_DATA, cfg);
  95. mouse_irq_restore(flags);
  96. return 0;
  97. }
  98. static void ps2_aux_irq_unmask(void)
  99. {
  100. unsigned char mask;
  101. /* Master IRQ2 is the cascade, slave IRQ12 is the AUX line. */
  102. mask = inb(0x21);
  103. outb(0x21, (unsigned char)(mask & ~0x04));
  104. mask = inb(0xA1);
  105. outb(0xA1, (unsigned char)(mask & ~0x10));
  106. }
  107. static void mouse_cmd_write(unsigned char val)
  108. {
  109. ps2_wait_input_empty();
  110. outb(PS2_CMD, 0xD4);
  111. ps2_wait_input_empty();
  112. outb(PS2_DATA, val);
  113. }
  114. static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
  115. {
  116. unsigned int t0 = get_ticks_ms();
  117. while (get_ticks_ms() - t0 < timeout_ticks) {
  118. unsigned char st = inb(PS2_STATUS);
  119. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
  120. (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
  121. *out = inb(PS2_DATA);
  122. return 0;
  123. }
  124. }
  125. return -1;
  126. }
  127. static int mouse_expect_ack(void)
  128. {
  129. unsigned char b;
  130. if (mouse_read_aux(&b, 500) != 0)
  131. return -1;
  132. if (b == MOUSE_RESEND)
  133. return -2;
  134. if (b != MOUSE_ACK)
  135. return -1;
  136. return 0;
  137. }
  138. static int mouse_send_cmd_ack(unsigned char cmd)
  139. {
  140. int r;
  141. mouse_cmd_write(cmd);
  142. r = mouse_expect_ack();
  143. if (r == -2) {
  144. mouse_cmd_write(cmd);
  145. r = mouse_expect_ack();
  146. }
  147. return r;
  148. }
  149. /* ---- Cursor image ---------------------------------------------------------
  150. *
  151. * cur_x/cur_y are the hotspot. The image (kernel/cursor.c: an Adwaita
  152. * shape or the built-in arrow) is drawn with its top-left at
  153. * (cur_x - hot_x, cur_y - hot_y), so near the top/left edge part of it is
  154. * off-screen. The software cursor blends the premultiplied ARGB over the
  155. * saved background; the hardware backends take the same image. */
  156. static void cursor_calc_rect(unsigned int *rx, unsigned int *ry,
  157. unsigned int *rw, unsigned int *rh)
  158. {
  159. int ox = (int)cur_x - (int)cur_img.hot_x;
  160. int oy = (int)cur_y - (int)cur_img.hot_y;
  161. int x0 = ox, y0 = oy;
  162. int x1 = ox + (int)cur_img.w, y1 = oy + (int)cur_img.h;
  163. if (x0 < 0) x0 = 0;
  164. if (y0 < 0) y0 = 0;
  165. if (x1 > (int)fbw) x1 = (int)fbw;
  166. if (y1 > (int)fbh) y1 = (int)fbh;
  167. if (x1 <= x0 || y1 <= y0) {
  168. *rx = *ry = *rw = *rh = 0;
  169. return;
  170. }
  171. *rx = (unsigned int)x0;
  172. *ry = (unsigned int)y0;
  173. *rw = (unsigned int)(x1 - x0);
  174. *rh = (unsigned int)(y1 - y0);
  175. }
  176. static void cursor_save_under(void)
  177. {
  178. if (!vga_shadow_is_active()) {
  179. /* Boot: the shadow buffer is not up yet, so there is no
  180. * background to save (and the saved buffer is uninitialized). */
  181. saved_w = saved_h = 0;
  182. return;
  183. }
  184. cursor_calc_rect(&saved_x, &saved_y, &saved_w, &saved_h);
  185. saved_ox = (int)cur_x - (int)cur_img.hot_x;
  186. saved_oy = (int)cur_y - (int)cur_img.hot_y;
  187. if (saved_w && saved_h)
  188. vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  189. }
  190. static void cursor_restore_under(void)
  191. {
  192. if (saved_w && saved_h)
  193. vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
  194. }
  195. /* Push the rectangle the cursor last touched to VRAM right away. The cursor
  196. * must not wait for the damage queue: over a large canvas (the browser) a
  197. * whole-window repaint can sit ahead of it for several flush iterations. */
  198. static void cursor_present(void)
  199. {
  200. if (saved_w && saved_h)
  201. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  202. }
  203. /* Blend the current image over the background saved by cursor_save_under()
  204. * (which must have run for the same position and image). */
  205. static void mouse_draw_cursor(void)
  206. {
  207. unsigned int pb = vga_fb_bpp() == 16 ? 2u : vga_fb_bpp() == 24 ? 3u : 4u;
  208. unsigned int r, c;
  209. if (!cur_img.px)
  210. return;
  211. for (r = 0; r < saved_h; r++) {
  212. unsigned int iy = saved_y + r - (unsigned int)saved_oy;
  213. const unsigned int *src = cur_img.px + iy * cur_img.w +
  214. (saved_x - (unsigned int)saved_ox);
  215. for (c = 0; c < saved_w; c++) {
  216. unsigned int s = src[c];
  217. unsigned int a = s >> 24;
  218. unsigned int sr = (s >> 16) & 0xFF, sg = (s >> 8) & 0xFF;
  219. unsigned int sb = s & 0xFF;
  220. if (a == 0)
  221. continue;
  222. if (a != 255) {
  223. const unsigned char *d =
  224. cur_saved + (r * saved_w + c) * pb;
  225. unsigned int dr, dg, db, inv = 255 - a;
  226. if (pb == 2) {
  227. unsigned int v = d[0] | (d[1] << 8);
  228. dr = ((v >> 11) & 0x1F) * 255 / 31;
  229. dg = ((v >> 5) & 0x3F) * 255 / 63;
  230. db = (v & 0x1F) * 255 / 31;
  231. } else {
  232. db = d[0];
  233. dg = d[1];
  234. dr = d[2];
  235. }
  236. /* Premultiplied source over the background. */
  237. sr += (dr * inv + 127) / 255;
  238. sg += (dg * inv + 127) / 255;
  239. sb += (db * inv + 127) / 255;
  240. if (sr > 255) sr = 255;
  241. if (sg > 255) sg = 255;
  242. if (sb > 255) sb = 255;
  243. }
  244. vga_fb_put_pixel(saved_x + c, saved_y + r, sr, sg, sb);
  245. }
  246. }
  247. }
  248. /* Hardware cursor position: the image's top-left, may be negative. */
  249. static void cursor_hw_move(void)
  250. {
  251. int ox = (int)cur_x - (int)cur_img.hot_x;
  252. int oy = (int)cur_y - (int)cur_img.hot_y;
  253. if (mouse_hw_cursor == 3)
  254. intel_cursor_move(ox, oy);
  255. else if (mouse_hw_cursor == 2)
  256. radeon_cursor_move(ox, oy);
  257. else if (mouse_hw_cursor == 1)
  258. cirrus_cursor_move(ox, oy);
  259. }
  260. static void cursor_hw_load(void)
  261. {
  262. if (mouse_hw_cursor == 3)
  263. intel_cursor_load(&cur_img);
  264. else if (mouse_hw_cursor == 2)
  265. radeon_cursor_load(&cur_img);
  266. else if (mouse_hw_cursor == 1)
  267. cirrus_cursor_load(&cur_img);
  268. }
  269. /* Switch to `img`: re-upload the hardware image, or erase and redraw the
  270. * software cursor (presenting the union of the old and new rectangles in
  271. * one go, so a shape change does not blink). */
  272. static void cursor_set_image(const struct cursor_image *img)
  273. {
  274. unsigned int ox, oy, ow, oh;
  275. if (!mouse_cursor_flag) {
  276. cur_img = *img;
  277. return;
  278. }
  279. if (mouse_hw_cursor) {
  280. unsigned int flags = mouse_irq_save();
  281. cur_img = *img;
  282. cursor_hw_load();
  283. cursor_hw_move();
  284. mouse_irq_restore(flags);
  285. return;
  286. }
  287. if (!mouse_auto_draw) {
  288. /* A WM frame owns the shadow; PRESENT redraws the cursor. */
  289. cur_img = *img;
  290. return;
  291. }
  292. cursor_restore_under();
  293. ox = saved_x; oy = saved_y; ow = saved_w; oh = saved_h;
  294. cur_img = *img;
  295. cursor_save_under();
  296. mouse_draw_cursor();
  297. if (ow && oh && saved_w && saved_h) {
  298. unsigned int x0 = ox < saved_x ? ox : saved_x;
  299. unsigned int y0 = oy < saved_y ? oy : saved_y;
  300. unsigned int x1 = ox + ow > saved_x + saved_w ?
  301. ox + ow : saved_x + saved_w;
  302. unsigned int y1 = oy + oh > saved_y + saved_h ?
  303. oy + oh : saved_y + saved_h;
  304. vga_fb_present_rect(x0, y0, x1 - x0, y1 - y0);
  305. } else {
  306. if (ow && oh)
  307. vga_fb_present_rect(ox, oy, ow, oh);
  308. cursor_present();
  309. }
  310. }
  311. static void cursor_select(int shape, unsigned int frame)
  312. {
  313. struct cursor_image img;
  314. cursor_get(shape, frame, &img);
  315. cursor_set_image(&img);
  316. }
  317. void mouse_set_shape(int shape)
  318. {
  319. if (shape < 0 || shape >= CURSOR_COUNT)
  320. shape = CURSOR_DEFAULT;
  321. if (shape == cur_shape)
  322. return;
  323. cur_shape = shape;
  324. cur_frame = 0;
  325. cur_frame_ms = get_ticks_ms();
  326. cursor_select(cur_shape, 0);
  327. }
  328. int mouse_get_shape(void)
  329. {
  330. return cur_shape;
  331. }
  332. void mouse_cursor_reload(void)
  333. {
  334. cursor_select(cur_shape, cur_frame);
  335. }
  336. /* Advance an animated cursor (wait/progress) from the poll loop. */
  337. static void mouse_cursor_tick(void)
  338. {
  339. unsigned int now;
  340. if (!mouse_cursor_flag || cur_img.frames <= 1)
  341. return;
  342. now = get_ticks_ms();
  343. if (now - cur_frame_ms < cur_img.delay_ms)
  344. return;
  345. cur_frame_ms = now;
  346. cur_frame = (cur_frame + 1) % cur_img.frames;
  347. cursor_select(cur_shape, cur_frame);
  348. }
  349. static void mouse_clamp_ptr(long *nx, long *ny)
  350. {
  351. long maxx = (long)fbw - 1;
  352. long maxy = (long)fbh - 1;
  353. if (maxx < 0)
  354. maxx = 0;
  355. if (maxy < 0)
  356. maxy = 0;
  357. if (*nx < 0)
  358. *nx = 0;
  359. else if (*nx > maxx)
  360. *nx = maxx;
  361. if (*ny < 0)
  362. *ny = 0;
  363. else if (*ny > maxy)
  364. *ny = maxy;
  365. }
  366. static void mouse_cursor_setup(void)
  367. {
  368. mouse_cursor_flag = 0;
  369. if (!vga_has_vesa())
  370. return;
  371. fbw = vga_fb_width();
  372. fbh = vga_fb_height();
  373. if (fbw < CURSOR_MAX_DIM || fbh < CURSOR_MAX_DIM)
  374. return;
  375. cursor_get(cur_shape, cur_frame, &cur_img);
  376. cur_frame_ms = get_ticks_ms();
  377. /* Cirrus hardware cursor: the card overlays it; no save/restore.
  378. * UEFI GOP modes are not programmed through the legacy VGA sequencer,
  379. * so use the software cursor there. */
  380. if (!bootinfo_efi() && cirrus_available()) {
  381. mouse_hw_cursor = 1;
  382. cur_x = 100;
  383. cur_y = 100;
  384. cirrus_cursor_setup(&cur_img);
  385. cursor_hw_move();
  386. mouse_cursor_flag = 1;
  387. return;
  388. }
  389. /* Radeon (R200/RV280) hardware cursor. */
  390. if (!bootinfo_efi() && radeon_available()) {
  391. mouse_hw_cursor = 2;
  392. cur_x = 100;
  393. cur_y = 100;
  394. radeon_cursor_setup(&cur_img);
  395. cursor_hw_move();
  396. mouse_cursor_flag = 1;
  397. return;
  398. }
  399. /* Intel Ironlake (Arrandale/X201) cursor plane. The cursor image is
  400. * mapped through the GTT by intel_init(); only enable it once a mouse
  401. * is actually present. */
  402. if (!bootinfo_efi() && intel_available()) {
  403. long nx = 100;
  404. long ny = 100;
  405. mouse_clamp_ptr(&nx, &ny);
  406. cur_x = (unsigned int)nx;
  407. cur_y = (unsigned int)ny;
  408. intel_cursor_load(&cur_img);
  409. if (intel_cursor_setup((int)cur_x - (int)cur_img.hot_x,
  410. (int)cur_y - (int)cur_img.hot_y) == 0) {
  411. mouse_hw_cursor = 3;
  412. mouse_cursor_flag = 1;
  413. return;
  414. }
  415. mouse_hw_cursor = 0;
  416. }
  417. /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
  418. the display engine (see nv50.c); the software cursor is used. */
  419. mouse_hw_cursor = 0;
  420. cur_x = 100;
  421. cur_y = 100;
  422. {
  423. long nx = (long)cur_x;
  424. long ny = (long)cur_y;
  425. mouse_clamp_ptr(&nx, &ny);
  426. cur_x = (unsigned int)nx;
  427. cur_y = (unsigned int)ny;
  428. }
  429. cursor_save_under();
  430. mouse_draw_cursor();
  431. cursor_present();
  432. mouse_cursor_flag = 1;
  433. }
  434. static int mouse_intellimouse_init(void)
  435. {
  436. /* First three rates are the Intellimouse enable magic (200/100/80);
  437. the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
  438. static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
  439. unsigned int i;
  440. for (i = 0; i < sizeof(rates); i++) {
  441. if (mouse_send_cmd_ack(0xF3) != 0)
  442. return -1;
  443. if (mouse_send_cmd_ack(rates[i]) != 0)
  444. return -1;
  445. }
  446. return 0;
  447. }
  448. void mouse_init(void)
  449. {
  450. unsigned char id = 0;
  451. mouse_present_flag = 0;
  452. mouse_streaming_flag = 0;
  453. mouse_id = 0;
  454. mouse_cursor_flag = 0;
  455. mouse_pkt_size = 3;
  456. mouse_irq_mode = 0;
  457. mouse_aux_irq_ok = 0;
  458. pkt_idx = 0;
  459. /* Built-in HID-over-I2C touchpad. The probe maps the LPSS I2C
  460. * window, so it must run at boot, before the kworker and command
  461. * processes clone the page directory. */
  462. touchpad_init();
  463. enable_aux_port();
  464. mouse_aux_irq_ok = (ps2_aux_irq_config() == 0);
  465. if (mouse_send_cmd_ack(0xF5) != 0)
  466. return;
  467. mouse_intellimouse_init();
  468. mouse_cmd_write(0xF2);
  469. if (mouse_expect_ack() != 0)
  470. return;
  471. if (mouse_read_aux(&id, 500) != 0)
  472. return;
  473. mouse_present_flag = 1;
  474. mouse_id = id;
  475. mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
  476. mouse_cursor_setup();
  477. if (mouse_send_cmd_ack(0xF4) != 0)
  478. return;
  479. mouse_streaming_flag = 1;
  480. /* A hardware cursor makes the IRQ12 path safe: the handler only has
  481. * to write the cursor registers, not touch the shadow buffer or the
  482. * damage list. Otherwise keep the kworker polling the controller. */
  483. if (mouse_hw_cursor && mouse_aux_irq_ok) {
  484. pkt_idx = 0;
  485. mouse_irq_mode = 1;
  486. ps2_aux_irq_unmask();
  487. }
  488. }
  489. /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
  490. +dy up), redrawing it with the current backend (hardware cursor or
  491. shadow-buffer save/restore). */
  492. static void mouse_move(int dx, int dy)
  493. {
  494. long nx, ny;
  495. if (!mouse_cursor_flag)
  496. return;
  497. nx = (long)cur_x + dx;
  498. ny = (long)cur_y - dy;
  499. mouse_clamp_ptr(&nx, &ny);
  500. if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
  501. return;
  502. if (mouse_hw_cursor) {
  503. /* The PS/2 AUX IRQ can preempt a USB-driven update in the
  504. * kworker; keep the position read/modify/write atomic. */
  505. unsigned int flags = mouse_irq_save();
  506. cur_x = (unsigned int)nx;
  507. cur_y = (unsigned int)ny;
  508. cursor_hw_move();
  509. mouse_irq_restore(flags);
  510. } else if (!mouse_auto_draw) {
  511. /* A WM frame is between BEGIN and PRESENT: it erased the
  512. * software cursor and will redraw it at the new position. */
  513. cur_x = (unsigned int)nx;
  514. cur_y = (unsigned int)ny;
  515. } else {
  516. cursor_restore_under();
  517. cursor_present(); /* erase the old position in VRAM */
  518. cur_x = (unsigned int)nx;
  519. cur_y = (unsigned int)ny;
  520. cursor_save_under();
  521. mouse_draw_cursor();
  522. cursor_present(); /* draw the new position in VRAM */
  523. }
  524. }
  525. static void mouse_scroll_add(int dy)
  526. {
  527. mouse_scroll_y += dy;
  528. if (mouse_scroll_y > MOUSE_SCROLL_MAX)
  529. mouse_scroll_y = MOUSE_SCROLL_MAX;
  530. else if (mouse_scroll_y < -MOUSE_SCROLL_MAX)
  531. mouse_scroll_y = -MOUSE_SCROLL_MAX;
  532. }
  533. /* Decode one complete PS/2 packet from pkt[] (3 or 4 bytes). */
  534. static void mouse_handle_packet(void)
  535. {
  536. unsigned char b0 = pkt[0];
  537. int dx = (int)(signed char)pkt[1];
  538. int dy = (int)(signed char)pkt[2];
  539. mouse_buttons = b0 & 0x07;
  540. if (b0 & 0x10)
  541. dx |= ~0xFF;
  542. if (b0 & 0x20)
  543. dy |= ~0xFF;
  544. if ((b0 & 0xC0) != 0)
  545. return;
  546. mouse_move(dx, dy);
  547. if (mouse_pkt_size >= 4) {
  548. /* IntelliMouse wheel: +1 is toward the user (later content). */
  549. int wheel = (int)(signed char)pkt[3];
  550. if (wheel)
  551. mouse_scroll_add(wheel * MOUSE_WHEEL_PX);
  552. }
  553. }
  554. /* IRQ12: PS/2 AUX data. With a hardware cursor the position update is just
  555. * a few MMIO writes, so it is safe and fast in interrupt context. This path
  556. * is only enabled when mouse_irq_mode is set (hardware cursor present). */
  557. void mouse_irq(void)
  558. {
  559. unsigned int guard = 0;
  560. if (mouse_irq_mode) {
  561. for (;;) {
  562. unsigned char st;
  563. if (guard++ >= 16)
  564. break;
  565. st = inb(PS2_STATUS);
  566. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  567. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  568. break;
  569. pkt[pkt_idx++] = inb(PS2_DATA);
  570. if (pkt_idx < mouse_pkt_size)
  571. continue;
  572. pkt_idx = 0;
  573. mouse_handle_packet();
  574. }
  575. }
  576. /* Slave first, then the master cascade. */
  577. outb(0xA0, 0x20);
  578. outb(0x20, 0x20);
  579. }
  580. void mouse_poll_update(void)
  581. {
  582. unsigned int guard = 0;
  583. unsigned int usb_buttons = 0;
  584. int usb_dx = 0, usb_dy = 0, usb_scroll = 0;
  585. int tp_buttons = 0, tp_dx = 0, tp_dy = 0, tp_scroll = 0;
  586. /* The cursor is set up for whichever backend shows up first (PS/2
  587. is probed at boot, USB may attach later). */
  588. if ((mouse_present_flag || usbhid_mouse_attached() ||
  589. touchpad_present()) && !mouse_cursor_flag)
  590. mouse_cursor_setup();
  591. mouse_cursor_tick();
  592. /* USB HID mouse: reports arrive through the armed interrupt
  593. endpoint; the backend also handles hotplug/detach. */
  594. if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy, &usb_scroll)) {
  595. mouse_buttons = usb_buttons;
  596. mouse_move(usb_dx, usb_dy);
  597. if (usb_scroll)
  598. mouse_scroll_add(usb_scroll);
  599. }
  600. /* Built-in touchpad: absolute positions converted to deltas. */
  601. if (touchpad_poll(&tp_buttons, &tp_dx, &tp_dy, &tp_scroll)) {
  602. mouse_buttons = tp_buttons;
  603. mouse_move(tp_dx, tp_dy);
  604. if (tp_scroll)
  605. mouse_scroll_add(tp_scroll);
  606. }
  607. if (mouse_irq_mode)
  608. return; /* IRQ12 owns the PS/2 packets */
  609. if (!mouse_streaming_flag)
  610. return;
  611. for (;;) {
  612. unsigned char st;
  613. /* Bound the drain: a stuck/emulated controller must not
  614. * wedge the WM loop; leftovers are read next iteration. */
  615. if (guard++ >= 64)
  616. return;
  617. st = inb(PS2_STATUS);
  618. if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
  619. (STATUS_OUT_FULL | STATUS_AUX_DATA))
  620. return;
  621. pkt[pkt_idx++] = inb(PS2_DATA);
  622. if (pkt_idx < mouse_pkt_size)
  623. continue;
  624. pkt_idx = 0;
  625. mouse_handle_packet();
  626. }
  627. }
  628. int mouse_is_present(void)
  629. {
  630. return mouse_present_flag || usbhid_mouse_attached() ||
  631. touchpad_present();
  632. }
  633. int mouse_ps2_present(void)
  634. {
  635. return mouse_present_flag;
  636. }
  637. int mouse_streaming_enabled(void)
  638. {
  639. return mouse_streaming_flag;
  640. }
  641. unsigned int mouse_device_id(void)
  642. {
  643. return mouse_id;
  644. }
  645. int mouse_cursor_visible(void)
  646. {
  647. return mouse_cursor_flag;
  648. }
  649. int mouse_cursor_backend(void)
  650. {
  651. return mouse_hw_cursor;
  652. }
  653. const char *mouse_cursor_backend_name(void)
  654. {
  655. switch (mouse_hw_cursor) {
  656. case 1: return "cirrus hw";
  657. case 2: return "radeon hw";
  658. case 3: return "intel hw";
  659. default: return "software";
  660. }
  661. }
  662. unsigned int mouse_packet_size(void)
  663. {
  664. return mouse_pkt_size;
  665. }
  666. unsigned int mouse_get_x(void)
  667. {
  668. return cur_x;
  669. }
  670. unsigned int mouse_get_y(void)
  671. {
  672. return cur_y;
  673. }
  674. unsigned int mouse_buttons_state(void)
  675. {
  676. return mouse_buttons;
  677. }
  678. void mouse_scroll_take(int *dx, int *dy)
  679. {
  680. unsigned int flags = mouse_irq_save();
  681. *dx = mouse_scroll_x;
  682. *dy = mouse_scroll_y;
  683. mouse_scroll_x = 0;
  684. mouse_scroll_y = 0;
  685. mouse_irq_restore(flags);
  686. }
  687. void mouse_redraw_cursor(void)
  688. {
  689. if (!mouse_cursor_flag || mouse_hw_cursor)
  690. return;
  691. /* If the pointer moved while a WM frame owned the shadow, the saved
  692. * rectangle is still the old position and has been restored to the
  693. * background there. Erase it in VRAM before drawing the new one, so
  694. * a moving cursor does not leave a trail behind a slow frame. */
  695. if (saved_w && saved_h) {
  696. unsigned int nx, ny, nw, nh;
  697. cursor_calc_rect(&nx, &ny, &nw, &nh);
  698. if (nx != saved_x || ny != saved_y || nw != saved_w ||
  699. nh != saved_h)
  700. vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
  701. }
  702. cursor_save_under();
  703. mouse_draw_cursor();
  704. cursor_present();
  705. }
  706. void mouse_cursor_erase(void)
  707. {
  708. if (!mouse_cursor_flag || mouse_hw_cursor)
  709. return;
  710. cursor_restore_under();
  711. /* No direct present here: the caller is about to redraw the scene.
  712. * Keeping the old cursor on screen until that flush avoids a visible
  713. * blink at the start of every window frame. The restore already
  714. * marked the rectangle dirty, so the flush erases it. */
  715. }
  716. void mouse_set_auto_draw(int on)
  717. {
  718. mouse_auto_draw = on ? 1 : 0;
  719. }