timerdev.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 list_head *list = &dev->expired;
  94. struct mISDNtimer *timer;
  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 (count < sizeof(int))
  100. return -ENOSPC;
  101. spin_lock_irq(&dev->lock);
  102. while (list_empty(list) && (dev->work == 0)) {
  103. spin_unlock_irq(&dev->lock);
  104. if (filep->f_flags & O_NONBLOCK)
  105. return -EAGAIN;
  106. wait_event_interruptible(dev->wait, (dev->work ||
  107. !list_empty(list)));
  108. if (signal_pending(current))
  109. return -ERESTARTSYS;
  110. spin_lock_irq(&dev->lock);
  111. }
  112. if (dev->work)
  113. dev->work = 0;
  114. if (!list_empty(list)) {
  115. timer = list_first_entry(list, struct mISDNtimer, list);
  116. list_del(&timer->list);
  117. spin_unlock_irq(&dev->lock);
  118. if (put_user(timer->id, (int __user *)buf))
  119. ret = -EFAULT;
  120. else
  121. ret = sizeof(int);
  122. kfree(timer);
  123. } else {
  124. spin_unlock_irq(&dev->lock);
  125. }
  126. return ret;
  127. }
  128. static unsigned int
  129. mISDN_poll(struct file *filep, poll_table *wait)
  130. {
  131. struct mISDNtimerdev *dev = filep->private_data;
  132. unsigned int mask = POLLERR;
  133. if (*debug & DEBUG_TIMER)
  134. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  135. if (dev) {
  136. poll_wait(filep, &dev->wait, wait);
  137. mask = 0;
  138. if (dev->work || !list_empty(&dev->expired))
  139. mask |= (POLLIN | POLLRDNORM);
  140. if (*debug & DEBUG_TIMER)
  141. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  142. dev->work, list_empty(&dev->expired));
  143. }
  144. return mask;
  145. }
  146. static void
  147. dev_expire_timer(unsigned long data)
  148. {
  149. struct mISDNtimer *timer = (void *)data;
  150. u_long flags;
  151. spin_lock_irqsave(&timer->dev->lock, flags);
  152. if (timer->id >= 0)
  153. list_move_tail(&timer->list, &timer->dev->expired);
  154. spin_unlock_irqrestore(&timer->dev->lock, flags);
  155. wake_up_interruptible(&timer->dev->wait);
  156. }
  157. static int
  158. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  159. {
  160. int id;
  161. struct mISDNtimer *timer;
  162. if (!timeout) {
  163. dev->work = 1;
  164. wake_up_interruptible(&dev->wait);
  165. id = 0;
  166. } else {
  167. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  168. if (!timer)
  169. return -ENOMEM;
  170. timer->dev = dev;
  171. setup_timer(&timer->tl, dev_expire_timer, (long)timer);
  172. spin_lock_irq(&dev->lock);
  173. id = timer->id = dev->next_id++;
  174. if (dev->next_id < 0)
  175. dev->next_id = 1;
  176. list_add_tail(&timer->list, &dev->pending);
  177. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  178. add_timer(&timer->tl);
  179. spin_unlock_irq(&dev->lock);
  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. }