| Nicholas Flintham | 9fafe90 | 2014-03-31 13:51:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * RTC subsystem, dev interface |
| 3 | * |
| 4 | * Copyright (C) 2005 Tower Technologies |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 6 | * |
| 7 | * based on arch/arm/common/rtctime.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/rtc.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include "rtc-core.h" |
| 18 | |
| 19 | static dev_t rtc_devt; |
| 20 | |
| 21 | #define RTC_DEV_MAX 16 |
| 22 | |
| 23 | static int rtc_dev_open(struct inode *inode, struct file *file) |
| 24 | { |
| 25 | int err; |
| 26 | struct rtc_device *rtc = container_of(inode->i_cdev, |
| 27 | struct rtc_device, char_dev); |
| 28 | const struct rtc_class_ops *ops = rtc->ops; |
| 29 | |
| 30 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
| 31 | return -EBUSY; |
| 32 | |
| 33 | file->private_data = rtc; |
| 34 | |
| 35 | err = ops->open ? ops->open(rtc->dev.parent) : 0; |
| 36 | if (err == 0) { |
| 37 | spin_lock_irq(&rtc->irq_lock); |
| 38 | rtc->irq_data = 0; |
| 39 | spin_unlock_irq(&rtc->irq_lock); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 50 | static void rtc_uie_task(struct work_struct *work) |
| 51 | { |
| 52 | struct rtc_device *rtc = |
| 53 | container_of(work, struct rtc_device, uie_task); |
| 54 | struct rtc_time tm; |
| 55 | int num = 0; |
| 56 | int err; |
| 57 | |
| 58 | err = rtc_read_time(rtc, &tm); |
| 59 | |
| 60 | spin_lock_irq(&rtc->irq_lock); |
| 61 | if (rtc->stop_uie_polling || err) { |
| 62 | rtc->uie_task_active = 0; |
| 63 | } else if (rtc->oldsecs != tm.tm_sec) { |
| 64 | num = (tm.tm_sec + 60 - rtc->oldsecs) % 60; |
| 65 | rtc->oldsecs = tm.tm_sec; |
| 66 | rtc->uie_timer.expires = jiffies + HZ - (HZ/10); |
| 67 | rtc->uie_timer_active = 1; |
| 68 | rtc->uie_task_active = 0; |
| 69 | add_timer(&rtc->uie_timer); |
| 70 | } else if (schedule_work(&rtc->uie_task) == 0) { |
| 71 | rtc->uie_task_active = 0; |
| 72 | } |
| 73 | spin_unlock_irq(&rtc->irq_lock); |
| 74 | if (num) |
| 75 | rtc_handle_legacy_irq(rtc, num, RTC_UF); |
| 76 | } |
| 77 | static void rtc_uie_timer(unsigned long data) |
| 78 | { |
| 79 | struct rtc_device *rtc = (struct rtc_device *)data; |
| 80 | unsigned long flags; |
| 81 | |
| 82 | spin_lock_irqsave(&rtc->irq_lock, flags); |
| 83 | rtc->uie_timer_active = 0; |
| 84 | rtc->uie_task_active = 1; |
| 85 | if ((schedule_work(&rtc->uie_task) == 0)) |
| 86 | rtc->uie_task_active = 0; |
| 87 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
| 88 | } |
| 89 | |
| 90 | static int clear_uie(struct rtc_device *rtc) |
| 91 | { |
| 92 | spin_lock_irq(&rtc->irq_lock); |
| 93 | if (rtc->uie_irq_active) { |
| 94 | rtc->stop_uie_polling = 1; |
| 95 | if (rtc->uie_timer_active) { |
| 96 | spin_unlock_irq(&rtc->irq_lock); |
| 97 | del_timer_sync(&rtc->uie_timer); |
| 98 | spin_lock_irq(&rtc->irq_lock); |
| 99 | rtc->uie_timer_active = 0; |
| 100 | } |
| 101 | if (rtc->uie_task_active) { |
| 102 | spin_unlock_irq(&rtc->irq_lock); |
| 103 | flush_scheduled_work(); |
| 104 | spin_lock_irq(&rtc->irq_lock); |
| 105 | } |
| 106 | rtc->uie_irq_active = 0; |
| 107 | } |
| 108 | spin_unlock_irq(&rtc->irq_lock); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int set_uie(struct rtc_device *rtc) |
| 113 | { |
| 114 | struct rtc_time tm; |
| 115 | int err; |
| 116 | |
| 117 | err = rtc_read_time(rtc, &tm); |
| 118 | if (err) |
| 119 | return err; |
| 120 | spin_lock_irq(&rtc->irq_lock); |
| 121 | if (!rtc->uie_irq_active) { |
| 122 | rtc->uie_irq_active = 1; |
| 123 | rtc->stop_uie_polling = 0; |
| 124 | rtc->oldsecs = tm.tm_sec; |
| 125 | rtc->uie_task_active = 1; |
| 126 | if (schedule_work(&rtc->uie_task) == 0) |
| 127 | rtc->uie_task_active = 0; |
| 128 | } |
| 129 | rtc->irq_data = 0; |
| 130 | spin_unlock_irq(&rtc->irq_lock); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled) |
| 135 | { |
| 136 | if (enabled) |
| 137 | return set_uie(rtc); |
| 138 | else |
| 139 | return clear_uie(rtc); |
| 140 | } |
| 141 | EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul); |
| 142 | |
| 143 | #endif |
| 144 | |
| 145 | static ssize_t |
| 146 | rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 147 | { |
| 148 | struct rtc_device *rtc = file->private_data; |
| 149 | |
| 150 | DECLARE_WAITQUEUE(wait, current); |
| 151 | unsigned long data; |
| 152 | ssize_t ret; |
| 153 | |
| 154 | if (count != sizeof(unsigned int) && count < sizeof(unsigned long)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | add_wait_queue(&rtc->irq_queue, &wait); |
| 158 | do { |
| 159 | __set_current_state(TASK_INTERRUPTIBLE); |
| 160 | |
| 161 | spin_lock_irq(&rtc->irq_lock); |
| 162 | data = rtc->irq_data; |
| 163 | rtc->irq_data = 0; |
| 164 | spin_unlock_irq(&rtc->irq_lock); |
| 165 | |
| 166 | if (data != 0) { |
| 167 | ret = 0; |
| 168 | break; |
| 169 | } |
| 170 | if (file->f_flags & O_NONBLOCK) { |
| 171 | ret = -EAGAIN; |
| 172 | break; |
| 173 | } |
| 174 | if (signal_pending(current)) { |
| 175 | ret = -ERESTARTSYS; |
| 176 | break; |
| 177 | } |
| 178 | schedule(); |
| 179 | } while (1); |
| 180 | set_current_state(TASK_RUNNING); |
| 181 | remove_wait_queue(&rtc->irq_queue, &wait); |
| 182 | |
| 183 | if (ret == 0) { |
| 184 | |
| 185 | if (rtc->ops->read_callback) |
| 186 | data = rtc->ops->read_callback(rtc->dev.parent, |
| 187 | data); |
| 188 | |
| 189 | if (sizeof(int) != sizeof(long) && |
| 190 | count == sizeof(unsigned int)) |
| 191 | ret = put_user(data, (unsigned int __user *)buf) ?: |
| 192 | sizeof(unsigned int); |
| 193 | else |
| 194 | ret = put_user(data, (unsigned long __user *)buf) ?: |
| 195 | sizeof(unsigned long); |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static unsigned int rtc_dev_poll(struct file *file, poll_table *wait) |
| 201 | { |
| 202 | struct rtc_device *rtc = file->private_data; |
| 203 | unsigned long data; |
| 204 | |
| 205 | poll_wait(file, &rtc->irq_queue, wait); |
| 206 | |
| 207 | data = rtc->irq_data; |
| 208 | |
| 209 | return (data != 0) ? (POLLIN | POLLRDNORM) : 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 | void __user *uarg = (void __user *) arg; |
| 221 | |
| 222 | err = mutex_lock_interruptible(&rtc->ops_lock); |
| 223 | if (err) |
| 224 | return err; |
| 225 | |
| 226 | switch (cmd) { |
| 227 | case RTC_EPOCH_SET: |
| 228 | case RTC_SET_TIME: |
| 229 | if (!capable(CAP_SYS_TIME)) |
| 230 | err = -EACCES; |
| 231 | break; |
| 232 | |
| 233 | case RTC_IRQP_SET: |
| 234 | if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE)) |
| 235 | err = -EACCES; |
| 236 | break; |
| 237 | |
| 238 | case RTC_PIE_ON: |
| 239 | if (rtc->irq_freq > rtc->max_user_freq && |
| 240 | !capable(CAP_SYS_RESOURCE)) |
| 241 | err = -EACCES; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | if (err) |
| 246 | goto done; |
| 247 | |
| 248 | |
| 249 | switch (cmd) { |
| 250 | case RTC_ALM_READ: |
| 251 | mutex_unlock(&rtc->ops_lock); |
| 252 | |
| 253 | err = rtc_read_alarm(rtc, &alarm); |
| 254 | if (err < 0) |
| 255 | return err; |
| 256 | |
| 257 | if (copy_to_user(uarg, &alarm.time, sizeof(tm))) |
| 258 | err = -EFAULT; |
| 259 | return err; |
| 260 | |
| 261 | case RTC_ALM_SET: |
| 262 | mutex_unlock(&rtc->ops_lock); |
| 263 | |
| 264 | if (copy_from_user(&alarm.time, uarg, sizeof(tm))) |
| 265 | return -EFAULT; |
| 266 | |
| 267 | alarm.enabled = 0; |
| 268 | alarm.pending = 0; |
| 269 | alarm.time.tm_wday = -1; |
| 270 | alarm.time.tm_yday = -1; |
| 271 | alarm.time.tm_isdst = -1; |
| 272 | |
| 273 | { |
| 274 | unsigned long now, then; |
| 275 | |
| 276 | err = rtc_read_time(rtc, &tm); |
| 277 | if (err < 0) |
| 278 | return err; |
| 279 | rtc_tm_to_time(&tm, &now); |
| 280 | |
| 281 | alarm.time.tm_mday = tm.tm_mday; |
| 282 | alarm.time.tm_mon = tm.tm_mon; |
| 283 | alarm.time.tm_year = tm.tm_year; |
| 284 | err = rtc_valid_tm(&alarm.time); |
| 285 | if (err < 0) |
| 286 | return err; |
| 287 | rtc_tm_to_time(&alarm.time, &then); |
| 288 | |
| 289 | |
| 290 | if (then < now) { |
| 291 | rtc_time_to_tm(now + 24 * 60 * 60, &tm); |
| 292 | alarm.time.tm_mday = tm.tm_mday; |
| 293 | alarm.time.tm_mon = tm.tm_mon; |
| 294 | alarm.time.tm_year = tm.tm_year; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return rtc_set_alarm(rtc, &alarm); |
| 299 | |
| 300 | case RTC_RD_TIME: |
| 301 | mutex_unlock(&rtc->ops_lock); |
| 302 | |
| 303 | err = rtc_read_time(rtc, &tm); |
| 304 | if (err < 0) |
| 305 | return err; |
| 306 | |
| 307 | if (copy_to_user(uarg, &tm, sizeof(tm))) |
| 308 | err = -EFAULT; |
| 309 | return err; |
| 310 | |
| 311 | case RTC_SET_TIME: |
| 312 | mutex_unlock(&rtc->ops_lock); |
| 313 | |
| 314 | if (copy_from_user(&tm, uarg, sizeof(tm))) |
| 315 | return -EFAULT; |
| 316 | |
| 317 | return rtc_set_time(rtc, &tm); |
| 318 | |
| 319 | case RTC_PIE_ON: |
| 320 | err = rtc_irq_set_state(rtc, NULL, 1); |
| 321 | break; |
| 322 | |
| 323 | case RTC_PIE_OFF: |
| 324 | err = rtc_irq_set_state(rtc, NULL, 0); |
| 325 | break; |
| 326 | |
| 327 | case RTC_AIE_ON: |
| 328 | mutex_unlock(&rtc->ops_lock); |
| 329 | return rtc_alarm_irq_enable(rtc, 1); |
| 330 | |
| 331 | case RTC_AIE_OFF: |
| 332 | mutex_unlock(&rtc->ops_lock); |
| 333 | return rtc_alarm_irq_enable(rtc, 0); |
| 334 | |
| 335 | case RTC_UIE_ON: |
| 336 | mutex_unlock(&rtc->ops_lock); |
| 337 | return rtc_update_irq_enable(rtc, 1); |
| 338 | |
| 339 | case RTC_UIE_OFF: |
| 340 | mutex_unlock(&rtc->ops_lock); |
| 341 | return rtc_update_irq_enable(rtc, 0); |
| 342 | |
| 343 | case RTC_IRQP_SET: |
| 344 | err = rtc_irq_set_freq(rtc, NULL, arg); |
| 345 | break; |
| 346 | |
| 347 | case RTC_IRQP_READ: |
| 348 | err = put_user(rtc->irq_freq, (unsigned long __user *)uarg); |
| 349 | break; |
| 350 | |
| 351 | #if 0 |
| 352 | case RTC_EPOCH_SET: |
| 353 | #ifndef rtc_epoch |
| 354 | if (arg < 1900) { |
| 355 | err = -EINVAL; |
| 356 | break; |
| 357 | } |
| 358 | rtc_epoch = arg; |
| 359 | err = 0; |
| 360 | #endif |
| 361 | break; |
| 362 | |
| 363 | case RTC_EPOCH_READ: |
| 364 | err = put_user(rtc_epoch, (unsigned long __user *)uarg); |
| 365 | break; |
| 366 | #endif |
| 367 | case RTC_WKALM_SET: |
| 368 | mutex_unlock(&rtc->ops_lock); |
| 369 | if (copy_from_user(&alarm, uarg, sizeof(alarm))) |
| 370 | return -EFAULT; |
| 371 | |
| 372 | return rtc_set_alarm(rtc, &alarm); |
| 373 | |
| 374 | case RTC_WKALM_RD: |
| 375 | mutex_unlock(&rtc->ops_lock); |
| 376 | err = rtc_read_alarm(rtc, &alarm); |
| 377 | if (err < 0) |
| 378 | return err; |
| 379 | |
| 380 | if (copy_to_user(uarg, &alarm, sizeof(alarm))) |
| 381 | err = -EFAULT; |
| 382 | return err; |
| 383 | |
| 384 | default: |
| 385 | |
| 386 | if (ops->ioctl) { |
| 387 | err = ops->ioctl(rtc->dev.parent, cmd, arg); |
| 388 | if (err == -ENOIOCTLCMD) |
| 389 | err = -ENOTTY; |
| 390 | } else |
| 391 | err = -ENOTTY; |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | done: |
| 396 | mutex_unlock(&rtc->ops_lock); |
| 397 | return err; |
| 398 | } |
| 399 | |
| 400 | static int rtc_dev_fasync(int fd, struct file *file, int on) |
| 401 | { |
| 402 | struct rtc_device *rtc = file->private_data; |
| 403 | return fasync_helper(fd, file, on, &rtc->async_queue); |
| 404 | } |
| 405 | |
| 406 | static int rtc_dev_release(struct inode *inode, struct file *file) |
| 407 | { |
| 408 | struct rtc_device *rtc = file->private_data; |
| 409 | |
| 410 | |
| 411 | |
| 412 | rtc_dev_ioctl(file, RTC_UIE_OFF, 0); |
| 413 | rtc_update_irq_enable(rtc, 0); |
| 414 | rtc_irq_set_state(rtc, NULL, 0); |
| 415 | |
| 416 | if (rtc->ops->release) |
| 417 | rtc->ops->release(rtc->dev.parent); |
| 418 | |
| 419 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static const struct file_operations rtc_dev_fops = { |
| 424 | .owner = THIS_MODULE, |
| 425 | .llseek = no_llseek, |
| 426 | .read = rtc_dev_read, |
| 427 | .poll = rtc_dev_poll, |
| 428 | .unlocked_ioctl = rtc_dev_ioctl, |
| 429 | .open = rtc_dev_open, |
| 430 | .release = rtc_dev_release, |
| 431 | .fasync = rtc_dev_fasync, |
| 432 | }; |
| 433 | |
| 434 | |
| 435 | void rtc_dev_prepare(struct rtc_device *rtc) |
| 436 | { |
| 437 | if (!rtc_devt) |
| 438 | return; |
| 439 | |
| 440 | if (rtc->id >= RTC_DEV_MAX) { |
| 441 | pr_debug("%s: too many RTC devices\n", rtc->name); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id); |
| 446 | |
| 447 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
| 448 | INIT_WORK(&rtc->uie_task, rtc_uie_task); |
| 449 | setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc); |
| 450 | #endif |
| 451 | |
| 452 | cdev_init(&rtc->char_dev, &rtc_dev_fops); |
| 453 | rtc->char_dev.owner = rtc->owner; |
| 454 | } |
| 455 | |
| 456 | void rtc_dev_add_device(struct rtc_device *rtc) |
| 457 | { |
| 458 | if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1)) |
| 459 | printk(KERN_WARNING "%s: failed to add char device %d:%d\n", |
| 460 | rtc->name, MAJOR(rtc_devt), rtc->id); |
| 461 | else |
| 462 | pr_debug("%s: dev (%d:%d)\n", rtc->name, |
| 463 | MAJOR(rtc_devt), rtc->id); |
| 464 | } |
| 465 | |
| 466 | void rtc_dev_del_device(struct rtc_device *rtc) |
| 467 | { |
| 468 | if (rtc->dev.devt) |
| 469 | cdev_del(&rtc->char_dev); |
| 470 | } |
| 471 | |
| 472 | void __init rtc_dev_init(void) |
| 473 | { |
| 474 | int err; |
| 475 | |
| 476 | err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); |
| 477 | if (err < 0) |
| 478 | printk(KERN_ERR "%s: failed to allocate char dev region\n", |
| 479 | __FILE__); |
| 480 | } |
| 481 | |
| 482 | void __exit rtc_dev_exit(void) |
| 483 | { |
| 484 | if (rtc_devt) |
| 485 | unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); |
| 486 | } |