1 // SPDX-License-Identifier: GPL-2.0 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 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/compat.h> 14 #include <linux/module.h> 15 #include <linux/rtc.h> 16 #include <linux/sched/signal.h> 17 #include "rtc-core.h" 18 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 { 25 struct rtc_device *rtc = container_of(inode->i_cdev, 26 struct rtc_device, char_dev); 27 28 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) 29 return -EBUSY; 30 31 file->private_data = rtc; 32 33 spin_lock_irq(&rtc->irq_lock); 34 rtc->irq_data = 0; 35 spin_unlock_irq(&rtc->irq_lock); 36 37 return 0; 38 } 39 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; 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; 67 } 68 spin_unlock_irq(&rtc->irq_lock); 69 if (num) 70 rtc_handle_legacy_irq(rtc, num, RTC_UF); 71 } 72 73 static void rtc_uie_timer(struct timer_list *t) 74 { 75 struct rtc_device *rtc = timer_container_of(rtc, t, uie_timer); 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 timer_delete_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_work(&rtc->uie_task); 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 */ 140 141 static ssize_t 142 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 143 { 144 struct rtc_device *rtc = file->private_data; 145 146 DECLARE_WAITQUEUE(wait, current); 147 unsigned long data; 148 ssize_t ret; 149 150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long)) 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) { 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); 187 } 188 return ret; 189 } 190 191 static __poll_t rtc_dev_poll(struct file *file, poll_table *wait) 192 { 193 struct rtc_device *rtc = file->private_data; 194 unsigned long data; 195 196 poll_wait(file, &rtc->irq_queue, wait); 197 198 /* 199 * This read can race with the write in rtc_handle_legacy_irq(). 200 * 201 * - If this check misses a zero to non-zero transition the next check 202 * will pick it up (rtc_handle_legacy_irq() wakes up rtc->irq_queue). 203 * - Non-zero to non-zero transition misses do not change return value. 204 * - And a non-zero to zero transition is unlikely to be missed, since 205 * it occurs on rtc_dev_read(), during which polling is not expected. 206 */ 207 data = data_race(rtc->irq_data); 208 209 return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0; 210 } 211 212 static long rtc_dev_ioctl(struct file *file, 213 unsigned int cmd, unsigned long arg) 214 { 215 int err = 0; 216 struct rtc_device *rtc = file->private_data; 217 const struct rtc_class_ops *ops = rtc->ops; 218 struct rtc_time tm; 219 struct rtc_wkalrm alarm; 220 struct rtc_param param; 221 void __user *uarg = (void __user *)arg; 222 223 err = mutex_lock_interruptible(&rtc->ops_lock); 224 if (err) 225 return err; 226 227 /* check that the calling task has appropriate permissions 228 * for certain ioctls. doing this check here is useful 229 * to avoid duplicate code in each driver. 230 */ 231 switch (cmd) { 232 case RTC_EPOCH_SET: 233 case RTC_SET_TIME: 234 case RTC_PARAM_SET: 235 if (!capable(CAP_SYS_TIME)) 236 err = -EACCES; 237 break; 238 239 case RTC_IRQP_SET: 240 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE)) 241 err = -EACCES; 242 break; 243 244 case RTC_PIE_ON: 245 if (rtc->irq_freq > rtc->max_user_freq && 246 !capable(CAP_SYS_RESOURCE)) 247 err = -EACCES; 248 break; 249 } 250 251 if (err) 252 goto done; 253 254 /* 255 * Drivers *SHOULD NOT* provide ioctl implementations 256 * for these requests. Instead, provide methods to 257 * support the following code, so that the RTC's main 258 * features are accessible without using ioctls. 259 * 260 * RTC and alarm times will be in UTC, by preference, 261 * but dual-booting with MS-Windows implies RTCs must 262 * use the local wall clock time. 263 */ 264 265 switch (cmd) { 266 case RTC_ALM_READ: 267 mutex_unlock(&rtc->ops_lock); 268 269 err = rtc_read_alarm(rtc, &alarm); 270 if (err < 0) 271 return err; 272 273 if (copy_to_user(uarg, &alarm.time, sizeof(tm))) 274 err = -EFAULT; 275 return err; 276 277 case RTC_ALM_SET: 278 mutex_unlock(&rtc->ops_lock); 279 280 if (copy_from_user(&alarm.time, uarg, sizeof(tm))) 281 return -EFAULT; 282 283 alarm.enabled = 0; 284 alarm.pending = 0; 285 alarm.time.tm_wday = -1; 286 alarm.time.tm_yday = -1; 287 alarm.time.tm_isdst = -1; 288 289 /* RTC_ALM_SET alarms may be up to 24 hours in the future. 290 * Rather than expecting every RTC to implement "don't care" 291 * for day/month/year fields, just force the alarm to have 292 * the right values for those fields. 293 * 294 * RTC_WKALM_SET should be used instead. Not only does it 295 * eliminate the need for a separate RTC_AIE_ON call, it 296 * doesn't have the "alarm 23:59:59 in the future" race. 297 * 298 * NOTE: some legacy code may have used invalid fields as 299 * wildcards, exposing hardware "periodic alarm" capabilities. 300 * Not supported here. 301 */ 302 { 303 time64_t now, then; 304 305 err = rtc_read_time(rtc, &tm); 306 if (err < 0) 307 return err; 308 now = rtc_tm_to_time64(&tm); 309 310 alarm.time.tm_mday = tm.tm_mday; 311 alarm.time.tm_mon = tm.tm_mon; 312 alarm.time.tm_year = tm.tm_year; 313 err = rtc_valid_tm(&alarm.time); 314 if (err < 0) 315 return err; 316 then = rtc_tm_to_time64(&alarm.time); 317 318 /* alarm may need to wrap into tomorrow */ 319 if (then < now) { 320 rtc_time64_to_tm(now + 24 * 60 * 60, &tm); 321 alarm.time.tm_mday = tm.tm_mday; 322 alarm.time.tm_mon = tm.tm_mon; 323 alarm.time.tm_year = tm.tm_year; 324 } 325 } 326 327 return rtc_set_alarm(rtc, &alarm); 328 329 case RTC_RD_TIME: 330 mutex_unlock(&rtc->ops_lock); 331 332 err = rtc_read_time(rtc, &tm); 333 if (err < 0) 334 return err; 335 336 if (copy_to_user(uarg, &tm, sizeof(tm))) 337 err = -EFAULT; 338 return err; 339 340 case RTC_SET_TIME: 341 mutex_unlock(&rtc->ops_lock); 342 343 if (copy_from_user(&tm, uarg, sizeof(tm))) 344 return -EFAULT; 345 346 return rtc_set_time(rtc, &tm); 347 348 case RTC_PIE_ON: 349 err = rtc_irq_set_state(rtc, 1); 350 break; 351 352 case RTC_PIE_OFF: 353 err = rtc_irq_set_state(rtc, 0); 354 break; 355 356 case RTC_AIE_ON: 357 mutex_unlock(&rtc->ops_lock); 358 return rtc_alarm_irq_enable(rtc, 1); 359 360 case RTC_AIE_OFF: 361 mutex_unlock(&rtc->ops_lock); 362 return rtc_alarm_irq_enable(rtc, 0); 363 364 case RTC_UIE_ON: 365 mutex_unlock(&rtc->ops_lock); 366 return rtc_update_irq_enable(rtc, 1); 367 368 case RTC_UIE_OFF: 369 mutex_unlock(&rtc->ops_lock); 370 return rtc_update_irq_enable(rtc, 0); 371 372 case RTC_IRQP_SET: 373 err = rtc_irq_set_freq(rtc, arg); 374 break; 375 case RTC_IRQP_READ: 376 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg); 377 break; 378 379 case RTC_WKALM_SET: 380 mutex_unlock(&rtc->ops_lock); 381 if (copy_from_user(&alarm, uarg, sizeof(alarm))) 382 return -EFAULT; 383 384 return rtc_set_alarm(rtc, &alarm); 385 386 case RTC_WKALM_RD: 387 mutex_unlock(&rtc->ops_lock); 388 err = rtc_read_alarm(rtc, &alarm); 389 if (err < 0) 390 return err; 391 392 if (copy_to_user(uarg, &alarm, sizeof(alarm))) 393 err = -EFAULT; 394 return err; 395 396 case RTC_PARAM_GET: 397 if (copy_from_user(¶m, uarg, sizeof(param))) { 398 mutex_unlock(&rtc->ops_lock); 399 return -EFAULT; 400 } 401 402 switch(param.param) { 403 case RTC_PARAM_FEATURES: 404 if (param.index != 0) 405 err = -EINVAL; 406 param.uvalue = rtc->features[0]; 407 break; 408 409 case RTC_PARAM_CORRECTION: { 410 long offset; 411 mutex_unlock(&rtc->ops_lock); 412 if (param.index != 0) 413 return -EINVAL; 414 err = rtc_read_offset(rtc, &offset); 415 mutex_lock(&rtc->ops_lock); 416 if (err == 0) 417 param.svalue = offset; 418 break; 419 } 420 default: 421 if (rtc->ops->param_get) 422 err = rtc->ops->param_get(rtc->dev.parent, ¶m); 423 else 424 err = -EINVAL; 425 } 426 427 if (!err) 428 if (copy_to_user(uarg, ¶m, sizeof(param))) 429 err = -EFAULT; 430 431 break; 432 433 case RTC_PARAM_SET: 434 if (copy_from_user(¶m, uarg, sizeof(param))) { 435 mutex_unlock(&rtc->ops_lock); 436 return -EFAULT; 437 } 438 439 switch(param.param) { 440 case RTC_PARAM_FEATURES: 441 err = -EINVAL; 442 break; 443 444 case RTC_PARAM_CORRECTION: 445 mutex_unlock(&rtc->ops_lock); 446 if (param.index != 0) 447 return -EINVAL; 448 return rtc_set_offset(rtc, param.svalue); 449 450 default: 451 if (rtc->ops->param_set) 452 err = rtc->ops->param_set(rtc->dev.parent, ¶m); 453 else 454 err = -EINVAL; 455 } 456 457 break; 458 459 default: 460 /* Finally try the driver's ioctl interface */ 461 if (ops->ioctl) { 462 err = ops->ioctl(rtc->dev.parent, cmd, arg); 463 if (err == -ENOIOCTLCMD) 464 err = -ENOTTY; 465 } else { 466 err = -ENOTTY; 467 } 468 break; 469 } 470 471 done: 472 mutex_unlock(&rtc->ops_lock); 473 return err; 474 } 475 476 #ifdef CONFIG_COMPAT 477 #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32) 478 #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32) 479 #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32) 480 481 static long rtc_dev_compat_ioctl(struct file *file, 482 unsigned int cmd, unsigned long arg) 483 { 484 struct rtc_device *rtc = file->private_data; 485 void __user *uarg = compat_ptr(arg); 486 487 switch (cmd) { 488 case RTC_IRQP_READ32: 489 return put_user(rtc->irq_freq, (__u32 __user *)uarg); 490 491 case RTC_IRQP_SET32: 492 /* arg is a plain integer, not pointer */ 493 return rtc_dev_ioctl(file, RTC_IRQP_SET, arg); 494 495 case RTC_EPOCH_SET32: 496 /* arg is a plain integer, not pointer */ 497 return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg); 498 } 499 500 return rtc_dev_ioctl(file, cmd, (unsigned long)uarg); 501 } 502 #endif 503 504 static int rtc_dev_fasync(int fd, struct file *file, int on) 505 { 506 struct rtc_device *rtc = file->private_data; 507 508 return fasync_helper(fd, file, on, &rtc->async_queue); 509 } 510 511 static int rtc_dev_release(struct inode *inode, struct file *file) 512 { 513 struct rtc_device *rtc = file->private_data; 514 515 /* We shut down the repeating IRQs that userspace enabled, 516 * since nothing is listening to them. 517 * - Update (UIE) ... currently only managed through ioctls 518 * - Periodic (PIE) ... also used through rtc_*() interface calls 519 * 520 * Leave the alarm alone; it may be set to trigger a system wakeup 521 * later, or be used by kernel code, and is a one-shot event anyway. 522 */ 523 524 /* Keep ioctl until all drivers are converted */ 525 rtc_dev_ioctl(file, RTC_UIE_OFF, 0); 526 rtc_update_irq_enable(rtc, 0); 527 rtc_irq_set_state(rtc, 0); 528 529 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); 530 return 0; 531 } 532 533 static const struct file_operations rtc_dev_fops = { 534 .owner = THIS_MODULE, 535 .read = rtc_dev_read, 536 .poll = rtc_dev_poll, 537 .unlocked_ioctl = rtc_dev_ioctl, 538 #ifdef CONFIG_COMPAT 539 .compat_ioctl = rtc_dev_compat_ioctl, 540 #endif 541 .open = rtc_dev_open, 542 .release = rtc_dev_release, 543 .fasync = rtc_dev_fasync, 544 }; 545 546 /* insertion/removal hooks */ 547 548 void rtc_dev_prepare(struct rtc_device *rtc) 549 { 550 if (!rtc_devt) 551 return; 552 553 if (rtc->id >= RTC_DEV_MAX) { 554 dev_dbg(&rtc->dev, "too many RTC devices\n"); 555 return; 556 } 557 558 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id); 559 560 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 561 INIT_WORK(&rtc->uie_task, rtc_uie_task); 562 timer_setup(&rtc->uie_timer, rtc_uie_timer, 0); 563 #endif 564 565 cdev_init(&rtc->char_dev, &rtc_dev_fops); 566 rtc->char_dev.owner = rtc->owner; 567 } 568 569 void __init rtc_dev_init(void) 570 { 571 int err; 572 573 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); 574 if (err < 0) 575 pr_err("failed to allocate char dev region\n"); 576 } 577