DevSMC.cpp revision f5e53763b0a581b0299e98028c6c52192eb06785
/* $Id$ */
/** @file
* DevSMC - SMC device emulation.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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.
* --------------------------------------------------------------------
*
* This code is based on:
*
* Apple SMC controller
*
* Copyright (c) 2007 Alexander Graf
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* *****************************************************************
*
* In all Intel-based Apple hardware there is an SMC chip to control the
* backlight, fans and several other generic device parameters. It also
* contains the magic keys used to dongle Mac OS X to the device.
*
* This driver was mostly created by looking at the Linux AppleSMC driver
* implementation and does not support IRQ.
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_SMC
#ifdef IN_RING0
# include <iprt/asm-amd64-x86.h>
#endif
#include "VBoxDD2.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* data port used by Apple SMC */
#define APPLESMC_DATA_PORT 0x300
#define APPLESMC_CMD_PORT 0x304
#define APPLESMC_MAX_DATA_LENGTH 32
#define APPLESMC_READ_CMD 0x10
#define APPLESMC_WRITE_CMD 0x11
#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
#define APPLESMC_GET_KEY_TYPE_CMD 0x13
/** The version of the saved state. */
#define SMC_SAVED_STATE_VERSION 1
/** The ring-0 operation number that attempts to get OSK0 and OSK1 from the real
* SMC. */
#define SMC_CALLR0_READ_OSK 1
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct AppleSMCData
{
const char *key;
const char *data;
} AppleSMCData;
typedef struct
{
/** The OSK0 value. This is currently only used in the constructor. */
/** The OSK1 value. This is currently only used in the constructor */
} SMCState;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef IN_RING3
static char osk[64];
static struct AppleSMCData data[] =
{
{6, "REV ", "\0x01\0x13\0x0f\0x00\0x00\0x03"},
{1, "NATJ", "\0" },
{1, "MSSP", "\0" },
{1, "MSSD", "\0x3" },
{1, "NTOK", "\0"},
};
#endif /* IN_RING3 */
#ifdef IN_RING0
/** Do once for the SMC ring-0 static data (g_abOsk0, g_abOsk1, g_fHaveOsk). */
/** Indicates whether we've successfully queried the OSK* keys. */
static bool g_fHaveOsk = false;
/** The OSK0 value. */
/** The OSK1 value. */
#endif /* IN_RING0 */
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef IN_RING0
/**
* Waits for the specified status on the host SMC.
*
* @returns success indicator.
* @param bStatus The desired status.
* @param pszWhat What we're currently doing. For the log.
*/
{
{
return true;
}
return false;
}
/**
* Reads a key by name from the host SMC.
*
* @returns success indicator.
* @param pszName The key name, must be exactly 4 chars long.
* @param pbBuf The output buffer.
* @param cbBuf The buffer size. Max 32 bytes.
*/
{
/*
* Issue the READ command.
*/
for (;;)
{
break;
cMsSleep <<= 1;
if (cMsSleep > 64)
{
return false;
}
}
/*
* Send it the key.
*/
{
return false;
}
/*
* The desired amount of output.
*/
/*
* Read the output.
*/
{
return false;
}
return true;
}
/**
* RTOnce callback that initializes g_fHaveOsk, g_abOsk0 and g_abOsk1.
*
* @returns VINF_SUCCESS.
* @param pvUser1Ignored Ignored.
* @param pvUser2Ignored Ignored.
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{FNPDMDEVREQHANDLERR0}
*/
{
int rc = VERR_INVALID_FUNCTION;
if (uOperation == SMC_CALLR0_READ_OSK)
{
if ( RT_SUCCESS(rc)
&& g_fHaveOsk)
{
}
}
return rc;
}
#endif /* IN_RING0 */
#ifdef IN_RING3
/**
* Saves a state of the SMC device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
{
/** @todo: implement serialization */
return VINF_SUCCESS;
}
/**
* Loads a SMC device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) smcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
{
if (uVersion != SMC_SAVED_STATE_VERSION)
/** @todo: implement serialization */
return VINF_SUCCESS;
}
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
{
LogFlow(("smcReset: \n"));
}
/**
* Info handler, device version.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
{
}
static void applesmc_fill_data(SMCState *s)
{
struct AppleSMCData *d;
{
if (key_data == key_current)
{
return;
}
}
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param uPort Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) smcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
/** @todo: status code? */
if (cb != 1)
return VERR_IOM_IOPORT_UNUSED;
switch (Port)
{
case APPLESMC_CMD_PORT:
{
break;
}
case APPLESMC_DATA_PORT:
{
switch (s->cmd) {
case APPLESMC_READ_CMD:
{
s->data_pos++;
{
s->status = 0x00;
Log(("APPLESMC: EOF\n"));
}
else
s->status = 0x05;
}
}
break;
}
}
return VINF_SUCCESS;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param uPort Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) smcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
/** @todo: status code? */
if (cb != 1)
return VINF_SUCCESS;
switch (Port)
{
case APPLESMC_CMD_PORT:
{
switch (u32)
{
case APPLESMC_READ_CMD:
s->status = 0x0c;
break;
}
s->read_pos = 0;
s->data_pos = 0;
break;
}
case APPLESMC_DATA_PORT:
{
switch(s->cmd)
{
case APPLESMC_READ_CMD:
if (s->read_pos < 4)
{
s->status = 0x04;
}
else
if (s->read_pos == 4)
{
s->status = 0x05;
s->data_pos = 0;
}
s->read_pos++;
break;
}
}
}
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
{
/*
* Store state.
*/
/*
* Validate and read the configuration.
*/
/*
* Read the DeviceKey config value.
*/
char *pszDeviceKey;
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"DeviceKey\" as a string failed"));
if (cchDeviceKey > 0)
/*
* Query the key from the real hardware if asked to do so.
*/
bool fGetKeyFromRealSMC;
if (RT_FAILURE(rc))
N_("Configuration error: Querying \"GetKeyFromRealSMC\" as a boolean failed"));
if (fGetKeyFromRealSMC)
{
if (RT_FAILURE(rc))
N_("Failed to query SMC value from the host"));
}
/*
* For practical/historical reasons, the OSK[0|1] data is stored in a
* global buffer in ring-3.
*/
AssertCompile(sizeof(char) == sizeof(uint8_t));
/*
* Register the IO ports.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
/* Register saved state management */
rc = PDMDevHlpSSMRegister(pDevIns, SMC_SAVED_STATE_VERSION, sizeof(*pThis), smcSaveExec, smcLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the device state.
*/
/**
* @todo: Register statistics.
*/
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceSMC =
{
/* u32Version */
/* szName */
"smc",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"System Management Controller (SMC) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36| PDM_DEVREG_FLAGS_R0,
/* fClass */
/* cMaxInstances */
1,
/* cbInstance */
sizeof(SMCState),
/* pfnConstruct */
/* pfnDestruct */
NULL,
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
};
#endif /* IN_RING3 */
#endif /* VBOX_DEVICE_STRUCT_TESTCASE */