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>
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 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
32 file->private_data = rtc;
34 err = ops->open ? ops->open(rtc->dev.parent) : 0;
36 spin_lock_irq(&rtc->irq_lock);
38 spin_unlock_irq(&rtc->irq_lock);
43 /* something has gone wrong */
44 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
48 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
50 * Routine to poll RTC seconds field for change as often as possible,
51 * after first RTC_UIE use timer to reduce polling
53 static void rtc_uie_task(struct work_struct *work)
55 struct rtc_device *rtc =
56 container_of(work, struct rtc_device, uie_task);
61 err = rtc_read_time(rtc, &tm);
63 spin_lock_irq(&rtc->irq_lock);
64 if (rtc->stop_uie_polling || err) {
65 rtc->uie_task_active = 0;
66 } else if (rtc->oldsecs != tm.tm_sec) {
67 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
68 rtc->oldsecs = tm.tm_sec;
69 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
70 rtc->uie_timer_active = 1;
71 rtc->uie_task_active = 0;
72 add_timer(&rtc->uie_timer);
73 } else if (schedule_work(&rtc->uie_task) == 0) {
74 rtc->uie_task_active = 0;
76 spin_unlock_irq(&rtc->irq_lock);
78 rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
80 static void rtc_uie_timer(unsigned long data)
82 struct rtc_device *rtc = (struct rtc_device *)data;
85 spin_lock_irqsave(&rtc->irq_lock, flags);
86 rtc->uie_timer_active = 0;
87 rtc->uie_task_active = 1;
88 if ((schedule_work(&rtc->uie_task) == 0))
89 rtc->uie_task_active = 0;
90 spin_unlock_irqrestore(&rtc->irq_lock, flags);
93 static int clear_uie(struct rtc_device *rtc)
95 spin_lock_irq(&rtc->irq_lock);
96 if (rtc->uie_irq_active) {
97 rtc->stop_uie_polling = 1;
98 if (rtc->uie_timer_active) {
99 spin_unlock_irq(&rtc->irq_lock);
100 del_timer_sync(&rtc->uie_timer);
101 spin_lock_irq(&rtc->irq_lock);
102 rtc->uie_timer_active = 0;
104 if (rtc->uie_task_active) {
105 spin_unlock_irq(&rtc->irq_lock);
106 flush_scheduled_work();
107 spin_lock_irq(&rtc->irq_lock);
109 rtc->uie_irq_active = 0;
111 spin_unlock_irq(&rtc->irq_lock);
115 static int set_uie(struct rtc_device *rtc)
120 err = rtc_read_time(rtc, &tm);
123 spin_lock_irq(&rtc->irq_lock);
124 if (!rtc->uie_irq_active) {
125 rtc->uie_irq_active = 1;
126 rtc->stop_uie_polling = 0;
127 rtc->oldsecs = tm.tm_sec;
128 rtc->uie_task_active = 1;
129 if (schedule_work(&rtc->uie_task) == 0)
130 rtc->uie_task_active = 0;
133 spin_unlock_irq(&rtc->irq_lock);
137 int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
142 return clear_uie(rtc);
144 EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
146 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
149 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
151 struct rtc_device *rtc = file->private_data;
153 DECLARE_WAITQUEUE(wait, current);
157 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
160 add_wait_queue(&rtc->irq_queue, &wait);
162 __set_current_state(TASK_INTERRUPTIBLE);
164 spin_lock_irq(&rtc->irq_lock);
165 data = rtc->irq_data;
167 spin_unlock_irq(&rtc->irq_lock);
173 if (file->f_flags & O_NONBLOCK) {
177 if (signal_pending(current)) {
183 set_current_state(TASK_RUNNING);
184 remove_wait_queue(&rtc->irq_queue, &wait);
187 /* Check for any data updates */
188 if (rtc->ops->read_callback)
189 data = rtc->ops->read_callback(rtc->dev.parent,
192 if (sizeof(int) != sizeof(long) &&
193 count == sizeof(unsigned int))
194 ret = put_user(data, (unsigned int __user *)buf) ?:
195 sizeof(unsigned int);
197 ret = put_user(data, (unsigned long __user *)buf) ?:
198 sizeof(unsigned long);
203 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
205 struct rtc_device *rtc = file->private_data;
208 poll_wait(file, &rtc->irq_queue, wait);
210 data = rtc->irq_data;
212 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
215 static long rtc_dev_ioctl(struct file *file,
216 unsigned int cmd, unsigned long arg)
219 struct rtc_device *rtc = file->private_data;
220 const struct rtc_class_ops *ops = rtc->ops;
222 struct rtc_wkalrm alarm;
223 void __user *uarg = (void __user *) arg;
225 err = mutex_lock_interruptible(&rtc->ops_lock);
229 /* check that the calling task has appropriate permissions
230 * for certain ioctls. doing this check here is useful
231 * to avoid duplicate code in each driver.
236 if (!capable(CAP_SYS_TIME))
241 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
246 if (rtc->irq_freq > rtc->max_user_freq &&
247 !capable(CAP_SYS_RESOURCE))
255 /* try the driver's ioctl interface */
257 err = ops->ioctl(rtc->dev.parent, cmd, arg);
258 if (err != -ENOIOCTLCMD) {
259 mutex_unlock(&rtc->ops_lock);
264 /* if the driver does not provide the ioctl interface
265 * or if that particular ioctl was not implemented
266 * (-ENOIOCTLCMD), we will try to emulate here.
268 * Drivers *SHOULD NOT* provide ioctl implementations
269 * for these requests. Instead, provide methods to
270 * support the following code, so that the RTC's main
271 * features are accessible without using ioctls.
273 * RTC and alarm times will be in UTC, by preference,
274 * but dual-booting with MS-Windows implies RTCs must
275 * use the local wall clock time.
280 mutex_unlock(&rtc->ops_lock);
282 err = rtc_read_alarm(rtc, &alarm);
286 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
291 mutex_unlock(&rtc->ops_lock);
293 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
298 alarm.time.tm_wday = -1;
299 alarm.time.tm_yday = -1;
300 alarm.time.tm_isdst = -1;
302 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
303 * Rather than expecting every RTC to implement "don't care"
304 * for day/month/year fields, just force the alarm to have
305 * the right values for those fields.
307 * RTC_WKALM_SET should be used instead. Not only does it
308 * eliminate the need for a separate RTC_AIE_ON call, it
309 * doesn't have the "alarm 23:59:59 in the future" race.
311 * NOTE: some legacy code may have used invalid fields as
312 * wildcards, exposing hardware "periodic alarm" capabilities.
313 * Not supported here.
316 unsigned long now, then;
318 err = rtc_read_time(rtc, &tm);
321 rtc_tm_to_time(&tm, &now);
323 alarm.time.tm_mday = tm.tm_mday;
324 alarm.time.tm_mon = tm.tm_mon;
325 alarm.time.tm_year = tm.tm_year;
326 err = rtc_valid_tm(&alarm.time);
329 rtc_tm_to_time(&alarm.time, &then);
331 /* alarm may need to wrap into tomorrow */
333 rtc_time_to_tm(now + 24 * 60 * 60, &tm);
334 alarm.time.tm_mday = tm.tm_mday;
335 alarm.time.tm_mon = tm.tm_mon;
336 alarm.time.tm_year = tm.tm_year;
340 return rtc_set_alarm(rtc, &alarm);
343 mutex_unlock(&rtc->ops_lock);
345 err = rtc_read_time(rtc, &tm);
349 if (copy_to_user(uarg, &tm, sizeof(tm)))
354 mutex_unlock(&rtc->ops_lock);
356 if (copy_from_user(&tm, uarg, sizeof(tm)))
359 return rtc_set_time(rtc, &tm);
362 err = rtc_irq_set_state(rtc, NULL, 1);
366 err = rtc_irq_set_state(rtc, NULL, 0);
370 mutex_unlock(&rtc->ops_lock);
371 return rtc_alarm_irq_enable(rtc, 1);
374 mutex_unlock(&rtc->ops_lock);
375 return rtc_alarm_irq_enable(rtc, 0);
378 mutex_unlock(&rtc->ops_lock);
379 return rtc_update_irq_enable(rtc, 1);
382 mutex_unlock(&rtc->ops_lock);
383 return rtc_update_irq_enable(rtc, 0);
386 err = rtc_irq_set_freq(rtc, NULL, arg);
390 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
397 * There were no RTC clocks before 1900.
409 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
413 mutex_unlock(&rtc->ops_lock);
414 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
417 return rtc_set_alarm(rtc, &alarm);
420 mutex_unlock(&rtc->ops_lock);
421 err = rtc_read_alarm(rtc, &alarm);
425 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
435 mutex_unlock(&rtc->ops_lock);
439 static int rtc_dev_fasync(int fd, struct file *file, int on)
441 struct rtc_device *rtc = file->private_data;
442 return fasync_helper(fd, file, on, &rtc->async_queue);
445 static int rtc_dev_release(struct inode *inode, struct file *file)
447 struct rtc_device *rtc = file->private_data;
449 /* We shut down the repeating IRQs that userspace enabled,
450 * since nothing is listening to them.
451 * - Update (UIE) ... currently only managed through ioctls
452 * - Periodic (PIE) ... also used through rtc_*() interface calls
454 * Leave the alarm alone; it may be set to trigger a system wakeup
455 * later, or be used by kernel code, and is a one-shot event anyway.
458 /* Keep ioctl until all drivers are converted */
459 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
460 rtc_update_irq_enable(rtc, 0);
461 rtc_irq_set_state(rtc, NULL, 0);
463 if (rtc->ops->release)
464 rtc->ops->release(rtc->dev.parent);
466 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
470 static const struct file_operations rtc_dev_fops = {
471 .owner = THIS_MODULE,
473 .read = rtc_dev_read,
474 .poll = rtc_dev_poll,
475 .unlocked_ioctl = rtc_dev_ioctl,
476 .open = rtc_dev_open,
477 .release = rtc_dev_release,
478 .fasync = rtc_dev_fasync,
481 /* insertion/removal hooks */
483 void rtc_dev_prepare(struct rtc_device *rtc)
488 if (rtc->id >= RTC_DEV_MAX) {
489 pr_debug("%s: too many RTC devices\n", rtc->name);
493 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
495 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
496 INIT_WORK(&rtc->uie_task, rtc_uie_task);
497 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
500 cdev_init(&rtc->char_dev, &rtc_dev_fops);
501 rtc->char_dev.owner = rtc->owner;
504 void rtc_dev_add_device(struct rtc_device *rtc)
506 if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
507 printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
508 rtc->name, MAJOR(rtc_devt), rtc->id);
510 pr_debug("%s: dev (%d:%d)\n", rtc->name,
511 MAJOR(rtc_devt), rtc->id);
514 void rtc_dev_del_device(struct rtc_device *rtc)
517 cdev_del(&rtc->char_dev);
520 void __init rtc_dev_init(void)
524 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
526 printk(KERN_ERR "%s: failed to allocate char dev region\n",
530 void __exit rtc_dev_exit(void)
533 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);