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 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/dev/ppbus/pps.c,v 1.24.2.1 2000/05/24 00:20:57 n_hibma Exp $
10 * $DragonFly: src/sys/dev/misc/pps/pps.c,v 1.17 2006/10/25 20:55:55 dillon Exp $
12 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
14 * The input pin is pin#10
15 * The echo output pin is pin#14
21 #include <sys/param.h>
22 #include <sys/kernel.h>
23 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/module.h>
28 #include <sys/timepps.h>
31 #include <bus/ppbus/ppbconf.h>
32 #include <bus/ppbus/ppbio.h>
36 #define PPS_NAME "pps" /* our official name */
40 struct ppb_device pps_dev;
43 struct resource *intr_resource; /* interrupt resource */
44 void *intr_cookie; /* interrupt registration cookie */
47 static void ppsintr(void *arg);
49 #define DEVTOSOFTC(dev) \
50 ((struct pps_data *)device_get_softc(dev))
51 #define UNITOSOFTC(unit) \
52 ((struct pps_data *)devclass_get_softc(pps_devclass, (unit)))
53 #define UNITODEVICE(unit) \
54 (devclass_get_device(pps_devclass, (unit)))
56 static devclass_t pps_devclass;
58 static d_open_t ppsopen;
59 static d_close_t ppsclose;
60 static d_ioctl_t ppsioctl;
63 static struct dev_ops pps_ops = {
64 { PPS_NAME, CDEV_MAJOR, 0 },
71 ppsprobe(device_t ppsdev)
75 sc = DEVTOSOFTC(ppsdev);
76 bzero(sc, sizeof(struct pps_data));
78 device_set_desc(ppsdev, "Pulse per second Timing Interface");
80 sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
86 ppsattach(device_t ppsdev)
88 struct pps_data *sc = DEVTOSOFTC(ppsdev);
89 device_t ppbus = device_get_parent(ppsdev);
94 /* retrieve the ppbus irq */
95 BUS_READ_IVAR(ppbus, ppsdev, PPBUS_IVAR_IRQ, &irq);
98 /* declare our interrupt handler */
99 sc->intr_resource = bus_alloc_resource(ppsdev, SYS_RES_IRQ,
100 &zero, irq, irq, 1, RF_SHAREABLE);
102 /* interrupts seem mandatory */
103 if (sc->intr_resource == 0)
106 unit = device_get_unit(ppsdev);
107 make_dev(&pps_ops, unit, UID_ROOT, GID_WHEEL,
108 0644, PPS_NAME "%d", unit);
113 ppsopen(struct dev_open_args *ap)
115 cdev_t dev = ap->a_head.a_dev;
116 u_int unit = minor(dev);
117 struct pps_data *sc = UNITOSOFTC(unit);
118 device_t ppsdev = UNITODEVICE(unit);
119 device_t ppbus = device_get_parent(ppsdev);
123 if (ppb_request_bus(ppbus, ppsdev, PPB_WAIT|PPB_INTR))
126 /* attach the interrupt handler */
127 if ((error = BUS_SETUP_INTR(ppbus, ppsdev, sc->intr_resource,
129 &sc->intr_cookie, NULL))) {
130 ppb_release_bus(ppbus, ppsdev);
135 ppb_wctr(ppbus, IRQENABLE);
143 ppsclose(struct dev_close_args *ap)
145 cdev_t dev = ap->a_head.a_dev;
146 u_int unit = minor(dev);
147 struct pps_data *sc = UNITOSOFTC(unit);
148 device_t ppsdev = UNITODEVICE(unit);
149 device_t ppbus = device_get_parent(ppsdev);
151 sc->pps.ppsparam.mode = 0; /* PHK ??? */
156 /* Note: the interrupt handler is automatically detached */
157 ppb_release_bus(ppbus, ppsdev);
165 device_t ppsdev = (device_t)arg;
166 device_t ppbus = device_get_parent(ppsdev);
167 struct pps_data *sc = DEVTOSOFTC(ppsdev);
170 count = sys_cputimer->count();
171 if (!(ppb_rstr(ppbus) & nACK))
173 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
174 ppb_wctr(ppbus, IRQENABLE | AUTOFEED);
175 pps_event(&sc->pps, count, PPS_CAPTUREASSERT);
176 if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
177 ppb_wctr(ppbus, IRQENABLE);
181 ppsioctl(struct dev_ioctl_args *ap)
183 cdev_t dev = ap->a_head.a_dev;
184 u_int unit = minor(dev);
185 struct pps_data *sc = UNITOSOFTC(unit);
187 return (pps_ioctl(ap->a_cmd, ap->a_data, &sc->pps));
191 * Becuase pps is a static device under any attached ppbus, and not scanned
192 * by the ppbus, we need an identify function to create the device.
194 static device_method_t pps_methods[] = {
195 /* device interface */
196 DEVMETHOD(device_identify, bus_generic_identify),
197 DEVMETHOD(device_probe, ppsprobe),
198 DEVMETHOD(device_attach, ppsattach),
203 static driver_t pps_driver = {
206 sizeof(struct pps_data),
208 DRIVER_MODULE(pps, ppbus, pps_driver, pps_devclass, 0, 0);