2 Copyright 2011 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include<util/delay.h>
22 #include "ps2_mouse.h"
23 #include "usb_mouse.h"
25 #define PS2_MOUSE_DEBUG
26 #ifdef PS2_MOUSE_DEBUG
35 // disable when errors occur 255 times.
36 #define ERROR_RETURN() do { \
38 if (ps2_mouse_error_count < 255) { \
39 ps2_mouse_error_count++; \
41 ps2_mouse_error_count = 0; \
42 ps2_mouse_enable = false; \
53 - Tracpoint command support: needed
54 - Middle button + move = Wheel traslation
56 bool ps2_mouse_enable = true;
57 uint8_t ps2_mouse_x = 0;
58 uint8_t ps2_mouse_y = 0;
59 uint8_t ps2_mouse_btn = 0;
60 uint8_t ps2_mouse_error_count = 0;
62 static uint8_t ps2_mouse_btn_prev = 0;
65 uint8_t ps2_mouse_init(void) {
68 if (!ps2_mouse_enable) return 1;
73 rcv = ps2_host_send(0xFF);
74 print("ps2_mouse_init: send 0xFF: ");
75 phex(ps2_error); print("\n");
79 rcv = ps2_host_recv();
80 print("ps2_mouse_init: read ACK: ");
81 phex(rcv); phex(ps2_error); print("\n");
84 // BAT takes some time
86 rcv = ps2_host_recv();
87 print("ps2_mouse_init: read BAT: ");
88 phex(rcv); phex(ps2_error); print("\n");
92 rcv = ps2_host_recv();
93 print("ps2_mouse_init: read DevID: ");
94 phex(rcv); phex(ps2_error); print("\n");
97 // Enable data reporting
99 print("ps2_mouse_init: send 0xF4: ");
100 phex(ps2_error); print("\n");
104 rcv = ps2_host_recv();
105 print("ps2_mouse_init: read ACK: ");
106 phex(rcv); phex(ps2_error); print("\n");
111 print("ps2_mouse_init: send 0xF0: ");
112 phex(ps2_error); print("\n");
116 rcv = ps2_host_recv();
117 print("ps2_mouse_init: read ACK: ");
118 phex(rcv); phex(ps2_error); print("\n");
127 -----------------------------------------------------------------------
128 0 btn: Yovflw Xovflw Ysign Xsign 1 Middle Right Left
129 1 x: X movement(0-255)
130 2 y: Y movement(0-255)
132 uint8_t ps2_mouse_read(void)
136 if (!ps2_mouse_enable) return 1;
145 ps2_mouse_btn = ps2_host_recv();
147 ps2_mouse_x = ps2_host_recv();
149 ps2_mouse_y = ps2_host_recv();
155 bool ps2_mouse_changed(void)
157 return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
160 #define PS2_MOUSE_SCROLL_BUTTON 0x04
161 void ps2_mouse_usb_send(void)
163 static bool scrolled = false;
165 if (!ps2_mouse_enable) return;
167 if (ps2_mouse_changed()) {
171 // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
172 if (ps2_mouse_btn & (1<<PS2_MOUSE_X_SIGN))
173 x = ps2_mouse_x > 128 ? (int8_t)ps2_mouse_x : -127;
175 x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
177 if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
178 y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
180 y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
182 // Y is needed to reverse
185 if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
187 if (x > 0 || x < 0) h = (x > 64 ? 64 : (x < -64 ? -64 :x));
188 if (y > 0 || y < 0) v = (y > 64 ? 64 : (y < -64 ? -64 :y));
191 usb_mouse_send(0,0, -v/16, h/16, 0);
194 } else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
195 usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
197 usb_mouse_send(0,0,0,0, 0);
200 usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
203 ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
211 void ps2_mouse_print(void)
213 if (!debug_mouse) return;
214 print("ps2_mouse[btn|x y]: ");
215 phex(ps2_mouse_btn); print("|");
216 phex(ps2_mouse_x); print(" ");
217 phex(ps2_mouse_y); print("\n");