pps: move idr stuff to pps.c
[linux-2.6.git] / drivers / pps / pps.c
1 /*
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
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
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>
33 #include <linux/slab.h>
34
35 /*
36  * Local variables
37  */
38
39 static dev_t pps_devt;
40 static struct class *pps_class;
41
42 static DEFINE_SPINLOCK(pps_idr_lock);
43 static DEFINE_IDR(pps_idr);
44
45 /*
46  * Char device methods
47  */
48
49 static 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
58 static 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
64 static 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;
69         void __user *uarg = (void __user *) arg;
70         int __user *iuarg = (int __user *) arg;
71         int err;
72
73         switch (cmd) {
74         case PPS_GETPARAMS:
75                 dev_dbg(pps->dev, "PPS_GETPARAMS\n");
76
77                 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));
85                 if (err)
86                         return -EFAULT;
87
88                 break;
89
90         case PPS_SETPARAMS:
91                 dev_dbg(pps->dev, "PPS_SETPARAMS\n");
92
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))) {
101                         dev_dbg(pps->dev, "capture mode unspecified (%x)\n",
102                                                                 params.mode);
103                         return -EINVAL;
104                 }
105
106                 /* Check for supported capabilities */
107                 if ((params.mode & ~pps->info.mode) != 0) {
108                         dev_dbg(pps->dev, "unsupported capabilities (%x)\n",
109                                                                 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 */
121                         dev_dbg(pps->dev, "time format unspecified (%x)\n",
122                                                                 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:
134                 dev_dbg(pps->dev, "PPS_GETCAP\n");
135
136                 err = put_user(pps->info.mode, iuarg);
137                 if (err)
138                         return -EFAULT;
139
140                 break;
141
142         case PPS_FETCH: {
143                 struct pps_fdata fdata;
144                 unsigned int ev;
145
146                 dev_dbg(pps->dev, "PPS_FETCH\n");
147
148                 err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata));
149                 if (err)
150                         return -EFAULT;
151
152                 ev = pps->last_ev;
153
154                 /* Manage the timeout */
155                 if (fdata.timeout.flags & PPS_TIME_INVALID)
156                         err = wait_event_interruptible(pps->queue,
157                                         ev != pps->last_ev);
158                 else {
159                         unsigned long ticks;
160
161                         dev_dbg(pps->dev, "timeout %lld.%09d\n",
162                                         (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(
169                                                 pps->queue,
170                                                 ev != pps->last_ev,
171                                                 ticks);
172                                 if (err == 0)
173                                         return -ETIMEDOUT;
174                         }
175                 }
176
177                 /* Check for pending signals */
178                 if (err == -ERESTARTSYS) {
179                         dev_dbg(pps->dev, "pending signal caught\n");
180                         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;
199         }
200         default:
201                 return -ENOTTY;
202                 break;
203         }
204
205         return 0;
206 }
207
208 static 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);
212         file->private_data = pps;
213
214         return 0;
215 }
216
217 static int pps_cdev_release(struct inode *inode, struct file *file)
218 {
219         return 0;
220 }
221
222 /*
223  * Char device stuff
224  */
225
226 static 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
236 static 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
250 int pps_register_cdev(struct pps_device *pps)
251 {
252         int err;
253         dev_t devt;
254
255         /* 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
278         devt = MKDEV(MAJOR(pps_devt), pps->id);
279
280         cdev_init(&pps->cdev, &pps_cdev_fops);
281         pps->cdev.owner = pps->info.owner;
282
283         err = cdev_add(&pps->cdev, devt, 1);
284         if (err) {
285                 pr_err("%s: failed to add char device %d:%d\n",
286                                 pps->info.name, MAJOR(pps_devt), pps->id);
287                 goto free_idr;
288         }
289         pps->dev = device_create(pps_class, pps->info.dev, devt, pps,
290                                                         "pps%d", pps->id);
291         if (IS_ERR(pps->dev))
292                 goto del_cdev;
293
294         pps->dev->release = pps_device_destruct;
295
296         pr_debug("source %s got cdev (%d:%d)\n", pps->info.name,
297                         MAJOR(pps_devt), pps->id);
298
299         return 0;
300
301 del_cdev:
302         cdev_del(&pps->cdev);
303
304 free_idr:
305         spin_lock_irq(&pps_idr_lock);
306         idr_remove(&pps_idr, pps->id);
307         spin_unlock_irq(&pps_idr_lock);
308
309         return err;
310 }
311
312 void pps_unregister_cdev(struct pps_device *pps)
313 {
314         device_destroy(pps_class, pps->dev->devt);
315         cdev_del(&pps->cdev);
316 }
317
318 /*
319  * Module stuff
320  */
321
322 static void __exit pps_exit(void)
323 {
324         class_destroy(pps_class);
325         unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES);
326 }
327
328 static int __init pps_init(void)
329 {
330         int err;
331
332         pps_class = class_create(THIS_MODULE, "pps");
333         if (!pps_class) {
334                 pr_err("failed to allocate class\n");
335                 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) {
341                 pr_err("failed to allocate char device region\n");
342                 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
351 remove_class:
352         class_destroy(pps_class);
353
354         return err;
355 }
356
357 subsys_initcall(pps_init);
358 module_exit(pps_exit);
359
360 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
361 MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION);
362 MODULE_LICENSE("GPL");