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

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 501 lines · 256 code · 92 blank · 153 comment · 28 complexity · 242a32e6577d2de47b76705da89dfb69 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 Solaris (2.6 and above). *
  25. * *
  26. ***********************************************************************
  27. * *
  28. * A full PPSAPI interface to the Solaris 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. /* Solaris version, TIOCGPPSEV and TIOCSPPS assumed to exist. */
  49. #ifndef _SYS_TIMEPPS_H_
  50. #define _SYS_TIMEPPS_H_
  51. #include <termios.h> /* to get TOCGPPSEV and TIOCSPPS */
  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. } pps_unit_t;
  159. typedef pps_unit_t* pps_handle_t; /* pps handlebars */
  160. /*
  161. *------ Here begins the implementation-specific part! ------
  162. */
  163. #include <errno.h>
  164. /*
  165. * create PPS handle from file descriptor
  166. */
  167. static inline int
  168. time_pps_create(
  169. int filedes, /* file descriptor */
  170. pps_handle_t *handle /* returned handle */
  171. )
  172. {
  173. int one = 1;
  174. /*
  175. * Check for valid arguments and attach PPS signal.
  176. */
  177. if (!handle) {
  178. errno = EFAULT;
  179. return (-1); /* null pointer */
  180. }
  181. if (ioctl(filedes, TIOCSPPS, &one) < 0) {
  182. perror("refclock_ioctl: TIOCSPPS failed:");
  183. return (-1);
  184. }
  185. /*
  186. * Allocate and initialize default unit structure.
  187. */
  188. *handle = malloc(sizeof(pps_unit_t));
  189. if (!(*handle)) {
  190. errno = EBADF;
  191. return (-1); /* what, no memory? */
  192. }
  193. memset(*handle, 0, sizeof(pps_unit_t));
  194. (*handle)->filedes = filedes;
  195. (*handle)->params.api_version = PPS_API_VERS_1;
  196. (*handle)->params.mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
  197. return (0);
  198. }
  199. /*
  200. * release PPS handle
  201. */
  202. static inline int
  203. time_pps_destroy(
  204. pps_handle_t handle
  205. )
  206. {
  207. /*
  208. * Check for valid arguments and detach PPS signal.
  209. */
  210. if (!handle) {
  211. errno = EBADF;
  212. return (-1); /* bad handle */
  213. }
  214. free(handle);
  215. return (0);
  216. }
  217. /*
  218. * set parameters for handle
  219. */
  220. static inline int
  221. time_pps_setparams(
  222. pps_handle_t handle,
  223. const pps_params_t *params
  224. )
  225. {
  226. int mode, mode_in;
  227. /*
  228. * Check for valid arguments and set parameters.
  229. */
  230. if (!handle) {
  231. errno = EBADF;
  232. return (-1); /* bad handle */
  233. }
  234. if (!params) {
  235. errno = EFAULT;
  236. return (-1); /* bad argument */
  237. }
  238. /*
  239. * There was no reasonable consensu in the API working group.
  240. * I require `api_version' to be set!
  241. */
  242. if (params->api_version != PPS_API_VERS_1) {
  243. errno = EINVAL;
  244. return(-1);
  245. }
  246. /*
  247. * only settable modes are PPS_CAPTUREASSERT and PPS_OFFSETASSERT
  248. */
  249. mode_in = params->mode;
  250. /* turn off read-only bits */
  251. mode_in &= ~PPS_RO;
  252. /* test remaining bits, should only have captureassert and/or offsetassert */
  253. if (mode_in & ~(PPS_CAPTUREASSERT | PPS_OFFSETASSERT)) {
  254. errno = EOPNOTSUPP;
  255. return(-1);
  256. }
  257. /*
  258. * ok, ready to go.
  259. */
  260. mode = handle->params.mode;
  261. memcpy(&handle->params, params, sizeof(pps_params_t));
  262. handle->params.api_version = PPS_API_VERS_1;
  263. handle->params.mode = mode | mode_in;
  264. return (0);
  265. }
  266. /*
  267. * get parameters for handle
  268. */
  269. static inline int
  270. time_pps_getparams(
  271. pps_handle_t handle,
  272. pps_params_t *params
  273. )
  274. {
  275. /*
  276. * Check for valid arguments and get parameters.
  277. */
  278. if (!handle) {
  279. errno = EBADF;
  280. return (-1); /* bad handle */
  281. }
  282. if (!params) {
  283. errno = EFAULT;
  284. return (-1); /* bad argument */
  285. }
  286. memcpy(params, &handle->params, sizeof(pps_params_t));
  287. return (0);
  288. }
  289. /* (
  290. * get capabilities for handle
  291. */
  292. static inline int
  293. time_pps_getcap(
  294. pps_handle_t handle,
  295. int *mode
  296. )
  297. {
  298. /*
  299. * Check for valid arguments and get capabilities.
  300. */
  301. if (!handle) {
  302. errno = EBADF;
  303. return (-1); /* bad handle */
  304. }
  305. if (!mode) {
  306. errno = EFAULT;
  307. return (-1); /* bad argument */
  308. }
  309. *mode = PPS_CAP;
  310. return (0);
  311. }
  312. /*
  313. * Fetch timestamps
  314. */
  315. static inline int
  316. time_pps_fetch(
  317. pps_handle_t handle,
  318. const int tsformat,
  319. pps_info_t *ppsinfo,
  320. const struct timespec *timeout
  321. )
  322. {
  323. struct ppsclockev {
  324. struct timeval tv;
  325. u_int serial;
  326. } ev;
  327. pps_info_t infobuf;
  328. /*
  329. * Check for valid arguments and fetch timestamps
  330. */
  331. if (!handle) {
  332. errno = EBADF;
  333. return (-1); /* bad handle */
  334. }
  335. if (!ppsinfo) {
  336. errno = EFAULT;
  337. return (-1); /* bad argument */
  338. }
  339. /*
  340. * nb. PPS_CANWAIT is NOT set by the implementation, we can totally
  341. * ignore the timeout variable.
  342. */
  343. memset(&infobuf, 0, sizeof(infobuf));
  344. /*
  345. * if not captureassert, nothing to return.
  346. */
  347. if (!handle->params.mode & PPS_CAPTUREASSERT) {
  348. memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
  349. return (0);
  350. }
  351. if (ioctl(handle->filedes, TIOCGPPSEV, (caddr_t) &ev) < 0) {
  352. perror("time_pps_fetch:");
  353. errno = EOPNOTSUPP;
  354. return(-1);
  355. }
  356. /*
  357. * Apply offsets as specified. Note that only assert timestamps
  358. * are captured by this interface.
  359. */
  360. infobuf.assert_sequence = ev.serial;
  361. infobuf.assert_timestamp.tv_sec = ev.tv.tv_sec;
  362. infobuf.assert_timestamp.tv_nsec = ev.tv.tv_usec * 1000;
  363. if (handle->params.mode & PPS_OFFSETASSERT) {
  364. infobuf.assert_timestamp.tv_sec += handle->params.assert_offset.tv_sec;
  365. infobuf.assert_timestamp.tv_nsec += handle->params.assert_offset.tv_nsec;
  366. PPS_NORMALIZE(infobuf.assert_timestamp);
  367. }
  368. /*
  369. * Translate to specified format
  370. */
  371. switch (tsformat) {
  372. case PPS_TSFMT_TSPEC:
  373. break; /* timespec format requires no translation */
  374. case PPS_TSFMT_NTPFP: /* NTP format requires conversion to fraction form */
  375. PPS_TSPECTONTP(infobuf.assert_timestamp_ntpfp);
  376. break;
  377. default:
  378. errno = EINVAL;
  379. return (-1);
  380. }
  381. infobuf.current_mode = handle->params.mode;
  382. memcpy(ppsinfo, &infobuf, sizeof(pps_info_t));
  383. return (0);
  384. }
  385. /*
  386. * specify kernel consumer
  387. */
  388. static inline int
  389. time_pps_kcbind(
  390. pps_handle_t handle,
  391. const int kernel_consumer,
  392. const int edge, const int tsformat
  393. )
  394. {
  395. /*
  396. * Check for valid arguments and bind kernel consumer
  397. */
  398. if (!handle) {
  399. errno = EBADF;
  400. return (-1); /* bad handle */
  401. }
  402. if (geteuid() != 0) {
  403. errno = EPERM;
  404. return (-1); /* must be superuser */
  405. }
  406. errno = EOPNOTSUPP;
  407. return(-1);
  408. }
  409. #endif /* _SYS_TIMEPPS_H_ */