timerdev.c 6.6 KB

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