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/file.h>
22 #include <linux/mutex.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 mutex_lock(&clk->mutex);
42 mutex_unlock(&clk->mutex);
47 static void put_posix_clock(struct posix_clock *clk)
49 mutex_unlock(&clk->mutex);
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);
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 mutex_lock(&clk->mutex);
166 err = clk->ops.open(clk, fp->f_mode);
171 kref_get(&clk->kref);
172 fp->private_data = clk;
175 mutex_unlock(&clk->mutex);
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 mutex_init(&clk->mutex);
216 cdev_init(&clk->cdev, &posix_clock_file_operations);
217 clk->cdev.owner = clk->ops.owner;
218 err = cdev_add(&clk->cdev, devid, 1);
224 mutex_destroy(&clk->mutex);
227 EXPORT_SYMBOL_GPL(posix_clock_register);
229 static void delete_clock(struct kref *kref)
231 struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
232 mutex_destroy(&clk->mutex);
237 void posix_clock_unregister(struct posix_clock *clk)
239 cdev_del(&clk->cdev);
241 mutex_lock(&clk->mutex);
243 mutex_unlock(&clk->mutex);
245 kref_put(&clk->kref, delete_clock);
247 EXPORT_SYMBOL_GPL(posix_clock_unregister);
249 struct posix_clock_desc {
251 struct posix_clock *clk;
254 static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
256 struct file *fp = fget(CLOCKID_TO_FD(id));
262 if (fp->f_op->open != posix_clock_open || !fp->private_data)
266 cd->clk = get_posix_clock(fp);
268 err = cd->clk ? 0 : -ENODEV;
275 static void put_clock_desc(struct posix_clock_desc *cd)
277 put_posix_clock(cd->clk);
281 static int pc_clock_adjtime(clockid_t id, struct timex *tx)
283 struct posix_clock_desc cd;
286 err = get_clock_desc(id, &cd);
290 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
295 if (cd.clk->ops.clock_adjtime)
296 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
305 static int pc_clock_gettime(clockid_t id, struct timespec *ts)
307 struct posix_clock_desc cd;
310 err = get_clock_desc(id, &cd);
314 if (cd.clk->ops.clock_gettime)
315 err = cd.clk->ops.clock_gettime(cd.clk, ts);
324 static int pc_clock_getres(clockid_t id, struct timespec *ts)
326 struct posix_clock_desc cd;
329 err = get_clock_desc(id, &cd);
333 if (cd.clk->ops.clock_getres)
334 err = cd.clk->ops.clock_getres(cd.clk, ts);
343 static int pc_clock_settime(clockid_t id, const struct timespec *ts)
345 struct posix_clock_desc cd;
348 err = get_clock_desc(id, &cd);
352 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
357 if (cd.clk->ops.clock_settime)
358 err = cd.clk->ops.clock_settime(cd.clk, ts);
367 static int pc_timer_create(struct k_itimer *kit)
369 clockid_t id = kit->it_clock;
370 struct posix_clock_desc cd;
373 err = get_clock_desc(id, &cd);
377 if (cd.clk->ops.timer_create)
378 err = cd.clk->ops.timer_create(cd.clk, kit);
387 static int pc_timer_delete(struct k_itimer *kit)
389 clockid_t id = kit->it_clock;
390 struct posix_clock_desc cd;
393 err = get_clock_desc(id, &cd);
397 if (cd.clk->ops.timer_delete)
398 err = cd.clk->ops.timer_delete(cd.clk, kit);
407 static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
409 clockid_t id = kit->it_clock;
410 struct posix_clock_desc cd;
412 if (get_clock_desc(id, &cd))
415 if (cd.clk->ops.timer_gettime)
416 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
421 static int pc_timer_settime(struct k_itimer *kit, int flags,
422 struct itimerspec *ts, struct itimerspec *old)
424 clockid_t id = kit->it_clock;
425 struct posix_clock_desc cd;
428 err = get_clock_desc(id, &cd);
432 if (cd.clk->ops.timer_settime)
433 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
442 struct k_clock clock_posix_dynamic = {
443 .clock_getres = pc_clock_getres,
444 .clock_set = pc_clock_settime,
445 .clock_get = pc_clock_gettime,
446 .clock_adj = pc_clock_adjtime,
447 .timer_create = pc_timer_create,
448 .timer_set = pc_timer_settime,
449 .timer_del = pc_timer_delete,
450 .timer_get = pc_timer_gettime,