rtc-dev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 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. static ssize_t
  42. rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  43. {
  44. struct rtc_device *rtc = to_rtc_device(file->private_data);
  45. DECLARE_WAITQUEUE(wait, current);
  46. unsigned long data;
  47. ssize_t ret;
  48. if (count < sizeof(unsigned long))
  49. return -EINVAL;
  50. add_wait_queue(&rtc->irq_queue, &wait);
  51. do {
  52. __set_current_state(TASK_INTERRUPTIBLE);
  53. spin_lock_irq(&rtc->irq_lock);
  54. data = rtc->irq_data;
  55. rtc->irq_data = 0;
  56. spin_unlock_irq(&rtc->irq_lock);
  57. if (data != 0) {
  58. ret = 0;
  59. break;
  60. }
  61. if (file->f_flags & O_NONBLOCK) {
  62. ret = -EAGAIN;
  63. break;
  64. }
  65. if (signal_pending(current)) {
  66. ret = -ERESTARTSYS;
  67. break;
  68. }
  69. schedule();
  70. } while (1);
  71. set_current_state(TASK_RUNNING);
  72. remove_wait_queue(&rtc->irq_queue, &wait);
  73. if (ret == 0) {
  74. /* Check for any data updates */
  75. if (rtc->ops->read_callback)
  76. data = rtc->ops->read_callback(rtc->class_dev.dev, data);
  77. ret = put_user(data, (unsigned long __user *)buf);
  78. if (ret == 0)
  79. ret = sizeof(unsigned long);
  80. }
  81. return ret;
  82. }
  83. static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
  84. {
  85. struct rtc_device *rtc = to_rtc_device(file->private_data);
  86. unsigned long data;
  87. poll_wait(file, &rtc->irq_queue, wait);
  88. data = rtc->irq_data;
  89. return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
  90. }
  91. static int rtc_dev_ioctl(struct inode *inode, struct file *file,
  92. unsigned int cmd, unsigned long arg)
  93. {
  94. int err = 0;
  95. struct class_device *class_dev = file->private_data;
  96. struct rtc_device *rtc = to_rtc_device(class_dev);
  97. struct rtc_class_ops *ops = rtc->ops;
  98. struct rtc_time tm;
  99. struct rtc_wkalrm alarm;
  100. void __user *uarg = (void __user *) arg;
  101. /* avoid conflicting IRQ users */
  102. if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
  103. spin_lock(&rtc->irq_task_lock);
  104. if (rtc->irq_task)
  105. err = -EBUSY;
  106. spin_unlock(&rtc->irq_task_lock);
  107. if (err < 0)
  108. return err;
  109. }
  110. /* try the driver's ioctl interface */
  111. if (ops->ioctl) {
  112. err = ops->ioctl(class_dev->dev, cmd, arg);
  113. if (err != -EINVAL)
  114. return err;
  115. }
  116. /* if the driver does not provide the ioctl interface
  117. * or if that particular ioctl was not implemented
  118. * (-EINVAL), we will try to emulate here.
  119. */
  120. switch (cmd) {
  121. case RTC_ALM_READ:
  122. err = rtc_read_alarm(class_dev, &alarm);
  123. if (err < 0)
  124. return err;
  125. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  126. return -EFAULT;
  127. break;
  128. case RTC_ALM_SET:
  129. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  130. return -EFAULT;
  131. alarm.enabled = 0;
  132. alarm.pending = 0;
  133. alarm.time.tm_mday = -1;
  134. alarm.time.tm_mon = -1;
  135. alarm.time.tm_year = -1;
  136. alarm.time.tm_wday = -1;
  137. alarm.time.tm_yday = -1;
  138. alarm.time.tm_isdst = -1;
  139. err = rtc_set_alarm(class_dev, &alarm);
  140. break;
  141. case RTC_RD_TIME:
  142. err = rtc_read_time(class_dev, &tm);
  143. if (err < 0)
  144. return err;
  145. if (copy_to_user(uarg, &tm, sizeof(tm)))
  146. return -EFAULT;
  147. break;
  148. case RTC_SET_TIME:
  149. if (!capable(CAP_SYS_TIME))
  150. return -EACCES;
  151. if (copy_from_user(&tm, uarg, sizeof(tm)))
  152. return -EFAULT;
  153. err = rtc_set_time(class_dev, &tm);
  154. break;
  155. #if 0
  156. case RTC_EPOCH_SET:
  157. #ifndef rtc_epoch
  158. /*
  159. * There were no RTC clocks before 1900.
  160. */
  161. if (arg < 1900) {
  162. err = -EINVAL;
  163. break;
  164. }
  165. if (!capable(CAP_SYS_TIME)) {
  166. err = -EACCES;
  167. break;
  168. }
  169. rtc_epoch = arg;
  170. err = 0;
  171. #endif
  172. break;
  173. case RTC_EPOCH_READ:
  174. err = put_user(rtc_epoch, (unsigned long __user *)uarg);
  175. break;
  176. #endif
  177. case RTC_WKALM_SET:
  178. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  179. return -EFAULT;
  180. err = rtc_set_alarm(class_dev, &alarm);
  181. break;
  182. case RTC_WKALM_RD:
  183. err = rtc_read_alarm(class_dev, &alarm);
  184. if (err < 0)
  185. return err;
  186. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  187. return -EFAULT;
  188. break;
  189. default:
  190. err = -EINVAL;
  191. break;
  192. }
  193. return err;
  194. }
  195. static int rtc_dev_release(struct inode *inode, struct file *file)
  196. {
  197. struct rtc_device *rtc = to_rtc_device(file->private_data);
  198. if (rtc->ops->release)
  199. rtc->ops->release(rtc->class_dev.dev);
  200. mutex_unlock(&rtc->char_lock);
  201. return 0;
  202. }
  203. static int rtc_dev_fasync(int fd, struct file *file, int on)
  204. {
  205. struct rtc_device *rtc = to_rtc_device(file->private_data);
  206. return fasync_helper(fd, file, on, &rtc->async_queue);
  207. }
  208. static struct file_operations rtc_dev_fops = {
  209. .owner = THIS_MODULE,
  210. .llseek = no_llseek,
  211. .read = rtc_dev_read,
  212. .poll = rtc_dev_poll,
  213. .ioctl = rtc_dev_ioctl,
  214. .open = rtc_dev_open,
  215. .release = rtc_dev_release,
  216. .fasync = rtc_dev_fasync,
  217. };
  218. /* insertion/removal hooks */
  219. static int rtc_dev_add_device(struct class_device *class_dev,
  220. struct class_interface *class_intf)
  221. {
  222. int err = 0;
  223. struct rtc_device *rtc = to_rtc_device(class_dev);
  224. if (rtc->id >= RTC_DEV_MAX) {
  225. dev_err(class_dev->dev, "too many RTCs\n");
  226. return -EINVAL;
  227. }
  228. mutex_init(&rtc->char_lock);
  229. spin_lock_init(&rtc->irq_lock);
  230. init_waitqueue_head(&rtc->irq_queue);
  231. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  232. rtc->char_dev.owner = rtc->owner;
  233. if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
  234. cdev_del(&rtc->char_dev);
  235. dev_err(class_dev->dev,
  236. "failed to add char device %d:%d\n",
  237. MAJOR(rtc_devt), rtc->id);
  238. return -ENODEV;
  239. }
  240. rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
  241. MKDEV(MAJOR(rtc_devt), rtc->id),
  242. class_dev->dev, "rtc%d", rtc->id);
  243. if (IS_ERR(rtc->rtc_dev)) {
  244. dev_err(class_dev->dev, "cannot create rtc_dev device\n");
  245. err = PTR_ERR(rtc->rtc_dev);
  246. goto err_cdev_del;
  247. }
  248. dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
  249. MAJOR(rtc->rtc_dev->devt),
  250. MINOR(rtc->rtc_dev->devt));
  251. return 0;
  252. err_cdev_del:
  253. cdev_del(&rtc->char_dev);
  254. return err;
  255. }
  256. static void rtc_dev_remove_device(struct class_device *class_dev,
  257. struct class_interface *class_intf)
  258. {
  259. struct rtc_device *rtc = to_rtc_device(class_dev);
  260. if (rtc->rtc_dev) {
  261. dev_dbg(class_dev->dev, "removing char %d:%d\n",
  262. MAJOR(rtc->rtc_dev->devt),
  263. MINOR(rtc->rtc_dev->devt));
  264. class_device_unregister(rtc->rtc_dev);
  265. cdev_del(&rtc->char_dev);
  266. }
  267. }
  268. /* interface registration */
  269. static struct class_interface rtc_dev_interface = {
  270. .add = &rtc_dev_add_device,
  271. .remove = &rtc_dev_remove_device,
  272. };
  273. static int __init rtc_dev_init(void)
  274. {
  275. int err;
  276. rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
  277. if (IS_ERR(rtc_dev_class))
  278. return PTR_ERR(rtc_dev_class);
  279. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  280. if (err < 0) {
  281. printk(KERN_ERR "%s: failed to allocate char dev region\n",
  282. __FILE__);
  283. goto err_destroy_class;
  284. }
  285. err = rtc_interface_register(&rtc_dev_interface);
  286. if (err < 0) {
  287. printk(KERN_ERR "%s: failed to register the interface\n",
  288. __FILE__);
  289. goto err_unregister_chrdev;
  290. }
  291. return 0;
  292. err_unregister_chrdev:
  293. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  294. err_destroy_class:
  295. class_destroy(rtc_dev_class);
  296. return err;
  297. }
  298. static void __exit rtc_dev_exit(void)
  299. {
  300. class_interface_unregister(&rtc_dev_interface);
  301. class_destroy(rtc_dev_class);
  302. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  303. }
  304. module_init(rtc_dev_init);
  305. module_exit(rtc_dev_exit);
  306. MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
  307. MODULE_DESCRIPTION("RTC class dev interface");
  308. MODULE_LICENSE("GPL");