rtc-dev.c 12 KB

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