Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 23 | #include <linux/timer.h> |
| 24 | #include <linux/miscdevice.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/mISDNif.h> |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Hannes Eder | 5b83435 | 2008-12-12 21:15:17 -0800 | [diff] [blame] | 28 | #include "core.h" |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 29 | |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 30 | static DEFINE_MUTEX(mISDN_mutex); |
Hannes Eder | dfa96ec | 2008-12-12 21:13:45 -0800 | [diff] [blame] | 31 | static u_int *debug; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 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); |
Andrew Morton | 6bff338 | 2008-10-13 18:42:07 -0700 | [diff] [blame] | 68 | return nonseekable_open(ino, filep); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int |
| 72 | mISDN_close(struct inode *ino, struct file *filep) |
| 73 | { |
| 74 | struct mISDNtimerdev *dev = filep->private_data; |
Al Viro | c08c464 | 2013-04-15 16:31:13 -0400 | [diff] [blame] | 75 | struct list_head *list = &dev->pending; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 76 | struct mISDNtimer *timer, *next; |
| 77 | |
| 78 | if (*debug & DEBUG_TIMER) |
| 79 | printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep); |
Al Viro | c08c464 | 2013-04-15 16:31:13 -0400 | [diff] [blame] | 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); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 89 | kfree(timer); |
| 90 | } |
Al Viro | c08c464 | 2013-04-15 16:31:13 -0400 | [diff] [blame] | 91 | spin_unlock_irq(&dev->lock); |
| 92 | |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 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 |
Hannes Eder | c46f0a2 | 2008-12-12 21:19:18 -0800 | [diff] [blame] | 102 | mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 103 | { |
| 104 | struct mISDNtimerdev *dev = filep->private_data; |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 105 | struct list_head *list = &dev->expired; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 106 | struct mISDNtimer *timer; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 107 | int ret = 0; |
| 108 | |
| 109 | if (*debug & DEBUG_TIMER) |
| 110 | printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 111 | filep, buf, (int)count, off); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 112 | |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 113 | if (count < sizeof(int)) |
| 114 | return -ENOSPC; |
| 115 | |
| 116 | spin_lock_irq(&dev->lock); |
| 117 | while (list_empty(list) && (dev->work == 0)) { |
| 118 | spin_unlock_irq(&dev->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 119 | if (filep->f_flags & O_NONBLOCK) |
| 120 | return -EAGAIN; |
| 121 | wait_event_interruptible(dev->wait, (dev->work || |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 122 | !list_empty(list))); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 123 | if (signal_pending(current)) |
| 124 | return -ERESTARTSYS; |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 125 | spin_lock_irq(&dev->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 126 | } |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 127 | if (dev->work) |
| 128 | dev->work = 0; |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 129 | if (!list_empty(list)) { |
| 130 | timer = list_first_entry(list, struct mISDNtimer, list); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 131 | list_del(&timer->list); |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 132 | spin_unlock_irq(&dev->lock); |
Hannes Eder | c46f0a2 | 2008-12-12 21:19:18 -0800 | [diff] [blame] | 133 | if (put_user(timer->id, (int __user *)buf)) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 134 | ret = -EFAULT; |
| 135 | else |
| 136 | ret = sizeof(int); |
| 137 | kfree(timer); |
Al Viro | ebb06be | 2013-04-15 17:18:17 -0400 | [diff] [blame^] | 138 | } else { |
| 139 | spin_unlock_irq(&dev->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 140 | } |
| 141 | return ret; |
| 142 | } |
| 143 | |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 144 | static unsigned int |
| 145 | mISDN_poll(struct file *filep, poll_table *wait) |
| 146 | { |
| 147 | struct mISDNtimerdev *dev = filep->private_data; |
| 148 | unsigned int mask = POLLERR; |
| 149 | |
| 150 | if (*debug & DEBUG_TIMER) |
| 151 | printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait); |
| 152 | if (dev) { |
| 153 | poll_wait(filep, &dev->wait, wait); |
| 154 | mask = 0; |
| 155 | if (dev->work || !list_empty(&dev->expired)) |
| 156 | mask |= (POLLIN | POLLRDNORM); |
| 157 | if (*debug & DEBUG_TIMER) |
| 158 | printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 159 | dev->work, list_empty(&dev->expired)); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 160 | } |
| 161 | return mask; |
| 162 | } |
| 163 | |
| 164 | static void |
Andi Kleen | ce425a9 | 2008-09-22 19:18:15 -0700 | [diff] [blame] | 165 | dev_expire_timer(unsigned long data) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 166 | { |
Andi Kleen | ce425a9 | 2008-09-22 19:18:15 -0700 | [diff] [blame] | 167 | struct mISDNtimer *timer = (void *)data; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 168 | u_long flags; |
| 169 | |
| 170 | spin_lock_irqsave(&timer->dev->lock, flags); |
Al Viro | 1b10895 | 2013-04-15 16:55:41 -0400 | [diff] [blame] | 171 | if (timer->id >= 0) |
| 172 | list_move_tail(&timer->list, &timer->dev->expired); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 173 | spin_unlock_irqrestore(&timer->dev->lock, flags); |
| 174 | wake_up_interruptible(&timer->dev->wait); |
| 175 | } |
| 176 | |
| 177 | static int |
| 178 | misdn_add_timer(struct mISDNtimerdev *dev, int timeout) |
| 179 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 180 | int id; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 181 | struct mISDNtimer *timer; |
| 182 | |
| 183 | if (!timeout) { |
| 184 | dev->work = 1; |
| 185 | wake_up_interruptible(&dev->wait); |
| 186 | id = 0; |
| 187 | } else { |
| 188 | timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL); |
| 189 | if (!timer) |
| 190 | return -ENOMEM; |
Al Viro | 1678ec0 | 2013-04-15 17:04:04 -0400 | [diff] [blame] | 191 | timer->dev = dev; |
| 192 | setup_timer(&timer->tl, dev_expire_timer, (long)timer); |
| 193 | spin_lock_irq(&dev->lock); |
| 194 | id = timer->id = dev->next_id++; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 195 | if (dev->next_id < 0) |
| 196 | dev->next_id = 1; |
| 197 | list_add_tail(&timer->list, &dev->pending); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 198 | timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000); |
| 199 | add_timer(&timer->tl); |
Al Viro | 1678ec0 | 2013-04-15 17:04:04 -0400 | [diff] [blame] | 200 | spin_unlock_irq(&dev->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 201 | } |
| 202 | return id; |
| 203 | } |
| 204 | |
| 205 | static int |
| 206 | misdn_del_timer(struct mISDNtimerdev *dev, int id) |
| 207 | { |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 208 | struct mISDNtimer *timer; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 209 | |
Al Viro | 1b10895 | 2013-04-15 16:55:41 -0400 | [diff] [blame] | 210 | spin_lock_irq(&dev->lock); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 211 | list_for_each_entry(timer, &dev->pending, list) { |
| 212 | if (timer->id == id) { |
| 213 | list_del_init(&timer->list); |
Al Viro | 1b10895 | 2013-04-15 16:55:41 -0400 | [diff] [blame] | 214 | timer->id = -1; |
| 215 | spin_unlock_irq(&dev->lock); |
| 216 | del_timer_sync(&timer->tl); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 217 | kfree(timer); |
Al Viro | 1b10895 | 2013-04-15 16:55:41 -0400 | [diff] [blame] | 218 | return id; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
Al Viro | 1b10895 | 2013-04-15 16:55:41 -0400 | [diff] [blame] | 221 | spin_unlock_irq(&dev->lock); |
| 222 | return 0; |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Arnd Bergmann | 703c631 | 2010-04-27 00:24:02 +0200 | [diff] [blame] | 225 | static long |
| 226 | mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 227 | { |
| 228 | struct mISDNtimerdev *dev = filep->private_data; |
| 229 | int id, tout, ret = 0; |
| 230 | |
| 231 | |
| 232 | if (*debug & DEBUG_TIMER) |
| 233 | printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 234 | filep, cmd, arg); |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 235 | mutex_lock(&mISDN_mutex); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 236 | switch (cmd) { |
| 237 | case IMADDTIMER: |
| 238 | if (get_user(tout, (int __user *)arg)) { |
| 239 | ret = -EFAULT; |
| 240 | break; |
| 241 | } |
| 242 | id = misdn_add_timer(dev, tout); |
| 243 | if (*debug & DEBUG_TIMER) |
| 244 | printk(KERN_DEBUG "%s add %d id %d\n", __func__, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 245 | tout, id); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 246 | if (id < 0) { |
| 247 | ret = id; |
| 248 | break; |
| 249 | } |
| 250 | if (put_user(id, (int __user *)arg)) |
| 251 | ret = -EFAULT; |
| 252 | break; |
| 253 | case IMDELTIMER: |
| 254 | if (get_user(id, (int __user *)arg)) { |
| 255 | ret = -EFAULT; |
| 256 | break; |
| 257 | } |
| 258 | if (*debug & DEBUG_TIMER) |
| 259 | printk(KERN_DEBUG "%s del id %d\n", __func__, id); |
| 260 | id = misdn_del_timer(dev, id); |
| 261 | if (put_user(id, (int __user *)arg)) |
| 262 | ret = -EFAULT; |
| 263 | break; |
| 264 | default: |
| 265 | ret = -EINVAL; |
| 266 | } |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 267 | mutex_unlock(&mISDN_mutex); |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 268 | return ret; |
| 269 | } |
| 270 | |
Karsten Keil | eac74af | 2009-05-22 11:04:56 +0000 | [diff] [blame] | 271 | static const struct file_operations mISDN_fops = { |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 272 | .read = mISDN_read, |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 273 | .poll = mISDN_poll, |
Arnd Bergmann | 703c631 | 2010-04-27 00:24:02 +0200 | [diff] [blame] | 274 | .unlocked_ioctl = mISDN_ioctl, |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 275 | .open = mISDN_open, |
| 276 | .release = mISDN_close, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 277 | .llseek = no_llseek, |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | static struct miscdevice mISDNtimer = { |
| 281 | .minor = MISC_DYNAMIC_MINOR, |
| 282 | .name = "mISDNtimer", |
| 283 | .fops = &mISDN_fops, |
| 284 | }; |
| 285 | |
| 286 | int |
Hannes Eder | dfa96ec | 2008-12-12 21:13:45 -0800 | [diff] [blame] | 287 | mISDN_inittimer(u_int *deb) |
Karsten Keil | 1b2b03f | 2008-07-27 01:54:58 +0200 | [diff] [blame] | 288 | { |
| 289 | int err; |
| 290 | |
| 291 | debug = deb; |
| 292 | err = misc_register(&mISDNtimer); |
| 293 | if (err) |
| 294 | printk(KERN_WARNING "mISDN: Could not register timer device\n"); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | void mISDN_timer_cleanup(void) |
| 299 | { |
| 300 | misc_deregister(&mISDNtimer); |
| 301 | } |