timerdev.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * general timer device for using in ISDN stacks
  5. *
  6. * Author Karsten Keil <[email protected]>
  7. *
  8. * Copyright 2008 by Karsten Keil <[email protected]>
  9. */
  10. #include <linux/poll.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/slab.h>
  13. #include <linux/timer.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/module.h>
  16. #include <linux/mISDNif.h>
  17. #include <linux/mutex.h>
  18. #include <linux/sched/signal.h>
  19. #include "core.h"
  20. static DEFINE_MUTEX(mISDN_mutex);
  21. static u_int *debug;
  22. struct mISDNtimerdev {
  23. int next_id;
  24. struct list_head pending;
  25. struct list_head expired;
  26. wait_queue_head_t wait;
  27. u_int work;
  28. spinlock_t lock; /* protect lists */
  29. };
  30. struct mISDNtimer {
  31. struct list_head list;
  32. struct mISDNtimerdev *dev;
  33. struct timer_list tl;
  34. int id;
  35. };
  36. static int
  37. mISDN_open(struct inode *ino, struct file *filep)
  38. {
  39. struct mISDNtimerdev *dev;
  40. if (*debug & DEBUG_TIMER)
  41. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  42. dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
  43. if (!dev)
  44. return -ENOMEM;
  45. dev->next_id = 1;
  46. INIT_LIST_HEAD(&dev->pending);
  47. INIT_LIST_HEAD(&dev->expired);
  48. spin_lock_init(&dev->lock);
  49. dev->work = 0;
  50. init_waitqueue_head(&dev->wait);
  51. filep->private_data = dev;
  52. return nonseekable_open(ino, filep);
  53. }
  54. static int
  55. mISDN_close(struct inode *ino, struct file *filep)
  56. {
  57. struct mISDNtimerdev *dev = filep->private_data;
  58. struct list_head *list = &dev->pending;
  59. struct mISDNtimer *timer, *next;
  60. if (*debug & DEBUG_TIMER)
  61. printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
  62. spin_lock_irq(&dev->lock);
  63. while (!list_empty(list)) {
  64. timer = list_first_entry(list, struct mISDNtimer, list);
  65. spin_unlock_irq(&dev->lock);
  66. del_timer_sync(&timer->tl);
  67. spin_lock_irq(&dev->lock);
  68. /* it might have been moved to ->expired */
  69. list_del(&timer->list);
  70. kfree(timer);
  71. }
  72. spin_unlock_irq(&dev->lock);
  73. list_for_each_entry_safe(timer, next, &dev->expired, list) {
  74. kfree(timer);
  75. }
  76. kfree(dev);
  77. return 0;
  78. }
  79. static ssize_t
  80. mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
  81. {
  82. struct mISDNtimerdev *dev = filep->private_data;
  83. struct list_head *list = &dev->expired;
  84. struct mISDNtimer *timer;
  85. int ret = 0;
  86. if (*debug & DEBUG_TIMER)
  87. printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
  88. filep, buf, (int)count, off);
  89. if (count < sizeof(int))
  90. return -ENOSPC;
  91. spin_lock_irq(&dev->lock);
  92. while (list_empty(list) && (dev->work == 0)) {
  93. spin_unlock_irq(&dev->lock);
  94. if (filep->f_flags & O_NONBLOCK)
  95. return -EAGAIN;
  96. wait_event_interruptible(dev->wait, (dev->work ||
  97. !list_empty(list)));
  98. if (signal_pending(current))
  99. return -ERESTARTSYS;
  100. spin_lock_irq(&dev->lock);
  101. }
  102. if (dev->work)
  103. dev->work = 0;
  104. if (!list_empty(list)) {
  105. timer = list_first_entry(list, struct mISDNtimer, list);
  106. list_del(&timer->list);
  107. spin_unlock_irq(&dev->lock);
  108. if (put_user(timer->id, (int __user *)buf))
  109. ret = -EFAULT;
  110. else
  111. ret = sizeof(int);
  112. kfree(timer);
  113. } else {
  114. spin_unlock_irq(&dev->lock);
  115. }
  116. return ret;
  117. }
  118. static __poll_t
  119. mISDN_poll(struct file *filep, poll_table *wait)
  120. {
  121. struct mISDNtimerdev *dev = filep->private_data;
  122. __poll_t mask = EPOLLERR;
  123. if (*debug & DEBUG_TIMER)
  124. printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
  125. if (dev) {
  126. poll_wait(filep, &dev->wait, wait);
  127. mask = 0;
  128. if (dev->work || !list_empty(&dev->expired))
  129. mask |= (EPOLLIN | EPOLLRDNORM);
  130. if (*debug & DEBUG_TIMER)
  131. printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
  132. dev->work, list_empty(&dev->expired));
  133. }
  134. return mask;
  135. }
  136. static void
  137. dev_expire_timer(struct timer_list *t)
  138. {
  139. struct mISDNtimer *timer = from_timer(timer, t, tl);
  140. u_long flags;
  141. spin_lock_irqsave(&timer->dev->lock, flags);
  142. if (timer->id >= 0)
  143. list_move_tail(&timer->list, &timer->dev->expired);
  144. wake_up_interruptible(&timer->dev->wait);
  145. spin_unlock_irqrestore(&timer->dev->lock, flags);
  146. }
  147. static int
  148. misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
  149. {
  150. int id;
  151. struct mISDNtimer *timer;
  152. if (!timeout) {
  153. dev->work = 1;
  154. wake_up_interruptible(&dev->wait);
  155. id = 0;
  156. } else {
  157. timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
  158. if (!timer)
  159. return -ENOMEM;
  160. timer->dev = dev;
  161. timer_setup(&timer->tl, dev_expire_timer, 0);
  162. spin_lock_irq(&dev->lock);
  163. id = timer->id = dev->next_id++;
  164. if (dev->next_id < 0)
  165. dev->next_id = 1;
  166. list_add_tail(&timer->list, &dev->pending);
  167. timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
  168. add_timer(&timer->tl);
  169. spin_unlock_irq(&dev->lock);
  170. }
  171. return id;
  172. }
  173. static int
  174. misdn_del_timer(struct mISDNtimerdev *dev, int id)
  175. {
  176. struct mISDNtimer *timer;
  177. spin_lock_irq(&dev->lock);
  178. list_for_each_entry(timer, &dev->pending, list) {
  179. if (timer->id == id) {
  180. list_del_init(&timer->list);
  181. timer->id = -1;
  182. spin_unlock_irq(&dev->lock);
  183. del_timer_sync(&timer->tl);
  184. kfree(timer);
  185. return id;
  186. }
  187. }
  188. spin_unlock_irq(&dev->lock);
  189. return 0;
  190. }
  191. static long
  192. mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  193. {
  194. struct mISDNtimerdev *dev = filep->private_data;
  195. int id, tout, ret = 0;
  196. if (*debug & DEBUG_TIMER)
  197. printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
  198. filep, cmd, arg);
  199. mutex_lock(&mISDN_mutex);
  200. switch (cmd) {
  201. case IMADDTIMER:
  202. if (get_user(tout, (int __user *)arg)) {
  203. ret = -EFAULT;
  204. break;
  205. }
  206. id = misdn_add_timer(dev, tout);
  207. if (*debug & DEBUG_TIMER)
  208. printk(KERN_DEBUG "%s add %d id %d\n", __func__,
  209. tout, id);
  210. if (id < 0) {
  211. ret = id;
  212. break;
  213. }
  214. if (put_user(id, (int __user *)arg))
  215. ret = -EFAULT;
  216. break;
  217. case IMDELTIMER:
  218. if (get_user(id, (int __user *)arg)) {
  219. ret = -EFAULT;
  220. break;
  221. }
  222. if (*debug & DEBUG_TIMER)
  223. printk(KERN_DEBUG "%s del id %d\n", __func__, id);
  224. id = misdn_del_timer(dev, id);
  225. if (put_user(id, (int __user *)arg))
  226. ret = -EFAULT;
  227. break;
  228. default:
  229. ret = -EINVAL;
  230. }
  231. mutex_unlock(&mISDN_mutex);
  232. return ret;
  233. }
  234. static const struct file_operations mISDN_fops = {
  235. .owner = THIS_MODULE,
  236. .read = mISDN_read,
  237. .poll = mISDN_poll,
  238. .unlocked_ioctl = mISDN_ioctl,
  239. .open = mISDN_open,
  240. .release = mISDN_close,
  241. .llseek = no_llseek,
  242. };
  243. static struct miscdevice mISDNtimer = {
  244. .minor = MISC_DYNAMIC_MINOR,
  245. .name = "mISDNtimer",
  246. .fops = &mISDN_fops,
  247. };
  248. int
  249. mISDN_inittimer(u_int *deb)
  250. {
  251. int err;
  252. debug = deb;
  253. err = misc_register(&mISDNtimer);
  254. if (err)
  255. printk(KERN_WARNING "mISDN: Could not register timer device\n");
  256. return err;
  257. }
  258. void mISDN_timer_cleanup(void)
  259. {
  260. misc_deregister(&mISDNtimer);
  261. }