1 /* (c) 2008, Peter Dinda <pdinda@northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
4 #include <devices/keyboard.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmm_types.h>
11 #define PrintDebug(fmt, args...)
14 #define KEYBOARD_DEBUG_80H 0
18 #define KEYBOARD_60H 0x60 // keyboard microcontroller
19 #define KEYBOARD_64H 0x64 // onboard microcontroller
21 #define KEYBOARD_DELAY_80H 0x80 // written for timing
23 #define KEYBOARD_IRQ 0x1
27 // extract bits for status byte
28 #define STATUS_OUTPUT_BUFFER_FULL 0x01 // 1=full (data for system)
29 #define STATUS_INPUT_BUFFER_FULL 0x02 // 1=full (data for 8042)
30 #define STATUS_SYSTEM 0x04 // 1=self-test-passed
31 #define STATUS_COMMAND_DATA_AVAIL 0x08 // internal: 0=data on 60h, 0=cmd on 64h
32 #define STATUS_ENABLED 0x10 // 1=keyboard is enabled
33 #define STATUS_MOUSE_BUFFER_FULL 0x20 // 1= mouse output buffer full
34 #define STATUS_TIMEOUT 0x40 // 1=timeout of keybd
35 #define STATUS_PARITY 0x80 // 1=parity error
39 #define CMD_INTR 0x01 // 1=interrupts enabled
40 #define CMD_MOUSE_INTR 0x02 // 1=interrupts enabled for mouse
41 #define CMD_SYSTEM 0x04 // 1= self test passed
42 #define CMD_OVERRIDE 0x08 // FORCE 0 for PS2
43 #define CMD_DISABLE 0x10 // 1=disabled keyboard
44 #define CMD_MOUSE_DISABLE 0x20 // 1=disabled mouse
45 #define CMD_SCANCODE_XLATE 0x40 // 1=translate to set 1 scancodes
46 #define CMD_RESERVED 0x80 // should be zero
48 // bits for the output port
51 #define OUTPUT_RESET 0x01 // System reset on 0
52 #define OUTPUT_A20 0x02 // A20 gate (1= A20 is gated)
53 #define OUTPUT_RES1 0x04 // reserved
54 #define OUTPUT_RES2 0x08 // reserved
55 #define OUTPUT_OUTPUT_FULL 0x10 // output buffer full
56 #define OUTPUT_INPUT_EMPTY 0x20 // input buffer empty
57 #define OUTPUT_KBD_CLOCK 0x40 // keyboard clock (?)
58 #define OUTPUT_KBD_DATA 0x80 // keyboard data
60 // bits for the input port
62 #define INPUT_RES0 0x01 // reserved
63 #define INPUT_RES1 0x02 // reserved
64 #define INPUT_RES2 0x04 // reserved
65 #define INPUT_RES3 0x08 // reserved
66 #define INPUT_RAM 0x10 // set to 1 if RAM exists?
67 #define INPUT_JUMPER 0x20 // manufacturing jumper?
68 #define INPUT_DISPLAY 0x40 // 0=color, 1=mono
69 #define INPUT_KBD_INHIBIT 0x80 // 1=inhibit keyboard ?
72 // for queue operations
76 // for queue operations - whether it's data or cmd waiting on 60h
80 // for queue operations - whether this is keyboard or mouse data on 60h
85 // The currently targetted keyboard
86 static struct vm_device * thekeyboard = NULL;
88 //#define QUEUE_SIZE 32
91 struct keyboard_internal {
93 // 0x60 is the port for the keyboard microcontroller
94 // writes are commands
95 // reads from it usually return scancodes
96 // however, it can also return other data
97 // depending on the state of the onboard microcontroller
99 // 0x64 is the port for the onboard microcontroller
100 // writes are commands
104 // state of the onboard microcontroller
105 // this is needed because sometimes 0x60 reads come
106 // from the onboard microcontroller
107 enum {// Normal mode measn we deliver keys
108 // to the vm and accept commands from it
110 // after receiving cmd 0x60
111 // keybaord uC cmd will subsequently arrive
113 // after recieving 0xa5
114 // password arrives on data port, null terminated
116 // after having reset sent to 0x60
117 // we immediately ack, and then
118 // push BAT success (0xaa) after the ack
120 // after having a d1 sent to 64
121 // we wait for a new output byte on 60
123 // after having a d2 sent to 64
124 // we wait for a new output byte on 60
125 // then make it available as a keystroke
127 // after having a d3 sent to 64
128 // we wait for a new output byte on 60
129 // then make it available as a mouse event
131 // after having a d4 sent to 64
132 // we wait for a new output byte on 60
133 // then send it to the mouse
139 // after receiving a mouse command 0f 0xff
140 // we return the ack and then the next thing will be the
141 // bat code (aa - success)
143 // followed by the device id (00 - mouse)
145 // Then it goes into stream mode
148 STREAM3, // for each of the following bytes in mouse_packet
149 // this is used for setting sample rate
151 // this is used for getting device id
153 // just like the stream moes
157 // For getting status info
167 uchar_t mouse_packet[3]; // byte 1: y over, xover, y sign, x sign, 1, middle, right, left
168 // byte 2: x movement
169 // byte 3: y movement
171 uchar_t mouse_needs_ack; //
172 uchar_t mouse_done_after_ack;
174 uchar_t cmd_byte; // for keyboard uC - read/written
175 // via read/write cmd byte command
176 uchar_t status_byte; // for on-board uC - read via 64h
178 uchar_t output_byte; // output port of onboard uC (e.g. A20)
180 uchar_t input_byte; // input port of onboard uC
183 uchar_t input_queue; //
184 uint_t input_queue_len; //
185 //uint_t input_queue_read;
186 //uint_t input_queue_write;
188 uchar_t output_queue; //
189 uint_t output_queue_len; //
190 //uint_t output_queue_read;
191 //uint_t output_queue_write;
198 // push item onto outputqueue, optionally overwriting if there is no room
199 // returns 0 if successful
201 static int PushToOutputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite, uchar_t cmd, uchar_t mouse)
203 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
205 if ((state->output_queue_len == 0) || overwrite) {
207 state->output_queue = value;
208 state->output_queue_len = 1;
211 state->status_byte |= STATUS_COMMAND_DATA_AVAIL;
213 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
217 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
221 state->status_byte |= STATUS_OUTPUT_BUFFER_FULL;
227 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
234 // pull item from outputqueue
235 // returns 0 if successful
237 static int PullFromOutputQueue(struct vm_device * dev, uchar_t * value)
239 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
241 if (1 || (state->output_queue_len == 1)) {
243 *value = state->output_queue;
244 state->output_queue_len = 0;
246 if (state->status_byte & STATUS_OUTPUT_BUFFER_FULL) {
247 state->status_byte &= ~STATUS_OUTPUT_BUFFER_FULL;
250 if (state->status_byte & STATUS_MOUSE_BUFFER_FULL) {
251 state->status_byte &= ~STATUS_MOUSE_BUFFER_FULL;
254 if (state->status_byte & STATUS_COMMAND_DATA_AVAIL) {
255 state->status_byte &= ~STATUS_COMMAND_DATA_AVAIL;
261 PrintError("keyboard: PullFromOutputQueue Failed - Queue Empty\n");
269 // push item onto inputqueue, optionally overwriting if there is no room
270 // returns 0 if successful
272 static int PushToInputQueue(struct vm_device * dev, uchar_t value, uchar_t overwrite)
274 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
276 if ((state->input_queue_len == 0) || overwrite) {
278 state->input_queue = value;
279 state->input_queue_len = 1;
280 state->status_byte |= STATUS_INPUT_BUFFER_FULL;
284 PrintError("keyboard: PushToOutputQueue Failed - Queue Full\n");
290 // pull item from inputqueue
291 // returns 0 if successful
293 static int PullFromInputQueue(struct vm_device *dev, uchar_t *value)
295 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
297 if (state->input_queue_len == 1) {
299 *value = state->input_queue;
300 state->input_queue_len = 0;
301 state->status_byte &= ~STATUS_INPUT_BUFFER_FULL;
305 PrintError("keyboard: PullFromInputQueue Failed - Queue Empty\n");
312 static struct vm_device *demultiplex_injected_key(uchar_t status, uchar_t scancode)
314 // this currently does nothing
318 static struct vm_device *demultiplex_injected_mouse(uchar_t mouse_packet[3])
320 // this currently does nothing
324 int keyboard_interrupt(uint_t irq, struct vm_device * dev);
326 void deliver_key_to_vmm(uchar_t status, uchar_t scancode)
328 struct vm_device *dev = demultiplex_injected_key(status, scancode);
329 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
331 PrintDebug("keyboard: injected status 0x%x, and scancode 0x%x\n", status, scancode);
333 if ( (state->status_byte & STATUS_ENABLED) // onboard is enabled
334 && (!(state->cmd_byte & CMD_DISABLE)) ) { // keyboard is enabled
336 PushToOutputQueue(dev, scancode, OVERWRITE, DATA, KEYBOARD);
338 if (state->cmd_byte & CMD_INTR) {
339 keyboard_interrupt(KEYBOARD_IRQ, dev);
346 void deliver_mouse_to_vmm(uchar_t data[3])
348 struct vm_device * dev = demultiplex_injected_mouse(data);
349 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
351 PrintDebug("keyboard: injected mouse packet 0x %x %x %x\n",
352 data[0], data[1], data[2]);
354 memcpy(state->mouse_packet, data, 3);
356 state->status_byte |= STATUS_MOUSE_BUFFER_FULL;
359 switch (state->mouse_state) {
363 if (!(state->cmd_byte & CMD_MOUSE_DISABLE)) {
364 keyboard_interrupt(MOUSE_IRQ, dev);
374 int keyboard_reset_device(struct vm_device * dev)
376 struct keyboard_internal *data = (struct keyboard_internal *)(dev->private_data);
378 memset(data, 0, sizeof(struct keyboard_internal));
380 data->state = NORMAL;
381 data->mouse_state = STREAM1;
384 CMD_INTR // interrupts on
385 | CMD_MOUSE_INTR // mouse interupts on
386 | CMD_SYSTEM ; // self test passed
387 // PS2, keyboard+mouse enabled, generic translation
390 STATUS_SYSTEM // self-tests passed
391 | STATUS_ENABLED ; // keyboard ready
392 // buffers empty, no errors
394 data->output_byte = 0; // ?
396 data->input_byte = INPUT_RAM; // we have some
397 // also display=color, jumper 0, keyboard enabled
401 PrintDebug("keyboard: reset device\n");
409 int keyboard_start_device(struct vm_device *dev)
411 PrintDebug("keyboard: start device\n");
416 int keyboard_stop_device(struct vm_device *dev)
418 PrintDebug("keyboard: stop device\n");
423 int mouse_read_input(struct vm_device *dev)
425 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
427 if (state->mouse_needs_ack) {
428 state->mouse_needs_ack = 0;
430 // the ack has been stuffed previously
431 if (state->mouse_done_after_ack) {
438 switch (state->mouse_state) {
440 case RESET1: // requesting the BAT code
441 PushToOutputQueue(dev, 0xaa, OVERWRITE, DATA, MOUSE) ; // BAT successful
442 PrintDebug(" mouse sent BAT code (sucesssful) ");
443 state->mouse_state = RESET2;
445 return 0; // not done with mouse processing yet
448 case RESET2: // requesting the device id
449 PushToOutputQueue(dev, 0x00, OVERWRITE, DATA, MOUSE) ; // normal mouse type
450 PrintDebug(" mouse sent device id ");
451 state->mouse_state = STREAM1;
453 return 1; // done with mouse processing
456 case STREAM1: // send data
457 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
458 PrintDebug(" mouse sent stream data1 ");
459 state->mouse_state = STREAM2;
464 case STREAM2: // send data
465 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
466 PrintDebug(" mouse sent stream data2 ");
467 state->mouse_state = STREAM3;
472 case STREAM3: // send data
473 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
474 PrintDebug(" mouse sent stream data3 ");
475 state->mouse_state = STREAM1;
477 return 1; // now done
480 case REMOTE1: // send data
481 PushToOutputQueue(dev, state->mouse_packet[0], OVERWRITE, DATA, MOUSE);
482 PrintDebug(" mouse sent remote data1 ");
483 state->mouse_state = REMOTE2;
488 case REMOTE2: // send data
489 PushToOutputQueue(dev, state->mouse_packet[1], OVERWRITE, DATA, MOUSE);
490 PrintDebug(" mouse sent remote data2 ");
491 state->mouse_state = REMOTE3;
496 case REMOTE3: // send data
497 PushToOutputQueue(dev, state->mouse_packet[2], OVERWRITE, DATA, MOUSE);
498 PrintDebug(" mouse sent remote data3 ");
499 state->mouse_state = REMOTE1;
501 return 1; // now done
504 case STATUS1: // send data
505 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
506 PrintDebug(" mouse sent status data1 ");
507 state->mouse_state = STATUS2;
512 case STATUS2: // send data
513 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
514 PrintDebug(" mouse sent status data2 ");
515 state->mouse_state = STATUS3;
520 case STATUS3: // send data
521 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
522 PrintDebug(" mouse sent status data3 ");
523 state->mouse_state = STREAM1;
525 return 1; // now done
528 case DEVICE1: // send device id
529 PushToOutputQueue(dev, 0x0, OVERWRITE, DATA, MOUSE);
530 PrintDebug(" mouse sent device id ");
531 state->mouse_state = STREAM1;
533 return 1; // now done
537 PrintDebug(" mouse has no data ");
543 int mouse_write_output(struct vm_device * dev, uchar_t data)
545 struct keyboard_internal * state = (struct keyboard_internal *)(dev->private_data);
547 switch (state->mouse_state) {
557 PushToOutputQueue(dev, 0xfe, OVERWRITE, DATA, MOUSE) ; // no mouse!
558 PrintDebug(" mouse reset begins (no mouse) ");
560 return 1; // not done;
565 PushToOutputQueue(dev,0xfa,OVERWRITE,DATA,MOUSE) ;
566 PrintDebug(" mouse reset begins ");
567 state->mouse_done_after_ack=0;
568 state->mouse_needs_ack=1;
569 state->mouse_state=RESET1;
570 return 0; // not done;
574 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
575 PrintDebug(" mouse resend begins ");
576 state->mouse_done_after_ack = 0;
577 state->mouse_needs_ack = 0;
578 state->mouse_state = STREAM1;
579 return 0; // not done
582 case 0xf6: // set defaults
583 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
584 PrintDebug(" mouse set defaults ");
585 state->mouse_done_after_ack = 1;
586 state->mouse_needs_ack = 1;
587 state->mouse_state = STREAM1;
588 return 0; // not done
591 case 0xf5: // disable data reporting
592 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
593 state->mouse_done_after_ack = 1;
594 state->mouse_needs_ack = 1;
595 PrintDebug(" mouse disable data reporting ");
596 state->mouse_state = STREAM1;
597 return 0; // not done
600 case 0xf4: // enable data reporting
601 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
602 state->mouse_done_after_ack = 1;
603 state->mouse_needs_ack = 1;
604 PrintDebug(" mouse enable data reporting ");
605 state->mouse_state = STREAM1;
606 return 0; // not done
609 case 0xf3: // set sample rate
610 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
611 state->mouse_done_after_ack = 0;
612 state->mouse_needs_ack = 1;
613 PrintDebug(" mouse set sample rate begins ");
614 state->mouse_state = SAMPLE1;
615 return 0; // not done
618 case 0xf2: // get device id
619 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
620 state->mouse_done_after_ack = 0;
621 state->mouse_needs_ack = 1;
622 PrintDebug(" mouse get device id begins ");
623 state->mouse_state = DEVICE1;
624 return 0; // not done
627 case 0xf0: // set remote mode
628 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
629 state->mouse_done_after_ack = 1;
630 state->mouse_needs_ack = 1;
631 PrintDebug(" mouse set remote mode ");
632 state->mouse_state = REMOTE1;
633 return 0; // not done
636 case 0xee: // set wrap mode
637 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
638 state->mouse_done_after_ack = 1;
639 state->mouse_needs_ack = 1;
640 PrintDebug(" mouse set wrap mode (ignored) ");
641 state->mouse_state = STREAM1;
642 return 0; // not done
645 case 0xec: // reset wrap mode
646 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
647 state->mouse_done_after_ack = 1;
648 state->mouse_needs_ack = 1;
649 PrintDebug(" mouse reset wrap mode (ignored) ");
650 state->mouse_state = STREAM1;
654 case 0xeb: // read data
655 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
656 state->mouse_done_after_ack = 0;
657 state->mouse_needs_ack = 1;
658 PrintDebug(" mouse switch to wrap mode (ignored) ");
659 state->mouse_state = REMOTE1;
660 return 0; // not done
663 case 0xea: // set stream mode
664 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
665 state->mouse_done_after_ack = 1;
666 state->mouse_needs_ack = 1;
667 PrintDebug(" mouse set stream mode ");
668 state->mouse_state = STREAM1;
669 return 0; // not done
672 case 0xe9: // status request
673 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
674 state->mouse_done_after_ack = 0;
675 state->mouse_needs_ack = 1;
676 PrintDebug(" mouse status request begins ");
677 state->mouse_state = STATUS1;
681 case 0xe8: // set resolution
682 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
683 state->mouse_done_after_ack = 0;
684 state->mouse_needs_ack = 1;
685 PrintDebug(" mouse set resolution begins ");
686 state->mouse_state = SETRES1;
690 case 0xe7: // set scaling 2:1
691 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
692 state->mouse_done_after_ack = 1;
693 state->mouse_needs_ack = 1;
694 PrintDebug(" mouse set scaling 2:1 ");
695 state->mouse_state = STREAM1;
696 return 0; // not done
699 case 0xe6: // set scaling 1:1
700 PushToOutputQueue(dev, 0xfa, OVERWRITE, DATA, MOUSE) ;
701 state->mouse_done_after_ack = 1;
702 state->mouse_needs_ack = 1;
703 PrintDebug(" mouse set scaling 1:1 ");
704 state->mouse_state = STREAM1;
709 PrintDebug(" receiving unknown mouse command (0x%x) in acceptable state ", data);
716 PrintDebug(" receiving mouse output in unhandled state (0x%x) ", state->mouse_state);
722 PrintDebug(" HUH? ");
728 #if KEYBOARD_DEBUG_80H
729 int keyboard_write_delay(ushort_t port,
732 struct vm_device * dev)
736 PrintDebug("keyboard: write of 0x%x to 80h\n", *((uchar_t*)src));
740 PrintDebug("keyboard: write of >1 byte to 80h\n", *((uchar_t*)src));
746 int keyboard_read_delay(ushort_t port,
749 struct vm_device * dev)
753 *((uchar_t*)dest) = v3_inb(port);
755 PrintDebug("keyboard: read of 0x%x from 80h\n", *((uchar_t*)dest));
759 PrintDebug("keyboard: read of >1 byte from 80h\n");
770 int keyboard_write_command(ushort_t port,
773 struct vm_device * dev)
775 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
778 // Should always be single byte write
781 PrintError("keyboard: write of >1 bytes (%d) to 64h\n", length);
785 cmd = *((uchar_t*)src);
787 if (state->state != NORMAL) {
788 PrintDebug("keyboard: warning - receiving command on 64h but state != NORMAL\n");
791 PrintDebug("keyboard: command 0x%x on 64h\n", cmd);
795 case 0x20: // READ COMMAND BYTE (returned in 60h)
796 PushToOutputQueue(dev, state->cmd_byte, OVERWRITE, COMMAND,KEYBOARD);
797 state->state = NORMAL; // the next read on 0x60 will get the right data
798 PrintDebug("keyboard: command byte 0x%x returned\n", state->cmd_byte);
801 case 0x60: // WRITE COMMAND BYTE (read from 60h)
802 state->state = WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
803 PrintDebug("keyboard: prepare to write command byte\n");
806 // case 0x90-9f - write to output port (?)
808 case 0xa1: // Get version number
809 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
810 state->state = NORMAL;
811 PrintDebug("keyboard: version number 0x0 returned\n");
814 case 0xa4: // is password installed? send result to 0x60
815 // we don't support passwords
816 PushToOutputQueue(dev, 0xf1, OVERWRITE, COMMAND, KEYBOARD);
817 PrintDebug("keyboard: password not installed\n");
818 state->state = NORMAL;
821 case 0xa5: // new password will arrive on 0x60
822 state->state = TRANSMIT_PASSWD;
823 PrintDebug("keyboard: pepare to transmit password\n");
826 case 0xa6: // check passwd;
827 // since we do not support passwords, we will simply ignore this
828 // the implication is that any password check immediately succeeds
829 // with a blank password
830 state->state = NORMAL;
831 PrintDebug("keyboard: password check succeeded\n");
834 case 0xa7: // disable mouse
835 state->cmd_byte |= CMD_MOUSE_DISABLE;
836 state->state = NORMAL;
837 PrintDebug("keyboard: mouse disabled\n");
840 case 0xa8: // enable mouse
841 state->cmd_byte &= ~CMD_MOUSE_DISABLE;
842 state->state = NORMAL;
843 PrintDebug("keyboard: mouse enabled\n");
846 case 0xa9: // mouse interface test (always succeeds)
847 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
848 PrintDebug("keyboard: mouse interface test succeeded\n");
849 state->state = NORMAL;
852 case 0xaa: // controller self test (always succeeds)
853 PushToOutputQueue(dev, 0x55, OVERWRITE, COMMAND, KEYBOARD);
854 PrintDebug("keyboard: controller self test succeeded\n");
855 state->state = NORMAL;
858 case 0xab: // keyboard interface test (always succeeds)
859 PushToOutputQueue(dev, 0, OVERWRITE, COMMAND, KEYBOARD);
860 state->state = NORMAL;
861 PrintDebug("keyboard: keyboard interface test succeeded\n");
864 case 0xad: // disable keyboard
865 state->cmd_byte |= CMD_DISABLE;
866 state->state = NORMAL;
867 PrintDebug("keyboard: keyboard disabled\n");
870 case 0xae: // enable keyboard
871 state->cmd_byte &= ~CMD_DISABLE;
872 state->state = NORMAL;
873 PrintDebug("keyboard: keyboard enabled\n");
876 case 0xaf: // get version
877 PushToOutputQueue(dev, 0x00, OVERWRITE, COMMAND, KEYBOARD);
878 state->state = NORMAL;
879 PrintDebug("keyboard: version 0 returned \n");
882 case 0xd0: // return microcontroller output on 60h
883 PushToOutputQueue(dev, state->output_byte, OVERWRITE, COMMAND, KEYBOARD);
884 state->state = NORMAL;
885 PrintDebug("keyboard: output byte 0x%x returned\n", state->output_byte);
888 case 0xd1: // request to write next byte on 60h to the microcontroller output port
889 state->state = WRITING_OUTPUT_PORT;
890 PrintDebug("keyboard: prepare to write output byte\n");
893 case 0xd2: // write keyboard buffer (inject key)
894 state->state = INJECTING_KEY;
895 PrintDebug("keyboard: prepare to inject key\n");
898 case 0xd3: // write mouse buffer (inject mouse)
899 state->state = INJECTING_MOUSE;
900 PrintDebug("keyboard: prepare to inject mouse\n");
903 case 0xd4: // write mouse device (command to mouse?)
904 state->state = IN_MOUSE;
905 PrintDebug("keyboard: prepare to inject mouse command\n");
908 case 0xc0: // read input port
909 PushToOutputQueue(dev, state->input_byte, OVERWRITE, COMMAND, KEYBOARD);
910 state->state = NORMAL;
911 PrintDebug("keyboard: input byte 0x%x returned\n", state->input_byte);
914 case 0xc1: //copy input port lsn to status msn
915 state->status_byte &= 0x0f;
916 state->status_byte |= (state->input_byte & 0xf) << 4;
917 state->state = NORMAL;
918 PrintDebug("keyboard: copied input byte lsn to status msn\n");
921 case 0xc2: // copy input port msn to status msn
922 state->status_byte &= 0x0f;
923 state->status_byte |= (state->input_byte & 0xf0);
924 state->state = NORMAL;
925 PrintDebug("keyboard: copied input byte msn to status msn\n");
928 case 0xe0: // read test port
929 PushToOutputQueue(dev, state->output_byte >> 6, OVERWRITE, COMMAND, KEYBOARD);
930 state->state = NORMAL;
931 PrintDebug("keyboard: read 0x%x from test port\n", state->output_byte >> 6);
935 case 0xf0: // pulse output port
936 case 0xf1: // this should pulse 0..3 of cmd_byte on output port
937 case 0xf2: // instead of what is currently in output_byte (I think)
938 case 0xf3: // main effect is taht if bit zero is zero
939 case 0xf4: // should cause reset
940 case 0xf5: // I doubt anything more recent than a 286 running
941 case 0xf6: // OS2 with the penalty box will care
952 PrintDebug("keyboard: ignoring pulse of 0x%x (low=pulsed) on output port\n", (cmd & 0xf));
953 state->state = NORMAL;
957 // case ac diagonstic - returns 16 bytes from keyboard microcontroler on 60h
959 PrintDebug("keyboard: ignoring command (unimplemented)\n");
960 state->state = NORMAL;
968 int keyboard_read_status(ushort_t port,
971 struct vm_device * dev)
973 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
977 PrintDebug("keyboard: read status (64h): ");
979 *((uchar_t*)dest) = state->status_byte;
981 PrintDebug("0x%x\n", *((uchar_t*)dest));
985 PrintError("keyboard: >1 byte read for status (64h)\n");
990 int keyboard_write_output(ushort_t port,
993 struct vm_device * dev)
995 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
998 PrintError("keyboard: write of 60h with >1 byte\n");
1002 uchar_t data = *((uchar_t*)src);
1004 PrintDebug("keyboard: output 0x%x on 60h\n", data);
1006 switch (state->state) {
1007 case WRITING_CMD_BYTE:
1008 state->cmd_byte = data;
1009 state->state = NORMAL;
1010 PrintDebug("keyboard: wrote new command byte 0x%x\n", state->cmd_byte);
1012 case WRITING_OUTPUT_PORT:
1013 state->output_byte = data;
1014 state->state = NORMAL;
1015 PrintDebug("keyboard: wrote new output byte 0x%x\n", state->output_byte);
1018 PushToOutputQueue(dev, data, OVERWRITE, COMMAND, KEYBOARD); // probably should be a call to deliver_key_to_vmm()
1019 state->state = NORMAL;
1020 PrintDebug("keyboard: injected key 0x%x\n", data);
1022 case INJECTING_MOUSE:
1023 PrintDebug("keyboard: ignoring injected mouse event 0x%x\n", data);
1024 state->state = NORMAL;
1027 PrintDebug("keyboard: mouse action: ");
1028 if (mouse_write_output(dev, data)) {
1029 state->state = NORMAL;
1033 case TRANSMIT_PASSWD:
1036 PrintDebug("keyboard: ignoring password character 0x%x\n",data);
1039 state->state = NORMAL;
1040 PrintDebug("keyboard: done with password\n");
1045 // command is being sent to keyboard controller
1048 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD); // ack
1049 state->state = RESET;
1050 PrintDebug("keyboard: reset complete and acked\n", data);
1052 case 0xf5: // disable scanning
1053 case 0xf4: // enable scanning
1055 PushToOutputQueue(dev, 0xfa, OVERWRITE, COMMAND, KEYBOARD);
1056 // should do something here... PAD
1057 state->state = NORMAL;
1058 PrintDebug("keyboard: %s scanning done and acked\n",data==0xf5 ? "disable" : "enable", data);
1060 case 0xfe: // resend
1061 case 0xfd: // set key type make
1062 case 0xfc: // set key typ make/break
1063 case 0xfb: // set key type typematic
1064 case 0xfa: // set all typematic make/break/typematic
1065 case 0xf9: // set all make
1066 case 0xf8: // set all make/break
1067 case 0xf7: // set all typemaktic
1068 case 0xf6: // set defaults
1069 case 0xf3: // set typematic delay/rate
1070 PrintDebug("keyboard: unhandled known command 0x%x on output buffer (60h)\n", data);
1073 PrintDebug("keyboard: unhandled unknown command 0x%x on output buffer (60h)\n", data);
1074 state->status_byte |= 0x1;
1080 PrintDebug("keyboard: unknown state %x on command 0x%x on output buffer (60h)\n", state->state, data);
1086 int keyboard_read_input(ushort_t port,
1089 struct vm_device * dev)
1091 struct keyboard_internal *state = (struct keyboard_internal *)(dev->private_data);
1097 PrintDebug("keyboard: read from input (60h): ");
1099 if (state->state == IN_MOUSE) {
1100 done_mouse = mouse_read_input(dev);
1102 state->state = NORMAL;
1106 PullFromOutputQueue(dev, &data);
1108 if (state->state == RESET) {
1109 // We just delivered the ack for the reset
1110 // now we will ready ourselves to deliver the BAT code (success)
1111 PushToOutputQueue(dev, 0xaa, OVERWRITE, COMMAND, KEYBOARD);
1112 state->state = NORMAL;
1113 PrintDebug(" (in reset, pushing BAT test code 0xaa) ");
1116 PrintDebug("0x%x\n", data);
1118 *((uchar_t*)dest) = data;
1122 PrintError("keyboard: unknown size read from input (60h)\n");
1128 int keyboard_interrupt(uint_t irq, struct vm_device * dev)
1130 PrintDebug("keyboard: interrupt 0x%x\n", irq);
1132 dev->vm->vm_ops.raise_irq(dev->vm, irq);
1139 int keyboard_init_device(struct vm_device * dev)
1142 // struct keyboard_internal *data = (struct keyboard_internal *) dev->private_data;
1144 PrintDebug("keyboard: init_device\n");
1146 // Would read state here
1148 keyboard_reset_device(dev);
1151 dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
1152 dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
1154 #if KEYBOARD_DEBUG_80H
1155 dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
1160 // We do not hook the IRQ here. Instead, the underlying device driver
1161 // is responsible to call us back
1167 int keyboard_deinit_device(struct vm_device *dev)
1170 dev_unhook_io(dev, KEYBOARD_60H);
1171 dev_unhook_io(dev, KEYBOARD_64H);
1172 #if KEYBOARD_DEBUG_80H
1173 dev_unhook_io(dev, KEYBOARD_DELAY_80H);
1175 keyboard_reset_device(dev);
1183 static struct vm_device_ops dev_ops = {
1184 .init = keyboard_init_device,
1185 .deinit = keyboard_deinit_device,
1186 .reset = keyboard_reset_device,
1187 .start = keyboard_start_device,
1188 .stop = keyboard_stop_device,
1194 struct vm_device *create_keyboard() {
1196 if (thekeyboard != NULL) {
1197 PrintDebug("keyboard: creating >1 keyboard device. This will probably fail!\n");
1200 struct keyboard_internal * keyboard_state = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
1202 struct vm_device *device = create_device("KEYBOARD", &dev_ops, keyboard_state);
1204 thekeyboard = device;