1 #include <devices/keyboard.h>
3 #include <palacios/vmm.h>
4 #include <palacios/vmm_types.h>
6 #define KEYBOARD_DEBUG 0
8 #define KEYBOARD_DEBUG_80H 0
11 #define KEYBOARD_DEBUG_PRINT(first, rest...) PrintDebug(first, ##rest)
13 #define KEYBOARD_DEBUG_PRINT(first, rest...)
17 extern struct vmm_os_hooks *os_hooks;
19 extern void SerialPrint(const char *format, ...);
23 #define KEYBOARD_60H 0x60 // keyboard microcontroller
24 #define KEYBOARD_64H 0x64 // onboard microcontroller
26 #define KEYBOARD_DELAY_80H 0x80 // written for timing
28 #define KEYBOARD_IRQ 0x1
31 // extract bits for status byte
32 #define STATUS_OUTPUT_BUFFER_FULL 0x01 // 1=full (data for system)
33 #define STATUS_INPUT_BUFFER_FULL 0x02 // 1=full (data for 8042)
34 #define STATUS_SYSTEM 0x04 // 1=self-test-passed
35 #define STATUS_COMMAND_DATA_AVAIL 0x08 // internal: 0=data on 60h, 0=cmd on 64h
36 #define STATUS_ENABLED 0x10 // 1=keyboard is enabled
37 #define STATUS_MOUSE_BUFFER_FULL 0x20 // 1= mouse output buffer full
38 #define STATUS_TIMEOUT 0x40 // 1=timeout of keybd
39 #define STATUS_PARITY 0x80 // 1=parity error
43 #define CMD_INTR 0x01 // 1=interrupts enabled
44 #define CMD_MOUSE_INTR 0x02 // 1=interrupts enabled for mouse
45 #define CMD_SYSTEM 0x04 // 1= self test passed
46 #define CMD_OVERRIDE 0x08 // FORCE 0 for PS2
47 #define CMD_DISABLE 0x10 // 1=disabled keyboard
48 #define CMD_MOUSE_DISABLE 0x20 // 1=disabled mouse
49 #define CMD_SCANCODE_XLATE 0x40 // 1=translate to set 1 scancodes
50 #define CMD_RESERVED 0x80 // should be zero
52 // The currently targetted keyboard
53 static struct vm_device *thekeyboard=NULL;
56 struct keyboard_internal {
58 // 0x60 is the port for the keyboard microcontroller
59 // writes are commands
60 // reads from it usually return scancodes
61 // however, it can also return other data
62 // depending on the state of the onboard microcontroller
64 // 0x64 is the port for the onboard microcontroller
65 // writes are commands
69 // state of the onboard microcontroller
70 // this is needed because sometimes 0x60 reads come
71 // from the onboard microcontroller
72 enum {// Normal mode measn we deliver keys
73 // to the vm and accept commands from it
75 // after receiving cmd 0x60
76 // keybaord uC cmd will subsequently arrive
78 // after recieving 0xa5
79 // password arrives on data port, null terminated
81 // after having reset sent to 0x60
82 // we immediately ack, and then
83 // push BAT success (0xaa) after the ack
89 uchar_t cmd_byte; // for keyboard uC - read/written
90 // via read/write cmd byte command
91 uchar_t status_byte; // for on-board uC - read via 64h
94 uchar_t input_queue; //
95 uint_t input_queue_len; //
97 uchar_t output_queue; //
98 uint_t output_queue_len; //
103 // push item onto outputqueue, optionally overwriting if there is no room
104 // returns 0 if successful
106 static int PushToOutputQueue(struct vm_device *dev, uchar_t value, uchar_t overwrite)
108 struct keyboard_internal *state = (struct keyboard_internal *)dev->private_data;
110 if (state->output_queue_len==0 || overwrite) {
111 state->output_queue=value;
112 state->output_queue_len=1;
113 state->status_byte |= STATUS_OUTPUT_BUFFER_FULL;
116 KEYBOARD_DEBUG_PRINT("keyboard: PushToOutputQueue Failed - Queue Full\n");
123 // pull item from outputqueue
124 // returns 0 if successful
126 static int PullFromOutputQueue(struct vm_device *dev,uchar_t *value)
128 struct keyboard_internal *state = (struct keyboard_internal *)dev->private_data;
129 if (state->output_queue_len==1) {
130 *value=state->output_queue;
131 state->output_queue_len=0;
132 state->status_byte &= ~STATUS_OUTPUT_BUFFER_FULL;
135 KEYBOARD_DEBUG_PRINT("keyboard: PullFromOutputQueue Failed - Queue Empty\n");
143 // push item onto inputqueue, optionally overwriting if there is no room
144 // returns 0 if successful
146 static int PushToInputQueue(struct vm_device *dev, uchar_t value, uchar_t overwrite)
148 struct keyboard_internal *state = (struct keyboard_internal *)dev->private_data;
149 if (state->input_queue_len==0 || overwrite) {
150 state->input_queue=value;
151 state->input_queue_len=1;
152 state->status_byte |= STATUS_INPUT_BUFFER_FULL;
155 KEYBOARD_DEBUG_PRINT("keyboard: PushToOutputQueue Failed - Queue Full\n");
161 // pull item from inputqueue
162 // returns 0 if successful
164 static int PullFromInputQueue(struct vm_device *dev, uchar_t *value)
166 struct keyboard_internal *state = (struct keyboard_internal *)dev->private_data;
167 if (state->input_queue_len==1) {
168 *value=state->input_queue;
169 state->input_queue_len=0;
170 state->status_byte &=~STATUS_INPUT_BUFFER_FULL;
173 KEYBOARD_DEBUG_PRINT("keyboard: PullFromInputQueue Failed - Queue Empty\n");
180 static struct vm_device *demultiplex_injected_key(uchar_t status, uchar_t scancode)
182 // this currently does nothing
186 int keyboard_interrupt(uint_t irq,struct vm_device * dev);
188 void deliver_key_to_vmm(uchar_t status, uchar_t scancode)
190 struct vm_device *dev = demultiplex_injected_key(status,scancode);
192 struct keyboard_internal *state = (struct keyboard_internal *)dev->private_data;
194 KEYBOARD_DEBUG_PRINT("keyboard: injected status 0x%x, and scancode 0x%x\n", status,scancode);
196 if ( (state->status_byte & STATUS_ENABLED) // onboard is enabled
197 && (!(state->cmd_byte & CMD_DISABLE))) { // keyboard is enabled
199 PushToOutputQueue(dev,scancode,1);
201 if (state->cmd_byte & CMD_INTR) {
202 keyboard_interrupt(KEYBOARD_IRQ,dev);
208 int keyboard_reset_device(struct vm_device * dev)
210 struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
212 memset(data,0,sizeof(struct keyboard_internal));
215 CMD_INTR // interrupts on
216 | CMD_MOUSE_INTR // mouse interupts on
217 | CMD_SYSTEM ; // self test passed
218 // PS2, keyboard+mouse enabled, generic translation
221 STATUS_SYSTEM // self-tests passed
222 | STATUS_ENABLED ; // keyboard ready
223 // buffers empty, no errors
225 KEYBOARD_DEBUG_PRINT("keyboard: reset device\n");
233 int keyboard_start_device(struct vm_device *dev)
235 KEYBOARD_DEBUG_PRINT("keyboard: start device\n");
240 int keyboard_stop_device(struct vm_device *dev)
242 KEYBOARD_DEBUG_PRINT("keyboard: stop device\n");
247 #if KEYBOARD_DEBUG_80H
248 int keyboard_write_delay(ushort_t port,
251 struct vm_device * dev)
254 KEYBOARD_DEBUG_PRINT("keyboard: write of 0x%x to 80h\n", *((uchar_t*)src));
257 KEYBOARD_DEBUG_PRINT("keyboard: write of >1 byte to 80h\n", *((uchar_t*)src));
262 int keyboard_read_delay(ushort_t port,
265 struct vm_device * dev)
268 *((uchar_t*)dest) = In_Byte(port);
269 KEYBOARD_DEBUG_PRINT("keyboard: read of 0x%x from 80h\n", *((uchar_t*)dest));
272 KEYBOARD_DEBUG_PRINT("keyboard: read of >1 byte from 80h\n");
282 int keyboard_write_command(ushort_t port,
285 struct vm_device * dev)
287 struct keyboard_internal *state = (struct keyboard_internal *) dev->private_data;
290 // Should always be single byte write
293 KEYBOARD_DEBUG_PRINT("keyboard: write of >1 bytes (%d) to 64h\n",length);
297 cmd = *((uchar_t*)src);
299 if (state->state!=NORMAL) {
300 KEYBOARD_DEBUG_PRINT("keyboard: warning - receiving command on 64h but state!=NORMAL\n");
303 KEYBOARD_DEBUG_PRINT("keyboard: command 0x%x on 64h\n",cmd);
307 case 0x20: // READ COMMAND BYTE (returned in 60h)
308 PushToOutputQueue(dev,state->cmd_byte,1);
309 state->state=NORMAL; // the next read on 0x60 will get the right data
312 case 0x60: // WRITE COMMAND BYTE (read from 60h)
313 state->state=WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
316 // case 0x90-9f - write to output port (?)
318 case 0xa1: // Get version number
319 PushToOutputQueue(dev,0,1);
323 case 0xa4: // is password installed? send result to 0x60
324 // we don't support passwords
325 PushToOutputQueue(dev,0xf1,1);
329 case 0xa5: // new password will arrive on 0x60
330 state->state=TRANSMIT_PASSWD;
333 case 0xa6: // check passwd;
334 // since we do not support passwords, we will simply ignore this
335 // the implication is that any password check immediately succeeds
336 // with a blank password
340 case 0xa7: // disable mouse
341 state->cmd_byte |= CMD_MOUSE_DISABLE;
345 case 0xa8: // enable mouse
346 state->cmd_byte &= ~CMD_MOUSE_DISABLE;
350 case 0xa9: // mouse interface test (always succeeds)
351 PushToOutputQueue(dev,0,1);
355 case 0xaa: // controller self test (always succeeds)
356 PushToOutputQueue(dev,0x55,1);
360 case 0xab: // keyboard interface test (always succeeds)
361 PushToOutputQueue(dev,0,1);
365 case 0xad: // disable keyboard
366 state->cmd_byte |= CMD_DISABLE;
370 case 0xae: // enable keyboard
371 state->cmd_byte &= ~CMD_DISABLE;
375 case 0xaf: // get version
376 PushToOutputQueue(dev,0x00,1);
380 // case c0 read input port ?
381 // case c1 copy input port lsn to status
382 // case c2 copy input port msn to status
384 // case d0 read output port
385 // case d1 write output port
386 // case d2 write keyboard buffer (inject key)
387 // case d3 write mouse buffer (inject mouse)
388 // case d4 write mouse device (command to mouse?)
390 // case e0 read test port
392 // case f0..ff pulse output port ?
396 KEYBOARD_DEBUG_PRINT("keyboard: ignoring command (unimplemented)\n");
405 int keyboard_read_status(ushort_t port,
408 struct vm_device * dev)
410 struct keyboard_internal *state = (struct keyboard_internal *) dev->private_data;
413 KEYBOARD_DEBUG_PRINT("keyboard: read status (64h): ");
414 *((uchar_t*)dest)=state->status_byte;
415 KEYBOARD_DEBUG_PRINT("0x%x\n",*((uchar_t*)dest));
418 KEYBOARD_DEBUG_PRINT("keyboard: >1 byte read for status (64h)\n");
423 int keyboard_write_output(ushort_t port,
426 struct vm_device * dev)
428 struct keyboard_internal *state = (struct keyboard_internal *) dev->private_data;
431 KEYBOARD_DEBUG_PRINT("keyboard: write of 60h with >1 byte\n");
435 uchar_t data=*((uchar_t*)src);
437 switch (state->state) {
438 case WRITING_CMD_BYTE:
439 state->cmd_byte=data;
442 case TRANSMIT_PASSWD:
451 // command is being sent to keyboard controller
454 PushToOutputQueue(dev,0xfa,1); // ack
457 case 0xf5: // disable scanning
458 case 0xf4: // enable scanning
460 PushToOutputQueue(dev,0xfa,1);
461 // should do something here... PAD
465 case 0xfd: // set key type make
466 case 0xfc: // set key typ make/break
467 case 0xfb: // set key type typematic
468 case 0xfa: // set all typematic make/break/typematic
469 case 0xf9: // set all make
470 case 0xf8: // set all make/break
471 case 0xf7: // set all typemaktic
472 case 0xf6: // set defaults
473 case 0xf3: // set typematic delay/rate
475 KEYBOARD_DEBUG_PRINT("keyboard: unhandled command 0x%x on output buffer (60h)\n",data);
480 KEYBOARD_DEBUG_PRINT("keyboard: unknown state %x on command 0x%x on output buffer (60h)\n",state->state, data);
486 int keyboard_read_input(ushort_t port,
489 struct vm_device * dev)
491 struct keyboard_internal *state = (struct keyboard_internal *) dev->private_data;
495 KEYBOARD_DEBUG_PRINT("keyboard: read from input (60h): ");
496 PullFromOutputQueue(dev,&data);
497 if (state->state==RESET) {
498 // We just delivered the ack for the reset
499 // now we will ready ourselves to deliver the BAT code (success)
500 PushToOutputQueue(dev,0xaa,1);
504 KEYBOARD_DEBUG_PRINT("0x%x\n",data);
505 *((uchar_t*)dest)=data;
508 KEYBOARD_DEBUG_PRINT("keyboard: unknown size read from input (60h)\n");
514 int keyboard_interrupt(uint_t irq,
515 struct vm_device * dev)
517 KEYBOARD_DEBUG_PRINT("keyboard: interrupt\n");
519 dev->vm->vm_ops.raise_irq(dev->vm,irq);
526 int keyboard_init_device(struct vm_device * dev)
529 // struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
531 KEYBOARD_DEBUG_PRINT("keyboard: init_device\n");
533 // Would read state here
535 keyboard_reset_device(dev);
538 dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
539 dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
541 #if KEYBOARD_DEBUG_80H
542 dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
547 // We do not hook the IRQ here. Instead, the underlying device driver
548 // is responsible to call us back
554 int keyboard_deinit_device(struct vm_device *dev)
557 dev_unhook_io(dev, KEYBOARD_60H);
558 dev_unhook_io(dev, KEYBOARD_64H);
559 #if KEYBOARD_DEBUG_80H
560 dev_unhook_io(dev, KEYBOARD_DELAY_80H);
562 keyboard_reset_device(dev);
570 static struct vm_device_ops dev_ops = {
571 .init = keyboard_init_device,
572 .deinit = keyboard_deinit_device,
573 .reset = keyboard_reset_device,
574 .start = keyboard_start_device,
575 .stop = keyboard_stop_device,
581 struct vm_device *create_keyboard() {
583 if (thekeyboard!=NULL) {
584 KEYBOARD_DEBUG_PRINT("keyboard: creating >1 keyboard device. This will probably fail!\n");
587 struct keyboard_internal * keyboard_state = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
589 struct vm_device *device = create_device("KEYBOARD", &dev_ops, keyboard_state);