]> git.donarmstrong.com Git - tmk_firmware.git/blob - protocol/ps2_mouse.c
Fix PS/2 mouse support
[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 "usb_mouse.h"
24
25 #define PS2_MOUSE_DEBUG
26 #ifdef PS2_MOUSE_DEBUG
27 #   include "print.h"
28 #   include "debug.h"
29 #else
30 #   define print(s)
31 #   define phex(h)
32 #   define phex16(h)
33 #endif
34
35
36 bool ps2_mouse_enable = true;
37 uint8_t ps2_mouse_x = 0;
38 uint8_t ps2_mouse_y = 0;
39 uint8_t ps2_mouse_btn = 0;
40 uint8_t ps2_mouse_error_count = 0;
41
42 static uint8_t ps2_mouse_btn_prev = 0;
43
44
45 uint8_t ps2_mouse_init(void) {
46     uint8_t rcv;
47
48     if (!ps2_mouse_enable) return 1;
49
50     ps2_host_init();
51
52     // Not reliable: sometime fail to initialize mouse
53
54     // send Reset
55     _delay_ms(1000);    // wait for powering up
56     rcv = ps2_host_send(0xFF);
57     print("ps2_mouse_init: send Reset: ");
58     phex(rcv); phex(ps2_error); print("\n");
59
60     // read completion code of BAT
61     //_delay_ms(1000);    // wait for Basic Assurance Test
62     rcv = ps2_host_recv();
63     print("ps2_mouse_init: read BAT: ");
64     phex(rcv); phex(ps2_error); print("\n");
65
66     // read Device ID
67     rcv = ps2_host_recv();
68     print("ps2_mouse_init: read DevID: ");
69     phex(rcv); phex(ps2_error); print("\n");
70
71     // send Enable Data Reporting
72     rcv = ps2_host_send(0xF4);
73     print("ps2_mouse_init: send 0xF4: ");
74     phex(rcv); phex(ps2_error); print("\n");
75
76     // send Set Remote mode
77     rcv = ps2_host_send(0xF0);
78     print("ps2_mouse_init: send 0xF0: ");
79     phex(rcv); phex(ps2_error); print("\n");
80
81     return 0;
82 }
83
84 uint8_t ps2_mouse_read(void)
85 {
86     uint8_t rcv;
87
88     if (!ps2_mouse_enable) return 1;
89
90     rcv = ps2_host_send(0xEB);
91
92     if(rcv==0xFA) {
93         ps2_mouse_btn = ps2_host_recv_response();
94         ps2_mouse_x = ps2_host_recv_response();
95         ps2_mouse_y = ps2_host_recv_response();
96     }
97     return 0;
98 }
99
100 bool ps2_mouse_changed(void)
101 {
102     return (ps2_mouse_x || ps2_mouse_y || (ps2_mouse_btn & PS2_MOUSE_BTN_MASK) != ps2_mouse_btn_prev);
103 }
104
105 #define PS2_MOUSE_SCROLL_BUTTON 0x04
106 void ps2_mouse_usb_send(void)
107 {
108     static bool scrolled = false;
109
110     if (!ps2_mouse_enable) return;
111
112     if (ps2_mouse_changed()) {
113         int8_t x, y, v, h;
114         x = y = v = h = 0;
115
116         // convert scale of X, Y: PS/2(-256/255) -> USB(-127/127)
117         if (ps2_mouse_btn & (1<<PS2_MOUSE_X_SIGN))
118             x = ps2_mouse_x > 128 ? (int8_t)ps2_mouse_x : -127;
119         else
120             x = ps2_mouse_x < 128 ? (int8_t)ps2_mouse_x : 127;
121
122         if (ps2_mouse_btn & (1<<PS2_MOUSE_Y_SIGN))
123             y = ps2_mouse_y > 128 ? (int8_t)ps2_mouse_y : -127;
124         else
125             y = ps2_mouse_y < 128 ? (int8_t)ps2_mouse_y : 127;
126
127         // Y is needed to reverse
128         y = -y;
129
130         if (ps2_mouse_btn & PS2_MOUSE_SCROLL_BUTTON) {
131             // scroll
132             if (x > 0 || x < 0) h = (x > 64 ? 64 : (x < -64 ? -64 :x));
133             if (y > 0 || y < 0) v = (y > 64 ? 64 : (y < -64 ? -64 :y));
134             if (h || v) {
135                 scrolled = true;
136                 usb_mouse_send(0,0, -v/16, h/16, 0);
137                 _delay_ms(100);
138             }
139         } else if (!scrolled && (ps2_mouse_btn_prev & PS2_MOUSE_SCROLL_BUTTON)) {
140             usb_mouse_send(0,0,0,0, PS2_MOUSE_SCROLL_BUTTON);
141             _delay_ms(100);
142             usb_mouse_send(0,0,0,0, 0);
143         } else { 
144             scrolled = false;
145             usb_mouse_send(x, y, 0, 0, ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
146         }
147
148         ps2_mouse_btn_prev = (ps2_mouse_btn & PS2_MOUSE_BTN_MASK);
149         ps2_mouse_print();
150     }
151     ps2_mouse_x = 0;
152     ps2_mouse_y = 0;
153     ps2_mouse_btn = 0;
154 }
155
156 void ps2_mouse_print(void)
157 {
158     if (!debug_mouse) return;
159     print("ps2_mouse[btn|x y]: ");
160     phex(ps2_mouse_btn); print("|");
161     phex(ps2_mouse_x); print(" ");
162     phex(ps2_mouse_y); print("\n");
163 }
164
165
166 /* PS/2 Mouse Synopsis
167  * http://www.computer-engineering.org/ps2mouse/
168  *
169  * Command:
170  * 0xFF: Reset
171  * 0xF6: Set Defaults Sampling; rate=100, resolution=4cnt/mm, scaling=1:1, reporting=disabled
172  * 0xF5: Disable Data Reporting
173  * 0xF4: Enable Data Reporting
174  * 0xF3: Set Sample Rate
175  * 0xF2: Get Device ID
176  * 0xF0: Set Remote Mode
177  * 0xEB: Read Data
178  * 0xEA: Set Stream Mode
179  * 0xE9: Status Request
180  * 0xE8: Set Resolution
181  * 0xE7: Set Scaling 2:1
182  * 0xE6: Set Scaling 1:1
183  *
184  * Mode:
185  * Stream Mode: devices sends the data when it changs its state
186  * Remote Mode: host polls the data periodically
187  *
188  * This code uses Remote Mode and polls the data with Read Data(0xEB).
189  *
190  * Data format:
191  * byte|7       6       5       4       3       2       1       0
192  * ----+--------------------------------------------------------------
193  *    0|Yovflw  Xovflw  Ysign   Xsign   1       Middle  Right   Left
194  *    1|                    X movement(0-255)
195  *    2|                    Y movement(0-255)
196  */