[PATCH] rtc class locking bugfixes
[linux-2.6.git] / drivers / rtc / rtc-dev.c
1 /*
2  * RTC subsystem, dev interface
3  *
4  * Copyright (C) 2005 Tower Technologies
5  * Author: Alessandro Zummo <a.zummo@towertech.it>
6  *
7  * based on arch/arm/common/rtctime.c
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12 */
13
14 #include <linux/module.h>
15 #include <linux/rtc.h>
16
17 static struct class *rtc_dev_class;
18 static dev_t rtc_devt;
19
20 #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
21
22 static int rtc_dev_open(struct inode *inode, struct file *file)
23 {
24         int err;
25         struct rtc_device *rtc = container_of(inode->i_cdev,
26                                         struct rtc_device, char_dev);
27         const struct rtc_class_ops *ops = rtc->ops;
28
29         /* We keep the lock as long as the device is in use
30          * and return immediately if busy
31          */
32         if (!(mutex_trylock(&rtc->char_lock)))
33                 return -EBUSY;
34
35         file->private_data = &rtc->class_dev;
36
37         err = ops->open ? ops->open(rtc->class_dev.dev) : 0;
38         if (err == 0) {
39                 spin_lock_irq(&rtc->irq_lock);
40                 rtc->irq_data = 0;
41                 spin_unlock_irq(&rtc->irq_lock);
42
43                 return 0;
44         }
45
46         /* something has gone wrong, release the lock */
47         mutex_unlock(&rtc->char_lock);
48         return err;
49 }
50
51 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
52 /*
53  * Routine to poll RTC seconds field for change as often as possible,
54  * after first RTC_UIE use timer to reduce polling
55  */
56 static void rtc_uie_task(void *data)
57 {
58         struct rtc_device *rtc = data;
59         struct rtc_time tm;
60         int num = 0;
61         int err;
62
63         err = rtc_read_time(&rtc->class_dev, &tm);
64
65         local_irq_disable();
66         spin_lock(&rtc->irq_lock);
67         if (rtc->stop_uie_polling || err) {
68                 rtc->uie_task_active = 0;
69         } else if (rtc->oldsecs != tm.tm_sec) {
70                 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
71                 rtc->oldsecs = tm.tm_sec;
72                 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
73                 rtc->uie_timer_active = 1;
74                 rtc->uie_task_active = 0;
75                 add_timer(&rtc->uie_timer);
76         } else if (schedule_work(&rtc->uie_task) == 0) {
77                 rtc->uie_task_active = 0;
78         }
79         spin_unlock(&rtc->irq_lock);
80         if (num)
81                 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
82         local_irq_enable();
83 }
84 static void rtc_uie_timer(unsigned long data)
85 {
86         struct rtc_device *rtc = (struct rtc_device *)data;
87         unsigned long flags;
88
89         spin_lock_irqsave(&rtc->irq_lock, flags);
90         rtc->uie_timer_active = 0;
91         rtc->uie_task_active = 1;
92         if ((schedule_work(&rtc->uie_task) == 0))
93                 rtc->uie_task_active = 0;
94         spin_unlock_irqrestore(&rtc->irq_lock, flags);
95 }
96
97 static void clear_uie(struct rtc_device *rtc)
98 {
99         spin_lock_irq(&rtc->irq_lock);
100         if (rtc->irq_active) {
101                 rtc->stop_uie_polling = 1;
102                 if (rtc->uie_timer_active) {
103                         spin_unlock_irq(&rtc->irq_lock);
104                         del_timer_sync(&rtc->uie_timer);
105                         spin_lock_irq(&rtc->irq_lock);
106                         rtc->uie_timer_active = 0;
107                 }
108                 if (rtc->uie_task_active) {
109                         spin_unlock_irq(&rtc->irq_lock);
110                         flush_scheduled_work();
111                         spin_lock_irq(&rtc->irq_lock);
112                 }
113                 rtc->irq_active = 0;
114         }
115         spin_unlock_irq(&rtc->irq_lock);
116 }
117
118 static int set_uie(struct rtc_device *rtc)
119 {
120         struct rtc_time tm;
121         int err;
122
123         err = rtc_read_time(&rtc->class_dev, &tm);
124         if (err)
125                 return err;
126         spin_lock_irq(&rtc->irq_lock);
127         if (!rtc->irq_active) {
128                 rtc->irq_active = 1;
129                 rtc->stop_uie_polling = 0;
130                 rtc->oldsecs = tm.tm_sec;
131                 rtc->uie_task_active = 1;
132                 if (schedule_work(&rtc->uie_task) == 0)
133                         rtc->uie_task_active = 0;
134         }
135         rtc->irq_data = 0;
136         spin_unlock_irq(&rtc->irq_lock);
137         return 0;
138 }
139 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
140
141 static ssize_t
142 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143 {
144         struct rtc_device *rtc = to_rtc_device(file->private_data);
145
146         DECLARE_WAITQUEUE(wait, current);
147         unsigned long data;
148         ssize_t ret;
149
150         if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
151                 return -EINVAL;
152
153         add_wait_queue(&rtc->irq_queue, &wait);
154         do {
155                 __set_current_state(TASK_INTERRUPTIBLE);
156
157                 spin_lock_irq(&rtc->irq_lock);
158                 data = rtc->irq_data;
159                 rtc->irq_data = 0;
160                 spin_unlock_irq(&rtc->irq_lock);
161
162                 if (data != 0) {
163                         ret = 0;
164                         break;
165                 }
166                 if (file->f_flags & O_NONBLOCK) {
167                         ret = -EAGAIN;
168                         break;
169                 }
170                 if (signal_pending(current)) {
171                         ret = -ERESTARTSYS;
172                         break;
173                 }
174                 schedule();
175         } while (1);
176         set_current_state(TASK_RUNNING);
177         remove_wait_queue(&rtc->irq_queue, &wait);
178
179         if (ret == 0) {
180                 /* Check for any data updates */
181                 if (rtc->ops->read_callback)
182                         data = rtc->ops->read_callback(rtc->class_dev.dev,
183                                                        data);
184
185                 if (sizeof(int) != sizeof(long) &&
186                     count == sizeof(unsigned int))
187                         ret = put_user(data, (unsigned int __user *)buf) ?:
188                                 sizeof(unsigned int);
189                 else
190                         ret = put_user(data, (unsigned long __user *)buf) ?:
191                                 sizeof(unsigned long);
192         }
193         return ret;
194 }
195
196 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
197 {
198         struct rtc_device *rtc = to_rtc_device(file->private_data);
199         unsigned long data;
200
201         poll_wait(file, &rtc->irq_queue, wait);
202
203         data = rtc->irq_data;
204
205         return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
206 }
207
208 static int rtc_dev_ioctl(struct inode *inode, struct file *file,
209                 unsigned int cmd, unsigned long arg)
210 {
211         int err = 0;
212         struct class_device *class_dev = file->private_data;
213         struct rtc_device *rtc = to_rtc_device(class_dev);
214         const struct rtc_class_ops *ops = rtc->ops;
215         struct rtc_time tm;
216         struct rtc_wkalrm alarm;
217         void __user *uarg = (void __user *) arg;
218
219         /* check that the calling task has appropriate permissions
220          * for certain ioctls. doing this check here is useful
221          * to avoid duplicate code in each driver.
222          */
223         switch (cmd) {
224         case RTC_EPOCH_SET:
225         case RTC_SET_TIME:
226                 if (!capable(CAP_SYS_TIME))
227                         return -EACCES;
228                 break;
229
230         case RTC_IRQP_SET:
231                 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
232                         return -EACCES;
233                 break;
234
235         case RTC_PIE_ON:
236                 if (!capable(CAP_SYS_RESOURCE))
237                         return -EACCES;
238                 break;
239         }
240
241         /* avoid conflicting IRQ users */
242         if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
243                 spin_lock_irq(&rtc->irq_task_lock);
244                 if (rtc->irq_task)
245                         err = -EBUSY;
246                 spin_unlock_irq(&rtc->irq_task_lock);
247
248                 if (err < 0)
249                         return err;
250         }
251
252         /* try the driver's ioctl interface */
253         if (ops->ioctl) {
254                 err = ops->ioctl(class_dev->dev, cmd, arg);
255                 if (err != -ENOIOCTLCMD)
256                         return err;
257         }
258
259         /* if the driver does not provide the ioctl interface
260          * or if that particular ioctl was not implemented
261          * (-ENOIOCTLCMD), we will try to emulate here.
262          */
263
264         switch (cmd) {
265         case RTC_ALM_READ:
266                 err = rtc_read_alarm(class_dev, &alarm);
267                 if (err < 0)
268                         return err;
269
270                 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
271                         return -EFAULT;
272                 break;
273
274         case RTC_ALM_SET:
275                 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
276                         return -EFAULT;
277
278                 alarm.enabled = 0;
279                 alarm.pending = 0;
280                 alarm.time.tm_mday = -1;
281                 alarm.time.tm_mon = -1;
282                 alarm.time.tm_year = -1;
283                 alarm.time.tm_wday = -1;
284                 alarm.time.tm_yday = -1;
285                 alarm.time.tm_isdst = -1;
286                 err = rtc_set_alarm(class_dev, &alarm);
287                 break;
288
289         case RTC_RD_TIME:
290                 err = rtc_read_time(class_dev, &tm);
291                 if (err < 0)
292                         return err;
293
294                 if (copy_to_user(uarg, &tm, sizeof(tm)))
295                         return -EFAULT;
296                 break;
297
298         case RTC_SET_TIME:
299                 if (copy_from_user(&tm, uarg, sizeof(tm)))
300                         return -EFAULT;
301
302                 err = rtc_set_time(class_dev, &tm);
303                 break;
304
305         case RTC_IRQP_READ:
306                 if (ops->irq_set_freq)
307                         err = put_user(rtc->irq_freq, (unsigned long *) arg);
308                 break;
309
310         case RTC_IRQP_SET:
311                 if (ops->irq_set_freq)
312                         err = rtc_irq_set_freq(class_dev, rtc->irq_task, arg);
313                 break;
314
315 #if 0
316         case RTC_EPOCH_SET:
317 #ifndef rtc_epoch
318                 /*
319                  * There were no RTC clocks before 1900.
320                  */
321                 if (arg < 1900) {
322                         err = -EINVAL;
323                         break;
324                 }
325                 rtc_epoch = arg;
326                 err = 0;
327 #endif
328                 break;
329
330         case RTC_EPOCH_READ:
331                 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
332                 break;
333 #endif
334         case RTC_WKALM_SET:
335                 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
336                         return -EFAULT;
337
338                 err = rtc_set_alarm(class_dev, &alarm);
339                 break;
340
341         case RTC_WKALM_RD:
342                 err = rtc_read_alarm(class_dev, &alarm);
343                 if (err < 0)
344                         return err;
345
346                 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
347                         return -EFAULT;
348                 break;
349
350 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
351         case RTC_UIE_OFF:
352                 clear_uie(rtc);
353                 return 0;
354
355         case RTC_UIE_ON:
356                 return set_uie(rtc);
357 #endif
358         default:
359                 err = -ENOTTY;
360                 break;
361         }
362
363         return err;
364 }
365
366 static int rtc_dev_release(struct inode *inode, struct file *file)
367 {
368         struct rtc_device *rtc = to_rtc_device(file->private_data);
369
370 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
371         clear_uie(rtc);
372 #endif
373         if (rtc->ops->release)
374                 rtc->ops->release(rtc->class_dev.dev);
375
376         mutex_unlock(&rtc->char_lock);
377         return 0;
378 }
379
380 static int rtc_dev_fasync(int fd, struct file *file, int on)
381 {
382         struct rtc_device *rtc = to_rtc_device(file->private_data);
383         return fasync_helper(fd, file, on, &rtc->async_queue);
384 }
385
386 static struct file_operations rtc_dev_fops = {
387         .owner          = THIS_MODULE,
388         .llseek         = no_llseek,
389         .read           = rtc_dev_read,
390         .poll           = rtc_dev_poll,
391         .ioctl          = rtc_dev_ioctl,
392         .open           = rtc_dev_open,
393         .release        = rtc_dev_release,
394         .fasync         = rtc_dev_fasync,
395 };
396
397 /* insertion/removal hooks */
398
399 static int rtc_dev_add_device(struct class_device *class_dev,
400                                 struct class_interface *class_intf)
401 {
402         int err = 0;
403         struct rtc_device *rtc = to_rtc_device(class_dev);
404
405         if (rtc->id >= RTC_DEV_MAX) {
406                 dev_err(class_dev->dev, "too many RTCs\n");
407                 return -EINVAL;
408         }
409
410         mutex_init(&rtc->char_lock);
411         spin_lock_init(&rtc->irq_lock);
412         init_waitqueue_head(&rtc->irq_queue);
413 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
414         INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
415         setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
416 #endif
417
418         cdev_init(&rtc->char_dev, &rtc_dev_fops);
419         rtc->char_dev.owner = rtc->owner;
420
421         if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
422                 dev_err(class_dev->dev,
423                         "failed to add char device %d:%d\n",
424                         MAJOR(rtc_devt), rtc->id);
425                 return -ENODEV;
426         }
427
428         rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
429                                                 MKDEV(MAJOR(rtc_devt), rtc->id),
430                                                 class_dev->dev, "rtc%d", rtc->id);
431         if (IS_ERR(rtc->rtc_dev)) {
432                 dev_err(class_dev->dev, "cannot create rtc_dev device\n");
433                 err = PTR_ERR(rtc->rtc_dev);
434                 goto err_cdev_del;
435         }
436
437         dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
438                 MAJOR(rtc->rtc_dev->devt),
439                 MINOR(rtc->rtc_dev->devt));
440
441         return 0;
442
443 err_cdev_del:
444
445         cdev_del(&rtc->char_dev);
446         return err;
447 }
448
449 static void rtc_dev_remove_device(struct class_device *class_dev,
450                                         struct class_interface *class_intf)
451 {
452         struct rtc_device *rtc = to_rtc_device(class_dev);
453
454         if (rtc->rtc_dev) {
455                 dev_dbg(class_dev->dev, "removing char %d:%d\n",
456                         MAJOR(rtc->rtc_dev->devt),
457                         MINOR(rtc->rtc_dev->devt));
458
459                 class_device_unregister(rtc->rtc_dev);
460                 cdev_del(&rtc->char_dev);
461         }
462 }
463
464 /* interface registration */
465
466 static struct class_interface rtc_dev_interface = {
467         .add = &rtc_dev_add_device,
468         .remove = &rtc_dev_remove_device,
469 };
470
471 static int __init rtc_dev_init(void)
472 {
473         int err;
474
475         rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
476         if (IS_ERR(rtc_dev_class))
477                 return PTR_ERR(rtc_dev_class);
478
479         err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
480         if (err < 0) {
481                 printk(KERN_ERR "%s: failed to allocate char dev region\n",
482                         __FILE__);
483                 goto err_destroy_class;
484         }
485
486         err = rtc_interface_register(&rtc_dev_interface);
487         if (err < 0) {
488                 printk(KERN_ERR "%s: failed to register the interface\n",
489                         __FILE__);
490                 goto err_unregister_chrdev;
491         }
492
493         return 0;
494
495 err_unregister_chrdev:
496         unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
497
498 err_destroy_class:
499         class_destroy(rtc_dev_class);
500
501         return err;
502 }
503
504 static void __exit rtc_dev_exit(void)
505 {
506         class_interface_unregister(&rtc_dev_interface);
507         class_destroy(rtc_dev_class);
508         unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
509 }
510
511 subsys_initcall(rtc_dev_init);
512 module_exit(rtc_dev_exit);
513
514 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
515 MODULE_DESCRIPTION("RTC class dev interface");
516 MODULE_LICENSE("GPL");