timerdev.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_del(&timer->list);
  140. list_add_tail(&timer->list, &timer->dev->expired);
  141. spin_unlock_irqrestore(&timer->dev->lock, flags);
  142. wake_up_interruptible(&timer->dev->wait);
  143. }
  144. static int
  145. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  146. {
  147. int id;
  148. u_long flags;
  149. struct mISDNtimer *timer;
  150. if (!timeout) {
  151. dev->work = 1;
  152. wake_up_interruptible(&dev->wait);
  153. id = 0;
  154. } else {
  155. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  156. if (!timer)
  157. return -ENOMEM;
  158. spin_lock_irqsave(&dev->lock, flags);
  159. timer->id = dev->next_id++;
  160. if (dev->next_id < 0)
  161. dev->next_id = 1;
  162. list_add_tail(&timer->list, &dev->pending);
  163. spin_unlock_irqrestore(&dev->lock, flags);
  164. timer->dev = dev;
  165. timer->tl.data = (long)timer;
  166. timer->tl.function = dev_expire_timer;
  167. init_timer(&timer->tl);
  168. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  169. add_timer(&timer->tl);
  170. id = timer->id;
  171. }
  172. return id;
  173. }
  174. static int
  175. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  176. {
  177. u_long flags;
  178. struct mISDNtimer *timer;
  179. int ret = 0;
  180. spin_lock_irqsave(&dev->lock, flags);
  181. list_for_each_entry(timer, &dev->pending, list) {
  182. if (timer->id == id) {
  183. list_del_init(&timer->list);
  184. /* RED-PEN AK: race -- timer can be still running on
  185. * other CPU. Needs reference count I think
  186. */
  187. del_timer(&timer->tl);
  188. ret = timer->id;
  189. kfree(timer);
  190. goto unlock;
  191. }
  192. }
  193. unlock:
  194. spin_unlock_irqrestore(&dev->lock, flags);
  195. return ret;
  196. }
  197. static int
  198. mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  199. unsigned long arg)
  200. {
  201. struct mISDNtimerdev *dev = filep->private_data;
  202. int id, tout, ret = 0;
  203. if (*debug & DEBUG_TIMER)
  204. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  205. filep, cmd, arg);
  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. return ret;
  238. }
  239. static struct file_operations mISDN_fops = {
  240. .read = mISDN_read,
  241. .poll = mISDN_poll,
  242. .ioctl = mISDN_ioctl,
  243. .open = mISDN_open,
  244. .release = mISDN_close,
  245. };
  246. static struct miscdevice mISDNtimer = {
  247. .minor = MISC_DYNAMIC_MINOR,
  248. .name = "mISDNtimer",
  249. .fops = &mISDN_fops,
  250. };
  251. int
  252. mISDN_inittimer(u_int *deb)
  253. {
  254. int err;
  255. debug = deb;
  256. err = misc_register(&mISDNtimer);
  257. if (err)
  258. printk(KERN_WARNING "mISDN: Could not register timer device\n");
  259. return err;
  260. }
  261. void mISDN_timer_cleanup(void)
  262. {
  263. misc_deregister(&mISDNtimer);
  264. }