xref: /netbsd-src/sys/sys/timepps.h (revision fbae48b901b55c2671f81f494926889226fd3ffe)
1*fbae48b9Sperry /*	$NetBSD: timepps.h,v 1.14 2006/02/16 20:17:20 perry Exp $	*/
278741b62Sjonathan 
378741b62Sjonathan /*
478741b62Sjonathan  * Copyright (c) 1998 Jonathan Stone
578741b62Sjonathan  * All rights reserved.
678741b62Sjonathan  *
778741b62Sjonathan  * Redistribution and use in source and binary forms, with or without
878741b62Sjonathan  * modification, are permitted provided that the following conditions
978741b62Sjonathan  * are met:
1078741b62Sjonathan  * 1. Redistributions of source code must retain the above copyright
1178741b62Sjonathan  *    notice, this list of conditions and the following disclaimer.
1278741b62Sjonathan  * 2. Redistributions in binary form must reproduce the above copyright
1378741b62Sjonathan  *    notice, this list of conditions and the following disclaimer in the
1478741b62Sjonathan  *    documentation and/or other materials provided with the distribution.
1578741b62Sjonathan  * 3. All advertising materials mentioning features or use of this software
1678741b62Sjonathan  *    must display the following acknowledgement:
1778741b62Sjonathan  *      This product includes software developed by Jonathan Stone for
1878741b62Sjonathan  *      the NetBSD Project.
1978741b62Sjonathan  * 4. The name of the author may not be used to endorse or promote products
2078741b62Sjonathan  *    derived from this software without specific prior written permission.
2178741b62Sjonathan  *
2278741b62Sjonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2378741b62Sjonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2478741b62Sjonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2578741b62Sjonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2678741b62Sjonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2778741b62Sjonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2878741b62Sjonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2978741b62Sjonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3078741b62Sjonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3178741b62Sjonathan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3278741b62Sjonathan  */
3378741b62Sjonathan 
3478741b62Sjonathan #ifndef _SYS_TIMEPPS_H_
3578741b62Sjonathan #define _SYS_TIMEPPS_H_
3678741b62Sjonathan 
370588cdfcSjonathan /*
380588cdfcSjonathan  * This header file complies with "Pulse-Per-Second API for UNIX-like
390588cdfcSjonathan  * Operating Systems, Version 1.0", draft-mogul-pps-api-05.txt
400588cdfcSjonathan  */
410588cdfcSjonathan 
4278741b62Sjonathan #include <sys/ioccom.h>
4378741b62Sjonathan 
440588cdfcSjonathan #define PPS_API_VERS_1	1	/* API version number */
450588cdfcSjonathan 
460588cdfcSjonathan /*
470588cdfcSjonathan  * PPSAPI type definitions
480588cdfcSjonathan  */
490588cdfcSjonathan typedef int32_t pps_handle_t;	/* represents a PPS source */
50144515ceSperry typedef uint32_t pps_seq_t;	/* sequence number, at least 32 bits */
5178741b62Sjonathan 
5278741b62Sjonathan typedef union pps_timeu {
5378741b62Sjonathan 	struct timespec	tspec;
5478741b62Sjonathan 	struct {        /* NTP long fixed-point format */
5578741b62Sjonathan 		unsigned int     integral;
5678741b62Sjonathan 		unsigned int     fractional;
5778741b62Sjonathan 	} ntplfp;
5878741b62Sjonathan 	unsigned long   longpair[2];
5978741b62Sjonathan } pps_timeu_t;
6078741b62Sjonathan 
6178741b62Sjonathan 
620588cdfcSjonathan /*
630588cdfcSjonathan  * timestamp information
640588cdfcSjonathan  */
6578741b62Sjonathan typedef struct {
6678741b62Sjonathan 	pps_seq_t	assert_sequence;	/* assert event seq # */
6778741b62Sjonathan 	pps_seq_t	clear_sequence;		/* clear event seq # */
6878741b62Sjonathan 	pps_timeu_t	assert_tu;
6978741b62Sjonathan 	pps_timeu_t	clear_tu;
7078741b62Sjonathan 	int		current_mode;		/* current mode bits */
7178741b62Sjonathan } pps_info_t;
7278741b62Sjonathan 
730588cdfcSjonathan #define assert_timestamp	assert_tu.tspec
740588cdfcSjonathan #define clear_timestamp		clear_tu.tspec
7578741b62Sjonathan 
760588cdfcSjonathan 
770588cdfcSjonathan /*
780588cdfcSjonathan  * Parameter structure
790588cdfcSjonathan  */
8078741b62Sjonathan typedef struct {
810588cdfcSjonathan 	int api_version;			/* API version number */
8278741b62Sjonathan 	int mode;				/* mode bits */
8378741b62Sjonathan 	pps_timeu_t	assert_off_tu;
8478741b62Sjonathan 	pps_timeu_t	clear_off_tu;
8578741b62Sjonathan } pps_params_t;
8678741b62Sjonathan #define assert_offset		assert_off_tu.tspec
8778741b62Sjonathan #define clear_offset		clear_off_tu.tspec
8878741b62Sjonathan 
8978741b62Sjonathan 
900588cdfcSjonathan /*
910588cdfcSjonathan  * Device/implementation parameters (mode, edge bits)
920588cdfcSjonathan  */
9378741b62Sjonathan #define PPS_CAPTUREASSERT	0x01
9478741b62Sjonathan #define PPS_CAPTURECLEAR	0x02
9578741b62Sjonathan #define PPS_CAPTUREBOTH		0x03
9678741b62Sjonathan #define PPS_OFFSETASSERT	0x10
9778741b62Sjonathan #define PPS_OFFSETCLEAR		0x20
980588cdfcSjonathan #define PPS_CANWAIT		0x100
990588cdfcSjonathan #define PPS_CANPOLL		0x200
10078741b62Sjonathan 
1010588cdfcSjonathan /*
1020588cdfcSjonathan  * Kernel actions
1030588cdfcSjonathan  */
10478741b62Sjonathan #define PPS_ECHOASSERT		0x40
10578741b62Sjonathan #define PPS_ECHOCLEAR		0x80
10678741b62Sjonathan 
10778741b62Sjonathan 
1080588cdfcSjonathan /*
1090588cdfcSjonathan  * timestamp formats (tsformat, mode)
1100588cdfcSjonathan  */
11178741b62Sjonathan #define PPS_TSFMT_TSPEC		0x1000
11278741b62Sjonathan #define PPS_TSFMT_NTPLFP	0x2000
11378741b62Sjonathan 
1140588cdfcSjonathan /*
1150588cdfcSjonathan  * Kernel discipline actions (kernel_consumer)
1160588cdfcSjonathan  */
1170588cdfcSjonathan #define PPS_KC_HARDPPS		0
1180588cdfcSjonathan #define PPS_KC_HARDPPS_PLL	1
1190588cdfcSjonathan #define PPS_KC_HARDPPS_FLL	2
1200588cdfcSjonathan 
1210588cdfcSjonathan /*
1220588cdfcSjonathan  * IOCTL definitions
1230588cdfcSjonathan  */
1240588cdfcSjonathan #define PPS_IOC_CREATE		_IO('1', 1)
1250588cdfcSjonathan #define PPS_IOC_DESTROY		_IO('1', 2)
1260588cdfcSjonathan #define PPS_IOC_SETPARAMS	_IOW('1', 3, pps_params_t)
1270588cdfcSjonathan #define PPS_IOC_GETPARAMS	_IOR('1', 4, pps_params_t)
1280588cdfcSjonathan #define PPS_IOC_GETCAP		_IOR('1', 5, int)
1290588cdfcSjonathan #define PPS_IOC_FETCH		_IOWR('1', 6, pps_info_t)
1302763a4b9Ssimonb #define PPS_IOC_KCBIND		_IOW('1', 7, int)
13178741b62Sjonathan 
13278741b62Sjonathan #ifndef _KERNEL
1330588cdfcSjonathan 
1340588cdfcSjonathan #include <sys/cdefs.h>
1350588cdfcSjonathan #include <sys/ioctl.h>
1360588cdfcSjonathan 
137*fbae48b9Sperry static __inline int time_pps_create(int, pps_handle_t *);
138*fbae48b9Sperry static __inline int time_pps_destroy(pps_handle_t);
139*fbae48b9Sperry static __inline int time_pps_setparams(pps_handle_t, const pps_params_t *);
140*fbae48b9Sperry static __inline int time_pps_getparams(pps_handle_t, pps_params_t *);
141*fbae48b9Sperry static __inline int time_pps_getcap(pps_handle_t, int *);
142*fbae48b9Sperry static __inline int time_pps_fetch(pps_handle_t, const int, pps_info_t *,
1430e1b702cSperry 	const struct timespec *);
1440b915538Smycroft #if 0
145*fbae48b9Sperry static __inline int time_pps_wait(pps_handle_t, const struct timespec *,
1460e1b702cSperry 	pps_info_t *);
1470b915538Smycroft #endif
1480588cdfcSjonathan 
149*fbae48b9Sperry static __inline int time_pps_kcbind(pps_handle_t, const int, const int,
1500e1b702cSperry 	const int);
15183eb9e9fSjonathan 
152*fbae48b9Sperry static __inline int
1530588cdfcSjonathan time_pps_create(filedes, handle)
1540588cdfcSjonathan 	int filedes;
1550588cdfcSjonathan 	pps_handle_t *handle;
1560588cdfcSjonathan {
157fcdfaefcSsimonb 
1580588cdfcSjonathan 	*handle = filedes;
1590588cdfcSjonathan 	return (0);
1600588cdfcSjonathan }
1610588cdfcSjonathan 
162*fbae48b9Sperry static __inline int
1630588cdfcSjonathan time_pps_destroy(handle)
1640588cdfcSjonathan 	pps_handle_t handle;
1650588cdfcSjonathan {
166fcdfaefcSsimonb 
1670588cdfcSjonathan 	return (0);
1680588cdfcSjonathan }
1690588cdfcSjonathan 
170*fbae48b9Sperry static __inline int
1710588cdfcSjonathan time_pps_setparams(handle, ppsparams)
1720588cdfcSjonathan 	pps_handle_t handle;
1730588cdfcSjonathan 	const pps_params_t *ppsparams;
1740588cdfcSjonathan {
175fcdfaefcSsimonb 
176ba9d68d0Schristos 	return (ioctl(handle, PPS_IOC_SETPARAMS, __UNCONST(ppsparams)));
1770588cdfcSjonathan }
1780588cdfcSjonathan 
179*fbae48b9Sperry static __inline int
1800588cdfcSjonathan time_pps_getparams(handle, ppsparams)
1810588cdfcSjonathan 	pps_handle_t handle;
1820588cdfcSjonathan 	pps_params_t *ppsparams;
1830588cdfcSjonathan {
184fcdfaefcSsimonb 
1850588cdfcSjonathan 	return (ioctl(handle, PPS_IOC_GETPARAMS, ppsparams));
1860588cdfcSjonathan }
1870588cdfcSjonathan 
188*fbae48b9Sperry static __inline int
1890588cdfcSjonathan time_pps_getcap(handle, mode)
1900588cdfcSjonathan 	pps_handle_t handle;
1910588cdfcSjonathan 	int *mode;
1920588cdfcSjonathan {
193fcdfaefcSsimonb 
1940588cdfcSjonathan 	return (ioctl(handle, PPS_IOC_GETCAP, mode));
1950588cdfcSjonathan }
1960588cdfcSjonathan 
197*fbae48b9Sperry static __inline int
1980588cdfcSjonathan time_pps_fetch(handle, tsformat, ppsinfobuf, timeout)
1990588cdfcSjonathan 	pps_handle_t handle;
2000588cdfcSjonathan 	const int tsformat;
2010588cdfcSjonathan 	pps_info_t *ppsinfobuf;
2020588cdfcSjonathan 	const struct timespec *timeout;
2030588cdfcSjonathan {
204fcdfaefcSsimonb 
2050588cdfcSjonathan 	return (ioctl(handle, PPS_IOC_FETCH, ppsinfobuf));
2060588cdfcSjonathan }
2070588cdfcSjonathan 
208*fbae48b9Sperry static __inline int
2090588cdfcSjonathan time_pps_kcbind(handle, kernel_consumer, edge, tsformat)
2100588cdfcSjonathan 	pps_handle_t handle;
2110588cdfcSjonathan 	const int kernel_consumer;
2120588cdfcSjonathan 	const int edge;
2130588cdfcSjonathan 	const int tsformat;
2140588cdfcSjonathan {
215fcdfaefcSsimonb 
216ba9d68d0Schristos 	return (ioctl(handle, PPS_IOC_KCBIND, __UNCONST(&edge)));
2170588cdfcSjonathan }
21878741b62Sjonathan #endif /* !_KERNEL*/
21978741b62Sjonathan #endif /* SYS_TIMEPPS_H_ */
220