17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/stat.h>		/* ddi_create_minor_node S_IFCHR */
277c478bd9Sstevel@tonic-gate #include <sys/modctl.h>		/* for modldrv */
287c478bd9Sstevel@tonic-gate #include <sys/open.h>		/* for open params.	 */
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/conf.h>		/* req. by dev_ops flags MTSAFE etc. */
337c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <sys/note.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/i2c/clients/pic16f819_impl.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void *pic16f819soft_statep;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int pic16f819_set(struct pic16f819_unit *, int, uchar_t);
427c478bd9Sstevel@tonic-gate static int pic16f819_get(struct pic16f819_unit *, int, uchar_t *, int);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int pic16f819_do_attach(dev_info_t *);
467c478bd9Sstevel@tonic-gate static int pic16f819_do_detach(dev_info_t *);
477c478bd9Sstevel@tonic-gate static int pic16f819_do_resume(void);
487c478bd9Sstevel@tonic-gate static int pic16f819_do_suspend(void);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * cb ops (only need ioctl)
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static int pic16f819_open(dev_t *, int, int, cred_t *);
547c478bd9Sstevel@tonic-gate static int pic16f819_close(dev_t, int, int, cred_t *);
557c478bd9Sstevel@tonic-gate static int pic16f819_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static struct cb_ops pic16f819_cbops = {
587c478bd9Sstevel@tonic-gate 	pic16f819_open,			/* open  */
597c478bd9Sstevel@tonic-gate 	pic16f819_close,			/* close */
607c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
617c478bd9Sstevel@tonic-gate 	nodev,				/* print */
627c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
637c478bd9Sstevel@tonic-gate 	nodev,				/* read */
647c478bd9Sstevel@tonic-gate 	nodev,				/* write */
657c478bd9Sstevel@tonic-gate 	pic16f819_ioctl,			/* ioctl */
667c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
677c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
687c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
697c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
707c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
717c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
727c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
737c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
747c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
757c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
767c478bd9Sstevel@tonic-gate };
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * dev ops
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static int pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
827c478bd9Sstevel@tonic-gate static int pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static struct dev_ops pic16f819_ops = {
857c478bd9Sstevel@tonic-gate 	DEVO_REV,
867c478bd9Sstevel@tonic-gate 	0,
877c478bd9Sstevel@tonic-gate 	ddi_no_info,
887c478bd9Sstevel@tonic-gate 	nulldev,
897c478bd9Sstevel@tonic-gate 	nulldev,
907c478bd9Sstevel@tonic-gate 	pic16f819_attach,
917c478bd9Sstevel@tonic-gate 	pic16f819_detach,
927c478bd9Sstevel@tonic-gate 	nodev,
937c478bd9Sstevel@tonic-gate 	&pic16f819_cbops,
9419397407SSherry Moore 	NULL,			/* bus ops */
9519397407SSherry Moore 	NULL,			/* power */
9619397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct modldrv pic16f819_modldrv = {
1027c478bd9Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
10388294e09SRichard Bean 	"PIC16F819 i2c device driver",
1047c478bd9Sstevel@tonic-gate 	&pic16f819_ops
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static struct modlinkage pic16f819_modlinkage = {
1087c478bd9Sstevel@tonic-gate 	MODREV_1,
1097c478bd9Sstevel@tonic-gate 	&pic16f819_modldrv,
1107c478bd9Sstevel@tonic-gate 	0
1117c478bd9Sstevel@tonic-gate };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate int
_init(void)1157c478bd9Sstevel@tonic-gate _init(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	int error;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	error = mod_install(&pic16f819_modlinkage);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!error)
1227c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&pic16f819soft_statep,
12319397407SSherry Moore 		    sizeof (struct pic16f819_unit), 1);
1247c478bd9Sstevel@tonic-gate 	return (error);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate int
_fini(void)1287c478bd9Sstevel@tonic-gate _fini(void)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	int error;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	error = mod_remove(&pic16f819_modlinkage);
1337c478bd9Sstevel@tonic-gate 	if (!error)
1347c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&pic16f819soft_statep);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (error);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1407c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	return (mod_info(&pic16f819_modlinkage, modinfop));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static int
pic16f819_get(struct pic16f819_unit * unitp,int reg,uchar_t * byte,int flags)1467c478bd9Sstevel@tonic-gate pic16f819_get(struct pic16f819_unit *unitp, int reg, uchar_t *byte, int flags)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
1497c478bd9Sstevel@tonic-gate 	int			err;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer,
15219397407SSherry Moore 	    1, 1, flags);
1537c478bd9Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
1547c478bd9Sstevel@tonic-gate 		return (ENOMEM);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR_RD;
1587c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
1597c478bd9Sstevel@tonic-gate 	err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer);
1607c478bd9Sstevel@tonic-gate 	if (err) {
1617c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: pic16f819_get failed reg=%x",
16219397407SSherry Moore 		    unitp->pic16f819_name, reg));
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		*byte = i2c_tran_pointer->i2c_rbuf[0];
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer);
1687c478bd9Sstevel@tonic-gate 	return (err);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static int
pic16f819_set(struct pic16f819_unit * unitp,int reg,uchar_t byte)1727c478bd9Sstevel@tonic-gate pic16f819_set(struct pic16f819_unit *unitp, int reg, uchar_t byte)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
1757c478bd9Sstevel@tonic-gate 	int			err;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->pic16f819_hdl, &i2c_tran_pointer,
17819397407SSherry Moore 	    2, 0, I2C_SLEEP);
1797c478bd9Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
1807c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in pic16f819_set "
1817c478bd9Sstevel@tonic-gate 		"i2c_tran_pointer not allocated", unitp->pic16f819_name));
1827c478bd9Sstevel@tonic-gate 		return (ENOMEM);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = I2C_WR;
1867c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[0] = (uchar_t)reg;
1877c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_wbuf[1] = byte;
1887c478bd9Sstevel@tonic-gate 	D1CMN_ERR((CE_NOTE, "%s: set reg %x to %x",
18919397407SSherry Moore 	    unitp->pic16f819_name, reg, byte));
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	err = i2c_transfer(unitp->pic16f819_hdl, i2c_tran_pointer);
1927c478bd9Sstevel@tonic-gate 	if (err) {
1937c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in the pic16f819_set"
19419397407SSherry Moore 		    " i2c_transfer routine", unitp->pic16f819_name));
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->pic16f819_hdl, i2c_tran_pointer);
1977c478bd9Sstevel@tonic-gate 	return (err);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
pic16f819_open(dev_t * devp,int flags,int otyp,cred_t * credp)2017c478bd9Sstevel@tonic-gate pic16f819_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
2067c478bd9Sstevel@tonic-gate 	int instance;
2077c478bd9Sstevel@tonic-gate 	int error = 0;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (instance < 0) {
2127c478bd9Sstevel@tonic-gate 		return (ENXIO);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
21619397407SSherry Moore 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2197c478bd9Sstevel@tonic-gate 		return (ENXIO);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
2237c478bd9Sstevel@tonic-gate 		return (EINVAL);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
2297c478bd9Sstevel@tonic-gate 		if (unitp->pic16f819_oflag != 0) {
2307c478bd9Sstevel@tonic-gate 			error = EBUSY;
2317c478bd9Sstevel@tonic-gate 		} else {
2327c478bd9Sstevel@tonic-gate 			unitp->pic16f819_oflag = FEXCL;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	} else {
2357c478bd9Sstevel@tonic-gate 		if (unitp->pic16f819_oflag == FEXCL) {
2367c478bd9Sstevel@tonic-gate 			error = EBUSY;
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			unitp->pic16f819_oflag = FOPEN;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	return (error);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int
pic16f819_close(dev_t dev,int flags,int otyp,cred_t * credp)2487c478bd9Sstevel@tonic-gate pic16f819_close(dev_t dev, int flags, int otyp, cred_t *credp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
2537c478bd9Sstevel@tonic-gate 	int instance;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (instance < 0) {
2587c478bd9Sstevel@tonic-gate 		return (ENXIO);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
26219397407SSherry Moore 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2657c478bd9Sstevel@tonic-gate 		return (ENXIO);
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	unitp->pic16f819_oflag = 0;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
2737c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static int
pic16f819_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2777c478bd9Sstevel@tonic-gate pic16f819_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
278*111d63acSToomas Soome     cred_t *credp, int *rvalp)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
2817c478bd9Sstevel@tonic-gate 
282*111d63acSToomas Soome 	struct pic16f819_unit	*unitp;
283*111d63acSToomas Soome 	int		instance;
284*111d63acSToomas Soome 	int			err = 0;
2857c478bd9Sstevel@tonic-gate 	i2c_reg_t		ioctl_reg;
286*111d63acSToomas Soome 	uchar_t			val8;
2877c478bd9Sstevel@tonic-gate 
288*111d63acSToomas Soome 	if (arg == (intptr_t)NULL) {
2897c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "PIC16F819: ioctl: arg passed in to ioctl "
29019397407SSherry Moore 		    "= NULL\n"));
2917c478bd9Sstevel@tonic-gate 		err = EINVAL;
2927c478bd9Sstevel@tonic-gate 		return (err);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
2957c478bd9Sstevel@tonic-gate 	unitp = (struct pic16f819_unit *)
29619397407SSherry Moore 	    ddi_get_soft_state(pic16f819soft_statep, instance);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->pic16f819_mutex);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	switch (cmd) {
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	case I2C_GET_REG:
3037c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
30419397407SSherry Moore 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3057c478bd9Sstevel@tonic-gate 			err = EFAULT;
3067c478bd9Sstevel@tonic-gate 			break;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		err = pic16f819_get(unitp, ioctl_reg.reg_num, &val8,
30919397407SSherry Moore 		    I2C_SLEEP);
3107c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3117c478bd9Sstevel@tonic-gate 			break;
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		ioctl_reg.reg_value = val8;
3157c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg,
31619397407SSherry Moore 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3177c478bd9Sstevel@tonic-gate 			err = EFAULT;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		break;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	case I2C_SET_REG:
3227c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
32319397407SSherry Moore 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
3247c478bd9Sstevel@tonic-gate 			err = EFAULT;
3257c478bd9Sstevel@tonic-gate 			break;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 		err = pic16f819_set(unitp, ioctl_reg.reg_num,
32819397407SSherry Moore 		    ioctl_reg.reg_value);
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 	default:
3317c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x\n",
33219397407SSherry Moore 		    unitp->pic16f819_name, cmd));
3337c478bd9Sstevel@tonic-gate 		err = EINVAL;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->pic16f819_mutex);
3377c478bd9Sstevel@tonic-gate 	return (err);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate static int
pic16f819_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)3417c478bd9Sstevel@tonic-gate pic16f819_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	switch (cmd) {
3447c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
3457c478bd9Sstevel@tonic-gate 		return (pic16f819_do_attach(dip));
3467c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3477c478bd9Sstevel@tonic-gate 		return (pic16f819_do_resume());
3487c478bd9Sstevel@tonic-gate 	default:
3497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate static int
pic16f819_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)3547c478bd9Sstevel@tonic-gate pic16f819_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 	switch (cmd) {
3577c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
3587c478bd9Sstevel@tonic-gate 		return (pic16f819_do_detach(dip));
3597c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3607c478bd9Sstevel@tonic-gate 		return (pic16f819_do_suspend());
3617c478bd9Sstevel@tonic-gate 	default:
3627c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate static int
pic16f819_do_attach(dev_info_t * dip)3677c478bd9Sstevel@tonic-gate pic16f819_do_attach(dev_info_t *dip)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
3707c478bd9Sstevel@tonic-gate 	int instance;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(pic16f819soft_statep, instance) != 0) {
3757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate\n",
37619397407SSherry Moore 		    ddi_get_name(dip), instance);
3777c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pic16f819soft_statep, instance);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
3837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
38419397407SSherry Moore 		    ddi_get_name(dip), instance);
3857c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	(void) snprintf(unitp->pic16f819_name, sizeof (unitp->pic16f819_name),
38919397407SSherry Moore 	    "%s%d", ddi_node_name(dip), instance);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "fan_1", S_IFCHR, instance,
392*111d63acSToomas Soome 	    "ddi_i2c:pic", 0) == DDI_FAILURE) {
3937c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
39419397407SSherry Moore 		    "%s\n", unitp->pic16f819_name, "pic16f819");
3957c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pic16f819soft_statep, instance);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->pic16f819_hdl) != I2C_SUCCESS) {
4017c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s i2c_client_register failed\n",
40219397407SSherry Moore 		    unitp->pic16f819_name);
4037c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
4047c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(pic16f819soft_statep, instance);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	mutex_init(&unitp->pic16f819_mutex, NULL, MUTEX_DRIVER, NULL);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static int
pic16f819_do_resume()4157c478bd9Sstevel@tonic-gate pic16f819_do_resume()
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	return (ret);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static int
pic16f819_do_suspend()4237c478bd9Sstevel@tonic-gate pic16f819_do_suspend()
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (ret);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int
pic16f819_do_detach(dev_info_t * dip)4317c478bd9Sstevel@tonic-gate pic16f819_do_detach(dev_info_t *dip)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	struct pic16f819_unit *unitp;
4347c478bd9Sstevel@tonic-gate 	int instance;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(pic16f819soft_statep, instance);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
4417c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled\n",
44219397407SSherry Moore 		    ddi_get_name(dip), instance);
4437c478bd9Sstevel@tonic-gate 		return (ENOMEM);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	i2c_client_unregister(unitp->pic16f819_hdl);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	mutex_destroy(&unitp->pic16f819_mutex);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(pic16f819soft_statep, instance);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4557c478bd9Sstevel@tonic-gate }
456