rtc-dev.c 12 KB

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