clockctl_50.c revision 1.3.4.2 1 1.3.4.2 christos /* $NetBSD: clockctl_50.c,v 1.3.4.2 2019/06/10 22:06:57 christos Exp $ */
2 1.3.4.2 christos
3 1.3.4.2 christos /*-
4 1.3.4.2 christos * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.3.4.2 christos * All rights reserved.
6 1.3.4.2 christos *
7 1.3.4.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 christos * by Emmanuel Dreyfus.
9 1.3.4.2 christos *
10 1.3.4.2 christos * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 christos * modification, are permitted provided that the following conditions
12 1.3.4.2 christos * are met:
13 1.3.4.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 christos * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 christos * documentation and/or other materials provided with the distribution.
18 1.3.4.2 christos * 3. The name of the author may not be used to endorse or promote products
19 1.3.4.2 christos * derived from this software without specific prior written permission.
20 1.3.4.2 christos *
21 1.3.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.3.4.2 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.3.4.2 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.3.4.2 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.3.4.2 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.3.4.2 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.3.4.2 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.3.4.2 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.3.4.2 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.3.4.2 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.3.4.2 christos */
32 1.3.4.2 christos
33 1.3.4.2 christos #include <sys/cdefs.h>
34 1.3.4.2 christos __KERNEL_RCSID(0, "$NetBSD: clockctl_50.c,v 1.3.4.2 2019/06/10 22:06:57 christos Exp $");
35 1.3.4.2 christos
36 1.3.4.2 christos #if defined(_KERNEL_OPT)
37 1.3.4.2 christos #include "opt_compat_netbsd.h"
38 1.3.4.2 christos #endif
39 1.3.4.2 christos
40 1.3.4.2 christos #include <sys/param.h>
41 1.3.4.2 christos #include <sys/systm.h>
42 1.3.4.2 christos #include <sys/proc.h>
43 1.3.4.2 christos #include <sys/errno.h>
44 1.3.4.2 christos #include <sys/ioctl.h>
45 1.3.4.2 christos #include <sys/device.h>
46 1.3.4.2 christos #include <sys/time.h>
47 1.3.4.2 christos #include <sys/conf.h>
48 1.3.4.2 christos #include <sys/timex.h>
49 1.3.4.2 christos #include <sys/kauth.h>
50 1.3.4.2 christos #include <sys/module.h>
51 1.3.4.2 christos #include <sys/mutex.h>
52 1.3.4.2 christos #include <sys/compat_stub.h>
53 1.3.4.2 christos
54 1.3.4.2 christos #include <sys/clockctl.h>
55 1.3.4.2 christos #include <compat/sys/clockctl.h>
56 1.3.4.2 christos #include <compat/sys/time_types.h>
57 1.3.4.2 christos
58 1.3.4.2 christos int
59 1.3.4.2 christos compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
60 1.3.4.2 christos struct lwp *l)
61 1.3.4.2 christos {
62 1.3.4.2 christos int error = 0;
63 1.3.4.2 christos const struct cdevsw *cd = cdevsw_lookup(dev);
64 1.3.4.2 christos
65 1.3.4.2 christos if (cd == NULL || cd->d_ioctl == NULL)
66 1.3.4.2 christos return ENXIO;
67 1.3.4.2 christos
68 1.3.4.2 christos switch (cmd) {
69 1.3.4.2 christos case CLOCKCTL_OSETTIMEOFDAY: {
70 1.3.4.2 christos struct timeval50 tv50;
71 1.3.4.2 christos struct timeval tv;
72 1.3.4.2 christos struct clockctl50_settimeofday *args = data;
73 1.3.4.2 christos
74 1.3.4.2 christos error = copyin(args->tv, &tv50, sizeof(tv50));
75 1.3.4.2 christos if (error)
76 1.3.4.2 christos return (error);
77 1.3.4.2 christos timeval50_to_timeval(&tv50, &tv);
78 1.3.4.2 christos error = settimeofday1(&tv, false, args->tzp, l, false);
79 1.3.4.2 christos break;
80 1.3.4.2 christos }
81 1.3.4.2 christos case CLOCKCTL_OADJTIME: {
82 1.3.4.2 christos struct timeval atv, oldatv;
83 1.3.4.2 christos struct timeval50 atv50;
84 1.3.4.2 christos struct clockctl50_adjtime *args = data;
85 1.3.4.2 christos
86 1.3.4.2 christos if (args->delta) {
87 1.3.4.2 christos error = copyin(args->delta, &atv50, sizeof(atv50));
88 1.3.4.2 christos if (error)
89 1.3.4.2 christos return (error);
90 1.3.4.2 christos timeval50_to_timeval(&atv50, &atv);
91 1.3.4.2 christos }
92 1.3.4.2 christos adjtime1(args->delta ? &atv : NULL,
93 1.3.4.2 christos args->olddelta ? &oldatv : NULL, l->l_proc);
94 1.3.4.2 christos if (args->olddelta) {
95 1.3.4.2 christos timeval_to_timeval50(&oldatv, &atv50);
96 1.3.4.2 christos error = copyout(&atv50, args->olddelta, sizeof(atv50));
97 1.3.4.2 christos }
98 1.3.4.2 christos break;
99 1.3.4.2 christos }
100 1.3.4.2 christos case CLOCKCTL_OCLOCK_SETTIME: {
101 1.3.4.2 christos struct timespec50 tp50;
102 1.3.4.2 christos struct timespec tp;
103 1.3.4.2 christos struct clockctl50_clock_settime *args = data;
104 1.3.4.2 christos
105 1.3.4.2 christos error = copyin(args->tp, &tp50, sizeof(tp50));
106 1.3.4.2 christos if (error)
107 1.3.4.2 christos return (error);
108 1.3.4.2 christos timespec50_to_timespec(&tp50, &tp);
109 1.3.4.2 christos error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
110 1.3.4.2 christos break;
111 1.3.4.2 christos }
112 1.3.4.2 christos case CLOCKCTL_ONTP_ADJTIME: {
113 1.3.4.2 christos if (vec_ntp_timestatus == NULL) {
114 1.3.4.2 christos error = ENOTTY;
115 1.3.4.2 christos break;
116 1.3.4.2 christos }
117 1.3.4.2 christos /* The ioctl number changed but the data did not change. */
118 1.3.4.2 christos error = (cd->d_ioctl)(dev, CLOCKCTL_NTP_ADJTIME,
119 1.3.4.2 christos data, flags, l);
120 1.3.4.2 christos break;
121 1.3.4.2 christos }
122 1.3.4.2 christos default:
123 1.3.4.2 christos error = ENOTTY;
124 1.3.4.2 christos }
125 1.3.4.2 christos
126 1.3.4.2 christos return (error);
127 1.3.4.2 christos }
128 1.3.4.2 christos
129 1.3.4.2 christos void
130 1.3.4.2 christos clockctl_50_init(void)
131 1.3.4.2 christos {
132 1.3.4.2 christos
133 1.3.4.2 christos MODULE_HOOK_SET(clockctl_ioctl_50_hook, "clk_50",
134 1.3.4.2 christos compat50_clockctlioctl);
135 1.3.4.2 christos }
136 1.3.4.2 christos
137 1.3.4.2 christos void
138 1.3.4.2 christos clockctl_50_fini(void)
139 1.3.4.2 christos {
140 1.3.4.2 christos
141 1.3.4.2 christos MODULE_HOOK_UNSET(clockctl_ioctl_50_hook);
142 1.3.4.2 christos }
143