rtc-dev.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /* try the driver's ioctl interface */
  129. if (ops->ioctl) {
  130. err = ops->ioctl(rtc->dev.parent, cmd, arg);
  131. if (err != -ENOIOCTLCMD) {
  132. mutex_unlock(&rtc->ops_lock);
  133. return err;
  134. }
  135. }
  136. /* if the driver does not provide the ioctl interface
  137. * or if that particular ioctl was not implemented
  138. * (-ENOIOCTLCMD), we will try to emulate here.
  139. *
  140. * Drivers *SHOULD NOT* provide ioctl implementations
  141. * for these requests. Instead, provide methods to
  142. * support the following code, so that the RTC's main
  143. * features are accessible without using ioctls.
  144. *
  145. * RTC and alarm times will be in UTC, by preference,
  146. * but dual-booting with MS-Windows implies RTCs must
  147. * use the local wall clock time.
  148. */
  149. switch (cmd) {
  150. case RTC_ALM_READ:
  151. mutex_unlock(&rtc->ops_lock);
  152. err = rtc_read_alarm(rtc, &alarm);
  153. if (err < 0)
  154. return err;
  155. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  156. err = -EFAULT;
  157. return err;
  158. case RTC_ALM_SET:
  159. mutex_unlock(&rtc->ops_lock);
  160. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  161. return -EFAULT;
  162. alarm.enabled = 0;
  163. alarm.pending = 0;
  164. alarm.time.tm_wday = -1;
  165. alarm.time.tm_yday = -1;
  166. alarm.time.tm_isdst = -1;
  167. /* RTC_ALM_SET alarms may be up to 24 hours in the future.
  168. * Rather than expecting every RTC to implement "don't care"
  169. * for day/month/year fields, just force the alarm to have
  170. * the right values for those fields.
  171. *
  172. * RTC_WKALM_SET should be used instead. Not only does it
  173. * eliminate the need for a separate RTC_AIE_ON call, it
  174. * doesn't have the "alarm 23:59:59 in the future" race.
  175. *
  176. * NOTE: some legacy code may have used invalid fields as
  177. * wildcards, exposing hardware "periodic alarm" capabilities.
  178. * Not supported here.
  179. */
  180. {
  181. unsigned long now, then;
  182. err = rtc_read_time(rtc, &tm);
  183. if (err < 0)
  184. return err;
  185. rtc_tm_to_time(&tm, &now);
  186. alarm.time.tm_mday = tm.tm_mday;
  187. alarm.time.tm_mon = tm.tm_mon;
  188. alarm.time.tm_year = tm.tm_year;
  189. err = rtc_valid_tm(&alarm.time);
  190. if (err < 0)
  191. return err;
  192. rtc_tm_to_time(&alarm.time, &then);
  193. /* alarm may need to wrap into tomorrow */
  194. if (then < now) {
  195. rtc_time_to_tm(now + 24 * 60 * 60, &tm);
  196. alarm.time.tm_mday = tm.tm_mday;
  197. alarm.time.tm_mon = tm.tm_mon;
  198. alarm.time.tm_year = tm.tm_year;
  199. }
  200. }
  201. return rtc_set_alarm(rtc, &alarm);
  202. case RTC_RD_TIME:
  203. mutex_unlock(&rtc->ops_lock);
  204. err = rtc_read_time(rtc, &tm);
  205. if (err < 0)
  206. return err;
  207. if (copy_to_user(uarg, &tm, sizeof(tm)))
  208. err = -EFAULT;
  209. return err;
  210. case RTC_SET_TIME:
  211. mutex_unlock(&rtc->ops_lock);
  212. if (copy_from_user(&tm, uarg, sizeof(tm)))
  213. return -EFAULT;
  214. return rtc_set_time(rtc, &tm);
  215. case RTC_PIE_ON:
  216. err = rtc_irq_set_state(rtc, NULL, 1);
  217. break;
  218. case RTC_PIE_OFF:
  219. err = rtc_irq_set_state(rtc, NULL, 0);
  220. break;
  221. case RTC_AIE_ON:
  222. mutex_unlock(&rtc->ops_lock);
  223. return rtc_alarm_irq_enable(rtc, 1);
  224. case RTC_AIE_OFF:
  225. mutex_unlock(&rtc->ops_lock);
  226. return rtc_alarm_irq_enable(rtc, 0);
  227. case RTC_UIE_ON:
  228. mutex_unlock(&rtc->ops_lock);
  229. return rtc_update_irq_enable(rtc, 1);
  230. case RTC_UIE_OFF:
  231. mutex_unlock(&rtc->ops_lock);
  232. return rtc_update_irq_enable(rtc, 0);
  233. case RTC_IRQP_SET:
  234. err = rtc_irq_set_freq(rtc, NULL, arg);
  235. break;
  236. case RTC_IRQP_READ:
  237. err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
  238. break;
  239. #if 0
  240. case RTC_EPOCH_SET:
  241. #ifndef rtc_epoch
  242. /*
  243. * There were no RTC clocks before 1900.
  244. */
  245. if (arg < 1900) {
  246. err = -EINVAL;
  247. break;
  248. }
  249. rtc_epoch = arg;
  250. err = 0;
  251. #endif
  252. break;
  253. case RTC_EPOCH_READ:
  254. err = put_user(rtc_epoch, (unsigned long __user *)uarg);
  255. break;
  256. #endif
  257. case RTC_WKALM_SET:
  258. mutex_unlock(&rtc->ops_lock);
  259. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  260. return -EFAULT;
  261. return rtc_set_alarm(rtc, &alarm);
  262. case RTC_WKALM_RD:
  263. mutex_unlock(&rtc->ops_lock);
  264. err = rtc_read_alarm(rtc, &alarm);
  265. if (err < 0)
  266. return err;
  267. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  268. err = -EFAULT;
  269. return err;
  270. default:
  271. err = -ENOTTY;
  272. break;
  273. }
  274. done:
  275. mutex_unlock(&rtc->ops_lock);
  276. return err;
  277. }
  278. static int rtc_dev_fasync(int fd, struct file *file, int on)
  279. {
  280. struct rtc_device *rtc = file->private_data;
  281. return fasync_helper(fd, file, on, &rtc->async_queue);
  282. }
  283. static int rtc_dev_release(struct inode *inode, struct file *file)
  284. {
  285. struct rtc_device *rtc = file->private_data;
  286. /* We shut down the repeating IRQs that userspace enabled,
  287. * since nothing is listening to them.
  288. * - Update (UIE) ... currently only managed through ioctls
  289. * - Periodic (PIE) ... also used through rtc_*() interface calls
  290. *
  291. * Leave the alarm alone; it may be set to trigger a system wakeup
  292. * later, or be used by kernel code, and is a one-shot event anyway.
  293. */
  294. /* Keep ioctl until all drivers are converted */
  295. rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
  296. rtc_update_irq_enable(rtc, 0);
  297. rtc_irq_set_state(rtc, NULL, 0);
  298. if (rtc->ops->release)
  299. rtc->ops->release(rtc->dev.parent);
  300. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  301. return 0;
  302. }
  303. static const struct file_operations rtc_dev_fops = {
  304. .owner = THIS_MODULE,
  305. .llseek = no_llseek,
  306. .read = rtc_dev_read,
  307. .poll = rtc_dev_poll,
  308. .unlocked_ioctl = rtc_dev_ioctl,
  309. .open = rtc_dev_open,
  310. .release = rtc_dev_release,
  311. .fasync = rtc_dev_fasync,
  312. };
  313. /* insertion/removal hooks */
  314. void rtc_dev_prepare(struct rtc_device *rtc)
  315. {
  316. if (!rtc_devt)
  317. return;
  318. if (rtc->id >= RTC_DEV_MAX) {
  319. pr_debug("%s: too many RTC devices\n", rtc->name);
  320. return;
  321. }
  322. rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
  323. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  324. rtc->char_dev.owner = rtc->owner;
  325. }
  326. void rtc_dev_add_device(struct rtc_device *rtc)
  327. {
  328. if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
  329. printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
  330. rtc->name, MAJOR(rtc_devt), rtc->id);
  331. else
  332. pr_debug("%s: dev (%d:%d)\n", rtc->name,
  333. MAJOR(rtc_devt), rtc->id);
  334. }
  335. void rtc_dev_del_device(struct rtc_device *rtc)
  336. {
  337. if (rtc->dev.devt)
  338. cdev_del(&rtc->char_dev);
  339. }
  340. void __init rtc_dev_init(void)
  341. {
  342. int err;
  343. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  344. if (err < 0)
  345. printk(KERN_ERR "%s: failed to allocate char dev region\n",
  346. __FILE__);
  347. }
  348. void __exit rtc_dev_exit(void)
  349. {
  350. if (rtc_devt)
  351. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  352. }