blob: 9e15cf1da946c3aa01e3752aebea0a683a9b3b97 [file] [log] [blame]
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -07001/*
2 * PPS core file
3 *
4 *
5 * Copyright (C) 2005-2009 Rodolfo Giometti <giometti@linux.it>
6 *
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.
11 *
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.
16 *
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.
20 */
21
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070023
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>
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -080030#include <linux/mutex.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070031#include <linux/cdev.h>
32#include <linux/poll.h>
33#include <linux/pps_kernel.h>
Alexander Gordeev083e5862011-01-12 17:00:53 -080034#include <linux/slab.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070035
36/*
37 * Local variables
38 */
39
40static dev_t pps_devt;
41static struct class *pps_class;
42
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -080043static DEFINE_MUTEX(pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -080044static DEFINE_IDR(pps_idr);
45
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070046/*
47 * Char device methods
48 */
49
50static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
51{
52 struct pps_device *pps = file->private_data;
53
54 poll_wait(file, &pps->queue, wait);
55
56 return POLLIN | POLLRDNORM;
57}
58
59static int pps_cdev_fasync(int fd, struct file *file, int on)
60{
61 struct pps_device *pps = file->private_data;
62 return fasync_helper(fd, file, on, &pps->async_queue);
63}
64
65static long pps_cdev_ioctl(struct file *file,
66 unsigned int cmd, unsigned long arg)
67{
68 struct pps_device *pps = file->private_data;
69 struct pps_kparams params;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070070 void __user *uarg = (void __user *) arg;
71 int __user *iuarg = (int __user *) arg;
72 int err;
73
74 switch (cmd) {
75 case PPS_GETPARAMS:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080076 dev_dbg(pps->dev, "PPS_GETPARAMS\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070077
Rodolfo Giometticbf83cc52009-11-11 14:26:52 -080078 spin_lock_irq(&pps->lock);
79
80 /* Get the current parameters */
81 params = pps->params;
82
83 spin_unlock_irq(&pps->lock);
84
85 err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070086 if (err)
87 return -EFAULT;
88
89 break;
90
91 case PPS_SETPARAMS:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080092 dev_dbg(pps->dev, "PPS_SETPARAMS\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070093
94 /* Check the capabilities */
95 if (!capable(CAP_SYS_TIME))
96 return -EPERM;
97
98 err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
99 if (err)
100 return -EFAULT;
101 if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800102 dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700103 params.mode);
104 return -EINVAL;
105 }
106
107 /* Check for supported capabilities */
108 if ((params.mode & ~pps->info.mode) != 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800109 dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700110 params.mode);
111 return -EINVAL;
112 }
113
114 spin_lock_irq(&pps->lock);
115
116 /* Save the new parameters */
117 pps->params = params;
118
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 */
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800122 dev_dbg(pps->dev, "time format unspecified (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700123 params.mode);
124 pps->params.mode |= PPS_TSFMT_TSPEC;
125 }
126 if (pps->info.mode & PPS_CANWAIT)
127 pps->params.mode |= PPS_CANWAIT;
128 pps->params.api_version = PPS_API_VERS;
129
130 spin_unlock_irq(&pps->lock);
131
132 break;
133
134 case PPS_GETCAP:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800135 dev_dbg(pps->dev, "PPS_GETCAP\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700136
137 err = put_user(pps->info.mode, iuarg);
138 if (err)
139 return -EFAULT;
140
141 break;
142
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800143 case PPS_FETCH: {
144 struct pps_fdata fdata;
Alexander Gordeev3003d552011-01-12 17:00:50 -0800145 unsigned int ev;
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800146
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800147 dev_dbg(pps->dev, "PPS_FETCH\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700148
149 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
150 if (err)
151 return -EFAULT;
152
Alexander Gordeev3003d552011-01-12 17:00:50 -0800153 ev = pps->last_ev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700154
155 /* Manage the timeout */
156 if (fdata.timeout.flags & PPS_TIME_INVALID)
Alexander Gordeev3003d552011-01-12 17:00:50 -0800157 err = wait_event_interruptible(pps->queue,
158 ev != pps->last_ev);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700159 else {
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800160 unsigned long ticks;
161
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800162 dev_dbg(pps->dev, "timeout %lld.%09d\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700163 (long long) fdata.timeout.sec,
164 fdata.timeout.nsec);
165 ticks = fdata.timeout.sec * HZ;
166 ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
167
168 if (ticks != 0) {
169 err = wait_event_interruptible_timeout(
Alexander Gordeev3003d552011-01-12 17:00:50 -0800170 pps->queue,
171 ev != pps->last_ev,
172 ticks);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700173 if (err == 0)
174 return -ETIMEDOUT;
175 }
176 }
177
178 /* Check for pending signals */
179 if (err == -ERESTARTSYS) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800180 dev_dbg(pps->dev, "pending signal caught\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700181 return -EINTR;
182 }
183
184 /* Return the fetched timestamp */
185 spin_lock_irq(&pps->lock);
186
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;
192
193 spin_unlock_irq(&pps->lock);
194
195 err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
196 if (err)
197 return -EFAULT;
198
199 break;
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800200 }
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700201 default:
202 return -ENOTTY;
203 break;
204 }
205
206 return 0;
207}
208
209static int pps_cdev_open(struct inode *inode, struct file *file)
210{
211 struct pps_device *pps = container_of(inode->i_cdev,
212 struct pps_device, cdev);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700213 file->private_data = pps;
214
215 return 0;
216}
217
218static int pps_cdev_release(struct inode *inode, struct file *file)
219{
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700220 return 0;
221}
222
223/*
224 * Char device stuff
225 */
226
227static const struct file_operations pps_cdev_fops = {
228 .owner = THIS_MODULE,
229 .llseek = no_llseek,
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,
235};
236
Alexander Gordeev083e5862011-01-12 17:00:53 -0800237static void pps_device_destruct(struct device *dev)
238{
239 struct pps_device *pps = dev_get_drvdata(dev);
240
241 /* release id here to protect others from using it while it's
242 * still in use */
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800243 mutex_lock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800244 idr_remove(&pps_idr, pps->id);
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800245 mutex_unlock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800246
247 kfree(dev);
248 kfree(pps);
249}
250
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700251int pps_register_cdev(struct pps_device *pps)
252{
253 int err;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800254 dev_t devt;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700255
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800256 mutex_lock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800257 /* Get new ID for the new PPS source */
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800258 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0) {
259 mutex_unlock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800260 return -ENOMEM;
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800261 }
Alexander Gordeev083e5862011-01-12 17:00:53 -0800262
263 /* Now really allocate the PPS source.
264 * After idr_get_new() calling the new source will be freely available
265 * into the kernel.
266 */
Alexander Gordeev083e5862011-01-12 17:00:53 -0800267 err = idr_get_new(&pps_idr, pps, &pps->id);
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800268 mutex_unlock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800269
270 if (err < 0)
271 return err;
272
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",
276 pps->info.name);
277 err = -EBUSY;
278 goto free_idr;
279 }
280
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800281 devt = MKDEV(MAJOR(pps_devt), pps->id);
282
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700283 cdev_init(&pps->cdev, &pps_cdev_fops);
284 pps->cdev.owner = pps->info.owner;
285
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800286 err = cdev_add(&pps->cdev, devt, 1);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700287 if (err) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800288 pr_err("%s: failed to add char device %d:%d\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700289 pps->info.name, MAJOR(pps_devt), pps->id);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800290 goto free_idr;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700291 }
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800292 pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700293 "pps%d", pps->id);
Joonwoo Park054b2b12009-08-26 14:29:18 -0700294 if (IS_ERR(pps->dev))
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700295 goto del_cdev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700296
Alexander Gordeev083e5862011-01-12 17:00:53 -0800297 pps->dev->release = pps_device_destruct;
298
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700299 pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
300 MAJOR(pps_devt), pps->id);
301
302 return 0;
303
304del_cdev:
305 cdev_del(&pps->cdev);
306
Alexander Gordeev083e5862011-01-12 17:00:53 -0800307free_idr:
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800308 mutex_lock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800309 idr_remove(&pps_idr, pps->id);
Alexander Gordeev2a5cd6e2011-01-12 17:00:53 -0800310 mutex_unlock(&pps_idr_lock);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800311
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700312 return err;
313}
314
315void pps_unregister_cdev(struct pps_device *pps)
316{
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800317 device_destroy(pps_class, pps->dev->devt);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700318 cdev_del(&pps->cdev);
319}
320
321/*
322 * Module stuff
323 */
324
325static void __exit pps_exit(void)
326{
327 class_destroy(pps_class);
328 unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
329}
330
331static int __init pps_init(void)
332{
333 int err;
334
335 pps_class = class_create(THIS_MODULE, "pps");
336 if (!pps_class) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800337 pr_err("failed to allocate class\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700338 return -ENOMEM;
339 }
340 pps_class->dev_attrs = pps_attrs;
341
342 err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
343 if (err < 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800344 pr_err("failed to allocate char device region\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700345 goto remove_class;
346 }
347
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);
351
352 return 0;
353
354remove_class:
355 class_destroy(pps_class);
356
357 return err;
358}
359
360subsys_initcall(pps_init);
361module_exit(pps_exit);
362
363MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
364MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
365MODULE_LICENSE("GPL");