1 | /*- |
---|
2 | * ---------------------------------------------------------------------------- |
---|
3 | * "THE BEER-WARE LICENSE" (Revision 42): |
---|
4 | * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you |
---|
5 | * can do whatever you want with this stuff. If we meet some day, and you think |
---|
6 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
---|
7 | * ---------------------------------------------------------------------------- |
---|
8 | * |
---|
9 | * Copyright (c) 2011 The FreeBSD Foundation |
---|
10 | * All rights reserved. |
---|
11 | * |
---|
12 | * Portions of this software were developed by Julien Ridoux at the University |
---|
13 | * of Melbourne under sponsorship from the FreeBSD Foundation. |
---|
14 | * |
---|
15 | * $FreeBSD r277406 2015-01-20T03:54:30Z$ |
---|
16 | * |
---|
17 | * The is a FreeBSD version of the RFC 2783 API for Pulse Per Second |
---|
18 | * timing interfaces. |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _SYS_TIMEPPS_H_ |
---|
22 | #define _SYS_TIMEPPS_H_ |
---|
23 | |
---|
24 | #include <sys/_ffcounter.h> |
---|
25 | #include <sys/ioccom.h> |
---|
26 | #include <sys/time.h> |
---|
27 | |
---|
28 | #define PPS_API_VERS_1 1 |
---|
29 | |
---|
30 | typedef int pps_handle_t; |
---|
31 | |
---|
32 | typedef unsigned pps_seq_t; |
---|
33 | |
---|
34 | typedef struct ntp_fp { |
---|
35 | unsigned int integral; |
---|
36 | unsigned int fractional; |
---|
37 | } ntp_fp_t; |
---|
38 | |
---|
39 | typedef union pps_timeu { |
---|
40 | struct timespec tspec; |
---|
41 | ntp_fp_t ntpfp; |
---|
42 | unsigned long longpad[3]; |
---|
43 | } pps_timeu_t; |
---|
44 | |
---|
45 | typedef struct { |
---|
46 | pps_seq_t assert_sequence; /* assert event seq # */ |
---|
47 | pps_seq_t clear_sequence; /* clear event seq # */ |
---|
48 | pps_timeu_t assert_tu; |
---|
49 | pps_timeu_t clear_tu; |
---|
50 | int current_mode; /* current mode bits */ |
---|
51 | } pps_info_t; |
---|
52 | |
---|
53 | typedef struct { |
---|
54 | pps_seq_t assert_sequence; /* assert event seq # */ |
---|
55 | pps_seq_t clear_sequence; /* clear event seq # */ |
---|
56 | pps_timeu_t assert_tu; |
---|
57 | pps_timeu_t clear_tu; |
---|
58 | ffcounter assert_ffcount; /* ffcounter on assert event */ |
---|
59 | ffcounter clear_ffcount; /* ffcounter on clear event */ |
---|
60 | int current_mode; /* current mode bits */ |
---|
61 | } pps_info_ffc_t; |
---|
62 | |
---|
63 | #define assert_timestamp assert_tu.tspec |
---|
64 | #define clear_timestamp clear_tu.tspec |
---|
65 | |
---|
66 | #define assert_timestamp_ntpfp assert_tu.ntpfp |
---|
67 | #define clear_timestamp_ntpfp clear_tu.ntpfp |
---|
68 | |
---|
69 | typedef struct { |
---|
70 | int api_version; /* API version # */ |
---|
71 | int mode; /* mode bits */ |
---|
72 | pps_timeu_t assert_off_tu; |
---|
73 | pps_timeu_t clear_off_tu; |
---|
74 | } pps_params_t; |
---|
75 | |
---|
76 | #define assert_offset assert_off_tu.tspec |
---|
77 | #define clear_offset clear_off_tu.tspec |
---|
78 | |
---|
79 | #define assert_offset_ntpfp assert_off_tu.ntpfp |
---|
80 | #define clear_offset_ntpfp clear_off_tu.ntpfp |
---|
81 | |
---|
82 | |
---|
83 | #define PPS_CAPTUREASSERT 0x01 |
---|
84 | #define PPS_CAPTURECLEAR 0x02 |
---|
85 | #define PPS_CAPTUREBOTH 0x03 |
---|
86 | |
---|
87 | #define PPS_OFFSETASSERT 0x10 |
---|
88 | #define PPS_OFFSETCLEAR 0x20 |
---|
89 | |
---|
90 | #define PPS_ECHOASSERT 0x40 |
---|
91 | #define PPS_ECHOCLEAR 0x80 |
---|
92 | |
---|
93 | #define PPS_CANWAIT 0x100 |
---|
94 | #define PPS_CANPOLL 0x200 |
---|
95 | |
---|
96 | #define PPS_TSFMT_TSPEC 0x1000 |
---|
97 | #define PPS_TSFMT_NTPFP 0x2000 |
---|
98 | |
---|
99 | #define PPS_TSCLK_FBCK 0x10000 |
---|
100 | #define PPS_TSCLK_FFWD 0x20000 |
---|
101 | #define PPS_TSCLK_MASK 0x30000 |
---|
102 | |
---|
103 | #define PPS_KC_HARDPPS 0 |
---|
104 | #define PPS_KC_HARDPPS_PLL 1 |
---|
105 | #define PPS_KC_HARDPPS_FLL 2 |
---|
106 | |
---|
107 | struct pps_fetch_args { |
---|
108 | int tsformat; |
---|
109 | pps_info_t pps_info_buf; |
---|
110 | struct timespec timeout; |
---|
111 | }; |
---|
112 | |
---|
113 | struct pps_fetch_ffc_args { |
---|
114 | int tsformat; |
---|
115 | pps_info_ffc_t pps_info_buf_ffc; |
---|
116 | struct timespec timeout; |
---|
117 | }; |
---|
118 | |
---|
119 | struct pps_kcbind_args { |
---|
120 | int kernel_consumer; |
---|
121 | int edge; |
---|
122 | int tsformat; |
---|
123 | }; |
---|
124 | |
---|
125 | #define PPS_IOC_CREATE _IO('1', 1) |
---|
126 | #define PPS_IOC_DESTROY _IO('1', 2) |
---|
127 | #define PPS_IOC_SETPARAMS _IOW('1', 3, pps_params_t) |
---|
128 | #define PPS_IOC_GETPARAMS _IOR('1', 4, pps_params_t) |
---|
129 | #define PPS_IOC_GETCAP _IOR('1', 5, int) |
---|
130 | #define PPS_IOC_FETCH _IOWR('1', 6, struct pps_fetch_args) |
---|
131 | #define PPS_IOC_KCBIND _IOW('1', 7, struct pps_kcbind_args) |
---|
132 | #define PPS_IOC_FETCH_FFCOUNTER _IOWR('1', 8, struct pps_fetch_ffc_args) |
---|
133 | |
---|
134 | #ifdef _KERNEL |
---|
135 | |
---|
136 | struct pps_state { |
---|
137 | /* Capture information. */ |
---|
138 | struct timehands *capth; |
---|
139 | struct fftimehands *capffth; |
---|
140 | unsigned capgen; |
---|
141 | unsigned capcount; |
---|
142 | |
---|
143 | /* State information. */ |
---|
144 | pps_params_t ppsparam; |
---|
145 | pps_info_t ppsinfo; |
---|
146 | pps_info_ffc_t ppsinfo_ffc; |
---|
147 | int kcmode; |
---|
148 | int ppscap; |
---|
149 | struct timecounter *ppstc; |
---|
150 | unsigned ppscount[3]; |
---|
151 | }; |
---|
152 | |
---|
153 | void pps_capture(struct pps_state *pps); |
---|
154 | void pps_event(struct pps_state *pps, int event); |
---|
155 | void pps_init(struct pps_state *pps); |
---|
156 | int pps_ioctl(unsigned long cmd, caddr_t data, struct pps_state *pps); |
---|
157 | void hardpps(struct timespec *tsp, long nsec); |
---|
158 | |
---|
159 | #else /* !_KERNEL */ |
---|
160 | |
---|
161 | static __inline int |
---|
162 | time_pps_create(int filedes, pps_handle_t *handle) |
---|
163 | { |
---|
164 | int error; |
---|
165 | |
---|
166 | *handle = -1; |
---|
167 | error = ioctl(filedes, PPS_IOC_CREATE, 0); |
---|
168 | if (error < 0) |
---|
169 | return (-1); |
---|
170 | *handle = filedes; |
---|
171 | return (0); |
---|
172 | } |
---|
173 | |
---|
174 | static __inline int |
---|
175 | time_pps_destroy(pps_handle_t handle) |
---|
176 | { |
---|
177 | return (ioctl(handle, PPS_IOC_DESTROY, 0)); |
---|
178 | } |
---|
179 | |
---|
180 | static __inline int |
---|
181 | time_pps_setparams(pps_handle_t handle, const pps_params_t *ppsparams) |
---|
182 | { |
---|
183 | return (ioctl(handle, PPS_IOC_SETPARAMS, ppsparams)); |
---|
184 | } |
---|
185 | |
---|
186 | static __inline int |
---|
187 | time_pps_getparams(pps_handle_t handle, pps_params_t *ppsparams) |
---|
188 | { |
---|
189 | return (ioctl(handle, PPS_IOC_GETPARAMS, ppsparams)); |
---|
190 | } |
---|
191 | |
---|
192 | static __inline int |
---|
193 | time_pps_getcap(pps_handle_t handle, int *mode) |
---|
194 | { |
---|
195 | return (ioctl(handle, PPS_IOC_GETCAP, mode)); |
---|
196 | } |
---|
197 | |
---|
198 | static __inline int |
---|
199 | time_pps_fetch(pps_handle_t handle, const int tsformat, |
---|
200 | pps_info_t *ppsinfobuf, const struct timespec *timeout) |
---|
201 | { |
---|
202 | int error; |
---|
203 | struct pps_fetch_args arg; |
---|
204 | |
---|
205 | arg.tsformat = tsformat; |
---|
206 | if (timeout == NULL) { |
---|
207 | arg.timeout.tv_sec = -1; |
---|
208 | arg.timeout.tv_nsec = -1; |
---|
209 | } else |
---|
210 | arg.timeout = *timeout; |
---|
211 | error = ioctl(handle, PPS_IOC_FETCH, &arg); |
---|
212 | *ppsinfobuf = arg.pps_info_buf; |
---|
213 | return (error); |
---|
214 | } |
---|
215 | |
---|
216 | static __inline int |
---|
217 | time_pps_fetch_ffc(pps_handle_t handle, const int tsformat, |
---|
218 | pps_info_ffc_t *ppsinfobuf, const struct timespec *timeout) |
---|
219 | { |
---|
220 | struct pps_fetch_ffc_args arg; |
---|
221 | int error; |
---|
222 | |
---|
223 | arg.tsformat = tsformat; |
---|
224 | if (timeout == NULL) { |
---|
225 | arg.timeout.tv_sec = -1; |
---|
226 | arg.timeout.tv_nsec = -1; |
---|
227 | } else { |
---|
228 | arg.timeout = *timeout; |
---|
229 | } |
---|
230 | error = ioctl(handle, PPS_IOC_FETCH_FFCOUNTER, &arg); |
---|
231 | *ppsinfobuf = arg.pps_info_buf_ffc; |
---|
232 | return (error); |
---|
233 | } |
---|
234 | |
---|
235 | static __inline int |
---|
236 | time_pps_kcbind(pps_handle_t handle, const int kernel_consumer, |
---|
237 | const int edge, const int tsformat) |
---|
238 | { |
---|
239 | struct pps_kcbind_args arg; |
---|
240 | |
---|
241 | arg.kernel_consumer = kernel_consumer; |
---|
242 | arg.edge = edge; |
---|
243 | arg.tsformat = tsformat; |
---|
244 | return (ioctl(handle, PPS_IOC_KCBIND, &arg)); |
---|
245 | } |
---|
246 | |
---|
247 | #endif /* KERNEL */ |
---|
248 | |
---|
249 | #endif /* !_SYS_TIMEPPS_H_ */ |
---|