xref: /onnv-gate/usr/src/uts/sun4u/io/i2c/clients/seeprom.c (revision 7656:2621e50fdf4a)
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
5*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.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 /*
22*7656SSherry.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 
270Sstevel@tonic-gate #include <sys/stat.h>
280Sstevel@tonic-gate #include <sys/modctl.h>
290Sstevel@tonic-gate #include <sys/open.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/ddi.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/file.h>
360Sstevel@tonic-gate #include <sys/note.h>
370Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h>
380Sstevel@tonic-gate #include <sys/i2c/clients/seeprom_impl.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * cb ops
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate static int seeprom_open(dev_t *, int, int, cred_t *);
440Sstevel@tonic-gate static int seeprom_close(dev_t, int, int, cred_t *);
450Sstevel@tonic-gate static int seeprom_read(dev_t, struct uio *, cred_t *);
460Sstevel@tonic-gate static int seeprom_write(dev_t, struct uio *, cred_t *);
470Sstevel@tonic-gate static int seeprom_io(dev_t, struct uio *, int);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * dev ops
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static int seeprom_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
530Sstevel@tonic-gate static int seeprom_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
540Sstevel@tonic-gate static int seeprom_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static struct cb_ops seeprom_cbops = {
570Sstevel@tonic-gate 	seeprom_open,			/* open */
580Sstevel@tonic-gate 	seeprom_close,			/* close */
590Sstevel@tonic-gate 	nodev,				/* strategy */
600Sstevel@tonic-gate 	nodev,				/* print */
610Sstevel@tonic-gate 	nodev,				/* dump */
620Sstevel@tonic-gate 	seeprom_read,			/* read */
630Sstevel@tonic-gate 	seeprom_write,			/* write */
640Sstevel@tonic-gate 	nodev,				/* ioctl */
650Sstevel@tonic-gate 	nodev,				/* devmap */
660Sstevel@tonic-gate 	nodev,				/* mmap */
670Sstevel@tonic-gate 	nodev,				/* segmap */
680Sstevel@tonic-gate 	nochpoll,			/* poll */
690Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
700Sstevel@tonic-gate 	NULL,				/* streamtab */
710Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
720Sstevel@tonic-gate 	CB_REV,				/* rev */
730Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
740Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate static struct dev_ops seeprom_ops = {
780Sstevel@tonic-gate 	DEVO_REV,
790Sstevel@tonic-gate 	0,
800Sstevel@tonic-gate 	seeprom_info,
810Sstevel@tonic-gate 	nulldev,
820Sstevel@tonic-gate 	nulldev,
830Sstevel@tonic-gate 	seeprom_attach,
840Sstevel@tonic-gate 	seeprom_detach,
850Sstevel@tonic-gate 	nodev,
860Sstevel@tonic-gate 	&seeprom_cbops,
8791Sec158148 	NULL,
88*7656SSherry.Moore@Sun.COM 	nulldev,
89*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static struct modldrv seeprom_modldrv = {
930Sstevel@tonic-gate 	&mod_driverops,	 /* type of module - driver */
94*7656SSherry.Moore@Sun.COM 	"I2C serial EEPROM device driver",
950Sstevel@tonic-gate 	&seeprom_ops,
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate static struct modlinkage seeprom_modlinkage = {
990Sstevel@tonic-gate 	MODREV_1,
1000Sstevel@tonic-gate 	&seeprom_modldrv,
1010Sstevel@tonic-gate 	0
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * globals
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static void *seepromsoft_statep;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate int
_init(void)1110Sstevel@tonic-gate _init(void)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	int error;
1140Sstevel@tonic-gate 
11591Sec158148 	if ((error = ddi_soft_state_init(&seepromsoft_statep,
11691Sec158148 	    sizeof (struct seepromunit), 1)) != 0)
11791Sec158148 		return (error);
11891Sec158148 
11991Sec158148 	if ((error = mod_install(&seeprom_modlinkage)) != 0) {
12091Sec158148 		ddi_soft_state_fini(&seepromsoft_statep);
12191Sec158148 		return (error);
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 
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(&seeprom_modlinkage);
1330Sstevel@tonic-gate 	if (error == 0) {
1340Sstevel@tonic-gate 		ddi_soft_state_fini(&seepromsoft_statep);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (error);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1410Sstevel@tonic-gate _info(struct modinfo *modinfop)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	return (mod_info(&seeprom_modlinkage, modinfop));
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static int
seeprom_do_attach(dev_info_t * dip)1470Sstevel@tonic-gate seeprom_do_attach(dev_info_t *dip)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	struct seepromunit *unitp;
1500Sstevel@tonic-gate 	int instance;
1510Sstevel@tonic-gate 	dev_t	dev;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(seepromsoft_statep, instance) != 0) {
1560Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s_%d: failed to zalloc softstate",
157*7656SSherry.Moore@Sun.COM 		    ddi_node_name(dip), instance);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		return (DDI_FAILURE);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	unitp = ddi_get_soft_state(seepromsoft_statep, instance);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	unitp->seeprom_dip = dip;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	(void) snprintf(unitp->seeprom_name, sizeof (unitp->seeprom_name),
167*7656SSherry.Moore@Sun.COM 	    "%s%d", ddi_driver_name(dip), instance);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, ddi_node_name(dip), S_IFCHR,
1700Sstevel@tonic-gate 	    instance, SEEPROM_NODE_TYPE, NULL) == DDI_FAILURE) {
1710Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed for '%s'",
1720Sstevel@tonic-gate 		    unitp->seeprom_name, ddi_node_name(dip));
1730Sstevel@tonic-gate 		ddi_soft_state_free(seepromsoft_statep, instance);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		return (DDI_FAILURE);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (i2c_client_register(dip, &unitp->seeprom_hdl) != I2C_SUCCESS) {
1790Sstevel@tonic-gate 		cmn_err(CE_WARN, "i2c_client_register failed\n");
1800Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
1810Sstevel@tonic-gate 		ddi_soft_state_free(seepromsoft_statep, instance);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		return (DDI_FAILURE);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (strcmp(ddi_binding_name(dip), "i2c-at34c02") == 0) {
1870Sstevel@tonic-gate 		unitp->seeprom_addrsize = AT34C02_ADDRSIZE;
1880Sstevel@tonic-gate 		unitp->seeprom_memsize = AT34C02_MEMSIZE;
1890Sstevel@tonic-gate 		unitp->seeprom_pagesize = AT34C02_PAGESIZE;
1900Sstevel@tonic-gate 		unitp->seeprom_pagemask = AT34C02_PAGEMASK;
1910Sstevel@tonic-gate 	} else {
1920Sstevel@tonic-gate 		/*
1930Sstevel@tonic-gate 		 * Default is i2c-at24c64
1940Sstevel@tonic-gate 		 */
1950Sstevel@tonic-gate 		unitp->seeprom_addrsize = AT24C64_ADDRSIZE;
1960Sstevel@tonic-gate 		unitp->seeprom_memsize = AT24C64_MEMSIZE;
1970Sstevel@tonic-gate 		unitp->seeprom_pagesize = AT24C64_PAGESIZE;
1980Sstevel@tonic-gate 		unitp->seeprom_pagemask = AT24C64_PAGEMASK;
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 	dev = makedevice(DDI_MAJOR_T_UNKNOWN, instance);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	(void) ddi_prop_create(dev, dip, DDI_PROP_CANSLEEP, "size",
2030Sstevel@tonic-gate 	    (caddr_t)&unitp->seeprom_memsize, sizeof (unitp->seeprom_memsize));
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	mutex_init(&unitp->seeprom_mutex, NULL, MUTEX_DRIVER, NULL);
2060Sstevel@tonic-gate 	cv_init(&unitp->seeprom_cv, NULL, CV_DRIVER, NULL);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	return (DDI_SUCCESS);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static int
seeprom_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2120Sstevel@tonic-gate seeprom_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	switch (cmd) {
2150Sstevel@tonic-gate 	case DDI_ATTACH:
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 		return (seeprom_do_attach(dip));
2180Sstevel@tonic-gate 	case DDI_RESUME:
2190Sstevel@tonic-gate 		/*
2200Sstevel@tonic-gate 		 * No state to restore.
2210Sstevel@tonic-gate 		 */
2220Sstevel@tonic-gate 		return (DDI_SUCCESS);
2230Sstevel@tonic-gate 	default:
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 		return (DDI_FAILURE);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static int
seeprom_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2300Sstevel@tonic-gate seeprom_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	_NOTE(ARGUNUSED(dip))
2330Sstevel@tonic-gate 	struct seepromunit *unitp;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	switch (infocmd) {
2360Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2370Sstevel@tonic-gate 		unitp = ddi_get_soft_state(seepromsoft_statep,
2380Sstevel@tonic-gate 		    getminor((dev_t)arg));
2390Sstevel@tonic-gate 		if (unitp == NULL) {
2400Sstevel@tonic-gate 
24191Sec158148 			return (DDI_FAILURE);
2420Sstevel@tonic-gate 		}
2430Sstevel@tonic-gate 		*result = (void *)unitp->seeprom_dip;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		return (DDI_SUCCESS);
2460Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
247946Smathue 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		return (DDI_SUCCESS);
2500Sstevel@tonic-gate 	default:
2510Sstevel@tonic-gate 		return (DDI_FAILURE);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static int
seeprom_do_detach(dev_info_t * dip)2570Sstevel@tonic-gate seeprom_do_detach(dev_info_t *dip)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	struct seepromunit *unitp;
2600Sstevel@tonic-gate 	int instance;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2630Sstevel@tonic-gate 	unitp = ddi_get_soft_state(seepromsoft_statep, instance);
2640Sstevel@tonic-gate 	i2c_client_unregister(unitp->seeprom_hdl);
2650Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2660Sstevel@tonic-gate 	mutex_destroy(&unitp->seeprom_mutex);
2670Sstevel@tonic-gate 	cv_destroy(&unitp->seeprom_cv);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	ddi_soft_state_free(seepromsoft_statep, instance);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	return (DDI_SUCCESS);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static int
seeprom_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2750Sstevel@tonic-gate seeprom_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	switch (cmd) {
2780Sstevel@tonic-gate 	case DDI_DETACH:
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		return (seeprom_do_detach(dip));
2810Sstevel@tonic-gate 	case DDI_SUSPEND:
2820Sstevel@tonic-gate 		/*
2830Sstevel@tonic-gate 		 * No state to save.  IO will be blocked by nexus.
2840Sstevel@tonic-gate 		 */
2850Sstevel@tonic-gate 		return (DDI_SUCCESS);
2860Sstevel@tonic-gate 	default:
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		return (DDI_FAILURE);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static int
seeprom_open(dev_t * devp,int flags,int otyp,cred_t * credp)2930Sstevel@tonic-gate seeprom_open(dev_t *devp, int flags, int otyp, cred_t *credp)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	_NOTE(ARGUNUSED(credp))
2960Sstevel@tonic-gate 	struct seepromunit *unitp;
2970Sstevel@tonic-gate 	int instance;
2980Sstevel@tonic-gate 	int err = 0;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if (otyp != OTYP_CHR) {
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		return (EINVAL);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	instance = getminor(*devp);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	unitp = (struct seepromunit *)
308*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(seepromsoft_statep, instance);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (unitp == NULL) {
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		return (ENXIO);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	mutex_enter(&unitp->seeprom_mutex);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (flags & FEXCL) {
3180Sstevel@tonic-gate 		if (unitp->seeprom_oflag != 0) {
3190Sstevel@tonic-gate 			err = EBUSY;
3200Sstevel@tonic-gate 		} else {
3210Sstevel@tonic-gate 			unitp->seeprom_oflag = FEXCL;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 	} else {
3240Sstevel@tonic-gate 		if (unitp->seeprom_oflag == FEXCL) {
3250Sstevel@tonic-gate 			err = EBUSY;
3260Sstevel@tonic-gate 		} else {
3270Sstevel@tonic-gate 			unitp->seeprom_oflag = FOPEN;
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	mutex_exit(&unitp->seeprom_mutex);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	return (err);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static int
seeprom_close(dev_t dev,int flags,int otyp,cred_t * credp)3370Sstevel@tonic-gate seeprom_close(dev_t dev, int flags, int otyp, cred_t *credp)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	_NOTE(ARGUNUSED(flags, otyp, credp))
3400Sstevel@tonic-gate 	struct seepromunit *unitp;
3410Sstevel@tonic-gate 	int instance;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	instance = getminor(dev);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	unitp = (struct seepromunit *)
346*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(seepromsoft_statep, instance);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (unitp == NULL) {
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		return (ENXIO);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	mutex_enter(&unitp->seeprom_mutex);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	unitp->seeprom_oflag = 0;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	mutex_exit(&unitp->seeprom_mutex);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	return (DDI_SUCCESS);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate static int
seeprom_read(dev_t dev,struct uio * uiop,cred_t * cred_p)3630Sstevel@tonic-gate seeprom_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
3660Sstevel@tonic-gate 	return (seeprom_io(dev, uiop, B_READ));
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate static int
seeprom_write(dev_t dev,struct uio * uiop,cred_t * cred_p)3700Sstevel@tonic-gate seeprom_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
3710Sstevel@tonic-gate {
3720Sstevel@tonic-gate 	_NOTE(ARGUNUSED(cred_p))
3730Sstevel@tonic-gate 	return (seeprom_io(dev, uiop, B_WRITE));
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static int
seeprom_io(dev_t dev,struct uio * uiop,int rw)3770Sstevel@tonic-gate seeprom_io(dev_t dev, struct uio *uiop, int rw)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	struct seepromunit *unitp;
3800Sstevel@tonic-gate 	int instance = getminor(dev);
3810Sstevel@tonic-gate 	int	seeprom_addr;
3820Sstevel@tonic-gate 	int	bytes_to_rw;
3830Sstevel@tonic-gate 	int	err = 0;
3840Sstevel@tonic-gate 	int	current_xfer_len;
3850Sstevel@tonic-gate 	int	actual_data_xfer;
3860Sstevel@tonic-gate 	i2c_transfer_t *i2ctp = NULL;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	unitp = (struct seepromunit *)
389*7656SSherry.Moore@Sun.COM 	    ddi_get_soft_state(seepromsoft_statep, instance);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (unitp == NULL) {
3930Sstevel@tonic-gate 		return (ENXIO);
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (uiop->uio_offset >= unitp->seeprom_memsize) {
3970Sstevel@tonic-gate 		/*
3980Sstevel@tonic-gate 		 * Exceeded seeprom size.
3990Sstevel@tonic-gate 		 */
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		return (ENXIO);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	seeprom_addr = uiop->uio_offset;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	if (uiop->uio_resid == 0) {
4070Sstevel@tonic-gate 		return (0);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	bytes_to_rw = min(uiop->uio_resid,
411*7656SSherry.Moore@Sun.COM 	    unitp->seeprom_memsize - uiop->uio_offset);
4120Sstevel@tonic-gate 	/*
4130Sstevel@tonic-gate 	 * Serialize access here to prevent a transaction starting
4140Sstevel@tonic-gate 	 * until after 20 ms delay if last operation was a write.
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 	mutex_enter(&unitp->seeprom_mutex);
4170Sstevel@tonic-gate 	while ((unitp->seeprom_flags & SEEPROM_BUSY) == SEEPROM_BUSY) {
4180Sstevel@tonic-gate 		if (cv_wait_sig(&unitp->seeprom_cv,
419*7656SSherry.Moore@Sun.COM 		    &unitp->seeprom_mutex) <= 0) {
4200Sstevel@tonic-gate 			mutex_exit(&unitp->seeprom_mutex);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 			return (EINTR);
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	unitp->seeprom_flags |= SEEPROM_BUSY;
4260Sstevel@tonic-gate 	mutex_exit(&unitp->seeprom_mutex);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	while ((bytes_to_rw != 0) && (err == 0)) {
4290Sstevel@tonic-gate 		current_xfer_len = min(bytes_to_rw, unitp->seeprom_pagesize -
4300Sstevel@tonic-gate 		    (seeprom_addr & unitp->seeprom_pagemask));
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		if (rw == B_WRITE) {
4330Sstevel@tonic-gate 			if (i2ctp == NULL) {
4340Sstevel@tonic-gate 				(void) i2c_transfer_alloc(unitp->seeprom_hdl,
4350Sstevel@tonic-gate 				    &i2ctp,
4360Sstevel@tonic-gate 				    unitp->seeprom_addrsize + current_xfer_len,
4370Sstevel@tonic-gate 				    0,
4380Sstevel@tonic-gate 				    I2C_SLEEP);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 				if ((err = uiomove(&i2ctp->i2c_wbuf[
4410Sstevel@tonic-gate 				    unitp->seeprom_addrsize],
4420Sstevel@tonic-gate 				    current_xfer_len, UIO_WRITE, uiop)) != 0) {
4430Sstevel@tonic-gate 					i2c_transfer_free(unitp->seeprom_hdl,
4440Sstevel@tonic-gate 					    i2ctp);
4450Sstevel@tonic-gate 					break;
4460Sstevel@tonic-gate 				}
4470Sstevel@tonic-gate 				i2ctp->i2c_version = I2C_XFER_REV;
4480Sstevel@tonic-gate 				i2ctp->i2c_flags = I2C_WR;
4490Sstevel@tonic-gate 			} else {
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 				/*
4520Sstevel@tonic-gate 				 * not all bytes were sent in previous attempt.
4530Sstevel@tonic-gate 				 * Adjust the write pointer to the unsent data.
4540Sstevel@tonic-gate 				 */
4550Sstevel@tonic-gate 				/*LINTED*/
4560Sstevel@tonic-gate 				i2ctp->i2c_wbuf += actual_data_xfer;
4570Sstevel@tonic-gate 				/*LINTED*/
4580Sstevel@tonic-gate 				i2ctp->i2c_wlen -= actual_data_xfer;
4590Sstevel@tonic-gate 			}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 			if (unitp->seeprom_addrsize == 2) {
4620Sstevel@tonic-gate 				i2ctp->i2c_wbuf[0] = (seeprom_addr >> 8);
4630Sstevel@tonic-gate 				i2ctp->i2c_wbuf[1] = (uchar_t)seeprom_addr;
4640Sstevel@tonic-gate 			} else {
4650Sstevel@tonic-gate 				i2ctp->i2c_wbuf[0] = (uchar_t)seeprom_addr;
4660Sstevel@tonic-gate 			}
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 			if ((err = i2c_transfer(unitp->seeprom_hdl, i2ctp)) !=
4690Sstevel@tonic-gate 			    I2C_SUCCESS) {
4700Sstevel@tonic-gate 				i2c_transfer_free(unitp->seeprom_hdl, i2ctp);
4710Sstevel@tonic-gate 				break;
4720Sstevel@tonic-gate 			}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 			actual_data_xfer = i2ctp->i2c_wlen -
4750Sstevel@tonic-gate 			    i2ctp->i2c_w_resid - unitp->seeprom_addrsize;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 			if (i2ctp->i2c_w_resid == 0) {
4780Sstevel@tonic-gate 				i2c_transfer_free(unitp->seeprom_hdl, i2ctp);
4790Sstevel@tonic-gate 				i2ctp = NULL;
4800Sstevel@tonic-gate 			}
4810Sstevel@tonic-gate 			/*
4820Sstevel@tonic-gate 			 * 20 ms(20000 Microsec) delay is required before
4830Sstevel@tonic-gate 			 * issuing another transaction.  This enforces that
4840Sstevel@tonic-gate 			 * wait.
4850Sstevel@tonic-gate 			 */
4860Sstevel@tonic-gate 			delay(drv_usectohz(20000));
4870Sstevel@tonic-gate 		} else {
4880Sstevel@tonic-gate 			/*
4890Sstevel@tonic-gate 			 * SEEPROM read.  First write out the address to read.
4900Sstevel@tonic-gate 			 */
4910Sstevel@tonic-gate 			(void) i2c_transfer_alloc(unitp->seeprom_hdl, &i2ctp,
4920Sstevel@tonic-gate 			    unitp->seeprom_addrsize, current_xfer_len,
4930Sstevel@tonic-gate 			    I2C_SLEEP);
4940Sstevel@tonic-gate 			i2ctp->i2c_version = I2C_XFER_REV;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 			if (unitp->seeprom_addrsize == 2) {
4970Sstevel@tonic-gate 				i2ctp->i2c_wbuf[0] = (seeprom_addr >> 8);
4980Sstevel@tonic-gate 				i2ctp->i2c_wbuf[1] = (uchar_t)seeprom_addr;
4990Sstevel@tonic-gate 			} else {
5000Sstevel@tonic-gate 				i2ctp->i2c_wbuf[0] = (uchar_t)seeprom_addr;
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 			i2ctp->i2c_flags = I2C_WR_RD;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 			if ((err = i2c_transfer(unitp->seeprom_hdl, i2ctp)) !=
5060Sstevel@tonic-gate 			    I2C_SUCCESS) {
5070Sstevel@tonic-gate 				i2c_transfer_free(unitp->seeprom_hdl, i2ctp);
5080Sstevel@tonic-gate 				break;
5090Sstevel@tonic-gate 			}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 			actual_data_xfer = i2ctp->i2c_rlen - i2ctp->i2c_r_resid;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 			err = uiomove(i2ctp->i2c_rbuf, actual_data_xfer,
5140Sstevel@tonic-gate 			    UIO_READ, uiop);
5150Sstevel@tonic-gate 			i2c_transfer_free(unitp->seeprom_hdl, i2ctp);
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		bytes_to_rw -= actual_data_xfer;
5190Sstevel@tonic-gate 		seeprom_addr += actual_data_xfer;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	mutex_enter(&unitp->seeprom_mutex);
5230Sstevel@tonic-gate 	unitp->seeprom_flags = 0;
5240Sstevel@tonic-gate 	cv_signal(&unitp->seeprom_cv);
5250Sstevel@tonic-gate 	mutex_exit(&unitp->seeprom_mutex);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	return (err);
5280Sstevel@tonic-gate }
529