/contrib/ntp/include/timepps-SCO.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 503 lines · 255 code · 92 blank · 156 comment · 31 complexity · 6049d188304b28b6f57375a3489df617 MD5 · raw file

  1. /***********************************************************************
  2. * *
  3. * Copyright (c) David L. Mills 1999-2000 *
  4. * *
  5. * Permission to use, copy, modify, and distribute this software and *
  6. * its documentation for any purpose and without fee is hereby *
  7. * granted, provided that the above copyright notice appears in all *
  8. * copies and that both the copyright notice and this permission *
  9. * notice appear in supporting documentation, and that the name *
  10. * University of Delaware not be used in advertising or publicity *
  11. * pertaining to distribution of the software without specific, *
  12. * written prior permission. The University of Delaware makes no *
  13. * representations about the suitability this software for any *
  14. * purpose. It is provided "as is" without express or implied *
  15. * warranty. *
  16. * *
  17. ***********************************************************************
  18. * *
  19. * This header file complies with "Pulse-Per-Second API for UNIX-like *
  20. * Operating Systems, Version 1.0", rfc2783. Credit is due Jeff Mogul *
  21. * and Marc Brett, from whom much of this code was shamelessly stolen. *
  22. * *
  23. * this modified timepps.h can be used to provide a PPSAPI interface *
  24. * to a machine running SCO Unix. *
  25. * *
  26. ***********************************************************************
  27. * *
  28. * A full PPSAPI interface to the SCO Unix kernel would be better, but *
  29. * this at least removes the necessity for special coding from the NTP *
  30. * NTP drivers. *
  31. * *
  32. ***********************************************************************
  33. * *
  34. * Some of this include file *
  35. * Copyright (c) 1999 by Ulrich Windl, *
  36. * based on code by Reg Clemens <reg@dwf.com> *
  37. * based on code by Poul-Henning Kamp <phk@FreeBSD.org> *
  38. * *
  39. ***********************************************************************
  40. * *
  41. * "THE BEER-WARE LICENSE" (Revision 42): *
  42. * <phk@FreeBSD.org> wrote this file. As long as you retain this *
  43. * notice you can do whatever you want with this stuff. If we meet some*
  44. * day, and you think this stuff is worth it, you can buy me a beer *
  45. * in return. Poul-Henning Kamp *
  46. * *
  47. **********************************************************************/
  48. /*SCO UNIX version, TIOCDCDTIMESTAMP assumed to exist. */
  49. #ifndef _SYS_TIMEPPS_H_
  50. #define _SYS_TIMEPPS_H_
  51. #include <termios.h> /* to get TIOCDCDTIMESTAMP */
  52. /* Implementation note: the logical states ``assert'' and ``clear''
  53. * are implemented in terms of the UART register, i.e. ``assert''
  54. * means the bit is set.
  55. */
  56. /*
  57. * The following definitions are architecture independent
  58. */
  59. #define PPS_API_VERS_1 1 /* API version number */
  60. #define PPS_JAN_1970 2208988800UL /* 1970 - 1900 in seconds */
  61. #define PPS_NANOSECOND 1000000000L /* one nanosecond in decimal */
  62. #define PPS_FRAC 4294967296. /* 2^32 as a double */
  63. #define PPS_NORMALIZE(x) /* normalize timespec */ \
  64. do { \
  65. if ((x).tv_nsec >= PPS_NANOSECOND) { \
  66. (x).tv_nsec -= PPS_NANOSECOND; \
  67. (x).tv_sec++; \
  68. } else if ((x).tv_nsec < 0) { \
  69. (x).tv_nsec += PPS_NANOSECOND; \
  70. (x).tv_sec--; \
  71. } \
  72. } while (0)
  73. #define PPS_TSPECTONTP(x) /* convert timespec to l_fp */ \
  74. do { \
  75. double d_temp; \
  76. \
  77. (x).integral += (unsigned int)PPS_JAN_1970; \
  78. d_temp = (x).fractional * PPS_FRAC / PPS_NANOSECOND; \
  79. if (d_temp >= PPS_FRAC) \
  80. (x).integral++; \
  81. (x).fractional = (unsigned int)d_temp; \
  82. } while (0)
  83. /*
  84. * Device/implementation parameters (mode)
  85. */
  86. #define PPS_CAPTUREASSERT 0x01 /* capture assert events */
  87. #define PPS_CAPTURECLEAR 0x02 /* capture clear events */
  88. #define PPS_CAPTUREBOTH 0x03 /* capture assert and clear events */
  89. #define PPS_OFFSETASSERT 0x10 /* apply compensation for assert ev. */
  90. #define PPS_OFFSETCLEAR 0x20 /* apply compensation for clear ev. */
  91. #define PPS_OFFSETBOTH 0x30 /* apply compensation for both */
  92. #define PPS_CANWAIT 0x100 /* Can we wait for an event? */
  93. #define PPS_CANPOLL 0x200 /* "This bit is reserved for */
  94. /*
  95. * Kernel actions (mode)
  96. */
  97. #define PPS_ECHOASSERT 0x40 /* feed back assert event to output */
  98. #define PPS_ECHOCLEAR 0x80 /* feed back clear event to output */
  99. /*
  100. * Timestamp formats (tsformat)
  101. */
  102. #define PPS_TSFMT_TSPEC 0x1000 /* select timespec format */
  103. #define PPS_TSFMT_NTPFP 0x2000 /* select NTP format */
  104. /*
  105. * Kernel discipline actions (not used in Solaris)
  106. */
  107. #define PPS_KC_HARDPPS 0 /* enable kernel consumer */
  108. #define PPS_KC_HARDPPS_PLL 1 /* phase-lock mode */
  109. #define PPS_KC_HARDPPS_FLL 2 /* frequency-lock mode */
  110. /*
  111. * Type definitions
  112. */
  113. typedef unsigned long pps_seq_t; /* sequence number */
  114. typedef struct ntp_fp {
  115. unsigned int integral;
  116. unsigned int fractional;
  117. } ntp_fp_t; /* NTP-compatible time stamp */
  118. typedef union pps_timeu { /* timestamp format */
  119. struct timespec tspec;
  120. ntp_fp_t ntpfp;
  121. unsigned long longpad[3];
  122. } pps_timeu_t; /* generic data type to represent time stamps */
  123. /*
  124. * Timestamp information structure
  125. */
  126. typedef struct pps_info {
  127. pps_seq_t assert_sequence; /* seq. num. of assert event */
  128. pps_seq_t clear_sequence; /* seq. num. of clear event */
  129. pps_timeu_t assert_tu; /* time of assert event */
  130. pps_timeu_t clear_tu; /* time of clear event */
  131. int current_mode; /* current mode bits */
  132. } pps_info_t;
  133. #define assert_timestamp assert_tu.tspec
  134. #define clear_timestamp clear_tu.tspec
  135. #define assert_timestamp_ntpfp assert_tu.ntpfp
  136. #define clear_timestamp_ntpfp clear_tu.ntpfp
  137. /*
  138. * Parameter structure
  139. */
  140. typedef struct pps_params {
  141. int api_version; /* API version # */
  142. int mode; /* mode bits */
  143. pps_timeu_t assert_off_tu; /* offset compensation for assert */
  144. pps_timeu_t clear_off_tu; /* offset compensation for clear */
  145. } pps_params_t;
  146. #define assert_offset assert_off_tu.tspec
  147. #define clear_offset clear_off_tu.tspec
  148. #define assert_offset_ntpfp assert_off_tu.ntpfp
  149. #define clear_offset_ntpfp clear_off_tu.ntpfp
  150. /*
  151. * The following definitions are architecture-dependent
  152. */
  153. #define PPS_CAP (PPS_CAPTUREASSERT | PPS_OFFSETASSERT | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
  154. #define PPS_RO (PPS_CANWAIT | PPS_CANPOLL | PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)
  155. typedef struct {
  156. int filedes; /* file descriptor */
  157. pps_params_t params; /* PPS parameters set by user */
  158. struct timeval tv_save;
  159. pps_seq_t serial;
  160. } pps_unit_t;
  161. typedef pps_unit_t* pps_handle_t; /* pps handlebars */
  162. /*
  163. *------ Here begins the implementation-specific part! ------
  164. */
  165. #include <errno.h>
  166. /*
  167. * create PPS handle from file descriptor
  168. */
  169. static inline int
  170. time_pps_create(
  171. int filedes, /* file descriptor */
  172. pps_handle_t *handle /* returned handle */
  173. )
  174. {
  175. int one = 1;
  176. /*
  177. * Check for valid arguments and attach PPS signal.
  178. */
  179. if (!handle) {
  180. errno = EFAULT;
  181. return (-1); /* null pointer */
  182. }
  183. /*
  184. * Allocate and initialize default unit structure.
  185. */
  186. *handle = malloc(sizeof(pps_unit_t));
  187. if (!(*handle)) {
  188. errno = EBADF;
  189. return (-1); /* what, no memory? */
  190. }
  191. memset(*handle, 0, sizeof(pps_unit_t));
  192. (*handle)->filedes = filedes;
  193. (*handle)->params.api_version = PPS_API_VERS_1;
  194. (*handle)->params.mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
  195. return (0);
  196. }
  197. /*
  198. * release PPS handle
  199. */
  200. static inline int
  201. time_pps_destroy(
  202. pps_handle_t handle
  203. )
  204. {
  205. /*
  206. * Check for valid arguments and detach PPS signal.
  207. */
  208. if (!handle) {
  209. errno = EBADF;
  210. return (-1); /* bad handle */
  211. }
  212. free(handle);
  213. return (0);
  214. }
  215. /*
  216. * set parameters for handle
  217. */
  218. static inline int
  219. time_pps_setparams(
  220. pps_handle_t handle,
  221. const pps_params_t *params
  222. )
  223. {
  224. int mode, mode_in;
  225. /*
  226. * Check for valid arguments and set parameters.
  227. */
  228. if (!handle) {
  229. errno = EBADF;
  230. return (-1); /* bad handle */
  231. }
  232. if (!params) {
  233. errno = EFAULT;
  234. return (-1); /* bad argument */
  235. }
  236. /*
  237. * There was no reasonable consensu in the API working group.
  238. * I require `api_version' to be set!
  239. */
  240. if (params->api_version != PPS_API_VERS_1) {
  241. errno = EINVAL;
  242. return(-1);
  243. }
  244. /*
  245. * only settable modes are PPS_CAPTUREASSERT and PPS_OFFSETASSERT
  246. */
  247. mode_in = params->mode;
  248. /* turn off read-only bits */
  249. mode_in &= ~PPS_RO;
  250. /* test remaining bits, should only have captureassert and/or offsetassert */
  251. if (mode_in & ~(PPS_CAPTUREASSERT | PPS_OFFSETASSERT)) {
  252. errno = EOPNOTSUPP;
  253. return(-1);
  254. }
  255. /*
  256. * ok, ready to go.
  257. */
  258. mode = handle->params.mode;
  259. memcpy(&handle->params, params, sizeof(pps_params_t));
  260. handle->params.api_version = PPS_API_VERS_1;
  261. handle->params.mode = mode | mode_in;
  262. return (0);
  263. }
  264. /*
  265. * get parameters for handle
  266. */
  267. static inline int
  268. time_pps_getparams(
  269. pps_handle_t handle,
  270. pps_params_t *params
  271. )
  272. {
  273. /*
  274. * Check for valid arguments and get parameters.
  275. */
  276. if (!handle) {
  277. errno = EBADF;
  278. return (-1); /* bad handle */
  279. }
  280. if (!params) {
  281. errno = EFAULT;
  282. return (-1); /* bad argument */
  283. }
  284. memcpy(params, &handle->params, sizeof(pps_params_t));
  285. return (0);
  286. }
  287. /* (
  288. * get capabilities for handle
  289. */
  290. static inline int
  291. time_pps_getcap(
  292. pps_handle_t handle,
  293. int *mode
  294. )
  295. {
  296. /*
  297. * Check for valid arguments and get capabilities.
  298. */
  299. if (!handle) {
  300. errno = EBADF;
  301. return (-1); /* bad handle */
  302. }
  303. if (!mode) {
  304. errno = EFAULT;
  305. return (-1); /* bad argument */
  306. }
  307. *mode = PPS_CAP;
  308. return (0);
  309. }
  310. /*
  311. * Fetch timestamps
  312. */
  313. static inline int
  314. time_pps_fetch(
  315. pps_handle_t handle,
  316. const int tsformat,
  317. pps_info_t *ppsinfo,
  318. const struct timespec *timeout
  319. )
  320. {
  321. struct timeval tv;
  322. pps_info_t infobuf;
  323. /*
  324. * Check for valid arguments and fetch timestamps
  325. */
  326. if (!handle) {
  327. errno = EBADF;
  328. return (-1); /* bad handle */
  329. }
  330. if (!ppsinfo) {
  331. errno = EFAULT;
  332. return (-1); /* bad argument */
  333. }
  334. /*
  335. * nb. PPS_CANWAIT is NOT set by the implementation, we can totally
  336. * ignore the timeout variable.
  337. */
  338. memset(&infobuf, 0, sizeof(infobuf));
  339. /*
  340. * if not captureassert, nothing to return.
  341. */
  342. if (!handle->params.mode & PPS_CAPTUREASSERT) {
  343. memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
  344. return (0);
  345. }
  346. if (ioctl(instance->filedes, TIOCDCDTIMESTAMP, &tv) < 0) {
  347. perror("time_pps_fetch:");
  348. errno = EOPNOTSUPP;
  349. return(-1);
  350. }
  351. /*
  352. * fake serial here
  353. */
  354. if (tv.tv_sec != handle->tv_save.tv_sec || tv.tv_usec != handle->tv_save.tv_usec) {
  355. handle->tv_save = tv;
  356. handle->serial++;
  357. }
  358. /*
  359. * Apply offsets as specified. Note that only assert timestamps
  360. * are captured by this interface.
  361. */
  362. infobuf.assert_sequence = handle->serial;
  363. infobuf.assert_timestamp.tv_sec = tv.tv_sec;
  364. infobuf.assert_timestamp.tv_nsec = tv.tv_usec * 1000;
  365. if (handle->params.mode & PPS_OFFSETASSERT) {
  366. infobuf.assert_timestamp.tv_sec += handle->params.assert_offset.tv_sec;
  367. infobuf.assert_timestamp.tv_nsec += handle->params.assert_offset.tv_nsec;
  368. PPS_NORMALIZE(infobuf.assert_timestamp);
  369. }
  370. /*
  371. * Translate to specified format
  372. */
  373. switch (tsformat) {
  374. case PPS_TSFMT_TSPEC:
  375. break; /* timespec format requires no translation */
  376. case PPS_TSFMT_NTPFP: /* NTP format requires conversion to fraction form */
  377. PPS_TSPECTONTP(infobuf.assert_timestamp_ntpfp);
  378. break;
  379. default:
  380. errno = EINVAL;
  381. return (-1);
  382. }
  383. infobuf.current_mode = handle->params.mode;
  384. memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
  385. return (0);
  386. }
  387. /*
  388. * specify kernel consumer
  389. */
  390. static inline int
  391. time_pps_kcbind(
  392. pps_handle_t handle,
  393. const int kernel_consumer,
  394. const int edge, const int tsformat
  395. )
  396. {
  397. /*
  398. * Check for valid arguments and bind kernel consumer
  399. */
  400. if (!handle) {
  401. errno = EBADF;
  402. return (-1); /* bad handle */
  403. }
  404. if (geteuid() != 0) {
  405. errno = EPERM;
  406. return (-1); /* must be superuser */
  407. }
  408. errno = EOPNOTSUPP;
  409. return(-1);
  410. }
  411. #endif /* _SYS_TIMEPPS_H_ */