pps.c revision 46589
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 * $Id: pps.c,v 1.16 1999/04/25 08:58:10 phk Exp $
10 *
11 * This driver implements a draft-mogul-pps-api-02.txt PPS source.
12 *
13 * The input pin is pin#10
14 * The echo output pin is pin#14
15 *
16 */
17
18#include "opt_devfs.h"
19
20#include <sys/param.h>
21#include <sys/kernel.h>
22#include <sys/systm.h>
23#include <sys/conf.h>
24#include <sys/timepps.h>
25#ifdef DEVFS
26#include <sys/devfsext.h>
27#endif
28#include <sys/malloc.h>
29
30#include <dev/ppbus/ppbconf.h>
31#include "pps.h"
32
33#define PPS_NAME	"lppps"		/* our official name */
34
35static struct pps_data {
36	int	pps_unit;
37	int	pps_open;
38	struct	ppb_device pps_dev;
39	struct	pps_state pps;
40} *softc[NPPS];
41
42static int npps;
43
44/*
45 * Make ourselves visible as a ppbus driver
46 */
47
48static struct ppb_device	*ppsprobe(struct ppb_data *ppb);
49static int			ppsattach(struct ppb_device *dev);
50static void			ppsintr(int unit);
51
52static struct ppb_driver ppsdriver = {
53    ppsprobe, ppsattach, PPS_NAME
54};
55
56DATA_SET(ppbdriver_set, ppsdriver);
57
58static	d_open_t	ppsopen;
59static	d_close_t	ppsclose;
60static	d_ioctl_t	ppsioctl;
61
62#define CDEV_MAJOR 89
63static struct cdevsw pps_cdevsw =
64	{ ppsopen,	ppsclose,	noread,		nowrite,
65	  ppsioctl,	nullstop,	nullreset,	nodevtotty,
66	  seltrue,	nommap,		nostrat,	PPS_NAME,
67	  NULL,		-1 };
68
69
70static struct ppb_device *
71ppsprobe(struct ppb_data *ppb)
72{
73	struct pps_data *sc;
74
75	sc = (struct pps_data *) malloc(sizeof(struct pps_data),
76							M_TEMP, M_NOWAIT);
77	if (!sc) {
78		printf(PPS_NAME ": cannot malloc!\n");
79		return (0);
80	}
81	bzero(sc, sizeof(struct pps_data));
82
83	softc[npps] = sc;
84
85	sc->pps_unit = npps++;
86
87	sc->pps_dev.id_unit = sc->pps_unit;
88	sc->pps_dev.ppb = ppb;
89	sc->pps_dev.name = ppsdriver.name;
90	sc->pps_dev.intr = ppsintr;
91
92	sc->pps.ppscap = PPS_CAPTUREASSERT | PPS_ECHOASSERT;
93	pps_init(&sc->pps);
94	return (&sc->pps_dev);
95}
96
97static int
98ppsattach(struct ppb_device *dev)
99{
100	dev_t devt;
101
102	/*
103	 * Report ourselves
104	 */
105	printf(PPS_NAME "%d: <Pulse per second Timing Interface> on ppbus %d\n",
106	       dev->id_unit, dev->ppb->ppb_link->adapter_unit);
107
108#ifdef DEVFS
109	devfs_add_devswf(&pps_cdevsw,
110		dev->id_unit, DV_CHR,
111		UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", dev->id_unit);
112#endif
113	devt = makedev(CDEV_MAJOR, 0);
114	cdevsw_add(&devt, &pps_cdevsw, NULL);
115	return (1);
116}
117
118static	int
119ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
120{
121	struct pps_data *sc;
122	u_int unit = minor(dev);
123
124	if ((unit >= npps))
125		return (ENXIO);
126
127	sc = softc[unit];
128
129	if (!sc->pps_open) {
130		if (ppb_request_bus(&sc->pps_dev, PPB_WAIT|PPB_INTR))
131			return (EINTR);
132
133		ppb_wctr(&sc->pps_dev, 0);
134		ppb_wctr(&sc->pps_dev, IRQENABLE);
135		sc->pps_open = 1;
136	}
137
138	return(0);
139}
140
141static	int
142ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
143{
144	struct pps_data *sc = softc[minor(dev)];
145
146	sc->pps.ppsparam.mode = 0;	/* PHK ??? */
147
148	ppb_wdtr(&sc->pps_dev, 0);
149	ppb_wctr(&sc->pps_dev, 0);
150
151	ppb_release_bus(&sc->pps_dev);
152	sc->pps_open = 0;
153	return(0);
154}
155
156static void
157ppsintr(int unit)
158{
159	struct pps_data *sc = softc[unit];
160	struct timecounter *tc;
161	unsigned count;
162
163	tc = timecounter;
164	count = timecounter->tc_get_timecount(tc);
165	if (!(ppb_rstr(&sc->pps_dev) & nACK))
166		return;
167	if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
168		ppb_wctr(&sc->pps_dev, IRQENABLE | AUTOFEED);
169	pps_event(&sc->pps, tc, count, PPS_CAPTUREASSERT);
170	if (sc->pps.ppsparam.mode & PPS_ECHOASSERT)
171		ppb_wctr(&sc->pps_dev, IRQENABLE);
172}
173
174static int
175ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
176{
177	struct pps_data *sc = softc[minor(dev)];
178
179	return (pps_ioctl(cmd, data, &sc->pps));
180}
181
182