2018-10-31 18:21:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-02-01 13:52:35 +00:00
|
|
|
/*
|
2018-10-31 18:21:08 +00:00
|
|
|
* Support for dynamic clock devices
|
2011-02-01 13:52:35 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 OMICRON electronics GmbH
|
|
|
|
*/
|
|
|
|
#include <linux/device.h>
|
2011-05-26 20:00:52 +00:00
|
|
|
#include <linux/export.h>
|
2011-02-01 13:52:35 +00:00
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/posix-clock.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
|
2017-05-30 21:15:41 +00:00
|
|
|
#include "posix-timers.h"
|
|
|
|
|
2011-02-01 13:52:35 +00:00
|
|
|
/*
|
|
|
|
* Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
|
|
|
|
*/
|
|
|
|
static struct posix_clock *get_posix_clock(struct file *fp)
|
|
|
|
{
|
|
|
|
struct posix_clock *clk = fp->private_data;
|
|
|
|
|
2011-03-30 13:24:21 +00:00
|
|
|
down_read(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
if (!clk->zombie)
|
|
|
|
return clk;
|
|
|
|
|
2011-03-30 13:24:21 +00:00
|
|
|
up_read(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_posix_clock(struct posix_clock *clk)
|
|
|
|
{
|
2011-03-30 13:24:21 +00:00
|
|
|
up_read(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t posix_clock_read(struct file *fp, char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct posix_clock *clk = get_posix_clock(fp);
|
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
if (!clk)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (clk->ops.read)
|
|
|
|
err = clk->ops.read(clk, fp->f_flags, buf, count);
|
|
|
|
|
|
|
|
put_posix_clock(clk);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-07-03 04:42:43 +00:00
|
|
|
static __poll_t posix_clock_poll(struct file *fp, poll_table *wait)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
struct posix_clock *clk = get_posix_clock(fp);
|
2017-07-03 04:42:43 +00:00
|
|
|
__poll_t result = 0;
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
if (!clk)
|
2018-02-11 22:34:03 +00:00
|
|
|
return EPOLLERR;
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
if (clk->ops.poll)
|
|
|
|
result = clk->ops.poll(clk, fp, wait);
|
|
|
|
|
|
|
|
put_posix_clock(clk);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long posix_clock_ioctl(struct file *fp,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct posix_clock *clk = get_posix_clock(fp);
|
|
|
|
int err = -ENOTTY;
|
|
|
|
|
|
|
|
if (!clk)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (clk->ops.ioctl)
|
|
|
|
err = clk->ops.ioctl(clk, cmd, arg);
|
|
|
|
|
|
|
|
put_posix_clock(clk);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static long posix_clock_compat_ioctl(struct file *fp,
|
|
|
|
unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct posix_clock *clk = get_posix_clock(fp);
|
|
|
|
int err = -ENOTTY;
|
|
|
|
|
|
|
|
if (!clk)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (clk->ops.ioctl)
|
|
|
|
err = clk->ops.ioctl(clk, cmd, arg);
|
|
|
|
|
|
|
|
put_posix_clock(clk);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int posix_clock_open(struct inode *inode, struct file *fp)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct posix_clock *clk =
|
|
|
|
container_of(inode->i_cdev, struct posix_clock, cdev);
|
|
|
|
|
2011-03-30 13:24:21 +00:00
|
|
|
down_read(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
if (clk->zombie) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (clk->ops.open)
|
|
|
|
err = clk->ops.open(clk, fp->f_mode);
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
if (!err) {
|
2019-12-27 02:26:27 +00:00
|
|
|
get_device(clk->dev);
|
2011-02-01 13:52:35 +00:00
|
|
|
fp->private_data = clk;
|
|
|
|
}
|
|
|
|
out:
|
2011-03-30 13:24:21 +00:00
|
|
|
up_read(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int posix_clock_release(struct inode *inode, struct file *fp)
|
|
|
|
{
|
|
|
|
struct posix_clock *clk = fp->private_data;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (clk->ops.release)
|
|
|
|
err = clk->ops.release(clk);
|
|
|
|
|
2019-12-27 02:26:27 +00:00
|
|
|
put_device(clk->dev);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
fp->private_data = NULL;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations posix_clock_file_operations = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.read = posix_clock_read,
|
|
|
|
.poll = posix_clock_poll,
|
|
|
|
.unlocked_ioctl = posix_clock_ioctl,
|
|
|
|
.open = posix_clock_open,
|
|
|
|
.release = posix_clock_release,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = posix_clock_compat_ioctl,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-12-27 02:26:27 +00:00
|
|
|
int posix_clock_register(struct posix_clock *clk, struct device *dev)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2011-03-30 13:24:21 +00:00
|
|
|
init_rwsem(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
|
|
|
cdev_init(&clk->cdev, &posix_clock_file_operations);
|
2019-12-27 02:26:27 +00:00
|
|
|
err = cdev_device_add(&clk->cdev, dev);
|
|
|
|
if (err) {
|
|
|
|
pr_err("%s unable to add device %d:%d\n",
|
|
|
|
dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
|
|
|
|
return err;
|
|
|
|
}
|
2011-02-01 13:52:35 +00:00
|
|
|
clk->cdev.owner = clk->ops.owner;
|
2019-12-27 02:26:27 +00:00
|
|
|
clk->dev = dev;
|
2011-02-01 13:52:35 +00:00
|
|
|
|
2019-12-27 02:26:27 +00:00
|
|
|
return 0;
|
2011-02-01 13:52:35 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(posix_clock_register);
|
|
|
|
|
|
|
|
void posix_clock_unregister(struct posix_clock *clk)
|
|
|
|
{
|
2019-12-27 02:26:27 +00:00
|
|
|
cdev_device_del(&clk->cdev, clk->dev);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
2011-03-30 13:24:21 +00:00
|
|
|
down_write(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
clk->zombie = true;
|
2011-03-30 13:24:21 +00:00
|
|
|
up_write(&clk->rwsem);
|
2011-02-01 13:52:35 +00:00
|
|
|
|
2019-12-27 02:26:27 +00:00
|
|
|
put_device(clk->dev);
|
2011-02-01 13:52:35 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(posix_clock_unregister);
|
|
|
|
|
|
|
|
struct posix_clock_desc {
|
|
|
|
struct file *fp;
|
|
|
|
struct posix_clock *clk;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
|
|
|
|
{
|
2017-12-29 03:11:36 +00:00
|
|
|
struct file *fp = fget(clockid_to_fd(id));
|
2011-02-01 13:52:35 +00:00
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (fp->f_op->open != posix_clock_open || !fp->private_data)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
cd->fp = fp;
|
|
|
|
cd->clk = get_posix_clock(fp);
|
|
|
|
|
|
|
|
err = cd->clk ? 0 : -ENODEV;
|
|
|
|
out:
|
|
|
|
if (err)
|
|
|
|
fput(fp);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void put_clock_desc(struct posix_clock_desc *cd)
|
|
|
|
{
|
|
|
|
put_posix_clock(cd->clk);
|
|
|
|
fput(cd->fp);
|
|
|
|
}
|
|
|
|
|
2018-07-03 05:44:21 +00:00
|
|
|
static int pc_clock_adjtime(clockid_t id, struct __kernel_timex *tx)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
struct posix_clock_desc cd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = get_clock_desc(id, &cd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-03-03 17:26:14 +00:00
|
|
|
if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
|
|
|
|
err = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-02-01 13:52:35 +00:00
|
|
|
if (cd.clk->ops.clock_adjtime)
|
|
|
|
err = cd.clk->ops.clock_adjtime(cd.clk, tx);
|
|
|
|
else
|
|
|
|
err = -EOPNOTSUPP;
|
2011-03-03 17:26:14 +00:00
|
|
|
out:
|
2011-02-01 13:52:35 +00:00
|
|
|
put_clock_desc(&cd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:04:14 +00:00
|
|
|
static int pc_clock_gettime(clockid_t id, struct timespec64 *ts)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
struct posix_clock_desc cd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = get_clock_desc(id, &cd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2017-03-26 19:04:14 +00:00
|
|
|
if (cd.clk->ops.clock_gettime)
|
|
|
|
err = cd.clk->ops.clock_gettime(cd.clk, ts);
|
2011-02-01 13:52:35 +00:00
|
|
|
else
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
|
|
|
|
put_clock_desc(&cd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:04:15 +00:00
|
|
|
static int pc_clock_getres(clockid_t id, struct timespec64 *ts)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
struct posix_clock_desc cd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = get_clock_desc(id, &cd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2017-03-26 19:04:15 +00:00
|
|
|
if (cd.clk->ops.clock_getres)
|
|
|
|
err = cd.clk->ops.clock_getres(cd.clk, ts);
|
2011-02-01 13:52:35 +00:00
|
|
|
else
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
|
|
|
|
put_clock_desc(&cd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:04:16 +00:00
|
|
|
static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
|
2011-02-01 13:52:35 +00:00
|
|
|
{
|
|
|
|
struct posix_clock_desc cd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = get_clock_desc(id, &cd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2011-03-03 17:26:14 +00:00
|
|
|
if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
|
|
|
|
err = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-02-01 13:52:35 +00:00
|
|
|
if (cd.clk->ops.clock_settime)
|
2017-03-26 19:04:16 +00:00
|
|
|
err = cd.clk->ops.clock_settime(cd.clk, ts);
|
2011-02-01 13:52:35 +00:00
|
|
|
else
|
|
|
|
err = -EOPNOTSUPP;
|
2011-03-03 17:26:14 +00:00
|
|
|
out:
|
2011-02-01 13:52:35 +00:00
|
|
|
put_clock_desc(&cd);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-05-26 09:03:11 +00:00
|
|
|
const struct k_clock clock_posix_dynamic = {
|
2019-11-12 01:26:54 +00:00
|
|
|
.clock_getres = pc_clock_getres,
|
|
|
|
.clock_set = pc_clock_settime,
|
|
|
|
.clock_get_timespec = pc_clock_gettime,
|
|
|
|
.clock_adj = pc_clock_adjtime,
|
2011-02-01 13:52:35 +00:00
|
|
|
};
|