VirtualBox

Changeset 19967 in vbox for trunk/src/VBox/Devices


Ignore:
Timestamp:
May 24, 2009 12:05:02 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
47669
Message:

ACPI: Add support for FreeBSD

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/PC/DrvACPI.cpp

    r14710 r19967  
    4747#endif
    4848
     49#ifdef RT_OS_FREEBSD
     50# include <sys/ioctl.h>
     51# include <dev/acpica/acpiio.h>
     52# include <sys/types.h>
     53# include <sys/sysctl.h>
     54# include <stdio.h>
     55# include <errno.h>
     56# include <fcntl.h>
     57# include <unistd.h>
     58#endif
     59
    4960#include "Builtins.h"
    5061
     
    225236    CFRelease(pBlob);
    226237    CFRelease(pSources);
    227 #else /* !RT_OS_DARWIN either - what could this be? */
     238#elif defined(RT_OS_FREEBSD) /* !RT_OS_DARWIN */
     239    int fAcLine = 0;
     240    size_t cbParameter = sizeof(fAcLine);
     241
     242    int rc = sysctlbyname("hw.acpi.acline", &fAcLine, &cbParameter, NULL, NULL);
     243
     244    if (!rc)
     245    {
     246        if (fAcLine == 1)
     247            *pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
     248        else if (fAcLine == 0)
     249            *pPowerSource = PDM_ACPI_POWER_SOURCE_BATTERY;
     250        else
     251            *pPowerSource = PDM_ACPI_POWER_SOURCE_UNKNOWN;
     252    }
     253    else
     254    {
     255        AssertMsg(errno == ENOENT, ("rc=%d (%s)\n", rc, strerror(errno)));
     256        *pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
     257    }
     258#else /* !RT_OS_FREEBSD either - what could this be? */
    228259    *pPowerSource = PDM_ACPI_POWER_SOURCE_OUTLET;
    229260#endif /* !RT_OS_WINDOWS */
     
    562593    CFRelease(pBlob);
    563594    CFRelease(pSources);
    564 #endif /* RT_OS_DARWIN */
     595#elif defined(RT_OS_FREEBSD)
     596    /* We try to use /dev/acpi first and if that fails use the sysctls. */
     597    bool fSuccess = true;
     598    int FileAcpi = 0;
     599    int rc = 0;
     600
     601    FileAcpi = open("/dev/acpi", O_RDONLY);
     602    if (FileAcpi != -1)
     603    {
     604        bool fMilliWatt;
     605        union acpi_battery_ioctl_arg BatteryIo;
     606
     607        memset(&BatteryIo, 0, sizeof(BatteryIo));
     608        BatteryIo.unit = 0; /* Always use the first battery. */
     609
     610        /* Determine the power units first. */
     611        if (ioctl(FileAcpi, ACPIIO_BATT_GET_BIF, &BatteryIo) == -1)
     612            fSuccess = false;
     613        else
     614        {
     615            if (BatteryIo.bif.units == ACPI_BIF_UNITS_MW)
     616                fMilliWatt = true;
     617            else
     618                fMilliWatt = false; /* mA */
     619
     620            BatteryIo.unit = 0;
     621            if (ioctl(FileAcpi, ACPIIO_BATT_GET_BATTINFO, &BatteryIo) == -1)
     622                fSuccess = false;
     623            else
     624            {
     625                if ((BatteryIo.battinfo.state & ACPI_BATT_STAT_NOT_PRESENT) == ACPI_BATT_STAT_NOT_PRESENT)
     626                    *pfPresent = false;
     627                else
     628                {
     629                    *pfPresent = true;
     630
     631                    if (BatteryIo.battinfo.state & ACPI_BATT_STAT_DISCHARG)
     632                        *penmBatteryState = PDM_ACPI_BAT_STATE_DISCHARGING;
     633                    else if (BatteryIo.battinfo.state & ACPI_BATT_STAT_CHARGING)
     634                        *penmBatteryState = PDM_ACPI_BAT_STATE_CHARGING;
     635                    else
     636                        *penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
     637
     638                    if (BatteryIo.battinfo.state & ACPI_BATT_STAT_CRITICAL)
     639                        *penmBatteryState = (PDMACPIBATSTATE)(*penmBatteryState | PDM_ACPI_BAT_STATE_CRITICAL);
     640                }
     641
     642                if (BatteryIo.battinfo.cap != -1)
     643                    *penmRemainingCapacity = (PDMACPIBATCAPACITY)BatteryIo.battinfo.cap;
     644
     645                BatteryIo.unit = 0;
     646                if (ioctl(FileAcpi, ACPIIO_BATT_GET_BST, &BatteryIo) == 0)
     647                {
     648                    /* The rate can be either mW or mA but the ACPI device wants mW. */
     649                    if (BatteryIo.bst.rate != 0xffffffff)
     650                    {
     651                        if (fMilliWatt)
     652                            *pu32PresentRate = BatteryIo.bst.rate;
     653                        else if (BatteryIo.bst.volt != 0xffffffff)
     654                        {
     655                            /*
     656                             * The rate is in mA so we have to convert it.
     657                             * The current power rate can be calculated with P = U * I
     658                             */
     659                            *pu32PresentRate = (uint32_t)((((float)BatteryIo.bst.volt/1000.0) * ((float)BatteryIo.bst.rate/1000.0)) * 1000.0);
     660                        }
     661                    }
     662                }
     663            }
     664        }
     665
     666        close(FileAcpi);
     667    }
     668    else
     669        fSuccess = false;
     670
     671    if (!fSuccess)
     672    {
     673        int fBatteryState = 0;
     674        size_t cbParameter = sizeof(fBatteryState);
     675
     676        rc = sysctlbyname("hw.acpi.battery.state", &fBatteryState, &cbParameter, NULL, NULL);
     677        if (!rc)
     678        {
     679            if ((fBatteryState & ACPI_BATT_STAT_NOT_PRESENT) == ACPI_BATT_STAT_NOT_PRESENT)
     680                *pfPresent = false;
     681            else
     682            {
     683                *pfPresent = true;
     684
     685                if (fBatteryState & ACPI_BATT_STAT_DISCHARG)
     686                    *penmBatteryState = PDM_ACPI_BAT_STATE_DISCHARGING;
     687                else if (fBatteryState & ACPI_BATT_STAT_CHARGING)
     688                    *penmBatteryState = PDM_ACPI_BAT_STATE_CHARGING;
     689                else
     690                    *penmBatteryState = PDM_ACPI_BAT_STATE_CHARGED;
     691
     692                if (fBatteryState & ACPI_BATT_STAT_CRITICAL)
     693                    *penmBatteryState = (PDMACPIBATSTATE)(*penmBatteryState | PDM_ACPI_BAT_STATE_CRITICAL);
     694
     695                /* Get battery level. */
     696                int curCapacity = 0;
     697                cbParameter = sizeof(curCapacity);
     698                rc = sysctlbyname("hw.acpi.battery.life", &curCapacity, &cbParameter, NULL, NULL);
     699                if ((!rc) && (curCapacity >= 0))
     700                    *penmRemainingCapacity = (PDMACPIBATCAPACITY)curCapacity;
     701
     702                /* The rate can't be determined with sysctls. */
     703            }
     704        }
     705    }
     706#endif /* RT_OS_FREEBSD */
    565707    return VINF_SUCCESS;
    566708}
Note: See TracChangeset for help on using the changeset viewer.

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