#include <sys/stat.h>
#include <sys/modctl.h>
#include <sys/open.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/sunddi.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/file.h>
#include <sys/note.h>
#include <sys/i2c/clients/ltc1427_impl.h>
static void *ltc1427soft_statep;
static int ltc1427_do_attach(dev_info_t *);
static int ltc1427_do_detach(dev_info_t *);
static int ltc1427_do_resume(void);
static int ltc1427_do_suspend(void);
static int ltc1427_open(dev_t *, int, int, cred_t *);
static int ltc1427_close(dev_t, int, int, cred_t *);
static int ltc1427_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static struct cb_ops ltc1427_cbops = {
ltc1427_open,
ltc1427_close,
nodev,
nodev,
nodev,
nodev,
nodev,
ltc1427_ioctl,
nodev,
nodev,
nodev,
nochpoll,
ddi_prop_op,
NULL,
D_NEW | D_MP | D_HOTPLUG,
CB_REV,
nodev,
nodev
};
static int ltc1427_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static int ltc1427_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static struct dev_ops ltc1427_ops = {
DEVO_REV,
0,
ddi_getinfo_1to1,
nulldev,
nulldev,
ltc1427_attach,
ltc1427_detach,
nodev,
<c1427_cbops,
NULL,
NULL,
ddi_quiesce_not_needed,
};
extern struct mod_ops mod_driverops;
static struct modldrv ltc1427_modldrv = {
&mod_driverops,
"LTC1427 i2c device driver",
<c1427_ops
};
static struct modlinkage ltc1427_modlinkage = {
MODREV_1,
<c1427_modldrv,
0
};
int
_init(void)
{
int error;
error = mod_install(<c1427_modlinkage);
if (!error)
(void) ddi_soft_state_init(<c1427soft_statep,
sizeof (struct ltc1427_unit), 1);
return (error);
}
int
_fini(void)
{
int error;
error = mod_remove(<c1427_modlinkage);
if (!error)
ddi_soft_state_fini(<c1427soft_statep);
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(<c1427_modlinkage, modinfop));
}
static int
ltc1427_open(dev_t *devp, int flags, int otyp, cred_t *credp)
{
_NOTE(ARGUNUSED(credp))
struct ltc1427_unit *unitp;
int instance;
int error = 0;
instance = getminor(*devp);
if (instance < 0) {
return (ENXIO);
}
unitp = (struct ltc1427_unit *)
ddi_get_soft_state(ltc1427soft_statep, instance);
if (unitp == NULL) {
return (ENXIO);
}
if (otyp != OTYP_CHR) {
return (EINVAL);
}
mutex_enter(&unitp->ltc1427_mutex);
if (flags & FEXCL) {
if (unitp->ltc1427_oflag != 0) {
error = EBUSY;
} else {
unitp->ltc1427_oflag = FEXCL;
}
} else {
if (unitp->ltc1427_oflag == FEXCL) {
error = EBUSY;
} else {
unitp->ltc1427_oflag = FOPEN;
}
}
mutex_exit(&unitp->ltc1427_mutex);
return (error);
}
static int
ltc1427_close(dev_t dev, int flags, int otyp, cred_t *credp)
{
_NOTE(ARGUNUSED(flags, otyp, credp))
struct ltc1427_unit *unitp;
int instance;
instance = getminor(dev);
if (instance < 0) {
return (ENXIO);
}
unitp = (struct ltc1427_unit *)
ddi_get_soft_state(ltc1427soft_statep, instance);
if (unitp == NULL) {
return (ENXIO);
}
mutex_enter(&unitp->ltc1427_mutex);
unitp->ltc1427_oflag = 0;
mutex_exit(&unitp->ltc1427_mutex);
return (DDI_SUCCESS);
}
static int
ltc1427_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
cred_t *credp, int *rvalp)
{
_NOTE(ARGUNUSED(credp, rvalp))
struct ltc1427_unit *unitp;
int instance;
int err = 0;
i2c_transfer_t *i2c_tran_pointer;
int32_t fan_speed;
if (arg == (intptr_t)NULL) {
D2CMN_ERR((CE_WARN, "LTC1427: ioctl: arg passed in to ioctl "
"= NULL\n"));
err = EINVAL;
return (err);
}
instance = getminor(dev);
unitp = (struct ltc1427_unit *)
ddi_get_soft_state(ltc1427soft_statep, instance);
mutex_enter(&unitp->ltc1427_mutex);
switch (cmd) {
case I2C_GET_OUTPUT:
D1CMN_ERR((CE_NOTE, "current_set_flag = %d\n",
unitp->current_set_flag));
if (unitp->current_set_flag == 0) {
err = EIO;
break;
} else {
if (ddi_copyout((caddr_t)&unitp->current_value,
(caddr_t)arg, sizeof (int32_t),
mode) != DDI_SUCCESS) {
D2CMN_ERR((CE_WARN,
"%s: Failed in I2C_GET_OUTPUT "
"ddi_copyout routine\n",
unitp->ltc1427_name));
err = EFAULT;
break;
}
}
break;
case I2C_SET_OUTPUT:
if (ddi_copyin((caddr_t)arg, (caddr_t)&fan_speed,
sizeof (int32_t), mode) != DDI_SUCCESS) {
D2CMN_ERR((CE_WARN,
"%s: Failed in I2C_SET_OUTPUT "
"ioctl before switch\n",
unitp->ltc1427_name));
err = EFAULT;
break;
}
(void) i2c_transfer_alloc(unitp->ltc1427_hdl,
&i2c_tran_pointer, 2, 0, I2C_SLEEP);
if (i2c_tran_pointer == NULL) {
D2CMN_ERR((CE_WARN,
"%s: Failed in I2C_SET_OUTPUT "
"i2c_transfer_pointer not allocated\n",
unitp->ltc1427_name));
err = ENOMEM;
break;
}
i2c_tran_pointer->i2c_flags = I2C_WR;
i2c_tran_pointer->i2c_wbuf[0] =
(uchar_t)((fan_speed >> 8) & 0x03);
i2c_tran_pointer->i2c_wbuf[1] =
(uchar_t)((fan_speed) & 0x000000ff);
err = i2c_transfer(unitp->ltc1427_hdl, i2c_tran_pointer);
if (!err) {
unitp->current_value = fan_speed;
unitp->current_set_flag = 1;
}
i2c_transfer_free(unitp->ltc1427_hdl, i2c_tran_pointer);
break;
default:
D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
unitp->ltc1427_name, cmd));
err = EINVAL;
}
mutex_exit(&unitp->ltc1427_mutex);
return (err);
}
static int
ltc1427_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
switch (cmd) {
case DDI_ATTACH:
return (ltc1427_do_attach(dip));
case DDI_RESUME:
return (ltc1427_do_resume());
default:
return (DDI_FAILURE);
}
}
static int
ltc1427_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
switch (cmd) {
case DDI_DETACH:
return (ltc1427_do_detach(dip));
case DDI_SUSPEND:
return (ltc1427_do_suspend());
default:
return (DDI_FAILURE);
}
}
static int
ltc1427_do_attach(dev_info_t *dip)
{
struct ltc1427_unit *unitp;
int instance;
instance = ddi_get_instance(dip);
if (ddi_soft_state_zalloc(ltc1427soft_statep, instance) != 0) {
cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
ddi_get_name(dip), instance);
return (DDI_FAILURE);
}
unitp = ddi_get_soft_state(ltc1427soft_statep, instance);
if (unitp == NULL) {
cmn_err(CE_WARN, "%s%d: unitp not filled\n",
ddi_get_name(dip), instance);
return (ENOMEM);
}
(void) snprintf(unitp->ltc1427_name, sizeof (unitp->ltc1427_name),
"%s%d", ddi_node_name(dip), instance);
if (ddi_create_minor_node(dip, "ltc1427", S_IFCHR, instance,
"ddi_i2c:adio", 0) == DDI_FAILURE) {
cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
"%s\n", unitp->ltc1427_name, "ltc1427");
ddi_soft_state_free(ltc1427soft_statep, instance);
return (DDI_FAILURE);
}
if (i2c_client_register(dip, &unitp->ltc1427_hdl) != I2C_SUCCESS) {
ddi_remove_minor_node(dip, NULL);
ddi_soft_state_free(ltc1427soft_statep, instance);
return (DDI_FAILURE);
}
mutex_init(&unitp->ltc1427_mutex, NULL, MUTEX_DRIVER, NULL);
return (DDI_SUCCESS);
}
static int
ltc1427_do_resume()
{
int ret = DDI_SUCCESS;
return (ret);
}
static int
ltc1427_do_suspend()
{
int ret = DDI_SUCCESS;
return (ret);
}
static int
ltc1427_do_detach(dev_info_t *dip)
{
struct ltc1427_unit *unitp;
int instance;
instance = ddi_get_instance(dip);
unitp = ddi_get_soft_state(ltc1427soft_statep, instance);
if (unitp == NULL) {
cmn_err(CE_WARN, "%s%d: unitp not filled\n",
ddi_get_name(dip), instance);
return (ENOMEM);
}
i2c_client_unregister(unitp->ltc1427_hdl);
ddi_remove_minor_node(dip, NULL);
mutex_destroy(&unitp->ltc1427_mutex);
ddi_soft_state_free(ltc1427soft_statep, instance);
return (DDI_SUCCESS);
}