Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/common/rtctime.c |
| 3 | * |
| 4 | * Copyright (C) 2003 Deep Blue Solutions Ltd. |
| 5 | * Based on sa1100-rtc.c, Nils Faerber, CIH, Nicolas Pitre. |
| 6 | * Based on rtc.c by Paul Gortmaker |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/rtc.h> |
| 16 | #include <linux/poll.h> |
| 17 | #include <linux/proc_fs.h> |
| 18 | #include <linux/miscdevice.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/device.h> |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 21 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include <asm/rtc.h> |
| 24 | #include <asm/semaphore.h> |
| 25 | |
| 26 | static DECLARE_WAIT_QUEUE_HEAD(rtc_wait); |
| 27 | static struct fasync_struct *rtc_async_queue; |
| 28 | |
| 29 | /* |
| 30 | * rtc_lock protects rtc_irq_data |
| 31 | */ |
| 32 | static DEFINE_SPINLOCK(rtc_lock); |
| 33 | static unsigned long rtc_irq_data; |
| 34 | |
| 35 | /* |
| 36 | * rtc_sem protects rtc_inuse and rtc_ops |
| 37 | */ |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 38 | static DEFINE_MUTEX(rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static unsigned long rtc_inuse; |
| 40 | static struct rtc_ops *rtc_ops; |
| 41 | |
| 42 | #define rtc_epoch 1900UL |
| 43 | |
| 44 | static const unsigned char days_in_month[] = { |
| 45 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 46 | }; |
| 47 | |
| 48 | #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400) |
| 49 | #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400)) |
| 50 | |
| 51 | static int month_days(unsigned int month, unsigned int year) |
| 52 | { |
| 53 | return days_in_month[month] + (LEAP_YEAR(year) && month == 1); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. |
| 58 | */ |
| 59 | void rtc_time_to_tm(unsigned long time, struct rtc_time *tm) |
| 60 | { |
| 61 | int days, month, year; |
| 62 | |
| 63 | days = time / 86400; |
| 64 | time -= days * 86400; |
| 65 | |
| 66 | tm->tm_wday = (days + 4) % 7; |
| 67 | |
| 68 | year = 1970 + days / 365; |
| 69 | days -= (year - 1970) * 365 |
| 70 | + LEAPS_THRU_END_OF(year - 1) |
| 71 | - LEAPS_THRU_END_OF(1970 - 1); |
| 72 | if (days < 0) { |
| 73 | year -= 1; |
| 74 | days += 365 + LEAP_YEAR(year); |
| 75 | } |
| 76 | tm->tm_year = year - 1900; |
| 77 | tm->tm_yday = days + 1; |
| 78 | |
| 79 | for (month = 0; month < 11; month++) { |
| 80 | int newdays; |
| 81 | |
| 82 | newdays = days - month_days(month, year); |
| 83 | if (newdays < 0) |
| 84 | break; |
| 85 | days = newdays; |
| 86 | } |
| 87 | tm->tm_mon = month; |
| 88 | tm->tm_mday = days + 1; |
| 89 | |
| 90 | tm->tm_hour = time / 3600; |
| 91 | time -= tm->tm_hour * 3600; |
| 92 | tm->tm_min = time / 60; |
| 93 | tm->tm_sec = time - tm->tm_min * 60; |
| 94 | } |
| 95 | EXPORT_SYMBOL(rtc_time_to_tm); |
| 96 | |
| 97 | /* |
| 98 | * Does the rtc_time represent a valid date/time? |
| 99 | */ |
| 100 | int rtc_valid_tm(struct rtc_time *tm) |
| 101 | { |
| 102 | if (tm->tm_year < 70 || |
| 103 | tm->tm_mon >= 12 || |
| 104 | tm->tm_mday < 1 || |
| 105 | tm->tm_mday > month_days(tm->tm_mon, tm->tm_year + 1900) || |
| 106 | tm->tm_hour >= 24 || |
| 107 | tm->tm_min >= 60 || |
| 108 | tm->tm_sec >= 60) |
| 109 | return -EINVAL; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | EXPORT_SYMBOL(rtc_valid_tm); |
| 114 | |
| 115 | /* |
| 116 | * Convert Gregorian date to seconds since 01-01-1970 00:00:00. |
| 117 | */ |
| 118 | int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time) |
| 119 | { |
| 120 | *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
| 121 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | EXPORT_SYMBOL(rtc_tm_to_time); |
| 126 | |
| 127 | /* |
| 128 | * Calculate the next alarm time given the requested alarm time mask |
| 129 | * and the current time. |
| 130 | * |
| 131 | * FIXME: for now, we just copy the alarm time because we're lazy (and |
| 132 | * is therefore buggy - setting a 10am alarm at 8pm will not result in |
| 133 | * the alarm triggering.) |
| 134 | */ |
| 135 | void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm) |
| 136 | { |
| 137 | next->tm_year = now->tm_year; |
| 138 | next->tm_mon = now->tm_mon; |
| 139 | next->tm_mday = now->tm_mday; |
| 140 | next->tm_hour = alrm->tm_hour; |
| 141 | next->tm_min = alrm->tm_min; |
| 142 | next->tm_sec = alrm->tm_sec; |
| 143 | } |
| 144 | |
Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 145 | static inline int rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | memset(tm, 0, sizeof(struct rtc_time)); |
Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 148 | return ops->read_time(tm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm) |
| 152 | { |
| 153 | int ret; |
| 154 | |
| 155 | ret = rtc_valid_tm(tm); |
| 156 | if (ret == 0) |
| 157 | ret = ops->set_time(tm); |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm) |
| 163 | { |
| 164 | int ret = -EINVAL; |
| 165 | if (ops->read_alarm) { |
| 166 | memset(alrm, 0, sizeof(struct rtc_wkalrm)); |
Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 167 | ret = ops->read_alarm(alrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm) |
| 173 | { |
| 174 | int ret = -EINVAL; |
| 175 | if (ops->set_alarm) |
| 176 | ret = ops->set_alarm(alrm); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | void rtc_update(unsigned long num, unsigned long events) |
| 181 | { |
| 182 | spin_lock(&rtc_lock); |
| 183 | rtc_irq_data = (rtc_irq_data + (num << 8)) | events; |
| 184 | spin_unlock(&rtc_lock); |
| 185 | |
| 186 | wake_up_interruptible(&rtc_wait); |
| 187 | kill_fasync(&rtc_async_queue, SIGIO, POLL_IN); |
| 188 | } |
| 189 | EXPORT_SYMBOL(rtc_update); |
| 190 | |
| 191 | |
| 192 | static ssize_t |
| 193 | rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 194 | { |
| 195 | DECLARE_WAITQUEUE(wait, current); |
| 196 | unsigned long data; |
| 197 | ssize_t ret; |
| 198 | |
| 199 | if (count < sizeof(unsigned long)) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | add_wait_queue(&rtc_wait, &wait); |
| 203 | do { |
| 204 | __set_current_state(TASK_INTERRUPTIBLE); |
| 205 | |
| 206 | spin_lock_irq(&rtc_lock); |
| 207 | data = rtc_irq_data; |
| 208 | rtc_irq_data = 0; |
| 209 | spin_unlock_irq(&rtc_lock); |
| 210 | |
| 211 | if (data != 0) { |
| 212 | ret = 0; |
| 213 | break; |
| 214 | } |
| 215 | if (file->f_flags & O_NONBLOCK) { |
| 216 | ret = -EAGAIN; |
| 217 | break; |
| 218 | } |
| 219 | if (signal_pending(current)) { |
| 220 | ret = -ERESTARTSYS; |
| 221 | break; |
| 222 | } |
| 223 | schedule(); |
| 224 | } while (1); |
| 225 | set_current_state(TASK_RUNNING); |
| 226 | remove_wait_queue(&rtc_wait, &wait); |
| 227 | |
| 228 | if (ret == 0) { |
| 229 | ret = put_user(data, (unsigned long __user *)buf); |
| 230 | if (ret == 0) |
| 231 | ret = sizeof(unsigned long); |
| 232 | } |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static unsigned int rtc_poll(struct file *file, poll_table *wait) |
| 237 | { |
| 238 | unsigned long data; |
| 239 | |
| 240 | poll_wait(file, &rtc_wait, wait); |
| 241 | |
| 242 | spin_lock_irq(&rtc_lock); |
| 243 | data = rtc_irq_data; |
| 244 | spin_unlock_irq(&rtc_lock); |
| 245 | |
| 246 | return data != 0 ? POLLIN | POLLRDNORM : 0; |
| 247 | } |
| 248 | |
| 249 | static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 250 | unsigned long arg) |
| 251 | { |
| 252 | struct rtc_ops *ops = file->private_data; |
| 253 | struct rtc_time tm; |
| 254 | struct rtc_wkalrm alrm; |
| 255 | void __user *uarg = (void __user *)arg; |
| 256 | int ret = -EINVAL; |
| 257 | |
| 258 | switch (cmd) { |
| 259 | case RTC_ALM_READ: |
| 260 | ret = rtc_read_alarm(ops, &alrm); |
| 261 | if (ret) |
| 262 | break; |
| 263 | ret = copy_to_user(uarg, &alrm.time, sizeof(tm)); |
| 264 | if (ret) |
| 265 | ret = -EFAULT; |
| 266 | break; |
| 267 | |
| 268 | case RTC_ALM_SET: |
| 269 | ret = copy_from_user(&alrm.time, uarg, sizeof(tm)); |
| 270 | if (ret) { |
| 271 | ret = -EFAULT; |
| 272 | break; |
| 273 | } |
| 274 | alrm.enabled = 0; |
| 275 | alrm.pending = 0; |
| 276 | alrm.time.tm_mday = -1; |
| 277 | alrm.time.tm_mon = -1; |
| 278 | alrm.time.tm_year = -1; |
| 279 | alrm.time.tm_wday = -1; |
| 280 | alrm.time.tm_yday = -1; |
| 281 | alrm.time.tm_isdst = -1; |
| 282 | ret = rtc_set_alarm(ops, &alrm); |
| 283 | break; |
| 284 | |
| 285 | case RTC_RD_TIME: |
Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 286 | ret = rtc_read_time(ops, &tm); |
| 287 | if (ret) |
| 288 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | ret = copy_to_user(uarg, &tm, sizeof(tm)); |
| 290 | if (ret) |
| 291 | ret = -EFAULT; |
| 292 | break; |
| 293 | |
| 294 | case RTC_SET_TIME: |
| 295 | if (!capable(CAP_SYS_TIME)) { |
| 296 | ret = -EACCES; |
| 297 | break; |
| 298 | } |
| 299 | ret = copy_from_user(&tm, uarg, sizeof(tm)); |
| 300 | if (ret) { |
| 301 | ret = -EFAULT; |
| 302 | break; |
| 303 | } |
| 304 | ret = rtc_set_time(ops, &tm); |
| 305 | break; |
| 306 | |
| 307 | case RTC_EPOCH_SET: |
| 308 | #ifndef rtc_epoch |
| 309 | /* |
| 310 | * There were no RTC clocks before 1900. |
| 311 | */ |
| 312 | if (arg < 1900) { |
| 313 | ret = -EINVAL; |
| 314 | break; |
| 315 | } |
| 316 | if (!capable(CAP_SYS_TIME)) { |
| 317 | ret = -EACCES; |
| 318 | break; |
| 319 | } |
| 320 | rtc_epoch = arg; |
| 321 | ret = 0; |
| 322 | #endif |
| 323 | break; |
| 324 | |
| 325 | case RTC_EPOCH_READ: |
| 326 | ret = put_user(rtc_epoch, (unsigned long __user *)uarg); |
| 327 | break; |
| 328 | |
| 329 | case RTC_WKALM_SET: |
| 330 | ret = copy_from_user(&alrm, uarg, sizeof(alrm)); |
| 331 | if (ret) { |
| 332 | ret = -EFAULT; |
| 333 | break; |
| 334 | } |
| 335 | ret = rtc_set_alarm(ops, &alrm); |
| 336 | break; |
| 337 | |
| 338 | case RTC_WKALM_RD: |
| 339 | ret = rtc_read_alarm(ops, &alrm); |
| 340 | if (ret) |
| 341 | break; |
| 342 | ret = copy_to_user(uarg, &alrm, sizeof(alrm)); |
| 343 | if (ret) |
| 344 | ret = -EFAULT; |
| 345 | break; |
| 346 | |
| 347 | default: |
| 348 | if (ops->ioctl) |
| 349 | ret = ops->ioctl(cmd, arg); |
| 350 | break; |
| 351 | } |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static int rtc_open(struct inode *inode, struct file *file) |
| 356 | { |
| 357 | int ret; |
| 358 | |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 359 | mutex_lock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
| 361 | if (rtc_inuse) { |
| 362 | ret = -EBUSY; |
| 363 | } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) { |
| 364 | ret = -ENODEV; |
| 365 | } else { |
| 366 | file->private_data = rtc_ops; |
| 367 | |
| 368 | ret = rtc_ops->open ? rtc_ops->open() : 0; |
| 369 | if (ret == 0) { |
| 370 | spin_lock_irq(&rtc_lock); |
| 371 | rtc_irq_data = 0; |
| 372 | spin_unlock_irq(&rtc_lock); |
| 373 | |
| 374 | rtc_inuse = 1; |
| 375 | } |
| 376 | } |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 377 | mutex_unlock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | static int rtc_release(struct inode *inode, struct file *file) |
| 383 | { |
| 384 | struct rtc_ops *ops = file->private_data; |
| 385 | |
| 386 | if (ops->release) |
| 387 | ops->release(); |
| 388 | |
| 389 | spin_lock_irq(&rtc_lock); |
| 390 | rtc_irq_data = 0; |
| 391 | spin_unlock_irq(&rtc_lock); |
| 392 | |
| 393 | module_put(rtc_ops->owner); |
| 394 | rtc_inuse = 0; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int rtc_fasync(int fd, struct file *file, int on) |
| 400 | { |
| 401 | return fasync_helper(fd, file, on, &rtc_async_queue); |
| 402 | } |
| 403 | |
| 404 | static struct file_operations rtc_fops = { |
| 405 | .owner = THIS_MODULE, |
| 406 | .llseek = no_llseek, |
| 407 | .read = rtc_read, |
| 408 | .poll = rtc_poll, |
| 409 | .ioctl = rtc_ioctl, |
| 410 | .open = rtc_open, |
| 411 | .release = rtc_release, |
| 412 | .fasync = rtc_fasync, |
| 413 | }; |
| 414 | |
| 415 | static struct miscdevice rtc_miscdev = { |
| 416 | .minor = RTC_MINOR, |
| 417 | .name = "rtc", |
| 418 | .fops = &rtc_fops, |
| 419 | }; |
| 420 | |
| 421 | |
| 422 | static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data) |
| 423 | { |
| 424 | struct rtc_ops *ops = data; |
| 425 | struct rtc_wkalrm alrm; |
| 426 | struct rtc_time tm; |
| 427 | char *p = page; |
| 428 | |
Russell King | d5aa207 | 2005-04-30 12:19:28 +0100 | [diff] [blame] | 429 | if (rtc_read_time(ops, &tm) == 0) { |
| 430 | p += sprintf(p, |
| 431 | "rtc_time\t: %02d:%02d:%02d\n" |
| 432 | "rtc_date\t: %04d-%02d-%02d\n" |
| 433 | "rtc_epoch\t: %04lu\n", |
| 434 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 435 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 436 | rtc_epoch); |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | if (rtc_read_alarm(ops, &alrm) == 0) { |
| 440 | p += sprintf(p, "alrm_time\t: "); |
| 441 | if ((unsigned int)alrm.time.tm_hour <= 24) |
| 442 | p += sprintf(p, "%02d:", alrm.time.tm_hour); |
| 443 | else |
| 444 | p += sprintf(p, "**:"); |
| 445 | if ((unsigned int)alrm.time.tm_min <= 59) |
| 446 | p += sprintf(p, "%02d:", alrm.time.tm_min); |
| 447 | else |
| 448 | p += sprintf(p, "**:"); |
| 449 | if ((unsigned int)alrm.time.tm_sec <= 59) |
| 450 | p += sprintf(p, "%02d\n", alrm.time.tm_sec); |
| 451 | else |
| 452 | p += sprintf(p, "**\n"); |
| 453 | |
| 454 | p += sprintf(p, "alrm_date\t: "); |
| 455 | if ((unsigned int)alrm.time.tm_year <= 200) |
| 456 | p += sprintf(p, "%04d-", alrm.time.tm_year + 1900); |
| 457 | else |
| 458 | p += sprintf(p, "****-"); |
| 459 | if ((unsigned int)alrm.time.tm_mon <= 11) |
| 460 | p += sprintf(p, "%02d-", alrm.time.tm_mon + 1); |
| 461 | else |
| 462 | p += sprintf(p, "**-"); |
| 463 | if ((unsigned int)alrm.time.tm_mday <= 31) |
| 464 | p += sprintf(p, "%02d\n", alrm.time.tm_mday); |
| 465 | else |
| 466 | p += sprintf(p, "**\n"); |
| 467 | p += sprintf(p, "alrm_wakeup\t: %s\n", |
| 468 | alrm.enabled ? "yes" : "no"); |
| 469 | p += sprintf(p, "alrm_pending\t: %s\n", |
| 470 | alrm.pending ? "yes" : "no"); |
| 471 | } |
| 472 | |
| 473 | if (ops->proc) |
| 474 | p += ops->proc(p); |
| 475 | |
| 476 | return p - page; |
| 477 | } |
| 478 | |
| 479 | int register_rtc(struct rtc_ops *ops) |
| 480 | { |
| 481 | int ret = -EBUSY; |
| 482 | |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 483 | mutex_lock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | if (rtc_ops == NULL) { |
| 485 | rtc_ops = ops; |
| 486 | |
| 487 | ret = misc_register(&rtc_miscdev); |
| 488 | if (ret == 0) |
| 489 | create_proc_read_entry("driver/rtc", 0, NULL, |
| 490 | rtc_read_proc, ops); |
| 491 | } |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 492 | mutex_unlock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | EXPORT_SYMBOL(register_rtc); |
| 497 | |
| 498 | void unregister_rtc(struct rtc_ops *rtc) |
| 499 | { |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 500 | mutex_lock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | if (rtc == rtc_ops) { |
| 502 | remove_proc_entry("driver/rtc", NULL); |
| 503 | misc_deregister(&rtc_miscdev); |
| 504 | rtc_ops = NULL; |
| 505 | } |
Arjan van de Ven | 0043170 | 2006-01-12 18:42:23 +0000 | [diff] [blame^] | 506 | mutex_unlock(&rtc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | EXPORT_SYMBOL(unregister_rtc); |