rtc-dev.c 12 KB

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