1eae9d2baSRodolfo Giometti /* 2eae9d2baSRodolfo Giometti * PPS core file 3eae9d2baSRodolfo Giometti * 4eae9d2baSRodolfo Giometti * 5eae9d2baSRodolfo Giometti * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it> 6eae9d2baSRodolfo Giometti * 7eae9d2baSRodolfo Giometti * This program is free software; you can redistribute it and/or modify 8eae9d2baSRodolfo Giometti * it under the terms of the GNU General Public License as published by 9eae9d2baSRodolfo Giometti * the Free Software Foundation; either version 2 of the License, or 10eae9d2baSRodolfo Giometti * (at your option) any later version. 11eae9d2baSRodolfo Giometti * 12eae9d2baSRodolfo Giometti * This program is distributed in the hope that it will be useful, 13eae9d2baSRodolfo Giometti * but WITHOUT ANY WARRANTY; without even the implied warranty of 14eae9d2baSRodolfo Giometti * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15eae9d2baSRodolfo Giometti * GNU General Public License for more details. 16eae9d2baSRodolfo Giometti * 17eae9d2baSRodolfo Giometti * You should have received a copy of the GNU General Public License 18eae9d2baSRodolfo Giometti * along with this program; if not, write to the Free Software 19eae9d2baSRodolfo Giometti * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20eae9d2baSRodolfo Giometti */ 21eae9d2baSRodolfo Giometti 22*7f7cce74SAlexander Gordeev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 23eae9d2baSRodolfo Giometti 24eae9d2baSRodolfo Giometti #include <linux/kernel.h> 25eae9d2baSRodolfo Giometti #include <linux/module.h> 26eae9d2baSRodolfo Giometti #include <linux/init.h> 27eae9d2baSRodolfo Giometti #include <linux/sched.h> 28eae9d2baSRodolfo Giometti #include <linux/uaccess.h> 29eae9d2baSRodolfo Giometti #include <linux/idr.h> 30eae9d2baSRodolfo Giometti #include <linux/cdev.h> 31eae9d2baSRodolfo Giometti #include <linux/poll.h> 32eae9d2baSRodolfo Giometti #include <linux/pps_kernel.h> 33eae9d2baSRodolfo Giometti 34eae9d2baSRodolfo Giometti /* 35eae9d2baSRodolfo Giometti * Local variables 36eae9d2baSRodolfo Giometti */ 37eae9d2baSRodolfo Giometti 38eae9d2baSRodolfo Giometti static dev_t pps_devt; 39eae9d2baSRodolfo Giometti static struct class *pps_class; 40eae9d2baSRodolfo Giometti 41eae9d2baSRodolfo Giometti /* 42eae9d2baSRodolfo Giometti * Char device methods 43eae9d2baSRodolfo Giometti */ 44eae9d2baSRodolfo Giometti 45eae9d2baSRodolfo Giometti static unsigned int pps_cdev_poll(struct file *file, poll_table *wait) 46eae9d2baSRodolfo Giometti { 47eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 48eae9d2baSRodolfo Giometti 49eae9d2baSRodolfo Giometti poll_wait(file, &pps->queue, wait); 50eae9d2baSRodolfo Giometti 51eae9d2baSRodolfo Giometti return POLLIN | POLLRDNORM; 52eae9d2baSRodolfo Giometti } 53eae9d2baSRodolfo Giometti 54eae9d2baSRodolfo Giometti static int pps_cdev_fasync(int fd, struct file *file, int on) 55eae9d2baSRodolfo Giometti { 56eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 57eae9d2baSRodolfo Giometti return fasync_helper(fd, file, on, &pps->async_queue); 58eae9d2baSRodolfo Giometti } 59eae9d2baSRodolfo Giometti 60eae9d2baSRodolfo Giometti static long pps_cdev_ioctl(struct file *file, 61eae9d2baSRodolfo Giometti unsigned int cmd, unsigned long arg) 62eae9d2baSRodolfo Giometti { 63eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 64eae9d2baSRodolfo Giometti struct pps_kparams params; 65eae9d2baSRodolfo Giometti void __user *uarg = (void __user *) arg; 66eae9d2baSRodolfo Giometti int __user *iuarg = (int __user *) arg; 67eae9d2baSRodolfo Giometti int err; 68eae9d2baSRodolfo Giometti 69eae9d2baSRodolfo Giometti switch (cmd) { 70eae9d2baSRodolfo Giometti case PPS_GETPARAMS: 71*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETPARAMS\n"); 72eae9d2baSRodolfo Giometti 73cbf83cc5SRodolfo Giometti spin_lock_irq(&pps->lock); 74cbf83cc5SRodolfo Giometti 75cbf83cc5SRodolfo Giometti /* Get the current parameters */ 76cbf83cc5SRodolfo Giometti params = pps->params; 77cbf83cc5SRodolfo Giometti 78cbf83cc5SRodolfo Giometti spin_unlock_irq(&pps->lock); 79cbf83cc5SRodolfo Giometti 80cbf83cc5SRodolfo Giometti err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams)); 81eae9d2baSRodolfo Giometti if (err) 82eae9d2baSRodolfo Giometti return -EFAULT; 83eae9d2baSRodolfo Giometti 84eae9d2baSRodolfo Giometti break; 85eae9d2baSRodolfo Giometti 86eae9d2baSRodolfo Giometti case PPS_SETPARAMS: 87*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_SETPARAMS\n"); 88eae9d2baSRodolfo Giometti 89eae9d2baSRodolfo Giometti /* Check the capabilities */ 90eae9d2baSRodolfo Giometti if (!capable(CAP_SYS_TIME)) 91eae9d2baSRodolfo Giometti return -EPERM; 92eae9d2baSRodolfo Giometti 93eae9d2baSRodolfo Giometti err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); 94eae9d2baSRodolfo Giometti if (err) 95eae9d2baSRodolfo Giometti return -EFAULT; 96eae9d2baSRodolfo Giometti if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { 97*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "capture mode unspecified (%x)\n", 98eae9d2baSRodolfo Giometti params.mode); 99eae9d2baSRodolfo Giometti return -EINVAL; 100eae9d2baSRodolfo Giometti } 101eae9d2baSRodolfo Giometti 102eae9d2baSRodolfo Giometti /* Check for supported capabilities */ 103eae9d2baSRodolfo Giometti if ((params.mode & ~pps->info.mode) != 0) { 104*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "unsupported capabilities (%x)\n", 105eae9d2baSRodolfo Giometti params.mode); 106eae9d2baSRodolfo Giometti return -EINVAL; 107eae9d2baSRodolfo Giometti } 108eae9d2baSRodolfo Giometti 109eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 110eae9d2baSRodolfo Giometti 111eae9d2baSRodolfo Giometti /* Save the new parameters */ 112eae9d2baSRodolfo Giometti pps->params = params; 113eae9d2baSRodolfo Giometti 114eae9d2baSRodolfo Giometti /* Restore the read only parameters */ 115eae9d2baSRodolfo Giometti if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { 116eae9d2baSRodolfo Giometti /* section 3.3 of RFC 2783 interpreted */ 117*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "time format unspecified (%x)\n", 118eae9d2baSRodolfo Giometti params.mode); 119eae9d2baSRodolfo Giometti pps->params.mode |= PPS_TSFMT_TSPEC; 120eae9d2baSRodolfo Giometti } 121eae9d2baSRodolfo Giometti if (pps->info.mode & PPS_CANWAIT) 122eae9d2baSRodolfo Giometti pps->params.mode |= PPS_CANWAIT; 123eae9d2baSRodolfo Giometti pps->params.api_version = PPS_API_VERS; 124eae9d2baSRodolfo Giometti 125eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 126eae9d2baSRodolfo Giometti 127eae9d2baSRodolfo Giometti break; 128eae9d2baSRodolfo Giometti 129eae9d2baSRodolfo Giometti case PPS_GETCAP: 130*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETCAP\n"); 131eae9d2baSRodolfo Giometti 132eae9d2baSRodolfo Giometti err = put_user(pps->info.mode, iuarg); 133eae9d2baSRodolfo Giometti if (err) 134eae9d2baSRodolfo Giometti return -EFAULT; 135eae9d2baSRodolfo Giometti 136eae9d2baSRodolfo Giometti break; 137eae9d2baSRodolfo Giometti 13886d921f9SAlexander Gordeev case PPS_FETCH: { 13986d921f9SAlexander Gordeev struct pps_fdata fdata; 1403003d55bSAlexander Gordeev unsigned int ev; 14186d921f9SAlexander Gordeev 142*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_FETCH\n"); 143eae9d2baSRodolfo Giometti 144eae9d2baSRodolfo Giometti err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); 145eae9d2baSRodolfo Giometti if (err) 146eae9d2baSRodolfo Giometti return -EFAULT; 147eae9d2baSRodolfo Giometti 1483003d55bSAlexander Gordeev ev = pps->last_ev; 149eae9d2baSRodolfo Giometti 150eae9d2baSRodolfo Giometti /* Manage the timeout */ 151eae9d2baSRodolfo Giometti if (fdata.timeout.flags & PPS_TIME_INVALID) 1523003d55bSAlexander Gordeev err = wait_event_interruptible(pps->queue, 1533003d55bSAlexander Gordeev ev != pps->last_ev); 154eae9d2baSRodolfo Giometti else { 15586d921f9SAlexander Gordeev unsigned long ticks; 15686d921f9SAlexander Gordeev 157*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "timeout %lld.%09d\n", 158eae9d2baSRodolfo Giometti (long long) fdata.timeout.sec, 159eae9d2baSRodolfo Giometti fdata.timeout.nsec); 160eae9d2baSRodolfo Giometti ticks = fdata.timeout.sec * HZ; 161eae9d2baSRodolfo Giometti ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ); 162eae9d2baSRodolfo Giometti 163eae9d2baSRodolfo Giometti if (ticks != 0) { 164eae9d2baSRodolfo Giometti err = wait_event_interruptible_timeout( 1653003d55bSAlexander Gordeev pps->queue, 1663003d55bSAlexander Gordeev ev != pps->last_ev, 1673003d55bSAlexander Gordeev ticks); 168eae9d2baSRodolfo Giometti if (err == 0) 169eae9d2baSRodolfo Giometti return -ETIMEDOUT; 170eae9d2baSRodolfo Giometti } 171eae9d2baSRodolfo Giometti } 172eae9d2baSRodolfo Giometti 173eae9d2baSRodolfo Giometti /* Check for pending signals */ 174eae9d2baSRodolfo Giometti if (err == -ERESTARTSYS) { 175*7f7cce74SAlexander Gordeev dev_dbg(pps->dev, "pending signal caught\n"); 176eae9d2baSRodolfo Giometti return -EINTR; 177eae9d2baSRodolfo Giometti } 178eae9d2baSRodolfo Giometti 179eae9d2baSRodolfo Giometti /* Return the fetched timestamp */ 180eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 181eae9d2baSRodolfo Giometti 182eae9d2baSRodolfo Giometti fdata.info.assert_sequence = pps->assert_sequence; 183eae9d2baSRodolfo Giometti fdata.info.clear_sequence = pps->clear_sequence; 184eae9d2baSRodolfo Giometti fdata.info.assert_tu = pps->assert_tu; 185eae9d2baSRodolfo Giometti fdata.info.clear_tu = pps->clear_tu; 186eae9d2baSRodolfo Giometti fdata.info.current_mode = pps->current_mode; 187eae9d2baSRodolfo Giometti 188eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 189eae9d2baSRodolfo Giometti 190eae9d2baSRodolfo Giometti err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); 191eae9d2baSRodolfo Giometti if (err) 192eae9d2baSRodolfo Giometti return -EFAULT; 193eae9d2baSRodolfo Giometti 194eae9d2baSRodolfo Giometti break; 19586d921f9SAlexander Gordeev } 196eae9d2baSRodolfo Giometti default: 197eae9d2baSRodolfo Giometti return -ENOTTY; 198eae9d2baSRodolfo Giometti break; 199eae9d2baSRodolfo Giometti } 200eae9d2baSRodolfo Giometti 201eae9d2baSRodolfo Giometti return 0; 202eae9d2baSRodolfo Giometti } 203eae9d2baSRodolfo Giometti 204eae9d2baSRodolfo Giometti static int pps_cdev_open(struct inode *inode, struct file *file) 205eae9d2baSRodolfo Giometti { 206eae9d2baSRodolfo Giometti struct pps_device *pps = container_of(inode->i_cdev, 207eae9d2baSRodolfo Giometti struct pps_device, cdev); 208eae9d2baSRodolfo Giometti file->private_data = pps; 209eae9d2baSRodolfo Giometti 210eae9d2baSRodolfo Giometti return 0; 211eae9d2baSRodolfo Giometti } 212eae9d2baSRodolfo Giometti 213eae9d2baSRodolfo Giometti static int pps_cdev_release(struct inode *inode, struct file *file) 214eae9d2baSRodolfo Giometti { 215eae9d2baSRodolfo Giometti return 0; 216eae9d2baSRodolfo Giometti } 217eae9d2baSRodolfo Giometti 218eae9d2baSRodolfo Giometti /* 219eae9d2baSRodolfo Giometti * Char device stuff 220eae9d2baSRodolfo Giometti */ 221eae9d2baSRodolfo Giometti 222eae9d2baSRodolfo Giometti static const struct file_operations pps_cdev_fops = { 223eae9d2baSRodolfo Giometti .owner = THIS_MODULE, 224eae9d2baSRodolfo Giometti .llseek = no_llseek, 225eae9d2baSRodolfo Giometti .poll = pps_cdev_poll, 226eae9d2baSRodolfo Giometti .fasync = pps_cdev_fasync, 227eae9d2baSRodolfo Giometti .unlocked_ioctl = pps_cdev_ioctl, 228eae9d2baSRodolfo Giometti .open = pps_cdev_open, 229eae9d2baSRodolfo Giometti .release = pps_cdev_release, 230eae9d2baSRodolfo Giometti }; 231eae9d2baSRodolfo Giometti 232eae9d2baSRodolfo Giometti int pps_register_cdev(struct pps_device *pps) 233eae9d2baSRodolfo Giometti { 234eae9d2baSRodolfo Giometti int err; 2355e196d34SAlexander Gordeev dev_t devt; 236eae9d2baSRodolfo Giometti 2375e196d34SAlexander Gordeev devt = MKDEV(MAJOR(pps_devt), pps->id); 2385e196d34SAlexander Gordeev 239eae9d2baSRodolfo Giometti cdev_init(&pps->cdev, &pps_cdev_fops); 240eae9d2baSRodolfo Giometti pps->cdev.owner = pps->info.owner; 241eae9d2baSRodolfo Giometti 2425e196d34SAlexander Gordeev err = cdev_add(&pps->cdev, devt, 1); 243eae9d2baSRodolfo Giometti if (err) { 244*7f7cce74SAlexander Gordeev pr_err("%s: failed to add char device %d:%d\n", 245eae9d2baSRodolfo Giometti pps->info.name, MAJOR(pps_devt), pps->id); 246eae9d2baSRodolfo Giometti return err; 247eae9d2baSRodolfo Giometti } 2485e196d34SAlexander Gordeev pps->dev = device_create(pps_class, pps->info.dev, devt, pps, 249eae9d2baSRodolfo Giometti "pps%d", pps->id); 250054b2b13SJoonwoo Park if (IS_ERR(pps->dev)) 251eae9d2baSRodolfo Giometti goto del_cdev; 252eae9d2baSRodolfo Giometti 253eae9d2baSRodolfo Giometti pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, 254eae9d2baSRodolfo Giometti MAJOR(pps_devt), pps->id); 255eae9d2baSRodolfo Giometti 256eae9d2baSRodolfo Giometti return 0; 257eae9d2baSRodolfo Giometti 258eae9d2baSRodolfo Giometti del_cdev: 259eae9d2baSRodolfo Giometti cdev_del(&pps->cdev); 260eae9d2baSRodolfo Giometti 261eae9d2baSRodolfo Giometti return err; 262eae9d2baSRodolfo Giometti } 263eae9d2baSRodolfo Giometti 264eae9d2baSRodolfo Giometti void pps_unregister_cdev(struct pps_device *pps) 265eae9d2baSRodolfo Giometti { 2665e196d34SAlexander Gordeev device_destroy(pps_class, pps->dev->devt); 267eae9d2baSRodolfo Giometti cdev_del(&pps->cdev); 268eae9d2baSRodolfo Giometti } 269eae9d2baSRodolfo Giometti 270eae9d2baSRodolfo Giometti /* 271eae9d2baSRodolfo Giometti * Module stuff 272eae9d2baSRodolfo Giometti */ 273eae9d2baSRodolfo Giometti 274eae9d2baSRodolfo Giometti static void __exit pps_exit(void) 275eae9d2baSRodolfo Giometti { 276eae9d2baSRodolfo Giometti class_destroy(pps_class); 277eae9d2baSRodolfo Giometti unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); 278eae9d2baSRodolfo Giometti } 279eae9d2baSRodolfo Giometti 280eae9d2baSRodolfo Giometti static int __init pps_init(void) 281eae9d2baSRodolfo Giometti { 282eae9d2baSRodolfo Giometti int err; 283eae9d2baSRodolfo Giometti 284eae9d2baSRodolfo Giometti pps_class = class_create(THIS_MODULE, "pps"); 285eae9d2baSRodolfo Giometti if (!pps_class) { 286*7f7cce74SAlexander Gordeev pr_err("failed to allocate class\n"); 287eae9d2baSRodolfo Giometti return -ENOMEM; 288eae9d2baSRodolfo Giometti } 289eae9d2baSRodolfo Giometti pps_class->dev_attrs = pps_attrs; 290eae9d2baSRodolfo Giometti 291eae9d2baSRodolfo Giometti err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); 292eae9d2baSRodolfo Giometti if (err < 0) { 293*7f7cce74SAlexander Gordeev pr_err("failed to allocate char device region\n"); 294eae9d2baSRodolfo Giometti goto remove_class; 295eae9d2baSRodolfo Giometti } 296eae9d2baSRodolfo Giometti 297eae9d2baSRodolfo Giometti pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); 298eae9d2baSRodolfo Giometti pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " 299eae9d2baSRodolfo Giometti "<giometti@linux.it>\n", PPS_VERSION); 300eae9d2baSRodolfo Giometti 301eae9d2baSRodolfo Giometti return 0; 302eae9d2baSRodolfo Giometti 303eae9d2baSRodolfo Giometti remove_class: 304eae9d2baSRodolfo Giometti class_destroy(pps_class); 305eae9d2baSRodolfo Giometti 306eae9d2baSRodolfo Giometti return err; 307eae9d2baSRodolfo Giometti } 308eae9d2baSRodolfo Giometti 309eae9d2baSRodolfo Giometti subsys_initcall(pps_init); 310eae9d2baSRodolfo Giometti module_exit(pps_exit); 311eae9d2baSRodolfo Giometti 312eae9d2baSRodolfo Giometti MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 313eae9d2baSRodolfo Giometti MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); 314eae9d2baSRodolfo Giometti MODULE_LICENSE("GPL"); 315