rtc-dev.c 8.4 KB

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