Blame view

kernel/time/posix-clock.c 8.36 KB
0606f422b   Richard Cochran   posix clocks: Int...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  /*
   * posix-clock.c - support for dynamic clock devices
   *
   * Copyright (C) 2010 OMICRON electronics GmbH
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; either version 2 of the License, or
   *  (at your option) any later version.
   *
   *  This program is distributed in the hope that it will be useful,
   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *  GNU General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License
   *  along with this program; if not, write to the Free Software
   *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */
  #include <linux/device.h>
6e5fdeedc   Paul Gortmaker   kernel: Fix files...
21
  #include <linux/export.h>
0606f422b   Richard Cochran   posix clocks: Int...
22
  #include <linux/file.h>
0606f422b   Richard Cochran   posix clocks: Int...
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <linux/posix-clock.h>
  #include <linux/slab.h>
  #include <linux/syscalls.h>
  #include <linux/uaccess.h>
  
  static void delete_clock(struct kref *kref);
  
  /*
   * 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;
1791f8814   Richard Cochran   posix clocks: Rep...
36
  	down_read(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
37
38
39
  
  	if (!clk->zombie)
  		return clk;
1791f8814   Richard Cochran   posix clocks: Rep...
40
  	up_read(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
41
42
43
44
45
46
  
  	return NULL;
  }
  
  static void put_posix_clock(struct posix_clock *clk)
  {
1791f8814   Richard Cochran   posix clocks: Rep...
47
  	up_read(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  }
  
  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;
  }
  
  static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
  {
  	struct posix_clock *clk = get_posix_clock(fp);
  	int result = 0;
  
  	if (!clk)
  		return -ENODEV;
  
  	if (clk->ops.poll)
  		result = clk->ops.poll(clk, fp, wait);
  
  	put_posix_clock(clk);
  
  	return result;
  }
  
  static int posix_clock_fasync(int fd, struct file *fp, int on)
  {
  	struct posix_clock *clk = get_posix_clock(fp);
  	int err = 0;
  
  	if (!clk)
  		return -ENODEV;
  
  	if (clk->ops.fasync)
  		err = clk->ops.fasync(clk, fd, fp, on);
  
  	put_posix_clock(clk);
  
  	return err;
  }
  
  static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
  {
  	struct posix_clock *clk = get_posix_clock(fp);
  	int err = -ENODEV;
  
  	if (!clk)
  		return -ENODEV;
  
  	if (clk->ops.mmap)
  		err = clk->ops.mmap(clk, vma);
  
  	put_posix_clock(clk);
  
  	return err;
  }
  
  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);
1791f8814   Richard Cochran   posix clocks: Rep...
156
  	down_read(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  
  	if (clk->zombie) {
  		err = -ENODEV;
  		goto out;
  	}
  	if (clk->ops.open)
  		err = clk->ops.open(clk, fp->f_mode);
  	else
  		err = 0;
  
  	if (!err) {
  		kref_get(&clk->kref);
  		fp->private_data = clk;
  	}
  out:
1791f8814   Richard Cochran   posix clocks: Rep...
172
  	up_read(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  	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);
  
  	kref_put(&clk->kref, delete_clock);
  
  	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,
  	.fasync		= posix_clock_fasync,
  	.mmap		= posix_clock_mmap,
  #ifdef CONFIG_COMPAT
  	.compat_ioctl	= posix_clock_compat_ioctl,
  #endif
  };
  
  int posix_clock_register(struct posix_clock *clk, dev_t devid)
  {
  	int err;
  
  	kref_init(&clk->kref);
1791f8814   Richard Cochran   posix clocks: Rep...
211
  	init_rwsem(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
212
213
214
215
  
  	cdev_init(&clk->cdev, &posix_clock_file_operations);
  	clk->cdev.owner = clk->ops.owner;
  	err = cdev_add(&clk->cdev, devid, 1);
0606f422b   Richard Cochran   posix clocks: Int...
216
217
  
  	return err;
0606f422b   Richard Cochran   posix clocks: Int...
218
219
220
221
222
223
  }
  EXPORT_SYMBOL_GPL(posix_clock_register);
  
  static void delete_clock(struct kref *kref)
  {
  	struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
1791f8814   Richard Cochran   posix clocks: Rep...
224

0606f422b   Richard Cochran   posix clocks: Int...
225
226
227
228
229
230
231
  	if (clk->release)
  		clk->release(clk);
  }
  
  void posix_clock_unregister(struct posix_clock *clk)
  {
  	cdev_del(&clk->cdev);
1791f8814   Richard Cochran   posix clocks: Rep...
232
  	down_write(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
233
  	clk->zombie = true;
1791f8814   Richard Cochran   posix clocks: Rep...
234
  	up_write(&clk->rwsem);
0606f422b   Richard Cochran   posix clocks: Int...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  
  	kref_put(&clk->kref, delete_clock);
  }
  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)
  {
  	struct file *fp = fget(CLOCKID_TO_FD(id));
  	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);
  }
  
  static int pc_clock_adjtime(clockid_t id, struct timex *tx)
  {
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
6e6823d17   Torben Hohn   posix-clocks: Che...
280
281
282
283
  	if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  		err = -EACCES;
  		goto out;
  	}
0606f422b   Richard Cochran   posix clocks: Int...
284
285
286
287
  	if (cd.clk->ops.clock_adjtime)
  		err = cd.clk->ops.clock_adjtime(cd.clk, tx);
  	else
  		err = -EOPNOTSUPP;
6e6823d17   Torben Hohn   posix-clocks: Che...
288
  out:
0606f422b   Richard Cochran   posix clocks: Int...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static int pc_clock_gettime(clockid_t id, struct timespec *ts)
  {
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
  
  	if (cd.clk->ops.clock_gettime)
  		err = cd.clk->ops.clock_gettime(cd.clk, ts);
  	else
  		err = -EOPNOTSUPP;
  
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static int pc_clock_getres(clockid_t id, struct timespec *ts)
  {
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
  
  	if (cd.clk->ops.clock_getres)
  		err = cd.clk->ops.clock_getres(cd.clk, ts);
  	else
  		err = -EOPNOTSUPP;
  
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static int pc_clock_settime(clockid_t id, const struct timespec *ts)
  {
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
6e6823d17   Torben Hohn   posix-clocks: Che...
340
341
342
343
  	if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
  		err = -EACCES;
  		goto out;
  	}
0606f422b   Richard Cochran   posix clocks: Int...
344
345
346
347
  	if (cd.clk->ops.clock_settime)
  		err = cd.clk->ops.clock_settime(cd.clk, ts);
  	else
  		err = -EOPNOTSUPP;
6e6823d17   Torben Hohn   posix-clocks: Che...
348
  out:
0606f422b   Richard Cochran   posix clocks: Int...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static int pc_timer_create(struct k_itimer *kit)
  {
  	clockid_t id = kit->it_clock;
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
  
  	if (cd.clk->ops.timer_create)
  		err = cd.clk->ops.timer_create(cd.clk, kit);
  	else
  		err = -EOPNOTSUPP;
  
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static int pc_timer_delete(struct k_itimer *kit)
  {
  	clockid_t id = kit->it_clock;
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
  
  	if (cd.clk->ops.timer_delete)
  		err = cd.clk->ops.timer_delete(cd.clk, kit);
  	else
  		err = -EOPNOTSUPP;
  
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
  {
  	clockid_t id = kit->it_clock;
  	struct posix_clock_desc cd;
  
  	if (get_clock_desc(id, &cd))
  		return;
  
  	if (cd.clk->ops.timer_gettime)
  		cd.clk->ops.timer_gettime(cd.clk, kit, ts);
  
  	put_clock_desc(&cd);
  }
  
  static int pc_timer_settime(struct k_itimer *kit, int flags,
  			    struct itimerspec *ts, struct itimerspec *old)
  {
  	clockid_t id = kit->it_clock;
  	struct posix_clock_desc cd;
  	int err;
  
  	err = get_clock_desc(id, &cd);
  	if (err)
  		return err;
  
  	if (cd.clk->ops.timer_settime)
  		err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
  	else
  		err = -EOPNOTSUPP;
  
  	put_clock_desc(&cd);
  
  	return err;
  }
  
  struct k_clock clock_posix_dynamic = {
  	.clock_getres	= pc_clock_getres,
  	.clock_set	= pc_clock_settime,
  	.clock_get	= pc_clock_gettime,
  	.clock_adj	= pc_clock_adjtime,
  	.timer_create	= pc_timer_create,
  	.timer_set	= pc_timer_settime,
  	.timer_del	= pc_timer_delete,
  	.timer_get	= pc_timer_gettime,
  };