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>
21 #include "ps2_mouse.h"
29 /* ============================= MACROS ============================ */
31 #define PS2_MOUSE_SEND(command, message) \
33 uint8_t rcv = ps2_host_send(command); \
36 xprintf(" command: %X, result: %X, error: %X \n", command, rcv, ps2_error); \
40 #define PS2_MOUSE_SEND_SAFE(command, message) \
42 if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
43 ps2_mouse_disable_data_reporting(); \
45 PS2_MOUSE_SEND(command, message); \
46 if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
47 ps2_mouse_enable_data_reporting(); \
51 #define PS2_MOUSE_SET_SAFE(command, value, message) \
53 if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
54 ps2_mouse_disable_data_reporting(); \
56 PS2_MOUSE_SEND(command, message); \
57 PS2_MOUSE_SEND(value, "Sending value"); \
58 if (PS2_MOUSE_STREAM_MODE == ps2_mouse_mode) { \
59 ps2_mouse_enable_data_reporting(); \
63 #define PS2_MOUSE_RECEIVE(message) \
65 uint8_t rcv = ps2_host_recv_response(); \
68 xprintf(" result: %X, error: %X \n", rcv, ps2_error); \
72 static enum ps2_mouse_mode_e {
73 PS2_MOUSE_STREAM_MODE,
74 PS2_MOUSE_REMOTE_MODE,
75 } ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
77 static report_mouse_t mouse_report = {};
79 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
80 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
81 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
82 static inline void ps2_mouse_enable_scrolling(void);
83 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
85 /* ============================= IMPLEMENTATION ============================ */
87 /* supports only 3 button mouse at this time */
88 void ps2_mouse_init(void) {
91 _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up
93 PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
95 PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
96 PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
98 #ifdef PS2_MOUSE_USE_REMOTE_MODE
99 ps2_mouse_set_remote_mode();
101 ps2_mouse_enable_data_reporting();
104 #ifdef PS2_MOUSE_ENABLE_SCROLLING
105 ps2_mouse_enable_scrolling();
108 #ifdef PS2_MOUSE_USE_2_1_SCALING
109 ps2_mouse_set_scaling_2_1();
113 void ps2_mouse_task(void) {
114 static uint8_t buttons_prev = 0;
116 /* receives packet from mouse */
118 rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
119 if (rcv == PS2_ACK) {
120 mouse_report.buttons = ps2_host_recv_response();
121 mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
122 mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
123 #ifdef PS2_MOUSE_ENABLE_SCROLLING
124 mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
127 if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
131 /* if mouse moves or buttons state changes */
132 if (mouse_report.x || mouse_report.y || mouse_report.v ||
133 ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
134 #ifdef PS2_MOUSE_DEBUG_RAW
135 // Used to debug raw ps2 bytes from mouse
136 ps2_mouse_print_report(&mouse_report);
138 buttons_prev = mouse_report.buttons;
139 ps2_mouse_convert_report_to_hid(&mouse_report);
140 #if PS2_MOUSE_SCROLL_BTN_MASK
141 ps2_mouse_scroll_button_task(&mouse_report);
143 #ifdef PS2_MOUSE_DEBUG_HID
144 // Used to debug the bytes sent to the host
145 ps2_mouse_print_report(&mouse_report);
147 host_mouse_send(&mouse_report);
150 ps2_mouse_clear_report(&mouse_report);
153 void ps2_mouse_disable_data_reporting(void) {
154 PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting");
157 void ps2_mouse_enable_data_reporting(void) {
158 PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting");
161 void ps2_mouse_set_remote_mode(void) {
162 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
163 ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
166 void ps2_mouse_set_stream_mode(void) {
167 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
168 ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
171 void ps2_mouse_set_scaling_2_1(void) {
172 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1");
175 void ps2_mouse_set_scaling_1_1(void) {
176 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1");
179 void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) {
180 PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution");
183 void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
184 PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate");
187 /* ============================= HELPERS ============================ */
189 #define X_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_X_SIGN))
190 #define Y_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_Y_SIGN))
191 #define X_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_X_OVFLW))
192 #define Y_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_Y_OVFLW))
193 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
194 // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
198 // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
200 // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
201 mouse_report->x = X_IS_NEG ?
202 ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) :
203 ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
204 mouse_report->y = Y_IS_NEG ?
205 ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) :
206 ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
208 // remove sign and overflow flags
209 mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
211 // invert coordinate of y to conform to USB HID mouse
212 mouse_report->y = -mouse_report->y;
215 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
220 mouse_report->buttons = 0;
223 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
224 if (!debug_mouse) return;
225 print("ps2_mouse: [");
226 phex(mouse_report->buttons); print("|");
227 print_hex8((uint8_t)mouse_report->x); print(" ");
228 print_hex8((uint8_t)mouse_report->y); print(" ");
229 print_hex8((uint8_t)mouse_report->v); print(" ");
230 print_hex8((uint8_t)mouse_report->h); print("]\n");
233 static inline void ps2_mouse_enable_scrolling(void) {
234 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
235 PS2_MOUSE_SEND(200, "200");
236 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
237 PS2_MOUSE_SEND(100, "100");
238 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
239 PS2_MOUSE_SEND(80, "80");
240 PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
244 #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
245 #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
246 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
251 } scroll_state = SCROLL_NONE;
252 static uint16_t scroll_button_time = 0;
254 if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
255 // All scroll buttons are pressed
257 if (scroll_state == SCROLL_NONE) {
258 scroll_button_time = timer_read();
259 scroll_state = SCROLL_BTN;
262 // If the mouse has moved, update the report to scroll instead of move the mouse
263 if (mouse_report->x || mouse_report->y) {
264 scroll_state = SCROLL_SENT;
265 mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V);
266 mouse_report->h = mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H);
270 } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
271 // None of the scroll buttons are pressed
273 #if PS2_MOUSE_SCROLL_BTN_SEND
274 if (scroll_state == SCROLL_BTN
275 && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
276 PRESS_SCROLL_BUTTONS;
277 host_mouse_send(mouse_report);
279 RELEASE_SCROLL_BUTTONS;
282 scroll_state = SCROLL_NONE;
285 RELEASE_SCROLL_BUTTONS;