rtctime.c 11 KB

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