#include <sys/types.h>
#include <sys/conf.h>
#include <sys/open.h>
#include <sys/modctl.h>
#include <sys/promif.h>
#include <sys/stat.h>
#include <sys/ddi_impldefs.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/epm.h>
#include <sys/acpi/acpi.h>
#include <sys/acpica.h>
#include <sys/psm_types.h>
extern void pc_tod_set_rtc_offsets(ACPI_TABLE_FADT *);
extern int acpica_use_safe_delay;
static int appm_attach(dev_info_t *, ddi_attach_cmd_t);
static int appm_detach(dev_info_t *, ddi_detach_cmd_t);
static int appm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int appm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
static int appm_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
static int appm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static struct cb_ops appm_cbops = {
appm_open,
appm_close,
nodev,
nodev,
nodev,
nodev,
nodev,
appm_ioctl,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
NULL,
D_MP | D_NEW,
CB_REV,
nodev,
nodev,
};
static struct dev_ops appm_ops = {
DEVO_REV,
0,
appm_getinfo,
nulldev,
nulldev,
appm_attach,
appm_detach,
nodev,
&appm_cbops,
NULL,
NULL,
ddi_quiesce_not_needed,
};
extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
&mod_driverops,
"ACPI ppm driver",
&appm_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
typedef struct {
dev_info_t *dip;
ddi_acc_handle_t devid_hndl;
ddi_acc_handle_t estar_hndl;
int lyropen;
} appm_unit;
static void *appm_statep;
static kmutex_t appm_lock;
extern int acpi_enter_sleepstate(s3a_t *);
extern int acpi_exit_sleepstate(s3a_t *);
int
_init(void)
{
int error;
if ((error = ddi_soft_state_init(&appm_statep,
sizeof (appm_unit), 0)) != DDI_SUCCESS) {
return (error);
}
mutex_init(&appm_lock, NULL, MUTEX_DRIVER, NULL);
if ((error = mod_install(&modlinkage)) != DDI_SUCCESS) {
mutex_destroy(&appm_lock);
ddi_soft_state_fini(&appm_statep);
return (error);
}
return (error);
}
int
_fini(void)
{
int error;
if ((error = mod_remove(&modlinkage)) == DDI_SUCCESS) {
mutex_destroy(&appm_lock);
ddi_soft_state_fini(&appm_statep);
}
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
appm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
char *str = "appm_attach";
int instance;
appm_unit *unitp;
ACPI_TABLE_FADT *fadt = NULL;
int rv = DDI_SUCCESS;
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
return (DDI_SUCCESS);
default:
cmn_err(CE_WARN, "%s: cmd %d unsupported.\n", str, cmd);
return (DDI_FAILURE);
}
instance = ddi_get_instance(dip);
rv = ddi_soft_state_zalloc(appm_statep, instance);
if (rv != DDI_SUCCESS) {
cmn_err(CE_WARN, "%s: failed alloc for dev(%s@%s)",
str, ddi_binding_name(dip),
ddi_get_name_addr(dip) ? ddi_get_name_addr(dip) : " ");
return (rv);
}
if ((unitp = ddi_get_soft_state(appm_statep, instance)) == NULL) {
rv = DDI_FAILURE;
goto doerrs;
}
rv = ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
DDI_KERNEL_IOCTL, NULL, 0);
if (rv != DDI_PROP_SUCCESS)
goto doerrs;
ddi_report_dev(dip);
unitp->dip = dip;
unitp->lyropen = 0;
rv = ddi_create_minor_node(dip, "acpi-ppm", S_IFCHR, instance, 0, 0);
if (rv != DDI_SUCCESS)
goto doerrs;
if (AcpiGetTable(ACPI_SIG_FADT, 1,
(ACPI_TABLE_HEADER **)&fadt) != AE_OK)
return (rv);
if (fadt != NULL)
pc_tod_set_rtc_offsets(fadt);
return (rv);
doerrs:
if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
DDI_PROP_NOTPROM, DDI_KERNEL_IOCTL))
ddi_prop_remove_all(dip);
ddi_soft_state_free(appm_statep, instance);
return (rv);
}
static int
appm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
{
appm_unit *unitp;
int instance;
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
instance = getminor((dev_t)arg);
unitp = ddi_get_soft_state(appm_statep, instance);
if (unitp == NULL) {
return (DDI_FAILURE);
}
*result = (void *) unitp->dip;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
instance = getminor((dev_t)arg);
*result = (void *)(uintptr_t)instance;
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
appm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
char *str = "appm_detach";
switch (cmd) {
case DDI_DETACH:
return (DDI_FAILURE);
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
cmn_err(CE_WARN, "%s: cmd %d unsupported", str, cmd);
return (DDI_FAILURE);
}
}
static int
appm_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
{
appm_unit *unitp;
if (drv_priv(cred_p) != DDI_SUCCESS)
return (EPERM);
if ((unitp = ddi_get_soft_state(
appm_statep, getminor(*dev_p))) == NULL) {
cmn_err(CE_WARN, "appm_open: failed to get soft state!");
return (DDI_FAILURE);
}
mutex_enter(&appm_lock);
if (unitp->lyropen != 0) {
mutex_exit(&appm_lock);
return (EBUSY);
}
unitp->lyropen++;
mutex_exit(&appm_lock);
return (DDI_SUCCESS);
}
static int
appm_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
appm_unit *unitp;
if ((unitp =
ddi_get_soft_state(appm_statep, getminor(dev))) == NULL)
return (DDI_FAILURE);
mutex_enter(&appm_lock);
unitp->lyropen = 0;
mutex_exit(&appm_lock);
return (DDI_SUCCESS);
}
#define APPMIOC ('A' << 8)
#define APPMIOC_ENTER_S3 (APPMIOC | 1)
#define APPMIOC_EXIT_S3 (APPMIOC | 2)
static int
appm_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
cred_t *cred_p, int *rval_p)
{
static boolean_t acpi_initted = B_FALSE;
char *str = "appm_ioctl";
int ret;
s3a_t *s3ap = (s3a_t *)arg;
PMD(PMD_SX, ("%s: called with %x\n", str, cmd))
if (drv_priv(cred_p) != 0) {
PMD(PMD_SX, ("%s: EPERM\n", str))
return (EPERM);
}
if (ddi_get_soft_state(appm_statep, getminor(dev)) == NULL) {
PMD(PMD_SX, ("%s: no soft state: EIO\n", str))
return (EIO);
}
if (!acpi_initted) {
PMD(PMD_SX, ("%s: !acpi_initted\n", str))
if (acpica_init() == 0) {
acpi_initted = B_TRUE;
} else {
if (rval_p != NULL) {
*rval_p = EINVAL;
}
PMD(PMD_SX, ("%s: EINVAL\n", str))
return (EINVAL);
}
}
PMD(PMD_SX, ("%s: looking for cmd %x\n", str, cmd))
switch (cmd) {
case APPMIOC_ENTER_S3:
PMD(PMD_SX, ("%s: cmd %x, arg %p\n", str, cmd, (void *)arg))
acpica_use_safe_delay = 1;
ret = acpi_enter_sleepstate(s3ap);
break;
case APPMIOC_EXIT_S3:
PMD(PMD_SX, ("%s: cmd %x, arg %p\n", str, cmd, (void *)arg))
ret = acpi_exit_sleepstate(s3ap);
acpica_use_safe_delay = 0;
break;
default:
PMD(PMD_SX, ("%s: cmd %x unrecognized: ENOTTY\n", str, cmd))
return (ENOTTY);
}
if (ret != 0) {
if (rval_p != NULL) {
*rval_p = EINVAL;
}
return (EINVAL);
}
return (0);
}