diff options
Diffstat (limited to 'drivers/pps/pps.c')
-rw-r--r-- | drivers/pps/pps.c | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/drivers/pps/pps.c b/drivers/pps/pps.c new file mode 100644 index 000000000000..ac8cc8cea1e3 --- /dev/null +++ b/drivers/pps/pps.c | |||
@@ -0,0 +1,312 @@ | |||
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 | |||
23 | #include <linux/kernel.h> | ||
24 | #include <linux/module.h> | ||
25 | #include <linux/init.h> | ||
26 | #include <linux/sched.h> | ||
27 | #include <linux/uaccess.h> | ||
28 | #include <linux/idr.h> | ||
29 | #include <linux/cdev.h> | ||
30 | #include <linux/poll.h> | ||
31 | #include <linux/pps_kernel.h> | ||
32 | |||
33 | /* | ||
34 | * Local variables | ||
35 | */ | ||
36 | |||
37 | static dev_t pps_devt; | ||
38 | static struct class *pps_class; | ||
39 | |||
40 | /* | ||
41 | * Char device methods | ||
42 | */ | ||
43 | |||
44 | static unsigned int pps_cdev_poll(struct file *file, poll_table *wait) | ||
45 | { | ||
46 | struct pps_device *pps = file->private_data; | ||
47 | |||
48 | poll_wait(file, &pps->queue, wait); | ||
49 | |||
50 | return POLLIN | POLLRDNORM; | ||
51 | } | ||
52 | |||
53 | static int pps_cdev_fasync(int fd, struct file *file, int on) | ||
54 | { | ||
55 | struct pps_device *pps = file->private_data; | ||
56 | return fasync_helper(fd, file, on, &pps->async_queue); | ||
57 | } | ||
58 | |||
59 | static long pps_cdev_ioctl(struct file *file, | ||
60 | unsigned int cmd, unsigned long arg) | ||
61 | { | ||
62 | struct pps_device *pps = file->private_data; | ||
63 | struct pps_kparams params; | ||
64 | struct pps_fdata fdata; | ||
65 | unsigned long ticks; | ||
66 | void __user *uarg = (void __user *) arg; | ||
67 | int __user *iuarg = (int __user *) arg; | ||
68 | int err; | ||
69 | |||
70 | switch (cmd) { | ||
71 | case PPS_GETPARAMS: | ||
72 | pr_debug("PPS_GETPARAMS: source %d\n", pps->id); | ||
73 | |||
74 | /* Return current parameters */ | ||
75 | err = copy_to_user(uarg, &pps->params, | ||
76 | sizeof(struct pps_kparams)); | ||
77 | if (err) | ||
78 | return -EFAULT; | ||
79 | |||
80 | break; | ||
81 | |||
82 | case PPS_SETPARAMS: | ||
83 | pr_debug("PPS_SETPARAMS: source %d\n", pps->id); | ||
84 | |||
85 | /* Check the capabilities */ | ||
86 | if (!capable(CAP_SYS_TIME)) | ||
87 | return -EPERM; | ||
88 | |||
89 | err = copy_from_user(¶ms, uarg, sizeof(struct pps_kparams)); | ||
90 | if (err) | ||
91 | return -EFAULT; | ||
92 | if (!(params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { | ||
93 | pr_debug("capture mode unspecified (%x)\n", | ||
94 | params.mode); | ||
95 | return -EINVAL; | ||
96 | } | ||
97 | |||
98 | /* Check for supported capabilities */ | ||
99 | if ((params.mode & ~pps->info.mode) != 0) { | ||
100 | pr_debug("unsupported capabilities (%x)\n", | ||
101 | params.mode); | ||
102 | return -EINVAL; | ||
103 | } | ||
104 | |||
105 | spin_lock_irq(&pps->lock); | ||
106 | |||
107 | /* Save the new parameters */ | ||
108 | pps->params = params; | ||
109 | |||
110 | /* Restore the read only parameters */ | ||
111 | if ((params.mode & (PPS_TSFMT_TSPEC | PPS_TSFMT_NTPFP)) == 0) { | ||
112 | /* section 3.3 of RFC 2783 interpreted */ | ||
113 | pr_debug("time format unspecified (%x)\n", | ||
114 | params.mode); | ||
115 | pps->params.mode |= PPS_TSFMT_TSPEC; | ||
116 | } | ||
117 | if (pps->info.mode & PPS_CANWAIT) | ||
118 | pps->params.mode |= PPS_CANWAIT; | ||
119 | pps->params.api_version = PPS_API_VERS; | ||
120 | |||
121 | spin_unlock_irq(&pps->lock); | ||
122 | |||
123 | break; | ||
124 | |||
125 | case PPS_GETCAP: | ||
126 | pr_debug("PPS_GETCAP: source %d\n", pps->id); | ||
127 | |||
128 | err = put_user(pps->info.mode, iuarg); | ||
129 | if (err) | ||
130 | return -EFAULT; | ||
131 | |||
132 | break; | ||
133 | |||
134 | case PPS_FETCH: | ||
135 | pr_debug("PPS_FETCH: source %d\n", pps->id); | ||
136 | |||
137 | err = copy_from_user(&fdata, uarg, sizeof(struct pps_fdata)); | ||
138 | if (err) | ||
139 | return -EFAULT; | ||
140 | |||
141 | pps->go = 0; | ||
142 | |||
143 | /* Manage the timeout */ | ||
144 | if (fdata.timeout.flags & PPS_TIME_INVALID) | ||
145 | err = wait_event_interruptible(pps->queue, pps->go); | ||
146 | else { | ||
147 | pr_debug("timeout %lld.%09d\n", | ||
148 | (long long) fdata.timeout.sec, | ||
149 | fdata.timeout.nsec); | ||
150 | ticks = fdata.timeout.sec * HZ; | ||
151 | ticks += fdata.timeout.nsec / (NSEC_PER_SEC / HZ); | ||
152 | |||
153 | if (ticks != 0) { | ||
154 | err = wait_event_interruptible_timeout( | ||
155 | pps->queue, pps->go, ticks); | ||
156 | if (err == 0) | ||
157 | return -ETIMEDOUT; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /* Check for pending signals */ | ||
162 | if (err == -ERESTARTSYS) { | ||
163 | pr_debug("pending signal caught\n"); | ||
164 | return -EINTR; | ||
165 | } | ||
166 | |||
167 | /* Return the fetched timestamp */ | ||
168 | spin_lock_irq(&pps->lock); | ||
169 | |||
170 | fdata.info.assert_sequence = pps->assert_sequence; | ||
171 | fdata.info.clear_sequence = pps->clear_sequence; | ||
172 | fdata.info.assert_tu = pps->assert_tu; | ||
173 | fdata.info.clear_tu = pps->clear_tu; | ||
174 | fdata.info.current_mode = pps->current_mode; | ||
175 | |||
176 | spin_unlock_irq(&pps->lock); | ||
177 | |||
178 | err = copy_to_user(uarg, &fdata, sizeof(struct pps_fdata)); | ||
179 | if (err) | ||
180 | return -EFAULT; | ||
181 | |||
182 | break; | ||
183 | |||
184 | default: | ||
185 | return -ENOTTY; | ||
186 | break; | ||
187 | } | ||
188 | |||
189 | return 0; | ||
190 | } | ||
191 | |||
192 | static int pps_cdev_open(struct inode *inode, struct file *file) | ||
193 | { | ||
194 | struct pps_device *pps = container_of(inode->i_cdev, | ||
195 | struct pps_device, cdev); | ||
196 | int found; | ||
197 | |||
198 | found = pps_get_source(pps->id) != 0; | ||
199 | if (!found) | ||
200 | return -ENODEV; | ||
201 | |||
202 | file->private_data = pps; | ||
203 | |||
204 | return 0; | ||
205 | } | ||
206 | |||
207 | static int pps_cdev_release(struct inode *inode, struct file *file) | ||
208 | { | ||
209 | struct pps_device *pps = file->private_data; | ||
210 | |||
211 | /* Free the PPS source and wake up (possible) deregistration */ | ||
212 | pps_put_source(pps); | ||
213 | |||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | /* | ||
218 | * Char device stuff | ||
219 | */ | ||
220 | |||
221 | static const struct file_operations pps_cdev_fops = { | ||
222 | .owner = THIS_MODULE, | ||
223 | .llseek = no_llseek, | ||
224 | .poll = pps_cdev_poll, | ||
225 | .fasync = pps_cdev_fasync, | ||
226 | .unlocked_ioctl = pps_cdev_ioctl, | ||
227 | .open = pps_cdev_open, | ||
228 | .release = pps_cdev_release, | ||
229 | }; | ||
230 | |||
231 | int pps_register_cdev(struct pps_device *pps) | ||
232 | { | ||
233 | int err; | ||
234 | |||
235 | pps->devno = MKDEV(MAJOR(pps_devt), pps->id); | ||
236 | cdev_init(&pps->cdev, &pps_cdev_fops); | ||
237 | pps->cdev.owner = pps->info.owner; | ||
238 | |||
239 | err = cdev_add(&pps->cdev, pps->devno, 1); | ||
240 | if (err) { | ||
241 | printk(KERN_ERR "pps: %s: failed to add char device %d:%d\n", | ||
242 | pps->info.name, MAJOR(pps_devt), pps->id); | ||
243 | return err; | ||
244 | } | ||
245 | pps->dev = device_create(pps_class, pps->info.dev, pps->devno, NULL, | ||
246 | "pps%d", pps->id); | ||
247 | if (err) | ||
248 | goto del_cdev; | ||
249 | dev_set_drvdata(pps->dev, pps); | ||
250 | |||
251 | pr_debug("source %s got cdev (%d:%d)\n", pps->info.name, | ||
252 | MAJOR(pps_devt), pps->id); | ||
253 | |||
254 | return 0; | ||
255 | |||
256 | del_cdev: | ||
257 | cdev_del(&pps->cdev); | ||
258 | |||
259 | return err; | ||
260 | } | ||
261 | |||
262 | void pps_unregister_cdev(struct pps_device *pps) | ||
263 | { | ||
264 | device_destroy(pps_class, pps->devno); | ||
265 | cdev_del(&pps->cdev); | ||
266 | } | ||
267 | |||
268 | /* | ||
269 | * Module stuff | ||
270 | */ | ||
271 | |||
272 | static void __exit pps_exit(void) | ||
273 | { | ||
274 | class_destroy(pps_class); | ||
275 | unregister_chrdev_region(pps_devt, PPS_MAX_SOURCES); | ||
276 | } | ||
277 | |||
278 | static int __init pps_init(void) | ||
279 | { | ||
280 | int err; | ||
281 | |||
282 | pps_class = class_create(THIS_MODULE, "pps"); | ||
283 | if (!pps_class) { | ||
284 | printk(KERN_ERR "pps: failed to allocate class\n"); | ||
285 | return -ENOMEM; | ||
286 | } | ||
287 | pps_class->dev_attrs = pps_attrs; | ||
288 | |||
289 | err = alloc_chrdev_region(&pps_devt, 0, PPS_MAX_SOURCES, "pps"); | ||
290 | if (err < 0) { | ||
291 | printk(KERN_ERR "pps: failed to allocate char device region\n"); | ||
292 | goto remove_class; | ||
293 | } | ||
294 | |||
295 | pr_info("LinuxPPS API ver. %d registered\n", PPS_API_VERS); | ||
296 | pr_info("Software ver. %s - Copyright 2005-2007 Rodolfo Giometti " | ||
297 | "<giometti@linux.it>\n", PPS_VERSION); | ||
298 | |||
299 | return 0; | ||
300 | |||
301 | remove_class: | ||
302 | class_destroy(pps_class); | ||
303 | |||
304 | return err; | ||
305 | } | ||
306 | |||
307 | subsys_initcall(pps_init); | ||
308 | module_exit(pps_exit); | ||
309 | |||
310 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); | ||
311 | MODULE_DESCRIPTION("LinuxPPS support (RFC 2783) - ver. " PPS_VERSION); | ||
312 | MODULE_LICENSE("GPL"); | ||