dvbdev.c 20.4 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"

37 38 39
/* Due to enum tuner_pad_index */
#include <media/tuner.h>

A
Arnd Bergmann 已提交
40
static DEFINE_MUTEX(dvbdev_mutex);
L
Linus Torvalds 已提交
41 42 43 44 45 46 47 48
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);
49
static DEFINE_MUTEX(dvbdev_register_lock);
L
Linus Torvalds 已提交
50 51

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

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

65
static struct class *dvb_class;
L
Linus Torvalds 已提交
66

67 68
static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
static DECLARE_RWSEM(minor_rwsem);
L
Linus Torvalds 已提交
69 70 71 72 73

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

A
Arnd Bergmann 已提交
74
	mutex_lock(&dvbdev_mutex);
75 76
	down_read(&minor_rwsem);
	dvbdev = dvb_minors[iminor(inode)];
L
Linus Torvalds 已提交
77 78 79

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

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


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

107
static struct cdev dvb_device_cdev;
L
Linus Torvalds 已提交
108 109 110

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

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

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

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

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


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

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

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

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


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

159 160
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
161 162 163 164

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

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

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static void dvb_media_device_free(struct dvb_device *dvbdev)
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
	if (dvbdev->entity) {
		media_device_unregister_entity(dvbdev->entity);
		kfree(dvbdev->entity);
		kfree(dvbdev->pads);
		dvbdev->entity = NULL;
		dvbdev->pads = NULL;
	}

	if (dvbdev->tsout_entity) {
		int i;

		for (i = 0; i < dvbdev->tsout_num_entities; i++) {
			media_device_unregister_entity(&dvbdev->tsout_entity[i]);
			kfree(dvbdev->tsout_entity[i].name);
		}
		kfree(dvbdev->tsout_entity);
		kfree(dvbdev->tsout_pads);
		dvbdev->tsout_entity = NULL;
		dvbdev->tsout_pads = NULL;

		dvbdev->tsout_num_entities = 0;
	}

	if (dvbdev->intf_devnode) {
		media_devnode_remove(dvbdev->intf_devnode);
		dvbdev->intf_devnode = NULL;
	}
#endif
}

static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
220 221 222 223 224 225 226 227
				    const char *name, int npads)
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
	int i, ret = 0;

	dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
				     GFP_KERNEL);
	if (!dvbdev->tsout_pads)
228 229
		return -ENOMEM;

230 231
	dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
				       GFP_KERNEL);
232 233 234 235 236
	if (!dvbdev->tsout_entity)
		return -ENOMEM;

	dvbdev->tsout_num_entities = npads;

237 238 239 240 241
	for (i = 0; i < npads; i++) {
		struct media_pad *pads = &dvbdev->tsout_pads[i];
		struct media_entity *entity = &dvbdev->tsout_entity[i];

		entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
242 243
		if (!entity->name)
			return -ENOMEM;
244

245
		entity->function = MEDIA_ENT_F_IO_DTV;
246 247 248 249
		pads->flags = MEDIA_PAD_FL_SINK;

		ret = media_entity_init(entity, 1, pads);
		if (ret < 0)
250
			return ret;
251 252 253 254

		ret = media_device_register_entity(dvbdev->adapter->mdev,
						   entity);
		if (ret < 0)
255
			return ret;
256 257
	}
#endif
258
	return 0;
259 260 261 262 263
}

#define DEMUX_TSOUT	"demux-tsout"
#define DVR_TSOUT	"dvr-tsout"

264 265
static int dvb_create_media_entity(struct dvb_device *dvbdev,
				   int type, int demux_sink_pads)
266 267
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
268
	int i, ret, npads;
269

270 271 272 273
	switch (type) {
	case DVB_DEVICE_FRONTEND:
		npads = 2;
		break;
274
	case DVB_DEVICE_DVR:
275 276 277
		ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
					      demux_sink_pads);
		return ret;
278
	case DVB_DEVICE_DEMUX:
279
		npads = 1 + demux_sink_pads;
280 281 282 283
		ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
					      demux_sink_pads);
		if (ret < 0)
			return ret;
284 285 286 287 288 289 290 291 292 293 294 295 296 297
		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.
		 */
298
		return 0;
299
	default:
300
		return 0;
301
	}
302 303 304

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

	dvbdev->entity->name = dvbdev->name;
308 309 310 311

	if (npads) {
		dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
				       GFP_KERNEL);
312 313
		if (!dvbdev->pads)
			return -ENOMEM;
314 315
	}

316 317
	switch (type) {
	case DVB_DEVICE_FRONTEND:
318
		dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
319 320
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
321 322
		break;
	case DVB_DEVICE_DEMUX:
323
		dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
324
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
325 326
		for (i = 1; i < npads; i++)
			dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
327 328
		break;
	case DVB_DEVICE_CA:
329
		dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
330 331
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
332 333
		break;
	default:
334
		/* Should never happen, as the first switch prevents it */
335
		kfree(dvbdev->entity);
336
		kfree(dvbdev->pads);
337
		dvbdev->entity = NULL;
338 339
		dvbdev->pads = NULL;
		return 0;
340 341
	}

342
	if (npads) {
343
		ret = media_entity_init(dvbdev->entity, npads, dvbdev->pads);
344 345
		if (ret)
			return ret;
346
	}
347 348 349 350
	ret = media_device_register_entity(dvbdev->adapter->mdev,
					   dvbdev->entity);
	if (ret)
		return (ret);
351

352
	printk(KERN_DEBUG "%s: media entity '%s' registered.\n",
353
		__func__, dvbdev->entity->name);
354

355
#endif
356
	return 0;
357
}
L
Linus Torvalds 已提交
358

359 360 361
static int dvb_register_media_device(struct dvb_device *dvbdev,
				     int type, int minor,
				     unsigned demux_sink_pads)
362 363
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
364
	struct media_link *link;
365
	u32 intf_type;
366
	int ret;
367 368

	if (!dvbdev->adapter->mdev)
369
		return 0;
370

371 372 373
	ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
	if (ret)
		return ret;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

	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:
392
		return 0;
393 394 395
	}

	dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
396
						    intf_type, 0,
397
						    DVB_MAJOR, minor);
398 399 400

	if (!dvbdev->intf_devnode)
		return -ENOMEM;
401 402 403 404 405 406 407 408 409

	/*
	 * 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
	 */

410 411
	if (!dvbdev->entity)
		return 0;
412

413 414 415 416
	link = media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf,
				      MEDIA_LNK_FL_ENABLED);
	if (!link)
		return -ENOMEM;
417
#endif
418
	return 0;
419 420
}

L
Linus Torvalds 已提交
421
int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
422 423
			const struct dvb_device *template, void *priv, int type,
			int demux_sink_pads)
L
Linus Torvalds 已提交
424 425
{
	struct dvb_device *dvbdev;
426
	struct file_operations *dvbdevfops;
427
	struct device *clsdev;
428
	int minor;
429
	int id, ret;
L
Linus Torvalds 已提交
430

431
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
432

433
	if ((id = dvbdev_get_free_id (adap, type)) < 0){
434
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
435
		*pdvbdev = NULL;
436
		printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
L
Linus Torvalds 已提交
437 438 439
		return -ENFILE;
	}

440
	*pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
L
Linus Torvalds 已提交
441

442 443 444 445 446 447 448 449 450
	if (!dvbdev){
		mutex_unlock(&dvbdev_register_lock);
		return -ENOMEM;
	}

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

	if (!dvbdevfops){
		kfree (dvbdev);
451
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459
		return -ENOMEM;
	}

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

463 464
	memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
	dvbdevfops->owner = adap->module;
L
Linus Torvalds 已提交
465 466 467

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

468 469 470 471 472 473 474 475 476
	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);
477
		up_write(&minor_rwsem);
478 479 480 481 482 483 484 485 486 487 488
		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);

489 490 491 492 493 494 495 496 497 498 499 500 501 502
	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
	if (ret) {
		printk(KERN_ERR
		      "%s: dvb_register_media_device failed to create the mediagraph\n",
		      __func__);

		dvb_media_device_free(dvbdev);
		kfree(dvbdevfops);
		kfree(dvbdev);
		up_write(&minor_rwsem);
		mutex_unlock(&dvbdev_register_lock);
		return ret;
	}

503 504
	mutex_unlock(&dvbdev_register_lock);

505
	clsdev = device_create(dvb_class, adap->device,
506
			       MKDEV(DVB_MAJOR, minor),
507
			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
508 509
	if (IS_ERR(clsdev)) {
		printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
510
		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
511 512
		return PTR_ERR(clsdev);
	}
513
	dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
514
		adap->num, dnames[type], id, minor, minor);
L
Linus Torvalds 已提交
515 516 517 518 519 520 521 522 523 524 525

	return 0;
}
EXPORT_SYMBOL(dvb_register_device);


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

526 527 528 529
	down_write(&minor_rwsem);
	dvb_minors[dvbdev->minor] = NULL;
	up_write(&minor_rwsem);

530
	dvb_media_device_free(dvbdev);
531

532
	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
533

L
Linus Torvalds 已提交
534
	list_del (&dvbdev->list_head);
535
	kfree (dvbdev->fops);
L
Linus Torvalds 已提交
536 537 538 539
	kfree (dvbdev);
}
EXPORT_SYMBOL(dvb_unregister_device);

540 541

#ifdef CONFIG_MEDIA_CONTROLLER_DVB
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

static int dvb_create_io_intf_links(struct dvb_adapter *adap,
				    struct media_interface *intf,
				    char *name)
{
	struct media_device *mdev = adap->mdev;
	struct media_entity *entity;
	struct media_link *link;

	media_device_for_each_entity(entity, mdev) {
		if (entity->function == MEDIA_ENT_F_IO_DTV) {
			if (strncmp(entity->name, name, strlen(name)))
				continue;
			link = media_create_intf_link(entity, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
	}
	return 0;
}

564
int dvb_create_media_graph(struct dvb_adapter *adap)
565 566
{
	struct media_device *mdev = adap->mdev;
567
	struct media_entity *entity, *tuner = NULL, *demod = NULL;
568
	struct media_entity *demux = NULL, *ca = NULL;
569
	struct media_link *link;
570
	struct media_interface *intf;
571 572
	unsigned demux_pad = 0;
	unsigned dvr_pad = 0;
573
	int ret;
574 575

	if (!mdev)
576
		return 0;
577 578

	media_device_for_each_entity(entity, mdev) {
579
		switch (entity->function) {
580
		case MEDIA_ENT_F_TUNER:
581 582
			tuner = entity;
			break;
583
		case MEDIA_ENT_F_DTV_DEMOD:
584
			demod = entity;
585
			break;
586
		case MEDIA_ENT_F_TS_DEMUX:
587 588
			demux = entity;
			break;
589
		case MEDIA_ENT_F_DTV_CA:
590 591 592 593 594
			ca = entity;
			break;
		}
	}

595 596 597 598 599 600
	if (tuner && demod) {
		ret = media_create_pad_link(tuner, TUNER_PAD_IF_OUTPUT,
					    demod, 0, MEDIA_LNK_FL_ENABLED);
		if (ret)
			return ret;
	}
601

602 603 604 605 606 607 608 609 610 611 612 613
	if (demod && demux) {
		ret = media_create_pad_link(demod, 1, demux,
					    0, MEDIA_LNK_FL_ENABLED);
		if (ret)
			return -ENOMEM;
	}
	if (demux && ca) {
		ret = media_create_pad_link(demux, 1, ca,
					    0, MEDIA_LNK_FL_ENABLED);
		if (!ret)
			return -ENOMEM;
	}
614

615 616 617
	/* Create demux links for each ringbuffer/pad */
	if (demux) {
		media_device_for_each_entity(entity, mdev) {
618
			if (entity->function == MEDIA_ENT_F_IO_DTV) {
619
				if (!strncmp(entity->name, DVR_TSOUT,
620 621 622 623 624 625 626
				    strlen(DVR_TSOUT))) {
					ret = media_create_pad_link(demux,
								++dvr_pad,
							    entity, 0, 0);
					if (ret)
						return ret;
				}
627
				if (!strncmp(entity->name, DEMUX_TSOUT,
628 629
				    strlen(DEMUX_TSOUT))) {
					ret = media_create_pad_link(demux,
630
							      ++demux_pad,
631 632 633 634
							    entity, 0, 0);
					if (ret)
						return ret;
				}
635 636 637 638
			}
		}
	}

639
	/* Create interface links for FE->tuner, DVR->demux and CA->ca */
640
	media_device_for_each_intf(intf, mdev) {
641 642 643 644 645 646
		if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
			link = media_create_intf_link(ca, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
647

648 649 650 651 652 653
		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
			link = media_create_intf_link(tuner, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
654 655 656 657 658 659
#if 0
		/*
		 * Indirect link - let's not create yet, as we don't know how
		 *		   to handle indirect links, nor if this will
		 *		   actually be needed.
		 */
660 661 662 663 664 665
		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
			link = media_create_intf_link(demux, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
666
#endif
667 668 669 670 671 672 673 674 675
		if (intf->type == MEDIA_INTF_T_DVB_DVR) {
			ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
			if (ret)
				return ret;
		}
		if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
			ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
			if (ret)
				return ret;
676
		}
677
	}
678
	return 0;
679 680
}
EXPORT_SYMBOL_GPL(dvb_create_media_graph);
681
#endif
682

683 684 685 686 687 688 689 690 691 692 693
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 已提交
694 695 696 697 698 699

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

	while (num < DVB_MAX_ADAPTERS) {
700 701
		if (dvbdev_check_free_adapter_num(num))
			return num;
L
Linus Torvalds 已提交
702 703 704 705 706 707 708
		num++;
	}

	return -ENFILE;
}


709 710 711
int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
			 struct module *module, struct device *device,
			 short *adapter_nums)
L
Linus Torvalds 已提交
712
{
713
	int i, num;
L
Linus Torvalds 已提交
714

715
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
716

717 718 719 720 721 722 723 724 725 726 727 728 729 730
	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) {
731
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
732 733 734 735 736 737
		return -ENFILE;
	}

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

738
	printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
L
Linus Torvalds 已提交
739 740 741 742

	adap->num = num;
	adap->name = name;
	adap->module = module;
743
	adap->device = device;
744 745 746
	adap->mfe_shared = 0;
	adap->mfe_dvbdev = NULL;
	mutex_init (&adap->mfe_lock);
L
Linus Torvalds 已提交
747 748 749

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

750
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
751 752 753 754 755 756 757 758

	return num;
}
EXPORT_SYMBOL(dvb_register_adapter);


int dvb_unregister_adapter(struct dvb_adapter *adap)
{
759
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
760
	list_del (&adap->list_head);
761
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770
	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...) */
771
int dvb_usercopy(struct file *file,
772
		     unsigned int cmd, unsigned long arg,
773
		     int (*func)(struct file *file,
L
Linus Torvalds 已提交
774 775
		     unsigned int cmd, void *arg))
{
776 777 778 779 780 781 782 783
	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 已提交
784 785 786 787 788 789
		/*
		 * For this command, the pointer is actually an integer
		 * argument.
		 */
		parg = (void *) arg;
		break;
790 791 792 793
	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
	case _IOC_WRITE:
	case (_IOC_WRITE | _IOC_READ):
		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
794
			parg = sbuf;
795
		} else {
796 797 798 799 800
			/* too big to allocate from stack */
			mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
			if (NULL == mbuf)
				return -ENOMEM;
			parg = mbuf;
801 802 803 804
		}

		err = -EFAULT;
		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
805
			goto out;
806 807 808 809
		break;
	}

	/* call driver */
810
	if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
811
		err = -ENOTTY;
812 813 814 815 816 817 818 819 820 821

	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)))
822
			err = -EFAULT;
823 824
		break;
	}
L
Linus Torvalds 已提交
825 826

out:
827 828
	kfree(mbuf);
	return err;
L
Linus Torvalds 已提交
829 830
}

831 832 833 834 835
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);
836 837
	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
838 839 840
	return 0;
}

841
static char *dvb_devnode(struct device *dev, umode_t *mode)
842 843 844 845 846 847 848 849
{
	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 已提交
850 851 852 853 854 855
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) {
856
		printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
L
Linus Torvalds 已提交
857 858 859 860 861
		return retval;
	}

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

866
	dvb_class = class_create(THIS_MODULE, "dvb");
L
Linus Torvalds 已提交
867 868 869 870
	if (IS_ERR(dvb_class)) {
		retval = PTR_ERR(dvb_class);
		goto error;
	}
871
	dvb_class->dev_uevent = dvb_uevent;
872
	dvb_class->devnode = dvb_devnode;
L
Linus Torvalds 已提交
873 874 875 876 877 878 879 880 881 882 883
	return 0;

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


static void __exit exit_dvbdev(void)
{
884
	class_destroy(dvb_class);
L
Linus Torvalds 已提交
885
	cdev_del(&dvb_device_cdev);
886
	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
L
Linus Torvalds 已提交
887 888
}

889
subsys_initcall(init_dvbdev);
L
Linus Torvalds 已提交
890 891 892 893 894
module_exit(exit_dvbdev);

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