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>
30 struct mISDNtimerdev
{
32 struct list_head pending
;
33 struct list_head expired
;
34 wait_queue_head_t wait
;
36 spinlock_t lock
; /* protect lists */
40 struct list_head list
;
41 struct mISDNtimerdev
*dev
;
47 mISDN_open(struct inode
*ino
, struct file
*filep
)
49 struct mISDNtimerdev
*dev
;
51 if (*debug
& DEBUG_TIMER
)
52 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
53 dev
= kmalloc(sizeof(struct mISDNtimerdev
) , GFP_KERNEL
);
57 INIT_LIST_HEAD(&dev
->pending
);
58 INIT_LIST_HEAD(&dev
->expired
);
59 spin_lock_init(&dev
->lock
);
61 init_waitqueue_head(&dev
->wait
);
62 filep
->private_data
= dev
;
63 __module_get(THIS_MODULE
);
68 mISDN_close(struct inode
*ino
, struct file
*filep
)
70 struct mISDNtimerdev
*dev
= filep
->private_data
;
71 struct mISDNtimer
*timer
, *next
;
73 if (*debug
& DEBUG_TIMER
)
74 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
75 list_for_each_entry_safe(timer
, next
, &dev
->pending
, list
) {
76 del_timer(&timer
->tl
);
79 list_for_each_entry_safe(timer
, next
, &dev
->expired
, list
) {
83 module_put(THIS_MODULE
);
88 mISDN_read(struct file
*filep
, char *buf
, size_t count
, loff_t
*off
)
90 struct mISDNtimerdev
*dev
= filep
->private_data
;
91 struct mISDNtimer
*timer
;
95 if (*debug
& DEBUG_TIMER
)
96 printk(KERN_DEBUG
"%s(%p, %p, %d, %p)\n", __func__
,
97 filep
, buf
, (int)count
, off
);
98 if (*off
!= filep
->f_pos
)
101 if (list_empty(&dev
->expired
) && (dev
->work
== 0)) {
102 if (filep
->f_flags
& O_NONBLOCK
)
104 wait_event_interruptible(dev
->wait
, (dev
->work
||
105 !list_empty(&dev
->expired
)));
106 if (signal_pending(current
))
109 if (count
< sizeof(int))
113 if (!list_empty(&dev
->expired
)) {
114 spin_lock_irqsave(&dev
->lock
, flags
);
115 timer
= (struct mISDNtimer
*)dev
->expired
.next
;
116 list_del(&timer
->list
);
117 spin_unlock_irqrestore(&dev
->lock
, flags
);
118 if (put_user(timer
->id
, (int *)buf
))
128 mISDN_llseek(struct file
*filep
, loff_t offset
, int orig
)
134 mISDN_write(struct file
*filep
, const char *buf
, size_t count
, loff_t
*off
)
140 mISDN_poll(struct file
*filep
, poll_table
*wait
)
142 struct mISDNtimerdev
*dev
= filep
->private_data
;
143 unsigned int mask
= POLLERR
;
145 if (*debug
& DEBUG_TIMER
)
146 printk(KERN_DEBUG
"%s(%p, %p)\n", __func__
, filep
, wait
);
148 poll_wait(filep
, &dev
->wait
, wait
);
150 if (dev
->work
|| !list_empty(&dev
->expired
))
151 mask
|= (POLLIN
| POLLRDNORM
);
152 if (*debug
& DEBUG_TIMER
)
153 printk(KERN_DEBUG
"%s work(%d) empty(%d)\n", __func__
,
154 dev
->work
, list_empty(&dev
->expired
));
160 dev_expire_timer(struct mISDNtimer
*timer
)
164 spin_lock_irqsave(&timer
->dev
->lock
, flags
);
165 list_del(&timer
->list
);
166 list_add_tail(&timer
->list
, &timer
->dev
->expired
);
167 spin_unlock_irqrestore(&timer
->dev
->lock
, flags
);
168 wake_up_interruptible(&timer
->dev
->wait
);
172 misdn_add_timer(struct mISDNtimerdev
*dev
, int timeout
)
176 struct mISDNtimer
*timer
;
180 wake_up_interruptible(&dev
->wait
);
183 timer
= kzalloc(sizeof(struct mISDNtimer
), GFP_KERNEL
);
186 spin_lock_irqsave(&dev
->lock
, flags
);
187 timer
->id
= dev
->next_id
++;
188 if (dev
->next_id
< 0)
190 list_add_tail(&timer
->list
, &dev
->pending
);
191 spin_unlock_irqrestore(&dev
->lock
, flags
);
193 timer
->tl
.data
= (long)timer
;
194 timer
->tl
.function
= (void *) dev_expire_timer
;
195 init_timer(&timer
->tl
);
196 timer
->tl
.expires
= jiffies
+ ((HZ
* (u_long
)timeout
) / 1000);
197 add_timer(&timer
->tl
);
204 misdn_del_timer(struct mISDNtimerdev
*dev
, int id
)
207 struct mISDNtimer
*timer
;
210 spin_lock_irqsave(&dev
->lock
, flags
);
211 list_for_each_entry(timer
, &dev
->pending
, list
) {
212 if (timer
->id
== id
) {
213 list_del_init(&timer
->list
);
214 del_timer(&timer
->tl
);
221 spin_unlock_irqrestore(&dev
->lock
, flags
);
226 mISDN_ioctl(struct inode
*inode
, struct file
*filep
, unsigned int cmd
,
229 struct mISDNtimerdev
*dev
= filep
->private_data
;
230 int id
, tout
, ret
= 0;
233 if (*debug
& DEBUG_TIMER
)
234 printk(KERN_DEBUG
"%s(%p, %x, %lx)\n", __func__
,
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
))
270 static struct file_operations mISDN_fops
= {
271 .llseek
= mISDN_llseek
,
273 .write
= mISDN_write
,
275 .ioctl
= mISDN_ioctl
,
277 .release
= mISDN_close
,
280 static struct miscdevice mISDNtimer
= {
281 .minor
= MISC_DYNAMIC_MINOR
,
282 .name
= "mISDNtimer",
287 mISDN_inittimer(int *deb
)
292 err
= misc_register(&mISDNtimer
);
294 printk(KERN_WARNING
"mISDN: Could not register timer device\n");
298 void mISDN_timer_cleanup(void)
300 misc_deregister(&mISDNtimer
);