timerdev.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 loff_t
  115. mISDN_llseek(struct file *filep, loff_t offset, int orig)
  116. {
  117. return -ESPIPE;
  118. }
  119. static ssize_t
  120. mISDN_write(struct file *filep, const char *buf, size_t count, loff_t *off)
  121. {
  122. return -EOPNOTSUPP;
  123. }
  124. static unsigned int
  125. mISDN_poll(struct file *filep, poll_table *wait)
  126. {
  127. struct mISDNtimerdev *dev = filep->private_data;
  128. unsigned int mask = POLLERR;
  129. if (*debug & DEBUG_TIMER)
  130. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  131. if (dev) {
  132. poll_wait(filep, &dev->wait, wait);
  133. mask = 0;
  134. if (dev->work || !list_empty(&dev->expired))
  135. mask |= (POLLIN | POLLRDNORM);
  136. if (*debug & DEBUG_TIMER)
  137. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  138. dev->work, list_empty(&dev->expired));
  139. }
  140. return mask;
  141. }
  142. static void
  143. dev_expire_timer(struct mISDNtimer *timer)
  144. {
  145. u_long flags;
  146. spin_lock_irqsave(&timer->dev->lock, flags);
  147. list_del(&timer->list);
  148. list_add_tail(&timer->list, &timer->dev->expired);
  149. spin_unlock_irqrestore(&timer->dev->lock, flags);
  150. wake_up_interruptible(&timer->dev->wait);
  151. }
  152. static int
  153. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  154. {
  155. int id;
  156. u_long flags;
  157. struct mISDNtimer *timer;
  158. if (!timeout) {
  159. dev->work = 1;
  160. wake_up_interruptible(&dev->wait);
  161. id = 0;
  162. } else {
  163. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  164. if (!timer)
  165. return -ENOMEM;
  166. spin_lock_irqsave(&dev->lock, flags);
  167. timer->id = dev->next_id++;
  168. if (dev->next_id < 0)
  169. dev->next_id = 1;
  170. list_add_tail(&timer->list, &dev->pending);
  171. spin_unlock_irqrestore(&dev->lock, flags);
  172. timer->dev = dev;
  173. timer->tl.data = (long)timer;
  174. timer->tl.function = (void *) dev_expire_timer;
  175. init_timer(&timer->tl);
  176. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  177. add_timer(&timer->tl);
  178. id = timer->id;
  179. }
  180. return id;
  181. }
  182. static int
  183. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  184. {
  185. u_long flags;
  186. struct mISDNtimer *timer;
  187. int ret = 0;
  188. spin_lock_irqsave(&dev->lock, flags);
  189. list_for_each_entry(timer, &dev->pending, list) {
  190. if (timer->id == id) {
  191. list_del_init(&timer->list);
  192. del_timer(&timer->tl);
  193. ret = timer->id;
  194. kfree(timer);
  195. goto unlock;
  196. }
  197. }
  198. unlock:
  199. spin_unlock_irqrestore(&dev->lock, flags);
  200. return ret;
  201. }
  202. static int
  203. mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  204. unsigned long arg)
  205. {
  206. struct mISDNtimerdev *dev = filep->private_data;
  207. int id, tout, ret = 0;
  208. if (*debug & DEBUG_TIMER)
  209. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  210. filep, cmd, arg);
  211. switch (cmd) {
  212. case IMADDTIMER:
  213. if (get_user(tout, (int __user *)arg)) {
  214. ret = -EFAULT;
  215. break;
  216. }
  217. id = misdn_add_timer(dev, tout);
  218. if (*debug & DEBUG_TIMER)
  219. printk(KERN_DEBUG "%s add %d id %d\n", __func__,
  220. tout, id);
  221. if (id < 0) {
  222. ret = id;
  223. break;
  224. }
  225. if (put_user(id, (int __user *)arg))
  226. ret = -EFAULT;
  227. break;
  228. case IMDELTIMER:
  229. if (get_user(id, (int __user *)arg)) {
  230. ret = -EFAULT;
  231. break;
  232. }
  233. if (*debug & DEBUG_TIMER)
  234. printk(KERN_DEBUG "%s del id %d\n", __func__, id);
  235. id = misdn_del_timer(dev, id);
  236. if (put_user(id, (int __user *)arg))
  237. ret = -EFAULT;
  238. break;
  239. default:
  240. ret = -EINVAL;
  241. }
  242. return ret;
  243. }
  244. static struct file_operations mISDN_fops = {
  245. .llseek = mISDN_llseek,
  246. .read = mISDN_read,
  247. .write = mISDN_write,
  248. .poll = mISDN_poll,
  249. .ioctl = mISDN_ioctl,
  250. .open = mISDN_open,
  251. .release = mISDN_close,
  252. };
  253. static struct miscdevice mISDNtimer = {
  254. .minor = MISC_DYNAMIC_MINOR,
  255. .name = "mISDNtimer",
  256. .fops = &mISDN_fops,
  257. };
  258. int
  259. mISDN_inittimer(int *deb)
  260. {
  261. int err;
  262. debug = deb;
  263. err = misc_register(&mISDNtimer);
  264. if (err)
  265. printk(KERN_WARNING "mISDN: Could not register timer device\n");
  266. return err;
  267. }
  268. void mISDN_timer_cleanup(void)
  269. {
  270. misc_deregister(&mISDNtimer);
  271. }