rtc-dev.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * RTC subsystem, dev interface
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * based on arch/arm/common/rtctime.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/rtc.h>
  15. #include <linux/sched.h>
  16. #include "rtc-core.h"
  17. static dev_t rtc_devt;
  18. #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
  19. static int rtc_dev_open(struct inode *inode, struct file *file)
  20. {
  21. int err;
  22. struct rtc_device *rtc = container_of(inode->i_cdev,
  23. struct rtc_device, char_dev);
  24. const struct rtc_class_ops *ops = rtc->ops;
  25. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  26. return -EBUSY;
  27. file->private_data = rtc;
  28. err = ops->open ? ops->open(rtc->dev.parent) : 0;
  29. if (err == 0) {
  30. spin_lock_irq(&rtc->irq_lock);
  31. rtc->irq_data = 0;
  32. spin_unlock_irq(&rtc->irq_lock);
  33. return 0;
  34. }
  35. /* something has gone wrong */
  36. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  37. return err;
  38. }
  39. static ssize_t
  40. rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  41. {
  42. struct rtc_device *rtc = file->private_data;
  43. DECLARE_WAITQUEUE(wait, current);
  44. unsigned long data;
  45. ssize_t ret;
  46. if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
  47. return -EINVAL;
  48. add_wait_queue(&rtc->irq_queue, &wait);
  49. do {
  50. __set_current_state(TASK_INTERRUPTIBLE);
  51. spin_lock_irq(&rtc->irq_lock);
  52. data = rtc->irq_data;
  53. rtc->irq_data = 0;
  54. spin_unlock_irq(&rtc->irq_lock);
  55. if (data != 0) {
  56. ret = 0;
  57. break;
  58. }
  59. if (file->f_flags & O_NONBLOCK) {
  60. ret = -EAGAIN;
  61. break;
  62. }
  63. if (signal_pending(current)) {
  64. ret = -ERESTARTSYS;
  65. break;
  66. }
  67. schedule();
  68. } while (1);
  69. set_current_state(TASK_RUNNING);
  70. remove_wait_queue(&rtc->irq_queue, &wait);
  71. if (ret == 0) {
  72. /* Check for any data updates */
  73. if (rtc->ops->read_callback)
  74. data = rtc->ops->read_callback(rtc->dev.parent,
  75. data);
  76. if (sizeof(int) != sizeof(long) &&
  77. count == sizeof(unsigned int))
  78. ret = put_user(data, (unsigned int __user *)buf) ?:
  79. sizeof(unsigned int);
  80. else
  81. ret = put_user(data, (unsigned long __user *)buf) ?:
  82. sizeof(unsigned long);
  83. }
  84. return ret;
  85. }
  86. static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
  87. {
  88. struct rtc_device *rtc = file->private_data;
  89. unsigned long data;
  90. poll_wait(file, &rtc->irq_queue, wait);
  91. data = rtc->irq_data;
  92. return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
  93. }
  94. static long rtc_dev_ioctl(struct file *file,
  95. unsigned int cmd, unsigned long arg)
  96. {
  97. int err = 0;
  98. struct rtc_device *rtc = file->private_data;
  99. const struct rtc_class_ops *ops = rtc->ops;
  100. struct rtc_time tm;
  101. struct rtc_wkalrm alarm;
  102. void __user *uarg = (void __user *) arg;
  103. err = mutex_lock_interruptible(&rtc->ops_lock);
  104. if (err)
  105. return err;
  106. /* check that the calling task has appropriate permissions
  107. * for certain ioctls. doing this check here is useful
  108. * to avoid duplicate code in each driver.
  109. */
  110. switch (cmd) {
  111. case RTC_EPOCH_SET:
  112. case RTC_SET_TIME:
  113. if (!capable(CAP_SYS_TIME))
  114. err = -EACCES;
  115. break;
  116. case RTC_IRQP_SET:
  117. if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
  118. err = -EACCES;
  119. break;
  120. case RTC_PIE_ON:
  121. if (rtc->irq_freq > rtc->max_user_freq &&
  122. !capable(CAP_SYS_RESOURCE))
  123. err = -EACCES;
  124. break;
  125. }
  126. if (err)
  127. goto done;
  128. /*
  129. * Drivers *SHOULD NOT* provide ioctl implementations
  130. * for these requests. Instead, provide methods to
  131. * support the following code, so that the RTC's main
  132. * features are accessible without using ioctls.
  133. *
  134. * RTC and alarm times will be in UTC, by preference,
  135. * but dual-booting with MS-Windows implies RTCs must
  136. * use the local wall clock time.
  137. */
  138. switch (cmd) {
  139. case RTC_ALM_READ:
  140. mutex_unlock(&rtc->ops_lock);
  141. err = rtc_read_alarm(rtc, &alarm);
  142. if (err < 0)
  143. return err;
  144. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  145. err = -EFAULT;
  146. return err;
  147. case RTC_ALM_SET:
  148. mutex_unlock(&rtc->ops_lock);
  149. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  150. return -EFAULT;
  151. alarm.enabled = 0;
  152. alarm.pending = 0;
  153. alarm.time.tm_wday = -1;
  154. alarm.time.tm_yday = -1;
  155. alarm.time.tm_isdst = -1;
  156. /* RTC_ALM_SET alarms may be up to 24 hours in the future.
  157. * Rather than expecting every RTC to implement "don't care"
  158. * for day/month/year fields, just force the alarm to have
  159. * the right values for those fields.
  160. *
  161. * RTC_WKALM_SET should be used instead. Not only does it
  162. * eliminate the need for a separate RTC_AIE_ON call, it
  163. * doesn't have the "alarm 23:59:59 in the future" race.
  164. *
  165. * NOTE: some legacy code may have used invalid fields as
  166. * wildcards, exposing hardware "periodic alarm" capabilities.
  167. * Not supported here.
  168. */
  169. {
  170. unsigned long now, then;
  171. err = rtc_read_time(rtc, &tm);
  172. if (err < 0)
  173. return err;
  174. rtc_tm_to_time(&tm, &now);
  175. alarm.time.tm_mday = tm.tm_mday;
  176. alarm.time.tm_mon = tm.tm_mon;
  177. alarm.time.tm_year = tm.tm_year;
  178. err = rtc_valid_tm(&alarm.time);
  179. if (err < 0)
  180. return err;
  181. rtc_tm_to_time(&alarm.time, &then);
  182. /* alarm may need to wrap into tomorrow */
  183. if (then < now) {
  184. rtc_time_to_tm(now + 24 * 60 * 60, &tm);
  185. alarm.time.tm_mday = tm.tm_mday;
  186. alarm.time.tm_mon = tm.tm_mon;
  187. alarm.time.tm_year = tm.tm_year;
  188. }
  189. }
  190. return rtc_set_alarm(rtc, &alarm);
  191. case RTC_RD_TIME:
  192. mutex_unlock(&rtc->ops_lock);
  193. err = rtc_read_time(rtc, &tm);
  194. if (err < 0)
  195. return err;
  196. if (copy_to_user(uarg, &tm, sizeof(tm)))
  197. err = -EFAULT;
  198. return err;
  199. case RTC_SET_TIME:
  200. mutex_unlock(&rtc->ops_lock);
  201. if (copy_from_user(&tm, uarg, sizeof(tm)))
  202. return -EFAULT;
  203. return rtc_set_time(rtc, &tm);
  204. case RTC_PIE_ON:
  205. err = rtc_irq_set_state(rtc, NULL, 1);
  206. break;
  207. case RTC_PIE_OFF:
  208. err = rtc_irq_set_state(rtc, NULL, 0);
  209. break;
  210. case RTC_AIE_ON:
  211. mutex_unlock(&rtc->ops_lock);
  212. return rtc_alarm_irq_enable(rtc, 1);
  213. case RTC_AIE_OFF:
  214. mutex_unlock(&rtc->ops_lock);
  215. return rtc_alarm_irq_enable(rtc, 0);
  216. case RTC_UIE_ON:
  217. mutex_unlock(&rtc->ops_lock);
  218. return rtc_update_irq_enable(rtc, 1);
  219. case RTC_UIE_OFF:
  220. mutex_unlock(&rtc->ops_lock);
  221. return rtc_update_irq_enable(rtc, 0);
  222. case RTC_IRQP_SET:
  223. err = rtc_irq_set_freq(rtc, NULL, arg);
  224. break;
  225. case RTC_IRQP_READ:
  226. err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
  227. break;
  228. #if 0
  229. case RTC_EPOCH_SET:
  230. #ifndef rtc_epoch
  231. /*
  232. * There were no RTC clocks before 1900.
  233. */
  234. if (arg < 1900) {
  235. err = -EINVAL;
  236. break;
  237. }
  238. rtc_epoch = arg;
  239. err = 0;
  240. #endif
  241. break;
  242. case RTC_EPOCH_READ:
  243. err = put_user(rtc_epoch, (unsigned long __user *)uarg);
  244. break;
  245. #endif
  246. case RTC_WKALM_SET:
  247. mutex_unlock(&rtc->ops_lock);
  248. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  249. return -EFAULT;
  250. return rtc_set_alarm(rtc, &alarm);
  251. case RTC_WKALM_RD:
  252. mutex_unlock(&rtc->ops_lock);
  253. err = rtc_read_alarm(rtc, &alarm);
  254. if (err < 0)
  255. return err;
  256. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  257. err = -EFAULT;
  258. return err;
  259. default:
  260. /* Finally try the driver's ioctl interface */
  261. if (ops->ioctl) {
  262. err = ops->ioctl(rtc->dev.parent, cmd, arg);
  263. if (err == -ENOIOCTLCMD)
  264. err = -ENOTTY;
  265. }
  266. break;
  267. }
  268. done:
  269. mutex_unlock(&rtc->ops_lock);
  270. return err;
  271. }
  272. static int rtc_dev_fasync(int fd, struct file *file, int on)
  273. {
  274. struct rtc_device *rtc = file->private_data;
  275. return fasync_helper(fd, file, on, &rtc->async_queue);
  276. }
  277. static int rtc_dev_release(struct inode *inode, struct file *file)
  278. {
  279. struct rtc_device *rtc = file->private_data;
  280. /* We shut down the repeating IRQs that userspace enabled,
  281. * since nothing is listening to them.
  282. * - Update (UIE) ... currently only managed through ioctls
  283. * - Periodic (PIE) ... also used through rtc_*() interface calls
  284. *
  285. * Leave the alarm alone; it may be set to trigger a system wakeup
  286. * later, or be used by kernel code, and is a one-shot event anyway.
  287. */
  288. /* Keep ioctl until all drivers are converted */
  289. rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
  290. rtc_update_irq_enable(rtc, 0);
  291. rtc_irq_set_state(rtc, NULL, 0);
  292. if (rtc->ops->release)
  293. rtc->ops->release(rtc->dev.parent);
  294. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  295. return 0;
  296. }
  297. static const struct file_operations rtc_dev_fops = {
  298. .owner = THIS_MODULE,
  299. .llseek = no_llseek,
  300. .read = rtc_dev_read,
  301. .poll = rtc_dev_poll,
  302. .unlocked_ioctl = rtc_dev_ioctl,
  303. .open = rtc_dev_open,
  304. .release = rtc_dev_release,
  305. .fasync = rtc_dev_fasync,
  306. };
  307. /* insertion/removal hooks */
  308. void rtc_dev_prepare(struct rtc_device *rtc)
  309. {
  310. if (!rtc_devt)
  311. return;
  312. if (rtc->id >= RTC_DEV_MAX) {
  313. pr_debug("%s: too many RTC devices\n", rtc->name);
  314. return;
  315. }
  316. rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
  317. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  318. rtc->char_dev.owner = rtc->owner;
  319. }
  320. void rtc_dev_add_device(struct rtc_device *rtc)
  321. {
  322. if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
  323. printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
  324. rtc->name, MAJOR(rtc_devt), rtc->id);
  325. else
  326. pr_debug("%s: dev (%d:%d)\n", rtc->name,
  327. MAJOR(rtc_devt), rtc->id);
  328. }
  329. void rtc_dev_del_device(struct rtc_device *rtc)
  330. {
  331. if (rtc->dev.devt)
  332. cdev_del(&rtc->char_dev);
  333. }
  334. void __init rtc_dev_init(void)
  335. {
  336. int err;
  337. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  338. if (err < 0)
  339. printk(KERN_ERR "%s: failed to allocate char dev region\n",
  340. __FILE__);
  341. }
  342. void __exit rtc_dev_exit(void)
  343. {
  344. if (rtc_devt)
  345. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  346. }