Linux Audio

Check our new training course

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