Home | History | Annotate | Line # | Download | only in ppbus
pps_ppbus.c revision 1.14.30.1
      1 /* $NetBSD: pps_ppbus.c,v 1.14.30.1 2014/05/22 11:40:35 yamt Exp $ */
      2 
      3 /*
      4  * ported to timecounters by Frank Kardel 2006
      5  *
      6  * Copyright (c) 2004
      7  * 	Matthias Drochner.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions, and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: pps_ppbus.c,v 1.14.30.1 2014/05/22 11:40:35 yamt Exp $");
     33 
     34 #include "opt_ntp.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/conf.h>
     39 #include <sys/proc.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/timepps.h>
     42 
     43 #include <dev/ppbus/ppbus_base.h>
     44 #include <dev/ppbus/ppbus_device.h>
     45 #include <dev/ppbus/ppbus_io.h>
     46 #include <dev/ppbus/ppbus_var.h>
     47 
     48 struct pps_softc {
     49 	struct ppbus_device_softc pps_dev;
     50 	device_t ppbus;
     51 	int busy;
     52 	struct pps_state pps_state;	/* pps state */
     53 };
     54 
     55 static int pps_probe(device_t, cfdata_t, void *);
     56 static void pps_attach(device_t, device_t, void *);
     57 CFATTACH_DECL_NEW(pps, sizeof(struct pps_softc), pps_probe, pps_attach,
     58 	NULL, NULL);
     59 extern struct cfdriver pps_cd;
     60 
     61 static dev_type_open(ppsopen);
     62 static dev_type_close(ppsclose);
     63 static dev_type_ioctl(ppsioctl);
     64 const struct cdevsw pps_cdevsw = {
     65 	.d_open = ppsopen,
     66 	.d_close = ppsclose,
     67 	.d_read = noread,
     68 	.d_write = nowrite,
     69 	.d_ioctl = ppsioctl,
     70 	.d_stop = nostop,
     71 	.d_tty = notty,
     72 	.d_poll = nopoll,
     73 	.d_mmap = nommap,
     74 	.d_kqfilter = nokqfilter,
     75 	.d_flag = D_OTHER
     76 };
     77 
     78 static void ppsintr(void *arg);
     79 
     80 static int
     81 pps_probe(device_t parent, cfdata_t match, void *aux)
     82 {
     83 	struct ppbus_attach_args *args = aux;
     84 
     85 	/* we need an interrupt */
     86 	if (!(args->capabilities & PPBUS_HAS_INTR))
     87 		return 0;
     88 
     89 	return 1;
     90 }
     91 
     92 static void
     93 pps_attach(device_t parent, device_t self, void *aux)
     94 {
     95 	struct pps_softc *sc = device_private(self);
     96 
     97 	sc->ppbus = parent;
     98 	sc->pps_dev.sc_dev = self;
     99 
    100 	printf("\n");
    101 }
    102 
    103 static int
    104 ppsopen(dev_t dev, int flags, int fmt, struct lwp *l)
    105 {
    106 	struct pps_softc *sc;
    107 	int res, weg = 0;
    108 
    109 	sc = device_lookup_private(&pps_cd, minor(dev));
    110 	if (!sc)
    111 		return (ENXIO);
    112 
    113 	if (sc->busy)
    114 		return (0);
    115 
    116 	if (ppbus_request_bus(sc->ppbus, sc->pps_dev.sc_dev,
    117 			      PPBUS_WAIT|PPBUS_INTR, 0))
    118 		return (EINTR);
    119 
    120 	ppbus_write_ivar(sc->ppbus, PPBUS_IVAR_IEEE, &weg);
    121 
    122 	/* attach the interrupt handler */
    123 	/* XXX priority should be set here */
    124 	res = ppbus_add_handler(sc->ppbus, ppsintr, sc);
    125 	if (res) {
    126 		ppbus_release_bus(sc->ppbus, sc->pps_dev.sc_dev,
    127 				  PPBUS_WAIT, 0);
    128 		return (res);
    129 	}
    130 
    131 	ppbus_set_mode(sc->ppbus, PPBUS_PS2, 0);
    132 	ppbus_wctr(sc->ppbus, IRQENABLE | PCD | nINIT | SELECTIN);
    133 
    134 	mutex_spin_enter(&timecounter_lock);
    135 	memset((void *)&sc->pps_state, 0, sizeof(sc->pps_state));
    136 	sc->pps_state.ppscap = PPS_CAPTUREASSERT;
    137 	pps_init(&sc->pps_state);
    138 	mutex_spin_exit(&timecounter_lock);
    139 
    140 	sc->busy = 1;
    141 
    142 	return (0);
    143 }
    144 
    145 static int
    146 ppsclose(dev_t dev, int flags, int fmt, struct lwp *l)
    147 {
    148 	struct pps_softc *sc = device_lookup_private(&pps_cd, minor(dev));
    149 	device_t ppbus = sc->ppbus;
    150 
    151 	sc->busy = 0;
    152 	mutex_spin_enter(&timecounter_lock);
    153 	sc->pps_state.ppsparam.mode = 0;
    154 	mutex_spin_exit(&timecounter_lock);
    155 
    156 	ppbus_wdtr(ppbus, 0);
    157 	ppbus_wctr(ppbus, 0);
    158 
    159 	ppbus_remove_handler(ppbus, ppsintr);
    160 	ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
    161 	ppbus_release_bus(ppbus, sc->pps_dev.sc_dev, PPBUS_WAIT, 0);
    162 	return (0);
    163 }
    164 
    165 static void
    166 ppsintr(void *arg)
    167 {
    168 	struct pps_softc *sc = arg;
    169 	device_t ppbus = sc->ppbus;
    170 
    171 	mutex_spin_enter(&timecounter_lock);
    172 	pps_capture(&sc->pps_state);
    173 	if (!(ppbus_rstr(ppbus) & nACK)) {
    174 		mutex_spin_exit(&timecounter_lock);
    175 		return;
    176 	}
    177 	if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
    178 		ppbus_wctr(ppbus, IRQENABLE | AUTOFEED);
    179 	pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
    180 	if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
    181 		ppbus_wctr(ppbus, IRQENABLE);
    182 	mutex_spin_exit(&timecounter_lock);
    183 }
    184 
    185 static int
    186 ppsioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    187 {
    188 	struct pps_softc *sc = device_lookup_private(&pps_cd, minor(dev));
    189 	int error = 0;
    190 
    191 	switch (cmd) {
    192 	case PPS_IOC_CREATE:
    193 	case PPS_IOC_DESTROY:
    194 	case PPS_IOC_GETPARAMS:
    195 	case PPS_IOC_SETPARAMS:
    196 	case PPS_IOC_GETCAP:
    197 	case PPS_IOC_FETCH:
    198 #ifdef PPS_SYNC
    199 	case PPS_IOC_KCBIND:
    200 #endif
    201 		mutex_spin_enter(&timecounter_lock);
    202 		error = pps_ioctl(cmd, data, &sc->pps_state);
    203 		mutex_spin_exit(&timecounter_lock);
    204 		break;
    205 
    206 	default:
    207 		error = EPASSTHROUGH;
    208 		break;
    209 	}
    210 	return (error);
    211 }
    212