blob: 81048b8ed8ad97b7890f631155c77264c35fd98d [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Karsten Keil1b2b03f2008-07-27 01:54:58 +020023#include <linux/timer.h>
24#include <linux/miscdevice.h>
25#include <linux/module.h>
26#include <linux/mISDNif.h>
Arnd Bergmann703c6312010-04-27 00:24:02 +020027#include <linux/smp_lock.h>
Hannes Eder5b834352008-12-12 21:15:17 -080028#include "core.h"
Karsten Keil1b2b03f2008-07-27 01:54:58 +020029
Hannes Ederdfa96ec2008-12-12 21:13:45 -080030static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020031
32
33struct 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
42struct mISDNtimer {
43 struct list_head list;
44 struct mISDNtimerdev *dev;
45 struct timer_list tl;
46 int id;
47};
48
49static int
50mISDN_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);
Andrew Morton6bff3382008-10-13 18:42:07 -070067 return nonseekable_open(ino, filep);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020068}
69
70static int
71mISDN_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
90static ssize_t
Hannes Ederc46f0a22008-12-12 21:19:18 -080091mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
Karsten Keil1b2b03f2008-07-27 01:54:58 +020092{
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);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200101
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);
Eric Sesterhenn211174e2009-02-26 22:38:15 -0800155 list_move_tail(&timer->list, &timer->dev->expired);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200156 spin_unlock_irqrestore(&timer->dev->lock, flags);
157 wake_up_interruptible(&timer->dev->wait);
158}
159
160static int
161misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
162{
163 int id;
164 u_long flags;
165 struct mISDNtimer *timer;
166
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;
Andi Kleence425a92008-09-22 19:18:15 -0700183 timer->tl.function = dev_expire_timer;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200184 init_timer(&timer->tl);
185 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
186 add_timer(&timer->tl);
187 id = timer->id;
188 }
189 return id;
190}
191
192static int
193misdn_del_timer(struct mISDNtimerdev *dev, int id)
194{
195 u_long flags;
196 struct mISDNtimer *timer;
197 int ret = 0;
198
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);
Andi Kleence425a92008-09-22 19:18:15 -0700203 /* RED-PEN AK: race -- timer can be still running on
204 * other CPU. Needs reference count I think
205 */
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200206 del_timer(&timer->tl);
207 ret = timer->id;
208 kfree(timer);
209 goto unlock;
210 }
211 }
212unlock:
213 spin_unlock_irqrestore(&dev->lock, flags);
214 return ret;
215}
216
Arnd Bergmann703c6312010-04-27 00:24:02 +0200217static long
218mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200219{
220 struct mISDNtimerdev *dev = filep->private_data;
221 int id, tout, ret = 0;
222
223
224 if (*debug & DEBUG_TIMER)
225 printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
226 filep, cmd, arg);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200227 lock_kernel();
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200228 switch (cmd) {
229 case IMADDTIMER:
230 if (get_user(tout, (int __user *)arg)) {
231 ret = -EFAULT;
232 break;
233 }
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;
241 }
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;
249 }
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;
258 }
Arnd Bergmann703c6312010-04-27 00:24:02 +0200259 unlock_kernel();
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200260 return ret;
261}
262
Karsten Keileac74af2009-05-22 11:04:56 +0000263static const 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,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200266 .unlocked_ioctl = mISDN_ioctl,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200267 .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}