timerdev.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. u_long flags;
  158. struct mISDNtimer *timer;
  159. if (!timeout) {
  160. dev->work = 1;
  161. wake_up_interruptible(&dev->wait);
  162. id = 0;
  163. } else {
  164. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  165. if (!timer)
  166. return -ENOMEM;
  167. spin_lock_irqsave(&dev->lock, flags);
  168. timer->id = dev->next_id++;
  169. if (dev->next_id < 0)
  170. dev->next_id = 1;
  171. list_add_tail(&timer->list, &dev->pending);
  172. spin_unlock_irqrestore(&dev->lock, flags);
  173. timer->dev = dev;
  174. timer->tl.data = (long)timer;
  175. timer->tl.function = dev_expire_timer;
  176. init_timer(&timer->tl);
  177. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  178. add_timer(&timer->tl);
  179. id = timer->id;
  180. }
  181. return id;
  182. }
  183. static int
  184. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  185. {
  186. struct mISDNtimer *timer;
  187. spin_lock_irq(&dev->lock);
  188. list_for_each_entry(timer, &dev->pending, list) {
  189. if (timer->id == id) {
  190. list_del_init(&timer->list);
  191. timer->id = -1;
  192. spin_unlock_irq(&dev->lock);
  193. del_timer_sync(&timer->tl);
  194. kfree(timer);
  195. return id;
  196. }
  197. }
  198. spin_unlock_irq(&dev->lock);
  199. return 0;
  200. }
  201. static long
  202. mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  203. {
  204. struct mISDNtimerdev *dev = filep->private_data;
  205. int id, tout, ret = 0;
  206. if (*debug & DEBUG_TIMER)
  207. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  208. filep, cmd, arg);
  209. mutex_lock(&mISDN_mutex);
  210. switch (cmd) {
  211. case IMADDTIMER:
  212. if (get_user(tout, (int __user *)arg)) {
  213. ret = -EFAULT;
  214. break;
  215. }
  216. id = misdn_add_timer(dev, tout);
  217. if (*debug & DEBUG_TIMER)
  218. printk(KERN_DEBUG "%s add %d id %d\n", __func__,
  219. tout, id);
  220. if (id < 0) {
  221. ret = id;
  222. break;
  223. }
  224. if (put_user(id, (int __user *)arg))
  225. ret = -EFAULT;
  226. break;
  227. case IMDELTIMER:
  228. if (get_user(id, (int __user *)arg)) {
  229. ret = -EFAULT;
  230. break;
  231. }
  232. if (*debug & DEBUG_TIMER)
  233. printk(KERN_DEBUG "%s del id %d\n", __func__, id);
  234. id = misdn_del_timer(dev, id);
  235. if (put_user(id, (int __user *)arg))
  236. ret = -EFAULT;
  237. break;
  238. default:
  239. ret = -EINVAL;
  240. }
  241. mutex_unlock(&mISDN_mutex);
  242. return ret;
  243. }
  244. static const struct file_operations mISDN_fops = {
  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. }