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 227f7cce74SAlexander 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> 302a5cd6e2SAlexander Gordeev #include <linux/mutex.h> 31eae9d2baSRodolfo Giometti #include <linux/cdev.h> 32eae9d2baSRodolfo Giometti #include <linux/poll.h> 33eae9d2baSRodolfo Giometti #include <linux/pps_kernel.h> 34083e5866SAlexander Gordeev #include <linux/slab.h> 35eae9d2baSRodolfo Giometti 36717c0336SAlexander Gordeev #include "kc.h" 37717c0336SAlexander Gordeev 38eae9d2baSRodolfo Giometti /* 39eae9d2baSRodolfo Giometti * Local variables 40eae9d2baSRodolfo Giometti */ 41eae9d2baSRodolfo Giometti 42eae9d2baSRodolfo Giometti static dev_t pps_devt; 43eae9d2baSRodolfo Giometti static struct class *pps_class; 44eae9d2baSRodolfo Giometti 452a5cd6e2SAlexander Gordeev static DEFINE_MUTEX(pps_idr_lock); 46083e5866SAlexander Gordeev static DEFINE_IDR(pps_idr); 47083e5866SAlexander Gordeev 48eae9d2baSRodolfo Giometti /* 49eae9d2baSRodolfo Giometti * Char device methods 50eae9d2baSRodolfo Giometti */ 51eae9d2baSRodolfo Giometti 52eae9d2baSRodolfo Giometti static unsigned int pps_cdev_poll(struct file *file, poll_table *wait) 53eae9d2baSRodolfo Giometti { 54eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 55eae9d2baSRodolfo Giometti 56eae9d2baSRodolfo Giometti poll_wait(file, &pps->queue, wait); 57eae9d2baSRodolfo Giometti 58eae9d2baSRodolfo Giometti return POLLIN | POLLRDNORM; 59eae9d2baSRodolfo Giometti } 60eae9d2baSRodolfo Giometti 61eae9d2baSRodolfo Giometti static int pps_cdev_fasync(int fd, struct file *file, int on) 62eae9d2baSRodolfo Giometti { 63eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 64eae9d2baSRodolfo Giometti return fasync_helper(fd, file, on, &pps->async_queue); 65eae9d2baSRodolfo Giometti } 66eae9d2baSRodolfo Giometti 67eae9d2baSRodolfo Giometti static long pps_cdev_ioctl(struct file *file, 68eae9d2baSRodolfo Giometti unsigned int cmd, unsigned long arg) 69eae9d2baSRodolfo Giometti { 70eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 71eae9d2baSRodolfo Giometti struct pps_kparams params; 72eae9d2baSRodolfo Giometti void __user *uarg = (void __user *) arg; 73eae9d2baSRodolfo Giometti int __user *iuarg = (int __user *) arg; 74eae9d2baSRodolfo Giometti int err; 75eae9d2baSRodolfo Giometti 76eae9d2baSRodolfo Giometti switch (cmd) { 77eae9d2baSRodolfo Giometti case PPS_GETPARAMS: 787f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETPARAMS\n"); 79eae9d2baSRodolfo Giometti 80cbf83cc5SRodolfo Giometti spin_lock_irq(&pps->lock); 81cbf83cc5SRodolfo Giometti 82cbf83cc5SRodolfo Giometti /* Get the current parameters */ 83cbf83cc5SRodolfo Giometti params = pps->params; 84cbf83cc5SRodolfo Giometti 85cbf83cc5SRodolfo Giometti spin_unlock_irq(&pps->lock); 86cbf83cc5SRodolfo Giometti 87cbf83cc5SRodolfo Giometti err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams)); 88eae9d2baSRodolfo Giometti if (err) 89eae9d2baSRodolfo Giometti return -EFAULT; 90eae9d2baSRodolfo Giometti 91eae9d2baSRodolfo Giometti break; 92eae9d2baSRodolfo Giometti 93eae9d2baSRodolfo Giometti case PPS_SETPARAMS: 947f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_SETPARAMS\n"); 95eae9d2baSRodolfo Giometti 96eae9d2baSRodolfo Giometti /* Check the capabilities */ 97eae9d2baSRodolfo Giometti if (!capable(CAP_SYS_TIME)) 98eae9d2baSRodolfo Giometti return -EPERM; 99eae9d2baSRodolfo Giometti 100eae9d2baSRodolfo Giometti err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); 101eae9d2baSRodolfo Giometti if (err) 102eae9d2baSRodolfo Giometti return -EFAULT; 103eae9d2baSRodolfo Giometti if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { 1047f7cce74SAlexander Gordeev dev_dbg(pps->dev, "capture mode unspecified (%x)\n", 105eae9d2baSRodolfo Giometti params.mode); 106eae9d2baSRodolfo Giometti return -EINVAL; 107eae9d2baSRodolfo Giometti } 108eae9d2baSRodolfo Giometti 109eae9d2baSRodolfo Giometti /* Check for supported capabilities */ 110eae9d2baSRodolfo Giometti if ((params.mode & ~pps->info.mode) != 0) { 1117f7cce74SAlexander Gordeev dev_dbg(pps->dev, "unsupported capabilities (%x)\n", 112eae9d2baSRodolfo Giometti params.mode); 113eae9d2baSRodolfo Giometti return -EINVAL; 114eae9d2baSRodolfo Giometti } 115eae9d2baSRodolfo Giometti 116eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 117eae9d2baSRodolfo Giometti 118eae9d2baSRodolfo Giometti /* Save the new parameters */ 119eae9d2baSRodolfo Giometti pps->params = params; 120eae9d2baSRodolfo Giometti 121eae9d2baSRodolfo Giometti /* Restore the read only parameters */ 122eae9d2baSRodolfo Giometti if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { 123eae9d2baSRodolfo Giometti /* section 3.3 of RFC 2783 interpreted */ 1247f7cce74SAlexander Gordeev dev_dbg(pps->dev, "time format unspecified (%x)\n", 125eae9d2baSRodolfo Giometti params.mode); 126eae9d2baSRodolfo Giometti pps->params.mode |= PPS_TSFMT_TSPEC; 127eae9d2baSRodolfo Giometti } 128eae9d2baSRodolfo Giometti if (pps->info.mode & PPS_CANWAIT) 129eae9d2baSRodolfo Giometti pps->params.mode |= PPS_CANWAIT; 130eae9d2baSRodolfo Giometti pps->params.api_version = PPS_API_VERS; 131eae9d2baSRodolfo Giometti 132eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 133eae9d2baSRodolfo Giometti 134eae9d2baSRodolfo Giometti break; 135eae9d2baSRodolfo Giometti 136eae9d2baSRodolfo Giometti case PPS_GETCAP: 1377f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETCAP\n"); 138eae9d2baSRodolfo Giometti 139eae9d2baSRodolfo Giometti err = put_user(pps->info.mode, iuarg); 140eae9d2baSRodolfo Giometti if (err) 141eae9d2baSRodolfo Giometti return -EFAULT; 142eae9d2baSRodolfo Giometti 143eae9d2baSRodolfo Giometti break; 144eae9d2baSRodolfo Giometti 14586d921f9SAlexander Gordeev case PPS_FETCH: { 14686d921f9SAlexander Gordeev struct pps_fdata fdata; 1473003d55bSAlexander Gordeev unsigned int ev; 14886d921f9SAlexander Gordeev 1497f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_FETCH\n"); 150eae9d2baSRodolfo Giometti 151eae9d2baSRodolfo Giometti err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); 152eae9d2baSRodolfo Giometti if (err) 153eae9d2baSRodolfo Giometti return -EFAULT; 154eae9d2baSRodolfo Giometti 1553003d55bSAlexander Gordeev ev = pps->last_ev; 156eae9d2baSRodolfo Giometti 157eae9d2baSRodolfo Giometti /* Manage the timeout */ 158eae9d2baSRodolfo Giometti if (fdata.timeout.flags & PPS_TIME_INVALID) 1593003d55bSAlexander Gordeev err = wait_event_interruptible(pps->queue, 1603003d55bSAlexander Gordeev ev != pps->last_ev); 161eae9d2baSRodolfo Giometti else { 16286d921f9SAlexander Gordeev unsigned long ticks; 16386d921f9SAlexander Gordeev 1647f7cce74SAlexander Gordeev dev_dbg(pps->dev, "timeout %lld.%09d\n", 165eae9d2baSRodolfo Giometti (long long) fdata.timeout.sec, 166eae9d2baSRodolfo Giometti fdata.timeout.nsec); 167eae9d2baSRodolfo Giometti ticks = fdata.timeout.sec * HZ; 168eae9d2baSRodolfo Giometti ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ); 169eae9d2baSRodolfo Giometti 170eae9d2baSRodolfo Giometti if (ticks != 0) { 171eae9d2baSRodolfo Giometti err = wait_event_interruptible_timeout( 1723003d55bSAlexander Gordeev pps->queue, 1733003d55bSAlexander Gordeev ev != pps->last_ev, 1743003d55bSAlexander Gordeev ticks); 175eae9d2baSRodolfo Giometti if (err == 0) 176eae9d2baSRodolfo Giometti return -ETIMEDOUT; 177eae9d2baSRodolfo Giometti } 178eae9d2baSRodolfo Giometti } 179eae9d2baSRodolfo Giometti 180eae9d2baSRodolfo Giometti /* Check for pending signals */ 181eae9d2baSRodolfo Giometti if (err == -ERESTARTSYS) { 1827f7cce74SAlexander Gordeev dev_dbg(pps->dev, "pending signal caught\n"); 183eae9d2baSRodolfo Giometti return -EINTR; 184eae9d2baSRodolfo Giometti } 185eae9d2baSRodolfo Giometti 186eae9d2baSRodolfo Giometti /* Return the fetched timestamp */ 187eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 188eae9d2baSRodolfo Giometti 189eae9d2baSRodolfo Giometti fdata.info.assert_sequence = pps->assert_sequence; 190eae9d2baSRodolfo Giometti fdata.info.clear_sequence = pps->clear_sequence; 191eae9d2baSRodolfo Giometti fdata.info.assert_tu = pps->assert_tu; 192eae9d2baSRodolfo Giometti fdata.info.clear_tu = pps->clear_tu; 193eae9d2baSRodolfo Giometti fdata.info.current_mode = pps->current_mode; 194eae9d2baSRodolfo Giometti 195eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 196eae9d2baSRodolfo Giometti 197eae9d2baSRodolfo Giometti err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); 198eae9d2baSRodolfo Giometti if (err) 199eae9d2baSRodolfo Giometti return -EFAULT; 200eae9d2baSRodolfo Giometti 201eae9d2baSRodolfo Giometti break; 20286d921f9SAlexander Gordeev } 203717c0336SAlexander Gordeev case PPS_KC_BIND: { 204717c0336SAlexander Gordeev struct pps_bind_args bind_args; 205717c0336SAlexander Gordeev 206717c0336SAlexander Gordeev dev_dbg(pps->dev, "PPS_KC_BIND\n"); 207717c0336SAlexander Gordeev 208717c0336SAlexander Gordeev /* Check the capabilities */ 209717c0336SAlexander Gordeev if (!capable(CAP_SYS_TIME)) 210717c0336SAlexander Gordeev return -EPERM; 211717c0336SAlexander Gordeev 212717c0336SAlexander Gordeev if (copy_from_user(&bind_args, uarg, 213717c0336SAlexander Gordeev sizeof(struct pps_bind_args))) 214717c0336SAlexander Gordeev return -EFAULT; 215717c0336SAlexander Gordeev 216717c0336SAlexander Gordeev /* Check for supported capabilities */ 217717c0336SAlexander Gordeev if ((bind_args.edge & ~pps->info.mode) != 0) { 218717c0336SAlexander Gordeev dev_err(pps->dev, "unsupported capabilities (%x)\n", 219717c0336SAlexander Gordeev bind_args.edge); 220717c0336SAlexander Gordeev return -EINVAL; 221717c0336SAlexander Gordeev } 222717c0336SAlexander Gordeev 223717c0336SAlexander Gordeev /* Validate parameters roughly */ 224717c0336SAlexander Gordeev if (bind_args.tsformat != PPS_TSFMT_TSPEC || 225717c0336SAlexander Gordeev (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 || 226717c0336SAlexander Gordeev bind_args.consumer != PPS_KC_HARDPPS) { 227717c0336SAlexander Gordeev dev_err(pps->dev, "invalid kernel consumer bind" 228717c0336SAlexander Gordeev " parameters (%x)\n", bind_args.edge); 229717c0336SAlexander Gordeev return -EINVAL; 230717c0336SAlexander Gordeev } 231717c0336SAlexander Gordeev 232717c0336SAlexander Gordeev err = pps_kc_bind(pps, &bind_args); 233717c0336SAlexander Gordeev if (err < 0) 234717c0336SAlexander Gordeev return err; 235717c0336SAlexander Gordeev 236717c0336SAlexander Gordeev break; 237717c0336SAlexander Gordeev } 238eae9d2baSRodolfo Giometti default: 239eae9d2baSRodolfo Giometti return -ENOTTY; 240eae9d2baSRodolfo Giometti } 241eae9d2baSRodolfo Giometti 242eae9d2baSRodolfo Giometti return 0; 243eae9d2baSRodolfo Giometti } 244eae9d2baSRodolfo Giometti 245eae9d2baSRodolfo Giometti static int pps_cdev_open(struct inode *inode, struct file *file) 246eae9d2baSRodolfo Giometti { 247eae9d2baSRodolfo Giometti struct pps_device *pps = container_of(inode->i_cdev, 248eae9d2baSRodolfo Giometti struct pps_device, cdev); 249eae9d2baSRodolfo Giometti file->private_data = pps; 250d953e0e8SGeorge Spelvin kobject_get(&pps->dev->kobj); 251eae9d2baSRodolfo Giometti return 0; 252eae9d2baSRodolfo Giometti } 253eae9d2baSRodolfo Giometti 254eae9d2baSRodolfo Giometti static int pps_cdev_release(struct inode *inode, struct file *file) 255eae9d2baSRodolfo Giometti { 256d953e0e8SGeorge Spelvin struct pps_device *pps = container_of(inode->i_cdev, 257d953e0e8SGeorge Spelvin struct pps_device, cdev); 258d953e0e8SGeorge Spelvin kobject_put(&pps->dev->kobj); 259eae9d2baSRodolfo Giometti return 0; 260eae9d2baSRodolfo Giometti } 261eae9d2baSRodolfo Giometti 262eae9d2baSRodolfo Giometti /* 263eae9d2baSRodolfo Giometti * Char device stuff 264eae9d2baSRodolfo Giometti */ 265eae9d2baSRodolfo Giometti 266eae9d2baSRodolfo Giometti static const struct file_operations pps_cdev_fops = { 267eae9d2baSRodolfo Giometti .owner = THIS_MODULE, 268eae9d2baSRodolfo Giometti .llseek = no_llseek, 269eae9d2baSRodolfo Giometti .poll = pps_cdev_poll, 270eae9d2baSRodolfo Giometti .fasync = pps_cdev_fasync, 271eae9d2baSRodolfo Giometti .unlocked_ioctl = pps_cdev_ioctl, 272eae9d2baSRodolfo Giometti .open = pps_cdev_open, 273eae9d2baSRodolfo Giometti .release = pps_cdev_release, 274eae9d2baSRodolfo Giometti }; 275eae9d2baSRodolfo Giometti 276083e5866SAlexander Gordeev static void pps_device_destruct(struct device *dev) 277083e5866SAlexander Gordeev { 278083e5866SAlexander Gordeev struct pps_device *pps = dev_get_drvdata(dev); 279083e5866SAlexander Gordeev 280d953e0e8SGeorge Spelvin cdev_del(&pps->cdev); 281d953e0e8SGeorge Spelvin 282d953e0e8SGeorge Spelvin /* Now we can release the ID for re-use */ 283d953e0e8SGeorge Spelvin pr_debug("deallocating pps%d\n", pps->id); 2842a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 285083e5866SAlexander Gordeev idr_remove(&pps_idr, pps->id); 2862a5cd6e2SAlexander Gordeev mutex_unlock(&pps_idr_lock); 287083e5866SAlexander Gordeev 288083e5866SAlexander Gordeev kfree(dev); 289083e5866SAlexander Gordeev kfree(pps); 290083e5866SAlexander Gordeev } 291083e5866SAlexander Gordeev 292eae9d2baSRodolfo Giometti int pps_register_cdev(struct pps_device *pps) 293eae9d2baSRodolfo Giometti { 294eae9d2baSRodolfo Giometti int err; 2955e196d34SAlexander Gordeev dev_t devt; 296eae9d2baSRodolfo Giometti 2972a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 298*19dd2da3STejun Heo /* 299*19dd2da3STejun Heo * Get new ID for the new PPS source. After idr_alloc() calling 300*19dd2da3STejun Heo * the new source will be freely available into the kernel. 301083e5866SAlexander Gordeev */ 302*19dd2da3STejun Heo err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL); 303*19dd2da3STejun Heo if (err < 0) { 304*19dd2da3STejun Heo if (err == -ENOSPC) { 305083e5866SAlexander Gordeev pr_err("%s: too many PPS sources in the system\n", 306083e5866SAlexander Gordeev pps->info.name); 307083e5866SAlexander Gordeev err = -EBUSY; 308083e5866SAlexander Gordeev } 309*19dd2da3STejun Heo goto out_unlock; 310*19dd2da3STejun Heo } 311*19dd2da3STejun Heo pps->id = err; 312*19dd2da3STejun Heo mutex_unlock(&pps_idr_lock); 313083e5866SAlexander Gordeev 3145e196d34SAlexander Gordeev devt = MKDEV(MAJOR(pps_devt), pps->id); 3155e196d34SAlexander Gordeev 316eae9d2baSRodolfo Giometti cdev_init(&pps->cdev, &pps_cdev_fops); 317eae9d2baSRodolfo Giometti pps->cdev.owner = pps->info.owner; 318eae9d2baSRodolfo Giometti 3195e196d34SAlexander Gordeev err = cdev_add(&pps->cdev, devt, 1); 320eae9d2baSRodolfo Giometti if (err) { 3217f7cce74SAlexander Gordeev pr_err("%s: failed to add char device %d:%d\n", 322eae9d2baSRodolfo Giometti pps->info.name, MAJOR(pps_devt), pps->id); 323083e5866SAlexander Gordeev goto free_idr; 324eae9d2baSRodolfo Giometti } 3255e196d34SAlexander Gordeev pps->dev = device_create(pps_class, pps->info.dev, devt, pps, 326eae9d2baSRodolfo Giometti "pps%d", pps->id); 327668f06b9SEmil Goode if (IS_ERR(pps->dev)) { 328668f06b9SEmil Goode err = PTR_ERR(pps->dev); 329eae9d2baSRodolfo Giometti goto del_cdev; 330668f06b9SEmil Goode } 331eae9d2baSRodolfo Giometti 332d953e0e8SGeorge Spelvin /* Override the release function with our own */ 333083e5866SAlexander Gordeev pps->dev->release = pps_device_destruct; 334083e5866SAlexander Gordeev 335eae9d2baSRodolfo Giometti pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, 336eae9d2baSRodolfo Giometti MAJOR(pps_devt), pps->id); 337eae9d2baSRodolfo Giometti 338eae9d2baSRodolfo Giometti return 0; 339eae9d2baSRodolfo Giometti 340eae9d2baSRodolfo Giometti del_cdev: 341eae9d2baSRodolfo Giometti cdev_del(&pps->cdev); 342eae9d2baSRodolfo Giometti 343083e5866SAlexander Gordeev free_idr: 3442a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 345083e5866SAlexander Gordeev idr_remove(&pps_idr, pps->id); 346*19dd2da3STejun Heo out_unlock: 3472a5cd6e2SAlexander Gordeev mutex_unlock(&pps_idr_lock); 348eae9d2baSRodolfo Giometti return err; 349eae9d2baSRodolfo Giometti } 350eae9d2baSRodolfo Giometti 351eae9d2baSRodolfo Giometti void pps_unregister_cdev(struct pps_device *pps) 352eae9d2baSRodolfo Giometti { 353d953e0e8SGeorge Spelvin pr_debug("unregistering pps%d\n", pps->id); 354513b032cSGeorge Spelvin pps->lookup_cookie = NULL; 3555e196d34SAlexander Gordeev device_destroy(pps_class, pps->dev->devt); 356eae9d2baSRodolfo Giometti } 357eae9d2baSRodolfo Giometti 358eae9d2baSRodolfo Giometti /* 359513b032cSGeorge Spelvin * Look up a pps device by magic cookie. 360513b032cSGeorge Spelvin * The cookie is usually a pointer to some enclosing device, but this 361513b032cSGeorge Spelvin * code doesn't care; you should never be dereferencing it. 362513b032cSGeorge Spelvin * 363513b032cSGeorge Spelvin * This is a bit of a kludge that is currently used only by the PPS 364513b032cSGeorge Spelvin * serial line discipline. It may need to be tweaked when a second user 365513b032cSGeorge Spelvin * is found. 366513b032cSGeorge Spelvin * 367513b032cSGeorge Spelvin * There is no function interface for setting the lookup_cookie field. 368513b032cSGeorge Spelvin * It's initialized to NULL when the pps device is created, and if a 369513b032cSGeorge Spelvin * client wants to use it, just fill it in afterward. 370513b032cSGeorge Spelvin * 371513b032cSGeorge Spelvin * The cookie is automatically set to NULL in pps_unregister_source() 372513b032cSGeorge Spelvin * so that it will not be used again, even if the pps device cannot 373513b032cSGeorge Spelvin * be removed from the idr due to pending references holding the minor 374513b032cSGeorge Spelvin * number in use. 375513b032cSGeorge Spelvin */ 376513b032cSGeorge Spelvin struct pps_device *pps_lookup_dev(void const *cookie) 377513b032cSGeorge Spelvin { 378513b032cSGeorge Spelvin struct pps_device *pps; 379513b032cSGeorge Spelvin unsigned id; 380513b032cSGeorge Spelvin 381513b032cSGeorge Spelvin rcu_read_lock(); 382513b032cSGeorge Spelvin idr_for_each_entry(&pps_idr, pps, id) 383513b032cSGeorge Spelvin if (cookie == pps->lookup_cookie) 384513b032cSGeorge Spelvin break; 385513b032cSGeorge Spelvin rcu_read_unlock(); 386513b032cSGeorge Spelvin return pps; 387513b032cSGeorge Spelvin } 388513b032cSGeorge Spelvin EXPORT_SYMBOL(pps_lookup_dev); 389513b032cSGeorge Spelvin 390513b032cSGeorge Spelvin /* 391eae9d2baSRodolfo Giometti * Module stuff 392eae9d2baSRodolfo Giometti */ 393eae9d2baSRodolfo Giometti 394eae9d2baSRodolfo Giometti static void __exit pps_exit(void) 395eae9d2baSRodolfo Giometti { 396eae9d2baSRodolfo Giometti class_destroy(pps_class); 397eae9d2baSRodolfo Giometti unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); 398eae9d2baSRodolfo Giometti } 399eae9d2baSRodolfo Giometti 400eae9d2baSRodolfo Giometti static int __init pps_init(void) 401eae9d2baSRodolfo Giometti { 402eae9d2baSRodolfo Giometti int err; 403eae9d2baSRodolfo Giometti 404eae9d2baSRodolfo Giometti pps_class = class_create(THIS_MODULE, "pps"); 4057ad12566SDan Carpenter if (IS_ERR(pps_class)) { 4067f7cce74SAlexander Gordeev pr_err("failed to allocate class\n"); 4077ad12566SDan Carpenter return PTR_ERR(pps_class); 408eae9d2baSRodolfo Giometti } 409eae9d2baSRodolfo Giometti pps_class->dev_attrs = pps_attrs; 410eae9d2baSRodolfo Giometti 411eae9d2baSRodolfo Giometti err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); 412eae9d2baSRodolfo Giometti if (err < 0) { 4137f7cce74SAlexander Gordeev pr_err("failed to allocate char device region\n"); 414eae9d2baSRodolfo Giometti goto remove_class; 415eae9d2baSRodolfo Giometti } 416eae9d2baSRodolfo Giometti 417eae9d2baSRodolfo Giometti pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); 418eae9d2baSRodolfo Giometti pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " 419eae9d2baSRodolfo Giometti "<giometti@linux.it>\n", PPS_VERSION); 420eae9d2baSRodolfo Giometti 421eae9d2baSRodolfo Giometti return 0; 422eae9d2baSRodolfo Giometti 423eae9d2baSRodolfo Giometti remove_class: 424eae9d2baSRodolfo Giometti class_destroy(pps_class); 425eae9d2baSRodolfo Giometti 426eae9d2baSRodolfo Giometti return err; 427eae9d2baSRodolfo Giometti } 428eae9d2baSRodolfo Giometti 429eae9d2baSRodolfo Giometti subsys_initcall(pps_init); 430eae9d2baSRodolfo Giometti module_exit(pps_exit); 431eae9d2baSRodolfo Giometti 432eae9d2baSRodolfo Giometti MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 433eae9d2baSRodolfo Giometti MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); 434eae9d2baSRodolfo Giometti MODULE_LICENSE("GPL"); 435