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>
25 #ifndef DEBUG_KEYBOARD
27 #define PrintDebug(fmt, args...)
30 #define KEYBOARD_DEBUG_80H 0
34 #define KEYBOARD_60H 0x60 // keyboard microcontroller
35 #define KEYBOARD_64H 0x64 // onboard microcontroller
37 #define KEYBOARD_DELAY_80H 0x80 // written for timing
39 #define KEYBOARD_IRQ 0x1
43 // extract bits for status byte
44 #define STATUS_OUTPUT_BUFFER_FULL 0x01 // 1=full (data for system)
45 #define STATUS_INPUT_BUFFER_FULL 0x02 // 1=full (data for 8042)
46 #define STATUS_SYSTEM 0x04 // 1=self-test-passed
47 #define STATUS_COMMAND_DATA_AVAIL 0x08 // internal: 0=data on 60h, 0=cmd on 64h
48 #define STATUS_ENABLED 0x10 // 1=keyboard is enabled
49 #define STATUS_MOUSE_BUFFER_FULL 0x20 // 1= mouse output buffer full
50 #define STATUS_TIMEOUT 0x40 // 1=timeout of keybd
51 #define STATUS_PARITY 0x80 // 1=parity error
55 #define CMD_INTR 0x01 // 1=interrupts enabled
56 #define CMD_MOUSE_INTR 0x02 // 1=interrupts enabled for mouse
57 #define CMD_SYSTEM 0x04 // 1= self test passed
58 #define CMD_OVERRIDE 0x08 // FORCE 0 for PS2
59 #define CMD_DISABLE 0x10 // 1=disabled keyboard
60 #define CMD_MOUSE_DISABLE 0x20 // 1=disabled mouse
61 #define CMD_SCANCODE_XLATE 0x40 // 1=translate to set 1 scancodes
62 #define CMD_RESERVED 0x80 // should be zero
64 // bits for the output port
67 #define OUTPUT_RESET 0x01 // System reset on 0
68 #define OUTPUT_A20 0x02 // A20 gate (1= A20 is gated)
69 #define OUTPUT_RES1 0x04 // reserved
70 #define OUTPUT_RES2 0x08 // reserved
71 #define OUTPUT_OUTPUT_FULL 0x10 // output buffer full
72 #define OUTPUT_INPUT_EMPTY 0x20 // input buffer empty
73 #define OUTPUT_KBD_CLOCK 0x40 // keyboard clock (?)
74 #define OUTPUT_KBD_DATA 0x80 // keyboard data
76 // bits for the input port
78 #define INPUT_RES0 0x01 // reserved
79 #define INPUT_RES1 0x02 // reserved
80 #define INPUT_RES2 0x04 // reserved
81 #define INPUT_RES3 0x08 // reserved
82 #define INPUT_RAM 0x10 // set to 1 if RAM exists?
83 #define INPUT_JUMPER 0x20 // manufacturing jumper?
84 #define INPUT_DISPLAY 0x40 // 0=color, 1=mono
85 #define INPUT_KBD_INHIBIT 0x80 // 1=inhibit keyboard ?
88 // for queue operations
92 // for queue operations - whether it's data or cmd waiting on 60h
96 // for queue operations - whether this is keyboard or mouse data on 60h
102 //#define QUEUE_SIZE 32
105 struct keyboard_internal {
107 // 0x60 is the port for the keyboard microcontroller
108 // writes are commands
109 // reads from it usually return scancodes
110 // however, it can also return other data
111 // depending on the state of the onboard microcontroller
113 // 0x64 is the port for the onboard microcontroller
114 // writes are commands
118 // state of the onboard microcontroller
119 // this is needed because sometimes 0x60 reads come
120 // from the onboard microcontroller
121 enum {// Normal mode measn we deliver keys
122 // to the vm and accept commands from it
124 // after receiving cmd 0x60
125 // keybaord uC cmd will subsequently arrive
127 // after recieving 0xa5
128 // password arrives on data port, null terminated
130 // after having reset sent to 0x60
131 // we immediately ack, and then
132 // push BAT success (0xaa) after the ack
134 // after having a d1 sent to 64
135 // we wait for a new output byte on 60
137 // after having a d2 sent to 64
138 // we wait for a new output byte on 60
139 // then make it available as a keystroke
141 // after having a d3 sent to 64
142 // we wait for a new output byte on 60
143 // then make it available as a mouse event
145 // after having a d4 sent to 64
146 // we wait for a new output byte on 60
147 // then send it to the mouse
149 // After the Keyboard LEDs are enabled
150 // we wait for the output byte on 64?
152 // After the Keyboard SET_RATE is called
153 // we wait for the output byte on 64?
155 // The keyboard requests an ID which
156 // generates 3 bytes of output...
165 // after receiving a mouse command 0f 0xff
166 // we return the ack and then the next thing will be the
167 // bat code (aa - success)
169 // followed by the device id (00 - mouse)
171 // Then it goes into stream mode
174 STREAM3, // for each of the following bytes in mouse_packet
175 // this is used for setting sample rate
177 // this is used for getting device id
179 // just like the stream moes
183 // For getting status info
193 uchar_t mouse_packet[3]; // byte 1: y over, xover, y sign, x sign, 1, middle, right, left
194 // byte 2: x movement
195 // byte 3: y movement
197 uchar_t mouse_needs_ack; //
198 uchar_t mouse_done_after_ack;
200 uchar_t cmd_byte; // for keyboard uC - read/written
201 // via read/write cmd byte command
202 uchar_t status_byte; // for on-board uC - read via 64h
204 uchar_t output_byte; // output port of onboard uC (e.g. A20)
206 uchar_t input_byte; // input port of onboard uC
209 uchar_t input_queue; //
210 uint_t input_queue_len; //
211 //uint_t input_queue_read;
212 //uint_t input_queue_write;
214 uchar_t output_queue; //
215 uint_t output_queue_len; //
216 //uint_t output_queue_read;
217 //uint_t output_queue_write;
223 static int keyboard_interrupt(struct vm_device * dev, uint_t irq) {
224 PrintDebug("keyboard: interrupt 0x%x\n", irq);
226 v3_raise_irq(dev->vm, irq);
235 // push item onto outputqueue, optionally overwriting if there is no room
236 // returns 0 if successful
238 static int PushToOutputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite, uchar_t cmd, uchar_t mouse)
240 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
242 if ((state->output_queue_len == 0) || overwrite) {
244 state->output_queue = value;
245 state->output_queue_len = 1;
248 state->status_byte |= STATUS_COMMAND_DATA_AVAIL;
250 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
254 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
258 state->status_byte |= STATUS_OUTPUT_BUFFER_FULL;
261 if (state->cmd_byte & CMD_INTR) {
262 keyboard_interrupt(dev, KEYBOARD_IRQ);
268 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
275 // pull item from outputqueue
276 // returns 0 if successful
278 static int PullFromOutputQueue(struct vm_device * dev, uchar_t * value)
280 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
282 if (1 || (state->output_queue_len == 1)) {
284 *value = state->output_queue;
285 state->output_queue_len = 0;
287 if (state->status_byte & STATUS_OUTPUT_BUFFER_FULL) {
288 state->status_byte &= ~STATUS_OUTPUT_BUFFER_FULL;
291 if (state->status_byte & STATUS_MOUSE_BUFFER_FULL) {
292 state->status_byte &= ~STATUS_MOUSE_BUFFER_FULL;
295 if (state->status_byte & STATUS_COMMAND_DATA_AVAIL) {
296 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
302 PrintError("keyboard: PullFromOutputQueue Failed - Queue Empty\n");
310 // push item onto inputqueue, optionally overwriting if there is no room
311 // returns 0 if successful
313 static int PushToInputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite)
315 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
317 if ((state->input_queue_len == 0) || overwrite) {
319 state->input_queue = value;
320 state->input_queue_len = 1;
321 state->status_byte |= STATUS_INPUT_BUFFER_FULL;
325 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
331 // pull item from inputqueue
332 // returns 0 if successful
334 static int PullFromInputQueue(struct vm_device *dev, uchar_t *value)
336 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
338 if (state->input_queue_len == 1) {
340 *value = state->input_queue;
341 state->input_queue_len = 0;
342 state->status_byte &= ~STATUS_INPUT_BUFFER_FULL;
346 PrintError("keyboard: PullFromInputQueue Failed - Queue Empty\n");
359 static int key_event_handler(struct guest_info * info,
360 struct v3_keyboard_event * evt,
361 void * private_data) {
362 struct vm_device * dev = (struct vm_device *)private_data;
363 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
365 PrintDebug("keyboard: injected status 0x%x, and scancode 0x%x\n", evt->status, evt->scan_code);
367 if ( (state->status_byte & STATUS_ENABLED) // onboard is enabled
368 && (!(state->cmd_byte & CMD_DISABLE)) ) { // keyboard is enabled
370 PushToOutputQueue(dev, evt->scan_code, OVERWRITE, DATA, KEYBOARD);
372 if (state->cmd_byte & CMD_INTR) {
373 keyboard_interrupt(dev, KEYBOARD_IRQ);
381 static int mouse_event_handler(struct guest_info * info,
382 struct v3_mouse_event * evt,
383 void * private_data) {
384 struct vm_device * dev = (struct vm_device *)private_data;
385 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
387 PrintDebug("keyboard: injected mouse packet 0x %x %x %x\n",
388 evt->data[0], evt->data[1], evt->data[2]);
390 memcpy(state->mouse_packet, evt->data, 3);
392 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
395 switch (state->mouse_state) {
399 if (!(state->cmd_byte & CMD_MOUSE_DISABLE)) {
400 keyboard_interrupt(dev, MOUSE_IRQ);
412 static int keyboard_reset_device(struct vm_device * dev)
414 struct keyboard_internal *data = (struct keyboard_internal *)(dev->private_data);
416 memset(data, 0, sizeof(struct keyboard_internal));
418 data->state = NORMAL;
419 data->mouse_state = STREAM1;
422 CMD_INTR // interrupts on
423 | CMD_MOUSE_INTR // mouse interupts on
424 | CMD_SYSTEM ; // self test passed
425 // PS2, keyboard+mouse enabled, generic translation
428 STATUS_SYSTEM // self-tests passed
429 | STATUS_ENABLED ; // keyboard ready
430 // buffers empty, no errors
432 data->output_byte = 0; // ?
434 data->input_byte = INPUT_RAM; // we have some
435 // also display=color, jumper 0, keyboard enabled
439 PrintDebug("keyboard: reset device\n");
447 static int keyboard_start_device(struct vm_device *dev)
449 PrintDebug("keyboard: start device\n");
454 static int keyboard_stop_device(struct vm_device *dev)
456 PrintDebug("keyboard: stop device\n");
461 static int mouse_read_input(struct vm_device *dev)
463 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
465 if (state->mouse_needs_ack) {
466 state->mouse_needs_ack = 0;
468 // the ack has been stuffed previously
469 if (state->mouse_done_after_ack) {
476 switch (state->mouse_state) {
478 case RESET1: // requesting the BAT code
479 PushToOutputQueue(dev, 0xaa, OVERWRITE, DATA, MOUSE) ; // BAT successful
480 PrintDebug(" mouse sent BAT code (sucesssful) ");
481 state->mouse_state = RESET2;
483 return 0; // not done with mouse processing yet
486 case RESET2: // requesting the device id
487 PushToOutputQueue(dev, 0x00, OVERWRITE, DATA, MOUSE) ; // normal mouse type
488 PrintDebug(" mouse sent device id ");
489 state->mouse_state = STREAM1;
491 return 1; // done with mouse processing
494 case STREAM1: // send data
495 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
496 PrintDebug(" mouse sent stream data1 ");
497 state->mouse_state = STREAM2;
502 case STREAM2: // send data
503 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
504 PrintDebug(" mouse sent stream data2 ");
505 state->mouse_state = STREAM3;
510 case STREAM3: // send data
511 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
512 PrintDebug(" mouse sent stream data3 ");
513 state->mouse_state = STREAM1;
515 return 1; // now done
518 case REMOTE1: // send data
519 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
520 PrintDebug(" mouse sent remote data1 ");
521 state->mouse_state = REMOTE2;
526 case REMOTE2: // send data
527 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
528 PrintDebug(" mouse sent remote data2 ");
529 state->mouse_state = REMOTE3;
534 case REMOTE3: // send data
535 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
536 PrintDebug(" mouse sent remote data3 ");
537 state->mouse_state = REMOTE1;
539 return 1; // now done
542 case STATUS1: // send data
543 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
544 PrintDebug(" mouse sent status data1 ");
545 state->mouse_state = STATUS2;
550 case STATUS2: // send data
551 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
552 PrintDebug(" mouse sent status data2 ");
553 state->mouse_state = STATUS3;
558 case STATUS3: // send data
559 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
560 PrintDebug(" mouse sent status data3 ");
561 state->mouse_state = STREAM1;
563 return 1; // now done
566 case DEVICE1: // send device id
567 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
568 PrintDebug(" mouse sent device id ");
569 state->mouse_state = STREAM1;
571 return 1; // now done
575 PrintDebug(" mouse has no data ");
581 static int mouse_write_output(struct vm_device * dev, uchar_t data)
583 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
585 switch (state->mouse_state) {
595 PushToOutputQueue(dev, 0xfe, OVERWRITE, DATA, MOUSE) ; // no mouse!
596 PrintDebug(" mouse reset begins (no mouse) ");
598 return 1; // not done;
603 PushToOutputQueue(dev,0xfa,OVERWRITE,DATA,MOUSE) ;
604 PrintDebug(" mouse reset begins ");
605 state->mouse_done_after_ack=0;
606 state->mouse_needs_ack=1;
607 state->mouse_state=RESET1;
608 return 0; // not done;
612 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
613 PrintDebug(" mouse resend begins ");
614 state->mouse_done_after_ack = 0;
615 state->mouse_needs_ack = 0;
616 state->mouse_state = STREAM1;
617 return 0; // not done
620 case 0xf6: // set defaults
621 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
622 PrintDebug(" mouse set defaults ");
623 state->mouse_done_after_ack = 1;
624 state->mouse_needs_ack = 1;
625 state->mouse_state = STREAM1;
626 return 0; // not done
629 case 0xf5: // disable data reporting
630 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
631 state->mouse_done_after_ack = 1;
632 state->mouse_needs_ack = 1;
633 PrintDebug(" mouse disable data reporting ");
634 state->mouse_state = STREAM1;
635 return 0; // not done
638 case 0xf4: // enable data reporting
639 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
640 state->mouse_done_after_ack = 1;
641 state->mouse_needs_ack = 1;
642 PrintDebug(" mouse enable data reporting ");
643 state->mouse_state = STREAM1;
644 return 0; // not done
647 case 0xf3: // set sample rate
648 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
649 state->mouse_done_after_ack = 0;
650 state->mouse_needs_ack = 1;
651 PrintDebug(" mouse set sample rate begins ");
652 state->mouse_state = SAMPLE1;
653 return 0; // not done
656 case 0xf2: // get device id
657 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
658 state->mouse_done_after_ack = 0;
659 state->mouse_needs_ack = 1;
660 PrintDebug(" mouse get device id begins ");
661 state->mouse_state = DEVICE1;
662 return 0; // not done
665 case 0xf0: // set remote mode
666 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
667 state->mouse_done_after_ack = 1;
668 state->mouse_needs_ack = 1;
669 PrintDebug(" mouse set remote mode ");
670 state->mouse_state = REMOTE1;
671 return 0; // not done
674 case 0xee: // set wrap mode
675 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
676 state->mouse_done_after_ack = 1;
677 state->mouse_needs_ack = 1;
678 PrintDebug(" mouse set wrap mode (ignored) ");
679 state->mouse_state = STREAM1;
680 return 0; // not done
683 case 0xec: // reset wrap mode
684 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
685 state->mouse_done_after_ack = 1;
686 state->mouse_needs_ack = 1;
687 PrintDebug(" mouse reset wrap mode (ignored) ");
688 state->mouse_state = STREAM1;
692 case 0xeb: // read data
693 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
694 state->mouse_done_after_ack = 0;
695 state->mouse_needs_ack = 1;
696 PrintDebug(" mouse switch to wrap mode (ignored) ");
697 state->mouse_state = REMOTE1;
698 return 0; // not done
701 case 0xea: // set stream mode
702 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
703 state->mouse_done_after_ack = 1;
704 state->mouse_needs_ack = 1;
705 PrintDebug(" mouse set stream mode ");
706 state->mouse_state = STREAM1;
707 return 0; // not done
710 case 0xe9: // status request
711 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
712 state->mouse_done_after_ack = 0;
713 state->mouse_needs_ack = 1;
714 PrintDebug(" mouse status request begins ");
715 state->mouse_state = STATUS1;
719 case 0xe8: // set resolution
720 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
721 state->mouse_done_after_ack = 0;
722 state->mouse_needs_ack = 1;
723 PrintDebug(" mouse set resolution begins ");
724 state->mouse_state = SETRES1;
728 case 0xe7: // set scaling 2:1
729 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
730 state->mouse_done_after_ack = 1;
731 state->mouse_needs_ack = 1;
732 PrintDebug(" mouse set scaling 2:1 ");
733 state->mouse_state = STREAM1;
734 return 0; // not done
737 case 0xe6: // set scaling 1:1
738 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
739 state->mouse_done_after_ack = 1;
740 state->mouse_needs_ack = 1;
741 PrintDebug(" mouse set scaling 1:1 ");
742 state->mouse_state = STREAM1;
747 PrintDebug(" receiving unknown mouse command (0x%x) in acceptable state ", data);
754 PrintDebug(" receiving mouse output in unhandled state (0x%x) ", state->mouse_state);
760 PrintDebug(" HUH? ");
766 #if KEYBOARD_DEBUG_80H
767 static int keyboard_write_delay(ushort_t port,
770 struct vm_device * dev)
774 PrintDebug("keyboard: write of 0x%x to 80h\n", *((uchar_t*)src));
778 PrintDebug("keyboard: write of >1 byte to 80h\n", *((uchar_t*)src));
784 static int keyboard_read_delay(ushort_t port,
787 struct vm_device * dev)
791 *((uchar_t*)dest) = v3_inb(port);
793 PrintDebug("keyboard: read of 0x%x from 80h\n", *((uchar_t*)dest));
797 PrintDebug("keyboard: read of >1 byte from 80h\n");
808 static int keyboard_write_command(ushort_t port,
811 struct vm_device * dev)
813 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
816 // Should always be single byte write
819 PrintError("keyboard: write of >1 bytes (%d) to 64h\n", length);
823 cmd = *((uchar_t*)src);
825 if (state->state != NORMAL) {
826 PrintDebug("keyboard: warning - receiving command on 64h but state != NORMAL\n");
829 PrintDebug("keyboard: command 0x%x on 64h\n", cmd);
833 case 0x20: // READ COMMAND BYTE (returned in 60h)
834 PushToOutputQueue(dev, state->cmd_byte, OVERWRITE, COMMAND,KEYBOARD);
835 state->state = NORMAL; // the next read on 0x60 will get the right data
836 PrintDebug("keyboard: command byte 0x%x returned\n", state->cmd_byte);
839 case 0x60: // WRITE COMMAND BYTE (read from 60h)
840 state->state = WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
841 PrintDebug("keyboard: prepare to write command byte\n");
844 // case 0x90-9f - write to output port (?)
846 case 0xa1: // Get version number
847 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
848 state->state = NORMAL;
849 PrintDebug("keyboard: version number 0x0 returned\n");
852 case 0xa4: // is password installed? send result to 0x60
853 // we don't support passwords
854 PushToOutputQueue(dev, 0xf1, OVERWRITE, COMMAND, KEYBOARD);
855 PrintDebug("keyboard: password not installed\n");
856 state->state = NORMAL;
859 case 0xa5: // new password will arrive on 0x60
860 state->state = TRANSMIT_PASSWD;
861 PrintDebug("keyboard: pepare to transmit password\n");
864 case 0xa6: // check passwd;
865 // since we do not support passwords, we will simply ignore this
866 // the implication is that any password check immediately succeeds
867 // with a blank password
868 state->state = NORMAL;
869 PrintDebug("keyboard: password check succeeded\n");
872 case 0xa7: // disable mouse
873 state->cmd_byte |= CMD_MOUSE_DISABLE;
874 state->state = NORMAL;
875 PrintDebug("keyboard: mouse disabled\n");
878 case 0xa8: // enable mouse
879 state->cmd_byte &= ~CMD_MOUSE_DISABLE;
880 state->state = NORMAL;
881 PrintDebug("keyboard: mouse enabled\n");
884 case 0xa9: // mouse interface test (always succeeds)
885 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
886 PrintDebug("keyboard: mouse interface test succeeded\n");
887 state->state = NORMAL;
890 case 0xaa: // controller self test (always succeeds)
891 PushToOutputQueue(dev, 0x55, OVERWRITE, COMMAND, KEYBOARD);
892 PrintDebug("keyboard: controller self test succeeded\n");
893 state->state = NORMAL;
896 case 0xab: // keyboard interface test (always succeeds)
897 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
898 state->state = NORMAL;
899 PrintDebug("keyboard: keyboard interface test succeeded\n");
902 case 0xad: // disable keyboard
903 state->cmd_byte |= CMD_DISABLE;
904 state->state = NORMAL;
905 PrintDebug("keyboard: keyboard disabled\n");
908 case 0xae: // enable keyboard
909 state->cmd_byte &= ~CMD_DISABLE;
910 state->state = NORMAL;
911 PrintDebug("keyboard: keyboard enabled\n");
914 case 0xaf: // get version
915 PushToOutputQueue(dev, 0x00, OVERWRITE, COMMAND, KEYBOARD);
916 state->state = NORMAL;
917 PrintDebug("keyboard: version 0 returned \n");
920 case 0xd0: // return microcontroller output on 60h
921 PushToOutputQueue(dev, state->output_byte, OVERWRITE, COMMAND, KEYBOARD);
922 state->state = NORMAL;
923 PrintDebug("keyboard: output byte 0x%x returned\n", state->output_byte);
926 case 0xd1: // request to write next byte on 60h to the microcontroller output port
927 state->state = WRITING_OUTPUT_PORT;
928 PrintDebug("keyboard: prepare to write output byte\n");
931 case 0xd2: // write keyboard buffer (inject key)
932 state->state = INJECTING_KEY;
933 PrintDebug("keyboard: prepare to inject key\n");
936 case 0xd3: // write mouse buffer (inject mouse)
937 state->state = INJECTING_MOUSE;
938 PrintDebug("keyboard: prepare to inject mouse\n");
941 case 0xd4: // write mouse device (command to mouse?)
942 state->state = IN_MOUSE;
943 PrintDebug("keyboard: prepare to inject mouse command\n");
946 case 0xc0: // read input port
947 PushToOutputQueue(dev, state->input_byte, OVERWRITE, COMMAND, KEYBOARD);
948 state->state = NORMAL;
949 PrintDebug("keyboard: input byte 0x%x returned\n", state->input_byte);
952 case 0xc1: //copy input port lsn to status msn
953 state->status_byte &= 0x0f;
954 state->status_byte |= (state->input_byte & 0xf) << 4;
955 state->state = NORMAL;
956 PrintDebug("keyboard: copied input byte lsn to status msn\n");
959 case 0xc2: // copy input port msn to status msn
960 state->status_byte &= 0x0f;
961 state->status_byte |= (state->input_byte & 0xf0);
962 state->state = NORMAL;
963 PrintDebug("keyboard: copied input byte msn to status msn\n");
966 case 0xe0: // read test port
967 PushToOutputQueue(dev, state->output_byte >> 6, OVERWRITE, COMMAND, KEYBOARD);
968 state->state = NORMAL;
969 PrintDebug("keyboard: read 0x%x from test port\n", state->output_byte >> 6);
973 case 0xf0: // pulse output port
974 case 0xf1: // this should pulse 0..3 of cmd_byte on output port
975 case 0xf2: // instead of what is currently in output_byte (I think)
976 case 0xf3: // main effect is taht if bit zero is zero
977 case 0xf4: // should cause reset
978 case 0xf5: // I doubt anything more recent than a 286 running
979 case 0xf6: // OS2 with the penalty box will care
990 PrintDebug("keyboard: ignoring pulse of 0x%x (low=pulsed) on output port\n", (cmd & 0xf));
991 state->state = NORMAL;
995 // case ac diagonstic - returns 16 bytes from keyboard microcontroler on 60h
997 PrintDebug("keyboard: ignoring command (unimplemented)\n");
998 state->state = NORMAL;
1006 static int keyboard_read_status(ushort_t port,
1009 struct vm_device * dev)
1011 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1015 PrintDebug("keyboard: read status (64h): ");
1017 *((uchar_t*)dest) = state->status_byte;
1019 PrintDebug("0x%x\n", *((uchar_t*)dest));
1023 PrintError("keyboard: >1 byte read for status (64h)\n");
1028 static int keyboard_write_output(ushort_t port,
1031 struct vm_device * dev)
1033 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1036 PrintError("keyboard: write of 60h with >1 byte\n");
1040 uchar_t data = *((uchar_t*)src);
1042 PrintDebug("keyboard: output 0x%x on 60h\n", data);
1044 switch (state->state) {
1045 case WRITING_CMD_BYTE:
1046 state->cmd_byte = data;
1047 state->state = NORMAL;
1048 PrintDebug("keyboard: wrote new command byte 0x%x\n", state->cmd_byte);
1050 case WRITING_OUTPUT_PORT:
1051 state->output_byte = data;
1052 state->state = NORMAL;
1053 PrintDebug("keyboard: wrote new output byte 0x%x\n", state->output_byte);
1056 PushToOutputQueue(dev, data, OVERWRITE, COMMAND, KEYBOARD); // probably should be a call to deliver_key_to_vmm()
1057 state->state = NORMAL;
1058 PrintDebug("keyboard: injected key 0x%x\n", data);
1060 case INJECTING_MOUSE:
1061 PushToOutputQueue(dev, data, OVERWRITE, DATA, MOUSE);
1062 // PrintDebug("keyboard: ignoring injected mouse event 0x%x\n", data);
1063 PrintDebug("keyboard: injected mouse event 0x%x\n", data);
1064 state->state = NORMAL;
1067 PrintDebug("keyboard: mouse action: ");
1068 if (mouse_write_output(dev, data)) {
1069 state->state = NORMAL;
1073 case TRANSMIT_PASSWD:
1076 PrintDebug("keyboard: ignoring password character 0x%x\n",data);
1079 state->state = NORMAL;
1080 PrintDebug("keyboard: done with password\n");
1084 PrintDebug("Keyboard: LEDs being set...\n");
1085 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1086 state->state = NORMAL;
1089 PrintDebug("Keyboard: Rate being set...\n");
1090 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1091 state->state = NORMAL;
1096 // command is being sent to keyboard controller
1099 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD); // ack
1100 state->state = RESET;
1101 PrintDebug("keyboard: reset complete and acked\n");
1103 case 0xf5: // disable scanning
1104 case 0xf4: // enable scanning
1106 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1107 // should do something here... PAD
1108 state->state = NORMAL;
1109 PrintDebug("keyboard: %s scanning done and acked\n", (data == 0xf5) ? "disable" : "enable");
1112 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1113 state->state = SET_RATE;
1115 case 0xf2: // get keyboard ID
1116 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1117 state->state = KBD_ID1;
1118 PrintDebug("Keyboard: Requesting Keyboard ID\n");
1120 //PushToOutputQueue(dev, 0xab, OVERWRITE, COMMAND, KEYBOARD);
1121 //state->state = KBD_ID2;
1123 case 0xed: // enable keyboard LEDs
1124 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1125 state->state = SET_LEDS;
1127 case 0xfe: // resend
1128 case 0xfd: // set key type make
1129 case 0xfc: // set key typ make/break
1130 case 0xfb: // set key type typematic
1131 case 0xfa: // set all typematic make/break/typematic
1132 case 0xf9: // set all make
1133 case 0xf8: // set all make/break
1134 case 0xf7: // set all typemaktic
1135 case 0xf6: // set defaults
1136 PrintDebug("keyboard: unhandled known command 0x%x on output buffer (60h)\n", data);
1140 PrintDebug("keyboard: unhandled unknown command 0x%x on output buffer (60h)\n", data);
1141 state->status_byte |= 0x1;
1153 static int keyboard_read_input(ushort_t port,
1156 struct vm_device * dev)
1158 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1164 PrintDebug("keyboard: read from input (60h): ");
1166 if (state->state == IN_MOUSE) {
1167 done_mouse = mouse_read_input(dev);
1169 state->state = NORMAL;
1173 PullFromOutputQueue(dev, &data);
1175 if (state->state == RESET) {
1176 // We just delivered the ack for the reset
1177 // now we will ready ourselves to deliver the BAT code (success)
1178 PushToOutputQueue(dev, 0xaa, OVERWRITE, COMMAND, KEYBOARD);
1179 state->state = NORMAL;
1180 PrintDebug(" (in reset, pushing BAT test code 0xaa) ");
1181 } else if (state->state == KBD_ID1) {
1182 PushToOutputQueue(dev, 0xab, OVERWRITE, COMMAND, KEYBOARD);
1183 state->state = KBD_ID2;
1184 PrintDebug(" (in kbd id request, pushing 1st ID val) ");
1185 } else if (state->state == KBD_ID2) {
1186 PushToOutputQueue(dev, 0x83, OVERWRITE, COMMAND, KEYBOARD);
1187 state->state = NORMAL;
1188 PrintDebug(" (in kbd id request, pushing 2nd ID val) ");
1191 PrintDebug("0x%x\n", data);
1193 *((uchar_t*)dest) = data;
1197 PrintError("keyboard: unknown size read from input (60h)\n");
1206 static int keyboard_init_device(struct vm_device * dev)
1209 // struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
1211 PrintDebug("keyboard: init_device\n");
1213 // Would read state here
1215 keyboard_reset_device(dev);
1218 v3_dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
1219 v3_dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
1221 v3_hook_host_event(dev->vm, HOST_KEYBOARD_EVT, V3_HOST_EVENT_HANDLER(key_event_handler), dev);
1222 v3_hook_host_event(dev->vm, HOST_MOUSE_EVT, V3_HOST_EVENT_HANDLER(mouse_event_handler), dev);
1225 #if KEYBOARD_DEBUG_80H
1226 v3_dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
1231 // We do not hook the IRQ here. Instead, the underlying device driver
1232 // is responsible to call us back
1238 static int keyboard_deinit_device(struct vm_device *dev)
1241 v3_dev_unhook_io(dev, KEYBOARD_60H);
1242 v3_dev_unhook_io(dev, KEYBOARD_64H);
1243 #if KEYBOARD_DEBUG_80H
1244 v3_dev_unhook_io(dev, KEYBOARD_DELAY_80H);
1246 keyboard_reset_device(dev);
1254 static struct vm_device_ops dev_ops = {
1255 .init = keyboard_init_device,
1256 .deinit = keyboard_deinit_device,
1257 .reset = keyboard_reset_device,
1258 .start = keyboard_start_device,
1259 .stop = keyboard_stop_device,
1265 struct vm_device * v3_create_keyboard() {
1266 struct keyboard_internal * keyboard_state = NULL;
1268 keyboard_state = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
1270 struct vm_device *device = v3_create_device("KEYBOARD", &dev_ops, keyboard_state);