2 Copyright 2011,2013 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"
26 #define PS2_MOUSE_DEBUG
27 #ifdef PS2_MOUSE_DEBUG
37 static report_mouse_t mouse_report
= {};
40 static void ps2_mouse_print_raw_data(void);
41 static void ps2_mouse_print_usb_data(void);
44 /* supports only 3 button mouse at this time */
45 uint8_t ps2_mouse_init(void) {
50 _delay_ms(1000); // wait for powering up
53 rcv
= ps2_host_send(0xFF);
54 print("ps2_mouse_init: send Reset: ");
55 phex(rcv
); phex(ps2_error
); print("\n");
57 // read completion code of BAT
58 rcv
= ps2_host_recv();
59 print("ps2_mouse_init: read BAT: ");
60 phex(rcv
); phex(ps2_error
); print("\n");
63 rcv
= ps2_host_recv();
64 print("ps2_mouse_init: read DevID: ");
65 phex(rcv
); phex(ps2_error
); print("\n");
67 // send Enable Data Reporting
68 rcv
= ps2_host_send(0xF4);
69 print("ps2_mouse_init: send 0xF4: ");
70 phex(rcv
); phex(ps2_error
); print("\n");
72 // send Set Remote mode
73 rcv
= ps2_host_send(0xF0);
74 print("ps2_mouse_init: send 0xF0: ");
75 phex(rcv
); phex(ps2_error
); print("\n");
81 * TODO: should be build option
83 #define PS2_MOUSE_SCROLL_BUTTON 0x04
84 #define X_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_X_SIGN))
85 #define Y_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_Y_SIGN))
86 #define X_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_X_OVFLW))
87 #define Y_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_Y_OVFLW))
88 void ps2_mouse_task(void)
90 enum { SCROLL_NONE
, SCROLL_BTN
, SCROLL_SENT
};
91 static uint8_t scroll_state
= SCROLL_NONE
;
92 static uint8_t buttons_prev
= 0;
94 /* receives packet from mouse */
96 rcv
= ps2_host_send(PS2_MOUSE_READ_DATA
);
98 mouse_report
.buttons
= ps2_host_recv_response();
99 mouse_report
.x
= ps2_host_recv_response();
100 mouse_report
.y
= ps2_host_recv_response();
102 if (!debug_mouse
) print("ps2_mouse: fail to get mouse packet\n");
106 /* if mouse moves or buttons state changes */
107 if (mouse_report
.x
|| mouse_report
.y
||
108 ((mouse_report
.buttons
^ buttons_prev
) & PS2_MOUSE_BTN_MASK
)) {
110 ps2_mouse_print_raw_data();
112 buttons_prev
= mouse_report
.buttons
;
114 // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
118 // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
120 // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
121 mouse_report
.x
= X_IS_NEG
?
122 ((!X_IS_OVF
&& -127 <= mouse_report
.x
&& mouse_report
.x
<= -1) ? mouse_report
.x
: -127) :
123 ((!X_IS_OVF
&& 0 <= mouse_report
.x
&& mouse_report
.x
<= 127) ? mouse_report
.x
: 127);
124 mouse_report
.y
= Y_IS_NEG
?
125 ((!Y_IS_OVF
&& -127 <= mouse_report
.y
&& mouse_report
.y
<= -1) ? mouse_report
.y
: -127) :
126 ((!Y_IS_OVF
&& 0 <= mouse_report
.y
&& mouse_report
.y
<= 127) ? mouse_report
.y
: 127);
128 // remove sign and overflow flags
129 mouse_report
.buttons
&= PS2_MOUSE_BTN_MASK
;
131 // invert coordinate of y to conform to USB HID mouse
132 mouse_report
.y
= -mouse_report
.y
;
135 if ((mouse_report
.buttons
& PS2_MOUSE_SCROLL_BUTTON
) == PS2_MOUSE_SCROLL_BUTTON
) {
136 if (scroll_state
== SCROLL_NONE
) scroll_state
= SCROLL_BTN
;
138 // doesn't send Scroll Button
139 mouse_report
.buttons
&= ~PS2_MOUSE_SCROLL_BUTTON
;
141 if (mouse_report
.x
|| mouse_report
.y
) {
142 scroll_state
= SCROLL_SENT
;
144 mouse_report
.v
= -mouse_report
.y
/2;
145 mouse_report
.h
= mouse_report
.x
/2;
148 host_mouse_send(&mouse_report
);
150 } else if (scroll_state
== SCROLL_BTN
&&
151 (mouse_report
.buttons
& PS2_MOUSE_SCROLL_BUTTON
) == 0) {
152 scroll_state
= SCROLL_NONE
;
154 // send Scroll Button(down and up at once) when not scrolled
155 mouse_report
.buttons
|= PS2_MOUSE_SCROLL_BUTTON
;
156 host_mouse_send(&mouse_report
);
158 mouse_report
.buttons
&= ~PS2_MOUSE_SCROLL_BUTTON
;
159 host_mouse_send(&mouse_report
);
161 scroll_state
= SCROLL_NONE
;
163 host_mouse_send(&mouse_report
);
165 ps2_mouse_print_usb_data();
172 mouse_report
.buttons
= 0;
175 static void ps2_mouse_print_raw_data(void)
177 if (!debug_mouse
) return;
178 print("ps2_mouse raw [btn|x y]: [");
179 phex(mouse_report
.buttons
); print("|");
180 print_hex8((uint8_t)mouse_report
.x
); print(" ");
181 print_hex8((uint8_t)mouse_report
.y
); print("]\n");
184 static void ps2_mouse_print_usb_data(void)
186 if (!debug_mouse
) return;
187 print("ps2_mouse usb [btn|x y v h]: [");
188 phex(mouse_report
.buttons
); print("|");
189 print_hex8((uint8_t)mouse_report
.x
); print(" ");
190 print_hex8((uint8_t)mouse_report
.y
); print(" ");
191 print_hex8((uint8_t)mouse_report
.v
); print(" ");
192 print_hex8((uint8_t)mouse_report
.h
); print("]\n");
196 /* PS/2 Mouse Synopsis
197 * http://www.computer-engineering.org/ps2mouse/
201 * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
202 * 0xF5: Disable Data Reporting
203 * 0xF4: Enable Data Reporting
204 * 0xF3: Set Sample Rate
205 * 0xF2: Get Device ID
206 * 0xF0: Set Remote Mode
208 * 0xEA: Set Stream Mode
209 * 0xE9: Status Request
210 * 0xE8: Set Resolution
211 * 0xE7: Set Scaling 2:1
212 * 0xE6: Set Scaling 1:1
215 * Stream Mode: devices sends the data when it changs its state
216 * Remote Mode: host polls the data periodically
218 * This code uses Remote Mode and polls the data with Read Data(0xEB).
221 * byte|7 6 5 4 3 2 1 0
222 * ----+--------------------------------------------------------------
223 * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left