10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57656SSherry.Moore@Sun.COM * Common Development and Distribution License (the "License").
67656SSherry.Moore@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
227656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23946Smathue * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/stat.h> /* ddi_create_minor_node S_IFCHR */
270Sstevel@tonic-gate #include <sys/modctl.h> /* for modldrv */
280Sstevel@tonic-gate #include <sys/open.h> /* for open params. */
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/conf.h> /* req. by dev_ops flags MTSAFE etc. */
330Sstevel@tonic-gate #include <sys/ddi.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/note.h>
360Sstevel@tonic-gate #include <sys/i2c/clients/lm75.h>
370Sstevel@tonic-gate #include <sys/i2c/clients/lm75_impl.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate static void *lm75soft_statep;
400Sstevel@tonic-gate
410Sstevel@tonic-gate static int lm75_do_attach(dev_info_t *);
420Sstevel@tonic-gate static int lm75_do_detach(dev_info_t *);
430Sstevel@tonic-gate static int lm75_do_resume(void);
440Sstevel@tonic-gate static int lm75_do_suspend(void);
450Sstevel@tonic-gate static int lm75_get16(intptr_t, int, struct lm75_unit *, int);
460Sstevel@tonic-gate static int lm75_set16(intptr_t, int, struct lm75_unit *, int);
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * cb ops (only need ioctl)
500Sstevel@tonic-gate */
510Sstevel@tonic-gate static int lm75_open(dev_t *, int, int, cred_t *);
520Sstevel@tonic-gate static int lm75_close(dev_t, int, int, cred_t *);
530Sstevel@tonic-gate static int lm75_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
540Sstevel@tonic-gate
550Sstevel@tonic-gate static struct cb_ops lm75_cbops = {
560Sstevel@tonic-gate lm75_open, /* open */
570Sstevel@tonic-gate lm75_close, /* close */
580Sstevel@tonic-gate nodev, /* strategy */
590Sstevel@tonic-gate nodev, /* print */
600Sstevel@tonic-gate nodev, /* dump */
610Sstevel@tonic-gate nodev, /* read */
620Sstevel@tonic-gate nodev, /* write */
630Sstevel@tonic-gate lm75_ioctl, /* ioctl */
640Sstevel@tonic-gate nodev, /* devmap */
650Sstevel@tonic-gate nodev, /* mmap */
660Sstevel@tonic-gate nodev, /* segmap */
670Sstevel@tonic-gate nochpoll, /* poll */
680Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */
690Sstevel@tonic-gate NULL, /* streamtab */
700Sstevel@tonic-gate D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */
710Sstevel@tonic-gate CB_REV, /* rev */
720Sstevel@tonic-gate nodev, /* int (*cb_aread)() */
730Sstevel@tonic-gate nodev /* int (*cb_awrite)() */
740Sstevel@tonic-gate };
750Sstevel@tonic-gate
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate * dev ops
780Sstevel@tonic-gate */
790Sstevel@tonic-gate static int lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
800Sstevel@tonic-gate static int lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
810Sstevel@tonic-gate
820Sstevel@tonic-gate static struct dev_ops lm75_ops = {
830Sstevel@tonic-gate DEVO_REV,
840Sstevel@tonic-gate 0,
850Sstevel@tonic-gate ddi_getinfo_1to1,
860Sstevel@tonic-gate nulldev,
870Sstevel@tonic-gate nulldev,
880Sstevel@tonic-gate lm75_attach,
890Sstevel@tonic-gate lm75_detach,
900Sstevel@tonic-gate nodev,
910Sstevel@tonic-gate &lm75_cbops,
927656SSherry.Moore@Sun.COM NULL,
937656SSherry.Moore@Sun.COM NULL,
947656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */
950Sstevel@tonic-gate };
960Sstevel@tonic-gate
970Sstevel@tonic-gate extern struct mod_ops mod_driverops;
980Sstevel@tonic-gate
990Sstevel@tonic-gate static struct modldrv lm75_modldrv = {
1000Sstevel@tonic-gate &mod_driverops, /* type of module - driver */
101*7696SRichard.Bean@Sun.COM "LM75 i2c device driver",
1020Sstevel@tonic-gate &lm75_ops
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate static struct modlinkage lm75_modlinkage = {
1060Sstevel@tonic-gate MODREV_1,
1070Sstevel@tonic-gate &lm75_modldrv,
1080Sstevel@tonic-gate 0
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate int
_init(void)1130Sstevel@tonic-gate _init(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate int error;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate error = mod_install(&lm75_modlinkage);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (!error)
1200Sstevel@tonic-gate (void) ddi_soft_state_init(&lm75soft_statep,
1217656SSherry.Moore@Sun.COM sizeof (struct lm75_unit), 1);
1220Sstevel@tonic-gate return (error);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate int
_fini(void)1260Sstevel@tonic-gate _fini(void)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate int error;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate error = mod_remove(&lm75_modlinkage);
1310Sstevel@tonic-gate if (!error)
1320Sstevel@tonic-gate ddi_soft_state_fini(&lm75soft_statep);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate return (error);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1380Sstevel@tonic-gate _info(struct modinfo *modinfop)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate return (mod_info(&lm75_modlinkage, modinfop));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate static int
lm75_open(dev_t * devp,int flags,int otyp,cred_t * credp)1440Sstevel@tonic-gate lm75_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate _NOTE(ARGUNUSED(credp))
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate struct lm75_unit *unitp;
1490Sstevel@tonic-gate int instance;
1500Sstevel@tonic-gate int error = 0;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate instance = getminor(*devp);
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate if (instance < 0) {
1550Sstevel@tonic-gate return (ENXIO);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate unitp = (struct lm75_unit *)
1597656SSherry.Moore@Sun.COM ddi_get_soft_state(lm75soft_statep, instance);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate if (unitp == NULL) {
1620Sstevel@tonic-gate return (ENXIO);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (otyp != OTYP_CHR) {
1660Sstevel@tonic-gate return (EINVAL);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate mutex_enter(&unitp->lm75_mutex);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if (flags & FEXCL) {
1720Sstevel@tonic-gate if (unitp->lm75_oflag != 0) {
1730Sstevel@tonic-gate error = EBUSY;
1740Sstevel@tonic-gate } else {
1750Sstevel@tonic-gate unitp->lm75_oflag = FEXCL;
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate } else {
1780Sstevel@tonic-gate if (unitp->lm75_oflag == FEXCL) {
1790Sstevel@tonic-gate error = EBUSY;
1800Sstevel@tonic-gate } else {
1810Sstevel@tonic-gate unitp->lm75_oflag = FOPEN;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate mutex_exit(&unitp->lm75_mutex);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate return (error);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate static int
lm75_close(dev_t dev,int flags,int otyp,cred_t * credp)1910Sstevel@tonic-gate lm75_close(dev_t dev, int flags, int otyp, cred_t *credp)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate _NOTE(ARGUNUSED(flags, otyp, credp))
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate struct lm75_unit *unitp;
1960Sstevel@tonic-gate int instance;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate instance = getminor(dev);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (instance < 0) {
2010Sstevel@tonic-gate return (ENXIO);
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate unitp = (struct lm75_unit *)
2057656SSherry.Moore@Sun.COM ddi_get_soft_state(lm75soft_statep, instance);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (unitp == NULL) {
2080Sstevel@tonic-gate return (ENXIO);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate mutex_enter(&unitp->lm75_mutex);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate unitp->lm75_oflag = 0;
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate mutex_exit(&unitp->lm75_mutex);
2160Sstevel@tonic-gate return (DDI_SUCCESS);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate static int
lm75_get16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)2200Sstevel@tonic-gate lm75_get16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode) {
2210Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer;
2220Sstevel@tonic-gate int err = DDI_SUCCESS;
2230Sstevel@tonic-gate int16_t temp16;
2240Sstevel@tonic-gate int8_t holder;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
2270Sstevel@tonic-gate 1, 2, I2C_SLEEP);
2280Sstevel@tonic-gate if (i2c_tran_pointer == NULL) {
2290Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
2300Sstevel@tonic-gate "i2c_tran_pointer not allocated\n",
2310Sstevel@tonic-gate unitp->lm75_name));
2320Sstevel@tonic-gate return (ENOMEM);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR_RD;
2360Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
2390Sstevel@tonic-gate if (err) {
2400Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
2410Sstevel@tonic-gate "i2c_transfer routine\n",
2420Sstevel@tonic-gate unitp->lm75_name));
2430Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
2440Sstevel@tonic-gate return (err);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate D1CMN_ERR((CE_NOTE, "%s: rbuf[0] = %x rbuf[1] = %x\n",
2480Sstevel@tonic-gate unitp->lm75_name, i2c_tran_pointer->i2c_rbuf[0],
2490Sstevel@tonic-gate i2c_tran_pointer->i2c_rbuf[0]));
2500Sstevel@tonic-gate temp16 = i2c_tran_pointer->i2c_rbuf[0];
2510Sstevel@tonic-gate temp16 = (temp16 << 1);
2520Sstevel@tonic-gate temp16 = (temp16 | ((i2c_tran_pointer->i2c_rbuf[1] & 0x80) >> 7));
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate if (temp16 & LM75_COMP_MASK) {
2560Sstevel@tonic-gate holder = (temp16 & LM75_COMP_MASK_UPPER);
2570Sstevel@tonic-gate holder = -holder;
2580Sstevel@tonic-gate holder = holder/2;
2590Sstevel@tonic-gate temp16 = 0 - holder;
2600Sstevel@tonic-gate } else {
2610Sstevel@tonic-gate temp16 = temp16 / 2;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate if (ddi_copyout((caddr_t)&temp16, (caddr_t)arg,
2640Sstevel@tonic-gate sizeof (int16_t), mode) != DDI_SUCCESS) {
2650Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_TEMPERATURE "
2660Sstevel@tonic-gate "ddi_copyout routine\n",
2670Sstevel@tonic-gate unitp->lm75_name));
2680Sstevel@tonic-gate err = EFAULT;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
2710Sstevel@tonic-gate return (err);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate static int
lm75_set16(intptr_t arg,int reg,struct lm75_unit * unitp,int mode)2750Sstevel@tonic-gate lm75_set16(intptr_t arg, int reg, struct lm75_unit *unitp, int mode) {
2760Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer;
2770Sstevel@tonic-gate int err = DDI_SUCCESS;
2780Sstevel@tonic-gate int16_t temp16;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&temp16,
2810Sstevel@tonic-gate sizeof (int16_t), mode) != DDI_SUCCESS) {
2820Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM74_SET_HYST "
2830Sstevel@tonic-gate "ddi_copyin routine\n",
2840Sstevel@tonic-gate unitp->lm75_name));
2850Sstevel@tonic-gate return (EFAULT);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
2890Sstevel@tonic-gate 3, 0, I2C_SLEEP);
2900Sstevel@tonic-gate if (i2c_tran_pointer == NULL) {
2910Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_HYST "
2920Sstevel@tonic-gate "i2c_tran_pointer not allocated\n",
2930Sstevel@tonic-gate unitp->lm75_name));
2940Sstevel@tonic-gate return (ENOMEM);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR;
2970Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
2980Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[1] = (temp16 >> 1);
2990Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[2] = ((temp16 & 0xFE) << 7);
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
3020Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3030Sstevel@tonic-gate return (err);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate static int
lm75_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)3070Sstevel@tonic-gate lm75_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
3080Sstevel@tonic-gate int *rvalp)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate _NOTE(ARGUNUSED(credp, rvalp))
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate struct lm75_unit *unitp;
3130Sstevel@tonic-gate int instance;
3140Sstevel@tonic-gate int err = 0;
3150Sstevel@tonic-gate i2c_transfer_t *i2c_tran_pointer;
3160Sstevel@tonic-gate uchar_t passin_byte;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate if (arg == NULL) {
3190Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "LM75: ioctl: arg passed in to ioctl "
3207656SSherry.Moore@Sun.COM "= NULL\n"));
3210Sstevel@tonic-gate err = EINVAL;
3220Sstevel@tonic-gate return (err);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate instance = getminor(dev);
3250Sstevel@tonic-gate unitp = (struct lm75_unit *)
3267656SSherry.Moore@Sun.COM ddi_get_soft_state(lm75soft_statep, instance);
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate if (unitp == NULL) {
3290Sstevel@tonic-gate cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
3300Sstevel@tonic-gate err = ENOMEM;
3310Sstevel@tonic-gate return (err);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate mutex_enter(&unitp->lm75_mutex);
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate switch (cmd) {
3370Sstevel@tonic-gate case I2C_GET_TEMPERATURE:
3380Sstevel@tonic-gate err = lm75_get16(arg, LM75_TEMPERATURE_REG, unitp, mode);
3390Sstevel@tonic-gate break;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate case LM75_GET_HYST:
3420Sstevel@tonic-gate err = lm75_get16(arg, LM75_HYST_REG, unitp, mode);
3430Sstevel@tonic-gate break;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate case LM75_SET_HYST:
3460Sstevel@tonic-gate err = lm75_set16(arg, LM75_HYST_REG, unitp, mode);
3470Sstevel@tonic-gate break;
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate case LM75_GET_OVERTEMP_SHUTDOWN:
3500Sstevel@tonic-gate err = lm75_get16(arg, LM75_OVERTEMP_REG, unitp, mode);
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate case LM75_SET_OVERTEMP_SHUTDOWN:
3540Sstevel@tonic-gate err = lm75_set16(arg, LM75_OVERTEMP_REG, unitp, mode);
3550Sstevel@tonic-gate break;
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate case LM75_GET_CONFIG:
3580Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
3597656SSherry.Moore@Sun.COM 1, 1, I2C_SLEEP);
3600Sstevel@tonic-gate if (i2c_tran_pointer == NULL) {
3610Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
3627656SSherry.Moore@Sun.COM "i2c_tran_pointer not allocated\n",
3637656SSherry.Moore@Sun.COM unitp->lm75_name));
3640Sstevel@tonic-gate err = ENOMEM;
3650Sstevel@tonic-gate break;
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR_RD;
3680Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
3710Sstevel@tonic-gate if (err) {
3720Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
3737656SSherry.Moore@Sun.COM "i2c_transfer routine\n",
3747656SSherry.Moore@Sun.COM unitp->lm75_name));
3750Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3760Sstevel@tonic-gate break;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate if (ddi_copyout((caddr_t)i2c_tran_pointer->i2c_rbuf,
3797656SSherry.Moore@Sun.COM (caddr_t)arg,
3807656SSherry.Moore@Sun.COM sizeof (uint8_t), mode) != DDI_SUCCESS) {
3810Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_GET_CONFIG "
3827656SSherry.Moore@Sun.COM "ddi_copyout routine\n",
3837656SSherry.Moore@Sun.COM unitp->lm75_name));
3840Sstevel@tonic-gate err = EFAULT;
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
3870Sstevel@tonic-gate break;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate case LM75_SET_CONFIG:
3900Sstevel@tonic-gate if (ddi_copyin((caddr_t)arg, (caddr_t)&passin_byte,
3917656SSherry.Moore@Sun.COM sizeof (uint8_t), mode) != DDI_SUCCESS) {
3920Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
3937656SSherry.Moore@Sun.COM "ddi_copyin routine\n",
3947656SSherry.Moore@Sun.COM unitp->lm75_name));
3950Sstevel@tonic-gate err = EFAULT;
3960Sstevel@tonic-gate break;
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate (void) i2c_transfer_alloc(unitp->lm75_hdl, &i2c_tran_pointer,
3997656SSherry.Moore@Sun.COM 2, 0, I2C_SLEEP);
4000Sstevel@tonic-gate if (i2c_tran_pointer == NULL) {
4010Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Failed in LM75_SET_CONFIG "
4027656SSherry.Moore@Sun.COM "i2c_tran_pointer not allocated\n",
4037656SSherry.Moore@Sun.COM unitp->lm75_name));
4040Sstevel@tonic-gate err = ENOMEM;
4050Sstevel@tonic-gate break;
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate i2c_tran_pointer->i2c_flags = I2C_WR;
4080Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[0] = LM75_CONFIGURATION_REG;
4090Sstevel@tonic-gate i2c_tran_pointer->i2c_wbuf[1] = passin_byte;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate err = i2c_transfer(unitp->lm75_hdl, i2c_tran_pointer);
4120Sstevel@tonic-gate i2c_transfer_free(unitp->lm75_hdl, i2c_tran_pointer);
4130Sstevel@tonic-gate break;
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate default:
4160Sstevel@tonic-gate D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd %x\n",
4177656SSherry.Moore@Sun.COM unitp->lm75_name, cmd));
4180Sstevel@tonic-gate err = EINVAL;
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate mutex_exit(&unitp->lm75_mutex);
4220Sstevel@tonic-gate return (err);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate
4250Sstevel@tonic-gate static int
lm75_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4260Sstevel@tonic-gate lm75_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate switch (cmd) {
4290Sstevel@tonic-gate case DDI_ATTACH:
4300Sstevel@tonic-gate return (lm75_do_attach(dip));
4310Sstevel@tonic-gate case DDI_RESUME:
4320Sstevel@tonic-gate return (lm75_do_resume());
4330Sstevel@tonic-gate default:
4340Sstevel@tonic-gate return (DDI_FAILURE);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate static int
lm75_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4390Sstevel@tonic-gate lm75_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate switch (cmd) {
4420Sstevel@tonic-gate case DDI_DETACH:
4430Sstevel@tonic-gate return (lm75_do_detach(dip));
4440Sstevel@tonic-gate case DDI_SUSPEND:
4450Sstevel@tonic-gate return (lm75_do_suspend());
4460Sstevel@tonic-gate default:
4470Sstevel@tonic-gate return (DDI_FAILURE);
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate static int
lm75_do_attach(dev_info_t * dip)4520Sstevel@tonic-gate lm75_do_attach(dev_info_t *dip)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate struct lm75_unit *unitp;
4550Sstevel@tonic-gate int instance;
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate instance = ddi_get_instance(dip);
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate if (ddi_soft_state_zalloc(lm75soft_statep, instance) != 0) {
4600Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
4617656SSherry.Moore@Sun.COM ddi_get_name(dip), instance);
4620Sstevel@tonic-gate return (DDI_FAILURE);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate unitp = ddi_get_soft_state(lm75soft_statep, instance);
4660Sstevel@tonic-gate
4670Sstevel@tonic-gate if (unitp == NULL) {
4680Sstevel@tonic-gate cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
4690Sstevel@tonic-gate return (ENOMEM);
4700Sstevel@tonic-gate }
4710Sstevel@tonic-gate
4720Sstevel@tonic-gate (void) snprintf(unitp->lm75_name, sizeof (unitp->lm75_name),
4737656SSherry.Moore@Sun.COM "%s%d", ddi_node_name(dip), instance);
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate if (ddi_create_minor_node(dip, "lm75", S_IFCHR, instance,
4767656SSherry.Moore@Sun.COM "ddi_i2c:temperature_sensor", NULL) == DDI_FAILURE) {
4770Sstevel@tonic-gate cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
4787656SSherry.Moore@Sun.COM "%s\n", unitp->lm75_name, "lm75");
4790Sstevel@tonic-gate ddi_soft_state_free(lm75soft_statep, instance);
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate return (DDI_FAILURE);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate if (i2c_client_register(dip, &unitp->lm75_hdl) != I2C_SUCCESS) {
4850Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
4860Sstevel@tonic-gate ddi_soft_state_free(lm75soft_statep, instance);
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate return (DDI_FAILURE);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate mutex_init(&unitp->lm75_mutex, NULL, MUTEX_DRIVER, NULL);
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate return (DDI_SUCCESS);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate static int
lm75_do_resume(void)4970Sstevel@tonic-gate lm75_do_resume(void)
4980Sstevel@tonic-gate {
4990Sstevel@tonic-gate int ret = DDI_SUCCESS;
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate return (ret);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate static int
lm75_do_suspend()5050Sstevel@tonic-gate lm75_do_suspend()
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate int ret = DDI_SUCCESS;
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate return (ret);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate static int
lm75_do_detach(dev_info_t * dip)5130Sstevel@tonic-gate lm75_do_detach(dev_info_t *dip)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate struct lm75_unit *unitp;
5160Sstevel@tonic-gate int instance;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate instance = ddi_get_instance(dip);
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate unitp = ddi_get_soft_state(lm75soft_statep, instance);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate if (unitp == NULL) {
5230Sstevel@tonic-gate cmn_err(CE_WARN, "LM75: ioctl: unitp = NULL\n");
5240Sstevel@tonic-gate return (ENOMEM);
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate i2c_client_unregister(unitp->lm75_hdl);
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL);
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate mutex_destroy(&unitp->lm75_mutex);
5320Sstevel@tonic-gate ddi_soft_state_free(lm75soft_statep, instance);
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate return (DDI_SUCCESS);
5350Sstevel@tonic-gate }
536