5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/sched.h>
28 #include <linux/uaccess.h>
29 #include <linux/idr.h>
30 #include <linux/mutex.h>
31 #include <linux/cdev.h>
32 #include <linux/poll.h>
33 #include <linux/pps_kernel.h>
34 #include <linux/slab.h>
40 static dev_t pps_devt;
41 static struct class *pps_class;
43 static DEFINE_MUTEX(pps_idr_lock);
44 static DEFINE_IDR(pps_idr);
50 static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
52 struct pps_device *pps = file->private_data;
54 poll_wait(file, &pps->queue, wait);
56 return POLLIN | POLLRDNORM;
59 static int pps_cdev_fasync(int fd, struct file *file, int on)
61 struct pps_device *pps = file->private_data;
62 return fasync_helper(fd, file, on, &pps->async_queue);
65 static long pps_cdev_ioctl(struct file *file,
66 unsigned int cmd, unsigned long arg)
68 struct pps_device *pps = file->private_data;
69 struct pps_kparams params;
70 void __user *uarg = (void __user *) arg;
71 int __user *iuarg = (int __user *) arg;
76 dev_dbg(pps->dev, "PPS_GETPARAMS\n");
78 spin_lock_irq(&pps->lock);
80 /* Get the current parameters */
83 spin_unlock_irq(&pps->lock);
85 err = copy_to_user(uarg, ¶ms, sizeof(struct pps_kparams));
92 dev_dbg(pps->dev, "PPS_SETPARAMS\n");
94 /* Check the capabilities */
95 if (!capable(CAP_SYS_TIME))
98 err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams));
101 if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
102 dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
107 /* Check for supported capabilities */
108 if ((params.mode & ~pps->info.mode) != 0) {
109 dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
114 spin_lock_irq(&pps->lock);
116 /* Save the new parameters */
117 pps->params = params;
119 /* Restore the read only parameters */
120 if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
121 /* section 3.3 of RFC 2783 interpreted */
122 dev_dbg(pps->dev, "time format unspecified (%x)\n",
124 pps->params.mode |= PPS_TSFMT_TSPEC;
126 if (pps->info.mode & PPS_CANWAIT)
127 pps->params.mode |= PPS_CANWAIT;
128 pps->params.api_version = PPS_API_VERS;
130 spin_unlock_irq(&pps->lock);
135 dev_dbg(pps->dev, "PPS_GETCAP\n");
137 err = put_user(pps->info.mode, iuarg);
144 struct pps_fdata fdata;
147 dev_dbg(pps->dev, "PPS_FETCH\n");
149 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
155 /* Manage the timeout */
156 if (fdata.timeout.flags & PPS_TIME_INVALID)
157 err = wait_event_interruptible(pps->queue,
162 dev_dbg(pps->dev, "timeout %lld.%09d\n",
163 (long long) fdata.timeout.sec,
165 ticks = fdata.timeout.sec * HZ;
166 ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
169 err = wait_event_interruptible_timeout(
178 /* Check for pending signals */
179 if (err == -ERESTARTSYS) {
180 dev_dbg(pps->dev, "pending signal caught\n");
184 /* Return the fetched timestamp */
185 spin_lock_irq(&pps->lock);
187 fdata.info.assert_sequence = pps->assert_sequence;
188 fdata.info.clear_sequence = pps->clear_sequence;
189 fdata.info.assert_tu = pps->assert_tu;
190 fdata.info.clear_tu = pps->clear_tu;
191 fdata.info.current_mode = pps->current_mode;
193 spin_unlock_irq(&pps->lock);
195 err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
209 static int pps_cdev_open(struct inode *inode, struct file *file)
211 struct pps_device *pps = container_of(inode->i_cdev,
212 struct pps_device, cdev);
213 file->private_data = pps;
218 static int pps_cdev_release(struct inode *inode, struct file *file)
227 static const struct file_operations pps_cdev_fops = {
228 .owner = THIS_MODULE,
230 .poll = pps_cdev_poll,
231 .fasync = pps_cdev_fasync,
232 .unlocked_ioctl = pps_cdev_ioctl,
233 .open = pps_cdev_open,
234 .release = pps_cdev_release,
237 static void pps_device_destruct(struct device *dev)
239 struct pps_device *pps = dev_get_drvdata(dev);
241 /* release id here to protect others from using it while it's
243 mutex_lock(&pps_idr_lock);
244 idr_remove(&pps_idr, pps->id);
245 mutex_unlock(&pps_idr_lock);
251 int pps_register_cdev(struct pps_device *pps)
256 mutex_lock(&pps_idr_lock);
257 /* Get new ID for the new PPS source */
258 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
259 mutex_unlock(&pps_idr_lock);
263 /* Now really allocate the PPS source.
264 * After idr_get_new() calling the new source will be freely available
267 err = idr_get_new(&pps_idr, pps, &pps->id);
268 mutex_unlock(&pps_idr_lock);
273 pps->id &= MAX_ID_MASK;
274 if (pps->id >= PPS_MAX_SOURCES) {
275 pr_err("%s: too many PPS sources in the system\n",
281 devt = MKDEV(MAJOR(pps_devt), pps->id);
283 cdev_init(&pps->cdev, &pps_cdev_fops);
284 pps->cdev.owner = pps->info.owner;
286 err = cdev_add(&pps->cdev, devt, 1);
288 pr_err("%s: failed to add char device %d:%d\n",
289 pps->info.name, MAJOR(pps_devt), pps->id);
292 pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
294 if (IS_ERR(pps->dev))
297 pps->dev->release = pps_device_destruct;
299 pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
300 MAJOR(pps_devt), pps->id);
305 cdev_del(&pps->cdev);
308 mutex_lock(&pps_idr_lock);
309 idr_remove(&pps_idr, pps->id);
310 mutex_unlock(&pps_idr_lock);
315 void pps_unregister_cdev(struct pps_device *pps)
317 device_destroy(pps_class, pps->dev->devt);
318 cdev_del(&pps->cdev);
325 static void __exit pps_exit(void)
327 class_destroy(pps_class);
328 unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
331 static int __init pps_init(void)
335 pps_class = class_create(THIS_MODULE, "pps");
337 pr_err("failed to allocate class\n");
340 pps_class->dev_attrs = pps_attrs;
342 err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
344 pr_err("failed to allocate char device region\n");
348 pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
349 pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
350 "<giometti@linux.it>\n", PPS_VERSION);
355 class_destroy(pps_class);
360 subsys_initcall(pps_init);
361 module_exit(pps_exit);
363 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
364 MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
365 MODULE_LICENSE("GPL");