^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * RTC subsystem, dev interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005 Tower Technologies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Alessandro Zummo <a.zummo@towertech.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * based on arch/arm/common/rtctime.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "rtc-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static dev_t rtc_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int rtc_dev_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct rtc_device *rtc = container_of(inode->i_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct rtc_device, char_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) file->private_data = rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) rtc->irq_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Routine to poll RTC seconds field for change as often as possible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * after first RTC_UIE use timer to reduce polling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static void rtc_uie_task(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct rtc_device *rtc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) container_of(work, struct rtc_device, uie_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (rtc->stop_uie_polling || err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) rtc->uie_task_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) } else if (rtc->oldsecs != tm.tm_sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) rtc->oldsecs = tm.tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) rtc->uie_timer_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) rtc->uie_task_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) add_timer(&rtc->uie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } else if (schedule_work(&rtc->uie_task) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) rtc->uie_task_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) rtc_handle_legacy_irq(rtc, num, RTC_UF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static void rtc_uie_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) spin_lock_irqsave(&rtc->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) rtc->uie_timer_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rtc->uie_task_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if ((schedule_work(&rtc->uie_task) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) rtc->uie_task_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) spin_unlock_irqrestore(&rtc->irq_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int clear_uie(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (rtc->uie_irq_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rtc->stop_uie_polling = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (rtc->uie_timer_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) del_timer_sync(&rtc->uie_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rtc->uie_timer_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (rtc->uie_task_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) flush_scheduled_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) rtc->uie_irq_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int set_uie(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!rtc->uie_irq_active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) rtc->uie_irq_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rtc->stop_uie_polling = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rtc->oldsecs = tm.tm_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) rtc->uie_task_active = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (schedule_work(&rtc->uie_task) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rtc->uie_task_active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rtc->irq_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return set_uie(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return clear_uie(rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) DECLARE_WAITQUEUE(wait, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned long data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) add_wait_queue(&rtc->irq_queue, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) __set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) spin_lock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) data = rtc->irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rtc->irq_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_unlock_irq(&rtc->irq_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (data != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (file->f_flags & O_NONBLOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (signal_pending(current)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ret = -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) } while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) set_current_state(TASK_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) remove_wait_queue(&rtc->irq_queue, &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (sizeof(int) != sizeof(long) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) count == sizeof(unsigned int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ret = put_user(data, (unsigned int __user *)buf) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) sizeof(unsigned int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = put_user(data, (unsigned long __user *)buf) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) sizeof(unsigned long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) unsigned long data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) poll_wait(file, &rtc->irq_queue, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) data = rtc->irq_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static long rtc_dev_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) const struct rtc_class_ops *ops = rtc->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct rtc_wkalrm alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) void __user *uarg = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) err = mutex_lock_interruptible(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* check that the calling task has appropriate permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * for certain ioctls. doing this check here is useful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * to avoid duplicate code in each driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) case RTC_EPOCH_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) case RTC_SET_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!capable(CAP_SYS_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) case RTC_IRQP_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) case RTC_PIE_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (rtc->irq_freq > rtc->max_user_freq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) !capable(CAP_SYS_RESOURCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * Drivers *SHOULD NOT* provide ioctl implementations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * for these requests. Instead, provide methods to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * support the following code, so that the RTC's main
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * features are accessible without using ioctls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * RTC and alarm times will be in UTC, by preference,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * but dual-booting with MS-Windows implies RTCs must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * use the local wall clock time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case RTC_ALM_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) err = rtc_read_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) case RTC_ALM_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) alarm.enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) alarm.pending = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) alarm.time.tm_wday = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) alarm.time.tm_yday = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) alarm.time.tm_isdst = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* RTC_ALM_SET alarms may be up to 24 hours in the future.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * Rather than expecting every RTC to implement "don't care"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * for day/month/year fields, just force the alarm to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * the right values for those fields.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * RTC_WKALM_SET should be used instead. Not only does it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * eliminate the need for a separate RTC_AIE_ON call, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * doesn't have the "alarm 23:59:59 in the future" race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * NOTE: some legacy code may have used invalid fields as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * wildcards, exposing hardware "periodic alarm" capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * Not supported here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) time64_t now, then;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) now = rtc_tm_to_time64(&tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) alarm.time.tm_mday = tm.tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) alarm.time.tm_mon = tm.tm_mon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) alarm.time.tm_year = tm.tm_year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) err = rtc_valid_tm(&alarm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) then = rtc_tm_to_time64(&alarm.time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* alarm may need to wrap into tomorrow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (then < now) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) alarm.time.tm_mday = tm.tm_mday;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) alarm.time.tm_mon = tm.tm_mon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) alarm.time.tm_year = tm.tm_year;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return rtc_set_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) case RTC_RD_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err = rtc_read_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (copy_to_user(uarg, &tm, sizeof(tm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case RTC_SET_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (copy_from_user(&tm, uarg, sizeof(tm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return rtc_set_time(rtc, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) case RTC_PIE_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) err = rtc_irq_set_state(rtc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) case RTC_PIE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = rtc_irq_set_state(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) case RTC_AIE_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return rtc_alarm_irq_enable(rtc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) case RTC_AIE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return rtc_alarm_irq_enable(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) case RTC_UIE_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return rtc_update_irq_enable(rtc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) case RTC_UIE_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return rtc_update_irq_enable(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) case RTC_IRQP_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) err = rtc_irq_set_freq(rtc, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) case RTC_IRQP_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) case RTC_WKALM_SET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (copy_from_user(&alarm, uarg, sizeof(alarm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return rtc_set_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) case RTC_WKALM_RD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) err = rtc_read_alarm(rtc, &alarm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (copy_to_user(uarg, &alarm, sizeof(alarm)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* Finally try the driver's ioctl interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ops->ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) err = ops->ioctl(rtc->dev.parent, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (err == -ENOIOCTLCMD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) err = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) mutex_unlock(&rtc->ops_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static long rtc_dev_compat_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) void __user *uarg = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) case RTC_IRQP_READ32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return put_user(rtc->irq_freq, (__u32 __user *)uarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) case RTC_IRQP_SET32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /* arg is a plain integer, not pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) case RTC_EPOCH_SET32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /* arg is a plain integer, not pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int rtc_dev_fasync(int fd, struct file *file, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return fasync_helper(fd, file, on, &rtc->async_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static int rtc_dev_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct rtc_device *rtc = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /* We shut down the repeating IRQs that userspace enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * since nothing is listening to them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * - Update (UIE) ... currently only managed through ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * - Periodic (PIE) ... also used through rtc_*() interface calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * Leave the alarm alone; it may be set to trigger a system wakeup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * later, or be used by kernel code, and is a one-shot event anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* Keep ioctl until all drivers are converted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) rtc_update_irq_enable(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) rtc_irq_set_state(rtc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static const struct file_operations rtc_dev_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) .read = rtc_dev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) .poll = rtc_dev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) .unlocked_ioctl = rtc_dev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) .compat_ioctl = rtc_dev_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) .open = rtc_dev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) .release = rtc_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) .fasync = rtc_dev_fasync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* insertion/removal hooks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) void rtc_dev_prepare(struct rtc_device *rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (!rtc_devt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (rtc->id >= RTC_DEV_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dev_dbg(&rtc->dev, "too many RTC devices\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) INIT_WORK(&rtc->uie_task, rtc_uie_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) cdev_init(&rtc->char_dev, &rtc_dev_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) rtc->char_dev.owner = rtc->owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) void __init rtc_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) pr_err("failed to allocate char dev region\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) void __exit rtc_dev_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (rtc_devt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }