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 static report_mouse_t mouse_report = {};
33 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report);
34 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report);
35 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report);
36 static inline void ps2_mouse_enable_scrolling(void);
37 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report);
39 /* ============================= IMPLEMENTATION ============================ */
41 /* supports only 3 button mouse at this time */
42 void ps2_mouse_init(void) {
45 _delay_ms(PS2_MOUSE_INIT_DELAY); // wait for powering up
47 PS2_MOUSE_SEND(PS2_MOUSE_RESET, "ps2_mouse_init: sending reset");
49 PS2_MOUSE_RECEIVE("ps2_mouse_init: read BAT");
50 PS2_MOUSE_RECEIVE("ps2_mouse_init: read DevID");
52 #ifdef PS2_MOUSE_USE_REMOTE_MODE
53 ps2_mouse_set_remote_mode();
55 ps2_mouse_enable_data_reporting();
58 #ifdef PS2_MOUSE_ENABLE_SCROLLING
59 ps2_mouse_enable_scrolling();
62 #ifdef PS2_MOUSE_USE_2_1_SCALING
63 ps2_mouse_set_scaling_2_1();
66 ps2_mouse_init_user();
70 void ps2_mouse_init_user(void) {
73 void ps2_mouse_task(void) {
74 static uint8_t buttons_prev = 0;
76 /* receives packet from mouse */
78 rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
80 mouse_report.buttons = ps2_host_recv_response();
81 mouse_report.x = ps2_host_recv_response() * PS2_MOUSE_X_MULTIPLIER;
82 mouse_report.y = ps2_host_recv_response() * PS2_MOUSE_Y_MULTIPLIER;
83 #ifdef PS2_MOUSE_ENABLE_SCROLLING
84 mouse_report.v = -(ps2_host_recv_response() & PS2_MOUSE_SCROLL_MASK) * PS2_MOUSE_V_MULTIPLIER;
87 if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
91 /* if mouse moves or buttons state changes */
92 if (mouse_report.x || mouse_report.y || mouse_report.v ||
93 ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
94 #ifdef PS2_MOUSE_DEBUG_RAW
95 // Used to debug raw ps2 bytes from mouse
96 ps2_mouse_print_report(&mouse_report);
98 buttons_prev = mouse_report.buttons;
99 ps2_mouse_convert_report_to_hid(&mouse_report);
100 #if PS2_MOUSE_SCROLL_BTN_MASK
101 ps2_mouse_scroll_button_task(&mouse_report);
103 #ifdef PS2_MOUSE_DEBUG_HID
104 // Used to debug the bytes sent to the host
105 ps2_mouse_print_report(&mouse_report);
107 host_mouse_send(&mouse_report);
110 ps2_mouse_clear_report(&mouse_report);
113 void ps2_mouse_disable_data_reporting(void) {
114 PS2_MOUSE_SEND(PS2_MOUSE_DISABLE_DATA_REPORTING, "ps2 mouse disable data reporting");
117 void ps2_mouse_enable_data_reporting(void) {
118 PS2_MOUSE_SEND(PS2_MOUSE_ENABLE_DATA_REPORTING, "ps2 mouse enable data reporting");
121 void ps2_mouse_set_remote_mode(void) {
122 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_REMOTE_MODE, "ps2 mouse set remote mode");
123 ps2_mouse_mode = PS2_MOUSE_REMOTE_MODE;
126 void ps2_mouse_set_stream_mode(void) {
127 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_STREAM_MODE, "ps2 mouse set stream mode");
128 ps2_mouse_mode = PS2_MOUSE_STREAM_MODE;
131 void ps2_mouse_set_scaling_2_1(void) {
132 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_2_1, "ps2 mouse set scaling 2:1");
135 void ps2_mouse_set_scaling_1_1(void) {
136 PS2_MOUSE_SEND_SAFE(PS2_MOUSE_SET_SCALING_1_1, "ps2 mouse set scaling 1:1");
139 void ps2_mouse_set_resolution(ps2_mouse_resolution_t resolution) {
140 PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_RESOLUTION, resolution, "ps2 mouse set resolution");
143 void ps2_mouse_set_sample_rate(ps2_mouse_sample_rate_t sample_rate) {
144 PS2_MOUSE_SET_SAFE(PS2_MOUSE_SET_SAMPLE_RATE, sample_rate, "ps2 mouse set sample rate");
147 /* ============================= HELPERS ============================ */
149 #define X_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_X_SIGN))
150 #define Y_IS_NEG (mouse_report->buttons & (1<<PS2_MOUSE_Y_SIGN))
151 #define X_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_X_OVFLW))
152 #define Y_IS_OVF (mouse_report->buttons & (1<<PS2_MOUSE_Y_OVFLW))
153 static inline void ps2_mouse_convert_report_to_hid(report_mouse_t *mouse_report) {
154 // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
158 // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
160 // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
161 mouse_report->x = X_IS_NEG ?
162 ((!X_IS_OVF && -127 <= mouse_report->x && mouse_report->x <= -1) ? mouse_report->x : -127) :
163 ((!X_IS_OVF && 0 <= mouse_report->x && mouse_report->x <= 127) ? mouse_report->x : 127);
164 mouse_report->y = Y_IS_NEG ?
165 ((!Y_IS_OVF && -127 <= mouse_report->y && mouse_report->y <= -1) ? mouse_report->y : -127) :
166 ((!Y_IS_OVF && 0 <= mouse_report->y && mouse_report->y <= 127) ? mouse_report->y : 127);
168 // remove sign and overflow flags
169 mouse_report->buttons &= PS2_MOUSE_BTN_MASK;
171 // invert coordinate of y to conform to USB HID mouse
172 mouse_report->y = -mouse_report->y;
175 static inline void ps2_mouse_clear_report(report_mouse_t *mouse_report) {
180 mouse_report->buttons = 0;
183 static inline void ps2_mouse_print_report(report_mouse_t *mouse_report) {
184 if (!debug_mouse) return;
185 print("ps2_mouse: [");
186 phex(mouse_report->buttons); print("|");
187 print_hex8((uint8_t)mouse_report->x); print(" ");
188 print_hex8((uint8_t)mouse_report->y); print(" ");
189 print_hex8((uint8_t)mouse_report->v); print(" ");
190 print_hex8((uint8_t)mouse_report->h); print("]\n");
193 static inline void ps2_mouse_enable_scrolling(void) {
194 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Initiaing scroll wheel enable: Set sample rate");
195 PS2_MOUSE_SEND(200, "200");
196 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
197 PS2_MOUSE_SEND(100, "100");
198 PS2_MOUSE_SEND(PS2_MOUSE_SET_SAMPLE_RATE, "Set sample rate");
199 PS2_MOUSE_SEND(80, "80");
200 PS2_MOUSE_SEND(PS2_MOUSE_GET_DEVICE_ID, "Finished enabling scroll wheel");
204 #define PRESS_SCROLL_BUTTONS mouse_report->buttons |= (PS2_MOUSE_SCROLL_BTN_MASK)
205 #define RELEASE_SCROLL_BUTTONS mouse_report->buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK)
206 static inline void ps2_mouse_scroll_button_task(report_mouse_t *mouse_report) {
211 } scroll_state = SCROLL_NONE;
212 static uint16_t scroll_button_time = 0;
214 if (PS2_MOUSE_SCROLL_BTN_MASK == (mouse_report->buttons & (PS2_MOUSE_SCROLL_BTN_MASK))) {
215 // All scroll buttons are pressed
217 if (scroll_state == SCROLL_NONE) {
218 scroll_button_time = timer_read();
219 scroll_state = SCROLL_BTN;
222 // If the mouse has moved, update the report to scroll instead of move the mouse
223 if (mouse_report->x || mouse_report->y) {
224 scroll_state = SCROLL_SENT;
225 mouse_report->v = -mouse_report->y/(PS2_MOUSE_SCROLL_DIVISOR_V);
226 mouse_report->h = mouse_report->x/(PS2_MOUSE_SCROLL_DIVISOR_H);
230 } else if (0 == (PS2_MOUSE_SCROLL_BTN_MASK & mouse_report->buttons)) {
231 // None of the scroll buttons are pressed
233 #if PS2_MOUSE_SCROLL_BTN_SEND
234 if (scroll_state == SCROLL_BTN
235 && timer_elapsed(scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
236 PRESS_SCROLL_BUTTONS;
237 host_mouse_send(mouse_report);
239 RELEASE_SCROLL_BUTTONS;
242 scroll_state = SCROLL_NONE;
245 RELEASE_SCROLL_BUTTONS;