]> git.proxmox.com Git - mirror_qemu.git/blob - hw/pckbd.c
Compile pckbd only once
[mirror_qemu.git] / hw / pckbd.c
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 /* Keyboard Controller Commands */
34 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
35 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
36 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
37 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
38 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
39 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
40 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
41 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
42 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
43 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
44 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
45 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
46 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
47 #define KBD_CCMD_WRITE_OBUF 0xD2
48 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
49 initiated by the auxiliary device */
50 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
51 #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
52 #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
53 #define KBD_CCMD_RESET 0xFE
54
55 /* Keyboard Commands */
56 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
57 #define KBD_CMD_ECHO 0xEE
58 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
59 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
60 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
61 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
62 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
63 #define KBD_CMD_RESET 0xFF /* Reset */
64
65 /* Keyboard Replies */
66 #define KBD_REPLY_POR 0xAA /* Power on reset */
67 #define KBD_REPLY_ACK 0xFA /* Command ACK */
68 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
69
70 /* Status Register Bits */
71 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
72 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
73 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
74 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
75 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
76 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
77 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
78 #define KBD_STAT_PERR 0x80 /* Parity error */
79
80 /* Controller Mode Register Bits */
81 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
82 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
83 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
84 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
85 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
86 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
87 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
88 #define KBD_MODE_RFU 0x80
89
90 /* Output Port Bits */
91 #define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
92 #define KBD_OUT_A20 0x02 /* x86 only */
93 #define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
94 #define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
95
96 /* Mouse Commands */
97 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
98 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
99 #define AUX_SET_RES 0xE8 /* Set resolution */
100 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
101 #define AUX_SET_STREAM 0xEA /* Set stream mode */
102 #define AUX_POLL 0xEB /* Poll */
103 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
104 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
105 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
106 #define AUX_GET_TYPE 0xF2 /* Get type */
107 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
108 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
109 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
110 #define AUX_SET_DEFAULT 0xF6
111 #define AUX_RESET 0xFF /* Reset aux device */
112 #define AUX_ACK 0xFA /* Command byte ACK. */
113
114 #define MOUSE_STATUS_REMOTE 0x40
115 #define MOUSE_STATUS_ENABLED 0x20
116 #define MOUSE_STATUS_SCALE21 0x10
117
118 #define KBD_PENDING_KBD 1
119 #define KBD_PENDING_AUX 2
120
121 typedef struct KBDState {
122 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
123 uint8_t status;
124 uint8_t mode;
125 uint8_t outport;
126 /* Bitmask of devices with data available. */
127 uint8_t pending;
128 void *kbd;
129 void *mouse;
130
131 qemu_irq irq_kbd;
132 qemu_irq irq_mouse;
133 qemu_irq *a20_out;
134 target_phys_addr_t mask;
135 } KBDState;
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 s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
148 if (s->pending) {
149 s->status |= KBD_STAT_OBF;
150 s->outport |= KBD_OUT_OBF;
151 /* kbd data takes priority over aux data. */
152 if (s->pending == KBD_PENDING_AUX) {
153 s->status |= KBD_STAT_MOUSE_OBF;
154 s->outport |= KBD_OUT_MOUSE_OBF;
155 if (s->mode & KBD_MODE_MOUSE_INT)
156 irq_mouse_level = 1;
157 } else {
158 if ((s->mode & KBD_MODE_KBD_INT) &&
159 !(s->mode & KBD_MODE_DISABLE_KBD))
160 irq_kbd_level = 1;
161 }
162 }
163 qemu_set_irq(s->irq_kbd, irq_kbd_level);
164 qemu_set_irq(s->irq_mouse, irq_mouse_level);
165 }
166
167 static void kbd_update_kbd_irq(void *opaque, int level)
168 {
169 KBDState *s = (KBDState *)opaque;
170
171 if (level)
172 s->pending |= KBD_PENDING_KBD;
173 else
174 s->pending &= ~KBD_PENDING_KBD;
175 kbd_update_irq(s);
176 }
177
178 static void kbd_update_aux_irq(void *opaque, int level)
179 {
180 KBDState *s = (KBDState *)opaque;
181
182 if (level)
183 s->pending |= KBD_PENDING_AUX;
184 else
185 s->pending &= ~KBD_PENDING_AUX;
186 kbd_update_irq(s);
187 }
188
189 static uint32_t kbd_read_status(void *opaque, uint32_t addr)
190 {
191 KBDState *s = opaque;
192 int val;
193 val = s->status;
194 #if defined(DEBUG_KBD)
195 printf("kbd: read status=0x%02x\n", val);
196 #endif
197 return val;
198 }
199
200 static void kbd_queue(KBDState *s, int b, int aux)
201 {
202 if (aux)
203 ps2_queue(s->mouse, b);
204 else
205 ps2_queue(s->kbd, b);
206 }
207
208 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
209 {
210 KBDState *s = opaque;
211
212 s->outport = val;
213 if (s->a20_out) {
214 qemu_set_irq(*s->a20_out, (val >> 1) & 1);
215 }
216 if (!(val & 1)) {
217 qemu_system_reset_request();
218 }
219 }
220
221 static uint32_t ioport92_read(void *opaque, uint32_t addr)
222 {
223 KBDState *s = opaque;
224
225 return s->outport;
226 }
227
228 static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
229 {
230 KBDState *s = opaque;
231
232 #ifdef DEBUG_KBD
233 printf("kbd: write cmd=0x%02x\n", val);
234 #endif
235 switch(val) {
236 case KBD_CCMD_READ_MODE:
237 kbd_queue(s, s->mode, 0);
238 break;
239 case KBD_CCMD_WRITE_MODE:
240 case KBD_CCMD_WRITE_OBUF:
241 case KBD_CCMD_WRITE_AUX_OBUF:
242 case KBD_CCMD_WRITE_MOUSE:
243 case KBD_CCMD_WRITE_OUTPORT:
244 s->write_cmd = val;
245 break;
246 case KBD_CCMD_MOUSE_DISABLE:
247 s->mode |= KBD_MODE_DISABLE_MOUSE;
248 break;
249 case KBD_CCMD_MOUSE_ENABLE:
250 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
251 break;
252 case KBD_CCMD_TEST_MOUSE:
253 kbd_queue(s, 0x00, 0);
254 break;
255 case KBD_CCMD_SELF_TEST:
256 s->status |= KBD_STAT_SELFTEST;
257 kbd_queue(s, 0x55, 0);
258 break;
259 case KBD_CCMD_KBD_TEST:
260 kbd_queue(s, 0x00, 0);
261 break;
262 case KBD_CCMD_KBD_DISABLE:
263 s->mode |= KBD_MODE_DISABLE_KBD;
264 kbd_update_irq(s);
265 break;
266 case KBD_CCMD_KBD_ENABLE:
267 s->mode &= ~KBD_MODE_DISABLE_KBD;
268 kbd_update_irq(s);
269 break;
270 case KBD_CCMD_READ_INPORT:
271 kbd_queue(s, 0x00, 0);
272 break;
273 case KBD_CCMD_READ_OUTPORT:
274 kbd_queue(s, s->outport, 0);
275 break;
276 case KBD_CCMD_ENABLE_A20:
277 if (s->a20_out) {
278 qemu_irq_raise(*s->a20_out);
279 }
280 s->outport |= KBD_OUT_A20;
281 break;
282 case KBD_CCMD_DISABLE_A20:
283 if (s->a20_out) {
284 qemu_irq_lower(*s->a20_out);
285 }
286 s->outport &= ~KBD_OUT_A20;
287 break;
288 case KBD_CCMD_RESET:
289 qemu_system_reset_request();
290 break;
291 case 0xff:
292 /* ignore that - I don't know what is its use */
293 break;
294 default:
295 fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
296 break;
297 }
298 }
299
300 static uint32_t kbd_read_data(void *opaque, uint32_t addr)
301 {
302 KBDState *s = opaque;
303 uint32_t val;
304
305 if (s->pending == KBD_PENDING_AUX)
306 val = ps2_read_data(s->mouse);
307 else
308 val = ps2_read_data(s->kbd);
309
310 #if defined(DEBUG_KBD)
311 printf("kbd: read data=0x%02x\n", val);
312 #endif
313 return val;
314 }
315
316 static void kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
317 {
318 KBDState *s = opaque;
319
320 #ifdef DEBUG_KBD
321 printf("kbd: write data=0x%02x\n", val);
322 #endif
323
324 switch(s->write_cmd) {
325 case 0:
326 ps2_write_keyboard(s->kbd, val);
327 break;
328 case KBD_CCMD_WRITE_MODE:
329 s->mode = val;
330 ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
331 /* ??? */
332 kbd_update_irq(s);
333 break;
334 case KBD_CCMD_WRITE_OBUF:
335 kbd_queue(s, val, 0);
336 break;
337 case KBD_CCMD_WRITE_AUX_OBUF:
338 kbd_queue(s, val, 1);
339 break;
340 case KBD_CCMD_WRITE_OUTPORT:
341 ioport92_write(s, 0, val);
342 break;
343 case KBD_CCMD_WRITE_MOUSE:
344 ps2_write_mouse(s->mouse, val);
345 break;
346 default:
347 break;
348 }
349 s->write_cmd = 0;
350 }
351
352 static void kbd_reset(void *opaque)
353 {
354 KBDState *s = opaque;
355
356 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
357 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
358 s->outport = KBD_OUT_RESET | KBD_OUT_A20;
359 }
360
361 static const VMStateDescription vmstate_kbd = {
362 .name = "pckbd",
363 .version_id = 3,
364 .minimum_version_id = 3,
365 .minimum_version_id_old = 3,
366 .fields = (VMStateField []) {
367 VMSTATE_UINT8(write_cmd, KBDState),
368 VMSTATE_UINT8(status, KBDState),
369 VMSTATE_UINT8(mode, KBDState),
370 VMSTATE_UINT8(pending, KBDState),
371 VMSTATE_END_OF_LIST()
372 }
373 };
374
375 /* Memory mapped interface */
376 static uint32_t kbd_mm_readb (void *opaque, target_phys_addr_t addr)
377 {
378 KBDState *s = opaque;
379
380 if (addr & s->mask)
381 return kbd_read_status(s, 0) & 0xff;
382 else
383 return kbd_read_data(s, 0) & 0xff;
384 }
385
386 static void kbd_mm_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
387 {
388 KBDState *s = opaque;
389
390 if (addr & s->mask)
391 kbd_write_command(s, 0, value & 0xff);
392 else
393 kbd_write_data(s, 0, value & 0xff);
394 }
395
396 static CPUReadMemoryFunc * const kbd_mm_read[] = {
397 &kbd_mm_readb,
398 &kbd_mm_readb,
399 &kbd_mm_readb,
400 };
401
402 static CPUWriteMemoryFunc * const kbd_mm_write[] = {
403 &kbd_mm_writeb,
404 &kbd_mm_writeb,
405 &kbd_mm_writeb,
406 };
407
408 void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
409 target_phys_addr_t base, ram_addr_t size,
410 target_phys_addr_t mask)
411 {
412 KBDState *s = qemu_mallocz(sizeof(KBDState));
413 int s_io_memory;
414
415 s->irq_kbd = kbd_irq;
416 s->irq_mouse = mouse_irq;
417 s->mask = mask;
418
419 vmstate_register(0, &vmstate_kbd, s);
420 s_io_memory = cpu_register_io_memory(kbd_mm_read, kbd_mm_write, s);
421 cpu_register_physical_memory(base, size, s_io_memory);
422
423 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
424 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
425 qemu_register_reset(kbd_reset, s);
426 }
427
428 typedef struct ISAKBDState {
429 ISADevice dev;
430 KBDState kbd;
431 } ISAKBDState;
432
433 void i8042_isa_mouse_fake_event(void *opaque)
434 {
435 ISADevice *dev = opaque;
436 KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
437
438 ps2_mouse_fake_event(s->mouse);
439 }
440
441 void i8042_setup_a20_line(ISADevice *dev, qemu_irq *a20_out)
442 {
443 KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
444
445 s->a20_out = a20_out;
446 }
447
448 static const VMStateDescription vmstate_kbd_isa = {
449 .name = "pckbd",
450 .version_id = 3,
451 .minimum_version_id = 3,
452 .minimum_version_id_old = 3,
453 .fields = (VMStateField []) {
454 VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
455 VMSTATE_END_OF_LIST()
456 }
457 };
458
459 static int i8042_initfn(ISADevice *dev)
460 {
461 KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
462
463 isa_init_irq(dev, &s->irq_kbd, 1);
464 isa_init_irq(dev, &s->irq_mouse, 12);
465
466 register_ioport_read(0x60, 1, 1, kbd_read_data, s);
467 register_ioport_write(0x60, 1, 1, kbd_write_data, s);
468 register_ioport_read(0x64, 1, 1, kbd_read_status, s);
469 register_ioport_write(0x64, 1, 1, kbd_write_command, s);
470 register_ioport_read(0x92, 1, 1, ioport92_read, s);
471 register_ioport_write(0x92, 1, 1, ioport92_write, s);
472
473 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
474 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
475 qemu_register_reset(kbd_reset, s);
476 return 0;
477 }
478
479 static ISADeviceInfo i8042_info = {
480 .qdev.name = "i8042",
481 .qdev.size = sizeof(ISAKBDState),
482 .qdev.vmsd = &vmstate_kbd_isa,
483 .qdev.no_user = 1,
484 .init = i8042_initfn,
485 };
486
487 static void i8042_register(void)
488 {
489 isa_qdev_register(&i8042_info);
490 }
491 device_init(i8042_register)