c3e8b3c1c3354753cc3ce12d902c6978caf8af16
[tmk_keyboard.git] / tmk_core / protocol / ps2_mouse.c
1 /*
2 Copyright 2011,2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdbool.h>
19 #include<avr/io.h>
20 #include<util/delay.h>
21 #include "ps2.h"
22 #include "ps2_mouse.h"
23 #include "report.h"
24 #include "host.h"
25 #include "timer.h"
26 #include "print.h"
27 #include "debug.h"
28
29
30 static report_mouse_t mouse_report = {};
31
32
33 static void print_usb_data(void);
34
35
36 /* supports only 3 button mouse at this time */
37 uint8_t ps2_mouse_init(void) {
38 uint8_t rcv;
39
40 ps2_host_init();
41
42 _delay_ms(1000); // wait for powering up
43
44 // send Reset
45 rcv = ps2_host_send(0xFF);
46 print("ps2_mouse_init: send Reset: ");
47 phex(rcv); phex(ps2_error); print("\n");
48
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");
53
54 // read Device ID
55 rcv = ps2_host_recv_response();
56 print("ps2_mouse_init: read DevID: ");
57 phex(rcv); phex(ps2_error); print("\n");
58
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");
63
64 return 0;
65 }
66
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)
72 {
73 enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT };
74 static uint8_t scroll_state = SCROLL_NONE;
75 static uint8_t buttons_prev = 0;
76
77 /* receives packet from mouse */
78 uint8_t rcv;
79 rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
80 if (rcv == PS2_ACK) {
81 mouse_report.buttons = ps2_host_recv_response();
82 mouse_report.x = ps2_host_recv_response();
83 mouse_report.y = ps2_host_recv_response();
84 } else {
85 if (debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
86 return;
87 }
88 xprintf("%ud ", timer_read());
89 print("ps2_mouse raw: [");
90 phex(mouse_report.buttons); print("|");
91 print_hex8((uint8_t)mouse_report.x); print(" ");
92 print_hex8((uint8_t)mouse_report.y); print("]\n");
93
94 /* if mouse moves or buttons state changes */
95 if (mouse_report.x || mouse_report.y ||
96 ((mouse_report.buttons ^ buttons_prev) & PS2_MOUSE_BTN_MASK)) {
97
98 #ifdef PS2_MOUSE_DEBUG
99 print("ps2_mouse raw: [");
100 phex(mouse_report.buttons); print("|");
101 print_hex8((uint8_t)mouse_report.x); print(" ");
102 print_hex8((uint8_t)mouse_report.y); print("]\n");
103 #endif
104
105 buttons_prev = mouse_report.buttons;
106
107 // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
108 // bit: 8 7 ... 0
109 // sign \8-bit/
110 //
111 // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
112 //
113 // This converts PS/2 data into HID value. Use only -127-127 out of PS/2 9-bit.
114 mouse_report.x = X_IS_NEG ?
115 ((!X_IS_OVF && -127 <= mouse_report.x && mouse_report.x <= -1) ? mouse_report.x : -127) :
116 ((!X_IS_OVF && 0 <= mouse_report.x && mouse_report.x <= 127) ? mouse_report.x : 127);
117 mouse_report.y = Y_IS_NEG ?
118 ((!Y_IS_OVF && -127 <= mouse_report.y && mouse_report.y <= -1) ? mouse_report.y : -127) :
119 ((!Y_IS_OVF && 0 <= mouse_report.y && mouse_report.y <= 127) ? mouse_report.y : 127);
120
121 // remove sign and overflow flags
122 mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
123
124 // invert coordinate of y to conform to USB HID mouse
125 mouse_report.y = -mouse_report.y;
126
127
128 #if PS2_MOUSE_SCROLL_BTN_MASK
129 static uint16_t scroll_button_time = 0;
130 if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == (PS2_MOUSE_SCROLL_BTN_MASK)) {
131 if (scroll_state == SCROLL_NONE) {
132 scroll_button_time = timer_read();
133 scroll_state = SCROLL_BTN;
134 }
135
136 // doesn't send Scroll Button
137 //mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
138
139 if (mouse_report.x || mouse_report.y) {
140 scroll_state = SCROLL_SENT;
141
142 mouse_report.v = -mouse_report.y/(PS2_MOUSE_SCROLL_DIVISOR_V);
143 mouse_report.h = mouse_report.x/(PS2_MOUSE_SCROLL_DIVISOR_H);
144 mouse_report.x = 0;
145 mouse_report.y = 0;
146 //host_mouse_send(&mouse_report);
147 }
148 }
149 else if ((mouse_report.buttons & (PS2_MOUSE_SCROLL_BTN_MASK)) == 0) {
150 #if PS2_MOUSE_SCROLL_BTN_SEND
151 if (scroll_state == SCROLL_BTN &&
152 TIMER_DIFF_16(timer_read(), scroll_button_time) < PS2_MOUSE_SCROLL_BTN_SEND) {
153 // send Scroll Button(down and up at once) when not scrolled
154 mouse_report.buttons |= (PS2_MOUSE_SCROLL_BTN_MASK);
155 host_mouse_send(&mouse_report);
156 _delay_ms(100);
157 mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
158 }
159 #endif
160 scroll_state = SCROLL_NONE;
161 }
162 // doesn't send Scroll Button
163 mouse_report.buttons &= ~(PS2_MOUSE_SCROLL_BTN_MASK);
164 #endif
165
166
167 host_mouse_send(&mouse_report);
168 print_usb_data();
169 }
170 // clear report
171 mouse_report.x = 0;
172 mouse_report.y = 0;
173 mouse_report.v = 0;
174 mouse_report.h = 0;
175 mouse_report.buttons = 0;
176 }
177
178 static void print_usb_data(void)
179 {
180 if (!debug_mouse) return;
181 print("ps2_mouse usb: [");
182 phex(mouse_report.buttons); print("|");
183 print_hex8((uint8_t)mouse_report.x); print(" ");
184 print_hex8((uint8_t)mouse_report.y); print(" ");
185 print_hex8((uint8_t)mouse_report.v); print(" ");
186 print_hex8((uint8_t)mouse_report.h); print("]\n");
187 }
188
189
190 /* PS/2 Mouse Synopsis
191 * http://www.computer-engineering.org/ps2mouse/
192 *
193 * Command:
194 * 0xFF: Reset
195 * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
196 * 0xF5: Disable Data Reporting
197 * 0xF4: Enable Data Reporting
198 * 0xF3: Set Sample Rate
199 * 0xF2: Get Device ID
200 * 0xF0: Set Remote Mode
201 * 0xEB: Read Data
202 * 0xEA: Set Stream Mode
203 * 0xE9: Status Request
204 * 0xE8: Set Resolution
205 * 0xE7: Set Scaling 2:1
206 * 0xE6: Set Scaling 1:1
207 *
208 * Mode:
209 * Stream Mode: devices sends the data when it changs its state
210 * Remote Mode: host polls the data periodically
211 *
212 * This code uses Remote Mode and polls the data with Read Data(0xEB).
213 *
214 * Data format:
215 * byte|7 6 5 4 3 2 1 0
216 * ----+--------------------------------------------------------------
217 * 0|Yovflw Xovflw Ysign Xsign 1 Middle Right Left
218 * 1| X movement
219 * 2| Y movement
220 */
Imprint / Impressum