mouse.c 20 KB

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