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