Change license from GPLv3 to LGPLv3.
[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\n", data);
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\n", data);
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     ret = ps2_recvbyte(aux, 1, 200);
191     if (ret < 0)
192         return ret;
193
194     return 0;
195 }
196
197 static int
198 ps2_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++) {
225         ret = ps2_sendbyte(aux, param[i]);
226         if (ret)
227             goto fail;
228     }
229
230     // Receive parameters (if any).
231     for (i = 0; i < receive; i++) {
232         u8 data = ps2_recvbyte(aux, 0, 200);
233         if (data < 0) {
234             // On a receive timeout, return the item number that the
235             // transfer failed on.
236             ret = i + 1;
237             goto fail;
238         }
239         param[i] = data;
240     }
241
242 fail:
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
251 int
252 kbd_command(int command, u8 *param)
253 {
254     dprintf(7, "kbd_command cmd=%x\n", command);
255     int ret = ps2_command(0, command, param);
256     if (ret)
257         dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
258     return ret;
259 }
260
261 int
262 aux_command(int command, u8 *param)
263 {
264     dprintf(7, "aux_command cmd=%x\n", command);
265     int ret = ps2_command(1, command, param);
266     if (ret)
267         dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);
268     return ret;
269 }