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