1 /* 2 * 3 * general timer device for using in ISDN stacks 4 * 5 * Author Karsten Keil <kkeil@novell.com> 6 * 7 * Copyright 2008 by Karsten Keil <kkeil@novell.com> 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/smp_lock.h> 28 #include "core.h" 29 30 static u_int *debug; 31 32 33 struct mISDNtimerdev { 34 int next_id; 35 struct list_head pending; 36 struct list_head expired; 37 wait_queue_head_t wait; 38 u_int work; 39 spinlock_t lock; /* protect lists */ 40 }; 41 42 struct mISDNtimer { 43 struct list_head list; 44 struct mISDNtimerdev *dev; 45 struct timer_list tl; 46 int id; 47 }; 48 49 static int 50 mISDN_open(struct inode *ino, struct file *filep) 51 { 52 struct mISDNtimerdev *dev; 53 54 if (*debug & DEBUG_TIMER) 55 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep); 56 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL); 57 if (!dev) 58 return -ENOMEM; 59 dev->next_id = 1; 60 INIT_LIST_HEAD(&dev->pending); 61 INIT_LIST_HEAD(&dev->expired); 62 spin_lock_init(&dev->lock); 63 dev->work = 0; 64 init_waitqueue_head(&dev->wait); 65 filep->private_data = dev; 66 __module_get(THIS_MODULE); 67 return nonseekable_open(ino, filep); 68 } 69 70 static int 71 mISDN_close(struct inode *ino, struct file *filep) 72 { 73 struct mISDNtimerdev *dev = filep->private_data; 74 struct mISDNtimer *timer, *next; 75 76 if (*debug & DEBUG_TIMER) 77 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep); 78 list_for_each_entry_safe(timer, next, &dev->pending, list) { 79 del_timer(&timer->tl); 80 kfree(timer); 81 } 82 list_for_each_entry_safe(timer, next, &dev->expired, list) { 83 kfree(timer); 84 } 85 kfree(dev); 86 module_put(THIS_MODULE); 87 return 0; 88 } 89 90 static ssize_t 91 mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off) 92 { 93 struct mISDNtimerdev *dev = filep->private_data; 94 struct mISDNtimer *timer; 95 u_long flags; 96 int ret = 0; 97 98 if (*debug & DEBUG_TIMER) 99 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__, 100 filep, buf, (int)count, off); 101 if (*off != filep->f_pos) 102 return -ESPIPE; 103 104 if (list_empty(&dev->expired) && (dev->work == 0)) { 105 if (filep->f_flags & O_NONBLOCK) 106 return -EAGAIN; 107 wait_event_interruptible(dev->wait, (dev->work || 108 !list_empty(&dev->expired))); 109 if (signal_pending(current)) 110 return -ERESTARTSYS; 111 } 112 if (count < sizeof(int)) 113 return -ENOSPC; 114 if (dev->work) 115 dev->work = 0; 116 if (!list_empty(&dev->expired)) { 117 spin_lock_irqsave(&dev->lock, flags); 118 timer = (struct mISDNtimer *)dev->expired.next; 119 list_del(&timer->list); 120 spin_unlock_irqrestore(&dev->lock, flags); 121 if (put_user(timer->id, (int __user *)buf)) 122 ret = -EFAULT; 123 else 124 ret = sizeof(int); 125 kfree(timer); 126 } 127 return ret; 128 } 129 130 static unsigned int 131 mISDN_poll(struct file *filep, poll_table *wait) 132 { 133 struct mISDNtimerdev *dev = filep->private_data; 134 unsigned int mask = POLLERR; 135 136 if (*debug & DEBUG_TIMER) 137 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait); 138 if (dev) { 139 poll_wait(filep, &dev->wait, wait); 140 mask = 0; 141 if (dev->work || !list_empty(&dev->expired)) 142 mask |= (POLLIN | POLLRDNORM); 143 if (*debug & DEBUG_TIMER) 144 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__, 145 dev->work, list_empty(&dev->expired)); 146 } 147 return mask; 148 } 149 150 static void 151 dev_expire_timer(unsigned long data) 152 { 153 struct mISDNtimer *timer = (void *)data; 154 u_long flags; 155 156 spin_lock_irqsave(&timer->dev->lock, flags); 157 list_move_tail(&timer->list, &timer->dev->expired); 158 spin_unlock_irqrestore(&timer->dev->lock, flags); 159 wake_up_interruptible(&timer->dev->wait); 160 } 161 162 static int 163 misdn_add_timer(struct mISDNtimerdev *dev, int timeout) 164 { 165 int id; 166 u_long flags; 167 struct mISDNtimer *timer; 168 169 if (!timeout) { 170 dev->work = 1; 171 wake_up_interruptible(&dev->wait); 172 id = 0; 173 } else { 174 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL); 175 if (!timer) 176 return -ENOMEM; 177 spin_lock_irqsave(&dev->lock, flags); 178 timer->id = dev->next_id++; 179 if (dev->next_id < 0) 180 dev->next_id = 1; 181 list_add_tail(&timer->list, &dev->pending); 182 spin_unlock_irqrestore(&dev->lock, flags); 183 timer->dev = dev; 184 timer->tl.data = (long)timer; 185 timer->tl.function = dev_expire_timer; 186 init_timer(&timer->tl); 187 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000); 188 add_timer(&timer->tl); 189 id = timer->id; 190 } 191 return id; 192 } 193 194 static int 195 misdn_del_timer(struct mISDNtimerdev *dev, int id) 196 { 197 u_long flags; 198 struct mISDNtimer *timer; 199 int ret = 0; 200 201 spin_lock_irqsave(&dev->lock, flags); 202 list_for_each_entry(timer, &dev->pending, list) { 203 if (timer->id == id) { 204 list_del_init(&timer->list); 205 /* RED-PEN AK: race -- timer can be still running on 206 * other CPU. Needs reference count I think 207 */ 208 del_timer(&timer->tl); 209 ret = timer->id; 210 kfree(timer); 211 goto unlock; 212 } 213 } 214 unlock: 215 spin_unlock_irqrestore(&dev->lock, flags); 216 return ret; 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 lock_kernel(); 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 unlock_kernel(); 262 return ret; 263 } 264 265 static const struct file_operations mISDN_fops = { 266 .read = mISDN_read, 267 .poll = mISDN_poll, 268 .unlocked_ioctl = mISDN_ioctl, 269 .open = mISDN_open, 270 .release = mISDN_close, 271 }; 272 273 static struct miscdevice mISDNtimer = { 274 .minor = MISC_DYNAMIC_MINOR, 275 .name = "mISDNtimer", 276 .fops = &mISDN_fops, 277 }; 278 279 int 280 mISDN_inittimer(u_int *deb) 281 { 282 int err; 283 284 debug = deb; 285 err = misc_register(&mISDNtimer); 286 if (err) 287 printk(KERN_WARNING "mISDN: Could not register timer device\n"); 288 return err; 289 } 290 291 void mISDN_timer_cleanup(void) 292 { 293 misc_deregister(&mISDNtimer); 294 } 295