Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Support for dynamic clock devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 OMICRON electronics GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/posix-clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "posix-timers.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static struct posix_clock *get_posix_clock(struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct posix_clock *clk = fp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	down_read(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	if (!clk->zombie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		return clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	up_read(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static void put_posix_clock(struct posix_clock *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	up_read(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static ssize_t posix_clock_read(struct file *fp, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct posix_clock *clk = get_posix_clock(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (!clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (clk->ops.read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		err = clk->ops.read(clk, fp->f_flags, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	put_posix_clock(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static __poll_t posix_clock_poll(struct file *fp, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct posix_clock *clk = get_posix_clock(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	__poll_t result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return EPOLLERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (clk->ops.poll)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		result = clk->ops.poll(clk, fp, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	put_posix_clock(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static long posix_clock_ioctl(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			      unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct posix_clock *clk = get_posix_clock(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int err = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (clk->ops.ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		err = clk->ops.ioctl(clk, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	put_posix_clock(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static long posix_clock_compat_ioctl(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 				     unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct posix_clock *clk = get_posix_clock(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int err = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (clk->ops.ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		err = clk->ops.ioctl(clk, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	put_posix_clock(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int posix_clock_open(struct inode *inode, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	struct posix_clock *clk =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		container_of(inode->i_cdev, struct posix_clock, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	down_read(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (clk->zombie) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (clk->ops.open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		err = clk->ops.open(clk, fp->f_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		get_device(clk->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		fp->private_data = clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	up_read(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int posix_clock_release(struct inode *inode, struct file *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct posix_clock *clk = fp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (clk->ops.release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		err = clk->ops.release(clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	put_device(clk->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	fp->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static const struct file_operations posix_clock_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	.read		= posix_clock_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	.poll		= posix_clock_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	.unlocked_ioctl	= posix_clock_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	.open		= posix_clock_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	.release	= posix_clock_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	.compat_ioctl	= posix_clock_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int posix_clock_register(struct posix_clock *clk, struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	init_rwsem(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	cdev_init(&clk->cdev, &posix_clock_file_operations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	err = cdev_device_add(&clk->cdev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		pr_err("%s unable to add device %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	clk->cdev.owner = clk->ops.owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	clk->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) EXPORT_SYMBOL_GPL(posix_clock_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void posix_clock_unregister(struct posix_clock *clk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	cdev_device_del(&clk->cdev, clk->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	down_write(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	clk->zombie = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	up_write(&clk->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	put_device(clk->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) EXPORT_SYMBOL_GPL(posix_clock_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct posix_clock_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	struct file *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct posix_clock *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct file *fp = fget(clockid_to_fd(id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (!fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (fp->f_op->open != posix_clock_open || !fp->private_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	cd->fp = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	cd->clk = get_posix_clock(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	err = cd->clk ? 0 : -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		fput(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static void put_clock_desc(struct posix_clock_desc *cd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	put_posix_clock(cd->clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	fput(cd->fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int pc_clock_adjtime(clockid_t id, struct __kernel_timex *tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct posix_clock_desc cd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	err = get_clock_desc(id, &cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (cd.clk->ops.clock_adjtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		err = cd.clk->ops.clock_adjtime(cd.clk, tx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	put_clock_desc(&cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static int pc_clock_gettime(clockid_t id, struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct posix_clock_desc cd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	err = get_clock_desc(id, &cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (cd.clk->ops.clock_gettime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		err = cd.clk->ops.clock_gettime(cd.clk, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	put_clock_desc(&cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int pc_clock_getres(clockid_t id, struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct posix_clock_desc cd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	err = get_clock_desc(id, &cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (cd.clk->ops.clock_getres)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		err = cd.clk->ops.clock_getres(cd.clk, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	put_clock_desc(&cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct posix_clock_desc cd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	err = get_clock_desc(id, &cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (cd.clk->ops.clock_settime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		err = cd.clk->ops.clock_settime(cd.clk, ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	put_clock_desc(&cd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) const struct k_clock clock_posix_dynamic = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.clock_getres		= pc_clock_getres,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	.clock_set		= pc_clock_settime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	.clock_get_timespec	= pc_clock_gettime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	.clock_adj		= pc_clock_adjtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) };