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.
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/spinlock.h>
30 #include <linux/idr.h>
32 #include <linux/pps_kernel.h>
33 #include <linux/slab.h>
39 static DEFINE_SPINLOCK(pps_idr_lock
);
40 static DEFINE_IDR(pps_idr
);
46 static void pps_add_offset(struct pps_ktime
*ts
, struct pps_ktime
*offset
)
48 ts
->nsec
+= offset
->nsec
;
49 while (ts
->nsec
>= NSEC_PER_SEC
) {
50 ts
->nsec
-= NSEC_PER_SEC
;
53 while (ts
->nsec
< 0) {
54 ts
->nsec
+= NSEC_PER_SEC
;
57 ts
->sec
+= offset
->sec
;
64 /* pps_register_source - add a PPS source in the system
65 * @info: the PPS info struct
66 * @default_params: the default PPS parameters of the new source
68 * This function is used to add a new PPS source in the system. The new
69 * source is described by info's fields and it will have, as default PPS
70 * parameters, the ones specified into default_params.
72 * The function returns, in case of success, the PPS device. Otherwise NULL.
75 struct pps_device
*pps_register_source(struct pps_source_info
*info
,
78 struct pps_device
*pps
;
83 if ((info
->mode
& default_params
) != default_params
) {
84 pr_err("%s: unsupported default parameters\n",
87 goto pps_register_source_exit
;
89 if ((info
->mode
& (PPS_ECHOASSERT
| PPS_ECHOCLEAR
)) != 0 &&
91 pr_err("%s: echo function is not defined\n",
94 goto pps_register_source_exit
;
96 if ((info
->mode
& (PPS_TSFMT_TSPEC
| PPS_TSFMT_NTPFP
)) == 0) {
97 pr_err("%s: unspecified time format\n",
100 goto pps_register_source_exit
;
103 /* Allocate memory for the new PPS source struct */
104 pps
= kzalloc(sizeof(struct pps_device
), GFP_KERNEL
);
107 goto pps_register_source_exit
;
110 /* These initializations must be done before calling idr_get_new()
111 * in order to avoid reces into pps_event().
113 pps
->params
.api_version
= PPS_API_VERS
;
114 pps
->params
.mode
= default_params
;
117 init_waitqueue_head(&pps
->queue
);
118 spin_lock_init(&pps
->lock
);
120 /* Get new ID for the new PPS source */
121 if (idr_pre_get(&pps_idr
, GFP_KERNEL
) == 0) {
126 spin_lock_irq(&pps_idr_lock
);
128 /* Now really allocate the PPS source.
129 * After idr_get_new() calling the new source will be freely available
132 err
= idr_get_new(&pps_idr
, pps
, &id
);
134 spin_unlock_irq(&pps_idr_lock
);
138 id
= id
& MAX_ID_MASK
;
139 if (id
>= PPS_MAX_SOURCES
) {
140 spin_unlock_irq(&pps_idr_lock
);
142 pr_err("%s: too many PPS sources in the system\n",
149 spin_unlock_irq(&pps_idr_lock
);
151 /* Create the char device */
152 err
= pps_register_cdev(pps
);
154 pr_err("%s: unable to create char device\n",
159 dev_info(pps
->dev
, "new PPS source %s\n", info
->name
);
164 spin_lock_irq(&pps_idr_lock
);
165 idr_remove(&pps_idr
, id
);
166 spin_unlock_irq(&pps_idr_lock
);
171 pps_register_source_exit
:
172 pr_err("%s: unable to register source\n", info
->name
);
176 EXPORT_SYMBOL(pps_register_source
);
178 /* pps_unregister_source - remove a PPS source from the system
179 * @pps: the PPS source
181 * This function is used to remove a previously registered PPS source from
185 void pps_unregister_source(struct pps_device
*pps
)
187 unsigned int id
= pps
->id
;
189 pps_unregister_cdev(pps
);
191 spin_lock_irq(&pps_idr_lock
);
192 idr_remove(&pps_idr
, pps
->id
);
193 spin_unlock_irq(&pps_idr_lock
);
197 EXPORT_SYMBOL(pps_unregister_source
);
199 /* pps_event - register a PPS event into the system
200 * @pps: the PPS device
201 * @ts: the event timestamp
202 * @event: the event type
203 * @data: userdef pointer
205 * This function is used by each PPS client in order to register a new
206 * PPS event into the system (it's usually called inside an IRQ handler).
208 * If an echo function is associated with the PPS device it will be called
210 * pps->info.echo(pps, event, data);
212 void pps_event(struct pps_device
*pps
, struct pps_event_time
*ts
, int event
,
217 struct pps_ktime ts_real
;
219 if ((event
& (PPS_CAPTUREASSERT
| PPS_CAPTURECLEAR
)) == 0) {
220 dev_err(pps
->dev
, "unknown event (%x)\n", event
);
224 dev_dbg(pps
->dev
, "PPS event at %ld.%09ld\n",
225 ts
->ts_real
.tv_sec
, ts
->ts_real
.tv_nsec
);
227 timespec_to_pps_ktime(&ts_real
, ts
->ts_real
);
229 spin_lock_irqsave(&pps
->lock
, flags
);
231 /* Must call the echo function? */
232 if ((pps
->params
.mode
& (PPS_ECHOASSERT
| PPS_ECHOCLEAR
)))
233 pps
->info
.echo(pps
, event
, data
);
235 /* Check the event */
236 pps
->current_mode
= pps
->params
.mode
;
237 if ((event
& PPS_CAPTUREASSERT
) &
238 (pps
->params
.mode
& PPS_CAPTUREASSERT
)) {
239 /* We have to add an offset? */
240 if (pps
->params
.mode
& PPS_OFFSETASSERT
)
241 pps_add_offset(&ts_real
,
242 &pps
->params
.assert_off_tu
);
244 /* Save the time stamp */
245 pps
->assert_tu
= ts_real
;
246 pps
->assert_sequence
++;
247 dev_dbg(pps
->dev
, "capture assert seq #%u\n",
248 pps
->assert_sequence
);
252 if ((event
& PPS_CAPTURECLEAR
) &
253 (pps
->params
.mode
& PPS_CAPTURECLEAR
)) {
254 /* We have to add an offset? */
255 if (pps
->params
.mode
& PPS_OFFSETCLEAR
)
256 pps_add_offset(&ts_real
,
257 &pps
->params
.clear_off_tu
);
259 /* Save the time stamp */
260 pps
->clear_tu
= ts_real
;
261 pps
->clear_sequence
++;
262 dev_dbg(pps
->dev
, "capture clear seq #%u\n",
263 pps
->clear_sequence
);
268 /* Wake up if captured something */
271 wake_up_interruptible_all(&pps
->queue
);
273 kill_fasync(&pps
->async_queue
, SIGIO
, POLL_IN
);
276 spin_unlock_irqrestore(&pps
->lock
, flags
);
278 EXPORT_SYMBOL(pps_event
);
This page took 0.040275 seconds and 6 git commands to generate.