1 /***********************************************************************
3 * Copyright (c) David L. Mills 1999-2009 *
5 * Permission to use, copy, modify, and distribute this software and *
6 * its documentation for any purpose and with or 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 Solaris (2.6 and above). *
26 ***********************************************************************
28 * A full PPSAPI interface to the Solaris 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 /* Solaris version, TIOCGPPSEV and TIOCSPPS assumed to exist. */
51 #ifndef _SYS_TIMEPPS_H_
52 #define _SYS_TIMEPPS_H_
54 #include <termios.h> /* to get TOCGPPSEV and TIOCSPPS */
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
181 /* addition of NTP fixed-point format */
183 #define NTPFP_M_ADD(r_i, r_f, a_i, a_f) /* r += a */ \
185 register u_int32 lo_tmp; \
186 register u_int32 hi_tmp; \
188 lo_tmp = ((r_f) & 0xffff) + ((a_f) & 0xffff); \
189 hi_tmp = (((r_f) >> 16) & 0xffff) + (((a_f) >> 16) & 0xffff); \
190 if (lo_tmp & 0x10000) \
192 (r_f) = ((hi_tmp & 0xffff) << 16) | (lo_tmp & 0xffff); \
195 if (hi_tmp & 0x10000) \
199 #define NTPFP_L_ADDS(r, a) NTPFP_M_ADD((r)->integral, (r)->fractional, \
200 (int)(a)->integral, (a)->fractional)
203 * The following definitions are architecture-dependent
206 #define PPS_CAP (PPS_CAPTUREASSERT | PPS_OFFSETASSERT | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
207 #define PPS_RO (PPS_CANWAIT | PPS_CANPOLL)
210 int filedes; /* file descriptor */
211 pps_params_t params; /* PPS parameters set by user */
215 *------ Here begins the implementation-specific part! ------
221 * pps handlebars, which are required to be an opaque scalar. This
222 * implementation uses the handle as a pointer so it must be large
223 * enough. uintptr_t is as large as a pointer.
225 typedef uintptr_t pps_handle_t;
228 * create PPS handle from file descriptor
233 int filedes, /* file descriptor */
234 pps_handle_t *handle /* returned handle */
241 * Check for valid arguments and attach PPS signal.
246 return (-1); /* null pointer */
249 if (ioctl(filedes, TIOCSPPS, &one) < 0) {
250 perror("refclock_ioctl: TIOCSPPS failed:");
255 * Allocate and initialize default unit structure.
258 punit = malloc(sizeof(*punit));
261 return (-1); /* what, no memory? */
264 memset(punit, 0, sizeof(*punit));
265 punit->filedes = filedes;
266 punit->params.api_version = PPS_API_VERS_1;
267 punit->params.mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
269 *handle = (pps_handle_t)punit;
285 * Check for valid arguments and detach PPS signal.
290 return (-1); /* bad handle */
292 punit = (pps_unit_t *)handle;
298 * set parameters for handle
304 const pps_params_t *params
310 * Check for valid arguments and set parameters.
315 return (-1); /* bad handle */
320 return (-1); /* bad argument */
324 * There was no reasonable consensu in the API working group.
325 * I require `api_version' to be set!
328 if (params->api_version != PPS_API_VERS_1) {
334 * only settable modes are PPS_CAPTUREASSERT and PPS_OFFSETASSERT
337 mode_in = params->mode;
338 punit = (pps_unit_t *)handle;
341 * Only one of the time formats may be selected
342 * if a nonzero assert offset is supplied.
344 if ((mode_in & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) ==
345 (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) {
347 if (punit->params.assert_offset.tv_sec ||
348 punit->params.assert_offset.tv_nsec) {
355 * If no offset was specified but both time
356 * format flags are used consider it harmless
357 * but turn off PPS_TSFMT_NTPFP so getparams
358 * will not show both formats lit.
360 mode_in &= ~PPS_TSFMT_NTPFP;
363 /* turn off read-only bits */
368 * test remaining bits, should only have captureassert,
369 * offsetassert, and/or timestamp format bits.
372 if (mode_in & ~(PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
373 PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) {
382 mode = punit->params.mode;
383 memcpy(&punit->params, params, sizeof(punit->params));
384 punit->params.api_version = PPS_API_VERS_1;
385 punit->params.mode = mode | mode_in;
390 * get parameters for handle
402 * Check for valid arguments and get parameters.
407 return (-1); /* bad handle */
412 return (-1); /* bad argument */
415 punit = (pps_unit_t *)handle;
416 memcpy(params, &punit->params, sizeof(*params));
421 * get capabilities for handle
431 * Check for valid arguments and get capabilities.
436 return (-1); /* bad handle */
441 return (-1); /* bad argument */
456 const struct timespec *timeout
468 * Check for valid arguments and fetch timestamps
473 return (-1); /* bad handle */
478 return (-1); /* bad argument */
482 * nb. PPS_CANWAIT is NOT set by the implementation, we can totally
483 * ignore the timeout variable.
486 memset(&infobuf, 0, sizeof(infobuf));
487 punit = (pps_unit_t *)handle;
490 * if not captureassert, nothing to return.
493 if (!punit->params.mode & PPS_CAPTUREASSERT) {
494 memcpy(ppsinfo, &infobuf, sizeof(*ppsinfo));
498 if (ioctl(punit->filedes, TIOCGPPSEV, (caddr_t) &ev) < 0) {
499 perror("time_pps_fetch:");
504 infobuf.assert_sequence = ev.serial;
505 infobuf.assert_timestamp.tv_sec = ev.tv.tv_sec;
506 infobuf.assert_timestamp.tv_nsec = ev.tv.tv_usec * 1000;
509 * Translate to specified format then apply offset
513 case PPS_TSFMT_TSPEC:
514 /* timespec format requires no conversion */
515 if (punit->params.mode & PPS_OFFSETASSERT) {
516 infobuf.assert_timestamp.tv_sec +=
517 punit->params.assert_offset.tv_sec;
518 infobuf.assert_timestamp.tv_nsec +=
519 punit->params.assert_offset.tv_nsec;
520 PPS_NORMALIZE(infobuf.assert_timestamp);
524 case PPS_TSFMT_NTPFP:
525 /* NTP format requires conversion to fraction form */
526 PPS_TSPECTONTP(infobuf.assert_timestamp_ntpfp);
527 if (punit->params.mode & PPS_OFFSETASSERT)
528 NTPFP_L_ADDS(&infobuf.assert_timestamp_ntpfp,
529 &punit->params.assert_offset_ntpfp);
537 infobuf.current_mode = punit->params.mode;
538 memcpy(ppsinfo, &infobuf, sizeof(*ppsinfo));
543 * specify kernel consumer
549 const int kernel_consumer,
555 * Check for valid arguments and bind kernel consumer
559 return (-1); /* bad handle */
561 if (geteuid() != 0) {
563 return (-1); /* must be superuser */
569 #endif /* _SYS_TIMEPPS_H_ */