pps_kernel.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * PPS API kernel header
  4. *
  5. * Copyright (C) 2009 Rodolfo Giometti <giometti@linux.it>
  6. */
  7. #ifndef LINUX_PPS_KERNEL_H
  8. #define LINUX_PPS_KERNEL_H
  9. #include <linux/pps.h>
  10. #include <linux/cdev.h>
  11. #include <linux/device.h>
  12. #include <linux/time.h>
  13. /*
  14. * Global defines
  15. */
  16. struct pps_device;
  17. /* The specific PPS source info */
  18. struct pps_source_info {
  19. char name[PPS_MAX_NAME_LEN]; /* symbolic name */
  20. char path[PPS_MAX_NAME_LEN]; /* path of connected device */
  21. int mode; /* PPS allowed mode */
  22. void (*echo)(struct pps_device *pps,
  23. int event, void *data); /* PPS echo function */
  24. struct module *owner;
  25. struct device *dev; /* Parent device for device_create */
  26. };
  27. struct pps_event_time {
  28. #ifdef CONFIG_NTP_PPS
  29. struct timespec64 ts_raw;
  30. #endif /* CONFIG_NTP_PPS */
  31. struct timespec64 ts_real;
  32. };
  33. /* The main struct */
  34. struct pps_device {
  35. struct pps_source_info info; /* PSS source info */
  36. struct pps_kparams params; /* PPS current params */
  37. __u32 assert_sequence; /* PPS assert event seq # */
  38. __u32 clear_sequence; /* PPS clear event seq # */
  39. struct pps_ktime assert_tu;
  40. struct pps_ktime clear_tu;
  41. int current_mode; /* PPS mode at event time */
  42. unsigned int last_ev; /* last PPS event id */
  43. wait_queue_head_t queue; /* PPS event queue */
  44. unsigned int id; /* PPS source unique ID */
  45. void const *lookup_cookie; /* For pps_lookup_dev() only */
  46. struct cdev cdev;
  47. struct device *dev;
  48. struct fasync_struct *async_queue; /* fasync method */
  49. spinlock_t lock;
  50. };
  51. /*
  52. * Global variables
  53. */
  54. extern const struct attribute_group *pps_groups[];
  55. /*
  56. * Internal functions.
  57. *
  58. * These are not actually part of the exported API, but this is a
  59. * convenient header file to put them in.
  60. */
  61. extern int pps_register_cdev(struct pps_device *pps);
  62. extern void pps_unregister_cdev(struct pps_device *pps);
  63. /*
  64. * Exported functions
  65. */
  66. extern struct pps_device *pps_register_source(
  67. struct pps_source_info *info, int default_params);
  68. extern void pps_unregister_source(struct pps_device *pps);
  69. extern void pps_event(struct pps_device *pps,
  70. struct pps_event_time *ts, int event, void *data);
  71. /* Look up a pps_device by magic cookie */
  72. struct pps_device *pps_lookup_dev(void const *cookie);
  73. static inline void timespec_to_pps_ktime(struct pps_ktime *kt,
  74. struct timespec64 ts)
  75. {
  76. kt->sec = ts.tv_sec;
  77. kt->nsec = ts.tv_nsec;
  78. }
  79. static inline void pps_get_ts(struct pps_event_time *ts)
  80. {
  81. struct system_time_snapshot snap;
  82. ktime_get_snapshot(&snap);
  83. ts->ts_real = ktime_to_timespec64(snap.real);
  84. #ifdef CONFIG_NTP_PPS
  85. ts->ts_raw = ktime_to_timespec64(snap.raw);
  86. #endif
  87. }
  88. /* Subtract known time delay from PPS event time(s) */
  89. static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta)
  90. {
  91. ts->ts_real = timespec64_sub(ts->ts_real, delta);
  92. #ifdef CONFIG_NTP_PPS
  93. ts->ts_raw = timespec64_sub(ts->ts_raw, delta);
  94. #endif
  95. }
  96. #endif /* LINUX_PPS_KERNEL_H */