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