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"
30 static void delete_clock(struct kref
*kref
);
33 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
35 static struct posix_clock
*get_posix_clock(struct file
*fp
)
37 struct posix_clock
*clk
= fp
->private_data
;
39 down_read(&clk
->rwsem
);
49 static void put_posix_clock(struct posix_clock
*clk
)
54 static ssize_t
posix_clock_read(struct file
*fp
, char __user
*buf
,
55 size_t count
, loff_t
*ppos
)
57 struct posix_clock
*clk
= get_posix_clock(fp
);
64 err
= clk
->ops
.read(clk
, fp
->f_flags
, buf
, count
);
71 static unsigned int posix_clock_poll(struct file
*fp
, poll_table
*wait
)
73 struct posix_clock
*clk
= get_posix_clock(fp
);
74 unsigned int result
= 0;
80 result
= clk
->ops
.poll(clk
, fp
, wait
);
87 static long posix_clock_ioctl(struct file
*fp
,
88 unsigned int cmd
, unsigned long arg
)
90 struct posix_clock
*clk
= get_posix_clock(fp
);
97 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
105 static long posix_clock_compat_ioctl(struct file
*fp
,
106 unsigned int cmd
, unsigned long arg
)
108 struct posix_clock
*clk
= get_posix_clock(fp
);
115 err
= clk
->ops
.ioctl(clk
, cmd
, arg
);
117 put_posix_clock(clk
);
123 static int posix_clock_open(struct inode
*inode
, struct file
*fp
)
126 struct posix_clock
*clk
=
127 container_of(inode
->i_cdev
, struct posix_clock
, cdev
);
129 down_read(&clk
->rwsem
);
136 err
= clk
->ops
.open(clk
, fp
->f_mode
);
141 kref_get(&clk
->kref
);
142 fp
->private_data
= clk
;
145 up_read(&clk
->rwsem
);
149 static int posix_clock_release(struct inode
*inode
, struct file
*fp
)
151 struct posix_clock
*clk
= fp
->private_data
;
154 if (clk
->ops
.release
)
155 err
= clk
->ops
.release(clk
);
157 kref_put(&clk
->kref
, delete_clock
);
159 fp
->private_data
= NULL
;
164 static const struct file_operations posix_clock_file_operations
= {
165 .owner
= THIS_MODULE
,
167 .read
= posix_clock_read
,
168 .poll
= posix_clock_poll
,
169 .unlocked_ioctl
= posix_clock_ioctl
,
170 .open
= posix_clock_open
,
171 .release
= posix_clock_release
,
173 .compat_ioctl
= posix_clock_compat_ioctl
,
177 int posix_clock_register(struct posix_clock
*clk
, dev_t devid
)
181 kref_init(&clk
->kref
);
182 init_rwsem(&clk
->rwsem
);
184 cdev_init(&clk
->cdev
, &posix_clock_file_operations
);
185 clk
->cdev
.owner
= clk
->ops
.owner
;
186 err
= cdev_add(&clk
->cdev
, devid
, 1);
190 EXPORT_SYMBOL_GPL(posix_clock_register
);
192 static void delete_clock(struct kref
*kref
)
194 struct posix_clock
*clk
= container_of(kref
, struct posix_clock
, kref
);
200 void posix_clock_unregister(struct posix_clock
*clk
)
202 cdev_del(&clk
->cdev
);
204 down_write(&clk
->rwsem
);
206 up_write(&clk
->rwsem
);
208 kref_put(&clk
->kref
, delete_clock
);
210 EXPORT_SYMBOL_GPL(posix_clock_unregister
);
212 struct posix_clock_desc
{
214 struct posix_clock
*clk
;
217 static int get_clock_desc(const clockid_t id
, struct posix_clock_desc
*cd
)
219 struct file
*fp
= fget(CLOCKID_TO_FD(id
));
225 if (fp
->f_op
->open
!= posix_clock_open
|| !fp
->private_data
)
229 cd
->clk
= get_posix_clock(fp
);
231 err
= cd
->clk
? 0 : -ENODEV
;
238 static void put_clock_desc(struct posix_clock_desc
*cd
)
240 put_posix_clock(cd
->clk
);
244 static int pc_clock_adjtime(clockid_t id
, struct timex
*tx
)
246 struct posix_clock_desc cd
;
249 err
= get_clock_desc(id
, &cd
);
253 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
258 if (cd
.clk
->ops
.clock_adjtime
)
259 err
= cd
.clk
->ops
.clock_adjtime(cd
.clk
, tx
);
268 static int pc_clock_gettime(clockid_t id
, struct timespec64
*ts
)
270 struct posix_clock_desc cd
;
273 err
= get_clock_desc(id
, &cd
);
277 if (cd
.clk
->ops
.clock_gettime
)
278 err
= cd
.clk
->ops
.clock_gettime(cd
.clk
, ts
);
287 static int pc_clock_getres(clockid_t id
, struct timespec64
*ts
)
289 struct posix_clock_desc cd
;
292 err
= get_clock_desc(id
, &cd
);
296 if (cd
.clk
->ops
.clock_getres
)
297 err
= cd
.clk
->ops
.clock_getres(cd
.clk
, ts
);
306 static int pc_clock_settime(clockid_t id
, const struct timespec64
*ts
)
308 struct posix_clock_desc cd
;
311 err
= get_clock_desc(id
, &cd
);
315 if ((cd
.fp
->f_mode
& FMODE_WRITE
) == 0) {
320 if (cd
.clk
->ops
.clock_settime
)
321 err
= cd
.clk
->ops
.clock_settime(cd
.clk
, ts
);
330 const struct k_clock clock_posix_dynamic
= {
331 .clock_getres
= pc_clock_getres
,
332 .clock_set
= pc_clock_settime
,
333 .clock_get
= pc_clock_gettime
,
334 .clock_adj
= pc_clock_adjtime
,