~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/time/posix-clock.c

Version: ~ [ linux-5.15-rc7 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * posix-clock.c - support for dynamic clock devices
  3  *
  4  * Copyright (C) 2010 OMICRON electronics GmbH
  5  *
  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.
 10  *
 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.
 15  *
 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.
 19  */
 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>
 27 
 28 /*
 29  * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
 30  */
 31 static struct posix_clock *get_posix_clock(struct file *fp)
 32 {
 33         struct posix_clock *clk = fp->private_data;
 34 
 35         down_read(&clk->rwsem);
 36 
 37         if (!clk->zombie)
 38                 return clk;
 39 
 40         up_read(&clk->rwsem);
 41 
 42         return NULL;
 43 }
 44 
 45 static void put_posix_clock(struct posix_clock *clk)
 46 {
 47         up_read(&clk->rwsem);
 48 }
 49 
 50 static ssize_t posix_clock_read(struct file *fp, char __user *buf,
 51                                 size_t count, loff_t *ppos)
 52 {
 53         struct posix_clock *clk = get_posix_clock(fp);
 54         int err = -EINVAL;
 55 
 56         if (!clk)
 57                 return -ENODEV;
 58 
 59         if (clk->ops.read)
 60                 err = clk->ops.read(clk, fp->f_flags, buf, count);
 61 
 62         put_posix_clock(clk);
 63 
 64         return err;
 65 }
 66 
 67 static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
 68 {
 69         struct posix_clock *clk = get_posix_clock(fp);
 70         unsigned int result = 0;
 71 
 72         if (!clk)
 73                 return POLLERR;
 74 
 75         if (clk->ops.poll)
 76                 result = clk->ops.poll(clk, fp, wait);
 77 
 78         put_posix_clock(clk);
 79 
 80         return result;
 81 }
 82 
 83 static int posix_clock_fasync(int fd, struct file *fp, int on)
 84 {
 85         struct posix_clock *clk = get_posix_clock(fp);
 86         int err = 0;
 87 
 88         if (!clk)
 89                 return -ENODEV;
 90 
 91         if (clk->ops.fasync)
 92                 err = clk->ops.fasync(clk, fd, fp, on);
 93 
 94         put_posix_clock(clk);
 95 
 96         return err;
 97 }
 98 
 99 static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
100 {
101         struct posix_clock *clk = get_posix_clock(fp);
102         int err = -ENODEV;
103 
104         if (!clk)
105                 return -ENODEV;
106 
107         if (clk->ops.mmap)
108                 err = clk->ops.mmap(clk, vma);
109 
110         put_posix_clock(clk);
111 
112         return err;
113 }
114 
115 static long posix_clock_ioctl(struct file *fp,
116                               unsigned int cmd, unsigned long arg)
117 {
118         struct posix_clock *clk = get_posix_clock(fp);
119         int err = -ENOTTY;
120 
121         if (!clk)
122                 return -ENODEV;
123 
124         if (clk->ops.ioctl)
125                 err = clk->ops.ioctl(clk, cmd, arg);
126 
127         put_posix_clock(clk);
128 
129         return err;
130 }
131 
132 #ifdef CONFIG_COMPAT
133 static long posix_clock_compat_ioctl(struct file *fp,
134                                      unsigned int cmd, unsigned long arg)
135 {
136         struct posix_clock *clk = get_posix_clock(fp);
137         int err = -ENOTTY;
138 
139         if (!clk)
140                 return -ENODEV;
141 
142         if (clk->ops.ioctl)
143                 err = clk->ops.ioctl(clk, cmd, arg);
144 
145         put_posix_clock(clk);
146 
147         return err;
148 }
149 #endif
150 
151 static int posix_clock_open(struct inode *inode, struct file *fp)
152 {
153         int err;
154         struct posix_clock *clk =
155                 container_of(inode->i_cdev, struct posix_clock, cdev);
156 
157         down_read(&clk->rwsem);
158 
159         if (clk->zombie) {
160                 err = -ENODEV;
161                 goto out;
162         }
163         if (clk->ops.open)
164                 err = clk->ops.open(clk, fp->f_mode);
165         else
166                 err = 0;
167 
168         if (!err) {
169                 get_device(clk->dev);
170                 fp->private_data = clk;
171         }
172 out:
173         up_read(&clk->rwsem);
174         return err;
175 }
176 
177 static int posix_clock_release(struct inode *inode, struct file *fp)
178 {
179         struct posix_clock *clk = fp->private_data;
180         int err = 0;
181 
182         if (clk->ops.release)
183                 err = clk->ops.release(clk);
184 
185         put_device(clk->dev);
186 
187         fp->private_data = NULL;
188 
189         return err;
190 }
191 
192 static const struct file_operations posix_clock_file_operations = {
193         .owner          = THIS_MODULE,
194         .llseek         = no_llseek,
195         .read           = posix_clock_read,
196         .poll           = posix_clock_poll,
197         .unlocked_ioctl = posix_clock_ioctl,
198         .open           = posix_clock_open,
199         .release        = posix_clock_release,
200         .fasync         = posix_clock_fasync,
201         .mmap           = posix_clock_mmap,
202 #ifdef CONFIG_COMPAT
203         .compat_ioctl   = posix_clock_compat_ioctl,
204 #endif
205 };
206 
207 int posix_clock_register(struct posix_clock *clk, struct device *dev)
208 {
209         int err;
210 
211         init_rwsem(&clk->rwsem);
212 
213         cdev_init(&clk->cdev, &posix_clock_file_operations);
214         err = cdev_device_add(&clk->cdev, dev);
215         if (err) {
216                 pr_err("%s unable to add device %d:%d\n",
217                         dev_name(dev), MAJOR(dev->devt), MINOR(dev->devt));
218                 return err;
219         }
220         clk->cdev.owner = clk->ops.owner;
221         clk->dev = dev;
222 
223         return 0;
224 }
225 EXPORT_SYMBOL_GPL(posix_clock_register);
226 
227 void posix_clock_unregister(struct posix_clock *clk)
228 {
229         cdev_device_del(&clk->cdev, clk->dev);
230 
231         down_write(&clk->rwsem);
232         clk->zombie = true;
233         up_write(&clk->rwsem);
234 
235         put_device(clk->dev);
236 }
237 EXPORT_SYMBOL_GPL(posix_clock_unregister);
238 
239 struct posix_clock_desc {
240         struct file *fp;
241         struct posix_clock *clk;
242 };
243 
244 static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
245 {
246         struct file *fp = fget(CLOCKID_TO_FD(id));
247         int err = -EINVAL;
248 
249         if (!fp)
250                 return err;
251 
252         if (fp->f_op->open != posix_clock_open || !fp->private_data)
253                 goto out;
254 
255         cd->fp = fp;
256         cd->clk = get_posix_clock(fp);
257 
258         err = cd->clk ? 0 : -ENODEV;
259 out:
260         if (err)
261                 fput(fp);
262         return err;
263 }
264 
265 static void put_clock_desc(struct posix_clock_desc *cd)
266 {
267         put_posix_clock(cd->clk);
268         fput(cd->fp);
269 }
270 
271 static int pc_clock_adjtime(clockid_t id, struct timex *tx)
272 {
273         struct posix_clock_desc cd;
274         int err;
275 
276         err = get_clock_desc(id, &cd);
277         if (err)
278                 return err;
279 
280         if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
281                 err = -EACCES;
282                 goto out;
283         }
284 
285         if (cd.clk->ops.clock_adjtime)
286                 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
287         else
288                 err = -EOPNOTSUPP;
289 out:
290         put_clock_desc(&cd);
291 
292         return err;
293 }
294 
295 static int pc_clock_gettime(clockid_t id, struct timespec *ts)
296 {
297         struct posix_clock_desc cd;
298         int err;
299 
300         err = get_clock_desc(id, &cd);
301         if (err)
302                 return err;
303 
304         if (cd.clk->ops.clock_gettime)
305                 err = cd.clk->ops.clock_gettime(cd.clk, ts);
306         else
307                 err = -EOPNOTSUPP;
308 
309         put_clock_desc(&cd);
310 
311         return err;
312 }
313 
314 static int pc_clock_getres(clockid_t id, struct timespec *ts)
315 {
316         struct posix_clock_desc cd;
317         int err;
318 
319         err = get_clock_desc(id, &cd);
320         if (err)
321                 return err;
322 
323         if (cd.clk->ops.clock_getres)
324                 err = cd.clk->ops.clock_getres(cd.clk, ts);
325         else
326                 err = -EOPNOTSUPP;
327 
328         put_clock_desc(&cd);
329 
330         return err;
331 }
332 
333 static int pc_clock_settime(clockid_t id, const struct timespec *ts)
334 {
335         struct posix_clock_desc cd;
336         int err;
337 
338         err = get_clock_desc(id, &cd);
339         if (err)
340                 return err;
341 
342         if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
343                 err = -EACCES;
344                 goto out;
345         }
346 
347         if (cd.clk->ops.clock_settime)
348                 err = cd.clk->ops.clock_settime(cd.clk, ts);
349         else
350                 err = -EOPNOTSUPP;
351 out:
352         put_clock_desc(&cd);
353 
354         return err;
355 }
356 
357 static int pc_timer_create(struct k_itimer *kit)
358 {
359         clockid_t id = kit->it_clock;
360         struct posix_clock_desc cd;
361         int err;
362 
363         err = get_clock_desc(id, &cd);
364         if (err)
365                 return err;
366 
367         if (cd.clk->ops.timer_create)
368                 err = cd.clk->ops.timer_create(cd.clk, kit);
369         else
370                 err = -EOPNOTSUPP;
371 
372         put_clock_desc(&cd);
373 
374         return err;
375 }
376 
377 static int pc_timer_delete(struct k_itimer *kit)
378 {
379         clockid_t id = kit->it_clock;
380         struct posix_clock_desc cd;
381         int err;
382 
383         err = get_clock_desc(id, &cd);
384         if (err)
385                 return err;
386 
387         if (cd.clk->ops.timer_delete)
388                 err = cd.clk->ops.timer_delete(cd.clk, kit);
389         else
390                 err = -EOPNOTSUPP;
391 
392         put_clock_desc(&cd);
393 
394         return err;
395 }
396 
397 static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
398 {
399         clockid_t id = kit->it_clock;
400         struct posix_clock_desc cd;
401 
402         if (get_clock_desc(id, &cd))
403                 return;
404 
405         if (cd.clk->ops.timer_gettime)
406                 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
407 
408         put_clock_desc(&cd);
409 }
410 
411 static int pc_timer_settime(struct k_itimer *kit, int flags,
412                             struct itimerspec *ts, struct itimerspec *old)
413 {
414         clockid_t id = kit->it_clock;
415         struct posix_clock_desc cd;
416         int err;
417 
418         err = get_clock_desc(id, &cd);
419         if (err)
420                 return err;
421 
422         if (cd.clk->ops.timer_settime)
423                 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
424         else
425                 err = -EOPNOTSUPP;
426 
427         put_clock_desc(&cd);
428 
429         return err;
430 }
431 
432 struct k_clock clock_posix_dynamic = {
433         .clock_getres   = pc_clock_getres,
434         .clock_set      = pc_clock_settime,
435         .clock_get      = pc_clock_gettime,
436         .clock_adj      = pc_clock_adjtime,
437         .timer_create   = pc_timer_create,
438         .timer_set      = pc_timer_settime,
439         .timer_del      = pc_timer_delete,
440         .timer_get      = pc_timer_gettime,
441 };
442 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp