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>
32 struct mISDNtimerdev {
34 struct list_head pending;
35 struct list_head expired;
36 wait_queue_head_t wait;
38 spinlock_t lock; /* protect lists */
42 struct list_head list;
43 struct mISDNtimerdev *dev;
49 mISDN_open(struct inode *ino, struct file *filep)
51 struct mISDNtimerdev *dev;
53 if (*debug & DEBUG_TIMER)
54 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
55 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
59 INIT_LIST_HEAD(&dev->pending);
60 INIT_LIST_HEAD(&dev->expired);
61 spin_lock_init(&dev->lock);
63 init_waitqueue_head(&dev->wait);
64 filep->private_data = dev;
65 __module_get(THIS_MODULE);
66 return nonseekable_open(ino, filep);
70 mISDN_close(struct inode *ino, struct file *filep)
72 struct mISDNtimerdev *dev = filep->private_data;
73 struct mISDNtimer *timer, *next;
75 if (*debug & DEBUG_TIMER)
76 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
77 list_for_each_entry_safe(timer, next, &dev->pending, list) {
78 del_timer(&timer->tl);
81 list_for_each_entry_safe(timer, next, &dev->expired, list) {
85 module_put(THIS_MODULE);
90 mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
92 struct mISDNtimerdev *dev = filep->private_data;
93 struct mISDNtimer *timer;
97 if (*debug & DEBUG_TIMER)
98 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
99 filep, buf, (int)count, off);
100 if (*off != filep->f_pos)
103 if (list_empty(&dev->expired) && (dev->work == 0)) {
104 if (filep->f_flags & O_NONBLOCK)
106 wait_event_interruptible(dev->wait, (dev->work ||
107 !list_empty(&dev->expired)));
108 if (signal_pending(current))
111 if (count < sizeof(int))
115 if (!list_empty(&dev->expired)) {
116 spin_lock_irqsave(&dev->lock, flags);
117 timer = (struct mISDNtimer *)dev->expired.next;
118 list_del(&timer->list);
119 spin_unlock_irqrestore(&dev->lock, flags);
120 if (put_user(timer->id, (int __user *)buf))
130 mISDN_poll(struct file *filep, poll_table *wait)
132 struct mISDNtimerdev *dev = filep->private_data;
133 unsigned int mask = POLLERR;
135 if (*debug & DEBUG_TIMER)
136 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
138 poll_wait(filep, &dev->wait, wait);
140 if (dev->work || !list_empty(&dev->expired))
141 mask |= (POLLIN | POLLRDNORM);
142 if (*debug & DEBUG_TIMER)
143 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
144 dev->work, list_empty(&dev->expired));
150 dev_expire_timer(unsigned long data)
152 struct mISDNtimer *timer = (void *)data;
155 spin_lock_irqsave(&timer->dev->lock, flags);
156 list_move_tail(&timer->list, &timer->dev->expired);
157 spin_unlock_irqrestore(&timer->dev->lock, flags);
158 wake_up_interruptible(&timer->dev->wait);
162 misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
166 struct mISDNtimer *timer;
170 wake_up_interruptible(&dev->wait);
173 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
176 spin_lock_irqsave(&dev->lock, flags);
177 timer->id = dev->next_id++;
178 if (dev->next_id < 0)
180 list_add_tail(&timer->list, &dev->pending);
181 spin_unlock_irqrestore(&dev->lock, flags);
183 timer->tl.data = (long)timer;
184 timer->tl.function = dev_expire_timer;
185 init_timer(&timer->tl);
186 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
187 add_timer(&timer->tl);
194 misdn_del_timer(struct mISDNtimerdev *dev, int id)
197 struct mISDNtimer *timer;
200 spin_lock_irqsave(&dev->lock, flags);
201 list_for_each_entry(timer, &dev->pending, list) {
202 if (timer->id == id) {
203 list_del_init(&timer->list);
204 /* RED-PEN AK: race -- timer can be still running on
205 * other CPU. Needs reference count I think
207 del_timer(&timer->tl);
214 spin_unlock_irqrestore(&dev->lock, flags);
219 mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
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__,
231 if (get_user(tout, (int __user *)arg)) {
235 id = misdn_add_timer(dev, tout);
236 if (*debug & DEBUG_TIMER)
237 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
243 if (put_user(id, (int __user *)arg))
247 if (get_user(id, (int __user *)arg)) {
251 if (*debug & DEBUG_TIMER)
252 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
253 id = misdn_del_timer(dev, id);
254 if (put_user(id, (int __user *)arg))
263 static const struct file_operations mISDN_fops = {
266 .ioctl = mISDN_ioctl,
268 .release = mISDN_close,
271 static struct miscdevice mISDNtimer = {
272 .minor = MISC_DYNAMIC_MINOR,
273 .name = "mISDNtimer",
278 mISDN_inittimer(u_int *deb)
283 err = misc_register(&mISDNtimer);
285 printk(KERN_WARNING "mISDN: Could not register timer device\n");
289 void mISDN_timer_cleanup(void)
291 misc_deregister(&mISDNtimer);