Commit | Line | Data |
---|---|---|
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 | ||
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> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
eae9d2ba RG |
33 | |
34 | /* | |
5e196d34 | 35 | * Local variables |
eae9d2ba RG |
36 | */ |
37 | ||
5e196d34 AG |
38 | static DEFINE_SPINLOCK(pps_idr_lock); |
39 | static DEFINE_IDR(pps_idr); | |
eae9d2ba RG |
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 | ||
eae9d2ba RG |
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 | * | |
5e196d34 | 71 | * The function returns, in case of success, the PPS device. Otherwise NULL. |
eae9d2ba RG |
72 | */ |
73 | ||
5e196d34 AG |
74 | struct pps_device *pps_register_source(struct pps_source_info *info, |
75 | int default_params) | |
eae9d2ba RG |
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); | |
eae9d2ba RG |
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 | ||
5e196d34 | 160 | return pps; |
eae9d2ba RG |
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 | ||
5e196d34 | 173 | return NULL; |
eae9d2ba RG |
174 | } |
175 | EXPORT_SYMBOL(pps_register_source); | |
176 | ||
177 | /* pps_unregister_source - remove a PPS source from the system | |
5e196d34 | 178 | * @pps: the PPS source |
eae9d2ba RG |
179 | * |
180 | * This function is used to remove a previously registered PPS source from | |
181 | * the system. | |
182 | */ | |
183 | ||
5e196d34 | 184 | void pps_unregister_source(struct pps_device *pps) |
eae9d2ba | 185 | { |
5e196d34 | 186 | unsigned int id = pps->id; |
eae9d2ba | 187 | |
5e196d34 | 188 | pps_unregister_cdev(pps); |
eae9d2ba | 189 | |
5e196d34 AG |
190 | spin_lock_irq(&pps_idr_lock); |
191 | idr_remove(&pps_idr, pps->id); | |
eae9d2ba RG |
192 | spin_unlock_irq(&pps_idr_lock); |
193 | ||
5e196d34 | 194 | kfree(pps); |
eae9d2ba RG |
195 | } |
196 | EXPORT_SYMBOL(pps_unregister_source); | |
197 | ||
198 | /* pps_event - register a PPS event into the system | |
5e196d34 | 199 | * @pps: the PPS device |
eae9d2ba RG |
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 | * | |
5e196d34 | 207 | * If an echo function is associated with the PPS device it will be called |
eae9d2ba | 208 | * as: |
5e196d34 | 209 | * pps->info.echo(pps, event, data); |
eae9d2ba | 210 | */ |
5e196d34 AG |
211 | void pps_event(struct pps_device *pps, struct pps_event_time *ts, int event, |
212 | void *data) | |
eae9d2ba | 213 | { |
eae9d2ba | 214 | unsigned long flags; |
276b282e | 215 | int captured = 0; |
6f4229b5 | 216 | struct pps_ktime ts_real; |
eae9d2ba RG |
217 | |
218 | if ((event & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR)) == 0) { | |
5e196d34 | 219 | dev_err(pps->dev, "unknown event (%x)\n", event); |
eae9d2ba RG |
220 | return; |
221 | } | |
222 | ||
5e196d34 AG |
223 | dev_dbg(pps->dev, "PPS event at %ld.%09ld\n", |
224 | ts->ts_real.tv_sec, ts->ts_real.tv_nsec); | |
6f4229b5 AG |
225 | |
226 | timespec_to_pps_ktime(&ts_real, ts->ts_real); | |
eae9d2ba RG |
227 | |
228 | spin_lock_irqsave(&pps->lock, flags); | |
229 | ||
230 | /* Must call the echo function? */ | |
231 | if ((pps->params.mode & (PPS_ECHOASSERT | PPS_ECHOCLEAR))) | |
5e196d34 | 232 | pps->info.echo(pps, event, data); |
eae9d2ba RG |
233 | |
234 | /* Check the event */ | |
235 | pps->current_mode = pps->params.mode; | |
276b282e RG |
236 | if ((event & PPS_CAPTUREASSERT) & |
237 | (pps->params.mode & PPS_CAPTUREASSERT)) { | |
eae9d2ba RG |
238 | /* We have to add an offset? */ |
239 | if (pps->params.mode & PPS_OFFSETASSERT) | |
6f4229b5 AG |
240 | pps_add_offset(&ts_real, |
241 | &pps->params.assert_off_tu); | |
eae9d2ba RG |
242 | |
243 | /* Save the time stamp */ | |
6f4229b5 | 244 | pps->assert_tu = ts_real; |
eae9d2ba | 245 | pps->assert_sequence++; |
5e196d34 AG |
246 | dev_dbg(pps->dev, "capture assert seq #%u\n", |
247 | pps->assert_sequence); | |
276b282e RG |
248 | |
249 | captured = ~0; | |
eae9d2ba | 250 | } |
276b282e RG |
251 | if ((event & PPS_CAPTURECLEAR) & |
252 | (pps->params.mode & PPS_CAPTURECLEAR)) { | |
eae9d2ba RG |
253 | /* We have to add an offset? */ |
254 | if (pps->params.mode & PPS_OFFSETCLEAR) | |
6f4229b5 AG |
255 | pps_add_offset(&ts_real, |
256 | &pps->params.clear_off_tu); | |
eae9d2ba RG |
257 | |
258 | /* Save the time stamp */ | |
6f4229b5 | 259 | pps->clear_tu = ts_real; |
eae9d2ba | 260 | pps->clear_sequence++; |
5e196d34 AG |
261 | dev_dbg(pps->dev, "capture clear seq #%u\n", |
262 | pps->clear_sequence); | |
276b282e RG |
263 | |
264 | captured = ~0; | |
eae9d2ba RG |
265 | } |
266 | ||
7a21a3cc | 267 | /* Wake up if captured something */ |
276b282e | 268 | if (captured) { |
3003d55b AG |
269 | pps->last_ev++; |
270 | wake_up_interruptible_all(&pps->queue); | |
eae9d2ba | 271 | |
276b282e RG |
272 | kill_fasync(&pps->async_queue, SIGIO, POLL_IN); |
273 | } | |
eae9d2ba RG |
274 | |
275 | spin_unlock_irqrestore(&pps->lock, flags); | |
eae9d2ba RG |
276 | } |
277 | EXPORT_SYMBOL(pps_event); |