3 * general timer device for using in ISDN stacks
5 * Author Karsten Keil <kkeil@novell.com>
7 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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.
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.
20 #include <linux/poll.h>
21 #include <linux/vmalloc.h>
22 #include <linux/slab.h>
23 #include <linux/timer.h>
24 #include <linux/miscdevice.h>
25 #include <linux/module.h>
26 #include <linux/mISDNif.h>
27 #include <linux/smp_lock.h>
33 struct mISDNtimerdev {
35 struct list_head pending;
36 struct list_head expired;
37 wait_queue_head_t wait;
39 spinlock_t lock; /* protect lists */
43 struct list_head list;
44 struct mISDNtimerdev *dev;
50 mISDN_open(struct inode *ino, struct file *filep)
52 struct mISDNtimerdev *dev;
54 if (*debug & DEBUG_TIMER)
55 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
56 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
60 INIT_LIST_HEAD(&dev->pending);
61 INIT_LIST_HEAD(&dev->expired);
62 spin_lock_init(&dev->lock);
64 init_waitqueue_head(&dev->wait);
65 filep->private_data = dev;
66 __module_get(THIS_MODULE);
67 return nonseekable_open(ino, filep);
71 mISDN_close(struct inode *ino, struct file *filep)
73 struct mISDNtimerdev *dev = filep->private_data;
74 struct mISDNtimer *timer, *next;
76 if (*debug & DEBUG_TIMER)
77 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
78 list_for_each_entry_safe(timer, next, &dev->pending, list) {
79 del_timer(&timer->tl);
82 list_for_each_entry_safe(timer, next, &dev->expired, list) {
86 module_put(THIS_MODULE);
91 mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
93 struct mISDNtimerdev *dev = filep->private_data;
94 struct mISDNtimer *timer;
98 if (*debug & DEBUG_TIMER)
99 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
100 filep, buf, (int)count, off);
101 if (*off != filep->f_pos)
104 if (list_empty(&dev->expired) && (dev->work == 0)) {
105 if (filep->f_flags & O_NONBLOCK)
107 wait_event_interruptible(dev->wait, (dev->work ||
108 !list_empty(&dev->expired)));
109 if (signal_pending(current))
112 if (count < sizeof(int))
116 if (!list_empty(&dev->expired)) {
117 spin_lock_irqsave(&dev->lock, flags);
118 timer = (struct mISDNtimer *)dev->expired.next;
119 list_del(&timer->list);
120 spin_unlock_irqrestore(&dev->lock, flags);
121 if (put_user(timer->id, (int __user *)buf))
131 mISDN_poll(struct file *filep, poll_table *wait)
133 struct mISDNtimerdev *dev = filep->private_data;
134 unsigned int mask = POLLERR;
136 if (*debug & DEBUG_TIMER)
137 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
139 poll_wait(filep, &dev->wait, wait);
141 if (dev->work || !list_empty(&dev->expired))
142 mask |= (POLLIN | POLLRDNORM);
143 if (*debug & DEBUG_TIMER)
144 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
145 dev->work, list_empty(&dev->expired));
151 dev_expire_timer(unsigned long data)
153 struct mISDNtimer *timer = (void *)data;
156 spin_lock_irqsave(&timer->dev->lock, flags);
157 list_move_tail(&timer->list, &timer->dev->expired);
158 spin_unlock_irqrestore(&timer->dev->lock, flags);
159 wake_up_interruptible(&timer->dev->wait);
163 misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
167 struct mISDNtimer *timer;
171 wake_up_interruptible(&dev->wait);
174 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
177 spin_lock_irqsave(&dev->lock, flags);
178 timer->id = dev->next_id++;
179 if (dev->next_id < 0)
181 list_add_tail(&timer->list, &dev->pending);
182 spin_unlock_irqrestore(&dev->lock, flags);
184 timer->tl.data = (long)timer;
185 timer->tl.function = dev_expire_timer;
186 init_timer(&timer->tl);
187 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
188 add_timer(&timer->tl);
195 misdn_del_timer(struct mISDNtimerdev *dev, int id)
198 struct mISDNtimer *timer;
201 spin_lock_irqsave(&dev->lock, flags);
202 list_for_each_entry(timer, &dev->pending, list) {
203 if (timer->id == id) {
204 list_del_init(&timer->list);
205 /* RED-PEN AK: race -- timer can be still running on
206 * other CPU. Needs reference count I think
208 del_timer(&timer->tl);
215 spin_unlock_irqrestore(&dev->lock, flags);
220 mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
222 struct mISDNtimerdev *dev = filep->private_data;
223 int id, tout, ret = 0;
226 if (*debug & DEBUG_TIMER)
227 printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
232 if (get_user(tout, (int __user *)arg)) {
236 id = misdn_add_timer(dev, tout);
237 if (*debug & DEBUG_TIMER)
238 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
244 if (put_user(id, (int __user *)arg))
248 if (get_user(id, (int __user *)arg)) {
252 if (*debug & DEBUG_TIMER)
253 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
254 id = misdn_del_timer(dev, id);
255 if (put_user(id, (int __user *)arg))
265 static const struct file_operations mISDN_fops = {
268 .unlocked_ioctl = mISDN_ioctl,
270 .release = mISDN_close,
273 static struct miscdevice mISDNtimer = {
274 .minor = MISC_DYNAMIC_MINOR,
275 .name = "mISDNtimer",
280 mISDN_inittimer(u_int *deb)
285 err = misc_register(&mISDNtimer);
287 printk(KERN_WARNING "mISDN: Could not register timer device\n");
291 void mISDN_timer_cleanup(void)
293 misc_deregister(&mISDNtimer);