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