at master 295 lines 6.7 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * 4 * general timer device for using in ISDN stacks 5 * 6 * Author Karsten Keil <kkeil@novell.com> 7 * 8 * Copyright 2008 by Karsten Keil <kkeil@novell.com> 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 23static DEFINE_MUTEX(mISDN_mutex); 24static u_int *debug; 25 26 27struct 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 36struct mISDNtimer { 37 struct list_head list; 38 struct mISDNtimerdev *dev; 39 struct timer_list tl; 40 int id; 41}; 42 43static int 44mISDN_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(sizeof(struct mISDNtimerdev) , GFP_KERNEL); 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 63static int 64mISDN_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 92static ssize_t 93mISDN_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 135static __poll_t 136mISDN_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 158static void 159dev_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 171static int 172misdn_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(sizeof(struct mISDNtimer), GFP_KERNEL); 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 199static int 200misdn_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 219static long 220mISDN_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 265static 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 274static struct miscdevice mISDNtimer = { 275 .minor = MISC_DYNAMIC_MINOR, 276 .name = "mISDNtimer", 277 .fops = &mISDN_fops, 278}; 279 280int 281mISDN_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 292void mISDN_timer_cleanup(void) 293{ 294 misc_deregister(&mISDNtimer); 295}