timerdev.c 6.6 KB

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