Cleanup keyboard reset handling.
[seabios.git] / src / ps2port.c
1 // Support for handling the PS/2 mouse/keyboard ports.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 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
23 static int
24 i8042_wait_read(void)
25 {
26     dprintf(7, "i8042_wait_read\n");
27     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
38 static int
39 i8042_wait_write(void)
40 {
41     dprintf(7, "i8042_wait_write\n");
42     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
53 int
54 i8042_flush(void)
55 {
56     dprintf(7, "i8042_flush\n");
57     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);
67         u8 data = inb(PORT_PS2_DATA);
68         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
69     }
70
71     irq_restore(flags);
72     dprintf(1, "i8042 timeout on flush\n");
73     return -1;
74 }
75
76 static 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);
103         dprintf(7, "i8042 param=%x\n", param[i]);
104     }
105
106     return 0;
107 }
108
109 int
110 i8042_command(int command, u8 *param)
111 {
112     dprintf(7, "i8042_command cmd=%x\n", command);
113     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
121 static int
122 i8042_kbd_write(u8 c)
123 {
124     dprintf(7, "i8042_kbd_write c=%d\n", c);
125     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
136 static int
137 i8042_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
150 static int
151 ps2_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 (status=%x)\n", data, status);
170             continue;
171         }
172
173         return data;
174     }
175 }
176
177 static int
178 ps2_sendbyte(int aux, u8 command)
179 {
180     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
181     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.
190     int timeout = command == ATKBD_CMD_RESET_BAT ? 1000 : 200;
191     ret = ps2_recvbyte(aux, 1, timeout);
192     if (ret < 0)
193         return ret;
194
195     return 0;
196 }
197
198 static int
199 ps2_command(int aux, int command, u8 *param)
200 {
201     int ret2;
202     int receive = (command >> 8) & 0xf;
203     int send = (command >> 12) & 0xf;
204
205     // Disable interrupts and keyboard/mouse.
206     u8 ps2ctr = GET_EBDA(ps2ctr);
207     u8 newctr = ps2ctr;
208     if (aux)
209         newctr |= I8042_CTR_KBDDIS;
210     else
211         newctr |= I8042_CTR_AUXDIS;
212     newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
213     dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
214     int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
215     if (ret)
216         return ret;
217
218     // Send command.
219     ret = ps2_sendbyte(aux, command);
220     if (ret)
221         goto fail;
222
223     // Send parameters (if any).
224     int i;
225     for (i = 0; i < send; i++) {
226         ret = ps2_sendbyte(aux, param[i]);
227         if (ret)
228             goto fail;
229     }
230
231     // Receive parameters (if any).
232     for (i = 0; i < receive; i++) {
233         int timeout = 500;
234         if (command == ATKBD_CMD_RESET_BAT) {
235             // Reset is special wrt timeouts.
236             if (i==0)
237                 timeout = 4000;
238             else
239                 timeout = 100;
240         }
241         int data = ps2_recvbyte(aux, 0, timeout);
242         if (data < 0) {
243             if (command == ATKBD_CMD_RESET_BAT && i==1) {
244                 // Some devices only respond with one byte on reset.
245                 param[i] = 0;
246                 break;
247             }
248             ret = -1;
249             goto fail;
250         }
251         param[i] = data;
252     }
253
254 fail:
255     // Restore interrupts and keyboard/mouse.
256     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
257     if (ret2)
258         return ret2;
259
260     return ret;
261 }
262
263 int
264 kbd_command(int command, u8 *param)
265 {
266     dprintf(7, "kbd_command cmd=%x\n", command);
267     int ret = ps2_command(0, command, param);
268     if (ret)
269         dprintf(2, "keyboard command %x failed\n", command);
270     return ret;
271 }
272
273 int
274 aux_command(int command, u8 *param)
275 {
276     dprintf(7, "aux_command cmd=%x\n", command);
277     int ret = ps2_command(1, command, param);
278     if (ret)
279         dprintf(2, "mouse command %x failed\n", command);
280     return ret;
281 }