xref: /titanic_44/usr/src/uts/sun4/io/tod.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1993-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/open.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/clock.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/tod.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/todio.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	getsoftc(minor)	\
47*7c478bd9Sstevel@tonic-gate 		((struct tod_softc *)ddi_get_soft_state(statep, (minor)))
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /* dev_ops and cb_ops entry point function declarations */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate static int	tod_attach(dev_info_t *, ddi_attach_cmd_t);
52*7c478bd9Sstevel@tonic-gate static int	tod_detach(dev_info_t *, ddi_detach_cmd_t);
53*7c478bd9Sstevel@tonic-gate static int	tod_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static int	tod_open(dev_t *, int, int, cred_t *);
56*7c478bd9Sstevel@tonic-gate static int	tod_close(dev_t, int, int, cred_t *);
57*7c478bd9Sstevel@tonic-gate static int	tod_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate struct cb_ops tod_cb_ops = {
60*7c478bd9Sstevel@tonic-gate 	tod_open,
61*7c478bd9Sstevel@tonic-gate 	tod_close,
62*7c478bd9Sstevel@tonic-gate 	nodev,
63*7c478bd9Sstevel@tonic-gate 	nodev,
64*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
65*7c478bd9Sstevel@tonic-gate 	nodev,
66*7c478bd9Sstevel@tonic-gate 	nodev,
67*7c478bd9Sstevel@tonic-gate 	tod_ioctl,
68*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
69*7c478bd9Sstevel@tonic-gate 	nodev,
70*7c478bd9Sstevel@tonic-gate 	ddi_segmap,		/* segmap */
71*7c478bd9Sstevel@tonic-gate 	nochpoll,
72*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,
73*7c478bd9Sstevel@tonic-gate 	NULL,			/* for STREAMS drivers */
74*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* driver compatibility flag */
75*7c478bd9Sstevel@tonic-gate };
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static struct dev_ops tod_dev_ops = {
78*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* driver build version */
79*7c478bd9Sstevel@tonic-gate 	0,			/* device reference count */
80*7c478bd9Sstevel@tonic-gate 	tod_getinfo,
81*7c478bd9Sstevel@tonic-gate 	nulldev,
82*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
83*7c478bd9Sstevel@tonic-gate 	tod_attach,
84*7c478bd9Sstevel@tonic-gate 	tod_detach,
85*7c478bd9Sstevel@tonic-gate 	nulldev,		/* reset */
86*7c478bd9Sstevel@tonic-gate 	&tod_cb_ops,
87*7c478bd9Sstevel@tonic-gate 	(struct bus_ops *)NULL,
88*7c478bd9Sstevel@tonic-gate 	nulldev			/* power */
89*7c478bd9Sstevel@tonic-gate };
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /* module configuration stuff */
92*7c478bd9Sstevel@tonic-gate static void    *statep;
93*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
96*7c478bd9Sstevel@tonic-gate 	&mod_driverops,
97*7c478bd9Sstevel@tonic-gate 	"tod driver (v1.0) %I%",
98*7c478bd9Sstevel@tonic-gate 	&tod_dev_ops
99*7c478bd9Sstevel@tonic-gate };
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
102*7c478bd9Sstevel@tonic-gate 	MODREV_1,
103*7c478bd9Sstevel@tonic-gate 	&modldrv,
104*7c478bd9Sstevel@tonic-gate 	0
105*7c478bd9Sstevel@tonic-gate };
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate int
109*7c478bd9Sstevel@tonic-gate _init(void)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	int    e;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (e = ddi_soft_state_init(&statep, sizeof (struct tod_softc), 1)) {
114*7c478bd9Sstevel@tonic-gate 		return (e);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	if ((e = mod_install(&modlinkage)) != 0) {
118*7c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&statep);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	return (e);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate int
126*7c478bd9Sstevel@tonic-gate _fini(void)
127*7c478bd9Sstevel@tonic-gate {
128*7c478bd9Sstevel@tonic-gate 	int e;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0) {
131*7c478bd9Sstevel@tonic-gate 		return (e);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&statep);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate int
141*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
148*7c478bd9Sstevel@tonic-gate static int
149*7c478bd9Sstevel@tonic-gate tod_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	int	inst = getminor((dev_t)arg);
152*7c478bd9Sstevel@tonic-gate 	int	retval = DDI_SUCCESS;
153*7c478bd9Sstevel@tonic-gate 	struct tod_softc *softc;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
158*7c478bd9Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL) {
159*7c478bd9Sstevel@tonic-gate 			*result = (void *)NULL;
160*7c478bd9Sstevel@tonic-gate 			retval = DDI_FAILURE;
161*7c478bd9Sstevel@tonic-gate 		} else {
162*7c478bd9Sstevel@tonic-gate 			*result = (void *)softc->dip;
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 		break;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
167*7c478bd9Sstevel@tonic-gate 		*result = (void *)inst;
168*7c478bd9Sstevel@tonic-gate 		break;
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	default:
171*7c478bd9Sstevel@tonic-gate 		retval = DDI_FAILURE;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	return (retval);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate static int
178*7c478bd9Sstevel@tonic-gate tod_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	int inst;
182*7c478bd9Sstevel@tonic-gate 	struct tod_softc *softc = NULL;
183*7c478bd9Sstevel@tonic-gate 	char name[80];
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
188*7c478bd9Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
189*7c478bd9Sstevel@tonic-gate 		/*
190*7c478bd9Sstevel@tonic-gate 		 * Create minor node.  The minor device number, inst, has no
191*7c478bd9Sstevel@tonic-gate 		 * meaning.  The model number above, which will be added to
192*7c478bd9Sstevel@tonic-gate 		 * the device's softc, is used to direct peculiar behavior.
193*7c478bd9Sstevel@tonic-gate 		 */
194*7c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "tod%d", inst);
195*7c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR, inst,
196*7c478bd9Sstevel@tonic-gate 		    DDI_PSEUDO, NULL) == DDI_FAILURE)
197*7c478bd9Sstevel@tonic-gate 			goto attach_failed;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		/*
200*7c478bd9Sstevel@tonic-gate 		 * Allocate a soft state structure for this instance.
201*7c478bd9Sstevel@tonic-gate 		 */
202*7c478bd9Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(statep, inst) != DDI_SUCCESS)
203*7c478bd9Sstevel@tonic-gate 			goto attach_failed;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		softc = getsoftc(inst);
206*7c478bd9Sstevel@tonic-gate 		softc->dip = dip;
207*7c478bd9Sstevel@tonic-gate 		softc->cpr_stage = ~TOD_SUSPENDED;
208*7c478bd9Sstevel@tonic-gate 		mutex_init(&softc->mutex, NULL, MUTEX_DRIVER, NULL);
209*7c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
210*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
213*7c478bd9Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
214*7c478bd9Sstevel@tonic-gate 		softc = getsoftc(inst);
215*7c478bd9Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
216*7c478bd9Sstevel@tonic-gate 		softc->cpr_stage = ~TOD_SUSPENDED;
217*7c478bd9Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
218*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	default:
221*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate attach_failed:
225*7c478bd9Sstevel@tonic-gate 	/* Free soft state, if allocated. remove minor node if added earlier */
226*7c478bd9Sstevel@tonic-gate 	if (softc)
227*7c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate static int
235*7c478bd9Sstevel@tonic-gate tod_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
236*7c478bd9Sstevel@tonic-gate {
237*7c478bd9Sstevel@tonic-gate 	int inst;
238*7c478bd9Sstevel@tonic-gate 	struct tod_softc *softc;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
243*7c478bd9Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
244*7c478bd9Sstevel@tonic-gate 		if ((softc = getsoftc(inst)) == NULL)
245*7c478bd9Sstevel@tonic-gate 			return (ENXIO);
246*7c478bd9Sstevel@tonic-gate 		/*
247*7c478bd9Sstevel@tonic-gate 		 * Free the soft state and remove minor node added earlier.
248*7c478bd9Sstevel@tonic-gate 		 */
249*7c478bd9Sstevel@tonic-gate 		mutex_destroy(&softc->mutex);
250*7c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(statep, inst);
251*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
252*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
255*7c478bd9Sstevel@tonic-gate 		inst = ddi_get_instance(dip);
256*7c478bd9Sstevel@tonic-gate 		softc = getsoftc(inst);
257*7c478bd9Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
258*7c478bd9Sstevel@tonic-gate 		softc->cpr_stage = TOD_SUSPENDED;
259*7c478bd9Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
260*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	default:
263*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
269*7c478bd9Sstevel@tonic-gate static int
270*7c478bd9Sstevel@tonic-gate tod_open(dev_t *devp, int flag, int otyp, cred_t *credp)
271*7c478bd9Sstevel@tonic-gate {
272*7c478bd9Sstevel@tonic-gate 	int	inst = getminor(*devp);
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
279*7c478bd9Sstevel@tonic-gate static int
280*7c478bd9Sstevel@tonic-gate tod_close(dev_t dev, int flag, int otyp, cred_t *credp)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	int	inst = getminor(dev);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	return (getsoftc(inst) == NULL ? ENXIO : 0);
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
289*7c478bd9Sstevel@tonic-gate static int
290*7c478bd9Sstevel@tonic-gate tod_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp, int *rvalp)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	int		inst = getminor(dev);
293*7c478bd9Sstevel@tonic-gate 	struct tod_softc *softc;
294*7c478bd9Sstevel@tonic-gate 	timestruc_t	ts;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	if ((softc = getsoftc(inst)) == NULL)
297*7c478bd9Sstevel@tonic-gate 		return (ENXIO);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	mutex_enter(&softc->mutex);
300*7c478bd9Sstevel@tonic-gate 	while (softc->cpr_stage == TOD_SUSPENDED) {
301*7c478bd9Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
302*7c478bd9Sstevel@tonic-gate 		(void) ddi_dev_is_needed(softc->dip, 0, 1);
303*7c478bd9Sstevel@tonic-gate 		mutex_enter(&softc->mutex);
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 	case TOD_CLEAR_ALARM:
309*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
310*7c478bd9Sstevel@tonic-gate 		tod_ops.tod_clear_power_alarm();
311*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
312*7c478bd9Sstevel@tonic-gate 		break;
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	case TOD_SET_ALARM:
315*7c478bd9Sstevel@tonic-gate 		if ((mode & FMODELS) == FNATIVE) {
316*7c478bd9Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg, (caddr_t)&ts.tv_sec,
317*7c478bd9Sstevel@tonic-gate 			    sizeof (ts.tv_sec), mode) != 0) {
318*7c478bd9Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
319*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
320*7c478bd9Sstevel@tonic-gate 			}
321*7c478bd9Sstevel@tonic-gate 		} else {
322*7c478bd9Sstevel@tonic-gate 			time32_t time32;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 			if (ddi_copyin((caddr_t)arg,
325*7c478bd9Sstevel@tonic-gate 			    &time32, sizeof (time32), mode) != 0) {
326*7c478bd9Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
327*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
328*7c478bd9Sstevel@tonic-gate 			}
329*7c478bd9Sstevel@tonic-gate 			ts.tv_sec = (time_t)time32;
330*7c478bd9Sstevel@tonic-gate 		}
331*7c478bd9Sstevel@tonic-gate 		ts.tv_nsec = 0;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
334*7c478bd9Sstevel@tonic-gate 		tod_ops.tod_set_power_alarm(ts);
335*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
336*7c478bd9Sstevel@tonic-gate 		break;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	case TOD_GET_DATE:
339*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tod_lock);
340*7c478bd9Sstevel@tonic-gate 		ts = tod_ops.tod_get();
341*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tod_lock);
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 		if ((mode & FMODELS) == FNATIVE) {
344*7c478bd9Sstevel@tonic-gate 			if (ddi_copyout((caddr_t)&ts.tv_sec, (caddr_t)arg,
345*7c478bd9Sstevel@tonic-gate 			    sizeof (ts.tv_sec), mode) != 0) {
346*7c478bd9Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
347*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
348*7c478bd9Sstevel@tonic-gate 			}
349*7c478bd9Sstevel@tonic-gate 		} else {
350*7c478bd9Sstevel@tonic-gate 			time32_t time32;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&ts)) {
353*7c478bd9Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
354*7c478bd9Sstevel@tonic-gate 				return (EOVERFLOW);
355*7c478bd9Sstevel@tonic-gate 			}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 			time32 = (time32_t)ts.tv_sec;
358*7c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&time32,
359*7c478bd9Sstevel@tonic-gate 			    (caddr_t)arg, sizeof (time32), mode) != 0) {
360*7c478bd9Sstevel@tonic-gate 				mutex_exit(&softc->mutex);
361*7c478bd9Sstevel@tonic-gate 				return (EFAULT);
362*7c478bd9Sstevel@tonic-gate 			}
363*7c478bd9Sstevel@tonic-gate 		}
364*7c478bd9Sstevel@tonic-gate 		break;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	default:
367*7c478bd9Sstevel@tonic-gate 		mutex_exit(&softc->mutex);
368*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
369*7c478bd9Sstevel@tonic-gate 	}
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	mutex_exit(&softc->mutex);
372*7c478bd9Sstevel@tonic-gate 	return (0);
373*7c478bd9Sstevel@tonic-gate }
374