1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Support for handling the PS/2 mouse/keyboard ports.
//
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
// Based on code Copyright (c) 1999-2004 Vojtech Pavlik
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include "ioport.h" // inb
#include "util.h" // dprintf
#include "biosvar.h" // GET_EBDA
#include "ps2port.h" // kbd_command
/****************************************************************
* Low level i8042 commands.
****************************************************************/
// Timeout value.
#define I8042_CTL_TIMEOUT 10000
#define I8042_BUFFER_SIZE 16
static int
i8042_wait_read(void)
{
dprintf(7, "i8042_wait_read\n");
int i;
for (i=0; i<I8042_CTL_TIMEOUT; i++) {
u8 status = inb(PORT_PS2_STATUS);
if (status & I8042_STR_OBF)
return 0;
udelay(50);
}
dprintf(1, "i8042 timeout on wait read\n");
return -1;
}
static int
i8042_wait_write(void)
{
dprintf(7, "i8042_wait_write\n");
int i;
for (i=0; i<I8042_CTL_TIMEOUT; i++) {
u8 status = inb(PORT_PS2_STATUS);
if (! (status & I8042_STR_IBF))
return 0;
udelay(50);
}
dprintf(1, "i8042 timeout on wait write\n");
return -1;
}
int
i8042_flush(void)
{
dprintf(7, "i8042_flush\n");
unsigned long flags = irq_save();
int i;
for (i=0; i<I8042_BUFFER_SIZE; i++) {
u8 status = inb(PORT_PS2_STATUS);
if (! (status & I8042_STR_OBF)) {
irq_restore(flags);
return 0;
}
udelay(50);
inb(PORT_PS2_DATA);
}
irq_restore(flags);
dprintf(1, "i8042 timeout on flush\n");
return -1;
}
static int
__i8042_command(int command, u8 *param)
{
int receive = (command >> 8) & 0xf;
int send = (command >> 12) & 0xf;
// Send the command.
int ret = i8042_wait_write();
if (ret)
return ret;
outb(command, PORT_PS2_STATUS);
// Send parameters (if any).
int i;
for (i = 0; i < send; i++) {
ret = i8042_wait_write();
if (ret)
return ret;
outb(param[i], PORT_PS2_DATA);
}
// Receive parameters (if any).
for (i = 0; i < receive; i++) {
ret = i8042_wait_read();
if (ret)
return ret;
param[i] = inb(PORT_PS2_DATA);
}
return 0;
}
int
i8042_command(int command, u8 *param)
{
dprintf(7, "i8042_command cmd=%x\n", command);
unsigned long flags = irq_save();
int ret = __i8042_command(command, param);
irq_restore(flags);
if (ret)
dprintf(2, "i8042 command %x failed\n", command);
return ret;
}
static int
i8042_kbd_write(u8 c)
{
dprintf(7, "i8042_kbd_write c=%d\n", c);
unsigned long flags = irq_save();
int ret = i8042_wait_write();
if (! ret)
outb(c, PORT_PS2_DATA);
irq_restore(flags);
return ret;
}
static int
i8042_aux_write(u8 c)
{
return i8042_command(I8042_CMD_AUX_SEND, &c);
}
/****************************************************************
* Device commands.
****************************************************************/
#define PS2_RET_ACK 0xfa
#define PS2_RET_NAK 0xfe
static int
ps2_sendbyte(int aux, u8 command)
{
dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
int ret;
if (aux)
ret = i8042_aux_write(command);
else
ret = i8042_kbd_write(command);
if (ret)
return ret;
// Read ack.
ret = i8042_wait_read();
if (ret)
return ret;
u8 ack = inb(PORT_PS2_DATA);
if (ack != PS2_RET_ACK) {
dprintf(1, "Missing ack (got %x not %x)\n", ack, PS2_RET_ACK);
return -1;
}
return 0;
}
static int
ps2_command(int aux, int command, u8 *param)
{
int ret2;
int receive = (command >> 8) & 0xf;
int send = (command >> 12) & 0xf;
// Disable interrupts and keyboard/mouse.
u8 ps2ctr = GET_EBDA(ps2ctr);
u8 newctr = ps2ctr;
if (aux)
newctr |= I8042_CTR_KBDDIS;
else
newctr |= I8042_CTR_AUXDIS;
newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
if (ret)
return ret;
// Send command.
ret = ps2_sendbyte(aux, command);
if (ret)
goto fail;
// Send parameters (if any).
int i;
for (i = 0; i < send; i++) {
ret = ps2_sendbyte(aux, param[i]);
if (ret)
goto fail;
}
// Receive parameters (if any).
for (i = 0; i < receive; i++) {
ret = i8042_wait_read();
if (ret) {
// On a receive timeout, return the item number that the
// transfer failed on.
ret = i + 1;
goto fail;
}
param[i] = inb(PORT_PS2_DATA);
}
fail:
// Restore interrupts and keyboard/mouse.
ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
if (ret2)
return ret2;
return ret;
}
int
kbd_command(int command, u8 *param)
{
dprintf(7, "kbd_command cmd=%x\n", command);
int ret = ps2_command(0, command, param);
if (ret)
dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
return ret;
}
int
aux_command(int command, u8 *param)
{
dprintf(7, "aux_command cmd=%x\n", command);
int ret = ps2_command(1, command, param);
if (ret)
dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);
return ret;
}
|