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