blob: 25d45446808573ec3b0f8f457104d75b824ebf20 [file] [log] [blame]
Kevin O'Connor3b897192008-07-20 10:08:59 -04001// Support for handling the PS/2 mouse/keyboard ports.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05004// Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
Kevin O'Connor3b897192008-07-20 10:08:59 -04005//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor3b897192008-07-20 10:08:59 -04007
8#include "ioport.h" // inb
9#include "util.h" // dprintf
10#include "biosvar.h" // GET_EBDA
11#include "ps2port.h" // kbd_command
12
13
14/****************************************************************
15 * Low level i8042 commands.
16 ****************************************************************/
17
18// Timeout value.
19#define I8042_CTL_TIMEOUT 10000
20
21#define I8042_BUFFER_SIZE 16
22
Kevin O'Connor3b897192008-07-20 10:08:59 -040023static int
24i8042_wait_read(void)
25{
Kevin O'Connordd3588f2008-11-29 12:41:48 -050026 dprintf(7, "i8042_wait_read\n");
Kevin O'Connor3b897192008-07-20 10:08:59 -040027 int i;
28 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
29 u8 status = inb(PORT_PS2_STATUS);
30 if (status & I8042_STR_OBF)
31 return 0;
32 udelay(50);
33 }
34 dprintf(1, "i8042 timeout on wait read\n");
35 return -1;
36}
37
38static int
39i8042_wait_write(void)
40{
Kevin O'Connordd3588f2008-11-29 12:41:48 -050041 dprintf(7, "i8042_wait_write\n");
Kevin O'Connor3b897192008-07-20 10:08:59 -040042 int i;
43 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
44 u8 status = inb(PORT_PS2_STATUS);
45 if (! (status & I8042_STR_IBF))
46 return 0;
47 udelay(50);
48 }
49 dprintf(1, "i8042 timeout on wait write\n");
50 return -1;
51}
52
53int
54i8042_flush(void)
55{
Kevin O'Connordd3588f2008-11-29 12:41:48 -050056 dprintf(7, "i8042_flush\n");
Kevin O'Connor3b897192008-07-20 10:08:59 -040057 unsigned long flags = irq_save();
58
59 int i;
60 for (i=0; i<I8042_BUFFER_SIZE; i++) {
61 u8 status = inb(PORT_PS2_STATUS);
62 if (! (status & I8042_STR_OBF)) {
63 irq_restore(flags);
64 return 0;
65 }
66 udelay(50);
Kevin O'Connor0234cd92009-01-04 12:20:02 -050067 u8 data = inb(PORT_PS2_DATA);
Kevin O'Connor9e91c7b2009-01-17 02:30:21 -050068 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
Kevin O'Connor3b897192008-07-20 10:08:59 -040069 }
70
71 irq_restore(flags);
72 dprintf(1, "i8042 timeout on flush\n");
73 return -1;
74}
75
76static int
77__i8042_command(int command, u8 *param)
78{
79 int receive = (command >> 8) & 0xf;
80 int send = (command >> 12) & 0xf;
81
82 // Send the command.
83 int ret = i8042_wait_write();
84 if (ret)
85 return ret;
86 outb(command, PORT_PS2_STATUS);
87
88 // Send parameters (if any).
89 int i;
90 for (i = 0; i < send; i++) {
91 ret = i8042_wait_write();
92 if (ret)
93 return ret;
94 outb(param[i], PORT_PS2_DATA);
95 }
96
97 // Receive parameters (if any).
98 for (i = 0; i < receive; i++) {
99 ret = i8042_wait_read();
100 if (ret)
101 return ret;
102 param[i] = inb(PORT_PS2_DATA);
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500103 dprintf(7, "i8042 param=%x\n", param[i]);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400104 }
105
106 return 0;
107}
108
109int
110i8042_command(int command, u8 *param)
111{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500112 dprintf(7, "i8042_command cmd=%x\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400113 unsigned long flags = irq_save();
114 int ret = __i8042_command(command, param);
115 irq_restore(flags);
116 if (ret)
117 dprintf(2, "i8042 command %x failed\n", command);
118 return ret;
119}
120
121static int
122i8042_kbd_write(u8 c)
123{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500124 dprintf(7, "i8042_kbd_write c=%d\n", c);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400125 unsigned long flags = irq_save();
126
127 int ret = i8042_wait_write();
128 if (! ret)
129 outb(c, PORT_PS2_DATA);
130
131 irq_restore(flags);
132
133 return ret;
134}
135
136static int
137i8042_aux_write(u8 c)
138{
139 return i8042_command(I8042_CMD_AUX_SEND, &c);
140}
141
142
143/****************************************************************
144 * Device commands.
145 ****************************************************************/
146
147#define PS2_RET_ACK 0xfa
148#define PS2_RET_NAK 0xfe
149
150static int
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500151ps2_recvbyte(int aux, int needack, int timeout)
152{
153 u64 end = calc_future_tsc(timeout);
154 for (;;) {
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400155 if (check_time(end)) {
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500156 dprintf(1, "ps2_recvbyte timeout\n");
157 return -1;
158 }
159
160 u8 status = inb(PORT_PS2_STATUS);
161 if (! (status & I8042_STR_OBF))
162 continue;
163 u8 data = inb(PORT_PS2_DATA);
164 dprintf(7, "ps2 read %x\n", data);
165
Kevin O'Connor7e6dfd82009-05-06 23:18:48 -0400166 if (!!(status & I8042_STR_AUXDATA) == aux) {
167 if (!needack)
168 return data;
169 if (data == PS2_RET_ACK)
170 return data;
171 if (data == PS2_RET_NAK) {
172 dprintf(1, "Got ps2 nak (status=%x); continuing\n", status);
173 return data;
174 }
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500175 }
176
Kevin O'Connor7e6dfd82009-05-06 23:18:48 -0400177 // This data not for us - XXX - just discard it for now.
178 dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500179 }
180}
181
182static int
Kevin O'Connor235df202009-01-18 12:54:51 -0500183ps2_sendbyte(int aux, u8 command, int timeout)
Kevin O'Connor3b897192008-07-20 10:08:59 -0400184{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500185 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400186 int ret;
187 if (aux)
188 ret = i8042_aux_write(command);
189 else
190 ret = i8042_kbd_write(command);
191 if (ret)
192 return ret;
193
194 // Read ack.
Kevin O'Connorb44a8522009-01-17 23:30:01 -0500195 ret = ps2_recvbyte(aux, 1, timeout);
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500196 if (ret < 0)
Kevin O'Connor3b897192008-07-20 10:08:59 -0400197 return ret;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400198
199 return 0;
200}
201
202static int
203ps2_command(int aux, int command, u8 *param)
204{
205 int ret2;
206 int receive = (command >> 8) & 0xf;
207 int send = (command >> 12) & 0xf;
208
209 // Disable interrupts and keyboard/mouse.
210 u8 ps2ctr = GET_EBDA(ps2ctr);
211 u8 newctr = ps2ctr;
212 if (aux)
213 newctr |= I8042_CTR_KBDDIS;
214 else
215 newctr |= I8042_CTR_AUXDIS;
216 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
217 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
218 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
219 if (ret)
220 return ret;
221
Kevin O'Connor235df202009-01-18 12:54:51 -0500222 if (command == ATKBD_CMD_RESET_BAT) {
223 // Reset is special wrt timeouts.
Kevin O'Connor3b897192008-07-20 10:08:59 -0400224
Kevin O'Connor235df202009-01-18 12:54:51 -0500225 // Send command.
226 ret = ps2_sendbyte(aux, command, 1000);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400227 if (ret)
228 goto fail;
Kevin O'Connor235df202009-01-18 12:54:51 -0500229
230 // Receive parameters.
231 ret = ps2_recvbyte(aux, 0, 4000);
232 if (ret < 0)
233 goto fail;
234 param[0] = ret;
235 ret = ps2_recvbyte(aux, 0, 100);
236 if (ret < 0)
237 // Some devices only respond with one byte on reset.
238 ret = 0;
239 param[1] = ret;
240 } else {
241 // Send command.
242 ret = ps2_sendbyte(aux, command, 200);
243 if (ret)
244 goto fail;
245
246 // Send parameters (if any).
247 int i;
248 for (i = 0; i < send; i++) {
249 ret = ps2_sendbyte(aux, param[i], 200);
250 if (ret)
251 goto fail;
252 }
253
254 // Receive parameters (if any).
255 for (i = 0; i < receive; i++) {
256 ret = ps2_recvbyte(aux, 0, 500);
257 if (ret < 0)
258 goto fail;
259 param[i] = ret;
260 }
Kevin O'Connor3b897192008-07-20 10:08:59 -0400261 }
262
Kevin O'Connor235df202009-01-18 12:54:51 -0500263 ret = 0;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400264
265fail:
266 // Restore interrupts and keyboard/mouse.
267 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
268 if (ret2)
269 return ret2;
270
271 return ret;
272}
273
274int
275kbd_command(int command, u8 *param)
276{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500277 dprintf(7, "kbd_command cmd=%x\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400278 int ret = ps2_command(0, command, param);
279 if (ret)
Kevin O'Connorb44a8522009-01-17 23:30:01 -0500280 dprintf(2, "keyboard command %x failed\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400281 return ret;
282}
283
284int
285aux_command(int command, u8 *param)
286{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500287 dprintf(7, "aux_command cmd=%x\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400288 int ret = ps2_command(1, command, param);
289 if (ret)
Kevin O'Connorb44a8522009-01-17 23:30:01 -0500290 dprintf(2, "mouse command %x failed\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400291 return ret;
292}