dvbdev.c 16.3 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>
L
Linus Torvalds 已提交
35 36
#include "dvbdev.h"

A
Arnd Bergmann 已提交
37
static DEFINE_MUTEX(dvbdev_mutex);
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45
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;

A
Arnd Bergmann 已提交
71
	mutex_lock(&dvbdev_mutex);
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 *new_fops;
L
Linus Torvalds 已提交
78

79 80
		new_fops = fops_get(dvbdev->fops);
		if (!new_fops)
L
Laurent Pinchart 已提交
81
			goto fail;
82 83 84
		file->private_data = dvbdev;
		replace_fops(file, new_fops);
		if (file->f_op->open)
85
			err = file->f_op->open(inode,file);
86
		up_read(&minor_rwsem);
A
Arnd Bergmann 已提交
87
		mutex_unlock(&dvbdev_mutex);
88
		return err;
L
Linus Torvalds 已提交
89
	}
L
Laurent Pinchart 已提交
90
fail:
91
	up_read(&minor_rwsem);
A
Arnd Bergmann 已提交
92
	mutex_unlock(&dvbdev_mutex);
L
Linus Torvalds 已提交
93 94 95 96
	return -ENODEV;
}


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

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

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

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

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

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

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


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

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

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

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


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

156 157
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
158 159 160 161

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

A
Arnd Bergmann 已提交
162
	return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171
}
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) {
172 173
		struct dvb_device *dev;
		list_for_each_entry(dev, &adap->device_list, list_head)
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182
			if (dev->type == type && dev->id == id)
				goto skip;
		return id;
skip:
		id++;
	}
	return -ENFILE;
}

183 184
static void dvb_create_media_entity(struct dvb_device *dvbdev,
				       int type, int minor)
185 186
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
187
	int ret = 0, npads;
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	switch (type) {
	case DVB_DEVICE_FRONTEND:
		npads = 2;
		break;
	case DVB_DEVICE_DEMUX:
		npads = 2;
		break;
	case DVB_DEVICE_CA:
		npads = 2;
		break;
	case DVB_DEVICE_NET:
		/*
		 * We should be creating entities for the MPE/ULE
		 * decapsulation hardware (or software implementation).
		 *
		 * However, the number of for the MPE/ULE decaps may not be
		 * fixed. As we don't have yet dynamic support for PADs at
		 * the Media Controller, let's not create the decap
		 * entities yet.
		 */
		return;
	default:
211
		return;
212
	}
213 214 215 216 217 218 219 220

	dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
	if (!dvbdev->entity)
		return;

	dvbdev->entity->info.dev.major = DVB_MAJOR;
	dvbdev->entity->info.dev.minor = minor;
	dvbdev->entity->name = dvbdev->name;
221 222 223 224 225 226 227 228 229 230

	if (npads) {
		dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
				       GFP_KERNEL);
		if (!dvbdev->pads) {
			kfree(dvbdev->entity);
			return;
		}
	}

231 232 233
	switch (type) {
	case DVB_DEVICE_FRONTEND:
		dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_FE;
234 235
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
236 237 238
		break;
	case DVB_DEVICE_DEMUX:
		dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_DEMUX;
239 240
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
241 242 243
		break;
	case DVB_DEVICE_CA:
		dvbdev->entity->type = MEDIA_ENT_T_DEVNODE_DVB_CA;
244 245
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
246 247 248 249 250 251 252
		break;
	default:
		kfree(dvbdev->entity);
		dvbdev->entity = NULL;
		return;
	}

253
	if (npads)
254
		ret = media_entity_init(dvbdev->entity, npads, dvbdev->pads);
255 256 257
	if (!ret)
		ret = media_device_register_entity(dvbdev->adapter->mdev,
						   dvbdev->entity);
258 259 260 261
	if (ret < 0) {
		printk(KERN_ERR
			"%s: media_device_register_entity failed for %s\n",
			__func__, dvbdev->entity->name);
262
		kfree(dvbdev->pads);
263 264 265 266 267
		kfree(dvbdev->entity);
		dvbdev->entity = NULL;
		return;
	}

268
	printk(KERN_DEBUG "%s: media entity '%s' registered.\n",
269 270 271
		__func__, dvbdev->entity->name);
#endif
}
L
Linus Torvalds 已提交
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
static void dvb_register_media_device(struct dvb_device *dvbdev,
				      int type, int minor)
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
	u32 intf_type;

	if (!dvbdev->adapter->mdev)
		return;

	dvb_create_media_entity(dvbdev, type, minor);

	switch (type) {
	case DVB_DEVICE_FRONTEND:
		intf_type = MEDIA_INTF_T_DVB_FE;
		break;
	case DVB_DEVICE_DEMUX:
		intf_type = MEDIA_INTF_T_DVB_DEMUX;
		break;
	case DVB_DEVICE_DVR:
		intf_type = MEDIA_INTF_T_DVB_DVR;
		break;
	case DVB_DEVICE_CA:
		intf_type = MEDIA_INTF_T_DVB_CA;
		break;
	case DVB_DEVICE_NET:
		intf_type = MEDIA_INTF_T_DVB_NET;
		break;
	default:
		return;
	}

	dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
						 intf_type, 0,
						 DVB_MAJOR, minor,
						 GFP_KERNEL);

	/*
	 * Create the "obvious" link, e. g. the ones that represent
	 * a direct association between an interface and an entity.
	 * Other links should be created elsewhere, like:
	 *		DVB FE intf    -> tuner
	 *		DVB demux intf -> dvr
	 */

	if (!dvbdev->entity || !dvbdev->intf_devnode)
		return;

	media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf, 0);

#endif
}

L
Linus Torvalds 已提交
325 326 327 328
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;
329
	struct file_operations *dvbdevfops;
330
	struct device *clsdev;
331
	int minor;
L
Linus Torvalds 已提交
332 333
	int id;

334
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
335

336
	if ((id = dvbdev_get_free_id (adap, type)) < 0){
337
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
338
		*pdvbdev = NULL;
339
		printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
L
Linus Torvalds 已提交
340 341 342 343 344
		return -ENFILE;
	}

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

345 346 347 348 349 350 351 352 353
	if (!dvbdev){
		mutex_unlock(&dvbdev_register_lock);
		return -ENOMEM;
	}

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

	if (!dvbdevfops){
		kfree (dvbdev);
354
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
355 356 357 358 359 360 361 362
		return -ENOMEM;
	}

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

366 367
	memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
	dvbdevfops->owner = adap->module;
L
Linus Torvalds 已提交
368 369 370

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

371 372 373 374 375 376 377 378 379
	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);
380
		up_write(&minor_rwsem);
381 382 383 384 385 386 387 388 389 390 391
		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);

392 393
	mutex_unlock(&dvbdev_register_lock);

394
	clsdev = device_create(dvb_class, adap->device,
395
			       MKDEV(DVB_MAJOR, minor),
396
			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
397 398
	if (IS_ERR(clsdev)) {
		printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
399
		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
400 401
		return PTR_ERR(clsdev);
	}
402
	dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
403
		adap->num, dnames[type], id, minor, minor);
L
Linus Torvalds 已提交
404

405 406
	dvb_register_media_device(dvbdev, type, minor);

L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416
	return 0;
}
EXPORT_SYMBOL(dvb_register_device);


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

417 418 419 420 421
	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 已提交
422

423 424 425 426
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
	if (dvbdev->entity) {
		media_device_unregister_entity(dvbdev->entity);
		kfree(dvbdev->entity);
427
		kfree(dvbdev->pads);
428 429 430
	}
#endif

L
Linus Torvalds 已提交
431
	list_del (&dvbdev->list_head);
432
	kfree (dvbdev->fops);
L
Linus Torvalds 已提交
433 434 435 436
	kfree (dvbdev);
}
EXPORT_SYMBOL(dvb_unregister_device);

437 438

#ifdef CONFIG_MEDIA_CONTROLLER_DVB
439 440 441
void dvb_create_media_graph(struct dvb_adapter *adap)
{
	struct media_device *mdev = adap->mdev;
442 443
	struct media_entity *entity, *tuner = NULL, *fe = NULL;
	struct media_entity *demux = NULL, *dvr = NULL, *ca = NULL;
444
	struct media_interface *intf;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	if (!mdev)
		return;

	media_device_for_each_entity(entity, mdev) {
		switch (entity->type) {
		case MEDIA_ENT_T_V4L2_SUBDEV_TUNER:
			tuner = entity;
			break;
		case MEDIA_ENT_T_DEVNODE_DVB_FE:
			fe = entity;
			break;
		case MEDIA_ENT_T_DEVNODE_DVB_DEMUX:
			demux = entity;
			break;
		case MEDIA_ENT_T_DEVNODE_DVB_DVR:
			dvr = entity;
			break;
		case MEDIA_ENT_T_DEVNODE_DVB_CA:
			ca = entity;
			break;
		}
	}

	if (tuner && fe)
470
		media_create_pad_link(tuner, 0, fe, 0, 0);
471 472

	if (fe && demux)
473
		media_create_pad_link(fe, 1, demux, 0, MEDIA_LNK_FL_ENABLED);
474 475

	if (demux && dvr)
476
		media_create_pad_link(demux, 1, dvr, 0, MEDIA_LNK_FL_ENABLED);
477 478

	if (demux && ca)
479
		media_create_pad_link(demux, 1, ca, 0, MEDIA_LNK_FL_ENABLED);
480 481 482 483 484 485 486 487 488 489

	/* Create indirect interface links for FE->tuner, DVR->demux and CA->ca */
	list_for_each_entry(intf, &mdev->interfaces, list) {
		if (intf->type == MEDIA_INTF_T_DVB_CA && ca)
			media_create_intf_link(ca, intf, 0);
		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner)
			media_create_intf_link(tuner, intf, 0);
		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux)
			media_create_intf_link(demux, intf, 0);
	}
490 491
}
EXPORT_SYMBOL_GPL(dvb_create_media_graph);
492
#endif
493

494 495 496 497 498 499 500 501 502 503 504
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 已提交
505 506 507 508 509 510

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

	while (num < DVB_MAX_ADAPTERS) {
511 512
		if (dvbdev_check_free_adapter_num(num))
			return num;
L
Linus Torvalds 已提交
513 514 515 516 517 518 519
		num++;
	}

	return -ENFILE;
}


520 521 522
int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
			 struct module *module, struct device *device,
			 short *adapter_nums)
L
Linus Torvalds 已提交
523
{
524
	int i, num;
L
Linus Torvalds 已提交
525

526
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541
	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) {
542
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
543 544 545 546 547 548
		return -ENFILE;
	}

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

549
	printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
L
Linus Torvalds 已提交
550 551 552 553

	adap->num = num;
	adap->name = name;
	adap->module = module;
554
	adap->device = device;
555 556 557
	adap->mfe_shared = 0;
	adap->mfe_dvbdev = NULL;
	mutex_init (&adap->mfe_lock);
L
Linus Torvalds 已提交
558 559 560

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

561
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569

	return num;
}
EXPORT_SYMBOL(dvb_register_adapter);


int dvb_unregister_adapter(struct dvb_adapter *adap)
{
570
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
571
	list_del (&adap->list_head);
572
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581
	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...) */
582
int dvb_usercopy(struct file *file,
583
		     unsigned int cmd, unsigned long arg,
584
		     int (*func)(struct file *file,
L
Linus Torvalds 已提交
585 586
		     unsigned int cmd, void *arg))
{
587 588 589 590 591 592 593 594
	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 已提交
595 596 597 598 599 600
		/*
		 * For this command, the pointer is actually an integer
		 * argument.
		 */
		parg = (void *) arg;
		break;
601 602 603 604
	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
	case _IOC_WRITE:
	case (_IOC_WRITE | _IOC_READ):
		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
605
			parg = sbuf;
606
		} else {
607 608 609 610 611
			/* too big to allocate from stack */
			mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
			if (NULL == mbuf)
				return -ENOMEM;
			parg = mbuf;
612 613 614 615
		}

		err = -EFAULT;
		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
616
			goto out;
617 618 619 620
		break;
	}

	/* call driver */
621
	if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
622
		err = -ENOTTY;
623 624 625 626 627 628 629 630 631 632

	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)))
633
			err = -EFAULT;
634 635
		break;
	}
L
Linus Torvalds 已提交
636 637

out:
638 639
	kfree(mbuf);
	return err;
L
Linus Torvalds 已提交
640 641
}

642 643 644 645 646
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_ADAPTER_NUM=%d", dvbdev->adapter->num);
647 648
	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
649 650 651
	return 0;
}

652
static char *dvb_devnode(struct device *dev, umode_t *mode)
653 654 655 656 657 658 659 660
{
	struct dvb_device *dvbdev = dev_get_drvdata(dev);

	return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
		dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
}


L
Linus Torvalds 已提交
661 662 663 664 665 666
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) {
667
		printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
L
Linus Torvalds 已提交
668 669 670 671 672
		return retval;
	}

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

677
	dvb_class = class_create(THIS_MODULE, "dvb");
L
Linus Torvalds 已提交
678 679 680 681
	if (IS_ERR(dvb_class)) {
		retval = PTR_ERR(dvb_class);
		goto error;
	}
682
	dvb_class->dev_uevent = dvb_uevent;
683
	dvb_class->devnode = dvb_devnode;
L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692 693 694
	return 0;

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


static void __exit exit_dvbdev(void)
{
695
	class_destroy(dvb_class);
L
Linus Torvalds 已提交
696
	cdev_del(&dvb_device_cdev);
697
	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
L
Linus Torvalds 已提交
698 699
}

700
subsys_initcall(init_dvbdev);
L
Linus Torvalds 已提交
701 702 703 704 705
module_exit(exit_dvbdev);

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