rtctime.c 11 KB

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