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