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