pps: convert printk/pr_* to dev_*
[deliverable/linux.git] / drivers / pps / kapi.c
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
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
33 #include <linux/slab.h>
34
35 /*
36 * Local variables
37 */
38
39 static DEFINE_SPINLOCK(pps_idr_lock);
40 static DEFINE_IDR(pps_idr);
41
42 /*
43 * Local functions
44 */
45
46 static 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
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 *
72 * The function returns, in case of success, the PPS device. Otherwise NULL.
73 */
74
75 struct pps_device *pps_register_source(struct pps_source_info *info,
76 int default_params)
77 {
78 struct pps_device *pps;
79 int id;
80 int err;
81
82 /* Sanity checks */
83 if ((info->mode & default_params) != default_params) {
84 pr_err("%s: unsupported default parameters\n",
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) {
91 pr_err("%s: echo function is not defined\n",
92 info->name);
93 err = -EINVAL;
94 goto pps_register_source_exit;
95 }
96 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
97 pr_err("%s: unspecified time format\n",
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);
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
142 pr_err("%s: too many PPS sources in the system\n",
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) {
154 pr_err("%s: unable to create char device\n",
155 info->name);
156 goto free_idr;
157 }
158
159 dev_info(pps->dev, "new PPS source %s\n", info->name);
160
161 return pps;
162
163 free_idr:
164 spin_lock_irq(&pps_idr_lock);
165 idr_remove(&pps_idr, id);
166 spin_unlock_irq(&pps_idr_lock);
167
168 kfree_pps:
169 kfree(pps);
170
171 pps_register_source_exit:
172 pr_err("%s: unable to register source\n", info->name);
173
174 return NULL;
175 }
176 EXPORT_SYMBOL(pps_register_source);
177
178 /* pps_unregister_source - remove a PPS source from the system
179 * @pps: the PPS source
180 *
181 * This function is used to remove a previously registered PPS source from
182 * the system.
183 */
184
185 void pps_unregister_source(struct pps_device *pps)
186 {
187 unsigned int id = pps->id;
188
189 pps_unregister_cdev(pps);
190
191 spin_lock_irq(&pps_idr_lock);
192 idr_remove(&pps_idr, pps->id);
193 spin_unlock_irq(&pps_idr_lock);
194
195 kfree(pps);
196 }
197 EXPORT_SYMBOL(pps_unregister_source);
198
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
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 *
208 * If an echo function is associated with the PPS device it will be called
209 * as:
210 * pps->info.echo(pps, event, data);
211 */
212 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
213 void *data)
214 {
215 unsigned long flags;
216 int captured = 0;
217 struct pps_ktime ts_real;
218
219 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
220 dev_err(pps->dev, "unknown event (%x)\n", event);
221 return;
222 }
223
224 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
225 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
226
227 timespec_to_pps_ktime(&ts_real, ts->ts_real);
228
229 spin_lock_irqsave(&pps->lock, flags);
230
231 /* Must call the echo function? */
232 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
233 pps->info.echo(pps, event, data);
234
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);
243
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);
249
250 captured = ~0;
251 }
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);
258
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);
264
265 captured = ~0;
266 }
267
268 /* Wake up if captured something */
269 if (captured) {
270 pps->last_ev++;
271 wake_up_interruptible_all(&pps->queue);
272
273 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
274 }
275
276 spin_unlock_irqrestore(&pps->lock, flags);
277 }
278 EXPORT_SYMBOL(pps_event);
This page took 0.040275 seconds and 6 git commands to generate.