timerdev.c 6.5 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. static int *debug;
  26. struct mISDNtimerdev {
  27. int next_id;
  28. struct list_head pending;
  29. struct list_head expired;
  30. wait_queue_head_t wait;
  31. u_int work;
  32. spinlock_t lock; /* protect lists */
  33. };
  34. struct mISDNtimer {
  35. struct list_head list;
  36. struct mISDNtimerdev *dev;
  37. struct timer_list tl;
  38. int id;
  39. };
  40. static int
  41. mISDN_open(struct inode *ino, struct file *filep)
  42. {
  43. struct mISDNtimerdev *dev;
  44. if (*debug & DEBUG_TIMER)
  45. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  46. dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
  47. if (!dev)
  48. return -ENOMEM;
  49. dev->next_id = 1;
  50. INIT_LIST_HEAD(&dev->pending);
  51. INIT_LIST_HEAD(&dev->expired);
  52. spin_lock_init(&dev->lock);
  53. dev->work = 0;
  54. init_waitqueue_head(&dev->wait);
  55. filep->private_data = dev;
  56. __module_get(THIS_MODULE);
  57. return 0;
  58. }
  59. static int
  60. mISDN_close(struct inode *ino, struct file *filep)
  61. {
  62. struct mISDNtimerdev *dev = filep->private_data;
  63. struct mISDNtimer *timer, *next;
  64. if (*debug & DEBUG_TIMER)
  65. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  66. list_for_each_entry_safe(timer, next, &dev->pending, list) {
  67. del_timer(&timer->tl);
  68. kfree(timer);
  69. }
  70. list_for_each_entry_safe(timer, next, &dev->expired, list) {
  71. kfree(timer);
  72. }
  73. kfree(dev);
  74. module_put(THIS_MODULE);
  75. return 0;
  76. }
  77. static ssize_t
  78. mISDN_read(struct file *filep, char *buf, size_t count, loff_t *off)
  79. {
  80. struct mISDNtimerdev *dev = filep->private_data;
  81. struct mISDNtimer *timer;
  82. u_long flags;
  83. int ret = 0;
  84. if (*debug & DEBUG_TIMER)
  85. printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
  86. filep, buf, (int)count, off);
  87. if (*off != filep->f_pos)
  88. return -ESPIPE;
  89. if (list_empty(&dev->expired) && (dev->work == 0)) {
  90. if (filep->f_flags & O_NONBLOCK)
  91. return -EAGAIN;
  92. wait_event_interruptible(dev->wait, (dev->work ||
  93. !list_empty(&dev->expired)));
  94. if (signal_pending(current))
  95. return -ERESTARTSYS;
  96. }
  97. if (count < sizeof(int))
  98. return -ENOSPC;
  99. if (dev->work)
  100. dev->work = 0;
  101. if (!list_empty(&dev->expired)) {
  102. spin_lock_irqsave(&dev->lock, flags);
  103. timer = (struct mISDNtimer *)dev->expired.next;
  104. list_del(&timer->list);
  105. spin_unlock_irqrestore(&dev->lock, flags);
  106. if (put_user(timer->id, (int *)buf))
  107. ret = -EFAULT;
  108. else
  109. ret = sizeof(int);
  110. kfree(timer);
  111. }
  112. return ret;
  113. }
  114. static unsigned int
  115. mISDN_poll(struct file *filep, poll_table *wait)
  116. {
  117. struct mISDNtimerdev *dev = filep->private_data;
  118. unsigned int mask = POLLERR;
  119. if (*debug & DEBUG_TIMER)
  120. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  121. if (dev) {
  122. poll_wait(filep, &dev->wait, wait);
  123. mask = 0;
  124. if (dev->work || !list_empty(&dev->expired))
  125. mask |= (POLLIN | POLLRDNORM);
  126. if (*debug & DEBUG_TIMER)
  127. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  128. dev->work, list_empty(&dev->expired));
  129. }
  130. return mask;
  131. }
  132. static void
  133. dev_expire_timer(unsigned long data)
  134. {
  135. struct mISDNtimer *timer = (void *)data;
  136. u_long flags;
  137. spin_lock_irqsave(&timer->dev->lock, flags);
  138. list_del(&timer->list);
  139. list_add_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 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(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. }