2 * posix-clock.c - support for dynamic clock devices
4 * Copyright (C) 2010 OMICRON electronics GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/device.h>
21 #include <linux/export.h>
22 #include <linux/file.h>
23 #include <linux/posix-clock.h>
24 #include <linux/slab.h>
25 #include <linux/syscalls.h>
26 #include <linux/uaccess.h>
28 static void delete_clock(struct kref *kref);
31 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
33 static struct posix_clock *get_posix_clock(struct file *fp)
35 struct posix_clock *clk = fp->private_data;
37 down_read(&clk->rwsem);
47 static void put_posix_clock(struct posix_clock *clk)
52 static ssize_t posix_clock_read(struct file *fp, char __user *buf,
53 size_t count, loff_t *ppos)
55 struct posix_clock *clk = get_posix_clock(fp);
62 err = clk->ops.read(clk, fp->f_flags, buf, count);
69 static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
71 struct posix_clock *clk = get_posix_clock(fp);
72 unsigned int result = 0;
78 result = clk->ops.poll(clk, fp, wait);
85 static int posix_clock_fasync(int fd, struct file *fp, int on)
87 struct posix_clock *clk = get_posix_clock(fp);
94 err = clk->ops.fasync(clk, fd, fp, on);
101 static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
103 struct posix_clock *clk = get_posix_clock(fp);
110 err = clk->ops.mmap(clk, vma);
112 put_posix_clock(clk);
117 static long posix_clock_ioctl(struct file *fp,
118 unsigned int cmd, unsigned long arg)
120 struct posix_clock *clk = get_posix_clock(fp);
127 err = clk->ops.ioctl(clk, cmd, arg);
129 put_posix_clock(clk);
135 static long posix_clock_compat_ioctl(struct file *fp,
136 unsigned int cmd, unsigned long arg)
138 struct posix_clock *clk = get_posix_clock(fp);
145 err = clk->ops.ioctl(clk, cmd, arg);
147 put_posix_clock(clk);
153 static int posix_clock_open(struct inode *inode, struct file *fp)
156 struct posix_clock *clk =
157 container_of(inode->i_cdev, struct posix_clock, cdev);
159 down_read(&clk->rwsem);
166 err = clk->ops.open(clk, fp->f_mode);
171 kref_get(&clk->kref);
172 fp->private_data = clk;
175 up_read(&clk->rwsem);
179 static int posix_clock_release(struct inode *inode, struct file *fp)
181 struct posix_clock *clk = fp->private_data;
184 if (clk->ops.release)
185 err = clk->ops.release(clk);
187 kref_put(&clk->kref, delete_clock);
189 fp->private_data = NULL;
194 static const struct file_operations posix_clock_file_operations = {
195 .owner = THIS_MODULE,
197 .read = posix_clock_read,
198 .poll = posix_clock_poll,
199 .unlocked_ioctl = posix_clock_ioctl,
200 .open = posix_clock_open,
201 .release = posix_clock_release,
202 .fasync = posix_clock_fasync,
203 .mmap = posix_clock_mmap,
205 .compat_ioctl = posix_clock_compat_ioctl,
209 int posix_clock_register(struct posix_clock *clk, dev_t devid)
213 kref_init(&clk->kref);
214 init_rwsem(&clk->rwsem);
216 cdev_init(&clk->cdev, &posix_clock_file_operations);
217 clk->cdev.owner = clk->ops.owner;
218 err = cdev_add(&clk->cdev, devid, 1);
222 EXPORT_SYMBOL_GPL(posix_clock_register);
224 static void delete_clock(struct kref *kref)
226 struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
232 void posix_clock_unregister(struct posix_clock *clk)
234 cdev_del(&clk->cdev);
236 down_write(&clk->rwsem);
238 up_write(&clk->rwsem);
240 kref_put(&clk->kref, delete_clock);
242 EXPORT_SYMBOL_GPL(posix_clock_unregister);
244 struct posix_clock_desc {
246 struct posix_clock *clk;
249 static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
251 struct file *fp = fget(CLOCKID_TO_FD(id));
257 if (fp->f_op->open != posix_clock_open || !fp->private_data)
261 cd->clk = get_posix_clock(fp);
263 err = cd->clk ? 0 : -ENODEV;
270 static void put_clock_desc(struct posix_clock_desc *cd)
272 put_posix_clock(cd->clk);
276 static int pc_clock_adjtime(clockid_t id, struct timex *tx)
278 struct posix_clock_desc cd;
281 err = get_clock_desc(id, &cd);
285 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
290 if (cd.clk->ops.clock_adjtime)
291 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
300 static int pc_clock_gettime(clockid_t id, struct timespec *ts)
302 struct posix_clock_desc cd;
305 err = get_clock_desc(id, &cd);
309 if (cd.clk->ops.clock_gettime)
310 err = cd.clk->ops.clock_gettime(cd.clk, ts);
319 static int pc_clock_getres(clockid_t id, struct timespec *ts)
321 struct posix_clock_desc cd;
324 err = get_clock_desc(id, &cd);
328 if (cd.clk->ops.clock_getres)
329 err = cd.clk->ops.clock_getres(cd.clk, ts);
338 static int pc_clock_settime(clockid_t id, const struct timespec *ts)
340 struct posix_clock_desc cd;
343 err = get_clock_desc(id, &cd);
347 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
352 if (cd.clk->ops.clock_settime)
353 err = cd.clk->ops.clock_settime(cd.clk, ts);
362 static int pc_timer_create(struct k_itimer *kit)
364 clockid_t id = kit->it_clock;
365 struct posix_clock_desc cd;
368 err = get_clock_desc(id, &cd);
372 if (cd.clk->ops.timer_create)
373 err = cd.clk->ops.timer_create(cd.clk, kit);
382 static int pc_timer_delete(struct k_itimer *kit)
384 clockid_t id = kit->it_clock;
385 struct posix_clock_desc cd;
388 err = get_clock_desc(id, &cd);
392 if (cd.clk->ops.timer_delete)
393 err = cd.clk->ops.timer_delete(cd.clk, kit);
402 static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
404 clockid_t id = kit->it_clock;
405 struct posix_clock_desc cd;
407 if (get_clock_desc(id, &cd))
410 if (cd.clk->ops.timer_gettime)
411 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
416 static int pc_timer_settime(struct k_itimer *kit, int flags,
417 struct itimerspec *ts, struct itimerspec *old)
419 clockid_t id = kit->it_clock;
420 struct posix_clock_desc cd;
423 err = get_clock_desc(id, &cd);
427 if (cd.clk->ops.timer_settime)
428 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
437 struct k_clock clock_posix_dynamic = {
438 .clock_getres = pc_clock_getres,
439 .clock_set = pc_clock_settime,
440 .clock_get = pc_clock_gettime,
441 .clock_adj = pc_clock_adjtime,
442 .timer_create = pc_timer_create,
443 .timer_set = pc_timer_settime,
444 .timer_del = pc_timer_delete,
445 .timer_get = pc_timer_gettime,