xref: /onnv-gate/usr/src/uts/sun4u/grover/io/grfans.c (revision 11311:639e7bc0b42f)
10Sstevel@tonic-gate /*
26982Sanbui  * CDDL HEADER START
36982Sanbui  *
46982Sanbui  * The contents of this file are subject to the terms of the
56982Sanbui  * Common Development and Distribution License (the "License").
66982Sanbui  * You may not use this file except in compliance with the License.
76982Sanbui  *
86982Sanbui  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96982Sanbui  * or http://www.opensolaris.org/os/licensing.
106982Sanbui  * See the License for the specific language governing permissions
116982Sanbui  * and limitations under the License.
126982Sanbui  *
136982Sanbui  * When distributing Covered Code, include this CDDL HEADER in each
146982Sanbui  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156982Sanbui  * If applicable, add the following below this CDDL HEADER, with the
166982Sanbui  * fields enclosed by brackets "[]" replaced with your own identifying
176982Sanbui  * information: Portions Copyright [yyyy] [name of copyright owner]
186982Sanbui  *
196982Sanbui  * CDDL HEADER END
206982Sanbui  */
216982Sanbui /*
22*11311SSurya.Prakki@Sun.COM  * Copyright 2009 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/file.h>
290Sstevel@tonic-gate #include <sys/uio.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/open.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/conf.h>
380Sstevel@tonic-gate #include <sys/mode.h>
390Sstevel@tonic-gate #include <sys/policy.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <sys/grfans.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * cb ops
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate static int grfans_open(dev_t *, int, int, cred_t *);
470Sstevel@tonic-gate static int grfans_close(dev_t, int, int, cred_t *);
480Sstevel@tonic-gate static int grfans_read(dev_t dev, struct uio *uiop, cred_t *cred_p);
490Sstevel@tonic-gate static int grfans_write(dev_t dev, struct uio *uiop, cred_t *cred_p);
500Sstevel@tonic-gate static int grfans_io(dev_t dev, struct uio *uiop, int rw);
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * dev ops
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate static int grfans_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
550Sstevel@tonic-gate 		void **result);
560Sstevel@tonic-gate static int grfans_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
570Sstevel@tonic-gate static int grfans_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static struct cb_ops grfans_cbops = {
600Sstevel@tonic-gate 	grfans_open,		/* open */
610Sstevel@tonic-gate 	grfans_close,		/* close */
620Sstevel@tonic-gate 	nodev,			/* strategy */
630Sstevel@tonic-gate 	nodev,			/* print */
640Sstevel@tonic-gate 	nodev,			/* dump */
650Sstevel@tonic-gate 	grfans_read,		/* read */
660Sstevel@tonic-gate 	grfans_write,		/* write */
670Sstevel@tonic-gate 	nodev,			/* ioctl */
680Sstevel@tonic-gate 	nodev,			/* devmap */
690Sstevel@tonic-gate 	nodev,			/* mmap */
700Sstevel@tonic-gate 	nodev,			/* segmap */
710Sstevel@tonic-gate 	nochpoll,		/* poll */
720Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
730Sstevel@tonic-gate 	NULL,			/* streamtab */
740Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG, /* Driver compatibility flag */
750Sstevel@tonic-gate 	CB_REV,			/* rev */
760Sstevel@tonic-gate 	nodev,			/* int (*cb_aread)() */
770Sstevel@tonic-gate 	nodev			/* int (*cb_awrite)() */
780Sstevel@tonic-gate };
790Sstevel@tonic-gate 
800Sstevel@tonic-gate static struct dev_ops grfans_ops = {
810Sstevel@tonic-gate 	DEVO_REV,
820Sstevel@tonic-gate 	0,
830Sstevel@tonic-gate 	grfans_info,
840Sstevel@tonic-gate 	nulldev,
850Sstevel@tonic-gate 	nulldev,
860Sstevel@tonic-gate 	grfans_attach,
870Sstevel@tonic-gate 	grfans_detach,
880Sstevel@tonic-gate 	nodev,
890Sstevel@tonic-gate 	&grfans_cbops,
907656SSherry.Moore@Sun.COM 	NULL,			/* bus_ops */
917656SSherry.Moore@Sun.COM 	NULL,			/* power */
927656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static struct modldrv grfans_modldrv = {
960Sstevel@tonic-gate 	&mod_driverops,		/* type of module - driver */
976982Sanbui 	"grfans device driver",
980Sstevel@tonic-gate 	&grfans_ops,
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct modlinkage grfans_modlinkage = {
1020Sstevel@tonic-gate 	MODREV_1,
1030Sstevel@tonic-gate 	&grfans_modldrv,
1040Sstevel@tonic-gate 	0
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static void *grfans_soft_statep;
1080Sstevel@tonic-gate static int grfans_debug = 0;
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 
1150Sstevel@tonic-gate 	error = mod_install(&grfans_modlinkage);
1160Sstevel@tonic-gate 	if (error == 0) {
1170Sstevel@tonic-gate 		(void) ddi_soft_state_init(&grfans_soft_statep,
1186982Sanbui 		    sizeof (struct grfans_unit), 1);
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	return (error);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
_fini(void)1250Sstevel@tonic-gate _fini(void)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	int    error;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	error = mod_remove(&grfans_modlinkage);
1300Sstevel@tonic-gate 	if (error == 0) {
1310Sstevel@tonic-gate 		ddi_soft_state_fini(&grfans_soft_statep);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (error);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1380Sstevel@tonic-gate _info(struct modinfo *modinfop)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	return (mod_info(&grfans_modlinkage, modinfop));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /* ARGSUSED */
1440Sstevel@tonic-gate static int
grfans_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1450Sstevel@tonic-gate grfans_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	dev_t	dev;
1480Sstevel@tonic-gate 	int	instance;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	if (infocmd == DDI_INFO_DEVT2INSTANCE) {
1510Sstevel@tonic-gate 		dev = (dev_t)arg;
1520Sstevel@tonic-gate 		instance = MINOR_TO_DEVINST(dev);
1530Sstevel@tonic-gate 		*result = (void *)(uintptr_t)instance;
1540Sstevel@tonic-gate 		return (DDI_SUCCESS);
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	return (DDI_FAILURE);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static int
grfans_do_attach(dev_info_t * dip)1600Sstevel@tonic-gate grfans_do_attach(dev_info_t *dip)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	struct grfans_unit *unitp;
1630Sstevel@tonic-gate 	int instance;
1640Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
1650Sstevel@tonic-gate 	int nregs;
1660Sstevel@tonic-gate 	char name[32];
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if (ddi_soft_state_zalloc(grfans_soft_statep, instance) != 0) {
1710Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to zalloc softstate",
1720Sstevel@tonic-gate 		    ddi_get_name(dip), instance);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		return (DDI_FAILURE);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (grfans_debug) {
1780Sstevel@tonic-gate 		printf("attached instance number %d\n", instance);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	unitp = ddi_get_soft_state(grfans_soft_statep, instance);
1820Sstevel@tonic-gate 	if (unitp == NULL)
1830Sstevel@tonic-gate 		return (DDI_FAILURE);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	(void) snprintf(name, sizeof (name), "%s%d", ddi_driver_name(dip),
1860Sstevel@tonic-gate 	    instance);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1890Sstevel@tonic-gate 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
1900Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	if (grfans_debug) {
1930Sstevel@tonic-gate 		printf("number of registers is %d\n",
1940Sstevel@tonic-gate 		    ddi_dev_nregs(dip, &nregs));
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0,
1980Sstevel@tonic-gate 	    (caddr_t *)&unitp->cpufan_reg,
1990Sstevel@tonic-gate 	    3, 1, &attr, &unitp->cpufan_rhandle) != DDI_SUCCESS) {
2000Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_regs_map_setup failed for regset "
2010Sstevel@tonic-gate 		    "0", name);
2020Sstevel@tonic-gate 		ddi_soft_state_free(grfans_soft_statep, instance);
2030Sstevel@tonic-gate 		return (DDI_FAILURE);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 1,
2070Sstevel@tonic-gate 	    (caddr_t *)&unitp->sysfan_reg,
2080Sstevel@tonic-gate 	    0, 1, &attr, &unitp->sysfan_rhandle) != DDI_SUCCESS) {
2090Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_regs_map_setup failed for regset "
2100Sstevel@tonic-gate 		    "1", name);
2110Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->cpufan_rhandle);
2120Sstevel@tonic-gate 		ddi_soft_state_free(grfans_soft_statep, instance);
2130Sstevel@tonic-gate 		return (DDI_FAILURE);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "cpu_fan", S_IFCHR,
2170Sstevel@tonic-gate 	    DEVINST_TO_MINOR(instance) | CHANNEL_TO_MINOR(CPU_FAN_CHANNEL),
2180Sstevel@tonic-gate 	    FANS_NODE_TYPE, NULL) == DDI_FAILURE) {
2190Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed"
2200Sstevel@tonic-gate 		    " for cpu fan", name);
2210Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->cpufan_rhandle);
2220Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->sysfan_rhandle);
2230Sstevel@tonic-gate 		ddi_soft_state_free(grfans_soft_statep, instance);
2240Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		return (DDI_FAILURE);
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "sys_fan", S_IFCHR,
2300Sstevel@tonic-gate 	    DEVINST_TO_MINOR(instance) | CHANNEL_TO_MINOR(SYSTEM_FAN_CHANNEL),
2310Sstevel@tonic-gate 	    FANS_NODE_TYPE, NULL) == DDI_FAILURE) {
2320Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s ddi_create_minor_node failed"
2330Sstevel@tonic-gate 		    " for system fan", name);
2340Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->cpufan_rhandle);
2350Sstevel@tonic-gate 		ddi_regs_map_free(&unitp->sysfan_rhandle);
2360Sstevel@tonic-gate 		ddi_soft_state_free(grfans_soft_statep, instance);
2370Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		return (DDI_FAILURE);
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	mutex_init(&unitp->mutex, NULL, MUTEX_DRIVER, NULL);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (DDI_SUCCESS);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate static int
grfans_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2480Sstevel@tonic-gate grfans_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	switch (cmd) {
2510Sstevel@tonic-gate 	case DDI_ATTACH:
2520Sstevel@tonic-gate 		return (grfans_do_attach(dip));
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	case DDI_RESUME:
2550Sstevel@tonic-gate 		return (DDI_SUCCESS);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	default:
2580Sstevel@tonic-gate 		return (DDI_FAILURE);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static int
grfans_do_detach(dev_info_t * dip)2630Sstevel@tonic-gate grfans_do_detach(dev_info_t *dip)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	struct grfans_unit *unitp;
2660Sstevel@tonic-gate 	int instance;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
2690Sstevel@tonic-gate 	unitp = ddi_get_soft_state(grfans_soft_statep, instance);
2700Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	ddi_regs_map_free(&unitp->cpufan_rhandle);
2730Sstevel@tonic-gate 	ddi_regs_map_free(&unitp->sysfan_rhandle);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	mutex_destroy(&unitp->mutex);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	ddi_soft_state_free(grfans_soft_statep, instance);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	return (DDI_SUCCESS);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate static int
grfans_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2830Sstevel@tonic-gate grfans_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	switch (cmd) {
2860Sstevel@tonic-gate 	case DDI_DETACH:
2870Sstevel@tonic-gate 		return (grfans_do_detach(dip));
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	case DDI_SUSPEND:
2900Sstevel@tonic-gate 		return (DDI_SUCCESS);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	default:
2930Sstevel@tonic-gate 		return (DDI_FAILURE);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*ARGSUSED*/
2980Sstevel@tonic-gate static int
grfans_open(dev_t * devp,int flags,int otyp,cred_t * credp)2990Sstevel@tonic-gate grfans_open(dev_t *devp, int flags, int otyp, cred_t *credp)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct grfans_unit *unitp;
3020Sstevel@tonic-gate 	int err = 0;
3030Sstevel@tonic-gate 	int instance = MINOR_TO_DEVINST(*devp);
3040Sstevel@tonic-gate 	int channel;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * must be privileged to access this device
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	if (secpolicy_sys_config(credp, B_FALSE) != 0)
3100Sstevel@tonic-gate 		return (EPERM);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (instance < 0) {
3130Sstevel@tonic-gate 		cmn_err(CE_WARN, "grfan: instance less than 0:  %d\n",
3140Sstevel@tonic-gate 		    instance);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		return (ENXIO);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	unitp = ddi_get_soft_state(grfans_soft_statep, instance);
3200Sstevel@tonic-gate 	if (unitp == NULL) {
3210Sstevel@tonic-gate 		cmn_err(CE_WARN, "grfan: no soft state for instance %d\n",
3220Sstevel@tonic-gate 		    instance);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		return (ENXIO);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
3280Sstevel@tonic-gate 		return (EINVAL);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	channel = MINOR_TO_CHANNEL(getminor(*devp));
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	mutex_enter(&unitp->mutex);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (flags & FEXCL) {
3350Sstevel@tonic-gate 		if (unitp->oflag[channel] != 0)
3360Sstevel@tonic-gate 			err = EBUSY;
3370Sstevel@tonic-gate 		else
3380Sstevel@tonic-gate 			unitp->oflag[channel] = FEXCL;
3390Sstevel@tonic-gate 	} else {
3400Sstevel@tonic-gate 		if (unitp->oflag[channel] == FEXCL)
3410Sstevel@tonic-gate 			err = EBUSY;
3420Sstevel@tonic-gate 		else
343946Smathue 			unitp->oflag[channel] = (uint16_t)FOPEN;
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	mutex_exit(&unitp->mutex);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	return (err);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*ARGSUSED*/
3520Sstevel@tonic-gate static int
grfans_close(dev_t dev,int flags,int otyp,cred_t * credp)3530Sstevel@tonic-gate grfans_close(dev_t dev, int flags, int otyp, cred_t *credp)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	struct grfans_unit *unitp;
3560Sstevel@tonic-gate 	int instance = MINOR_TO_DEVINST(dev);
3570Sstevel@tonic-gate 	int channel;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (instance < 0)
3600Sstevel@tonic-gate 		return (ENXIO);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	unitp = ddi_get_soft_state(grfans_soft_statep, instance);
3630Sstevel@tonic-gate 	if (unitp == NULL)
3640Sstevel@tonic-gate 		return (ENXIO);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	channel = MINOR_TO_CHANNEL(getminor(dev));
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	unitp->oflag[channel] = 0;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	return (DDI_SUCCESS);
3710Sstevel@tonic-gate }
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate /*ARGSUSED*/
3740Sstevel@tonic-gate static int
grfans_read(dev_t dev,struct uio * uiop,cred_t * cred_p)3750Sstevel@tonic-gate grfans_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	return (grfans_io(dev, uiop, B_READ));
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*ARGSUSED*/
3810Sstevel@tonic-gate static int
grfans_write(dev_t dev,struct uio * uiop,cred_t * cred_p)3820Sstevel@tonic-gate grfans_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	return (grfans_io(dev, uiop, B_WRITE));
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate static int
grfans_io(dev_t dev,struct uio * uiop,int rw)3880Sstevel@tonic-gate grfans_io(dev_t dev, struct uio *uiop, int rw)
3890Sstevel@tonic-gate {
3900Sstevel@tonic-gate 	struct grfans_unit *unitp;
3910Sstevel@tonic-gate 	int instance = MINOR_TO_DEVINST(getminor(dev));
3920Sstevel@tonic-gate 	int ret = 0;
3930Sstevel@tonic-gate 	size_t len = uiop->uio_resid;
3940Sstevel@tonic-gate 	int8_t out_value, req_value, reg_value;
3950Sstevel@tonic-gate 	caddr_t outputaddr;
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (instance < 0)
3980Sstevel@tonic-gate 		return (ENXIO);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (len == 0)
4010Sstevel@tonic-gate 		return (0);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	unitp = ddi_get_soft_state(grfans_soft_statep, instance);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (unitp == NULL)
4060Sstevel@tonic-gate 		return (ENXIO);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if (MINOR_TO_CHANNEL(getminor(dev)) == CPU_FAN_CHANNEL)
4090Sstevel@tonic-gate 		outputaddr = &unitp->cpufan_output;
4100Sstevel@tonic-gate 	else
4110Sstevel@tonic-gate 		outputaddr = &unitp->sysfan_output;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (rw == B_READ) {
4140Sstevel@tonic-gate 		if (*outputaddr == UNKNOWN_OUT)
4150Sstevel@tonic-gate 			return (EIO);
4160Sstevel@tonic-gate 		return (uiomove(outputaddr, 1, UIO_READ, uiop));
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * rw == B_WRITE.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	if ((ret = uiomove(&req_value, sizeof (req_value), UIO_WRITE,
4230Sstevel@tonic-gate 	    uiop)) == 0) {
4240Sstevel@tonic-gate 		if (MINOR_TO_CHANNEL(dev) == CPU_FAN_CHANNEL) {
4250Sstevel@tonic-gate 			/*
4260Sstevel@tonic-gate 			 * Check bounds for cpu fan
4270Sstevel@tonic-gate 			 */
4280Sstevel@tonic-gate 			if (req_value == 0) {
4290Sstevel@tonic-gate 				reg_value = CPU_FAN_0;
4300Sstevel@tonic-gate 				out_value = 0;
4310Sstevel@tonic-gate 			} else if (req_value <= 25) {
4320Sstevel@tonic-gate 				reg_value = CPU_FAN_25;
4330Sstevel@tonic-gate 				out_value = 25;
4340Sstevel@tonic-gate 			} else if (req_value <= 50) {
4350Sstevel@tonic-gate 				reg_value = CPU_FAN_50;
4360Sstevel@tonic-gate 				out_value = 50;
4370Sstevel@tonic-gate 			} else if (req_value <= 75) {
4380Sstevel@tonic-gate 				reg_value = CPU_FAN_75;
4390Sstevel@tonic-gate 				out_value = 75;
4400Sstevel@tonic-gate 			} else if (req_value <= 100) {
4410Sstevel@tonic-gate 				reg_value = CPU_FAN_100;
4420Sstevel@tonic-gate 				out_value = 100;
4430Sstevel@tonic-gate 			} else
4440Sstevel@tonic-gate 				ret = EINVAL;
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 			if (ret != EINVAL) {
4470Sstevel@tonic-gate 				uint8_t reg;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 				*outputaddr = out_value;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 				reg = ddi_get8(unitp->cpufan_rhandle,
4520Sstevel@tonic-gate 				    unitp->cpufan_reg);
4530Sstevel@tonic-gate 				reg = (reg & ~CPU_FAN_MASK) | reg_value;
4540Sstevel@tonic-gate 				ddi_put8(unitp->cpufan_rhandle,
4550Sstevel@tonic-gate 				    unitp->cpufan_reg, reg);
4560Sstevel@tonic-gate 				(void) ddi_get8(unitp->cpufan_rhandle,
4570Sstevel@tonic-gate 				    unitp->cpufan_reg);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 				if (grfans_debug) {
4600Sstevel@tonic-gate 					printf("set output to %d at addr %p\n",
4610Sstevel@tonic-gate 					    out_value,
462*11311SSurya.Prakki@Sun.COM 					    (void *)unitp->cpufan_reg);
4630Sstevel@tonic-gate 				}
4640Sstevel@tonic-gate 			}
4650Sstevel@tonic-gate 		} else {
4660Sstevel@tonic-gate 			if (req_value == 0) {
4670Sstevel@tonic-gate 				reg_value = SYS_FAN_OFF;
4680Sstevel@tonic-gate 				out_value = 0;
4690Sstevel@tonic-gate 			} else if (req_value > 0) {
4700Sstevel@tonic-gate 				reg_value = SYS_FAN_ON;
4710Sstevel@tonic-gate 				out_value = 100;
4720Sstevel@tonic-gate 			} else {
4730Sstevel@tonic-gate 				ret = EINVAL;
4740Sstevel@tonic-gate 			}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 			if (ret != EINVAL) {
4770Sstevel@tonic-gate 				*outputaddr = out_value;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 				ddi_put8(unitp->sysfan_rhandle,
4800Sstevel@tonic-gate 				    unitp->sysfan_reg,
4810Sstevel@tonic-gate 				    reg_value);
4820Sstevel@tonic-gate 				(void) ddi_get8(unitp->sysfan_rhandle,
4830Sstevel@tonic-gate 				    unitp->sysfan_reg);
4840Sstevel@tonic-gate 				if (grfans_debug) {
4850Sstevel@tonic-gate 					printf("set SYSFAN output to %d at "
4860Sstevel@tonic-gate 					    "addr %p\n", out_value,
487*11311SSurya.Prakki@Sun.COM 					    (void *)unitp->sysfan_reg);
4880Sstevel@tonic-gate 				}
4890Sstevel@tonic-gate 			}
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 	} else {
4920Sstevel@tonic-gate 		ret = EFAULT;
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	return (ret);
4960Sstevel@tonic-gate }
497