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/posix-clock.h>
23 #include <linux/slab.h>
24 #include <linux/syscalls.h>
25 #include <linux/uaccess.h>
27 static void delete_clock(struct kref
*kref
);
30 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
32 static struct posix_clock
*get_posix_clock(struct file
*fp
)
34 struct posix_clock
*clk
= fp
->private_data
;
36 down_read(&clk
->rwsem
);
46 static void put_posix_clock(struct posix_clock
*clk
)
51 static ssize_t
posix_clock_read(struct file
*fp
, char __user
*buf
,
52 size_t count
, loff_t
*ppos
)
54 struct posix_clock
*clk
= get_posix_clock(fp
);
61 err
= clk
->ops
.read(clk
, fp
->f_flags
, buf
, count
);
68 static unsigned int posix_clock_poll(struct file
*fp
, poll_table
*wait
)
70 struct posix_clock
*clk
= get_posix_clock(fp
);
77 result
= clk
->ops
.poll(clk
, fp
, wait
);
84 static int posix_clock_fasync(int fd
, struct file
*fp
, int on
)
86 struct posix_clock
*clk
= get_posix_clock(fp
);
93 err
= clk
->ops
.fasync(clk
, fd
, fp
, on
);
100 static int posix_clock_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
102 struct posix_clock
*clk
= get_posix_clock(fp
);
109 err
= clk
->ops
.mmap(clk
, vma
);
111 put_posix_clock(clk
);
116 static long posix_clock_ioctl(struct file
*fp
,
117 unsigned int cmd
, unsigned long arg
)
119 struct posix_clock
*clk
= get_posix_clock(fp
);
126 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
128 put_posix_clock(clk
);
134 static long posix_clock_compat_ioctl(struct file
*fp
,
135 unsigned int cmd
, unsigned long arg
)
137 struct posix_clock
*clk
= get_posix_clock(fp
);
144 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
146 put_posix_clock(clk
);
152 static int posix_clock_open(struct inode
*inode
, struct file
*fp
)
155 struct posix_clock
*clk
=
156 container_of(inode
->i_cdev
, struct posix_clock
, cdev
);
158 down_read(&clk
->rwsem
);
165 err
= clk
->ops
.open(clk
, fp
->f_mode
);
170 kref_get(&clk
->kref
);
171 fp
->private_data
= clk
;
174 up_read(&clk
->rwsem
);
178 static int posix_clock_release(struct inode
*inode
, struct file
*fp
)
180 struct posix_clock
*clk
= fp
->private_data
;
183 if (clk
->ops
.release
)
184 err
= clk
->ops
.release(clk
);
186 kref_put(&clk
->kref
, delete_clock
);
188 fp
->private_data
= NULL
;
193 static const struct file_operations posix_clock_file_operations
= {
194 .owner
= THIS_MODULE
,
196 .read
= posix_clock_read
,
197 .poll
= posix_clock_poll
,
198 .unlocked_ioctl
= posix_clock_ioctl
,
199 .open
= posix_clock_open
,
200 .release
= posix_clock_release
,
201 .fasync
= posix_clock_fasync
,
202 .mmap
= posix_clock_mmap
,
204 .compat_ioctl
= posix_clock_compat_ioctl
,
208 int posix_clock_register(struct posix_clock
*clk
, dev_t devid
)
212 kref_init(&clk
->kref
);
213 init_rwsem(&clk
->rwsem
);
215 cdev_init(&clk
->cdev
, &posix_clock_file_operations
);
216 clk
->cdev
.owner
= clk
->ops
.owner
;
217 err
= cdev_add(&clk
->cdev
, devid
, 1);
221 EXPORT_SYMBOL_GPL(posix_clock_register
);
223 static void delete_clock(struct kref
*kref
)
225 struct posix_clock
*clk
= container_of(kref
, struct posix_clock
, kref
);
231 void posix_clock_unregister(struct posix_clock
*clk
)
233 cdev_del(&clk
->cdev
);
235 down_write(&clk
->rwsem
);
237 up_write(&clk
->rwsem
);
239 kref_put(&clk
->kref
, delete_clock
);
241 EXPORT_SYMBOL_GPL(posix_clock_unregister
);
243 struct posix_clock_desc
{
245 struct posix_clock
*clk
;
248 static int get_clock_desc(const clockid_t id
, struct posix_clock_desc
*cd
)
250 struct file
*fp
= fget(CLOCKID_TO_FD(id
));
256 if (fp
->f_op
->open
!= posix_clock_open
|| !fp
->private_data
)
260 cd
->clk
= get_posix_clock(fp
);
262 err
= cd
->clk
? 0 : -ENODEV
;
269 static void put_clock_desc(struct posix_clock_desc
*cd
)
271 put_posix_clock(cd
->clk
);
275 static int pc_clock_adjtime(clockid_t id
, struct timex
*tx
)
277 struct posix_clock_desc cd
;
280 err
= get_clock_desc(id
, &cd
);
284 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
289 if (cd
.clk
->ops
.clock_adjtime
)
290 err
= cd
.clk
->ops
.clock_adjtime(cd
.clk
, tx
);
299 static int pc_clock_gettime(clockid_t id
, struct timespec
*ts
)
301 struct posix_clock_desc cd
;
304 err
= get_clock_desc(id
, &cd
);
308 if (cd
.clk
->ops
.clock_gettime
)
309 err
= cd
.clk
->ops
.clock_gettime(cd
.clk
, ts
);
318 static int pc_clock_getres(clockid_t id
, struct timespec
*ts
)
320 struct posix_clock_desc cd
;
323 err
= get_clock_desc(id
, &cd
);
327 if (cd
.clk
->ops
.clock_getres
)
328 err
= cd
.clk
->ops
.clock_getres(cd
.clk
, ts
);
337 static int pc_clock_settime(clockid_t id
, const struct timespec
*ts
)
339 struct posix_clock_desc cd
;
342 err
= get_clock_desc(id
, &cd
);
346 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
351 if (cd
.clk
->ops
.clock_settime
)
352 err
= cd
.clk
->ops
.clock_settime(cd
.clk
, ts
);
361 static int pc_timer_create(struct k_itimer
*kit
)
363 clockid_t id
= kit
->it_clock
;
364 struct posix_clock_desc cd
;
367 err
= get_clock_desc(id
, &cd
);
371 if (cd
.clk
->ops
.timer_create
)
372 err
= cd
.clk
->ops
.timer_create(cd
.clk
, kit
);
381 static int pc_timer_delete(struct k_itimer
*kit
)
383 clockid_t id
= kit
->it_clock
;
384 struct posix_clock_desc cd
;
387 err
= get_clock_desc(id
, &cd
);
391 if (cd
.clk
->ops
.timer_delete
)
392 err
= cd
.clk
->ops
.timer_delete(cd
.clk
, kit
);
401 static void pc_timer_gettime(struct k_itimer
*kit
, struct itimerspec
*ts
)
403 clockid_t id
= kit
->it_clock
;
404 struct posix_clock_desc cd
;
406 if (get_clock_desc(id
, &cd
))
409 if (cd
.clk
->ops
.timer_gettime
)
410 cd
.clk
->ops
.timer_gettime(cd
.clk
, kit
, ts
);
415 static int pc_timer_settime(struct k_itimer
*kit
, int flags
,
416 struct itimerspec
*ts
, struct itimerspec
*old
)
418 clockid_t id
= kit
->it_clock
;
419 struct posix_clock_desc cd
;
422 err
= get_clock_desc(id
, &cd
);
426 if (cd
.clk
->ops
.timer_settime
)
427 err
= cd
.clk
->ops
.timer_settime(cd
.clk
, kit
, flags
, ts
, old
);
436 struct k_clock clock_posix_dynamic
= {
437 .clock_getres
= pc_clock_getres
,
438 .clock_set
= pc_clock_settime
,
439 .clock_get
= pc_clock_gettime
,
440 .clock_adj
= pc_clock_adjtime
,
441 .timer_create
= pc_timer_create
,
442 .timer_set
= pc_timer_settime
,
443 .timer_del
= pc_timer_delete
,
444 .timer_get
= pc_timer_gettime
,