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 static DEFINE_SPINLOCK(pps_idr_lock
);
39 static DEFINE_IDR(pps_idr
);
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_register_source - add a PPS source in the system
64 * @info: the PPS info struct
65 * @default_params: the default PPS parameters of the new source
67 * This function is used to add a new PPS source in the system. The new
68 * source is described by info's fields and it will have, as default PPS
69 * parameters, the ones specified into default_params.
71 * The function returns, in case of success, the PPS device. Otherwise NULL.
74 struct pps_device
*pps_register_source(struct pps_source_info
*info
,
77 struct pps_device
*pps
;
82 if ((info
->mode
& default_params
) != default_params
) {
83 printk(KERN_ERR
"pps: %s: unsupported default parameters\n",
86 goto pps_register_source_exit
;
88 if ((info
->mode
& (PPS_ECHOASSERT
| PPS_ECHOCLEAR
)) != 0 &&
90 printk(KERN_ERR
"pps: %s: echo function is not defined\n",
93 goto pps_register_source_exit
;
95 if ((info
->mode
& (PPS_TSFMT_TSPEC
| PPS_TSFMT_NTPFP
)) == 0) {
96 printk(KERN_ERR
"pps: %s: unspecified time format\n",
99 goto pps_register_source_exit
;
102 /* Allocate memory for the new PPS source struct */
103 pps
= kzalloc(sizeof(struct pps_device
), GFP_KERNEL
);
106 goto pps_register_source_exit
;
109 /* These initializations must be done before calling idr_get_new()
110 * in order to avoid reces into pps_event().
112 pps
->params
.api_version
= PPS_API_VERS
;
113 pps
->params
.mode
= default_params
;
116 init_waitqueue_head(&pps
->queue
);
117 spin_lock_init(&pps
->lock
);
119 /* Get new ID for the new PPS source */
120 if (idr_pre_get(&pps_idr
, GFP_KERNEL
) == 0) {
125 spin_lock_irq(&pps_idr_lock
);
127 /* Now really allocate the PPS source.
128 * After idr_get_new() calling the new source will be freely available
131 err
= idr_get_new(&pps_idr
, pps
, &id
);
133 spin_unlock_irq(&pps_idr_lock
);
137 id
= id
& MAX_ID_MASK
;
138 if (id
>= PPS_MAX_SOURCES
) {
139 spin_unlock_irq(&pps_idr_lock
);
141 printk(KERN_ERR
"pps: %s: too many PPS sources in the system\n",
148 spin_unlock_irq(&pps_idr_lock
);
150 /* Create the char device */
151 err
= pps_register_cdev(pps
);
153 printk(KERN_ERR
"pps: %s: unable to create char device\n",
158 pr_info("new PPS source %s at ID %d\n", info
->name
, id
);
163 spin_lock_irq(&pps_idr_lock
);
164 idr_remove(&pps_idr
, id
);
165 spin_unlock_irq(&pps_idr_lock
);
170 pps_register_source_exit
:
171 printk(KERN_ERR
"pps: %s: unable to register source\n", info
->name
);
175 EXPORT_SYMBOL(pps_register_source
);
177 /* pps_unregister_source - remove a PPS source from the system
178 * @pps: the PPS source
180 * This function is used to remove a previously registered PPS source from
184 void pps_unregister_source(struct pps_device
*pps
)
186 unsigned int id
= pps
->id
;
188 pps_unregister_cdev(pps
);
190 spin_lock_irq(&pps_idr_lock
);
191 idr_remove(&pps_idr
, pps
->id
);
192 spin_unlock_irq(&pps_idr_lock
);
196 EXPORT_SYMBOL(pps_unregister_source
);
198 /* pps_event - register a PPS event into the system
199 * @pps: the PPS device
200 * @ts: the event timestamp
201 * @event: the event type
202 * @data: userdef pointer
204 * This function is used by each PPS client in order to register a new
205 * PPS event into the system (it's usually called inside an IRQ handler).
207 * If an echo function is associated with the PPS device it will be called
209 * pps->info.echo(pps, event, data);
211 void pps_event(struct pps_device
*pps
, struct pps_event_time
*ts
, int event
,
216 struct pps_ktime ts_real
;
218 if ((event
& (PPS_CAPTUREASSERT
| PPS_CAPTURECLEAR
)) == 0) {
219 dev_err(pps
->dev
, "unknown event (%x)\n", event
);
223 dev_dbg(pps
->dev
, "PPS event at %ld.%09ld\n",
224 ts
->ts_real
.tv_sec
, ts
->ts_real
.tv_nsec
);
226 timespec_to_pps_ktime(&ts_real
, ts
->ts_real
);
228 spin_lock_irqsave(&pps
->lock
, flags
);
230 /* Must call the echo function? */
231 if ((pps
->params
.mode
& (PPS_ECHOASSERT
| PPS_ECHOCLEAR
)))
232 pps
->info
.echo(pps
, event
, data
);
234 /* Check the event */
235 pps
->current_mode
= pps
->params
.mode
;
236 if ((event
& PPS_CAPTUREASSERT
) &
237 (pps
->params
.mode
& PPS_CAPTUREASSERT
)) {
238 /* We have to add an offset? */
239 if (pps
->params
.mode
& PPS_OFFSETASSERT
)
240 pps_add_offset(&ts_real
,
241 &pps
->params
.assert_off_tu
);
243 /* Save the time stamp */
244 pps
->assert_tu
= ts_real
;
245 pps
->assert_sequence
++;
246 dev_dbg(pps
->dev
, "capture assert seq #%u\n",
247 pps
->assert_sequence
);
251 if ((event
& PPS_CAPTURECLEAR
) &
252 (pps
->params
.mode
& PPS_CAPTURECLEAR
)) {
253 /* We have to add an offset? */
254 if (pps
->params
.mode
& PPS_OFFSETCLEAR
)
255 pps_add_offset(&ts_real
,
256 &pps
->params
.clear_off_tu
);
258 /* Save the time stamp */
259 pps
->clear_tu
= ts_real
;
260 pps
->clear_sequence
++;
261 dev_dbg(pps
->dev
, "capture clear seq #%u\n",
262 pps
->clear_sequence
);
267 /* Wake up if captured something */
270 wake_up_interruptible_all(&pps
->queue
);
272 kill_fasync(&pps
->async_queue
, SIGIO
, POLL_IN
);
275 spin_unlock_irqrestore(&pps
->lock
, flags
);
277 EXPORT_SYMBOL(pps_event
);
This page took 0.036445 seconds and 6 git commands to generate.