blob: f2b32186d4a19f2fbec632b5a7cf7c915a94134a [file] [log] [blame]
Karsten Keil1b2b03f2008-07-27 01:54:58 +02001/*
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/timer.h>
23#include <linux/miscdevice.h>
24#include <linux/module.h>
25#include <linux/mISDNif.h>
Hannes Eder5b834352008-12-12 21:15:17 -080026#include "core.h"
Karsten Keil1b2b03f2008-07-27 01:54:58 +020027
Hannes Ederdfa96ec2008-12-12 21:13:45 -080028static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020029
30
31struct mISDNtimerdev {
32 int next_id;
33 struct list_head pending;
34 struct list_head expired;
35 wait_queue_head_t wait;
36 u_int work;
37 spinlock_t lock; /* protect lists */
38};
39
40struct mISDNtimer {
41 struct list_head list;
42 struct mISDNtimerdev *dev;
43 struct timer_list tl;
44 int id;
45};
46
47static int
48mISDN_open(struct inode *ino, struct file *filep)
49{
50 struct mISDNtimerdev *dev;
51
52 if (*debug & DEBUG_TIMER)
53 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
54 dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
55 if (!dev)
56 return -ENOMEM;
57 dev->next_id = 1;
58 INIT_LIST_HEAD(&dev->pending);
59 INIT_LIST_HEAD(&dev->expired);
60 spin_lock_init(&dev->lock);
61 dev->work = 0;
62 init_waitqueue_head(&dev->wait);
63 filep->private_data = dev;
64 __module_get(THIS_MODULE);
Andrew Morton6bff3382008-10-13 18:42:07 -070065 return nonseekable_open(ino, filep);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020066}
67
68static int
69mISDN_close(struct inode *ino, struct file *filep)
70{
71 struct mISDNtimerdev *dev = filep->private_data;
72 struct mISDNtimer *timer, *next;
73
74 if (*debug & DEBUG_TIMER)
75 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
76 list_for_each_entry_safe(timer, next, &dev->pending, list) {
77 del_timer(&timer->tl);
78 kfree(timer);
79 }
80 list_for_each_entry_safe(timer, next, &dev->expired, list) {
81 kfree(timer);
82 }
83 kfree(dev);
84 module_put(THIS_MODULE);
85 return 0;
86}
87
88static ssize_t
Hannes Ederc46f0a22008-12-12 21:19:18 -080089mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020090{
91 struct mISDNtimerdev *dev = filep->private_data;
92 struct mISDNtimer *timer;
93 u_long flags;
94 int ret = 0;
95
96 if (*debug & DEBUG_TIMER)
97 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
98 filep, buf, (int)count, off);
99 if (*off != filep->f_pos)
100 return -ESPIPE;
101
102 if (list_empty(&dev->expired) && (dev->work == 0)) {
103 if (filep->f_flags & O_NONBLOCK)
104 return -EAGAIN;
105 wait_event_interruptible(dev->wait, (dev->work ||
106 !list_empty(&dev->expired)));
107 if (signal_pending(current))
108 return -ERESTARTSYS;
109 }
110 if (count < sizeof(int))
111 return -ENOSPC;
112 if (dev->work)
113 dev->work = 0;
114 if (!list_empty(&dev->expired)) {
115 spin_lock_irqsave(&dev->lock, flags);
116 timer = (struct mISDNtimer *)dev->expired.next;
117 list_del(&timer->list);
118 spin_unlock_irqrestore(&dev->lock, flags);
Hannes Ederc46f0a22008-12-12 21:19:18 -0800119 if (put_user(timer->id, (int __user *)buf))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200120 ret = -EFAULT;
121 else
122 ret = sizeof(int);
123 kfree(timer);
124 }
125 return ret;
126}
127
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200128static unsigned int
129mISDN_poll(struct file *filep, poll_table *wait)
130{
131 struct mISDNtimerdev *dev = filep->private_data;
132 unsigned int mask = POLLERR;
133
134 if (*debug & DEBUG_TIMER)
135 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
136 if (dev) {
137 poll_wait(filep, &dev->wait, wait);
138 mask = 0;
139 if (dev->work || !list_empty(&dev->expired))
140 mask |= (POLLIN | POLLRDNORM);
141 if (*debug & DEBUG_TIMER)
142 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
143 dev->work, list_empty(&dev->expired));
144 }
145 return mask;
146}
147
148static void
Andi Kleence425a92008-09-22 19:18:15 -0700149dev_expire_timer(unsigned long data)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200150{
Andi Kleence425a92008-09-22 19:18:15 -0700151 struct mISDNtimer *timer = (void *)data;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200152 u_long flags;
153
154 spin_lock_irqsave(&timer->dev->lock, flags);
155 list_del(&timer->list);
156 list_add_tail(&timer->list, &timer->dev->expired);
157 spin_unlock_irqrestore(&timer->dev->lock, flags);
158 wake_up_interruptible(&timer->dev->wait);
159}
160
161static int
162misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
163{
164 int id;
165 u_long flags;
166 struct mISDNtimer *timer;
167
168 if (!timeout) {
169 dev->work = 1;
170 wake_up_interruptible(&dev->wait);
171 id = 0;
172 } else {
173 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
174 if (!timer)
175 return -ENOMEM;
176 spin_lock_irqsave(&dev->lock, flags);
177 timer->id = dev->next_id++;
178 if (dev->next_id < 0)
179 dev->next_id = 1;
180 list_add_tail(&timer->list, &dev->pending);
181 spin_unlock_irqrestore(&dev->lock, flags);
182 timer->dev = dev;
183 timer->tl.data = (long)timer;
Andi Kleence425a92008-09-22 19:18:15 -0700184 timer->tl.function = dev_expire_timer;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200185 init_timer(&timer->tl);
186 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
187 add_timer(&timer->tl);
188 id = timer->id;
189 }
190 return id;
191}
192
193static int
194misdn_del_timer(struct mISDNtimerdev *dev, int id)
195{
196 u_long flags;
197 struct mISDNtimer *timer;
198 int ret = 0;
199
200 spin_lock_irqsave(&dev->lock, flags);
201 list_for_each_entry(timer, &dev->pending, list) {
202 if (timer->id == id) {
203 list_del_init(&timer->list);
Andi Kleence425a92008-09-22 19:18:15 -0700204 /* RED-PEN AK: race -- timer can be still running on
205 * other CPU. Needs reference count I think
206 */
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200207 del_timer(&timer->tl);
208 ret = timer->id;
209 kfree(timer);
210 goto unlock;
211 }
212 }
213unlock:
214 spin_unlock_irqrestore(&dev->lock, flags);
215 return ret;
216}
217
218static int
219mISDN_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
220 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 switch (cmd) {
230 case IMADDTIMER:
231 if (get_user(tout, (int __user *)arg)) {
232 ret = -EFAULT;
233 break;
234 }
235 id = misdn_add_timer(dev, tout);
236 if (*debug & DEBUG_TIMER)
237 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
238 tout, id);
239 if (id < 0) {
240 ret = id;
241 break;
242 }
243 if (put_user(id, (int __user *)arg))
244 ret = -EFAULT;
245 break;
246 case IMDELTIMER:
247 if (get_user(id, (int __user *)arg)) {
248 ret = -EFAULT;
249 break;
250 }
251 if (*debug & DEBUG_TIMER)
252 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
253 id = misdn_del_timer(dev, id);
254 if (put_user(id, (int __user *)arg))
255 ret = -EFAULT;
256 break;
257 default:
258 ret = -EINVAL;
259 }
260 return ret;
261}
262
263static struct file_operations mISDN_fops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200264 .read = mISDN_read,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200265 .poll = mISDN_poll,
266 .ioctl = mISDN_ioctl,
267 .open = mISDN_open,
268 .release = mISDN_close,
269};
270
271static struct miscdevice mISDNtimer = {
272 .minor = MISC_DYNAMIC_MINOR,
273 .name = "mISDNtimer",
274 .fops = &mISDN_fops,
275};
276
277int
Hannes Ederdfa96ec2008-12-12 21:13:45 -0800278mISDN_inittimer(u_int *deb)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200279{
280 int err;
281
282 debug = deb;
283 err = misc_register(&mISDNtimer);
284 if (err)
285 printk(KERN_WARNING "mISDN: Could not register timer device\n");
286 return err;
287}
288
289void mISDN_timer_cleanup(void)
290{
291 misc_deregister(&mISDNtimer);
292}