rtctime.c 10 KB

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