#include <sys/types.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/buf.h>
#include <sys/modctl.h>
#include <sys/open.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
static int tcli_open(dev_t *, int, int, cred_t *);
static int tcli_close(dev_t, int, int, cred_t *);
static int tcli_read(dev_t, struct uio *, cred_t *);
static int tcli_write(dev_t, struct uio *, cred_t *);
static int tcli_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int tcli_attach(dev_info_t *, ddi_attach_cmd_t);
static int tcli_detach(dev_info_t *, ddi_detach_cmd_t);
static int tcli_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
struct dstate {
dev_info_t *dip;
int oflag;
};
static void *dstates;
#define INST_TO_MINOR(i) (i)
#define MINOR_TO_INST(mn) (mn)
static struct cb_ops tcli_cb_ops = {
tcli_open,
tcli_close,
nodev,
nodev,
nodev,
tcli_read,
tcli_write,
tcli_ioctl,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
NULL,
D_NEW | D_MP | D_HOTPLUG,
CB_REV,
nodev,
nodev
};
static struct dev_ops tcli_ops = {
DEVO_REV,
0,
tcli_info,
nulldev,
nulldev,
tcli_attach,
tcli_detach,
nodev,
&tcli_cb_ops,
(struct bus_ops *)0,
NULL,
ddi_quiesce_not_needed,
};
static struct modldrv modldrv = {
&mod_driverops,
"vhci client test driver",
&tcli_ops
};
static struct modlinkage modlinkage = {
MODREV_1, &modldrv, NULL
};
int
_init(void)
{
int e;
if ((e = ddi_soft_state_init(&dstates,
sizeof (struct dstate), 0)) != 0) {
return (e);
}
if ((e = mod_install(&modlinkage)) != 0) {
ddi_soft_state_fini(&dstates);
}
return (e);
}
int
_fini(void)
{
int e;
if ((e = mod_remove(&modlinkage)) != 0) {
return (e);
}
ddi_soft_state_fini(&dstates);
return (e);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
tcli_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
int instance = ddi_get_instance(devi);
struct dstate *dstatep;
int rval;
if (cmd != DDI_ATTACH)
return (DDI_SUCCESS);
if (ddi_soft_state_zalloc(dstates, instance) != DDI_SUCCESS) {
cmn_err(CE_CONT, "%s%d: can't allocate state\n",
ddi_get_name(devi), instance);
return (DDI_FAILURE);
}
dstatep = ddi_get_soft_state(dstates, instance);
dstatep->dip = devi;
rval = ddi_create_minor_node(devi, "client", S_IFCHR,
(INST_TO_MINOR(instance)), DDI_PSEUDO, 0);
if (rval == DDI_FAILURE) {
ddi_remove_minor_node(devi, NULL);
ddi_soft_state_free(dstates, instance);
cmn_err(CE_WARN, "%s%d: can't create minor nodes",
ddi_get_name(devi), instance);
return (DDI_FAILURE);
}
ddi_report_dev(devi);
return (DDI_SUCCESS);
}
static int
tcli_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
int instance;
if (cmd != DDI_DETACH)
return (DDI_SUCCESS);
ddi_remove_minor_node(devi, NULL);
instance = ddi_get_instance(devi);
ddi_soft_state_free(dstates, instance);
return (DDI_SUCCESS);
}
static int
tcli_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
dev_t dev;
int instance;
if (infocmd != DDI_INFO_DEVT2INSTANCE)
return (DDI_FAILURE);
dev = (dev_t)arg;
instance = MINOR_TO_INST(getminor(dev));
*result = (void *)(uintptr_t)instance;
return (DDI_SUCCESS);
}
static int
tcli_open(dev_t *devp, int flag, int otyp, cred_t *cred)
{
minor_t minor;
struct dstate *dstatep;
if (otyp != OTYP_BLK && otyp != OTYP_CHR)
return (EINVAL);
minor = getminor(*devp);
if ((dstatep = ddi_get_soft_state(dstates,
MINOR_TO_INST(minor))) == NULL)
return (ENXIO);
dstatep->oflag = 1;
return (0);
}
static int
tcli_close(dev_t dev, int flag, int otyp, cred_t *cred)
{
struct dstate *dstatep;
minor_t minor = getminor(dev);
if (otyp != OTYP_BLK && otyp != OTYP_CHR)
return (EINVAL);
dstatep = ddi_get_soft_state(dstates, MINOR_TO_INST(minor));
if (dstatep == NULL)
return (ENXIO);
dstatep->oflag = 0;
return (0);
}
static int
tcli_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
int *rvalp)
{
struct dstate *dstatep;
int instance;
instance = MINOR_TO_INST(getminor(dev));
dstatep = ddi_get_soft_state(dstates, instance);
if (dstatep == NULL)
return (ENXIO);
return (0);
}
static int
tcli_read(dev_t dev, struct uio *uiop, cred_t *credp)
{
return (0);
}
static int
tcli_write(dev_t dev, struct uio *uiop, cred_t *credp)
{
return (0);
}