rtctime.c 8.9 KB

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