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>
16 #include <linux/smp_lock.h>
19 static dev_t rtc_devt
;
21 #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
23 static int rtc_dev_open(struct inode
*inode
, struct file
*file
)
26 struct rtc_device
*rtc
= container_of(inode
->i_cdev
,
27 struct rtc_device
, char_dev
);
28 const struct rtc_class_ops
*ops
= rtc
->ops
;
31 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
)) {
36 file
->private_data
= rtc
;
38 err
= ops
->open
? ops
->open(rtc
->dev
.parent
) : 0;
40 spin_lock_irq(&rtc
->irq_lock
);
42 spin_unlock_irq(&rtc
->irq_lock
);
47 /* something has gone wrong */
48 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
54 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
56 * Routine to poll RTC seconds field for change as often as possible,
57 * after first RTC_UIE use timer to reduce polling
59 static void rtc_uie_task(struct work_struct
*work
)
61 struct rtc_device
*rtc
=
62 container_of(work
, struct rtc_device
, uie_task
);
67 err
= rtc_read_time(rtc
, &tm
);
70 spin_lock(&rtc
->irq_lock
);
71 if (rtc
->stop_uie_polling
|| err
) {
72 rtc
->uie_task_active
= 0;
73 } else if (rtc
->oldsecs
!= tm
.tm_sec
) {
74 num
= (tm
.tm_sec
+ 60 - rtc
->oldsecs
) % 60;
75 rtc
->oldsecs
= tm
.tm_sec
;
76 rtc
->uie_timer
.expires
= jiffies
+ HZ
- (HZ
/10);
77 rtc
->uie_timer_active
= 1;
78 rtc
->uie_task_active
= 0;
79 add_timer(&rtc
->uie_timer
);
80 } else if (schedule_work(&rtc
->uie_task
) == 0) {
81 rtc
->uie_task_active
= 0;
83 spin_unlock(&rtc
->irq_lock
);
85 rtc_update_irq(rtc
, num
, RTC_UF
| RTC_IRQF
);
88 static void rtc_uie_timer(unsigned long data
)
90 struct rtc_device
*rtc
= (struct rtc_device
*)data
;
93 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
94 rtc
->uie_timer_active
= 0;
95 rtc
->uie_task_active
= 1;
96 if ((schedule_work(&rtc
->uie_task
) == 0))
97 rtc
->uie_task_active
= 0;
98 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
101 static void clear_uie(struct rtc_device
*rtc
)
103 spin_lock_irq(&rtc
->irq_lock
);
104 if (rtc
->irq_active
) {
105 rtc
->stop_uie_polling
= 1;
106 if (rtc
->uie_timer_active
) {
107 spin_unlock_irq(&rtc
->irq_lock
);
108 del_timer_sync(&rtc
->uie_timer
);
109 spin_lock_irq(&rtc
->irq_lock
);
110 rtc
->uie_timer_active
= 0;
112 if (rtc
->uie_task_active
) {
113 spin_unlock_irq(&rtc
->irq_lock
);
114 flush_scheduled_work();
115 spin_lock_irq(&rtc
->irq_lock
);
119 spin_unlock_irq(&rtc
->irq_lock
);
122 static int set_uie(struct rtc_device
*rtc
)
127 err
= rtc_read_time(rtc
, &tm
);
130 spin_lock_irq(&rtc
->irq_lock
);
131 if (!rtc
->irq_active
) {
133 rtc
->stop_uie_polling
= 0;
134 rtc
->oldsecs
= tm
.tm_sec
;
135 rtc
->uie_task_active
= 1;
136 if (schedule_work(&rtc
->uie_task
) == 0)
137 rtc
->uie_task_active
= 0;
140 spin_unlock_irq(&rtc
->irq_lock
);
143 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
146 rtc_dev_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
148 struct rtc_device
*rtc
= file
->private_data
;
150 DECLARE_WAITQUEUE(wait
, current
);
154 if (count
!= sizeof(unsigned int) && count
< sizeof(unsigned long))
157 add_wait_queue(&rtc
->irq_queue
, &wait
);
159 __set_current_state(TASK_INTERRUPTIBLE
);
161 spin_lock_irq(&rtc
->irq_lock
);
162 data
= rtc
->irq_data
;
164 spin_unlock_irq(&rtc
->irq_lock
);
170 if (file
->f_flags
& O_NONBLOCK
) {
174 if (signal_pending(current
)) {
180 set_current_state(TASK_RUNNING
);
181 remove_wait_queue(&rtc
->irq_queue
, &wait
);
184 /* Check for any data updates */
185 if (rtc
->ops
->read_callback
)
186 data
= rtc
->ops
->read_callback(rtc
->dev
.parent
,
189 if (sizeof(int) != sizeof(long) &&
190 count
== sizeof(unsigned int))
191 ret
= put_user(data
, (unsigned int __user
*)buf
) ?:
192 sizeof(unsigned int);
194 ret
= put_user(data
, (unsigned long __user
*)buf
) ?:
195 sizeof(unsigned long);
200 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
202 struct rtc_device
*rtc
= file
->private_data
;
205 poll_wait(file
, &rtc
->irq_queue
, wait
);
207 data
= rtc
->irq_data
;
209 return (data
!= 0) ? (POLLIN
| POLLRDNORM
) : 0;
212 static long rtc_dev_ioctl(struct file
*file
,
213 unsigned int cmd
, unsigned long arg
)
216 struct rtc_device
*rtc
= file
->private_data
;
217 const struct rtc_class_ops
*ops
= rtc
->ops
;
219 struct rtc_wkalrm alarm
;
220 void __user
*uarg
= (void __user
*) arg
;
222 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
226 /* check that the calling task has appropriate permissions
227 * for certain ioctls. doing this check here is useful
228 * to avoid duplicate code in each driver.
233 if (!capable(CAP_SYS_TIME
))
238 if (arg
> rtc
->max_user_freq
&& !capable(CAP_SYS_RESOURCE
))
243 if (rtc
->irq_freq
> rtc
->max_user_freq
&&
244 !capable(CAP_SYS_RESOURCE
))
252 /* try the driver's ioctl interface */
254 err
= ops
->ioctl(rtc
->dev
.parent
, cmd
, arg
);
255 if (err
!= -ENOIOCTLCMD
) {
256 mutex_unlock(&rtc
->ops_lock
);
261 /* if the driver does not provide the ioctl interface
262 * or if that particular ioctl was not implemented
263 * (-ENOIOCTLCMD), we will try to emulate here.
265 * Drivers *SHOULD NOT* provide ioctl implementations
266 * for these requests. Instead, provide methods to
267 * support the following code, so that the RTC's main
268 * features are accessible without using ioctls.
270 * RTC and alarm times will be in UTC, by preference,
271 * but dual-booting with MS-Windows implies RTCs must
272 * use the local wall clock time.
277 mutex_unlock(&rtc
->ops_lock
);
279 err
= rtc_read_alarm(rtc
, &alarm
);
283 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
288 mutex_unlock(&rtc
->ops_lock
);
290 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
295 alarm
.time
.tm_wday
= -1;
296 alarm
.time
.tm_yday
= -1;
297 alarm
.time
.tm_isdst
= -1;
299 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
300 * Rather than expecting every RTC to implement "don't care"
301 * for day/month/year fields, just force the alarm to have
302 * the right values for those fields.
304 * RTC_WKALM_SET should be used instead. Not only does it
305 * eliminate the need for a separate RTC_AIE_ON call, it
306 * doesn't have the "alarm 23:59:59 in the future" race.
308 * NOTE: some legacy code may have used invalid fields as
309 * wildcards, exposing hardware "periodic alarm" capabilities.
310 * Not supported here.
313 unsigned long now
, then
;
315 err
= rtc_read_time(rtc
, &tm
);
318 rtc_tm_to_time(&tm
, &now
);
320 alarm
.time
.tm_mday
= tm
.tm_mday
;
321 alarm
.time
.tm_mon
= tm
.tm_mon
;
322 alarm
.time
.tm_year
= tm
.tm_year
;
323 err
= rtc_valid_tm(&alarm
.time
);
326 rtc_tm_to_time(&alarm
.time
, &then
);
328 /* alarm may need to wrap into tomorrow */
330 rtc_time_to_tm(now
+ 24 * 60 * 60, &tm
);
331 alarm
.time
.tm_mday
= tm
.tm_mday
;
332 alarm
.time
.tm_mon
= tm
.tm_mon
;
333 alarm
.time
.tm_year
= tm
.tm_year
;
337 return rtc_set_alarm(rtc
, &alarm
);
340 mutex_unlock(&rtc
->ops_lock
);
342 err
= rtc_read_time(rtc
, &tm
);
346 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
351 mutex_unlock(&rtc
->ops_lock
);
353 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
356 return rtc_set_time(rtc
, &tm
);
359 err
= rtc_irq_set_state(rtc
, NULL
, 1);
363 err
= rtc_irq_set_state(rtc
, NULL
, 0);
367 err
= rtc_irq_set_freq(rtc
, NULL
, arg
);
371 err
= put_user(rtc
->irq_freq
, (unsigned long __user
*)uarg
);
378 * There were no RTC clocks before 1900.
390 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
394 mutex_unlock(&rtc
->ops_lock
);
395 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
398 return rtc_set_alarm(rtc
, &alarm
);
401 mutex_unlock(&rtc
->ops_lock
);
402 err
= rtc_read_alarm(rtc
, &alarm
);
406 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
410 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
424 mutex_unlock(&rtc
->ops_lock
);
428 static int rtc_dev_release(struct inode
*inode
, struct file
*file
)
430 struct rtc_device
*rtc
= file
->private_data
;
432 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
435 if (rtc
->ops
->release
)
436 rtc
->ops
->release(rtc
->dev
.parent
);
438 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
442 static int rtc_dev_fasync(int fd
, struct file
*file
, int on
)
444 struct rtc_device
*rtc
= file
->private_data
;
445 return fasync_helper(fd
, file
, on
, &rtc
->async_queue
);
448 static const struct file_operations rtc_dev_fops
= {
449 .owner
= THIS_MODULE
,
451 .read
= rtc_dev_read
,
452 .poll
= rtc_dev_poll
,
453 .unlocked_ioctl
= rtc_dev_ioctl
,
454 .open
= rtc_dev_open
,
455 .release
= rtc_dev_release
,
456 .fasync
= rtc_dev_fasync
,
459 /* insertion/removal hooks */
461 void rtc_dev_prepare(struct rtc_device
*rtc
)
466 if (rtc
->id
>= RTC_DEV_MAX
) {
467 pr_debug("%s: too many RTC devices\n", rtc
->name
);
471 rtc
->dev
.devt
= MKDEV(MAJOR(rtc_devt
), rtc
->id
);
473 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
474 INIT_WORK(&rtc
->uie_task
, rtc_uie_task
);
475 setup_timer(&rtc
->uie_timer
, rtc_uie_timer
, (unsigned long)rtc
);
478 cdev_init(&rtc
->char_dev
, &rtc_dev_fops
);
479 rtc
->char_dev
.owner
= rtc
->owner
;
482 void rtc_dev_add_device(struct rtc_device
*rtc
)
484 if (cdev_add(&rtc
->char_dev
, rtc
->dev
.devt
, 1))
485 printk(KERN_WARNING
"%s: failed to add char device %d:%d\n",
486 rtc
->name
, MAJOR(rtc_devt
), rtc
->id
);
488 pr_debug("%s: dev (%d:%d)\n", rtc
->name
,
489 MAJOR(rtc_devt
), rtc
->id
);
492 void rtc_dev_del_device(struct rtc_device
*rtc
)
495 cdev_del(&rtc
->char_dev
);
498 void __init
rtc_dev_init(void)
502 err
= alloc_chrdev_region(&rtc_devt
, 0, RTC_DEV_MAX
, "rtc");
504 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
508 void __exit
rtc_dev_exit(void)
511 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);