#include <sys/conf.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/ddi.h>
#include <sys/stat.h>
#include <sys/modctl.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/ksynch.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/xcalwd.h>
#include <sys/policy.h>
#include <sys/platform_module.h>
extern struct mod_ops mod_driverops;
#define MINOR_DEVICE_NAME "xcalwd"
typedef struct xcalwd_state {
kmutex_t lock;
boolean_t started;
int intvl;
timeout_id_t tid;
dev_info_t *dip;
} xcalwd_state_t;
static void *xcalwd_statep;
static int xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
void *arg, void **resultp);
static int xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static int xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp);
static int xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp);
static int xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
cred_t *credp, int *rvalp);
static void xcalwd_timeout(void *arg);
static struct cb_ops xcalwd_cb_ops = {
xcalwd_open,
xcalwd_close,
nodev,
nodev,
nodev,
nodev,
nodev,
xcalwd_ioctl,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
NULL,
D_NEW | D_MP | D_64BIT,
CB_REV,
nodev,
nodev
};
static struct dev_ops xcalwd_dev_ops = {
DEVO_REV,
0,
xcalwd_getinfo,
nulldev,
nulldev,
xcalwd_attach,
xcalwd_detach,
nodev,
&xcalwd_cb_ops,
NULL,
NULL,
ddi_quiesce_not_needed,
};
static struct modldrv xcalwd_modldrv = {
&mod_driverops,
"Excalibur watchdog timer v1.7 ",
&xcalwd_dev_ops
};
static struct modlinkage xcalwd_modlinkage = {
MODREV_1,
&xcalwd_modldrv,
NULL
};
int
_init(void)
{
int error;
error = ddi_soft_state_init(&xcalwd_statep,
sizeof (xcalwd_state_t), 0);
if (error) {
return (error);
}
error = mod_install(&xcalwd_modlinkage);
if (error) {
ddi_soft_state_fini(&xcalwd_statep);
return (error);
}
return (0);
}
int
_fini(void)
{
int error;
error = mod_remove(&xcalwd_modlinkage);
if (error != 0) {
return (error);
}
ddi_soft_state_fini(&xcalwd_statep);
return (0);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&xcalwd_modlinkage, modinfop));
}
static int
xcalwd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd,
void *arg, void **resultp)
{
int retval;
dev_t dev = (dev_t)arg;
int instance;
xcalwd_state_t *tsp;
retval = DDI_FAILURE;
switch (cmd) {
case DDI_INFO_DEVT2DEVINFO:
instance = getminor(dev);
tsp = ddi_get_soft_state(xcalwd_statep, instance);
if (tsp == NULL)
*resultp = NULL;
else {
*resultp = tsp->dip;
retval = DDI_SUCCESS;
}
break;
case DDI_INFO_DEVT2INSTANCE:
*resultp = (void *)(uintptr_t)getminor(dev);
retval = DDI_SUCCESS;
break;
default:
break;
}
return (retval);
}
static int
xcalwd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int instance;
xcalwd_state_t *tsp;
switch (cmd) {
case DDI_ATTACH:
instance = ddi_get_instance(dip);
if (&plat_fan_blast == NULL) {
cmn_err(CE_WARN, "missing plat_fan_blast function");
return (DDI_FAILURE);
}
if (ddi_soft_state_zalloc(xcalwd_statep, instance) !=
DDI_SUCCESS) {
cmn_err(CE_WARN, "attach could not alloc"
"%d state structure", instance);
return (DDI_FAILURE);
}
tsp = ddi_get_soft_state(xcalwd_statep, instance);
if (tsp == NULL) {
cmn_err(CE_WARN, "get state failed %d",
instance);
return (DDI_FAILURE);
}
if (ddi_create_minor_node(dip, MINOR_DEVICE_NAME,
S_IFCHR, instance, DDI_PSEUDO, 0) == DDI_FAILURE) {
cmn_err(CE_WARN, "create minor node failed\n");
return (DDI_FAILURE);
}
mutex_init(&tsp->lock, NULL, MUTEX_DRIVER, NULL);
tsp->started = B_FALSE;
tsp->intvl = 0;
tsp->tid = 0;
tsp->dip = dip;
ddi_report_dev(dip);
return (DDI_SUCCESS);
case DDI_RESUME:
return (DDI_SUCCESS);
default:
break;
}
return (DDI_FAILURE);
}
static int
xcalwd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
xcalwd_state_t *tsp;
int instance;
switch (cmd) {
case DDI_DETACH:
instance = ddi_get_instance(dip);
tsp = ddi_get_soft_state(xcalwd_statep, instance);
ddi_remove_minor_node(dip, NULL);
mutex_destroy(&tsp->lock);
ddi_soft_state_free(xcalwd_statep, instance);
return (DDI_SUCCESS);
case DDI_SUSPEND:
return (DDI_SUCCESS);
default:
break;
}
return (DDI_FAILURE);
}
static void
xcalwd_timeout(void *arg)
{
int instance = (int)(uintptr_t)arg;
xcalwd_state_t *tsp;
if (instance < 0)
return;
tsp = ddi_get_soft_state(xcalwd_statep, instance);
if (tsp == NULL)
return;
mutex_enter(&tsp->lock);
if (tsp->started == B_FALSE || tsp->tid == 0) {
tsp->tid = 0;
mutex_exit(&tsp->lock);
return;
}
mutex_exit(&tsp->lock);
plat_fan_blast();
}
static int
xcalwd_open(dev_t *devp, int flag, int otyp, cred_t *credp)
{
int instance;
if (secpolicy_sys_config(credp, B_FALSE) != 0)
return (EPERM);
if (otyp != OTYP_CHR)
return (EINVAL);
instance = getminor(*devp);
if (instance < 0)
return (ENXIO);
if (ddi_get_soft_state(xcalwd_statep, instance) == NULL) {
return (ENXIO);
}
return (0);
}
static int
xcalwd_close(dev_t dev, int flag, int otyp, cred_t *credp)
{
xcalwd_state_t *tsp;
int instance;
timeout_id_t tid;
instance = getminor(dev);
if (instance < 0)
return (ENXIO);
tsp = ddi_get_soft_state(xcalwd_statep, instance);
if (tsp == NULL)
return (ENXIO);
mutex_enter(&tsp->lock);
if (tsp->started == B_FALSE) {
tsp->tid = 0;
mutex_exit(&tsp->lock);
return (0);
}
tsp->started = B_FALSE;
tid = tsp->tid;
tsp->tid = 0;
mutex_exit(&tsp->lock);
if (tid != 0)
(void) untimeout(tid);
plat_fan_blast();
return (0);
}
static int
xcalwd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
cred_t *cred_p, int *rvalp)
{
int instance;
xcalwd_state_t *tsp;
int intvl;
int o_intvl;
boolean_t curstate;
timeout_id_t tid;
if (secpolicy_sys_config(cred_p, B_FALSE) != 0)
return (EPERM);
instance = getminor(dev);
if (instance < 0)
return (ENXIO);
tsp = ddi_get_soft_state(xcalwd_statep, instance);
if (tsp == NULL)
return (ENXIO);
switch (cmd) {
case XCALWD_STOPWATCHDOG:
tid = 0;
mutex_enter(&tsp->lock);
if (tsp->started == B_FALSE) {
mutex_exit(&tsp->lock);
return (0);
}
tid = tsp->tid;
tsp->started = B_FALSE;
tsp->tid = 0;
mutex_exit(&tsp->lock);
if (tid != 0)
(void) untimeout(tid);
return (0);
case XCALWD_STARTWATCHDOG:
if (ddi_copyin((void *)arg, &intvl, sizeof (intvl), flag))
return (EFAULT);
if (intvl == 0)
return (EINVAL);
mutex_enter(&tsp->lock);
o_intvl = tsp->intvl;
mutex_exit(&tsp->lock);
if (ddi_copyout((const void *)&o_intvl, (void *)arg,
sizeof (o_intvl), flag))
return (EFAULT);
mutex_enter(&tsp->lock);
if (tsp->started == B_TRUE) {
mutex_exit(&tsp->lock);
return (EINVAL);
}
tsp->intvl = intvl;
tsp->tid = realtime_timeout(xcalwd_timeout,
(void *)(uintptr_t)instance,
drv_usectohz(1000000) * tsp->intvl);
tsp->started = B_TRUE;
mutex_exit(&tsp->lock);
return (0);
case XCALWD_KEEPALIVE:
tid = 0;
mutex_enter(&tsp->lock);
tid = tsp->tid;
tsp->tid = 0;
mutex_exit(&tsp->lock);
if (tid != 0)
(void) untimeout(tid);
mutex_enter(&tsp->lock);
if (tsp->started == B_TRUE)
tsp->tid = realtime_timeout(xcalwd_timeout,
(void *)(uintptr_t)instance,
drv_usectohz(1000000) * tsp->intvl);
mutex_exit(&tsp->lock);
return (0);
case XCALWD_GETSTATE:
mutex_enter(&tsp->lock);
curstate = tsp->started;
mutex_exit(&tsp->lock);
if (ddi_copyout((const void *)&curstate, (void *)arg,
sizeof (curstate), flag))
return (EFAULT);
return (0);
default:
return (EINVAL);
}
}