pps: access pps device by direct pointer
[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
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>
30 #include <linux/fs.h>
31 #include <linux/pps_kernel.h>
32 #include <linux/slab.h>
33
34 /*
35 * Local variables
36 */
37
38 static DEFINE_SPINLOCK(pps_idr_lock);
39 static DEFINE_IDR(pps_idr);
40
41 /*
42 * Local functions
43 */
44
45 static void pps_add_offset(struct pps_ktime *ts, struct pps_ktime *offset)
46 {
47 ts->nsec += offset->nsec;
48 while (ts->nsec >= NSEC_PER_SEC) {
49 ts->nsec -= NSEC_PER_SEC;
50 ts->sec++;
51 }
52 while (ts->nsec < 0) {
53 ts->nsec += NSEC_PER_SEC;
54 ts->sec--;
55 }
56 ts->sec += offset->sec;
57 }
58
59 /*
60 * Exported functions
61 */
62
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
66 *
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.
70 *
71 * The function returns, in case of success, the PPS device. Otherwise NULL.
72 */
73
74 struct pps_device *pps_register_source(struct pps_source_info *info,
75 int default_params)
76 {
77 struct pps_device *pps;
78 int id;
79 int err;
80
81 /* Sanity checks */
82 if ((info->mode & default_params) != default_params) {
83 printk(KERN_ERR "pps: %s: unsupported default parameters\n",
84 info->name);
85 err = -EINVAL;
86 goto pps_register_source_exit;
87 }
88 if ((info->mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)) != 0 &&
89 info->echo == NULL) {
90 printk(KERN_ERR "pps: %s: echo function is not defined\n",
91 info->name);
92 err = -EINVAL;
93 goto pps_register_source_exit;
94 }
95 if ((info->mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
96 printk(KERN_ERR "pps: %s: unspecified time format\n",
97 info->name);
98 err = -EINVAL;
99 goto pps_register_source_exit;
100 }
101
102 /* Allocate memory for the new PPS source struct */
103 pps = kzalloc(sizeof(struct pps_device), GFP_KERNEL);
104 if (pps == NULL) {
105 err = -ENOMEM;
106 goto pps_register_source_exit;
107 }
108
109 /* These initializations must be done before calling idr_get_new()
110 * in order to avoid reces into pps_event().
111 */
112 pps->params.api_version = PPS_API_VERS;
113 pps->params.mode = default_params;
114 pps->info = *info;
115
116 init_waitqueue_head(&pps->queue);
117 spin_lock_init(&pps->lock);
118
119 /* Get new ID for the new PPS source */
120 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
121 err = -ENOMEM;
122 goto kfree_pps;
123 }
124
125 spin_lock_irq(&pps_idr_lock);
126
127 /* Now really allocate the PPS source.
128 * After idr_get_new() calling the new source will be freely available
129 * into the kernel.
130 */
131 err = idr_get_new(&pps_idr, pps, &id);
132 if (err < 0) {
133 spin_unlock_irq(&pps_idr_lock);
134 goto kfree_pps;
135 }
136
137 id = id & MAX_ID_MASK;
138 if (id >= PPS_MAX_SOURCES) {
139 spin_unlock_irq(&pps_idr_lock);
140
141 printk(KERN_ERR "pps: %s: too many PPS sources in the system\n",
142 info->name);
143 err = -EBUSY;
144 goto free_idr;
145 }
146 pps->id = id;
147
148 spin_unlock_irq(&pps_idr_lock);
149
150 /* Create the char device */
151 err = pps_register_cdev(pps);
152 if (err < 0) {
153 printk(KERN_ERR "pps: %s: unable to create char device\n",
154 info->name);
155 goto free_idr;
156 }
157
158 pr_info("new PPS source %s at ID %d\n", info->name, id);
159
160 return pps;
161
162 free_idr:
163 spin_lock_irq(&pps_idr_lock);
164 idr_remove(&pps_idr, id);
165 spin_unlock_irq(&pps_idr_lock);
166
167 kfree_pps:
168 kfree(pps);
169
170 pps_register_source_exit:
171 printk(KERN_ERR "pps: %s: unable to register source\n", info->name);
172
173 return NULL;
174 }
175 EXPORT_SYMBOL(pps_register_source);
176
177 /* pps_unregister_source - remove a PPS source from the system
178 * @pps: the PPS source
179 *
180 * This function is used to remove a previously registered PPS source from
181 * the system.
182 */
183
184 void pps_unregister_source(struct pps_device *pps)
185 {
186 unsigned int id = pps->id;
187
188 pps_unregister_cdev(pps);
189
190 spin_lock_irq(&pps_idr_lock);
191 idr_remove(&pps_idr, pps->id);
192 spin_unlock_irq(&pps_idr_lock);
193
194 kfree(pps);
195 }
196 EXPORT_SYMBOL(pps_unregister_source);
197
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
203 *
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).
206 *
207 * If an echo function is associated with the PPS device it will be called
208 * as:
209 * pps->info.echo(pps, event, data);
210 */
211 void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event,
212 void *data)
213 {
214 unsigned long flags;
215 int captured = 0;
216 struct pps_ktime ts_real;
217
218 if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) {
219 dev_err(pps->dev, "unknown event (%x)\n", event);
220 return;
221 }
222
223 dev_dbg(pps->dev, "PPS event at %ld.%09ld\n",
224 ts->ts_real.tv_sec, ts->ts_real.tv_nsec);
225
226 timespec_to_pps_ktime(&ts_real, ts->ts_real);
227
228 spin_lock_irqsave(&pps->lock, flags);
229
230 /* Must call the echo function? */
231 if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR)))
232 pps->info.echo(pps, event, data);
233
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);
242
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);
248
249 captured = ~0;
250 }
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);
257
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);
263
264 captured = ~0;
265 }
266
267 /* Wake up if captured something */
268 if (captured) {
269 pps->last_ev++;
270 wake_up_interruptible_all(&pps->queue);
271
272 kill_fasync(&pps->async_queue, SIGIO, POLL_IN);
273 }
274
275 spin_unlock_irqrestore(&pps->lock, flags);
276 }
277 EXPORT_SYMBOL(pps_event);
This page took 0.036445 seconds and 6 git commands to generate.