timerdev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. __module_get(THIS_MODULE);
  61. return nonseekable_open(ino, filep);
  62. }
  63. static int
  64. mISDN_close(struct inode *ino, struct file *filep)
  65. {
  66. struct mISDNtimerdev *dev = filep->private_data;
  67. struct mISDNtimer *timer, *next;
  68. if (*debug & DEBUG_TIMER)
  69. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  70. list_for_each_entry_safe(timer, next, &dev->pending, list) {
  71. del_timer(&timer->tl);
  72. kfree(timer);
  73. }
  74. list_for_each_entry_safe(timer, next, &dev->expired, list) {
  75. kfree(timer);
  76. }
  77. kfree(dev);
  78. module_put(THIS_MODULE);
  79. return 0;
  80. }
  81. static ssize_t
  82. mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
  83. {
  84. struct mISDNtimerdev *dev = filep->private_data;
  85. struct mISDNtimer *timer;
  86. u_long flags;
  87. int ret = 0;
  88. if (*debug & DEBUG_TIMER)
  89. printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
  90. filep, buf, (int)count, off);
  91. if (list_empty(&dev->expired) && (dev->work == 0)) {
  92. if (filep->f_flags & O_NONBLOCK)
  93. return -EAGAIN;
  94. wait_event_interruptible(dev->wait, (dev->work ||
  95. !list_empty(&dev->expired)));
  96. if (signal_pending(current))
  97. return -ERESTARTSYS;
  98. }
  99. if (count < sizeof(int))
  100. return -ENOSPC;
  101. if (dev->work)
  102. dev->work = 0;
  103. if (!list_empty(&dev->expired)) {
  104. spin_lock_irqsave(&dev->lock, flags);
  105. timer = (struct mISDNtimer *)dev->expired.next;
  106. list_del(&timer->list);
  107. spin_unlock_irqrestore(&dev->lock, flags);
  108. if (put_user(timer->id, (int __user *)buf))
  109. ret = -EFAULT;
  110. else
  111. ret = sizeof(int);
  112. kfree(timer);
  113. }
  114. return ret;
  115. }
  116. static unsigned int
  117. mISDN_poll(struct file *filep, poll_table *wait)
  118. {
  119. struct mISDNtimerdev *dev = filep->private_data;
  120. unsigned int mask = POLLERR;
  121. if (*debug & DEBUG_TIMER)
  122. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  123. if (dev) {
  124. poll_wait(filep, &dev->wait, wait);
  125. mask = 0;
  126. if (dev->work || !list_empty(&dev->expired))
  127. mask |= (POLLIN | POLLRDNORM);
  128. if (*debug & DEBUG_TIMER)
  129. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  130. dev->work, list_empty(&dev->expired));
  131. }
  132. return mask;
  133. }
  134. static void
  135. dev_expire_timer(unsigned long data)
  136. {
  137. struct mISDNtimer *timer = (void *)data;
  138. u_long flags;
  139. spin_lock_irqsave(&timer->dev->lock, flags);
  140. list_move_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 long
  198. mISDN_ioctl(struct file *filep, unsigned int cmd, 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. mutex_lock(&mISDN_mutex);
  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. mutex_unlock(&mISDN_mutex);
  238. return ret;
  239. }
  240. static const struct file_operations mISDN_fops = {
  241. .read = mISDN_read,
  242. .poll = mISDN_poll,
  243. .unlocked_ioctl = mISDN_ioctl,
  244. .open = mISDN_open,
  245. .release = mISDN_close,
  246. .llseek = no_llseek,
  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. }