Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PPS API kernel header |
| 3 | * |
| 4 | * Copyright (C) 2009 Rodolfo Giometti <giometti@linux.it> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
Alexander Gordeev | 7a21a3c | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 21 | #ifndef LINUX_PPS_KERNEL_H |
| 22 | #define LINUX_PPS_KERNEL_H |
| 23 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 24 | #include <linux/pps.h> |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 25 | #include <linux/cdev.h> |
| 26 | #include <linux/device.h> |
| 27 | #include <linux/time.h> |
| 28 | |
| 29 | /* |
| 30 | * Global defines |
| 31 | */ |
| 32 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 33 | struct pps_device; |
| 34 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 35 | /* The specific PPS source info */ |
| 36 | struct pps_source_info { |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 37 | char name[PPS_MAX_NAME_LEN]; /* symbolic name */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 38 | char path[PPS_MAX_NAME_LEN]; /* path of connected device */ |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 39 | int mode; /* PPS allowed mode */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 40 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 41 | void (*echo)(struct pps_device *pps, |
| 42 | int event, void *data); /* PPS echo function */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 43 | |
| 44 | struct module *owner; |
George Spelvin | 513b032 | 2013-02-10 04:08:32 -0500 | [diff] [blame] | 45 | struct device *dev; /* Parent device for device_create */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 48 | struct pps_event_time { |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 49 | #ifdef CONFIG_NTP_PPS |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 50 | struct timespec64 ts_raw; |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 51 | #endif /* CONFIG_NTP_PPS */ |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 52 | struct timespec64 ts_real; |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 55 | /* The main struct */ |
| 56 | struct pps_device { |
| 57 | struct pps_source_info info; /* PSS source info */ |
| 58 | |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 59 | struct pps_kparams params; /* PPS current params */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 60 | |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 61 | __u32 assert_sequence; /* PPS assert event seq # */ |
| 62 | __u32 clear_sequence; /* PPS clear event seq # */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 63 | struct pps_ktime assert_tu; |
| 64 | struct pps_ktime clear_tu; |
| 65 | int current_mode; /* PPS mode at event time */ |
| 66 | |
Alexander Gordeev | 3003d55 | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 67 | unsigned int last_ev; /* last PPS event id */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 68 | wait_queue_head_t queue; /* PPS event queue */ |
| 69 | |
| 70 | unsigned int id; /* PPS source unique ID */ |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 71 | void const *lookup_cookie; /* For pps_lookup_dev() only */ |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 72 | struct cdev cdev; |
| 73 | struct device *dev; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 74 | struct fasync_struct *async_queue; /* fasync method */ |
| 75 | spinlock_t lock; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * Global variables |
| 80 | */ |
| 81 | |
Greg Kroah-Hartman | bd0eae4 | 2013-07-24 15:05:19 -0700 | [diff] [blame] | 82 | extern const struct attribute_group *pps_groups[]; |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
George Spelvin | 513b032 | 2013-02-10 04:08:32 -0500 | [diff] [blame] | 85 | * Internal functions. |
| 86 | * |
| 87 | * These are not actually part of the exported API, but this is a |
| 88 | * convenient header file to put them in. |
| 89 | */ |
| 90 | |
| 91 | extern int pps_register_cdev(struct pps_device *pps); |
| 92 | extern void pps_unregister_cdev(struct pps_device *pps); |
| 93 | |
| 94 | /* |
Rodolfo Giometti | eae9d2b | 2009-06-17 16:28:37 -0700 | [diff] [blame] | 95 | * Exported functions |
| 96 | */ |
| 97 | |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 98 | extern struct pps_device *pps_register_source( |
| 99 | struct pps_source_info *info, int default_params); |
| 100 | extern void pps_unregister_source(struct pps_device *pps); |
Alexander Gordeev | 5e196d3 | 2011-01-12 17:00:51 -0800 | [diff] [blame] | 101 | extern void pps_event(struct pps_device *pps, |
| 102 | struct pps_event_time *ts, int event, void *data); |
Robert P. J. Day | a2d8180 | 2017-09-08 16:17:19 -0700 | [diff] [blame] | 103 | /* Look up a pps_device by magic cookie */ |
George Spelvin | 513b032 | 2013-02-10 04:08:32 -0500 | [diff] [blame] | 104 | struct pps_device *pps_lookup_dev(void const *cookie); |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 105 | |
| 106 | static inline void timespec_to_pps_ktime(struct pps_ktime *kt, |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 107 | struct timespec64 ts) |
Alexander Gordeev | 6f4229b | 2011-01-12 17:00:50 -0800 | [diff] [blame] | 108 | { |
| 109 | kt->sec = ts.tv_sec; |
| 110 | kt->nsec = ts.tv_nsec; |
| 111 | } |
| 112 | |
Christopher S. Hall | ba26621 | 2016-02-22 03:15:21 -0800 | [diff] [blame] | 113 | static inline void pps_get_ts(struct pps_event_time *ts) |
| 114 | { |
| 115 | struct system_time_snapshot snap; |
| 116 | |
| 117 | ktime_get_snapshot(&snap); |
| 118 | ts->ts_real = ktime_to_timespec64(snap.real); |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 119 | #ifdef CONFIG_NTP_PPS |
Christopher S. Hall | ba26621 | 2016-02-22 03:15:21 -0800 | [diff] [blame] | 120 | ts->ts_raw = ktime_to_timespec64(snap.raw); |
| 121 | #endif |
Alexander Gordeev | e2c18e4 | 2011-01-12 17:00:57 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Ben Hutchings | 220a60a | 2012-09-03 11:34:58 +0100 | [diff] [blame] | 124 | /* Subtract known time delay from PPS event time(s) */ |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 125 | static inline void pps_sub_ts(struct pps_event_time *ts, struct timespec64 delta) |
Ben Hutchings | 220a60a | 2012-09-03 11:34:58 +0100 | [diff] [blame] | 126 | { |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 127 | ts->ts_real = timespec64_sub(ts->ts_real, delta); |
Ben Hutchings | 220a60a | 2012-09-03 11:34:58 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_NTP_PPS |
Arnd Bergmann | ade1bdf | 2015-09-28 22:21:31 +0200 | [diff] [blame] | 129 | ts->ts_raw = timespec64_sub(ts->ts_raw, delta); |
Ben Hutchings | 220a60a | 2012-09-03 11:34:58 +0100 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
Alexander Gordeev | 7a21a3c | 2011-01-12 17:00:49 -0800 | [diff] [blame] | 133 | #endif /* LINUX_PPS_KERNEL_H */ |