staging: brcm80211: removed accessor functions for band type and etheraddress.
[zen-stable.git] / kernel / time / posix-clock.c
blobc340ca658f37a0c09726a31a6f126f18c9380355
1 /*
2 * posix-clock.c - support for dynamic clock devices
4 * Copyright (C) 2010 OMICRON electronics GmbH
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.
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.
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.
20 #include <linux/device.h>
21 #include <linux/file.h>
22 #include <linux/posix-clock.h>
23 #include <linux/slab.h>
24 #include <linux/syscalls.h>
25 #include <linux/uaccess.h>
27 static void delete_clock(struct kref *kref);
30 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
32 static struct posix_clock *get_posix_clock(struct file *fp)
34 struct posix_clock *clk = fp->private_data;
36 down_read(&clk->rwsem);
38 if (!clk->zombie)
39 return clk;
41 up_read(&clk->rwsem);
43 return NULL;
46 static void put_posix_clock(struct posix_clock *clk)
48 up_read(&clk->rwsem);
51 static ssize_t posix_clock_read(struct file *fp, char __user *buf,
52 size_t count, loff_t *ppos)
54 struct posix_clock *clk = get_posix_clock(fp);
55 int err = -EINVAL;
57 if (!clk)
58 return -ENODEV;
60 if (clk->ops.read)
61 err = clk->ops.read(clk, fp->f_flags, buf, count);
63 put_posix_clock(clk);
65 return err;
68 static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
70 struct posix_clock *clk = get_posix_clock(fp);
71 int result = 0;
73 if (!clk)
74 return -ENODEV;
76 if (clk->ops.poll)
77 result = clk->ops.poll(clk, fp, wait);
79 put_posix_clock(clk);
81 return result;
84 static int posix_clock_fasync(int fd, struct file *fp, int on)
86 struct posix_clock *clk = get_posix_clock(fp);
87 int err = 0;
89 if (!clk)
90 return -ENODEV;
92 if (clk->ops.fasync)
93 err = clk->ops.fasync(clk, fd, fp, on);
95 put_posix_clock(clk);
97 return err;
100 static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
102 struct posix_clock *clk = get_posix_clock(fp);
103 int err = -ENODEV;
105 if (!clk)
106 return -ENODEV;
108 if (clk->ops.mmap)
109 err = clk->ops.mmap(clk, vma);
111 put_posix_clock(clk);
113 return err;
116 static long posix_clock_ioctl(struct file *fp,
117 unsigned int cmd, unsigned long arg)
119 struct posix_clock *clk = get_posix_clock(fp);
120 int err = -ENOTTY;
122 if (!clk)
123 return -ENODEV;
125 if (clk->ops.ioctl)
126 err = clk->ops.ioctl(clk, cmd, arg);
128 put_posix_clock(clk);
130 return err;
133 #ifdef CONFIG_COMPAT
134 static long posix_clock_compat_ioctl(struct file *fp,
135 unsigned int cmd, unsigned long arg)
137 struct posix_clock *clk = get_posix_clock(fp);
138 int err = -ENOTTY;
140 if (!clk)
141 return -ENODEV;
143 if (clk->ops.ioctl)
144 err = clk->ops.ioctl(clk, cmd, arg);
146 put_posix_clock(clk);
148 return err;
150 #endif
152 static int posix_clock_open(struct inode *inode, struct file *fp)
154 int err;
155 struct posix_clock *clk =
156 container_of(inode->i_cdev, struct posix_clock, cdev);
158 down_read(&clk->rwsem);
160 if (clk->zombie) {
161 err = -ENODEV;
162 goto out;
164 if (clk->ops.open)
165 err = clk->ops.open(clk, fp->f_mode);
166 else
167 err = 0;
169 if (!err) {
170 kref_get(&clk->kref);
171 fp->private_data = clk;
173 out:
174 up_read(&clk->rwsem);
175 return err;
178 static int posix_clock_release(struct inode *inode, struct file *fp)
180 struct posix_clock *clk = fp->private_data;
181 int err = 0;
183 if (clk->ops.release)
184 err = clk->ops.release(clk);
186 kref_put(&clk->kref, delete_clock);
188 fp->private_data = NULL;
190 return err;
193 static const struct file_operations posix_clock_file_operations = {
194 .owner = THIS_MODULE,
195 .llseek = no_llseek,
196 .read = posix_clock_read,
197 .poll = posix_clock_poll,
198 .unlocked_ioctl = posix_clock_ioctl,
199 .open = posix_clock_open,
200 .release = posix_clock_release,
201 .fasync = posix_clock_fasync,
202 .mmap = posix_clock_mmap,
203 #ifdef CONFIG_COMPAT
204 .compat_ioctl = posix_clock_compat_ioctl,
205 #endif
208 int posix_clock_register(struct posix_clock *clk, dev_t devid)
210 int err;
212 kref_init(&clk->kref);
213 init_rwsem(&clk->rwsem);
215 cdev_init(&clk->cdev, &posix_clock_file_operations);
216 clk->cdev.owner = clk->ops.owner;
217 err = cdev_add(&clk->cdev, devid, 1);
219 return err;
221 EXPORT_SYMBOL_GPL(posix_clock_register);
223 static void delete_clock(struct kref *kref)
225 struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
227 if (clk->release)
228 clk->release(clk);
231 void posix_clock_unregister(struct posix_clock *clk)
233 cdev_del(&clk->cdev);
235 down_write(&clk->rwsem);
236 clk->zombie = true;
237 up_write(&clk->rwsem);
239 kref_put(&clk->kref, delete_clock);
241 EXPORT_SYMBOL_GPL(posix_clock_unregister);
243 struct posix_clock_desc {
244 struct file *fp;
245 struct posix_clock *clk;
248 static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
250 struct file *fp = fget(CLOCKID_TO_FD(id));
251 int err = -EINVAL;
253 if (!fp)
254 return err;
256 if (fp->f_op->open != posix_clock_open || !fp->private_data)
257 goto out;
259 cd->fp = fp;
260 cd->clk = get_posix_clock(fp);
262 err = cd->clk ? 0 : -ENODEV;
263 out:
264 if (err)
265 fput(fp);
266 return err;
269 static void put_clock_desc(struct posix_clock_desc *cd)
271 put_posix_clock(cd->clk);
272 fput(cd->fp);
275 static int pc_clock_adjtime(clockid_t id, struct timex *tx)
277 struct posix_clock_desc cd;
278 int err;
280 err = get_clock_desc(id, &cd);
281 if (err)
282 return err;
284 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
285 err = -EACCES;
286 goto out;
289 if (cd.clk->ops.clock_adjtime)
290 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
291 else
292 err = -EOPNOTSUPP;
293 out:
294 put_clock_desc(&cd);
296 return err;
299 static int pc_clock_gettime(clockid_t id, struct timespec *ts)
301 struct posix_clock_desc cd;
302 int err;
304 err = get_clock_desc(id, &cd);
305 if (err)
306 return err;
308 if (cd.clk->ops.clock_gettime)
309 err = cd.clk->ops.clock_gettime(cd.clk, ts);
310 else
311 err = -EOPNOTSUPP;
313 put_clock_desc(&cd);
315 return err;
318 static int pc_clock_getres(clockid_t id, struct timespec *ts)
320 struct posix_clock_desc cd;
321 int err;
323 err = get_clock_desc(id, &cd);
324 if (err)
325 return err;
327 if (cd.clk->ops.clock_getres)
328 err = cd.clk->ops.clock_getres(cd.clk, ts);
329 else
330 err = -EOPNOTSUPP;
332 put_clock_desc(&cd);
334 return err;
337 static int pc_clock_settime(clockid_t id, const struct timespec *ts)
339 struct posix_clock_desc cd;
340 int err;
342 err = get_clock_desc(id, &cd);
343 if (err)
344 return err;
346 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
347 err = -EACCES;
348 goto out;
351 if (cd.clk->ops.clock_settime)
352 err = cd.clk->ops.clock_settime(cd.clk, ts);
353 else
354 err = -EOPNOTSUPP;
355 out:
356 put_clock_desc(&cd);
358 return err;
361 static int pc_timer_create(struct k_itimer *kit)
363 clockid_t id = kit->it_clock;
364 struct posix_clock_desc cd;
365 int err;
367 err = get_clock_desc(id, &cd);
368 if (err)
369 return err;
371 if (cd.clk->ops.timer_create)
372 err = cd.clk->ops.timer_create(cd.clk, kit);
373 else
374 err = -EOPNOTSUPP;
376 put_clock_desc(&cd);
378 return err;
381 static int pc_timer_delete(struct k_itimer *kit)
383 clockid_t id = kit->it_clock;
384 struct posix_clock_desc cd;
385 int err;
387 err = get_clock_desc(id, &cd);
388 if (err)
389 return err;
391 if (cd.clk->ops.timer_delete)
392 err = cd.clk->ops.timer_delete(cd.clk, kit);
393 else
394 err = -EOPNOTSUPP;
396 put_clock_desc(&cd);
398 return err;
401 static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
403 clockid_t id = kit->it_clock;
404 struct posix_clock_desc cd;
406 if (get_clock_desc(id, &cd))
407 return;
409 if (cd.clk->ops.timer_gettime)
410 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
412 put_clock_desc(&cd);
415 static int pc_timer_settime(struct k_itimer *kit, int flags,
416 struct itimerspec *ts, struct itimerspec *old)
418 clockid_t id = kit->it_clock;
419 struct posix_clock_desc cd;
420 int err;
422 err = get_clock_desc(id, &cd);
423 if (err)
424 return err;
426 if (cd.clk->ops.timer_settime)
427 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
428 else
429 err = -EOPNOTSUPP;
431 put_clock_desc(&cd);
433 return err;
436 struct k_clock clock_posix_dynamic = {
437 .clock_getres = pc_clock_getres,
438 .clock_set = pc_clock_settime,
439 .clock_get = pc_clock_gettime,
440 .clock_adj = pc_clock_adjtime,
441 .timer_create = pc_timer_create,
442 .timer_set = pc_timer_settime,
443 .timer_del = pc_timer_delete,
444 .timer_get = pc_timer_gettime,