1 // SPDX-License-Identifier: GPL-2.0-only
4 * general timer device for using in ISDN stacks
6 * Author Karsten Keil <kkeil@novell.com>
8 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
11 #include <linux/poll.h>
12 #include <linux/vmalloc.h>
13 #include <linux/slab.h>
14 #include <linux/timer.h>
15 #include <linux/miscdevice.h>
16 #include <linux/module.h>
17 #include <linux/mISDNif.h>
18 #include <linux/mutex.h>
19 #include <linux/sched/signal.h>
23 static DEFINE_MUTEX(mISDN_mutex
);
27 struct mISDNtimerdev
{
29 struct list_head pending
;
30 struct list_head expired
;
31 wait_queue_head_t wait
;
33 spinlock_t lock
; /* protect lists */
37 struct list_head list
;
38 struct mISDNtimerdev
*dev
;
44 mISDN_open(struct inode
*ino
, struct file
*filep
)
46 struct mISDNtimerdev
*dev
;
48 if (*debug
& DEBUG_TIMER
)
49 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
50 dev
= kmalloc(sizeof(struct mISDNtimerdev
) , GFP_KERNEL
);
54 INIT_LIST_HEAD(&dev
->pending
);
55 INIT_LIST_HEAD(&dev
->expired
);
56 spin_lock_init(&dev
->lock
);
58 init_waitqueue_head(&dev
->wait
);
59 filep
->private_data
= dev
;
60 return nonseekable_open(ino
, filep
);
64 mISDN_close(struct inode
*ino
, struct file
*filep
)
66 struct mISDNtimerdev
*dev
= filep
->private_data
;
67 struct list_head
*list
= &dev
->pending
;
68 struct mISDNtimer
*timer
, *next
;
70 if (*debug
& DEBUG_TIMER
)
71 printk(KERN_DEBUG
"%s(%p,%p)\n", __func__
, ino
, filep
);
73 spin_lock_irq(&dev
->lock
);
74 while (!list_empty(list
)) {
75 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
76 spin_unlock_irq(&dev
->lock
);
77 del_timer_sync(&timer
->tl
);
78 spin_lock_irq(&dev
->lock
);
79 /* it might have been moved to ->expired */
80 list_del(&timer
->list
);
83 spin_unlock_irq(&dev
->lock
);
85 list_for_each_entry_safe(timer
, next
, &dev
->expired
, list
) {
93 mISDN_read(struct file
*filep
, char __user
*buf
, size_t count
, loff_t
*off
)
95 struct mISDNtimerdev
*dev
= filep
->private_data
;
96 struct list_head
*list
= &dev
->expired
;
97 struct mISDNtimer
*timer
;
100 if (*debug
& DEBUG_TIMER
)
101 printk(KERN_DEBUG
"%s(%p, %p, %d, %p)\n", __func__
,
102 filep
, buf
, (int)count
, off
);
104 if (count
< sizeof(int))
107 spin_lock_irq(&dev
->lock
);
108 while (list_empty(list
) && (dev
->work
== 0)) {
109 spin_unlock_irq(&dev
->lock
);
110 if (filep
->f_flags
& O_NONBLOCK
)
112 wait_event_interruptible(dev
->wait
, (dev
->work
||
114 if (signal_pending(current
))
116 spin_lock_irq(&dev
->lock
);
120 if (!list_empty(list
)) {
121 timer
= list_first_entry(list
, struct mISDNtimer
, list
);
122 list_del(&timer
->list
);
123 spin_unlock_irq(&dev
->lock
);
124 if (put_user(timer
->id
, (int __user
*)buf
))
130 spin_unlock_irq(&dev
->lock
);
136 mISDN_poll(struct file
*filep
, poll_table
*wait
)
138 struct mISDNtimerdev
*dev
= filep
->private_data
;
139 __poll_t mask
= EPOLLERR
;
141 if (*debug
& DEBUG_TIMER
)
142 printk(KERN_DEBUG
"%s(%p, %p)\n", __func__
, filep
, wait
);
144 poll_wait(filep
, &dev
->wait
, wait
);
146 if (dev
->work
|| !list_empty(&dev
->expired
))
147 mask
|= (EPOLLIN
| EPOLLRDNORM
);
148 if (*debug
& DEBUG_TIMER
)
149 printk(KERN_DEBUG
"%s work(%d) empty(%d)\n", __func__
,
150 dev
->work
, list_empty(&dev
->expired
));
156 dev_expire_timer(struct timer_list
*t
)
158 struct mISDNtimer
*timer
= from_timer(timer
, t
, tl
);
161 spin_lock_irqsave(&timer
->dev
->lock
, flags
);
163 list_move_tail(&timer
->list
, &timer
->dev
->expired
);
164 wake_up_interruptible(&timer
->dev
->wait
);
165 spin_unlock_irqrestore(&timer
->dev
->lock
, flags
);
169 misdn_add_timer(struct mISDNtimerdev
*dev
, int timeout
)
172 struct mISDNtimer
*timer
;
176 wake_up_interruptible(&dev
->wait
);
179 timer
= kzalloc(sizeof(struct mISDNtimer
), GFP_KERNEL
);
183 timer_setup(&timer
->tl
, dev_expire_timer
, 0);
184 spin_lock_irq(&dev
->lock
);
185 id
= timer
->id
= dev
->next_id
++;
186 if (dev
->next_id
< 0)
188 list_add_tail(&timer
->list
, &dev
->pending
);
189 timer
->tl
.expires
= jiffies
+ ((HZ
* (u_long
)timeout
) / 1000);
190 add_timer(&timer
->tl
);
191 spin_unlock_irq(&dev
->lock
);
197 misdn_del_timer(struct mISDNtimerdev
*dev
, int id
)
199 struct mISDNtimer
*timer
;
201 spin_lock_irq(&dev
->lock
);
202 list_for_each_entry(timer
, &dev
->pending
, list
) {
203 if (timer
->id
== id
) {
204 list_del_init(&timer
->list
);
206 spin_unlock_irq(&dev
->lock
);
207 del_timer_sync(&timer
->tl
);
212 spin_unlock_irq(&dev
->lock
);
217 mISDN_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
219 struct mISDNtimerdev
*dev
= filep
->private_data
;
220 int id
, tout
, ret
= 0;
223 if (*debug
& DEBUG_TIMER
)
224 printk(KERN_DEBUG
"%s(%p, %x, %lx)\n", __func__
,
226 mutex_lock(&mISDN_mutex
);
229 if (get_user(tout
, (int __user
*)arg
)) {
233 id
= misdn_add_timer(dev
, tout
);
234 if (*debug
& DEBUG_TIMER
)
235 printk(KERN_DEBUG
"%s add %d id %d\n", __func__
,
241 if (put_user(id
, (int __user
*)arg
))
245 if (get_user(id
, (int __user
*)arg
)) {
249 if (*debug
& DEBUG_TIMER
)
250 printk(KERN_DEBUG
"%s del id %d\n", __func__
, id
);
251 id
= misdn_del_timer(dev
, id
);
252 if (put_user(id
, (int __user
*)arg
))
258 mutex_unlock(&mISDN_mutex
);
262 static const struct file_operations mISDN_fops
= {
263 .owner
= THIS_MODULE
,
266 .unlocked_ioctl
= mISDN_ioctl
,
268 .release
= mISDN_close
,
272 static struct miscdevice mISDNtimer
= {
273 .minor
= MISC_DYNAMIC_MINOR
,
274 .name
= "mISDNtimer",
279 mISDN_inittimer(u_int
*deb
)
284 err
= misc_register(&mISDNtimer
);
286 printk(KERN_WARNING
"mISDN: Could not register timer device\n");
290 void mISDN_timer_cleanup(void)
292 misc_deregister(&mISDNtimer
);