]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_mouse.c
ff730196c6b0f480315b61653cde969a650bbbc2
[tmk_firmware.git] / 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
26 #define PS2_MOUSE_DEBUG
27 #ifdef PS2_MOUSE_DEBUG
28 #   include "print.h"
29 #   include "debug.h"
30 #else
31 #   define print(s)
32 #   define phex(h)
33 #   define phex16(h)
34 #endif
35
36
37 static report_mouse_t mouse_report = {};
38
39
40 static void ps2_mouse_print_raw_data(void);
41 static void ps2_mouse_print_usb_data(void);
42
43
44 /* supports only 3 button mouse at this time */
45 uint8_t ps2_mouse_init(void) {
46     uint8_t rcv;
47
48     ps2_host_init();
49
50     _delay_ms(1000);    // wait for powering up
51
52     // send Reset
53     rcv = ps2_host_send(0xFF);
54     print("ps2_mouse_init: send Reset: ");
55     phex(rcv); phex(ps2_error); print("\n");
56
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");
61
62     // read Device ID
63     rcv = ps2_host_recv();
64     print("ps2_mouse_init: read DevID: ");
65     phex(rcv); phex(ps2_error); print("\n");
66
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");
71
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");
76
77     return 0;
78 }
79
80 /* scroll support 
81  * TODO: should be build option
82  */
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)
89 {
90     enum { SCROLL_NONE, SCROLL_BTN, SCROLL_SENT };
91     static uint8_t scroll_state = SCROLL_NONE;
92     static uint8_t buttons_prev = 0;
93
94     /* receives packet from mouse */
95     uint8_t rcv;
96     rcv = ps2_host_send(PS2_MOUSE_READ_DATA);
97     if (rcv == PS2_ACK) {
98         mouse_report.buttons = ps2_host_recv_response();
99         mouse_report.x = ps2_host_recv_response();
100         mouse_report.y = ps2_host_recv_response();
101     } else {
102         if (!debug_mouse) print("ps2_mouse: fail to get mouse packet\n");
103         return;
104     }
105
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)) {
109
110         ps2_mouse_print_raw_data();
111
112         buttons_prev = mouse_report.buttons;
113
114         // PS/2 mouse data is '9-bit integer'(-256 to 255) which is comprised of sign-bit and 8-bit value.
115         // bit: 8    7 ... 0
116         //      sign \8-bit/
117         //
118         // Meanwhile USB HID mouse indicates 8bit data(-127 to 127), note that -128 is not used.
119         //
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);
127
128         // remove sign and overflow flags
129         mouse_report.buttons &= PS2_MOUSE_BTN_MASK;
130
131         // invert coordinate of y to conform to USB HID mouse
132         mouse_report.y = -mouse_report.y;
133
134
135         if ((mouse_report.buttons & PS2_MOUSE_SCROLL_BUTTON) == PS2_MOUSE_SCROLL_BUTTON) {
136             if (scroll_state == SCROLL_NONE) scroll_state = SCROLL_BTN;
137
138             // doesn't send Scroll Button
139             mouse_report.buttons &= ~PS2_MOUSE_SCROLL_BUTTON;
140
141             if (mouse_report.x || mouse_report.y) {
142                 scroll_state = SCROLL_SENT;
143
144                 mouse_report.v = -mouse_report.y/2;
145                 mouse_report.h =  mouse_report.x/2;
146                 mouse_report.x = 0;
147                 mouse_report.y = 0;
148                 host_mouse_send(&mouse_report);
149             }
150         } else if (scroll_state == SCROLL_BTN &&
151                 (mouse_report.buttons & PS2_MOUSE_SCROLL_BUTTON) == 0) {
152             scroll_state = SCROLL_NONE;
153
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);
157             _delay_ms(100);
158             mouse_report.buttons &= ~PS2_MOUSE_SCROLL_BUTTON;
159             host_mouse_send(&mouse_report);
160         } else { 
161             scroll_state = SCROLL_NONE;
162
163             host_mouse_send(&mouse_report);
164         }
165         ps2_mouse_print_usb_data();
166     }
167     // clear report
168     mouse_report.x = 0;
169     mouse_report.y = 0;
170     mouse_report.v = 0;
171     mouse_report.h = 0;
172     mouse_report.buttons = 0;
173 }
174
175 static void ps2_mouse_print_raw_data(void)
176 {
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");
182 }
183
184 static void ps2_mouse_print_usb_data(void)
185 {
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");
193 }
194
195
196 /* PS/2 Mouse Synopsis
197  * http://www.computer-engineering.org/ps2mouse/
198  *
199  * Command:
200  * 0xFF: Reset
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
207  * 0xEB: Read Data
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
213  *
214  * Mode:
215  * Stream Mode: devices sends the data when it changs its state
216  * Remote Mode: host polls the data periodically
217  *
218  * This code uses Remote Mode and polls the data with Read Data(0xEB).
219  *
220  * Data format:
221  * byte|7       6       5       4       3       2       1       0
222  * ----+--------------------------------------------------------------
223  *    0|Yovflw  Xovflw  Ysign   Xsign   1       Middle  Right   Left
224  *    1|                    X movement
225  *    2|                    Y movement
226  */