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 #include "posix-timers.h"
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 __poll_t 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 long posix_clock_ioctl(struct file *fp,
86 unsigned int cmd, unsigned long arg)
88 struct posix_clock *clk = get_posix_clock(fp);
95 err = clk->ops.ioctl(clk, cmd, arg);
103 static long posix_clock_compat_ioctl(struct file *fp,
104 unsigned int cmd, unsigned long arg)
106 struct posix_clock *clk = get_posix_clock(fp);
113 err = clk->ops.ioctl(clk, cmd, arg);
115 put_posix_clock(clk);
121 static int posix_clock_open(struct inode *inode, struct file *fp)
124 struct posix_clock *clk =
125 container_of(inode->i_cdev, struct posix_clock, cdev);
127 down_read(&clk->rwsem);
134 err = clk->ops.open(clk, fp->f_mode);
139 get_device(clk->dev);
140 fp->private_data = clk;
143 up_read(&clk->rwsem);
147 static int posix_clock_release(struct inode *inode, struct file *fp)
149 struct posix_clock *clk = fp->private_data;
152 if (clk->ops.release)
153 err = clk->ops.release(clk);
155 put_device(clk->dev);
157 fp->private_data = NULL;
162 static const struct file_operations posix_clock_file_operations = {
163 .owner = THIS_MODULE,
165 .read = posix_clock_read,
166 .poll = posix_clock_poll,
167 .unlocked_ioctl = posix_clock_ioctl,
168 .open = posix_clock_open,
169 .release = posix_clock_release,
171 .compat_ioctl = posix_clock_compat_ioctl,
175 int posix_clock_register(struct posix_clock *clk, struct device *dev)
179 init_rwsem(&clk->rwsem);
181 cdev_init(&clk->cdev, &posix_clock_file_operations);
182 err = cdev_device_add(&clk->cdev, dev);
184 pr_err("%s unable to add device %d:%d\n",
185 dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
188 clk->cdev.owner = clk->ops.owner;
193 EXPORT_SYMBOL_GPL(posix_clock_register);
195 void posix_clock_unregister(struct posix_clock *clk)
197 cdev_device_del(&clk->cdev, clk->dev);
199 down_write(&clk->rwsem);
201 up_write(&clk->rwsem);
203 put_device(clk->dev);
205 EXPORT_SYMBOL_GPL(posix_clock_unregister);
207 struct posix_clock_desc {
209 struct posix_clock *clk;
212 static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
214 struct file *fp = fget(clockid_to_fd(id));
220 if (fp->f_op->open != posix_clock_open || !fp->private_data)
224 cd->clk = get_posix_clock(fp);
226 err = cd->clk ? 0 : -ENODEV;
233 static void put_clock_desc(struct posix_clock_desc *cd)
235 put_posix_clock(cd->clk);
239 static int pc_clock_adjtime(clockid_t id, struct timex *tx)
241 struct posix_clock_desc cd;
244 err = get_clock_desc(id, &cd);
248 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
253 if (cd.clk->ops.clock_adjtime)
254 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
263 static int pc_clock_gettime(clockid_t id, struct timespec64 *ts)
265 struct posix_clock_desc cd;
268 err = get_clock_desc(id, &cd);
272 if (cd.clk->ops.clock_gettime)
273 err = cd.clk->ops.clock_gettime(cd.clk, ts);
282 static int pc_clock_getres(clockid_t id, struct timespec64 *ts)
284 struct posix_clock_desc cd;
287 err = get_clock_desc(id, &cd);
291 if (cd.clk->ops.clock_getres)
292 err = cd.clk->ops.clock_getres(cd.clk, ts);
301 static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
303 struct posix_clock_desc cd;
306 err = get_clock_desc(id, &cd);
310 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
315 if (cd.clk->ops.clock_settime)
316 err = cd.clk->ops.clock_settime(cd.clk, ts);
325 const struct k_clock clock_posix_dynamic = {
326 .clock_getres = pc_clock_getres,
327 .clock_set = pc_clock_settime,
328 .clock_get = pc_clock_gettime,
329 .clock_adj = pc_clock_adjtime,