1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, dev interface
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
8 * based on arch/arm/common/rtctime.c
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/compat.h>
14 #include <linux/module.h>
15 #include <linux/rtc.h>
16 #include <linux/sched/signal.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)
25 struct rtc_device *rtc = container_of(inode->i_cdev,
26 struct rtc_device, char_dev);
28 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
31 file->private_data = rtc;
33 spin_lock_irq(&rtc->irq_lock);
35 spin_unlock_irq(&rtc->irq_lock);
40 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
42 * Routine to poll RTC seconds field for change as often as possible,
43 * after first RTC_UIE use timer to reduce polling
45 static void rtc_uie_task(struct work_struct *work)
47 struct rtc_device *rtc =
48 container_of(work, struct rtc_device, uie_task);
53 err = rtc_read_time(rtc, &tm);
55 spin_lock_irq(&rtc->irq_lock);
56 if (rtc->stop_uie_polling || err) {
57 rtc->uie_task_active = 0;
58 } else if (rtc->oldsecs != tm.tm_sec) {
59 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
60 rtc->oldsecs = tm.tm_sec;
61 rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
62 rtc->uie_timer_active = 1;
63 rtc->uie_task_active = 0;
64 add_timer(&rtc->uie_timer);
65 } else if (schedule_work(&rtc->uie_task) == 0) {
66 rtc->uie_task_active = 0;
68 spin_unlock_irq(&rtc->irq_lock);
70 rtc_handle_legacy_irq(rtc, num, RTC_UF);
73 static void rtc_uie_timer(struct timer_list *t)
75 struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
78 spin_lock_irqsave(&rtc->irq_lock, flags);
79 rtc->uie_timer_active = 0;
80 rtc->uie_task_active = 1;
81 if ((schedule_work(&rtc->uie_task) == 0))
82 rtc->uie_task_active = 0;
83 spin_unlock_irqrestore(&rtc->irq_lock, flags);
86 static int clear_uie(struct rtc_device *rtc)
88 spin_lock_irq(&rtc->irq_lock);
89 if (rtc->uie_irq_active) {
90 rtc->stop_uie_polling = 1;
91 if (rtc->uie_timer_active) {
92 spin_unlock_irq(&rtc->irq_lock);
93 del_timer_sync(&rtc->uie_timer);
94 spin_lock_irq(&rtc->irq_lock);
95 rtc->uie_timer_active = 0;
97 if (rtc->uie_task_active) {
98 spin_unlock_irq(&rtc->irq_lock);
99 flush_work(&rtc->uie_task);
100 spin_lock_irq(&rtc->irq_lock);
102 rtc->uie_irq_active = 0;
104 spin_unlock_irq(&rtc->irq_lock);
108 static int set_uie(struct rtc_device *rtc)
113 err = rtc_read_time(rtc, &tm);
116 spin_lock_irq(&rtc->irq_lock);
117 if (!rtc->uie_irq_active) {
118 rtc->uie_irq_active = 1;
119 rtc->stop_uie_polling = 0;
120 rtc->oldsecs = tm.tm_sec;
121 rtc->uie_task_active = 1;
122 if (schedule_work(&rtc->uie_task) == 0)
123 rtc->uie_task_active = 0;
126 spin_unlock_irq(&rtc->irq_lock);
130 int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
135 return clear_uie(rtc);
137 EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
139 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
142 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
144 struct rtc_device *rtc = file->private_data;
146 DECLARE_WAITQUEUE(wait, current);
150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
153 add_wait_queue(&rtc->irq_queue, &wait);
155 __set_current_state(TASK_INTERRUPTIBLE);
157 spin_lock_irq(&rtc->irq_lock);
158 data = rtc->irq_data;
160 spin_unlock_irq(&rtc->irq_lock);
166 if (file->f_flags & O_NONBLOCK) {
170 if (signal_pending(current)) {
176 set_current_state(TASK_RUNNING);
177 remove_wait_queue(&rtc->irq_queue, &wait);
180 if (sizeof(int) != sizeof(long) &&
181 count == sizeof(unsigned int))
182 ret = put_user(data, (unsigned int __user *)buf) ?:
183 sizeof(unsigned int);
185 ret = put_user(data, (unsigned long __user *)buf) ?:
186 sizeof(unsigned long);
191 static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
193 struct rtc_device *rtc = file->private_data;
196 poll_wait(file, &rtc->irq_queue, wait);
198 data = rtc->irq_data;
200 return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
203 static long rtc_dev_ioctl(struct file *file,
204 unsigned int cmd, unsigned long arg)
207 struct rtc_device *rtc = file->private_data;
208 const struct rtc_class_ops *ops = rtc->ops;
210 struct rtc_wkalrm alarm;
211 struct rtc_param param;
212 void __user *uarg = (void __user *)arg;
214 err = mutex_lock_interruptible(&rtc->ops_lock);
218 /* check that the calling task has appropriate permissions
219 * for certain ioctls. doing this check here is useful
220 * to avoid duplicate code in each driver.
226 if (!capable(CAP_SYS_TIME))
231 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
236 if (rtc->irq_freq > rtc->max_user_freq &&
237 !capable(CAP_SYS_RESOURCE))
246 * Drivers *SHOULD NOT* provide ioctl implementations
247 * for these requests. Instead, provide methods to
248 * support the following code, so that the RTC's main
249 * features are accessible without using ioctls.
251 * RTC and alarm times will be in UTC, by preference,
252 * but dual-booting with MS-Windows implies RTCs must
253 * use the local wall clock time.
258 mutex_unlock(&rtc->ops_lock);
260 err = rtc_read_alarm(rtc, &alarm);
264 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
269 mutex_unlock(&rtc->ops_lock);
271 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
276 alarm.time.tm_wday = -1;
277 alarm.time.tm_yday = -1;
278 alarm.time.tm_isdst = -1;
280 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
281 * Rather than expecting every RTC to implement "don't care"
282 * for day/month/year fields, just force the alarm to have
283 * the right values for those fields.
285 * RTC_WKALM_SET should be used instead. Not only does it
286 * eliminate the need for a separate RTC_AIE_ON call, it
287 * doesn't have the "alarm 23:59:59 in the future" race.
289 * NOTE: some legacy code may have used invalid fields as
290 * wildcards, exposing hardware "periodic alarm" capabilities.
291 * Not supported here.
296 err = rtc_read_time(rtc, &tm);
299 now = rtc_tm_to_time64(&tm);
301 alarm.time.tm_mday = tm.tm_mday;
302 alarm.time.tm_mon = tm.tm_mon;
303 alarm.time.tm_year = tm.tm_year;
304 err = rtc_valid_tm(&alarm.time);
307 then = rtc_tm_to_time64(&alarm.time);
309 /* alarm may need to wrap into tomorrow */
311 rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
312 alarm.time.tm_mday = tm.tm_mday;
313 alarm.time.tm_mon = tm.tm_mon;
314 alarm.time.tm_year = tm.tm_year;
318 return rtc_set_alarm(rtc, &alarm);
321 mutex_unlock(&rtc->ops_lock);
323 err = rtc_read_time(rtc, &tm);
327 if (copy_to_user(uarg, &tm, sizeof(tm)))
332 mutex_unlock(&rtc->ops_lock);
334 if (copy_from_user(&tm, uarg, sizeof(tm)))
337 return rtc_set_time(rtc, &tm);
340 err = rtc_irq_set_state(rtc, 1);
344 err = rtc_irq_set_state(rtc, 0);
348 mutex_unlock(&rtc->ops_lock);
349 return rtc_alarm_irq_enable(rtc, 1);
352 mutex_unlock(&rtc->ops_lock);
353 return rtc_alarm_irq_enable(rtc, 0);
356 mutex_unlock(&rtc->ops_lock);
357 return rtc_update_irq_enable(rtc, 1);
360 mutex_unlock(&rtc->ops_lock);
361 return rtc_update_irq_enable(rtc, 0);
364 err = rtc_irq_set_freq(rtc, arg);
367 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
371 mutex_unlock(&rtc->ops_lock);
372 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
375 return rtc_set_alarm(rtc, &alarm);
378 mutex_unlock(&rtc->ops_lock);
379 err = rtc_read_alarm(rtc, &alarm);
383 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
388 if (copy_from_user(¶m, uarg, sizeof(param))) {
389 mutex_unlock(&rtc->ops_lock);
393 switch(param.param) {
394 case RTC_PARAM_FEATURES:
395 if (param.index != 0)
397 param.uvalue = rtc->features[0];
400 case RTC_PARAM_CORRECTION: {
402 mutex_unlock(&rtc->ops_lock);
403 if (param.index != 0)
405 err = rtc_read_offset(rtc, &offset);
406 mutex_lock(&rtc->ops_lock);
408 param.svalue = offset;
412 if (rtc->ops->param_get)
413 err = rtc->ops->param_get(rtc->dev.parent, ¶m);
419 if (copy_to_user(uarg, ¶m, sizeof(param)))
425 if (copy_from_user(¶m, uarg, sizeof(param))) {
426 mutex_unlock(&rtc->ops_lock);
430 switch(param.param) {
431 case RTC_PARAM_FEATURES:
435 case RTC_PARAM_CORRECTION:
436 mutex_unlock(&rtc->ops_lock);
437 if (param.index != 0)
439 return rtc_set_offset(rtc, param.svalue);
442 if (rtc->ops->param_set)
443 err = rtc->ops->param_set(rtc->dev.parent, ¶m);
451 /* Finally try the driver's ioctl interface */
453 err = ops->ioctl(rtc->dev.parent, cmd, arg);
454 if (err == -ENOIOCTLCMD)
463 mutex_unlock(&rtc->ops_lock);
468 #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
469 #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
470 #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
472 static long rtc_dev_compat_ioctl(struct file *file,
473 unsigned int cmd, unsigned long arg)
475 struct rtc_device *rtc = file->private_data;
476 void __user *uarg = compat_ptr(arg);
479 case RTC_IRQP_READ32:
480 return put_user(rtc->irq_freq, (__u32 __user *)uarg);
483 /* arg is a plain integer, not pointer */
484 return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
486 case RTC_EPOCH_SET32:
487 /* arg is a plain integer, not pointer */
488 return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
491 return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
495 static int rtc_dev_fasync(int fd, struct file *file, int on)
497 struct rtc_device *rtc = file->private_data;
499 return fasync_helper(fd, file, on, &rtc->async_queue);
502 static int rtc_dev_release(struct inode *inode, struct file *file)
504 struct rtc_device *rtc = file->private_data;
506 /* We shut down the repeating IRQs that userspace enabled,
507 * since nothing is listening to them.
508 * - Update (UIE) ... currently only managed through ioctls
509 * - Periodic (PIE) ... also used through rtc_*() interface calls
511 * Leave the alarm alone; it may be set to trigger a system wakeup
512 * later, or be used by kernel code, and is a one-shot event anyway.
515 /* Keep ioctl until all drivers are converted */
516 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
517 rtc_update_irq_enable(rtc, 0);
518 rtc_irq_set_state(rtc, 0);
520 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
524 static const struct file_operations rtc_dev_fops = {
525 .owner = THIS_MODULE,
527 .read = rtc_dev_read,
528 .poll = rtc_dev_poll,
529 .unlocked_ioctl = rtc_dev_ioctl,
531 .compat_ioctl = rtc_dev_compat_ioctl,
533 .open = rtc_dev_open,
534 .release = rtc_dev_release,
535 .fasync = rtc_dev_fasync,
538 /* insertion/removal hooks */
540 void rtc_dev_prepare(struct rtc_device *rtc)
545 if (rtc->id >= RTC_DEV_MAX) {
546 dev_dbg(&rtc->dev, "too many RTC devices\n");
550 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
552 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
553 INIT_WORK(&rtc->uie_task, rtc_uie_task);
554 timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
557 cdev_init(&rtc->char_dev, &rtc_dev_fops);
558 rtc->char_dev.owner = rtc->owner;
561 void __init rtc_dev_init(void)
565 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
567 pr_err("failed to allocate char dev region\n");