2 * RTC subsystem, dev interface
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c
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.
14 #include <linux/module.h>
15 #include <linux/rtc.h>
17 static struct class *rtc_dev_class;
18 static dev_t rtc_devt;
20 #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
22 static int rtc_dev_open(struct inode *inode, struct file *file)
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;
29 /* We keep the lock as long as the device is in use
30 * and return immediately if busy
32 if (!(mutex_trylock(&rtc->char_lock)))
35 file->private_data = &rtc->class_dev;
37 err = ops->open ? ops->open(rtc->class_dev.dev) : 0;
39 spin_lock_irq(&rtc->irq_lock);
41 spin_unlock_irq(&rtc->irq_lock);
46 /* something has gone wrong, release the lock */
47 mutex_unlock(&rtc->char_lock);
51 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
53 * Routine to poll RTC seconds field for change as often as possible,
54 * after first RTC_UIE use timer to reduce polling
56 static void rtc_uie_task(void *data)
58 struct rtc_device *rtc = data;
63 err = rtc_read_time(&rtc->class_dev, &tm);
64 spin_lock_irq(&rtc->irq_lock);
65 if (rtc->stop_uie_polling || err) {
66 rtc->uie_task_active = 0;
67 } else if (rtc->oldsecs != tm.tm_sec) {
68 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
69 rtc->oldsecs = tm.tm_sec;
70 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
71 rtc->uie_timer_active = 1;
72 rtc->uie_task_active = 0;
73 add_timer(&rtc->uie_timer);
74 } else if (schedule_work(&rtc->uie_task) == 0) {
75 rtc->uie_task_active = 0;
77 spin_unlock_irq(&rtc->irq_lock);
79 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
82 static void rtc_uie_timer(unsigned long data)
84 struct rtc_device *rtc = (struct rtc_device *)data;
87 spin_lock_irqsave(&rtc->irq_lock, flags);
88 rtc->uie_timer_active = 0;
89 rtc->uie_task_active = 1;
90 if ((schedule_work(&rtc->uie_task) == 0))
91 rtc->uie_task_active = 0;
92 spin_unlock_irqrestore(&rtc->irq_lock, flags);
95 static void clear_uie(struct rtc_device *rtc)
97 spin_lock_irq(&rtc->irq_lock);
98 if (rtc->irq_active) {
99 rtc->stop_uie_polling = 1;
100 if (rtc->uie_timer_active) {
101 spin_unlock_irq(&rtc->irq_lock);
102 del_timer_sync(&rtc->uie_timer);
103 spin_lock_irq(&rtc->irq_lock);
104 rtc->uie_timer_active = 0;
106 if (rtc->uie_task_active) {
107 spin_unlock_irq(&rtc->irq_lock);
108 flush_scheduled_work();
109 spin_lock_irq(&rtc->irq_lock);
113 spin_unlock_irq(&rtc->irq_lock);
116 static int set_uie(struct rtc_device *rtc)
121 err = rtc_read_time(&rtc->class_dev, &tm);
124 spin_lock_irq(&rtc->irq_lock);
125 if (!rtc->irq_active) {
127 rtc->stop_uie_polling = 0;
128 rtc->oldsecs = tm.tm_sec;
129 rtc->uie_task_active = 1;
130 if (schedule_work(&rtc->uie_task) == 0)
131 rtc->uie_task_active = 0;
134 spin_unlock_irq(&rtc->irq_lock);
137 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
140 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
142 struct rtc_device *rtc = to_rtc_device(file->private_data);
144 DECLARE_WAITQUEUE(wait, current);
148 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
151 add_wait_queue(&rtc->irq_queue, &wait);
153 __set_current_state(TASK_INTERRUPTIBLE);
155 spin_lock_irq(&rtc->irq_lock);
156 data = rtc->irq_data;
158 spin_unlock_irq(&rtc->irq_lock);
164 if (file->f_flags & O_NONBLOCK) {
168 if (signal_pending(current)) {
174 set_current_state(TASK_RUNNING);
175 remove_wait_queue(&rtc->irq_queue, &wait);
178 /* Check for any data updates */
179 if (rtc->ops->read_callback)
180 data = rtc->ops->read_callback(rtc->class_dev.dev,
183 if (sizeof(int) != sizeof(long) &&
184 count == sizeof(unsigned int))
185 ret = put_user(data, (unsigned int __user *)buf) ?:
186 sizeof(unsigned int);
188 ret = put_user(data, (unsigned long __user *)buf) ?:
189 sizeof(unsigned long);
194 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
196 struct rtc_device *rtc = to_rtc_device(file->private_data);
199 poll_wait(file, &rtc->irq_queue, wait);
201 data = rtc->irq_data;
203 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
206 static int rtc_dev_ioctl(struct inode *inode, struct file *file,
207 unsigned int cmd, unsigned long arg)
210 struct class_device *class_dev = file->private_data;
211 struct rtc_device *rtc = to_rtc_device(class_dev);
212 const struct rtc_class_ops *ops = rtc->ops;
214 struct rtc_wkalrm alarm;
215 void __user *uarg = (void __user *) arg;
217 /* check that the calling task has appropriate permissions
218 * for certain ioctls. doing this check here is useful
219 * to avoid duplicate code in each driver.
224 if (!capable(CAP_SYS_TIME))
229 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
234 if (!capable(CAP_SYS_RESOURCE))
239 /* avoid conflicting IRQ users */
240 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
241 spin_lock(&rtc->irq_task_lock);
244 spin_unlock(&rtc->irq_task_lock);
250 /* try the driver's ioctl interface */
252 err = ops->ioctl(class_dev->dev, cmd, arg);
253 if (err != -ENOIOCTLCMD)
257 /* if the driver does not provide the ioctl interface
258 * or if that particular ioctl was not implemented
259 * (-ENOIOCTLCMD), we will try to emulate here.
264 err = rtc_read_alarm(class_dev, &alarm);
268 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
273 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
278 alarm.time.tm_mday = -1;
279 alarm.time.tm_mon = -1;
280 alarm.time.tm_year = -1;
281 alarm.time.tm_wday = -1;
282 alarm.time.tm_yday = -1;
283 alarm.time.tm_isdst = -1;
284 err = rtc_set_alarm(class_dev, &alarm);
288 err = rtc_read_time(class_dev, &tm);
292 if (copy_to_user(uarg, &tm, sizeof(tm)))
297 if (copy_from_user(&tm, uarg, sizeof(tm)))
300 err = rtc_set_time(class_dev, &tm);
304 if (ops->irq_set_freq)
305 err = put_user(rtc->irq_freq, (unsigned long *) arg);
309 if (ops->irq_set_freq)
310 err = rtc_irq_set_freq(class_dev, rtc->irq_task, arg);
317 * There were no RTC clocks before 1900.
329 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
333 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
336 err = rtc_set_alarm(class_dev, &alarm);
340 err = rtc_read_alarm(class_dev, &alarm);
344 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
348 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
364 static int rtc_dev_release(struct inode *inode, struct file *file)
366 struct rtc_device *rtc = to_rtc_device(file->private_data);
368 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
371 if (rtc->ops->release)
372 rtc->ops->release(rtc->class_dev.dev);
374 mutex_unlock(&rtc->char_lock);
378 static int rtc_dev_fasync(int fd, struct file *file, int on)
380 struct rtc_device *rtc = to_rtc_device(file->private_data);
381 return fasync_helper(fd, file, on, &rtc->async_queue);
384 static struct file_operations rtc_dev_fops = {
385 .owner = THIS_MODULE,
387 .read = rtc_dev_read,
388 .poll = rtc_dev_poll,
389 .ioctl = rtc_dev_ioctl,
390 .open = rtc_dev_open,
391 .release = rtc_dev_release,
392 .fasync = rtc_dev_fasync,
395 /* insertion/removal hooks */
397 static int rtc_dev_add_device(struct class_device *class_dev,
398 struct class_interface *class_intf)
401 struct rtc_device *rtc = to_rtc_device(class_dev);
403 if (rtc->id >= RTC_DEV_MAX) {
404 dev_err(class_dev->dev, "too many RTCs\n");
408 mutex_init(&rtc->char_lock);
409 spin_lock_init(&rtc->irq_lock);
410 init_waitqueue_head(&rtc->irq_queue);
411 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
412 INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
413 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
416 cdev_init(&rtc->char_dev, &rtc_dev_fops);
417 rtc->char_dev.owner = rtc->owner;
419 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
420 dev_err(class_dev->dev,
421 "failed to add char device %d:%d\n",
422 MAJOR(rtc_devt), rtc->id);
426 rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
427 MKDEV(MAJOR(rtc_devt), rtc->id),
428 class_dev->dev, "rtc%d", rtc->id);
429 if (IS_ERR(rtc->rtc_dev)) {
430 dev_err(class_dev->dev, "cannot create rtc_dev device\n");
431 err = PTR_ERR(rtc->rtc_dev);
435 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
436 MAJOR(rtc->rtc_dev->devt),
437 MINOR(rtc->rtc_dev->devt));
443 cdev_del(&rtc->char_dev);
447 static void rtc_dev_remove_device(struct class_device *class_dev,
448 struct class_interface *class_intf)
450 struct rtc_device *rtc = to_rtc_device(class_dev);
453 dev_dbg(class_dev->dev, "removing char %d:%d\n",
454 MAJOR(rtc->rtc_dev->devt),
455 MINOR(rtc->rtc_dev->devt));
457 class_device_unregister(rtc->rtc_dev);
458 cdev_del(&rtc->char_dev);
462 /* interface registration */
464 static struct class_interface rtc_dev_interface = {
465 .add = &rtc_dev_add_device,
466 .remove = &rtc_dev_remove_device,
469 static int __init rtc_dev_init(void)
473 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
474 if (IS_ERR(rtc_dev_class))
475 return PTR_ERR(rtc_dev_class);
477 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
479 printk(KERN_ERR "%s: failed to allocate char dev region\n",
481 goto err_destroy_class;
484 err = rtc_interface_register(&rtc_dev_interface);
486 printk(KERN_ERR "%s: failed to register the interface\n",
488 goto err_unregister_chrdev;
493 err_unregister_chrdev:
494 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
497 class_destroy(rtc_dev_class);
502 static void __exit rtc_dev_exit(void)
504 class_interface_unregister(&rtc_dev_interface);
505 class_destroy(rtc_dev_class);
506 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
509 subsys_initcall(rtc_dev_init);
510 module_exit(rtc_dev_exit);
512 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
513 MODULE_DESCRIPTION("RTC class dev interface");
514 MODULE_LICENSE("GPL");