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>
29 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
31 static struct posix_clock
*get_posix_clock(struct file
*fp
)
33 struct posix_clock
*clk
= fp
->private_data
;
35 down_read(&clk
->rwsem
);
45 static void put_posix_clock(struct posix_clock
*clk
)
50 static ssize_t
posix_clock_read(struct file
*fp
, char __user
*buf
,
51 size_t count
, loff_t
*ppos
)
53 struct posix_clock
*clk
= get_posix_clock(fp
);
60 err
= clk
->ops
.read(clk
, fp
->f_flags
, buf
, count
);
67 static unsigned int posix_clock_poll(struct file
*fp
, poll_table
*wait
)
69 struct posix_clock
*clk
= get_posix_clock(fp
);
70 unsigned int result
= 0;
76 result
= clk
->ops
.poll(clk
, fp
, wait
);
83 static int posix_clock_fasync(int fd
, struct file
*fp
, int on
)
85 struct posix_clock
*clk
= get_posix_clock(fp
);
92 err
= clk
->ops
.fasync(clk
, fd
, fp
, on
);
99 static int posix_clock_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
101 struct posix_clock
*clk
= get_posix_clock(fp
);
108 err
= clk
->ops
.mmap(clk
, vma
);
110 put_posix_clock(clk
);
115 static long posix_clock_ioctl(struct file
*fp
,
116 unsigned int cmd
, unsigned long arg
)
118 struct posix_clock
*clk
= get_posix_clock(fp
);
125 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
127 put_posix_clock(clk
);
133 static long posix_clock_compat_ioctl(struct file
*fp
,
134 unsigned int cmd
, unsigned long arg
)
136 struct posix_clock
*clk
= get_posix_clock(fp
);
143 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
145 put_posix_clock(clk
);
151 static int posix_clock_open(struct inode
*inode
, struct file
*fp
)
154 struct posix_clock
*clk
=
155 container_of(inode
->i_cdev
, struct posix_clock
, cdev
);
157 down_read(&clk
->rwsem
);
164 err
= clk
->ops
.open(clk
, fp
->f_mode
);
169 get_device(clk
->dev
);
170 fp
->private_data
= clk
;
173 up_read(&clk
->rwsem
);
177 static int posix_clock_release(struct inode
*inode
, struct file
*fp
)
179 struct posix_clock
*clk
= fp
->private_data
;
182 if (clk
->ops
.release
)
183 err
= clk
->ops
.release(clk
);
185 put_device(clk
->dev
);
187 fp
->private_data
= NULL
;
192 static const struct file_operations posix_clock_file_operations
= {
193 .owner
= THIS_MODULE
,
195 .read
= posix_clock_read
,
196 .poll
= posix_clock_poll
,
197 .unlocked_ioctl
= posix_clock_ioctl
,
198 .open
= posix_clock_open
,
199 .release
= posix_clock_release
,
200 .fasync
= posix_clock_fasync
,
201 .mmap
= posix_clock_mmap
,
203 .compat_ioctl
= posix_clock_compat_ioctl
,
207 int posix_clock_register(struct posix_clock
*clk
, struct device
*dev
)
211 init_rwsem(&clk
->rwsem
);
213 cdev_init(&clk
->cdev
, &posix_clock_file_operations
);
214 err
= cdev_device_add(&clk
->cdev
, dev
);
216 pr_err("%s unable to add device %d:%d\n",
217 dev_name(dev
), MAJOR(dev
->devt
), MINOR(dev
->devt
));
220 clk
->cdev
.owner
= clk
->ops
.owner
;
225 EXPORT_SYMBOL_GPL(posix_clock_register
);
227 void posix_clock_unregister(struct posix_clock
*clk
)
229 cdev_device_del(&clk
->cdev
, clk
->dev
);
231 down_write(&clk
->rwsem
);
233 up_write(&clk
->rwsem
);
235 put_device(clk
->dev
);
237 EXPORT_SYMBOL_GPL(posix_clock_unregister
);
239 struct posix_clock_desc
{
241 struct posix_clock
*clk
;
244 static int get_clock_desc(const clockid_t id
, struct posix_clock_desc
*cd
)
246 struct file
*fp
= fget(CLOCKID_TO_FD(id
));
252 if (fp
->f_op
->open
!= posix_clock_open
|| !fp
->private_data
)
256 cd
->clk
= get_posix_clock(fp
);
258 err
= cd
->clk
? 0 : -ENODEV
;
265 static void put_clock_desc(struct posix_clock_desc
*cd
)
267 put_posix_clock(cd
->clk
);
271 static int pc_clock_adjtime(clockid_t id
, struct timex
*tx
)
273 struct posix_clock_desc cd
;
276 err
= get_clock_desc(id
, &cd
);
280 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
285 if (cd
.clk
->ops
.clock_adjtime
)
286 err
= cd
.clk
->ops
.clock_adjtime(cd
.clk
, tx
);
295 static int pc_clock_gettime(clockid_t id
, struct timespec
*ts
)
297 struct posix_clock_desc cd
;
298 struct timespec64 ts64
;
301 err
= get_clock_desc(id
, &cd
);
305 if (cd
.clk
->ops
.clock_gettime
) {
306 err
= cd
.clk
->ops
.clock_gettime(cd
.clk
, &ts64
);
307 *ts
= timespec64_to_timespec(ts64
);
317 static int pc_clock_getres(clockid_t id
, struct timespec
*ts
)
319 struct posix_clock_desc cd
;
320 struct timespec64 ts64
;
323 err
= get_clock_desc(id
, &cd
);
327 if (cd
.clk
->ops
.clock_getres
) {
328 err
= cd
.clk
->ops
.clock_getres(cd
.clk
, &ts64
);
329 *ts
= timespec64_to_timespec(ts64
);
339 static int pc_clock_settime(clockid_t id
, const struct timespec
*ts
)
341 struct timespec64 ts64
= timespec_to_timespec64(*ts
);
342 struct posix_clock_desc cd
;
345 err
= get_clock_desc(id
, &cd
);
349 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
354 if (cd
.clk
->ops
.clock_settime
)
355 err
= cd
.clk
->ops
.clock_settime(cd
.clk
, &ts64
);
364 static int pc_timer_create(struct k_itimer
*kit
)
366 clockid_t id
= kit
->it_clock
;
367 struct posix_clock_desc cd
;
370 err
= get_clock_desc(id
, &cd
);
374 if (cd
.clk
->ops
.timer_create
)
375 err
= cd
.clk
->ops
.timer_create(cd
.clk
, kit
);
384 static int pc_timer_delete(struct k_itimer
*kit
)
386 clockid_t id
= kit
->it_clock
;
387 struct posix_clock_desc cd
;
390 err
= get_clock_desc(id
, &cd
);
394 if (cd
.clk
->ops
.timer_delete
)
395 err
= cd
.clk
->ops
.timer_delete(cd
.clk
, kit
);
404 static void pc_timer_gettime(struct k_itimer
*kit
, struct itimerspec
*ts
)
406 clockid_t id
= kit
->it_clock
;
407 struct posix_clock_desc cd
;
408 struct itimerspec64 ts64
;
410 if (get_clock_desc(id
, &cd
))
413 if (cd
.clk
->ops
.timer_gettime
) {
414 cd
.clk
->ops
.timer_gettime(cd
.clk
, kit
, &ts64
);
415 *ts
= itimerspec64_to_itimerspec(&ts64
);
420 static int pc_timer_settime(struct k_itimer
*kit
, int flags
,
421 struct itimerspec
*ts
, struct itimerspec
*old
)
423 struct itimerspec64 ts64
= itimerspec_to_itimerspec64(ts
);
424 clockid_t id
= kit
->it_clock
;
425 struct posix_clock_desc cd
;
426 struct itimerspec64 old64
;
429 err
= get_clock_desc(id
, &cd
);
433 if (cd
.clk
->ops
.timer_settime
) {
434 err
= cd
.clk
->ops
.timer_settime(cd
.clk
, kit
, flags
, &ts64
, &old64
);
436 *old
= itimerspec64_to_itimerspec(&old64
);
446 struct k_clock clock_posix_dynamic
= {
447 .clock_getres
= pc_clock_getres
,
448 .clock_set
= pc_clock_settime
,
449 .clock_get
= pc_clock_gettime
,
450 .clock_adj
= pc_clock_adjtime
,
451 .timer_create
= pc_timer_create
,
452 .timer_set
= pc_timer_settime
,
453 .timer_del
= pc_timer_delete
,
454 .timer_get
= pc_timer_gettime
,