VirtualBox

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

Last change on this file since 54030 was 48419, checked in by vboxsync, 11 years ago

Filled in a few blanks.

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