| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* |
| 4 |
* general timer device for using in ISDN stacks |
| 5 |
* |
| 6 |
* Author Karsten Keil <[email protected]> |
| 7 |
* |
| 8 |
* Copyright 2008 by Karsten Keil <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 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> |
| 20 |
|
| 21 |
#include "core.h" |
| 22 |
|
| 23 |
static DEFINE_MUTEX(mISDN_mutex); |
| 24 |
static u_int *debug; |
| 25 |
|
| 26 |
|
| 27 |
struct mISDNtimerdev { |
| 28 |
int next_id; |
| 29 |
struct list_head pending; |
| 30 |
struct list_head expired; |
| 31 |
wait_queue_head_t wait; |
| 32 |
u_int work; |
| 33 |
spinlock_t lock; /* protect lists */ |
| 34 |
}; |
| 35 |
|
| 36 |
struct mISDNtimer { |
| 37 |
struct list_head list; |
| 38 |
struct mISDNtimerdev *dev; |
| 39 |
struct timer_list tl; |
| 40 |
int id; |
| 41 |
}; |
| 42 |
|
| 43 |
static int |
| 44 |
mISDN_open(struct inode *ino, struct file *filep) |
| 45 |
{ |
| 46 |
struct mISDNtimerdev *dev; |
| 47 |
|
| 48 |
if (*debug & DEBUG_TIMER) |
| 49 |
printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep); |
| 50 |
dev = kmalloc_obj(struct mISDNtimerdev); |
| 51 |
if (!dev) |
| 52 |
return -ENOMEM; |
| 53 |
dev->next_id = 1; |
| 54 |
INIT_LIST_HEAD(&dev->pending); |
| 55 |
INIT_LIST_HEAD(&dev->expired); |
| 56 |
spin_lock_init(&dev->lock); |
| 57 |
dev->work = 0; |
| 58 |
init_waitqueue_head(&dev->wait); |
| 59 |
filep->private_data = dev; |
| 60 |
return nonseekable_open(ino, filep); |
| 61 |
} |
| 62 |
|
| 63 |
static int |
| 64 |
mISDN_close(struct inode *ino, struct file *filep) |
| 65 |
{ |
| 66 |
struct mISDNtimerdev *dev = filep->private_data; |
| 67 |
struct list_head *list = &dev->pending; |
| 68 |
struct mISDNtimer *timer, *next; |
| 69 |
|
| 70 |
if (*debug & DEBUG_TIMER) |
| 71 |
printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep); |
| 72 |
|
| 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 |
timer_shutdown_sync(&timer->tl); |
| 78 |
spin_lock_irq(&dev->lock); |
| 79 |
/* it might have been moved to ->expired */ |
| 80 |
list_del(&timer->list); |
| 81 |
kfree(timer); |
| 82 |
} |
| 83 |
spin_unlock_irq(&dev->lock); |
| 84 |
|
| 85 |
list_for_each_entry_safe(timer, next, &dev->expired, list) { |
| 86 |
kfree(timer); |
| 87 |
} |
| 88 |
kfree(dev); |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
static ssize_t |
| 93 |
mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off) |
| 94 |
{ |
| 95 |
struct mISDNtimerdev *dev = filep->private_data; |
| 96 |
struct list_head *list = &dev->expired; |
| 97 |
struct mISDNtimer *timer; |
| 98 |
int ret = 0; |
| 99 |
|
| 100 |
if (*debug & DEBUG_TIMER) |
| 101 |
printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__, |
| 102 |
filep, buf, (int)count, off); |
| 103 |
|
| 104 |
if (count < sizeof(int)) |
| 105 |
return -ENOSPC; |
| 106 |
|
| 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) |
| 111 |
return -EAGAIN; |
| 112 |
wait_event_interruptible(dev->wait, (READ_ONCE(dev->work) || |
| 113 |
!list_empty(list))); |
| 114 |
if (signal_pending(current)) |
| 115 |
return -ERESTARTSYS; |
| 116 |
spin_lock_irq(&dev->lock); |
| 117 |
} |
| 118 |
if (dev->work) |
| 119 |
WRITE_ONCE(dev->work, 0); |
| 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)) |
| 125 |
ret = -EFAULT; |
| 126 |
else |
| 127 |
ret = sizeof(int); |
| 128 |
kfree(timer); |
| 129 |
} else { |
| 130 |
spin_unlock_irq(&dev->lock); |
| 131 |
} |
| 132 |
return ret; |
| 133 |
} |
| 134 |
|
| 135 |
static __poll_t |
| 136 |
mISDN_poll(struct file *filep, poll_table *wait) |
| 137 |
{ |
| 138 |
struct mISDNtimerdev *dev = filep->private_data; |
| 139 |
__poll_t mask = EPOLLERR; |
| 140 |
|
| 141 |
if (*debug & DEBUG_TIMER) |
| 142 |
printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait); |
| 143 |
if (dev) { |
| 144 |
u32 work; |
| 145 |
|
| 146 |
poll_wait(filep, &dev->wait, wait); |
| 147 |
mask = 0; |
| 148 |
work = READ_ONCE(dev->work); |
| 149 |
if (work || !list_empty(&dev->expired)) |
| 150 |
mask |= (EPOLLIN | EPOLLRDNORM); |
| 151 |
if (*debug & DEBUG_TIMER) |
| 152 |
printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__, |
| 153 |
work, list_empty(&dev->expired)); |
| 154 |
} |
| 155 |
return mask; |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
dev_expire_timer(struct timer_list *t) |
| 160 |
{ |
| 161 |
struct mISDNtimer *timer = timer_container_of(timer, t, tl); |
| 162 |
u_long flags; |
| 163 |
|
| 164 |
spin_lock_irqsave(&timer->dev->lock, flags); |
| 165 |
if (timer->id >= 0) |
| 166 |
list_move_tail(&timer->list, &timer->dev->expired); |
| 167 |
wake_up_interruptible(&timer->dev->wait); |
| 168 |
spin_unlock_irqrestore(&timer->dev->lock, flags); |
| 169 |
} |
| 170 |
|
| 171 |
static int |
| 172 |
misdn_add_timer(struct mISDNtimerdev *dev, int timeout) |
| 173 |
{ |
| 174 |
int id; |
| 175 |
struct mISDNtimer *timer; |
| 176 |
|
| 177 |
if (!timeout) { |
| 178 |
WRITE_ONCE(dev->work, 1); |
| 179 |
wake_up_interruptible(&dev->wait); |
| 180 |
id = 0; |
| 181 |
} else { |
| 182 |
timer = kzalloc_obj(struct mISDNtimer); |
| 183 |
if (!timer) |
| 184 |
return -ENOMEM; |
| 185 |
timer->dev = dev; |
| 186 |
timer_setup(&timer->tl, dev_expire_timer, 0); |
| 187 |
spin_lock_irq(&dev->lock); |
| 188 |
id = timer->id = dev->next_id++; |
| 189 |
if (dev->next_id < 0) |
| 190 |
dev->next_id = 1; |
| 191 |
list_add_tail(&timer->list, &dev->pending); |
| 192 |
timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000); |
| 193 |
add_timer(&timer->tl); |
| 194 |
spin_unlock_irq(&dev->lock); |
| 195 |
} |
| 196 |
return id; |
| 197 |
} |
| 198 |
|
| 199 |
static int |
| 200 |
misdn_del_timer(struct mISDNtimerdev *dev, int id) |
| 201 |
{ |
| 202 |
struct mISDNtimer *timer; |
| 203 |
|
| 204 |
spin_lock_irq(&dev->lock); |
| 205 |
list_for_each_entry(timer, &dev->pending, list) { |
| 206 |
if (timer->id == id) { |
| 207 |
list_del_init(&timer->list); |
| 208 |
timer->id = -1; |
| 209 |
spin_unlock_irq(&dev->lock); |
| 210 |
timer_shutdown_sync(&timer->tl); |
| 211 |
kfree(timer); |
| 212 |
return id; |
| 213 |
} |
| 214 |
} |
| 215 |
spin_unlock_irq(&dev->lock); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
static long |
| 220 |
mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
| 221 |
{ |
| 222 |
struct mISDNtimerdev *dev = filep->private_data; |
| 223 |
int id, tout, ret = 0; |
| 224 |
|
| 225 |
|
| 226 |
if (*debug & DEBUG_TIMER) |
| 227 |
printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__, |
| 228 |
filep, cmd, arg); |
| 229 |
mutex_lock(&mISDN_mutex); |
| 230 |
switch (cmd) { |
| 231 |
case IMADDTIMER: |
| 232 |
if (get_user(tout, (int __user *)arg)) { |
| 233 |
ret = -EFAULT; |
| 234 |
break; |
| 235 |
} |
| 236 |
id = misdn_add_timer(dev, tout); |
| 237 |
if (*debug & DEBUG_TIMER) |
| 238 |
printk(KERN_DEBUG "%s add %d id %d\n", __func__, |
| 239 |
tout, id); |
| 240 |
if (id < 0) { |
| 241 |
ret = id; |
| 242 |
break; |
| 243 |
} |
| 244 |
if (put_user(id, (int __user *)arg)) |
| 245 |
ret = -EFAULT; |
| 246 |
break; |
| 247 |
case IMDELTIMER: |
| 248 |
if (get_user(id, (int __user *)arg)) { |
| 249 |
ret = -EFAULT; |
| 250 |
break; |
| 251 |
} |
| 252 |
if (*debug & DEBUG_TIMER) |
| 253 |
printk(KERN_DEBUG "%s del id %d\n", __func__, id); |
| 254 |
id = misdn_del_timer(dev, id); |
| 255 |
if (put_user(id, (int __user *)arg)) |
| 256 |
ret = -EFAULT; |
| 257 |
break; |
| 258 |
default: |
| 259 |
ret = -EINVAL; |
| 260 |
} |
| 261 |
mutex_unlock(&mISDN_mutex); |
| 262 |
return ret; |
| 263 |
} |
| 264 |
|
| 265 |
static const struct file_operations mISDN_fops = { |
| 266 |
.owner = THIS_MODULE, |
| 267 |
.read = mISDN_read, |
| 268 |
.poll = mISDN_poll, |
| 269 |
.unlocked_ioctl = mISDN_ioctl, |
| 270 |
.open = mISDN_open, |
| 271 |
.release = mISDN_close, |
| 272 |
}; |
| 273 |
|
| 274 |
static struct miscdevice mISDNtimer = { |
| 275 |
.minor = MISC_DYNAMIC_MINOR, |
| 276 |
.name = "mISDNtimer", |
| 277 |
.fops = &mISDN_fops, |
| 278 |
}; |
| 279 |
|
| 280 |
int |
| 281 |
mISDN_inittimer(u_int *deb) |
| 282 |
{ |
| 283 |
int err; |
| 284 |
|
| 285 |
debug = deb; |
| 286 |
err = misc_register(&mISDNtimer); |
| 287 |
if (err) |
| 288 |
printk(KERN_WARNING "mISDN: Could not register timer device\n"); |
| 289 |
return err; |
| 290 |
} |
| 291 |
|
| 292 |
void mISDN_timer_cleanup(void) |
| 293 |
{ |
| 294 |
misc_deregister(&mISDNtimer); |
| 295 |
} |