rtc-dev.c 12 KB

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