mISDN: misc timerdev fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / isdn / mISDN / timerdev.c
blobe7462924b5050fb70cad3ddfd9f607368d61e774
1 /*
3 * general timer device for using in ISDN stacks
5 * Author Karsten Keil <kkeil@novell.com>
7 * Copyright 2008 by Karsten Keil <kkeil@novell.com>
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.
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.
20 #include <linux/poll.h>
21 #include <linux/vmalloc.h>
22 #include <linux/timer.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
25 #include <linux/mISDNif.h>
27 static int *debug;
30 struct mISDNtimerdev {
31 int next_id;
32 struct list_head pending;
33 struct list_head expired;
34 wait_queue_head_t wait;
35 u_int work;
36 spinlock_t lock; /* protect lists */
39 struct mISDNtimer {
40 struct list_head list;
41 struct mISDNtimerdev *dev;
42 struct timer_list tl;
43 int id;
46 static int
47 mISDN_open(struct inode *ino, struct file *filep)
49 struct mISDNtimerdev *dev;
51 if (*debug & DEBUG_TIMER)
52 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
53 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
54 if (!dev)
55 return -ENOMEM;
56 dev->next_id = 1;
57 INIT_LIST_HEAD(&dev->pending);
58 INIT_LIST_HEAD(&dev->expired);
59 spin_lock_init(&dev->lock);
60 dev->work = 0;
61 init_waitqueue_head(&dev->wait);
62 filep->private_data = dev;
63 __module_get(THIS_MODULE);
64 return 0;
67 static int
68 mISDN_close(struct inode *ino, struct file *filep)
70 struct mISDNtimerdev *dev = filep->private_data;
71 struct mISDNtimer *timer, *next;
73 if (*debug & DEBUG_TIMER)
74 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
75 list_for_each_entry_safe(timer, next, &dev->pending, list) {
76 del_timer(&timer->tl);
77 kfree(timer);
79 list_for_each_entry_safe(timer, next, &dev->expired, list) {
80 kfree(timer);
82 kfree(dev);
83 module_put(THIS_MODULE);
84 return 0;
87 static ssize_t
88 mISDN_read(struct file *filep, char *buf, size_t count, loff_t *off)
90 struct mISDNtimerdev *dev = filep->private_data;
91 struct mISDNtimer *timer;
92 u_long flags;
93 int ret = 0;
95 if (*debug & DEBUG_TIMER)
96 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
97 filep, buf, (int)count, off);
98 if (*off != filep->f_pos)
99 return -ESPIPE;
101 if (list_empty(&dev->expired) && (dev->work == 0)) {
102 if (filep->f_flags & O_NONBLOCK)
103 return -EAGAIN;
104 wait_event_interruptible(dev->wait, (dev->work ||
105 !list_empty(&dev->expired)));
106 if (signal_pending(current))
107 return -ERESTARTSYS;
109 if (count < sizeof(int))
110 return -ENOSPC;
111 if (dev->work)
112 dev->work = 0;
113 if (!list_empty(&dev->expired)) {
114 spin_lock_irqsave(&dev->lock, flags);
115 timer = (struct mISDNtimer *)dev->expired.next;
116 list_del(&timer->list);
117 spin_unlock_irqrestore(&dev->lock, flags);
118 if (put_user(timer->id, (int *)buf))
119 ret = -EFAULT;
120 else
121 ret = sizeof(int);
122 kfree(timer);
124 return ret;
127 static unsigned int
128 mISDN_poll(struct file *filep, poll_table *wait)
130 struct mISDNtimerdev *dev = filep->private_data;
131 unsigned int mask = POLLERR;
133 if (*debug & DEBUG_TIMER)
134 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
135 if (dev) {
136 poll_wait(filep, &dev->wait, wait);
137 mask = 0;
138 if (dev->work || !list_empty(&dev->expired))
139 mask |= (POLLIN | POLLRDNORM);
140 if (*debug & DEBUG_TIMER)
141 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
142 dev->work, list_empty(&dev->expired));
144 return mask;
147 static void
148 dev_expire_timer(unsigned long data)
150 struct mISDNtimer *timer = (void *)data;
151 u_long flags;
153 spin_lock_irqsave(&timer->dev->lock, flags);
154 list_del(&timer->list);
155 list_add_tail(&timer->list, &timer->dev->expired);
156 spin_unlock_irqrestore(&timer->dev->lock, flags);
157 wake_up_interruptible(&timer->dev->wait);
160 static int
161 misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
163 int id;
164 u_long flags;
165 struct mISDNtimer *timer;
167 if (!timeout) {
168 dev->work = 1;
169 wake_up_interruptible(&dev->wait);
170 id = 0;
171 } else {
172 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
173 if (!timer)
174 return -ENOMEM;
175 spin_lock_irqsave(&dev->lock, flags);
176 timer->id = dev->next_id++;
177 if (dev->next_id < 0)
178 dev->next_id = 1;
179 list_add_tail(&timer->list, &dev->pending);
180 spin_unlock_irqrestore(&dev->lock, flags);
181 timer->dev = dev;
182 timer->tl.data = (long)timer;
183 timer->tl.function = dev_expire_timer;
184 init_timer(&timer->tl);
185 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
186 add_timer(&timer->tl);
187 id = timer->id;
189 return id;
192 static int
193 misdn_del_timer(struct mISDNtimerdev *dev, int id)
195 u_long flags;
196 struct mISDNtimer *timer;
197 int ret = 0;
199 spin_lock_irqsave(&dev->lock, flags);
200 list_for_each_entry(timer, &dev->pending, list) {
201 if (timer->id == id) {
202 list_del_init(&timer->list);
203 /* RED-PEN AK: race -- timer can be still running on
204 * other CPU. Needs reference count I think
206 del_timer(&timer->tl);
207 ret = timer->id;
208 kfree(timer);
209 goto unlock;
212 unlock:
213 spin_unlock_irqrestore(&dev->lock, flags);
214 return ret;
217 static int
218 mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
219 unsigned long arg)
221 struct mISDNtimerdev *dev = filep->private_data;
222 int id, tout, ret = 0;
225 if (*debug & DEBUG_TIMER)
226 printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
227 filep, cmd, arg);
228 switch (cmd) {
229 case IMADDTIMER:
230 if (get_user(tout, (int __user *)arg)) {
231 ret = -EFAULT;
232 break;
234 id = misdn_add_timer(dev, tout);
235 if (*debug & DEBUG_TIMER)
236 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
237 tout, id);
238 if (id < 0) {
239 ret = id;
240 break;
242 if (put_user(id, (int __user *)arg))
243 ret = -EFAULT;
244 break;
245 case IMDELTIMER:
246 if (get_user(id, (int __user *)arg)) {
247 ret = -EFAULT;
248 break;
250 if (*debug & DEBUG_TIMER)
251 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
252 id = misdn_del_timer(dev, id);
253 if (put_user(id, (int __user *)arg))
254 ret = -EFAULT;
255 break;
256 default:
257 ret = -EINVAL;
259 return ret;
262 static struct file_operations mISDN_fops = {
263 .read = mISDN_read,
264 .poll = mISDN_poll,
265 .ioctl = mISDN_ioctl,
266 .open = mISDN_open,
267 .release = mISDN_close,
270 static struct miscdevice mISDNtimer = {
271 .minor = MISC_DYNAMIC_MINOR,
272 .name = "mISDNtimer",
273 .fops = &mISDN_fops,
277 mISDN_inittimer(int *deb)
279 int err;
281 debug = deb;
282 err = misc_register(&mISDNtimer);
283 if (err)
284 printk(KERN_WARNING "mISDN: Could not register timer device\n");
285 return err;
288 void mISDN_timer_cleanup(void)
290 misc_deregister(&mISDNtimer);