rtctime.c 8.9 KB

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