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/timer.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/mISDNif.h>
31 struct mISDNtimerdev {
33 struct list_head pending;
34 struct list_head expired;
35 wait_queue_head_t wait;
37 spinlock_t lock; /* protect lists */
41 struct list_head list;
42 struct mISDNtimerdev *dev;
48 mISDN_open(struct inode *ino, struct file *filep)
50 struct mISDNtimerdev *dev;
52 if (*debug & DEBUG_TIMER)
53 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
54 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
58 INIT_LIST_HEAD(&dev->pending);
59 INIT_LIST_HEAD(&dev->expired);
60 spin_lock_init(&dev->lock);
62 init_waitqueue_head(&dev->wait);
63 filep->private_data = dev;
64 __module_get(THIS_MODULE);
65 return nonseekable_open(ino, filep);
69 mISDN_close(struct inode *ino, struct file *filep)
71 struct mISDNtimerdev *dev = filep->private_data;
72 struct mISDNtimer *timer, *next;
74 if (*debug & DEBUG_TIMER)
75 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
76 list_for_each_entry_safe(timer, next, &dev->pending, list) {
77 del_timer(&timer->tl);
80 list_for_each_entry_safe(timer, next, &dev->expired, list) {
84 module_put(THIS_MODULE);
89 mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
91 struct mISDNtimerdev *dev = filep->private_data;
92 struct mISDNtimer *timer;
96 if (*debug & DEBUG_TIMER)
97 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
98 filep, buf, (int)count, off);
99 if (*off != filep->f_pos)
102 if (list_empty(&dev->expired) && (dev->work == 0)) {
103 if (filep->f_flags & O_NONBLOCK)
105 wait_event_interruptible(dev->wait, (dev->work ||
106 !list_empty(&dev->expired)));
107 if (signal_pending(current))
110 if (count < sizeof(int))
114 if (!list_empty(&dev->expired)) {
115 spin_lock_irqsave(&dev->lock, flags);
116 timer = (struct mISDNtimer *)dev->expired.next;
117 list_del(&timer->list);
118 spin_unlock_irqrestore(&dev->lock, flags);
119 if (put_user(timer->id, (int __user *)buf))
129 mISDN_poll(struct file *filep, poll_table *wait)
131 struct mISDNtimerdev *dev = filep->private_data;
132 unsigned int mask = POLLERR;
134 if (*debug & DEBUG_TIMER)
135 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
137 poll_wait(filep, &dev->wait, wait);
139 if (dev->work || !list_empty(&dev->expired))
140 mask |= (POLLIN | POLLRDNORM);
141 if (*debug & DEBUG_TIMER)
142 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
143 dev->work, list_empty(&dev->expired));
149 dev_expire_timer(unsigned long data)
151 struct mISDNtimer *timer = (void *)data;
154 spin_lock_irqsave(&timer->dev->lock, flags);
155 list_move_tail(&timer->list, &timer->dev->expired);
156 spin_unlock_irqrestore(&timer->dev->lock, flags);
157 wake_up_interruptible(&timer->dev->wait);
161 misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
165 struct mISDNtimer *timer;
169 wake_up_interruptible(&dev->wait);
172 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
175 spin_lock_irqsave(&dev->lock, flags);
176 timer->id = dev->next_id++;
177 if (dev->next_id < 0)
179 list_add_tail(&timer->list, &dev->pending);
180 spin_unlock_irqrestore(&dev->lock, flags);
182 timer->tl.data = (long)timer;
183 timer->tl.function = dev_expire_timer;
184 init_timer(&timer->tl);
185 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
186 add_timer(&timer->tl);
193 misdn_del_timer(struct mISDNtimerdev *dev, int id)
196 struct mISDNtimer *timer;
199 spin_lock_irqsave(&dev->lock, flags);
200 list_for_each_entry(timer, &dev->pending, list) {
201 if (timer->id == id) {
202 list_del_init(&timer->list);
203 /* RED-PEN AK: race -- timer can be still running on
204 * other CPU. Needs reference count I think
206 del_timer(&timer->tl);
213 spin_unlock_irqrestore(&dev->lock, flags);
218 mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
221 struct mISDNtimerdev *dev = filep->private_data;
222 int id, tout, ret = 0;
225 if (*debug & DEBUG_TIMER)
226 printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
230 if (get_user(tout, (int __user *)arg)) {
234 id = misdn_add_timer(dev, tout);
235 if (*debug & DEBUG_TIMER)
236 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
242 if (put_user(id, (int __user *)arg))
246 if (get_user(id, (int __user *)arg)) {
250 if (*debug & DEBUG_TIMER)
251 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
252 id = misdn_del_timer(dev, id);
253 if (put_user(id, (int __user *)arg))
262 static struct file_operations mISDN_fops = {
265 .ioctl = mISDN_ioctl,
267 .release = mISDN_close,
270 static struct miscdevice mISDNtimer = {
271 .minor = MISC_DYNAMIC_MINOR,
272 .name = "mISDNtimer",
277 mISDN_inittimer(u_int *deb)
282 err = misc_register(&mISDNtimer);
284 printk(KERN_WARNING "mISDN: Could not register timer device\n");
288 void mISDN_timer_cleanup(void)
290 misc_deregister(&mISDNtimer);