timerdev.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. *
  3. * general timer device for using in ISDN stacks
  4. *
  5. * Author Karsten Keil <kkeil@novell.com>
  6. *
  7. * Copyright 2008 by Karsten Keil <kkeil@novell.com>
  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. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/poll.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/slab.h>
  22. #include <linux/timer.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/module.h>
  25. #include <linux/mISDNif.h>
  26. #include <linux/mutex.h>
  27. #include <linux/sched/signal.h>
  28. #include "core.h"
  29. static DEFINE_MUTEX(mISDN_mutex);
  30. static u_int *debug;
  31. struct mISDNtimerdev {
  32. int next_id;
  33. struct list_head pending;
  34. struct list_head expired;
  35. wait_queue_head_t wait;
  36. u_int work;
  37. spinlock_t lock; /* protect lists */
  38. };
  39. struct mISDNtimer {
  40. struct list_head list;
  41. struct mISDNtimerdev *dev;
  42. struct timer_list tl;
  43. int id;
  44. };
  45. static int
  46. mISDN_open(struct inode *ino, struct file *filep)
  47. {
  48. struct mISDNtimerdev *dev;
  49. if (*debug & DEBUG_TIMER)
  50. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  51. dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
  52. if (!dev)
  53. return -ENOMEM;
  54. dev->next_id = 1;
  55. INIT_LIST_HEAD(&dev->pending);
  56. INIT_LIST_HEAD(&dev->expired);
  57. spin_lock_init(&dev->lock);
  58. dev->work = 0;
  59. init_waitqueue_head(&dev->wait);
  60. filep->private_data = dev;
  61. return nonseekable_open(ino, filep);
  62. }
  63. static int
  64. mISDN_close(struct inode *ino, struct file *filep)
  65. {
  66. struct mISDNtimerdev *dev = filep->private_data;
  67. struct list_head *list = &dev->pending;
  68. struct mISDNtimer *timer, *next;
  69. if (*debug & DEBUG_TIMER)
  70. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  71. spin_lock_irq(&dev->lock);
  72. while (!list_empty(list)) {
  73. timer = list_first_entry(list, struct mISDNtimer, list);
  74. spin_unlock_irq(&dev->lock);
  75. del_timer_sync(&timer->tl);
  76. spin_lock_irq(&dev->lock);
  77. /* it might have been moved to ->expired */
  78. list_del(&timer->list);
  79. kfree(timer);
  80. }
  81. spin_unlock_irq(&dev->lock);
  82. list_for_each_entry_safe(timer, next, &dev->expired, list) {
  83. kfree(timer);
  84. }
  85. kfree(dev);
  86. return 0;
  87. }
  88. static ssize_t
  89. mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
  90. {
  91. struct mISDNtimerdev *dev = filep->private_data;
  92. struct list_head *list = &dev->expired;
  93. struct mISDNtimer *timer;
  94. int ret = 0;
  95. if (*debug & DEBUG_TIMER)
  96. printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
  97. filep, buf, (int)count, off);
  98. if (count < sizeof(int))
  99. return -ENOSPC;
  100. spin_lock_irq(&dev->lock);
  101. while (list_empty(list) && (dev->work == 0)) {
  102. spin_unlock_irq(&dev->lock);
  103. if (filep->f_flags & O_NONBLOCK)
  104. return -EAGAIN;
  105. wait_event_interruptible(dev->wait, (dev->work ||
  106. !list_empty(list)));
  107. if (signal_pending(current))
  108. return -ERESTARTSYS;
  109. spin_lock_irq(&dev->lock);
  110. }
  111. if (dev->work)
  112. dev->work = 0;
  113. if (!list_empty(list)) {
  114. timer = list_first_entry(list, struct mISDNtimer, list);
  115. list_del(&timer->list);
  116. spin_unlock_irq(&dev->lock);
  117. if (put_user(timer->id, (int __user *)buf))
  118. ret = -EFAULT;
  119. else
  120. ret = sizeof(int);
  121. kfree(timer);
  122. } else {
  123. spin_unlock_irq(&dev->lock);
  124. }
  125. return ret;
  126. }
  127. static __poll_t
  128. mISDN_poll(struct file *filep, poll_table *wait)
  129. {
  130. struct mISDNtimerdev *dev = filep->private_data;
  131. __poll_t mask = EPOLLERR;
  132. if (*debug & DEBUG_TIMER)
  133. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  134. if (dev) {
  135. poll_wait(filep, &dev->wait, wait);
  136. mask = 0;
  137. if (dev->work || !list_empty(&dev->expired))
  138. mask |= (EPOLLIN | EPOLLRDNORM);
  139. if (*debug & DEBUG_TIMER)
  140. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  141. dev->work, list_empty(&dev->expired));
  142. }
  143. return mask;
  144. }
  145. static void
  146. dev_expire_timer(struct timer_list *t)
  147. {
  148. struct mISDNtimer *timer = from_timer(timer, t, tl);
  149. u_long flags;
  150. spin_lock_irqsave(&timer->dev->lock, flags);
  151. if (timer->id >= 0)
  152. list_move_tail(&timer->list, &timer->dev->expired);
  153. wake_up_interruptible(&timer->dev->wait);
  154. spin_unlock_irqrestore(&timer->dev->lock, flags);
  155. }
  156. static int
  157. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  158. {
  159. int id;
  160. struct mISDNtimer *timer;
  161. if (!timeout) {
  162. dev->work = 1;
  163. wake_up_interruptible(&dev->wait);
  164. id = 0;
  165. } else {
  166. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  167. if (!timer)
  168. return -ENOMEM;
  169. timer->dev = dev;
  170. timer_setup(&timer->tl, dev_expire_timer, 0);
  171. spin_lock_irq(&dev->lock);
  172. id = timer->id = dev->next_id++;
  173. if (dev->next_id < 0)
  174. dev->next_id = 1;
  175. list_add_tail(&timer->list, &dev->pending);
  176. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  177. add_timer(&timer->tl);
  178. spin_unlock_irq(&dev->lock);
  179. }
  180. return id;
  181. }
  182. static int
  183. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  184. {
  185. struct mISDNtimer *timer;
  186. spin_lock_irq(&dev->lock);
  187. list_for_each_entry(timer, &dev->pending, list) {
  188. if (timer->id == id) {
  189. list_del_init(&timer->list);
  190. timer->id = -1;
  191. spin_unlock_irq(&dev->lock);
  192. del_timer_sync(&timer->tl);
  193. kfree(timer);
  194. return id;
  195. }
  196. }
  197. spin_unlock_irq(&dev->lock);
  198. return 0;
  199. }
  200. static long
  201. mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  202. {
  203. struct mISDNtimerdev *dev = filep->private_data;
  204. int id, tout, ret = 0;
  205. if (*debug & DEBUG_TIMER)
  206. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  207. filep, cmd, arg);
  208. mutex_lock(&mISDN_mutex);
  209. switch (cmd) {
  210. case IMADDTIMER:
  211. if (get_user(tout, (int __user *)arg)) {
  212. ret = -EFAULT;
  213. break;
  214. }
  215. id = misdn_add_timer(dev, tout);
  216. if (*debug & DEBUG_TIMER)
  217. printk(KERN_DEBUG "%s add %d id %d\n", __func__,
  218. tout, id);
  219. if (id < 0) {
  220. ret = id;
  221. break;
  222. }
  223. if (put_user(id, (int __user *)arg))
  224. ret = -EFAULT;
  225. break;
  226. case IMDELTIMER:
  227. if (get_user(id, (int __user *)arg)) {
  228. ret = -EFAULT;
  229. break;
  230. }
  231. if (*debug & DEBUG_TIMER)
  232. printk(KERN_DEBUG "%s del id %d\n", __func__, id);
  233. id = misdn_del_timer(dev, id);
  234. if (put_user(id, (int __user *)arg))
  235. ret = -EFAULT;
  236. break;
  237. default:
  238. ret = -EINVAL;
  239. }
  240. mutex_unlock(&mISDN_mutex);
  241. return ret;
  242. }
  243. static const struct file_operations mISDN_fops = {
  244. .owner = THIS_MODULE,
  245. .read = mISDN_read,
  246. .poll = mISDN_poll,
  247. .unlocked_ioctl = mISDN_ioctl,
  248. .open = mISDN_open,
  249. .release = mISDN_close,
  250. .llseek = no_llseek,
  251. };
  252. static struct miscdevice mISDNtimer = {
  253. .minor = MISC_DYNAMIC_MINOR,
  254. .name = "mISDNtimer",
  255. .fops = &mISDN_fops,
  256. };
  257. int
  258. mISDN_inittimer(u_int *deb)
  259. {
  260. int err;
  261. debug = deb;
  262. err = misc_register(&mISDNtimer);
  263. if (err)
  264. printk(KERN_WARNING "mISDN: Could not register timer device\n");
  265. return err;
  266. }
  267. void mISDN_timer_cleanup(void)
  268. {
  269. misc_deregister(&mISDNtimer);
  270. }