rtc-dev.c 11 KB

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