clockctl.c revision 1.23.14.1 1 1.23.14.1 mjf /* $NetBSD: clockctl.c,v 1.23.14.1 2008/04/05 23:33:20 mjf Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Emmanuel Dreyfus.
9 1.1 manu *
10 1.1 manu * Redistribution and use in source and binary forms, with or without
11 1.1 manu * modification, are permitted provided that the following conditions
12 1.1 manu * are met:
13 1.1 manu * 1. Redistributions of source code must retain the above copyright
14 1.1 manu * notice, this list of conditions and the following disclaimer.
15 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 manu * notice, this list of conditions and the following disclaimer in the
17 1.1 manu * documentation and/or other materials provided with the distribution.
18 1.1 manu * 3. The name of the author may not be used to endorse or promote products
19 1.1 manu * derived from this software without specific prior written permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 manu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 manu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 manu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 manu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 manu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 manu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 manu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 manu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 manu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 manu */
32 1.2 lukem
33 1.2 lukem #include <sys/cdefs.h>
34 1.23.14.1 mjf __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.23.14.1 2008/04/05 23:33:20 mjf Exp $");
35 1.5 manu
36 1.5 manu #include "opt_ntp.h"
37 1.1 manu
38 1.1 manu #include <sys/param.h>
39 1.1 manu #include <sys/systm.h>
40 1.1 manu #include <sys/proc.h>
41 1.1 manu #include <sys/errno.h>
42 1.1 manu #include <sys/ioctl.h>
43 1.1 manu #include <sys/device.h>
44 1.1 manu #include <sys/time.h>
45 1.7 gehenna #include <sys/conf.h>
46 1.1 manu #ifdef NTP
47 1.1 manu #include <sys/timex.h>
48 1.1 manu #endif /* NTP */
49 1.1 manu
50 1.1 manu #include <sys/clockctl.h>
51 1.1 manu
52 1.1 manu struct clockctl_softc {
53 1.1 manu struct device clockctl_dev;
54 1.1 manu };
55 1.1 manu
56 1.7 gehenna dev_type_ioctl(clockctlioctl);
57 1.7 gehenna
58 1.7 gehenna const struct cdevsw clockctl_cdevsw = {
59 1.7 gehenna nullopen, nullclose, noread, nowrite, clockctlioctl,
60 1.18 christos nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
61 1.7 gehenna };
62 1.1 manu
63 1.10 perry /*ARGSUSED*/
64 1.1 manu void
65 1.20 christos clockctlattach(int num)
66 1.1 manu {
67 1.23.14.1 mjf int maj = cdevsw_lookup_major(&clockctl_cdevsw);
68 1.23.14.1 mjf
69 1.23.14.1 mjf /* XXX: Would we ever require more than one clockctl? */
70 1.23.14.1 mjf device_register_name(makedev(maj, 0), NULL, true,
71 1.23.14.1 mjf DEV_OTHER, "clockctl");
72 1.23.14.1 mjf
73 1.1 manu return;
74 1.1 manu }
75 1.1 manu
76 1.1 manu int
77 1.19 christos clockctlioctl(
78 1.20 christos dev_t dev,
79 1.19 christos u_long cmd,
80 1.21 christos void *data,
81 1.20 christos int flags,
82 1.19 christos struct lwp *l)
83 1.1 manu {
84 1.1 manu int error = 0;
85 1.13 perry
86 1.1 manu switch (cmd) {
87 1.1 manu case CLOCKCTL_SETTIMEOFDAY: {
88 1.16 christos struct clockctl_settimeofday *args =
89 1.16 christos (struct clockctl_settimeofday *)data;
90 1.1 manu
91 1.22 dsl error = settimeofday1(args->tv, true, args->tzp, l, false);
92 1.1 manu if (error)
93 1.1 manu return (error);
94 1.1 manu break;
95 1.1 manu }
96 1.1 manu case CLOCKCTL_ADJTIME: {
97 1.16 christos struct clockctl_adjtime *args =
98 1.16 christos (struct clockctl_adjtime *)data;
99 1.1 manu
100 1.16 christos error = adjtime1(args->delta, args->olddelta,
101 1.16 christos l->l_proc);
102 1.1 manu if (error)
103 1.13 perry return (error);
104 1.1 manu break;
105 1.1 manu }
106 1.1 manu case CLOCKCTL_CLOCK_SETTIME: {
107 1.16 christos struct clockctl_clock_settime *args =
108 1.16 christos (struct clockctl_clock_settime *)data;
109 1.1 manu
110 1.16 christos error = clock_settime1(l->l_proc, args->clock_id,
111 1.23 elad args->tp, false);
112 1.1 manu if (error)
113 1.13 perry return (error);
114 1.1 manu break;
115 1.1 manu }
116 1.1 manu #ifdef NTP
117 1.1 manu case CLOCKCTL_NTP_ADJTIME: {
118 1.16 christos struct clockctl_ntp_adjtime *args =
119 1.16 christos (struct clockctl_ntp_adjtime *)data;
120 1.6 manu struct timex ntv;
121 1.16 christos register_t retval;
122 1.1 manu
123 1.16 christos error = copyin(args->tp, &ntv, sizeof(ntv));
124 1.6 manu if (error)
125 1.6 manu return (error);
126 1.6 manu
127 1.17 kardel ntp_adjtime1(&ntv);
128 1.17 kardel
129 1.17 kardel error = copyout(&ntv, args->tp, sizeof(ntv));
130 1.17 kardel if (error == 0)
131 1.17 kardel (void)copyout(&retval, &args->retval, sizeof(retval));
132 1.17 kardel
133 1.6 manu return (error);
134 1.1 manu }
135 1.1 manu #endif /* NTP */
136 1.1 manu default:
137 1.1 manu error = EINVAL;
138 1.1 manu }
139 1.1 manu
140 1.1 manu return (error);
141 1.1 manu }
142 1.1 manu
143 1.1 manu
144