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