xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/pic16f819.c (revision 7696:21f5c73c0c15)
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.
230Sstevel@tonic-gate  * 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 
370Sstevel@tonic-gate #include <sys/i2c/clients/pic16f819_impl.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static void *pic16f819soft_statep;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static int pic16f819_set(struct pic16f819_unit *, int, uchar_t);
420Sstevel@tonic-gate static int pic16f819_get(struct pic16f819_unit *, int, uchar_t *, int);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int pic16f819_do_attach(dev_info_t *);
460Sstevel@tonic-gate static int pic16f819_do_detach(dev_info_t *);
470Sstevel@tonic-gate static int pic16f819_do_resume(void);
480Sstevel@tonic-gate static int pic16f819_do_suspend(void);
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * cb ops (only need ioctl)
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate static int pic16f819_open(dev_t *, int, int, cred_t *);
540Sstevel@tonic-gate static int pic16f819_close(dev_t, int, int, cred_t *);
550Sstevel@tonic-gate static int pic16f819_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static struct cb_ops pic16f819_cbops = {
580Sstevel@tonic-gate 	pic16f819_open,			/* open  */
590Sstevel@tonic-gate 	pic16f819_close,			/* close */
600Sstevel@tonic-gate 	nodev,				/* strategy */
610Sstevel@tonic-gate 	nodev,				/* print */
620Sstevel@tonic-gate 	nodev,				/* dump */
630Sstevel@tonic-gate 	nodev,				/* read */
640Sstevel@tonic-gate 	nodev,				/* write */
650Sstevel@tonic-gate 	pic16f819_ioctl,			/* ioctl */
660Sstevel@tonic-gate 	nodev,				/* devmap */
670Sstevel@tonic-gate 	nodev,				/* mmap */
680Sstevel@tonic-gate 	nodev,				/* segmap */
690Sstevel@tonic-gate 	nochpoll,			/* poll */
700Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
710Sstevel@tonic-gate 	NULL,				/* streamtab */
720Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
730Sstevel@tonic-gate 	CB_REV,				/* rev */
740Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
750Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * dev ops
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static int pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
820Sstevel@tonic-gate static int pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static struct dev_ops pic16f819_ops = {
850Sstevel@tonic-gate 	DEVO_REV,
860Sstevel@tonic-gate 	0,
870Sstevel@tonic-gate 	ddi_no_info,
880Sstevel@tonic-gate 	nulldev,
890Sstevel@tonic-gate 	nulldev,
900Sstevel@tonic-gate 	pic16f819_attach,
910Sstevel@tonic-gate 	pic16f819_detach,
920Sstevel@tonic-gate 	nodev,
930Sstevel@tonic-gate 	&pic16f819_cbops,
947656SSherry.Moore@Sun.COM 	NULL,			/* bus ops */
957656SSherry.Moore@Sun.COM 	NULL,			/* power */
967656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct modldrv pic16f819_modldrv = {
1020Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
103*7696SRichard.Bean@Sun.COM 	"PIC16F819 i2c device driver",
1040Sstevel@tonic-gate 	&pic16f819_ops
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct modlinkage pic16f819_modlinkage = {
1080Sstevel@tonic-gate 	MODREV_1,
1090Sstevel@tonic-gate 	&pic16f819_modldrv,
1100Sstevel@tonic-gate 	0
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate int
_init(void)1150Sstevel@tonic-gate _init(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	int error;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	error = mod_install(&pic16f819_modlinkage);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (!error)
1220Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pic16f819soft_statep,
1237656SSherry.Moore@Sun.COM 		    sizeof (struct pic16f819_unit), 1);
1240Sstevel@tonic-gate 	return (error);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate int
_fini(void)1280Sstevel@tonic-gate _fini(void)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	int error;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	error = mod_remove(&pic16f819_modlinkage);
1330Sstevel@tonic-gate 	if (!error)
1340Sstevel@tonic-gate 		ddi_soft_state_fini(&pic16f819soft_statep);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	return (error);
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1400Sstevel@tonic-gate _info(struct modinfo *modinfop)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	return (mod_info(&pic16f819_modlinkage, modinfop));
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static int
pic16f819_get(struct pic16f819_unit * unitp,int reg,uchar_t * byte,int flags)1460Sstevel@tonic-gate pic16f819_get(struct pic16f819_unit *unitp, int reg, uchar_t *byte, int flags)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
1490Sstevel@tonic-gate 	int			err;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer,
1527656SSherry.Moore@Sun.COM 	    1, 1, flags);
1530Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
1540Sstevel@tonic-gate 		return (ENOMEM);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR_RD;
1580Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
1590Sstevel@tonic-gate 	err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer);
1600Sstevel@tonic-gate 	if (err) {
1610Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: pic16f819_get failed reg=%x",
1627656SSherry.Moore@Sun.COM 		    unitp->pic16f819_name, reg));
1630Sstevel@tonic-gate 	} else {
1640Sstevel@tonic-gate 		*byte = i2c_tran_pointer->i2c_rbuf[0];
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer);
1680Sstevel@tonic-gate 	return (err);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate static int
pic16f819_set(struct pic16f819_unit * unitp,int reg,uchar_t byte)1720Sstevel@tonic-gate pic16f819_set(struct pic16f819_unit *unitp, int reg, uchar_t byte)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
1750Sstevel@tonic-gate 	int			err;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer,
1787656SSherry.Moore@Sun.COM 	    2, 0, I2C_SLEEP);
1790Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
1800Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pic16f819_set "
1810Sstevel@tonic-gate 		"i2c_tran_pointer not allocated", unitp->pic16f819_name));
1820Sstevel@tonic-gate 		return (ENOMEM);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
1860Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
1870Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[1] = byte;
1880Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: set reg %x to %x",
1897656SSherry.Moore@Sun.COM 	    unitp->pic16f819_name, reg, byte));
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer);
1920Sstevel@tonic-gate 	if (err) {
1930Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the pic16f819_set"
1947656SSherry.Moore@Sun.COM 		    " i2c_transfer routine", unitp->pic16f819_name));
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer);
1970Sstevel@tonic-gate 	return (err);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static int
pic16f819_open(dev_t * devp,int flags,int otyp,cred_t * credp)2010Sstevel@tonic-gate pic16f819_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
2060Sstevel@tonic-gate 	int instance;
2070Sstevel@tonic-gate 	int error = 0;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	instance = getminor(*devp);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	if (instance < 0) {
2120Sstevel@tonic-gate 		return (ENXIO);
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
2167656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (unitp == NULL) {
2190Sstevel@tonic-gate 		return (ENXIO);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
2230Sstevel@tonic-gate 		return (EINVAL);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (flags & FEXCL) {
2290Sstevel@tonic-gate 		if (unitp->pic16f819_oflag != 0) {
2300Sstevel@tonic-gate 			error = EBUSY;
2310Sstevel@tonic-gate 		} else {
2320Sstevel@tonic-gate 			unitp->pic16f819_oflag = FEXCL;
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 	} else {
2350Sstevel@tonic-gate 		if (unitp->pic16f819_oflag == FEXCL) {
2360Sstevel@tonic-gate 			error = EBUSY;
2370Sstevel@tonic-gate 		} else {
2380Sstevel@tonic-gate 			unitp->pic16f819_oflag = FOPEN;
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (error);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static int
pic16f819_close(dev_t dev,int flags,int otyp,cred_t * credp)2480Sstevel@tonic-gate pic16f819_close(dev_t dev, int flags, int otyp, cred_t *credp)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
2530Sstevel@tonic-gate 	int instance;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	instance = getminor(dev);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if (instance < 0) {
2580Sstevel@tonic-gate 		return (ENXIO);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
2627656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (unitp == NULL) {
2650Sstevel@tonic-gate 		return (ENXIO);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	unitp->pic16f819_oflag = 0;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
2730Sstevel@tonic-gate 	return (DDI_SUCCESS);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static int
pic16f819_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2770Sstevel@tonic-gate pic16f819_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
2780Sstevel@tonic-gate 		cred_t *credp, int *rvalp)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	struct pic16f819_unit 	*unitp;
2830Sstevel@tonic-gate 	int 		instance;
2840Sstevel@tonic-gate 	int 			err = 0;
2850Sstevel@tonic-gate 	i2c_reg_t		ioctl_reg;
2860Sstevel@tonic-gate 	uchar_t 		val8;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (arg == NULL) {
2890Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PIC16F819: ioctl: arg passed in to ioctl "
2907656SSherry.Moore@Sun.COM 		    "= NULL\n"));
2910Sstevel@tonic-gate 		err = EINVAL;
2920Sstevel@tonic-gate 		return (err);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 	instance = getminor(dev);
2950Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
2967656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	switch (cmd) {
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	case I2C_GET_REG:
3030Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
3047656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3050Sstevel@tonic-gate 			err = EFAULT;
3060Sstevel@tonic-gate 			break;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		err = pic16f819_get(unitp, ioctl_reg.reg_num, &val8,
3097656SSherry.Moore@Sun.COM 		    I2C_SLEEP);
3100Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3110Sstevel@tonic-gate 			break;
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 		ioctl_reg.reg_value = val8;
3150Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg,
3167656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3170Sstevel@tonic-gate 			err = EFAULT;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 		break;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	case I2C_SET_REG:
3220Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
3237656SSherry.Moore@Sun.COM 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3240Sstevel@tonic-gate 			err = EFAULT;
3250Sstevel@tonic-gate 			break;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 		err = pic16f819_set(unitp, ioctl_reg.reg_num,
3287656SSherry.Moore@Sun.COM 		    ioctl_reg.reg_value);
3290Sstevel@tonic-gate 		break;
3300Sstevel@tonic-gate 	default:
3310Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
3327656SSherry.Moore@Sun.COM 		    unitp->pic16f819_name, cmd));
3330Sstevel@tonic-gate 		err = EINVAL;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
3370Sstevel@tonic-gate 	return (err);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate static int
pic16f819_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3410Sstevel@tonic-gate pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	switch (cmd) {
3440Sstevel@tonic-gate 	case DDI_ATTACH:
3450Sstevel@tonic-gate 		return (pic16f819_do_attach(dip));
3460Sstevel@tonic-gate 	case DDI_RESUME:
3470Sstevel@tonic-gate 		return (pic16f819_do_resume());
3480Sstevel@tonic-gate 	default:
3490Sstevel@tonic-gate 		return (DDI_FAILURE);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static int
pic16f819_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3540Sstevel@tonic-gate pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	switch (cmd) {
3570Sstevel@tonic-gate 	case DDI_DETACH:
3580Sstevel@tonic-gate 		return (pic16f819_do_detach(dip));
3590Sstevel@tonic-gate 	case DDI_SUSPEND:
3600Sstevel@tonic-gate 		return (pic16f819_do_suspend());
3610Sstevel@tonic-gate 	default:
3620Sstevel@tonic-gate 		return (DDI_FAILURE);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate static int
pic16f819_do_attach(dev_info_t * dip)3670Sstevel@tonic-gate pic16f819_do_attach(dev_info_t *dip)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
3700Sstevel@tonic-gate 	int instance;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pic16f819soft_statep, instance) != 0) {
3750Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
3767656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
3770Sstevel@tonic-gate 		return (DDI_FAILURE);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pic16f819soft_statep, instance);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if (unitp == NULL) {
3830Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
3847656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
3850Sstevel@tonic-gate 		return (ENOMEM);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	(void) snprintf(unitp->pic16f819_name, sizeof (unitp->pic16f819_name),
3897656SSherry.Moore@Sun.COM 	    "%s%d", ddi_node_name(dip), instance);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "fan_1", S_IFCHR, instance,
3927656SSherry.Moore@Sun.COM 	    "ddi_i2c:pic",	NULL) == DDI_FAILURE) {
3930Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
3947656SSherry.Moore@Sun.COM 		    "%s\n", unitp->pic16f819_name, "pic16f819");
3950Sstevel@tonic-gate 		ddi_soft_state_free(pic16f819soft_statep, instance);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		return (DDI_FAILURE);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pic16f819_hdl) != I2C_SUCCESS) {
4010Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_client_register failed\n",
4027656SSherry.Moore@Sun.COM 		    unitp->pic16f819_name);
4030Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4040Sstevel@tonic-gate 		ddi_soft_state_free(pic16f819soft_statep, instance);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		return (DDI_FAILURE);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	mutex_init(&unitp->pic16f819_mutex, NULL, MUTEX_DRIVER, NULL);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	return (DDI_SUCCESS);
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate static int
pic16f819_do_resume()4150Sstevel@tonic-gate pic16f819_do_resume()
4160Sstevel@tonic-gate {
4170Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	return (ret);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate static int
pic16f819_do_suspend()4230Sstevel@tonic-gate pic16f819_do_suspend()
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	return (ret);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static int
pic16f819_do_detach(dev_info_t * dip)4310Sstevel@tonic-gate pic16f819_do_detach(dev_info_t *dip)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
4340Sstevel@tonic-gate 	int instance;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pic16f819soft_statep, instance);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if (unitp == NULL) {
4410Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
4427656SSherry.Moore@Sun.COM 		    ddi_get_name(dip), instance);
4430Sstevel@tonic-gate 		return (ENOMEM);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	i2c_client_unregister(unitp->pic16f819_hdl);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	mutex_destroy(&unitp->pic16f819_mutex);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	ddi_soft_state_free(pic16f819soft_statep, instance);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	return (DDI_SUCCESS);
4550Sstevel@tonic-gate }
456