blob: ddb8adcd5fbb4bbbd6d574f660c16374e99cf6d1 [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 Bergmann76a64922010-07-11 11:18:53 +000027#include <linux/mutex.h>
Hannes Eder5b834352008-12-12 21:15:17 -080028#include "core.h"
Karsten Keil1b2b03f2008-07-27 01:54:58 +020029
Arnd Bergmann76a64922010-07-11 11:18:53 +000030static DEFINE_MUTEX(mISDN_mutex);
Hannes Ederdfa96ec2008-12-12 21:13:45 -080031static u_int *debug;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020032
33
34struct 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
43struct mISDNtimer {
44 struct list_head list;
45 struct mISDNtimerdev *dev;
46 struct timer_list tl;
47 int id;
48};
49
50static int
51mISDN_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 Morton6bff3382008-10-13 18:42:07 -070068 return nonseekable_open(ino, filep);
Karsten Keil1b2b03f2008-07-27 01:54:58 +020069}
70
71static int
72mISDN_close(struct inode *ino, struct file *filep)
73{
74 struct mISDNtimerdev *dev = filep->private_data;
Al Viroc08c4642013-04-15 16:31:13 -040075 struct list_head *list = &dev->pending;
Karsten Keil1b2b03f2008-07-27 01:54:58 +020076 struct mISDNtimer *timer, *next;
77
78 if (*debug & DEBUG_TIMER)
79 printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
Al Viroc08c4642013-04-15 16:31:13 -040080
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 Keil1b2b03f2008-07-27 01:54:58 +020089 kfree(timer);
90 }
Al Viroc08c4642013-04-15 16:31:13 -040091 spin_unlock_irq(&dev->lock);
92
Karsten Keil1b2b03f2008-07-27 01:54:58 +020093 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
101static ssize_t
Hannes Ederc46f0a22008-12-12 21:19:18 -0800102mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200103{
104 struct mISDNtimerdev *dev = filep->private_data;
105 struct mISDNtimer *timer;
106 u_long flags;
107 int ret = 0;
108
109 if (*debug & DEBUG_TIMER)
110 printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800111 filep, buf, (int)count, off);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200112
113 if (list_empty(&dev->expired) && (dev->work == 0)) {
114 if (filep->f_flags & O_NONBLOCK)
115 return -EAGAIN;
116 wait_event_interruptible(dev->wait, (dev->work ||
Joe Perches475be4d2012-02-19 19:52:38 -0800117 !list_empty(&dev->expired)));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200118 if (signal_pending(current))
119 return -ERESTARTSYS;
120 }
121 if (count < sizeof(int))
122 return -ENOSPC;
123 if (dev->work)
124 dev->work = 0;
125 if (!list_empty(&dev->expired)) {
126 spin_lock_irqsave(&dev->lock, flags);
127 timer = (struct mISDNtimer *)dev->expired.next;
128 list_del(&timer->list);
129 spin_unlock_irqrestore(&dev->lock, flags);
Hannes Ederc46f0a22008-12-12 21:19:18 -0800130 if (put_user(timer->id, (int __user *)buf))
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200131 ret = -EFAULT;
132 else
133 ret = sizeof(int);
134 kfree(timer);
135 }
136 return ret;
137}
138
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200139static unsigned int
140mISDN_poll(struct file *filep, poll_table *wait)
141{
142 struct mISDNtimerdev *dev = filep->private_data;
143 unsigned int mask = POLLERR;
144
145 if (*debug & DEBUG_TIMER)
146 printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
147 if (dev) {
148 poll_wait(filep, &dev->wait, wait);
149 mask = 0;
150 if (dev->work || !list_empty(&dev->expired))
151 mask |= (POLLIN | POLLRDNORM);
152 if (*debug & DEBUG_TIMER)
153 printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800154 dev->work, list_empty(&dev->expired));
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200155 }
156 return mask;
157}
158
159static void
Andi Kleence425a92008-09-22 19:18:15 -0700160dev_expire_timer(unsigned long data)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200161{
Andi Kleence425a92008-09-22 19:18:15 -0700162 struct mISDNtimer *timer = (void *)data;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200163 u_long flags;
164
165 spin_lock_irqsave(&timer->dev->lock, flags);
Al Viro1b108952013-04-15 16:55:41 -0400166 if (timer->id >= 0)
167 list_move_tail(&timer->list, &timer->dev->expired);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200168 spin_unlock_irqrestore(&timer->dev->lock, flags);
169 wake_up_interruptible(&timer->dev->wait);
170}
171
172static int
173misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
174{
Joe Perches475be4d2012-02-19 19:52:38 -0800175 int id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200176 struct mISDNtimer *timer;
177
178 if (!timeout) {
179 dev->work = 1;
180 wake_up_interruptible(&dev->wait);
181 id = 0;
182 } else {
183 timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
184 if (!timer)
185 return -ENOMEM;
Al Viro1678ec02013-04-15 17:04:04 -0400186 timer->dev = dev;
187 setup_timer(&timer->tl, dev_expire_timer, (long)timer);
188 spin_lock_irq(&dev->lock);
189 id = timer->id = dev->next_id++;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200190 if (dev->next_id < 0)
191 dev->next_id = 1;
192 list_add_tail(&timer->list, &dev->pending);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200193 timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
194 add_timer(&timer->tl);
Al Viro1678ec02013-04-15 17:04:04 -0400195 spin_unlock_irq(&dev->lock);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200196 }
197 return id;
198}
199
200static int
201misdn_del_timer(struct mISDNtimerdev *dev, int id)
202{
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200203 struct mISDNtimer *timer;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200204
Al Viro1b108952013-04-15 16:55:41 -0400205 spin_lock_irq(&dev->lock);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200206 list_for_each_entry(timer, &dev->pending, list) {
207 if (timer->id == id) {
208 list_del_init(&timer->list);
Al Viro1b108952013-04-15 16:55:41 -0400209 timer->id = -1;
210 spin_unlock_irq(&dev->lock);
211 del_timer_sync(&timer->tl);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200212 kfree(timer);
Al Viro1b108952013-04-15 16:55:41 -0400213 return id;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200214 }
215 }
Al Viro1b108952013-04-15 16:55:41 -0400216 spin_unlock_irq(&dev->lock);
217 return 0;
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200218}
219
Arnd Bergmann703c6312010-04-27 00:24:02 +0200220static long
221mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200222{
223 struct mISDNtimerdev *dev = filep->private_data;
224 int id, tout, ret = 0;
225
226
227 if (*debug & DEBUG_TIMER)
228 printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800229 filep, cmd, arg);
Arnd Bergmann76a64922010-07-11 11:18:53 +0000230 mutex_lock(&mISDN_mutex);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200231 switch (cmd) {
232 case IMADDTIMER:
233 if (get_user(tout, (int __user *)arg)) {
234 ret = -EFAULT;
235 break;
236 }
237 id = misdn_add_timer(dev, tout);
238 if (*debug & DEBUG_TIMER)
239 printk(KERN_DEBUG "%s add %d id %d\n", __func__,
Joe Perches475be4d2012-02-19 19:52:38 -0800240 tout, id);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200241 if (id < 0) {
242 ret = id;
243 break;
244 }
245 if (put_user(id, (int __user *)arg))
246 ret = -EFAULT;
247 break;
248 case IMDELTIMER:
249 if (get_user(id, (int __user *)arg)) {
250 ret = -EFAULT;
251 break;
252 }
253 if (*debug & DEBUG_TIMER)
254 printk(KERN_DEBUG "%s del id %d\n", __func__, id);
255 id = misdn_del_timer(dev, id);
256 if (put_user(id, (int __user *)arg))
257 ret = -EFAULT;
258 break;
259 default:
260 ret = -EINVAL;
261 }
Arnd Bergmann76a64922010-07-11 11:18:53 +0000262 mutex_unlock(&mISDN_mutex);
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200263 return ret;
264}
265
Karsten Keileac74af2009-05-22 11:04:56 +0000266static const struct file_operations mISDN_fops = {
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200267 .read = mISDN_read,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200268 .poll = mISDN_poll,
Arnd Bergmann703c6312010-04-27 00:24:02 +0200269 .unlocked_ioctl = mISDN_ioctl,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200270 .open = mISDN_open,
271 .release = mISDN_close,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200272 .llseek = no_llseek,
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200273};
274
275static struct miscdevice mISDNtimer = {
276 .minor = MISC_DYNAMIC_MINOR,
277 .name = "mISDNtimer",
278 .fops = &mISDN_fops,
279};
280
281int
Hannes Ederdfa96ec2008-12-12 21:13:45 -0800282mISDN_inittimer(u_int *deb)
Karsten Keil1b2b03f2008-07-27 01:54:58 +0200283{
284 int err;
285
286 debug = deb;
287 err = misc_register(&mISDNtimer);
288 if (err)
289 printk(KERN_WARNING "mISDN: Could not register timer device\n");
290 return err;
291}
292
293void mISDN_timer_cleanup(void)
294{
295 misc_deregister(&mISDNtimer);
296}