1 | /* $Id: DrvGpioButton.cpp 101618 2023-10-27 12:47:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvGpioButton - Virtual GPIO driver for power/sleep button presses.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_DRV_GPIO
|
---|
33 |
|
---|
34 | #include <VBox/vmm/pdmdrv.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/uuid.h>
|
---|
39 |
|
---|
40 | #include "VBoxDD.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Defined Constants And Macros *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 |
|
---|
47 | /** The power button is currently pressed. */
|
---|
48 | #define DRV_GPIO_BUTTON_PRESSED_POWER RT_BIT_32(0)
|
---|
49 | /** The sleep button is currently pressed. */
|
---|
50 | #define DRV_GPIO_BUTTON_PRESSED_SLEEP RT_BIT_32(1)
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Structures and Typedefs *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /**
|
---|
57 | * GPIO button driver instance data.
|
---|
58 | *
|
---|
59 | * @implements PDMIEVENTBUTTONPORT
|
---|
60 | * @implements PDMIGPIOCONNECTOR
|
---|
61 | */
|
---|
62 | typedef struct DRVGPIOBUTTON
|
---|
63 | {
|
---|
64 | /** The button event interface for use by Main. */
|
---|
65 | PDMIEVENTBUTTONPORT IEventButtonPort;
|
---|
66 | /** The GPIO interface interface. */
|
---|
67 | PDMIGPIOCONNECTOR IGpioConnector;
|
---|
68 | /** The GPIO port interface above. */
|
---|
69 | PPDMIGPIOPORT pGpioPort;
|
---|
70 | /** Pointer to the driver instance. */
|
---|
71 | PPDMDRVINS pDrvIns;
|
---|
72 |
|
---|
73 | /** Currently pressed button. */
|
---|
74 | volatile uint32_t fButtonsPressed;
|
---|
75 |
|
---|
76 | /** The power button GPIO line to trigger. */
|
---|
77 | uint32_t uPowerButtonGpio;
|
---|
78 | /** The sleep button GPIO line to trigger. */
|
---|
79 | uint32_t uSleepButtonGpio;
|
---|
80 |
|
---|
81 | TMTIMERHANDLE hTimerDepress;
|
---|
82 |
|
---|
83 | } DRVGPIOBUTTON;
|
---|
84 | /** Pointer to a GPIO button driver instance. */
|
---|
85 | typedef DRVGPIOBUTTON *PDRVGPIOBUTTON;
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
90 | */
|
---|
91 | static DECLCALLBACK(void *) drvGpioButton_QueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
92 | {
|
---|
93 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
94 | PDRVGPIOBUTTON pThis = PDMINS_2_DATA(pDrvIns, PDRVGPIOBUTTON);
|
---|
95 |
|
---|
96 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
97 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIGPIOCONNECTOR, &pThis->IGpioConnector);
|
---|
98 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIEVENTBUTTONPORT, &pThis->IEventButtonPort);
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * @interface_method_impl{PDMIEVENTBUTTONPORT,pfnQueryGuestCanHandleButtonEvents}
|
---|
105 | */
|
---|
106 | static DECLCALLBACK(int) drvGpioButton_QueryGuestCanHandleButtonEvents(PPDMIEVENTBUTTONPORT pInterface, bool *pfCanHandleButtonEvents)
|
---|
107 | {
|
---|
108 | PDRVGPIOBUTTON pThis = RT_FROM_MEMBER(pInterface, DRVGPIOBUTTON, IEventButtonPort);
|
---|
109 |
|
---|
110 | /** @todo Better interface for this. */
|
---|
111 | *pfCanHandleButtonEvents = pThis->pGpioPort->pfnGpioLineIsInput(pThis->pGpioPort, pThis->uPowerButtonGpio)
|
---|
112 | || pThis->pGpioPort->pfnGpioLineIsInput(pThis->pGpioPort, pThis->uSleepButtonGpio);
|
---|
113 | return VINF_SUCCESS;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * @interface_method_impl{PDMIEVENTBUTTONPORT,pfnPowerButtonPress}
|
---|
119 | */
|
---|
120 | static DECLCALLBACK(int) drvGpioButton_PowerButtonPress(PPDMIEVENTBUTTONPORT pInterface)
|
---|
121 | {
|
---|
122 | PDRVGPIOBUTTON pThis = RT_FROM_MEMBER(pInterface, DRVGPIOBUTTON, IEventButtonPort);
|
---|
123 |
|
---|
124 | ASMAtomicOrU32(&pThis->fButtonsPressed, DRV_GPIO_BUTTON_PRESSED_POWER);
|
---|
125 | int rc = pThis->pGpioPort->pfnGpioLineChange(pThis->pGpioPort, pThis->uPowerButtonGpio, true /*fVal*/);
|
---|
126 | if (RT_SUCCESS(rc))
|
---|
127 | rc = PDMDrvHlpTimerSetMillies(pThis->pDrvIns, pThis->hTimerDepress, 250);
|
---|
128 | return rc;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * @interface_method_impl{PDMIEVENTBUTTONPORT,pfnSleepButtonPress}
|
---|
134 | */
|
---|
135 | static DECLCALLBACK(int) drvGpioButton_SleepButtonPress(PPDMIEVENTBUTTONPORT pInterface)
|
---|
136 | {
|
---|
137 | PDRVGPIOBUTTON pThis = RT_FROM_MEMBER(pInterface, DRVGPIOBUTTON, IEventButtonPort);
|
---|
138 |
|
---|
139 | /** @todo Reset to 0. */
|
---|
140 | return pThis->pGpioPort->pfnGpioLineChange(pThis->pGpioPort, pThis->uSleepButtonGpio, true /*fVal*/);
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @interface_method_impl{PDMIEVENTBUTTONPORT,pfnQueryPowerButtonHandled}
|
---|
146 | */
|
---|
147 | static DECLCALLBACK(int) drvGpioButton_QueryPowerButtonHandled(PPDMIEVENTBUTTONPORT pInterface, bool *pfHandled)
|
---|
148 | {
|
---|
149 | RT_NOREF(pInterface);
|
---|
150 |
|
---|
151 | /** @todo */
|
---|
152 | *pfHandled = true;
|
---|
153 | return VINF_SUCCESS;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Timer callback that depresses a button.
|
---|
159 | */
|
---|
160 | static DECLCALLBACK(void) drvGpioButtonTimerDepress(PPDMDRVINS pDrvIns, TMTIMERHANDLE hTimer, void *pvUser)
|
---|
161 | {
|
---|
162 | PDRVGPIOBUTTON pThis = PDMINS_2_DATA(pDrvIns, PDRVGPIOBUTTON);
|
---|
163 | Assert(hTimer == pThis->hTimerDepress); RT_NOREF(hTimer, pvUser);
|
---|
164 |
|
---|
165 | uint32_t fButtonsPressed = ASMAtomicXchgU32(&pThis->fButtonsPressed, 0);
|
---|
166 | if (fButtonsPressed & DRV_GPIO_BUTTON_PRESSED_POWER)
|
---|
167 | pThis->pGpioPort->pfnGpioLineChange(pThis->pGpioPort, pThis->uPowerButtonGpio, false /*fVal*/);
|
---|
168 | if (fButtonsPressed & DRV_GPIO_BUTTON_PRESSED_SLEEP)
|
---|
169 | pThis->pGpioPort->pfnGpioLineChange(pThis->pGpioPort, pThis->uSleepButtonGpio, false /*fVal*/);
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Construct an GPIO button driver instance.
|
---|
175 | *
|
---|
176 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
177 | */
|
---|
178 | static DECLCALLBACK(int) drvGpioButtonConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
179 | {
|
---|
180 | RT_NOREF(pCfg, fFlags);
|
---|
181 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
182 | PDRVGPIOBUTTON pThis = PDMINS_2_DATA(pDrvIns, PDRVGPIOBUTTON);
|
---|
183 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
184 | int rc = VINF_SUCCESS;
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Init the static parts.
|
---|
188 | */
|
---|
189 | /* IBase */
|
---|
190 | pDrvIns->IBase.pfnQueryInterface = drvGpioButton_QueryInterface;
|
---|
191 | /* IEventButtonPort */
|
---|
192 | pThis->IEventButtonPort.pfnQueryGuestCanHandleButtonEvents = drvGpioButton_QueryGuestCanHandleButtonEvents;
|
---|
193 | pThis->IEventButtonPort.pfnPowerButtonPress = drvGpioButton_PowerButtonPress;
|
---|
194 | pThis->IEventButtonPort.pfnSleepButtonPress = drvGpioButton_SleepButtonPress;
|
---|
195 | pThis->IEventButtonPort.pfnQueryPowerButtonHandled = drvGpioButton_QueryPowerButtonHandled;
|
---|
196 |
|
---|
197 | pThis->pDrvIns = pDrvIns;
|
---|
198 | pThis->fButtonsPressed = 0;
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Validate and read the configuration.
|
---|
202 | */
|
---|
203 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "PowerButtonGpio"
|
---|
204 | "|SleepButtonGpio",
|
---|
205 | "");
|
---|
206 |
|
---|
207 | rc = pHlp->pfnCFGMQueryU32(pCfg, "PowerButtonGpio", &pThis->uPowerButtonGpio);
|
---|
208 | if (RT_FAILURE(rc))
|
---|
209 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
210 | N_("Configuration error: Failed to get the \"PowerButtonGpio\" value"));
|
---|
211 |
|
---|
212 | rc = pHlp->pfnCFGMQueryU32(pCfg, "SleepButtonGpio", &pThis->uSleepButtonGpio);
|
---|
213 | if (RT_FAILURE(rc))
|
---|
214 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
215 | N_("Configuration error: Failed to get the \"SleepButtonGpio\" value"));
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Check that no-one is attached to us.
|
---|
219 | */
|
---|
220 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
221 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
222 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
223 |
|
---|
224 | /*
|
---|
225 | * Query the GPIO port interface.
|
---|
226 | */
|
---|
227 | pThis->pGpioPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIGPIOPORT);
|
---|
228 | if (!pThis->pGpioPort)
|
---|
229 | {
|
---|
230 | AssertMsgFailed(("Configuration error: the above device/driver didn't export the GPIO port interface!\n"));
|
---|
231 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
232 | }
|
---|
233 |
|
---|
234 | rc = PDMDrvHlpTMTimerCreate(pDrvIns, TMCLOCK_VIRTUAL, drvGpioButtonTimerDepress, NULL,
|
---|
235 | TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
|
---|
236 | "Button depress timer", &pThis->hTimerDepress);
|
---|
237 |
|
---|
238 | return VINF_SUCCESS;
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * GPIO button driver registration record.
|
---|
244 | */
|
---|
245 | const PDMDRVREG g_DrvGpioButton =
|
---|
246 | {
|
---|
247 | /* .u32Version = */ PDM_DRVREG_VERSION,
|
---|
248 | /* .szName = */ "GpioButton",
|
---|
249 | /* .szRCMod = */ "",
|
---|
250 | /* .szR0Mod = */ "",
|
---|
251 | /* .pszDescription = */ "GPIO Button Driver",
|
---|
252 | /* .fFlags = */ PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
253 | /* .fClass = */ PDM_DRVREG_CLASS_GPIO,
|
---|
254 | /* .cMaxInstances = */ UINT32_MAX,
|
---|
255 | /* .cbInstance = */ sizeof(DRVGPIOBUTTON),
|
---|
256 | /* .pfnConstruct = */ drvGpioButtonConstruct,
|
---|
257 | /* .pfnDestruct = */ NULL,
|
---|
258 | /* .pfnRelocate = */ NULL,
|
---|
259 | /* .pfnIOCtl = */ NULL,
|
---|
260 | /* .pfnPowerOn = */ NULL,
|
---|
261 | /* .pfnReset = */ NULL,
|
---|
262 | /* .pfnSuspend = */ NULL,
|
---|
263 | /* .pfnResume = */ NULL,
|
---|
264 | /* .pfnAttach = */ NULL,
|
---|
265 | /* .pfnDetach = */ NULL,
|
---|
266 | /* .pfnPowerOff = */ NULL,
|
---|
267 | /* .pfnSoftReset = */ NULL,
|
---|
268 | /* .u32EndVersion = */ PDM_DRVREG_VERSION
|
---|
269 | };
|
---|