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/sched.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
;
30 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
33 file
->private_data
= rtc
;
35 err
= ops
->open
? ops
->open(rtc
->dev
.parent
) : 0;
37 spin_lock_irq(&rtc
->irq_lock
);
39 spin_unlock_irq(&rtc
->irq_lock
);
44 /* something has gone wrong */
45 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
51 rtc_dev_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
53 struct rtc_device
*rtc
= file
->private_data
;
55 DECLARE_WAITQUEUE(wait
, current
);
59 if (count
!= sizeof(unsigned int) && count
< sizeof(unsigned long))
62 add_wait_queue(&rtc
->irq_queue
, &wait
);
64 __set_current_state(TASK_INTERRUPTIBLE
);
66 spin_lock_irq(&rtc
->irq_lock
);
69 spin_unlock_irq(&rtc
->irq_lock
);
75 if (file
->f_flags
& O_NONBLOCK
) {
79 if (signal_pending(current
)) {
85 set_current_state(TASK_RUNNING
);
86 remove_wait_queue(&rtc
->irq_queue
, &wait
);
89 /* Check for any data updates */
90 if (rtc
->ops
->read_callback
)
91 data
= rtc
->ops
->read_callback(rtc
->dev
.parent
,
94 if (sizeof(int) != sizeof(long) &&
95 count
== sizeof(unsigned int))
96 ret
= put_user(data
, (unsigned int __user
*)buf
) ?:
99 ret
= put_user(data
, (unsigned long __user
*)buf
) ?:
100 sizeof(unsigned long);
105 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
107 struct rtc_device
*rtc
= file
->private_data
;
110 poll_wait(file
, &rtc
->irq_queue
, wait
);
112 data
= rtc
->irq_data
;
114 return (data
!= 0) ? (POLLIN
| POLLRDNORM
) : 0;
117 static long rtc_dev_ioctl(struct file
*file
,
118 unsigned int cmd
, unsigned long arg
)
121 struct rtc_device
*rtc
= file
->private_data
;
122 const struct rtc_class_ops
*ops
= rtc
->ops
;
124 struct rtc_wkalrm alarm
;
125 void __user
*uarg
= (void __user
*) arg
;
127 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
131 /* check that the calling task has appropriate permissions
132 * for certain ioctls. doing this check here is useful
133 * to avoid duplicate code in each driver.
138 if (!capable(CAP_SYS_TIME
))
143 if (arg
> rtc
->max_user_freq
&& !capable(CAP_SYS_RESOURCE
))
148 if (rtc
->irq_freq
> rtc
->max_user_freq
&&
149 !capable(CAP_SYS_RESOURCE
))
157 /* try the driver's ioctl interface */
159 err
= ops
->ioctl(rtc
->dev
.parent
, cmd
, arg
);
160 if (err
!= -ENOIOCTLCMD
) {
161 mutex_unlock(&rtc
->ops_lock
);
166 /* if the driver does not provide the ioctl interface
167 * or if that particular ioctl was not implemented
168 * (-ENOIOCTLCMD), we will try to emulate here.
170 * Drivers *SHOULD NOT* provide ioctl implementations
171 * for these requests. Instead, provide methods to
172 * support the following code, so that the RTC's main
173 * features are accessible without using ioctls.
175 * RTC and alarm times will be in UTC, by preference,
176 * but dual-booting with MS-Windows implies RTCs must
177 * use the local wall clock time.
182 mutex_unlock(&rtc
->ops_lock
);
184 err
= rtc_read_alarm(rtc
, &alarm
);
188 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
193 mutex_unlock(&rtc
->ops_lock
);
195 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
200 alarm
.time
.tm_wday
= -1;
201 alarm
.time
.tm_yday
= -1;
202 alarm
.time
.tm_isdst
= -1;
204 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
205 * Rather than expecting every RTC to implement "don't care"
206 * for day/month/year fields, just force the alarm to have
207 * the right values for those fields.
209 * RTC_WKALM_SET should be used instead. Not only does it
210 * eliminate the need for a separate RTC_AIE_ON call, it
211 * doesn't have the "alarm 23:59:59 in the future" race.
213 * NOTE: some legacy code may have used invalid fields as
214 * wildcards, exposing hardware "periodic alarm" capabilities.
215 * Not supported here.
218 unsigned long now
, then
;
220 err
= rtc_read_time(rtc
, &tm
);
223 rtc_tm_to_time(&tm
, &now
);
225 alarm
.time
.tm_mday
= tm
.tm_mday
;
226 alarm
.time
.tm_mon
= tm
.tm_mon
;
227 alarm
.time
.tm_year
= tm
.tm_year
;
228 err
= rtc_valid_tm(&alarm
.time
);
231 rtc_tm_to_time(&alarm
.time
, &then
);
233 /* alarm may need to wrap into tomorrow */
235 rtc_time_to_tm(now
+ 24 * 60 * 60, &tm
);
236 alarm
.time
.tm_mday
= tm
.tm_mday
;
237 alarm
.time
.tm_mon
= tm
.tm_mon
;
238 alarm
.time
.tm_year
= tm
.tm_year
;
242 return rtc_set_alarm(rtc
, &alarm
);
245 mutex_unlock(&rtc
->ops_lock
);
247 err
= rtc_read_time(rtc
, &tm
);
251 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
256 mutex_unlock(&rtc
->ops_lock
);
258 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
261 return rtc_set_time(rtc
, &tm
);
264 err
= rtc_irq_set_state(rtc
, NULL
, 1);
268 err
= rtc_irq_set_state(rtc
, NULL
, 0);
272 mutex_unlock(&rtc
->ops_lock
);
273 return rtc_alarm_irq_enable(rtc
, 1);
276 mutex_unlock(&rtc
->ops_lock
);
277 return rtc_alarm_irq_enable(rtc
, 0);
280 mutex_unlock(&rtc
->ops_lock
);
281 return rtc_update_irq_enable(rtc
, 1);
284 mutex_unlock(&rtc
->ops_lock
);
285 return rtc_update_irq_enable(rtc
, 0);
288 err
= rtc_irq_set_freq(rtc
, NULL
, arg
);
292 err
= put_user(rtc
->irq_freq
, (unsigned long __user
*)uarg
);
299 * There were no RTC clocks before 1900.
311 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
315 mutex_unlock(&rtc
->ops_lock
);
316 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
319 return rtc_set_alarm(rtc
, &alarm
);
322 mutex_unlock(&rtc
->ops_lock
);
323 err
= rtc_read_alarm(rtc
, &alarm
);
327 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
337 mutex_unlock(&rtc
->ops_lock
);
341 static int rtc_dev_fasync(int fd
, struct file
*file
, int on
)
343 struct rtc_device
*rtc
= file
->private_data
;
344 return fasync_helper(fd
, file
, on
, &rtc
->async_queue
);
347 static int rtc_dev_release(struct inode
*inode
, struct file
*file
)
349 struct rtc_device
*rtc
= file
->private_data
;
351 /* We shut down the repeating IRQs that userspace enabled,
352 * since nothing is listening to them.
353 * - Update (UIE) ... currently only managed through ioctls
354 * - Periodic (PIE) ... also used through rtc_*() interface calls
356 * Leave the alarm alone; it may be set to trigger a system wakeup
357 * later, or be used by kernel code, and is a one-shot event anyway.
360 /* Keep ioctl until all drivers are converted */
361 rtc_dev_ioctl(file
, RTC_UIE_OFF
, 0);
362 rtc_update_irq_enable(rtc
, 0);
363 rtc_irq_set_state(rtc
, NULL
, 0);
365 if (rtc
->ops
->release
)
366 rtc
->ops
->release(rtc
->dev
.parent
);
368 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
372 static const struct file_operations rtc_dev_fops
= {
373 .owner
= THIS_MODULE
,
375 .read
= rtc_dev_read
,
376 .poll
= rtc_dev_poll
,
377 .unlocked_ioctl
= rtc_dev_ioctl
,
378 .open
= rtc_dev_open
,
379 .release
= rtc_dev_release
,
380 .fasync
= rtc_dev_fasync
,
383 /* insertion/removal hooks */
385 void rtc_dev_prepare(struct rtc_device
*rtc
)
390 if (rtc
->id
>= RTC_DEV_MAX
) {
391 pr_debug("%s: too many RTC devices\n", rtc
->name
);
395 rtc
->dev
.devt
= MKDEV(MAJOR(rtc_devt
), rtc
->id
);
397 cdev_init(&rtc
->char_dev
, &rtc_dev_fops
);
398 rtc
->char_dev
.owner
= rtc
->owner
;
401 void rtc_dev_add_device(struct rtc_device
*rtc
)
403 if (cdev_add(&rtc
->char_dev
, rtc
->dev
.devt
, 1))
404 printk(KERN_WARNING
"%s: failed to add char device %d:%d\n",
405 rtc
->name
, MAJOR(rtc_devt
), rtc
->id
);
407 pr_debug("%s: dev (%d:%d)\n", rtc
->name
,
408 MAJOR(rtc_devt
), rtc
->id
);
411 void rtc_dev_del_device(struct rtc_device
*rtc
)
414 cdev_del(&rtc
->char_dev
);
417 void __init
rtc_dev_init(void)
421 err
= alloc_chrdev_region(&rtc_devt
, 0, RTC_DEV_MAX
, "rtc");
423 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
427 void __exit
rtc_dev_exit(void)
430 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);