pps_ppbus.c revision 1.9.16.1 1 /* pps_ppbus.c,v 1.9 2007/03/04 06:02:28 christos 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, "pps_ppbus.c,v 1.9 2007/03/04 06:02:28 christos 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 struct device *ppbus;
51 int busy;
52 struct pps_state pps_state; /* pps state */
53 };
54
55 static int pps_probe(struct device *, struct cfdata *, void *);
56 static void pps_attach(struct device *, struct device *, void *);
57 CFATTACH_DECL(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 ppsopen, ppsclose, noread, nowrite, ppsioctl,
66 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
67 };
68
69 static void ppsintr(void *arg);
70
71 static int
72 pps_probe(struct device *parent, struct cfdata *match, void *aux)
73 {
74 struct ppbus_attach_args *args = aux;
75
76 /* we need an interrupt */
77 if (!(args->capabilities & PPBUS_HAS_INTR))
78 return 0;
79
80 return 1;
81 }
82
83 static void
84 pps_attach(struct device *parent, struct device *self, void *aux)
85 {
86 struct pps_softc *sc = device_private(self);
87
88 sc->ppbus = parent;
89
90 printf("\n");
91 }
92
93 static int
94 ppsopen(dev_t dev, int flags, int fmt, struct lwp *l)
95 {
96 struct pps_softc *sc;
97 int res, weg = 0;
98
99 sc = device_lookup(&pps_cd, minor(dev));
100 if (!sc)
101 return (ENXIO);
102
103 if (sc->busy)
104 return (0);
105
106 if (ppbus_request_bus(sc->ppbus, &sc->pps_dev.sc_dev,
107 PPBUS_WAIT|PPBUS_INTR, 0))
108 return (EINTR);
109
110 ppbus_write_ivar(sc->ppbus, PPBUS_IVAR_IEEE, &weg);
111
112 /* attach the interrupt handler */
113 /* XXX priority should be set here */
114 res = ppbus_add_handler(sc->ppbus, ppsintr, sc);
115 if (res) {
116 ppbus_release_bus(sc->ppbus, &sc->pps_dev.sc_dev,
117 PPBUS_WAIT, 0);
118 return (res);
119 }
120
121 ppbus_set_mode(sc->ppbus, PPBUS_PS2, 0);
122 ppbus_wctr(sc->ppbus, IRQENABLE | PCD | nINIT | SELECTIN);
123
124 memset((void *)&sc->pps_state, 0, sizeof(sc->pps_state));
125 sc->pps_state.ppscap = PPS_CAPTUREASSERT;
126 pps_init(&sc->pps_state);
127
128 sc->busy = 1;
129 return (0);
130 }
131
132 static int
133 ppsclose(dev_t dev, int flags, int fmt, struct lwp *l)
134 {
135 struct pps_softc *sc = device_lookup(&pps_cd, minor(dev));
136 struct device *ppbus = sc->ppbus;
137
138 sc->busy = 0;
139 sc->pps_state.ppsparam.mode = 0;
140
141 ppbus_wdtr(ppbus, 0);
142 ppbus_wctr(ppbus, 0);
143
144 ppbus_remove_handler(ppbus, ppsintr);
145 ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
146 ppbus_release_bus(ppbus, &sc->pps_dev.sc_dev, PPBUS_WAIT, 0);
147 return (0);
148 }
149
150 static void
151 ppsintr(void *arg)
152 {
153 struct pps_softc *sc = arg;
154 struct device *ppbus = sc->ppbus;
155
156 pps_capture(&sc->pps_state);
157
158 if (!(ppbus_rstr(ppbus) & nACK))
159 return;
160
161 if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
162 ppbus_wctr(ppbus, IRQENABLE | AUTOFEED);
163
164 pps_event(&sc->pps_state, PPS_CAPTUREASSERT);
165
166 if (sc->pps_state.ppsparam.mode & PPS_ECHOASSERT)
167 ppbus_wctr(ppbus, IRQENABLE);
168 }
169
170 static int
171 ppsioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
172 {
173 struct pps_softc *sc = device_lookup(&pps_cd, minor(dev));
174 int error = 0;
175
176 switch (cmd) {
177 case PPS_IOC_CREATE:
178 case PPS_IOC_DESTROY:
179 case PPS_IOC_GETPARAMS:
180 case PPS_IOC_SETPARAMS:
181 case PPS_IOC_GETCAP:
182 case PPS_IOC_FETCH:
183 #ifdef PPS_SYNC
184 case PPS_IOC_KCBIND:
185 #endif
186 error = pps_ioctl(cmd, data, &sc->pps_state);
187 break;
188
189 default:
190 error = EPASSTHROUGH;
191 break;
192 }
193 return (error);
194 }
195