pps: convert printk/pr_* to dev_*
[deliverable/linux.git] / drivers / pps / kapi.c
CommitLineData
eae9d2ba
RG
1/*
2 * kernel API
3 *
4 *
5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
6 *
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.
11 *
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.
16 *
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.
20 */
21
7f7cce74 22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
eae9d2ba
RG
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/spinlock.h>
30#include <linux/idr.h>
31#include <linux/fs.h>
32#include <linux/pps_kernel.h>
5a0e3ad6 33#include <linux/slab.h>
eae9d2ba
RG
34
35/*
5e196d34 36 * Local variables
eae9d2ba
RG
37 */
38
5e196d34
AG
39static DEFINE_SPINLOCK(pps_idr_lock);
40static DEFINE_IDR(pps_idr);
eae9d2ba
RG
41
42/*
43 * Local functions
44 */
45
46static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
47{
48 ts->nsec += offset->nsec;
49 while (ts->nsec >= NSEC_PER_SEC) {
50 ts->nsec -= NSEC_PER_SEC;
51 ts->sec++;
52 }
53 while (ts->nsec < 0) {
54 ts->nsec += NSEC_PER_SEC;
55 ts->sec--;
56 }
57 ts->sec += offset->sec;
58}
59
60/*
61 * Exported functions
62 */
63
eae9d2ba
RG
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
67 *
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.
71 *
5e196d34 72 * The function returns, in case of success, the PPS device. Otherwise NULL.
eae9d2ba
RG
73 */
74
5e196d34
AG
75struct pps_device *pps_register_source(struct pps_source_info *info,
76 int default_params)
eae9d2ba
RG
77{
78 struct pps_device *pps;
79 int id;
80 int err;
81
82 /* Sanity checks */
83 if ((info->mode & default_params) != default_params) {
7f7cce74 84 pr_err("%s: unsupported default parameters\n",
eae9d2ba
RG
85 info->name);
86 err = -EINVAL;
87 goto pps_register_source_exit;
88 }
89 if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
90 info->echo == NULL) {
7f7cce74 91 pr_err("%s: echo function is not defined\n",
eae9d2ba
RG
92 info->name);
93 err = -EINVAL;
94 goto pps_register_source_exit;
95 }
96 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
7f7cce74 97 pr_err("%s: unspecified time format\n",
eae9d2ba
RG
98 info->name);
99 err = -EINVAL;
100 goto pps_register_source_exit;
101 }
102
103 /* Allocate memory for the new PPS source struct */
104 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
105 if (pps == NULL) {
106 err = -ENOMEM;
107 goto pps_register_source_exit;
108 }
109
110 /* These initializations must be done before calling idr_get_new()
111 * in order to avoid reces into pps_event().
112 */
113 pps->params.api_version = PPS_API_VERS;
114 pps->params.mode = default_params;
115 pps->info = *info;
116
117 init_waitqueue_head(&pps->queue);
118 spin_lock_init(&pps->lock);
eae9d2ba
RG
119
120 /* Get new ID for the new PPS source */
121 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
122 err = -ENOMEM;
123 goto kfree_pps;
124 }
125
126 spin_lock_irq(&pps_idr_lock);
127
128 /* Now really allocate the PPS source.
129 * After idr_get_new() calling the new source will be freely available
130 * into the kernel.
131 */
132 err = idr_get_new(&pps_idr, pps, &id);
133 if (err < 0) {
134 spin_unlock_irq(&pps_idr_lock);
135 goto kfree_pps;
136 }
137
138 id = id & MAX_ID_MASK;
139 if (id >= PPS_MAX_SOURCES) {
140 spin_unlock_irq(&pps_idr_lock);
141
7f7cce74 142 pr_err("%s: too many PPS sources in the system\n",
eae9d2ba
RG
143 info->name);
144 err = -EBUSY;
145 goto free_idr;
146 }
147 pps->id = id;
148
149 spin_unlock_irq(&pps_idr_lock);
150
151 /* Create the char device */
152 err = pps_register_cdev(pps);
153 if (err < 0) {
7f7cce74 154 pr_err("%s: unable to create char device\n",
eae9d2ba
RG
155 info->name);
156 goto free_idr;
157 }
158
7f7cce74 159 dev_info(pps->dev, "new PPS source %s\n", info->name);
eae9d2ba 160
5e196d34 161 return pps;
eae9d2ba
RG
162
163free_idr:
164 spin_lock_irq(&pps_idr_lock);
165 idr_remove(&pps_idr, id);
166 spin_unlock_irq(&pps_idr_lock);
167
168kfree_pps:
169 kfree(pps);
170
171pps_register_source_exit:
7f7cce74 172 pr_err("%s: unable to register source\n", info->name);
eae9d2ba 173
5e196d34 174 return NULL;
eae9d2ba
RG
175}
176EXPORT_SYMBOL(pps_register_source);
177
178/* pps_unregister_source - remove a PPS source from the system
5e196d34 179 * @pps: the PPS source
eae9d2ba
RG
180 *
181 * This function is used to remove a previously registered PPS source from
182 * the system.
183 */
184
5e196d34 185void pps_unregister_source(struct pps_device *pps)
eae9d2ba 186{
5e196d34 187 unsigned int id = pps->id;
eae9d2ba 188
5e196d34 189 pps_unregister_cdev(pps);
eae9d2ba 190
5e196d34
AG
191 spin_lock_irq(&pps_idr_lock);
192 idr_remove(&pps_idr, pps->id);
eae9d2ba
RG
193 spin_unlock_irq(&pps_idr_lock);
194
5e196d34 195 kfree(pps);
eae9d2ba
RG
196}
197EXPORT_SYMBOL(pps_unregister_source);
198
199/* pps_event - register a PPS event into the system
5e196d34 200 * @pps: the PPS device
eae9d2ba
RG
201 * @ts: the event timestamp
202 * @event: the event type
203 * @data: userdef pointer
204 *
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).
207 *
5e196d34 208 * If an echo function is associated with the PPS device it will be called
eae9d2ba 209 * as:
5e196d34 210 * pps->info.echo(pps, event, data);
eae9d2ba 211 */
5e196d34
AG
212void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
213 void *data)
eae9d2ba 214{
eae9d2ba 215 unsigned long flags;
276b282e 216 int captured = 0;
6f4229b5 217 struct pps_ktime ts_real;
eae9d2ba
RG
218
219 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
5e196d34 220 dev_err(pps->dev, "unknown event (%x)\n", event);
eae9d2ba
RG
221 return;
222 }
223
5e196d34
AG
224 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
225 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
6f4229b5
AG
226
227 timespec_to_pps_ktime(&ts_real, ts->ts_real);
eae9d2ba
RG
228
229 spin_lock_irqsave(&pps->lock, flags);
230
231 /* Must call the echo function? */
232 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
5e196d34 233 pps->info.echo(pps, event, data);
eae9d2ba
RG
234
235 /* Check the event */
236 pps->current_mode = pps->params.mode;
276b282e
RG
237 if ((event & PPS_CAPTUREASSERT) &
238 (pps->params.mode & PPS_CAPTUREASSERT)) {
eae9d2ba
RG
239 /* We have to add an offset? */
240 if (pps->params.mode & PPS_OFFSETASSERT)
6f4229b5
AG
241 pps_add_offset(&ts_real,
242 &pps->params.assert_off_tu);
eae9d2ba
RG
243
244 /* Save the time stamp */
6f4229b5 245 pps->assert_tu = ts_real;
eae9d2ba 246 pps->assert_sequence++;
5e196d34
AG
247 dev_dbg(pps->dev, "capture assert seq #%u\n",
248 pps->assert_sequence);
276b282e
RG
249
250 captured = ~0;
eae9d2ba 251 }
276b282e
RG
252 if ((event & PPS_CAPTURECLEAR) &
253 (pps->params.mode & PPS_CAPTURECLEAR)) {
eae9d2ba
RG
254 /* We have to add an offset? */
255 if (pps->params.mode & PPS_OFFSETCLEAR)
6f4229b5
AG
256 pps_add_offset(&ts_real,
257 &pps->params.clear_off_tu);
eae9d2ba
RG
258
259 /* Save the time stamp */
6f4229b5 260 pps->clear_tu = ts_real;
eae9d2ba 261 pps->clear_sequence++;
5e196d34
AG
262 dev_dbg(pps->dev, "capture clear seq #%u\n",
263 pps->clear_sequence);
276b282e
RG
264
265 captured = ~0;
eae9d2ba
RG
266 }
267
7a21a3cc 268 /* Wake up if captured something */
276b282e 269 if (captured) {
3003d55b
AG
270 pps->last_ev++;
271 wake_up_interruptible_all(&pps->queue);
eae9d2ba 272
276b282e
RG
273 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
274 }
eae9d2ba
RG
275
276 spin_unlock_irqrestore(&pps->lock, flags);
eae9d2ba
RG
277}
278EXPORT_SYMBOL(pps_event);
This page took 0.150413 seconds and 5 git commands to generate.