1998-02-13 13:00:02 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*
|
1998-06-08 02:43:12 +00:00
|
|
|
* $Id: pps.c,v 1.5 1998/06/07 19:44:22 phk Exp $
|
1998-02-13 13:00:02 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
#include "opt_devfs.h"
|
|
|
|
|
1998-02-13 13:00:02 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/uio.h>
|
1998-06-07 19:44:22 +00:00
|
|
|
#include <sys/timepps.h>
|
1998-02-13 13:00:02 +00:00
|
|
|
#ifdef DEVFS
|
|
|
|
#include <sys/devfsext.h>
|
1998-02-15 14:54:09 +00:00
|
|
|
#endif
|
1998-02-13 13:00:02 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
|
|
|
|
#include <dev/ppbus/ppbconf.h>
|
1998-02-13 17:35:33 +00:00
|
|
|
#include "pps.h"
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
#define PPS_NAME "lppps" /* our official name */
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static struct pps_data {
|
|
|
|
int pps_unit;
|
|
|
|
struct ppb_device pps_dev;
|
1998-06-07 19:44:22 +00:00
|
|
|
pps_params_t ppsparam;
|
|
|
|
pps_info_t ppsinfo;
|
1998-02-13 17:35:33 +00:00
|
|
|
} *softc[NPPS];
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
static int ppscap =
|
|
|
|
PPS_CAPTUREASSERT |
|
|
|
|
PPS_HARDPPSONASSERT |
|
|
|
|
PPS_OFFSETASSERT |
|
|
|
|
PPS_ECHOASSERT;
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static int npps;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make ourselves visible as a ppbus driver
|
|
|
|
*/
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static struct ppb_device *ppsprobe(struct ppb_data *ppb);
|
|
|
|
static int ppsattach(struct ppb_device *dev);
|
|
|
|
static void ppsintr(int unit);
|
|
|
|
static void pps_drvinit(void *unused);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static struct ppb_driver ppsdriver = {
|
|
|
|
ppsprobe, ppsattach, PPS_NAME
|
1998-02-13 13:00:02 +00:00
|
|
|
};
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
DATA_SET(ppbdriver_set, ppsdriver);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static d_open_t ppsopen;
|
|
|
|
static d_close_t ppsclose;
|
1998-06-07 19:44:22 +00:00
|
|
|
static d_ioctl_t ppsioctl;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
#define CDEV_MAJOR 89
|
1998-02-15 14:54:09 +00:00
|
|
|
static struct cdevsw pps_cdevsw =
|
1998-06-07 19:44:22 +00:00
|
|
|
{ ppsopen, ppsclose, noread, nowrite,
|
|
|
|
ppsioctl, nullstop, nullreset, nodevtotty,
|
1998-02-13 17:35:33 +00:00
|
|
|
seltrue, nommap, nostrat, PPS_NAME,
|
1998-02-13 13:00:02 +00:00
|
|
|
NULL, -1 };
|
|
|
|
|
|
|
|
static struct ppb_device *
|
1998-02-15 14:54:09 +00:00
|
|
|
ppsprobe(struct ppb_data *ppb)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
1998-02-15 14:54:09 +00:00
|
|
|
struct pps_data *sc;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
sc = (struct pps_data *) malloc(sizeof(struct pps_data),
|
1998-02-13 13:00:02 +00:00
|
|
|
M_TEMP, M_NOWAIT);
|
|
|
|
if (!sc) {
|
1998-02-13 17:35:33 +00:00
|
|
|
printf(PPS_NAME ": cannot malloc!\n");
|
1998-02-13 13:00:02 +00:00
|
|
|
return (0);
|
|
|
|
}
|
1998-02-15 14:54:09 +00:00
|
|
|
bzero(sc, sizeof(struct pps_data));
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
softc[npps] = sc;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
sc->pps_unit = npps++;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
sc->pps_dev.id_unit = sc->pps_unit;
|
|
|
|
sc->pps_dev.ppb = ppb;
|
|
|
|
sc->pps_dev.intr = ppsintr;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
return (&sc->pps_dev);
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-02-15 14:54:09 +00:00
|
|
|
ppsattach(struct ppb_device *dev)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Report ourselves
|
|
|
|
*/
|
1998-02-13 17:35:33 +00:00
|
|
|
printf(PPS_NAME "%d: <Pulse per second Timing Interface> on ppbus %d\n",
|
1998-02-13 13:00:02 +00:00
|
|
|
dev->id_unit, dev->ppb->ppb_link->adapter_unit);
|
|
|
|
|
|
|
|
#ifdef DEVFS
|
1998-02-15 14:54:09 +00:00
|
|
|
devfs_add_devswf(&pps_cdevsw,
|
1998-02-13 13:00:02 +00:00
|
|
|
dev->id_unit, DV_CHR,
|
1998-02-13 17:35:33 +00:00
|
|
|
UID_ROOT, GID_WHEEL, 0600, PPS_NAME "%d", dev->id_unit);
|
1998-02-13 13:00:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-02-15 14:54:09 +00:00
|
|
|
ppsopen(dev_t dev, int flags, int fmt, struct proc *p)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
1998-02-15 14:54:09 +00:00
|
|
|
struct pps_data *sc;
|
1998-02-13 13:00:02 +00:00
|
|
|
u_int unit = minor(dev);
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
if ((unit >= npps))
|
1998-02-13 13:00:02 +00:00
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
sc = softc[unit];
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
if (ppb_request_bus(&sc->pps_dev, PPB_WAIT|PPB_INTR))
|
1998-02-13 13:00:02 +00:00
|
|
|
return (EINTR);
|
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
ppb_wctr(&sc->pps_dev, IRQENABLE);
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-02-15 14:54:09 +00:00
|
|
|
ppsclose(dev_t dev, int flags, int fmt, struct proc *p)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
1998-02-15 14:54:09 +00:00
|
|
|
struct pps_data *sc = softc[minor(dev)];
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
sc->ppsparam.mode = 0;
|
1998-02-15 14:54:09 +00:00
|
|
|
ppb_release_bus(&sc->pps_dev);
|
1998-02-13 13:00:02 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1998-02-15 14:54:09 +00:00
|
|
|
ppsintr(int unit)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
1998-02-15 14:54:09 +00:00
|
|
|
struct pps_data *sc = softc[unit];
|
1998-02-13 13:00:02 +00:00
|
|
|
struct timespec tc;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
nanotime(&tc);
|
1998-02-15 14:54:09 +00:00
|
|
|
if (!(ppb_rstr(&sc->pps_dev) & nACK))
|
1998-02-13 13:00:02 +00:00
|
|
|
return;
|
1998-06-07 19:44:22 +00:00
|
|
|
if (sc->ppsparam.mode & PPS_ECHOASSERT)
|
|
|
|
ppb_wctr(&sc->pps_dev, IRQENABLE | AUTOFEED);
|
|
|
|
timespecadd(&tc, &sc->ppsparam.assert_offset);
|
|
|
|
if (tc.tv_nsec < 0) {
|
1998-02-13 13:00:02 +00:00
|
|
|
tc.tv_sec--;
|
|
|
|
tc.tv_nsec += 1000000000;
|
|
|
|
}
|
1998-06-07 19:44:22 +00:00
|
|
|
sc->ppsinfo.assert_timestamp = tc;
|
|
|
|
sc->ppsinfo.assert_sequence++;
|
|
|
|
if (sc->ppsparam.mode & PPS_HARDPPSONASSERT) {
|
|
|
|
tv.tv_sec = tc.tv_sec;
|
|
|
|
tv.tv_usec = tc.tv_nsec / 1000;
|
|
|
|
hardpps(&tv, tv.tv_usec);
|
|
|
|
}
|
|
|
|
if (sc->ppsparam.mode & PPS_ECHOASSERT)
|
|
|
|
ppb_wctr(&sc->pps_dev, IRQENABLE);
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
static int
|
1998-06-08 02:43:12 +00:00
|
|
|
ppsioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
1998-02-15 14:54:09 +00:00
|
|
|
struct pps_data *sc = softc[minor(dev)];
|
1998-06-07 19:44:22 +00:00
|
|
|
pps_params_t *pp;
|
|
|
|
pps_info_t *pi;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
1998-06-07 19:44:22 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case PPS_IOC_CREATE:
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_DESTROY:
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_SETPARAMS:
|
|
|
|
pp = (pps_params_t *)data;
|
|
|
|
if (pp->mode & ~ppscap)
|
|
|
|
return (EINVAL);
|
|
|
|
sc->ppsparam = *pp;
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_GETPARAMS:
|
|
|
|
pp = (pps_params_t *)data;
|
|
|
|
*pp = sc->ppsparam;
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_GETCAP:
|
|
|
|
*(int*)data = ppscap;
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_FETCH:
|
|
|
|
pi = (pps_info_t *)data;
|
|
|
|
*pi = sc->ppsinfo;
|
|
|
|
return (0);
|
|
|
|
case PPS_IOC_WAIT:
|
|
|
|
return (EOPNOTSUPP);
|
|
|
|
default:
|
|
|
|
return (ENODEV);
|
|
|
|
}
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
static pps_devsw_installed = 0;
|
1998-02-13 13:00:02 +00:00
|
|
|
|
|
|
|
static void
|
1998-02-15 14:54:09 +00:00
|
|
|
pps_drvinit(void *unused)
|
1998-02-13 13:00:02 +00:00
|
|
|
{
|
|
|
|
dev_t dev;
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
if( ! pps_devsw_installed ) {
|
1998-02-13 13:00:02 +00:00
|
|
|
dev = makedev(CDEV_MAJOR, 0);
|
1998-02-15 14:54:09 +00:00
|
|
|
cdevsw_add(&dev, &pps_cdevsw, NULL);
|
|
|
|
pps_devsw_installed = 1;
|
1998-02-13 13:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-15 14:54:09 +00:00
|
|
|
SYSINIT(ppsdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,pps_drvinit,NULL)
|