dvbdev.c 22.9 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
/*
 * 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.
 *
 */

20 21
#define pr_fmt(fmt) "dvbdev: " fmt

L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31
#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>
32
#include <linux/mutex.h>
L
Linus Torvalds 已提交
33 34
#include "dvbdev.h"

35 36 37
/* Due to enum tuner_pad_index */
#include <media/tuner.h>

A
Arnd Bergmann 已提交
38
static DEFINE_MUTEX(dvbdev_mutex);
L
Linus Torvalds 已提交
39 40 41 42 43
static int dvbdev_debug;

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

44 45 46 47 48
#define dprintk(fmt, arg...) do {					\
	if (dvbdev_debug)						\
		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
		       __func__, ##arg);				\
} while (0)
L
Linus Torvalds 已提交
49 50

static LIST_HEAD(dvb_adapter_list);
51
static DEFINE_MUTEX(dvbdev_register_lock);
L
Linus Torvalds 已提交
52 53

static const char * const dnames[] = {
54 55 56 57 58 59 60 61 62
	[DVB_DEVICE_VIDEO] =		"video",
	[DVB_DEVICE_AUDIO] =		"audio",
	[DVB_DEVICE_SEC] =		"sec",
	[DVB_DEVICE_FRONTEND] =		"frontend",
	[DVB_DEVICE_DEMUX] =		"demux",
	[DVB_DEVICE_DVR] =		"dvr",
	[DVB_DEVICE_CA] =		"ca",
	[DVB_DEVICE_NET] =		"net",
	[DVB_DEVICE_OSD] =		"osd"
L
Linus Torvalds 已提交
63 64
};

65 66 67 68
#ifdef CONFIG_DVB_DYNAMIC_MINORS
#define MAX_DVB_MINORS		256
#define DVB_MAX_IDS		MAX_DVB_MINORS
#else
L
Linus Torvalds 已提交
69
#define DVB_MAX_IDS		4
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84
static const u8 minor_type[] = {
       [DVB_DEVICE_VIDEO]      = 0,
       [DVB_DEVICE_AUDIO]      = 1,
       [DVB_DEVICE_SEC]        = 2,
       [DVB_DEVICE_FRONTEND]   = 3,
       [DVB_DEVICE_DEMUX]      = 4,
       [DVB_DEVICE_DVR]        = 5,
       [DVB_DEVICE_CA]         = 6,
       [DVB_DEVICE_NET]        = 7,
       [DVB_DEVICE_OSD]        = 8,
};

#define nums2minor(num, type, id) \
       (((num) << 6) | ((id) << 4) | minor_type[type])
85

L
Linus Torvalds 已提交
86
#define MAX_DVB_MINORS		(DVB_MAX_ADAPTERS*64)
87
#endif
L
Linus Torvalds 已提交
88

89
static struct class *dvb_class;
L
Linus Torvalds 已提交
90

91 92
static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
static DECLARE_RWSEM(minor_rwsem);
L
Linus Torvalds 已提交
93 94 95 96 97

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

A
Arnd Bergmann 已提交
98
	mutex_lock(&dvbdev_mutex);
99 100
	down_read(&minor_rwsem);
	dvbdev = dvb_minors[iminor(inode)];
L
Linus Torvalds 已提交
101 102 103

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

106 107
		new_fops = fops_get(dvbdev->fops);
		if (!new_fops)
L
Laurent Pinchart 已提交
108
			goto fail;
109 110 111
		file->private_data = dvbdev;
		replace_fops(file, new_fops);
		if (file->f_op->open)
112
			err = file->f_op->open(inode, file);
113
		up_read(&minor_rwsem);
A
Arnd Bergmann 已提交
114
		mutex_unlock(&dvbdev_mutex);
115
		return err;
L
Linus Torvalds 已提交
116
	}
L
Laurent Pinchart 已提交
117
fail:
118
	up_read(&minor_rwsem);
A
Arnd Bergmann 已提交
119
	mutex_unlock(&dvbdev_mutex);
L
Linus Torvalds 已提交
120 121 122 123
	return -ENODEV;
}


124
static const struct file_operations dvb_device_fops =
L
Linus Torvalds 已提交
125 126 127
{
	.owner =	THIS_MODULE,
	.open =		dvb_device_open,
128
	.llseek =	noop_llseek,
L
Linus Torvalds 已提交
129 130
};

131
static struct cdev dvb_device_cdev;
L
Linus Torvalds 已提交
132 133 134

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

137 138
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
139 140

	if (!dvbdev->users)
141
		return -EBUSY;
L
Linus Torvalds 已提交
142 143

	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
144 145
		if (!dvbdev->readers)
			return -EBUSY;
L
Linus Torvalds 已提交
146 147
		dvbdev->readers--;
	} else {
148 149
		if (!dvbdev->writers)
			return -EBUSY;
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160
		dvbdev->writers--;
	}

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


int dvb_generic_release(struct inode *inode, struct file *file)
{
161
	struct dvb_device *dvbdev = file->private_data;
L
Linus Torvalds 已提交
162 163

	if (!dvbdev)
164
		return -ENODEV;
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177

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

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


178 179
long dvb_generic_ioctl(struct file *file,
		       unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
180
{
181
	struct dvb_device *dvbdev = file->private_data;
L
Linus Torvalds 已提交
182

183 184
	if (!dvbdev)
		return -ENODEV;
L
Linus Torvalds 已提交
185 186 187 188

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

A
Arnd Bergmann 已提交
189
	return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198
}
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) {
199 200
		struct dvb_device *dev;
		list_for_each_entry(dev, &adap->device_list, list_head)
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209
			if (dev->type == type && dev->id == id)
				goto skip;
		return id;
skip:
		id++;
	}
	return -ENFILE;
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
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;
	}
240 241 242 243 244 245 246

	if (dvbdev->adapter->conn) {
		media_device_unregister_entity(dvbdev->adapter->conn);
		dvbdev->adapter->conn = NULL;
		kfree(dvbdev->adapter->conn_pads);
		dvbdev->adapter->conn_pads = NULL;
	}
247 248 249
#endif
}

250
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
251
static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
252 253 254 255 256 257 258
				    const char *name, int npads)
{
	int i, ret = 0;

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

261 262
	dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
				       GFP_KERNEL);
263 264 265 266 267
	if (!dvbdev->tsout_entity)
		return -ENOMEM;

	dvbdev->tsout_num_entities = npads;

268 269 270 271 272
	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);
273 274
		if (!entity->name)
			return -ENOMEM;
275

276
		entity->function = MEDIA_ENT_F_IO_DTV;
277 278
		pads->flags = MEDIA_PAD_FL_SINK;

279
		ret = media_entity_pads_init(entity, 1, pads);
280
		if (ret < 0)
281
			return ret;
282 283 284 285

		ret = media_device_register_entity(dvbdev->adapter->mdev,
						   entity);
		if (ret < 0)
286
			return ret;
287
	}
288
	return 0;
289 290 291 292 293
}

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

294 295
static int dvb_create_media_entity(struct dvb_device *dvbdev,
				   int type, int demux_sink_pads)
296
{
297
	int i, ret, npads;
298

299 300 301 302
	switch (type) {
	case DVB_DEVICE_FRONTEND:
		npads = 2;
		break;
303
	case DVB_DEVICE_DVR:
304 305 306
		ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
					      demux_sink_pads);
		return ret;
307
	case DVB_DEVICE_DEMUX:
308
		npads = 1 + demux_sink_pads;
309 310 311 312
		ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
					      demux_sink_pads);
		if (ret < 0)
			return ret;
313 314 315 316 317 318 319 320 321 322 323 324 325 326
		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.
		 */
327
		return 0;
328
	default:
329
		return 0;
330
	}
331 332 333

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

	dvbdev->entity->name = dvbdev->name;
337 338 339 340

	if (npads) {
		dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
				       GFP_KERNEL);
341 342
		if (!dvbdev->pads)
			return -ENOMEM;
343 344
	}

345 346
	switch (type) {
	case DVB_DEVICE_FRONTEND:
347
		dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
348 349
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
350 351
		break;
	case DVB_DEVICE_DEMUX:
352
		dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
353
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
354 355
		for (i = 1; i < npads; i++)
			dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
356 357
		break;
	case DVB_DEVICE_CA:
358
		dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
359 360
		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
361 362
		break;
	default:
363
		/* Should never happen, as the first switch prevents it */
364
		kfree(dvbdev->entity);
365
		kfree(dvbdev->pads);
366
		dvbdev->entity = NULL;
367 368
		dvbdev->pads = NULL;
		return 0;
369 370
	}

371
	if (npads) {
372
		ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
373 374
		if (ret)
			return ret;
375
	}
376 377 378
	ret = media_device_register_entity(dvbdev->adapter->mdev,
					   dvbdev->entity);
	if (ret)
379
		return ret;
380

381
	pr_info("%s: media entity '%s' registered.\n",
382
		__func__, dvbdev->entity->name);
383 384

	return 0;
385
}
386
#endif
L
Linus Torvalds 已提交
387

388 389 390
static int dvb_register_media_device(struct dvb_device *dvbdev,
				     int type, int minor,
				     unsigned demux_sink_pads)
391 392
{
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
393
	struct media_link *link;
394
	u32 intf_type;
395
	int ret;
396 397

	if (!dvbdev->adapter->mdev)
398
		return 0;
399

400 401 402
	ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
	if (ret)
		return ret;
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

	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:
421
		return 0;
422 423 424
	}

	dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
425
						    intf_type, 0,
426
						    DVB_MAJOR, minor);
427 428 429

	if (!dvbdev->intf_devnode)
		return -ENOMEM;
430 431 432 433 434 435 436 437 438

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

439 440
	if (!dvbdev->entity)
		return 0;
441

442 443 444 445
	link = media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf,
				      MEDIA_LNK_FL_ENABLED);
	if (!link)
		return -ENOMEM;
446
#endif
447
	return 0;
448 449
}

L
Linus Torvalds 已提交
450
int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
451 452
			const struct dvb_device *template, void *priv,
			enum dvb_device_type type, int demux_sink_pads)
L
Linus Torvalds 已提交
453 454
{
	struct dvb_device *dvbdev;
455
	struct file_operations *dvbdevfops;
456
	struct device *clsdev;
457
	int minor;
458
	int id, ret;
L
Linus Torvalds 已提交
459

460
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
461

462
	if ((id = dvbdev_get_free_id (adap, type)) < 0){
463
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
464
		*pdvbdev = NULL;
465
		pr_err("%s: couldn't find free device id\n", __func__);
L
Linus Torvalds 已提交
466 467 468
		return -ENFILE;
	}

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

471 472 473 474 475 476 477 478 479
	if (!dvbdev){
		mutex_unlock(&dvbdev_register_lock);
		return -ENOMEM;
	}

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

	if (!dvbdevfops){
		kfree (dvbdev);
480
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
481 482 483 484 485 486 487 488
		return -ENOMEM;
	}

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

492 493
	memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
	dvbdevfops->owner = adap->module;
L
Linus Torvalds 已提交
494 495 496

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

497 498 499 500 501 502 503 504 505
	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);
506
		up_write(&minor_rwsem);
507 508 509 510 511 512 513 514 515 516 517
		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);

518 519
	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
	if (ret) {
520
		pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
521 522 523 524 525 526 527 528 529 530
		      __func__);

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

531 532
	mutex_unlock(&dvbdev_register_lock);

533
	clsdev = device_create(dvb_class, adap->device,
534
			       MKDEV(DVB_MAJOR, minor),
535
			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
536
	if (IS_ERR(clsdev)) {
537
		pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
538
		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
539 540
		return PTR_ERR(clsdev);
	}
541
	dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
542
		adap->num, dnames[type], id, minor, minor);
L
Linus Torvalds 已提交
543 544 545 546 547 548

	return 0;
}
EXPORT_SYMBOL(dvb_register_device);


549
void dvb_remove_device(struct dvb_device *dvbdev)
L
Linus Torvalds 已提交
550 551 552 553
{
	if (!dvbdev)
		return;

554 555 556 557
	down_write(&minor_rwsem);
	dvb_minors[dvbdev->minor] = NULL;
	up_write(&minor_rwsem);

558
	dvb_media_device_free(dvbdev);
559

560
	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
561

L
Linus Torvalds 已提交
562
	list_del (&dvbdev->list_head);
563 564 565 566 567 568 569 570 571
}
EXPORT_SYMBOL(dvb_remove_device);


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

572
	kfree (dvbdev->fops);
L
Linus Torvalds 已提交
573 574
	kfree (dvbdev);
}
575 576 577 578 579 580 581 582
EXPORT_SYMBOL(dvb_free_device);


void dvb_unregister_device(struct dvb_device *dvbdev)
{
	dvb_remove_device(dvbdev);
	dvb_free_device(dvbdev);
}
L
Linus Torvalds 已提交
583 584
EXPORT_SYMBOL(dvb_unregister_device);

585 586

#ifdef CONFIG_MEDIA_CONTROLLER_DVB
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

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;
}

609 610
int dvb_create_media_graph(struct dvb_adapter *adap,
			   bool create_rf_connector)
611 612
{
	struct media_device *mdev = adap->mdev;
613
	struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
614
	struct media_entity *demux = NULL, *ca = NULL;
615
	struct media_link *link;
616
	struct media_interface *intf;
617 618
	unsigned demux_pad = 0;
	unsigned dvr_pad = 0;
619
	unsigned ntuner = 0, ndemod = 0;
620
	int ret;
621
	static const char *connector_name = "Television";
622 623

	if (!mdev)
624
		return 0;
625 626

	media_device_for_each_entity(entity, mdev) {
627
		switch (entity->function) {
628
		case MEDIA_ENT_F_TUNER:
629
			tuner = entity;
630
			ntuner++;
631
			break;
632
		case MEDIA_ENT_F_DTV_DEMOD:
633
			demod = entity;
634
			ndemod++;
635
			break;
636
		case MEDIA_ENT_F_TS_DEMUX:
637 638
			demux = entity;
			break;
639
		case MEDIA_ENT_F_DTV_CA:
640 641 642 643 644
			ca = entity;
			break;
		}
	}

645 646 647 648 649 650 651 652 653 654 655 656
	/*
	 * Prepare to signalize to media_create_pad_links() that multiple
	 * entities of the same type exists and a 1:n or n:1 links need to be
	 * created.
	 * NOTE: if both tuner and demod have multiple instances, it is up
	 * to the caller driver to create such links.
	 */
	if (ntuner > 1)
		tuner = NULL;
	if (ndemod > 1)
		demod = NULL;

657 658 659 660 661 662
	if (create_rf_connector) {
		conn = kzalloc(sizeof(*conn), GFP_KERNEL);
		if (!conn)
			return -ENOMEM;
		adap->conn = conn;

663
		adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
		if (!adap->conn_pads)
			return -ENOMEM;

		conn->flags = MEDIA_ENT_FL_CONNECTOR;
		conn->function = MEDIA_ENT_F_CONN_RF;
		conn->name = connector_name;
		adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;

		ret = media_entity_pads_init(conn, 1, adap->conn_pads);
		if (ret)
			return ret;

		ret = media_device_register_entity(mdev, conn);
		if (ret)
			return ret;

680 681 682 683 684 685 686 687
		if (!ntuner)
			ret = media_create_pad_links(mdev,
						     MEDIA_ENT_F_CONN_RF,
						     conn, 0,
						     MEDIA_ENT_F_DTV_DEMOD,
						     demod, 0,
						     MEDIA_LNK_FL_ENABLED,
						     false);
688
		else
689 690 691 692 693 694 695
			ret = media_create_pad_links(mdev,
						     MEDIA_ENT_F_CONN_RF,
						     conn, 0,
						     MEDIA_ENT_F_TUNER,
						     tuner, TUNER_PAD_RF_INPUT,
						     MEDIA_LNK_FL_ENABLED,
						     false);
696 697 698 699
		if (ret)
			return ret;
	}

700 701 702
	if (ntuner && ndemod) {
		ret = media_create_pad_links(mdev,
					     MEDIA_ENT_F_TUNER,
703
					     tuner, TUNER_PAD_OUTPUT,
704 705 706
					     MEDIA_ENT_F_DTV_DEMOD,
					     demod, 0, MEDIA_LNK_FL_ENABLED,
					     false);
707 708 709
		if (ret)
			return ret;
	}
710

711 712 713 714 715 716 717
	if (ndemod && demux) {
		ret = media_create_pad_links(mdev,
					     MEDIA_ENT_F_DTV_DEMOD,
					     demod, 1,
					     MEDIA_ENT_F_TS_DEMUX,
					     demux, 0, MEDIA_LNK_FL_ENABLED,
					     false);
718
		if (ret)
719
			return ret;
720 721 722 723
	}
	if (demux && ca) {
		ret = media_create_pad_link(demux, 1, ca,
					    0, MEDIA_LNK_FL_ENABLED);
724
		if (ret)
725
			return ret;
726
	}
727

728 729 730
	/* Create demux links for each ringbuffer/pad */
	if (demux) {
		media_device_for_each_entity(entity, mdev) {
731
			if (entity->function == MEDIA_ENT_F_IO_DTV) {
732
				if (!strncmp(entity->name, DVR_TSOUT,
733 734 735 736 737 738 739
				    strlen(DVR_TSOUT))) {
					ret = media_create_pad_link(demux,
								++dvr_pad,
							    entity, 0, 0);
					if (ret)
						return ret;
				}
740
				if (!strncmp(entity->name, DEMUX_TSOUT,
741 742
				    strlen(DEMUX_TSOUT))) {
					ret = media_create_pad_link(demux,
743
							      ++demux_pad,
744 745 746 747
							    entity, 0, 0);
					if (ret)
						return ret;
				}
748 749 750 751
			}
		}
	}

752
	/* Create interface links for FE->tuner, DVR->demux and CA->ca */
753
	media_device_for_each_intf(intf, mdev) {
754 755 756 757 758 759
		if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
			link = media_create_intf_link(ca, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
760

761 762 763 764 765 766
		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
			link = media_create_intf_link(tuner, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
767 768 769 770 771 772
#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.
		 */
773 774 775 776 777 778
		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
			link = media_create_intf_link(demux, intf,
						      MEDIA_LNK_FL_ENABLED);
			if (!link)
				return -ENOMEM;
		}
779
#endif
780 781 782 783 784 785 786 787 788
		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;
789
		}
790
	}
791
	return 0;
792 793
}
EXPORT_SYMBOL_GPL(dvb_create_media_graph);
794
#endif
795

796 797 798 799 800 801 802 803 804 805 806
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 已提交
807 808 809 810 811 812

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

	while (num < DVB_MAX_ADAPTERS) {
813 814
		if (dvbdev_check_free_adapter_num(num))
			return num;
L
Linus Torvalds 已提交
815 816 817 818 819 820 821
		num++;
	}

	return -ENFILE;
}


822 823 824
int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
			 struct module *module, struct device *device,
			 short *adapter_nums)
L
Linus Torvalds 已提交
825
{
826
	int i, num;
L
Linus Torvalds 已提交
827

828
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
829

830 831 832 833 834 835 836 837 838 839 840 841 842 843
	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) {
844
		mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
845 846 847 848 849 850
		return -ENFILE;
	}

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

851
	pr_info("DVB: registering new adapter (%s)\n", name);
L
Linus Torvalds 已提交
852 853 854 855

	adap->num = num;
	adap->name = name;
	adap->module = module;
856
	adap->device = device;
857 858 859
	adap->mfe_shared = 0;
	adap->mfe_dvbdev = NULL;
	mutex_init (&adap->mfe_lock);
L
Linus Torvalds 已提交
860 861 862

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

863
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871

	return num;
}
EXPORT_SYMBOL(dvb_register_adapter);


int dvb_unregister_adapter(struct dvb_adapter *adap)
{
872
	mutex_lock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
873
	list_del (&adap->list_head);
874
	mutex_unlock(&dvbdev_register_lock);
L
Linus Torvalds 已提交
875 876 877 878 879 880 881 882 883
	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...) */
884
int dvb_usercopy(struct file *file,
885
		     unsigned int cmd, unsigned long arg,
886
		     int (*func)(struct file *file,
L
Linus Torvalds 已提交
887 888
		     unsigned int cmd, void *arg))
{
889 890 891 892 893 894 895 896
	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 已提交
897 898 899 900 901 902
		/*
		 * For this command, the pointer is actually an integer
		 * argument.
		 */
		parg = (void *) arg;
		break;
903 904 905 906
	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
	case _IOC_WRITE:
	case (_IOC_WRITE | _IOC_READ):
		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
907
			parg = sbuf;
908
		} else {
909
			/* too big to allocate from stack */
910
			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
911 912 913
			if (NULL == mbuf)
				return -ENOMEM;
			parg = mbuf;
914 915 916 917
		}

		err = -EFAULT;
		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
918
			goto out;
919 920 921 922
		break;
	}

	/* call driver */
923
	if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
924
		err = -ENOTTY;
925 926 927 928 929 930 931 932 933 934

	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)))
935
			err = -EFAULT;
936 937
		break;
	}
L
Linus Torvalds 已提交
938 939

out:
940 941
	kfree(mbuf);
	return err;
L
Linus Torvalds 已提交
942 943
}

944 945 946 947 948
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);
949 950
	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
951 952 953
	return 0;
}

954
static char *dvb_devnode(struct device *dev, umode_t *mode)
955 956 957 958 959 960 961 962
{
	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 已提交
963 964 965 966 967 968
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) {
969
		pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
L
Linus Torvalds 已提交
970 971 972 973 974
		return retval;
	}

	cdev_init(&dvb_device_cdev, &dvb_device_fops);
	if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
975
		pr_err("dvb-core: unable register character device\n");
L
Linus Torvalds 已提交
976 977 978
		goto error;
	}

979
	dvb_class = class_create(THIS_MODULE, "dvb");
L
Linus Torvalds 已提交
980 981 982 983
	if (IS_ERR(dvb_class)) {
		retval = PTR_ERR(dvb_class);
		goto error;
	}
984
	dvb_class->dev_uevent = dvb_uevent;
985
	dvb_class->devnode = dvb_devnode;
L
Linus Torvalds 已提交
986 987 988 989 990 991 992 993 994 995 996
	return 0;

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


static void __exit exit_dvbdev(void)
{
997
	class_destroy(dvb_class);
L
Linus Torvalds 已提交
998
	cdev_del(&dvb_device_cdev);
999
	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
L
Linus Torvalds 已提交
1000 1001
}

1002
subsys_initcall(init_dvbdev);
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007
module_exit(exit_dvbdev);

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