blob: c3243c913ec033b570c04678c6408efa8e13a694 [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);
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);
Hannes Ederc46f0a22008-12-12 21:19:18 -0800121 if (put_user(timer->id, (int __user *)buf))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200122 ret = -EFAULT;
123 else
124 ret = sizeof(int);
125 kfree(timer);
126 }
127 return ret;
128}
129
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200130static unsigned int
131mISDN_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
150static void
Andi Kleence425a92008-09-22 19:18:15 -0700151dev_expire_timer(unsigned long data)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200152{
Andi Kleence425a92008-09-22 19:18:15 -0700153 struct mISDNtimer *timer = (void *)data;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200154 u_long flags;
155
156 spin_lock_irqsave(&timer->dev->lock, flags);
Eric Sesterhenn211174e2009-02-26 22:38:15 -0800157 list_move_tail(&timer->list, &timer->dev->expired);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200158 spin_unlock_irqrestore(&timer->dev->lock, flags);
159 wake_up_interruptible(&timer->dev->wait);
160}
161
162static int
163misdn_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;
Andi Kleence425a92008-09-22 19:18:15 -0700185 timer->tl.function = dev_expire_timer;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200186 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
194static int
195misdn_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);
Andi Kleence425a92008-09-22 19:18:15 -0700205 /* RED-PEN AK: race -- timer can be still running on
206 * other CPU. Needs reference count I think
207 */
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200208 del_timer(&timer->tl);
209 ret = timer->id;
210 kfree(timer);
211 goto unlock;
212 }
213 }
214unlock:
215 spin_unlock_irqrestore(&dev->lock, flags);
216 return ret;
217}
218
Arnd Bergmann703c6312010-04-27 00:24:02 +0200219static long
220mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200221{
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);
Arnd Bergmann703c6312010-04-27 00:24:02 +0200229 lock_kernel();
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200230 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 }
Arnd Bergmann703c6312010-04-27 00:24:02 +0200261 unlock_kernel();
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200262 return ret;
263}
264
Karsten Keileac74af2009-05-22 11:04:56 +0000265static const struct file_operations mISDN_fops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200266 .read = mISDN_read,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200267 .poll = mISDN_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200268 .unlocked_ioctl = mISDN_ioctl,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200269 .open = mISDN_open,
270 .release = mISDN_close,
271};
272
273static struct miscdevice mISDNtimer = {
274 .minor = MISC_DYNAMIC_MINOR,
275 .name = "mISDNtimer",
276 .fops = &mISDN_fops,
277};
278
279int
Hannes Ederdfa96ec2008-12-12 21:13:45 -0800280mISDN_inittimer(u_int *deb)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200281{
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
291void mISDN_timer_cleanup(void)
292{
293 misc_deregister(&mISDNtimer);
294}