| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #include "mouse.h"
- #include "bootinfo.h"
- #include "cirrus.h"
- #include "cursor_shape.h"
- #include "radeon.h"
- #include "timer.h"
- #include "usbhid.h"
- #include "vga.h"
- #define PS2_DATA 0x60
- #define PS2_STATUS 0x64
- #define PS2_CMD 0x64
- #define STATUS_OUT_FULL 0x01
- #define STATUS_IN_FULL 0x02
- #define STATUS_AUX_DATA 0x20
- #define MOUSE_ACK 0xFA
- #define MOUSE_RESEND 0xFE
- #define MOUSE_CUR_W CURSOR_SHAPE_WIDTH
- #define MOUSE_CUR_H CURSOR_SHAPE_HEIGHT
- static int mouse_present_flag;
- static int mouse_streaming_flag;
- static int mouse_auto_draw = 1; /* 0 while a WM frame owns the shadow */
- static unsigned int mouse_id;
- static int mouse_cursor_flag;
- static unsigned int mouse_pkt_size;
- static unsigned int mouse_buttons; /* bit0=left bit1=right bit2=middle */
- static int mouse_hw_cursor; /* 1 = Cirrus, 2 = Radeon hardware cursor */
- static unsigned int fbw, fbh;
- static unsigned int cur_x, cur_y;
- static unsigned int saved_x, saved_y, saved_w, saved_h;
- static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
- static unsigned char pkt[3];
- static unsigned int pkt_idx;
- static inline unsigned char inb(unsigned short port)
- {
- unsigned char v;
- __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
- return v;
- }
- static inline void outb(unsigned short port, unsigned char v)
- {
- __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
- }
- static void ps2_wait_input_empty(void)
- {
- unsigned spin = 100000;
- while (spin-- && (inb(PS2_STATUS) & STATUS_IN_FULL))
- ;
- }
- static void enable_aux_port(void)
- {
- ps2_wait_input_empty();
- outb(PS2_CMD, 0xA8);
- }
- static void mouse_cmd_write(unsigned char val)
- {
- ps2_wait_input_empty();
- outb(PS2_CMD, 0xD4);
- ps2_wait_input_empty();
- outb(PS2_DATA, val);
- }
- static int mouse_read_aux(unsigned char *out, unsigned int timeout_ticks)
- {
- unsigned int t0 = get_ticks_ms();
- while (get_ticks_ms() - t0 < timeout_ticks) {
- unsigned char st = inb(PS2_STATUS);
- if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) ==
- (STATUS_OUT_FULL | STATUS_AUX_DATA)) {
- *out = inb(PS2_DATA);
- return 0;
- }
- }
- return -1;
- }
- static int mouse_expect_ack(void)
- {
- unsigned char b;
- if (mouse_read_aux(&b, 500) != 0)
- return -1;
- if (b == MOUSE_RESEND)
- return -2;
- if (b != MOUSE_ACK)
- return -1;
- return 0;
- }
- static int mouse_send_cmd_ack(unsigned char cmd)
- {
- int r;
- mouse_cmd_write(cmd);
- r = mouse_expect_ack();
- if (r == -2) {
- mouse_cmd_write(cmd);
- r = mouse_expect_ack();
- }
- return r;
- }
- enum {
- MOUSE_PIX_TRANSPARENT = 0,
- MOUSE_PIX_OUTLINE,
- MOUSE_PIX_FILL
- };
- static int mouse_pixel_at(int r, int c)
- {
- if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
- return MOUSE_PIX_TRANSPARENT;
- if (cursor_shape_fill[(unsigned int)r] & (0x8000u >> c))
- return MOUSE_PIX_FILL;
- if (cursor_shape_outline[(unsigned int)r] & (0x8000u >> c))
- return MOUSE_PIX_OUTLINE;
- return MOUSE_PIX_TRANSPARENT;
- }
- static void cursor_save_under(void)
- {
- saved_x = cur_x;
- saved_y = cur_y;
- saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
- saved_h = (cur_y + MOUSE_CUR_H <= fbh) ? MOUSE_CUR_H : fbh - cur_y;
- if (saved_w && saved_h)
- vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
- }
- static void cursor_restore_under(void)
- {
- if (saved_w && saved_h)
- vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
- }
- static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
- {
- unsigned int r, c;
- /* White arrow body with a black outline around it. */
- for (r = 0; r < MOUSE_CUR_H; r++) {
- for (c = 0; c < MOUSE_CUR_W; c++) {
- int px = mouse_pixel_at((int)r, (int)c);
- if (px == MOUSE_PIX_FILL)
- vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
- else if (px == MOUSE_PIX_OUTLINE)
- vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
- }
- }
- }
- static void mouse_clamp_ptr(long *nx, long *ny)
- {
- long maxx = (long)fbw - 1;
- long maxy = (long)fbh - 1;
- if (maxx < 0)
- maxx = 0;
- if (maxy < 0)
- maxy = 0;
- if (*nx < 0)
- *nx = 0;
- else if (*nx > maxx)
- *nx = maxx;
- if (*ny < 0)
- *ny = 0;
- else if (*ny > maxy)
- *ny = maxy;
- }
- static void mouse_cursor_setup(void)
- {
- mouse_cursor_flag = 0;
- if (!vga_has_vesa())
- return;
- fbw = vga_fb_width();
- fbh = vga_fb_height();
- if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
- return;
- /* Cirrus hardware cursor: the card overlays it; no save/restore.
- * UEFI GOP modes are not programmed through the legacy VGA sequencer,
- * so use the software cursor there. */
- if (!bootinfo_efi() && cirrus_available()) {
- mouse_hw_cursor = 1;
- cur_x = 100;
- cur_y = 100;
- cirrus_cursor_setup();
- cirrus_cursor_move(cur_x, cur_y);
- mouse_cursor_flag = 1;
- return;
- }
- /* Radeon (R200/RV280) hardware cursor. */
- if (!bootinfo_efi() && radeon_available()) {
- mouse_hw_cursor = 2;
- cur_x = 100;
- cur_y = 100;
- radeon_cursor_setup();
- radeon_cursor_move(cur_x, cur_y);
- mouse_cursor_flag = 1;
- return;
- }
- /* NVIDIA nv50-family (ION/MCP79): no hardware cursor -- the VBIOS keeps
- the display engine (see nv50.c); the software cursor is used. */
- mouse_hw_cursor = 0;
- cur_x = 100;
- cur_y = 100;
- {
- long nx = (long)cur_x;
- long ny = (long)cur_y;
- mouse_clamp_ptr(&nx, &ny);
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- }
- cursor_save_under();
- mouse_draw_cursor(cur_x, cur_y);
- mouse_cursor_flag = 1;
- }
- static int mouse_intellimouse_init(void)
- {
- /* First three rates are the Intellimouse enable magic (200/100/80);
- the last is the final report rate. 0xC8 = 200 Hz for a smooth cursor. */
- static const unsigned char rates[] = { 0xC8, 0x64, 0x50, 0xC8 };
- unsigned int i;
- for (i = 0; i < sizeof(rates); i++) {
- if (mouse_send_cmd_ack(0xF3) != 0)
- return -1;
- if (mouse_send_cmd_ack(rates[i]) != 0)
- return -1;
- }
- return 0;
- }
- void mouse_init(void)
- {
- unsigned char id = 0;
- mouse_present_flag = 0;
- mouse_streaming_flag = 0;
- mouse_id = 0;
- mouse_cursor_flag = 0;
- mouse_pkt_size = 3;
- pkt_idx = 0;
- enable_aux_port();
- if (mouse_send_cmd_ack(0xF5) != 0)
- return;
- mouse_intellimouse_init();
- mouse_cmd_write(0xF2);
- if (mouse_expect_ack() != 0)
- return;
- if (mouse_read_aux(&id, 500) != 0)
- return;
- mouse_present_flag = 1;
- mouse_id = id;
- mouse_pkt_size = (id == 0x03 || id == 0x04) ? 4u : 3u;
- mouse_cursor_setup();
- if (mouse_send_cmd_ack(0xF4) != 0)
- return;
- mouse_streaming_flag = 1;
- }
- /* Move the cursor by a relative delta (PS/2 sign convention: +dx right,
- +dy up), redrawing it with the current backend (hardware cursor or
- shadow-buffer save/restore). */
- static void mouse_move(int dx, int dy)
- {
- long nx, ny;
- if (!mouse_cursor_flag)
- return;
- nx = (long)cur_x + dx;
- ny = (long)cur_y - dy;
- mouse_clamp_ptr(&nx, &ny);
- if ((unsigned int)nx == cur_x && (unsigned int)ny == cur_y)
- return;
- if (mouse_hw_cursor) {
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- if (mouse_hw_cursor == 2)
- radeon_cursor_move(cur_x, cur_y);
- else
- cirrus_cursor_move(cur_x, cur_y);
- } else if (!mouse_auto_draw) {
- /* A WM frame is between BEGIN and PRESENT: it erased the
- * software cursor and will redraw it at the new position. */
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- } else {
- cursor_restore_under();
- cur_x = (unsigned int)nx;
- cur_y = (unsigned int)ny;
- cursor_save_under();
- mouse_draw_cursor(cur_x, cur_y);
- }
- }
- void mouse_poll_update(void)
- {
- unsigned int guard = 0;
- unsigned int usb_buttons = 0;
- int usb_dx = 0, usb_dy = 0;
- /* The cursor is set up for whichever backend shows up first (PS/2
- is probed at boot, USB may attach later). */
- if ((mouse_present_flag || usbhid_mouse_attached()) && !mouse_cursor_flag)
- mouse_cursor_setup();
- /* USB HID mouse: reports arrive through the armed interrupt
- endpoint; the backend also handles hotplug/detach. */
- if (usbhid_poll(&usb_buttons, &usb_dx, &usb_dy)) {
- mouse_buttons = usb_buttons;
- mouse_move(usb_dx, usb_dy);
- }
- if (!mouse_streaming_flag)
- return;
- for (;;) {
- unsigned char st;
- unsigned char b0;
- int dx, dy;
- /* Bound the drain: a stuck/emulated controller must not
- * wedge the WM loop; leftovers are read next iteration. */
- if (guard++ >= 64)
- return;
- st = inb(PS2_STATUS);
- if ((st & (STATUS_OUT_FULL | STATUS_AUX_DATA)) !=
- (STATUS_OUT_FULL | STATUS_AUX_DATA))
- return;
- pkt[pkt_idx++] = inb(PS2_DATA);
- if (pkt_idx < mouse_pkt_size)
- continue;
- pkt_idx = 0;
- b0 = pkt[0];
- mouse_buttons = b0 & 0x07;
- dx = (int)(signed char)pkt[1];
- dy = (int)(signed char)pkt[2];
- if (b0 & 0x10)
- dx |= ~0xFF;
- if (b0 & 0x20)
- dy |= ~0xFF;
- if ((b0 & 0xC0) != 0)
- continue;
- mouse_move(dx, dy);
- }
- }
- int mouse_is_present(void)
- {
- return mouse_present_flag || usbhid_mouse_attached();
- }
- int mouse_ps2_present(void)
- {
- return mouse_present_flag;
- }
- int mouse_streaming_enabled(void)
- {
- return mouse_streaming_flag;
- }
- unsigned int mouse_device_id(void)
- {
- return mouse_id;
- }
- int mouse_cursor_visible(void)
- {
- return mouse_cursor_flag;
- }
- int mouse_cursor_backend(void)
- {
- return mouse_hw_cursor;
- }
- const char *mouse_cursor_backend_name(void)
- {
- switch (mouse_hw_cursor) {
- case 1: return "cirrus hw";
- case 2: return "radeon hw";
- default: return "software";
- }
- }
- unsigned int mouse_packet_size(void)
- {
- return mouse_pkt_size;
- }
- unsigned int mouse_get_x(void)
- {
- return cur_x;
- }
- unsigned int mouse_get_y(void)
- {
- return cur_y;
- }
- unsigned int mouse_buttons_state(void)
- {
- return mouse_buttons;
- }
- void mouse_redraw_cursor(void)
- {
- if (!mouse_cursor_flag || mouse_hw_cursor)
- return;
- cursor_save_under();
- mouse_draw_cursor(cur_x, cur_y);
- }
- void mouse_cursor_erase(void)
- {
- if (!mouse_cursor_flag || mouse_hw_cursor)
- return;
- cursor_restore_under();
- }
- void mouse_set_auto_draw(int on)
- {
- mouse_auto_draw = on ? 1 : 0;
- }
|