1 /***********************************************************************
3 * Copyright (c) David L. Mills 1999-2000 *
5 * Permission to use, copy, modify, and distribute this software and *
6 * its documentation for any purpose and without fee is hereby *
7 * granted, provided that the above copyright notice appears in all *
8 * copies and that both the copyright notice and this permission *
9 * notice appear in supporting documentation, and that the name *
10 * University of Delaware not be used in advertising or publicity *
11 * pertaining to distribution of the software without specific, *
12 * written prior permission. The University of Delaware makes no *
13 * representations about the suitability this software for any *
14 * purpose. It is provided "as is" without express or implied *
17 ***********************************************************************
19 * This header file complies with "Pulse-Per-Second API for UNIX-like *
20 * Operating Systems, Version 1.0", rfc2783. Credit is due Jeff Mogul *
21 * and Marc Brett, from whom much of this code was shamelessly stolen. *
23 * this modified timepps.h can be used to provide a PPSAPI interface *
24 * to a machine running SCO Unix. *
26 ***********************************************************************
28 * A full PPSAPI interface to the SCO Unix kernel would be better, but *
29 * this at least removes the necessity for special coding from the NTP *
32 ***********************************************************************
34 * Some of this include file *
35 * Copyright (c) 1999 by Ulrich Windl, *
36 * based on code by Reg Clemens <reg@dwf.com> *
37 * based on code by Poul-Henning Kamp <phk@FreeBSD.org> *
39 ***********************************************************************
41 * "THE BEER-WARE LICENSE" (Revision 42): *
42 * <phk@FreeBSD.org> wrote this file. As long as you retain this *
43 * notice you can do whatever you want with this stuff. If we meet some*
44 * day, and you think this stuff is worth it, you can buy me a beer *
45 * in return. Poul-Henning Kamp *
47 **********************************************************************/
49 /*SCO UNIX version, TIOCDCDTIMESTAMP assumed to exist. */
51 #ifndef _SYS_TIMEPPS_H_
52 #define _SYS_TIMEPPS_H_
54 #include <termios.h> /* to get TIOCDCDTIMESTAMP */
56 /* Implementation note: the logical states ``assert'' and ``clear''
57 * are implemented in terms of the UART register, i.e. ``assert''
58 * means the bit is set.
62 * The following definitions are architecture independent
65 #define PPS_API_VERS_1 1 /* API version number */
66 #define PPS_JAN_1970 2208988800UL /* 1970 - 1900 in seconds */
67 #define PPS_NANOSECOND 1000000000L /* one nanosecond in decimal */
68 #define PPS_FRAC 4294967296. /* 2^32 as a double */
70 #define PPS_NORMALIZE(x) /* normalize timespec */ \
72 if ((x).tv_nsec >= PPS_NANOSECOND) { \
73 (x).tv_nsec -= PPS_NANOSECOND; \
75 } else if ((x).tv_nsec < 0) { \
76 (x).tv_nsec += PPS_NANOSECOND; \
81 #define PPS_TSPECTONTP(x) /* convert timespec to l_fp */ \
85 (x).integral += (unsigned int)PPS_JAN_1970; \
86 d_temp = (x).fractional * PPS_FRAC / PPS_NANOSECOND; \
87 if (d_temp >= PPS_FRAC) \
89 (x).fractional = (unsigned int)d_temp; \
93 * Device/implementation parameters (mode)
96 #define PPS_CAPTUREASSERT 0x01 /* capture assert events */
97 #define PPS_CAPTURECLEAR 0x02 /* capture clear events */
98 #define PPS_CAPTUREBOTH 0x03 /* capture assert and clear events */
100 #define PPS_OFFSETASSERT 0x10 /* apply compensation for assert ev. */
101 #define PPS_OFFSETCLEAR 0x20 /* apply compensation for clear ev. */
102 #define PPS_OFFSETBOTH 0x30 /* apply compensation for both */
104 #define PPS_CANWAIT 0x100 /* Can we wait for an event? */
105 #define PPS_CANPOLL 0x200 /* "This bit is reserved for */
108 * Kernel actions (mode)
111 #define PPS_ECHOASSERT 0x40 /* feed back assert event to output */
112 #define PPS_ECHOCLEAR 0x80 /* feed back clear event to output */
115 * Timestamp formats (tsformat)
118 #define PPS_TSFMT_TSPEC 0x1000 /* select timespec format */
119 #define PPS_TSFMT_NTPFP 0x2000 /* select NTP format */
122 * Kernel discipline actions (not used in Solaris)
125 #define PPS_KC_HARDPPS 0 /* enable kernel consumer */
126 #define PPS_KC_HARDPPS_PLL 1 /* phase-lock mode */
127 #define PPS_KC_HARDPPS_FLL 2 /* frequency-lock mode */
133 typedef unsigned long pps_seq_t; /* sequence number */
135 typedef struct ntp_fp {
136 unsigned int integral;
137 unsigned int fractional;
138 } ntp_fp_t; /* NTP-compatible time stamp */
140 typedef union pps_timeu { /* timestamp format */
141 struct timespec tspec;
143 unsigned long longpad[3];
144 } pps_timeu_t; /* generic data type to represent time stamps */
147 * Timestamp information structure
150 typedef struct pps_info {
151 pps_seq_t assert_sequence; /* seq. num. of assert event */
152 pps_seq_t clear_sequence; /* seq. num. of clear event */
153 pps_timeu_t assert_tu; /* time of assert event */
154 pps_timeu_t clear_tu; /* time of clear event */
155 int current_mode; /* current mode bits */
158 #define assert_timestamp assert_tu.tspec
159 #define clear_timestamp clear_tu.tspec
161 #define assert_timestamp_ntpfp assert_tu.ntpfp
162 #define clear_timestamp_ntpfp clear_tu.ntpfp
165 * Parameter structure
168 typedef struct pps_params {
169 int api_version; /* API version # */
170 int mode; /* mode bits */
171 pps_timeu_t assert_off_tu; /* offset compensation for assert */
172 pps_timeu_t clear_off_tu; /* offset compensation for clear */
175 #define assert_offset assert_off_tu.tspec
176 #define clear_offset clear_off_tu.tspec
178 #define assert_offset_ntpfp assert_off_tu.ntpfp
179 #define clear_offset_ntpfp clear_off_tu.ntpfp
182 * The following definitions are architecture-dependent
185 #define PPS_CAP (PPS_CAPTUREASSERT | PPS_OFFSETASSERT | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
186 #define PPS_RO (PPS_CANWAIT | PPS_CANPOLL | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
189 int filedes; /* file descriptor */
190 pps_params_t params; /* PPS parameters set by user */
191 struct timeval tv_save;
195 typedef pps_unit_t* pps_handle_t; /* pps handlebars */
198 *------ Here begins the implementation-specific part! ------
204 * create PPS handle from file descriptor
209 int filedes, /* file descriptor */
210 pps_handle_t *handle /* returned handle */
216 * Check for valid arguments and attach PPS signal.
221 return (-1); /* null pointer */
225 * Allocate and initialize default unit structure.
228 *handle = malloc(sizeof(pps_unit_t));
231 return (-1); /* what, no memory? */
234 memset(*handle, 0, sizeof(pps_unit_t));
235 (*handle)->filedes = filedes;
236 (*handle)->params.api_version = PPS_API_VERS_1;
237 (*handle)->params.mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
251 * Check for valid arguments and detach PPS signal.
256 return (-1); /* bad handle */
263 * set parameters for handle
269 const pps_params_t *params
274 * Check for valid arguments and set parameters.
279 return (-1); /* bad handle */
284 return (-1); /* bad argument */
288 * There was no reasonable consensu in the API working group.
289 * I require `api_version' to be set!
292 if (params->api_version != PPS_API_VERS_1) {
298 * only settable modes are PPS_CAPTUREASSERT and PPS_OFFSETASSERT
301 mode_in = params->mode;
303 /* turn off read-only bits */
307 /* test remaining bits, should only have captureassert and/or offsetassert */
309 if (mode_in & ~(PPS_CAPTUREASSERT | PPS_OFFSETASSERT)) {
318 mode = handle->params.mode;
319 memcpy(&handle->params, params, sizeof(pps_params_t));
320 handle->params.api_version = PPS_API_VERS_1;
321 handle->params.mode = mode | mode_in;
326 * get parameters for handle
336 * Check for valid arguments and get parameters.
341 return (-1); /* bad handle */
346 return (-1); /* bad argument */
349 memcpy(params, &handle->params, sizeof(pps_params_t));
354 * get capabilities for handle
364 * Check for valid arguments and get capabilities.
369 return (-1); /* bad handle */
374 return (-1); /* bad argument */
389 const struct timespec *timeout
396 * Check for valid arguments and fetch timestamps
401 return (-1); /* bad handle */
406 return (-1); /* bad argument */
410 * nb. PPS_CANWAIT is NOT set by the implementation, we can totally
411 * ignore the timeout variable.
414 memset(&infobuf, 0, sizeof(infobuf));
417 * if not captureassert, nothing to return.
420 if (!handle->params.mode & PPS_CAPTUREASSERT) {
421 memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
425 if (ioctl(instance->filedes, TIOCDCDTIMESTAMP, &tv) < 0) {
426 perror("time_pps_fetch:");
435 if (tv.tv_sec != handle->tv_save.tv_sec || tv.tv_usec != handle->tv_save.tv_usec) {
436 handle->tv_save = tv;
441 * Apply offsets as specified. Note that only assert timestamps
442 * are captured by this interface.
445 infobuf.assert_sequence = handle->serial;
446 infobuf.assert_timestamp.tv_sec = tv.tv_sec;
447 infobuf.assert_timestamp.tv_nsec = tv.tv_usec * 1000;
449 if (handle->params.mode & PPS_OFFSETASSERT) {
450 infobuf.assert_timestamp.tv_sec += handle->params.assert_offset.tv_sec;
451 infobuf.assert_timestamp.tv_nsec += handle->params.assert_offset.tv_nsec;
452 PPS_NORMALIZE(infobuf.assert_timestamp);
456 * Translate to specified format
460 case PPS_TSFMT_TSPEC:
461 break; /* timespec format requires no translation */
463 case PPS_TSFMT_NTPFP: /* NTP format requires conversion to fraction form */
464 PPS_TSPECTONTP(infobuf.assert_timestamp_ntpfp);
472 infobuf.current_mode = handle->params.mode;
473 memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
478 * specify kernel consumer
484 const int kernel_consumer,
485 const int edge, const int tsformat
489 * Check for valid arguments and bind kernel consumer
493 return (-1); /* bad handle */
495 if (geteuid() != 0) {
497 return (-1); /* must be superuser */
503 #endif /* _SYS_TIMEPPS_H_ */