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/mutex.h>
30 static DEFINE_MUTEX(mISDN_mutex
);
34 struct mISDNtimerdev
{
36 struct list_head pending
;
37 struct list_head expired
;
38 wait_queue_head_t wait
;
40 spinlock_t lock
; /* protect lists */
44 struct list_head list
;
45 struct mISDNtimerdev
*dev
;
51 mISDN_open(struct inode
*ino
, struct file
*filep
)
53 struct mISDNtimerdev
*dev
;
55 if (*debug
& DEBUG_TIMER
)
56 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
57 dev
= kmalloc(sizeof(struct mISDNtimerdev
) , GFP_KERNEL
);
61 INIT_LIST_HEAD(&dev
->pending
);
62 INIT_LIST_HEAD(&dev
->expired
);
63 spin_lock_init(&dev
->lock
);
65 init_waitqueue_head(&dev
->wait
);
66 filep
->private_data
= dev
;
67 return nonseekable_open(ino
, filep
);
71 mISDN_close(struct inode
*ino
, struct file
*filep
)
73 struct mISDNtimerdev
*dev
= filep
->private_data
;
74 struct list_head
*list
= &dev
->pending
;
75 struct mISDNtimer
*timer
, *next
;
77 if (*debug
& DEBUG_TIMER
)
78 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
80 spin_lock_irq(&dev
->lock
);
81 while (!list_empty(list
)) {
82 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
83 spin_unlock_irq(&dev
->lock
);
84 del_timer_sync(&timer
->tl
);
85 spin_lock_irq(&dev
->lock
);
86 /* it might have been moved to ->expired */
87 list_del(&timer
->list
);
90 spin_unlock_irq(&dev
->lock
);
92 list_for_each_entry_safe(timer
, next
, &dev
->expired
, list
) {
100 mISDN_read(struct file
*filep
, char __user
*buf
, size_t count
, loff_t
*off
)
102 struct mISDNtimerdev
*dev
= filep
->private_data
;
103 struct list_head
*list
= &dev
->expired
;
104 struct mISDNtimer
*timer
;
107 if (*debug
& DEBUG_TIMER
)
108 printk(KERN_DEBUG
"%s(%p, %p, %d, %p)\n", __func__
,
109 filep
, buf
, (int)count
, off
);
111 if (count
< sizeof(int))
114 spin_lock_irq(&dev
->lock
);
115 while (list_empty(list
) && (dev
->work
== 0)) {
116 spin_unlock_irq(&dev
->lock
);
117 if (filep
->f_flags
& O_NONBLOCK
)
119 wait_event_interruptible(dev
->wait
, (dev
->work
||
121 if (signal_pending(current
))
123 spin_lock_irq(&dev
->lock
);
127 if (!list_empty(list
)) {
128 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
129 list_del(&timer
->list
);
130 spin_unlock_irq(&dev
->lock
);
131 if (put_user(timer
->id
, (int __user
*)buf
))
137 spin_unlock_irq(&dev
->lock
);
143 mISDN_poll(struct file
*filep
, poll_table
*wait
)
145 struct mISDNtimerdev
*dev
= filep
->private_data
;
146 unsigned int mask
= POLLERR
;
148 if (*debug
& DEBUG_TIMER
)
149 printk(KERN_DEBUG
"%s(%p, %p)\n", __func__
, filep
, wait
);
151 poll_wait(filep
, &dev
->wait
, wait
);
153 if (dev
->work
|| !list_empty(&dev
->expired
))
154 mask
|= (POLLIN
| POLLRDNORM
);
155 if (*debug
& DEBUG_TIMER
)
156 printk(KERN_DEBUG
"%s work(%d) empty(%d)\n", __func__
,
157 dev
->work
, list_empty(&dev
->expired
));
163 dev_expire_timer(unsigned long data
)
165 struct mISDNtimer
*timer
= (void *)data
;
168 spin_lock_irqsave(&timer
->dev
->lock
, flags
);
170 list_move_tail(&timer
->list
, &timer
->dev
->expired
);
171 spin_unlock_irqrestore(&timer
->dev
->lock
, flags
);
172 wake_up_interruptible(&timer
->dev
->wait
);
176 misdn_add_timer(struct mISDNtimerdev
*dev
, int timeout
)
179 struct mISDNtimer
*timer
;
183 wake_up_interruptible(&dev
->wait
);
186 timer
= kzalloc(sizeof(struct mISDNtimer
), GFP_KERNEL
);
190 setup_timer(&timer
->tl
, dev_expire_timer
, (long)timer
);
191 spin_lock_irq(&dev
->lock
);
192 id
= timer
->id
= dev
->next_id
++;
193 if (dev
->next_id
< 0)
195 list_add_tail(&timer
->list
, &dev
->pending
);
196 timer
->tl
.expires
= jiffies
+ ((HZ
* (u_long
)timeout
) / 1000);
197 add_timer(&timer
->tl
);
198 spin_unlock_irq(&dev
->lock
);
204 misdn_del_timer(struct mISDNtimerdev
*dev
, int id
)
206 struct mISDNtimer
*timer
;
208 spin_lock_irq(&dev
->lock
);
209 list_for_each_entry(timer
, &dev
->pending
, list
) {
210 if (timer
->id
== id
) {
211 list_del_init(&timer
->list
);
213 spin_unlock_irq(&dev
->lock
);
214 del_timer_sync(&timer
->tl
);
219 spin_unlock_irq(&dev
->lock
);
224 mISDN_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
226 struct mISDNtimerdev
*dev
= filep
->private_data
;
227 int id
, tout
, ret
= 0;
230 if (*debug
& DEBUG_TIMER
)
231 printk(KERN_DEBUG
"%s(%p, %x, %lx)\n", __func__
,
233 mutex_lock(&mISDN_mutex
);
236 if (get_user(tout
, (int __user
*)arg
)) {
240 id
= misdn_add_timer(dev
, tout
);
241 if (*debug
& DEBUG_TIMER
)
242 printk(KERN_DEBUG
"%s add %d id %d\n", __func__
,
248 if (put_user(id
, (int __user
*)arg
))
252 if (get_user(id
, (int __user
*)arg
)) {
256 if (*debug
& DEBUG_TIMER
)
257 printk(KERN_DEBUG
"%s del id %d\n", __func__
, id
);
258 id
= misdn_del_timer(dev
, id
);
259 if (put_user(id
, (int __user
*)arg
))
265 mutex_unlock(&mISDN_mutex
);
269 static const struct file_operations mISDN_fops
= {
270 .owner
= THIS_MODULE
,
273 .unlocked_ioctl
= mISDN_ioctl
,
275 .release
= mISDN_close
,
279 static struct miscdevice mISDNtimer
= {
280 .minor
= MISC_DYNAMIC_MINOR
,
281 .name
= "mISDNtimer",
286 mISDN_inittimer(u_int
*deb
)
291 err
= misc_register(&mISDNtimer
);
293 printk(KERN_WARNING
"mISDN: Could not register timer device\n");
297 void mISDN_timer_cleanup(void)
299 misc_deregister(&mISDNtimer
);