0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/compat.h>
0014 #include <linux/module.h>
0015 #include <linux/rtc.h>
0016 #include <linux/sched/signal.h>
0017 #include "rtc-core.h"
0018
0019 static dev_t rtc_devt;
0020
0021 #define RTC_DEV_MAX 16
0022
0023 static int rtc_dev_open(struct inode *inode, struct file *file)
0024 {
0025 struct rtc_device *rtc = container_of(inode->i_cdev,
0026 struct rtc_device, char_dev);
0027
0028 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
0029 return -EBUSY;
0030
0031 file->private_data = rtc;
0032
0033 spin_lock_irq(&rtc->irq_lock);
0034 rtc->irq_data = 0;
0035 spin_unlock_irq(&rtc->irq_lock);
0036
0037 return 0;
0038 }
0039
0040 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0041
0042
0043
0044
0045 static void rtc_uie_task(struct work_struct *work)
0046 {
0047 struct rtc_device *rtc =
0048 container_of(work, struct rtc_device, uie_task);
0049 struct rtc_time tm;
0050 int num = 0;
0051 int err;
0052
0053 err = rtc_read_time(rtc, &tm);
0054
0055 spin_lock_irq(&rtc->irq_lock);
0056 if (rtc->stop_uie_polling || err) {
0057 rtc->uie_task_active = 0;
0058 } else if (rtc->oldsecs != tm.tm_sec) {
0059 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
0060 rtc->oldsecs = tm.tm_sec;
0061 rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
0062 rtc->uie_timer_active = 1;
0063 rtc->uie_task_active = 0;
0064 add_timer(&rtc->uie_timer);
0065 } else if (schedule_work(&rtc->uie_task) == 0) {
0066 rtc->uie_task_active = 0;
0067 }
0068 spin_unlock_irq(&rtc->irq_lock);
0069 if (num)
0070 rtc_handle_legacy_irq(rtc, num, RTC_UF);
0071 }
0072
0073 static void rtc_uie_timer(struct timer_list *t)
0074 {
0075 struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
0076 unsigned long flags;
0077
0078 spin_lock_irqsave(&rtc->irq_lock, flags);
0079 rtc->uie_timer_active = 0;
0080 rtc->uie_task_active = 1;
0081 if ((schedule_work(&rtc->uie_task) == 0))
0082 rtc->uie_task_active = 0;
0083 spin_unlock_irqrestore(&rtc->irq_lock, flags);
0084 }
0085
0086 static int clear_uie(struct rtc_device *rtc)
0087 {
0088 spin_lock_irq(&rtc->irq_lock);
0089 if (rtc->uie_irq_active) {
0090 rtc->stop_uie_polling = 1;
0091 if (rtc->uie_timer_active) {
0092 spin_unlock_irq(&rtc->irq_lock);
0093 del_timer_sync(&rtc->uie_timer);
0094 spin_lock_irq(&rtc->irq_lock);
0095 rtc->uie_timer_active = 0;
0096 }
0097 if (rtc->uie_task_active) {
0098 spin_unlock_irq(&rtc->irq_lock);
0099 flush_work(&rtc->uie_task);
0100 spin_lock_irq(&rtc->irq_lock);
0101 }
0102 rtc->uie_irq_active = 0;
0103 }
0104 spin_unlock_irq(&rtc->irq_lock);
0105 return 0;
0106 }
0107
0108 static int set_uie(struct rtc_device *rtc)
0109 {
0110 struct rtc_time tm;
0111 int err;
0112
0113 err = rtc_read_time(rtc, &tm);
0114 if (err)
0115 return err;
0116 spin_lock_irq(&rtc->irq_lock);
0117 if (!rtc->uie_irq_active) {
0118 rtc->uie_irq_active = 1;
0119 rtc->stop_uie_polling = 0;
0120 rtc->oldsecs = tm.tm_sec;
0121 rtc->uie_task_active = 1;
0122 if (schedule_work(&rtc->uie_task) == 0)
0123 rtc->uie_task_active = 0;
0124 }
0125 rtc->irq_data = 0;
0126 spin_unlock_irq(&rtc->irq_lock);
0127 return 0;
0128 }
0129
0130 int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
0131 {
0132 if (enabled)
0133 return set_uie(rtc);
0134 else
0135 return clear_uie(rtc);
0136 }
0137 EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
0138
0139 #endif
0140
0141 static ssize_t
0142 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
0143 {
0144 struct rtc_device *rtc = file->private_data;
0145
0146 DECLARE_WAITQUEUE(wait, current);
0147 unsigned long data;
0148 ssize_t ret;
0149
0150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
0151 return -EINVAL;
0152
0153 add_wait_queue(&rtc->irq_queue, &wait);
0154 do {
0155 __set_current_state(TASK_INTERRUPTIBLE);
0156
0157 spin_lock_irq(&rtc->irq_lock);
0158 data = rtc->irq_data;
0159 rtc->irq_data = 0;
0160 spin_unlock_irq(&rtc->irq_lock);
0161
0162 if (data != 0) {
0163 ret = 0;
0164 break;
0165 }
0166 if (file->f_flags & O_NONBLOCK) {
0167 ret = -EAGAIN;
0168 break;
0169 }
0170 if (signal_pending(current)) {
0171 ret = -ERESTARTSYS;
0172 break;
0173 }
0174 schedule();
0175 } while (1);
0176 set_current_state(TASK_RUNNING);
0177 remove_wait_queue(&rtc->irq_queue, &wait);
0178
0179 if (ret == 0) {
0180 if (sizeof(int) != sizeof(long) &&
0181 count == sizeof(unsigned int))
0182 ret = put_user(data, (unsigned int __user *)buf) ?:
0183 sizeof(unsigned int);
0184 else
0185 ret = put_user(data, (unsigned long __user *)buf) ?:
0186 sizeof(unsigned long);
0187 }
0188 return ret;
0189 }
0190
0191 static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
0192 {
0193 struct rtc_device *rtc = file->private_data;
0194 unsigned long data;
0195
0196 poll_wait(file, &rtc->irq_queue, wait);
0197
0198 data = rtc->irq_data;
0199
0200 return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
0201 }
0202
0203 static long rtc_dev_ioctl(struct file *file,
0204 unsigned int cmd, unsigned long arg)
0205 {
0206 int err = 0;
0207 struct rtc_device *rtc = file->private_data;
0208 const struct rtc_class_ops *ops = rtc->ops;
0209 struct rtc_time tm;
0210 struct rtc_wkalrm alarm;
0211 struct rtc_param param;
0212 void __user *uarg = (void __user *)arg;
0213
0214 err = mutex_lock_interruptible(&rtc->ops_lock);
0215 if (err)
0216 return err;
0217
0218
0219
0220
0221
0222 switch (cmd) {
0223 case RTC_EPOCH_SET:
0224 case RTC_SET_TIME:
0225 case RTC_PARAM_SET:
0226 if (!capable(CAP_SYS_TIME))
0227 err = -EACCES;
0228 break;
0229
0230 case RTC_IRQP_SET:
0231 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
0232 err = -EACCES;
0233 break;
0234
0235 case RTC_PIE_ON:
0236 if (rtc->irq_freq > rtc->max_user_freq &&
0237 !capable(CAP_SYS_RESOURCE))
0238 err = -EACCES;
0239 break;
0240 }
0241
0242 if (err)
0243 goto done;
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 switch (cmd) {
0257 case RTC_ALM_READ:
0258 mutex_unlock(&rtc->ops_lock);
0259
0260 err = rtc_read_alarm(rtc, &alarm);
0261 if (err < 0)
0262 return err;
0263
0264 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
0265 err = -EFAULT;
0266 return err;
0267
0268 case RTC_ALM_SET:
0269 mutex_unlock(&rtc->ops_lock);
0270
0271 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
0272 return -EFAULT;
0273
0274 alarm.enabled = 0;
0275 alarm.pending = 0;
0276 alarm.time.tm_wday = -1;
0277 alarm.time.tm_yday = -1;
0278 alarm.time.tm_isdst = -1;
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293 {
0294 time64_t now, then;
0295
0296 err = rtc_read_time(rtc, &tm);
0297 if (err < 0)
0298 return err;
0299 now = rtc_tm_to_time64(&tm);
0300
0301 alarm.time.tm_mday = tm.tm_mday;
0302 alarm.time.tm_mon = tm.tm_mon;
0303 alarm.time.tm_year = tm.tm_year;
0304 err = rtc_valid_tm(&alarm.time);
0305 if (err < 0)
0306 return err;
0307 then = rtc_tm_to_time64(&alarm.time);
0308
0309
0310 if (then < now) {
0311 rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
0312 alarm.time.tm_mday = tm.tm_mday;
0313 alarm.time.tm_mon = tm.tm_mon;
0314 alarm.time.tm_year = tm.tm_year;
0315 }
0316 }
0317
0318 return rtc_set_alarm(rtc, &alarm);
0319
0320 case RTC_RD_TIME:
0321 mutex_unlock(&rtc->ops_lock);
0322
0323 err = rtc_read_time(rtc, &tm);
0324 if (err < 0)
0325 return err;
0326
0327 if (copy_to_user(uarg, &tm, sizeof(tm)))
0328 err = -EFAULT;
0329 return err;
0330
0331 case RTC_SET_TIME:
0332 mutex_unlock(&rtc->ops_lock);
0333
0334 if (copy_from_user(&tm, uarg, sizeof(tm)))
0335 return -EFAULT;
0336
0337 return rtc_set_time(rtc, &tm);
0338
0339 case RTC_PIE_ON:
0340 err = rtc_irq_set_state(rtc, 1);
0341 break;
0342
0343 case RTC_PIE_OFF:
0344 err = rtc_irq_set_state(rtc, 0);
0345 break;
0346
0347 case RTC_AIE_ON:
0348 mutex_unlock(&rtc->ops_lock);
0349 return rtc_alarm_irq_enable(rtc, 1);
0350
0351 case RTC_AIE_OFF:
0352 mutex_unlock(&rtc->ops_lock);
0353 return rtc_alarm_irq_enable(rtc, 0);
0354
0355 case RTC_UIE_ON:
0356 mutex_unlock(&rtc->ops_lock);
0357 return rtc_update_irq_enable(rtc, 1);
0358
0359 case RTC_UIE_OFF:
0360 mutex_unlock(&rtc->ops_lock);
0361 return rtc_update_irq_enable(rtc, 0);
0362
0363 case RTC_IRQP_SET:
0364 err = rtc_irq_set_freq(rtc, arg);
0365 break;
0366 case RTC_IRQP_READ:
0367 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
0368 break;
0369
0370 case RTC_WKALM_SET:
0371 mutex_unlock(&rtc->ops_lock);
0372 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
0373 return -EFAULT;
0374
0375 return rtc_set_alarm(rtc, &alarm);
0376
0377 case RTC_WKALM_RD:
0378 mutex_unlock(&rtc->ops_lock);
0379 err = rtc_read_alarm(rtc, &alarm);
0380 if (err < 0)
0381 return err;
0382
0383 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
0384 err = -EFAULT;
0385 return err;
0386
0387 case RTC_PARAM_GET:
0388 if (copy_from_user(¶m, uarg, sizeof(param))) {
0389 mutex_unlock(&rtc->ops_lock);
0390 return -EFAULT;
0391 }
0392
0393 switch(param.param) {
0394 case RTC_PARAM_FEATURES:
0395 if (param.index != 0)
0396 err = -EINVAL;
0397 param.uvalue = rtc->features[0];
0398 break;
0399
0400 case RTC_PARAM_CORRECTION: {
0401 long offset;
0402 mutex_unlock(&rtc->ops_lock);
0403 if (param.index != 0)
0404 return -EINVAL;
0405 err = rtc_read_offset(rtc, &offset);
0406 mutex_lock(&rtc->ops_lock);
0407 if (err == 0)
0408 param.svalue = offset;
0409 break;
0410 }
0411 default:
0412 if (rtc->ops->param_get)
0413 err = rtc->ops->param_get(rtc->dev.parent, ¶m);
0414 else
0415 err = -EINVAL;
0416 }
0417
0418 if (!err)
0419 if (copy_to_user(uarg, ¶m, sizeof(param)))
0420 err = -EFAULT;
0421
0422 break;
0423
0424 case RTC_PARAM_SET:
0425 if (copy_from_user(¶m, uarg, sizeof(param))) {
0426 mutex_unlock(&rtc->ops_lock);
0427 return -EFAULT;
0428 }
0429
0430 switch(param.param) {
0431 case RTC_PARAM_FEATURES:
0432 err = -EINVAL;
0433 break;
0434
0435 case RTC_PARAM_CORRECTION:
0436 mutex_unlock(&rtc->ops_lock);
0437 if (param.index != 0)
0438 return -EINVAL;
0439 return rtc_set_offset(rtc, param.svalue);
0440
0441 default:
0442 if (rtc->ops->param_set)
0443 err = rtc->ops->param_set(rtc->dev.parent, ¶m);
0444 else
0445 err = -EINVAL;
0446 }
0447
0448 break;
0449
0450 default:
0451
0452 if (ops->ioctl) {
0453 err = ops->ioctl(rtc->dev.parent, cmd, arg);
0454 if (err == -ENOIOCTLCMD)
0455 err = -ENOTTY;
0456 } else {
0457 err = -ENOTTY;
0458 }
0459 break;
0460 }
0461
0462 done:
0463 mutex_unlock(&rtc->ops_lock);
0464 return err;
0465 }
0466
0467 #ifdef CONFIG_COMPAT
0468 #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
0469 #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
0470 #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
0471
0472 static long rtc_dev_compat_ioctl(struct file *file,
0473 unsigned int cmd, unsigned long arg)
0474 {
0475 struct rtc_device *rtc = file->private_data;
0476 void __user *uarg = compat_ptr(arg);
0477
0478 switch (cmd) {
0479 case RTC_IRQP_READ32:
0480 return put_user(rtc->irq_freq, (__u32 __user *)uarg);
0481
0482 case RTC_IRQP_SET32:
0483
0484 return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
0485
0486 case RTC_EPOCH_SET32:
0487
0488 return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
0489 }
0490
0491 return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
0492 }
0493 #endif
0494
0495 static int rtc_dev_fasync(int fd, struct file *file, int on)
0496 {
0497 struct rtc_device *rtc = file->private_data;
0498
0499 return fasync_helper(fd, file, on, &rtc->async_queue);
0500 }
0501
0502 static int rtc_dev_release(struct inode *inode, struct file *file)
0503 {
0504 struct rtc_device *rtc = file->private_data;
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516 rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
0517 rtc_update_irq_enable(rtc, 0);
0518 rtc_irq_set_state(rtc, 0);
0519
0520 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
0521 return 0;
0522 }
0523
0524 static const struct file_operations rtc_dev_fops = {
0525 .owner = THIS_MODULE,
0526 .llseek = no_llseek,
0527 .read = rtc_dev_read,
0528 .poll = rtc_dev_poll,
0529 .unlocked_ioctl = rtc_dev_ioctl,
0530 #ifdef CONFIG_COMPAT
0531 .compat_ioctl = rtc_dev_compat_ioctl,
0532 #endif
0533 .open = rtc_dev_open,
0534 .release = rtc_dev_release,
0535 .fasync = rtc_dev_fasync,
0536 };
0537
0538
0539
0540 void rtc_dev_prepare(struct rtc_device *rtc)
0541 {
0542 if (!rtc_devt)
0543 return;
0544
0545 if (rtc->id >= RTC_DEV_MAX) {
0546 dev_dbg(&rtc->dev, "too many RTC devices\n");
0547 return;
0548 }
0549
0550 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
0551
0552 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
0553 INIT_WORK(&rtc->uie_task, rtc_uie_task);
0554 timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
0555 #endif
0556
0557 cdev_init(&rtc->char_dev, &rtc_dev_fops);
0558 rtc->char_dev.owner = rtc->owner;
0559 }
0560
0561 void __init rtc_dev_init(void)
0562 {
0563 int err;
0564
0565 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
0566 if (err < 0)
0567 pr_err("failed to allocate char dev region\n");
0568 }