Introduce standard warnings for allocation failures and timeouts.
[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 void
140 process_ps2byte(u8 status, u8 data)
141 {
142     if (!MODE16) {
143         // Don't pull in all of keyboard/mouse code into 32bit code -
144         // just discard the data.
145         dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
146         return;
147     }
148     if (status & I8042_STR_AUXDATA)
149         process_mouse(data);
150     else
151         process_key(data);
152 }
153
154 static void
155 process_ps2bytes(void)
156 {
157     for (;;) {
158         u8 status = inb(PORT_PS2_STATUS);
159         if (!(status & I8042_STR_OBF))
160             return;
161         u8 data = inb(PORT_PS2_DATA);
162         process_ps2byte(status, data);
163     }
164 }
165
166 static int
167 ps2_recvbyte(int aux, int needack, int timeout)
168 {
169     u64 end = calc_future_tsc(timeout);
170     for (;;) {
171         u8 status = inb(PORT_PS2_STATUS);
172         if (status & I8042_STR_OBF) {
173             u8 data = inb(PORT_PS2_DATA);
174             dprintf(7, "ps2 read %x\n", data);
175
176             if (!!(status & I8042_STR_AUXDATA) == aux) {
177                 if (!needack)
178                     return data;
179                 if (data == PS2_RET_ACK)
180                     return data;
181                 if (data == PS2_RET_NAK) {
182                     dprintf(1, "Got ps2 nak (status=%x)\n", status);
183                     return data;
184                 }
185             }
186
187             // Data not part of this command.
188             process_ps2byte(status, data);
189         }
190
191         if (check_time(end)) {
192             // Don't warn on second byte of a reset
193             if (timeout > 100)
194                 warn_timeout();
195             return -1;
196         }
197         yield();
198     }
199 }
200
201 static int
202 ps2_sendbyte(int aux, u8 command, int timeout)
203 {
204     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
205     int ret;
206     if (aux)
207         ret = i8042_aux_write(command);
208     else
209         ret = i8042_kbd_write(command);
210     if (ret)
211         return ret;
212
213     // Read ack.
214     ret = ps2_recvbyte(aux, 1, timeout);
215     if (ret < 0)
216         return ret;
217     if (ret != PS2_RET_ACK)
218         return -1;
219
220     return 0;
221 }
222
223 static int
224 ps2_command(int aux, int command, u8 *param)
225 {
226     int ret;
227     int receive = (command >> 8) & 0xf;
228     int send = (command >> 12) & 0xf;
229
230     // Disable processing of interrupts.
231     u8 kbdflag = GET_BDA(kbd_flag3);
232     SET_BDA(kbd_flag3, kbdflag | KF3_CMD_PENDING);
233
234     if (command == ATKBD_CMD_RESET_BAT) {
235         // Reset is special wrt timeouts.
236
237         // Send command.
238         ret = ps2_sendbyte(aux, command, 1000);
239         if (ret)
240             goto fail;
241
242         // Receive parameters.
243         ret = ps2_recvbyte(aux, 0, 4000);
244         if (ret < 0)
245             goto fail;
246         param[0] = ret;
247         ret = ps2_recvbyte(aux, 0, 100);
248         if (ret < 0)
249             // Some devices only respond with one byte on reset.
250             ret = 0;
251         param[1] = ret;
252     } else {
253         // Send command.
254         ret = ps2_sendbyte(aux, command, 200);
255         if (ret)
256             goto fail;
257
258         // Send parameters (if any).
259         int i;
260         for (i = 0; i < send; i++) {
261             ret = ps2_sendbyte(aux, param[i], 200);
262             if (ret)
263                 goto fail;
264         }
265
266         // Receive parameters (if any).
267         for (i = 0; i < receive; i++) {
268             ret = ps2_recvbyte(aux, 0, 500);
269             if (ret < 0)
270                 goto fail;
271             param[i] = ret;
272         }
273     }
274
275     ret = 0;
276
277 fail:
278     // Restore processing of interrupts.
279     if (!(kbdflag & KF3_CMD_PENDING))
280         process_ps2bytes();
281     SET_BDA(kbd_flag3, kbdflag);
282
283     return ret;
284 }
285
286 int
287 kbd_command(int command, u8 *param)
288 {
289     dprintf(7, "kbd_command cmd=%x\n", command);
290     int ret = ps2_command(0, command, param);
291     if (ret)
292         dprintf(2, "keyboard command %x failed\n", command);
293     return ret;
294 }
295
296 int
297 aux_command(int command, u8 *param)
298 {
299     dprintf(7, "aux_command cmd=%x\n", command);
300     int ret = ps2_command(1, command, param);
301     if (ret)
302         dprintf(2, "mouse command %x failed\n", command);
303     return ret;
304 }
305
306
307 /****************************************************************
308  * IRQ handlers
309  ****************************************************************/
310
311 static void
312 process_ps2irq(void)
313 {
314     if (GET_BDA(kbd_flag3) & KF3_CMD_PENDING)
315         // PS/2 command in progress - it will handle this event.
316         return;
317     process_ps2bytes();
318 }
319
320 // INT74h : PS/2 mouse hardware interrupt
321 void VISIBLE16
322 handle_74(void)
323 {
324     if (! CONFIG_PS2PORT)
325         return;
326
327     debug_isr(DEBUG_ISR_74);
328     process_ps2irq();
329     eoi_pic2();
330 }
331
332 // INT09h : Keyboard Hardware Service Entry Point
333 void VISIBLE16
334 handle_09(void)
335 {
336     if (! CONFIG_PS2PORT)
337         return;
338
339     debug_isr(DEBUG_ISR_09);
340     process_ps2irq();
341     eoi_pic1();
342 }
343
344
345 /****************************************************************
346  * Setup
347  ****************************************************************/
348
349 static void
350 keyboard_init(void *data)
351 {
352     /* flush incoming keys */
353     int ret = i8042_flush();
354     if (ret)
355         return;
356
357     // Controller self-test.
358     u8 param[2];
359     ret = i8042_command(I8042_CMD_CTL_TEST, param);
360     if (ret)
361         return;
362     if (param[0] != 0x55) {
363         dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
364         return;
365     }
366
367     // Controller keyboard test.
368     ret = i8042_command(I8042_CMD_KBD_TEST, param);
369     if (ret)
370         return;
371     if (param[0] != 0x00) {
372         dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
373         return;
374     }
375
376     // Enable keyboard and mouse ports.
377     ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
378     if (ret)
379         return;
380     ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
381     if (ret)
382         return;
383
384
385     /* ------------------- keyboard side ------------------------*/
386     /* reset keyboard and self test  (keyboard side) */
387     ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
388     if (ret)
389         return;
390     if (param[0] != 0xaa) {
391         dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
392         return;
393     }
394
395     /* Disable keyboard */
396     ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
397     if (ret)
398         return;
399
400     // Set scancode command (mode 2)
401     param[0] = 0x02;
402     ret = kbd_command(ATKBD_CMD_SSCANSET, param);
403     if (ret)
404         return;
405
406     // Mode: scan code convert, enable IRQ 1, enable IRQ 12
407     param[0] = I8042_CTR_XLATE | I8042_CTR_KBDINT | I8042_CTR_AUXINT;
408     ret = i8042_command(I8042_CMD_CTL_WCTR, param);
409     if (ret)
410         return;
411     CLEARBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
412
413     /* Enable keyboard */
414     ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
415     if (ret)
416         return;
417
418     dprintf(1, "keyboard initialized\n");
419 }
420
421 void
422 ps2port_setup(void)
423 {
424     ASSERT32FLAT();
425     if (! CONFIG_PS2PORT)
426         return;
427     dprintf(3, "init ps2port\n");
428
429     // Setup irqs, but disable them until init complete.
430     SETBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
431     enable_hwirq(1, entry_09);
432     enable_hwirq(12, entry_74);
433
434     run_thread(keyboard_init, NULL);
435 }