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