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"
30 static report_mouse_t mouse_report = {};
33 static void print_usb_data(void);
36 /* supports only 3 button mouse at this time */
37 uint8_t ps2_mouse_init(void) {
42 _delay_ms(1000); // wait for powering up
45 rcv = ps2_host_send(0xFF);
46 print("ps2_mouse_init: send Reset: ");
47 phex(rcv); phex(ps2_error); print("\n");
49 // read completion code of BAT
50 rcv = ps2_host_recv_response();
51 print("ps2_mouse_init: read BAT: ");
52 phex(rcv); phex(ps2_error); print("\n");
55 rcv = ps2_host_recv_response();
56 print("ps2_mouse_init: read DevID: ");
57 phex(rcv); phex(ps2_error); print("\n");
59 // send Set Remote mode
60 rcv = ps2_host_send(0xF0);
61 print("ps2_mouse_init: send 0xF0: ");
62 phex(rcv); phex(ps2_error); print("\n");
67 #define X_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_X_SIGN))
68 #define Y_IS_NEG (mouse_report.buttons & (1<<PS2_MOUSE_Y_SIGN))
69 #define X_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_X_OVFLW))
70 #define Y_IS_OVF (mouse_report.buttons & (1<<PS2_MOUSE_Y_OVFLW))
71 void ps2_mouse_task(void)
73 enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT };
74 static uint8_t scroll_state = SCROLL_NONE;
75 static uint8_t buttons_prev = 0;
77 /* receives packet from mouse */
79 rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
81 mouse_report.buttons = ps2_host_recv_response();
82 mouse_report.x = ps2_host_recv_response();
83 mouse_report.y = ps2_host_recv_response();
85 if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
89 /* if mouse moves or buttons state changes */
90 if (mouse_report.x || mouse_report.y ||
91 ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
93 #ifdef PS2_MOUSE_DEBUG
94 xprintf("%ud ", timer_read());
95 print("ps2_mouse raw: [");
96 phex(mouse_report.buttons); print("|");
97 print_hex8((uint8_t)mouse_report.x); print(" ");
98 print_hex8((uint8_t)mouse_report.y); print("]\n");
101 buttons_prev = mouse_report.buttons;
103 // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
107 // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
109 // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
110 mouse_report.x = X_IS_NEG ?
111 ((!X_IS_OVF && -127 <= mouse_report.x && mouse_report.x <= -1) ? mouse_report.x : -127) :
112 ((!X_IS_OVF && 0 <= mouse_report.x && mouse_report.x <= 127) ? mouse_report.x : 127);
113 mouse_report.y = Y_IS_NEG ?
114 ((!Y_IS_OVF && -127 <= mouse_report.y && mouse_report.y <= -1) ? mouse_report.y : -127) :
115 ((!Y_IS_OVF && 0 <= mouse_report.y && mouse_report.y <= 127) ? mouse_report.y : 127);
117 // remove sign and overflow flags
118 mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
120 // invert coordinate of y to conform to USB HID mouse
121 mouse_report.y = -mouse_report.y;
124 #if PS2_MOUSE_SCROLL_BTN_MASK
125 static uint16_t scroll_button_time = 0;
126 if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == (PS2_MOUSE_SCROLL_BTN_MASK)) {
127 if (scroll_state == SCROLL_NONE) {
128 scroll_button_time = timer_read();
129 scroll_state = SCROLL_BTN;
132 // doesn't send Scroll Button
133 //mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
135 if (mouse_report.x || mouse_report.y) {
136 scroll_state = SCROLL_SENT;
138 mouse_report.v = -mouse_report.y/(PS2_MOUSE_SCROLL_DIVISOR_V);
139 mouse_report.h = mouse_report.x/(PS2_MOUSE_SCROLL_DIVISOR_H);
142 //host_mouse_send(&mouse_report);
145 else if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == 0) {
146 #if PS2_MOUSE_SCROLL_BTN_SEND
147 if (scroll_state == SCROLL_BTN &&
148 TIMER_DIFF_16(timer_read(), scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
149 // send Scroll Button(down and up at once) when not scrolled
150 mouse_report.buttons |= (PS2_MOUSE_SCROLL_BTN_MASK);
151 host_mouse_send(&mouse_report);
153 mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
156 scroll_state = SCROLL_NONE;
158 // doesn't send Scroll Button
159 mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
163 host_mouse_send(&mouse_report);
171 mouse_report.buttons = 0;
174 static void print_usb_data(void)
176 if (!debug_mouse) return;
177 print("ps2_mouse usb: [");
178 phex(mouse_report.buttons); print("|");
179 print_hex8((uint8_t)mouse_report.x); print(" ");
180 print_hex8((uint8_t)mouse_report.y); print(" ");
181 print_hex8((uint8_t)mouse_report.v); print(" ");
182 print_hex8((uint8_t)mouse_report.h); print("]\n");
186 /* PS/2 Mouse Synopsis
187 * http://www.computer-engineering.org/ps2mouse/
191 * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
192 * 0xF5: Disable Data Reporting
193 * 0xF4: Enable Data Reporting
194 * 0xF3: Set Sample Rate
195 * 0xF2: Get Device ID
196 * 0xF0: Set Remote Mode
198 * 0xEA: Set Stream Mode
199 * 0xE9: Status Request
200 * 0xE8: Set Resolution
201 * 0xE7: Set Scaling 2:1
202 * 0xE6: Set Scaling 1:1
205 * Stream Mode: devices sends the data when it changs its state
206 * Remote Mode: host polls the data periodically
208 * This code uses Remote Mode and polls the data with Read Data(0xEB).
211 * byte|7 6 5 4 3 2 1 0
212 * ----+--------------------------------------------------------------
213 * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left