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/ssc100_impl.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void *ssc100soft_statep;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static int ssc100_do_attach(dev_info_t *);
427c478bd9Sstevel@tonic-gate static int ssc100_do_detach(dev_info_t *);
437c478bd9Sstevel@tonic-gate static int ssc100_do_resume(void);
447c478bd9Sstevel@tonic-gate static int ssc100_do_suspend(void);
457c478bd9Sstevel@tonic-gate static int ssc100_get(struct ssc100_unit *, uchar_t *);
467c478bd9Sstevel@tonic-gate static int ssc100_set(struct ssc100_unit *, uchar_t);
477c478bd9Sstevel@tonic-gate static int ssc100_get_reg(struct ssc100_unit *, uchar_t *, uchar_t);
487c478bd9Sstevel@tonic-gate static int ssc100_common(struct ssc100_unit *, uchar_t *, uchar_t, int8_t);
497c478bd9Sstevel@tonic-gate static int ssc100_read(dev_t, struct uio *, cred_t *);
507c478bd9Sstevel@tonic-gate static int ssc100_write(dev_t, struct uio *, cred_t *);
517c478bd9Sstevel@tonic-gate static int ssc100_io(dev_t, struct uio *, int);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * cb ops (only need ioctl)
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static int ssc100_open(dev_t *, int, int, cred_t *);
577c478bd9Sstevel@tonic-gate static int ssc100_close(dev_t, int, int, cred_t *);
587c478bd9Sstevel@tonic-gate static int ssc100_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate static struct cb_ops ssc100_cbops = {
617c478bd9Sstevel@tonic-gate 	ssc100_open,			/* open  */
627c478bd9Sstevel@tonic-gate 	ssc100_close,			/* close */
637c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
647c478bd9Sstevel@tonic-gate 	nodev,				/* print */
657c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
667c478bd9Sstevel@tonic-gate 	ssc100_read,			/* read */
677c478bd9Sstevel@tonic-gate 	ssc100_write,			/* write */
687c478bd9Sstevel@tonic-gate 	ssc100_ioctl,			/* ioctl */
697c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
707c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
717c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
727c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
737c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
747c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
757c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
767c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
777c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
787c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * dev ops
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static int ssc100_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
857c478bd9Sstevel@tonic-gate static int ssc100_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static struct dev_ops ssc100_ops = {
887c478bd9Sstevel@tonic-gate 	DEVO_REV,
897c478bd9Sstevel@tonic-gate 	0,
907c478bd9Sstevel@tonic-gate 	ddi_getinfo_1to1,
917c478bd9Sstevel@tonic-gate 	nulldev,
927c478bd9Sstevel@tonic-gate 	nulldev,
937c478bd9Sstevel@tonic-gate 	ssc100_attach,
947c478bd9Sstevel@tonic-gate 	ssc100_detach,
957c478bd9Sstevel@tonic-gate 	nodev,
967c478bd9Sstevel@tonic-gate 	&ssc100_cbops,
9719397407SSherry Moore 	NULL,
9819397407SSherry Moore 	NULL,
9919397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static struct modldrv ssc100_modldrv = {
1057c478bd9Sstevel@tonic-gate 	&mod_driverops,			/* type of module - driver */
10688294e09SRichard Bean 	"SSC100 i2c device driver",
1077c478bd9Sstevel@tonic-gate 	&ssc100_ops
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static struct modlinkage ssc100_modlinkage = {
1117c478bd9Sstevel@tonic-gate 	MODREV_1,
1127c478bd9Sstevel@tonic-gate 	&ssc100_modldrv,
1137c478bd9Sstevel@tonic-gate 	0
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
_init(void)1187c478bd9Sstevel@tonic-gate _init(void)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int error;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	error = mod_install(&ssc100_modlinkage);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (!error)
1257c478bd9Sstevel@tonic-gate 		(void) ddi_soft_state_init(&ssc100soft_statep,
12619397407SSherry Moore 		    sizeof (struct ssc100_unit), 1);
1277c478bd9Sstevel@tonic-gate 	return (error);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate int
_fini(void)1317c478bd9Sstevel@tonic-gate _fini(void)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	int error;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	error = mod_remove(&ssc100_modlinkage);
1367c478bd9Sstevel@tonic-gate 	if (!error)
1377c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&ssc100soft_statep);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	return (error);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1437c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	return (mod_info(&ssc100_modlinkage, modinfop));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static int
ssc100_open(dev_t * devp,int flags,int otyp,cred_t * credp)1497c478bd9Sstevel@tonic-gate ssc100_open(dev_t *devp, int flags, int otyp, cred_t *credp)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	struct ssc100_unit *unitp;
1547c478bd9Sstevel@tonic-gate 	int instance;
1557c478bd9Sstevel@tonic-gate 	int error = 0;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	instance = getminor(*devp);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (instance < 0) {
1607c478bd9Sstevel@tonic-gate 		return (ENXIO);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	unitp = (struct ssc100_unit *)
16419397407SSherry Moore 	    ddi_get_soft_state(ssc100soft_statep, instance);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
1677c478bd9Sstevel@tonic-gate 		return (ENXIO);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
1717c478bd9Sstevel@tonic-gate 		return (EINVAL);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->ssc100_mutex);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
1777c478bd9Sstevel@tonic-gate 		if (unitp->ssc100_oflag != 0) {
1787c478bd9Sstevel@tonic-gate 			error = EBUSY;
1797c478bd9Sstevel@tonic-gate 		} else {
1807c478bd9Sstevel@tonic-gate 			unitp->ssc100_oflag = FEXCL;
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	} else {
1837c478bd9Sstevel@tonic-gate 		if (unitp->ssc100_oflag == FEXCL) {
1847c478bd9Sstevel@tonic-gate 			error = EBUSY;
1857c478bd9Sstevel@tonic-gate 		} else {
1867c478bd9Sstevel@tonic-gate 			unitp->ssc100_oflag = FOPEN;
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->ssc100_mutex);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	return (error);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static int
ssc100_close(dev_t dev,int flags,int otyp,cred_t * credp)1967c478bd9Sstevel@tonic-gate ssc100_close(dev_t dev, int flags, int otyp, cred_t *credp)
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	struct ssc100_unit *unitp;
2017c478bd9Sstevel@tonic-gate 	int instance;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (instance < 0) {
2067c478bd9Sstevel@tonic-gate 		return (ENXIO);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	unitp = (struct ssc100_unit *)
20919397407SSherry Moore 	    ddi_get_soft_state(ssc100soft_statep, instance);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
2127c478bd9Sstevel@tonic-gate 		return (ENXIO);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->ssc100_mutex);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	unitp->ssc100_oflag = 0;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->ssc100_mutex);
2207c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
ssc100_common(struct ssc100_unit * unitp,uchar_t * byte,uchar_t input,int8_t flag)2247c478bd9Sstevel@tonic-gate ssc100_common(struct ssc100_unit *unitp, uchar_t *byte, uchar_t input,
2257c478bd9Sstevel@tonic-gate     int8_t flag)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	i2c_transfer_t		*i2c_tran_pointer;
2287c478bd9Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	(void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2c_tran_pointer,
2317c478bd9Sstevel@tonic-gate 	    1, 1, I2C_SLEEP);
2327c478bd9Sstevel@tonic-gate 	if (i2c_tran_pointer == NULL) {
2337c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_COMMON "
23419397407SSherry Moore 		    "i2c_tran_pointer not allocated",
23519397407SSherry Moore 		    unitp->ssc100_name));
2367c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	i2c_tran_pointer->i2c_flags = flag;
2407c478bd9Sstevel@tonic-gate 	if (flag != I2C_RD) {
2417c478bd9Sstevel@tonic-gate 		i2c_tran_pointer->i2c_wbuf[0] = input;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	err = i2c_transfer(unitp->ssc100_hdl, i2c_tran_pointer);
2457c478bd9Sstevel@tonic-gate 	if (err) {
2467c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_COMMON "
24719397407SSherry Moore 		    "i2c_transfer routine", unitp->ssc100_name));
2487c478bd9Sstevel@tonic-gate 	} else if (flag != I2C_WR) {
2497c478bd9Sstevel@tonic-gate 		*byte = i2c_tran_pointer->i2c_rbuf[0];
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->ssc100_hdl, i2c_tran_pointer);
2537c478bd9Sstevel@tonic-gate 	return (err);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static int
ssc100_get_reg(struct ssc100_unit * unitp,uchar_t * byte,uchar_t reg)2577c478bd9Sstevel@tonic-gate ssc100_get_reg(struct ssc100_unit *unitp, uchar_t *byte, uchar_t reg)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	err = ssc100_common(unitp, byte, reg, I2C_WR_RD);
2627c478bd9Sstevel@tonic-gate 	if (err) {
2637c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_GET_REG "
26419397407SSherry Moore 		    "i2c_common routine", unitp->ssc100_name));
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	return (err);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static int
ssc100_get(struct ssc100_unit * unitp,uchar_t * byte)2707c478bd9Sstevel@tonic-gate ssc100_get(struct ssc100_unit *unitp, uchar_t *byte)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	err = ssc100_common(unitp, byte, 0, I2C_RD);
2757c478bd9Sstevel@tonic-gate 	if (err) {
2767c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_GET "
27719397407SSherry Moore 		    "i2c_common routine", unitp->ssc100_name));
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 	return (err);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static int
ssc100_set(struct ssc100_unit * unitp,uchar_t byte)2837c478bd9Sstevel@tonic-gate ssc100_set(struct ssc100_unit *unitp, uchar_t byte)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	int			err = I2C_SUCCESS;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	err = ssc100_common(unitp, NULL, byte, I2C_WR);
2887c478bd9Sstevel@tonic-gate 	if (err) {
2897c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Failed in SSC100_SET "
29019397407SSherry Moore 		    "i2c_common routine", unitp->ssc100_name));
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	return (err);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static int
ssc100_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2967c478bd9Sstevel@tonic-gate ssc100_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
297*b10a7a5cSToomas Soome     cred_t *credp, int *rvalp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp, rvalp))
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	struct ssc100_unit	*unitp;
3027c478bd9Sstevel@tonic-gate 	int		instance;
3037c478bd9Sstevel@tonic-gate 	int			err = 0;
3047c478bd9Sstevel@tonic-gate 	i2c_bit_t		ioctl_bit;
3057c478bd9Sstevel@tonic-gate 	i2c_port_t		ioctl_port;
3067c478bd9Sstevel@tonic-gate 	i2c_reg_t ioctl_reg;
3077c478bd9Sstevel@tonic-gate 	uchar_t			byte;
3087c478bd9Sstevel@tonic-gate 
309*b10a7a5cSToomas Soome 	if (arg == (intptr_t)NULL) {
3107c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "SSC100: ioctl: arg passed in to ioctl "
31119397407SSherry Moore 		    "= NULL"));
3127c478bd9Sstevel@tonic-gate 		err = EINVAL;
3137c478bd9Sstevel@tonic-gate 		return (err);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	instance = getminor(dev);
3177c478bd9Sstevel@tonic-gate 	unitp = (struct ssc100_unit *)
31819397407SSherry Moore 	    ddi_get_soft_state(ssc100soft_statep, instance);
3197c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
3207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "SSC100: ioctl: unitp not filled");
3217c478bd9Sstevel@tonic-gate 		return (ENOMEM);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	mutex_enter(&unitp->ssc100_mutex);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	switch (cmd) {
3277c478bd9Sstevel@tonic-gate 	case I2C_GET_PORT:
3287c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
32919397407SSherry Moore 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3307c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
33119397407SSherry Moore 			    " ddi_copyin routine", unitp->ssc100_name));
3327c478bd9Sstevel@tonic-gate 			err = EFAULT;
3337c478bd9Sstevel@tonic-gate 			break;
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		err = ssc100_get(unitp, &byte);
3377c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3387c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_PORT"
33919397407SSherry Moore 			    " ssc100_get routine", unitp->ssc100_name));
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		ioctl_port.value = byte;
3447c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_port, (caddr_t)arg,
34519397407SSherry Moore 		    sizeof (i2c_port_t), mode) != DDI_SUCCESS) {
3467c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_PORT "
34719397407SSherry Moore 			    "ddi_copyout routine", unitp->ssc100_name));
3487c478bd9Sstevel@tonic-gate 			err = EFAULT;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: contains %x", unitp->ssc100_name,
35219397407SSherry Moore 		    byte));
3537c478bd9Sstevel@tonic-gate 		break;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	case I2C_SET_PORT:
3567c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_port,
35719397407SSherry Moore 		    sizeof (uint8_t), mode) != DDI_SUCCESS) {
3587c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
35919397407SSherry Moore 			    "ddi_cpoyin routine", unitp->ssc100_name));
3607c478bd9Sstevel@tonic-gate 			err = EFAULT;
3617c478bd9Sstevel@tonic-gate 			break;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		err = ssc100_set(unitp, ioctl_port.value);
3657c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3667c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_PORT"
36719397407SSherry Moore 			    " ssc100_set routine", unitp->ssc100_name));
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		break;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	case I2C_GET_BIT:
3737c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
37419397407SSherry Moore 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
3757c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
37619397407SSherry Moore 			    " ddi_copyin routine", unitp->ssc100_name));
3777c478bd9Sstevel@tonic-gate 			err = EFAULT;
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
381f47a9c50Smathue 		if (ioctl_bit.bit_num > 7) {
3827c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: In I2C_GET_BIT bit num"
38319397407SSherry Moore 			    " was not between 0 and 7",
38419397407SSherry Moore 			    unitp->ssc100_name));
3857c478bd9Sstevel@tonic-gate 			err = EIO;
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		err = ssc100_get(unitp, &byte);
3907c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
3917c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_GET_BIT"
39219397407SSherry Moore 			    " ssc100_get routine", unitp->ssc100_name));
3937c478bd9Sstevel@tonic-gate 			break;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x",
39719397407SSherry Moore 		    unitp->ssc100_name, byte));
3987c478bd9Sstevel@tonic-gate 		ioctl_bit.bit_value = (boolean_t)SSC100_BIT_READ_MASK(byte,
3997c478bd9Sstevel@tonic-gate 		    ioctl_bit.bit_num);
4007c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte now contains %x",
40119397407SSherry Moore 		    unitp->ssc100_name, byte));
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_bit, (caddr_t)arg,
40419397407SSherry Moore 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4057c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_BIT"
40619397407SSherry Moore 			    " ddi_copyout routine", unitp->ssc100_name));
4077c478bd9Sstevel@tonic-gate 			err = EFAULT;
4087c478bd9Sstevel@tonic-gate 		}
4097c478bd9Sstevel@tonic-gate 		break;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	case I2C_SET_BIT:
4127c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_bit,
41319397407SSherry Moore 		    sizeof (i2c_bit_t), mode) != DDI_SUCCESS) {
4147c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_SET_BIT"
41519397407SSherry Moore 			    " ddi_copyin routine", unitp->ssc100_name));
4167c478bd9Sstevel@tonic-gate 			err = EFAULT;
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 
420f47a9c50Smathue 		if (ioctl_bit.bit_num > 7) {
4217c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: I2C_SET_BIT: bit_num sent"
42219397407SSherry Moore 			    " in was not between 0 and 7",
42319397407SSherry Moore 			    unitp->ssc100_name));
4247c478bd9Sstevel@tonic-gate 			err = EIO;
4257c478bd9Sstevel@tonic-gate 			break;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		err = ssc100_get(unitp, &byte);
4297c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4307c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
43119397407SSherry Moore 			    " ssc100_get routine", unitp->ssc100_name));
4327c478bd9Sstevel@tonic-gate 			break;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte returned from device is %x",
43619397407SSherry Moore 		    unitp->ssc100_name, byte));
4377c478bd9Sstevel@tonic-gate 		byte = SSC100_BIT_WRITE_MASK(byte, ioctl_bit.bit_num,
4387c478bd9Sstevel@tonic-gate 		    ioctl_bit.bit_value);
4397c478bd9Sstevel@tonic-gate 		D1CMN_ERR((CE_NOTE, "%s: byte after shifting is %x",
44019397407SSherry Moore 		    unitp->ssc100_name, byte));
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		err = ssc100_set(unitp, byte);
4437c478bd9Sstevel@tonic-gate 		if (err != I2C_SUCCESS) {
4447c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in the I2C_SET_BIT"
44519397407SSherry Moore 			    " ssc100_set routine", unitp->ssc100_name));
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	case I2C_GET_REG:
4517c478bd9Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&ioctl_reg,
45219397407SSherry Moore 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
4537c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_REG "
4547c478bd9Sstevel@tonic-gate 			    "ddi_copyin routine", unitp->ssc100_name));
4557c478bd9Sstevel@tonic-gate 			err = EFAULT;
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		err = ssc100_get_reg(unitp, &byte, ioctl_reg.reg_num);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		ioctl_reg.reg_value = byte;
4627c478bd9Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&ioctl_reg, (caddr_t)arg,
46319397407SSherry Moore 		    sizeof (i2c_reg_t), mode) != DDI_SUCCESS) {
4647c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in I2C_GET_REG "
4657c478bd9Sstevel@tonic-gate 			    "ddi_copyout routine", unitp->ssc100_name));
4667c478bd9Sstevel@tonic-gate 			err = EFAULT;
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 		break;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	default:
4717c478bd9Sstevel@tonic-gate 		D2CMN_ERR((CE_WARN, "%s: Invalid IOCTL cmd: %x",
47219397407SSherry Moore 		    unitp->ssc100_name, cmd));
4737c478bd9Sstevel@tonic-gate 		err = EINVAL;
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	mutex_exit(&unitp->ssc100_mutex);
4777c478bd9Sstevel@tonic-gate 	return (err);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
ssc100_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)4817c478bd9Sstevel@tonic-gate ssc100_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	switch (cmd) {
4847c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
4857c478bd9Sstevel@tonic-gate 		return (ssc100_do_attach(dip));
4867c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
4877c478bd9Sstevel@tonic-gate 		return (ssc100_do_resume());
4887c478bd9Sstevel@tonic-gate 	default:
4897c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate static int
ssc100_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4947c478bd9Sstevel@tonic-gate ssc100_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	switch (cmd) {
4977c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4987c478bd9Sstevel@tonic-gate 		return (ssc100_do_detach(dip));
4997c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
5007c478bd9Sstevel@tonic-gate 		return (ssc100_do_suspend());
5017c478bd9Sstevel@tonic-gate 	default:
5027c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate static int
ssc100_do_attach(dev_info_t * dip)5077c478bd9Sstevel@tonic-gate ssc100_do_attach(dev_info_t *dip)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct ssc100_unit *unitp;
5107c478bd9Sstevel@tonic-gate 	int instance;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(ssc100soft_statep, instance) != 0) {
5157c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: failed to zalloc softstate",
51619397407SSherry Moore 		    ddi_get_name(dip), instance);
5177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ssc100soft_statep, instance);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
5237c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: unitp not filled",
52419397407SSherry Moore 		    ddi_get_name(dip), instance);
5257c478bd9Sstevel@tonic-gate 		return (ENOMEM);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	(void) snprintf(unitp->ssc100_name, sizeof (unitp->ssc100_name),
52919397407SSherry Moore 	    "%s%d", ddi_node_name(dip), instance);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "ssc100", S_IFCHR, instance,
532*b10a7a5cSToomas Soome 	    "ddi_i2c:ioexp", 0) == DDI_FAILURE) {
5337c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for "
53419397407SSherry Moore 		    "%s", unitp->ssc100_name, "ssc100");
5357c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ssc100soft_statep, instance);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/*
5417c478bd9Sstevel@tonic-gate 	 * If we had different sizes in the future, this could be read
5427c478bd9Sstevel@tonic-gate 	 * from a property.
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	unitp->ssc100_size = SSC100_SIZE;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
54719397407SSherry Moore 	    DDI_PROP_CANSLEEP, "size",
54819397407SSherry Moore 	    (caddr_t)&unitp->ssc100_size, sizeof (unitp->ssc100_size));
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->ssc100_hdl) != I2C_SUCCESS) {
5517c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
5527c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ssc100soft_statep, instance);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	mutex_init(&unitp->ssc100_mutex, NULL, MUTEX_DRIVER, NULL);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate static int
ssc100_do_resume()5637c478bd9Sstevel@tonic-gate ssc100_do_resume()
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (ret);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate static int
ssc100_do_suspend()5717c478bd9Sstevel@tonic-gate ssc100_do_suspend()
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	int ret = DDI_SUCCESS;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	return (ret);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate static int
ssc100_do_detach(dev_info_t * dip)5797c478bd9Sstevel@tonic-gate ssc100_do_detach(dev_info_t *dip)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	struct ssc100_unit *unitp;
5827c478bd9Sstevel@tonic-gate 	int instance;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	unitp = ddi_get_soft_state(ssc100soft_statep, instance);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	i2c_client_unregister(unitp->ssc100_hdl);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	mutex_destroy(&unitp->ssc100_mutex);
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	ddi_soft_state_free(ssc100soft_statep, instance);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate static int
ssc100_read(dev_t dev,struct uio * uiop,cred_t * cred_p)6017c478bd9Sstevel@tonic-gate ssc100_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	return (ssc100_io(dev, uiop, B_READ));
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate static int
ssc100_write(dev_t dev,struct uio * uiop,cred_t * cred_p)6097c478bd9Sstevel@tonic-gate ssc100_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	return (ssc100_io(dev, uiop, B_WRITE));
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate static int
ssc100_io(dev_t dev,struct uio * uiop,int rw)6177c478bd9Sstevel@tonic-gate ssc100_io(dev_t dev, struct uio *uiop, int rw)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	struct ssc100_unit *unitp;
6207c478bd9Sstevel@tonic-gate 	int instance = getminor(dev);
6217c478bd9Sstevel@tonic-gate 	int	ssc100_addr;
6227c478bd9Sstevel@tonic-gate 	int	bytes_to_rw;
6237c478bd9Sstevel@tonic-gate 	int	err = 0;
6247c478bd9Sstevel@tonic-gate 	int	current_xfer_len;
6257c478bd9Sstevel@tonic-gate 	i2c_transfer_t *i2ctp = NULL;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if (instance < 0) {
6287c478bd9Sstevel@tonic-gate 		return (ENXIO);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	unitp = (struct ssc100_unit *)
63219397407SSherry Moore 	    ddi_get_soft_state(ssc100soft_statep, instance);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (unitp == NULL) {
6367c478bd9Sstevel@tonic-gate 		return (ENXIO);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (uiop->uio_offset >= unitp->ssc100_size) {
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * Exceeded ssc100 size.
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		if (rw == B_WRITE) {
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 			return (ENOSPC);
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		return (0);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	ssc100_addr = uiop->uio_offset;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid == 0) {
6537c478bd9Sstevel@tonic-gate 		return (0);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	bytes_to_rw = min(uiop->uio_resid,
65719397407SSherry Moore 	    unitp->ssc100_size - uiop->uio_offset);
6587c478bd9Sstevel@tonic-gate 	current_xfer_len = bytes_to_rw;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (rw == B_WRITE) {
6617c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2ctp,
66219397407SSherry Moore 		    current_xfer_len+1, 0, I2C_SLEEP);
6637c478bd9Sstevel@tonic-gate 		if (i2ctp == NULL) {
6647c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
66519397407SSherry Moore 			    "i2c_tran_pointer not allocated",
66619397407SSherry Moore 			    unitp->ssc100_name));
6677c478bd9Sstevel@tonic-gate 			return (ENOMEM);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		i2ctp->i2c_version = I2C_XFER_REV;
6707c478bd9Sstevel@tonic-gate 		i2ctp->i2c_flags = I2C_WR;
6717c478bd9Sstevel@tonic-gate 		i2ctp->i2c_wbuf[0] = (uchar_t)ssc100_addr;
6727c478bd9Sstevel@tonic-gate 		if ((err = uiomove(&i2ctp->i2c_wbuf[1], current_xfer_len,
67319397407SSherry Moore 		    UIO_WRITE, uiop)) != 0) {
6747c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
67519397407SSherry Moore 			    "uiomove failed", unitp->ssc100_name));
6767c478bd9Sstevel@tonic-gate 			goto end;
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		if ((err = i2c_transfer(unitp->ssc100_hdl, i2ctp)) !=
6807c478bd9Sstevel@tonic-gate 		    I2C_SUCCESS) {
6817c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io WRITE "
68219397407SSherry Moore 			    "i2c_transfer failed", unitp->ssc100_name));
6837c478bd9Sstevel@tonic-gate 			goto end;
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	} else {
6867c478bd9Sstevel@tonic-gate 		/*
6877c478bd9Sstevel@tonic-gate 		 * SSC100 read.  We need to first write out the address
6887c478bd9Sstevel@tonic-gate 		 * that we wish to read.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		(void) i2c_transfer_alloc(unitp->ssc100_hdl, &i2ctp, 1,
6917c478bd9Sstevel@tonic-gate 		    current_xfer_len, I2C_SLEEP);
6927c478bd9Sstevel@tonic-gate 		if (i2ctp == NULL) {
6937c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
69419397407SSherry Moore 			    "i2c_tran_pointer not allocated",
69519397407SSherry Moore 			    unitp->ssc100_name));
6967c478bd9Sstevel@tonic-gate 			return (ENOMEM);
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 		i2ctp->i2c_version = I2C_XFER_REV;
6997c478bd9Sstevel@tonic-gate 		i2ctp->i2c_wbuf[0] = (uchar_t)ssc100_addr;
7007c478bd9Sstevel@tonic-gate 		i2ctp->i2c_flags = I2C_WR_RD;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if ((err = i2c_transfer(unitp->ssc100_hdl, i2ctp)) !=
7037c478bd9Sstevel@tonic-gate 		    I2C_SUCCESS) {
7047c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
70519397407SSherry Moore 			    "i2c_transfer failed", unitp->ssc100_name));
7067c478bd9Sstevel@tonic-gate 			goto end;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		if ((err = uiomove(i2ctp->i2c_rbuf, current_xfer_len,
71019397407SSherry Moore 		    UIO_READ, uiop)) != 0) {
7117c478bd9Sstevel@tonic-gate 			D2CMN_ERR((CE_WARN, "%s: Failed in ssc100_io READ "
71219397407SSherry Moore 			    "uiomove failed", unitp->ssc100_name));
7137c478bd9Sstevel@tonic-gate 			goto end;
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate end:
7187c478bd9Sstevel@tonic-gate 	i2c_transfer_free(unitp->ssc100_hdl, i2ctp);
7197c478bd9Sstevel@tonic-gate 	return (err);
7207c478bd9Sstevel@tonic-gate }
721