root / hw / pckbd.c @ 9596ebb7
History | View | Annotate | Download (13.1 kB)
1 |
/*
|
---|---|
2 |
* QEMU PC keyboard emulation
|
3 |
*
|
4 |
* Copyright (c) 2003 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "hw.h" |
25 |
#include "isa.h" |
26 |
#include "pc.h" |
27 |
#include "ps2.h" |
28 |
#include "sysemu.h" |
29 |
|
30 |
/* debug PC keyboard */
|
31 |
//#define DEBUG_KBD
|
32 |
|
33 |
/* debug PC keyboard : only mouse */
|
34 |
//#define DEBUG_MOUSE
|
35 |
|
36 |
/* Keyboard Controller Commands */
|
37 |
#define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */ |
38 |
#define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */ |
39 |
#define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */ |
40 |
#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
41 |
#define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */ |
42 |
#define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */ |
43 |
#define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */ |
44 |
#define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */ |
45 |
#define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */ |
46 |
#define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */ |
47 |
#define KBD_CCMD_READ_INPORT 0xC0 /* read input port */ |
48 |
#define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */ |
49 |
#define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */ |
50 |
#define KBD_CCMD_WRITE_OBUF 0xD2 |
51 |
#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
52 |
initiated by the auxiliary device */
|
53 |
#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
54 |
#define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */ |
55 |
#define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */ |
56 |
#define KBD_CCMD_RESET 0xFE |
57 |
|
58 |
/* Keyboard Commands */
|
59 |
#define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ |
60 |
#define KBD_CMD_ECHO 0xEE |
61 |
#define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */ |
62 |
#define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ |
63 |
#define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ |
64 |
#define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */ |
65 |
#define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */ |
66 |
#define KBD_CMD_RESET 0xFF /* Reset */ |
67 |
|
68 |
/* Keyboard Replies */
|
69 |
#define KBD_REPLY_POR 0xAA /* Power on reset */ |
70 |
#define KBD_REPLY_ACK 0xFA /* Command ACK */ |
71 |
#define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ |
72 |
|
73 |
/* Status Register Bits */
|
74 |
#define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ |
75 |
#define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */ |
76 |
#define KBD_STAT_SELFTEST 0x04 /* Self test successful */ |
77 |
#define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */ |
78 |
#define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */ |
79 |
#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
80 |
#define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */ |
81 |
#define KBD_STAT_PERR 0x80 /* Parity error */ |
82 |
|
83 |
/* Controller Mode Register Bits */
|
84 |
#define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */ |
85 |
#define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */ |
86 |
#define KBD_MODE_SYS 0x04 /* The system flag (?) */ |
87 |
#define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */ |
88 |
#define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */ |
89 |
#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
90 |
#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
91 |
#define KBD_MODE_RFU 0x80 |
92 |
|
93 |
/* Mouse Commands */
|
94 |
#define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */ |
95 |
#define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */ |
96 |
#define AUX_SET_RES 0xE8 /* Set resolution */ |
97 |
#define AUX_GET_SCALE 0xE9 /* Get scaling factor */ |
98 |
#define AUX_SET_STREAM 0xEA /* Set stream mode */ |
99 |
#define AUX_POLL 0xEB /* Poll */ |
100 |
#define AUX_RESET_WRAP 0xEC /* Reset wrap mode */ |
101 |
#define AUX_SET_WRAP 0xEE /* Set wrap mode */ |
102 |
#define AUX_SET_REMOTE 0xF0 /* Set remote mode */ |
103 |
#define AUX_GET_TYPE 0xF2 /* Get type */ |
104 |
#define AUX_SET_SAMPLE 0xF3 /* Set sample rate */ |
105 |
#define AUX_ENABLE_DEV 0xF4 /* Enable aux device */ |
106 |
#define AUX_DISABLE_DEV 0xF5 /* Disable aux device */ |
107 |
#define AUX_SET_DEFAULT 0xF6 |
108 |
#define AUX_RESET 0xFF /* Reset aux device */ |
109 |
#define AUX_ACK 0xFA /* Command byte ACK. */ |
110 |
|
111 |
#define MOUSE_STATUS_REMOTE 0x40 |
112 |
#define MOUSE_STATUS_ENABLED 0x20 |
113 |
#define MOUSE_STATUS_SCALE21 0x10 |
114 |
|
115 |
#define KBD_QUEUE_SIZE 256 |
116 |
|
117 |
#define KBD_PENDING_KBD 1 |
118 |
#define KBD_PENDING_AUX 2 |
119 |
|
120 |
typedef struct KBDState { |
121 |
uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
|
122 |
uint8_t status; |
123 |
uint8_t mode; |
124 |
/* Bitmask of devices with data available. */
|
125 |
uint8_t pending; |
126 |
void *kbd;
|
127 |
void *mouse;
|
128 |
|
129 |
qemu_irq irq_kbd; |
130 |
qemu_irq irq_mouse; |
131 |
target_phys_addr_t base; |
132 |
int it_shift;
|
133 |
} KBDState; |
134 |
|
135 |
KBDState kbd_state; |
136 |
|
137 |
/* update irq and KBD_STAT_[MOUSE_]OBF */
|
138 |
/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
|
139 |
incorrect, but it avoids having to simulate exact delays */
|
140 |
static void kbd_update_irq(KBDState *s) |
141 |
{ |
142 |
int irq_kbd_level, irq_mouse_level;
|
143 |
|
144 |
irq_kbd_level = 0;
|
145 |
irq_mouse_level = 0;
|
146 |
s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF); |
147 |
if (s->pending) {
|
148 |
s->status |= KBD_STAT_OBF; |
149 |
/* kbd data takes priority over aux data. */
|
150 |
if (s->pending == KBD_PENDING_AUX) {
|
151 |
s->status |= KBD_STAT_MOUSE_OBF; |
152 |
if (s->mode & KBD_MODE_MOUSE_INT)
|
153 |
irq_mouse_level = 1;
|
154 |
} else {
|
155 |
if ((s->mode & KBD_MODE_KBD_INT) &&
|
156 |
!(s->mode & KBD_MODE_DISABLE_KBD)) |
157 |
irq_kbd_level = 1;
|
158 |
} |
159 |
} |
160 |
qemu_set_irq(s->irq_kbd, irq_kbd_level); |
161 |
qemu_set_irq(s->irq_mouse, irq_mouse_level); |
162 |
} |
163 |
|
164 |
static void kbd_update_kbd_irq(void *opaque, int level) |
165 |
{ |
166 |
KBDState *s = (KBDState *)opaque; |
167 |
|
168 |
if (level)
|
169 |
s->pending |= KBD_PENDING_KBD; |
170 |
else
|
171 |
s->pending &= ~KBD_PENDING_KBD; |
172 |
kbd_update_irq(s); |
173 |
} |
174 |
|
175 |
static void kbd_update_aux_irq(void *opaque, int level) |
176 |
{ |
177 |
KBDState *s = (KBDState *)opaque; |
178 |
|
179 |
if (level)
|
180 |
s->pending |= KBD_PENDING_AUX; |
181 |
else
|
182 |
s->pending &= ~KBD_PENDING_AUX; |
183 |
kbd_update_irq(s); |
184 |
} |
185 |
|
186 |
static uint32_t kbd_read_status(void *opaque, uint32_t addr) |
187 |
{ |
188 |
KBDState *s = opaque; |
189 |
int val;
|
190 |
val = s->status; |
191 |
#if defined(DEBUG_KBD)
|
192 |
printf("kbd: read status=0x%02x\n", val);
|
193 |
#endif
|
194 |
return val;
|
195 |
} |
196 |
|
197 |
static void kbd_queue(KBDState *s, int b, int aux) |
198 |
{ |
199 |
if (aux)
|
200 |
ps2_queue(s->mouse, b); |
201 |
else
|
202 |
ps2_queue(s->kbd, b); |
203 |
} |
204 |
|
205 |
static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val) |
206 |
{ |
207 |
KBDState *s = opaque; |
208 |
|
209 |
#ifdef DEBUG_KBD
|
210 |
printf("kbd: write cmd=0x%02x\n", val);
|
211 |
#endif
|
212 |
switch(val) {
|
213 |
case KBD_CCMD_READ_MODE:
|
214 |
kbd_queue(s, s->mode, 1);
|
215 |
break;
|
216 |
case KBD_CCMD_WRITE_MODE:
|
217 |
case KBD_CCMD_WRITE_OBUF:
|
218 |
case KBD_CCMD_WRITE_AUX_OBUF:
|
219 |
case KBD_CCMD_WRITE_MOUSE:
|
220 |
case KBD_CCMD_WRITE_OUTPORT:
|
221 |
s->write_cmd = val; |
222 |
break;
|
223 |
case KBD_CCMD_MOUSE_DISABLE:
|
224 |
s->mode |= KBD_MODE_DISABLE_MOUSE; |
225 |
break;
|
226 |
case KBD_CCMD_MOUSE_ENABLE:
|
227 |
s->mode &= ~KBD_MODE_DISABLE_MOUSE; |
228 |
break;
|
229 |
case KBD_CCMD_TEST_MOUSE:
|
230 |
kbd_queue(s, 0x00, 0); |
231 |
break;
|
232 |
case KBD_CCMD_SELF_TEST:
|
233 |
s->status |= KBD_STAT_SELFTEST; |
234 |
kbd_queue(s, 0x55, 0); |
235 |
break;
|
236 |
case KBD_CCMD_KBD_TEST:
|
237 |
kbd_queue(s, 0x00, 0); |
238 |
break;
|
239 |
case KBD_CCMD_KBD_DISABLE:
|
240 |
s->mode |= KBD_MODE_DISABLE_KBD; |
241 |
kbd_update_irq(s); |
242 |
break;
|
243 |
case KBD_CCMD_KBD_ENABLE:
|
244 |
s->mode &= ~KBD_MODE_DISABLE_KBD; |
245 |
kbd_update_irq(s); |
246 |
break;
|
247 |
case KBD_CCMD_READ_INPORT:
|
248 |
kbd_queue(s, 0x00, 0); |
249 |
break;
|
250 |
case KBD_CCMD_READ_OUTPORT:
|
251 |
/* XXX: check that */
|
252 |
#ifdef TARGET_I386
|
253 |
val = 0x01 | (ioport_get_a20() << 1); |
254 |
#else
|
255 |
val = 0x01;
|
256 |
#endif
|
257 |
if (s->status & KBD_STAT_OBF)
|
258 |
val |= 0x10;
|
259 |
if (s->status & KBD_STAT_MOUSE_OBF)
|
260 |
val |= 0x20;
|
261 |
kbd_queue(s, val, 0);
|
262 |
break;
|
263 |
#ifdef TARGET_I386
|
264 |
case KBD_CCMD_ENABLE_A20:
|
265 |
ioport_set_a20(1);
|
266 |
break;
|
267 |
case KBD_CCMD_DISABLE_A20:
|
268 |
ioport_set_a20(0);
|
269 |
break;
|
270 |
#endif
|
271 |
case KBD_CCMD_RESET:
|
272 |
qemu_system_reset_request(); |
273 |
break;
|
274 |
case 0xff: |
275 |
/* ignore that - I don't know what is its use */
|
276 |
break;
|
277 |
default:
|
278 |
fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
|
279 |
break;
|
280 |
} |
281 |
} |
282 |
|
283 |
static uint32_t kbd_read_data(void *opaque, uint32_t addr) |
284 |
{ |
285 |
KBDState *s = opaque; |
286 |
|
287 |
if (s->pending == KBD_PENDING_AUX)
|
288 |
return ps2_read_data(s->mouse);
|
289 |
|
290 |
return ps2_read_data(s->kbd);
|
291 |
} |
292 |
|
293 |
static void kbd_write_data(void *opaque, uint32_t addr, uint32_t val) |
294 |
{ |
295 |
KBDState *s = opaque; |
296 |
|
297 |
#ifdef DEBUG_KBD
|
298 |
printf("kbd: write data=0x%02x\n", val);
|
299 |
#endif
|
300 |
|
301 |
switch(s->write_cmd) {
|
302 |
case 0: |
303 |
ps2_write_keyboard(s->kbd, val); |
304 |
break;
|
305 |
case KBD_CCMD_WRITE_MODE:
|
306 |
s->mode = val; |
307 |
ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
|
308 |
/* ??? */
|
309 |
kbd_update_irq(s); |
310 |
break;
|
311 |
case KBD_CCMD_WRITE_OBUF:
|
312 |
kbd_queue(s, val, 0);
|
313 |
break;
|
314 |
case KBD_CCMD_WRITE_AUX_OBUF:
|
315 |
kbd_queue(s, val, 1);
|
316 |
break;
|
317 |
case KBD_CCMD_WRITE_OUTPORT:
|
318 |
#ifdef TARGET_I386
|
319 |
ioport_set_a20((val >> 1) & 1); |
320 |
#endif
|
321 |
if (!(val & 1)) { |
322 |
qemu_system_reset_request(); |
323 |
} |
324 |
break;
|
325 |
case KBD_CCMD_WRITE_MOUSE:
|
326 |
ps2_write_mouse(s->mouse, val); |
327 |
break;
|
328 |
default:
|
329 |
break;
|
330 |
} |
331 |
s->write_cmd = 0;
|
332 |
} |
333 |
|
334 |
static void kbd_reset(void *opaque) |
335 |
{ |
336 |
KBDState *s = opaque; |
337 |
|
338 |
s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT; |
339 |
s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED; |
340 |
} |
341 |
|
342 |
static void kbd_save(QEMUFile* f, void* opaque) |
343 |
{ |
344 |
KBDState *s = (KBDState*)opaque; |
345 |
|
346 |
qemu_put_8s(f, &s->write_cmd); |
347 |
qemu_put_8s(f, &s->status); |
348 |
qemu_put_8s(f, &s->mode); |
349 |
qemu_put_8s(f, &s->pending); |
350 |
} |
351 |
|
352 |
static int kbd_load(QEMUFile* f, void* opaque, int version_id) |
353 |
{ |
354 |
KBDState *s = (KBDState*)opaque; |
355 |
|
356 |
if (version_id != 3) |
357 |
return -EINVAL;
|
358 |
qemu_get_8s(f, &s->write_cmd); |
359 |
qemu_get_8s(f, &s->status); |
360 |
qemu_get_8s(f, &s->mode); |
361 |
qemu_get_8s(f, &s->pending); |
362 |
return 0; |
363 |
} |
364 |
|
365 |
void i8042_init(qemu_irq kbd_irq, qemu_irq mouse_irq, uint32_t io_base)
|
366 |
{ |
367 |
KBDState *s = &kbd_state; |
368 |
|
369 |
s->irq_kbd = kbd_irq; |
370 |
s->irq_mouse = mouse_irq; |
371 |
|
372 |
kbd_reset(s); |
373 |
register_savevm("pckbd", 0, 3, kbd_save, kbd_load, s); |
374 |
register_ioport_read(io_base, 1, 1, kbd_read_data, s); |
375 |
register_ioport_write(io_base, 1, 1, kbd_write_data, s); |
376 |
register_ioport_read(io_base + 4, 1, 1, kbd_read_status, s); |
377 |
register_ioport_write(io_base + 4, 1, 1, kbd_write_command, s); |
378 |
|
379 |
s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s); |
380 |
s->mouse = ps2_mouse_init(kbd_update_aux_irq, s); |
381 |
#ifdef TARGET_I386
|
382 |
vmmouse_init(s->mouse); |
383 |
#endif
|
384 |
qemu_register_reset(kbd_reset, s); |
385 |
} |
386 |
|
387 |
/* Memory mapped interface */
|
388 |
static uint32_t kbd_mm_readb (void *opaque, target_phys_addr_t addr) |
389 |
{ |
390 |
KBDState *s = opaque; |
391 |
|
392 |
switch ((addr - s->base) >> s->it_shift) {
|
393 |
case 0: |
394 |
return kbd_read_data(s, 0) & 0xff; |
395 |
case 1: |
396 |
return kbd_read_status(s, 0) & 0xff; |
397 |
default:
|
398 |
return 0xff; |
399 |
} |
400 |
} |
401 |
|
402 |
static void kbd_mm_writeb (void *opaque, target_phys_addr_t addr, uint32_t value) |
403 |
{ |
404 |
KBDState *s = opaque; |
405 |
|
406 |
switch ((addr - s->base) >> s->it_shift) {
|
407 |
case 0: |
408 |
kbd_write_data(s, 0, value & 0xff); |
409 |
break;
|
410 |
case 1: |
411 |
kbd_write_command(s, 0, value & 0xff); |
412 |
break;
|
413 |
} |
414 |
} |
415 |
|
416 |
static CPUReadMemoryFunc *kbd_mm_read[] = {
|
417 |
&kbd_mm_readb, |
418 |
&kbd_mm_readb, |
419 |
&kbd_mm_readb, |
420 |
}; |
421 |
|
422 |
static CPUWriteMemoryFunc *kbd_mm_write[] = {
|
423 |
&kbd_mm_writeb, |
424 |
&kbd_mm_writeb, |
425 |
&kbd_mm_writeb, |
426 |
}; |
427 |
|
428 |
void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
|
429 |
target_phys_addr_t base, int it_shift)
|
430 |
{ |
431 |
KBDState *s = &kbd_state; |
432 |
int s_io_memory;
|
433 |
|
434 |
s->irq_kbd = kbd_irq; |
435 |
s->irq_mouse = mouse_irq; |
436 |
s->base = base; |
437 |
s->it_shift = it_shift; |
438 |
|
439 |
kbd_reset(s); |
440 |
register_savevm("pckbd", 0, 3, kbd_save, kbd_load, s); |
441 |
s_io_memory = cpu_register_io_memory(0, kbd_mm_read, kbd_mm_write, s);
|
442 |
cpu_register_physical_memory(base, 8 << it_shift, s_io_memory);
|
443 |
|
444 |
s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s); |
445 |
s->mouse = ps2_mouse_init(kbd_update_aux_irq, s); |
446 |
#ifdef TARGET_I386
|
447 |
vmmouse_init(s->mouse); |
448 |
#endif
|
449 |
qemu_register_reset(kbd_reset, s); |
450 |
} |