root / hw / pckbd.c @ fb6cf1d0
History | View | Annotate | Download (19 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU PC keyboard emulation
|
3 | 80cabfad | bellard | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 80cabfad | bellard | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 80cabfad | bellard | #include "vl.h" |
25 | 80cabfad | bellard | |
26 | 80cabfad | bellard | /* debug PC keyboard */
|
27 | 80cabfad | bellard | //#define DEBUG_KBD
|
28 | 80cabfad | bellard | |
29 | 80cabfad | bellard | /* debug PC keyboard : only mouse */
|
30 | 80cabfad | bellard | //#define DEBUG_MOUSE
|
31 | 80cabfad | bellard | |
32 | 80cabfad | bellard | /* Keyboard Controller Commands */
|
33 | 80cabfad | bellard | #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */ |
34 | 80cabfad | bellard | #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */ |
35 | 80cabfad | bellard | #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */ |
36 | 80cabfad | bellard | #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
37 | 80cabfad | bellard | #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */ |
38 | 80cabfad | bellard | #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */ |
39 | 80cabfad | bellard | #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */ |
40 | 80cabfad | bellard | #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */ |
41 | 80cabfad | bellard | #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */ |
42 | 80cabfad | bellard | #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */ |
43 | 80cabfad | bellard | #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */ |
44 | 80cabfad | bellard | #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */ |
45 | 80cabfad | bellard | #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */ |
46 | 80cabfad | bellard | #define KBD_CCMD_WRITE_OBUF 0xD2 |
47 | 80cabfad | bellard | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
48 | 80cabfad | bellard | initiated by the auxiliary device */
|
49 | 80cabfad | bellard | #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
50 | 80cabfad | bellard | #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */ |
51 | 80cabfad | bellard | #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */ |
52 | 80cabfad | bellard | #define KBD_CCMD_RESET 0xFE |
53 | 80cabfad | bellard | |
54 | 80cabfad | bellard | /* Keyboard Commands */
|
55 | 80cabfad | bellard | #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ |
56 | 80cabfad | bellard | #define KBD_CMD_ECHO 0xEE |
57 | 80cabfad | bellard | #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */ |
58 | 80cabfad | bellard | #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ |
59 | 80cabfad | bellard | #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ |
60 | 80cabfad | bellard | #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */ |
61 | 80cabfad | bellard | #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */ |
62 | 80cabfad | bellard | #define KBD_CMD_RESET 0xFF /* Reset */ |
63 | 80cabfad | bellard | |
64 | 80cabfad | bellard | /* Keyboard Replies */
|
65 | 80cabfad | bellard | #define KBD_REPLY_POR 0xAA /* Power on reset */ |
66 | 80cabfad | bellard | #define KBD_REPLY_ACK 0xFA /* Command ACK */ |
67 | 80cabfad | bellard | #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ |
68 | 80cabfad | bellard | |
69 | 80cabfad | bellard | /* Status Register Bits */
|
70 | 80cabfad | bellard | #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ |
71 | 80cabfad | bellard | #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */ |
72 | 80cabfad | bellard | #define KBD_STAT_SELFTEST 0x04 /* Self test successful */ |
73 | 80cabfad | bellard | #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */ |
74 | 80cabfad | bellard | #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */ |
75 | 80cabfad | bellard | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
76 | 80cabfad | bellard | #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */ |
77 | 80cabfad | bellard | #define KBD_STAT_PERR 0x80 /* Parity error */ |
78 | 80cabfad | bellard | |
79 | 80cabfad | bellard | /* Controller Mode Register Bits */
|
80 | 80cabfad | bellard | #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */ |
81 | 80cabfad | bellard | #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */ |
82 | 80cabfad | bellard | #define KBD_MODE_SYS 0x04 /* The system flag (?) */ |
83 | 80cabfad | bellard | #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */ |
84 | 80cabfad | bellard | #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */ |
85 | 80cabfad | bellard | #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
86 | 80cabfad | bellard | #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
87 | 80cabfad | bellard | #define KBD_MODE_RFU 0x80 |
88 | 80cabfad | bellard | |
89 | 80cabfad | bellard | /* Mouse Commands */
|
90 | 80cabfad | bellard | #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */ |
91 | 80cabfad | bellard | #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */ |
92 | 80cabfad | bellard | #define AUX_SET_RES 0xE8 /* Set resolution */ |
93 | 80cabfad | bellard | #define AUX_GET_SCALE 0xE9 /* Get scaling factor */ |
94 | 80cabfad | bellard | #define AUX_SET_STREAM 0xEA /* Set stream mode */ |
95 | 80cabfad | bellard | #define AUX_POLL 0xEB /* Poll */ |
96 | 80cabfad | bellard | #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */ |
97 | 80cabfad | bellard | #define AUX_SET_WRAP 0xEE /* Set wrap mode */ |
98 | 80cabfad | bellard | #define AUX_SET_REMOTE 0xF0 /* Set remote mode */ |
99 | 80cabfad | bellard | #define AUX_GET_TYPE 0xF2 /* Get type */ |
100 | 80cabfad | bellard | #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */ |
101 | 80cabfad | bellard | #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */ |
102 | 80cabfad | bellard | #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */ |
103 | 80cabfad | bellard | #define AUX_SET_DEFAULT 0xF6 |
104 | 80cabfad | bellard | #define AUX_RESET 0xFF /* Reset aux device */ |
105 | 80cabfad | bellard | #define AUX_ACK 0xFA /* Command byte ACK. */ |
106 | 80cabfad | bellard | |
107 | 80cabfad | bellard | #define MOUSE_STATUS_REMOTE 0x40 |
108 | 80cabfad | bellard | #define MOUSE_STATUS_ENABLED 0x20 |
109 | 80cabfad | bellard | #define MOUSE_STATUS_SCALE21 0x10 |
110 | 80cabfad | bellard | |
111 | 80cabfad | bellard | #define KBD_QUEUE_SIZE 256 |
112 | 80cabfad | bellard | |
113 | 80cabfad | bellard | typedef struct { |
114 | 6f51f6b5 | bellard | uint8_t aux[KBD_QUEUE_SIZE]; |
115 | 80cabfad | bellard | uint8_t data[KBD_QUEUE_SIZE]; |
116 | 80cabfad | bellard | int rptr, wptr, count;
|
117 | 80cabfad | bellard | } KBDQueue; |
118 | 80cabfad | bellard | |
119 | 80cabfad | bellard | typedef struct KBDState { |
120 | 6f51f6b5 | bellard | KBDQueue queue; |
121 | 80cabfad | bellard | uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
|
122 | 80cabfad | bellard | uint8_t status; |
123 | 80cabfad | bellard | uint8_t mode; |
124 | 80cabfad | bellard | /* keyboard state */
|
125 | 80cabfad | bellard | int kbd_write_cmd;
|
126 | 80cabfad | bellard | int scan_enabled;
|
127 | 80cabfad | bellard | /* mouse state */
|
128 | 80cabfad | bellard | int mouse_write_cmd;
|
129 | 80cabfad | bellard | uint8_t mouse_status; |
130 | 80cabfad | bellard | uint8_t mouse_resolution; |
131 | 80cabfad | bellard | uint8_t mouse_sample_rate; |
132 | 80cabfad | bellard | uint8_t mouse_wrap; |
133 | 80cabfad | bellard | uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
|
134 | 80cabfad | bellard | uint8_t mouse_detect_state; |
135 | 80cabfad | bellard | int mouse_dx; /* current values, needed for 'poll' mode */ |
136 | 80cabfad | bellard | int mouse_dy;
|
137 | 80cabfad | bellard | int mouse_dz;
|
138 | 80cabfad | bellard | uint8_t mouse_buttons; |
139 | 80cabfad | bellard | } KBDState; |
140 | 80cabfad | bellard | |
141 | 80cabfad | bellard | KBDState kbd_state; |
142 | 80cabfad | bellard | int reset_requested;
|
143 | 80cabfad | bellard | |
144 | 80cabfad | bellard | /* update irq and KBD_STAT_[MOUSE_]OBF */
|
145 | 80cabfad | bellard | /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
|
146 | 80cabfad | bellard | incorrect, but it avoids having to simulate exact delays */
|
147 | 80cabfad | bellard | static void kbd_update_irq(KBDState *s) |
148 | 80cabfad | bellard | { |
149 | 6f51f6b5 | bellard | KBDQueue *q = &s->queue; |
150 | 80cabfad | bellard | int irq12_level, irq1_level;
|
151 | 80cabfad | bellard | |
152 | 80cabfad | bellard | irq1_level = 0;
|
153 | 80cabfad | bellard | irq12_level = 0;
|
154 | 80cabfad | bellard | s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF); |
155 | 6f51f6b5 | bellard | if (q->count != 0) { |
156 | 80cabfad | bellard | s->status |= KBD_STAT_OBF; |
157 | 6f51f6b5 | bellard | if (q->aux[q->rptr]) {
|
158 | 80cabfad | bellard | s->status |= KBD_STAT_MOUSE_OBF; |
159 | 80cabfad | bellard | if (s->mode & KBD_MODE_MOUSE_INT)
|
160 | 80cabfad | bellard | irq12_level = 1;
|
161 | 80cabfad | bellard | } else {
|
162 | 80cabfad | bellard | if ((s->mode & KBD_MODE_KBD_INT) &&
|
163 | 80cabfad | bellard | !(s->mode & KBD_MODE_DISABLE_KBD)) |
164 | 80cabfad | bellard | irq1_level = 1;
|
165 | 80cabfad | bellard | } |
166 | 80cabfad | bellard | } |
167 | 80cabfad | bellard | pic_set_irq(1, irq1_level);
|
168 | 80cabfad | bellard | pic_set_irq(12, irq12_level);
|
169 | 80cabfad | bellard | } |
170 | 80cabfad | bellard | |
171 | 80cabfad | bellard | static void kbd_queue(KBDState *s, int b, int aux) |
172 | 80cabfad | bellard | { |
173 | 6f51f6b5 | bellard | KBDQueue *q = &s->queue; |
174 | 80cabfad | bellard | |
175 | 80cabfad | bellard | #if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
|
176 | 80cabfad | bellard | if (aux)
|
177 | 80cabfad | bellard | printf("mouse event: 0x%02x\n", b);
|
178 | 80cabfad | bellard | #ifdef DEBUG_KBD
|
179 | 80cabfad | bellard | else
|
180 | 80cabfad | bellard | printf("kbd event: 0x%02x\n", b);
|
181 | 80cabfad | bellard | #endif
|
182 | 80cabfad | bellard | #endif
|
183 | 80cabfad | bellard | if (q->count >= KBD_QUEUE_SIZE)
|
184 | 80cabfad | bellard | return;
|
185 | 6f51f6b5 | bellard | q->aux[q->wptr] = aux; |
186 | 80cabfad | bellard | q->data[q->wptr] = b; |
187 | 80cabfad | bellard | if (++q->wptr == KBD_QUEUE_SIZE)
|
188 | 80cabfad | bellard | q->wptr = 0;
|
189 | 80cabfad | bellard | q->count++; |
190 | 80cabfad | bellard | kbd_update_irq(s); |
191 | 80cabfad | bellard | } |
192 | 80cabfad | bellard | |
193 | 80cabfad | bellard | void kbd_put_keycode(int keycode) |
194 | 80cabfad | bellard | { |
195 | 80cabfad | bellard | KBDState *s = &kbd_state; |
196 | 80cabfad | bellard | kbd_queue(s, keycode, 0);
|
197 | 80cabfad | bellard | } |
198 | 80cabfad | bellard | |
199 | b41a2cd1 | bellard | static uint32_t kbd_read_status(void *opaque, uint32_t addr) |
200 | 80cabfad | bellard | { |
201 | b41a2cd1 | bellard | KBDState *s = opaque; |
202 | 80cabfad | bellard | int val;
|
203 | 80cabfad | bellard | val = s->status; |
204 | 80cabfad | bellard | #if defined(DEBUG_KBD)
|
205 | 80cabfad | bellard | printf("kbd: read status=0x%02x\n", val);
|
206 | 80cabfad | bellard | #endif
|
207 | 80cabfad | bellard | return val;
|
208 | 80cabfad | bellard | } |
209 | 80cabfad | bellard | |
210 | b41a2cd1 | bellard | static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val) |
211 | 80cabfad | bellard | { |
212 | b41a2cd1 | bellard | KBDState *s = opaque; |
213 | 80cabfad | bellard | |
214 | 80cabfad | bellard | #ifdef DEBUG_KBD
|
215 | 80cabfad | bellard | printf("kbd: write cmd=0x%02x\n", val);
|
216 | 80cabfad | bellard | #endif
|
217 | 80cabfad | bellard | switch(val) {
|
218 | 80cabfad | bellard | case KBD_CCMD_READ_MODE:
|
219 | 80cabfad | bellard | kbd_queue(s, s->mode, 0);
|
220 | 80cabfad | bellard | break;
|
221 | 80cabfad | bellard | case KBD_CCMD_WRITE_MODE:
|
222 | 80cabfad | bellard | case KBD_CCMD_WRITE_OBUF:
|
223 | 80cabfad | bellard | case KBD_CCMD_WRITE_AUX_OBUF:
|
224 | 80cabfad | bellard | case KBD_CCMD_WRITE_MOUSE:
|
225 | 80cabfad | bellard | case KBD_CCMD_WRITE_OUTPORT:
|
226 | 80cabfad | bellard | s->write_cmd = val; |
227 | 80cabfad | bellard | break;
|
228 | 80cabfad | bellard | case KBD_CCMD_MOUSE_DISABLE:
|
229 | 80cabfad | bellard | s->mode |= KBD_MODE_DISABLE_MOUSE; |
230 | 80cabfad | bellard | break;
|
231 | 80cabfad | bellard | case KBD_CCMD_MOUSE_ENABLE:
|
232 | 80cabfad | bellard | s->mode &= ~KBD_MODE_DISABLE_MOUSE; |
233 | 80cabfad | bellard | break;
|
234 | 80cabfad | bellard | case KBD_CCMD_TEST_MOUSE:
|
235 | 80cabfad | bellard | kbd_queue(s, 0x00, 0); |
236 | 80cabfad | bellard | break;
|
237 | 80cabfad | bellard | case KBD_CCMD_SELF_TEST:
|
238 | 80cabfad | bellard | s->status |= KBD_STAT_SELFTEST; |
239 | 80cabfad | bellard | kbd_queue(s, 0x55, 0); |
240 | 80cabfad | bellard | break;
|
241 | 80cabfad | bellard | case KBD_CCMD_KBD_TEST:
|
242 | 80cabfad | bellard | kbd_queue(s, 0x00, 0); |
243 | 80cabfad | bellard | break;
|
244 | 80cabfad | bellard | case KBD_CCMD_KBD_DISABLE:
|
245 | 80cabfad | bellard | s->mode |= KBD_MODE_DISABLE_KBD; |
246 | 80cabfad | bellard | kbd_update_irq(s); |
247 | 80cabfad | bellard | break;
|
248 | 80cabfad | bellard | case KBD_CCMD_KBD_ENABLE:
|
249 | 80cabfad | bellard | s->mode &= ~KBD_MODE_DISABLE_KBD; |
250 | 80cabfad | bellard | kbd_update_irq(s); |
251 | 80cabfad | bellard | break;
|
252 | 80cabfad | bellard | case KBD_CCMD_READ_INPORT:
|
253 | 80cabfad | bellard | kbd_queue(s, 0x00, 0); |
254 | 80cabfad | bellard | break;
|
255 | 80cabfad | bellard | case KBD_CCMD_READ_OUTPORT:
|
256 | 80cabfad | bellard | /* XXX: check that */
|
257 | 80cabfad | bellard | #ifdef TARGET_I386
|
258 | 80cabfad | bellard | val = 0x01 | (((cpu_single_env->a20_mask >> 20) & 1) << 1); |
259 | 80cabfad | bellard | #else
|
260 | 80cabfad | bellard | val = 0x01;
|
261 | 80cabfad | bellard | #endif
|
262 | 80cabfad | bellard | if (s->status & KBD_STAT_OBF)
|
263 | 80cabfad | bellard | val |= 0x10;
|
264 | 80cabfad | bellard | if (s->status & KBD_STAT_MOUSE_OBF)
|
265 | 80cabfad | bellard | val |= 0x20;
|
266 | 80cabfad | bellard | kbd_queue(s, val, 0);
|
267 | 80cabfad | bellard | break;
|
268 | 80cabfad | bellard | #ifdef TARGET_I386
|
269 | 80cabfad | bellard | case KBD_CCMD_ENABLE_A20:
|
270 | b41a2cd1 | bellard | cpu_x86_set_a20(cpu_single_env, 1);
|
271 | 80cabfad | bellard | break;
|
272 | 80cabfad | bellard | case KBD_CCMD_DISABLE_A20:
|
273 | b41a2cd1 | bellard | cpu_x86_set_a20(cpu_single_env, 0);
|
274 | 80cabfad | bellard | break;
|
275 | 80cabfad | bellard | #endif
|
276 | 80cabfad | bellard | case KBD_CCMD_RESET:
|
277 | 80cabfad | bellard | reset_requested = 1;
|
278 | 80cabfad | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
279 | 80cabfad | bellard | break;
|
280 | 80cabfad | bellard | case 0xff: |
281 | 80cabfad | bellard | /* ignore that - I don't know what is its use */
|
282 | 80cabfad | bellard | break;
|
283 | 80cabfad | bellard | default:
|
284 | 80cabfad | bellard | fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
|
285 | 80cabfad | bellard | break;
|
286 | 80cabfad | bellard | } |
287 | 80cabfad | bellard | } |
288 | 80cabfad | bellard | |
289 | b41a2cd1 | bellard | static uint32_t kbd_read_data(void *opaque, uint32_t addr) |
290 | 80cabfad | bellard | { |
291 | b41a2cd1 | bellard | KBDState *s = opaque; |
292 | 80cabfad | bellard | KBDQueue *q; |
293 | 6f51f6b5 | bellard | int val, index, aux;
|
294 | 80cabfad | bellard | |
295 | 6f51f6b5 | bellard | q = &s->queue; |
296 | 80cabfad | bellard | if (q->count == 0) { |
297 | 80cabfad | bellard | /* NOTE: if no data left, we return the last keyboard one
|
298 | 80cabfad | bellard | (needed for EMM386) */
|
299 | 80cabfad | bellard | /* XXX: need a timer to do things correctly */
|
300 | 80cabfad | bellard | index = q->rptr - 1;
|
301 | 80cabfad | bellard | if (index < 0) |
302 | 80cabfad | bellard | index = KBD_QUEUE_SIZE - 1;
|
303 | 80cabfad | bellard | val = q->data[index]; |
304 | 80cabfad | bellard | } else {
|
305 | 6f51f6b5 | bellard | aux = q->aux[q->rptr]; |
306 | 80cabfad | bellard | val = q->data[q->rptr]; |
307 | 80cabfad | bellard | if (++q->rptr == KBD_QUEUE_SIZE)
|
308 | 80cabfad | bellard | q->rptr = 0;
|
309 | 80cabfad | bellard | q->count--; |
310 | 80cabfad | bellard | /* reading deasserts IRQ */
|
311 | 6f51f6b5 | bellard | if (aux)
|
312 | 80cabfad | bellard | pic_set_irq(12, 0); |
313 | 6f51f6b5 | bellard | else
|
314 | 6f51f6b5 | bellard | pic_set_irq(1, 0); |
315 | 80cabfad | bellard | } |
316 | 80cabfad | bellard | /* reassert IRQs if data left */
|
317 | 80cabfad | bellard | kbd_update_irq(s); |
318 | 80cabfad | bellard | #ifdef DEBUG_KBD
|
319 | 80cabfad | bellard | printf("kbd: read data=0x%02x\n", val);
|
320 | 80cabfad | bellard | #endif
|
321 | 80cabfad | bellard | return val;
|
322 | 80cabfad | bellard | } |
323 | 80cabfad | bellard | |
324 | 80cabfad | bellard | static void kbd_reset_keyboard(KBDState *s) |
325 | 80cabfad | bellard | { |
326 | 80cabfad | bellard | s->scan_enabled = 1;
|
327 | 80cabfad | bellard | } |
328 | 80cabfad | bellard | |
329 | 80cabfad | bellard | static void kbd_write_keyboard(KBDState *s, int val) |
330 | 80cabfad | bellard | { |
331 | 80cabfad | bellard | switch(s->kbd_write_cmd) {
|
332 | 80cabfad | bellard | default:
|
333 | 80cabfad | bellard | case -1: |
334 | 80cabfad | bellard | switch(val) {
|
335 | 80cabfad | bellard | case 0x00: |
336 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
337 | 80cabfad | bellard | break;
|
338 | 80cabfad | bellard | case 0x05: |
339 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_RESEND, 0);
|
340 | 80cabfad | bellard | break;
|
341 | 80cabfad | bellard | case KBD_CMD_GET_ID:
|
342 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
343 | 80cabfad | bellard | kbd_queue(s, 0xab, 0); |
344 | 80cabfad | bellard | kbd_queue(s, 0x83, 0); |
345 | 80cabfad | bellard | break;
|
346 | 80cabfad | bellard | case KBD_CMD_ECHO:
|
347 | 80cabfad | bellard | kbd_queue(s, KBD_CMD_ECHO, 0);
|
348 | 80cabfad | bellard | break;
|
349 | 80cabfad | bellard | case KBD_CMD_ENABLE:
|
350 | 80cabfad | bellard | s->scan_enabled = 1;
|
351 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
352 | 80cabfad | bellard | break;
|
353 | 80cabfad | bellard | case KBD_CMD_SET_LEDS:
|
354 | 80cabfad | bellard | case KBD_CMD_SET_RATE:
|
355 | 80cabfad | bellard | s->kbd_write_cmd = val; |
356 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
357 | 80cabfad | bellard | break;
|
358 | 80cabfad | bellard | case KBD_CMD_RESET_DISABLE:
|
359 | 80cabfad | bellard | kbd_reset_keyboard(s); |
360 | 80cabfad | bellard | s->scan_enabled = 0;
|
361 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
362 | 80cabfad | bellard | break;
|
363 | 80cabfad | bellard | case KBD_CMD_RESET_ENABLE:
|
364 | 80cabfad | bellard | kbd_reset_keyboard(s); |
365 | 80cabfad | bellard | s->scan_enabled = 1;
|
366 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
367 | 80cabfad | bellard | break;
|
368 | 80cabfad | bellard | case KBD_CMD_RESET:
|
369 | 80cabfad | bellard | kbd_reset_keyboard(s); |
370 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
371 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_POR, 0);
|
372 | 80cabfad | bellard | break;
|
373 | 80cabfad | bellard | default:
|
374 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
375 | 80cabfad | bellard | break;
|
376 | 80cabfad | bellard | } |
377 | 80cabfad | bellard | break;
|
378 | 80cabfad | bellard | case KBD_CMD_SET_LEDS:
|
379 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
380 | 80cabfad | bellard | s->kbd_write_cmd = -1;
|
381 | 80cabfad | bellard | break;
|
382 | 80cabfad | bellard | case KBD_CMD_SET_RATE:
|
383 | 80cabfad | bellard | kbd_queue(s, KBD_REPLY_ACK, 0);
|
384 | 80cabfad | bellard | s->kbd_write_cmd = -1;
|
385 | 80cabfad | bellard | break;
|
386 | 80cabfad | bellard | } |
387 | 80cabfad | bellard | } |
388 | 80cabfad | bellard | |
389 | 80cabfad | bellard | static void kbd_mouse_send_packet(KBDState *s) |
390 | 80cabfad | bellard | { |
391 | 80cabfad | bellard | unsigned int b; |
392 | 80cabfad | bellard | int dx1, dy1, dz1;
|
393 | 80cabfad | bellard | |
394 | 80cabfad | bellard | dx1 = s->mouse_dx; |
395 | 80cabfad | bellard | dy1 = s->mouse_dy; |
396 | 80cabfad | bellard | dz1 = s->mouse_dz; |
397 | 80cabfad | bellard | /* XXX: increase range to 8 bits ? */
|
398 | 80cabfad | bellard | if (dx1 > 127) |
399 | 80cabfad | bellard | dx1 = 127;
|
400 | 80cabfad | bellard | else if (dx1 < -127) |
401 | 80cabfad | bellard | dx1 = -127;
|
402 | 80cabfad | bellard | if (dy1 > 127) |
403 | 80cabfad | bellard | dy1 = 127;
|
404 | 80cabfad | bellard | else if (dy1 < -127) |
405 | 80cabfad | bellard | dy1 = -127;
|
406 | 80cabfad | bellard | b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07); |
407 | 80cabfad | bellard | kbd_queue(s, b, 1);
|
408 | 80cabfad | bellard | kbd_queue(s, dx1 & 0xff, 1); |
409 | 80cabfad | bellard | kbd_queue(s, dy1 & 0xff, 1); |
410 | 80cabfad | bellard | /* extra byte for IMPS/2 or IMEX */
|
411 | 80cabfad | bellard | switch(s->mouse_type) {
|
412 | 80cabfad | bellard | default:
|
413 | 80cabfad | bellard | break;
|
414 | 80cabfad | bellard | case 3: |
415 | 80cabfad | bellard | if (dz1 > 127) |
416 | 80cabfad | bellard | dz1 = 127;
|
417 | 80cabfad | bellard | else if (dz1 < -127) |
418 | 80cabfad | bellard | dz1 = -127;
|
419 | 80cabfad | bellard | kbd_queue(s, dz1 & 0xff, 1); |
420 | 80cabfad | bellard | break;
|
421 | 80cabfad | bellard | case 4: |
422 | 80cabfad | bellard | if (dz1 > 7) |
423 | 80cabfad | bellard | dz1 = 7;
|
424 | 80cabfad | bellard | else if (dz1 < -7) |
425 | 80cabfad | bellard | dz1 = -7;
|
426 | 80cabfad | bellard | b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1); |
427 | 80cabfad | bellard | kbd_queue(s, b, 1);
|
428 | 80cabfad | bellard | break;
|
429 | 80cabfad | bellard | } |
430 | 80cabfad | bellard | |
431 | 80cabfad | bellard | /* update deltas */
|
432 | 80cabfad | bellard | s->mouse_dx -= dx1; |
433 | 80cabfad | bellard | s->mouse_dy -= dy1; |
434 | 80cabfad | bellard | s->mouse_dz -= dz1; |
435 | 80cabfad | bellard | } |
436 | 80cabfad | bellard | |
437 | 80cabfad | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
438 | 80cabfad | bellard | { |
439 | 80cabfad | bellard | KBDState *s = &kbd_state; |
440 | 80cabfad | bellard | |
441 | 80cabfad | bellard | /* check if deltas are recorded when disabled */
|
442 | 80cabfad | bellard | if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
|
443 | 80cabfad | bellard | return;
|
444 | 80cabfad | bellard | |
445 | 80cabfad | bellard | s->mouse_dx += dx; |
446 | 80cabfad | bellard | s->mouse_dy -= dy; |
447 | 80cabfad | bellard | s->mouse_dz += dz; |
448 | 92cb7d54 | bellard | /* XXX: SDL sometimes generates nul events: we delete them */
|
449 | 92cb7d54 | bellard | if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 && |
450 | 92cb7d54 | bellard | s->mouse_buttons == buttons_state) |
451 | 92cb7d54 | bellard | return;
|
452 | 80cabfad | bellard | s->mouse_buttons = buttons_state; |
453 | 80cabfad | bellard | |
454 | 80cabfad | bellard | if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
|
455 | 6f51f6b5 | bellard | (s->queue.count < (KBD_QUEUE_SIZE - 16))) {
|
456 | 80cabfad | bellard | for(;;) {
|
457 | 80cabfad | bellard | /* if not remote, send event. Multiple events are sent if
|
458 | 80cabfad | bellard | too big deltas */
|
459 | 80cabfad | bellard | kbd_mouse_send_packet(s); |
460 | 80cabfad | bellard | if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0) |
461 | 80cabfad | bellard | break;
|
462 | 80cabfad | bellard | } |
463 | 80cabfad | bellard | } |
464 | 80cabfad | bellard | } |
465 | 80cabfad | bellard | |
466 | 80cabfad | bellard | static void kbd_write_mouse(KBDState *s, int val) |
467 | 80cabfad | bellard | { |
468 | 80cabfad | bellard | #ifdef DEBUG_MOUSE
|
469 | 80cabfad | bellard | printf("kbd: write mouse 0x%02x\n", val);
|
470 | 80cabfad | bellard | #endif
|
471 | 80cabfad | bellard | switch(s->mouse_write_cmd) {
|
472 | 80cabfad | bellard | default:
|
473 | 80cabfad | bellard | case -1: |
474 | 80cabfad | bellard | /* mouse command */
|
475 | 80cabfad | bellard | if (s->mouse_wrap) {
|
476 | 80cabfad | bellard | if (val == AUX_RESET_WRAP) {
|
477 | 80cabfad | bellard | s->mouse_wrap = 0;
|
478 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
479 | 80cabfad | bellard | return;
|
480 | 80cabfad | bellard | } else if (val != AUX_RESET) { |
481 | 80cabfad | bellard | kbd_queue(s, val, 1);
|
482 | 80cabfad | bellard | return;
|
483 | 80cabfad | bellard | } |
484 | 80cabfad | bellard | } |
485 | 80cabfad | bellard | switch(val) {
|
486 | 80cabfad | bellard | case AUX_SET_SCALE11:
|
487 | 80cabfad | bellard | s->mouse_status &= ~MOUSE_STATUS_SCALE21; |
488 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
489 | 80cabfad | bellard | break;
|
490 | 80cabfad | bellard | case AUX_SET_SCALE21:
|
491 | 80cabfad | bellard | s->mouse_status |= MOUSE_STATUS_SCALE21; |
492 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
493 | 80cabfad | bellard | break;
|
494 | 80cabfad | bellard | case AUX_SET_STREAM:
|
495 | 80cabfad | bellard | s->mouse_status &= ~MOUSE_STATUS_REMOTE; |
496 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
497 | 80cabfad | bellard | break;
|
498 | 80cabfad | bellard | case AUX_SET_WRAP:
|
499 | 80cabfad | bellard | s->mouse_wrap = 1;
|
500 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
501 | 80cabfad | bellard | break;
|
502 | 80cabfad | bellard | case AUX_SET_REMOTE:
|
503 | 80cabfad | bellard | s->mouse_status |= MOUSE_STATUS_REMOTE; |
504 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
505 | 80cabfad | bellard | break;
|
506 | 80cabfad | bellard | case AUX_GET_TYPE:
|
507 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
508 | 80cabfad | bellard | kbd_queue(s, s->mouse_type, 1);
|
509 | 80cabfad | bellard | break;
|
510 | 80cabfad | bellard | case AUX_SET_RES:
|
511 | 80cabfad | bellard | case AUX_SET_SAMPLE:
|
512 | 80cabfad | bellard | s->mouse_write_cmd = val; |
513 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
514 | 80cabfad | bellard | break;
|
515 | 80cabfad | bellard | case AUX_GET_SCALE:
|
516 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
517 | 80cabfad | bellard | kbd_queue(s, s->mouse_status, 1);
|
518 | 80cabfad | bellard | kbd_queue(s, s->mouse_resolution, 1);
|
519 | 80cabfad | bellard | kbd_queue(s, s->mouse_sample_rate, 1);
|
520 | 80cabfad | bellard | break;
|
521 | 80cabfad | bellard | case AUX_POLL:
|
522 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
523 | 80cabfad | bellard | kbd_mouse_send_packet(s); |
524 | 80cabfad | bellard | break;
|
525 | 80cabfad | bellard | case AUX_ENABLE_DEV:
|
526 | 80cabfad | bellard | s->mouse_status |= MOUSE_STATUS_ENABLED; |
527 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
528 | 80cabfad | bellard | break;
|
529 | 80cabfad | bellard | case AUX_DISABLE_DEV:
|
530 | 80cabfad | bellard | s->mouse_status &= ~MOUSE_STATUS_ENABLED; |
531 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
532 | 80cabfad | bellard | break;
|
533 | 80cabfad | bellard | case AUX_SET_DEFAULT:
|
534 | 80cabfad | bellard | s->mouse_sample_rate = 100;
|
535 | 80cabfad | bellard | s->mouse_resolution = 2;
|
536 | 80cabfad | bellard | s->mouse_status = 0;
|
537 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
538 | 80cabfad | bellard | break;
|
539 | 80cabfad | bellard | case AUX_RESET:
|
540 | 80cabfad | bellard | s->mouse_sample_rate = 100;
|
541 | 80cabfad | bellard | s->mouse_resolution = 2;
|
542 | 80cabfad | bellard | s->mouse_status = 0;
|
543 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
544 | 80cabfad | bellard | kbd_queue(s, 0xaa, 1); |
545 | 80cabfad | bellard | kbd_queue(s, s->mouse_type, 1);
|
546 | 80cabfad | bellard | break;
|
547 | 80cabfad | bellard | default:
|
548 | 80cabfad | bellard | break;
|
549 | 80cabfad | bellard | } |
550 | 80cabfad | bellard | break;
|
551 | 80cabfad | bellard | case AUX_SET_SAMPLE:
|
552 | 80cabfad | bellard | s->mouse_sample_rate = val; |
553 | 80cabfad | bellard | #if 0
|
554 | 80cabfad | bellard | /* detect IMPS/2 or IMEX */
|
555 | 80cabfad | bellard | switch(s->mouse_detect_state) {
|
556 | 80cabfad | bellard | default:
|
557 | 80cabfad | bellard | case 0:
|
558 | 80cabfad | bellard | if (val == 200)
|
559 | 80cabfad | bellard | s->mouse_detect_state = 1;
|
560 | 80cabfad | bellard | break;
|
561 | 80cabfad | bellard | case 1:
|
562 | 80cabfad | bellard | if (val == 100)
|
563 | 80cabfad | bellard | s->mouse_detect_state = 2;
|
564 | 80cabfad | bellard | else if (val == 200)
|
565 | 80cabfad | bellard | s->mouse_detect_state = 3;
|
566 | 80cabfad | bellard | else
|
567 | 80cabfad | bellard | s->mouse_detect_state = 0;
|
568 | 80cabfad | bellard | break;
|
569 | 80cabfad | bellard | case 2:
|
570 | 80cabfad | bellard | if (val == 80)
|
571 | 80cabfad | bellard | s->mouse_type = 3; /* IMPS/2 */
|
572 | 80cabfad | bellard | s->mouse_detect_state = 0;
|
573 | 80cabfad | bellard | break;
|
574 | 80cabfad | bellard | case 3:
|
575 | 80cabfad | bellard | if (val == 80)
|
576 | 80cabfad | bellard | s->mouse_type = 4; /* IMEX */
|
577 | 80cabfad | bellard | s->mouse_detect_state = 0;
|
578 | 80cabfad | bellard | break;
|
579 | 80cabfad | bellard | }
|
580 | 80cabfad | bellard | #endif
|
581 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
582 | 80cabfad | bellard | s->mouse_write_cmd = -1;
|
583 | 80cabfad | bellard | break;
|
584 | 80cabfad | bellard | case AUX_SET_RES:
|
585 | 80cabfad | bellard | s->mouse_resolution = val; |
586 | 80cabfad | bellard | kbd_queue(s, AUX_ACK, 1);
|
587 | 80cabfad | bellard | s->mouse_write_cmd = -1;
|
588 | 80cabfad | bellard | break;
|
589 | 80cabfad | bellard | } |
590 | 80cabfad | bellard | } |
591 | 80cabfad | bellard | |
592 | b41a2cd1 | bellard | void kbd_write_data(void *opaque, uint32_t addr, uint32_t val) |
593 | 80cabfad | bellard | { |
594 | b41a2cd1 | bellard | KBDState *s = opaque; |
595 | 80cabfad | bellard | |
596 | 80cabfad | bellard | #ifdef DEBUG_KBD
|
597 | 80cabfad | bellard | printf("kbd: write data=0x%02x\n", val);
|
598 | 80cabfad | bellard | #endif
|
599 | 80cabfad | bellard | |
600 | 80cabfad | bellard | switch(s->write_cmd) {
|
601 | 80cabfad | bellard | case 0: |
602 | 80cabfad | bellard | kbd_write_keyboard(s, val); |
603 | 80cabfad | bellard | break;
|
604 | 80cabfad | bellard | case KBD_CCMD_WRITE_MODE:
|
605 | 80cabfad | bellard | s->mode = val; |
606 | 80cabfad | bellard | kbd_update_irq(s); |
607 | 80cabfad | bellard | break;
|
608 | 80cabfad | bellard | case KBD_CCMD_WRITE_OBUF:
|
609 | 80cabfad | bellard | kbd_queue(s, val, 0);
|
610 | 80cabfad | bellard | break;
|
611 | 80cabfad | bellard | case KBD_CCMD_WRITE_AUX_OBUF:
|
612 | 80cabfad | bellard | kbd_queue(s, val, 1);
|
613 | 80cabfad | bellard | break;
|
614 | 80cabfad | bellard | case KBD_CCMD_WRITE_OUTPORT:
|
615 | 80cabfad | bellard | #ifdef TARGET_I386
|
616 | b41a2cd1 | bellard | cpu_x86_set_a20(cpu_single_env, (val >> 1) & 1); |
617 | 80cabfad | bellard | #endif
|
618 | 80cabfad | bellard | if (!(val & 1)) { |
619 | 80cabfad | bellard | reset_requested = 1;
|
620 | 80cabfad | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
621 | 80cabfad | bellard | } |
622 | 80cabfad | bellard | break;
|
623 | 80cabfad | bellard | case KBD_CCMD_WRITE_MOUSE:
|
624 | 80cabfad | bellard | kbd_write_mouse(s, val); |
625 | 80cabfad | bellard | break;
|
626 | 80cabfad | bellard | default:
|
627 | 80cabfad | bellard | break;
|
628 | 80cabfad | bellard | } |
629 | 80cabfad | bellard | s->write_cmd = 0;
|
630 | 80cabfad | bellard | } |
631 | 80cabfad | bellard | |
632 | 80cabfad | bellard | void kbd_reset(KBDState *s)
|
633 | 80cabfad | bellard | { |
634 | 80cabfad | bellard | KBDQueue *q; |
635 | 80cabfad | bellard | |
636 | 80cabfad | bellard | s->kbd_write_cmd = -1;
|
637 | 80cabfad | bellard | s->mouse_write_cmd = -1;
|
638 | 80cabfad | bellard | s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT; |
639 | 80cabfad | bellard | s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED; |
640 | 6f51f6b5 | bellard | q = &s->queue; |
641 | 6f51f6b5 | bellard | q->rptr = 0;
|
642 | 6f51f6b5 | bellard | q->wptr = 0;
|
643 | 6f51f6b5 | bellard | q->count = 0;
|
644 | 80cabfad | bellard | } |
645 | 80cabfad | bellard | |
646 | 80cabfad | bellard | void kbd_init(void) |
647 | 80cabfad | bellard | { |
648 | b41a2cd1 | bellard | KBDState *s = &kbd_state; |
649 | b41a2cd1 | bellard | |
650 | b41a2cd1 | bellard | kbd_reset(s); |
651 | b41a2cd1 | bellard | register_ioport_read(0x60, 1, 1, kbd_read_data, s); |
652 | b41a2cd1 | bellard | register_ioport_write(0x60, 1, 1, kbd_write_data, s); |
653 | b41a2cd1 | bellard | register_ioport_read(0x64, 1, 1, kbd_read_status, s); |
654 | b41a2cd1 | bellard | register_ioport_write(0x64, 1, 1, kbd_write_command, s); |
655 | 80cabfad | bellard | } |