VirtualBox

source: vbox/trunk/src/VBox/ExtPacks/BusMouseSample/BusMouse.cpp@ 61451

最後變更 在這個檔案從61451是 58170,由 vboxsync 提交於 9 年 前

doxygen: fixes.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 29.5 KB
 
1/* $Id: BusMouse.cpp 58170 2015-10-12 09:27:14Z vboxsync $ */
2/** @file
3 * BusMouse - Microsoft Bus (parallel) mouse controller device.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*********************************************************************************************************************************
33* Header Files *
34*********************************************************************************************************************************/
35#define LOG_GROUP LOG_GROUP_DEV_KBD
36#include <VBox/vmm/pdmdev.h>
37#include <VBox/version.h>
38#include <iprt/assert.h>
39#include <iprt/uuid.h>
40
41/** @page pg_busmouse DevBusMouse - Microsoft Bus Mouse Emulation
42 *
43 * The Microsoft Bus Mouse was an early mouse sold by Microsoft, originally
44 * introduced in 1983. The mouse had a D-shaped 9-pin connector which plugged
45 * into a small ISA add-in board.
46 *
47 * The mouse itself was very simple (compared to a serial mouse) and most of the
48 * logic was located on the ISA board. Later, Microsoft sold an InPort mouse,
49 * which was also called a "bus mouse", but used a different interface.
50 *
51 * Microsoft part numbers for the Bus Mouse were 037-099 (100 ppi)
52 * and 037-199 (200 ppi).
53 *
54 * The Bus Mouse adapter included IRQ configuration jumpers (ref. MS article
55 * Q12230). The IRQ could be set to one of 2, 3, 4, 5. The typical setting
56 * would be IRQ 2 for a PC/XT and IRQ 5 for an AT compatible. Because IRQ 5
57 * may conflict with a SoundBlaster or a PCI device, this device defaults to
58 * IRQ 3. Note that IRQ 3 is also used by the COM 2 device, not often needed.
59 *
60 * The ISA adapter was built around an Intel 8255A compatible chip (ref.
61 * MS article Q46369). Once enabled, the adapter raises the configured IRQ
62 * 30 times per second; the rate is not configurable. The interrupts
63 * occur regardless of whether the mouse state has changed or not.
64 *
65 * To function properly, the 8255A must be programmed as follows:
66 * - Port A: Input. Used to read motion deltas and button states.
67 * - Port B: Output. Not used except for mouse detection.
68 * - Port C: Split. Upper bits set as output, used for control purposes.
69 * Lower bits set as input, reflecting IRQ state.
70 *
71 * Detailed information was gleaned from Windows and OS/2 DDK mouse samples.
72 */
73
74
75/*********************************************************************************************************************************
76* Defined Constants And Macros *
77*********************************************************************************************************************************/
78/** The original bus mouse controller is fixed at I/O port 0x23C. */
79#define BMS_IO_BASE 0x23C
80#define BMS_IO_SIZE 4
81
82/** @name Offsets relative to the I/O base.
83 *@{ */
84#define BMS_PORT_DATA 0 /**< 8255 Port A. */
85#define BMS_PORT_SIG 1 /**< 8255 Port B. */
86#define BMS_PORT_CTRL 2 /**< 8255 Port C. */
87#define BMS_PORT_INIT 3 /**< 8255 Control Port. */
88/** @} */
89
90/** @name Port C bits (control port).
91 * @{ */
92#define BMS_CTL_INT_DIS RT_BIT(4) /**< Disable IRQ (else enabled). */
93#define BMS_CTL_SEL_HIGH RT_BIT(5) /**< Select hi nibble (else lo). */
94#define BMS_CTL_SEL_Y RT_BIT(6) /**< Select X to read (else Y). */
95#define BMS_CTL_HOLD RT_BIT(7) /**< Hold counter (else clear). */
96/** @} */
97
98/** @name Port A bits (data port).
99 * @{ */
100#define BMS_DATA_DELTA 0x0F /**< Motion delta in lower nibble. */
101#define BMS_DATA_B3_UP RT_BIT(5) /**< Button 3 (right) is up. */
102#define BMS_DATA_B2_UP RT_BIT(6) /**< Button 2 (middle) is up. */
103#define BMS_DATA_B1_UP RT_BIT(7) /**< Button 1 (left) is up. */
104/** @} */
105
106/** Convert IRQ level (2/3/4/5) to a bit in the control register. */
107#define BMS_IRQ_BIT(a) (1 << (5 - a))
108
109/** IRQ period, corresponds to approx. 30 Hz. */
110#define BMS_IRQ_PERIOD_MS 34
111
112/** Default IRQ setting. */
113#define BMS_DEFAULT_IRQ 3
114
115/** The saved state version. */
116#define BMS_SAVED_STATE_VERSION 1
117
118
119/*********************************************************************************************************************************
120* Structures and Typedefs *
121*********************************************************************************************************************************/
122/**
123 * The device state.
124 */
125typedef struct MouState
126{
127 /** @name 8255A state
128 * @{ */
129 uint8_t port_a;
130 uint8_t port_b;
131 uint8_t port_c;
132 uint8_t ctrl_port;
133 uint8_t cnt_held; /**< Counters held for reading. */
134 uint8_t held_dx;
135 uint8_t held_dy;
136 uint8_t irq; /**< The "jumpered" IRQ level. */
137 int32_t irq_toggle_counter;
138 /** Mouse timer handle - HC. */
139 PTMTIMERR3 MouseTimer;
140 /** Timer period in milliseconds. */
141 uint32_t cTimerPeriodMs;
142 /** @} */
143
144 /** @name mouse state
145 * @{ */
146 int32_t disable_counter;
147 uint8_t mouse_enabled;
148 int32_t mouse_dx; /* current values, needed for 'poll' mode */
149 int32_t mouse_dy;
150 uint8_t mouse_buttons;
151 uint8_t mouse_buttons_reported;
152 /** @} */
153
154 /** Pointer to the device instance - RC. */
155 PPDMDEVINSRC pDevInsRC;
156 /** Pointer to the device instance - R3 . */
157 PPDMDEVINSR3 pDevInsR3;
158 /** Pointer to the device instance. */
159 PPDMDEVINSR0 pDevInsR0;
160
161 /**
162 * Mouse port - LUN#0.
163 *
164 * @implements PDMIBASE
165 * @implements PDMIMOUSEPORT
166 */
167 struct
168 {
169 /** The base interface for the mouse port. */
170 PDMIBASE IBase;
171 /** The mouse port base interface. */
172 PDMIMOUSEPORT IPort;
173
174 /** The base interface of the attached mouse driver. */
175 R3PTRTYPE(PPDMIBASE) pDrvBase;
176 /** The mouse interface of the attached mouse driver. */
177 R3PTRTYPE(PPDMIMOUSECONNECTOR) pDrv;
178 } Mouse;
179} MouState;
180
181
182
183#ifndef VBOX_DEVICE_STRUCT_TESTCASE
184
185# ifdef IN_RING3
186
187/**
188 * Report a change in status down the driver chain.
189 *
190 * We want to report the mouse as enabled if and only if the guest is "using"
191 * it. That way, other devices (e.g. a PS/2 or USB mouse) can receive mouse
192 * events when the bus mouse is disabled. Enabling interrupts constitutes
193 * enabling the bus mouse. The mouse is considered disabled if interrupts are
194 * disabled for several consecutive mouse timer ticks; this is because the
195 * interrupt handler in the guest typically temporarily disables interrupts and
196 * we do not want to toggle the enabled/disabled state more often than
197 * necessary.
198 */
199static void bms_update_downstream_status(MouState *pThis)
200{
201 PPDMIMOUSECONNECTOR pDrv = pThis->Mouse.pDrv;
202 bool fEnabled = !!pThis->mouse_enabled;
203 pDrv->pfnReportModes(pDrv, fEnabled, false, false);
204}
205
206/**
207 * Set the emulated hardware to a known initial state.
208 */
209static void bms_reset(MouState *pThis)
210{
211 /* Clear the device setup. */
212 pThis->port_a = pThis->port_b = 0;
213 pThis->port_c = BMS_CTL_INT_DIS; /* Interrupts disabled. */
214 pThis->ctrl_port = 0x91; /* Default 8255A setup. */
215
216 /* Clear motion/button state. */
217 pThis->cnt_held = false;
218 pThis->mouse_dx = pThis->mouse_dy = 0;
219 pThis->mouse_buttons = 0;
220 pThis->mouse_buttons_reported = 0;
221 pThis->disable_counter = 0;
222 pThis->irq_toggle_counter = 1000;
223
224 if (pThis->mouse_enabled)
225 {
226 pThis->mouse_enabled = false;
227 bms_update_downstream_status(pThis);
228 }
229}
230
231/* Process a mouse event coming from the host. */
232static void bms_mouse_event(MouState *pThis, int dx, int dy, int dz, int dw,
233 int buttons_state)
234{
235 LogRel3(("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons_state=0x%x\n",
236 __PRETTY_FUNCTION__, dx, dy, dz, dw, buttons_state));
237
238 /* Only record X/Y movement and buttons. */
239 pThis->mouse_dx += dx;
240 pThis->mouse_dy += dy;
241 pThis->mouse_buttons = buttons_state;
242}
243
244static DECLCALLBACK(void) bmsTimerCallback(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
245{
246 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
247 uint8_t irq_bit;
248
249 /* Toggle the IRQ line if interrupts are enabled. */
250 irq_bit = BMS_IRQ_BIT(pThis->irq);
251
252 if (pThis->port_c & irq_bit)
253 {
254 if (!(pThis->port_c & BMS_CTL_INT_DIS))
255 PDMDevHlpISASetIrq(pThis->CTX_SUFF(pDevIns), pThis->irq, PDM_IRQ_LEVEL_LOW);
256 pThis->port_c &= ~irq_bit;
257 }
258 else
259 {
260 pThis->port_c |= irq_bit;
261 if (!(pThis->port_c & BMS_CTL_INT_DIS))
262 PDMDevHlpISASetIrq(pThis->CTX_SUFF(pDevIns), pThis->irq, PDM_IRQ_LEVEL_HIGH);
263 }
264
265 /* Handle enabling/disabling of the mouse interface. */
266 if (pThis->port_c & BMS_CTL_INT_DIS)
267 {
268 if (pThis->disable_counter)
269 --pThis->disable_counter;
270
271 if (pThis->disable_counter == 0 && pThis->mouse_enabled)
272 {
273 pThis->mouse_enabled = false;
274 bms_update_downstream_status(pThis);
275 }
276 }
277 else
278 {
279 pThis->disable_counter = 8; /* Re-arm the disable countdown. */
280 if (!pThis->mouse_enabled)
281 {
282 pThis->mouse_enabled = true;
283 bms_update_downstream_status(pThis);
284 }
285 }
286
287 /* Re-arm the timer. */
288 TMTimerSetMillies(pTimer, pThis->cTimerPeriodMs);
289}
290
291# endif /* IN_RING3 */
292
293static void bms_set_reported_buttons(MouState *pThis, unsigned fButtons, unsigned fButtonMask)
294{
295 pThis->mouse_buttons_reported |= (fButtons & fButtonMask);
296 pThis->mouse_buttons_reported &= (fButtons | ~fButtonMask);
297}
298
299/* Update the internal state after a write to port C. */
300static void bms_update_ctrl(MouState *pThis)
301{
302 int32_t dx, dy;
303
304 /* If the controller is in hold state, transfer data from counters. */
305 if (pThis->port_c & BMS_CTL_HOLD)
306 {
307 if (!pThis->cnt_held)
308 {
309 pThis->cnt_held = true;
310 dx = pThis->mouse_dx < 0 ? RT_MAX(pThis->mouse_dx, -128)
311 : RT_MIN(pThis->mouse_dx, 127);
312 dy = pThis->mouse_dy < 0 ? RT_MAX(pThis->mouse_dy, -128)
313 : RT_MIN(pThis->mouse_dy, 127);
314 pThis->mouse_dx -= dx;
315 pThis->mouse_dy -= dy;
316 bms_set_reported_buttons(pThis, pThis->mouse_buttons & 0x07, 0x07);
317
318 /* Force type conversion. */
319 pThis->held_dx = dx;
320 pThis->held_dy = dy;
321 }
322 }
323 else
324 pThis->cnt_held = false;
325
326 /* Move the appropriate nibble into port A. */
327 if (pThis->cnt_held)
328 {
329 if (pThis->port_c & BMS_CTL_SEL_Y)
330 {
331 if (pThis->port_c & BMS_CTL_SEL_HIGH)
332 pThis->port_a = pThis->held_dy >> 4;
333 else
334 pThis->port_a = pThis->held_dy & 0xF;
335 }
336 else
337 {
338 if (pThis->port_c & BMS_CTL_SEL_HIGH)
339 pThis->port_a = pThis->held_dx >> 4;
340 else
341 pThis->port_a = pThis->held_dx & 0xF;
342 }
343 /* And update the button bits. */
344 pThis->port_a |= pThis->mouse_buttons & 1 ? 0 : BMS_DATA_B1_UP;
345 pThis->port_a |= pThis->mouse_buttons & 2 ? 0 : BMS_DATA_B3_UP;
346 pThis->port_a |= pThis->mouse_buttons & 4 ? 0 : BMS_DATA_B2_UP;
347 }
348 /* Immediately clear the IRQ if necessary. */
349 if (pThis->port_c & BMS_CTL_INT_DIS)
350 {
351 PDMDevHlpISASetIrq(pThis->CTX_SUFF(pDevIns), pThis->irq, PDM_IRQ_LEVEL_LOW);
352 pThis->port_c &= ~(BMS_IRQ_BIT(pThis->irq));
353 }
354}
355
356static int bms_write_port(MouState *pThis, uint32_t offPort, uint32_t uValue)
357{
358 int rc = VINF_SUCCESS;
359
360 LogRel3(("%s: write port %d: 0x%02x\n", __PRETTY_FUNCTION__, offPort, uValue));
361
362 switch (offPort)
363 {
364 case BMS_PORT_SIG:
365 /* Update port B. */
366 pThis->port_b = uValue;
367 break;
368 case BMS_PORT_DATA:
369 /* Do nothing, port A is not writable. */
370 break;
371 case BMS_PORT_INIT:
372 pThis->ctrl_port = uValue;
373 break;
374 case BMS_PORT_CTRL:
375 /* Update the high nibble of port C. */
376 pThis->port_c = (uValue & 0xF0) | (pThis->port_c & 0x0F);
377 bms_update_ctrl(pThis);
378 break;
379 default:
380 AssertMsgFailed(("invalid port %#x\n", offPort));
381 break;
382 }
383 return rc;
384}
385
386static uint32_t bms_read_port(MouState *pThis, uint32_t offPort)
387{
388 uint32_t uValue;
389
390 switch (offPort)
391 {
392 case BMS_PORT_DATA:
393 /* Read port A. */
394 uValue = pThis->port_a;
395 break;
396 case BMS_PORT_SIG:
397 /* Read port B. */
398 uValue = pThis->port_b;
399 break;
400 case BMS_PORT_CTRL:
401 /* Read port C. */
402 uValue = pThis->port_c;
403 /* Some Microsoft driver code reads the control port 10,000 times when
404 * determining the IRQ level. This can occur faster than the IRQ line
405 * transitions and the detection fails. To work around this, we force
406 * the IRQ bit to toggle every once in a while.
407 */
408 if (pThis->irq_toggle_counter)
409 pThis->irq_toggle_counter--;
410 else
411 {
412 pThis->irq_toggle_counter = 1000;
413 uValue ^= BMS_IRQ_BIT(pThis->irq);
414 }
415 break;
416 case BMS_PORT_INIT:
417 /* Read the 8255A control port. */
418 uValue = pThis->ctrl_port;
419 break;
420 default:
421 AssertMsgFailed(("invalid port %#x\n", offPort));
422 break;
423 }
424 LogRel3(("%s: read port %d: 0x%02x\n", __PRETTY_FUNCTION__, offPort, uValue));
425 return uValue;
426}
427
428/**
429 * Port I/O Handler for port IN operations.
430 *
431 * @returns VBox status code.
432 *
433 * @param pDevIns The device instance.
434 * @param pvUser User argument - ignored.
435 * @param Port Port number used for the IN operation.
436 * @param pu32 Where to store the result.
437 * @param cb Number of bytes read.
438 */
439PDMBOTHCBDECL(int) mouIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
440{
441 NOREF(pvUser);
442 if (cb == 1)
443 {
444 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
445 *pu32 = bms_read_port(pThis, Port & 3);
446 Log2(("mouIOPortRead: Port=%#x cb=%d *pu32=%#x\n", Port, cb, *pu32));
447 return VINF_SUCCESS;
448 }
449 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
450 return VERR_IOM_IOPORT_UNUSED;
451}
452
453/**
454 * Port I/O Handler for port OUT operations.
455 *
456 * @returns VBox status code.
457 *
458 * @param pDevIns The device instance.
459 * @param pvUser User argument - ignored.
460 * @param Port Port number used for the IN operation.
461 * @param u32 The value to output.
462 * @param cb The value size in bytes.
463 */
464PDMBOTHCBDECL(int) mouIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
465{
466 int rc = VINF_SUCCESS;
467 NOREF(pvUser);
468 if (cb == 1)
469 {
470 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
471 rc = bms_write_port(pThis, Port & 3, u32);
472 Log2(("mouIOPortWrite: Port=%#x cb=%d u32=%#x\n", Port, cb, u32));
473 }
474 else
475 AssertMsgFailed(("Port=%#x cb=%d\n", Port, cb));
476 return rc;
477}
478
479# ifdef IN_RING3
480
481/**
482 * Saves the state of the device.
483 *
484 * @returns VBox status code.
485 * @param pDevIns The device instance.
486 * @param pSSMHandle The handle to save the state to.
487 */
488static DECLCALLBACK(int) mouSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
489{
490 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
491
492 /* 8255A state. */
493 SSMR3PutU8(pSSMHandle, pThis->port_a);
494 SSMR3PutU8(pSSMHandle, pThis->port_b);
495 SSMR3PutU8(pSSMHandle, pThis->port_c);
496 SSMR3PutU8(pSSMHandle, pThis->ctrl_port);
497 /* Other device state. */
498 SSMR3PutU8(pSSMHandle, pThis->cnt_held);
499 SSMR3PutU8(pSSMHandle, pThis->held_dx);
500 SSMR3PutU8(pSSMHandle, pThis->held_dy);
501 SSMR3PutU8(pSSMHandle, pThis->irq);
502 SSMR3PutU32(pSSMHandle, pThis->cTimerPeriodMs);
503 /* Current mouse state deltas. */
504 SSMR3PutS32(pSSMHandle, pThis->mouse_dx);
505 SSMR3PutS32(pSSMHandle, pThis->mouse_dy);
506 SSMR3PutU8(pSSMHandle, pThis->mouse_buttons_reported);
507 /* Timer. */
508 return TMR3TimerSave(pThis->MouseTimer, pSSMHandle);
509}
510
511/**
512 * Loads a saved device state.
513 *
514 * @returns VBox status code.
515 * @param pDevIns The device instance.
516 * @param pSSMHandle The handle to the saved state.
517 * @param uVersion The data unit version number.
518 * @param uPass The data pass.
519 */
520static DECLCALLBACK(int) mouLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
521{
522 int rc;
523 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
524
525 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
526
527 if (uVersion > BMS_SAVED_STATE_VERSION)
528 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
529
530 /* 8255A state. */
531 SSMR3GetU8(pSSMHandle, &pThis->port_a);
532 SSMR3GetU8(pSSMHandle, &pThis->port_b);
533 SSMR3GetU8(pSSMHandle, &pThis->port_c);
534 SSMR3GetU8(pSSMHandle, &pThis->ctrl_port);
535 /* Other device state. */
536 SSMR3GetU8(pSSMHandle, &pThis->cnt_held);
537 SSMR3GetU8(pSSMHandle, &pThis->held_dx);
538 SSMR3GetU8(pSSMHandle, &pThis->held_dy);
539 SSMR3GetU8(pSSMHandle, &pThis->irq);
540 SSMR3GetU32(pSSMHandle, &pThis->cTimerPeriodMs);
541 /* Current mouse state deltas. */
542 SSMR3GetS32(pSSMHandle, &pThis->mouse_dx);
543 SSMR3GetS32(pSSMHandle, &pThis->mouse_dy);
544 SSMR3GetU8(pSSMHandle, &pThis->mouse_buttons_reported);
545 /* Timer. */
546 rc = TMR3TimerLoad(pThis->MouseTimer, pSSMHandle);
547 return rc;
548}
549
550/**
551 * Reset notification.
552 *
553 * @returns VBox status code.
554 * @param pDevIns The device instance data.
555 */
556static DECLCALLBACK(void) mouReset(PPDMDEVINS pDevIns)
557{
558 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
559
560 /* Reinitialize the timer. */
561 pThis->cTimerPeriodMs = BMS_IRQ_PERIOD_MS / 2;
562 TMTimerSetMillies(pThis->MouseTimer, pThis->cTimerPeriodMs);
563
564 bms_reset(pThis);
565}
566
567
568/* -=-=-=-=-=- Mouse: IBase -=-=-=-=-=- */
569
570/**
571 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
572 */
573static DECLCALLBACK(void *) mouQueryMouseInterface(PPDMIBASE pInterface, const char *pszIID)
574{
575 MouState *pThis = RT_FROM_MEMBER(pInterface, MouState, Mouse.IBase);
576 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->Mouse.IBase);
577 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->Mouse.IPort);
578 return NULL;
579}
580
581
582/* -=-=-=-=-=- Mouse: IMousePort -=-=-=-=-=- */
583
584/**
585 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEvent}
586 */
587static DECLCALLBACK(int) mouPutEvent(PPDMIMOUSEPORT pInterface, int32_t dx,
588 int32_t dy, int32_t dz, int32_t dw,
589 uint32_t fButtons)
590{
591 MouState *pThis = RT_FROM_MEMBER(pInterface, MouState, Mouse.IPort);
592 int rc = PDMCritSectEnter(pThis->CTX_SUFF(pDevIns)->CTX_SUFF(pCritSectRo), VERR_SEM_BUSY);
593 AssertReleaseRC(rc);
594
595 bms_mouse_event(pThis, dx, dy, dz, dw, fButtons);
596
597 PDMCritSectLeave(pThis->CTX_SUFF(pDevIns)->CTX_SUFF(pCritSectRo));
598 return VINF_SUCCESS;
599}
600
601/**
602 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEventAbs}
603 */
604static DECLCALLBACK(int) mouPutEventAbs(PPDMIMOUSEPORT pInterface, uint32_t x,
605 uint32_t y, int32_t dz, int32_t dw,
606 uint32_t fButtons)
607{
608 AssertFailedReturn(VERR_NOT_SUPPORTED);
609}
610
611/**
612 * @interface_method_impl{PDMIMOUSEPORT, pfnPutEventMultiTouch}
613 */
614static DECLCALLBACK(int) mouPutEventMultiTouch(PPDMIMOUSEPORT pInterface, uint8_t cContacts,
615 const uint64_t *pau64Contacts, uint32_t u32ScanTime)
616{
617 AssertFailedReturn(VERR_NOT_SUPPORTED);
618}
619
620/* -=-=-=-=-=- setup code -=-=-=-=-=- */
621
622
623/**
624 * Attach command.
625 *
626 * This is called to let the device attach to a driver for a specified LUN
627 * during runtime. This is not called during VM construction, the device
628 * constructor have to attach to all the available drivers.
629 *
630 * This is like plugging in the mouse after turning on the PC.
631 *
632 * @returns VBox status code.
633 * @param pDevIns The device instance.
634 * @param iLUN The logical unit which is being detached.
635 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
636 * @remark The controller doesn't support this action, this is just
637 * implemented to try out the driver<->device structure.
638 */
639static DECLCALLBACK(int) mouAttach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
640{
641 int rc;
642 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
643
644 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
645 ("Bus mouse device does not support hotplugging\n"),
646 VERR_INVALID_PARAMETER);
647
648 switch (iLUN)
649 {
650 /* LUN #0: mouse */
651 case 0:
652 rc = PDMDevHlpDriverAttach(pDevIns, iLUN, &pThis->Mouse.IBase, &pThis->Mouse.pDrvBase, "Bus Mouse Port");
653 if (RT_SUCCESS(rc))
654 {
655 pThis->Mouse.pDrv = PDMIBASE_QUERY_INTERFACE(pThis->Mouse.pDrvBase, PDMIMOUSECONNECTOR);
656 if (!pThis->Mouse.pDrv)
657 {
658 AssertLogRelMsgFailed(("LUN #0 doesn't have a mouse interface! rc=%Rrc\n", rc));
659 rc = VERR_PDM_MISSING_INTERFACE;
660 }
661 }
662 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
663 {
664 Log(("%s/%d: warning: no driver attached to LUN #0!\n", pDevIns->pReg->szName, pDevIns->iInstance));
665 rc = VINF_SUCCESS;
666 }
667 else
668 AssertLogRelMsgFailed(("Failed to attach LUN #0! rc=%Rrc\n", rc));
669 break;
670
671 default:
672 AssertMsgFailed(("Invalid LUN #%d\n", iLUN));
673 return VERR_PDM_NO_SUCH_LUN;
674 }
675
676 return rc;
677}
678
679
680/**
681 * Detach notification.
682 *
683 * This is called when a driver is detaching itself from a LUN of the device.
684 * The device should adjust it's state to reflect this.
685 *
686 * This is like unplugging the network cable to use it for the laptop or
687 * something while the PC is still running.
688 *
689 * @param pDevIns The device instance.
690 * @param iLUN The logical unit which is being detached.
691 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
692 * @remark The controller doesn't support this action, this is just
693 * implemented to try out the driver<->device structure.
694 */
695static DECLCALLBACK(void) mouDetach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
696{
697#if 0
698 /*
699 * Reset the interfaces and update the controller state.
700 */
701 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
702 switch (iLUN)
703 {
704 /* LUN #0: mouse */
705 case 0:
706 pThis->Mouse.pDrv = NULL;
707 pThis->Mouse.pDrvBase = NULL;
708 break;
709
710 default:
711 AssertMsgFailed(("Invalid LUN #%d\n", iLUN));
712 break;
713 }
714#endif
715}
716
717
718/**
719 * @copydoc FNPDMDEVRELOCATE
720 */
721static DECLCALLBACK(void) mouRelocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
722{
723 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
724 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
725}
726
727
728/**
729 * @interface_method_impl{PDMDEVREG,pfnConstruct}
730 */
731static DECLCALLBACK(int) mouConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
732{
733 MouState *pThis = PDMINS_2_DATA(pDevIns, MouState *);
734 int rc;
735 bool fGCEnabled;
736 bool fR0Enabled;
737 uint8_t irq_lvl;
738 Assert(iInstance == 0);
739
740 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
741
742 /*
743 * Validate and read the configuration.
744 */
745 if (!CFGMR3AreValuesValid(pCfg, "IRQ\0GCEnabled\0R0Enabled\0"))
746 return VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES;
747 rc = CFGMR3QueryBoolDef(pCfg, "GCEnabled", &fGCEnabled, true);
748 if (RT_FAILURE(rc))
749 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"GCEnabled\" from the config"));
750 rc = CFGMR3QueryBoolDef(pCfg, "R0Enabled", &fR0Enabled, true);
751 if (RT_FAILURE(rc))
752 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"R0Enabled\" from the config"));
753 rc = CFGMR3QueryU8Def(pCfg, "IRQ", &irq_lvl, BMS_DEFAULT_IRQ);
754 if (RT_FAILURE(rc))
755 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to query \"IRQ\" from the config"));
756 if ((irq_lvl < 2) || (irq_lvl > 5))
757 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Invalid \"IRQ\" config setting"));
758
759 pThis->irq = irq_lvl;
760 ///@todo: remove after properly enabling RC/GC support
761 fGCEnabled = fR0Enabled = false;
762 Log(("busmouse: IRQ=%d fGCEnabled=%RTbool fR0Enabled=%RTbool\n", irq_lvl, fGCEnabled, fR0Enabled));
763
764 /*
765 * Initialize the interfaces.
766 */
767 pThis->pDevInsR3 = pDevIns;
768 pThis->pDevInsR0 = PDMDEVINS_2_R0PTR(pDevIns);
769 pThis->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
770 pThis->Mouse.IBase.pfnQueryInterface = mouQueryMouseInterface;
771 pThis->Mouse.IPort.pfnPutEvent = mouPutEvent;
772 pThis->Mouse.IPort.pfnPutEventAbs = mouPutEventAbs;
773 pThis->Mouse.IPort.pfnPutEventMultiTouch = mouPutEventMultiTouch;
774
775 /*
776 * Create the interrupt timer.
777 */
778 rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, bmsTimerCallback,
779 pThis, TMTIMER_FLAGS_DEFAULT_CRIT_SECT,
780 "Bus Mouse Timer", &pThis->MouseTimer);
781 if (RT_FAILURE(rc))
782 return rc;
783
784 /*
785 * Register I/O ports, saved state, and mouse event handlers.
786 */
787 rc = PDMDevHlpIOPortRegister(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, NULL, mouIOPortWrite, mouIOPortRead, NULL, NULL, "Bus Mouse");
788 if (RT_FAILURE(rc))
789 return rc;
790 if (fGCEnabled)
791 {
792 rc = PDMDevHlpIOPortRegisterRC(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, 0, "mouIOPortWrite", "mouIOPortRead", NULL, NULL, "Bus Mouse");
793 if (RT_FAILURE(rc))
794 return rc;
795 }
796 if (fR0Enabled)
797 {
798 rc = PDMDevHlpIOPortRegisterR0(pDevIns, BMS_IO_BASE, BMS_IO_SIZE, 0, "mouIOPortWrite", "mouIOPortRead", NULL, NULL, "Bus Mouse");
799 if (RT_FAILURE(rc))
800 return rc;
801 }
802 rc = PDMDevHlpSSMRegister(pDevIns, BMS_SAVED_STATE_VERSION, sizeof(*pThis), mouSaveExec, mouLoadExec);
803 if (RT_FAILURE(rc))
804 return rc;
805
806 /*
807 * Attach to the mouse driver.
808 */
809 rc = mouAttach(pDevIns, 0, PDM_TACH_FLAGS_NOT_HOT_PLUG);
810 if (RT_FAILURE(rc))
811 return rc;
812
813 /*
814 * Initialize the device state.
815 */
816 mouReset(pDevIns);
817
818 return VINF_SUCCESS;
819}
820
821
822/**
823 * The device registration structure.
824 */
825const PDMDEVREG g_DeviceBusMouse =
826{
827 /* u32Version */
828 PDM_DEVREG_VERSION,
829 /* szName */
830 "busmouse",
831 /* szRCMod */
832 "VBoxDDRC.rc",
833 /* szR0Mod */
834 "VBoxDDR0.r0",
835 /* pszDescription */
836 "Microsoft Bus Mouse controller. "
837 "LUN #0 is the mouse connector.",
838 /* fFlags */
839 PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36
840 | PDM_DEVREG_FLAGS_RC | PDM_DEVREG_FLAGS_R0,
841 /* fClass */
842 PDM_DEVREG_CLASS_INPUT,
843 /* cMaxInstances */
844 1,
845 /* cbInstance */
846 sizeof(MouState),
847 /* pfnConstruct */
848 mouConstruct,
849 /* pfnDestruct */
850 NULL,
851 /* pfnRelocate */
852 mouRelocate,
853 /* pfnMemSetup */
854 NULL,
855 /* pfnPowerOn */
856 NULL,
857 /* pfnReset */
858 mouReset,
859 /* pfnSuspend */
860 NULL,
861 /* pfnResume */
862 NULL,
863 /* pfnAttach */
864 mouAttach,
865 /* pfnDetach */
866 mouDetach,
867 /* pfnQueryInterface. */
868 NULL,
869 /* pfnInitComplete */
870 NULL,
871 /* pfnPowerOff */
872 NULL,
873 /* pfnSoftReset */
874 NULL,
875 /* u32VersionEnd */
876 PDM_DEVREG_VERSION
877};
878
879#ifdef VBOX_IN_EXTPACK_R3
880/**
881 * @callback_method_impl{FNPDMVBOXDEVICESREGISTER}
882 */
883extern "C" DECLEXPORT(int) VBoxDevicesRegister(PPDMDEVREGCB pCallbacks, uint32_t u32Version)
884{
885 AssertLogRelMsgReturn(u32Version >= VBOX_VERSION,
886 ("u32Version=%#x VBOX_VERSION=%#x\n", u32Version, VBOX_VERSION),
887 VERR_EXTPACK_VBOX_VERSION_MISMATCH);
888 AssertLogRelMsgReturn(pCallbacks->u32Version == PDM_DEVREG_CB_VERSION,
889 ("pCallbacks->u32Version=%#x PDM_DEVREG_CB_VERSION=%#x\n", pCallbacks->u32Version, PDM_DEVREG_CB_VERSION),
890 VERR_VERSION_MISMATCH);
891
892 return pCallbacks->pfnRegister(pCallbacks, &g_DeviceBusMouse);
893}
894#endif /* VBOX_IN_EXTPACK_R3 */
895
896# endif /* IN_RING3 */
897#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
898
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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