2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
11 * Copyright (c) 2011 The FreeBSD Foundation
13 * Portions of this software were developed by Julien Ridoux at the University
14 * of Melbourne under sponsorship from the FreeBSD Foundation.
16 * The is a FreeBSD version of the RFC 2783 API for Pulse Per Second
20 #ifndef _SYS_TIMEPPS_H_
21 #define _SYS_TIMEPPS_H_
23 #include <sys/_ffcounter.h>
24 #include <sys/ioccom.h>
27 #define PPS_API_VERS_1 1
29 typedef int pps_handle_t;
31 typedef unsigned pps_seq_t;
33 typedef struct ntp_fp {
34 unsigned int integral;
35 unsigned int fractional;
38 typedef union pps_timeu {
39 struct timespec tspec;
41 unsigned long longpad[3];
45 pps_seq_t assert_sequence; /* assert event seq # */
46 pps_seq_t clear_sequence; /* clear event seq # */
47 pps_timeu_t assert_tu;
49 int current_mode; /* current mode bits */
53 pps_seq_t assert_sequence; /* assert event seq # */
54 pps_seq_t clear_sequence; /* clear event seq # */
55 pps_timeu_t assert_tu;
57 ffcounter assert_ffcount; /* ffcounter on assert event */
58 ffcounter clear_ffcount; /* ffcounter on clear event */
59 int current_mode; /* current mode bits */
62 #define assert_timestamp assert_tu.tspec
63 #define clear_timestamp clear_tu.tspec
65 #define assert_timestamp_ntpfp assert_tu.ntpfp
66 #define clear_timestamp_ntpfp clear_tu.ntpfp
69 int api_version; /* API version # */
70 int mode; /* mode bits */
71 pps_timeu_t assert_off_tu;
72 pps_timeu_t clear_off_tu;
75 #define assert_offset assert_off_tu.tspec
76 #define clear_offset clear_off_tu.tspec
78 #define assert_offset_ntpfp assert_off_tu.ntpfp
79 #define clear_offset_ntpfp clear_off_tu.ntpfp
81 #define PPS_CAPTUREASSERT 0x01
82 #define PPS_CAPTURECLEAR 0x02
83 #define PPS_CAPTUREBOTH 0x03
85 #define PPS_OFFSETASSERT 0x10
86 #define PPS_OFFSETCLEAR 0x20
88 #define PPS_ECHOASSERT 0x40
89 #define PPS_ECHOCLEAR 0x80
91 #define PPS_CANWAIT 0x100
92 #define PPS_CANPOLL 0x200
94 #define PPS_TSFMT_TSPEC 0x1000
95 #define PPS_TSFMT_NTPFP 0x2000
97 #define PPS_TSCLK_FBCK 0x10000
98 #define PPS_TSCLK_FFWD 0x20000
99 #define PPS_TSCLK_MASK 0x30000
101 #define PPS_KC_HARDPPS 0
102 #define PPS_KC_HARDPPS_PLL 1
103 #define PPS_KC_HARDPPS_FLL 2
105 struct pps_fetch_args {
107 pps_info_t pps_info_buf;
108 struct timespec timeout;
111 struct pps_fetch_ffc_args {
113 pps_info_ffc_t pps_info_buf_ffc;
114 struct timespec timeout;
117 struct pps_kcbind_args {
123 #define PPS_IOC_CREATE _IO('1', 1)
124 #define PPS_IOC_DESTROY _IO('1', 2)
125 #define PPS_IOC_SETPARAMS _IOW('1', 3, pps_params_t)
126 #define PPS_IOC_GETPARAMS _IOR('1', 4, pps_params_t)
127 #define PPS_IOC_GETCAP _IOR('1', 5, int)
128 #define PPS_IOC_FETCH _IOWR('1', 6, struct pps_fetch_args)
129 #define PPS_IOC_KCBIND _IOW('1', 7, struct pps_kcbind_args)
130 #define PPS_IOC_FETCH_FFCOUNTER _IOWR('1', 8, struct pps_fetch_ffc_args)
136 #define KCMODE_EDGEMASK 0x03
137 #define KCMODE_ABIFLAG 0x80000000 /* Internal use: abi-aware driver. */
139 #define PPS_ABI_VERSION 1
141 #define PPSFLAG_MTX_SPIN 0x01 /* Driver mtx is MTX_SPIN type. */
144 /* Capture information. */
145 struct timehands *capth;
146 struct fftimehands *capffth;
150 /* State information. */
151 pps_params_t ppsparam;
153 pps_info_ffc_t ppsinfo_ffc;
156 struct timecounter *ppstc;
157 unsigned ppscount[3];
159 * The following fields are valid if the driver calls pps_init_abi().
161 uint16_t driver_abi; /* Driver sets before pps_init_abi(). */
162 uint16_t kernel_abi; /* Kernel sets during pps_init_abi(). */
163 struct mtx *driver_mtx; /* Optional, valid if non-NULL. */
167 void pps_capture(struct pps_state *pps);
168 void pps_event(struct pps_state *pps, int event);
169 void pps_init(struct pps_state *pps);
170 void pps_init_abi(struct pps_state *pps);
171 int pps_ioctl(unsigned long cmd, caddr_t data, struct pps_state *pps);
172 void hardpps(struct timespec *tsp, long nsec);
177 time_pps_create(int filedes, pps_handle_t *handle)
182 error = ioctl(filedes, PPS_IOC_CREATE, 0);
190 time_pps_destroy(pps_handle_t handle)
192 return (ioctl(handle, PPS_IOC_DESTROY, 0));
196 time_pps_setparams(pps_handle_t handle, const pps_params_t *ppsparams)
198 return (ioctl(handle, PPS_IOC_SETPARAMS, ppsparams));
202 time_pps_getparams(pps_handle_t handle, pps_params_t *ppsparams)
204 return (ioctl(handle, PPS_IOC_GETPARAMS, ppsparams));
208 time_pps_getcap(pps_handle_t handle, int *mode)
210 return (ioctl(handle, PPS_IOC_GETCAP, mode));
214 time_pps_fetch(pps_handle_t handle, const int tsformat,
215 pps_info_t *ppsinfobuf, const struct timespec *timeout)
218 struct pps_fetch_args arg;
220 arg.tsformat = tsformat;
221 if (timeout == NULL) {
222 arg.timeout.tv_sec = -1;
223 arg.timeout.tv_nsec = -1;
225 arg.timeout = *timeout;
226 error = ioctl(handle, PPS_IOC_FETCH, &arg);
227 *ppsinfobuf = arg.pps_info_buf;
232 time_pps_fetch_ffc(pps_handle_t handle, const int tsformat,
233 pps_info_ffc_t *ppsinfobuf, const struct timespec *timeout)
235 struct pps_fetch_ffc_args arg;
238 arg.tsformat = tsformat;
239 if (timeout == NULL) {
240 arg.timeout.tv_sec = -1;
241 arg.timeout.tv_nsec = -1;
243 arg.timeout = *timeout;
245 error = ioctl(handle, PPS_IOC_FETCH_FFCOUNTER, &arg);
246 *ppsinfobuf = arg.pps_info_buf_ffc;
251 time_pps_kcbind(pps_handle_t handle, const int kernel_consumer,
252 const int edge, const int tsformat)
254 struct pps_kcbind_args arg;
256 arg.kernel_consumer = kernel_consumer;
258 arg.tsformat = tsformat;
259 return (ioctl(handle, PPS_IOC_KCBIND, &arg));
264 #endif /* !_SYS_TIMEPPS_H_ */