Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / dev / misc / pps / pps.c
CommitLineData
984263bc
MD
1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 *
9 * $FreeBSD: src/sys/dev/ppbus/pps.c,v 1.24.2.1 2000/05/24 00:20:57 n_hibma Exp $
b13267a5 10 * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.16 2006/09/10 01:26:34 dillon Exp $
984263bc
MD
11 *
12 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
13 *
14 * The input pin is pin#10
15 * The echo output pin is pin#14
16 *
17 */
18
1f2de5d4
MD
19#include "use_pps.h"
20
984263bc
MD
21#include <sys/param.h>
22#include <sys/kernel.h>
23#include <sys/systm.h>
fef8985e
MD
24#include <sys/conf.h>
25#include <sys/device.h>
984263bc
MD
26#include <sys/module.h>
27#include <sys/bus.h>
984263bc
MD
28#include <sys/timepps.h>
29#include <machine/bus.h>
30#include <machine/resource.h>
31#include <sys/rman.h>
32
1f2de5d4 33#include <bus/ppbus/ppbconf.h>
984263bc 34#include "ppbus_if.h"
1f2de5d4 35#include <bus/ppbus/ppbio.h>
984263bc
MD
36
37#define PPS_NAME "pps" /* our official name */
38
39struct pps_data {
40 int pps_open;
41 struct ppb_device pps_dev;
42 struct pps_state pps;
43
44 struct resource *intr_resource; /* interrupt resource */
45 void *intr_cookie; /* interrupt registration cookie */
46};
47
48static void ppsintr(void *arg);
49
50#define DEVTOSOFTC(dev) \
51 ((struct pps_data *)device_get_softc(dev))
52#define UNITOSOFTC(unit) \
53 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
54#define UNITODEVICE(unit) \
55 (devclass_get_device(pps_devclass, (unit)))
56
57static devclass_t pps_devclass;
58
59static d_open_t ppsopen;
60static d_close_t ppsclose;
61static d_ioctl_t ppsioctl;
62
63#define CDEV_MAJOR 89
fef8985e
MD
64static struct dev_ops pps_ops = {
65 { PPS_NAME, CDEV_MAJOR, 0 },
66 .d_open = ppsopen,
67 .d_close = ppsclose,
68 .d_ioctl = ppsioctl,
984263bc
MD
69};
70
984263bc
MD
71static int
72ppsprobe(device_t ppsdev)
73{
74 struct pps_data *sc;
984263bc
MD
75
76 sc = DEVTOSOFTC(ppsdev);
77 bzero(sc, sizeof(struct pps_data));
78
984263bc
MD
79 device_set_desc(ppsdev, "Pulse per second Timing Interface");
80
81 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
82 pps_init(&sc->pps);
83 return (0);
84}
85
86static int
d471ee7d 87ppsattach(device_t ppsdev)
984263bc 88{
d471ee7d
MD
89 struct pps_data *sc = DEVTOSOFTC(ppsdev);
90 device_t ppbus = device_get_parent(ppsdev);
91 int irq;
e4c9c0c8 92 int unit;
d471ee7d 93 int zero = 0;
984263bc
MD
94
95 /* retrieve the ppbus irq */
d471ee7d 96 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq);
984263bc
MD
97
98 if (irq > 0) {
99 /* declare our interrupt handler */
d471ee7d 100 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ,
984263bc
MD
101 &zero, irq, irq, 1, RF_SHAREABLE);
102 }
103 /* interrupts seem mandatory */
104 if (sc->intr_resource == 0)
105 return (ENXIO);
106
e4c9c0c8 107 unit = device_get_unit(ppsdev);
fef8985e
MD
108 dev_ops_add(&pps_ops, -1, unit);
109 make_dev(&pps_ops, unit, UID_ROOT, GID_WHEEL, 0644,
d471ee7d 110 PPS_NAME "%d", unit);
984263bc
MD
111 return (0);
112}
113
114static int
fef8985e 115ppsopen(struct dev_open_args *ap)
984263bc 116{
b13267a5 117 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
118 u_int unit = minor(dev);
119 struct pps_data *sc = UNITOSOFTC(unit);
120 device_t ppsdev = UNITODEVICE(unit);
121 device_t ppbus = device_get_parent(ppsdev);
122 int error;
123
124 if (!sc->pps_open) {
125 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
126 return (EINTR);
127
128 /* attach the interrupt handler */
129 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
ee61f228 130 0, ppsintr, ppsdev,
e9cb6d99 131 &sc->intr_cookie, NULL))) {
984263bc
MD
132 ppb_release_bus(ppbus, ppsdev);
133 return (error);
134 }
135
136 ppb_wctr(ppbus, 0);
137 ppb_wctr(ppbus, IRQENABLE);
138 sc->pps_open = 1;
139 }
140
141 return(0);
142}
143
144static int
fef8985e 145ppsclose(struct dev_close_args *ap)
984263bc 146{
b13267a5 147 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
148 u_int unit = minor(dev);
149 struct pps_data *sc = UNITOSOFTC(unit);
150 device_t ppsdev = UNITODEVICE(unit);
151 device_t ppbus = device_get_parent(ppsdev);
152
153 sc->pps.ppsparam.mode = 0; /* PHK ??? */
154
155 ppb_wdtr(ppbus, 0);
156 ppb_wctr(ppbus, 0);
157
158 /* Note: the interrupt handler is automatically detached */
159 ppb_release_bus(ppbus, ppsdev);
160 sc->pps_open = 0;
161 return(0);
162}
163
164static void
165ppsintr(void *arg)
166{
167 device_t ppsdev = (device_t)arg;
168 device_t ppbus = device_get_parent(ppsdev);
169 struct pps_data *sc = DEVTOSOFTC(ppsdev);
88c4d2f6 170 sysclock_t count;
984263bc 171
044ee7c4 172 count = sys_cputimer->count();
984263bc
MD
173 if (!(ppb_rstr(ppbus) & nACK))
174 return;
175 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
176 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
88c4d2f6 177 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
984263bc
MD
178 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
179 ppb_wctr(ppbus, IRQENABLE);
180}
181
182static int
fef8985e 183ppsioctl(struct dev_ioctl_args *ap)
984263bc 184{
b13267a5 185 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
186 u_int unit = minor(dev);
187 struct pps_data *sc = UNITOSOFTC(unit);
188
fef8985e 189 return (pps_ioctl(ap->a_cmd, ap->a_data, &sc->pps));
984263bc
MD
190}
191
39b5d600
MD
192/*
193 * Becuase pps is a static device under any attached ppbus, and not scanned
194 * by the ppbus, we need an identify function to create the device.
195 */
984263bc
MD
196static device_method_t pps_methods[] = {
197 /* device interface */
39b5d600 198 DEVMETHOD(device_identify, bus_generic_identify),
984263bc
MD
199 DEVMETHOD(device_probe, ppsprobe),
200 DEVMETHOD(device_attach, ppsattach),
201
202 { 0, 0 }
203};
204
205static driver_t pps_driver = {
206 PPS_NAME,
207 pps_methods,
208 sizeof(struct pps_data),
209};
210DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);