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
153 // after receiving a mouse command 0f 0xff
154 // we return the ack and then the next thing will be the
155 // bat code (aa - success)
157 // followed by the device id (00 - mouse)
159 // Then it goes into stream mode
162 STREAM3, // for each of the following bytes in mouse_packet
163 // this is used for setting sample rate
165 // this is used for getting device id
167 // just like the stream moes
171 // For getting status info
181 uchar_t mouse_packet[3]; // byte 1: y over, xover, y sign, x sign, 1, middle, right, left
182 // byte 2: x movement
183 // byte 3: y movement
185 uchar_t mouse_needs_ack; //
186 uchar_t mouse_done_after_ack;
188 uchar_t cmd_byte; // for keyboard uC - read/written
189 // via read/write cmd byte command
190 uchar_t status_byte; // for on-board uC - read via 64h
192 uchar_t output_byte; // output port of onboard uC (e.g. A20)
194 uchar_t input_byte; // input port of onboard uC
197 uchar_t input_queue; //
198 uint_t input_queue_len; //
199 //uint_t input_queue_read;
200 //uint_t input_queue_write;
202 uchar_t output_queue; //
203 uint_t output_queue_len; //
204 //uint_t output_queue_read;
205 //uint_t output_queue_write;
212 // push item onto outputqueue, optionally overwriting if there is no room
213 // returns 0 if successful
215 static int PushToOutputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite, uchar_t cmd, uchar_t mouse)
217 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
219 if ((state->output_queue_len == 0) || overwrite) {
221 state->output_queue = value;
222 state->output_queue_len = 1;
225 state->status_byte |= STATUS_COMMAND_DATA_AVAIL;
227 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
231 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
235 state->status_byte |= STATUS_OUTPUT_BUFFER_FULL;
241 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
248 // pull item from outputqueue
249 // returns 0 if successful
251 static int PullFromOutputQueue(struct vm_device * dev, uchar_t * value)
253 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
255 if (1 || (state->output_queue_len == 1)) {
257 *value = state->output_queue;
258 state->output_queue_len = 0;
260 if (state->status_byte & STATUS_OUTPUT_BUFFER_FULL) {
261 state->status_byte &= ~STATUS_OUTPUT_BUFFER_FULL;
264 if (state->status_byte & STATUS_MOUSE_BUFFER_FULL) {
265 state->status_byte &= ~STATUS_MOUSE_BUFFER_FULL;
268 if (state->status_byte & STATUS_COMMAND_DATA_AVAIL) {
269 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
275 PrintError("keyboard: PullFromOutputQueue Failed - Queue Empty\n");
283 // push item onto inputqueue, optionally overwriting if there is no room
284 // returns 0 if successful
286 static int PushToInputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite)
288 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
290 if ((state->input_queue_len == 0) || overwrite) {
292 state->input_queue = value;
293 state->input_queue_len = 1;
294 state->status_byte |= STATUS_INPUT_BUFFER_FULL;
298 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
304 // pull item from inputqueue
305 // returns 0 if successful
307 static int PullFromInputQueue(struct vm_device *dev, uchar_t *value)
309 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
311 if (state->input_queue_len == 1) {
313 *value = state->input_queue;
314 state->input_queue_len = 0;
315 state->status_byte &= ~STATUS_INPUT_BUFFER_FULL;
319 PrintError("keyboard: PullFromInputQueue Failed - Queue Empty\n");
329 static int keyboard_interrupt(struct vm_device * dev, uint_t irq) {
330 PrintDebug("keyboard: interrupt 0x%x\n", irq);
332 v3_raise_irq(dev->vm, irq);
340 static int key_event_handler(struct guest_info * info,
341 struct v3_keyboard_event * evt,
342 void * private_data) {
343 struct vm_device * dev = (struct vm_device *)private_data;
344 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
346 PrintDebug("keyboard: injected status 0x%x, and scancode 0x%x\n", evt->status, evt->scan_code);
348 if ( (state->status_byte & STATUS_ENABLED) // onboard is enabled
349 && (!(state->cmd_byte & CMD_DISABLE)) ) { // keyboard is enabled
351 PushToOutputQueue(dev, evt->scan_code, OVERWRITE, DATA, KEYBOARD);
353 if (state->cmd_byte & CMD_INTR) {
354 keyboard_interrupt(dev, KEYBOARD_IRQ);
362 static int mouse_event_handler(struct guest_info * info,
363 struct v3_mouse_event * evt,
364 void * private_data) {
365 struct vm_device * dev = (struct vm_device *)private_data;
366 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
368 PrintDebug("keyboard: injected mouse packet 0x %x %x %x\n",
369 evt->data[0], evt->data[1], evt->data[2]);
371 memcpy(state->mouse_packet, evt->data, 3);
373 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
376 switch (state->mouse_state) {
380 if (!(state->cmd_byte & CMD_MOUSE_DISABLE)) {
381 keyboard_interrupt(dev, MOUSE_IRQ);
393 static int keyboard_reset_device(struct vm_device * dev)
395 struct keyboard_internal *data = (struct keyboard_internal *)(dev->private_data);
397 memset(data, 0, sizeof(struct keyboard_internal));
399 data->state = NORMAL;
400 data->mouse_state = STREAM1;
403 CMD_INTR // interrupts on
404 | CMD_MOUSE_INTR // mouse interupts on
405 | CMD_SYSTEM ; // self test passed
406 // PS2, keyboard+mouse enabled, generic translation
409 STATUS_SYSTEM // self-tests passed
410 | STATUS_ENABLED ; // keyboard ready
411 // buffers empty, no errors
413 data->output_byte = 0; // ?
415 data->input_byte = INPUT_RAM; // we have some
416 // also display=color, jumper 0, keyboard enabled
420 PrintDebug("keyboard: reset device\n");
428 static int keyboard_start_device(struct vm_device *dev)
430 PrintDebug("keyboard: start device\n");
435 static int keyboard_stop_device(struct vm_device *dev)
437 PrintDebug("keyboard: stop device\n");
442 static int mouse_read_input(struct vm_device *dev)
444 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
446 if (state->mouse_needs_ack) {
447 state->mouse_needs_ack = 0;
449 // the ack has been stuffed previously
450 if (state->mouse_done_after_ack) {
457 switch (state->mouse_state) {
459 case RESET1: // requesting the BAT code
460 PushToOutputQueue(dev, 0xaa, OVERWRITE, DATA, MOUSE) ; // BAT successful
461 PrintDebug(" mouse sent BAT code (sucesssful) ");
462 state->mouse_state = RESET2;
464 return 0; // not done with mouse processing yet
467 case RESET2: // requesting the device id
468 PushToOutputQueue(dev, 0x00, OVERWRITE, DATA, MOUSE) ; // normal mouse type
469 PrintDebug(" mouse sent device id ");
470 state->mouse_state = STREAM1;
472 return 1; // done with mouse processing
475 case STREAM1: // send data
476 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
477 PrintDebug(" mouse sent stream data1 ");
478 state->mouse_state = STREAM2;
483 case STREAM2: // send data
484 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
485 PrintDebug(" mouse sent stream data2 ");
486 state->mouse_state = STREAM3;
491 case STREAM3: // send data
492 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
493 PrintDebug(" mouse sent stream data3 ");
494 state->mouse_state = STREAM1;
496 return 1; // now done
499 case REMOTE1: // send data
500 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
501 PrintDebug(" mouse sent remote data1 ");
502 state->mouse_state = REMOTE2;
507 case REMOTE2: // send data
508 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
509 PrintDebug(" mouse sent remote data2 ");
510 state->mouse_state = REMOTE3;
515 case REMOTE3: // send data
516 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
517 PrintDebug(" mouse sent remote data3 ");
518 state->mouse_state = REMOTE1;
520 return 1; // now done
523 case STATUS1: // send data
524 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
525 PrintDebug(" mouse sent status data1 ");
526 state->mouse_state = STATUS2;
531 case STATUS2: // send data
532 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
533 PrintDebug(" mouse sent status data2 ");
534 state->mouse_state = STATUS3;
539 case STATUS3: // send data
540 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
541 PrintDebug(" mouse sent status data3 ");
542 state->mouse_state = STREAM1;
544 return 1; // now done
547 case DEVICE1: // send device id
548 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
549 PrintDebug(" mouse sent device id ");
550 state->mouse_state = STREAM1;
552 return 1; // now done
556 PrintDebug(" mouse has no data ");
562 static int mouse_write_output(struct vm_device * dev, uchar_t data)
564 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
566 switch (state->mouse_state) {
576 PushToOutputQueue(dev, 0xfe, OVERWRITE, DATA, MOUSE) ; // no mouse!
577 PrintDebug(" mouse reset begins (no mouse) ");
579 return 1; // not done;
584 PushToOutputQueue(dev,0xfa,OVERWRITE,DATA,MOUSE) ;
585 PrintDebug(" mouse reset begins ");
586 state->mouse_done_after_ack=0;
587 state->mouse_needs_ack=1;
588 state->mouse_state=RESET1;
589 return 0; // not done;
593 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
594 PrintDebug(" mouse resend begins ");
595 state->mouse_done_after_ack = 0;
596 state->mouse_needs_ack = 0;
597 state->mouse_state = STREAM1;
598 return 0; // not done
601 case 0xf6: // set defaults
602 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
603 PrintDebug(" mouse set defaults ");
604 state->mouse_done_after_ack = 1;
605 state->mouse_needs_ack = 1;
606 state->mouse_state = STREAM1;
607 return 0; // not done
610 case 0xf5: // disable data reporting
611 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
612 state->mouse_done_after_ack = 1;
613 state->mouse_needs_ack = 1;
614 PrintDebug(" mouse disable data reporting ");
615 state->mouse_state = STREAM1;
616 return 0; // not done
619 case 0xf4: // enable data reporting
620 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
621 state->mouse_done_after_ack = 1;
622 state->mouse_needs_ack = 1;
623 PrintDebug(" mouse enable data reporting ");
624 state->mouse_state = STREAM1;
625 return 0; // not done
628 case 0xf3: // set sample rate
629 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
630 state->mouse_done_after_ack = 0;
631 state->mouse_needs_ack = 1;
632 PrintDebug(" mouse set sample rate begins ");
633 state->mouse_state = SAMPLE1;
634 return 0; // not done
637 case 0xf2: // get device id
638 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
639 state->mouse_done_after_ack = 0;
640 state->mouse_needs_ack = 1;
641 PrintDebug(" mouse get device id begins ");
642 state->mouse_state = DEVICE1;
643 return 0; // not done
646 case 0xf0: // set remote mode
647 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
648 state->mouse_done_after_ack = 1;
649 state->mouse_needs_ack = 1;
650 PrintDebug(" mouse set remote mode ");
651 state->mouse_state = REMOTE1;
652 return 0; // not done
655 case 0xee: // set wrap mode
656 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
657 state->mouse_done_after_ack = 1;
658 state->mouse_needs_ack = 1;
659 PrintDebug(" mouse set wrap mode (ignored) ");
660 state->mouse_state = STREAM1;
661 return 0; // not done
664 case 0xec: // reset wrap mode
665 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
666 state->mouse_done_after_ack = 1;
667 state->mouse_needs_ack = 1;
668 PrintDebug(" mouse reset wrap mode (ignored) ");
669 state->mouse_state = STREAM1;
673 case 0xeb: // read data
674 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
675 state->mouse_done_after_ack = 0;
676 state->mouse_needs_ack = 1;
677 PrintDebug(" mouse switch to wrap mode (ignored) ");
678 state->mouse_state = REMOTE1;
679 return 0; // not done
682 case 0xea: // set stream mode
683 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
684 state->mouse_done_after_ack = 1;
685 state->mouse_needs_ack = 1;
686 PrintDebug(" mouse set stream mode ");
687 state->mouse_state = STREAM1;
688 return 0; // not done
691 case 0xe9: // status request
692 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
693 state->mouse_done_after_ack = 0;
694 state->mouse_needs_ack = 1;
695 PrintDebug(" mouse status request begins ");
696 state->mouse_state = STATUS1;
700 case 0xe8: // set resolution
701 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
702 state->mouse_done_after_ack = 0;
703 state->mouse_needs_ack = 1;
704 PrintDebug(" mouse set resolution begins ");
705 state->mouse_state = SETRES1;
709 case 0xe7: // set scaling 2:1
710 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
711 state->mouse_done_after_ack = 1;
712 state->mouse_needs_ack = 1;
713 PrintDebug(" mouse set scaling 2:1 ");
714 state->mouse_state = STREAM1;
715 return 0; // not done
718 case 0xe6: // set scaling 1:1
719 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
720 state->mouse_done_after_ack = 1;
721 state->mouse_needs_ack = 1;
722 PrintDebug(" mouse set scaling 1:1 ");
723 state->mouse_state = STREAM1;
728 PrintDebug(" receiving unknown mouse command (0x%x) in acceptable state ", data);
735 PrintDebug(" receiving mouse output in unhandled state (0x%x) ", state->mouse_state);
741 PrintDebug(" HUH? ");
747 #if KEYBOARD_DEBUG_80H
748 static int keyboard_write_delay(ushort_t port,
751 struct vm_device * dev)
755 PrintDebug("keyboard: write of 0x%x to 80h\n", *((uchar_t*)src));
759 PrintDebug("keyboard: write of >1 byte to 80h\n", *((uchar_t*)src));
765 static int keyboard_read_delay(ushort_t port,
768 struct vm_device * dev)
772 *((uchar_t*)dest) = v3_inb(port);
774 PrintDebug("keyboard: read of 0x%x from 80h\n", *((uchar_t*)dest));
778 PrintDebug("keyboard: read of >1 byte from 80h\n");
789 static int keyboard_write_command(ushort_t port,
792 struct vm_device * dev)
794 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
797 // Should always be single byte write
800 PrintError("keyboard: write of >1 bytes (%d) to 64h\n", length);
804 cmd = *((uchar_t*)src);
806 if (state->state != NORMAL) {
807 PrintDebug("keyboard: warning - receiving command on 64h but state != NORMAL\n");
810 PrintDebug("keyboard: command 0x%x on 64h\n", cmd);
814 case 0x20: // READ COMMAND BYTE (returned in 60h)
815 PushToOutputQueue(dev, state->cmd_byte, OVERWRITE, COMMAND,KEYBOARD);
816 state->state = NORMAL; // the next read on 0x60 will get the right data
817 PrintDebug("keyboard: command byte 0x%x returned\n", state->cmd_byte);
820 case 0x60: // WRITE COMMAND BYTE (read from 60h)
821 state->state = WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
822 PrintDebug("keyboard: prepare to write command byte\n");
825 // case 0x90-9f - write to output port (?)
827 case 0xa1: // Get version number
828 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
829 state->state = NORMAL;
830 PrintDebug("keyboard: version number 0x0 returned\n");
833 case 0xa4: // is password installed? send result to 0x60
834 // we don't support passwords
835 PushToOutputQueue(dev, 0xf1, OVERWRITE, COMMAND, KEYBOARD);
836 PrintDebug("keyboard: password not installed\n");
837 state->state = NORMAL;
840 case 0xa5: // new password will arrive on 0x60
841 state->state = TRANSMIT_PASSWD;
842 PrintDebug("keyboard: pepare to transmit password\n");
845 case 0xa6: // check passwd;
846 // since we do not support passwords, we will simply ignore this
847 // the implication is that any password check immediately succeeds
848 // with a blank password
849 state->state = NORMAL;
850 PrintDebug("keyboard: password check succeeded\n");
853 case 0xa7: // disable mouse
854 state->cmd_byte |= CMD_MOUSE_DISABLE;
855 state->state = NORMAL;
856 PrintDebug("keyboard: mouse disabled\n");
859 case 0xa8: // enable mouse
860 state->cmd_byte &= ~CMD_MOUSE_DISABLE;
861 state->state = NORMAL;
862 PrintDebug("keyboard: mouse enabled\n");
865 case 0xa9: // mouse interface test (always succeeds)
866 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
867 PrintDebug("keyboard: mouse interface test succeeded\n");
868 state->state = NORMAL;
871 case 0xaa: // controller self test (always succeeds)
872 PushToOutputQueue(dev, 0x55, OVERWRITE, COMMAND, KEYBOARD);
873 PrintDebug("keyboard: controller self test succeeded\n");
874 state->state = NORMAL;
877 case 0xab: // keyboard interface test (always succeeds)
878 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
879 state->state = NORMAL;
880 PrintDebug("keyboard: keyboard interface test succeeded\n");
883 case 0xad: // disable keyboard
884 state->cmd_byte |= CMD_DISABLE;
885 state->state = NORMAL;
886 PrintDebug("keyboard: keyboard disabled\n");
889 case 0xae: // enable keyboard
890 state->cmd_byte &= ~CMD_DISABLE;
891 state->state = NORMAL;
892 PrintDebug("keyboard: keyboard enabled\n");
895 case 0xaf: // get version
896 PushToOutputQueue(dev, 0x00, OVERWRITE, COMMAND, KEYBOARD);
897 state->state = NORMAL;
898 PrintDebug("keyboard: version 0 returned \n");
901 case 0xd0: // return microcontroller output on 60h
902 PushToOutputQueue(dev, state->output_byte, OVERWRITE, COMMAND, KEYBOARD);
903 state->state = NORMAL;
904 PrintDebug("keyboard: output byte 0x%x returned\n", state->output_byte);
907 case 0xd1: // request to write next byte on 60h to the microcontroller output port
908 state->state = WRITING_OUTPUT_PORT;
909 PrintDebug("keyboard: prepare to write output byte\n");
912 case 0xd2: // write keyboard buffer (inject key)
913 state->state = INJECTING_KEY;
914 PrintDebug("keyboard: prepare to inject key\n");
917 case 0xd3: // write mouse buffer (inject mouse)
918 state->state = INJECTING_MOUSE;
919 PrintDebug("keyboard: prepare to inject mouse\n");
922 case 0xd4: // write mouse device (command to mouse?)
923 state->state = IN_MOUSE;
924 PrintDebug("keyboard: prepare to inject mouse command\n");
927 case 0xc0: // read input port
928 PushToOutputQueue(dev, state->input_byte, OVERWRITE, COMMAND, KEYBOARD);
929 state->state = NORMAL;
930 PrintDebug("keyboard: input byte 0x%x returned\n", state->input_byte);
933 case 0xc1: //copy input port lsn to status msn
934 state->status_byte &= 0x0f;
935 state->status_byte |= (state->input_byte & 0xf) << 4;
936 state->state = NORMAL;
937 PrintDebug("keyboard: copied input byte lsn to status msn\n");
940 case 0xc2: // copy input port msn to status msn
941 state->status_byte &= 0x0f;
942 state->status_byte |= (state->input_byte & 0xf0);
943 state->state = NORMAL;
944 PrintDebug("keyboard: copied input byte msn to status msn\n");
947 case 0xe0: // read test port
948 PushToOutputQueue(dev, state->output_byte >> 6, OVERWRITE, COMMAND, KEYBOARD);
949 state->state = NORMAL;
950 PrintDebug("keyboard: read 0x%x from test port\n", state->output_byte >> 6);
954 case 0xf0: // pulse output port
955 case 0xf1: // this should pulse 0..3 of cmd_byte on output port
956 case 0xf2: // instead of what is currently in output_byte (I think)
957 case 0xf3: // main effect is taht if bit zero is zero
958 case 0xf4: // should cause reset
959 case 0xf5: // I doubt anything more recent than a 286 running
960 case 0xf6: // OS2 with the penalty box will care
971 PrintDebug("keyboard: ignoring pulse of 0x%x (low=pulsed) on output port\n", (cmd & 0xf));
972 state->state = NORMAL;
976 // case ac diagonstic - returns 16 bytes from keyboard microcontroler on 60h
978 PrintDebug("keyboard: ignoring command (unimplemented)\n");
979 state->state = NORMAL;
987 static int keyboard_read_status(ushort_t port,
990 struct vm_device * dev)
992 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
996 PrintDebug("keyboard: read status (64h): ");
998 *((uchar_t*)dest) = state->status_byte;
1000 PrintDebug("0x%x\n", *((uchar_t*)dest));
1004 PrintError("keyboard: >1 byte read for status (64h)\n");
1009 static int keyboard_write_output(ushort_t port,
1012 struct vm_device * dev)
1014 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1017 PrintError("keyboard: write of 60h with >1 byte\n");
1021 uchar_t data = *((uchar_t*)src);
1023 PrintDebug("keyboard: output 0x%x on 60h\n", data);
1025 switch (state->state) {
1026 case WRITING_CMD_BYTE:
1027 state->cmd_byte = data;
1028 state->state = NORMAL;
1029 PrintDebug("keyboard: wrote new command byte 0x%x\n", state->cmd_byte);
1031 case WRITING_OUTPUT_PORT:
1032 state->output_byte = data;
1033 state->state = NORMAL;
1034 PrintDebug("keyboard: wrote new output byte 0x%x\n", state->output_byte);
1037 PushToOutputQueue(dev, data, OVERWRITE, COMMAND, KEYBOARD); // probably should be a call to deliver_key_to_vmm()
1038 state->state = NORMAL;
1039 PrintDebug("keyboard: injected key 0x%x\n", data);
1041 case INJECTING_MOUSE:
1042 PrintDebug("keyboard: ignoring injected mouse event 0x%x\n", data);
1043 state->state = NORMAL;
1046 PrintDebug("keyboard: mouse action: ");
1047 if (mouse_write_output(dev, data)) {
1048 state->state = NORMAL;
1052 case TRANSMIT_PASSWD:
1055 PrintDebug("keyboard: ignoring password character 0x%x\n",data);
1058 state->state = NORMAL;
1059 PrintDebug("keyboard: done with password\n");
1064 // command is being sent to keyboard controller
1067 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD); // ack
1068 state->state = RESET;
1069 PrintDebug("keyboard: reset complete and acked\n");
1071 case 0xf5: // disable scanning
1072 case 0xf4: // enable scanning
1074 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1075 // should do something here... PAD
1076 state->state = NORMAL;
1077 PrintDebug("keyboard: %s scanning done and acked\n",data==0xf5 ? "disable" : "enable");
1079 case 0xfe: // resend
1080 case 0xfd: // set key type make
1081 case 0xfc: // set key typ make/break
1082 case 0xfb: // set key type typematic
1083 case 0xfa: // set all typematic make/break/typematic
1084 case 0xf9: // set all make
1085 case 0xf8: // set all make/break
1086 case 0xf7: // set all typemaktic
1087 case 0xf6: // set defaults
1088 case 0xf3: // set typematic delay/rate
1089 PrintDebug("keyboard: unhandled known command 0x%x on output buffer (60h)\n", data);
1092 PrintDebug("keyboard: unhandled unknown command 0x%x on output buffer (60h)\n", data);
1093 state->status_byte |= 0x1;
1099 PrintDebug("keyboard: unknown state %x on command 0x%x on output buffer (60h)\n", state->state, data);
1105 static int keyboard_read_input(ushort_t port,
1108 struct vm_device * dev)
1110 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1116 PrintDebug("keyboard: read from input (60h): ");
1118 if (state->state == IN_MOUSE) {
1119 done_mouse = mouse_read_input(dev);
1121 state->state = NORMAL;
1125 PullFromOutputQueue(dev, &data);
1127 if (state->state == RESET) {
1128 // We just delivered the ack for the reset
1129 // now we will ready ourselves to deliver the BAT code (success)
1130 PushToOutputQueue(dev, 0xaa, OVERWRITE, COMMAND, KEYBOARD);
1131 state->state = NORMAL;
1132 PrintDebug(" (in reset, pushing BAT test code 0xaa) ");
1135 PrintDebug("0x%x\n", data);
1137 *((uchar_t*)dest) = data;
1141 PrintError("keyboard: unknown size read from input (60h)\n");
1150 static int keyboard_init_device(struct vm_device * dev)
1153 // struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
1155 PrintDebug("keyboard: init_device\n");
1157 // Would read state here
1159 keyboard_reset_device(dev);
1162 v3_dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
1163 v3_dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
1165 v3_hook_host_event(dev->vm, HOST_KEYBOARD_EVT, V3_HOST_EVENT_HANDLER(key_event_handler), dev);
1166 v3_hook_host_event(dev->vm, HOST_MOUSE_EVT, V3_HOST_EVENT_HANDLER(mouse_event_handler), dev);
1169 #if KEYBOARD_DEBUG_80H
1170 v3_dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
1175 // We do not hook the IRQ here. Instead, the underlying device driver
1176 // is responsible to call us back
1182 static int keyboard_deinit_device(struct vm_device *dev)
1185 v3_dev_unhook_io(dev, KEYBOARD_60H);
1186 v3_dev_unhook_io(dev, KEYBOARD_64H);
1187 #if KEYBOARD_DEBUG_80H
1188 v3_dev_unhook_io(dev, KEYBOARD_DELAY_80H);
1190 keyboard_reset_device(dev);
1198 static struct vm_device_ops dev_ops = {
1199 .init = keyboard_init_device,
1200 .deinit = keyboard_deinit_device,
1201 .reset = keyboard_reset_device,
1202 .start = keyboard_start_device,
1203 .stop = keyboard_stop_device,
1209 struct vm_device * v3_create_keyboard() {
1210 struct keyboard_internal * keyboard_state = NULL;
1212 keyboard_state = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
1214 struct vm_device *device = v3_create_device("KEYBOARD", &dev_ops, keyboard_state);