DrvACPI.cpp revision ad77e3ec3cde24263bc7537575f5cae442bee3b1
/* $Id$ */
/** @file
* DrvACPI - ACPI Host Driver.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_ACPI
#ifdef RT_OS_WINDOWS
# include <windows.h>
#endif
#ifdef RT_OS_LINUX
# include <dirent.h>
# include <stdio.h>
#endif
#ifdef RT_OS_DARWIN
#endif
#ifdef RT_OS_FREEBSD
# include <stdio.h>
# include <errno.h>
# include <fcntl.h>
# include <unistd.h>
#endif
#include "Builtins.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* ACPI driver instance data.
*/
typedef struct DRVACPI
{
/** The ACPI interface. */
/** The ACPI port interface. */
/** Pointer to the driver instance. */
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
* @thread Any thread.
*/
{
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pThis->IACPIConnector;
default:
return NULL;
}
}
/**
* Get the current power source of the host system.
*
* @returns status code
* @param pInterface Pointer to the interface structure containing the called function pointer.
* @param pPowerSource Pointer to the power source result variable.
*/
{
#if defined(RT_OS_WINDOWS)
if (GetSystemPowerStatus(&powerStatus))
{
/* running on battery? */
if ( (powerStatus.ACLineStatus == 0)
{
}
/* running on AC link? */
{
}
else
/* what the hell we're running on? */
{
}
}
else
{
AssertMsgFailed(("Could not determine system power status, error: 0x%x\n",
GetLastError()));
}
/* start with no result */
if (dfd)
{
for (;;)
{
if (dp == 0)
break;
continue;
/* there's another possible name for this file */
if (!statusFile)
{
}
if (statusFile)
break;
}
}
if (statusFile)
{
for (;;)
{
char buff2[1024];
break;
{
else
}
}
}
const void *psValue;
bool result;
if (CFArrayGetCount(pSources) > 0)
{
for(int i = 0; i < CFArrayGetCount(pSources); ++i)
{
/* If the source is empty skip over to the next one. */
if(!pSource)
continue;
/* Skip all power sources which are currently not present like a
* second battery. */
continue;
/* Only internal power types are of interest. */
if (result &&
{
/* Check which power source we are connect on. */
if (result &&
else if (result &&
}
}
}
int fAcLine = 0;
if (!rc)
{
if (fAcLine == 1)
else if (fAcLine == 0)
else
}
else
{
}
#else /* !RT_OS_FREEBSD either - what could this be? */
#endif /* !RT_OS_WINDOWS */
return VINF_SUCCESS;
}
/**
* @copydoc PDMIACPICONNECTOR::pfnQueryBatteryStatus
*/
{
/* default return values for all architectures */
*pfPresent = false; /* no battery present */
*pu32PresentRate = ~0; /* present rate is unknown */
#if defined(RT_OS_WINDOWS)
if (GetSystemPowerStatus(&powerStatus))
{
/* 128 means no battery present */
/* just forward the value directly */
/* we assume that we are discharging the battery if we are not on-line and
* not charge the battery */
/* on Windows it is difficult to request the present charging/discharging rate */
}
else
{
AssertMsgFailed(("Could not determine system power status, error: 0x%x\n",
GetLastError()));
}
#elif defined(RT_OS_LINUX)
/* the summed up maximum capacity */
int maxCapacityTotal = ~0;
/* the summed up total capacity */
int currentCapacityTotal = ~0;
int presentRate = 0;
int presentRateTotal = 0;
if (dfd)
{
for (;;)
{
if (dp == 0)
break;
continue;
/* there is a 2nd variant of that file */
if (!statusFile)
{
}
/* we need both files */
if (!statusFile || !infoFile)
{
if (statusFile)
if (infoFile)
break;
}
/* get 'present' status from the info file */
for (;;)
{
char buff2[1024];
break;
{
fBatteryPresent = true;
}
}
/* move file pointer back to start of file */
if (fBatteryPresent)
{
/* get the maximum capacity from the info file */
for (;;)
{
char buff2[1024];
int maxCapacity = ~0;
break;
{
maxCapacity = ~0;
/* did we get a valid capacity and it's the first value we got? */
if (maxCapacityTotal < 0 && maxCapacity > 0)
{
/* take this as the maximum capacity */
}
else
{
/* sum up the maximum capacity */
if (maxCapacityTotal > 0 && maxCapacity > 0)
}
/* we got all we need */
break;
}
}
bool gotRemainingCapacity=false, gotBatteryState=false,
gotCapacityState=false, gotPresentRate=false;
while (!gotRemainingCapacity || !gotBatteryState ||
{
char buff2[1024];
int currentCapacity = ~0;
break;
{
currentCapacity = ~0;
/* is this the first valid value we see? If so, take it! */
if (currentCapacityTotal < 0 && currentCapacity >= 0)
{
}
else
{
/* just sum up the current value */
if (currentCapacityTotal > 0 && currentCapacity > 0)
}
gotRemainingCapacity = true;
}
{
fDischarging = true;
fCharging = true;
gotBatteryState = true;
}
{
fCritical = true;
gotCapacityState = true;
}
{
presentRate = 0;
gotPresentRate = true;
}
}
}
if (presentRate)
{
if (fDischarging)
else
}
if (statusFile)
if (infoFile)
}
}
/* charging/discharging bits are mutual exclusive */
if (fDischarging)
else if (fCharging)
if (fCritical)
if (presentRateTotal < 0)
if (maxCapacityTotal > 0 && currentCapacityTotal > 0)
{
/* calculate the percentage */
*penmRemainingCapacity = (PDMACPIBATCAPACITY)(((float)currentCapacityTotal / (float)maxCapacityTotal)
}
#elif defined(RT_OS_DARWIN)
const void *psValue;
bool result;
if (CFArrayGetCount(pSources) > 0)
{
for(int i = 0; i < CFArrayGetCount(pSources); ++i)
{
/* If the source is empty skip over to the next one. */
if(!pSource)
continue;
/* Skip all power sources which are currently not present like a
* second battery. */
continue;
/* Only internal power types are of interest. */
if (result &&
{
/* First check which power source we are connect on. */
if (result &&
else if (result &&
/* At this point the power source is present. */
*pfPresent = true;
int curCapacity = 0;
int maxCapacity = 1;
float remCapacity = 0.0f;
/* Fetch the current capacity value of the power source */
if (result)
/* Fetch the maximum capacity value of the power source */
if (result)
/* Calculate the remaining capacity in percent */
{
/* If we are on battery power we are discharging in every
* case */
int timeToEmpty = -1;
/* Get the time till the battery source will be empty */
if (result)
if (timeToEmpty != -1)
/* 0...1000 */
}
if (powerSource == PDM_ACPI_POWER_SOURCE_OUTLET &&
{
/* We are running on an AC power source, but we also have a
* battery power source present. */
{
/* This means charging. */
int timeToFull = -1;
/* Get the time till the battery source will be charged */
if (result)
if (timeToFull != -1)
/* 0...1000 */
}
}
/* Check for critical */
int criticalValue = 20;
if (result)
if (remCapacity < criticalValue)
}
}
}
#elif defined(RT_OS_FREEBSD)
bool fSuccess = true;
int FileAcpi = 0;
int rc = 0;
if (FileAcpi != -1)
{
bool fMilliWatt;
union acpi_battery_ioctl_arg BatteryIo;
/* Determine the power units first. */
fSuccess = false;
else
{
fMilliWatt = true;
else
fMilliWatt = false; /* mA */
fSuccess = false;
else
{
*pfPresent = false;
else
{
*pfPresent = true;
else
}
{
/* The rate can be either mW or mA but the ACPI device wants mW. */
{
if (fMilliWatt)
{
/*
* The rate is in mA so we have to convert it.
* The current power rate can be calculated with P = U * I
*/
*pu32PresentRate = (uint32_t)((((float)BatteryIo.bst.volt/1000.0) * ((float)BatteryIo.bst.rate/1000.0)) * 1000.0);
}
}
}
}
}
}
else
fSuccess = false;
if (!fSuccess)
{
int fBatteryState = 0;
if (!rc)
{
*pfPresent = false;
else
{
*pfPresent = true;
else if (fBatteryState & ACPI_BATT_STAT_CHARGING)
else
/* Get battery level. */
int curCapacity = 0;
cbParameter = sizeof(curCapacity);
if ((!rc) && (curCapacity >= 0))
/* The rate can't be determined with sysctls. */
}
}
}
#endif /* RT_OS_FREEBSD */
return VINF_SUCCESS;
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
LogFlow(("drvACPIDestruct\n"));
}
/**
* Construct an ACPI driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
static DECLCALLBACK(int) drvACPIConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
{
/*
* Init the static parts.
*/
/* IBase */
/* IACPIConnector */
/*
* Validate the config.
*/
/*
* Check that no-one is attached to us.
*/
("Configuration error: Not possible to attach anything to this driver!\n"),
/*
* Query the ACPI port interface.
*/
{
AssertMsgFailed(("Configuration error: the above device/driver didn't export the ACPI port interface!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
return VINF_SUCCESS;
}
/**
* ACPI driver registration record.
*/
{
/* u32Version */
/* szDriverName */
"ACPIHost",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"ACPI Host Driver",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVACPI),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};