Home | History | Annotate | Line # | Download | only in dev
clockctl.c revision 1.23.14.2
      1  1.23.14.2       mjf /*      $NetBSD: clockctl.c,v 1.23.14.2 2009/01/17 13:28:52 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.2       mjf __KERNEL_RCSID(0, "$NetBSD: clockctl.c,v 1.23.14.2 2009/01/17 13:28:52 mjf Exp $");
     35        1.5      manu 
     36        1.5      manu #include "opt_ntp.h"
     37  1.23.14.2       mjf #include "opt_compat_netbsd.h"
     38        1.1      manu 
     39        1.1      manu #include <sys/param.h>
     40        1.1      manu #include <sys/systm.h>
     41        1.1      manu #include <sys/proc.h>
     42        1.1      manu #include <sys/errno.h>
     43        1.1      manu #include <sys/ioctl.h>
     44        1.1      manu #include <sys/device.h>
     45        1.1      manu #include <sys/time.h>
     46        1.7   gehenna #include <sys/conf.h>
     47        1.1      manu #ifdef NTP
     48        1.1      manu #include <sys/timex.h>
     49        1.1      manu #endif /* NTP */
     50        1.1      manu 
     51        1.1      manu #include <sys/clockctl.h>
     52  1.23.14.2       mjf #ifdef COMPAT_50
     53  1.23.14.2       mjf #include <compat/sys/clockctl.h>
     54  1.23.14.2       mjf #endif
     55        1.1      manu 
     56        1.1      manu struct clockctl_softc {
     57        1.1      manu 	struct device   clockctl_dev;
     58        1.1      manu };
     59        1.1      manu 
     60        1.7   gehenna dev_type_ioctl(clockctlioctl);
     61        1.7   gehenna 
     62        1.7   gehenna const struct cdevsw clockctl_cdevsw = {
     63        1.7   gehenna 	nullopen, nullclose, noread, nowrite, clockctlioctl,
     64       1.18  christos 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
     65        1.7   gehenna };
     66        1.1      manu 
     67       1.10     perry /*ARGSUSED*/
     68        1.1      manu void
     69       1.20  christos clockctlattach(int num)
     70        1.1      manu {
     71  1.23.14.1       mjf 	int maj = cdevsw_lookup_major(&clockctl_cdevsw);
     72  1.23.14.1       mjf 
     73  1.23.14.1       mjf 	/* XXX: Would we ever require more than one clockctl? */
     74  1.23.14.1       mjf 	device_register_name(makedev(maj, 0), NULL, true,
     75  1.23.14.1       mjf 	    DEV_OTHER, "clockctl");
     76  1.23.14.1       mjf 
     77        1.1      manu 	return;
     78        1.1      manu }
     79        1.1      manu 
     80        1.1      manu int
     81       1.19  christos clockctlioctl(
     82       1.20  christos     dev_t dev,
     83       1.19  christos     u_long cmd,
     84       1.21  christos     void *data,
     85       1.20  christos     int flags,
     86       1.19  christos     struct lwp *l)
     87        1.1      manu {
     88        1.1      manu 	int error = 0;
     89       1.13     perry 
     90        1.1      manu 	switch (cmd) {
     91  1.23.14.2       mjf 	case CLOCKCTL_SETTIMEOFDAY: {
     92  1.23.14.2       mjf 		struct clockctl_settimeofday *args = data;
     93        1.1      manu 
     94  1.23.14.2       mjf 		error = settimeofday1(args->tv, true, args->tzp, l, false);
     95  1.23.14.2       mjf 		break;
     96  1.23.14.2       mjf 	}
     97  1.23.14.2       mjf 	case CLOCKCTL_ADJTIME: {
     98  1.23.14.2       mjf 		struct timeval atv, oldatv;
     99  1.23.14.2       mjf 		struct clockctl_adjtime *args = data;
    100        1.1      manu 
    101  1.23.14.2       mjf 		if (args->delta) {
    102  1.23.14.2       mjf 			error = copyin(args->delta, &atv, sizeof(*args->delta));
    103        1.1      manu 			if (error)
    104       1.13     perry 				return (error);
    105        1.1      manu 		}
    106  1.23.14.2       mjf 		adjtime1(args->delta ? &atv : NULL,
    107  1.23.14.2       mjf 		    args->olddelta ? &oldatv : NULL, l->l_proc);
    108  1.23.14.2       mjf 		if (args->olddelta)
    109  1.23.14.2       mjf 			error = copyout(&oldatv, args->olddelta,
    110  1.23.14.2       mjf 			    sizeof(args->olddelta));
    111  1.23.14.2       mjf 		break;
    112  1.23.14.2       mjf 	}
    113  1.23.14.2       mjf 	case CLOCKCTL_CLOCK_SETTIME: {
    114  1.23.14.2       mjf 		struct clockctl_clock_settime *args = data;
    115        1.1      manu 
    116  1.23.14.2       mjf 		error = clock_settime1(l->l_proc, args->clock_id,
    117  1.23.14.2       mjf 		    args->tp, false);
    118  1.23.14.2       mjf 		break;
    119  1.23.14.2       mjf 	}
    120        1.1      manu #ifdef NTP
    121  1.23.14.2       mjf 	case CLOCKCTL_NTP_ADJTIME: {
    122  1.23.14.2       mjf 		struct clockctl_ntp_adjtime *args = data;
    123  1.23.14.2       mjf 		struct timex ntv;
    124  1.23.14.2       mjf 		register_t retval;
    125        1.6      manu 
    126  1.23.14.2       mjf 		error = copyin(args->tp, &ntv, sizeof(ntv));
    127  1.23.14.2       mjf 		if (error)
    128  1.23.14.2       mjf 			return (error);
    129       1.17    kardel 
    130  1.23.14.2       mjf 		ntp_adjtime1(&ntv);
    131       1.17    kardel 
    132  1.23.14.2       mjf 		error = copyout(&ntv, args->tp, sizeof(ntv));
    133  1.23.14.2       mjf 		if (error == 0)
    134  1.23.14.2       mjf 			error = copyout(&retval, &args->retval, sizeof(retval));
    135  1.23.14.2       mjf 	}
    136        1.1      manu #endif /* NTP */
    137  1.23.14.2       mjf 	default:
    138  1.23.14.2       mjf #ifdef COMPAT_50
    139  1.23.14.2       mjf 		error = compat50_clockctlioctl(dev, cmd, data, flags, l);
    140  1.23.14.2       mjf #else
    141  1.23.14.2       mjf 		error = EINVAL;
    142  1.23.14.2       mjf #endif
    143        1.1      manu 	}
    144        1.1      manu 
    145        1.1      manu 	return (error);
    146        1.1      manu }
    147        1.1      manu 
    148        1.1      manu 
    149