1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PPS core file
4 *
5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/uaccess.h>
15 #include <linux/idr.h>
16 #include <linux/mutex.h>
17 #include <linux/cdev.h>
18 #include <linux/poll.h>
19 #include <linux/pps_kernel.h>
20 #include <linux/slab.h>
21
22 #include "kc.h"
23
24 /*
25 * Local variables
26 */
27
28 static int pps_major;
29 static struct class *pps_class;
30
31 static DEFINE_MUTEX(pps_idr_lock);
32 static DEFINE_IDR(pps_idr);
33
34 /*
35 * Char device methods
36 */
37
pps_cdev_poll(struct file * file,poll_table * wait)38 static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
39 {
40 struct pps_device *pps = file->private_data;
41
42 poll_wait(file, &pps->queue, wait);
43
44 if (pps->last_fetched_ev == pps->last_ev)
45 return 0;
46
47 return EPOLLIN | EPOLLRDNORM;
48 }
49
pps_cdev_fasync(int fd,struct file * file,int on)50 static int pps_cdev_fasync(int fd, struct file *file, int on)
51 {
52 struct pps_device *pps = file->private_data;
53 return fasync_helper(fd, file, on, &pps->async_queue);
54 }
55
pps_cdev_pps_fetch(struct pps_device * pps,struct pps_fdata * fdata)56 static int pps_cdev_pps_fetch(struct pps_device *pps, struct pps_fdata *fdata)
57 {
58 unsigned int ev = pps->last_ev;
59 int err = 0;
60
61 /* Manage the timeout */
62 if (fdata->timeout.flags & PPS_TIME_INVALID)
63 err = wait_event_interruptible(pps->queue,
64 ev != pps->last_ev);
65 else {
66 unsigned long ticks;
67
68 dev_dbg(&pps->dev, "timeout %lld.%09d\n",
69 (long long) fdata->timeout.sec,
70 fdata->timeout.nsec);
71 ticks = fdata->timeout.sec * HZ;
72 ticks += fdata->timeout.nsec / (NSEC_PER_SEC / HZ);
73
74 if (ticks != 0) {
75 err = wait_event_interruptible_timeout(
76 pps->queue,
77 ev != pps->last_ev,
78 ticks);
79 if (err == 0)
80 return -ETIMEDOUT;
81 }
82 }
83
84 /* Check for pending signals */
85 if (err == -ERESTARTSYS) {
86 dev_dbg(&pps->dev, "pending signal caught\n");
87 return -EINTR;
88 }
89
90 return 0;
91 }
92
pps_cdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)93 static long pps_cdev_ioctl(struct file *file,
94 unsigned int cmd, unsigned long arg)
95 {
96 struct pps_device *pps = file->private_data;
97 struct pps_kparams params;
98 void __user *uarg = (void __user *) arg;
99 int __user *iuarg = (int __user *) arg;
100 int err;
101
102 switch (cmd) {
103 case PPS_GETPARAMS:
104 dev_dbg(&pps->dev, "PPS_GETPARAMS\n");
105
106 spin_lock_irq(&pps->lock);
107
108 /* Get the current parameters */
109 params = pps->params;
110
111 spin_unlock_irq(&pps->lock);
112
113 err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams));
114 if (err)
115 return -EFAULT;
116
117 break;
118
119 case PPS_SETPARAMS:
120 dev_dbg(&pps->dev, "PPS_SETPARAMS\n");
121
122 /* Check the capabilities */
123 if (!capable(CAP_SYS_TIME))
124 return -EPERM;
125
126 err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams));
127 if (err)
128 return -EFAULT;
129 if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
130 dev_dbg(&pps->dev, "capture mode unspecified (%x)\n",
131 params.mode);
132 return -EINVAL;
133 }
134
135 /* Check for supported capabilities */
136 if ((params.mode & ~pps->info.mode) != 0) {
137 dev_dbg(&pps->dev, "unsupported capabilities (%x)\n",
138 params.mode);
139 return -EINVAL;
140 }
141
142 spin_lock_irq(&pps->lock);
143
144 /* Save the new parameters */
145 pps->params = params;
146
147 /* Restore the read only parameters */
148 if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
149 /* section 3.3 of RFC 2783 interpreted */
150 dev_dbg(&pps->dev, "time format unspecified (%x)\n",
151 params.mode);
152 pps->params.mode |= PPS_TSFMT_TSPEC;
153 }
154 if (pps->info.mode & PPS_CANWAIT)
155 pps->params.mode |= PPS_CANWAIT;
156 pps->params.api_version = PPS_API_VERS;
157
158 /*
159 * Clear unused fields of pps_kparams to avoid leaking
160 * uninitialized data of the PPS_SETPARAMS caller via
161 * PPS_GETPARAMS
162 */
163 pps->params.assert_off_tu.flags = 0;
164 pps->params.clear_off_tu.flags = 0;
165
166 spin_unlock_irq(&pps->lock);
167
168 break;
169
170 case PPS_GETCAP:
171 dev_dbg(&pps->dev, "PPS_GETCAP\n");
172
173 err = put_user(pps->info.mode, iuarg);
174 if (err)
175 return -EFAULT;
176
177 break;
178
179 case PPS_FETCH: {
180 struct pps_fdata fdata;
181
182 dev_dbg(&pps->dev, "PPS_FETCH\n");
183
184 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
185 if (err)
186 return -EFAULT;
187
188 err = pps_cdev_pps_fetch(pps, &fdata);
189 if (err)
190 return err;
191
192 /* Return the fetched timestamp and save last fetched event */
193 spin_lock_irq(&pps->lock);
194
195 pps->last_fetched_ev = pps->last_ev;
196
197 fdata.info.assert_sequence = pps->assert_sequence;
198 fdata.info.clear_sequence = pps->clear_sequence;
199 fdata.info.assert_tu = pps->assert_tu;
200 fdata.info.clear_tu = pps->clear_tu;
201 fdata.info.current_mode = pps->current_mode;
202
203 spin_unlock_irq(&pps->lock);
204
205 err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
206 if (err)
207 return -EFAULT;
208
209 break;
210 }
211 case PPS_KC_BIND: {
212 struct pps_bind_args bind_args;
213
214 dev_dbg(&pps->dev, "PPS_KC_BIND\n");
215
216 /* Check the capabilities */
217 if (!capable(CAP_SYS_TIME))
218 return -EPERM;
219
220 if (copy_from_user(&bind_args, uarg,
221 sizeof(struct pps_bind_args)))
222 return -EFAULT;
223
224 /* Check for supported capabilities */
225 if ((bind_args.edge & ~pps->info.mode) != 0) {
226 dev_err(&pps->dev, "unsupported capabilities (%x)\n",
227 bind_args.edge);
228 return -EINVAL;
229 }
230
231 /* Validate parameters roughly */
232 if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
233 (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
234 bind_args.consumer != PPS_KC_HARDPPS) {
235 dev_err(&pps->dev, "invalid kernel consumer bind"
236 " parameters (%x)\n", bind_args.edge);
237 return -EINVAL;
238 }
239
240 err = pps_kc_bind(pps, &bind_args);
241 if (err < 0)
242 return err;
243
244 break;
245 }
246 default:
247 return -ENOTTY;
248 }
249
250 return 0;
251 }
252
253 #ifdef CONFIG_COMPAT
pps_cdev_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)254 static long pps_cdev_compat_ioctl(struct file *file,
255 unsigned int cmd, unsigned long arg)
256 {
257 struct pps_device *pps = file->private_data;
258 void __user *uarg = (void __user *) arg;
259
260 cmd = _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(void *));
261
262 if (cmd == PPS_FETCH) {
263 struct pps_fdata_compat compat;
264 struct pps_fdata fdata;
265 int err;
266
267 dev_dbg(&pps->dev, "PPS_FETCH\n");
268
269 err = copy_from_user(&compat, uarg, sizeof(struct pps_fdata_compat));
270 if (err)
271 return -EFAULT;
272
273 memcpy(&fdata.timeout, &compat.timeout,
274 sizeof(struct pps_ktime_compat));
275
276 err = pps_cdev_pps_fetch(pps, &fdata);
277 if (err)
278 return err;
279
280 /* Return the fetched timestamp and save last fetched event */
281 spin_lock_irq(&pps->lock);
282
283 pps->last_fetched_ev = pps->last_ev;
284
285 compat.info.assert_sequence = pps->assert_sequence;
286 compat.info.clear_sequence = pps->clear_sequence;
287 compat.info.current_mode = pps->current_mode;
288
289 memcpy(&compat.info.assert_tu, &pps->assert_tu,
290 sizeof(struct pps_ktime_compat));
291 memcpy(&compat.info.clear_tu, &pps->clear_tu,
292 sizeof(struct pps_ktime_compat));
293
294 spin_unlock_irq(&pps->lock);
295
296 return copy_to_user(uarg, &compat,
297 sizeof(struct pps_fdata_compat)) ? -EFAULT : 0;
298 }
299
300 return pps_cdev_ioctl(file, cmd, arg);
301 }
302 #else
303 #define pps_cdev_compat_ioctl NULL
304 #endif
305
pps_idr_get(unsigned long id)306 static struct pps_device *pps_idr_get(unsigned long id)
307 {
308 struct pps_device *pps;
309
310 mutex_lock(&pps_idr_lock);
311 pps = idr_find(&pps_idr, id);
312 if (pps)
313 get_device(&pps->dev);
314
315 mutex_unlock(&pps_idr_lock);
316 return pps;
317 }
318
pps_cdev_open(struct inode * inode,struct file * file)319 static int pps_cdev_open(struct inode *inode, struct file *file)
320 {
321 struct pps_device *pps = pps_idr_get(iminor(inode));
322
323 if (!pps)
324 return -ENODEV;
325
326 file->private_data = pps;
327 return 0;
328 }
329
pps_cdev_release(struct inode * inode,struct file * file)330 static int pps_cdev_release(struct inode *inode, struct file *file)
331 {
332 struct pps_device *pps = file->private_data;
333
334 WARN_ON(pps->id != iminor(inode));
335 put_device(&pps->dev);
336 return 0;
337 }
338
339 /*
340 * Char device stuff
341 */
342
343 static const struct file_operations pps_cdev_fops = {
344 .owner = THIS_MODULE,
345 .poll = pps_cdev_poll,
346 .fasync = pps_cdev_fasync,
347 .compat_ioctl = pps_cdev_compat_ioctl,
348 .unlocked_ioctl = pps_cdev_ioctl,
349 .open = pps_cdev_open,
350 .release = pps_cdev_release,
351 };
352
pps_device_destruct(struct device * dev)353 static void pps_device_destruct(struct device *dev)
354 {
355 struct pps_device *pps = dev_get_drvdata(dev);
356
357 pr_debug("deallocating pps%d\n", pps->id);
358 kfree(pps);
359 }
360
pps_register_cdev(struct pps_device * pps)361 int pps_register_cdev(struct pps_device *pps)
362 {
363 int err;
364
365 mutex_lock(&pps_idr_lock);
366 /*
367 * Get new ID for the new PPS source. After idr_alloc() calling
368 * the new source will be freely available into the kernel.
369 */
370 err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
371 if (err < 0) {
372 if (err == -ENOSPC) {
373 pr_err("%s: too many PPS sources in the system\n",
374 pps->info.name);
375 err = -EBUSY;
376 }
377 goto out_unlock;
378 }
379 pps->id = err;
380
381 pps->dev.class = pps_class;
382 pps->dev.parent = pps->info.dev;
383 pps->dev.devt = MKDEV(pps_major, pps->id);
384 dev_set_drvdata(&pps->dev, pps);
385 dev_set_name(&pps->dev, "pps%d", pps->id);
386 err = device_register(&pps->dev);
387 if (err)
388 goto free_idr;
389
390 /* Override the release function with our own */
391 pps->dev.release = pps_device_destruct;
392
393 pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, pps_major,
394 pps->id);
395
396 get_device(&pps->dev);
397 mutex_unlock(&pps_idr_lock);
398 return 0;
399
400 free_idr:
401 idr_remove(&pps_idr, pps->id);
402 put_device(&pps->dev);
403 out_unlock:
404 mutex_unlock(&pps_idr_lock);
405 return err;
406 }
407
pps_unregister_cdev(struct pps_device * pps)408 void pps_unregister_cdev(struct pps_device *pps)
409 {
410 pr_debug("unregistering pps%d\n", pps->id);
411 pps->lookup_cookie = NULL;
412 device_destroy(pps_class, pps->dev.devt);
413
414 /* Now we can release the ID for re-use */
415 mutex_lock(&pps_idr_lock);
416 idr_remove(&pps_idr, pps->id);
417 put_device(&pps->dev);
418 mutex_unlock(&pps_idr_lock);
419 }
420
421 /*
422 * Look up a pps device by magic cookie.
423 * The cookie is usually a pointer to some enclosing device, but this
424 * code doesn't care; you should never be dereferencing it.
425 *
426 * This is a bit of a kludge that is currently used only by the PPS
427 * serial line discipline. It may need to be tweaked when a second user
428 * is found.
429 *
430 * There is no function interface for setting the lookup_cookie field.
431 * It's initialized to NULL when the pps device is created, and if a
432 * client wants to use it, just fill it in afterward.
433 *
434 * The cookie is automatically set to NULL in pps_unregister_source()
435 * so that it will not be used again, even if the pps device cannot
436 * be removed from the idr due to pending references holding the minor
437 * number in use.
438 *
439 * Since pps_idr holds a reference to the device, the returned
440 * pps_device is guaranteed to be valid until pps_unregister_cdev() is
441 * called on it. But after calling pps_unregister_cdev(), it may be
442 * freed at any time.
443 */
pps_lookup_dev(void const * cookie)444 struct pps_device *pps_lookup_dev(void const *cookie)
445 {
446 struct pps_device *pps;
447 unsigned id;
448
449 rcu_read_lock();
450 idr_for_each_entry(&pps_idr, pps, id)
451 if (cookie == pps->lookup_cookie)
452 break;
453 rcu_read_unlock();
454 return pps;
455 }
456 EXPORT_SYMBOL(pps_lookup_dev);
457
458 /*
459 * Module stuff
460 */
461
pps_exit(void)462 static void __exit pps_exit(void)
463 {
464 class_destroy(pps_class);
465 __unregister_chrdev(pps_major, 0, PPS_MAX_SOURCES, "pps");
466 }
467
pps_init(void)468 static int __init pps_init(void)
469 {
470 pps_class = class_create("pps");
471 if (IS_ERR(pps_class)) {
472 pr_err("failed to allocate class\n");
473 return PTR_ERR(pps_class);
474 }
475 pps_class->dev_groups = pps_groups;
476
477 pps_major = __register_chrdev(0, 0, PPS_MAX_SOURCES, "pps",
478 &pps_cdev_fops);
479 if (pps_major < 0) {
480 pr_err("failed to allocate char device region\n");
481 goto remove_class;
482 }
483
484 pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
485 pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
486 "<giometti@linux.it>\n", PPS_VERSION);
487
488 return 0;
489
490 remove_class:
491 class_destroy(pps_class);
492 return pps_major;
493 }
494
495 subsys_initcall(pps_init);
496 module_exit(pps_exit);
497
498 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
499 MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
500 MODULE_LICENSE("GPL");
501