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