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