xref: /onnv-gate/usr/src/uts/sun4/io/tod.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/types.h>
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/open.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/clock.h>
370Sstevel@tonic-gate #include <sys/tod.h>
380Sstevel@tonic-gate #include <sys/todio.h>
390Sstevel@tonic-gate #include <sys/ddi.h>
400Sstevel@tonic-gate #include <sys/sunddi.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #define	getsoftc(minor)	\
450Sstevel@tonic-gate 		((struct tod_softc *)ddi_get_soft_state(statep, (minor)))
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static int	tod_attach(dev_info_t *, ddi_attach_cmd_t);
500Sstevel@tonic-gate static int	tod_detach(dev_info_t *, ddi_detach_cmd_t);
510Sstevel@tonic-gate static int	tod_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static int	tod_open(dev_t *, int, int, cred_t *);
540Sstevel@tonic-gate static int	tod_close(dev_t, int, int, cred_t *);
550Sstevel@tonic-gate static int	tod_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct cb_ops tod_cb_ops = {
580Sstevel@tonic-gate 	tod_open,
590Sstevel@tonic-gate 	tod_close,
600Sstevel@tonic-gate 	nodev,
610Sstevel@tonic-gate 	nodev,
620Sstevel@tonic-gate 	nodev,			/* dump */
630Sstevel@tonic-gate 	nodev,
640Sstevel@tonic-gate 	nodev,
650Sstevel@tonic-gate 	tod_ioctl,
660Sstevel@tonic-gate 	nodev,			/* devmap */
670Sstevel@tonic-gate 	nodev,
680Sstevel@tonic-gate 	ddi_segmap,		/* segmap */
690Sstevel@tonic-gate 	nochpoll,
700Sstevel@tonic-gate 	ddi_prop_op,
710Sstevel@tonic-gate 	NULL,			/* for STREAMS drivers */
720Sstevel@tonic-gate 	D_NEW | D_MP		/* driver compatibility flag */
730Sstevel@tonic-gate };
740Sstevel@tonic-gate 
750Sstevel@tonic-gate static struct dev_ops tod_dev_ops = {
760Sstevel@tonic-gate 	DEVO_REV,		/* driver build version */
770Sstevel@tonic-gate 	0,			/* device reference count */
780Sstevel@tonic-gate 	tod_getinfo,
790Sstevel@tonic-gate 	nulldev,
800Sstevel@tonic-gate 	nulldev,		/* probe */
810Sstevel@tonic-gate 	tod_attach,
820Sstevel@tonic-gate 	tod_detach,
830Sstevel@tonic-gate 	nulldev,		/* reset */
840Sstevel@tonic-gate 	&tod_cb_ops,
850Sstevel@tonic-gate 	(struct bus_ops *)NULL,
86*7656SSherry.Moore@Sun.COM 	nulldev,		/* power */
87*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
880Sstevel@tonic-gate };
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* module configuration stuff */
910Sstevel@tonic-gate static void    *statep;
920Sstevel@tonic-gate extern struct mod_ops mod_driverops;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static struct modldrv modldrv = {
950Sstevel@tonic-gate 	&mod_driverops,
96*7656SSherry.Moore@Sun.COM 	"tod driver",
970Sstevel@tonic-gate 	&tod_dev_ops
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate static struct modlinkage modlinkage = {
1010Sstevel@tonic-gate 	MODREV_1,
1020Sstevel@tonic-gate 	&modldrv,
1030Sstevel@tonic-gate 	0
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate int
_init(void)1080Sstevel@tonic-gate _init(void)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	int    e;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (e = ddi_soft_state_init(&statep, sizeof (struct tod_softc), 1)) {
1130Sstevel@tonic-gate 		return (e);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0) {
1170Sstevel@tonic-gate 		ddi_soft_state_fini(&statep);
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (e);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
_fini(void)1250Sstevel@tonic-gate _fini(void)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	int e;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0) {
1300Sstevel@tonic-gate 		return (e);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	ddi_soft_state_fini(&statep);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	return (DDI_SUCCESS);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1400Sstevel@tonic-gate _info(struct modinfo *modinfop)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* ARGSUSED */
1470Sstevel@tonic-gate static int
tod_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)1480Sstevel@tonic-gate tod_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	int	inst = getminor((dev_t)arg);
1510Sstevel@tonic-gate 	int	retval = DDI_SUCCESS;
1520Sstevel@tonic-gate 	struct tod_softc *softc;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	switch (cmd) {
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1570Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL) {
1580Sstevel@tonic-gate 			*result = (void *)NULL;
1590Sstevel@tonic-gate 			retval = DDI_FAILURE;
1600Sstevel@tonic-gate 		} else {
1610Sstevel@tonic-gate 			*result = (void *)softc->dip;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 		break;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1661004Smike_s 		*result = (void *)(uintptr_t)inst;
1670Sstevel@tonic-gate 		break;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	default:
1700Sstevel@tonic-gate 		retval = DDI_FAILURE;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	return (retval);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static int
tod_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1770Sstevel@tonic-gate tod_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	int inst;
1810Sstevel@tonic-gate 	struct tod_softc *softc = NULL;
1820Sstevel@tonic-gate 	char name[80];
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	switch (cmd) {
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	case DDI_ATTACH:
1870Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
1880Sstevel@tonic-gate 		/*
1890Sstevel@tonic-gate 		 * Create minor node.  The minor device number, inst, has no
1900Sstevel@tonic-gate 		 * meaning.  The model number above, which will be added to
1910Sstevel@tonic-gate 		 * the device's softc, is used to direct peculiar behavior.
1920Sstevel@tonic-gate 		 */
1930Sstevel@tonic-gate 		(void) sprintf(name, "tod%d", inst);
1940Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, inst,
1950Sstevel@tonic-gate 		    DDI_PSEUDO, NULL) == DDI_FAILURE)
1960Sstevel@tonic-gate 			goto attach_failed;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		/*
1990Sstevel@tonic-gate 		 * Allocate a soft state structure for this instance.
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS)
2020Sstevel@tonic-gate 			goto attach_failed;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		softc = getsoftc(inst);
2050Sstevel@tonic-gate 		softc->dip = dip;
2060Sstevel@tonic-gate 		softc->cpr_stage = ~TOD_SUSPENDED;
2070Sstevel@tonic-gate 		mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL);
2080Sstevel@tonic-gate 		ddi_report_dev(dip);
2090Sstevel@tonic-gate 		return (DDI_SUCCESS);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	case DDI_RESUME:
2120Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
2130Sstevel@tonic-gate 		softc = getsoftc(inst);
2140Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
2150Sstevel@tonic-gate 		softc->cpr_stage = ~TOD_SUSPENDED;
2160Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
2170Sstevel@tonic-gate 		return (DDI_SUCCESS);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	default:
2200Sstevel@tonic-gate 		return (DDI_FAILURE);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate attach_failed:
2240Sstevel@tonic-gate 	/* Free soft state, if allocated. remove minor node if added earlier */
2250Sstevel@tonic-gate 	if (softc)
2260Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	return (DDI_FAILURE);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static int
tod_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)2340Sstevel@tonic-gate tod_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	int inst;
2370Sstevel@tonic-gate 	struct tod_softc *softc;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	switch (cmd) {
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	case DDI_DETACH:
2420Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
2430Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL)
2440Sstevel@tonic-gate 			return (ENXIO);
2450Sstevel@tonic-gate 		/*
2460Sstevel@tonic-gate 		 * Free the soft state and remove minor node added earlier.
2470Sstevel@tonic-gate 		 */
2480Sstevel@tonic-gate 		mutex_destroy(&softc->mutex);
2490Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
2500Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
2510Sstevel@tonic-gate 		return (DDI_SUCCESS);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	case DDI_SUSPEND:
2540Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
2550Sstevel@tonic-gate 		softc = getsoftc(inst);
2560Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
2570Sstevel@tonic-gate 		softc->cpr_stage = TOD_SUSPENDED;
2580Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
2590Sstevel@tonic-gate 		return (DDI_SUCCESS);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	default:
2620Sstevel@tonic-gate 		return (DDI_FAILURE);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /* ARGSUSED */
2680Sstevel@tonic-gate static int
tod_open(dev_t * devp,int flag,int otyp,cred_t * credp)2690Sstevel@tonic-gate tod_open(dev_t *devp, int flag, int otyp, cred_t *credp)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	int	inst = getminor(*devp);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /* ARGSUSED */
2780Sstevel@tonic-gate static int
tod_close(dev_t dev,int flag,int otyp,cred_t * credp)2790Sstevel@tonic-gate tod_close(dev_t dev, int flag, int otyp, cred_t *credp)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate 	int	inst = getminor(dev);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /* ARGSUSED */
2880Sstevel@tonic-gate static int
tod_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)2890Sstevel@tonic-gate tod_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	int		inst = getminor(dev);
2920Sstevel@tonic-gate 	struct tod_softc *softc;
2930Sstevel@tonic-gate 	timestruc_t	ts;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	if ((softc = getsoftc(inst)) == NULL)
2960Sstevel@tonic-gate 		return (ENXIO);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	mutex_enter(&softc->mutex);
2990Sstevel@tonic-gate 	while (softc->cpr_stage == TOD_SUSPENDED) {
3000Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
3010Sstevel@tonic-gate 		(void) ddi_dev_is_needed(softc->dip, 0, 1);
3020Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	switch (cmd) {
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	case TOD_CLEAR_ALARM:
3080Sstevel@tonic-gate 		mutex_enter(&tod_lock);
3090Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm();
3100Sstevel@tonic-gate 		mutex_exit(&tod_lock);
3110Sstevel@tonic-gate 		break;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	case TOD_SET_ALARM:
3140Sstevel@tonic-gate 		if ((mode & FMODELS) == FNATIVE) {
3150Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, (caddr_t)&ts.tv_sec,
3160Sstevel@tonic-gate 			    sizeof (ts.tv_sec), mode) != 0) {
3170Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
3180Sstevel@tonic-gate 				return (EFAULT);
3190Sstevel@tonic-gate 			}
3200Sstevel@tonic-gate 		} else {
3210Sstevel@tonic-gate 			time32_t time32;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg,
3240Sstevel@tonic-gate 			    &time32, sizeof (time32), mode) != 0) {
3250Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
3260Sstevel@tonic-gate 				return (EFAULT);
3270Sstevel@tonic-gate 			}
3280Sstevel@tonic-gate 			ts.tv_sec = (time_t)time32;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		ts.tv_nsec = 0;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		mutex_enter(&tod_lock);
3330Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm(ts);
3340Sstevel@tonic-gate 		mutex_exit(&tod_lock);
3350Sstevel@tonic-gate 		break;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	case TOD_GET_DATE:
3380Sstevel@tonic-gate 		mutex_enter(&tod_lock);
3390Sstevel@tonic-gate 		ts = tod_ops.tod_get();
3400Sstevel@tonic-gate 		mutex_exit(&tod_lock);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		if ((mode & FMODELS) == FNATIVE) {
3430Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&ts.tv_sec, (caddr_t)arg,
3440Sstevel@tonic-gate 			    sizeof (ts.tv_sec), mode) != 0) {
3450Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
3460Sstevel@tonic-gate 				return (EFAULT);
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 		} else {
3490Sstevel@tonic-gate 			time32_t time32;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&ts)) {
3520Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
3530Sstevel@tonic-gate 				return (EOVERFLOW);
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 			time32 = (time32_t)ts.tv_sec;
3570Sstevel@tonic-gate 			if (ddi_copyout(&time32,
3580Sstevel@tonic-gate 			    (caddr_t)arg, sizeof (time32), mode) != 0) {
3590Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
3600Sstevel@tonic-gate 				return (EFAULT);
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 		break;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	default:
3660Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
3670Sstevel@tonic-gate 		return (EINVAL);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	mutex_exit(&softc->mutex);
3710Sstevel@tonic-gate 	return (0);
3720Sstevel@tonic-gate }
373