rtctime.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
  113. {
  114. unsigned long next_time;
  115. unsigned long now_time;
  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. rtc_tm_to_time(now, &now_time);
  123. rtc_tm_to_time(next, &next_time);
  124. if (next_time < now_time) {
  125. /* Advance one day */
  126. next_time += 60 * 60 * 24;
  127. rtc_time_to_tm(next_time, next);
  128. }
  129. }
  130. static inline int rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm)
  131. {
  132. memset(tm, 0, sizeof(struct rtc_time));
  133. return ops->read_time(tm);
  134. }
  135. static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm)
  136. {
  137. int ret;
  138. ret = rtc_valid_tm(tm);
  139. if (ret == 0)
  140. ret = ops->set_time(tm);
  141. return ret;
  142. }
  143. static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
  144. {
  145. int ret = -EINVAL;
  146. if (ops->read_alarm) {
  147. memset(alrm, 0, sizeof(struct rtc_wkalrm));
  148. ret = ops->read_alarm(alrm);
  149. }
  150. return ret;
  151. }
  152. static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
  153. {
  154. int ret = -EINVAL;
  155. if (ops->set_alarm)
  156. ret = ops->set_alarm(alrm);
  157. return ret;
  158. }
  159. void rtc_update(unsigned long num, unsigned long events)
  160. {
  161. spin_lock(&rtc_lock);
  162. rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
  163. spin_unlock(&rtc_lock);
  164. wake_up_interruptible(&rtc_wait);
  165. kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
  166. }
  167. EXPORT_SYMBOL(rtc_update);
  168. static ssize_t
  169. rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  170. {
  171. DECLARE_WAITQUEUE(wait, current);
  172. unsigned long data;
  173. ssize_t ret;
  174. if (count < sizeof(unsigned long))
  175. return -EINVAL;
  176. add_wait_queue(&rtc_wait, &wait);
  177. do {
  178. __set_current_state(TASK_INTERRUPTIBLE);
  179. spin_lock_irq(&rtc_lock);
  180. data = rtc_irq_data;
  181. rtc_irq_data = 0;
  182. spin_unlock_irq(&rtc_lock);
  183. if (data != 0) {
  184. ret = 0;
  185. break;
  186. }
  187. if (file->f_flags & O_NONBLOCK) {
  188. ret = -EAGAIN;
  189. break;
  190. }
  191. if (signal_pending(current)) {
  192. ret = -ERESTARTSYS;
  193. break;
  194. }
  195. schedule();
  196. } while (1);
  197. set_current_state(TASK_RUNNING);
  198. remove_wait_queue(&rtc_wait, &wait);
  199. if (ret == 0) {
  200. ret = put_user(data, (unsigned long __user *)buf);
  201. if (ret == 0)
  202. ret = sizeof(unsigned long);
  203. }
  204. return ret;
  205. }
  206. static unsigned int rtc_poll(struct file *file, poll_table *wait)
  207. {
  208. unsigned long data;
  209. poll_wait(file, &rtc_wait, wait);
  210. spin_lock_irq(&rtc_lock);
  211. data = rtc_irq_data;
  212. spin_unlock_irq(&rtc_lock);
  213. return data != 0 ? POLLIN | POLLRDNORM : 0;
  214. }
  215. static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  216. unsigned long arg)
  217. {
  218. struct rtc_ops *ops = file->private_data;
  219. struct rtc_time tm;
  220. struct rtc_wkalrm alrm;
  221. void __user *uarg = (void __user *)arg;
  222. int ret = -EINVAL;
  223. switch (cmd) {
  224. case RTC_ALM_READ:
  225. ret = rtc_read_alarm(ops, &alrm);
  226. if (ret)
  227. break;
  228. ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
  229. if (ret)
  230. ret = -EFAULT;
  231. break;
  232. case RTC_ALM_SET:
  233. ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
  234. if (ret) {
  235. ret = -EFAULT;
  236. break;
  237. }
  238. alrm.enabled = 0;
  239. alrm.pending = 0;
  240. alrm.time.tm_mday = -1;
  241. alrm.time.tm_mon = -1;
  242. alrm.time.tm_year = -1;
  243. alrm.time.tm_wday = -1;
  244. alrm.time.tm_yday = -1;
  245. alrm.time.tm_isdst = -1;
  246. ret = rtc_set_alarm(ops, &alrm);
  247. break;
  248. case RTC_RD_TIME:
  249. ret = rtc_read_time(ops, &tm);
  250. if (ret)
  251. break;
  252. ret = copy_to_user(uarg, &tm, sizeof(tm));
  253. if (ret)
  254. ret = -EFAULT;
  255. break;
  256. case RTC_SET_TIME:
  257. if (!capable(CAP_SYS_TIME)) {
  258. ret = -EACCES;
  259. break;
  260. }
  261. ret = copy_from_user(&tm, uarg, sizeof(tm));
  262. if (ret) {
  263. ret = -EFAULT;
  264. break;
  265. }
  266. ret = rtc_set_time(ops, &tm);
  267. break;
  268. case RTC_EPOCH_SET:
  269. #ifndef rtc_epoch
  270. /*
  271. * There were no RTC clocks before 1900.
  272. */
  273. if (arg < 1900) {
  274. ret = -EINVAL;
  275. break;
  276. }
  277. if (!capable(CAP_SYS_TIME)) {
  278. ret = -EACCES;
  279. break;
  280. }
  281. rtc_epoch = arg;
  282. ret = 0;
  283. #endif
  284. break;
  285. case RTC_EPOCH_READ:
  286. ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
  287. break;
  288. case RTC_WKALM_SET:
  289. ret = copy_from_user(&alrm, uarg, sizeof(alrm));
  290. if (ret) {
  291. ret = -EFAULT;
  292. break;
  293. }
  294. ret = rtc_set_alarm(ops, &alrm);
  295. break;
  296. case RTC_WKALM_RD:
  297. ret = rtc_read_alarm(ops, &alrm);
  298. if (ret)
  299. break;
  300. ret = copy_to_user(uarg, &alrm, sizeof(alrm));
  301. if (ret)
  302. ret = -EFAULT;
  303. break;
  304. default:
  305. if (ops->ioctl)
  306. ret = ops->ioctl(cmd, arg);
  307. break;
  308. }
  309. return ret;
  310. }
  311. static int rtc_open(struct inode *inode, struct file *file)
  312. {
  313. int ret;
  314. mutex_lock(&rtc_mutex);
  315. if (rtc_inuse) {
  316. ret = -EBUSY;
  317. } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
  318. ret = -ENODEV;
  319. } else {
  320. file->private_data = rtc_ops;
  321. ret = rtc_ops->open ? rtc_ops->open() : 0;
  322. if (ret == 0) {
  323. spin_lock_irq(&rtc_lock);
  324. rtc_irq_data = 0;
  325. spin_unlock_irq(&rtc_lock);
  326. rtc_inuse = 1;
  327. }
  328. }
  329. mutex_unlock(&rtc_mutex);
  330. return ret;
  331. }
  332. static int rtc_release(struct inode *inode, struct file *file)
  333. {
  334. struct rtc_ops *ops = file->private_data;
  335. if (ops->release)
  336. ops->release();
  337. spin_lock_irq(&rtc_lock);
  338. rtc_irq_data = 0;
  339. spin_unlock_irq(&rtc_lock);
  340. module_put(rtc_ops->owner);
  341. rtc_inuse = 0;
  342. return 0;
  343. }
  344. static int rtc_fasync(int fd, struct file *file, int on)
  345. {
  346. return fasync_helper(fd, file, on, &rtc_async_queue);
  347. }
  348. static struct file_operations rtc_fops = {
  349. .owner = THIS_MODULE,
  350. .llseek = no_llseek,
  351. .read = rtc_read,
  352. .poll = rtc_poll,
  353. .ioctl = rtc_ioctl,
  354. .open = rtc_open,
  355. .release = rtc_release,
  356. .fasync = rtc_fasync,
  357. };
  358. static struct miscdevice rtc_miscdev = {
  359. .minor = RTC_MINOR,
  360. .name = "rtc",
  361. .fops = &rtc_fops,
  362. };
  363. static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
  364. {
  365. struct rtc_ops *ops = data;
  366. struct rtc_wkalrm alrm;
  367. struct rtc_time tm;
  368. char *p = page;
  369. if (rtc_read_time(ops, &tm) == 0) {
  370. p += sprintf(p,
  371. "rtc_time\t: %02d:%02d:%02d\n"
  372. "rtc_date\t: %04d-%02d-%02d\n"
  373. "rtc_epoch\t: %04lu\n",
  374. tm.tm_hour, tm.tm_min, tm.tm_sec,
  375. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  376. rtc_epoch);
  377. }
  378. if (rtc_read_alarm(ops, &alrm) == 0) {
  379. p += sprintf(p, "alrm_time\t: ");
  380. if ((unsigned int)alrm.time.tm_hour <= 24)
  381. p += sprintf(p, "%02d:", alrm.time.tm_hour);
  382. else
  383. p += sprintf(p, "**:");
  384. if ((unsigned int)alrm.time.tm_min <= 59)
  385. p += sprintf(p, "%02d:", alrm.time.tm_min);
  386. else
  387. p += sprintf(p, "**:");
  388. if ((unsigned int)alrm.time.tm_sec <= 59)
  389. p += sprintf(p, "%02d\n", alrm.time.tm_sec);
  390. else
  391. p += sprintf(p, "**\n");
  392. p += sprintf(p, "alrm_date\t: ");
  393. if ((unsigned int)alrm.time.tm_year <= 200)
  394. p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
  395. else
  396. p += sprintf(p, "****-");
  397. if ((unsigned int)alrm.time.tm_mon <= 11)
  398. p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
  399. else
  400. p += sprintf(p, "**-");
  401. if ((unsigned int)alrm.time.tm_mday <= 31)
  402. p += sprintf(p, "%02d\n", alrm.time.tm_mday);
  403. else
  404. p += sprintf(p, "**\n");
  405. p += sprintf(p, "alrm_wakeup\t: %s\n",
  406. alrm.enabled ? "yes" : "no");
  407. p += sprintf(p, "alrm_pending\t: %s\n",
  408. alrm.pending ? "yes" : "no");
  409. }
  410. if (ops->proc)
  411. p += ops->proc(p);
  412. return p - page;
  413. }
  414. int register_rtc(struct rtc_ops *ops)
  415. {
  416. int ret = -EBUSY;
  417. mutex_lock(&rtc_mutex);
  418. if (rtc_ops == NULL) {
  419. rtc_ops = ops;
  420. ret = misc_register(&rtc_miscdev);
  421. if (ret == 0)
  422. create_proc_read_entry("driver/rtc", 0, NULL,
  423. rtc_read_proc, ops);
  424. }
  425. mutex_unlock(&rtc_mutex);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(register_rtc);
  429. void unregister_rtc(struct rtc_ops *rtc)
  430. {
  431. mutex_lock(&rtc_mutex);
  432. if (rtc == rtc_ops) {
  433. remove_proc_entry("driver/rtc", NULL);
  434. misc_deregister(&rtc_miscdev);
  435. rtc_ops = NULL;
  436. }
  437. mutex_unlock(&rtc_mutex);
  438. }
  439. EXPORT_SYMBOL(unregister_rtc);