Home | History | Annotate | Line # | Download | only in common
clockctl_50.c revision 1.1.2.3
      1 /*      $NetBSD: clockctl_50.c,v 1.1.2.3 2018/09/18 23:03:54 pgoyette 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_50.c,v 1.1.2.3 2018/09/18 23:03:54 pgoyette Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/proc.h>
     39 #include <sys/errno.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/device.h>
     42 #include <sys/time.h>
     43 #include <sys/conf.h>
     44 #include <sys/timex.h>
     45 #include <sys/kauth.h>
     46 #include <sys/module.h>
     47 #include <sys/mutex.h>
     48 #include <sys/compat_stub.h>
     49 
     50 #include <sys/clockctl.h>
     51 #include <compat/sys/clockctl.h>
     52 #include <compat/sys/time_types.h>
     53 
     54 int
     55 compat50_clockctlioctl(dev_t dev, u_long cmd, void *data, int flags,
     56     struct lwp *l)
     57 {
     58 	int error = 0;
     59 	const struct cdevsw *cd = cdevsw_lookup(dev);
     60 
     61 	if (cd == NULL || cd->d_ioctl == NULL)
     62 		return ENXIO;
     63 
     64 	switch (cmd) {
     65 	case CLOCKCTL_OSETTIMEOFDAY: {
     66 		struct timeval50 tv50;
     67 		struct timeval tv;
     68 		struct clockctl50_settimeofday *args = data;
     69 
     70 		error = copyin(args->tv, &tv50, sizeof(tv50));
     71 		if (error)
     72 			return (error);
     73 		timeval50_to_timeval(&tv50, &tv);
     74 		error = settimeofday1(&tv, false, args->tzp, l, false);
     75 		break;
     76 	}
     77 	case CLOCKCTL_OADJTIME: {
     78 		struct timeval atv, oldatv;
     79 		struct timeval50 atv50;
     80 		struct clockctl50_adjtime *args = data;
     81 
     82 		if (args->delta) {
     83 			error = copyin(args->delta, &atv50, sizeof(atv50));
     84 			if (error)
     85 				return (error);
     86 			timeval50_to_timeval(&atv50, &atv);
     87 		}
     88 		adjtime1(args->delta ? &atv : NULL,
     89 		    args->olddelta ? &oldatv : NULL, l->l_proc);
     90 		if (args->olddelta) {
     91 			timeval_to_timeval50(&oldatv, &atv50);
     92 			error = copyout(&atv50, args->olddelta, sizeof(atv50));
     93 		}
     94 		break;
     95 	}
     96 	case CLOCKCTL_OCLOCK_SETTIME: {
     97 		struct timespec50 tp50;
     98 		struct timespec tp;
     99 		struct clockctl50_clock_settime *args = data;
    100 
    101 		error = copyin(args->tp, &tp50, sizeof(tp50));
    102 		if (error)
    103 			return (error);
    104 		timespec50_to_timespec(&tp50, &tp);
    105 		error = clock_settime1(l->l_proc, args->clock_id, &tp, true);
    106 		break;
    107 	}
    108 	case CLOCKCTL_ONTP_ADJTIME: {
    109 		if (vec_ntp_timestatus == NULL) {
    110 			error = ENOTTY;
    111 			break;
    112 		}
    113 		/* The ioctl number changed but the data did not change. */
    114 		error = (cd->d_ioctl)(dev, CLOCKCTL_NTP_ADJTIME,
    115 		    data, flags, l);
    116 		break;
    117 	}
    118 	default:
    119 		error = ENOTTY;
    120 	}
    121 
    122 	return (error);
    123 }
    124 
    125 MODULE_SET_HOOK(clockctl_ioctl_50_hook, "clk_50", compat50_clockctlioctl);
    126 MODULE_UNSET_HOOK(clockctl_ioctl_50_hook);
    127 
    128 void
    129 clockctl_50_init(void)
    130 {
    131 
    132 	clockctl_ioctl_50_hook_set();
    133 }
    134 
    135 void
    136 clockctl_50_fini(void)
    137 {
    138 
    139 	clockctl_ioctl_50_hook_unset();
    140 }
    141