5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
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.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/time.h>
28 #include <linux/spinlock.h>
29 #include <linux/idr.h>
31 #include <linux/pps_kernel.h>
32 #include <linux/slab.h>
38 DEFINE_SPINLOCK(pps_idr_lock);
45 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
47 ts->nsec += offset->nsec;
48 while (ts->nsec >= NSEC_PER_SEC) {
49 ts->nsec -= NSEC_PER_SEC;
52 while (ts->nsec < 0) {
53 ts->nsec += NSEC_PER_SEC;
56 ts->sec += offset->sec;
63 /* pps_get_source - find a PPS source
64 * @source: the PPS source ID.
66 * This function is used to find an already registered PPS source into the
69 * The function returns NULL if found nothing, otherwise it returns a pointer
70 * to the PPS source data struct (the refcounter is incremented by 1).
73 struct pps_device *pps_get_source(int source)
75 struct pps_device *pps;
78 spin_lock_irqsave(&pps_idr_lock, flags);
80 pps = idr_find(&pps_idr, source);
82 atomic_inc(&pps->usage);
84 spin_unlock_irqrestore(&pps_idr_lock, flags);
89 /* pps_put_source - free the PPS source data
90 * @pps: a pointer to the PPS source.
92 * This function is used to free a PPS data struct if its refcount is 0.
95 void pps_put_source(struct pps_device *pps)
99 spin_lock_irqsave(&pps_idr_lock, flags);
100 BUG_ON(atomic_read(&pps->usage) == 0);
102 if (!atomic_dec_and_test(&pps->usage)) {
107 /* No more reference to the PPS source. We can safely remove the
110 idr_remove(&pps_idr, pps->id);
113 spin_unlock_irqrestore(&pps_idr_lock, flags);
117 /* pps_register_source - add a PPS source in the system
118 * @info: the PPS info struct
119 * @default_params: the default PPS parameters of the new source
121 * This function is used to add a new PPS source in the system. The new
122 * source is described by info's fields and it will have, as default PPS
123 * parameters, the ones specified into default_params.
125 * The function returns, in case of success, the PPS source ID.
128 int pps_register_source(struct pps_source_info *info, int default_params)
130 struct pps_device *pps;
135 if ((info->mode & default_params) != default_params) {
136 printk(KERN_ERR "pps: %s: unsupported default parameters\n",
139 goto pps_register_source_exit;
141 if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
142 info->echo == NULL) {
143 printk(KERN_ERR "pps: %s: echo function is not defined\n",
146 goto pps_register_source_exit;
148 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
149 printk(KERN_ERR "pps: %s: unspecified time format\n",
152 goto pps_register_source_exit;
155 /* Allocate memory for the new PPS source struct */
156 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
159 goto pps_register_source_exit;
162 /* These initializations must be done before calling idr_get_new()
163 * in order to avoid reces into pps_event().
165 pps->params.api_version = PPS_API_VERS;
166 pps->params.mode = default_params;
169 init_waitqueue_head(&pps->queue);
170 spin_lock_init(&pps->lock);
171 atomic_set(&pps->usage, 1);
173 /* Get new ID for the new PPS source */
174 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
179 spin_lock_irq(&pps_idr_lock);
181 /* Now really allocate the PPS source.
182 * After idr_get_new() calling the new source will be freely available
185 err = idr_get_new(&pps_idr, pps, &id);
187 spin_unlock_irq(&pps_idr_lock);
191 id = id & MAX_ID_MASK;
192 if (id >= PPS_MAX_SOURCES) {
193 spin_unlock_irq(&pps_idr_lock);
195 printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
202 spin_unlock_irq(&pps_idr_lock);
204 /* Create the char device */
205 err = pps_register_cdev(pps);
207 printk(KERN_ERR "pps: %s: unable to create char device\n",
212 pr_info("new PPS source %s at ID %d\n", info->name, id);
217 spin_lock_irq(&pps_idr_lock);
218 idr_remove(&pps_idr, id);
219 spin_unlock_irq(&pps_idr_lock);
224 pps_register_source_exit:
225 printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
229 EXPORT_SYMBOL(pps_register_source);
231 /* pps_unregister_source - remove a PPS source from the system
232 * @source: the PPS source ID
234 * This function is used to remove a previously registered PPS source from
238 void pps_unregister_source(int source)
240 struct pps_device *pps;
242 spin_lock_irq(&pps_idr_lock);
243 pps = idr_find(&pps_idr, source);
247 spin_unlock_irq(&pps_idr_lock);
250 spin_unlock_irq(&pps_idr_lock);
252 pps_unregister_cdev(pps);
255 EXPORT_SYMBOL(pps_unregister_source);
257 /* pps_event - register a PPS event into the system
258 * @source: the PPS source ID
259 * @ts: the event timestamp
260 * @event: the event type
261 * @data: userdef pointer
263 * This function is used by each PPS client in order to register a new
264 * PPS event into the system (it's usually called inside an IRQ handler).
266 * If an echo function is associated with the PPS source it will be called
268 * pps->info.echo(source, event, data);
271 void pps_event(int source, struct pps_ktime *ts, int event, void *data)
273 struct pps_device *pps;
277 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
278 printk(KERN_ERR "pps: unknown event (%x) for source %d\n",
283 pps = pps_get_source(source);
287 pr_debug("PPS event on source %d at %llu.%06u\n",
288 pps->id, (unsigned long long) ts->sec, ts->nsec);
290 spin_lock_irqsave(&pps->lock, flags);
292 /* Must call the echo function? */
293 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
294 pps->info.echo(source, event, data);
296 /* Check the event */
297 pps->current_mode = pps->params.mode;
298 if ((event & PPS_CAPTUREASSERT) &
299 (pps->params.mode & PPS_CAPTUREASSERT)) {
300 /* We have to add an offset? */
301 if (pps->params.mode & PPS_OFFSETASSERT)
302 pps_add_offset(ts, &pps->params.assert_off_tu);
304 /* Save the time stamp */
305 pps->assert_tu = *ts;
306 pps->assert_sequence++;
307 pr_debug("capture assert seq #%u for source %d\n",
308 pps->assert_sequence, source);
312 if ((event & PPS_CAPTURECLEAR) &
313 (pps->params.mode & PPS_CAPTURECLEAR)) {
314 /* We have to add an offset? */
315 if (pps->params.mode & PPS_OFFSETCLEAR)
316 pps_add_offset(ts, &pps->params.clear_off_tu);
318 /* Save the time stamp */
320 pps->clear_sequence++;
321 pr_debug("capture clear seq #%u for source %d\n",
322 pps->clear_sequence, source);
327 /* Wake up iif captured somthing */
330 wake_up_interruptible(&pps->queue);
332 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
335 spin_unlock_irqrestore(&pps->lock, flags);
337 /* Now we can release the PPS source for (possible) deregistration */
340 EXPORT_SYMBOL(pps_event);