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