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