1*74ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2eae9d2baSRodolfo Giometti /* 3eae9d2baSRodolfo Giometti * PPS core file 4eae9d2baSRodolfo Giometti * 5eae9d2baSRodolfo Giometti * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it> 6eae9d2baSRodolfo Giometti */ 7eae9d2baSRodolfo Giometti 87f7cce74SAlexander Gordeev #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9eae9d2baSRodolfo Giometti 10eae9d2baSRodolfo Giometti #include <linux/kernel.h> 11eae9d2baSRodolfo Giometti #include <linux/module.h> 12eae9d2baSRodolfo Giometti #include <linux/init.h> 13eae9d2baSRodolfo Giometti #include <linux/sched.h> 14eae9d2baSRodolfo Giometti #include <linux/uaccess.h> 15eae9d2baSRodolfo Giometti #include <linux/idr.h> 162a5cd6e2SAlexander Gordeev #include <linux/mutex.h> 17eae9d2baSRodolfo Giometti #include <linux/cdev.h> 18eae9d2baSRodolfo Giometti #include <linux/poll.h> 19eae9d2baSRodolfo Giometti #include <linux/pps_kernel.h> 20083e5866SAlexander Gordeev #include <linux/slab.h> 21eae9d2baSRodolfo Giometti 22717c0336SAlexander Gordeev #include "kc.h" 23717c0336SAlexander Gordeev 24eae9d2baSRodolfo Giometti /* 25eae9d2baSRodolfo Giometti * Local variables 26eae9d2baSRodolfo Giometti */ 27eae9d2baSRodolfo Giometti 28eae9d2baSRodolfo Giometti static dev_t pps_devt; 29eae9d2baSRodolfo Giometti static struct class *pps_class; 30eae9d2baSRodolfo Giometti 312a5cd6e2SAlexander Gordeev static DEFINE_MUTEX(pps_idr_lock); 32083e5866SAlexander Gordeev static DEFINE_IDR(pps_idr); 33083e5866SAlexander Gordeev 34eae9d2baSRodolfo Giometti /* 35eae9d2baSRodolfo Giometti * Char device methods 36eae9d2baSRodolfo Giometti */ 37eae9d2baSRodolfo Giometti 38afc9a42bSAl Viro static __poll_t pps_cdev_poll(struct file *file, poll_table *wait) 39eae9d2baSRodolfo Giometti { 40eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 41eae9d2baSRodolfo Giometti 42eae9d2baSRodolfo Giometti poll_wait(file, &pps->queue, wait); 43eae9d2baSRodolfo Giometti 44a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM; 45eae9d2baSRodolfo Giometti } 46eae9d2baSRodolfo Giometti 47eae9d2baSRodolfo Giometti static int pps_cdev_fasync(int fd, struct file *file, int on) 48eae9d2baSRodolfo Giometti { 49eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 50eae9d2baSRodolfo Giometti return fasync_helper(fd, file, on, &pps->async_queue); 51eae9d2baSRodolfo Giometti } 52eae9d2baSRodolfo Giometti 53c2a49fe8SMatt Ranostay static int pps_cdev_pps_fetch(struct pps_device *pps, struct pps_fdata *fdata) 54c2a49fe8SMatt Ranostay { 55c2a49fe8SMatt Ranostay unsigned int ev = pps->last_ev; 56c2a49fe8SMatt Ranostay int err = 0; 57c2a49fe8SMatt Ranostay 58c2a49fe8SMatt Ranostay /* Manage the timeout */ 59c2a49fe8SMatt Ranostay if (fdata->timeout.flags & PPS_TIME_INVALID) 60c2a49fe8SMatt Ranostay err = wait_event_interruptible(pps->queue, 61c2a49fe8SMatt Ranostay ev != pps->last_ev); 62c2a49fe8SMatt Ranostay else { 63c2a49fe8SMatt Ranostay unsigned long ticks; 64c2a49fe8SMatt Ranostay 65c2a49fe8SMatt Ranostay dev_dbg(pps->dev, "timeout %lld.%09d\n", 66c2a49fe8SMatt Ranostay (long long) fdata->timeout.sec, 67c2a49fe8SMatt Ranostay fdata->timeout.nsec); 68c2a49fe8SMatt Ranostay ticks = fdata->timeout.sec * HZ; 69c2a49fe8SMatt Ranostay ticks += fdata->timeout.nsec / (NSEC_PER_SEC / HZ); 70c2a49fe8SMatt Ranostay 71c2a49fe8SMatt Ranostay if (ticks != 0) { 72c2a49fe8SMatt Ranostay err = wait_event_interruptible_timeout( 73c2a49fe8SMatt Ranostay pps->queue, 74c2a49fe8SMatt Ranostay ev != pps->last_ev, 75c2a49fe8SMatt Ranostay ticks); 76c2a49fe8SMatt Ranostay if (err == 0) 77c2a49fe8SMatt Ranostay return -ETIMEDOUT; 78c2a49fe8SMatt Ranostay } 79c2a49fe8SMatt Ranostay } 80c2a49fe8SMatt Ranostay 81c2a49fe8SMatt Ranostay /* Check for pending signals */ 82c2a49fe8SMatt Ranostay if (err == -ERESTARTSYS) { 83c2a49fe8SMatt Ranostay dev_dbg(pps->dev, "pending signal caught\n"); 84c2a49fe8SMatt Ranostay return -EINTR; 85c2a49fe8SMatt Ranostay } 86c2a49fe8SMatt Ranostay 87c2a49fe8SMatt Ranostay return 0; 88c2a49fe8SMatt Ranostay } 89c2a49fe8SMatt Ranostay 90eae9d2baSRodolfo Giometti static long pps_cdev_ioctl(struct file *file, 91eae9d2baSRodolfo Giometti unsigned int cmd, unsigned long arg) 92eae9d2baSRodolfo Giometti { 93eae9d2baSRodolfo Giometti struct pps_device *pps = file->private_data; 94eae9d2baSRodolfo Giometti struct pps_kparams params; 95eae9d2baSRodolfo Giometti void __user *uarg = (void __user *) arg; 96eae9d2baSRodolfo Giometti int __user *iuarg = (int __user *) arg; 97eae9d2baSRodolfo Giometti int err; 98eae9d2baSRodolfo Giometti 99eae9d2baSRodolfo Giometti switch (cmd) { 100eae9d2baSRodolfo Giometti case PPS_GETPARAMS: 1017f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETPARAMS\n"); 102eae9d2baSRodolfo Giometti 103cbf83cc5SRodolfo Giometti spin_lock_irq(&pps->lock); 104cbf83cc5SRodolfo Giometti 105cbf83cc5SRodolfo Giometti /* Get the current parameters */ 106cbf83cc5SRodolfo Giometti params = pps->params; 107cbf83cc5SRodolfo Giometti 108cbf83cc5SRodolfo Giometti spin_unlock_irq(&pps->lock); 109cbf83cc5SRodolfo Giometti 110cbf83cc5SRodolfo Giometti err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams)); 111eae9d2baSRodolfo Giometti if (err) 112eae9d2baSRodolfo Giometti return -EFAULT; 113eae9d2baSRodolfo Giometti 114eae9d2baSRodolfo Giometti break; 115eae9d2baSRodolfo Giometti 116eae9d2baSRodolfo Giometti case PPS_SETPARAMS: 1177f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_SETPARAMS\n"); 118eae9d2baSRodolfo Giometti 119eae9d2baSRodolfo Giometti /* Check the capabilities */ 120eae9d2baSRodolfo Giometti if (!capable(CAP_SYS_TIME)) 121eae9d2baSRodolfo Giometti return -EPERM; 122eae9d2baSRodolfo Giometti 123eae9d2baSRodolfo Giometti err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); 124eae9d2baSRodolfo Giometti if (err) 125eae9d2baSRodolfo Giometti return -EFAULT; 126eae9d2baSRodolfo Giometti if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { 1277f7cce74SAlexander Gordeev dev_dbg(pps->dev, "capture mode unspecified (%x)\n", 128eae9d2baSRodolfo Giometti params.mode); 129eae9d2baSRodolfo Giometti return -EINVAL; 130eae9d2baSRodolfo Giometti } 131eae9d2baSRodolfo Giometti 132eae9d2baSRodolfo Giometti /* Check for supported capabilities */ 133eae9d2baSRodolfo Giometti if ((params.mode & ~pps->info.mode) != 0) { 1347f7cce74SAlexander Gordeev dev_dbg(pps->dev, "unsupported capabilities (%x)\n", 135eae9d2baSRodolfo Giometti params.mode); 136eae9d2baSRodolfo Giometti return -EINVAL; 137eae9d2baSRodolfo Giometti } 138eae9d2baSRodolfo Giometti 139eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 140eae9d2baSRodolfo Giometti 141eae9d2baSRodolfo Giometti /* Save the new parameters */ 142eae9d2baSRodolfo Giometti pps->params = params; 143eae9d2baSRodolfo Giometti 144eae9d2baSRodolfo Giometti /* Restore the read only parameters */ 145eae9d2baSRodolfo Giometti if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { 146eae9d2baSRodolfo Giometti /* section 3.3 of RFC 2783 interpreted */ 1477f7cce74SAlexander Gordeev dev_dbg(pps->dev, "time format unspecified (%x)\n", 148eae9d2baSRodolfo Giometti params.mode); 149eae9d2baSRodolfo Giometti pps->params.mode |= PPS_TSFMT_TSPEC; 150eae9d2baSRodolfo Giometti } 151eae9d2baSRodolfo Giometti if (pps->info.mode & PPS_CANWAIT) 152eae9d2baSRodolfo Giometti pps->params.mode |= PPS_CANWAIT; 153eae9d2baSRodolfo Giometti pps->params.api_version = PPS_API_VERS; 154eae9d2baSRodolfo Giometti 155eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 156eae9d2baSRodolfo Giometti 157eae9d2baSRodolfo Giometti break; 158eae9d2baSRodolfo Giometti 159eae9d2baSRodolfo Giometti case PPS_GETCAP: 1607f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_GETCAP\n"); 161eae9d2baSRodolfo Giometti 162eae9d2baSRodolfo Giometti err = put_user(pps->info.mode, iuarg); 163eae9d2baSRodolfo Giometti if (err) 164eae9d2baSRodolfo Giometti return -EFAULT; 165eae9d2baSRodolfo Giometti 166eae9d2baSRodolfo Giometti break; 167eae9d2baSRodolfo Giometti 16886d921f9SAlexander Gordeev case PPS_FETCH: { 16986d921f9SAlexander Gordeev struct pps_fdata fdata; 17086d921f9SAlexander Gordeev 1717f7cce74SAlexander Gordeev dev_dbg(pps->dev, "PPS_FETCH\n"); 172eae9d2baSRodolfo Giometti 173eae9d2baSRodolfo Giometti err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); 174eae9d2baSRodolfo Giometti if (err) 175eae9d2baSRodolfo Giometti return -EFAULT; 176eae9d2baSRodolfo Giometti 177c2a49fe8SMatt Ranostay err = pps_cdev_pps_fetch(pps, &fdata); 178c2a49fe8SMatt Ranostay if (err) 179c2a49fe8SMatt Ranostay return err; 180eae9d2baSRodolfo Giometti 181eae9d2baSRodolfo Giometti /* Return the fetched timestamp */ 182eae9d2baSRodolfo Giometti spin_lock_irq(&pps->lock); 183eae9d2baSRodolfo Giometti 184eae9d2baSRodolfo Giometti fdata.info.assert_sequence = pps->assert_sequence; 185eae9d2baSRodolfo Giometti fdata.info.clear_sequence = pps->clear_sequence; 186eae9d2baSRodolfo Giometti fdata.info.assert_tu = pps->assert_tu; 187eae9d2baSRodolfo Giometti fdata.info.clear_tu = pps->clear_tu; 188eae9d2baSRodolfo Giometti fdata.info.current_mode = pps->current_mode; 189eae9d2baSRodolfo Giometti 190eae9d2baSRodolfo Giometti spin_unlock_irq(&pps->lock); 191eae9d2baSRodolfo Giometti 192eae9d2baSRodolfo Giometti err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); 193eae9d2baSRodolfo Giometti if (err) 194eae9d2baSRodolfo Giometti return -EFAULT; 195eae9d2baSRodolfo Giometti 196eae9d2baSRodolfo Giometti break; 19786d921f9SAlexander Gordeev } 198717c0336SAlexander Gordeev case PPS_KC_BIND: { 199717c0336SAlexander Gordeev struct pps_bind_args bind_args; 200717c0336SAlexander Gordeev 201717c0336SAlexander Gordeev dev_dbg(pps->dev, "PPS_KC_BIND\n"); 202717c0336SAlexander Gordeev 203717c0336SAlexander Gordeev /* Check the capabilities */ 204717c0336SAlexander Gordeev if (!capable(CAP_SYS_TIME)) 205717c0336SAlexander Gordeev return -EPERM; 206717c0336SAlexander Gordeev 207717c0336SAlexander Gordeev if (copy_from_user(&bind_args, uarg, 208717c0336SAlexander Gordeev sizeof(struct pps_bind_args))) 209717c0336SAlexander Gordeev return -EFAULT; 210717c0336SAlexander Gordeev 211717c0336SAlexander Gordeev /* Check for supported capabilities */ 212717c0336SAlexander Gordeev if ((bind_args.edge & ~pps->info.mode) != 0) { 213717c0336SAlexander Gordeev dev_err(pps->dev, "unsupported capabilities (%x)\n", 214717c0336SAlexander Gordeev bind_args.edge); 215717c0336SAlexander Gordeev return -EINVAL; 216717c0336SAlexander Gordeev } 217717c0336SAlexander Gordeev 218717c0336SAlexander Gordeev /* Validate parameters roughly */ 219717c0336SAlexander Gordeev if (bind_args.tsformat != PPS_TSFMT_TSPEC || 220717c0336SAlexander Gordeev (bind_args.edge & ~PPS_CAPTUREBOTH) != 0 || 221717c0336SAlexander Gordeev bind_args.consumer != PPS_KC_HARDPPS) { 222717c0336SAlexander Gordeev dev_err(pps->dev, "invalid kernel consumer bind" 223717c0336SAlexander Gordeev " parameters (%x)\n", bind_args.edge); 224717c0336SAlexander Gordeev return -EINVAL; 225717c0336SAlexander Gordeev } 226717c0336SAlexander Gordeev 227717c0336SAlexander Gordeev err = pps_kc_bind(pps, &bind_args); 228717c0336SAlexander Gordeev if (err < 0) 229717c0336SAlexander Gordeev return err; 230717c0336SAlexander Gordeev 231717c0336SAlexander Gordeev break; 232717c0336SAlexander Gordeev } 233eae9d2baSRodolfo Giometti default: 234eae9d2baSRodolfo Giometti return -ENOTTY; 235eae9d2baSRodolfo Giometti } 236eae9d2baSRodolfo Giometti 237eae9d2baSRodolfo Giometti return 0; 238eae9d2baSRodolfo Giometti } 239eae9d2baSRodolfo Giometti 2402ac66657SMatt Ranostay #ifdef CONFIG_COMPAT 2412ac66657SMatt Ranostay static long pps_cdev_compat_ioctl(struct file *file, 2422ac66657SMatt Ranostay unsigned int cmd, unsigned long arg) 2432ac66657SMatt Ranostay { 244c2a49fe8SMatt Ranostay struct pps_device *pps = file->private_data; 245c2a49fe8SMatt Ranostay void __user *uarg = (void __user *) arg; 246c2a49fe8SMatt Ranostay 2472ac66657SMatt Ranostay cmd = _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(void *)); 2482ac66657SMatt Ranostay 249c2a49fe8SMatt Ranostay if (cmd == PPS_FETCH) { 250c2a49fe8SMatt Ranostay struct pps_fdata_compat compat; 251c2a49fe8SMatt Ranostay struct pps_fdata fdata; 252c2a49fe8SMatt Ranostay int err; 253c2a49fe8SMatt Ranostay 254c2a49fe8SMatt Ranostay dev_dbg(pps->dev, "PPS_FETCH\n"); 255c2a49fe8SMatt Ranostay 256c2a49fe8SMatt Ranostay err = copy_from_user(&compat, uarg, sizeof(struct pps_fdata_compat)); 257c2a49fe8SMatt Ranostay if (err) 258c2a49fe8SMatt Ranostay return -EFAULT; 259c2a49fe8SMatt Ranostay 260c2a49fe8SMatt Ranostay memcpy(&fdata.timeout, &compat.timeout, 261c2a49fe8SMatt Ranostay sizeof(struct pps_ktime_compat)); 262c2a49fe8SMatt Ranostay 263c2a49fe8SMatt Ranostay err = pps_cdev_pps_fetch(pps, &fdata); 264c2a49fe8SMatt Ranostay if (err) 265c2a49fe8SMatt Ranostay return err; 266c2a49fe8SMatt Ranostay 267c2a49fe8SMatt Ranostay /* Return the fetched timestamp */ 268c2a49fe8SMatt Ranostay spin_lock_irq(&pps->lock); 269c2a49fe8SMatt Ranostay 270c2a49fe8SMatt Ranostay compat.info.assert_sequence = pps->assert_sequence; 271c2a49fe8SMatt Ranostay compat.info.clear_sequence = pps->clear_sequence; 272c2a49fe8SMatt Ranostay compat.info.current_mode = pps->current_mode; 273c2a49fe8SMatt Ranostay 274c2a49fe8SMatt Ranostay memcpy(&compat.info.assert_tu, &pps->assert_tu, 275c2a49fe8SMatt Ranostay sizeof(struct pps_ktime_compat)); 276c2a49fe8SMatt Ranostay memcpy(&compat.info.clear_tu, &pps->clear_tu, 277c2a49fe8SMatt Ranostay sizeof(struct pps_ktime_compat)); 278c2a49fe8SMatt Ranostay 279c2a49fe8SMatt Ranostay spin_unlock_irq(&pps->lock); 280c2a49fe8SMatt Ranostay 281c2a49fe8SMatt Ranostay return copy_to_user(uarg, &compat, 282c2a49fe8SMatt Ranostay sizeof(struct pps_fdata_compat)) ? -EFAULT : 0; 283c2a49fe8SMatt Ranostay } 284c2a49fe8SMatt Ranostay 2852ac66657SMatt Ranostay return pps_cdev_ioctl(file, cmd, arg); 2862ac66657SMatt Ranostay } 2872ac66657SMatt Ranostay #else 2882ac66657SMatt Ranostay #define pps_cdev_compat_ioctl NULL 2892ac66657SMatt Ranostay #endif 2902ac66657SMatt Ranostay 291eae9d2baSRodolfo Giometti static int pps_cdev_open(struct inode *inode, struct file *file) 292eae9d2baSRodolfo Giometti { 293eae9d2baSRodolfo Giometti struct pps_device *pps = container_of(inode->i_cdev, 294eae9d2baSRodolfo Giometti struct pps_device, cdev); 295eae9d2baSRodolfo Giometti file->private_data = pps; 296d953e0e8SGeorge Spelvin kobject_get(&pps->dev->kobj); 297eae9d2baSRodolfo Giometti return 0; 298eae9d2baSRodolfo Giometti } 299eae9d2baSRodolfo Giometti 300eae9d2baSRodolfo Giometti static int pps_cdev_release(struct inode *inode, struct file *file) 301eae9d2baSRodolfo Giometti { 302d953e0e8SGeorge Spelvin struct pps_device *pps = container_of(inode->i_cdev, 303d953e0e8SGeorge Spelvin struct pps_device, cdev); 304d953e0e8SGeorge Spelvin kobject_put(&pps->dev->kobj); 305eae9d2baSRodolfo Giometti return 0; 306eae9d2baSRodolfo Giometti } 307eae9d2baSRodolfo Giometti 308eae9d2baSRodolfo Giometti /* 309eae9d2baSRodolfo Giometti * Char device stuff 310eae9d2baSRodolfo Giometti */ 311eae9d2baSRodolfo Giometti 312eae9d2baSRodolfo Giometti static const struct file_operations pps_cdev_fops = { 313eae9d2baSRodolfo Giometti .owner = THIS_MODULE, 314eae9d2baSRodolfo Giometti .llseek = no_llseek, 315eae9d2baSRodolfo Giometti .poll = pps_cdev_poll, 316eae9d2baSRodolfo Giometti .fasync = pps_cdev_fasync, 3172ac66657SMatt Ranostay .compat_ioctl = pps_cdev_compat_ioctl, 318eae9d2baSRodolfo Giometti .unlocked_ioctl = pps_cdev_ioctl, 319eae9d2baSRodolfo Giometti .open = pps_cdev_open, 320eae9d2baSRodolfo Giometti .release = pps_cdev_release, 321eae9d2baSRodolfo Giometti }; 322eae9d2baSRodolfo Giometti 323083e5866SAlexander Gordeev static void pps_device_destruct(struct device *dev) 324083e5866SAlexander Gordeev { 325083e5866SAlexander Gordeev struct pps_device *pps = dev_get_drvdata(dev); 326083e5866SAlexander Gordeev 327d953e0e8SGeorge Spelvin cdev_del(&pps->cdev); 328d953e0e8SGeorge Spelvin 329d953e0e8SGeorge Spelvin /* Now we can release the ID for re-use */ 330d953e0e8SGeorge Spelvin pr_debug("deallocating pps%d\n", pps->id); 3312a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 332083e5866SAlexander Gordeev idr_remove(&pps_idr, pps->id); 3332a5cd6e2SAlexander Gordeev mutex_unlock(&pps_idr_lock); 334083e5866SAlexander Gordeev 335083e5866SAlexander Gordeev kfree(dev); 336083e5866SAlexander Gordeev kfree(pps); 337083e5866SAlexander Gordeev } 338083e5866SAlexander Gordeev 339eae9d2baSRodolfo Giometti int pps_register_cdev(struct pps_device *pps) 340eae9d2baSRodolfo Giometti { 341eae9d2baSRodolfo Giometti int err; 3425e196d34SAlexander Gordeev dev_t devt; 343eae9d2baSRodolfo Giometti 3442a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 34519dd2da3STejun Heo /* 34619dd2da3STejun Heo * Get new ID for the new PPS source. After idr_alloc() calling 34719dd2da3STejun Heo * the new source will be freely available into the kernel. 348083e5866SAlexander Gordeev */ 34919dd2da3STejun Heo err = idr_alloc(&pps_idr, pps, 0, PPS_MAX_SOURCES, GFP_KERNEL); 35019dd2da3STejun Heo if (err < 0) { 35119dd2da3STejun Heo if (err == -ENOSPC) { 352083e5866SAlexander Gordeev pr_err("%s: too many PPS sources in the system\n", 353083e5866SAlexander Gordeev pps->info.name); 354083e5866SAlexander Gordeev err = -EBUSY; 355083e5866SAlexander Gordeev } 35619dd2da3STejun Heo goto out_unlock; 35719dd2da3STejun Heo } 35819dd2da3STejun Heo pps->id = err; 35919dd2da3STejun Heo mutex_unlock(&pps_idr_lock); 360083e5866SAlexander Gordeev 3615e196d34SAlexander Gordeev devt = MKDEV(MAJOR(pps_devt), pps->id); 3625e196d34SAlexander Gordeev 363eae9d2baSRodolfo Giometti cdev_init(&pps->cdev, &pps_cdev_fops); 364eae9d2baSRodolfo Giometti pps->cdev.owner = pps->info.owner; 365eae9d2baSRodolfo Giometti 3665e196d34SAlexander Gordeev err = cdev_add(&pps->cdev, devt, 1); 367eae9d2baSRodolfo Giometti if (err) { 3687f7cce74SAlexander Gordeev pr_err("%s: failed to add char device %d:%d\n", 369eae9d2baSRodolfo Giometti pps->info.name, MAJOR(pps_devt), pps->id); 370083e5866SAlexander Gordeev goto free_idr; 371eae9d2baSRodolfo Giometti } 3725e196d34SAlexander Gordeev pps->dev = device_create(pps_class, pps->info.dev, devt, pps, 373eae9d2baSRodolfo Giometti "pps%d", pps->id); 374668f06b9SEmil Goode if (IS_ERR(pps->dev)) { 375668f06b9SEmil Goode err = PTR_ERR(pps->dev); 376eae9d2baSRodolfo Giometti goto del_cdev; 377668f06b9SEmil Goode } 378eae9d2baSRodolfo Giometti 379d953e0e8SGeorge Spelvin /* Override the release function with our own */ 380083e5866SAlexander Gordeev pps->dev->release = pps_device_destruct; 381083e5866SAlexander Gordeev 382eae9d2baSRodolfo Giometti pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, 383eae9d2baSRodolfo Giometti MAJOR(pps_devt), pps->id); 384eae9d2baSRodolfo Giometti 385eae9d2baSRodolfo Giometti return 0; 386eae9d2baSRodolfo Giometti 387eae9d2baSRodolfo Giometti del_cdev: 388eae9d2baSRodolfo Giometti cdev_del(&pps->cdev); 389eae9d2baSRodolfo Giometti 390083e5866SAlexander Gordeev free_idr: 3912a5cd6e2SAlexander Gordeev mutex_lock(&pps_idr_lock); 392083e5866SAlexander Gordeev idr_remove(&pps_idr, pps->id); 39319dd2da3STejun Heo out_unlock: 3942a5cd6e2SAlexander Gordeev mutex_unlock(&pps_idr_lock); 395eae9d2baSRodolfo Giometti return err; 396eae9d2baSRodolfo Giometti } 397eae9d2baSRodolfo Giometti 398eae9d2baSRodolfo Giometti void pps_unregister_cdev(struct pps_device *pps) 399eae9d2baSRodolfo Giometti { 400d953e0e8SGeorge Spelvin pr_debug("unregistering pps%d\n", pps->id); 401513b032cSGeorge Spelvin pps->lookup_cookie = NULL; 4025e196d34SAlexander Gordeev device_destroy(pps_class, pps->dev->devt); 403eae9d2baSRodolfo Giometti } 404eae9d2baSRodolfo Giometti 405eae9d2baSRodolfo Giometti /* 406513b032cSGeorge Spelvin * Look up a pps device by magic cookie. 407513b032cSGeorge Spelvin * The cookie is usually a pointer to some enclosing device, but this 408513b032cSGeorge Spelvin * code doesn't care; you should never be dereferencing it. 409513b032cSGeorge Spelvin * 410513b032cSGeorge Spelvin * This is a bit of a kludge that is currently used only by the PPS 411513b032cSGeorge Spelvin * serial line discipline. It may need to be tweaked when a second user 412513b032cSGeorge Spelvin * is found. 413513b032cSGeorge Spelvin * 414513b032cSGeorge Spelvin * There is no function interface for setting the lookup_cookie field. 415513b032cSGeorge Spelvin * It's initialized to NULL when the pps device is created, and if a 416513b032cSGeorge Spelvin * client wants to use it, just fill it in afterward. 417513b032cSGeorge Spelvin * 418513b032cSGeorge Spelvin * The cookie is automatically set to NULL in pps_unregister_source() 419513b032cSGeorge Spelvin * so that it will not be used again, even if the pps device cannot 420513b032cSGeorge Spelvin * be removed from the idr due to pending references holding the minor 421513b032cSGeorge Spelvin * number in use. 422513b032cSGeorge Spelvin */ 423513b032cSGeorge Spelvin struct pps_device *pps_lookup_dev(void const *cookie) 424513b032cSGeorge Spelvin { 425513b032cSGeorge Spelvin struct pps_device *pps; 426513b032cSGeorge Spelvin unsigned id; 427513b032cSGeorge Spelvin 428513b032cSGeorge Spelvin rcu_read_lock(); 429513b032cSGeorge Spelvin idr_for_each_entry(&pps_idr, pps, id) 430513b032cSGeorge Spelvin if (cookie == pps->lookup_cookie) 431513b032cSGeorge Spelvin break; 432513b032cSGeorge Spelvin rcu_read_unlock(); 433513b032cSGeorge Spelvin return pps; 434513b032cSGeorge Spelvin } 435513b032cSGeorge Spelvin EXPORT_SYMBOL(pps_lookup_dev); 436513b032cSGeorge Spelvin 437513b032cSGeorge Spelvin /* 438eae9d2baSRodolfo Giometti * Module stuff 439eae9d2baSRodolfo Giometti */ 440eae9d2baSRodolfo Giometti 441eae9d2baSRodolfo Giometti static void __exit pps_exit(void) 442eae9d2baSRodolfo Giometti { 443eae9d2baSRodolfo Giometti class_destroy(pps_class); 444eae9d2baSRodolfo Giometti unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); 445eae9d2baSRodolfo Giometti } 446eae9d2baSRodolfo Giometti 447eae9d2baSRodolfo Giometti static int __init pps_init(void) 448eae9d2baSRodolfo Giometti { 449eae9d2baSRodolfo Giometti int err; 450eae9d2baSRodolfo Giometti 451eae9d2baSRodolfo Giometti pps_class = class_create(THIS_MODULE, "pps"); 4527ad12566SDan Carpenter if (IS_ERR(pps_class)) { 4537f7cce74SAlexander Gordeev pr_err("failed to allocate class\n"); 4547ad12566SDan Carpenter return PTR_ERR(pps_class); 455eae9d2baSRodolfo Giometti } 456bd0eae4eSGreg Kroah-Hartman pps_class->dev_groups = pps_groups; 457eae9d2baSRodolfo Giometti 458eae9d2baSRodolfo Giometti err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); 459eae9d2baSRodolfo Giometti if (err < 0) { 4607f7cce74SAlexander Gordeev pr_err("failed to allocate char device region\n"); 461eae9d2baSRodolfo Giometti goto remove_class; 462eae9d2baSRodolfo Giometti } 463eae9d2baSRodolfo Giometti 464eae9d2baSRodolfo Giometti pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); 465eae9d2baSRodolfo Giometti pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " 466eae9d2baSRodolfo Giometti "<giometti@linux.it>\n", PPS_VERSION); 467eae9d2baSRodolfo Giometti 468eae9d2baSRodolfo Giometti return 0; 469eae9d2baSRodolfo Giometti 470eae9d2baSRodolfo Giometti remove_class: 471eae9d2baSRodolfo Giometti class_destroy(pps_class); 472eae9d2baSRodolfo Giometti 473eae9d2baSRodolfo Giometti return err; 474eae9d2baSRodolfo Giometti } 475eae9d2baSRodolfo Giometti 476eae9d2baSRodolfo Giometti subsys_initcall(pps_init); 477eae9d2baSRodolfo Giometti module_exit(pps_exit); 478eae9d2baSRodolfo Giometti 479eae9d2baSRodolfo Giometti MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); 480eae9d2baSRodolfo Giometti MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); 481eae9d2baSRodolfo Giometti MODULE_LICENSE("GPL"); 482