blob: ed401bf13673320e5801489682507581effaacdf [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>
4// Based on code Copyright (c) 1999-2004 Vojtech Pavlik
5//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
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);
68 dprintf(7, "i8042 flushed %x\n", data);
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 (;;) {
155 if (rdtscll() >= end) {
156 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
166 if ((!!(status & I8042_STR_AUXDATA) != aux)
167 || (needack && data != PS2_RET_ACK)) {
168 // This data not for us - XXX - just discard it for now.
169 dprintf(1, "Discarding ps2 data %x\n", data);
170 continue;
171 }
172
173 return data;
174 }
175}
176
177static int
Kevin O'Connor3b897192008-07-20 10:08:59 -0400178ps2_sendbyte(int aux, u8 command)
179{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500180 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400181 int ret;
182 if (aux)
183 ret = i8042_aux_write(command);
184 else
185 ret = i8042_kbd_write(command);
186 if (ret)
187 return ret;
188
189 // Read ack.
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500190 ret = ps2_recvbyte(aux, 1, 200);
191 if (ret < 0)
Kevin O'Connor3b897192008-07-20 10:08:59 -0400192 return ret;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400193
194 return 0;
195}
196
197static int
198ps2_command(int aux, int command, u8 *param)
199{
200 int ret2;
201 int receive = (command >> 8) & 0xf;
202 int send = (command >> 12) & 0xf;
203
204 // Disable interrupts and keyboard/mouse.
205 u8 ps2ctr = GET_EBDA(ps2ctr);
206 u8 newctr = ps2ctr;
207 if (aux)
208 newctr |= I8042_CTR_KBDDIS;
209 else
210 newctr |= I8042_CTR_AUXDIS;
211 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
212 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
213 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
214 if (ret)
215 return ret;
216
217 // Send command.
218 ret = ps2_sendbyte(aux, command);
219 if (ret)
220 goto fail;
221
222 // Send parameters (if any).
223 int i;
224 for (i = 0; i < send; i++) {
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500225 ret = ps2_sendbyte(aux, param[i]);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400226 if (ret)
227 goto fail;
228 }
229
230 // Receive parameters (if any).
231 for (i = 0; i < receive; i++) {
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500232 u8 data = ps2_recvbyte(aux, 0, 200);
233 if (data < 0) {
Kevin O'Connor7481a302008-07-21 23:42:34 -0400234 // On a receive timeout, return the item number that the
235 // transfer failed on.
236 ret = i + 1;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400237 goto fail;
Kevin O'Connor7481a302008-07-21 23:42:34 -0400238 }
Kevin O'Connor0234cd92009-01-04 12:20:02 -0500239 param[i] = data;
Kevin O'Connor3b897192008-07-20 10:08:59 -0400240 }
241
242fail:
243 // Restore interrupts and keyboard/mouse.
244 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
245 if (ret2)
246 return ret2;
247
248 return ret;
249}
250
251int
252kbd_command(int command, u8 *param)
253{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500254 dprintf(7, "kbd_command cmd=%x\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400255 int ret = ps2_command(0, command, param);
256 if (ret)
Kevin O'Connor7481a302008-07-21 23:42:34 -0400257 dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400258 return ret;
259}
260
261int
262aux_command(int command, u8 *param)
263{
Kevin O'Connordd3588f2008-11-29 12:41:48 -0500264 dprintf(7, "aux_command cmd=%x\n", command);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400265 int ret = ps2_command(1, command, param);
266 if (ret)
Kevin O'Connor7481a302008-07-21 23:42:34 -0400267 dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);
Kevin O'Connor3b897192008-07-20 10:08:59 -0400268 return ret;
269}