Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / sys / timepps.h
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  * $FreeBSD: src/sys/sys/timepps.h,v 1.12 1999/12/29 04:24:48 peter Exp $
10  *
11  * The is a FreeBSD protype version of the "draft-mogul-pps-api-05.txt" 
12  * specification for Pulse Per Second timing interfaces.  
13  */
14
15 #ifndef _SYS_TIMEPPS_H_
16 #define _SYS_TIMEPPS_H_
17
18 #include <sys/ioccom.h>
19
20 #define PPS_API_VERS_1  1
21
22 typedef int pps_handle_t;       
23
24 typedef unsigned pps_seq_t;
25
26 typedef struct ntp_fp {
27         unsigned int    integral;
28         unsigned int    fractional;
29 } ntp_fp_t;
30
31 typedef union pps_timeu {
32         struct timespec tspec;
33         ntp_fp_t        ntpfp;
34         unsigned long   longpad[3];
35 } pps_timeu_t;
36
37 typedef struct {
38         pps_seq_t       assert_sequence;        /* assert event seq # */
39         pps_seq_t       clear_sequence;         /* clear event seq # */
40         pps_timeu_t     assert_tu;
41         pps_timeu_t     clear_tu;
42         int             current_mode;           /* current mode bits */
43 } pps_info_t;
44
45 #define assert_timestamp        assert_tu.tspec
46 #define clear_timestamp         clear_tu.tspec
47
48 #define assert_timestamp_ntpfp  assert_tu.ntpfp
49 #define clear_timestamp_ntpfp   clear_tu.ntpfp
50
51 typedef struct {
52         int api_version;                        /* API version # */
53         int mode;                               /* mode bits */
54         pps_timeu_t assert_off_tu;
55         pps_timeu_t clear_off_tu;
56 } pps_params_t;
57
58 #define assert_offset   assert_off_tu.tspec
59 #define clear_offset    clear_off_tu.tspec
60
61 #define assert_offset_ntpfp     assert_off_tu.ntpfp
62 #define clear_offset_ntpfp      clear_off_tu.ntpfp
63
64
65 #define PPS_CAPTUREASSERT       0x01
66 #define PPS_CAPTURECLEAR        0x02
67 #define PPS_CAPTUREBOTH         0x03
68
69 #define PPS_OFFSETASSERT        0x10
70 #define PPS_OFFSETCLEAR         0x20
71
72 #define PPS_ECHOASSERT          0x40
73 #define PPS_ECHOCLEAR           0x80
74
75 #define PPS_CANWAIT             0x100
76 #define PPS_CANPOLL             0x200
77
78 #define PPS_TSFMT_TSPEC         0x1000
79 #define PPS_TSFMT_NTPFP         0x2000
80
81 #define PPS_KC_HARDPPS          0
82 #define PPS_KC_HARDPPS_PLL      1
83 #define PPS_KC_HARDPPS_FLL      2
84
85 struct pps_fetch_args {
86         int tsformat;
87         pps_info_t      pps_info_buf;
88         struct timespec timeout;
89 };
90
91 struct pps_kcbind_args {
92         int kernel_consumer;
93         int edge;
94         int tsformat;
95 };
96
97 #define PPS_IOC_CREATE          _IO('1', 1)
98 #define PPS_IOC_DESTROY         _IO('1', 2)
99 #define PPS_IOC_SETPARAMS       _IOW('1', 3, pps_params_t)
100 #define PPS_IOC_GETPARAMS       _IOR('1', 4, pps_params_t)
101 #define PPS_IOC_GETCAP          _IOR('1', 5, int)
102 #define PPS_IOC_FETCH           _IOWR('1', 6, struct pps_fetch_args)
103 #define PPS_IOC_KCBIND          _IOW('1', 7, struct pps_kcbind_args)
104
105 #ifdef _KERNEL
106 struct pps_state {
107         pps_params_t    ppsparam;
108         pps_info_t      ppsinfo;
109         int             kcmode;
110         int             ppscap;
111         struct timecounter *ppstc;
112         unsigned        ppscount[3];
113 };
114
115 void pps_event __P((struct pps_state *pps, struct timecounter *tc, unsigned count, int event));
116 void pps_init __P((struct pps_state *pps));
117 int pps_ioctl __P((u_long cmd, caddr_t data, struct pps_state *pps));
118 void hardpps __P((struct timespec *tsp, long nsec));
119
120 #else /* !_KERNEL */
121
122 static __inline int
123 time_pps_create(int filedes, pps_handle_t *handle)
124 {
125         int error;
126
127         *handle = -1;
128         error = ioctl(filedes, PPS_IOC_CREATE, 0);
129         if (error < 0) 
130                 return (-1);
131         *handle = filedes;
132         return (0);
133 }
134
135 static __inline int
136 time_pps_destroy(pps_handle_t handle)
137 {
138         return (ioctl(handle, PPS_IOC_DESTROY, 0));
139 }
140
141 static __inline int
142 time_pps_setparams(pps_handle_t handle, const pps_params_t *ppsparams)
143 {
144         return (ioctl(handle, PPS_IOC_SETPARAMS, ppsparams));
145 }
146
147 static __inline int
148 time_pps_getparams(pps_handle_t handle, pps_params_t *ppsparams)
149 {
150         return (ioctl(handle, PPS_IOC_GETPARAMS, ppsparams));
151 }
152
153 static __inline int 
154 time_pps_getcap(pps_handle_t handle, int *mode)
155 {
156         return (ioctl(handle, PPS_IOC_GETCAP, mode));
157 }
158
159 static __inline int
160 time_pps_fetch(pps_handle_t handle, const int tsformat,
161         pps_info_t *ppsinfobuf, const struct timespec *timeout)
162 {
163         int error;
164         struct pps_fetch_args arg;
165
166         arg.tsformat = tsformat;
167         if (timeout == NULL) {
168                 arg.timeout.tv_sec = -1;
169                 arg.timeout.tv_nsec = -1;
170         } else
171                 arg.timeout = *timeout;
172         error = ioctl(handle, PPS_IOC_FETCH, &arg);
173         *ppsinfobuf = arg.pps_info_buf;
174         return (error);
175 }
176
177 static __inline int
178 time_pps_kcbind(pps_handle_t handle, const int kernel_consumer,
179         const int edge, const int tsformat)
180 {
181         struct pps_kcbind_args arg;
182
183         arg.kernel_consumer = kernel_consumer;
184         arg.edge = edge;
185         arg.tsformat = tsformat;
186         return (ioctl(handle, PPS_IOC_KCBIND, &arg));
187 }
188
189 #endif /* !_KERNEL */
190 #endif /* _SYS_TIMEPPS_H_ */