rtctime.c 8.9 KB

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