xref: /qemu/include/hw/input/ps2.h (revision 108cb22e4837a36bc22959a612f8bb50471139f6)
1 /*
2  * QEMU PS/2 keyboard/mouse emulation
3  *
4  * Copyright (C) 2003 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef HW_PS2_H
26 #define HW_PS2_H
27 
28 #include "hw/sysbus.h"
29 
30 #define PS2_MOUSE_BUTTON_LEFT   0x01
31 #define PS2_MOUSE_BUTTON_RIGHT  0x02
32 #define PS2_MOUSE_BUTTON_MIDDLE 0x04
33 #define PS2_MOUSE_BUTTON_SIDE   0x08
34 #define PS2_MOUSE_BUTTON_EXTRA  0x10
35 
36 struct PS2DeviceClass {
37     SysBusDeviceClass parent_class;
38 
39     DeviceReset parent_reset;
40 };
41 
42 /*
43  * PS/2 buffer size. Keep 256 bytes for compatibility with
44  * older QEMU versions.
45  */
46 #define PS2_BUFFER_SIZE     256
47 
48 typedef struct {
49     uint8_t data[PS2_BUFFER_SIZE];
50     int rptr, wptr, cwptr, count;
51 } PS2Queue;
52 
53 struct PS2State {
54     SysBusDevice parent_obj;
55 
56     PS2Queue queue;
57     int32_t write_cmd;
58     void (*update_irq)(void *, int);
59     void *update_arg;
60 };
61 
62 #define TYPE_PS2_DEVICE "ps2-device"
63 OBJECT_DECLARE_TYPE(PS2State, PS2DeviceClass, PS2_DEVICE)
64 
65 struct PS2KbdState {
66     PS2State parent_obj;
67 
68     int scan_enabled;
69     int translate;
70     int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
71     int ledstate;
72     bool need_high_bit;
73     unsigned int modifiers; /* bitmask of MOD_* constants above */
74 };
75 
76 #define TYPE_PS2_KBD_DEVICE "ps2-kbd"
77 OBJECT_DECLARE_SIMPLE_TYPE(PS2KbdState, PS2_KBD_DEVICE)
78 
79 struct PS2MouseState {
80     PS2State parent_obj;
81 
82     uint8_t mouse_status;
83     uint8_t mouse_resolution;
84     uint8_t mouse_sample_rate;
85     uint8_t mouse_wrap;
86     uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
87     uint8_t mouse_detect_state;
88     int mouse_dx; /* current values, needed for 'poll' mode */
89     int mouse_dy;
90     int mouse_dz;
91     int mouse_dw;
92     uint8_t mouse_buttons;
93 };
94 
95 #define TYPE_PS2_MOUSE_DEVICE "ps2-mouse"
96 OBJECT_DECLARE_SIMPLE_TYPE(PS2MouseState, PS2_MOUSE_DEVICE)
97 
98 /* ps2.c */
99 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg);
100 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg);
101 void ps2_write_mouse(PS2MouseState *s, int val);
102 void ps2_write_keyboard(PS2KbdState *s, int val);
103 uint32_t ps2_read_data(PS2State *s);
104 void ps2_queue_noirq(PS2State *s, int b);
105 void ps2_raise_irq(PS2State *s);
106 void ps2_queue(PS2State *s, int b);
107 void ps2_queue_2(PS2State *s, int b1, int b2);
108 void ps2_queue_3(PS2State *s, int b1, int b2, int b3);
109 void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4);
110 void ps2_keyboard_set_translation(PS2KbdState *s, int mode);
111 void ps2_mouse_fake_event(PS2MouseState *s);
112 int ps2_queue_empty(PS2State *s);
113 
114 #endif /* HW_PS2_H */
115