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 <palacios/vmm.h>
21 #include <palacios/vmm_dev_mgr.h>
22 #include <palacios/vmm_types.h>
24 #include <palacios/vmm_ringbuffer.h>
25 #include <palacios/vmm_lock.h>
26 #include <palacios/vmm_intr.h>
27 #include <palacios/vmm_host_events.h>
28 #include <palacios/vm_guest.h>
31 #ifndef CONFIG_DEBUG_KEYBOARD
33 #define PrintDebug(fmt, args...)
36 #define KEYBOARD_DEBUG_80H 0
40 #define KEYBOARD_60H 0x60 // keyboard microcontroller
41 #define KEYBOARD_64H 0x64 // onboard microcontroller
43 #define KEYBOARD_DELAY_80H 0x80 // written for timing
45 #define KEYBOARD_IRQ 0x1
50 // 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 #define MOUSE_ACK 0xfa
74 // for queue operations
78 // for queue operations - whether it's data or cmd waiting on 60h
82 // for queue operations - whether this is keyboard or mouse data on 60h
92 uint8_t irq_en : 1; // 1=interrupts enabled
93 uint8_t mouse_irq_en : 1; // 1=interrupts enabled for mouse
94 uint8_t self_test_ok : 1; // 1= self test passed
95 uint8_t override : 1; // MBZ for PS2
96 uint8_t disable : 1; // 1=disabled keyboard
97 uint8_t mouse_disable : 1; // 1=disabled mouse
98 uint8_t translate : 1; // 1=translate to set 1 scancodes (For PC Compatibility)
99 uint8_t rsvd : 1; // must be zero
100 } __attribute__((packed));
101 } __attribute__((packed));
102 } __attribute__((packed));
111 uint8_t out_buf_full : 1; // 1=full (data for system)
112 uint8_t in_buf_full : 1; // 1=full (data for 8042)
113 uint8_t self_test_ok : 1; // 1=self-test-passed
114 uint8_t cmd : 1; // 0=data on 60h, 1=cmd on 64h
115 uint8_t enabled : 1; // 1=keyboard is enabled
116 uint8_t mouse_buf_full : 1; // 1= mouse output buffer full
117 uint8_t timeout_err : 1; // 1=timeout of keybd
118 uint8_t parity_err : 1; // 1=parity error
119 } __attribute__((packed));
120 } __attribute__((packed));
121 } __attribute__((packed));
128 /* This QUEUE_SIZE must be 256 */
129 /* Its designed this way to cause the start/end index to automatically
130 wrap around (2^8 = 256) so an overrun will automatically readjust the
133 #define QUEUE_SIZE 256
135 uint8_t queue[QUEUE_SIZE];
142 struct keyboard_internal {
144 // 0x60 is the port for the keyboard microcontroller
145 // writes are commands
146 // reads from it usually return scancodes
147 // however, it can also return other data
148 // depending on the state of the onboard microcontroller
150 // 0x64 is the port for the onboard microcontroller
151 // writes are commands
155 // state of the onboard microcontroller
156 // this is needed because sometimes 0x60 reads come
157 // from the onboard microcontroller
158 enum {// Normal mode measn we deliver keys
159 // to the vm and accept commands from it
161 // after receiving cmd 0x60
162 // keybaord uC cmd will subsequently arrive
164 // after recieving 0xa5
165 // password arrives on data port, null terminated
167 // after having a d1 sent to 64
168 // we wait for a new output byte on 60
170 // after having a d2 sent to 64
171 // we wait for a new output byte on 60
172 // then make it available as a keystroke
174 // after having a d3 sent to 64
175 // we wait for a new output byte on 60
176 // then make it available as a mouse event
178 // after having a d4 sent to 64
179 // we wait for a new output byte on 60
180 // then send it to the mouse
182 // After the Keyboard LEDs are enabled
183 // we wait for the output byte on 64?
185 // After the Keyboard SET_RATE is called
186 // we wait for the output byte on 64?
192 // Normal mouse state
194 // this is used for setting sample rate
203 struct status_reg status;
205 uint8_t output_byte; // output port of onboard uC (e.g. A20)
206 uint8_t input_byte; // input port of onboard uC
213 struct queue kbd_queue;
214 struct queue mouse_queue;
216 struct v3_vm_info * vm;
222 static int update_kb_irq(struct keyboard_internal * state) {
226 state->status.out_buf_full = 0;
227 state->status.mouse_buf_full = 0;
230 // If there is pending Keyboard data then it overrides mouse data
231 if (state->kbd_queue.count > 0) {
232 irq_num = KEYBOARD_IRQ;
233 } else if (state->mouse_queue.count > 0) {
235 state->status.mouse_buf_full = 1;
238 PrintDebug("keyboard: interrupt 0x%d\n", irq_num);
241 // Global output buffer flag (for both Keyboard and mouse)
242 state->status.out_buf_full = 1;
244 if (state->cmd.irq_en == 1) {
245 v3_raise_irq(state->vm, irq_num);
254 /* Only one byte is read per irq
255 * So if the queue is still full after a data read, we re-raise the irq
256 * If we keep reading an empty queue we return the last queue entry
259 static int push_to_output_queue(struct keyboard_internal * state, uint8_t value, uint8_t cmd, uint8_t mouse) {
260 struct queue * q = NULL;
264 q = &(state->mouse_queue);
266 q = &(state->kbd_queue);
269 if (q->count >= QUEUE_SIZE) {
274 state->status.cmd = 1;
276 state->status.cmd = 0;
279 q->queue[q->end] = value;
281 if (q->end >= (QUEUE_SIZE - 1)) {
290 update_kb_irq(state);
297 static int pull_from_output_queue(struct keyboard_internal * state, uint8_t * value) {
298 struct queue * q = NULL;
300 if (state->kbd_queue.count > 0) {
301 q = &(state->kbd_queue);
302 PrintDebug("Reading from Keyboard Queue\n");
303 } else if (state->mouse_queue.count > 0) {
304 q = &(state->mouse_queue);
305 PrintDebug("Reading from Mouse Queue\n");
307 uint8_t idx = state->kbd_queue.start - 1;
308 PrintDebug("No Data in any queue\n");
309 *value = state->kbd_queue.queue[idx];
313 *value = q->queue[q->start];
315 if (q->start >= (QUEUE_SIZE - 1)) {
324 PrintDebug("Read from Queue: %x\n", *value);
325 PrintDebug("QStart=%d, QEnd=%d\n", q->start, q->end);
327 update_kb_irq(state);
333 #include <palacios/vmm_telemetry.h>
335 #include <palacios/vmm_symmod.h>
338 static int key_event_handler(struct v3_vm_info * vm,
339 struct v3_keyboard_event * evt,
340 void * private_data) {
341 struct keyboard_internal * state = (struct keyboard_internal *)private_data;
343 PrintDebug("keyboard: injected status 0x%x, and scancode 0x%x\n", evt->status, evt->scan_code);
345 if (evt->scan_code == 0x44) { // F10 debug dump
347 for (i = 0; i < vm->num_cores; i++) {
348 v3_print_guest_state(&(vm->cores[i]));
350 // PrintGuestPageTables(info, info->shdw_pg_state.guest_cr3);
352 #ifdef CONFIG_SYMCALL
353 else if (evt->scan_code == 0x43) { // F9 Sym test
354 struct guest_info * core = &(vm->cores[0]);
355 PrintDebug("Testing sym call\n");
356 sym_arg_t a0 = 0x1111;
357 sym_arg_t a1 = 0x2222;
358 sym_arg_t a2 = 0x3333;
359 sym_arg_t a3 = 0x4444;
360 sym_arg_t a4 = 0x5555;
361 uint64_t call_start = 0;
362 uint64_t call_end = 0;
364 V3_Print("Exits before symcall: %d\n", (uint32_t)core->num_exits);
367 v3_sym_call5(core, SYMCALL_TEST, &a0, &a1, &a2, &a3, &a4);
370 V3_Print("Symcall latency = %d cycles (%d exits)\n", (uint32_t)(call_end - call_start), (uint32_t)core->num_exits);
372 V3_Print("Symcall Test Returned arg0=%x, arg1=%x, arg2=%x, arg3=%x, arg4=%x\n",
373 (uint32_t)a0, (uint32_t)a1, (uint32_t)a2, (uint32_t)a3, (uint32_t)a4);
377 else if (evt->scan_code == 0x42) { // F8 debug toggle
378 extern int v3_dbg_enable;
380 PrintDebug("Toggling Debugging\n");
384 #ifdef CONFIG_TELEMETRY
386 else if (evt->scan_code == 0x41) { // F7 telemetry dump
387 v3_print_telemetry(vm);
391 else if (evt->scan_code == 0x40) { // F6 Test symmod load
392 v3_load_sym_capsule(vm, "lnx_test");
397 addr_t irq_state = v3_lock_irqsave(state->kb_lock);
399 if ( (state->status.enabled == 1) // onboard is enabled
400 && (state->cmd.disable == 0) ) { // keyboard is enabled
402 push_to_output_queue(state, evt->scan_code, DATA, KEYBOARD);
405 v3_unlock_irqrestore(state->kb_lock, irq_state);
411 static int mouse_event_handler(struct v3_vm_info * vm,
412 struct v3_mouse_event * evt,
413 void * private_data) {
414 struct keyboard_internal * kbd = (struct keyboard_internal *)private_data;
417 PrintDebug("keyboard: injected mouse packet 0x %x %x %x\n",
418 evt->data[0], evt->data[1], evt->data[2]);
420 addr_t irq_state = v3_lock_irqsave(kbd->kb_lock);
422 switch (kbd->mouse_state) {
425 if (kbd->cmd.mouse_disable == 0) {
426 push_to_output_queue(kbd, evt->data[0], DATA, MOUSE);
427 push_to_output_queue(kbd, evt->data[1], DATA, MOUSE);
428 push_to_output_queue(kbd, evt->data[2], DATA, MOUSE);
432 PrintError("Invalid mouse state\n");
438 v3_unlock_irqrestore(kbd->kb_lock, irq_state);
444 static int keyboard_reset_device(struct keyboard_internal * kbd) {
446 memset(kbd, 0, sizeof(struct keyboard_internal));
449 kbd->mouse_state = STREAM;
452 // PS2, keyboard+mouse enabled, generic translation
456 kbd->cmd.mouse_irq_en = 1;
457 kbd->cmd.self_test_ok = 1;
461 // buffers empty, no errors
464 kbd->status.self_test_ok = 1; // self-tests passed
465 kbd->status.enabled = 1;// keyboard ready
469 kbd->output_byte = 0; // ?
471 kbd->input_byte = INPUT_RAM; // we have some
472 // also display=color, jumper 0, keyboard enabled
474 PrintDebug("keyboard: reset device\n");
486 static int mouse_write_output(struct keyboard_internal * kbd, uint8_t data) {
487 switch (kbd->mouse_state) {
492 if (kbd->mouse_enabled == 0) {
493 push_to_output_queue(kbd, 0xfe, DATA, MOUSE) ; // no mouse!
495 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
496 push_to_output_queue(kbd, 0xaa, DATA, MOUSE) ;
497 push_to_output_queue(kbd, 0x00, DATA, MOUSE) ;
501 /* case 0xfe: //resend */
502 /* PushToOutputQueue(kbd, 0xfa, OVERWRITE, DATA, MOUSE) ; */
503 /* PrintDebug(" mouse resend begins "); */
504 /* kbd->mouse_done_after_ack = 0; */
505 /* kbd->mouse_needs_ack = 0; */
506 /* kbd->mouse_state = STREAM1; */
507 /* return 0; // not done */
510 case 0xf6: // set defaults
511 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
512 PrintDebug(" mouse set defaults ");
516 case 0xf5: // disable data reporting
517 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
518 PrintDebug(" mouse disable data reporting ");
521 case 0xf4: // enable data reporting
522 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
523 PrintDebug(" mouse enable data reporting ");
526 case 0xf3: // set sample rate
527 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
528 kbd->mouse_state = SAMPLE;
529 PrintDebug(" mouse set sample rate begins ");
532 case 0xf2: // get device id
533 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
534 push_to_output_queue(kbd, 0x0, DATA, MOUSE);
535 PrintDebug(" mouse get device id begins ");
538 case 0xf0: // set remote mode
539 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
540 PrintDebug(" mouse set remote mode ");
543 case 0xee: // set wrap mode
544 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
545 PrintError(" mouse set wrap mode (ignored) ");
548 case 0xec: // reset wrap mode
549 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
550 PrintError(" mouse reset wrap mode (ignored) ");
553 case 0xeb: // read data
554 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
555 PrintError(" mouse switch to wrap mode (ignored) ");
558 case 0xea: // set stream mode
559 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
560 PrintDebug(" mouse set stream mode ");
563 case 0xe9: // status request
564 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
565 push_to_output_queue(kbd, 0x00, DATA, MOUSE);
566 push_to_output_queue(kbd, 0x00, DATA, MOUSE);
567 push_to_output_queue(kbd, 0x00, DATA, MOUSE);
568 PrintDebug(" mouse status request begins ");
571 case 0xe8: // set resolution
572 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
573 PrintDebug(" mouse set resolution begins ");
574 kbd->mouse_state = SET_RES;
577 case 0xe7: // set scaling 2:1
578 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
579 PrintDebug(" mouse set scaling 2:1 ");
582 case 0xe6: // set scaling 1:1
583 push_to_output_queue(kbd, MOUSE_ACK, DATA, MOUSE) ;
584 PrintDebug(" mouse set scaling 1:1 ");
588 PrintDebug(" receiving unknown mouse command (0x%x) in acceptable kbd ", data);
596 PrintDebug(" receiving mouse output in unhandled kbd (0x%x) ", kbd->mouse_state);
605 #if KEYBOARD_DEBUG_80H
606 static int keyboard_write_delay(ushort_t port, void * src, uint_t length, void * priv_data) {
609 PrintDebug("keyboard: write of 0x%x to 80h\n", *((uint8_t*)src));
612 PrintDebug("keyboard: write of >1 byte to 80h\n", *((uint8_t*)src));
617 static int keyboard_read_delay(struct guest_info * core, ushort_t port, void * dest, uint_t length, void * priv_data) {
620 *(uint8_t *)dest = v3_inb(port);
622 PrintDebug("keyboard: read of 0x%x from 80h\n", *((uint8_t*)dest));
626 PrintDebug("keyboard: read of >1 byte from 80h\n");
637 static int keyboard_write_command(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
638 struct keyboard_internal * kbd = priv_data;
639 uint8_t cmd = *(uint8_t *)src;
641 // Should always be single byte write
643 PrintError("keyboard: write of >1 bytes (%d) to 64h\n", length);
648 addr_t irq_state = v3_lock_irqsave(kbd->kb_lock);
650 if (kbd->state != NORMAL) {
651 PrintDebug("keyboard: warning - receiving command on 64h but state != NORMAL\n");
654 PrintDebug("keyboard: command 0x%x on 64h\n", cmd);
657 case 0x20: // READ COMMAND BYTE (returned in 60h)
658 push_to_output_queue(kbd, kbd->cmd.val, COMMAND, KEYBOARD);
659 PrintDebug("keyboard: command byte 0x%x returned\n", kbd->cmd.val);
662 case 0x60: // WRITE COMMAND BYTE (read from 60h)
663 kbd->state = WRITING_CMD_BYTE; // we need to make sure we send the next 0x60 byte appropriately
664 PrintDebug("keyboard: prepare to write command byte\n");
667 // case 0x90-9f - write to output port (?)
669 case 0xa1: // Get version number
670 push_to_output_queue(kbd, 0x00, COMMAND, KEYBOARD);
671 PrintDebug("keyboard: version number 0x0 returned\n");
674 case 0xa4: // is password installed? send result to 0x60
675 // we don't support passwords
676 push_to_output_queue(kbd, 0xf1, COMMAND, KEYBOARD);
677 PrintDebug("keyboard: password not installed\n");
680 case 0xa5: // new password will arrive on 0x60
681 kbd->state = TRANSMIT_PASSWD;
682 PrintDebug("keyboard: pepare to transmit password\n");
685 case 0xa6: // check passwd;
686 // since we do not support passwords, we will simply ignore this
687 // the implication is that any password check immediately succeeds
688 // with a blank password
689 PrintDebug("keyboard: password check succeeded\n");
692 case 0xa7: // disable mouse
693 kbd->cmd.mouse_disable = 1;
694 PrintDebug("keyboard: mouse disabled\n");
697 case 0xa8: // enable mouse
698 kbd->cmd.mouse_disable = 0;
699 PrintDebug("keyboard: mouse enabled\n");
702 case 0xa9: // mouse interface test (always succeeds)
703 push_to_output_queue(kbd, 0x00, COMMAND, KEYBOARD);
704 PrintDebug("keyboard: mouse interface test succeeded\n");
707 case 0xaa: // controller self test (always succeeds)
708 push_to_output_queue(kbd, 0x55, COMMAND, KEYBOARD);
709 PrintDebug("keyboard: controller self test succeeded\n");
712 case 0xab: // keyboard interface test (always succeeds)
713 push_to_output_queue(kbd, 0, COMMAND, KEYBOARD);
714 PrintDebug("keyboard: keyboard interface test succeeded\n");
717 case 0xad: // disable keyboard
718 kbd->cmd.disable = 1;
719 PrintDebug("keyboard: keyboard disabled\n");
722 case 0xae: // enable keyboard
723 kbd->cmd.disable = 0;
724 PrintDebug("keyboard: keyboard enabled\n");
727 case 0xaf: // get version
728 push_to_output_queue(kbd, 0x00, COMMAND, KEYBOARD);
729 PrintDebug("keyboard: version 0 returned \n");
732 case 0xd0: // return microcontroller output on 60h
733 push_to_output_queue(kbd, kbd->output_byte, COMMAND, KEYBOARD);
734 PrintDebug("keyboard: output byte 0x%x returned\n", kbd->output_byte);
737 case 0xd1: // request to write next byte on 60h to the microcontroller output port
738 kbd->state = WRITING_OUTPUT_PORT;
739 PrintDebug("keyboard: prepare to write output byte\n");
742 case 0xd2: // write keyboard buffer (inject key)
743 kbd->state = INJECTING_KEY;
744 PrintDebug("keyboard: prepare to inject key\n");
747 case 0xd3: // write mouse buffer (inject mouse)
748 kbd->state = INJECTING_MOUSE;
749 PrintDebug("keyboard: prepare to inject mouse\n");
752 case 0xd4: // write mouse device (command to mouse?)
753 kbd->state = IN_MOUSE;
754 PrintDebug("keyboard: prepare to inject mouse command\n");
757 case 0xc0: // read input port
758 push_to_output_queue(kbd, kbd->input_byte, COMMAND, KEYBOARD);
759 PrintDebug("keyboard: input byte 0x%x returned\n", kbd->input_byte);
762 case 0xc1: //copy input port lsn to status msn
763 kbd->status.val &= 0x0f;
764 kbd->status.val |= (kbd->input_byte & 0xf) << 4;
765 PrintDebug("keyboard: copied input byte low 4 bits to status reg hi 4 bits\n");
768 case 0xc2: // copy input port msn to status msn
769 kbd->status.val &= 0x0f;
770 kbd->status.val |= (kbd->input_byte & 0xf0);
771 PrintDebug("keyboard: copied input byte hi 4 bits to status reg hi 4 bits\n");
774 case 0xe0: // read test port
775 push_to_output_queue(kbd, kbd->output_byte >> 6, COMMAND, KEYBOARD);
776 PrintDebug("keyboard: read 0x%x from test port\n", kbd->output_byte >> 6);
780 case 0xf0: // pulse output port
781 case 0xf1: // this should pulse 0..3 of cmd_byte on output port
782 case 0xf2: // instead of what is currently in output_byte (I think)
783 case 0xf3: // main effect is taht if bit zero is zero
784 case 0xf4: // should cause reset
785 case 0xf5: // I doubt anything more recent than a 286 running
786 case 0xf6: // OS2 with the penalty box will care
796 PrintDebug("keyboard: ignoring pulse of 0x%x (low=pulsed) on output port\n", (cmd & 0xf));
799 // case ac diagonstic - returns 16 bytes from keyboard microcontroler on 60h
801 PrintDebug("keyboard: ignoring command (unimplemented)\n");
805 v3_unlock_irqrestore(kbd->kb_lock, irq_state);
810 static int keyboard_read_status(struct guest_info * core, ushort_t port, void * dest, uint_t length, void * priv_data) {
811 struct keyboard_internal * kbd = priv_data;
814 PrintError("keyboard: >1 byte read for status (64h)\n");
818 PrintDebug("keyboard: read status (64h): ");
820 addr_t irq_state = v3_lock_irqsave(kbd->kb_lock);
822 *(uint8_t *)dest = kbd->status.val;
824 v3_unlock_irqrestore(kbd->kb_lock, irq_state);
826 PrintDebug("0x%x\n", *(uint8_t *)dest);
831 static int keyboard_write_output(struct guest_info * core, ushort_t port, void * src, uint_t length, void * priv_data) {
832 struct keyboard_internal * kbd = priv_data;
836 PrintError("keyboard: write of 60h with >1 byte\n");
840 uint8_t data = *(uint8_t *)src;
842 PrintDebug("keyboard: output 0x%x on 60h\n", data);
844 addr_t irq_state = v3_lock_irqsave(kbd->kb_lock);
846 switch (kbd->state) {
847 case WRITING_CMD_BYTE:
850 PrintDebug("keyboard: wrote new command byte 0x%x\n", kbd->cmd.val);
853 case WRITING_OUTPUT_PORT:
854 kbd->output_byte = data;
856 PrintDebug("keyboard: wrote new output byte 0x%x\n", kbd->output_byte);
860 push_to_output_queue(kbd, data, COMMAND, KEYBOARD); // probably should be a call to deliver_key_to_vmm()
862 PrintDebug("keyboard: injected key 0x%x\n", data);
865 case INJECTING_MOUSE:
866 push_to_output_queue(kbd, data, DATA, MOUSE);
867 // PrintDebug("keyboard: ignoring injected mouse event 0x%x\n", data);
868 PrintDebug("keyboard: injected mouse event 0x%x\n", data);
873 PrintDebug("keyboard: mouse action: ");
874 if (mouse_write_output(kbd, data)) {
880 case TRANSMIT_PASSWD:
883 PrintDebug("keyboard: ignoring password character 0x%x\n",data);
887 PrintDebug("keyboard: done with password\n");
892 PrintDebug("Keyboard: LEDs being set...\n");
893 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
898 PrintDebug("Keyboard: Rate being set...\n");
899 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
905 // command is being sent to keyboard controller
908 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD); // ack
909 push_to_output_queue(kbd, 0xaa, COMMAND, KEYBOARD);
910 PrintDebug("keyboard: reset complete and acked\n");
913 case 0xf5: // disable scanning
914 case 0xf4: // enable scanning
916 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
917 // should do something here... PAD
918 PrintDebug("keyboard: %s scanning done and acked\n", (data == 0xf5) ? "disable" : "enable");
922 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
923 kbd->state = SET_RATE;
926 case 0xf2: // get keyboard ID
927 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
928 push_to_output_queue(kbd, 0xab, COMMAND, KEYBOARD);
929 push_to_output_queue(kbd, 0x83, COMMAND, KEYBOARD);
930 PrintDebug("Keyboard: Requesting Keyboard ID\n");
933 case 0xed: // enable keyboard LEDs
934 push_to_output_queue(kbd, 0xfa, COMMAND, KEYBOARD);
935 kbd->state = SET_LEDS;
938 case 0xee: // echo, used by FreeBSD to probe controller
939 push_to_output_queue(kbd, 0xee, COMMAND, KEYBOARD);
943 case 0xfd: // set key type make
944 case 0xfc: // set key typ make/break
945 case 0xfb: // set key type typematic
946 case 0xfa: // set all typematic make/break/typematic
947 case 0xf9: // set all make
948 case 0xf8: // set all make/break
949 case 0xf7: // set all typemaktic
950 case 0xf6: // set defaults
951 PrintError("keyboard: unhandled known command 0x%x on output buffer (60h)\n", data);
956 PrintError("keyboard: unhandled unknown command 0x%x on output buffer (60h)\n", data);
957 kbd->status.out_buf_full = 1;
965 v3_unlock_irqrestore(kbd->kb_lock, irq_state);
970 static int keyboard_read_input(struct guest_info * core, ushort_t port, void * dest, uint_t length, void * priv_data) {
971 struct keyboard_internal * kbd = priv_data;
974 PrintError("keyboard: unknown size read from input (60h)\n");
978 addr_t irq_state = v3_lock_irqsave(kbd->kb_lock);
980 pull_from_output_queue(kbd, (uint8_t *)dest);
982 v3_unlock_irqrestore(kbd->kb_lock, irq_state);
984 PrintDebug("keyboard: read from input (60h): 0x%x\n", *(uint8_t *)dest);
994 static int keyboard_free(struct vm_device * dev) {
1003 static struct v3_device_ops dev_ops = {
1004 .free = keyboard_free,
1011 static int keyboard_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
1012 struct keyboard_internal * kbd = NULL;
1013 char * dev_id = v3_cfg_val(cfg, "ID");
1015 PrintDebug("keyboard: init_device\n");
1017 kbd = (struct keyboard_internal *)V3_Malloc(sizeof(struct keyboard_internal));
1019 memset(kbd, 0, sizeof(struct keyboard_internal));
1023 kbd->mouse_queue.start = 0;
1024 kbd->mouse_queue.end = 0;
1025 kbd->mouse_queue.count = 0;
1027 kbd->kbd_queue.start = 0;
1028 kbd->kbd_queue.end = 0;
1029 kbd->kbd_queue.count = 0;
1031 kbd->mouse_enabled = 0;
1033 struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, kbd);
1035 if (v3_attach_device(vm, dev) == -1) {
1036 PrintError("Could not attach device %s\n", dev_id);
1040 keyboard_reset_device(kbd);
1043 v3_lock_init(&(kbd->kb_lock));
1047 v3_dev_hook_io(dev, KEYBOARD_64H, &keyboard_read_status, &keyboard_write_command);
1048 v3_dev_hook_io(dev, KEYBOARD_60H, &keyboard_read_input, &keyboard_write_output);
1050 v3_hook_host_event(vm, HOST_KEYBOARD_EVT, V3_HOST_EVENT_HANDLER(key_event_handler), dev);
1051 v3_hook_host_event(vm, HOST_MOUSE_EVT, V3_HOST_EVENT_HANDLER(mouse_event_handler), dev);
1054 #if KEYBOARD_DEBUG_80H
1055 v3_dev_hook_io(dev, KEYBOARD_DELAY_80H, &keyboard_read_delay, &keyboard_write_delay);
1060 // We do not hook the IRQ here. Instead, the underlying device driver
1061 // is responsible to call us back
1068 device_register("KEYBOARD", keyboard_init)