work around to make pps do its thing
[wyse_ntpd.git] / src / buildroot-2013.05 / output / host / usr / i386-buildroot-linux-uclibc / sysroot / usr / include / timepps.h
1 /*
2  * timepps.h -- PPS API main header
3  *
4  * Copyright (C) 2005-2007   Rodolfo Giometti <giometti@linux.it>
5  * Copyright (C) 2009-2011   Alexander Gordeev <alex@gordick.net>
6  *
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.
11  *
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.
16  */
17
18 #ifndef _SYS_TIMEPPS_H_
19 #define _SYS_TIMEPPS_H_
20
21 #include <errno.h>
22 #include <sys/time.h>
23 #include <sys/ioctl.h>
24 #include <linux/types.h>
25 #include <linux/pps.h>
26
27 #define LINUXPPS        1               /* signal we are using LinuxPPS */
28
29 /*
30  * New data structures
31  */
32
33 struct ntp_fp {
34         unsigned int integral;
35         unsigned int fractional;
36 };
37
38 union pps_timeu {
39         struct timespec tspec;
40         struct ntp_fp ntpfp;
41         unsigned long longpad[3];
42 };
43
44 struct pps_info {
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 */
50 };
51
52 struct pps_params {
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 */
57 };
58
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;
65
66 #define assert_timestamp        assert_tu.tspec
67 #define clear_timestamp         clear_tu.tspec
68
69 #define assert_timestamp_ntpfp  assert_tu.ntpfp
70 #define clear_timestamp_ntpfp   clear_tu.ntpfp
71
72 #define assert_offset           assert_off_tu.tspec
73 #define clear_offset            clear_off_tu.tspec
74
75 #define assert_offset_ntpfp     assert_off_tu.ntpfp
76 #define clear_offset_ntpfp      clear_off_tu.ntpfp
77
78 /*
79  * The PPS API
80  */
81
82 static __inline int time_pps_create(int source, pps_handle_t *handle)
83 {
84         int ret;
85         struct pps_kparams dummy;
86
87         if (!handle) {
88                 errno = EINVAL;
89                 return -1;
90         }
91
92         /* First we check if current device is a valid PPS one by
93          * doing a dummy PPS_GETPARAMS...
94          */
95         ret = ioctl(source, PPS_GETPARAMS, &dummy);
96         if (ret) {
97                 errno = EOPNOTSUPP;
98                 return -1;
99         }
100
101         /* ... then since in LinuxPPS there are no differences between a
102          * "PPS source" and a "PPS handle", we simply return the same value.
103          */
104         *handle = source;
105
106         return 0;
107 }
108
109 static __inline int time_pps_destroy(pps_handle_t handle)
110 {
111         return close(handle);
112 }
113
114 static __inline int time_pps_getparams(pps_handle_t handle,
115                                         pps_params_t *ppsparams)
116 {
117         int ret;
118         struct pps_kparams __ppsparams;
119
120         ret = ioctl(handle, PPS_GETPARAMS, &__ppsparams);
121
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;
128
129         return ret;
130 }
131
132 static __inline int time_pps_setparams(pps_handle_t handle,
133                                         const pps_params_t *ppsparams)
134 {
135         struct pps_kparams __ppsparams;
136
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;
143
144         return ioctl(handle, PPS_SETPARAMS, &__ppsparams);
145 }
146
147 /* Get capabilities for handle */
148 static __inline int time_pps_getcap(pps_handle_t handle, int *mode)
149 {
150         return ioctl(handle, PPS_GETCAP, mode);
151 }
152
153 static __inline int time_pps_fetch(pps_handle_t handle, const int tsformat,
154                                         pps_info_t *ppsinfobuf,
155                                         const struct timespec *timeout)
156 {
157         struct pps_fdata __fdata;
158         int ret;
159
160         /* Sanity checks */
161         if (tsformat != PPS_TSFMT_TSPEC) {
162                 errno = EINVAL;
163                 return -1;
164         }
165
166         if (timeout) {
167                 __fdata.timeout.sec = timeout->tv_sec;
168                 __fdata.timeout.nsec = timeout->tv_nsec;
169                 __fdata.timeout.flags = ~PPS_TIME_INVALID;
170         } else
171                 __fdata.timeout.flags = PPS_TIME_INVALID;
172
173         ret = ioctl(handle, PPS_FETCH, &__fdata);
174
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;
182
183         return ret;
184 }
185
186 #ifdef PPS_KC_BIND
187
188 static __inline int time_pps_kcbind(pps_handle_t handle,
189                                         const int kernel_consumer,
190                                         const int edge, const int tsformat)
191 {
192         struct pps_bind_args __bind_args;
193
194         __bind_args.tsformat = tsformat;
195         __bind_args.edge = edge;
196         __bind_args.consumer = kernel_consumer;
197
198         return ioctl(handle, PPS_KC_BIND, &__bind_args);
199 }
200
201 #else /* !PPS_KC_BIND */
202
203 static __inline int time_pps_kcbind(pps_handle_t handle,
204                                         const int kernel_consumer,
205                                         const int edge, const int tsformat)
206 {
207         /* LinuxPPS doesn't implement kernel consumer feature */
208         errno = EOPNOTSUPP;
209         return -1;
210 }
211
212 #endif /* PPS_KC_BIND */
213
214 #endif /* _SYS_TIMEPPS_H_ */