dvbdev.c 11.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * dvbdev.c
 *
 * Copyright (C) 2000 Ralph  Metzler <ralph@convergence.de>
 *                  & Marcus Metzler <marcus@convergence.de>
 *                    for convergence integrated media GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/cdev.h>
34
#include <linux/mutex.h>
35
#include <linux/smp_lock.h>
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45
#include "dvbdev.h"

static int dvbdev_debug;

module_param(dvbdev_debug, int, 0644);
MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");

#define dprintk if (dvbdev_debug) printk

static LIST_HEAD(dvb_adapter_list);
46
static DEFINE_MUTEX(dvbdev_register_lock);
L
Linus Torvalds 已提交
47 48

static const char * const dnames[] = {
49
	"video", "audio", "sec", "frontend", "demux", "dvr", "ca",
L
Linus Torvalds 已提交
50 51 52
	"net", "osd"
};

53 54 55 56
#ifdef CONFIG_DVB_DYNAMIC_MINORS
#define MAX_DVB_MINORS		256
#define DVB_MAX_IDS		MAX_DVB_MINORS
#else
L
Linus Torvalds 已提交
57 58 59
#define DVB_MAX_IDS		4
#define nums2minor(num,type,id)	((num << 6) | (id << 4) | type)
#define MAX_DVB_MINORS		(DVB_MAX_ADAPTERS*64)
60
#endif
L
Linus Torvalds 已提交
61

62
static struct class *dvb_class;
L
Linus Torvalds 已提交
63

64 65
static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
static DECLARE_RWSEM(minor_rwsem);
L
Linus Torvalds 已提交
66 67 68 69 70

static int dvb_device_open(struct inode *inode, struct file *file)
{
	struct dvb_device *dvbdev;

71
	lock_kernel();
72 73
	down_read(&minor_rwsem);
	dvbdev = dvb_minors[iminor(inode)];
L
Linus Torvalds 已提交
74 75 76

	if (dvbdev && dvbdev->fops) {
		int err = 0;
77
		const struct file_operations *old_fops;
L
Linus Torvalds 已提交
78 79 80

		file->private_data = dvbdev;
		old_fops = file->f_op;
81 82
		file->f_op = fops_get(dvbdev->fops);
		if(file->f_op->open)
83
			err = file->f_op->open(inode,file);
84
		if (err) {
85 86
			fops_put(file->f_op);
			file->f_op = fops_get(old_fops);
87 88
		}
		fops_put(old_fops);
89
		up_read(&minor_rwsem);
90
		unlock_kernel();
91
		return err;
L
Linus Torvalds 已提交
92
	}
93
	up_read(&minor_rwsem);
94
	unlock_kernel();
L
Linus Torvalds 已提交
95 96 97 98
	return -ENODEV;
}


99
static const struct file_operations dvb_device_fops =
L
Linus Torvalds 已提交
100 101 102 103 104
{
	.owner =	THIS_MODULE,
	.open =		dvb_device_open,
};

105
static struct cdev dvb_device_cdev;
L
Linus Torvalds 已提交
106 107 108

int dvb_generic_open(struct inode *inode, struct file *file)
{
109
	struct dvb_device *dvbdev = file->private_data;
L
Linus Torvalds 已提交
110

111 112
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
113 114

	if (!dvbdev->users)
115
		return -EBUSY;
L
Linus Torvalds 已提交
116 117

	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
118 119
		if (!dvbdev->readers)
			return -EBUSY;
L
Linus Torvalds 已提交
120 121
		dvbdev->readers--;
	} else {
122 123
		if (!dvbdev->writers)
			return -EBUSY;
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132 133 134
		dvbdev->writers--;
	}

	dvbdev->users--;
	return 0;
}
EXPORT_SYMBOL(dvb_generic_open);


int dvb_generic_release(struct inode *inode, struct file *file)
{
135
	struct dvb_device *dvbdev = file->private_data;
L
Linus Torvalds 已提交
136 137

	if (!dvbdev)
138
		return -ENODEV;
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
		dvbdev->readers++;
	} else {
		dvbdev->writers++;
	}

	dvbdev->users++;
	return 0;
}
EXPORT_SYMBOL(dvb_generic_release);


int dvb_generic_ioctl(struct inode *inode, struct file *file,
		      unsigned int cmd, unsigned long arg)
{
155
	struct dvb_device *dvbdev = file->private_data;
L
Linus Torvalds 已提交
156

157 158
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172

	if (!dvbdev->kernel_ioctl)
		return -EINVAL;

	return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
}
EXPORT_SYMBOL(dvb_generic_ioctl);


static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
{
	u32 id = 0;

	while (id < DVB_MAX_IDS) {
173 174
		struct dvb_device *dev;
		list_for_each_entry(dev, &adap->device_list, list_head)
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188
			if (dev->type == type && dev->id == id)
				goto skip;
		return id;
skip:
		id++;
	}
	return -ENFILE;
}


int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
			const struct dvb_device *template, void *priv, int type)
{
	struct dvb_device *dvbdev;
189
	struct file_operations *dvbdevfops;
190
	struct device *clsdev;
191
	int minor;
L
Linus Torvalds 已提交
192 193
	int id;

194
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
195

196
	if ((id = dvbdev_get_free_id (adap, type)) < 0){
197
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
198
		*pdvbdev = NULL;
199
		printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
L
Linus Torvalds 已提交
200 201 202 203 204
		return -ENFILE;
	}

	*pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);

205 206 207 208 209 210 211 212 213
	if (!dvbdev){
		mutex_unlock(&dvbdev_register_lock);
		return -ENOMEM;
	}

	dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);

	if (!dvbdevfops){
		kfree (dvbdev);
214
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222
		return -ENOMEM;
	}

	memcpy(dvbdev, template, sizeof(struct dvb_device));
	dvbdev->type = type;
	dvbdev->id = id;
	dvbdev->adapter = adap;
	dvbdev->priv = priv;
223
	dvbdev->fops = dvbdevfops;
224
	init_waitqueue_head (&dvbdev->wait_queue);
L
Linus Torvalds 已提交
225

226
	memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
L
Linus Torvalds 已提交
227 228 229 230
	dvbdev->fops->owner = adap->module;

	list_add_tail (&dvbdev->list_head, &adap->device_list);

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	down_write(&minor_rwsem);
#ifdef CONFIG_DVB_DYNAMIC_MINORS
	for (minor = 0; minor < MAX_DVB_MINORS; minor++)
		if (dvb_minors[minor] == NULL)
			break;

	if (minor == MAX_DVB_MINORS) {
		kfree(dvbdevfops);
		kfree(dvbdev);
		mutex_unlock(&dvbdev_register_lock);
		return -EINVAL;
	}
#else
	minor = nums2minor(adap->num, type, id);
#endif

	dvbdev->minor = minor;
	dvb_minors[minor] = dvbdev;
	up_write(&minor_rwsem);

251 252
	mutex_unlock(&dvbdev_register_lock);

253
	clsdev = device_create(dvb_class, adap->device,
254
			       MKDEV(DVB_MAJOR, minor,
255
			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
256 257
	if (IS_ERR(clsdev)) {
		printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
258
		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
259 260
		return PTR_ERR(clsdev);
	}
L
Linus Torvalds 已提交
261

262
	dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
263
		adap->num, dnames[type], id, minor, minor);
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274

	return 0;
}
EXPORT_SYMBOL(dvb_register_device);


void dvb_unregister_device(struct dvb_device *dvbdev)
{
	if (!dvbdev)
		return;

275 276 277 278 279
	down_write(&minor_rwsem);
	dvb_minors[dvbdev->minor] = NULL;
	up_write(&minor_rwsem);

	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
L
Linus Torvalds 已提交
280 281

	list_del (&dvbdev->list_head);
282
	kfree (dvbdev->fops);
L
Linus Torvalds 已提交
283 284 285 286
	kfree (dvbdev);
}
EXPORT_SYMBOL(dvb_unregister_device);

287 288 289 290 291 292 293 294 295 296 297
static int dvbdev_check_free_adapter_num(int num)
{
	struct list_head *entry;
	list_for_each(entry, &dvb_adapter_list) {
		struct dvb_adapter *adap;
		adap = list_entry(entry, struct dvb_adapter, list_head);
		if (adap->num == num)
			return 0;
	}
	return 1;
}
L
Linus Torvalds 已提交
298 299 300 301 302 303

static int dvbdev_get_free_adapter_num (void)
{
	int num = 0;

	while (num < DVB_MAX_ADAPTERS) {
304 305
		if (dvbdev_check_free_adapter_num(num))
			return num;
L
Linus Torvalds 已提交
306 307 308 309 310 311 312
		num++;
	}

	return -ENFILE;
}


313 314 315
int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
			 struct module *module, struct device *device,
			 short *adapter_nums)
L
Linus Torvalds 已提交
316
{
317
	int i, num;
L
Linus Torvalds 已提交
318

319
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334
	for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
		num = adapter_nums[i];
		if (num >= 0  &&  num < DVB_MAX_ADAPTERS) {
		/* use the one the driver asked for */
			if (dvbdev_check_free_adapter_num(num))
				break;
		} else {
			num = dvbdev_get_free_adapter_num();
			break;
		}
		num = -1;
	}

	if (num < 0) {
335
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
336 337 338 339 340 341
		return -ENFILE;
	}

	memset (adap, 0, sizeof(struct dvb_adapter));
	INIT_LIST_HEAD (&adap->device_list);

342
	printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
L
Linus Torvalds 已提交
343 344 345 346

	adap->num = num;
	adap->name = name;
	adap->module = module;
347
	adap->device = device;
348 349 350
	adap->mfe_shared = 0;
	adap->mfe_dvbdev = NULL;
	mutex_init (&adap->mfe_lock);
L
Linus Torvalds 已提交
351 352 353

	list_add_tail (&adap->list_head, &dvb_adapter_list);

354
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362

	return num;
}
EXPORT_SYMBOL(dvb_register_adapter);


int dvb_unregister_adapter(struct dvb_adapter *adap)
{
363
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
364
	list_del (&adap->list_head);
365
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
366 367 368 369 370 371 372 373 374 375
	return 0;
}
EXPORT_SYMBOL(dvb_unregister_adapter);

/* if the miracle happens and "generic_usercopy()" is included into
   the kernel, then this can vanish. please don't make the mistake and
   define this as video_usercopy(). this will introduce a dependecy
   to the v4l "videodev.o" module, which is unnecessary for some
   cards (ie. the budget dvb-cards don't need the v4l module...) */
int dvb_usercopy(struct inode *inode, struct file *file,
376
		     unsigned int cmd, unsigned long arg,
L
Linus Torvalds 已提交
377 378 379
		     int (*func)(struct inode *inode, struct file *file,
		     unsigned int cmd, void *arg))
{
380 381 382 383 384 385 386 387
	char    sbuf[128];
	void    *mbuf = NULL;
	void    *parg = NULL;
	int     err  = -EINVAL;

	/*  Copy arguments into temp kernel buffer  */
	switch (_IOC_DIR(cmd)) {
	case _IOC_NONE:
L
Linus Torvalds 已提交
388 389 390 391 392 393
		/*
		 * For this command, the pointer is actually an integer
		 * argument.
		 */
		parg = (void *) arg;
		break;
394 395 396 397
	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
	case _IOC_WRITE:
	case (_IOC_WRITE | _IOC_READ):
		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
398
			parg = sbuf;
399
		} else {
400 401 402 403 404
			/* too big to allocate from stack */
			mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
			if (NULL == mbuf)
				return -ENOMEM;
			parg = mbuf;
405 406 407 408
		}

		err = -EFAULT;
		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
409
			goto out;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		break;
	}

	/* call driver */
	if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
		err = -EINVAL;

	if (err < 0)
		goto out;

	/*  Copy results into user buffer  */
	switch (_IOC_DIR(cmd))
	{
	case _IOC_READ:
	case (_IOC_WRITE | _IOC_READ):
		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
426
			err = -EFAULT;
427 428
		break;
	}
L
Linus Torvalds 已提交
429 430

out:
431 432
	kfree(mbuf);
	return err;
L
Linus Torvalds 已提交
433 434
}

435 436 437 438 439 440 441 442 443
static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	struct dvb_device *dvbdev = dev_get_drvdata(dev);

	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
	add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
	return 0;
}

L
Linus Torvalds 已提交
444 445 446 447 448 449
static int __init init_dvbdev(void)
{
	int retval;
	dev_t dev = MKDEV(DVB_MAJOR, 0);

	if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
450
		printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
L
Linus Torvalds 已提交
451 452 453 454 455
		return retval;
	}

	cdev_init(&dvb_device_cdev, &dvb_device_fops);
	if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
456
		printk(KERN_ERR "dvb-core: unable register character device\n");
L
Linus Torvalds 已提交
457 458 459
		goto error;
	}

460
	dvb_class = class_create(THIS_MODULE, "dvb");
L
Linus Torvalds 已提交
461 462 463 464
	if (IS_ERR(dvb_class)) {
		retval = PTR_ERR(dvb_class);
		goto error;
	}
465
	dvb_class->dev_uevent = dvb_uevent;
L
Linus Torvalds 已提交
466 467 468 469 470 471 472 473 474 475 476
	return 0;

error:
	cdev_del(&dvb_device_cdev);
	unregister_chrdev_region(dev, MAX_DVB_MINORS);
	return retval;
}


static void __exit exit_dvbdev(void)
{
477
	class_destroy(dvb_class);
L
Linus Torvalds 已提交
478
	cdev_del(&dvb_device_cdev);
479
	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
L
Linus Torvalds 已提交
480 481
}

482
subsys_initcall(init_dvbdev);
L
Linus Torvalds 已提交
483 484 485 486 487
module_exit(exit_dvbdev);

MODULE_DESCRIPTION("DVB Core Driver");
MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
MODULE_LICENSE("GPL");