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>
28 #include <linux/sched/signal.h>
32 static DEFINE_MUTEX(mISDN_mutex
);
36 struct mISDNtimerdev
{
38 struct list_head pending
;
39 struct list_head expired
;
40 wait_queue_head_t wait
;
42 spinlock_t lock
; /* protect lists */
46 struct list_head list
;
47 struct mISDNtimerdev
*dev
;
53 mISDN_open(struct inode
*ino
, struct file
*filep
)
55 struct mISDNtimerdev
*dev
;
57 if (*debug
& DEBUG_TIMER
)
58 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
59 dev
= kmalloc(sizeof(struct mISDNtimerdev
) , GFP_KERNEL
);
63 INIT_LIST_HEAD(&dev
->pending
);
64 INIT_LIST_HEAD(&dev
->expired
);
65 spin_lock_init(&dev
->lock
);
67 init_waitqueue_head(&dev
->wait
);
68 filep
->private_data
= dev
;
69 return nonseekable_open(ino
, filep
);
73 mISDN_close(struct inode
*ino
, struct file
*filep
)
75 struct mISDNtimerdev
*dev
= filep
->private_data
;
76 struct list_head
*list
= &dev
->pending
;
77 struct mISDNtimer
*timer
, *next
;
79 if (*debug
& DEBUG_TIMER
)
80 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
82 spin_lock_irq(&dev
->lock
);
83 while (!list_empty(list
)) {
84 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
85 spin_unlock_irq(&dev
->lock
);
86 del_timer_sync(&timer
->tl
);
87 spin_lock_irq(&dev
->lock
);
88 /* it might have been moved to ->expired */
89 list_del(&timer
->list
);
92 spin_unlock_irq(&dev
->lock
);
94 list_for_each_entry_safe(timer
, next
, &dev
->expired
, list
) {
102 mISDN_read(struct file
*filep
, char __user
*buf
, size_t count
, loff_t
*off
)
104 struct mISDNtimerdev
*dev
= filep
->private_data
;
105 struct list_head
*list
= &dev
->expired
;
106 struct mISDNtimer
*timer
;
109 if (*debug
& DEBUG_TIMER
)
110 printk(KERN_DEBUG
"%s(%p, %p, %d, %p)\n", __func__
,
111 filep
, buf
, (int)count
, off
);
113 if (count
< sizeof(int))
116 spin_lock_irq(&dev
->lock
);
117 while (list_empty(list
) && (dev
->work
== 0)) {
118 spin_unlock_irq(&dev
->lock
);
119 if (filep
->f_flags
& O_NONBLOCK
)
121 wait_event_interruptible(dev
->wait
, (dev
->work
||
123 if (signal_pending(current
))
125 spin_lock_irq(&dev
->lock
);
129 if (!list_empty(list
)) {
130 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
131 list_del(&timer
->list
);
132 spin_unlock_irq(&dev
->lock
);
133 if (put_user(timer
->id
, (int __user
*)buf
))
139 spin_unlock_irq(&dev
->lock
);
145 mISDN_poll(struct file
*filep
, poll_table
*wait
)
147 struct mISDNtimerdev
*dev
= filep
->private_data
;
148 unsigned int mask
= POLLERR
;
150 if (*debug
& DEBUG_TIMER
)
151 printk(KERN_DEBUG
"%s(%p, %p)\n", __func__
, filep
, wait
);
153 poll_wait(filep
, &dev
->wait
, wait
);
155 if (dev
->work
|| !list_empty(&dev
->expired
))
156 mask
|= (POLLIN
| POLLRDNORM
);
157 if (*debug
& DEBUG_TIMER
)
158 printk(KERN_DEBUG
"%s work(%d) empty(%d)\n", __func__
,
159 dev
->work
, list_empty(&dev
->expired
));
165 dev_expire_timer(unsigned long data
)
167 struct mISDNtimer
*timer
= (void *)data
;
170 spin_lock_irqsave(&timer
->dev
->lock
, flags
);
172 list_move_tail(&timer
->list
, &timer
->dev
->expired
);
173 spin_unlock_irqrestore(&timer
->dev
->lock
, flags
);
174 wake_up_interruptible(&timer
->dev
->wait
);
178 misdn_add_timer(struct mISDNtimerdev
*dev
, int timeout
)
181 struct mISDNtimer
*timer
;
185 wake_up_interruptible(&dev
->wait
);
188 timer
= kzalloc(sizeof(struct mISDNtimer
), GFP_KERNEL
);
192 setup_timer(&timer
->tl
, dev_expire_timer
, (long)timer
);
193 spin_lock_irq(&dev
->lock
);
194 id
= timer
->id
= dev
->next_id
++;
195 if (dev
->next_id
< 0)
197 list_add_tail(&timer
->list
, &dev
->pending
);
198 timer
->tl
.expires
= jiffies
+ ((HZ
* (u_long
)timeout
) / 1000);
199 add_timer(&timer
->tl
);
200 spin_unlock_irq(&dev
->lock
);
206 misdn_del_timer(struct mISDNtimerdev
*dev
, int id
)
208 struct mISDNtimer
*timer
;
210 spin_lock_irq(&dev
->lock
);
211 list_for_each_entry(timer
, &dev
->pending
, list
) {
212 if (timer
->id
== id
) {
213 list_del_init(&timer
->list
);
215 spin_unlock_irq(&dev
->lock
);
216 del_timer_sync(&timer
->tl
);
221 spin_unlock_irq(&dev
->lock
);
226 mISDN_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
228 struct mISDNtimerdev
*dev
= filep
->private_data
;
229 int id
, tout
, ret
= 0;
232 if (*debug
& DEBUG_TIMER
)
233 printk(KERN_DEBUG
"%s(%p, %x, %lx)\n", __func__
,
235 mutex_lock(&mISDN_mutex
);
238 if (get_user(tout
, (int __user
*)arg
)) {
242 id
= misdn_add_timer(dev
, tout
);
243 if (*debug
& DEBUG_TIMER
)
244 printk(KERN_DEBUG
"%s add %d id %d\n", __func__
,
250 if (put_user(id
, (int __user
*)arg
))
254 if (get_user(id
, (int __user
*)arg
)) {
258 if (*debug
& DEBUG_TIMER
)
259 printk(KERN_DEBUG
"%s del id %d\n", __func__
, id
);
260 id
= misdn_del_timer(dev
, id
);
261 if (put_user(id
, (int __user
*)arg
))
267 mutex_unlock(&mISDN_mutex
);
271 static const struct file_operations mISDN_fops
= {
272 .owner
= THIS_MODULE
,
275 .unlocked_ioctl
= mISDN_ioctl
,
277 .release
= mISDN_close
,
281 static struct miscdevice mISDNtimer
= {
282 .minor
= MISC_DYNAMIC_MINOR
,
283 .name
= "mISDNtimer",
288 mISDN_inittimer(u_int
*deb
)
293 err
= misc_register(&mISDNtimer
);
295 printk(KERN_WARNING
"mISDN: Could not register timer device\n");
299 void mISDN_timer_cleanup(void)
301 misc_deregister(&mISDNtimer
);