VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/DevPS2.cpp@ 47313

Last change on this file since 47313 was 47259, checked in by vboxsync, 11 years ago

pdmifs: fix putEventMT definition.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.0 KB
Line 
1/* $Id: DevPS2.cpp 47259 2013-07-19 13:58:26Z vboxsync $ */
2/** @file
3 * DevPS2 - PS/2 keyboard & mouse controller device.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 * --------------------------------------------------------------------
17 *
18 * This code is based on:
19 *
20 * QEMU PC keyboard emulation (revision 1.12)
21 *
22 * Copyright (c) 2003 Fabrice Bellard
23 *
24 * Permission is hereby granted, free of charge, to any person obtaining a copy
25 * of this software and associated documentation files (the "Software"), to deal
26 * in the Software without restriction, including without limitation the rights
27 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28 * copies of the Software, and to permit persons to whom the Software is
29 * furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
37 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40 * THE SOFTWARE.
41 *
42 */
43
44/*******************************************************************************
45* Header Files *
46*******************************************************************************/
47#define LOG_GROUP LOG_GROUP_DEV_KBD
48#include "vl_vbox.h"
49#include <VBox/vmm/pdmdev.h>
50#include <iprt/assert.h>
51#include <iprt/uuid.h>
52
53#include "VBoxDD.h"
54#include "PS2Dev.h"
55
56#define PCKBD_SAVED_STATE_VERSION 6
57
58
59#ifndef VBOX_DEVICE_STRUCT_TESTCASE
60/*******************************************************************************
61* Internal Functions *
62*******************************************************************************/
63RT_C_DECLS_BEGIN
64PDMBOTHCBDECL(int) kbdIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
65PDMBOTHCBDECL(int) kbdIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
66PDMBOTHCBDECL(int) kbdIOPortStatusRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb);
67PDMBOTHCBDECL(int) kbdIOPortCommandWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb);
68RT_C_DECLS_END
69#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
70
71/* debug PC keyboard */
72#define DEBUG_KBD
73
74/* debug PC keyboard : only mouse */
75#define DEBUG_MOUSE
76
77/* Keyboard Controller Commands */
78#define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
79#define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
80#define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
81#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
82#define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
83#define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
84#define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
85#define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
86#define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
87#define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
88#define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
89#define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
90#define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
91#define KBD_CCMD_WRITE_OBUF 0xD2
92#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
93 initiated by the auxiliary device */
94#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
95#define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
96#define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
97#define KBD_CCMD_READ_TSTINP 0xE0 /* Read test inputs T0, T1 */
98#define KBD_CCMD_RESET 0xFE
99
100/* Status Register Bits */
101#define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
102#define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
103#define KBD_STAT_SELFTEST 0x04 /* Self test successful */
104#define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
105#define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
106#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
107#define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
108#define KBD_STAT_PERR 0x80 /* Parity error */
109
110/* Controller Mode Register Bits */
111#define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
112#define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
113#define KBD_MODE_SYS 0x04 /* The system flag (?) */
114#define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
115#define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
116#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
117#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
118#define KBD_MODE_RFU 0x80
119
120/* Mouse Commands */
121#define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
122#define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
123#define AUX_SET_RES 0xE8 /* Set resolution */
124#define AUX_GET_SCALE 0xE9 /* Get scaling factor */
125#define AUX_SET_STREAM 0xEA /* Set stream mode */
126#define AUX_POLL 0xEB /* Poll */
127#define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
128#define AUX_SET_WRAP 0xEE /* Set wrap mode */
129#define AUX_SET_REMOTE 0xF0 /* Set remote mode */
130#define AUX_GET_TYPE 0xF2 /* Get type */
131#define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
132#define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
133#define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
134#define AUX_SET_DEFAULT 0xF6
135#define AUX_RESET 0xFF /* Reset aux device */
136#define AUX_ACK 0xFA /* Command byte ACK. */
137#define AUX_NACK 0xFE /* Command byte NACK. */
138
139#define MOUSE_STATUS_REMOTE 0x40
140#define MOUSE_STATUS_ENABLED 0x20
141#define MOUSE_STATUS_SCALE21 0x10
142
143/** Supported mouse protocols */
144enum
145{
146 MOUSE_PROT_PS2 = 0,
147 MOUSE_PROT_IMPS2 = 3,
148 MOUSE_PROT_IMEX = 4
149};
150
151/** @name Mouse flags */
152/** @{ */
153/** IMEX horizontal scroll-wheel mode is active */
154# define MOUSE_REPORT_HORIZONTAL 0x01
155/** @} */
156
157#define MOUSE_CMD_QUEUE_SIZE 8
158
159typedef struct {
160 uint8_t data[MOUSE_CMD_QUEUE_SIZE];
161 int rptr, wptr, count;
162} MouseCmdQueue;
163
164
165#define MOUSE_EVENT_QUEUE_SIZE 256
166
167typedef struct
168{
169 uint8_t data[MOUSE_EVENT_QUEUE_SIZE];
170 int rptr;
171 int wptr;
172 int count;
173} MouseEventQueue;
174
175/**
176 * The keyboard controller/device state.
177 *
178 * @note We use the default critical section for serialize data access.
179 */
180typedef struct KBDState
181{
182 MouseCmdQueue mouse_command_queue;
183 MouseEventQueue mouse_event_queue;
184 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
185 uint8_t status;
186 uint8_t mode;
187 uint8_t dbbout; /* data buffer byte */
188 /* keyboard state */
189 int32_t translate;
190 int32_t xlat_state;
191 /* mouse state */
192 int32_t mouse_write_cmd;
193 uint8_t mouse_status;
194 uint8_t mouse_resolution;
195 uint8_t mouse_sample_rate;
196 uint8_t mouse_wrap;
197 uint8_t mouse_type; /* MOUSE_PROT_PS2, *_IMPS/2, *_IMEX */
198 uint8_t mouse_detect_state;
199 int32_t mouse_dx; /* current values, needed for 'poll' mode */
200 int32_t mouse_dy;
201 int32_t mouse_dz;
202 int32_t mouse_dw;
203 int32_t mouse_flags;
204 uint8_t mouse_buttons;
205 uint8_t mouse_buttons_reported;
206
207 uint32_t Alignment0;
208
209 /** Pointer to the device instance - RC. */
210 PPDMDEVINSRC pDevInsRC;
211 /** Pointer to the device instance - R3 . */
212 PPDMDEVINSR3 pDevInsR3;
213 /** Pointer to the device instance. */
214 PPDMDEVINSR0 pDevInsR0;
215
216 /** Keyboard state (implemented in separate PS2K module). */
217#ifdef VBOX_DEVICE_STRUCT_TESTCASE
218 uint8_t KbdFiller[PS2K_STRUCT_FILLER];
219#else
220 PS2K Kbd;
221#endif
222
223 /**
224 * Mouse port - LUN#1.
225 *
226 * @implements PDMIBASE
227 * @implements PDMIMOUSEPORT
228 */
229 struct
230 {
231 /** The base interface for the mouse port. */
232 PDMIBASE IBase;
233 /** The mouse port base interface. */
234 PDMIMOUSEPORT IPort;
235
236 /** The base interface of the attached mouse driver. */
237 R3PTRTYPE(PPDMIBASE) pDrvBase;
238 /** The mouse interface of the attached mouse driver. */
239 R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
240 } Mouse;
241} KBDState;
242
243#ifndef VBOX_DEVICE_STRUCT_TESTCASE
244
245/* update irq and KBD_STAT_[MOUSE_]OBF */
246static void kbd_update_irq(KBDState *s)
247{
248 MouseCmdQueue *mcq = &s->mouse_command_queue;
249 MouseEventQueue *meq = &s->mouse_event_queue;
250 int irq12_level, irq1_level;
251 uint8_t val;
252
253 irq1_level = 0;
254 irq12_level = 0;
255
256 /* Determine new OBF state, but only if OBF is clear. If OBF was already
257 * set, we cannot risk changing the event type after an ISR potentially
258 * started executing! Only kbd_read_data() clears the OBF bits.
259 */
260 if (!(s->status & KBD_STAT_OBF)) {
261 s->status &= ~KBD_STAT_MOUSE_OBF;
262 /* Keyboard data has priority if both kbd and aux data is available. */
263 if (!(s->mode & KBD_MODE_DISABLE_KBD) && PS2KByteFromKbd(&s->Kbd, &val) == VINF_SUCCESS)
264 {
265 bool fHaveData = true;
266
267 /* If scancode translation is on (it usually is), there's more work to do. */
268 if (s->translate)
269 {
270 uint8_t xlated_val;
271
272 s->xlat_state = XlateAT2PC(s->xlat_state, val, &xlated_val);
273 val = xlated_val;
274
275 /* If the translation state is XS_BREAK, there's nothing to report
276 * and we keep going until the state changes or there's no more data.
277 */
278 while (s->xlat_state == XS_BREAK && PS2KByteFromKbd(&s->Kbd, &val) == VINF_SUCCESS)
279 {
280 s->xlat_state = XlateAT2PC(s->xlat_state, val, &xlated_val);
281 val = xlated_val;
282 }
283 /* This can happen if the last byte in the queue is F0... */
284 if (s->xlat_state == XS_BREAK)
285 fHaveData = false;
286 }
287 if (fHaveData)
288 {
289 s->dbbout = val;
290 s->status |= KBD_STAT_OBF;
291 }
292 }
293 else if ((mcq->count || meq->count) && !(s->mode & KBD_MODE_DISABLE_MOUSE))
294 {
295 s->status |= KBD_STAT_OBF | KBD_STAT_MOUSE_OBF;
296 if (mcq->count)
297 {
298 s->dbbout = mcq->data[mcq->rptr];
299 if (++mcq->rptr == MOUSE_CMD_QUEUE_SIZE)
300 mcq->rptr = 0;
301 mcq->count--;
302 }
303 else
304 {
305 s->dbbout = meq->data[meq->rptr];
306 if (++meq->rptr == MOUSE_EVENT_QUEUE_SIZE)
307 meq->rptr = 0;
308 meq->count--;
309 }
310 }
311 }
312 /* Determine new IRQ state. */
313 if (s->status & KBD_STAT_OBF) {
314 if (s->status & KBD_STAT_MOUSE_OBF)
315 {
316 if (s->mode & KBD_MODE_MOUSE_INT)
317 irq12_level = 1;
318 }
319 else
320 { /* KBD_STAT_OBF set but KBD_STAT_MOUSE_OBF isn't. */
321 if (s->mode & KBD_MODE_KBD_INT)
322 irq1_level = 1;
323 }
324 }
325 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), 1, irq1_level);
326 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), 12, irq12_level);
327}
328
329void KBCUpdateInterrupts(void *pKbc)
330{
331 KBDState *s = (KBDState *)pKbc;
332 kbd_update_irq(s);
333}
334
335static void kbd_queue(KBDState *s, int b, int aux)
336{
337 MouseCmdQueue *mcq = &s->mouse_command_queue;
338 MouseEventQueue *meq = &s->mouse_event_queue;
339
340#if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
341 if (aux == 1)
342 LogRel3(("%s: mouse command response: 0x%02x\n", __PRETTY_FUNCTION__, b));
343 else if (aux == 2)
344 LogRel3(("%s: mouse event data: 0x%02x\n", __PRETTY_FUNCTION__, b));
345#ifdef DEBUG_KBD
346 else
347 LogRel3(("%s: kbd event: 0x%02x\n", __PRETTY_FUNCTION__, b));
348#endif
349#endif
350 switch (aux)
351 {
352 case 0: /* keyboard */
353 AssertMsgFailed(("kbd_queue() no longer supported for keyboard!\n"));
354 break;
355 case 1: /* mouse command response */
356 if (mcq->count >= MOUSE_CMD_QUEUE_SIZE)
357 return;
358 mcq->data[mcq->wptr] = b;
359 if (++mcq->wptr == MOUSE_CMD_QUEUE_SIZE)
360 mcq->wptr = 0;
361 mcq->count++;
362 break;
363 case 2: /* mouse event data */
364 if (meq->count >= MOUSE_EVENT_QUEUE_SIZE)
365 return;
366 meq->data[meq->wptr] = b;
367 if (++meq->wptr == MOUSE_EVENT_QUEUE_SIZE)
368 meq->wptr = 0;
369 meq->count++;
370 break;
371 default:
372 AssertMsgFailed(("aux=%d\n", aux));
373 }
374 kbd_update_irq(s);
375}
376
377static void kbc_dbb_out(void *opaque, uint8_t val)
378{
379 KBDState *s = (KBDState*)opaque;
380
381 s->dbbout = val;
382 /* Set the OBF and raise IRQ. */
383 s->status |= KBD_STAT_OBF;
384 if (s->mode & KBD_MODE_KBD_INT)
385 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), 1, 1);
386}
387
388static uint32_t kbd_read_status(void *opaque, uint32_t addr)
389{
390 KBDState *s = (KBDState*)opaque;
391 int val = s->status;
392 NOREF(addr);
393
394#if defined(DEBUG_KBD)
395 Log(("kbd: read status=0x%02x\n", val));
396#endif
397 return val;
398}
399
400static int kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
401{
402 int rc = VINF_SUCCESS;
403 KBDState *s = (KBDState*)opaque;
404 NOREF(addr);
405
406#ifdef DEBUG_KBD
407 Log(("kbd: write cmd=0x%02x\n", val));
408#endif
409 switch(val) {
410 case KBD_CCMD_READ_MODE:
411 kbc_dbb_out(s, s->mode);
412 break;
413 case KBD_CCMD_WRITE_MODE:
414 case KBD_CCMD_WRITE_OBUF:
415 case KBD_CCMD_WRITE_AUX_OBUF:
416 case KBD_CCMD_WRITE_MOUSE:
417 case KBD_CCMD_WRITE_OUTPORT:
418 s->write_cmd = val;
419 break;
420 case KBD_CCMD_MOUSE_DISABLE:
421 s->mode |= KBD_MODE_DISABLE_MOUSE;
422 break;
423 case KBD_CCMD_MOUSE_ENABLE:
424 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
425 /* Check for queued input. */
426 kbd_update_irq(s);
427 break;
428 case KBD_CCMD_TEST_MOUSE:
429 kbc_dbb_out(s, 0x00);
430 break;
431 case KBD_CCMD_SELF_TEST:
432 /* Enable the A20 line - that is the power-on state(!). */
433# ifndef IN_RING3
434 if (!PDMDevHlpA20IsEnabled(s->CTX_SUFF(pDevIns)))
435 {
436 rc = VINF_IOM_R3_IOPORT_WRITE;
437 break;
438 }
439# else /* IN_RING3 */
440 PDMDevHlpA20Set(s->CTX_SUFF(pDevIns), true);
441# endif /* IN_RING3 */
442 s->status |= KBD_STAT_SELFTEST;
443 s->mode |= KBD_MODE_DISABLE_KBD;
444 kbc_dbb_out(s, 0x55);
445 break;
446 case KBD_CCMD_KBD_TEST:
447 kbc_dbb_out(s, 0x00);
448 break;
449 case KBD_CCMD_KBD_DISABLE:
450 s->mode |= KBD_MODE_DISABLE_KBD;
451 break;
452 case KBD_CCMD_KBD_ENABLE:
453 s->mode &= ~KBD_MODE_DISABLE_KBD;
454 /* Check for queued input. */
455 kbd_update_irq(s);
456 break;
457 case KBD_CCMD_READ_INPORT:
458 kbc_dbb_out(s, 0x00);
459 break;
460 case KBD_CCMD_READ_OUTPORT:
461 /* XXX: check that */
462#ifdef TARGET_I386
463 val = 0x01 | (PDMDevHlpA20IsEnabled(s->CTX_SUFF(pDevIns)) << 1);
464#else
465 val = 0x01;
466#endif
467 if (s->status & KBD_STAT_OBF)
468 val |= 0x10;
469 if (s->status & KBD_STAT_MOUSE_OBF)
470 val |= 0x20;
471 kbc_dbb_out(s, val);
472 break;
473#ifdef TARGET_I386
474 case KBD_CCMD_ENABLE_A20:
475# ifndef IN_RING3
476 if (!PDMDevHlpA20IsEnabled(s->CTX_SUFF(pDevIns)))
477 rc = VINF_IOM_R3_IOPORT_WRITE;
478# else /* IN_RING3 */
479 PDMDevHlpA20Set(s->CTX_SUFF(pDevIns), true);
480# endif /* IN_RING3 */
481 break;
482 case KBD_CCMD_DISABLE_A20:
483# ifndef IN_RING3
484 if (PDMDevHlpA20IsEnabled(s->CTX_SUFF(pDevIns)))
485 rc = VINF_IOM_R3_IOPORT_WRITE;
486# else /* IN_RING3 */
487 PDMDevHlpA20Set(s->CTX_SUFF(pDevIns), false);
488# endif /* !IN_RING3 */
489 break;
490#endif
491 case KBD_CCMD_READ_TSTINP:
492 /* Keyboard clock line is zero IFF keyboard is disabled */
493 val = (s->mode & KBD_MODE_DISABLE_KBD) ? 0 : 1;
494 kbc_dbb_out(s, val);
495 break;
496 case KBD_CCMD_RESET:
497#ifndef IN_RING3
498 rc = VINF_IOM_R3_IOPORT_WRITE;
499#else /* IN_RING3 */
500 LogRel(("Reset initiated by keyboard controller\n"));
501 rc = PDMDevHlpVMReset(s->CTX_SUFF(pDevIns));
502#endif /* !IN_RING3 */
503 break;
504 case 0xff:
505 /* ignore that - I don't know what is its use */
506 break;
507 /* Make OS/2 happy. */
508 /* The 8042 RAM is readable using commands 0x20 thru 0x3f, and writable
509 by 0x60 thru 0x7f. Now days only the firs byte, the mode, is used.
510 We'll ignore the writes (0x61..7f) and return 0 for all the reads
511 just to make some OS/2 debug stuff a bit happier. */
512 case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
513 case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
514 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
515 case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
516 kbc_dbb_out(s, 0);
517 Log(("kbd: reading non-standard RAM addr %#x\n", val & 0x1f));
518 break;
519 default:
520 Log(("kbd: unsupported keyboard cmd=0x%02x\n", val));
521 break;
522 }
523 return rc;
524}
525
526static uint32_t kbd_read_data(void *opaque, uint32_t addr)
527{
528 KBDState *s = (KBDState*)opaque;
529 uint32_t val;
530 NOREF(addr);
531
532 /* Return the current DBB contents. */
533 val = s->dbbout;
534
535 /* Reading the DBB deasserts IRQs... */
536 if (s->status & KBD_STAT_MOUSE_OBF)
537 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), 12, 0);
538 else
539 PDMDevHlpISASetIrq(s->CTX_SUFF(pDevIns), 1, 0);
540 /* ...and clears the OBF bits. */
541 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
542
543 /* Check if more data is available. */
544 kbd_update_irq(s);
545#ifdef DEBUG_KBD
546 Log(("kbd: read data=0x%02x\n", val));
547#endif
548 return val;
549}
550
551PS2K *KBDGetPS2KFromDevIns(PPDMDEVINS pDevIns)
552{
553 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
554 return &pThis->Kbd;
555}
556
557static void kbd_mouse_set_reported_buttons(KBDState *s, unsigned fButtons, unsigned fButtonMask)
558{
559 s->mouse_buttons_reported |= (fButtons & fButtonMask);
560 s->mouse_buttons_reported &= (fButtons | ~fButtonMask);
561}
562
563/**
564 * Send a single relative packet in 3-byte PS/2 format to the PS/2 controller.
565 * @param s keyboard state object
566 * @param dx relative X value, must be between -256 and +255
567 * @param dy relative y value, must be between -256 and +255
568 * @param fButtonsLow the state of the two first mouse buttons
569 * @param fButtonsPacked the state of the upper three mouse buttons and
570 * scroll wheel movement, packed as per the
571 * MOUSE_EXT_* defines. For standard PS/2 packets
572 * only pass the value of button 3 here.
573 */
574static void kbd_mouse_send_rel3_packet(KBDState *s, bool fToCmdQueue)
575{
576 int aux = fToCmdQueue ? 1 : 2;
577 int dx1 = s->mouse_dx < 0 ? RT_MAX(s->mouse_dx, -256)
578 : RT_MIN(s->mouse_dx, 255);
579 int dy1 = s->mouse_dy < 0 ? RT_MAX(s->mouse_dy, -256)
580 : RT_MIN(s->mouse_dy, 255);
581 unsigned int b;
582 unsigned fButtonsLow = s->mouse_buttons & 0x07;
583 s->mouse_dx -= dx1;
584 s->mouse_dy -= dy1;
585 kbd_mouse_set_reported_buttons(s, fButtonsLow, 0x07);
586 LogRel3(("%s: dx1=%d, dy1=%d, fButtonsLow=0x%x\n",
587 __PRETTY_FUNCTION__, dx1, dy1, fButtonsLow));
588 b = 0x08 | ((dx1 < 0 ? 1 : 0) << 4) | ((dy1 < 0 ? 1 : 0) << 5)
589 | fButtonsLow;
590 kbd_queue(s, b, aux);
591 kbd_queue(s, dx1 & 0xff, aux);
592 kbd_queue(s, dy1 & 0xff, aux);
593}
594
595static void kbd_mouse_send_imps2_byte4(KBDState *s, bool fToCmdQueue)
596{
597 int aux = fToCmdQueue ? 1 : 2;
598
599 int dz1 = s->mouse_dz < 0 ? RT_MAX(s->mouse_dz, -127)
600 : RT_MIN(s->mouse_dz, 127);
601 LogRel3(("%s: dz1=%d\n", __PRETTY_FUNCTION__, dz1));
602 s->mouse_dz -= dz1;
603 kbd_queue(s, dz1 & 0xff, aux);
604}
605
606static void kbd_mouse_send_imex_byte4(KBDState *s, bool fToCmdQueue)
607{
608 int aux = fToCmdQueue ? 1 : 2;
609 int dz1 = 0, dw1 = 0;
610 unsigned fButtonsHigh = s->mouse_buttons & 0x18;
611
612 if (s->mouse_dw > 0)
613 dw1 = 1;
614 else if (s->mouse_dw < 0)
615 dw1 = -1;
616 else if (s->mouse_dz > 0)
617 dz1 = 1;
618 else if (s->mouse_dz < 0)
619 dz1 = -1;
620 if (s->mouse_dw && s->mouse_flags & MOUSE_REPORT_HORIZONTAL)
621 {
622 LogRel3(("%s: dw1=%d\n", __PRETTY_FUNCTION__, dw1));
623 kbd_queue(s, 0x40 | (dw1 & 0x3f), aux);
624 }
625 else
626 {
627 LogRel3(("%s: dz1=%d, dw1=%d, fButtonsHigh=0x%x\n",
628 __PRETTY_FUNCTION__, dz1, dw1, fButtonsHigh));
629 unsigned u4Low = dw1 > 0 ? 9 /* -7 & 0xf */
630 : dw1 < 0 ? 7
631 : dz1 > 0 ? 1
632 : dz1 < 0 ? 0xf /* -1 & 0xf */
633 : 0;
634 kbd_mouse_set_reported_buttons(s, fButtonsHigh, 0x18);
635 kbd_queue(s, (fButtonsHigh << 1) | u4Low, aux);
636 }
637 s->mouse_dz -= dz1;
638 s->mouse_dw -= dw1;
639}
640
641/**
642 * Send a single relative packet in (IM)PS/2 or IMEX format to the PS/2
643 * controller.
644 * @param s keyboard state object
645 * @param fToCmdQueue should this packet go to the command queue (or the
646 * event queue)?
647 */
648static void kbd_mouse_send_packet(KBDState *s, bool fToCmdQueue)
649{
650 kbd_mouse_send_rel3_packet(s, fToCmdQueue);
651 if (s->mouse_type == MOUSE_PROT_IMPS2)
652 kbd_mouse_send_imps2_byte4(s, fToCmdQueue);
653 if (s->mouse_type == MOUSE_PROT_IMEX)
654 kbd_mouse_send_imex_byte4(s, fToCmdQueue);
655}
656
657#ifdef IN_RING3
658
659static bool kbd_mouse_unreported(KBDState *s)
660{
661 return s->mouse_dx
662 || s->mouse_dy
663 || s->mouse_dz
664 || s->mouse_dw
665 || s->mouse_buttons != s->mouse_buttons_reported;
666}
667
668static size_t kbd_mouse_event_queue_free(KBDState *s)
669{
670 AssertReturn(s->mouse_event_queue.count <= MOUSE_EVENT_QUEUE_SIZE, 0);
671 return MOUSE_EVENT_QUEUE_SIZE - s->mouse_event_queue.count;
672}
673
674static void pc_kbd_mouse_event(void *opaque, int dx, int dy, int dz, int dw,
675 int buttons_state)
676{
677 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons_state=0x%x\n",
678 __PRETTY_FUNCTION__, dx, dy, dz, dw, buttons_state));
679 KBDState *s = (KBDState*)opaque;
680
681 /* check if deltas are recorded when disabled */
682 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
683 return;
684 AssertReturnVoid((buttons_state & ~0x1f) == 0);
685
686 s->mouse_dx += dx;
687 s->mouse_dy -= dy;
688 if ( (s->mouse_type == MOUSE_PROT_IMPS2)
689 || (s->mouse_type == MOUSE_PROT_IMEX))
690 s->mouse_dz += dz;
691 if (s->mouse_type == MOUSE_PROT_IMEX)
692 s->mouse_dw += dw;
693 s->mouse_buttons = buttons_state;
694 if (!(s->mouse_status & MOUSE_STATUS_REMOTE))
695 /* if not remote, send event. Multiple events are sent if
696 too big deltas */
697 while ( kbd_mouse_unreported(s)
698 && kbd_mouse_event_queue_free(s) > 4)
699 kbd_mouse_send_packet(s, false);
700}
701
702/* Report a change in status down the driver chain */
703static void kbd_mouse_update_downstream_status(KBDState *pThis)
704{
705 PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
706 bool fEnabled = !!(pThis->mouse_status & MOUSE_STATUS_ENABLED);
707 if (pDrv)
708 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
709}
710
711#endif /* IN_RING3 */
712
713static int kbd_write_mouse(KBDState *s, int val)
714{
715#ifdef DEBUG_MOUSE
716 LogRelFlowFunc(("kbd: write mouse 0x%02x\n", val));
717#endif
718 int rc = VINF_SUCCESS;
719 /* Flush the mouse command response queue. */
720 s->mouse_command_queue.count = 0;
721 s->mouse_command_queue.rptr = 0;
722 s->mouse_command_queue.wptr = 0;
723 switch(s->mouse_write_cmd) {
724 default:
725 case -1:
726 /* mouse command */
727 if (s->mouse_wrap) {
728 if (val == AUX_RESET_WRAP) {
729 s->mouse_wrap = 0;
730 kbd_queue(s, AUX_ACK, 1);
731 return VINF_SUCCESS;
732 } else if (val != AUX_RESET) {
733 kbd_queue(s, val, 1);
734 return VINF_SUCCESS;
735 }
736 }
737 switch(val) {
738 case AUX_SET_SCALE11:
739 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
740 kbd_queue(s, AUX_ACK, 1);
741 break;
742 case AUX_SET_SCALE21:
743 s->mouse_status |= MOUSE_STATUS_SCALE21;
744 kbd_queue(s, AUX_ACK, 1);
745 break;
746 case AUX_SET_STREAM:
747 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
748 kbd_queue(s, AUX_ACK, 1);
749 break;
750 case AUX_SET_WRAP:
751 s->mouse_wrap = 1;
752 kbd_queue(s, AUX_ACK, 1);
753 break;
754 case AUX_SET_REMOTE:
755 s->mouse_status |= MOUSE_STATUS_REMOTE;
756 kbd_queue(s, AUX_ACK, 1);
757 break;
758 case AUX_GET_TYPE:
759 kbd_queue(s, AUX_ACK, 1);
760 kbd_queue(s, s->mouse_type, 1);
761 break;
762 case AUX_SET_RES:
763 case AUX_SET_SAMPLE:
764 s->mouse_write_cmd = val;
765 kbd_queue(s, AUX_ACK, 1);
766 break;
767 case AUX_GET_SCALE:
768 kbd_queue(s, AUX_ACK, 1);
769 kbd_queue(s, s->mouse_status, 1);
770 kbd_queue(s, s->mouse_resolution, 1);
771 kbd_queue(s, s->mouse_sample_rate, 1);
772 break;
773 case AUX_POLL:
774 kbd_queue(s, AUX_ACK, 1);
775 kbd_mouse_send_packet(s, true);
776 break;
777 case AUX_ENABLE_DEV:
778#ifdef IN_RING3
779 LogRelFlowFunc(("Enabling mouse device\n"));
780 s->mouse_status |= MOUSE_STATUS_ENABLED;
781 kbd_queue(s, AUX_ACK, 1);
782 kbd_mouse_update_downstream_status(s);
783#else
784 LogRelFlowFunc(("Enabling mouse device, R0 stub\n"));
785 rc = VINF_IOM_R3_IOPORT_WRITE;
786#endif
787 break;
788 case AUX_DISABLE_DEV:
789#ifdef IN_RING3
790 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
791 kbd_queue(s, AUX_ACK, 1);
792 /* Flush the mouse events queue. */
793 s->mouse_event_queue.count = 0;
794 s->mouse_event_queue.rptr = 0;
795 s->mouse_event_queue.wptr = 0;
796 kbd_mouse_update_downstream_status(s);
797#else
798 rc = VINF_IOM_R3_IOPORT_WRITE;
799#endif
800 break;
801 case AUX_SET_DEFAULT:
802#ifdef IN_RING3
803 s->mouse_sample_rate = 100;
804 s->mouse_resolution = 2;
805 s->mouse_status = 0;
806 kbd_queue(s, AUX_ACK, 1);
807 kbd_mouse_update_downstream_status(s);
808#else
809 rc = VINF_IOM_R3_IOPORT_WRITE;
810#endif
811 break;
812 case AUX_RESET:
813#ifdef IN_RING3
814 s->mouse_sample_rate = 100;
815 s->mouse_resolution = 2;
816 s->mouse_status = 0;
817 s->mouse_type = MOUSE_PROT_PS2;
818 kbd_queue(s, AUX_ACK, 1);
819 kbd_queue(s, 0xaa, 1);
820 kbd_queue(s, s->mouse_type, 1);
821 /* Flush the mouse events queue. */
822 s->mouse_event_queue.count = 0;
823 s->mouse_event_queue.rptr = 0;
824 s->mouse_event_queue.wptr = 0;
825 kbd_mouse_update_downstream_status(s);
826#else
827 rc = VINF_IOM_R3_IOPORT_WRITE;
828#endif
829 break;
830 default:
831 /* NACK all commands we don't know.
832
833 The usecase for this is the OS/2 mouse driver which will try
834 read 0xE2 in order to figure out if it's a trackpoint device
835 or not. If it doesn't get a NACK (or ACK) on the command it'll
836 do several hundred thousand status reads before giving up. This
837 is slows down the OS/2 boot up considerably. (It also seems that
838 the code is somehow vulnerable while polling like this and that
839 mouse or keyboard input at this point might screw things up badly.)
840
841 From http://www.win.tue.nl/~aeb/linux/kbd/scancodes-13.html:
842
843 Every command or data byte sent to the mouse (except for the
844 resend command fe) is ACKed with fa. If the command or data
845 is invalid, it is NACKed with fe. If the next byte is again
846 invalid, the reply is ERROR: fc. */
847 /** @todo send error if we NACKed the previous command? */
848 kbd_queue(s, AUX_NACK, 1);
849 break;
850 }
851 break;
852 case AUX_SET_SAMPLE:
853 s->mouse_sample_rate = val;
854 /* detect IMPS/2 or IMEX */
855 /* And enable horizontal scrolling reporting when requested */
856 switch(s->mouse_detect_state) {
857 default:
858 case 0:
859 if (val == 200)
860 s->mouse_detect_state = 1;
861 break;
862 case 1:
863 if (val == 100)
864 s->mouse_detect_state = 2;
865 else if (val == 200)
866 s->mouse_detect_state = 3;
867 else if ((val == 80) && s->mouse_type == MOUSE_PROT_IMEX)
868 /* enable horizontal scrolling, byte two */
869 s->mouse_detect_state = 4;
870 else
871 s->mouse_detect_state = 0;
872 break;
873 case 2:
874 if (val == 80 && s->mouse_type < MOUSE_PROT_IMEX)
875 {
876 LogRelFlowFunc(("switching mouse device to IMPS/2 mode\n"));
877 s->mouse_type = MOUSE_PROT_IMPS2;
878 }
879 s->mouse_detect_state = 0;
880 break;
881 case 3:
882 if (val == 80)
883 {
884 LogRelFlowFunc(("switching mouse device to IMEX mode\n"));
885 s->mouse_type = MOUSE_PROT_IMEX;
886 }
887 s->mouse_detect_state = 0;
888 break;
889 case 4:
890 if (val == 40)
891 {
892 LogRelFlowFunc(("enabling IMEX horizontal scrolling reporting\n"));
893 s->mouse_flags |= MOUSE_REPORT_HORIZONTAL;
894 }
895 s->mouse_detect_state = 0;
896 break;
897 }
898 kbd_queue(s, AUX_ACK, 1);
899 s->mouse_write_cmd = -1;
900 break;
901 case AUX_SET_RES:
902 if (0 <= val && val < 4)
903 {
904 s->mouse_resolution = val;
905 kbd_queue(s, AUX_ACK, 1);
906 }
907 else
908 kbd_queue(s, AUX_NACK, 1);
909 s->mouse_write_cmd = -1;
910 break;
911 }
912 return rc;
913}
914
915static int kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
916{
917 int rc = VINF_SUCCESS;
918 KBDState *s = (KBDState*)opaque;
919 NOREF(addr);
920
921#ifdef DEBUG_KBD
922 Log(("kbd: write data=0x%02x\n", val));
923#endif
924
925 switch(s->write_cmd) {
926 case 0:
927 /* Automatically enables keyboard interface. */
928 s->mode &= ~KBD_MODE_DISABLE_KBD;
929 rc = PS2KByteToKbd(&s->Kbd, val);
930 if (rc == VINF_SUCCESS)
931 kbd_update_irq(s);
932 break;
933 case KBD_CCMD_WRITE_MODE:
934 s->mode = val;
935 s->translate = (s->mode & KBD_MODE_KCC) == KBD_MODE_KCC;
936 kbd_update_irq(s);
937 break;
938 case KBD_CCMD_WRITE_OBUF:
939 kbc_dbb_out(s, val);
940 break;
941 case KBD_CCMD_WRITE_AUX_OBUF:
942 kbd_queue(s, val, 1);
943 break;
944 case KBD_CCMD_WRITE_OUTPORT:
945#ifdef TARGET_I386
946# ifndef IN_RING3
947 if (PDMDevHlpA20IsEnabled(s->CTX_SUFF(pDevIns)) != !!(val & 2))
948 rc = VINF_IOM_R3_IOPORT_WRITE;
949# else /* IN_RING3 */
950 PDMDevHlpA20Set(s->CTX_SUFF(pDevIns), !!(val & 2));
951# endif /* !IN_RING3 */
952#endif
953 if (!(val & 1)) {
954# ifndef IN_RING3
955 rc = VINF_IOM_R3_IOPORT_WRITE;
956# else
957 rc = PDMDevHlpVMReset(s->CTX_SUFF(pDevIns));
958# endif
959 }
960 break;
961 case KBD_CCMD_WRITE_MOUSE:
962 /* Automatically enables aux interface. */
963 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
964 rc = kbd_write_mouse(s, val);
965 break;
966 default:
967 break;
968 }
969 if (rc != VINF_IOM_R3_IOPORT_WRITE)
970 s->write_cmd = 0;
971 return rc;
972}
973
974#ifdef IN_RING3
975
976static void kbd_reset(void *opaque)
977{
978 KBDState *s = (KBDState*)opaque;
979 MouseCmdQueue *mcq;
980 MouseEventQueue *meq;
981
982 s->mouse_write_cmd = -1;
983 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
984 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
985 /* Resetting everything, keyword was not working right on NT4 reboot. */
986 s->write_cmd = 0;
987 s->translate = 0;
988 if (s->mouse_status)
989 {
990 s->mouse_status = 0;
991 kbd_mouse_update_downstream_status(s);
992 }
993 s->mouse_resolution = 0;
994 s->mouse_sample_rate = 0;
995 s->mouse_wrap = 0;
996 s->mouse_type = MOUSE_PROT_PS2;
997 s->mouse_detect_state = 0;
998 s->mouse_dx = 0;
999 s->mouse_dy = 0;
1000 s->mouse_dz = 0;
1001 s->mouse_dw = 0;
1002 s->mouse_flags = 0;
1003 s->mouse_buttons = 0;
1004 s->mouse_buttons_reported = 0;
1005 mcq = &s->mouse_command_queue;
1006 mcq->rptr = 0;
1007 mcq->wptr = 0;
1008 mcq->count = 0;
1009 meq = &s->mouse_event_queue;
1010 meq->rptr = 0;
1011 meq->wptr = 0;
1012 meq->count = 0;
1013}
1014
1015static void kbd_save(QEMUFile* f, void* opaque)
1016{
1017 uint32_t cItems;
1018 int i;
1019 KBDState *s = (KBDState*)opaque;
1020
1021 qemu_put_8s(f, &s->write_cmd);
1022 qemu_put_8s(f, &s->status);
1023 qemu_put_8s(f, &s->mode);
1024 qemu_put_8s(f, &s->dbbout);
1025 qemu_put_be32s(f, &s->mouse_write_cmd);
1026 qemu_put_8s(f, &s->mouse_status);
1027 qemu_put_8s(f, &s->mouse_resolution);
1028 qemu_put_8s(f, &s->mouse_sample_rate);
1029 qemu_put_8s(f, &s->mouse_wrap);
1030 qemu_put_8s(f, &s->mouse_type);
1031 qemu_put_8s(f, &s->mouse_detect_state);
1032 qemu_put_be32s(f, &s->mouse_dx);
1033 qemu_put_be32s(f, &s->mouse_dy);
1034 qemu_put_be32s(f, &s->mouse_dz);
1035 qemu_put_be32s(f, &s->mouse_dw);
1036 qemu_put_be32s(f, &s->mouse_flags);
1037 qemu_put_8s(f, &s->mouse_buttons);
1038 qemu_put_8s(f, &s->mouse_buttons_reported);
1039
1040 cItems = s->mouse_command_queue.count;
1041 SSMR3PutU32(f, cItems);
1042 for (i = s->mouse_command_queue.rptr; cItems-- > 0; i = (i + 1) % RT_ELEMENTS(s->mouse_command_queue.data))
1043 SSMR3PutU8(f, s->mouse_command_queue.data[i]);
1044 Log(("kbd_save: %d mouse command queue items stored\n", s->mouse_command_queue.count));
1045
1046 cItems = s->mouse_event_queue.count;
1047 SSMR3PutU32(f, cItems);
1048 for (i = s->mouse_event_queue.rptr; cItems-- > 0; i = (i + 1) % RT_ELEMENTS(s->mouse_event_queue.data))
1049 SSMR3PutU8(f, s->mouse_event_queue.data[i]);
1050 Log(("kbd_save: %d mouse event queue items stored\n", s->mouse_event_queue.count));
1051
1052 /* terminator */
1053 SSMR3PutU32(f, ~0);
1054}
1055
1056static int kbd_load(QEMUFile* f, void* opaque, int version_id)
1057{
1058 uint32_t u32, i;
1059 uint8_t u8Dummy;
1060 uint32_t u32Dummy;
1061 int rc;
1062 KBDState *s = (KBDState*)opaque;
1063
1064#if 0
1065 /** @todo enable this and remove the "if (version_id == 4)" code at some
1066 * later time */
1067 /* Version 4 was never created by any publicly released version of VBox */
1068 AssertReturn(version_id != 4, VERR_NOT_SUPPORTED);
1069#endif
1070 if (version_id < 2 || version_id > PCKBD_SAVED_STATE_VERSION)
1071 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1072 qemu_get_8s(f, &s->write_cmd);
1073 qemu_get_8s(f, &s->status);
1074 qemu_get_8s(f, &s->mode);
1075 if (version_id <= 5)
1076 {
1077 qemu_get_be32s(f, (uint32_t *)&u32Dummy);
1078 qemu_get_be32s(f, (uint32_t *)&u32Dummy);
1079 }
1080 else
1081 {
1082 qemu_get_8s(f, &s->dbbout);
1083 }
1084 qemu_get_be32s(f, (uint32_t *)&s->mouse_write_cmd);
1085 qemu_get_8s(f, &s->mouse_status);
1086 qemu_get_8s(f, &s->mouse_resolution);
1087 qemu_get_8s(f, &s->mouse_sample_rate);
1088 qemu_get_8s(f, &s->mouse_wrap);
1089 qemu_get_8s(f, &s->mouse_type);
1090 qemu_get_8s(f, &s->mouse_detect_state);
1091 qemu_get_be32s(f, (uint32_t *)&s->mouse_dx);
1092 qemu_get_be32s(f, (uint32_t *)&s->mouse_dy);
1093 qemu_get_be32s(f, (uint32_t *)&s->mouse_dz);
1094 if (version_id > 2)
1095 {
1096 SSMR3GetS32(f, &s->mouse_dw);
1097 SSMR3GetS32(f, &s->mouse_flags);
1098 }
1099 qemu_get_8s(f, &s->mouse_buttons);
1100 if (version_id == 4)
1101 {
1102 SSMR3GetU32(f, &u32Dummy);
1103 SSMR3GetU32(f, &u32Dummy);
1104 }
1105 if (version_id > 3)
1106 SSMR3GetU8(f, &s->mouse_buttons_reported);
1107 if (version_id == 4)
1108 SSMR3GetU8(f, &u8Dummy);
1109 s->mouse_command_queue.count = 0;
1110 s->mouse_command_queue.rptr = 0;
1111 s->mouse_command_queue.wptr = 0;
1112 s->mouse_event_queue.count = 0;
1113 s->mouse_event_queue.rptr = 0;
1114 s->mouse_event_queue.wptr = 0;
1115
1116 /* Determine the translation state. */
1117 s->translate = (s->mode & KBD_MODE_KCC) == KBD_MODE_KCC;
1118
1119 /*
1120 * Load the queues
1121 */
1122 if (version_id <= 5)
1123 {
1124 rc = SSMR3GetU32(f, &u32);
1125 if (RT_FAILURE(rc))
1126 return rc;
1127 for (i = 0; i < u32; i++)
1128 {
1129 rc = SSMR3GetU8(f, &u8Dummy);
1130 if (RT_FAILURE(rc))
1131 return rc;
1132 }
1133 Log(("kbd_load: %d keyboard queue items discarded from old saved state\n", u32));
1134 }
1135
1136 rc = SSMR3GetU32(f, &u32);
1137 if (RT_FAILURE(rc))
1138 return rc;
1139 if (u32 > RT_ELEMENTS(s->mouse_command_queue.data))
1140 {
1141 AssertMsgFailed(("u32=%#x\n", u32));
1142 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1143 }
1144 for (i = 0; i < u32; i++)
1145 {
1146 rc = SSMR3GetU8(f, &s->mouse_command_queue.data[i]);
1147 if (RT_FAILURE(rc))
1148 return rc;
1149 }
1150 s->mouse_command_queue.wptr = u32 % RT_ELEMENTS(s->mouse_command_queue.data);
1151 s->mouse_command_queue.count = u32;
1152 Log(("kbd_load: %d mouse command queue items loaded\n", u32));
1153
1154 rc = SSMR3GetU32(f, &u32);
1155 if (RT_FAILURE(rc))
1156 return rc;
1157 if (u32 > RT_ELEMENTS(s->mouse_event_queue.data))
1158 {
1159 AssertMsgFailed(("u32=%#x\n", u32));
1160 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1161 }
1162 for (i = 0; i < u32; i++)
1163 {
1164 rc = SSMR3GetU8(f, &s->mouse_event_queue.data[i]);
1165 if (RT_FAILURE(rc))
1166 return rc;
1167 }
1168 s->mouse_event_queue.wptr = u32 % RT_ELEMENTS(s->mouse_event_queue.data);
1169 s->mouse_event_queue.count = u32;
1170 Log(("kbd_load: %d mouse event queue items loaded\n", u32));
1171
1172 /* terminator */
1173 rc = SSMR3GetU32(f, &u32);
1174 if (RT_FAILURE(rc))
1175 return rc;
1176 if (u32 != ~0U)
1177 {
1178 AssertMsgFailed(("u32=%#x\n", u32));
1179 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1180 }
1181 /* Resend a notification to Main if the device is active */
1182 kbd_mouse_update_downstream_status(s);
1183 return 0;
1184}
1185#endif /* IN_RING3 */
1186
1187
1188/* VirtualBox code start */
1189
1190/* -=-=-=-=-=- wrappers -=-=-=-=-=- */
1191
1192/**
1193 * Port I/O Handler for keyboard data IN operations.
1194 *
1195 * @returns VBox status code.
1196 *
1197 * @param pDevIns The device instance.
1198 * @param pvUser User argument - ignored.
1199 * @param Port Port number used for the IN operation.
1200 * @param pu32 Where to store the result.
1201 * @param cb Number of bytes read.
1202 */
1203PDMBOTHCBDECL(int) kbdIOPortDataRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1204{
1205 NOREF(pvUser);
1206 if (cb == 1)
1207 {
1208 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1209 *pu32 = kbd_read_data(pThis, Port);
1210 Log2(("kbdIOPortDataRead: Port=%#x cb=%d *pu32=%#x\n", Port, cb, *pu32));
1211 return VINF_SUCCESS;
1212 }
1213 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
1214 return VERR_IOM_IOPORT_UNUSED;
1215}
1216
1217/**
1218 * Port I/O Handler for keyboard data OUT operations.
1219 *
1220 * @returns VBox status code.
1221 *
1222 * @param pDevIns The device instance.
1223 * @param pvUser User argument - ignored.
1224 * @param Port Port number used for the IN operation.
1225 * @param u32 The value to output.
1226 * @param cb The value size in bytes.
1227 */
1228PDMBOTHCBDECL(int) kbdIOPortDataWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1229{
1230 int rc = VINF_SUCCESS;
1231 NOREF(pvUser);
1232 if (cb == 1)
1233 {
1234 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1235 rc = kbd_write_data(pThis, Port, u32);
1236 Log2(("kbdIOPortDataWrite: Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
1237 }
1238 else
1239 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
1240 return rc;
1241}
1242
1243/**
1244 * Port I/O Handler for keyboard status IN operations.
1245 *
1246 * @returns VBox status code.
1247 *
1248 * @param pDevIns The device instance.
1249 * @param pvUser User argument - ignored.
1250 * @param Port Port number used for the IN operation.
1251 * @param pu32 Where to store the result.
1252 * @param cb Number of bytes read.
1253 */
1254PDMBOTHCBDECL(int) kbdIOPortStatusRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
1255{
1256 NOREF(pvUser);
1257 if (cb == 1)
1258 {
1259 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1260 *pu32 = kbd_read_status(pThis, Port);
1261 Log2(("kbdIOPortStatusRead: Port=%#x cb=%d -> *pu32=%#x\n", Port, cb, *pu32));
1262 return VINF_SUCCESS;
1263 }
1264 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
1265 return VERR_IOM_IOPORT_UNUSED;
1266}
1267
1268/**
1269 * Port I/O Handler for keyboard command OUT operations.
1270 *
1271 * @returns VBox status code.
1272 *
1273 * @param pDevIns The device instance.
1274 * @param pvUser User argument - ignored.
1275 * @param Port Port number used for the IN operation.
1276 * @param u32 The value to output.
1277 * @param cb The value size in bytes.
1278 */
1279PDMBOTHCBDECL(int) kbdIOPortCommandWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
1280{
1281 int rc = VINF_SUCCESS;
1282 NOREF(pvUser);
1283 if (cb == 1)
1284 {
1285 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1286 rc = kbd_write_command(pThis, Port, u32);
1287 Log2(("kbdIOPortCommandWrite: Port=%#x cb=%d u32=%#x rc=%Rrc\n", Port, cb, u32, rc));
1288 }
1289 else
1290 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
1291 return rc;
1292}
1293
1294#ifdef IN_RING3
1295
1296/**
1297 * Saves a state of the keyboard device.
1298 *
1299 * @returns VBox status code.
1300 * @param pDevIns The device instance.
1301 * @param pSSM The handle to save the state to.
1302 */
1303static DECLCALLBACK(int) kbdSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
1304{
1305 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1306 kbd_save(pSSM, pThis);
1307 PS2KSaveState(&pThis->Kbd, pSSM);
1308 return VINF_SUCCESS;
1309}
1310
1311
1312/**
1313 * Loads a saved keyboard device state.
1314 *
1315 * @returns VBox status code.
1316 * @param pDevIns The device instance.
1317 * @param pSSM The handle to the saved state.
1318 * @param uVersion The data unit version number.
1319 * @param uPass The data pass.
1320 */
1321static DECLCALLBACK(int) kbdLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1322{
1323 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1324 int rc;
1325
1326 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1327 rc = kbd_load(pSSM, pThis, uVersion);
1328 if (uVersion >= 6)
1329 rc = PS2KLoadState(&pThis->Kbd, pSSM, uVersion);
1330 return rc;
1331}
1332
1333/**
1334 * Reset notification.
1335 *
1336 * @returns VBox status.
1337 * @param pDevIns The device instance data.
1338 */
1339static DECLCALLBACK(void) kbdReset(PPDMDEVINS pDevIns)
1340{
1341 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1342
1343 kbd_reset(pThis);
1344 PS2KReset(&pThis->Kbd);
1345}
1346
1347
1348/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
1349
1350/**
1351 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1352 */
1353static DECLCALLBACK(void *) kbdMouseQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1354{
1355 KBDState *pThis = RT_FROM_MEMBER(pInterface, KBDState, Mouse.IBase);
1356 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
1357 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
1358 return NULL;
1359}
1360
1361
1362/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
1363
1364/**
1365 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEvent}
1366 */
1367static DECLCALLBACK(int) kbdMousePutEvent(PPDMIMOUSEPORT pInterface, int32_t dx,
1368 int32_t dy, int32_t dz, int32_t dw,
1369 uint32_t fButtons)
1370{
1371 KBDState *pThis = RT_FROM_MEMBER(pInterface, KBDState, Mouse.IPort);
1372 int rc = PDMCritSectEnter(pThis->pDevInsR3->pCritSectRoR3, VERR_SEM_BUSY);
1373 AssertReleaseRC(rc);
1374
1375 pc_kbd_mouse_event(pThis, dx, dy, dz, dw, fButtons);
1376
1377 PDMCritSectLeave(pThis->pDevInsR3->pCritSectRoR3);
1378 return VINF_SUCCESS;
1379}
1380
1381/**
1382 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEventAbs}
1383 */
1384static DECLCALLBACK(int) kbdMousePutEventAbs(PPDMIMOUSEPORT pInterface,
1385 uint32_t x, uint32_t y, int32_t dz,
1386 int32_t dw, uint32_t fButtons)
1387{
1388 AssertFailedReturn(VERR_NOT_SUPPORTED);
1389 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(dz); NOREF(dw); NOREF(fButtons);
1390}
1391
1392/**
1393 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEventMT}
1394 */
1395static DECLCALLBACK(int) kbdMousePutEventMT(PPDMIMOUSEPORT pInterface,
1396 uint32_t x, uint32_t y,
1397 uint32_t cContact,
1398 uint32_t fContact)
1399{
1400 AssertFailedReturn(VERR_NOT_SUPPORTED);
1401 NOREF(pInterface); NOREF(x); NOREF(y); NOREF(cContact); NOREF(fContact);
1402}
1403
1404
1405/* -=-=-=-=-=- real code -=-=-=-=-=- */
1406
1407
1408/**
1409 * Attach command.
1410 *
1411 * This is called to let the device attach to a driver for a specified LUN
1412 * during runtime. This is not called during VM construction, the device
1413 * constructor have to attach to all the available drivers.
1414 *
1415 * This is like plugging in the keyboard or mouse after turning on the PC.
1416 *
1417 * @returns VBox status code.
1418 * @param pDevIns The device instance.
1419 * @param iLUN The logical unit which is being detached.
1420 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1421 * @remark The keyboard controller doesn't support this action, this is just
1422 * implemented to try out the driver<->device structure.
1423 */
1424static DECLCALLBACK(int) kbdAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1425{
1426 int rc;
1427 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1428
1429 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
1430 ("PS/2 device does not support hotplugging\n"),
1431 VERR_INVALID_PARAMETER);
1432
1433 switch (iLUN)
1434 {
1435 /* LUN #0: keyboard */
1436 case 0:
1437 rc = PS2KAttach(&pThis->Kbd, pDevIns, iLUN, fFlags);
1438 if (RT_FAILURE(rc))
1439 return rc;
1440 break;
1441
1442 /* LUN #1: aux/mouse */
1443 case 1:
1444 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Aux (Mouse) Port");
1445 if (RT_SUCCESS(rc))
1446 {
1447 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
1448 if (!pThis->Mouse.pDrv)
1449 {
1450 AssertLogRelMsgFailed(("LUN #1 doesn't have a mouse interface! rc=%Rrc\n", rc));
1451 rc = VERR_PDM_MISSING_INTERFACE;
1452 }
1453 }
1454 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
1455 {
1456 Log(("%s/%d: warning: no driver attached to LUN #1!\n", pDevIns->pReg->szName, pDevIns->iInstance));
1457 rc = VINF_SUCCESS;
1458 }
1459 else
1460 AssertLogRelMsgFailed(("Failed to attach LUN #1! rc=%Rrc\n", rc));
1461 break;
1462
1463 default:
1464 AssertMsgFailed(("Invalid LUN #%d\n", iLUN));
1465 return VERR_PDM_NO_SUCH_LUN;
1466 }
1467
1468 return rc;
1469}
1470
1471
1472/**
1473 * Detach notification.
1474 *
1475 * This is called when a driver is detaching itself from a LUN of the device.
1476 * The device should adjust it's state to reflect this.
1477 *
1478 * This is like unplugging the network cable to use it for the laptop or
1479 * something while the PC is still running.
1480 *
1481 * @param pDevIns The device instance.
1482 * @param iLUN The logical unit which is being detached.
1483 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
1484 * @remark The keyboard controller doesn't support this action, this is just
1485 * implemented to try out the driver<->device structure.
1486 */
1487static DECLCALLBACK(void) kbdDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
1488{
1489#if 0
1490 /*
1491 * Reset the interfaces and update the controller state.
1492 */
1493 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1494 switch (iLUN)
1495 {
1496 /* LUN #0: keyboard */
1497 case 0:
1498 pThis->Keyboard.pDrv = NULL;
1499 pThis->Keyboard.pDrvBase = NULL;
1500 break;
1501
1502 /* LUN #1: aux/mouse */
1503 case 1:
1504 pThis->Mouse.pDrv = NULL;
1505 pThis->Mouse.pDrvBase = NULL;
1506 break;
1507
1508 default:
1509 AssertMsgFailed(("Invalid LUN #%d\n", iLUN));
1510 break;
1511 }
1512#else
1513 NOREF(pDevIns); NOREF(iLUN); NOREF(fFlags);
1514#endif
1515}
1516
1517
1518/**
1519 * @copydoc FNPDMDEVRELOCATE
1520 */
1521static DECLCALLBACK(void) kbdRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
1522{
1523 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1524 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1525 PS2KRelocate(&pThis->Kbd, offDelta, pDevIns);
1526}
1527
1528
1529/**
1530 * @interface_method_impl{PDMDEVREG,pfnConstruct}
1531 */
1532static DECLCALLBACK(int) kbdConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
1533{
1534 KBDState *pThis = PDMINS_2_DATA(pDevIns, KBDState *);
1535 int rc;
1536 bool fGCEnabled;
1537 bool fR0Enabled;
1538 Assert(iInstance == 0);
1539
1540 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
1541
1542 /*
1543 * Validate and read the configuration.
1544 */
1545 if (!CFGMR3AreValuesValid(pCfg, "GCEnabled\0R0Enabled\0"))
1546 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
1547 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
1548 if (RT_FAILURE(rc))
1549 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"GCEnabled\" from the config"));
1550 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
1551 if (RT_FAILURE(rc))
1552 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"R0Enabled\" from the config"));
1553 Log(("pckbd: fGCEnabled=%RTbool fR0Enabled=%RTbool\n", fGCEnabled, fR0Enabled));
1554
1555
1556 /*
1557 * Initialize the interfaces.
1558 */
1559 pThis->pDevInsR3 = pDevIns;
1560 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
1561 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
1562
1563 rc = PS2KConstruct(&pThis->Kbd, pDevIns, pThis, iInstance);
1564 if (RT_FAILURE(rc))
1565 return rc;
1566
1567 pThis->Mouse.IBase.pfnQueryInterface = kbdMouseQueryInterface;
1568 pThis->Mouse.IPort.pfnPutEvent = kbdMousePutEvent;
1569 pThis->Mouse.IPort.pfnPutEventAbs = kbdMousePutEventAbs;
1570 pThis->Mouse.IPort.pfnPutEventMT = kbdMousePutEventMT;
1571
1572 /*
1573 * Register I/O ports, save state, keyboard event handler and mouse event handlers.
1574 */
1575 rc = PDMDevHlpIOPortRegister(pDevIns, 0x60, 1, NULL, kbdIOPortDataWrite, kbdIOPortDataRead, NULL, NULL, "PC Keyboard - Data");
1576 if (RT_FAILURE(rc))
1577 return rc;
1578 rc = PDMDevHlpIOPortRegister(pDevIns, 0x64, 1, NULL, kbdIOPortCommandWrite, kbdIOPortStatusRead, NULL, NULL, "PC Keyboard - Command / Status");
1579 if (RT_FAILURE(rc))
1580 return rc;
1581 if (fGCEnabled)
1582 {
1583 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x60, 1, 0, "kbdIOPortDataWrite", "kbdIOPortDataRead", NULL, NULL, "PC Keyboard - Data");
1584 if (RT_FAILURE(rc))
1585 return rc;
1586 rc = PDMDevHlpIOPortRegisterRC(pDevIns, 0x64, 1, 0, "kbdIOPortCommandWrite", "kbdIOPortStatusRead", NULL, NULL, "PC Keyboard - Command / Status");
1587 if (RT_FAILURE(rc))
1588 return rc;
1589 }
1590 if (fR0Enabled)
1591 {
1592 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x60, 1, 0, "kbdIOPortDataWrite", "kbdIOPortDataRead", NULL, NULL, "PC Keyboard - Data");
1593 if (RT_FAILURE(rc))
1594 return rc;
1595 rc = PDMDevHlpIOPortRegisterR0(pDevIns, 0x64, 1, 0, "kbdIOPortCommandWrite", "kbdIOPortStatusRead", NULL, NULL, "PC Keyboard - Command / Status");
1596 if (RT_FAILURE(rc))
1597 return rc;
1598 }
1599 rc = PDMDevHlpSSMRegister(pDevIns, PCKBD_SAVED_STATE_VERSION, sizeof(*pThis), kbdSaveExec, kbdLoadExec);
1600 if (RT_FAILURE(rc))
1601 return rc;
1602
1603 /*
1604 * Attach to the keyboard and mouse drivers.
1605 */
1606 rc = kbdAttach(pDevIns, 0 /* keyboard LUN # */, PDM_TACH_FLAGS_NOT_HOT_PLUG);
1607 if (RT_FAILURE(rc))
1608 return rc;
1609 rc = kbdAttach(pDevIns, 1 /* aux/mouse LUN # */, PDM_TACH_FLAGS_NOT_HOT_PLUG);
1610 if (RT_FAILURE(rc))
1611 return rc;
1612
1613 /*
1614 * Initialize the device state.
1615 */
1616 kbdReset(pDevIns);
1617
1618 return VINF_SUCCESS;
1619}
1620
1621
1622/**
1623 * The device registration structure.
1624 */
1625const PDMDEVREG g_DevicePS2KeyboardMouse =
1626{
1627 /* u32Version */
1628 PDM_DEVREG_VERSION,
1629 /* szName */
1630 "pckbd",
1631 /* szRCMod */
1632 "VBoxDDGC.gc",
1633 /* szR0Mod */
1634 "VBoxDDR0.r0",
1635 /* pszDescription */
1636 "PS/2 Keyboard and Mouse device. Emulates both the keyboard, mouse and the keyboard controller. "
1637 "LUN #0 is the keyboard connector. "
1638 "LUN #1 is the aux/mouse connector.",
1639 /* fFlags */
1640 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
1641 /* fClass */
1642 PDM_DEVREG_CLASS_INPUT,
1643 /* cMaxInstances */
1644 1,
1645 /* cbInstance */
1646 sizeof(KBDState),
1647 /* pfnConstruct */
1648 kbdConstruct,
1649 /* pfnDestruct */
1650 NULL,
1651 /* pfnRelocate */
1652 kbdRelocate,
1653 /* pfnMemSetup */
1654 NULL,
1655 /* pfnPowerOn */
1656 NULL,
1657 /* pfnReset */
1658 kbdReset,
1659 /* pfnSuspend */
1660 NULL,
1661 /* pfnResume */
1662 NULL,
1663 /* pfnAttach */
1664 kbdAttach,
1665 /* pfnDetach */
1666 kbdDetach,
1667 /* pfnQueryInterface. */
1668 NULL,
1669 /* pfnInitComplete */
1670 NULL,
1671 /* pfnPowerOff */
1672 NULL,
1673 /* pfnSoftReset */
1674 NULL,
1675 /* u32VersionEnd */
1676 PDM_DEVREG_VERSION
1677};
1678
1679#endif /* IN_RING3 */
1680#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1681
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette