blob: e5706a826c1f00d86efbd604b2134562ed1496a8 [file] [log] [blame]
Richard Cochran0606f422011-02-01 13:52:35 +00001/*
2 * posix-clock.c - support for dynamic clock devices
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
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.
10 *
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.
15 *
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.
19 */
20#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040021#include <linux/export.h>
Richard Cochran0606f422011-02-01 13:52:35 +000022#include <linux/file.h>
Richard Cochran0606f422011-02-01 13:52:35 +000023#include <linux/posix-clock.h>
24#include <linux/slab.h>
25#include <linux/syscalls.h>
26#include <linux/uaccess.h>
27
Thomas Gleixnerbab0aae2017-05-30 23:15:41 +020028#include "posix-timers.h"
29
Richard Cochran0606f422011-02-01 13:52:35 +000030/*
31 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
32 */
33static struct posix_clock *get_posix_clock(struct file *fp)
34{
35 struct posix_clock *clk = fp->private_data;
36
Richard Cochran1791f882011-03-30 15:24:21 +020037 down_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +000038
39 if (!clk->zombie)
40 return clk;
41
Richard Cochran1791f882011-03-30 15:24:21 +020042 up_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +000043
44 return NULL;
45}
46
47static void put_posix_clock(struct posix_clock *clk)
48{
Richard Cochran1791f882011-03-30 15:24:21 +020049 up_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +000050}
51
52static ssize_t posix_clock_read(struct file *fp, char __user *buf,
53 size_t count, loff_t *ppos)
54{
55 struct posix_clock *clk = get_posix_clock(fp);
56 int err = -EINVAL;
57
58 if (!clk)
59 return -ENODEV;
60
61 if (clk->ops.read)
62 err = clk->ops.read(clk, fp->f_flags, buf, count);
63
64 put_posix_clock(clk);
65
66 return err;
67}
68
69static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
70{
71 struct posix_clock *clk = get_posix_clock(fp);
Richard Cochran1b9f2372015-12-22 22:19:58 +010072 unsigned int result = 0;
Richard Cochran0606f422011-02-01 13:52:35 +000073
74 if (!clk)
Richard Cochran1b9f2372015-12-22 22:19:58 +010075 return POLLERR;
Richard Cochran0606f422011-02-01 13:52:35 +000076
77 if (clk->ops.poll)
78 result = clk->ops.poll(clk, fp, wait);
79
80 put_posix_clock(clk);
81
82 return result;
83}
84
Richard Cochran0606f422011-02-01 13:52:35 +000085static long posix_clock_ioctl(struct file *fp,
86 unsigned int cmd, unsigned long arg)
87{
88 struct posix_clock *clk = get_posix_clock(fp);
89 int err = -ENOTTY;
90
91 if (!clk)
92 return -ENODEV;
93
94 if (clk->ops.ioctl)
95 err = clk->ops.ioctl(clk, cmd, arg);
96
97 put_posix_clock(clk);
98
99 return err;
100}
101
102#ifdef CONFIG_COMPAT
103static long posix_clock_compat_ioctl(struct file *fp,
104 unsigned int cmd, unsigned long arg)
105{
106 struct posix_clock *clk = get_posix_clock(fp);
107 int err = -ENOTTY;
108
109 if (!clk)
110 return -ENODEV;
111
112 if (clk->ops.ioctl)
113 err = clk->ops.ioctl(clk, cmd, arg);
114
115 put_posix_clock(clk);
116
117 return err;
118}
119#endif
120
121static int posix_clock_open(struct inode *inode, struct file *fp)
122{
123 int err;
124 struct posix_clock *clk =
125 container_of(inode->i_cdev, struct posix_clock, cdev);
126
Richard Cochran1791f882011-03-30 15:24:21 +0200127 down_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000128
129 if (clk->zombie) {
130 err = -ENODEV;
131 goto out;
132 }
133 if (clk->ops.open)
134 err = clk->ops.open(clk, fp->f_mode);
135 else
136 err = 0;
137
138 if (!err) {
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100139 get_device(clk->dev);
Richard Cochran0606f422011-02-01 13:52:35 +0000140 fp->private_data = clk;
141 }
142out:
Richard Cochran1791f882011-03-30 15:24:21 +0200143 up_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000144 return err;
145}
146
147static int posix_clock_release(struct inode *inode, struct file *fp)
148{
149 struct posix_clock *clk = fp->private_data;
150 int err = 0;
151
152 if (clk->ops.release)
153 err = clk->ops.release(clk);
154
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100155 put_device(clk->dev);
Richard Cochran0606f422011-02-01 13:52:35 +0000156
157 fp->private_data = NULL;
158
159 return err;
160}
161
162static const struct file_operations posix_clock_file_operations = {
163 .owner = THIS_MODULE,
164 .llseek = no_llseek,
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,
Richard Cochran0606f422011-02-01 13:52:35 +0000170#ifdef CONFIG_COMPAT
171 .compat_ioctl = posix_clock_compat_ioctl,
172#endif
173};
174
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100175int posix_clock_register(struct posix_clock *clk, struct device *dev)
Richard Cochran0606f422011-02-01 13:52:35 +0000176{
177 int err;
178
Richard Cochran1791f882011-03-30 15:24:21 +0200179 init_rwsem(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000180
181 cdev_init(&clk->cdev, &posix_clock_file_operations);
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100182 err = cdev_device_add(&clk->cdev, dev);
183 if (err) {
184 pr_err("%s unable to add device %d:%d\n",
185 dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
186 return err;
187 }
Richard Cochran0606f422011-02-01 13:52:35 +0000188 clk->cdev.owner = clk->ops.owner;
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100189 clk->dev = dev;
Richard Cochran0606f422011-02-01 13:52:35 +0000190
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100191 return 0;
Richard Cochran0606f422011-02-01 13:52:35 +0000192}
193EXPORT_SYMBOL_GPL(posix_clock_register);
194
Richard Cochran0606f422011-02-01 13:52:35 +0000195void posix_clock_unregister(struct posix_clock *clk)
196{
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100197 cdev_device_del(&clk->cdev, clk->dev);
Richard Cochran0606f422011-02-01 13:52:35 +0000198
Richard Cochran1791f882011-03-30 15:24:21 +0200199 down_write(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000200 clk->zombie = true;
Richard Cochran1791f882011-03-30 15:24:21 +0200201 up_write(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000202
Vladis Dronov2dece4d2019-12-27 03:26:27 +0100203 put_device(clk->dev);
Richard Cochran0606f422011-02-01 13:52:35 +0000204}
205EXPORT_SYMBOL_GPL(posix_clock_unregister);
206
207struct posix_clock_desc {
208 struct file *fp;
209 struct posix_clock *clk;
210};
211
212static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
213{
214 struct file *fp = fget(CLOCKID_TO_FD(id));
215 int err = -EINVAL;
216
217 if (!fp)
218 return err;
219
220 if (fp->f_op->open != posix_clock_open || !fp->private_data)
221 goto out;
222
223 cd->fp = fp;
224 cd->clk = get_posix_clock(fp);
225
226 err = cd->clk ? 0 : -ENODEV;
227out:
228 if (err)
229 fput(fp);
230 return err;
231}
232
233static void put_clock_desc(struct posix_clock_desc *cd)
234{
235 put_posix_clock(cd->clk);
236 fput(cd->fp);
237}
238
239static int pc_clock_adjtime(clockid_t id, struct timex *tx)
240{
241 struct posix_clock_desc cd;
242 int err;
243
244 err = get_clock_desc(id, &cd);
245 if (err)
246 return err;
247
Torben Hohn6e6823d2011-03-03 18:26:14 +0100248 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
249 err = -EACCES;
250 goto out;
251 }
252
Richard Cochran0606f422011-02-01 13:52:35 +0000253 if (cd.clk->ops.clock_adjtime)
254 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
255 else
256 err = -EOPNOTSUPP;
Torben Hohn6e6823d2011-03-03 18:26:14 +0100257out:
Richard Cochran0606f422011-02-01 13:52:35 +0000258 put_clock_desc(&cd);
259
260 return err;
261}
262
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700263static int pc_clock_gettime(clockid_t id, struct timespec64 *ts)
Richard Cochran0606f422011-02-01 13:52:35 +0000264{
265 struct posix_clock_desc cd;
266 int err;
267
268 err = get_clock_desc(id, &cd);
269 if (err)
270 return err;
271
Deepa Dinamani3c9c12f2017-03-26 12:04:14 -0700272 if (cd.clk->ops.clock_gettime)
273 err = cd.clk->ops.clock_gettime(cd.clk, ts);
Richard Cochran0606f422011-02-01 13:52:35 +0000274 else
275 err = -EOPNOTSUPP;
276
277 put_clock_desc(&cd);
278
279 return err;
280}
281
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700282static int pc_clock_getres(clockid_t id, struct timespec64 *ts)
Richard Cochran0606f422011-02-01 13:52:35 +0000283{
284 struct posix_clock_desc cd;
285 int err;
286
287 err = get_clock_desc(id, &cd);
288 if (err)
289 return err;
290
Deepa Dinamanid2e3e0c2017-03-26 12:04:15 -0700291 if (cd.clk->ops.clock_getres)
292 err = cd.clk->ops.clock_getres(cd.clk, ts);
Richard Cochran0606f422011-02-01 13:52:35 +0000293 else
294 err = -EOPNOTSUPP;
295
296 put_clock_desc(&cd);
297
298 return err;
299}
300
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700301static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
Richard Cochran0606f422011-02-01 13:52:35 +0000302{
303 struct posix_clock_desc cd;
304 int err;
305
306 err = get_clock_desc(id, &cd);
307 if (err)
308 return err;
309
Torben Hohn6e6823d2011-03-03 18:26:14 +0100310 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
311 err = -EACCES;
312 goto out;
313 }
314
Richard Cochran0606f422011-02-01 13:52:35 +0000315 if (cd.clk->ops.clock_settime)
Deepa Dinamani0fe6afe2017-03-26 12:04:16 -0700316 err = cd.clk->ops.clock_settime(cd.clk, ts);
Richard Cochran0606f422011-02-01 13:52:35 +0000317 else
318 err = -EOPNOTSUPP;
Torben Hohn6e6823d2011-03-03 18:26:14 +0100319out:
Richard Cochran0606f422011-02-01 13:52:35 +0000320 put_clock_desc(&cd);
321
322 return err;
323}
324
Christoph Hellwigd3ba5a92017-05-26 12:03:11 +0300325const struct k_clock clock_posix_dynamic = {
Richard Cochran0606f422011-02-01 13:52:35 +0000326 .clock_getres = pc_clock_getres,
327 .clock_set = pc_clock_settime,
328 .clock_get = pc_clock_gettime,
329 .clock_adj = pc_clock_adjtime,
Richard Cochran0606f422011-02-01 13:52:35 +0000330};