Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PPS core file
0004  *
0005  * Copyright (C) 2005-2009   Rodolfo Giometti <giometti@linux.it>
0006  */
0007 
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/sched.h>
0014 #include <linux/uaccess.h>
0015 #include <linux/idr.h>
0016 #include <linux/mutex.h>
0017 #include <linux/cdev.h>
0018 #include <linux/poll.h>
0019 #include <linux/pps_kernel.h>
0020 #include <linux/slab.h>
0021 
0022 #include "kc.h"
0023 
0024 /*
0025  * Local variables
0026  */
0027 
0028 static dev_t pps_devt;
0029 static struct class *pps_class;
0030 
0031 static DEFINE_MUTEX(pps_idr_lock);
0032 static DEFINE_IDR(pps_idr);
0033 
0034 /*
0035  * Char device methods
0036  */
0037 
0038 static __poll_t pps_cdev_poll(struct file *file, poll_table *wait)
0039 {
0040     struct pps_device *pps = file->private_data;
0041 
0042     poll_wait(file, &pps->queue, wait);
0043 
0044     return EPOLLIN | EPOLLRDNORM;
0045 }
0046 
0047 static int pps_cdev_fasync(int fd, struct file *file, int on)
0048 {
0049     struct pps_device *pps = file->private_data;
0050     return fasync_helper(fd, file, on, &pps->async_queue);
0051 }
0052 
0053 static int pps_cdev_pps_fetch(struct pps_device *pps, struct pps_fdata *fdata)
0054 {
0055     unsigned int ev = pps->last_ev;
0056     int err = 0;
0057 
0058     /* Manage the timeout */
0059     if (fdata->timeout.flags & PPS_TIME_INVALID)
0060         err = wait_event_interruptible(pps->queue,
0061                 ev != pps->last_ev);
0062     else {
0063         unsigned long ticks;
0064 
0065         dev_dbg(pps->dev, "timeout %lld.%09d\n",
0066                 (long long) fdata->timeout.sec,
0067                 fdata->timeout.nsec);
0068         ticks = fdata->timeout.sec * HZ;
0069         ticks += fdata->timeout.nsec / (NSEC_PER_SEC / HZ);
0070 
0071         if (ticks != 0) {
0072             err = wait_event_interruptible_timeout(
0073                     pps->queue,
0074                     ev != pps->last_ev,
0075                     ticks);
0076             if (err == 0)
0077                 return -ETIMEDOUT;
0078         }
0079     }
0080 
0081     /* Check for pending signals */
0082     if (err == -ERESTARTSYS) {
0083         dev_dbg(pps->dev, "pending signal caught\n");
0084         return -EINTR;
0085     }
0086 
0087     return 0;
0088 }
0089 
0090 static long pps_cdev_ioctl(struct file *file,
0091         unsigned int cmd, unsigned long arg)
0092 {
0093     struct pps_device *pps = file->private_data;
0094     struct pps_kparams params;
0095     void __user *uarg = (void __user *) arg;
0096     int __user *iuarg = (int __user *) arg;
0097     int err;
0098 
0099     switch (cmd) {
0100     case PPS_GETPARAMS:
0101         dev_dbg(pps->dev, "PPS_GETPARAMS\n");
0102 
0103         spin_lock_irq(&pps->lock);
0104 
0105         /* Get the current parameters */
0106         params = pps->params;
0107 
0108         spin_unlock_irq(&pps->lock);
0109 
0110         err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
0111         if (err)
0112             return -EFAULT;
0113 
0114         break;
0115 
0116     case PPS_SETPARAMS:
0117         dev_dbg(pps->dev, "PPS_SETPARAMS\n");
0118 
0119         /* Check the capabilities */
0120         if (!capable(CAP_SYS_TIME))
0121             return -EPERM;
0122 
0123         err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
0124         if (err)
0125             return -EFAULT;
0126         if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
0127             dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
0128                                 params.mode);
0129             return -EINVAL;
0130         }
0131 
0132         /* Check for supported capabilities */
0133         if ((params.mode & ~pps->info.mode) != 0) {
0134             dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
0135                                 params.mode);
0136             return -EINVAL;
0137         }
0138 
0139         spin_lock_irq(&pps->lock);
0140 
0141         /* Save the new parameters */
0142         pps->params = params;
0143 
0144         /* Restore the read only parameters */
0145         if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
0146             /* section 3.3 of RFC 2783 interpreted */
0147             dev_dbg(pps->dev, "time format unspecified (%x)\n",
0148                                 params.mode);
0149             pps->params.mode |= PPS_TSFMT_TSPEC;
0150         }
0151         if (pps->info.mode & PPS_CANWAIT)
0152             pps->params.mode |= PPS_CANWAIT;
0153         pps->params.api_version = PPS_API_VERS;
0154 
0155         /*
0156          * Clear unused fields of pps_kparams to avoid leaking
0157          * uninitialized data of the PPS_SETPARAMS caller via
0158          * PPS_GETPARAMS
0159          */
0160         pps->params.assert_off_tu.flags = 0;
0161         pps->params.clear_off_tu.flags = 0;
0162 
0163         spin_unlock_irq(&pps->lock);
0164 
0165         break;
0166 
0167     case PPS_GETCAP:
0168         dev_dbg(pps->dev, "PPS_GETCAP\n");
0169 
0170         err = put_user(pps->info.mode, iuarg);
0171         if (err)
0172             return -EFAULT;
0173 
0174         break;
0175 
0176     case PPS_FETCH: {
0177         struct pps_fdata fdata;
0178 
0179         dev_dbg(pps->dev, "PPS_FETCH\n");
0180 
0181         err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
0182         if (err)
0183             return -EFAULT;
0184 
0185         err = pps_cdev_pps_fetch(pps, &fdata);
0186         if (err)
0187             return err;
0188 
0189         /* Return the fetched timestamp */
0190         spin_lock_irq(&pps->lock);
0191 
0192         fdata.info.assert_sequence = pps->assert_sequence;
0193         fdata.info.clear_sequence = pps->clear_sequence;
0194         fdata.info.assert_tu = pps->assert_tu;
0195         fdata.info.clear_tu = pps->clear_tu;
0196         fdata.info.current_mode = pps->current_mode;
0197 
0198         spin_unlock_irq(&pps->lock);
0199 
0200         err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
0201         if (err)
0202             return -EFAULT;
0203 
0204         break;
0205     }
0206     case PPS_KC_BIND: {
0207         struct pps_bind_args bind_args;
0208 
0209         dev_dbg(pps->dev, "PPS_KC_BIND\n");
0210 
0211         /* Check the capabilities */
0212         if (!capable(CAP_SYS_TIME))
0213             return -EPERM;
0214 
0215         if (copy_from_user(&bind_args, uarg,
0216                     sizeof(struct pps_bind_args)))
0217             return -EFAULT;
0218 
0219         /* Check for supported capabilities */
0220         if ((bind_args.edge & ~pps->info.mode) != 0) {
0221             dev_err(pps->dev, "unsupported capabilities (%x)\n",
0222                     bind_args.edge);
0223             return -EINVAL;
0224         }
0225 
0226         /* Validate parameters roughly */
0227         if (bind_args.tsformat != PPS_TSFMT_TSPEC ||
0228                 (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 ||
0229                 bind_args.consumer != PPS_KC_HARDPPS) {
0230             dev_err(pps->dev, "invalid kernel consumer bind"
0231                     " parameters (%x)\n", bind_args.edge);
0232             return -EINVAL;
0233         }
0234 
0235         err = pps_kc_bind(pps, &bind_args);
0236         if (err < 0)
0237             return err;
0238 
0239         break;
0240     }
0241     default:
0242         return -ENOTTY;
0243     }
0244 
0245     return 0;
0246 }
0247 
0248 #ifdef CONFIG_COMPAT
0249 static long pps_cdev_compat_ioctl(struct file *file,
0250         unsigned int cmd, unsigned long arg)
0251 {
0252     struct pps_device *pps = file->private_data;
0253     void __user *uarg = (void __user *) arg;
0254 
0255     cmd = _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(void *));
0256 
0257     if (cmd == PPS_FETCH) {
0258         struct pps_fdata_compat compat;
0259         struct pps_fdata fdata;
0260         int err;
0261 
0262         dev_dbg(pps->dev, "PPS_FETCH\n");
0263 
0264         err = copy_from_user(&compat, uarg, sizeof(struct pps_fdata_compat));
0265         if (err)
0266             return -EFAULT;
0267 
0268         memcpy(&fdata.timeout, &compat.timeout,
0269                     sizeof(struct pps_ktime_compat));
0270 
0271         err = pps_cdev_pps_fetch(pps, &fdata);
0272         if (err)
0273             return err;
0274 
0275         /* Return the fetched timestamp */
0276         spin_lock_irq(&pps->lock);
0277 
0278         compat.info.assert_sequence = pps->assert_sequence;
0279         compat.info.clear_sequence = pps->clear_sequence;
0280         compat.info.current_mode = pps->current_mode;
0281 
0282         memcpy(&compat.info.assert_tu, &pps->assert_tu,
0283                 sizeof(struct pps_ktime_compat));
0284         memcpy(&compat.info.clear_tu, &pps->clear_tu,
0285                 sizeof(struct pps_ktime_compat));
0286 
0287         spin_unlock_irq(&pps->lock);
0288 
0289         return copy_to_user(uarg, &compat,
0290                 sizeof(struct pps_fdata_compat)) ? -EFAULT : 0;
0291     }
0292 
0293     return pps_cdev_ioctl(file, cmd, arg);
0294 }
0295 #else
0296 #define pps_cdev_compat_ioctl   NULL
0297 #endif
0298 
0299 static int pps_cdev_open(struct inode *inode, struct file *file)
0300 {
0301     struct pps_device *pps = container_of(inode->i_cdev,
0302                         struct pps_device, cdev);
0303     file->private_data = pps;
0304     kobject_get(&pps->dev->kobj);
0305     return 0;
0306 }
0307 
0308 static int pps_cdev_release(struct inode *inode, struct file *file)
0309 {
0310     struct pps_device *pps = container_of(inode->i_cdev,
0311                         struct pps_device, cdev);
0312     kobject_put(&pps->dev->kobj);
0313     return 0;
0314 }
0315 
0316 /*
0317  * Char device stuff
0318  */
0319 
0320 static const struct file_operations pps_cdev_fops = {
0321     .owner      = THIS_MODULE,
0322     .llseek     = no_llseek,
0323     .poll       = pps_cdev_poll,
0324     .fasync     = pps_cdev_fasync,
0325     .compat_ioctl   = pps_cdev_compat_ioctl,
0326     .unlocked_ioctl = pps_cdev_ioctl,
0327     .open       = pps_cdev_open,
0328     .release    = pps_cdev_release,
0329 };
0330 
0331 static void pps_device_destruct(struct device *dev)
0332 {
0333     struct pps_device *pps = dev_get_drvdata(dev);
0334 
0335     cdev_del(&pps->cdev);
0336 
0337     /* Now we can release the ID for re-use */
0338     pr_debug("deallocating pps%d\n", pps->id);
0339     mutex_lock(&pps_idr_lock);
0340     idr_remove(&pps_idr, pps->id);
0341     mutex_unlock(&pps_idr_lock);
0342 
0343     kfree(dev);
0344     kfree(pps);
0345 }
0346 
0347 int pps_register_cdev(struct pps_device *pps)
0348 {
0349     int err;
0350     dev_t devt;
0351 
0352     mutex_lock(&pps_idr_lock);
0353     /*
0354      * Get new ID for the new PPS source.  After idr_alloc() calling
0355      * the new source will be freely available into the kernel.
0356      */
0357     err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL);
0358     if (err < 0) {
0359         if (err == -ENOSPC) {
0360             pr_err("%s: too many PPS sources in the system\n",
0361                    pps->info.name);
0362             err = -EBUSY;
0363         }
0364         goto out_unlock;
0365     }
0366     pps->id = err;
0367     mutex_unlock(&pps_idr_lock);
0368 
0369     devt = MKDEV(MAJOR(pps_devt), pps->id);
0370 
0371     cdev_init(&pps->cdev, &pps_cdev_fops);
0372     pps->cdev.owner = pps->info.owner;
0373 
0374     err = cdev_add(&pps->cdev, devt, 1);
0375     if (err) {
0376         pr_err("%s: failed to add char device %d:%d\n",
0377                 pps->info.name, MAJOR(pps_devt), pps->id);
0378         goto free_idr;
0379     }
0380     pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
0381                             "pps%d", pps->id);
0382     if (IS_ERR(pps->dev)) {
0383         err = PTR_ERR(pps->dev);
0384         goto del_cdev;
0385     }
0386 
0387     /* Override the release function with our own */
0388     pps->dev->release = pps_device_destruct;
0389 
0390     pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
0391             MAJOR(pps_devt), pps->id);
0392 
0393     return 0;
0394 
0395 del_cdev:
0396     cdev_del(&pps->cdev);
0397 
0398 free_idr:
0399     mutex_lock(&pps_idr_lock);
0400     idr_remove(&pps_idr, pps->id);
0401 out_unlock:
0402     mutex_unlock(&pps_idr_lock);
0403     return err;
0404 }
0405 
0406 void pps_unregister_cdev(struct pps_device *pps)
0407 {
0408     pr_debug("unregistering pps%d\n", pps->id);
0409     pps->lookup_cookie = NULL;
0410     device_destroy(pps_class, pps->dev->devt);
0411 }
0412 
0413 /*
0414  * Look up a pps device by magic cookie.
0415  * The cookie is usually a pointer to some enclosing device, but this
0416  * code doesn't care; you should never be dereferencing it.
0417  *
0418  * This is a bit of a kludge that is currently used only by the PPS
0419  * serial line discipline.  It may need to be tweaked when a second user
0420  * is found.
0421  *
0422  * There is no function interface for setting the lookup_cookie field.
0423  * It's initialized to NULL when the pps device is created, and if a
0424  * client wants to use it, just fill it in afterward.
0425  *
0426  * The cookie is automatically set to NULL in pps_unregister_source()
0427  * so that it will not be used again, even if the pps device cannot
0428  * be removed from the idr due to pending references holding the minor
0429  * number in use.
0430  */
0431 struct pps_device *pps_lookup_dev(void const *cookie)
0432 {
0433     struct pps_device *pps;
0434     unsigned id;
0435 
0436     rcu_read_lock();
0437     idr_for_each_entry(&pps_idr, pps, id)
0438         if (cookie == pps->lookup_cookie)
0439             break;
0440     rcu_read_unlock();
0441     return pps;
0442 }
0443 EXPORT_SYMBOL(pps_lookup_dev);
0444 
0445 /*
0446  * Module stuff
0447  */
0448 
0449 static void __exit pps_exit(void)
0450 {
0451     class_destroy(pps_class);
0452     unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
0453 }
0454 
0455 static int __init pps_init(void)
0456 {
0457     int err;
0458 
0459     pps_class = class_create(THIS_MODULE, "pps");
0460     if (IS_ERR(pps_class)) {
0461         pr_err("failed to allocate class\n");
0462         return PTR_ERR(pps_class);
0463     }
0464     pps_class->dev_groups = pps_groups;
0465 
0466     err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
0467     if (err < 0) {
0468         pr_err("failed to allocate char device region\n");
0469         goto remove_class;
0470     }
0471 
0472     pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
0473     pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
0474         "<giometti@linux.it>\n", PPS_VERSION);
0475 
0476     return 0;
0477 
0478 remove_class:
0479     class_destroy(pps_class);
0480 
0481     return err;
0482 }
0483 
0484 subsys_initcall(pps_init);
0485 module_exit(pps_exit);
0486 
0487 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
0488 MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
0489 MODULE_LICENSE("GPL");