2 * timepps.h -- PPS API main header
4 * Copyright (C) 2005-2007 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2009-2011 Alexander Gordeev <alex@gordick.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #ifndef _SYS_TIMEPPS_H_
19 #define _SYS_TIMEPPS_H_
23 #include <sys/ioctl.h>
24 #include <linux/types.h>
25 #include <linux/pps.h>
27 #define LINUXPPS 1 /* signal we are using LinuxPPS */
34 unsigned int integral;
35 unsigned int fractional;
39 struct timespec tspec;
41 unsigned long longpad[3];
45 unsigned long assert_sequence; /* seq. num. of assert event */
46 unsigned long clear_sequence; /* seq. num. of clear event */
47 union pps_timeu assert_tu; /* time of assert event */
48 union pps_timeu clear_tu; /* time of clear event */
49 int current_mode; /* current mode bits */
53 int api_version; /* API version # */
54 int mode; /* mode bits */
55 union pps_timeu assert_off_tu; /* offset compensation for assert */
56 union pps_timeu clear_off_tu; /* offset compensation for clear */
59 typedef int pps_handle_t; /* represents a PPS source */
60 typedef unsigned long pps_seq_t; /* sequence number */
61 typedef struct ntp_fp ntp_fp_t; /* NTP-compatible time stamp */
62 typedef union pps_timeu pps_timeu_t; /* generic data type for time stamps */
63 typedef struct pps_info pps_info_t;
64 typedef struct pps_params pps_params_t;
66 #define assert_timestamp assert_tu.tspec
67 #define clear_timestamp clear_tu.tspec
69 #define assert_timestamp_ntpfp assert_tu.ntpfp
70 #define clear_timestamp_ntpfp clear_tu.ntpfp
72 #define assert_offset assert_off_tu.tspec
73 #define clear_offset clear_off_tu.tspec
75 #define assert_offset_ntpfp assert_off_tu.ntpfp
76 #define clear_offset_ntpfp clear_off_tu.ntpfp
82 static __inline int time_pps_create(int source, pps_handle_t *handle)
85 struct pps_kparams dummy;
92 /* First we check if current device is a valid PPS one by
93 * doing a dummy PPS_GETPARAMS...
95 ret = ioctl(source, PPS_GETPARAMS, &dummy);
101 /* ... then since in LinuxPPS there are no differences between a
102 * "PPS source" and a "PPS handle", we simply return the same value.
109 static __inline int time_pps_destroy(pps_handle_t handle)
111 return close(handle);
114 static __inline int time_pps_getparams(pps_handle_t handle,
115 pps_params_t *ppsparams)
118 struct pps_kparams __ppsparams;
120 ret = ioctl(handle, PPS_GETPARAMS, &__ppsparams);
122 ppsparams->api_version = __ppsparams.api_version;
123 ppsparams->mode = __ppsparams.mode;
124 ppsparams->assert_off_tu.tspec.tv_sec = __ppsparams.assert_off_tu.sec;
125 ppsparams->assert_off_tu.tspec.tv_nsec = __ppsparams.assert_off_tu.nsec;
126 ppsparams->clear_off_tu.tspec.tv_sec = __ppsparams.clear_off_tu.sec;
127 ppsparams->clear_off_tu.tspec.tv_nsec = __ppsparams.clear_off_tu.nsec;
132 static __inline int time_pps_setparams(pps_handle_t handle,
133 const pps_params_t *ppsparams)
135 struct pps_kparams __ppsparams;
137 __ppsparams.api_version = ppsparams->api_version;
138 __ppsparams.mode = ppsparams->mode;
139 __ppsparams.assert_off_tu.sec = ppsparams->assert_off_tu.tspec.tv_sec;
140 __ppsparams.assert_off_tu.nsec = ppsparams->assert_off_tu.tspec.tv_nsec;
141 __ppsparams.clear_off_tu.sec = ppsparams->clear_off_tu.tspec.tv_sec;
142 __ppsparams.clear_off_tu.nsec = ppsparams->clear_off_tu.tspec.tv_nsec;
144 return ioctl(handle, PPS_SETPARAMS, &__ppsparams);
147 /* Get capabilities for handle */
148 static __inline int time_pps_getcap(pps_handle_t handle, int *mode)
150 return ioctl(handle, PPS_GETCAP, mode);
153 static __inline int time_pps_fetch(pps_handle_t handle, const int tsformat,
154 pps_info_t *ppsinfobuf,
155 const struct timespec *timeout)
157 struct pps_fdata __fdata;
161 if (tsformat != PPS_TSFMT_TSPEC) {
167 __fdata.timeout.sec = timeout->tv_sec;
168 __fdata.timeout.nsec = timeout->tv_nsec;
169 __fdata.timeout.flags = ~PPS_TIME_INVALID;
171 __fdata.timeout.flags = PPS_TIME_INVALID;
173 ret = ioctl(handle, PPS_FETCH, &__fdata);
175 ppsinfobuf->assert_sequence = __fdata.info.assert_sequence;
176 ppsinfobuf->clear_sequence = __fdata.info.clear_sequence;
177 ppsinfobuf->assert_tu.tspec.tv_sec = __fdata.info.assert_tu.sec;
178 ppsinfobuf->assert_tu.tspec.tv_nsec = __fdata.info.assert_tu.nsec;
179 ppsinfobuf->clear_tu.tspec.tv_sec = __fdata.info.clear_tu.sec;
180 ppsinfobuf->clear_tu.tspec.tv_nsec = __fdata.info.clear_tu.nsec;
181 ppsinfobuf->current_mode = __fdata.info.current_mode;
188 static __inline int time_pps_kcbind(pps_handle_t handle,
189 const int kernel_consumer,
190 const int edge, const int tsformat)
192 struct pps_bind_args __bind_args;
194 __bind_args.tsformat = tsformat;
195 __bind_args.edge = edge;
196 __bind_args.consumer = kernel_consumer;
198 return ioctl(handle, PPS_KC_BIND, &__bind_args);
201 #else /* !PPS_KC_BIND */
203 static __inline int time_pps_kcbind(pps_handle_t handle,
204 const int kernel_consumer,
205 const int edge, const int tsformat)
207 /* LinuxPPS doesn't implement kernel consumer feature */
212 #endif /* PPS_KC_BIND */
214 #endif /* _SYS_TIMEPPS_H_ */