2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Peter Dinda <pdinda@northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Peter Dinda <pdinda@northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <devices/keyboard.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_types.h>
24 #include <palacios/vmm_lock.h>
27 #ifndef DEBUG_KEYBOARD
29 #define PrintDebug(fmt, args...)
32 #define KEYBOARD_DEBUG_80H 0
36 #define KEYBOARD_60H 0x60 // keyboard microcontroller
37 #define KEYBOARD_64H 0x64 // onboard microcontroller
39 #define KEYBOARD_DELAY_80H 0x80 // written for timing
41 #define KEYBOARD_IRQ 0x1
45 // extract bits for status byte
46 #define STATUS_OUTPUT_BUFFER_FULL 0x01 // 1=full (data for system)
47 #define STATUS_INPUT_BUFFER_FULL 0x02 // 1=full (data for 8042)
48 #define STATUS_SYSTEM 0x04 // 1=self-test-passed
49 #define STATUS_COMMAND_DATA_AVAIL 0x08 // internal: 0=data on 60h, 0=cmd on 64h
50 #define STATUS_ENABLED 0x10 // 1=keyboard is enabled
51 #define STATUS_MOUSE_BUFFER_FULL 0x20 // 1= mouse output buffer full
52 #define STATUS_TIMEOUT 0x40 // 1=timeout of keybd
53 #define STATUS_PARITY 0x80 // 1=parity error
57 #define CMD_INTR 0x01 // 1=interrupts enabled
58 #define CMD_MOUSE_INTR 0x02 // 1=interrupts enabled for mouse
59 #define CMD_SYSTEM 0x04 // 1= self test passed
60 #define CMD_OVERRIDE 0x08 // FORCE 0 for PS2
61 #define CMD_DISABLE 0x10 // 1=disabled keyboard
62 #define CMD_MOUSE_DISABLE 0x20 // 1=disabled mouse
63 #define CMD_SCANCODE_XLATE 0x40 // 1=translate to set 1 scancodes
64 #define CMD_RESERVED 0x80 // should be zero
66 // bits for the output port
69 #define OUTPUT_RESET 0x01 // System reset on 0
70 #define OUTPUT_A20 0x02 // A20 gate (1= A20 is gated)
71 #define OUTPUT_RES1 0x04 // reserved
72 #define OUTPUT_RES2 0x08 // reserved
73 #define OUTPUT_OUTPUT_FULL 0x10 // output buffer full
74 #define OUTPUT_INPUT_EMPTY 0x20 // input buffer empty
75 #define OUTPUT_KBD_CLOCK 0x40 // keyboard clock (?)
76 #define OUTPUT_KBD_DATA 0x80 // keyboard data
78 // bits for the input port
80 #define INPUT_RES0 0x01 // reserved
81 #define INPUT_RES1 0x02 // reserved
82 #define INPUT_RES2 0x04 // reserved
83 #define INPUT_RES3 0x08 // reserved
84 #define INPUT_RAM 0x10 // set to 1 if RAM exists?
85 #define INPUT_JUMPER 0x20 // manufacturing jumper?
86 #define INPUT_DISPLAY 0x40 // 0=color, 1=mono
87 #define INPUT_KBD_INHIBIT 0x80 // 1=inhibit keyboard ?
90 // for queue operations
94 // for queue operations - whether it's data or cmd waiting on 60h
98 // for queue operations - whether this is keyboard or mouse data on 60h
104 //#define QUEUE_SIZE 32
107 struct keyboard_internal {
109 // 0x60 is the port for the keyboard microcontroller
110 // writes are commands
111 // reads from it usually return scancodes
112 // however, it can also return other data
113 // depending on the state of the onboard microcontroller
115 // 0x64 is the port for the onboard microcontroller
116 // writes are commands
120 // state of the onboard microcontroller
121 // this is needed because sometimes 0x60 reads come
122 // from the onboard microcontroller
123 enum {// Normal mode measn we deliver keys
124 // to the vm and accept commands from it
126 // after receiving cmd 0x60
127 // keybaord uC cmd will subsequently arrive
129 // after recieving 0xa5
130 // password arrives on data port, null terminated
132 // after having reset sent to 0x60
133 // we immediately ack, and then
134 // push BAT success (0xaa) after the ack
136 // after having a d1 sent to 64
137 // we wait for a new output byte on 60
139 // after having a d2 sent to 64
140 // we wait for a new output byte on 60
141 // then make it available as a keystroke
143 // after having a d3 sent to 64
144 // we wait for a new output byte on 60
145 // then make it available as a mouse event
147 // after having a d4 sent to 64
148 // we wait for a new output byte on 60
149 // then send it to the mouse
151 // After the Keyboard LEDs are enabled
152 // we wait for the output byte on 64?
154 // After the Keyboard SET_RATE is called
155 // we wait for the output byte on 64?
157 // The keyboard requests an ID which
158 // generates 3 bytes of output...
167 // after receiving a mouse command 0f 0xff
168 // we return the ack and then the next thing will be the
169 // bat code (aa - success)
171 // followed by the device id (00 - mouse)
173 // Then it goes into stream mode
176 STREAM3, // for each of the following bytes in mouse_packet
177 // this is used for setting sample rate
179 // this is used for getting device id
181 // just like the stream moes
185 // For getting status info
195 uchar_t mouse_packet[3]; // byte 1: y over, xover, y sign, x sign, 1, middle, right, left
196 // byte 2: x movement
197 // byte 3: y movement
199 uchar_t mouse_needs_ack; //
200 uchar_t mouse_done_after_ack;
202 uchar_t cmd_byte; // for keyboard uC - read/written
203 // via read/write cmd byte command
204 uchar_t status_byte; // for on-board uC - read via 64h
206 uchar_t output_byte; // output port of onboard uC (e.g. A20)
208 uchar_t input_byte; // input port of onboard uC
211 uchar_t output_queue; //
212 uint_t output_queue_len; //
213 //uint_t output_queue_read;
214 //uint_t output_queue_write;
221 static int keyboard_interrupt(struct vm_device * dev, uint_t irq) {
222 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
224 PrintDebug("keyboard: interrupt 0x%x\n", irq);
226 if (state->cmd_byte & CMD_INTR) {
227 v3_raise_irq(dev->vm, irq);
236 // push item onto outputqueue, optionally overwriting if there is no room
237 // returns 0 if successful
239 static int PushToOutputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite, uchar_t cmd, uchar_t mouse)
241 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
243 if ((state->output_queue_len == 0) || overwrite) {
245 state->output_queue = value;
246 state->output_queue_len = 1;
249 state->status_byte |= STATUS_COMMAND_DATA_AVAIL;
251 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
255 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
256 keyboard_interrupt(dev, MOUSE_IRQ);
258 keyboard_interrupt(dev, KEYBOARD_IRQ);
261 state->status_byte |= STATUS_OUTPUT_BUFFER_FULL;
266 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
273 // pull item from outputqueue
274 // returns 0 if successful
276 static int PullFromOutputQueue(struct vm_device * dev, uchar_t * value)
278 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
280 if (1 || (state->output_queue_len == 1)) {
282 *value = state->output_queue;
283 state->output_queue_len = 0;
285 if (state->status_byte & STATUS_OUTPUT_BUFFER_FULL) {
286 state->status_byte &= ~STATUS_OUTPUT_BUFFER_FULL;
289 if (state->status_byte & STATUS_MOUSE_BUFFER_FULL) {
290 state->status_byte &= ~STATUS_MOUSE_BUFFER_FULL;
293 if (state->status_byte & STATUS_COMMAND_DATA_AVAIL) {
294 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
300 PrintError("keyboard: PullFromOutputQueue Failed - Queue Empty\n");
308 static int key_event_handler(struct guest_info * info,
309 struct v3_keyboard_event * evt,
310 void * private_data) {
311 struct vm_device * dev = (struct vm_device *)private_data;
312 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
314 PrintDebug("keyboard: injected status 0x%x, and scancode 0x%x\n", evt->status, evt->scan_code);
316 if (evt->scan_code == 0x44) { // F10 debug dump
317 v3_print_guest_state(info);
318 // PrintGuestPageTables(info, info->shdw_pg_state.guest_cr3);
321 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
323 if ( (state->status_byte & STATUS_ENABLED) // onboard is enabled
324 && (!(state->cmd_byte & CMD_DISABLE)) ) { // keyboard is enabled
326 PushToOutputQueue(dev, evt->scan_code, OVERWRITE, DATA, KEYBOARD);
329 v3_unlock_irqrestore(state->kb_lock, irq_state);
335 static int mouse_event_handler(struct guest_info * info,
336 struct v3_mouse_event * evt,
337 void * private_data) {
338 struct vm_device * dev = (struct vm_device *)private_data;
339 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
341 PrintDebug("keyboard: injected mouse packet 0x %x %x %x\n",
342 evt->data[0], evt->data[1], evt->data[2]);
344 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
346 memcpy(state->mouse_packet, evt->data, 3);
348 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
351 switch (state->mouse_state) {
355 if (!(state->cmd_byte & CMD_MOUSE_DISABLE)) {
356 keyboard_interrupt(dev, MOUSE_IRQ);
360 PrintError("Invalid mouse state\n");
366 v3_unlock_irqrestore(state->kb_lock, irq_state);
372 static int keyboard_reset_device(struct vm_device * dev)
374 struct keyboard_internal *data = (struct keyboard_internal *)(dev->private_data);
376 memset(data, 0, sizeof(struct keyboard_internal));
378 data->state = NORMAL;
379 data->mouse_state = STREAM1;
382 CMD_INTR // interrupts on
383 | CMD_MOUSE_INTR // mouse interupts on
384 | CMD_SYSTEM ; // self test passed
385 // PS2, keyboard+mouse enabled, generic translation
388 STATUS_SYSTEM // self-tests passed
389 | STATUS_ENABLED ; // keyboard ready
390 // buffers empty, no errors
392 data->output_byte = 0; // ?
394 data->input_byte = INPUT_RAM; // we have some
395 // also display=color, jumper 0, keyboard enabled
399 PrintDebug("keyboard: reset device\n");
407 static int keyboard_start_device(struct vm_device *dev)
409 PrintDebug("keyboard: start device\n");
414 static int keyboard_stop_device(struct vm_device *dev)
416 PrintDebug("keyboard: stop device\n");
421 static int mouse_read_input(struct vm_device *dev)
423 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
425 if (state->mouse_needs_ack) {
426 state->mouse_needs_ack = 0;
428 // the ack has been stuffed previously
429 if (state->mouse_done_after_ack) {
436 switch (state->mouse_state) {
438 case RESET1: // requesting the BAT code
439 PushToOutputQueue(dev, 0xaa, OVERWRITE, DATA, MOUSE) ; // BAT successful
440 PrintDebug(" mouse sent BAT code (sucesssful) ");
441 state->mouse_state = RESET2;
443 return 0; // not done with mouse processing yet
446 case RESET2: // requesting the device id
447 PushToOutputQueue(dev, 0x00, OVERWRITE, DATA, MOUSE) ; // normal mouse type
448 PrintDebug(" mouse sent device id ");
449 state->mouse_state = STREAM1;
451 return 1; // done with mouse processing
454 case STREAM1: // send data
455 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
456 PrintDebug(" mouse sent stream data1 ");
457 state->mouse_state = STREAM2;
462 case STREAM2: // send data
463 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
464 PrintDebug(" mouse sent stream data2 ");
465 state->mouse_state = STREAM3;
470 case STREAM3: // send data
471 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
472 PrintDebug(" mouse sent stream data3 ");
473 state->mouse_state = STREAM1;
475 return 1; // now done
478 case REMOTE1: // send data
479 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
480 PrintDebug(" mouse sent remote data1 ");
481 state->mouse_state = REMOTE2;
486 case REMOTE2: // send data
487 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
488 PrintDebug(" mouse sent remote data2 ");
489 state->mouse_state = REMOTE3;
494 case REMOTE3: // send data
495 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
496 PrintDebug(" mouse sent remote data3 ");
497 state->mouse_state = REMOTE1;
499 return 1; // now done
502 case STATUS1: // send data
503 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
504 PrintDebug(" mouse sent status data1 ");
505 state->mouse_state = STATUS2;
510 case STATUS2: // send data
511 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
512 PrintDebug(" mouse sent status data2 ");
513 state->mouse_state = STATUS3;
518 case STATUS3: // send data
519 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
520 PrintDebug(" mouse sent status data3 ");
521 state->mouse_state = STREAM1;
523 return 1; // now done
526 case DEVICE1: // send device id
527 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
528 PrintDebug(" mouse sent device id ");
529 state->mouse_state = STREAM1;
531 return 1; // now done
535 PrintDebug(" mouse has no data ");
541 static int mouse_write_output(struct vm_device * dev, uchar_t data)
543 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
545 switch (state->mouse_state) {
555 PushToOutputQueue(dev, 0xfe, OVERWRITE, DATA, MOUSE) ; // no mouse!
556 PrintDebug(" mouse reset begins (no mouse) ");
558 return 1; // not done;
563 PushToOutputQueue(dev,0xfa,OVERWRITE,DATA,MOUSE) ;
564 PrintDebug(" mouse reset begins ");
565 state->mouse_done_after_ack=0;
566 state->mouse_needs_ack=1;
567 state->mouse_state=RESET1;
568 return 0; // not done;
572 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
573 PrintDebug(" mouse resend begins ");
574 state->mouse_done_after_ack = 0;
575 state->mouse_needs_ack = 0;
576 state->mouse_state = STREAM1;
577 return 0; // not done
580 case 0xf6: // set defaults
581 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
582 PrintDebug(" mouse set defaults ");
583 state->mouse_done_after_ack = 1;
584 state->mouse_needs_ack = 1;
585 state->mouse_state = STREAM1;
586 return 0; // not done
589 case 0xf5: // disable data reporting
590 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
591 state->mouse_done_after_ack = 1;
592 state->mouse_needs_ack = 1;
593 PrintDebug(" mouse disable data reporting ");
594 state->mouse_state = STREAM1;
595 return 0; // not done
598 case 0xf4: // enable data reporting
599 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
600 state->mouse_done_after_ack = 1;
601 state->mouse_needs_ack = 1;
602 PrintDebug(" mouse enable data reporting ");
603 state->mouse_state = STREAM1;
604 return 0; // not done
607 case 0xf3: // set sample rate
608 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
609 state->mouse_done_after_ack = 0;
610 state->mouse_needs_ack = 1;
611 PrintDebug(" mouse set sample rate begins ");
612 state->mouse_state = SAMPLE1;
613 return 0; // not done
616 case 0xf2: // get device id
617 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
618 state->mouse_done_after_ack = 0;
619 state->mouse_needs_ack = 1;
620 PrintDebug(" mouse get device id begins ");
621 state->mouse_state = DEVICE1;
622 return 0; // not done
625 case 0xf0: // set remote mode
626 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
627 state->mouse_done_after_ack = 1;
628 state->mouse_needs_ack = 1;
629 PrintDebug(" mouse set remote mode ");
630 state->mouse_state = REMOTE1;
631 return 0; // not done
634 case 0xee: // set wrap mode
635 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
636 state->mouse_done_after_ack = 1;
637 state->mouse_needs_ack = 1;
638 PrintDebug(" mouse set wrap mode (ignored) ");
639 state->mouse_state = STREAM1;
640 return 0; // not done
643 case 0xec: // reset wrap mode
644 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
645 state->mouse_done_after_ack = 1;
646 state->mouse_needs_ack = 1;
647 PrintDebug(" mouse reset wrap mode (ignored) ");
648 state->mouse_state = STREAM1;
652 case 0xeb: // read data
653 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
654 state->mouse_done_after_ack = 0;
655 state->mouse_needs_ack = 1;
656 PrintDebug(" mouse switch to wrap mode (ignored) ");
657 state->mouse_state = REMOTE1;
658 return 0; // not done
661 case 0xea: // set stream mode
662 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
663 state->mouse_done_after_ack = 1;
664 state->mouse_needs_ack = 1;
665 PrintDebug(" mouse set stream mode ");
666 state->mouse_state = STREAM1;
667 return 0; // not done
670 case 0xe9: // status request
671 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
672 state->mouse_done_after_ack = 0;
673 state->mouse_needs_ack = 1;
674 PrintDebug(" mouse status request begins ");
675 state->mouse_state = STATUS1;
679 case 0xe8: // set resolution
680 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
681 state->mouse_done_after_ack = 0;
682 state->mouse_needs_ack = 1;
683 PrintDebug(" mouse set resolution begins ");
684 state->mouse_state = SETRES1;
688 case 0xe7: // set scaling 2:1
689 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
690 state->mouse_done_after_ack = 1;
691 state->mouse_needs_ack = 1;
692 PrintDebug(" mouse set scaling 2:1 ");
693 state->mouse_state = STREAM1;
694 return 0; // not done
697 case 0xe6: // set scaling 1:1
698 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
699 state->mouse_done_after_ack = 1;
700 state->mouse_needs_ack = 1;
701 PrintDebug(" mouse set scaling 1:1 ");
702 state->mouse_state = STREAM1;
707 PrintDebug(" receiving unknown mouse command (0x%x) in acceptable state ", data);
714 PrintDebug(" receiving mouse output in unhandled state (0x%x) ", state->mouse_state);
720 PrintDebug(" HUH? ");
726 #if KEYBOARD_DEBUG_80H
727 static int keyboard_write_delay(ushort_t port,
730 struct vm_device * dev)
734 PrintDebug("keyboard: write of 0x%x to 80h\n", *((uchar_t*)src));
738 PrintDebug("keyboard: write of >1 byte to 80h\n", *((uchar_t*)src));
744 static int keyboard_read_delay(ushort_t port,
747 struct vm_device * dev)
751 *((uchar_t*)dest) = v3_inb(port);
753 PrintDebug("keyboard: read of 0x%x from 80h\n", *((uchar_t*)dest));
757 PrintDebug("keyboard: read of >1 byte from 80h\n");
768 static int keyboard_write_command(ushort_t port,
771 struct vm_device * dev)
773 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
776 // Should always be single byte write
779 PrintError("keyboard: write of >1 bytes (%d) to 64h\n", length);
783 cmd = *((uchar_t*)src);
786 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
788 if (state->state != NORMAL) {
789 PrintDebug("keyboard: warning - receiving command on 64h but state != NORMAL\n");
792 PrintDebug("keyboard: command 0x%x on 64h\n", cmd);
796 case 0x20: // READ COMMAND BYTE (returned in 60h)
797 PushToOutputQueue(dev, state->cmd_byte, OVERWRITE, COMMAND,KEYBOARD);
798 state->state = NORMAL; // the next read on 0x60 will get the right data
799 PrintDebug("keyboard: command byte 0x%x returned\n", state->cmd_byte);
802 case 0x60: // WRITE COMMAND BYTE (read from 60h)
803 state->state = WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
804 PrintDebug("keyboard: prepare to write command byte\n");
807 // case 0x90-9f - write to output port (?)
809 case 0xa1: // Get version number
810 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
811 state->state = NORMAL;
812 PrintDebug("keyboard: version number 0x0 returned\n");
815 case 0xa4: // is password installed? send result to 0x60
816 // we don't support passwords
817 PushToOutputQueue(dev, 0xf1, OVERWRITE, COMMAND, KEYBOARD);
818 PrintDebug("keyboard: password not installed\n");
819 state->state = NORMAL;
822 case 0xa5: // new password will arrive on 0x60
823 state->state = TRANSMIT_PASSWD;
824 PrintDebug("keyboard: pepare to transmit password\n");
827 case 0xa6: // check passwd;
828 // since we do not support passwords, we will simply ignore this
829 // the implication is that any password check immediately succeeds
830 // with a blank password
831 state->state = NORMAL;
832 PrintDebug("keyboard: password check succeeded\n");
835 case 0xa7: // disable mouse
836 state->cmd_byte |= CMD_MOUSE_DISABLE;
837 state->state = NORMAL;
838 PrintDebug("keyboard: mouse disabled\n");
841 case 0xa8: // enable mouse
842 state->cmd_byte &= ~CMD_MOUSE_DISABLE;
843 state->state = NORMAL;
844 PrintDebug("keyboard: mouse enabled\n");
847 case 0xa9: // mouse interface test (always succeeds)
848 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
849 PrintDebug("keyboard: mouse interface test succeeded\n");
850 state->state = NORMAL;
853 case 0xaa: // controller self test (always succeeds)
854 PushToOutputQueue(dev, 0x55, OVERWRITE, COMMAND, KEYBOARD);
855 PrintDebug("keyboard: controller self test succeeded\n");
856 state->state = NORMAL;
859 case 0xab: // keyboard interface test (always succeeds)
860 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
861 state->state = NORMAL;
862 PrintDebug("keyboard: keyboard interface test succeeded\n");
865 case 0xad: // disable keyboard
866 state->cmd_byte |= CMD_DISABLE;
867 state->state = NORMAL;
868 PrintDebug("keyboard: keyboard disabled\n");
871 case 0xae: // enable keyboard
872 state->cmd_byte &= ~CMD_DISABLE;
873 state->state = NORMAL;
874 PrintDebug("keyboard: keyboard enabled\n");
877 case 0xaf: // get version
878 PushToOutputQueue(dev, 0x00, OVERWRITE, COMMAND, KEYBOARD);
879 state->state = NORMAL;
880 PrintDebug("keyboard: version 0 returned \n");
883 case 0xd0: // return microcontroller output on 60h
884 PushToOutputQueue(dev, state->output_byte, OVERWRITE, COMMAND, KEYBOARD);
885 state->state = NORMAL;
886 PrintDebug("keyboard: output byte 0x%x returned\n", state->output_byte);
889 case 0xd1: // request to write next byte on 60h to the microcontroller output port
890 state->state = WRITING_OUTPUT_PORT;
891 PrintDebug("keyboard: prepare to write output byte\n");
894 case 0xd2: // write keyboard buffer (inject key)
895 state->state = INJECTING_KEY;
896 PrintDebug("keyboard: prepare to inject key\n");
899 case 0xd3: // write mouse buffer (inject mouse)
900 state->state = INJECTING_MOUSE;
901 PrintDebug("keyboard: prepare to inject mouse\n");
904 case 0xd4: // write mouse device (command to mouse?)
905 state->state = IN_MOUSE;
906 PrintDebug("keyboard: prepare to inject mouse command\n");
909 case 0xc0: // read input port
910 PushToOutputQueue(dev, state->input_byte, OVERWRITE, COMMAND, KEYBOARD);
911 state->state = NORMAL;
912 PrintDebug("keyboard: input byte 0x%x returned\n", state->input_byte);
915 case 0xc1: //copy input port lsn to status msn
916 state->status_byte &= 0x0f;
917 state->status_byte |= (state->input_byte & 0xf) << 4;
918 state->state = NORMAL;
919 PrintDebug("keyboard: copied input byte lsn to status msn\n");
922 case 0xc2: // copy input port msn to status msn
923 state->status_byte &= 0x0f;
924 state->status_byte |= (state->input_byte & 0xf0);
925 state->state = NORMAL;
926 PrintDebug("keyboard: copied input byte msn to status msn\n");
929 case 0xe0: // read test port
930 PushToOutputQueue(dev, state->output_byte >> 6, OVERWRITE, COMMAND, KEYBOARD);
931 state->state = NORMAL;
932 PrintDebug("keyboard: read 0x%x from test port\n", state->output_byte >> 6);
936 case 0xf0: // pulse output port
937 case 0xf1: // this should pulse 0..3 of cmd_byte on output port
938 case 0xf2: // instead of what is currently in output_byte (I think)
939 case 0xf3: // main effect is taht if bit zero is zero
940 case 0xf4: // should cause reset
941 case 0xf5: // I doubt anything more recent than a 286 running
942 case 0xf6: // OS2 with the penalty box will care
953 PrintDebug("keyboard: ignoring pulse of 0x%x (low=pulsed) on output port\n", (cmd & 0xf));
954 state->state = NORMAL;
958 // case ac diagonstic - returns 16 bytes from keyboard microcontroler on 60h
960 PrintDebug("keyboard: ignoring command (unimplemented)\n");
961 state->state = NORMAL;
965 v3_unlock_irqrestore(state->kb_lock, irq_state);
970 static int keyboard_read_status(ushort_t port,
973 struct vm_device * dev)
975 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
978 PrintError("keyboard: >1 byte read for status (64h)\n");
983 PrintDebug("keyboard: read status (64h): ");
985 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
986 *((uchar_t*)dest) = state->status_byte;
987 v3_unlock_irqrestore(state->kb_lock, irq_state);
989 PrintDebug("0x%x\n", *((uchar_t*)dest));
994 static int keyboard_write_output(ushort_t port,
997 struct vm_device * dev)
999 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1003 PrintError("keyboard: write of 60h with >1 byte\n");
1007 uchar_t data = *((uchar_t*)src);
1009 PrintDebug("keyboard: output 0x%x on 60h\n", data);
1011 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
1013 switch (state->state) {
1014 case WRITING_CMD_BYTE:
1015 state->cmd_byte = data;
1016 state->state = NORMAL;
1017 PrintDebug("keyboard: wrote new command byte 0x%x\n", state->cmd_byte);
1019 case WRITING_OUTPUT_PORT:
1020 state->output_byte = data;
1021 state->state = NORMAL;
1022 PrintDebug("keyboard: wrote new output byte 0x%x\n", state->output_byte);
1025 PushToOutputQueue(dev, data, OVERWRITE, COMMAND, KEYBOARD); // probably should be a call to deliver_key_to_vmm()
1026 state->state = NORMAL;
1027 PrintDebug("keyboard: injected key 0x%x\n", data);
1029 case INJECTING_MOUSE:
1030 PushToOutputQueue(dev, data, OVERWRITE, DATA, MOUSE);
1031 // PrintDebug("keyboard: ignoring injected mouse event 0x%x\n", data);
1032 PrintDebug("keyboard: injected mouse event 0x%x\n", data);
1033 state->state = NORMAL;
1036 PrintDebug("keyboard: mouse action: ");
1037 if (mouse_write_output(dev, data)) {
1038 state->state = NORMAL;
1042 case TRANSMIT_PASSWD:
1045 PrintDebug("keyboard: ignoring password character 0x%x\n",data);
1048 state->state = NORMAL;
1049 PrintDebug("keyboard: done with password\n");
1053 PrintDebug("Keyboard: LEDs being set...\n");
1054 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1055 state->state = NORMAL;
1058 PrintDebug("Keyboard: Rate being set...\n");
1059 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1060 state->state = NORMAL;
1065 // command is being sent to keyboard controller
1068 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD); // ack
1069 state->state = RESET;
1070 PrintDebug("keyboard: reset complete and acked\n");
1072 case 0xf5: // disable scanning
1073 case 0xf4: // enable scanning
1075 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1076 // should do something here... PAD
1077 state->state = NORMAL;
1078 PrintDebug("keyboard: %s scanning done and acked\n", (data == 0xf5) ? "disable" : "enable");
1081 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1082 state->state = SET_RATE;
1084 case 0xf2: // get keyboard ID
1085 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1086 state->state = KBD_ID1;
1087 PrintDebug("Keyboard: Requesting Keyboard ID\n");
1089 //PushToOutputQueue(dev, 0xab, OVERWRITE, COMMAND, KEYBOARD);
1090 //state->state = KBD_ID2;
1092 case 0xed: // enable keyboard LEDs
1093 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1094 state->state = SET_LEDS;
1096 case 0xfe: // resend
1097 case 0xfd: // set key type make
1098 case 0xfc: // set key typ make/break
1099 case 0xfb: // set key type typematic
1100 case 0xfa: // set all typematic make/break/typematic
1101 case 0xf9: // set all make
1102 case 0xf8: // set all make/break
1103 case 0xf7: // set all typemaktic
1104 case 0xf6: // set defaults
1105 PrintError("keyboard: unhandled known command 0x%x on output buffer (60h)\n", data);
1110 PrintError("keyboard: unhandled unknown command 0x%x on output buffer (60h)\n", data);
1111 state->status_byte |= 0x1;
1121 v3_unlock_irqrestore(state->kb_lock, irq_state);
1130 static int keyboard_read_input(ushort_t port,
1133 struct vm_device * dev)
1138 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1141 PrintError("keyboard: unknown size read from input (60h)\n");
1145 PrintDebug("keyboard: read from input (60h): ");
1147 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
1149 if (state->state == IN_MOUSE) {
1150 done_mouse = mouse_read_input(dev);
1152 state->state = NORMAL;
1156 PullFromOutputQueue(dev, &data);
1158 if (state->state == RESET) {
1159 // We just delivered the ack for the reset
1160 // now we will ready ourselves to deliver the BAT code (success)
1161 PushToOutputQueue(dev, 0xaa, OVERWRITE, COMMAND, KEYBOARD);
1162 state->state = NORMAL;
1163 PrintDebug(" (in reset, pushing BAT test code 0xaa) ");
1164 } else if (state->state == KBD_ID1) {
1165 PushToOutputQueue(dev, 0xab, OVERWRITE, COMMAND, KEYBOARD);
1166 state->state = KBD_ID2;
1167 PrintDebug(" (in kbd id request, pushing 1st ID val) ");
1168 } else if (state->state == KBD_ID2) {
1169 PushToOutputQueue(dev, 0x83, OVERWRITE, COMMAND, KEYBOARD);
1170 state->state = NORMAL;
1171 PrintDebug(" (in kbd id request, pushing 2nd ID val) ");
1174 v3_unlock_irqrestore(state->kb_lock, irq_state);
1176 PrintDebug("0x%x\n", data);
1178 *((uchar_t*)dest) = data;
1188 static int keyboard_init_device(struct vm_device * dev)
1191 struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
1193 PrintDebug("keyboard: init_device\n");
1195 keyboard_reset_device(dev);
1198 v3_lock_init(&(data->kb_lock));
1202 v3_dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
1203 v3_dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
1205 v3_hook_host_event(dev->vm, HOST_KEYBOARD_EVT, V3_HOST_EVENT_HANDLER(key_event_handler), dev);
1206 v3_hook_host_event(dev->vm, HOST_MOUSE_EVT, V3_HOST_EVENT_HANDLER(mouse_event_handler), dev);
1209 #if KEYBOARD_DEBUG_80H
1210 v3_dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
1215 // We do not hook the IRQ here. Instead, the underlying device driver
1216 // is responsible to call us back
1222 static int keyboard_deinit_device(struct vm_device *dev)
1225 v3_dev_unhook_io(dev, KEYBOARD_60H);
1226 v3_dev_unhook_io(dev, KEYBOARD_64H);
1227 #if KEYBOARD_DEBUG_80H
1228 v3_dev_unhook_io(dev, KEYBOARD_DELAY_80H);
1230 keyboard_reset_device(dev);
1238 static struct vm_device_ops dev_ops = {
1239 .init = keyboard_init_device,
1240 .deinit = keyboard_deinit_device,
1241 .reset = keyboard_reset_device,
1242 .start = keyboard_start_device,
1243 .stop = keyboard_stop_device,
1249 struct vm_device * v3_create_keyboard() {
1250 struct keyboard_internal * keyboard_state = NULL;
1252 keyboard_state = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
1254 struct vm_device *device = v3_create_device("KEYBOARD", &dev_ops, keyboard_state);