Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 2 | /* |
| 3 | * RTC subsystem, dev interface |
| 4 | * |
| 5 | * Copyright (C) 2005 Tower Technologies |
| 6 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 7 | * |
| 8 | * based on arch/arm/common/rtctime.c |
Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 9 | */ |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 10 | |
Jingoo Han | c100a5e | 2013-02-21 16:45:23 -0800 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 13 | #include <linux/compat.h> |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/rtc.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 17 | #include "rtc-core.h" |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 18 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 19 | static dev_t rtc_devt; |
| 20 | |
| 21 | #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */ |
| 22 | |
| 23 | static int rtc_dev_open(struct inode *inode, struct file *file) |
| 24 | { |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 25 | struct rtc_device *rtc = container_of(inode->i_cdev, |
| 26 | struct rtc_device, char_dev); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 27 | |
David Brownell | b1c3c89 | 2008-08-12 15:08:41 -0700 | [diff] [blame] | 28 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
| 29 | return -EBUSY; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 30 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 31 | file->private_data = rtc; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 32 | |
Alexandre Belloni | ea369ea | 2017-08-23 02:33:04 +0200 | [diff] [blame] | 33 | spin_lock_irq(&rtc->irq_lock); |
| 34 | rtc->irq_data = 0; |
| 35 | spin_unlock_irq(&rtc->irq_lock); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 36 | |
Alexandre Belloni | ea369ea | 2017-08-23 02:33:04 +0200 | [diff] [blame] | 37 | return 0; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 38 | } |
| 39 | |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 40 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 41 | /* |
| 42 | * Routine to poll RTC seconds field for change as often as possible, |
| 43 | * after first RTC_UIE use timer to reduce polling |
| 44 | */ |
| 45 | static void rtc_uie_task(struct work_struct *work) |
| 46 | { |
| 47 | struct rtc_device *rtc = |
| 48 | container_of(work, struct rtc_device, uie_task); |
| 49 | struct rtc_time tm; |
| 50 | int num = 0; |
| 51 | int err; |
| 52 | |
| 53 | err = rtc_read_time(rtc, &tm); |
| 54 | |
| 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; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 61 | rtc->uie_timer.expires = jiffies + HZ - (HZ / 10); |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 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; |
| 67 | } |
| 68 | spin_unlock_irq(&rtc->irq_lock); |
| 69 | if (num) |
John Stultz | 456d66e | 2011-02-11 18:15:23 -0800 | [diff] [blame] | 70 | rtc_handle_legacy_irq(rtc, num, RTC_UF); |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 71 | } |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 72 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 73 | static void rtc_uie_timer(struct timer_list *t) |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 74 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 75 | struct rtc_device *rtc = from_timer(rtc, t, uie_timer); |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 76 | unsigned long flags; |
| 77 | |
| 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); |
| 84 | } |
| 85 | |
| 86 | static int clear_uie(struct rtc_device *rtc) |
| 87 | { |
| 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; |
| 96 | } |
| 97 | if (rtc->uie_task_active) { |
| 98 | spin_unlock_irq(&rtc->irq_lock); |
| 99 | flush_scheduled_work(); |
| 100 | spin_lock_irq(&rtc->irq_lock); |
| 101 | } |
| 102 | rtc->uie_irq_active = 0; |
| 103 | } |
| 104 | spin_unlock_irq(&rtc->irq_lock); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int set_uie(struct rtc_device *rtc) |
| 109 | { |
| 110 | struct rtc_time tm; |
| 111 | int err; |
| 112 | |
| 113 | err = rtc_read_time(rtc, &tm); |
| 114 | if (err) |
| 115 | return err; |
| 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; |
| 124 | } |
| 125 | rtc->irq_data = 0; |
| 126 | spin_unlock_irq(&rtc->irq_lock); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled) |
| 131 | { |
| 132 | if (enabled) |
| 133 | return set_uie(rtc); |
| 134 | else |
| 135 | return clear_uie(rtc); |
| 136 | } |
| 137 | EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul); |
| 138 | |
| 139 | #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */ |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 140 | |
| 141 | static ssize_t |
| 142 | rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 143 | { |
Mark Zhan | 88efe13 | 2007-10-16 01:28:17 -0700 | [diff] [blame] | 144 | struct rtc_device *rtc = file->private_data; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 145 | |
| 146 | DECLARE_WAITQUEUE(wait, current); |
| 147 | unsigned long data; |
| 148 | ssize_t ret; |
| 149 | |
Atsushi Nemoto | 3418ff7 | 2006-05-01 12:16:16 -0700 | [diff] [blame] | 150 | if (count != sizeof(unsigned int) && count < sizeof(unsigned long)) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | |
| 153 | add_wait_queue(&rtc->irq_queue, &wait); |
| 154 | do { |
| 155 | __set_current_state(TASK_INTERRUPTIBLE); |
| 156 | |
| 157 | spin_lock_irq(&rtc->irq_lock); |
| 158 | data = rtc->irq_data; |
| 159 | rtc->irq_data = 0; |
| 160 | spin_unlock_irq(&rtc->irq_lock); |
| 161 | |
| 162 | if (data != 0) { |
| 163 | ret = 0; |
| 164 | break; |
| 165 | } |
| 166 | if (file->f_flags & O_NONBLOCK) { |
| 167 | ret = -EAGAIN; |
| 168 | break; |
| 169 | } |
| 170 | if (signal_pending(current)) { |
| 171 | ret = -ERESTARTSYS; |
| 172 | break; |
| 173 | } |
| 174 | schedule(); |
| 175 | } while (1); |
| 176 | set_current_state(TASK_RUNNING); |
| 177 | remove_wait_queue(&rtc->irq_queue, &wait); |
| 178 | |
| 179 | if (ret == 0) { |
Atsushi Nemoto | 3418ff7 | 2006-05-01 12:16:16 -0700 | [diff] [blame] | 180 | if (sizeof(int) != sizeof(long) && |
| 181 | count == sizeof(unsigned int)) |
| 182 | ret = put_user(data, (unsigned int __user *)buf) ?: |
| 183 | sizeof(unsigned int); |
| 184 | else |
| 185 | ret = put_user(data, (unsigned long __user *)buf) ?: |
| 186 | sizeof(unsigned long); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 187 | } |
| 188 | return ret; |
| 189 | } |
| 190 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 191 | static __poll_t rtc_dev_poll(struct file *file, poll_table *wait) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 192 | { |
Mark Zhan | 88efe13 | 2007-10-16 01:28:17 -0700 | [diff] [blame] | 193 | struct rtc_device *rtc = file->private_data; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 194 | unsigned long data; |
| 195 | |
| 196 | poll_wait(file, &rtc->irq_queue, wait); |
| 197 | |
| 198 | data = rtc->irq_data; |
| 199 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 200 | return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 201 | } |
| 202 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 203 | static long rtc_dev_ioctl(struct file *file, |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 204 | unsigned int cmd, unsigned long arg) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 205 | { |
| 206 | int err = 0; |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 207 | struct rtc_device *rtc = file->private_data; |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 208 | const struct rtc_class_ops *ops = rtc->ops; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 209 | struct rtc_time tm; |
| 210 | struct rtc_wkalrm alarm; |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 211 | struct rtc_param param; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 212 | void __user *uarg = (void __user *)arg; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 213 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 214 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 215 | if (err) |
David Brownell | b68bb26 | 2008-07-29 22:33:30 -0700 | [diff] [blame] | 216 | return err; |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 217 | |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 218 | /* check that the calling task has appropriate permissions |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 219 | * for certain ioctls. doing this check here is useful |
| 220 | * to avoid duplicate code in each driver. |
| 221 | */ |
| 222 | switch (cmd) { |
| 223 | case RTC_EPOCH_SET: |
| 224 | case RTC_SET_TIME: |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 225 | case RTC_PARAM_SET: |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 226 | if (!capable(CAP_SYS_TIME)) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 227 | err = -EACCES; |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 228 | break; |
| 229 | |
| 230 | case RTC_IRQP_SET: |
| 231 | if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE)) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 232 | err = -EACCES; |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 233 | break; |
| 234 | |
| 235 | case RTC_PIE_ON: |
Bryan Kadzban | 9d013d3 | 2007-10-16 01:28:23 -0700 | [diff] [blame] | 236 | if (rtc->irq_freq > rtc->max_user_freq && |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 237 | !capable(CAP_SYS_RESOURCE)) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 238 | err = -EACCES; |
Alessandro Zummo | 110d693 | 2006-06-25 05:48:20 -0700 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 242 | if (err) |
| 243 | goto done; |
| 244 | |
John Stultz | ac54cd2 | 2011-02-02 16:55:19 -0800 | [diff] [blame] | 245 | /* |
David Brownell | 8a0bdfd | 2008-02-06 01:38:45 -0800 | [diff] [blame] | 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. |
| 250 | * |
| 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. |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 254 | */ |
| 255 | |
| 256 | switch (cmd) { |
| 257 | case RTC_ALM_READ: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 258 | mutex_unlock(&rtc->ops_lock); |
| 259 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 260 | err = rtc_read_alarm(rtc, &alarm); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 261 | if (err < 0) |
| 262 | return err; |
| 263 | |
| 264 | if (copy_to_user(uarg, &alarm.time, sizeof(tm))) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 265 | err = -EFAULT; |
| 266 | return err; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 267 | |
| 268 | case RTC_ALM_SET: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 269 | mutex_unlock(&rtc->ops_lock); |
| 270 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 271 | if (copy_from_user(&alarm.time, uarg, sizeof(tm))) |
| 272 | return -EFAULT; |
| 273 | |
| 274 | alarm.enabled = 0; |
| 275 | alarm.pending = 0; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 276 | alarm.time.tm_wday = -1; |
| 277 | alarm.time.tm_yday = -1; |
| 278 | alarm.time.tm_isdst = -1; |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 279 | |
| 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. |
| 284 | * |
| 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. |
| 288 | * |
| 289 | * NOTE: some legacy code may have used invalid fields as |
| 290 | * wildcards, exposing hardware "periodic alarm" capabilities. |
| 291 | * Not supported here. |
| 292 | */ |
| 293 | { |
Xunlei Pang | 4ec2364 | 2015-01-22 02:31:52 +0000 | [diff] [blame] | 294 | time64_t now, then; |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 295 | |
| 296 | err = rtc_read_time(rtc, &tm); |
| 297 | if (err < 0) |
| 298 | return err; |
Xunlei Pang | 4ec2364 | 2015-01-22 02:31:52 +0000 | [diff] [blame] | 299 | now = rtc_tm_to_time64(&tm); |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 300 | |
| 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); |
| 305 | if (err < 0) |
| 306 | return err; |
Xunlei Pang | 4ec2364 | 2015-01-22 02:31:52 +0000 | [diff] [blame] | 307 | then = rtc_tm_to_time64(&alarm.time); |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 308 | |
| 309 | /* alarm may need to wrap into tomorrow */ |
| 310 | if (then < now) { |
Xunlei Pang | 4ec2364 | 2015-01-22 02:31:52 +0000 | [diff] [blame] | 311 | rtc_time64_to_tm(now + 24 * 60 * 60, &tm); |
David Brownell | f8245c2 | 2007-05-08 00:34:07 -0700 | [diff] [blame] | 312 | alarm.time.tm_mday = tm.tm_mday; |
| 313 | alarm.time.tm_mon = tm.tm_mon; |
| 314 | alarm.time.tm_year = tm.tm_year; |
| 315 | } |
| 316 | } |
| 317 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 318 | return rtc_set_alarm(rtc, &alarm); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 319 | |
| 320 | case RTC_RD_TIME: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 321 | mutex_unlock(&rtc->ops_lock); |
| 322 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 323 | err = rtc_read_time(rtc, &tm); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 324 | if (err < 0) |
| 325 | return err; |
| 326 | |
| 327 | if (copy_to_user(uarg, &tm, sizeof(tm))) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 328 | err = -EFAULT; |
| 329 | return err; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 330 | |
| 331 | case RTC_SET_TIME: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 332 | mutex_unlock(&rtc->ops_lock); |
| 333 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 334 | if (copy_from_user(&tm, uarg, sizeof(tm))) |
| 335 | return -EFAULT; |
| 336 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 337 | return rtc_set_time(rtc, &tm); |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 338 | |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 339 | case RTC_PIE_ON: |
Alexandre Belloni | 8719d3c | 2018-07-25 15:07:09 +0200 | [diff] [blame] | 340 | err = rtc_irq_set_state(rtc, 1); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case RTC_PIE_OFF: |
Alexandre Belloni | 8719d3c | 2018-07-25 15:07:09 +0200 | [diff] [blame] | 344 | err = rtc_irq_set_state(rtc, 0); |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 345 | break; |
| 346 | |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 347 | case RTC_AIE_ON: |
| 348 | mutex_unlock(&rtc->ops_lock); |
| 349 | return rtc_alarm_irq_enable(rtc, 1); |
| 350 | |
| 351 | case RTC_AIE_OFF: |
| 352 | mutex_unlock(&rtc->ops_lock); |
| 353 | return rtc_alarm_irq_enable(rtc, 0); |
| 354 | |
| 355 | case RTC_UIE_ON: |
| 356 | mutex_unlock(&rtc->ops_lock); |
| 357 | return rtc_update_irq_enable(rtc, 1); |
| 358 | |
| 359 | case RTC_UIE_OFF: |
| 360 | mutex_unlock(&rtc->ops_lock); |
| 361 | return rtc_update_irq_enable(rtc, 0); |
| 362 | |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 363 | case RTC_IRQP_SET: |
Alexandre Belloni | 8719d3c | 2018-07-25 15:07:09 +0200 | [diff] [blame] | 364 | err = rtc_irq_set_freq(rtc, arg); |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 365 | break; |
Alessandro Zummo | d691eb9 | 2007-10-16 01:28:15 -0700 | [diff] [blame] | 366 | case RTC_IRQP_READ: |
| 367 | err = put_user(rtc->irq_freq, (unsigned long __user *)uarg); |
David Brownell | 2601a46 | 2006-11-25 11:09:27 -0800 | [diff] [blame] | 368 | break; |
| 369 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 370 | case RTC_WKALM_SET: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 371 | mutex_unlock(&rtc->ops_lock); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 372 | if (copy_from_user(&alarm, uarg, sizeof(alarm))) |
| 373 | return -EFAULT; |
| 374 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 375 | return rtc_set_alarm(rtc, &alarm); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 376 | |
| 377 | case RTC_WKALM_RD: |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 378 | mutex_unlock(&rtc->ops_lock); |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 379 | err = rtc_read_alarm(rtc, &alarm); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 380 | if (err < 0) |
| 381 | return err; |
| 382 | |
| 383 | if (copy_to_user(uarg, &alarm, sizeof(alarm))) |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 384 | err = -EFAULT; |
| 385 | return err; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 386 | |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 387 | case RTC_PARAM_GET: |
| 388 | if (copy_from_user(¶m, uarg, sizeof(param))) { |
| 389 | mutex_unlock(&rtc->ops_lock); |
| 390 | return -EFAULT; |
| 391 | } |
| 392 | |
| 393 | switch(param.param) { |
| 394 | long offset; |
| 395 | case RTC_PARAM_FEATURES: |
| 396 | if (param.index != 0) |
| 397 | err = -EINVAL; |
| 398 | param.uvalue = rtc->features[0]; |
| 399 | break; |
| 400 | |
Alexandre Belloni | a6d8c6e | 2021-10-18 17:19:30 +0200 | [diff] [blame] | 401 | case RTC_PARAM_CORRECTION: |
| 402 | mutex_unlock(&rtc->ops_lock); |
| 403 | if (param.index != 0) |
| 404 | return -EINVAL; |
| 405 | err = rtc_read_offset(rtc, &offset); |
| 406 | mutex_lock(&rtc->ops_lock); |
| 407 | if (err == 0) |
| 408 | param.svalue = offset; |
| 409 | break; |
| 410 | |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 411 | default: |
Alexandre Belloni | 0d20e9f | 2021-10-18 17:19:31 +0200 | [diff] [blame] | 412 | if (rtc->ops->param_get) |
| 413 | err = rtc->ops->param_get(rtc->dev.parent, ¶m); |
| 414 | else |
| 415 | err = -EINVAL; |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (!err) |
| 419 | if (copy_to_user(uarg, ¶m, sizeof(param))) |
| 420 | err = -EFAULT; |
| 421 | |
| 422 | break; |
| 423 | |
| 424 | case RTC_PARAM_SET: |
| 425 | if (copy_from_user(¶m, uarg, sizeof(param))) { |
| 426 | mutex_unlock(&rtc->ops_lock); |
| 427 | return -EFAULT; |
| 428 | } |
| 429 | |
| 430 | switch(param.param) { |
| 431 | case RTC_PARAM_FEATURES: |
Alexandre Belloni | a6d8c6e | 2021-10-18 17:19:30 +0200 | [diff] [blame] | 432 | err = -EINVAL; |
| 433 | break; |
| 434 | |
| 435 | case RTC_PARAM_CORRECTION: |
| 436 | mutex_unlock(&rtc->ops_lock); |
| 437 | if (param.index != 0) |
| 438 | return -EINVAL; |
| 439 | return rtc_set_offset(rtc, param.svalue); |
| 440 | |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 441 | default: |
Alexandre Belloni | 0d20e9f | 2021-10-18 17:19:31 +0200 | [diff] [blame] | 442 | if (rtc->ops->param_set) |
| 443 | err = rtc->ops->param_set(rtc->dev.parent, ¶m); |
| 444 | else |
| 445 | err = -EINVAL; |
Alexandre Belloni | 6a8af1b | 2021-10-18 17:19:28 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | break; |
| 449 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 450 | default: |
John Stultz | ac54cd2 | 2011-02-02 16:55:19 -0800 | [diff] [blame] | 451 | /* Finally try the driver's ioctl interface */ |
| 452 | if (ops->ioctl) { |
| 453 | err = ops->ioctl(rtc->dev.parent, cmd, arg); |
| 454 | if (err == -ENOIOCTLCMD) |
| 455 | err = -ENOTTY; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 456 | } else { |
John Stultz | e17fd4b | 2011-05-31 23:26:11 -0700 | [diff] [blame] | 457 | err = -ENOTTY; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 458 | } |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 462 | done: |
| 463 | mutex_unlock(&rtc->ops_lock); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 464 | return err; |
| 465 | } |
| 466 | |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 467 | #ifdef CONFIG_COMPAT |
| 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) |
| 471 | |
| 472 | static long rtc_dev_compat_ioctl(struct file *file, |
| 473 | unsigned int cmd, unsigned long arg) |
| 474 | { |
| 475 | struct rtc_device *rtc = file->private_data; |
| 476 | void __user *uarg = compat_ptr(arg); |
| 477 | |
| 478 | switch (cmd) { |
| 479 | case RTC_IRQP_READ32: |
| 480 | return put_user(rtc->irq_freq, (__u32 __user *)uarg); |
| 481 | |
| 482 | case RTC_IRQP_SET32: |
| 483 | /* arg is a plain integer, not pointer */ |
| 484 | return rtc_dev_ioctl(file, RTC_IRQP_SET, arg); |
| 485 | |
| 486 | case RTC_EPOCH_SET32: |
| 487 | /* arg is a plain integer, not pointer */ |
| 488 | return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg); |
| 489 | } |
| 490 | |
| 491 | return rtc_dev_ioctl(file, cmd, (unsigned long)uarg); |
| 492 | } |
| 493 | #endif |
| 494 | |
Marcin Slusarz | 2e4a75c | 2008-10-03 15:23:36 -0700 | [diff] [blame] | 495 | static int rtc_dev_fasync(int fd, struct file *file, int on) |
| 496 | { |
| 497 | struct rtc_device *rtc = file->private_data; |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 498 | |
Marcin Slusarz | 2e4a75c | 2008-10-03 15:23:36 -0700 | [diff] [blame] | 499 | return fasync_helper(fd, file, on, &rtc->async_queue); |
| 500 | } |
| 501 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 502 | static int rtc_dev_release(struct inode *inode, struct file *file) |
| 503 | { |
Mark Zhan | 88efe13 | 2007-10-16 01:28:17 -0700 | [diff] [blame] | 504 | struct rtc_device *rtc = file->private_data; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 505 | |
David Brownell | 743e6a5 | 2008-10-15 22:03:04 -0700 | [diff] [blame] | 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 |
| 510 | * |
| 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. |
| 513 | */ |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 514 | |
| 515 | /* Keep ioctl until all drivers are converted */ |
David Brownell | 743e6a5 | 2008-10-15 22:03:04 -0700 | [diff] [blame] | 516 | rtc_dev_ioctl(file, RTC_UIE_OFF, 0); |
Alessandro Zummo | 099e657 | 2009-01-04 12:00:54 -0800 | [diff] [blame] | 517 | rtc_update_irq_enable(rtc, 0); |
Alexandre Belloni | 8719d3c | 2018-07-25 15:07:09 +0200 | [diff] [blame] | 518 | rtc_irq_set_state(rtc, 0); |
Tomas Janousek | 5cdc98b | 2008-07-29 22:33:51 -0700 | [diff] [blame] | 519 | |
Jiri Kosina | 372a302 | 2007-12-04 23:45:05 -0800 | [diff] [blame] | 520 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 524 | static const struct file_operations rtc_dev_fops = { |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 525 | .owner = THIS_MODULE, |
| 526 | .llseek = no_llseek, |
| 527 | .read = rtc_dev_read, |
| 528 | .poll = rtc_dev_poll, |
David Brownell | 5ad31a57 | 2008-07-23 21:30:33 -0700 | [diff] [blame] | 529 | .unlocked_ioctl = rtc_dev_ioctl, |
Arnd Bergmann | 076ff65 | 2018-08-24 00:11:19 +0200 | [diff] [blame] | 530 | #ifdef CONFIG_COMPAT |
| 531 | .compat_ioctl = rtc_dev_compat_ioctl, |
| 532 | #endif |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 533 | .open = rtc_dev_open, |
| 534 | .release = rtc_dev_release, |
| 535 | .fasync = rtc_dev_fasync, |
| 536 | }; |
| 537 | |
| 538 | /* insertion/removal hooks */ |
| 539 | |
David Brownell | cb3a58d | 2007-05-08 00:33:46 -0700 | [diff] [blame] | 540 | void rtc_dev_prepare(struct rtc_device *rtc) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 541 | { |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 542 | if (!rtc_devt) |
| 543 | return; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 544 | |
| 545 | if (rtc->id >= RTC_DEV_MAX) { |
Alexandre Belloni | 9f86065 | 2017-06-02 14:01:37 +0200 | [diff] [blame] | 546 | dev_dbg(&rtc->dev, "too many RTC devices\n"); |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 547 | return; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 548 | } |
| 549 | |
David Brownell | cd96620 | 2007-05-08 00:33:40 -0700 | [diff] [blame] | 550 | rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id); |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 551 | |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 552 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 553 | INIT_WORK(&rtc->uie_task, rtc_uie_task); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 554 | timer_setup(&rtc->uie_timer, rtc_uie_timer, 0); |
John Stultz | 6e57b1d | 2011-02-11 17:45:40 -0800 | [diff] [blame] | 555 | #endif |
| 556 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 557 | cdev_init(&rtc->char_dev, &rtc_dev_fops); |
| 558 | rtc->char_dev.owner = rtc->owner; |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 559 | } |
| 560 | |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 561 | void __init rtc_dev_init(void) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 562 | { |
| 563 | int err; |
| 564 | |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 565 | err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 566 | if (err < 0) |
Jingoo Han | c100a5e | 2013-02-21 16:45:23 -0800 | [diff] [blame] | 567 | pr_err("failed to allocate char dev region\n"); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 568 | } |
| 569 | |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 570 | void __exit rtc_dev_exit(void) |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 571 | { |
David Brownell | 5726fb2 | 2007-05-08 00:33:27 -0700 | [diff] [blame] | 572 | if (rtc_devt) |
| 573 | unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); |
Alessandro Zummo | e824290 | 2006-03-27 01:16:41 -0800 | [diff] [blame] | 574 | } |