blob: 79b44557813225fec09d2e902c111f774722aafd [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>
30#include <linux/cdev.h>
31#include <linux/poll.h>
32#include <linux/pps_kernel.h>
Alexander Gordeev083e5862011-01-12 17:00:53 -080033#include <linux/slab.h>
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070034
35/*
36 * Local variables
37 */
38
39static dev_t pps_devt;
40static struct class *pps_class;
41
Alexander Gordeev083e5862011-01-12 17:00:53 -080042static DEFINE_SPINLOCK(pps_idr_lock);
43static DEFINE_IDR(pps_idr);
44
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070045/*
46 * Char device methods
47 */
48
49static unsigned int pps_cdev_poll(struct file *file, poll_table *wait)
50{
51 struct pps_device *pps = file->private_data;
52
53 poll_wait(file, &pps->queue, wait);
54
55 return POLLIN | POLLRDNORM;
56}
57
58static int pps_cdev_fasync(int fd, struct file *file, int on)
59{
60 struct pps_device *pps = file->private_data;
61 return fasync_helper(fd, file, on, &pps->async_queue);
62}
63
64static long pps_cdev_ioctl(struct file *file,
65 unsigned int cmd, unsigned long arg)
66{
67 struct pps_device *pps = file->private_data;
68 struct pps_kparams params;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070069 void __user *uarg = (void __user *) arg;
70 int __user *iuarg = (int __user *) arg;
71 int err;
72
73 switch (cmd) {
74 case PPS_GETPARAMS:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080075 dev_dbg(pps->dev, "PPS_GETPARAMS\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070076
Rodolfo Giometticbf83cc52009-11-11 14:26:52 -080077 spin_lock_irq(&pps->lock);
78
79 /* Get the current parameters */
80 params = pps->params;
81
82 spin_unlock_irq(&pps->lock);
83
84 err = copy_to_user(uarg, &params, sizeof(struct pps_kparams));
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070085 if (err)
86 return -EFAULT;
87
88 break;
89
90 case PPS_SETPARAMS:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -080091 dev_dbg(pps->dev, "PPS_SETPARAMS\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -070092
93 /* Check the capabilities */
94 if (!capable(CAP_SYS_TIME))
95 return -EPERM;
96
97 err = copy_from_user(&params, uarg, sizeof(struct pps_kparams));
98 if (err)
99 return -EFAULT;
100 if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800101 dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700102 params.mode);
103 return -EINVAL;
104 }
105
106 /* Check for supported capabilities */
107 if ((params.mode & ~pps->info.mode) != 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800108 dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700109 params.mode);
110 return -EINVAL;
111 }
112
113 spin_lock_irq(&pps->lock);
114
115 /* Save the new parameters */
116 pps->params = params;
117
118 /* Restore the read only parameters */
119 if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) {
120 /* section 3.3 of RFC 2783 interpreted */
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800121 dev_dbg(pps->dev, "time format unspecified (%x)\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700122 params.mode);
123 pps->params.mode |= PPS_TSFMT_TSPEC;
124 }
125 if (pps->info.mode & PPS_CANWAIT)
126 pps->params.mode |= PPS_CANWAIT;
127 pps->params.api_version = PPS_API_VERS;
128
129 spin_unlock_irq(&pps->lock);
130
131 break;
132
133 case PPS_GETCAP:
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800134 dev_dbg(pps->dev, "PPS_GETCAP\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700135
136 err = put_user(pps->info.mode, iuarg);
137 if (err)
138 return -EFAULT;
139
140 break;
141
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800142 case PPS_FETCH: {
143 struct pps_fdata fdata;
Alexander Gordeev3003d552011-01-12 17:00:50 -0800144 unsigned int ev;
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800145
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800146 dev_dbg(pps->dev, "PPS_FETCH\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700147
148 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
149 if (err)
150 return -EFAULT;
151
Alexander Gordeev3003d552011-01-12 17:00:50 -0800152 ev = pps->last_ev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700153
154 /* Manage the timeout */
155 if (fdata.timeout.flags & PPS_TIME_INVALID)
Alexander Gordeev3003d552011-01-12 17:00:50 -0800156 err = wait_event_interruptible(pps->queue,
157 ev != pps->last_ev);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700158 else {
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800159 unsigned long ticks;
160
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800161 dev_dbg(pps->dev, "timeout %lld.%09d\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700162 (long long) fdata.timeout.sec,
163 fdata.timeout.nsec);
164 ticks = fdata.timeout.sec * HZ;
165 ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ);
166
167 if (ticks != 0) {
168 err = wait_event_interruptible_timeout(
Alexander Gordeev3003d552011-01-12 17:00:50 -0800169 pps->queue,
170 ev != pps->last_ev,
171 ticks);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700172 if (err == 0)
173 return -ETIMEDOUT;
174 }
175 }
176
177 /* Check for pending signals */
178 if (err == -ERESTARTSYS) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800179 dev_dbg(pps->dev, "pending signal caught\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700180 return -EINTR;
181 }
182
183 /* Return the fetched timestamp */
184 spin_lock_irq(&pps->lock);
185
186 fdata.info.assert_sequence = pps->assert_sequence;
187 fdata.info.clear_sequence = pps->clear_sequence;
188 fdata.info.assert_tu = pps->assert_tu;
189 fdata.info.clear_tu = pps->clear_tu;
190 fdata.info.current_mode = pps->current_mode;
191
192 spin_unlock_irq(&pps->lock);
193
194 err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata));
195 if (err)
196 return -EFAULT;
197
198 break;
Alexander Gordeev86d921f2011-01-12 17:00:49 -0800199 }
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700200 default:
201 return -ENOTTY;
202 break;
203 }
204
205 return 0;
206}
207
208static int pps_cdev_open(struct inode *inode, struct file *file)
209{
210 struct pps_device *pps = container_of(inode->i_cdev,
211 struct pps_device, cdev);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700212 file->private_data = pps;
213
214 return 0;
215}
216
217static int pps_cdev_release(struct inode *inode, struct file *file)
218{
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700219 return 0;
220}
221
222/*
223 * Char device stuff
224 */
225
226static const struct file_operations pps_cdev_fops = {
227 .owner = THIS_MODULE,
228 .llseek = no_llseek,
229 .poll = pps_cdev_poll,
230 .fasync = pps_cdev_fasync,
231 .unlocked_ioctl = pps_cdev_ioctl,
232 .open = pps_cdev_open,
233 .release = pps_cdev_release,
234};
235
Alexander Gordeev083e5862011-01-12 17:00:53 -0800236static void pps_device_destruct(struct device *dev)
237{
238 struct pps_device *pps = dev_get_drvdata(dev);
239
240 /* release id here to protect others from using it while it's
241 * still in use */
242 spin_lock_irq(&pps_idr_lock);
243 idr_remove(&pps_idr, pps->id);
244 spin_unlock_irq(&pps_idr_lock);
245
246 kfree(dev);
247 kfree(pps);
248}
249
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700250int pps_register_cdev(struct pps_device *pps)
251{
252 int err;
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800253 dev_t devt;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700254
Alexander Gordeev083e5862011-01-12 17:00:53 -0800255 /* Get new ID for the new PPS source */
256 if (idr_pre_get(&pps_idr, GFP_KERNEL) == 0)
257 return -ENOMEM;
258
259 /* Now really allocate the PPS source.
260 * After idr_get_new() calling the new source will be freely available
261 * into the kernel.
262 */
263 spin_lock_irq(&pps_idr_lock);
264 err = idr_get_new(&pps_idr, pps, &pps->id);
265 spin_unlock_irq(&pps_idr_lock);
266
267 if (err < 0)
268 return err;
269
270 pps->id &= MAX_ID_MASK;
271 if (pps->id >= PPS_MAX_SOURCES) {
272 pr_err("%s: too many PPS sources in the system\n",
273 pps->info.name);
274 err = -EBUSY;
275 goto free_idr;
276 }
277
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800278 devt = MKDEV(MAJOR(pps_devt), pps->id);
279
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700280 cdev_init(&pps->cdev, &pps_cdev_fops);
281 pps->cdev.owner = pps->info.owner;
282
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800283 err = cdev_add(&pps->cdev, devt, 1);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700284 if (err) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800285 pr_err("%s: failed to add char device %d:%d\n",
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700286 pps->info.name, MAJOR(pps_devt), pps->id);
Alexander Gordeev083e5862011-01-12 17:00:53 -0800287 goto free_idr;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700288 }
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800289 pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700290 "pps%d", pps->id);
Joonwoo Park054b2b12009-08-26 14:29:18 -0700291 if (IS_ERR(pps->dev))
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700292 goto del_cdev;
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700293
Alexander Gordeev083e5862011-01-12 17:00:53 -0800294 pps->dev->release = pps_device_destruct;
295
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700296 pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
297 MAJOR(pps_devt), pps->id);
298
299 return 0;
300
301del_cdev:
302 cdev_del(&pps->cdev);
303
Alexander Gordeev083e5862011-01-12 17:00:53 -0800304free_idr:
305 spin_lock_irq(&pps_idr_lock);
306 idr_remove(&pps_idr, pps->id);
307 spin_unlock_irq(&pps_idr_lock);
308
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700309 return err;
310}
311
312void pps_unregister_cdev(struct pps_device *pps)
313{
Alexander Gordeev5e196d32011-01-12 17:00:51 -0800314 device_destroy(pps_class, pps->dev->devt);
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700315 cdev_del(&pps->cdev);
316}
317
318/*
319 * Module stuff
320 */
321
322static void __exit pps_exit(void)
323{
324 class_destroy(pps_class);
325 unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
326}
327
328static int __init pps_init(void)
329{
330 int err;
331
332 pps_class = class_create(THIS_MODULE, "pps");
333 if (!pps_class) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800334 pr_err("failed to allocate class\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700335 return -ENOMEM;
336 }
337 pps_class->dev_attrs = pps_attrs;
338
339 err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps");
340 if (err < 0) {
Alexander Gordeev7f7cce72011-01-12 17:00:52 -0800341 pr_err("failed to allocate char device region\n");
Rodolfo Giomettieae9d2b2009-06-17 16:28:37 -0700342 goto remove_class;
343 }
344
345 pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS);
346 pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti "
347 "<giometti@linux.it>\n", PPS_VERSION);
348
349 return 0;
350
351remove_class:
352 class_destroy(pps_class);
353
354 return err;
355}
356
357subsys_initcall(pps_init);
358module_exit(pps_exit);
359
360MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
361MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
362MODULE_LICENSE("GPL");