1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/sched.h>
28#include <linux/time.h>
29#include <linux/timex.h>
30#include <linux/spinlock.h>
31#include <linux/fs.h>
32#include <linux/pps_kernel.h>
33#include <linux/slab.h>
34
35#include "kc.h"
36
37
38
39
40
41static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
42{
43 ts->nsec += offset->nsec;
44 while (ts->nsec >= NSEC_PER_SEC) {
45 ts->nsec -= NSEC_PER_SEC;
46 ts->sec++;
47 }
48 while (ts->nsec < 0) {
49 ts->nsec += NSEC_PER_SEC;
50 ts->sec--;
51 }
52 ts->sec += offset->sec;
53}
54
55static void pps_echo_client_default(struct pps_device *pps, int event,
56 void *data)
57{
58 dev_info(pps->dev, "echo %s %s\n",
59 event & PPS_CAPTUREASSERT ? "assert" : "",
60 event & PPS_CAPTURECLEAR ? "clear" : "");
61}
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78struct pps_device *pps_register_source(struct pps_source_info *info,
79 int default_params)
80{
81 struct pps_device *pps;
82 int err;
83
84
85 if ((info->mode & default_params) != default_params) {
86 pr_err("%s: unsupported default parameters\n",
87 info->name);
88 err = -EINVAL;
89 goto pps_register_source_exit;
90 }
91 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
92 pr_err("%s: unspecified time format\n",
93 info->name);
94 err = -EINVAL;
95 goto pps_register_source_exit;
96 }
97
98
99 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
100 if (pps == NULL) {
101 err = -ENOMEM;
102 goto pps_register_source_exit;
103 }
104
105
106
107
108 pps->params.api_version = PPS_API_VERS;
109 pps->params.mode = default_params;
110 pps->info = *info;
111
112
113 if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
114 pps->info.echo == NULL)
115 pps->info.echo = pps_echo_client_default;
116
117 init_waitqueue_head(&pps->queue);
118 spin_lock_init(&pps->lock);
119
120
121 err = pps_register_cdev(pps);
122 if (err < 0) {
123 pr_err("%s: unable to create char device\n",
124 info->name);
125 goto kfree_pps;
126 }
127
128 dev_info(pps->dev, "new PPS source %s\n", info->name);
129
130 return pps;
131
132kfree_pps:
133 kfree(pps);
134
135pps_register_source_exit:
136 pr_err("%s: unable to register source\n", info->name);
137
138 return NULL;
139}
140EXPORT_SYMBOL(pps_register_source);
141
142
143
144
145
146
147
148
149void pps_unregister_source(struct pps_device *pps)
150{
151 pps_kc_remove(pps);
152 pps_unregister_cdev(pps);
153
154
155
156}
157EXPORT_SYMBOL(pps_unregister_source);
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
173 void *data)
174{
175 unsigned long flags;
176 int captured = 0;
177 struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
178
179
180 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
181
182 dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
183 (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
184
185 timespec_to_pps_ktime(&ts_real, ts->ts_real);
186
187 spin_lock_irqsave(&pps->lock, flags);
188
189
190 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
191 pps->info.echo(pps, event, data);
192
193
194 pps->current_mode = pps->params.mode;
195 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
196
197 if (pps->params.mode & PPS_OFFSETASSERT)
198 pps_add_offset(&ts_real,
199 &pps->params.assert_off_tu);
200
201
202 pps->assert_tu = ts_real;
203 pps->assert_sequence++;
204 dev_dbg(pps->dev, "capture assert seq #%u\n",
205 pps->assert_sequence);
206
207 captured = ~0;
208 }
209 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
210
211 if (pps->params.mode & PPS_OFFSETCLEAR)
212 pps_add_offset(&ts_real,
213 &pps->params.clear_off_tu);
214
215
216 pps->clear_tu = ts_real;
217 pps->clear_sequence++;
218 dev_dbg(pps->dev, "capture clear seq #%u\n",
219 pps->clear_sequence);
220
221 captured = ~0;
222 }
223
224 pps_kc_event(pps, ts, event);
225
226
227 if (captured) {
228 pps->last_ev++;
229 wake_up_interruptible_all(&pps->queue);
230
231 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
232 }
233
234 spin_unlock_irqrestore(&pps->lock, flags);
235}
236EXPORT_SYMBOL(pps_event);
237