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