1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, dev interface
5 * Copyright (C) 2005 Tower Technologies
8 * based on arch/arm/common/rtctime.c
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/rtc.h>
15 #include <linux/sched/signal.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)
24 struct rtc_device *rtc = container_of(inode->i_cdev,
25 struct rtc_device, char_dev);
27 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
30 file->private_data = rtc;
32 spin_lock_irq(&rtc->irq_lock);
34 spin_unlock_irq(&rtc->irq_lock);
39 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
41 * Routine to poll RTC seconds field for change as often as possible,
42 * after first RTC_UIE use timer to reduce polling
44 static void rtc_uie_task(struct work_struct *work)
46 struct rtc_device *rtc =
47 container_of(work, struct rtc_device, uie_task);
52 err = rtc_read_time(rtc, &tm);
54 spin_lock_irq(&rtc->irq_lock);
55 if (rtc->stop_uie_polling || err) {
56 rtc->uie_task_active = 0;
57 } else if (rtc->oldsecs != tm.tm_sec) {
58 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
59 rtc->oldsecs = tm.tm_sec;
60 rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
61 rtc->uie_timer_active = 1;
62 rtc->uie_task_active = 0;
63 add_timer(&rtc->uie_timer);
64 } else if (schedule_work(&rtc->uie_task) == 0) {
65 rtc->uie_task_active = 0;
67 spin_unlock_irq(&rtc->irq_lock);
69 rtc_handle_legacy_irq(rtc, num, RTC_UF);
72 static void rtc_uie_timer(struct timer_list *t)
74 struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
77 spin_lock_irqsave(&rtc->irq_lock, flags);
78 rtc->uie_timer_active = 0;
79 rtc->uie_task_active = 1;
80 if ((schedule_work(&rtc->uie_task) == 0))
81 rtc->uie_task_active = 0;
82 spin_unlock_irqrestore(&rtc->irq_lock, flags);
85 static int clear_uie(struct rtc_device *rtc)
87 spin_lock_irq(&rtc->irq_lock);
88 if (rtc->uie_irq_active) {
89 rtc->stop_uie_polling = 1;
90 if (rtc->uie_timer_active) {
91 spin_unlock_irq(&rtc->irq_lock);
92 del_timer_sync(&rtc->uie_timer);
93 spin_lock_irq(&rtc->irq_lock);
94 rtc->uie_timer_active = 0;
96 if (rtc->uie_task_active) {
97 spin_unlock_irq(&rtc->irq_lock);
98 flush_scheduled_work();
99 spin_lock_irq(&rtc->irq_lock);
101 rtc->uie_irq_active = 0;
103 spin_unlock_irq(&rtc->irq_lock);
107 static int set_uie(struct rtc_device *rtc)
112 err = rtc_read_time(rtc, &tm);
115 spin_lock_irq(&rtc->irq_lock);
116 if (!rtc->uie_irq_active) {
117 rtc->uie_irq_active = 1;
118 rtc->stop_uie_polling = 0;
119 rtc->oldsecs = tm.tm_sec;
120 rtc->uie_task_active = 1;
121 if (schedule_work(&rtc->uie_task) == 0)
122 rtc->uie_task_active = 0;
125 spin_unlock_irq(&rtc->irq_lock);
129 int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
134 return clear_uie(rtc);
136 EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
138 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
141 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143 struct rtc_device *rtc = file->private_data;
145 DECLARE_WAITQUEUE(wait, current);
149 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
152 add_wait_queue(&rtc->irq_queue, &wait);
154 __set_current_state(TASK_INTERRUPTIBLE);
156 spin_lock_irq(&rtc->irq_lock);
157 data = rtc->irq_data;
159 spin_unlock_irq(&rtc->irq_lock);
165 if (file->f_flags & O_NONBLOCK) {
169 if (signal_pending(current)) {
175 set_current_state(TASK_RUNNING);
176 remove_wait_queue(&rtc->irq_queue, &wait);
179 if (sizeof(int) != sizeof(long) &&
180 count == sizeof(unsigned int))
181 ret = put_user(data, (unsigned int __user *)buf) ?:
182 sizeof(unsigned int);
184 ret = put_user(data, (unsigned long __user *)buf) ?:
185 sizeof(unsigned long);
190 static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
192 struct rtc_device *rtc = file->private_data;
195 poll_wait(file, &rtc->irq_queue, wait);
197 data = rtc->irq_data;
199 return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
202 static long rtc_dev_ioctl(struct file *file,
203 unsigned int cmd, unsigned long arg)
206 struct rtc_device *rtc = file->private_data;
207 const struct rtc_class_ops *ops = rtc->ops;
209 struct rtc_wkalrm alarm;
210 void __user *uarg = (void __user *)arg;
212 err = mutex_lock_interruptible(&rtc->ops_lock);
216 /* check that the calling task has appropriate permissions
217 * for certain ioctls. doing this check here is useful
218 * to avoid duplicate code in each driver.
223 if (!capable(CAP_SYS_TIME))
228 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
233 if (rtc->irq_freq > rtc->max_user_freq &&
234 !capable(CAP_SYS_RESOURCE))
243 * Drivers *SHOULD NOT* provide ioctl implementations
244 * for these requests. Instead, provide methods to
245 * support the following code, so that the RTC's main
246 * features are accessible without using ioctls.
248 * RTC and alarm times will be in UTC, by preference,
249 * but dual-booting with MS-Windows implies RTCs must
250 * use the local wall clock time.
255 mutex_unlock(&rtc->ops_lock);
257 err = rtc_read_alarm(rtc, &alarm);
261 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
266 mutex_unlock(&rtc->ops_lock);
268 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
273 alarm.time.tm_wday = -1;
274 alarm.time.tm_yday = -1;
275 alarm.time.tm_isdst = -1;
277 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
278 * Rather than expecting every RTC to implement "don't care"
279 * for day/month/year fields, just force the alarm to have
280 * the right values for those fields.
282 * RTC_WKALM_SET should be used instead. Not only does it
283 * eliminate the need for a separate RTC_AIE_ON call, it
284 * doesn't have the "alarm 23:59:59 in the future" race.
286 * NOTE: some legacy code may have used invalid fields as
287 * wildcards, exposing hardware "periodic alarm" capabilities.
288 * Not supported here.
293 err = rtc_read_time(rtc, &tm);
296 now = rtc_tm_to_time64(&tm);
298 alarm.time.tm_mday = tm.tm_mday;
299 alarm.time.tm_mon = tm.tm_mon;
300 alarm.time.tm_year = tm.tm_year;
301 err = rtc_valid_tm(&alarm.time);
304 then = rtc_tm_to_time64(&alarm.time);
306 /* alarm may need to wrap into tomorrow */
308 rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
309 alarm.time.tm_mday = tm.tm_mday;
310 alarm.time.tm_mon = tm.tm_mon;
311 alarm.time.tm_year = tm.tm_year;
315 return rtc_set_alarm(rtc, &alarm);
318 mutex_unlock(&rtc->ops_lock);
320 err = rtc_read_time(rtc, &tm);
324 if (copy_to_user(uarg, &tm, sizeof(tm)))
329 mutex_unlock(&rtc->ops_lock);
331 if (copy_from_user(&tm, uarg, sizeof(tm)))
334 return rtc_set_time(rtc, &tm);
337 err = rtc_irq_set_state(rtc, 1);
341 err = rtc_irq_set_state(rtc, 0);
345 mutex_unlock(&rtc->ops_lock);
346 return rtc_alarm_irq_enable(rtc, 1);
349 mutex_unlock(&rtc->ops_lock);
350 return rtc_alarm_irq_enable(rtc, 0);
353 mutex_unlock(&rtc->ops_lock);
354 return rtc_update_irq_enable(rtc, 1);
357 mutex_unlock(&rtc->ops_lock);
358 return rtc_update_irq_enable(rtc, 0);
361 err = rtc_irq_set_freq(rtc, arg);
365 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
369 mutex_unlock(&rtc->ops_lock);
370 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
373 return rtc_set_alarm(rtc, &alarm);
376 mutex_unlock(&rtc->ops_lock);
377 err = rtc_read_alarm(rtc, &alarm);
381 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
386 /* Finally try the driver's ioctl interface */
388 err = ops->ioctl(rtc->dev.parent, cmd, arg);
389 if (err == -ENOIOCTLCMD)
398 mutex_unlock(&rtc->ops_lock);
402 static int rtc_dev_fasync(int fd, struct file *file, int on)
404 struct rtc_device *rtc = file->private_data;
406 return fasync_helper(fd, file, on, &rtc->async_queue);
409 static int rtc_dev_release(struct inode *inode, struct file *file)
411 struct rtc_device *rtc = file->private_data;
413 /* We shut down the repeating IRQs that userspace enabled,
414 * since nothing is listening to them.
415 * - Update (UIE) ... currently only managed through ioctls
416 * - Periodic (PIE) ... also used through rtc_*() interface calls
418 * Leave the alarm alone; it may be set to trigger a system wakeup
419 * later, or be used by kernel code, and is a one-shot event anyway.
422 /* Keep ioctl until all drivers are converted */
423 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
424 rtc_update_irq_enable(rtc, 0);
425 rtc_irq_set_state(rtc, 0);
427 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
431 static const struct file_operations rtc_dev_fops = {
432 .owner = THIS_MODULE,
434 .read = rtc_dev_read,
435 .poll = rtc_dev_poll,
436 .unlocked_ioctl = rtc_dev_ioctl,
437 .open = rtc_dev_open,
438 .release = rtc_dev_release,
439 .fasync = rtc_dev_fasync,
442 /* insertion/removal hooks */
444 void rtc_dev_prepare(struct rtc_device *rtc)
449 if (rtc->id >= RTC_DEV_MAX) {
450 dev_dbg(&rtc->dev, "too many RTC devices\n");
454 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
456 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
457 INIT_WORK(&rtc->uie_task, rtc_uie_task);
458 timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
461 cdev_init(&rtc->char_dev, &rtc_dev_fops);
462 rtc->char_dev.owner = rtc->owner;
465 void __init rtc_dev_init(void)
469 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
471 pr_err("failed to allocate char dev region\n");
474 void __exit rtc_dev_exit(void)
477 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);