timerdev.c 6.8 KB
Newer Older
K
Karsten Keil 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *
 * general timer device for using in ISDN stacks
 *
 * Author	Karsten Keil <kkeil@novell.com>
 *
 * Copyright 2008  by Karsten Keil <kkeil@novell.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <linux/poll.h>
#include <linux/vmalloc.h>
22
#include <linux/slab.h>
K
Karsten Keil 已提交
23 24 25 26
#include <linux/timer.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mISDNif.h>
27
#include <linux/mutex.h>
28
#include "core.h"
K
Karsten Keil 已提交
29

30
static DEFINE_MUTEX(mISDN_mutex);
31
static u_int	*debug;
K
Karsten Keil 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67


struct mISDNtimerdev {
	int			next_id;
	struct list_head	pending;
	struct list_head	expired;
	wait_queue_head_t	wait;
	u_int			work;
	spinlock_t		lock; /* protect lists */
};

struct mISDNtimer {
	struct list_head	list;
	struct  mISDNtimerdev	*dev;
	struct timer_list	tl;
	int			id;
};

static int
mISDN_open(struct inode *ino, struct file *filep)
{
	struct mISDNtimerdev	*dev;

	if (*debug & DEBUG_TIMER)
		printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
	dev = kmalloc(sizeof(struct mISDNtimerdev) , GFP_KERNEL);
	if (!dev)
		return -ENOMEM;
	dev->next_id = 1;
	INIT_LIST_HEAD(&dev->pending);
	INIT_LIST_HEAD(&dev->expired);
	spin_lock_init(&dev->lock);
	dev->work = 0;
	init_waitqueue_head(&dev->wait);
	filep->private_data = dev;
	__module_get(THIS_MODULE);
A
Andrew Morton 已提交
68
	return nonseekable_open(ino, filep);
K
Karsten Keil 已提交
69 70 71 72 73 74
}

static int
mISDN_close(struct inode *ino, struct file *filep)
{
	struct mISDNtimerdev	*dev = filep->private_data;
75
	struct list_head	*list = &dev->pending;
K
Karsten Keil 已提交
76 77 78 79
	struct mISDNtimer	*timer, *next;

	if (*debug & DEBUG_TIMER)
		printk(KERN_DEBUG "%s(%p,%p)\n", __func__, ino, filep);
80 81 82 83 84 85 86 87 88

	spin_lock_irq(&dev->lock);
	while (!list_empty(list)) {
		timer = list_first_entry(list, struct mISDNtimer, list);
		spin_unlock_irq(&dev->lock);
		del_timer_sync(&timer->tl);
		spin_lock_irq(&dev->lock);
		/* it might have been moved to ->expired */
		list_del(&timer->list);
K
Karsten Keil 已提交
89 90
		kfree(timer);
	}
91 92
	spin_unlock_irq(&dev->lock);

K
Karsten Keil 已提交
93 94 95 96 97 98 99 100 101
	list_for_each_entry_safe(timer, next, &dev->expired, list) {
		kfree(timer);
	}
	kfree(dev);
	module_put(THIS_MODULE);
	return 0;
}

static ssize_t
102
mISDN_read(struct file *filep, char __user *buf, size_t count, loff_t *off)
K
Karsten Keil 已提交
103 104 105 106 107 108 109 110
{
	struct mISDNtimerdev	*dev = filep->private_data;
	struct mISDNtimer	*timer;
	u_long	flags;
	int	ret = 0;

	if (*debug & DEBUG_TIMER)
		printk(KERN_DEBUG "%s(%p, %p, %d, %p)\n", __func__,
111
		       filep, buf, (int)count, off);
K
Karsten Keil 已提交
112 113 114 115 116

	if (list_empty(&dev->expired) && (dev->work == 0)) {
		if (filep->f_flags & O_NONBLOCK)
			return -EAGAIN;
		wait_event_interruptible(dev->wait, (dev->work ||
117
						     !list_empty(&dev->expired)));
K
Karsten Keil 已提交
118 119 120 121 122 123 124 125 126 127 128 129
		if (signal_pending(current))
			return -ERESTARTSYS;
	}
	if (count < sizeof(int))
		return -ENOSPC;
	if (dev->work)
		dev->work = 0;
	if (!list_empty(&dev->expired)) {
		spin_lock_irqsave(&dev->lock, flags);
		timer = (struct mISDNtimer *)dev->expired.next;
		list_del(&timer->list);
		spin_unlock_irqrestore(&dev->lock, flags);
130
		if (put_user(timer->id, (int __user *)buf))
K
Karsten Keil 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
			ret = -EFAULT;
		else
			ret = sizeof(int);
		kfree(timer);
	}
	return ret;
}

static unsigned int
mISDN_poll(struct file *filep, poll_table *wait)
{
	struct mISDNtimerdev	*dev = filep->private_data;
	unsigned int		mask = POLLERR;

	if (*debug & DEBUG_TIMER)
		printk(KERN_DEBUG "%s(%p, %p)\n", __func__, filep, wait);
	if (dev) {
		poll_wait(filep, &dev->wait, wait);
		mask = 0;
		if (dev->work || !list_empty(&dev->expired))
			mask |= (POLLIN | POLLRDNORM);
		if (*debug & DEBUG_TIMER)
			printk(KERN_DEBUG "%s work(%d) empty(%d)\n", __func__,
154
			       dev->work, list_empty(&dev->expired));
K
Karsten Keil 已提交
155 156 157 158 159
	}
	return mask;
}

static void
A
Andi Kleen 已提交
160
dev_expire_timer(unsigned long data)
K
Karsten Keil 已提交
161
{
A
Andi Kleen 已提交
162
	struct mISDNtimer *timer = (void *)data;
K
Karsten Keil 已提交
163 164 165
	u_long			flags;

	spin_lock_irqsave(&timer->dev->lock, flags);
166 167
	if (timer->id >= 0)
		list_move_tail(&timer->list, &timer->dev->expired);
K
Karsten Keil 已提交
168 169 170 171 172 173 174
	spin_unlock_irqrestore(&timer->dev->lock, flags);
	wake_up_interruptible(&timer->dev->wait);
}

static int
misdn_add_timer(struct mISDNtimerdev *dev, int timeout)
{
175
	int			id;
K
Karsten Keil 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	u_long			flags;
	struct mISDNtimer	*timer;

	if (!timeout) {
		dev->work = 1;
		wake_up_interruptible(&dev->wait);
		id = 0;
	} else {
		timer = kzalloc(sizeof(struct mISDNtimer), GFP_KERNEL);
		if (!timer)
			return -ENOMEM;
		spin_lock_irqsave(&dev->lock, flags);
		timer->id = dev->next_id++;
		if (dev->next_id < 0)
			dev->next_id = 1;
		list_add_tail(&timer->list, &dev->pending);
		spin_unlock_irqrestore(&dev->lock, flags);
		timer->dev = dev;
		timer->tl.data = (long)timer;
A
Andi Kleen 已提交
195
		timer->tl.function = dev_expire_timer;
K
Karsten Keil 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208
		init_timer(&timer->tl);
		timer->tl.expires = jiffies + ((HZ * (u_long)timeout) / 1000);
		add_timer(&timer->tl);
		id = timer->id;
	}
	return id;
}

static int
misdn_del_timer(struct mISDNtimerdev *dev, int id)
{
	struct mISDNtimer	*timer;

209
	spin_lock_irq(&dev->lock);
K
Karsten Keil 已提交
210 211 212
	list_for_each_entry(timer, &dev->pending, list) {
		if (timer->id == id) {
			list_del_init(&timer->list);
213 214 215
			timer->id = -1;
			spin_unlock_irq(&dev->lock);
			del_timer_sync(&timer->tl);
K
Karsten Keil 已提交
216
			kfree(timer);
217
			return id;
K
Karsten Keil 已提交
218 219
		}
	}
220 221
	spin_unlock_irq(&dev->lock);
	return 0;
K
Karsten Keil 已提交
222 223
}

224 225
static long
mISDN_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
K
Karsten Keil 已提交
226 227 228 229 230 231 232
{
	struct mISDNtimerdev	*dev = filep->private_data;
	int			id, tout, ret = 0;


	if (*debug & DEBUG_TIMER)
		printk(KERN_DEBUG "%s(%p, %x, %lx)\n", __func__,
233
		       filep, cmd, arg);
234
	mutex_lock(&mISDN_mutex);
K
Karsten Keil 已提交
235 236 237 238 239 240 241 242 243
	switch (cmd) {
	case IMADDTIMER:
		if (get_user(tout, (int __user *)arg)) {
			ret = -EFAULT;
			break;
		}
		id = misdn_add_timer(dev, tout);
		if (*debug & DEBUG_TIMER)
			printk(KERN_DEBUG "%s add %d id %d\n", __func__,
244
			       tout, id);
K
Karsten Keil 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
		if (id < 0) {
			ret = id;
			break;
		}
		if (put_user(id, (int __user *)arg))
			ret = -EFAULT;
		break;
	case IMDELTIMER:
		if (get_user(id, (int __user *)arg)) {
			ret = -EFAULT;
			break;
		}
		if (*debug & DEBUG_TIMER)
			printk(KERN_DEBUG "%s del id %d\n", __func__, id);
		id = misdn_del_timer(dev, id);
		if (put_user(id, (int __user *)arg))
			ret = -EFAULT;
		break;
	default:
		ret = -EINVAL;
	}
266
	mutex_unlock(&mISDN_mutex);
K
Karsten Keil 已提交
267 268 269
	return ret;
}

K
Karsten Keil 已提交
270
static const struct file_operations mISDN_fops = {
K
Karsten Keil 已提交
271 272
	.read		= mISDN_read,
	.poll		= mISDN_poll,
273
	.unlocked_ioctl	= mISDN_ioctl,
K
Karsten Keil 已提交
274 275
	.open		= mISDN_open,
	.release	= mISDN_close,
276
	.llseek		= no_llseek,
K
Karsten Keil 已提交
277 278 279 280 281 282 283 284 285
};

static struct miscdevice mISDNtimer = {
	.minor	= MISC_DYNAMIC_MINOR,
	.name	= "mISDNtimer",
	.fops	= &mISDN_fops,
};

int
286
mISDN_inittimer(u_int *deb)
K
Karsten Keil 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300
{
	int	err;

	debug = deb;
	err = misc_register(&mISDNtimer);
	if (err)
		printk(KERN_WARNING "mISDN: Could not register timer device\n");
	return err;
}

void mISDN_timer_cleanup(void)
{
	misc_deregister(&mISDNtimer);
}