7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
32 #include <linux/pps_kernel.h>
33 #include <linux/slab.h>
41 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
43 ts->nsec += offset->nsec;
44 while (ts->nsec >= NSEC_PER_SEC) {
45 ts->nsec -= NSEC_PER_SEC;
48 while (ts->nsec < 0) {
49 ts->nsec += NSEC_PER_SEC;
52 ts->sec += offset->sec;
55 static void pps_echo_client_default(struct pps_device *pps, int event,
58 dev_info(pps->dev, "echo %s %s\n",
59 event & PPS_CAPTUREASSERT ? "assert" : "",
60 event & PPS_CAPTURECLEAR ? "clear" : "");
67 /* pps_register_source - add a PPS source in the system
68 * @info: the PPS info struct
69 * @default_params: the default PPS parameters of the new source
71 * This function is used to add a new PPS source in the system. The new
72 * source is described by info's fields and it will have, as default PPS
73 * parameters, the ones specified into default_params.
75 * The function returns, in case of success, the PPS device. Otherwise
79 struct pps_device *pps_register_source(struct pps_source_info *info,
82 struct pps_device *pps;
86 if ((info->mode & default_params) != default_params) {
87 pr_err("%s: unsupported default parameters\n",
90 goto pps_register_source_exit;
92 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
93 pr_err("%s: unspecified time format\n",
96 goto pps_register_source_exit;
99 /* Allocate memory for the new PPS source struct */
100 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
103 goto pps_register_source_exit;
106 /* These initializations must be done before calling idr_alloc()
107 * in order to avoid reces into pps_event().
109 pps->params.api_version = PPS_API_VERS;
110 pps->params.mode = default_params;
113 /* check for default echo function */
114 if ((pps->info.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) &&
115 pps->info.echo == NULL)
116 pps->info.echo = pps_echo_client_default;
118 init_waitqueue_head(&pps->queue);
119 spin_lock_init(&pps->lock);
121 /* Create the char device */
122 err = pps_register_cdev(pps);
124 pr_err("%s: unable to create char device\n",
129 dev_info(pps->dev, "new PPS source %s\n", info->name);
136 pps_register_source_exit:
137 pr_err("%s: unable to register source\n", info->name);
141 EXPORT_SYMBOL(pps_register_source);
143 /* pps_unregister_source - remove a PPS source from the system
144 * @pps: the PPS source
146 * This function is used to remove a previously registered PPS source from
150 void pps_unregister_source(struct pps_device *pps)
153 pps_unregister_cdev(pps);
155 /* don't have to kfree(pps) here because it will be done on
156 * device destruction */
158 EXPORT_SYMBOL(pps_unregister_source);
160 /* pps_event - register a PPS event into the system
161 * @pps: the PPS device
162 * @ts: the event timestamp
163 * @event: the event type
164 * @data: userdef pointer
166 * This function is used by each PPS client in order to register a new
167 * PPS event into the system (it's usually called inside an IRQ handler).
169 * If an echo function is associated with the PPS device it will be called
171 * pps->info.echo(pps, event, data);
173 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
178 struct pps_ktime ts_real = { .sec = 0, .nsec = 0, .flags = 0 };
180 /* check event type */
181 BUG_ON((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0);
183 dev_dbg(pps->dev, "PPS event at %lld.%09ld\n",
184 (s64)ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
186 timespec_to_pps_ktime(&ts_real, ts->ts_real);
188 spin_lock_irqsave(&pps->lock, flags);
190 /* Must call the echo function? */
191 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
192 pps->info.echo(pps, event, data);
194 /* Check the event */
195 pps->current_mode = pps->params.mode;
196 if (event & pps->params.mode & PPS_CAPTUREASSERT) {
197 /* We have to add an offset? */
198 if (pps->params.mode & PPS_OFFSETASSERT)
199 pps_add_offset(&ts_real,
200 &pps->params.assert_off_tu);
202 /* Save the time stamp */
203 pps->assert_tu = ts_real;
204 pps->assert_sequence++;
205 dev_dbg(pps->dev, "capture assert seq #%u\n",
206 pps->assert_sequence);
210 if (event & pps->params.mode & PPS_CAPTURECLEAR) {
211 /* We have to add an offset? */
212 if (pps->params.mode & PPS_OFFSETCLEAR)
213 pps_add_offset(&ts_real,
214 &pps->params.clear_off_tu);
216 /* Save the time stamp */
217 pps->clear_tu = ts_real;
218 pps->clear_sequence++;
219 dev_dbg(pps->dev, "capture clear seq #%u\n",
220 pps->clear_sequence);
225 pps_kc_event(pps, ts, event);
227 /* Wake up if captured something */
230 wake_up_interruptible_all(&pps->queue);
232 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
235 spin_unlock_irqrestore(&pps->lock, flags);
237 EXPORT_SYMBOL(pps_event);