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