rtc-dev.c 12 KB

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