Process event on ps2 keyboard irq even if event already read.
[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 #include "pic.h" // eoi_pic1
13
14
15 /****************************************************************
16  * Low level i8042 commands.
17  ****************************************************************/
18
19 // Timeout value.
20 #define I8042_CTL_TIMEOUT       10000
21
22 #define I8042_BUFFER_SIZE       16
23
24 static int
25 i8042_wait_read(void)
26 {
27     dprintf(7, "i8042_wait_read\n");
28     int i;
29     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
30         u8 status = inb(PORT_PS2_STATUS);
31         if (status & I8042_STR_OBF)
32             return 0;
33         udelay(50);
34     }
35     warn_timeout();
36     return -1;
37 }
38
39 static int
40 i8042_wait_write(void)
41 {
42     dprintf(7, "i8042_wait_write\n");
43     int i;
44     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
45         u8 status = inb(PORT_PS2_STATUS);
46         if (! (status & I8042_STR_IBF))
47             return 0;
48         udelay(50);
49     }
50     warn_timeout();
51     return -1;
52 }
53
54 int
55 i8042_flush(void)
56 {
57     dprintf(7, "i8042_flush\n");
58     int i;
59     for (i=0; i<I8042_BUFFER_SIZE; i++) {
60         u8 status = inb(PORT_PS2_STATUS);
61         if (! (status & I8042_STR_OBF))
62             return 0;
63         udelay(50);
64         u8 data = inb(PORT_PS2_DATA);
65         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
66     }
67
68     warn_timeout();
69     return -1;
70 }
71
72 static int
73 __i8042_command(int command, u8 *param)
74 {
75     int receive = (command >> 8) & 0xf;
76     int send = (command >> 12) & 0xf;
77
78     // Send the command.
79     int ret = i8042_wait_write();
80     if (ret)
81         return ret;
82     outb(command, PORT_PS2_STATUS);
83
84     // Send parameters (if any).
85     int i;
86     for (i = 0; i < send; i++) {
87         ret = i8042_wait_write();
88         if (ret)
89             return ret;
90         outb(param[i], PORT_PS2_DATA);
91     }
92
93     // Receive parameters (if any).
94     for (i = 0; i < receive; i++) {
95         ret = i8042_wait_read();
96         if (ret)
97             return ret;
98         param[i] = inb(PORT_PS2_DATA);
99         dprintf(7, "i8042 param=%x\n", param[i]);
100     }
101
102     return 0;
103 }
104
105 int
106 i8042_command(int command, u8 *param)
107 {
108     dprintf(7, "i8042_command cmd=%x\n", command);
109     int ret = __i8042_command(command, param);
110     if (ret)
111         dprintf(2, "i8042 command %x failed\n", command);
112     return ret;
113 }
114
115 static int
116 i8042_kbd_write(u8 c)
117 {
118     dprintf(7, "i8042_kbd_write c=%d\n", c);
119     int ret = i8042_wait_write();
120     if (! ret)
121         outb(c, PORT_PS2_DATA);
122     return ret;
123 }
124
125 static int
126 i8042_aux_write(u8 c)
127 {
128     return i8042_command(I8042_CMD_AUX_SEND, &c);
129 }
130
131
132 /****************************************************************
133  * Device commands.
134  ****************************************************************/
135
136 #define PS2_RET_ACK             0xfa
137 #define PS2_RET_NAK             0xfe
138
139 static int
140 ps2_recvbyte(int aux, int needack, int timeout)
141 {
142     u64 end = calc_future_tsc(timeout);
143     for (;;) {
144         u8 status = inb(PORT_PS2_STATUS);
145         if (status & I8042_STR_OBF) {
146             u8 data = inb(PORT_PS2_DATA);
147             dprintf(7, "ps2 read %x\n", data);
148
149             if (!!(status & I8042_STR_AUXDATA) == aux) {
150                 if (!needack)
151                     return data;
152                 if (data == PS2_RET_ACK)
153                     return data;
154                 if (data == PS2_RET_NAK) {
155                     dprintf(1, "Got ps2 nak (status=%x)\n", status);
156                     return data;
157                 }
158             }
159
160             // This data not part of command - just discard it.
161             dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
162         }
163
164         if (check_time(end)) {
165             // Don't warn on second byte of a reset
166             if (timeout > 100)
167                 warn_timeout();
168             return -1;
169         }
170         yield();
171     }
172 }
173
174 static int
175 ps2_sendbyte(int aux, u8 command, int timeout)
176 {
177     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
178     int ret;
179     if (aux)
180         ret = i8042_aux_write(command);
181     else
182         ret = i8042_kbd_write(command);
183     if (ret)
184         return ret;
185
186     // Read ack.
187     ret = ps2_recvbyte(aux, 1, timeout);
188     if (ret < 0)
189         return ret;
190     if (ret != PS2_RET_ACK)
191         return -1;
192
193     return 0;
194 }
195
196 static int
197 ps2_command(int aux, int command, u8 *param)
198 {
199     int ret2;
200     int receive = (command >> 8) & 0xf;
201     int send = (command >> 12) & 0xf;
202
203     // Disable interrupts and keyboard/mouse.
204     u8 ps2ctr = GET_EBDA(ps2ctr);
205     u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS)
206                  & ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT));
207     dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
208     int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
209     if (ret)
210         return ret;
211
212     // Flush any interrupts already pending.
213     yield();
214
215     // Enable port command is being sent to.
216     if (aux)
217         newctr &= ~I8042_CTR_AUXDIS;
218     else
219         newctr &= ~I8042_CTR_KBDDIS;
220     ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
221     if (ret)
222         goto fail;
223
224     if (command == ATKBD_CMD_RESET_BAT) {
225         // Reset is special wrt timeouts and bytes received.
226
227         // Send command.
228         ret = ps2_sendbyte(aux, command, 1000);
229         if (ret)
230             goto fail;
231
232         // Receive parameters.
233         ret = ps2_recvbyte(aux, 0, 4000);
234         if (ret < 0)
235             goto fail;
236         param[0] = ret;
237         ret = ps2_recvbyte(aux, 0, 100);
238         if (ret < 0)
239             // Some devices only respond with one byte on reset.
240             ret = 0;
241         param[1] = ret;
242     } else if (command == ATKBD_CMD_GETID) {
243         // Getid is special wrt bytes received.
244
245         // Send command.
246         ret = ps2_sendbyte(aux, command, 200);
247         if (ret)
248             goto fail;
249
250         // Receive parameters.
251         ret = ps2_recvbyte(aux, 0, 500);
252         if (ret < 0)
253             goto fail;
254         param[0] = ret;
255         if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
256             || ret == 0x60 || ret == 0x47) {
257             // These ids (keyboards) return two bytes.
258             ret = ps2_recvbyte(aux, 0, 500);
259             if (ret < 0)
260                 goto fail;
261             param[1] = ret;
262         } else {
263             param[1] = 0;
264         }
265     } else {
266         // Send command.
267         ret = ps2_sendbyte(aux, command, 200);
268         if (ret)
269             goto fail;
270
271         // Send parameters (if any).
272         int i;
273         for (i = 0; i < send; i++) {
274             ret = ps2_sendbyte(aux, param[i], 200);
275             if (ret)
276                 goto fail;
277         }
278
279         // Receive parameters (if any).
280         for (i = 0; i < receive; i++) {
281             ret = ps2_recvbyte(aux, 0, 500);
282             if (ret < 0)
283                 goto fail;
284             param[i] = ret;
285         }
286     }
287
288     ret = 0;
289
290 fail:
291     // Restore interrupts and keyboard/mouse.
292     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
293     if (ret2)
294         return ret2;
295
296     return ret;
297 }
298
299 int
300 kbd_command(int command, u8 *param)
301 {
302     dprintf(7, "kbd_command cmd=%x\n", command);
303     int ret = ps2_command(0, command, param);
304     if (ret)
305         dprintf(2, "keyboard command %x failed\n", command);
306     return ret;
307 }
308
309 int
310 aux_command(int command, u8 *param)
311 {
312     dprintf(7, "aux_command cmd=%x\n", command);
313     int ret = ps2_command(1, command, param);
314     if (ret)
315         dprintf(2, "mouse command %x failed\n", command);
316     return ret;
317 }
318
319
320 /****************************************************************
321  * IRQ handlers
322  ****************************************************************/
323
324 // INT74h : PS/2 mouse hardware interrupt
325 void VISIBLE16
326 handle_74(void)
327 {
328     if (! CONFIG_PS2PORT)
329         return;
330
331     debug_isr(DEBUG_ISR_74);
332
333     u8 v = inb(PORT_PS2_STATUS);
334     if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
335         != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
336         dprintf(1, "ps2 mouse irq but no mouse data.\n");
337         goto done;
338     }
339     v = inb(PORT_PS2_DATA);
340
341     if (!(GET_EBDA(ps2ctr) & I8042_CTR_AUXINT))
342         // Interrupts not enabled.
343         goto done;
344
345     process_mouse(v);
346
347 done:
348     eoi_pic2();
349 }
350
351 // INT09h : Keyboard Hardware Service Entry Point
352 void VISIBLE16
353 handle_09(void)
354 {
355     if (! CONFIG_PS2PORT)
356         return;
357
358     debug_isr(DEBUG_ISR_09);
359
360     // read key from keyboard controller
361     u8 v = inb(PORT_PS2_STATUS);
362     if (v & I8042_STR_AUXDATA) {
363         dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
364         goto done;
365     }
366     v = inb(PORT_PS2_DATA);
367
368     if (!(GET_EBDA(ps2ctr) & I8042_CTR_KBDINT))
369         // Interrupts not enabled.
370         goto done;
371
372     process_key(v);
373
374 done:
375     eoi_pic1();
376 }
377
378
379 /****************************************************************
380  * Setup
381  ****************************************************************/
382
383 static void
384 keyboard_init(void *data)
385 {
386     /* flush incoming keys */
387     int ret = i8042_flush();
388     if (ret)
389         return;
390
391     // Controller self-test.
392     u8 param[2];
393     ret = i8042_command(I8042_CMD_CTL_TEST, param);
394     if (ret)
395         return;
396     if (param[0] != 0x55) {
397         dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
398         return;
399     }
400
401     // Controller keyboard test.
402     ret = i8042_command(I8042_CMD_KBD_TEST, param);
403     if (ret)
404         return;
405     if (param[0] != 0x00) {
406         dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
407         return;
408     }
409
410     // Disable keyboard and mouse events.
411     SET_EBDA(ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
412
413
414     /* ------------------- keyboard side ------------------------*/
415     /* reset keyboard and self test  (keyboard side) */
416     ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
417     if (ret)
418         return;
419     if (param[0] != 0xaa) {
420         dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
421         return;
422     }
423
424     /* Disable keyboard */
425     ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
426     if (ret)
427         return;
428
429     // Set scancode command (mode 2)
430     param[0] = 0x02;
431     ret = kbd_command(ATKBD_CMD_SSCANSET, param);
432     if (ret)
433         return;
434
435     // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
436     SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
437
438     /* Enable keyboard */
439     ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
440     if (ret)
441         return;
442
443     dprintf(1, "PS2 keyboard initialized\n");
444 }
445
446 void
447 ps2port_setup(void)
448 {
449     ASSERT32FLAT();
450     if (! CONFIG_PS2PORT)
451         return;
452     dprintf(3, "init ps2port\n");
453
454     enable_hwirq(1, entry_09);
455     enable_hwirq(12, entry_74);
456
457     run_thread(keyboard_init, NULL);
458 }