timerdev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "core.h"
  28. static DEFINE_MUTEX(mISDN_mutex);
  29. static u_int *debug;
  30. struct mISDNtimerdev {
  31. int next_id;
  32. struct list_head pending;
  33. struct list_head expired;
  34. wait_queue_head_t wait;
  35. u_int work;
  36. spinlock_t lock; /* protect lists */
  37. };
  38. struct mISDNtimer {
  39. struct list_head list;
  40. struct mISDNtimerdev *dev;
  41. struct timer_list tl;
  42. int id;
  43. };
  44. static int
  45. mISDN_open(struct inode *ino, struct file *filep)
  46. {
  47. struct mISDNtimerdev *dev;
  48. if (*debug & DEBUG_TIMER)
  49. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  50. dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
  51. if (!dev)
  52. return -ENOMEM;
  53. dev->next_id = 1;
  54. INIT_LIST_HEAD(&dev->pending);
  55. INIT_LIST_HEAD(&dev->expired);
  56. spin_lock_init(&dev->lock);
  57. dev->work = 0;
  58. init_waitqueue_head(&dev->wait);
  59. filep->private_data = dev;
  60. __module_get(THIS_MODULE);
  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. module_put(THIS_MODULE);
  87. return 0;
  88. }
  89. static ssize_t
  90. mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
  91. {
  92. struct mISDNtimerdev *dev = filep->private_data;
  93. struct mISDNtimer *timer;
  94. u_long flags;
  95. int ret = 0;
  96. if (*debug & DEBUG_TIMER)
  97. printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
  98. filep, buf, (int)count, off);
  99. if (list_empty(&dev->expired) && (dev->work == 0)) {
  100. if (filep->f_flags & O_NONBLOCK)
  101. return -EAGAIN;
  102. wait_event_interruptible(dev->wait, (dev->work ||
  103. !list_empty(&dev->expired)));
  104. if (signal_pending(current))
  105. return -ERESTARTSYS;
  106. }
  107. if (count < sizeof(int))
  108. return -ENOSPC;
  109. if (dev->work)
  110. dev->work = 0;
  111. if (!list_empty(&dev->expired)) {
  112. spin_lock_irqsave(&dev->lock, flags);
  113. timer = (struct mISDNtimer *)dev->expired.next;
  114. list_del(&timer->list);
  115. spin_unlock_irqrestore(&dev->lock, flags);
  116. if (put_user(timer->id, (int __user *)buf))
  117. ret = -EFAULT;
  118. else
  119. ret = sizeof(int);
  120. kfree(timer);
  121. }
  122. return ret;
  123. }
  124. static unsigned int
  125. mISDN_poll(struct file *filep, poll_table *wait)
  126. {
  127. struct mISDNtimerdev *dev = filep->private_data;
  128. unsigned int mask = POLLERR;
  129. if (*debug & DEBUG_TIMER)
  130. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  131. if (dev) {
  132. poll_wait(filep, &dev->wait, wait);
  133. mask = 0;
  134. if (dev->work || !list_empty(&dev->expired))
  135. mask |= (POLLIN | POLLRDNORM);
  136. if (*debug & DEBUG_TIMER)
  137. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  138. dev->work, list_empty(&dev->expired));
  139. }
  140. return mask;
  141. }
  142. static void
  143. dev_expire_timer(unsigned long data)
  144. {
  145. struct mISDNtimer *timer = (void *)data;
  146. u_long flags;
  147. spin_lock_irqsave(&timer->dev->lock, flags);
  148. if (timer->id >= 0)
  149. list_move_tail(&timer->list, &timer->dev->expired);
  150. spin_unlock_irqrestore(&timer->dev->lock, flags);
  151. wake_up_interruptible(&timer->dev->wait);
  152. }
  153. static int
  154. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  155. {
  156. int id;
  157. struct mISDNtimer *timer;
  158. if (!timeout) {
  159. dev->work = 1;
  160. wake_up_interruptible(&dev->wait);
  161. id = 0;
  162. } else {
  163. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  164. if (!timer)
  165. return -ENOMEM;
  166. timer->dev = dev;
  167. setup_timer(&timer->tl, dev_expire_timer, (long)timer);
  168. spin_lock_irq(&dev->lock);
  169. id = timer->id = dev->next_id++;
  170. if (dev->next_id < 0)
  171. dev->next_id = 1;
  172. list_add_tail(&timer->list, &dev->pending);
  173. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  174. add_timer(&timer->tl);
  175. spin_unlock_irq(&dev->lock);
  176. }
  177. return id;
  178. }
  179. static int
  180. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  181. {
  182. struct mISDNtimer *timer;
  183. spin_lock_irq(&dev->lock);
  184. list_for_each_entry(timer, &dev->pending, list) {
  185. if (timer->id == id) {
  186. list_del_init(&timer->list);
  187. timer->id = -1;
  188. spin_unlock_irq(&dev->lock);
  189. del_timer_sync(&timer->tl);
  190. kfree(timer);
  191. return id;
  192. }
  193. }
  194. spin_unlock_irq(&dev->lock);
  195. return 0;
  196. }
  197. static long
  198. mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  199. {
  200. struct mISDNtimerdev *dev = filep->private_data;
  201. int id, tout, ret = 0;
  202. if (*debug & DEBUG_TIMER)
  203. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  204. filep, cmd, arg);
  205. mutex_lock(&mISDN_mutex);
  206. switch (cmd) {
  207. case IMADDTIMER:
  208. if (get_user(tout, (int __user *)arg)) {
  209. ret = -EFAULT;
  210. break;
  211. }
  212. id = misdn_add_timer(dev, tout);
  213. if (*debug & DEBUG_TIMER)
  214. printk(KERN_DEBUG "%s add %d id %d\n", __func__,
  215. tout, id);
  216. if (id < 0) {
  217. ret = id;
  218. break;
  219. }
  220. if (put_user(id, (int __user *)arg))
  221. ret = -EFAULT;
  222. break;
  223. case IMDELTIMER:
  224. if (get_user(id, (int __user *)arg)) {
  225. ret = -EFAULT;
  226. break;
  227. }
  228. if (*debug & DEBUG_TIMER)
  229. printk(KERN_DEBUG "%s del id %d\n", __func__, id);
  230. id = misdn_del_timer(dev, id);
  231. if (put_user(id, (int __user *)arg))
  232. ret = -EFAULT;
  233. break;
  234. default:
  235. ret = -EINVAL;
  236. }
  237. mutex_unlock(&mISDN_mutex);
  238. return ret;
  239. }
  240. static const struct file_operations mISDN_fops = {
  241. .read = mISDN_read,
  242. .poll = mISDN_poll,
  243. .unlocked_ioctl = mISDN_ioctl,
  244. .open = mISDN_open,
  245. .release = mISDN_close,
  246. .llseek = no_llseek,
  247. };
  248. static struct miscdevice mISDNtimer = {
  249. .minor = MISC_DYNAMIC_MINOR,
  250. .name = "mISDNtimer",
  251. .fops = &mISDN_fops,
  252. };
  253. int
  254. mISDN_inittimer(u_int *deb)
  255. {
  256. int err;
  257. debug = deb;
  258. err = misc_register(&mISDNtimer);
  259. if (err)
  260. printk(KERN_WARNING "mISDN: Could not register timer device\n");
  261. return err;
  262. }
  263. void mISDN_timer_cleanup(void)
  264. {
  265. misc_deregister(&mISDNtimer);
  266. }