blob: e24008c098c6b8ca149afe58e255a8c1303086d9 [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
28static void delete_clock(struct kref *kref);
29
30/*
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
85static int posix_clock_fasync(int fd, struct file *fp, int on)
86{
87 struct posix_clock *clk = get_posix_clock(fp);
88 int err = 0;
89
90 if (!clk)
91 return -ENODEV;
92
93 if (clk->ops.fasync)
94 err = clk->ops.fasync(clk, fd, fp, on);
95
96 put_posix_clock(clk);
97
98 return err;
99}
100
101static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
102{
103 struct posix_clock *clk = get_posix_clock(fp);
104 int err = -ENODEV;
105
106 if (!clk)
107 return -ENODEV;
108
109 if (clk->ops.mmap)
110 err = clk->ops.mmap(clk, vma);
111
112 put_posix_clock(clk);
113
114 return err;
115}
116
117static long posix_clock_ioctl(struct file *fp,
118 unsigned int cmd, unsigned long arg)
119{
120 struct posix_clock *clk = get_posix_clock(fp);
121 int err = -ENOTTY;
122
123 if (!clk)
124 return -ENODEV;
125
126 if (clk->ops.ioctl)
127 err = clk->ops.ioctl(clk, cmd, arg);
128
129 put_posix_clock(clk);
130
131 return err;
132}
133
134#ifdef CONFIG_COMPAT
135static long posix_clock_compat_ioctl(struct file *fp,
136 unsigned int cmd, unsigned long arg)
137{
138 struct posix_clock *clk = get_posix_clock(fp);
139 int err = -ENOTTY;
140
141 if (!clk)
142 return -ENODEV;
143
144 if (clk->ops.ioctl)
145 err = clk->ops.ioctl(clk, cmd, arg);
146
147 put_posix_clock(clk);
148
149 return err;
150}
151#endif
152
153static int posix_clock_open(struct inode *inode, struct file *fp)
154{
155 int err;
156 struct posix_clock *clk =
157 container_of(inode->i_cdev, struct posix_clock, cdev);
158
Richard Cochran1791f882011-03-30 15:24:21 +0200159 down_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000160
161 if (clk->zombie) {
162 err = -ENODEV;
163 goto out;
164 }
165 if (clk->ops.open)
166 err = clk->ops.open(clk, fp->f_mode);
167 else
168 err = 0;
169
170 if (!err) {
171 kref_get(&clk->kref);
172 fp->private_data = clk;
173 }
174out:
Richard Cochran1791f882011-03-30 15:24:21 +0200175 up_read(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000176 return err;
177}
178
179static int posix_clock_release(struct inode *inode, struct file *fp)
180{
181 struct posix_clock *clk = fp->private_data;
182 int err = 0;
183
184 if (clk->ops.release)
185 err = clk->ops.release(clk);
186
187 kref_put(&clk->kref, delete_clock);
188
189 fp->private_data = NULL;
190
191 return err;
192}
193
194static const struct file_operations posix_clock_file_operations = {
195 .owner = THIS_MODULE,
196 .llseek = no_llseek,
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,
204#ifdef CONFIG_COMPAT
205 .compat_ioctl = posix_clock_compat_ioctl,
206#endif
207};
208
209int posix_clock_register(struct posix_clock *clk, dev_t devid)
210{
211 int err;
212
213 kref_init(&clk->kref);
Richard Cochran1791f882011-03-30 15:24:21 +0200214 init_rwsem(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000215
216 cdev_init(&clk->cdev, &posix_clock_file_operations);
217 clk->cdev.owner = clk->ops.owner;
218 err = cdev_add(&clk->cdev, devid, 1);
Richard Cochran0606f422011-02-01 13:52:35 +0000219
220 return err;
Richard Cochran0606f422011-02-01 13:52:35 +0000221}
222EXPORT_SYMBOL_GPL(posix_clock_register);
223
224static void delete_clock(struct kref *kref)
225{
226 struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
Richard Cochran1791f882011-03-30 15:24:21 +0200227
Richard Cochran0606f422011-02-01 13:52:35 +0000228 if (clk->release)
229 clk->release(clk);
230}
231
232void posix_clock_unregister(struct posix_clock *clk)
233{
234 cdev_del(&clk->cdev);
235
Richard Cochran1791f882011-03-30 15:24:21 +0200236 down_write(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000237 clk->zombie = true;
Richard Cochran1791f882011-03-30 15:24:21 +0200238 up_write(&clk->rwsem);
Richard Cochran0606f422011-02-01 13:52:35 +0000239
240 kref_put(&clk->kref, delete_clock);
241}
242EXPORT_SYMBOL_GPL(posix_clock_unregister);
243
244struct posix_clock_desc {
245 struct file *fp;
246 struct posix_clock *clk;
247};
248
249static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
250{
251 struct file *fp = fget(CLOCKID_TO_FD(id));
252 int err = -EINVAL;
253
254 if (!fp)
255 return err;
256
257 if (fp->f_op->open != posix_clock_open || !fp->private_data)
258 goto out;
259
260 cd->fp = fp;
261 cd->clk = get_posix_clock(fp);
262
263 err = cd->clk ? 0 : -ENODEV;
264out:
265 if (err)
266 fput(fp);
267 return err;
268}
269
270static void put_clock_desc(struct posix_clock_desc *cd)
271{
272 put_posix_clock(cd->clk);
273 fput(cd->fp);
274}
275
276static int pc_clock_adjtime(clockid_t id, struct timex *tx)
277{
278 struct posix_clock_desc cd;
279 int err;
280
281 err = get_clock_desc(id, &cd);
282 if (err)
283 return err;
284
Torben Hohn6e6823d2011-03-03 18:26:14 +0100285 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
286 err = -EACCES;
287 goto out;
288 }
289
Richard Cochran0606f422011-02-01 13:52:35 +0000290 if (cd.clk->ops.clock_adjtime)
291 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
292 else
293 err = -EOPNOTSUPP;
Torben Hohn6e6823d2011-03-03 18:26:14 +0100294out:
Richard Cochran0606f422011-02-01 13:52:35 +0000295 put_clock_desc(&cd);
296
297 return err;
298}
299
300static int pc_clock_gettime(clockid_t id, struct timespec *ts)
301{
302 struct posix_clock_desc cd;
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700303 struct timespec64 ts64;
Richard Cochran0606f422011-02-01 13:52:35 +0000304 int err;
305
306 err = get_clock_desc(id, &cd);
307 if (err)
308 return err;
309
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700310 if (cd.clk->ops.clock_gettime) {
311 err = cd.clk->ops.clock_gettime(cd.clk, &ts64);
312 *ts = timespec64_to_timespec(ts64);
313 }
Richard Cochran0606f422011-02-01 13:52:35 +0000314 else
315 err = -EOPNOTSUPP;
316
317 put_clock_desc(&cd);
318
319 return err;
320}
321
322static int pc_clock_getres(clockid_t id, struct timespec *ts)
323{
324 struct posix_clock_desc cd;
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700325 struct timespec64 ts64;
Richard Cochran0606f422011-02-01 13:52:35 +0000326 int err;
327
328 err = get_clock_desc(id, &cd);
329 if (err)
330 return err;
331
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700332 if (cd.clk->ops.clock_getres) {
333 err = cd.clk->ops.clock_getres(cd.clk, &ts64);
334 *ts = timespec64_to_timespec(ts64);
335 }
Richard Cochran0606f422011-02-01 13:52:35 +0000336 else
337 err = -EOPNOTSUPP;
338
339 put_clock_desc(&cd);
340
341 return err;
342}
343
344static int pc_clock_settime(clockid_t id, const struct timespec *ts)
345{
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700346 struct timespec64 ts64 = timespec_to_timespec64(*ts);
Richard Cochran0606f422011-02-01 13:52:35 +0000347 struct posix_clock_desc cd;
348 int err;
349
350 err = get_clock_desc(id, &cd);
351 if (err)
352 return err;
353
Torben Hohn6e6823d2011-03-03 18:26:14 +0100354 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
355 err = -EACCES;
356 goto out;
357 }
358
Richard Cochran0606f422011-02-01 13:52:35 +0000359 if (cd.clk->ops.clock_settime)
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700360 err = cd.clk->ops.clock_settime(cd.clk, &ts64);
Richard Cochran0606f422011-02-01 13:52:35 +0000361 else
362 err = -EOPNOTSUPP;
Torben Hohn6e6823d2011-03-03 18:26:14 +0100363out:
Richard Cochran0606f422011-02-01 13:52:35 +0000364 put_clock_desc(&cd);
365
366 return err;
367}
368
369static int pc_timer_create(struct k_itimer *kit)
370{
371 clockid_t id = kit->it_clock;
372 struct posix_clock_desc cd;
373 int err;
374
375 err = get_clock_desc(id, &cd);
376 if (err)
377 return err;
378
379 if (cd.clk->ops.timer_create)
380 err = cd.clk->ops.timer_create(cd.clk, kit);
381 else
382 err = -EOPNOTSUPP;
383
384 put_clock_desc(&cd);
385
386 return err;
387}
388
389static int pc_timer_delete(struct k_itimer *kit)
390{
391 clockid_t id = kit->it_clock;
392 struct posix_clock_desc cd;
393 int err;
394
395 err = get_clock_desc(id, &cd);
396 if (err)
397 return err;
398
399 if (cd.clk->ops.timer_delete)
400 err = cd.clk->ops.timer_delete(cd.clk, kit);
401 else
402 err = -EOPNOTSUPP;
403
404 put_clock_desc(&cd);
405
406 return err;
407}
408
409static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
410{
411 clockid_t id = kit->it_clock;
412 struct posix_clock_desc cd;
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700413 struct itimerspec64 ts64;
Richard Cochran0606f422011-02-01 13:52:35 +0000414
415 if (get_clock_desc(id, &cd))
416 return;
417
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700418 if (cd.clk->ops.timer_gettime) {
419 cd.clk->ops.timer_gettime(cd.clk, kit, &ts64);
420 *ts = itimerspec64_to_itimerspec(&ts64);
421 }
Richard Cochran0606f422011-02-01 13:52:35 +0000422 put_clock_desc(&cd);
423}
424
425static int pc_timer_settime(struct k_itimer *kit, int flags,
426 struct itimerspec *ts, struct itimerspec *old)
427{
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700428 struct itimerspec64 ts64 = itimerspec_to_itimerspec64(ts);
Richard Cochran0606f422011-02-01 13:52:35 +0000429 clockid_t id = kit->it_clock;
430 struct posix_clock_desc cd;
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700431 struct itimerspec64 old64;
Richard Cochran0606f422011-02-01 13:52:35 +0000432 int err;
433
434 err = get_clock_desc(id, &cd);
435 if (err)
436 return err;
437
Deepa Dinamani58e7fd92017-03-26 12:04:13 -0700438 if (cd.clk->ops.timer_settime) {
439 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, &ts64, &old64);
440 if (old)
441 *old = itimerspec64_to_itimerspec(&old64);
442 }
Richard Cochran0606f422011-02-01 13:52:35 +0000443 else
444 err = -EOPNOTSUPP;
445
446 put_clock_desc(&cd);
447
448 return err;
449}
450
451struct k_clock clock_posix_dynamic = {
452 .clock_getres = pc_clock_getres,
453 .clock_set = pc_clock_settime,
454 .clock_get = pc_clock_gettime,
455 .clock_adj = pc_clock_adjtime,
456 .timer_create = pc_timer_create,
457 .timer_set = pc_timer_settime,
458 .timer_del = pc_timer_delete,
459 .timer_get = pc_timer_gettime,
460};