rtc-dev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/module.h>
  15. #include <linux/rtc.h>
  16. #include <linux/sched/signal.h>
  17. #include "rtc-core.h"
  18. static dev_t rtc_devt;
  19. #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
  20. static int rtc_dev_open(struct inode *inode, struct file *file)
  21. {
  22. struct rtc_device *rtc = container_of(inode->i_cdev,
  23. struct rtc_device, char_dev);
  24. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  25. return -EBUSY;
  26. file->private_data = rtc;
  27. spin_lock_irq(&rtc->irq_lock);
  28. rtc->irq_data = 0;
  29. spin_unlock_irq(&rtc->irq_lock);
  30. return 0;
  31. }
  32. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  33. /*
  34. * Routine to poll RTC seconds field for change as often as possible,
  35. * after first RTC_UIE use timer to reduce polling
  36. */
  37. static void rtc_uie_task(struct work_struct *work)
  38. {
  39. struct rtc_device *rtc =
  40. container_of(work, struct rtc_device, uie_task);
  41. struct rtc_time tm;
  42. int num = 0;
  43. int err;
  44. err = rtc_read_time(rtc, &tm);
  45. spin_lock_irq(&rtc->irq_lock);
  46. if (rtc->stop_uie_polling || err) {
  47. rtc->uie_task_active = 0;
  48. } else if (rtc->oldsecs != tm.tm_sec) {
  49. num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
  50. rtc->oldsecs = tm.tm_sec;
  51. rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
  52. rtc->uie_timer_active = 1;
  53. rtc->uie_task_active = 0;
  54. add_timer(&rtc->uie_timer);
  55. } else if (schedule_work(&rtc->uie_task) == 0) {
  56. rtc->uie_task_active = 0;
  57. }
  58. spin_unlock_irq(&rtc->irq_lock);
  59. if (num)
  60. rtc_handle_legacy_irq(rtc, num, RTC_UF);
  61. }
  62. static void rtc_uie_timer(struct timer_list *t)
  63. {
  64. struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
  65. unsigned long flags;
  66. spin_lock_irqsave(&rtc->irq_lock, flags);
  67. rtc->uie_timer_active = 0;
  68. rtc->uie_task_active = 1;
  69. if ((schedule_work(&rtc->uie_task) == 0))
  70. rtc->uie_task_active = 0;
  71. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  72. }
  73. static int clear_uie(struct rtc_device *rtc)
  74. {
  75. spin_lock_irq(&rtc->irq_lock);
  76. if (rtc->uie_irq_active) {
  77. rtc->stop_uie_polling = 1;
  78. if (rtc->uie_timer_active) {
  79. spin_unlock_irq(&rtc->irq_lock);
  80. del_timer_sync(&rtc->uie_timer);
  81. spin_lock_irq(&rtc->irq_lock);
  82. rtc->uie_timer_active = 0;
  83. }
  84. if (rtc->uie_task_active) {
  85. spin_unlock_irq(&rtc->irq_lock);
  86. flush_scheduled_work();
  87. spin_lock_irq(&rtc->irq_lock);
  88. }
  89. rtc->uie_irq_active = 0;
  90. }
  91. spin_unlock_irq(&rtc->irq_lock);
  92. return 0;
  93. }
  94. static int set_uie(struct rtc_device *rtc)
  95. {
  96. struct rtc_time tm;
  97. int err;
  98. err = rtc_read_time(rtc, &tm);
  99. if (err)
  100. return err;
  101. spin_lock_irq(&rtc->irq_lock);
  102. if (!rtc->uie_irq_active) {
  103. rtc->uie_irq_active = 1;
  104. rtc->stop_uie_polling = 0;
  105. rtc->oldsecs = tm.tm_sec;
  106. rtc->uie_task_active = 1;
  107. if (schedule_work(&rtc->uie_task) == 0)
  108. rtc->uie_task_active = 0;
  109. }
  110. rtc->irq_data = 0;
  111. spin_unlock_irq(&rtc->irq_lock);
  112. return 0;
  113. }
  114. int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
  115. {
  116. if (enabled)
  117. return set_uie(rtc);
  118. else
  119. return clear_uie(rtc);
  120. }
  121. EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
  122. #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
  123. static ssize_t
  124. rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  125. {
  126. struct rtc_device *rtc = file->private_data;
  127. DECLARE_WAITQUEUE(wait, current);
  128. unsigned long data;
  129. ssize_t ret;
  130. if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
  131. return -EINVAL;
  132. add_wait_queue(&rtc->irq_queue, &wait);
  133. do {
  134. __set_current_state(TASK_INTERRUPTIBLE);
  135. spin_lock_irq(&rtc->irq_lock);
  136. data = rtc->irq_data;
  137. rtc->irq_data = 0;
  138. spin_unlock_irq(&rtc->irq_lock);
  139. if (data != 0) {
  140. ret = 0;
  141. break;
  142. }
  143. if (file->f_flags & O_NONBLOCK) {
  144. ret = -EAGAIN;
  145. break;
  146. }
  147. if (signal_pending(current)) {
  148. ret = -ERESTARTSYS;
  149. break;
  150. }
  151. schedule();
  152. } while (1);
  153. set_current_state(TASK_RUNNING);
  154. remove_wait_queue(&rtc->irq_queue, &wait);
  155. if (ret == 0) {
  156. /* Check for any data updates */
  157. if (rtc->ops->read_callback)
  158. data = rtc->ops->read_callback(rtc->dev.parent,
  159. data);
  160. if (sizeof(int) != sizeof(long) &&
  161. count == sizeof(unsigned int))
  162. ret = put_user(data, (unsigned int __user *)buf) ?:
  163. sizeof(unsigned int);
  164. else
  165. ret = put_user(data, (unsigned long __user *)buf) ?:
  166. sizeof(unsigned long);
  167. }
  168. return ret;
  169. }
  170. static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
  171. {
  172. struct rtc_device *rtc = file->private_data;
  173. unsigned long data;
  174. poll_wait(file, &rtc->irq_queue, wait);
  175. data = rtc->irq_data;
  176. return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
  177. }
  178. static long rtc_dev_ioctl(struct file *file,
  179. unsigned int cmd, unsigned long arg)
  180. {
  181. int err = 0;
  182. struct rtc_device *rtc = file->private_data;
  183. const struct rtc_class_ops *ops = rtc->ops;
  184. struct rtc_time tm;
  185. struct rtc_wkalrm alarm;
  186. void __user *uarg = (void __user *) arg;
  187. err = mutex_lock_interruptible(&rtc->ops_lock);
  188. if (err)
  189. return err;
  190. /* check that the calling task has appropriate permissions
  191. * for certain ioctls. doing this check here is useful
  192. * to avoid duplicate code in each driver.
  193. */
  194. switch (cmd) {
  195. case RTC_EPOCH_SET:
  196. case RTC_SET_TIME:
  197. if (!capable(CAP_SYS_TIME))
  198. err = -EACCES;
  199. break;
  200. case RTC_IRQP_SET:
  201. if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
  202. err = -EACCES;
  203. break;
  204. case RTC_PIE_ON:
  205. if (rtc->irq_freq > rtc->max_user_freq &&
  206. !capable(CAP_SYS_RESOURCE))
  207. err = -EACCES;
  208. break;
  209. }
  210. if (err)
  211. goto done;
  212. /*
  213. * Drivers *SHOULD NOT* provide ioctl implementations
  214. * for these requests. Instead, provide methods to
  215. * support the following code, so that the RTC's main
  216. * features are accessible without using ioctls.
  217. *
  218. * RTC and alarm times will be in UTC, by preference,
  219. * but dual-booting with MS-Windows implies RTCs must
  220. * use the local wall clock time.
  221. */
  222. switch (cmd) {
  223. case RTC_ALM_READ:
  224. mutex_unlock(&rtc->ops_lock);
  225. err = rtc_read_alarm(rtc, &alarm);
  226. if (err < 0)
  227. return err;
  228. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  229. err = -EFAULT;
  230. return err;
  231. case RTC_ALM_SET:
  232. mutex_unlock(&rtc->ops_lock);
  233. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  234. return -EFAULT;
  235. alarm.enabled = 0;
  236. alarm.pending = 0;
  237. alarm.time.tm_wday = -1;
  238. alarm.time.tm_yday = -1;
  239. alarm.time.tm_isdst = -1;
  240. /* RTC_ALM_SET alarms may be up to 24 hours in the future.
  241. * Rather than expecting every RTC to implement "don't care"
  242. * for day/month/year fields, just force the alarm to have
  243. * the right values for those fields.
  244. *
  245. * RTC_WKALM_SET should be used instead. Not only does it
  246. * eliminate the need for a separate RTC_AIE_ON call, it
  247. * doesn't have the "alarm 23:59:59 in the future" race.
  248. *
  249. * NOTE: some legacy code may have used invalid fields as
  250. * wildcards, exposing hardware "periodic alarm" capabilities.
  251. * Not supported here.
  252. */
  253. {
  254. time64_t now, then;
  255. err = rtc_read_time(rtc, &tm);
  256. if (err < 0)
  257. return err;
  258. now = rtc_tm_to_time64(&tm);
  259. alarm.time.tm_mday = tm.tm_mday;
  260. alarm.time.tm_mon = tm.tm_mon;
  261. alarm.time.tm_year = tm.tm_year;
  262. err = rtc_valid_tm(&alarm.time);
  263. if (err < 0)
  264. return err;
  265. then = rtc_tm_to_time64(&alarm.time);
  266. /* alarm may need to wrap into tomorrow */
  267. if (then < now) {
  268. rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
  269. alarm.time.tm_mday = tm.tm_mday;
  270. alarm.time.tm_mon = tm.tm_mon;
  271. alarm.time.tm_year = tm.tm_year;
  272. }
  273. }
  274. return rtc_set_alarm(rtc, &alarm);
  275. case RTC_RD_TIME:
  276. mutex_unlock(&rtc->ops_lock);
  277. err = rtc_read_time(rtc, &tm);
  278. if (err < 0)
  279. return err;
  280. if (copy_to_user(uarg, &tm, sizeof(tm)))
  281. err = -EFAULT;
  282. return err;
  283. case RTC_SET_TIME:
  284. mutex_unlock(&rtc->ops_lock);
  285. if (copy_from_user(&tm, uarg, sizeof(tm)))
  286. return -EFAULT;
  287. return rtc_set_time(rtc, &tm);
  288. case RTC_PIE_ON:
  289. err = rtc_irq_set_state(rtc, 1);
  290. break;
  291. case RTC_PIE_OFF:
  292. err = rtc_irq_set_state(rtc, 0);
  293. break;
  294. case RTC_AIE_ON:
  295. mutex_unlock(&rtc->ops_lock);
  296. return rtc_alarm_irq_enable(rtc, 1);
  297. case RTC_AIE_OFF:
  298. mutex_unlock(&rtc->ops_lock);
  299. return rtc_alarm_irq_enable(rtc, 0);
  300. case RTC_UIE_ON:
  301. mutex_unlock(&rtc->ops_lock);
  302. return rtc_update_irq_enable(rtc, 1);
  303. case RTC_UIE_OFF:
  304. mutex_unlock(&rtc->ops_lock);
  305. return rtc_update_irq_enable(rtc, 0);
  306. case RTC_IRQP_SET:
  307. err = rtc_irq_set_freq(rtc, arg);
  308. break;
  309. case RTC_IRQP_READ:
  310. err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
  311. break;
  312. case RTC_WKALM_SET:
  313. mutex_unlock(&rtc->ops_lock);
  314. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  315. return -EFAULT;
  316. return rtc_set_alarm(rtc, &alarm);
  317. case RTC_WKALM_RD:
  318. mutex_unlock(&rtc->ops_lock);
  319. err = rtc_read_alarm(rtc, &alarm);
  320. if (err < 0)
  321. return err;
  322. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  323. err = -EFAULT;
  324. return err;
  325. default:
  326. /* Finally try the driver's ioctl interface */
  327. if (ops->ioctl) {
  328. err = ops->ioctl(rtc->dev.parent, cmd, arg);
  329. if (err == -ENOIOCTLCMD)
  330. err = -ENOTTY;
  331. } else
  332. err = -ENOTTY;
  333. break;
  334. }
  335. done:
  336. mutex_unlock(&rtc->ops_lock);
  337. return err;
  338. }
  339. static int rtc_dev_fasync(int fd, struct file *file, int on)
  340. {
  341. struct rtc_device *rtc = file->private_data;
  342. return fasync_helper(fd, file, on, &rtc->async_queue);
  343. }
  344. static int rtc_dev_release(struct inode *inode, struct file *file)
  345. {
  346. struct rtc_device *rtc = file->private_data;
  347. /* We shut down the repeating IRQs that userspace enabled,
  348. * since nothing is listening to them.
  349. * - Update (UIE) ... currently only managed through ioctls
  350. * - Periodic (PIE) ... also used through rtc_*() interface calls
  351. *
  352. * Leave the alarm alone; it may be set to trigger a system wakeup
  353. * later, or be used by kernel code, and is a one-shot event anyway.
  354. */
  355. /* Keep ioctl until all drivers are converted */
  356. rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
  357. rtc_update_irq_enable(rtc, 0);
  358. rtc_irq_set_state(rtc, 0);
  359. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  360. return 0;
  361. }
  362. static const struct file_operations rtc_dev_fops = {
  363. .owner = THIS_MODULE,
  364. .llseek = no_llseek,
  365. .read = rtc_dev_read,
  366. .poll = rtc_dev_poll,
  367. .unlocked_ioctl = rtc_dev_ioctl,
  368. .open = rtc_dev_open,
  369. .release = rtc_dev_release,
  370. .fasync = rtc_dev_fasync,
  371. };
  372. /* insertion/removal hooks */
  373. void rtc_dev_prepare(struct rtc_device *rtc)
  374. {
  375. if (!rtc_devt)
  376. return;
  377. if (rtc->id >= RTC_DEV_MAX) {
  378. dev_dbg(&rtc->dev, "too many RTC devices\n");
  379. return;
  380. }
  381. rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
  382. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  383. INIT_WORK(&rtc->uie_task, rtc_uie_task);
  384. timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
  385. #endif
  386. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  387. rtc->char_dev.owner = rtc->owner;
  388. }
  389. void __init rtc_dev_init(void)
  390. {
  391. int err;
  392. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  393. if (err < 0)
  394. pr_err("failed to allocate char dev region\n");
  395. }
  396. void __exit rtc_dev_exit(void)
  397. {
  398. if (rtc_devt)
  399. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  400. }