Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: LGPL-2.1-or-later
   2/*
   3 * dvbdev.c
   4 *
   5 * Copyright (C) 2000 Ralph  Metzler <ralph@convergence.de>
   6 *                  & Marcus Metzler <marcus@convergence.de>
   7 *                    for convergence integrated media GmbH
   8 */
   9
  10#define pr_fmt(fmt) "dvbdev: " fmt
  11
  12#include <linux/types.h>
  13#include <linux/errno.h>
  14#include <linux/string.h>
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/i2c.h>
  18#include <linux/init.h>
  19#include <linux/slab.h>
  20#include <linux/device.h>
  21#include <linux/fs.h>
  22#include <linux/cdev.h>
  23#include <linux/mutex.h>
  24#include <media/dvbdev.h>
  25
  26/* Due to enum tuner_pad_index */
  27#include <media/tuner.h>
  28
  29static DEFINE_MUTEX(dvbdev_mutex);
  30static LIST_HEAD(dvbdevfops_list);
  31static int dvbdev_debug;
  32
  33module_param(dvbdev_debug, int, 0644);
  34MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
  35
  36#define dprintk(fmt, arg...) do {					\
  37	if (dvbdev_debug)						\
  38		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
  39		       __func__, ##arg);				\
  40} while (0)
  41
  42static LIST_HEAD(dvb_adapter_list);
  43static DEFINE_MUTEX(dvbdev_register_lock);
  44
  45static const char * const dnames[] = {
  46	[DVB_DEVICE_VIDEO] =		"video",
  47	[DVB_DEVICE_AUDIO] =		"audio",
  48	[DVB_DEVICE_SEC] =		"sec",
  49	[DVB_DEVICE_FRONTEND] =		"frontend",
  50	[DVB_DEVICE_DEMUX] =		"demux",
  51	[DVB_DEVICE_DVR] =		"dvr",
  52	[DVB_DEVICE_CA] =		"ca",
  53	[DVB_DEVICE_NET] =		"net",
  54	[DVB_DEVICE_OSD] =		"osd"
  55};
  56
  57#ifdef CONFIG_DVB_DYNAMIC_MINORS
  58#define MAX_DVB_MINORS		256
  59#define DVB_MAX_IDS		MAX_DVB_MINORS
  60#else
  61#define DVB_MAX_IDS		4
  62
  63static const u8 minor_type[] = {
  64	[DVB_DEVICE_VIDEO]      = 0,
  65	[DVB_DEVICE_AUDIO]      = 1,
  66	[DVB_DEVICE_SEC]        = 2,
  67	[DVB_DEVICE_FRONTEND]   = 3,
  68	[DVB_DEVICE_DEMUX]      = 4,
  69	[DVB_DEVICE_DVR]        = 5,
  70	[DVB_DEVICE_CA]         = 6,
  71	[DVB_DEVICE_NET]        = 7,
  72	[DVB_DEVICE_OSD]        = 8,
  73};
  74
  75#define nums2minor(num, type, id) \
  76	(((num) << 6) | ((id) << 4) | minor_type[type])
  77
  78#define MAX_DVB_MINORS		(DVB_MAX_ADAPTERS * 64)
  79#endif
  80
  81static struct class *dvb_class;
  82
  83static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
  84static DECLARE_RWSEM(minor_rwsem);
  85
  86static int dvb_device_open(struct inode *inode, struct file *file)
  87{
  88	struct dvb_device *dvbdev;
  89	unsigned int minor = iminor(inode);
  90
  91	if (minor >= MAX_DVB_MINORS)
  92		return -ENODEV;
  93
  94	mutex_lock(&dvbdev_mutex);
  95	down_read(&minor_rwsem);
  96
  97	dvbdev = dvb_minors[minor];
  98
  99	if (dvbdev && dvbdev->fops) {
 100		int err = 0;
 101		const struct file_operations *new_fops;
 102
 103		new_fops = fops_get(dvbdev->fops);
 104		if (!new_fops)
 105			goto fail;
 106		file->private_data = dvb_device_get(dvbdev);
 107		replace_fops(file, new_fops);
 108		if (file->f_op->open)
 109			err = file->f_op->open(inode, file);
 110		up_read(&minor_rwsem);
 111		mutex_unlock(&dvbdev_mutex);
 112		if (err)
 113			dvb_device_put(dvbdev);
 114		return err;
 115	}
 116fail:
 117	up_read(&minor_rwsem);
 118	mutex_unlock(&dvbdev_mutex);
 119	return -ENODEV;
 120}
 121
 122static const struct file_operations dvb_device_fops = {
 123	.owner =	THIS_MODULE,
 124	.open =		dvb_device_open,
 125	.llseek =	noop_llseek,
 126};
 127
 128static struct cdev dvb_device_cdev;
 129
 130int dvb_generic_open(struct inode *inode, struct file *file)
 131{
 132	struct dvb_device *dvbdev = file->private_data;
 133
 134	if (!dvbdev)
 135		return -ENODEV;
 136
 137	if (!dvbdev->users)
 138		return -EBUSY;
 139
 140	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
 141		if (!dvbdev->readers)
 142			return -EBUSY;
 143		dvbdev->readers--;
 144	} else {
 145		if (!dvbdev->writers)
 146			return -EBUSY;
 147		dvbdev->writers--;
 148	}
 149
 150	dvbdev->users--;
 151	return 0;
 152}
 153EXPORT_SYMBOL(dvb_generic_open);
 154
 155int dvb_generic_release(struct inode *inode, struct file *file)
 156{
 157	struct dvb_device *dvbdev = file->private_data;
 158
 159	if (!dvbdev)
 160		return -ENODEV;
 161
 162	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
 163		dvbdev->readers++;
 164	else
 165		dvbdev->writers++;
 166
 167	dvbdev->users++;
 168
 169	dvb_device_put(dvbdev);
 170
 171	return 0;
 172}
 173EXPORT_SYMBOL(dvb_generic_release);
 174
 175long dvb_generic_ioctl(struct file *file,
 176		       unsigned int cmd, unsigned long arg)
 177{
 178	struct dvb_device *dvbdev = file->private_data;
 179
 180	if (!dvbdev)
 181		return -ENODEV;
 182
 183	if (!dvbdev->kernel_ioctl)
 184		return -EINVAL;
 185
 186	return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
 187}
 188EXPORT_SYMBOL(dvb_generic_ioctl);
 189
 190static int dvbdev_get_free_id(struct dvb_adapter *adap, int type)
 191{
 192	u32 id = 0;
 193
 194	while (id < DVB_MAX_IDS) {
 195		struct dvb_device *dev;
 196
 197		list_for_each_entry(dev, &adap->device_list, list_head)
 198			if (dev->type == type && dev->id == id)
 199				goto skip;
 200		return id;
 201skip:
 202		id++;
 203	}
 204	return -ENFILE;
 205}
 206
 207static void dvb_media_device_free(struct dvb_device *dvbdev)
 208{
 209#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 210	if (dvbdev->entity) {
 211		media_device_unregister_entity(dvbdev->entity);
 212		kfree(dvbdev->entity);
 213		kfree(dvbdev->pads);
 214		dvbdev->entity = NULL;
 215		dvbdev->pads = NULL;
 216	}
 217
 218	if (dvbdev->tsout_entity) {
 219		int i;
 220
 221		for (i = 0; i < dvbdev->tsout_num_entities; i++) {
 222			media_device_unregister_entity(&dvbdev->tsout_entity[i]);
 223			kfree(dvbdev->tsout_entity[i].name);
 224		}
 225		kfree(dvbdev->tsout_entity);
 226		kfree(dvbdev->tsout_pads);
 227		dvbdev->tsout_entity = NULL;
 228		dvbdev->tsout_pads = NULL;
 229
 230		dvbdev->tsout_num_entities = 0;
 231	}
 232
 233	if (dvbdev->intf_devnode) {
 234		media_devnode_remove(dvbdev->intf_devnode);
 235		dvbdev->intf_devnode = NULL;
 236	}
 237
 238	if (dvbdev->adapter->conn) {
 239		media_device_unregister_entity(dvbdev->adapter->conn);
 240		kfree(dvbdev->adapter->conn);
 241		dvbdev->adapter->conn = NULL;
 242		kfree(dvbdev->adapter->conn_pads);
 243		dvbdev->adapter->conn_pads = NULL;
 244	}
 245#endif
 246}
 247
 248#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 249static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
 250				   const char *name, int npads)
 251{
 252	int i;
 253
 254	dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
 255				     GFP_KERNEL);
 256	if (!dvbdev->tsout_pads)
 257		return -ENOMEM;
 258
 259	dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
 260				       GFP_KERNEL);
 261	if (!dvbdev->tsout_entity)
 262		return -ENOMEM;
 263
 264	dvbdev->tsout_num_entities = npads;
 265
 266	for (i = 0; i < npads; i++) {
 267		struct media_pad *pads = &dvbdev->tsout_pads[i];
 268		struct media_entity *entity = &dvbdev->tsout_entity[i];
 269		int ret;
 270
 271		entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
 272		if (!entity->name)
 273			return -ENOMEM;
 274
 275		entity->function = MEDIA_ENT_F_IO_DTV;
 276		pads->flags = MEDIA_PAD_FL_SINK;
 277
 278		ret = media_entity_pads_init(entity, 1, pads);
 279		if (ret < 0)
 280			return ret;
 281
 282		ret = media_device_register_entity(dvbdev->adapter->mdev,
 283						   entity);
 284		if (ret < 0)
 285			return ret;
 286	}
 287	return 0;
 288}
 289
 290#define DEMUX_TSOUT	"demux-tsout"
 291#define DVR_TSOUT	"dvr-tsout"
 292
 293static int dvb_create_media_entity(struct dvb_device *dvbdev,
 294				   int type, int demux_sink_pads)
 295{
 296	int i, ret, npads;
 297
 298	switch (type) {
 299	case DVB_DEVICE_FRONTEND:
 300		npads = 2;
 301		break;
 302	case DVB_DEVICE_DVR:
 303		ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
 304					      demux_sink_pads);
 305		return ret;
 306	case DVB_DEVICE_DEMUX:
 307		npads = 1 + demux_sink_pads;
 308		ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
 309					      demux_sink_pads);
 310		if (ret < 0)
 311			return ret;
 312		break;
 313	case DVB_DEVICE_CA:
 314		npads = 2;
 315		break;
 316	case DVB_DEVICE_NET:
 317		/*
 318		 * We should be creating entities for the MPE/ULE
 319		 * decapsulation hardware (or software implementation).
 320		 *
 321		 * However, the number of for the MPE/ULE decaps may not be
 322		 * fixed. As we don't have yet dynamic support for PADs at
 323		 * the Media Controller, let's not create the decap
 324		 * entities yet.
 325		 */
 326		return 0;
 327	default:
 328		return 0;
 329	}
 330
 331	dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
 332	if (!dvbdev->entity)
 333		return -ENOMEM;
 334
 335	dvbdev->entity->name = dvbdev->name;
 336
 337	if (npads) {
 338		dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
 339				       GFP_KERNEL);
 340		if (!dvbdev->pads) {
 341			kfree(dvbdev->entity);
 342			dvbdev->entity = NULL;
 343			return -ENOMEM;
 344		}
 345	}
 346
 347	switch (type) {
 348	case DVB_DEVICE_FRONTEND:
 349		dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
 350		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 351		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
 352		break;
 353	case DVB_DEVICE_DEMUX:
 354		dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
 355		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 356		for (i = 1; i < npads; i++)
 357			dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
 358		break;
 359	case DVB_DEVICE_CA:
 360		dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
 361		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 362		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
 363		break;
 364	default:
 365		/* Should never happen, as the first switch prevents it */
 366		kfree(dvbdev->entity);
 367		kfree(dvbdev->pads);
 368		dvbdev->entity = NULL;
 369		dvbdev->pads = NULL;
 370		return 0;
 371	}
 372
 373	if (npads) {
 374		ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
 375		if (ret)
 376			return ret;
 377	}
 378	ret = media_device_register_entity(dvbdev->adapter->mdev,
 379					   dvbdev->entity);
 380	if (ret)
 381		return ret;
 382
 383	pr_info("%s: media entity '%s' registered.\n",
 384		__func__, dvbdev->entity->name);
 385
 386	return 0;
 387}
 388#endif
 389
 390static int dvb_register_media_device(struct dvb_device *dvbdev,
 391				     int type, int minor,
 392				     unsigned int demux_sink_pads)
 393{
 394#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 395	struct media_link *link;
 396	u32 intf_type;
 397	int ret;
 398
 399	if (!dvbdev->adapter->mdev)
 400		return 0;
 401
 402	ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
 403	if (ret)
 404		return ret;
 405
 406	switch (type) {
 407	case DVB_DEVICE_FRONTEND:
 408		intf_type = MEDIA_INTF_T_DVB_FE;
 409		break;
 410	case DVB_DEVICE_DEMUX:
 411		intf_type = MEDIA_INTF_T_DVB_DEMUX;
 412		break;
 413	case DVB_DEVICE_DVR:
 414		intf_type = MEDIA_INTF_T_DVB_DVR;
 415		break;
 416	case DVB_DEVICE_CA:
 417		intf_type = MEDIA_INTF_T_DVB_CA;
 418		break;
 419	case DVB_DEVICE_NET:
 420		intf_type = MEDIA_INTF_T_DVB_NET;
 421		break;
 422	default:
 423		return 0;
 424	}
 425
 426	dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
 427						    intf_type, 0,
 428						    DVB_MAJOR, minor);
 429
 430	if (!dvbdev->intf_devnode)
 431		return -ENOMEM;
 432
 433	/*
 434	 * Create the "obvious" link, e. g. the ones that represent
 435	 * a direct association between an interface and an entity.
 436	 * Other links should be created elsewhere, like:
 437	 *		DVB FE intf    -> tuner
 438	 *		DVB demux intf -> dvr
 439	 */
 440
 441	if (!dvbdev->entity)
 442		return 0;
 443
 444	link = media_create_intf_link(dvbdev->entity,
 445				      &dvbdev->intf_devnode->intf,
 446				      MEDIA_LNK_FL_ENABLED |
 447				      MEDIA_LNK_FL_IMMUTABLE);
 448	if (!link)
 449		return -ENOMEM;
 450#endif
 451	return 0;
 452}
 453
 454int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 455			const struct dvb_device *template, void *priv,
 456			enum dvb_device_type type, int demux_sink_pads)
 457{
 458	struct dvb_device *dvbdev;
 459	struct file_operations *dvbdevfops = NULL;
 460	struct dvbdevfops_node *node = NULL, *new_node = NULL;
 461	struct device *clsdev;
 462	int minor;
 463	int id, ret;
 464
 465	mutex_lock(&dvbdev_register_lock);
 466
 467	id = dvbdev_get_free_id(adap, type);
 468	if (id < 0) {
 469		mutex_unlock(&dvbdev_register_lock);
 470		*pdvbdev = NULL;
 471		pr_err("%s: couldn't find free device id\n", __func__);
 472		return -ENFILE;
 473	}
 474
 475	*pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
 476	if (!dvbdev) {
 477		mutex_unlock(&dvbdev_register_lock);
 478		return -ENOMEM;
 479	}
 480
 481	/*
 482	 * When a device of the same type is probe()d more than once,
 483	 * the first allocated fops are used. This prevents memory leaks
 484	 * that can occur when the same device is probe()d repeatedly.
 485	 */
 486	list_for_each_entry(node, &dvbdevfops_list, list_head) {
 487		if (node->fops->owner == adap->module &&
 488		    node->type == type && node->template == template) {
 489			dvbdevfops = node->fops;
 490			break;
 491		}
 492	}
 493
 494	if (!dvbdevfops) {
 495		dvbdevfops = kmemdup(template->fops, sizeof(*dvbdevfops), GFP_KERNEL);
 496		if (!dvbdevfops) {
 497			kfree(dvbdev);
 498			*pdvbdev = NULL;
 499			mutex_unlock(&dvbdev_register_lock);
 500			return -ENOMEM;
 501		}
 502
 503		new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
 504		if (!new_node) {
 505			kfree(dvbdevfops);
 506			kfree(dvbdev);
 507			*pdvbdev = NULL;
 508			mutex_unlock(&dvbdev_register_lock);
 509			return -ENOMEM;
 510		}
 511
 512		new_node->fops = dvbdevfops;
 513		new_node->type = type;
 514		new_node->template = template;
 515		list_add_tail(&new_node->list_head, &dvbdevfops_list);
 516	}
 517
 518	memcpy(dvbdev, template, sizeof(struct dvb_device));
 519	kref_init(&dvbdev->ref);
 520	dvbdev->type = type;
 521	dvbdev->id = id;
 522	dvbdev->adapter = adap;
 523	dvbdev->priv = priv;
 524	dvbdev->fops = dvbdevfops;
 525	init_waitqueue_head(&dvbdev->wait_queue);
 526	dvbdevfops->owner = adap->module;
 527	list_add_tail(&dvbdev->list_head, &adap->device_list);
 528	down_write(&minor_rwsem);
 529#ifdef CONFIG_DVB_DYNAMIC_MINORS
 530	for (minor = 0; minor < MAX_DVB_MINORS; minor++)
 531		if (!dvb_minors[minor])
 532			break;
 533#else
 534	minor = nums2minor(adap->num, type, id);
 535#endif
 536	if (minor >= MAX_DVB_MINORS) {
 537		if (new_node) {
 538			list_del(&new_node->list_head);
 539			kfree(dvbdevfops);
 540			kfree(new_node);
 541		}
 542		list_del(&dvbdev->list_head);
 543		kfree(dvbdev);
 544		*pdvbdev = NULL;
 545		up_write(&minor_rwsem);
 546		mutex_unlock(&dvbdev_register_lock);
 547		return -EINVAL;
 548	}
 549
 
 
 550	dvbdev->minor = minor;
 551	dvb_minors[minor] = dvb_device_get(dvbdev);
 552	up_write(&minor_rwsem);
 553	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
 554	if (ret) {
 555		pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
 556		       __func__);
 557		if (new_node) {
 558			list_del(&new_node->list_head);
 559			kfree(dvbdevfops);
 560			kfree(new_node);
 561		}
 562		dvb_media_device_free(dvbdev);
 563		list_del(&dvbdev->list_head);
 564		kfree(dvbdev);
 565		*pdvbdev = NULL;
 566		mutex_unlock(&dvbdev_register_lock);
 567		return ret;
 568	}
 569
 570	clsdev = device_create(dvb_class, adap->device,
 571			       MKDEV(DVB_MAJOR, minor),
 572			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
 573	if (IS_ERR(clsdev)) {
 574		pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
 575		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
 576		if (new_node) {
 577			list_del(&new_node->list_head);
 578			kfree(dvbdevfops);
 579			kfree(new_node);
 580		}
 581		dvb_media_device_free(dvbdev);
 582		list_del(&dvbdev->list_head);
 583		kfree(dvbdev);
 584		*pdvbdev = NULL;
 585		mutex_unlock(&dvbdev_register_lock);
 586		return PTR_ERR(clsdev);
 587	}
 588
 589	dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
 590		adap->num, dnames[type], id, minor, minor);
 591
 592	mutex_unlock(&dvbdev_register_lock);
 593	return 0;
 594}
 595EXPORT_SYMBOL(dvb_register_device);
 596
 597void dvb_remove_device(struct dvb_device *dvbdev)
 598{
 599	if (!dvbdev)
 600		return;
 601
 602	down_write(&minor_rwsem);
 603	dvb_minors[dvbdev->minor] = NULL;
 604	dvb_device_put(dvbdev);
 605	up_write(&minor_rwsem);
 606
 607	dvb_media_device_free(dvbdev);
 608
 609	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
 610
 611	list_del(&dvbdev->list_head);
 612}
 613EXPORT_SYMBOL(dvb_remove_device);
 614
 615static void dvb_free_device(struct kref *ref)
 616{
 617	struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref);
 618
 619	kfree(dvbdev);
 620}
 621
 622struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
 623{
 624	kref_get(&dvbdev->ref);
 625	return dvbdev;
 626}
 627EXPORT_SYMBOL(dvb_device_get);
 628
 629void dvb_device_put(struct dvb_device *dvbdev)
 630{
 631	if (dvbdev)
 632		kref_put(&dvbdev->ref, dvb_free_device);
 633}
 634
 635void dvb_unregister_device(struct dvb_device *dvbdev)
 636{
 637	dvb_remove_device(dvbdev);
 638	dvb_device_put(dvbdev);
 639}
 640EXPORT_SYMBOL(dvb_unregister_device);
 641
 642#ifdef CONFIG_MEDIA_CONTROLLER_DVB
 643
 644static int dvb_create_io_intf_links(struct dvb_adapter *adap,
 645				    struct media_interface *intf,
 646				    char *name)
 647{
 648	struct media_device *mdev = adap->mdev;
 649	struct media_entity *entity;
 650	struct media_link *link;
 651
 652	media_device_for_each_entity(entity, mdev) {
 653		if (entity->function == MEDIA_ENT_F_IO_DTV) {
 654			if (strncmp(entity->name, name, strlen(name)))
 655				continue;
 656			link = media_create_intf_link(entity, intf,
 657						      MEDIA_LNK_FL_ENABLED |
 658						      MEDIA_LNK_FL_IMMUTABLE);
 659			if (!link)
 660				return -ENOMEM;
 661		}
 662	}
 663	return 0;
 664}
 665
 666int dvb_create_media_graph(struct dvb_adapter *adap,
 667			   bool create_rf_connector)
 668{
 669	struct media_device *mdev = adap->mdev;
 670	struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
 671	struct media_entity *demux = NULL, *ca = NULL;
 672	struct media_link *link;
 673	struct media_interface *intf;
 674	unsigned int demux_pad = 0;
 675	unsigned int dvr_pad = 0;
 676	unsigned int ntuner = 0, ndemod = 0;
 677	int ret, pad_source, pad_sink;
 678	static const char *connector_name = "Television";
 679
 680	if (!mdev)
 681		return 0;
 682
 683	media_device_for_each_entity(entity, mdev) {
 684		switch (entity->function) {
 685		case MEDIA_ENT_F_TUNER:
 686			tuner = entity;
 687			ntuner++;
 688			break;
 689		case MEDIA_ENT_F_DTV_DEMOD:
 690			demod = entity;
 691			ndemod++;
 692			break;
 693		case MEDIA_ENT_F_TS_DEMUX:
 694			demux = entity;
 695			break;
 696		case MEDIA_ENT_F_DTV_CA:
 697			ca = entity;
 698			break;
 699		}
 700	}
 701
 702	/*
 703	 * Prepare to signalize to media_create_pad_links() that multiple
 704	 * entities of the same type exists and a 1:n or n:1 links need to be
 705	 * created.
 706	 * NOTE: if both tuner and demod have multiple instances, it is up
 707	 * to the caller driver to create such links.
 708	 */
 709	if (ntuner > 1)
 710		tuner = NULL;
 711	if (ndemod > 1)
 712		demod = NULL;
 713
 714	if (create_rf_connector) {
 715		conn = kzalloc(sizeof(*conn), GFP_KERNEL);
 716		if (!conn)
 717			return -ENOMEM;
 718		adap->conn = conn;
 719
 720		adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
 721		if (!adap->conn_pads)
 722			return -ENOMEM;
 723
 724		conn->flags = MEDIA_ENT_FL_CONNECTOR;
 725		conn->function = MEDIA_ENT_F_CONN_RF;
 726		conn->name = connector_name;
 727		adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
 728
 729		ret = media_entity_pads_init(conn, 1, adap->conn_pads);
 730		if (ret)
 731			return ret;
 732
 733		ret = media_device_register_entity(mdev, conn);
 734		if (ret)
 735			return ret;
 736
 737		if (!ntuner) {
 738			ret = media_create_pad_links(mdev,
 739						     MEDIA_ENT_F_CONN_RF,
 740						     conn, 0,
 741						     MEDIA_ENT_F_DTV_DEMOD,
 742						     demod, 0,
 743						     MEDIA_LNK_FL_ENABLED,
 744						     false);
 745		} else {
 746			pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
 747						       PAD_SIGNAL_ANALOG);
 748			if (pad_sink < 0)
 749				return -EINVAL;
 750			ret = media_create_pad_links(mdev,
 751						     MEDIA_ENT_F_CONN_RF,
 752						     conn, 0,
 753						     MEDIA_ENT_F_TUNER,
 754						     tuner, pad_sink,
 755						     MEDIA_LNK_FL_ENABLED,
 756						     false);
 757		}
 758		if (ret)
 759			return ret;
 760	}
 761
 762	if (ntuner && ndemod) {
 763		/* NOTE: first found tuner source pad presumed correct */
 764		pad_source = media_get_pad_index(tuner, MEDIA_PAD_FL_SOURCE,
 765						 PAD_SIGNAL_ANALOG);
 766		if (pad_source < 0)
 767			return -EINVAL;
 768		ret = media_create_pad_links(mdev,
 769					     MEDIA_ENT_F_TUNER,
 770					     tuner, pad_source,
 771					     MEDIA_ENT_F_DTV_DEMOD,
 772					     demod, 0, MEDIA_LNK_FL_ENABLED,
 773					     false);
 774		if (ret)
 775			return ret;
 776	}
 777
 778	if (ndemod && demux) {
 779		ret = media_create_pad_links(mdev,
 780					     MEDIA_ENT_F_DTV_DEMOD,
 781					     demod, 1,
 782					     MEDIA_ENT_F_TS_DEMUX,
 783					     demux, 0, MEDIA_LNK_FL_ENABLED,
 784					     false);
 785		if (ret)
 786			return ret;
 787	}
 788	if (demux && ca) {
 789		ret = media_create_pad_link(demux, 1, ca,
 790					    0, MEDIA_LNK_FL_ENABLED);
 791		if (ret)
 792			return ret;
 793	}
 794
 795	/* Create demux links for each ringbuffer/pad */
 796	if (demux) {
 797		media_device_for_each_entity(entity, mdev) {
 798			if (entity->function == MEDIA_ENT_F_IO_DTV) {
 799				if (!strncmp(entity->name, DVR_TSOUT,
 800					     strlen(DVR_TSOUT))) {
 801					ret = media_create_pad_link(demux,
 802								    ++dvr_pad,
 803								    entity, 0, 0);
 804					if (ret)
 805						return ret;
 806				}
 807				if (!strncmp(entity->name, DEMUX_TSOUT,
 808					     strlen(DEMUX_TSOUT))) {
 809					ret = media_create_pad_link(demux,
 810								    ++demux_pad,
 811								    entity, 0, 0);
 812					if (ret)
 813						return ret;
 814				}
 815			}
 816		}
 817	}
 818
 819	/* Create interface links for FE->tuner, DVR->demux and CA->ca */
 820	media_device_for_each_intf(intf, mdev) {
 821		if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
 822			link = media_create_intf_link(ca, intf,
 823						      MEDIA_LNK_FL_ENABLED |
 824						      MEDIA_LNK_FL_IMMUTABLE);
 825			if (!link)
 826				return -ENOMEM;
 827		}
 828
 829		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
 830			link = media_create_intf_link(tuner, intf,
 831						      MEDIA_LNK_FL_ENABLED |
 832						      MEDIA_LNK_FL_IMMUTABLE);
 833			if (!link)
 834				return -ENOMEM;
 835		}
 836#if 0
 837		/*
 838		 * Indirect link - let's not create yet, as we don't know how
 839		 *		   to handle indirect links, nor if this will
 840		 *		   actually be needed.
 841		 */
 842		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
 843			link = media_create_intf_link(demux, intf,
 844						      MEDIA_LNK_FL_ENABLED |
 845						      MEDIA_LNK_FL_IMMUTABLE);
 846			if (!link)
 847				return -ENOMEM;
 848		}
 849#endif
 850		if (intf->type == MEDIA_INTF_T_DVB_DVR) {
 851			ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
 852			if (ret)
 853				return ret;
 854		}
 855		if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
 856			ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
 857			if (ret)
 858				return ret;
 859		}
 860	}
 861	return 0;
 862}
 863EXPORT_SYMBOL_GPL(dvb_create_media_graph);
 864#endif
 865
 866static int dvbdev_check_free_adapter_num(int num)
 867{
 868	struct list_head *entry;
 869
 870	list_for_each(entry, &dvb_adapter_list) {
 871		struct dvb_adapter *adap;
 872
 873		adap = list_entry(entry, struct dvb_adapter, list_head);
 874		if (adap->num == num)
 875			return 0;
 876	}
 877	return 1;
 878}
 879
 880static int dvbdev_get_free_adapter_num(void)
 881{
 882	int num = 0;
 883
 884	while (num < DVB_MAX_ADAPTERS) {
 885		if (dvbdev_check_free_adapter_num(num))
 886			return num;
 887		num++;
 888	}
 889
 890	return -ENFILE;
 891}
 892
 893int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
 894			 struct module *module, struct device *device,
 895			 short *adapter_nums)
 896{
 897	int i, num;
 898
 899	mutex_lock(&dvbdev_register_lock);
 900
 901	for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
 902		num = adapter_nums[i];
 903		if (num >= 0  &&  num < DVB_MAX_ADAPTERS) {
 904		/* use the one the driver asked for */
 905			if (dvbdev_check_free_adapter_num(num))
 906				break;
 907		} else {
 908			num = dvbdev_get_free_adapter_num();
 909			break;
 910		}
 911		num = -1;
 912	}
 913
 914	if (num < 0) {
 915		mutex_unlock(&dvbdev_register_lock);
 916		return -ENFILE;
 917	}
 918
 919	memset(adap, 0, sizeof(struct dvb_adapter));
 920	INIT_LIST_HEAD(&adap->device_list);
 921
 922	pr_info("DVB: registering new adapter (%s)\n", name);
 923
 924	adap->num = num;
 925	adap->name = name;
 926	adap->module = module;
 927	adap->device = device;
 928	adap->mfe_shared = 0;
 929	adap->mfe_dvbdev = NULL;
 930	mutex_init(&adap->mfe_lock);
 931
 932#ifdef CONFIG_MEDIA_CONTROLLER_DVB
 933	mutex_init(&adap->mdev_lock);
 934#endif
 935
 936	list_add_tail(&adap->list_head, &dvb_adapter_list);
 937
 938	mutex_unlock(&dvbdev_register_lock);
 939
 940	return num;
 941}
 942EXPORT_SYMBOL(dvb_register_adapter);
 943
 944int dvb_unregister_adapter(struct dvb_adapter *adap)
 945{
 946	mutex_lock(&dvbdev_register_lock);
 947	list_del(&adap->list_head);
 948	mutex_unlock(&dvbdev_register_lock);
 949	return 0;
 950}
 951EXPORT_SYMBOL(dvb_unregister_adapter);
 952
 953/*
 954 * if the miracle happens and "generic_usercopy()" is included into
 955 * the kernel, then this can vanish. please don't make the mistake and
 956 * define this as video_usercopy(). this will introduce a dependency
 957 * to the v4l "videodev.o" module, which is unnecessary for some
 958 * cards (ie. the budget dvb-cards don't need the v4l module...)
 959 */
 960int dvb_usercopy(struct file *file,
 961		 unsigned int cmd, unsigned long arg,
 962		 int (*func)(struct file *file,
 963			     unsigned int cmd, void *arg))
 964{
 965	char    sbuf[128] = {};
 966	void    *mbuf = NULL;
 967	void    *parg = NULL;
 968	int     err  = -EINVAL;
 969
 970	/*  Copy arguments into temp kernel buffer  */
 971	switch (_IOC_DIR(cmd)) {
 972	case _IOC_NONE:
 973		/*
 974		 * For this command, the pointer is actually an integer
 975		 * argument.
 976		 */
 977		parg = (void *)arg;
 978		break;
 979	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
 980	case _IOC_WRITE:
 981	case (_IOC_WRITE | _IOC_READ):
 982		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
 983			parg = sbuf;
 984		} else {
 985			/* too big to allocate from stack */
 986			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
 987			if (!mbuf)
 988				return -ENOMEM;
 989			parg = mbuf;
 990		}
 991
 992		err = -EFAULT;
 993		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
 994			goto out;
 995		break;
 996	}
 997
 998	/* call driver */
 999	err = func(file, cmd, parg);
1000	if (err == -ENOIOCTLCMD)
1001		err = -ENOTTY;
1002
1003	if (err < 0)
1004		goto out;
1005
1006	/*  Copy results into user buffer  */
1007	switch (_IOC_DIR(cmd)) {
1008	case _IOC_READ:
1009	case (_IOC_WRITE | _IOC_READ):
1010		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1011			err = -EFAULT;
1012		break;
1013	}
1014
1015out:
1016	kfree(mbuf);
1017	return err;
1018}
1019
1020#if IS_ENABLED(CONFIG_I2C)
1021struct i2c_client *dvb_module_probe(const char *module_name,
1022				    const char *name,
1023				    struct i2c_adapter *adap,
1024				    unsigned char addr,
1025				    void *platform_data)
1026{
1027	struct i2c_client *client;
1028	struct i2c_board_info *board_info;
1029
1030	board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
1031	if (!board_info)
1032		return NULL;
1033
1034	if (name)
1035		strscpy(board_info->type, name, I2C_NAME_SIZE);
1036	else
1037		strscpy(board_info->type, module_name, I2C_NAME_SIZE);
1038
1039	board_info->addr = addr;
1040	board_info->platform_data = platform_data;
1041	request_module(module_name);
1042	client = i2c_new_client_device(adap, board_info);
1043	if (!i2c_client_has_driver(client)) {
1044		kfree(board_info);
1045		return NULL;
1046	}
1047
1048	if (!try_module_get(client->dev.driver->owner)) {
1049		i2c_unregister_device(client);
1050		client = NULL;
1051	}
1052
1053	kfree(board_info);
1054	return client;
1055}
1056EXPORT_SYMBOL_GPL(dvb_module_probe);
1057
1058void dvb_module_release(struct i2c_client *client)
1059{
1060	if (!client)
1061		return;
1062
1063	module_put(client->dev.driver->owner);
1064	i2c_unregister_device(client);
1065}
1066EXPORT_SYMBOL_GPL(dvb_module_release);
1067#endif
1068
1069static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env)
1070{
1071	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1072
1073	add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1074	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1075	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1076	return 0;
1077}
1078
1079static char *dvb_devnode(const struct device *dev, umode_t *mode)
1080{
1081	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1082
1083	return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1084		dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1085}
1086
1087static int __init init_dvbdev(void)
1088{
1089	int retval;
1090	dev_t dev = MKDEV(DVB_MAJOR, 0);
1091
1092	retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB");
1093	if (retval != 0) {
1094		pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1095		return retval;
1096	}
1097
1098	cdev_init(&dvb_device_cdev, &dvb_device_fops);
1099	retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS);
1100	if (retval != 0) {
1101		pr_err("dvb-core: unable register character device\n");
1102		goto error;
1103	}
1104
1105	dvb_class = class_create("dvb");
1106	if (IS_ERR(dvb_class)) {
1107		retval = PTR_ERR(dvb_class);
1108		goto error;
1109	}
1110	dvb_class->dev_uevent = dvb_uevent;
1111	dvb_class->devnode = dvb_devnode;
1112	return 0;
1113
1114error:
1115	cdev_del(&dvb_device_cdev);
1116	unregister_chrdev_region(dev, MAX_DVB_MINORS);
1117	return retval;
1118}
1119
1120static void __exit exit_dvbdev(void)
1121{
1122	struct dvbdevfops_node *node, *next;
1123
1124	class_destroy(dvb_class);
1125	cdev_del(&dvb_device_cdev);
1126	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1127
1128	list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) {
1129		list_del(&node->list_head);
1130		kfree(node->fops);
1131		kfree(node);
1132	}
1133}
1134
1135subsys_initcall(init_dvbdev);
1136module_exit(exit_dvbdev);
1137
1138MODULE_DESCRIPTION("DVB Core Driver");
1139MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1140MODULE_LICENSE("GPL");
v6.8
   1// SPDX-License-Identifier: LGPL-2.1-or-later
   2/*
   3 * dvbdev.c
   4 *
   5 * Copyright (C) 2000 Ralph  Metzler <ralph@convergence.de>
   6 *                  & Marcus Metzler <marcus@convergence.de>
   7 *                    for convergence integrated media GmbH
   8 */
   9
  10#define pr_fmt(fmt) "dvbdev: " fmt
  11
  12#include <linux/types.h>
  13#include <linux/errno.h>
  14#include <linux/string.h>
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/i2c.h>
  18#include <linux/init.h>
  19#include <linux/slab.h>
  20#include <linux/device.h>
  21#include <linux/fs.h>
  22#include <linux/cdev.h>
  23#include <linux/mutex.h>
  24#include <media/dvbdev.h>
  25
  26/* Due to enum tuner_pad_index */
  27#include <media/tuner.h>
  28
  29static DEFINE_MUTEX(dvbdev_mutex);
  30static LIST_HEAD(dvbdevfops_list);
  31static int dvbdev_debug;
  32
  33module_param(dvbdev_debug, int, 0644);
  34MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
  35
  36#define dprintk(fmt, arg...) do {					\
  37	if (dvbdev_debug)						\
  38		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
  39		       __func__, ##arg);				\
  40} while (0)
  41
  42static LIST_HEAD(dvb_adapter_list);
  43static DEFINE_MUTEX(dvbdev_register_lock);
  44
  45static const char * const dnames[] = {
  46	[DVB_DEVICE_VIDEO] =		"video",
  47	[DVB_DEVICE_AUDIO] =		"audio",
  48	[DVB_DEVICE_SEC] =		"sec",
  49	[DVB_DEVICE_FRONTEND] =		"frontend",
  50	[DVB_DEVICE_DEMUX] =		"demux",
  51	[DVB_DEVICE_DVR] =		"dvr",
  52	[DVB_DEVICE_CA] =		"ca",
  53	[DVB_DEVICE_NET] =		"net",
  54	[DVB_DEVICE_OSD] =		"osd"
  55};
  56
  57#ifdef CONFIG_DVB_DYNAMIC_MINORS
  58#define MAX_DVB_MINORS		256
  59#define DVB_MAX_IDS		MAX_DVB_MINORS
  60#else
  61#define DVB_MAX_IDS		4
  62
  63static const u8 minor_type[] = {
  64	[DVB_DEVICE_VIDEO]      = 0,
  65	[DVB_DEVICE_AUDIO]      = 1,
  66	[DVB_DEVICE_SEC]        = 2,
  67	[DVB_DEVICE_FRONTEND]   = 3,
  68	[DVB_DEVICE_DEMUX]      = 4,
  69	[DVB_DEVICE_DVR]        = 5,
  70	[DVB_DEVICE_CA]         = 6,
  71	[DVB_DEVICE_NET]        = 7,
  72	[DVB_DEVICE_OSD]        = 8,
  73};
  74
  75#define nums2minor(num, type, id) \
  76	(((num) << 6) | ((id) << 4) | minor_type[type])
  77
  78#define MAX_DVB_MINORS		(DVB_MAX_ADAPTERS * 64)
  79#endif
  80
  81static struct class *dvb_class;
  82
  83static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
  84static DECLARE_RWSEM(minor_rwsem);
  85
  86static int dvb_device_open(struct inode *inode, struct file *file)
  87{
  88	struct dvb_device *dvbdev;
 
 
 
 
  89
  90	mutex_lock(&dvbdev_mutex);
  91	down_read(&minor_rwsem);
  92	dvbdev = dvb_minors[iminor(inode)];
 
  93
  94	if (dvbdev && dvbdev->fops) {
  95		int err = 0;
  96		const struct file_operations *new_fops;
  97
  98		new_fops = fops_get(dvbdev->fops);
  99		if (!new_fops)
 100			goto fail;
 101		file->private_data = dvb_device_get(dvbdev);
 102		replace_fops(file, new_fops);
 103		if (file->f_op->open)
 104			err = file->f_op->open(inode, file);
 105		up_read(&minor_rwsem);
 106		mutex_unlock(&dvbdev_mutex);
 107		if (err)
 108			dvb_device_put(dvbdev);
 109		return err;
 110	}
 111fail:
 112	up_read(&minor_rwsem);
 113	mutex_unlock(&dvbdev_mutex);
 114	return -ENODEV;
 115}
 116
 117static const struct file_operations dvb_device_fops = {
 118	.owner =	THIS_MODULE,
 119	.open =		dvb_device_open,
 120	.llseek =	noop_llseek,
 121};
 122
 123static struct cdev dvb_device_cdev;
 124
 125int dvb_generic_open(struct inode *inode, struct file *file)
 126{
 127	struct dvb_device *dvbdev = file->private_data;
 128
 129	if (!dvbdev)
 130		return -ENODEV;
 131
 132	if (!dvbdev->users)
 133		return -EBUSY;
 134
 135	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
 136		if (!dvbdev->readers)
 137			return -EBUSY;
 138		dvbdev->readers--;
 139	} else {
 140		if (!dvbdev->writers)
 141			return -EBUSY;
 142		dvbdev->writers--;
 143	}
 144
 145	dvbdev->users--;
 146	return 0;
 147}
 148EXPORT_SYMBOL(dvb_generic_open);
 149
 150int dvb_generic_release(struct inode *inode, struct file *file)
 151{
 152	struct dvb_device *dvbdev = file->private_data;
 153
 154	if (!dvbdev)
 155		return -ENODEV;
 156
 157	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
 158		dvbdev->readers++;
 159	else
 160		dvbdev->writers++;
 161
 162	dvbdev->users++;
 163
 164	dvb_device_put(dvbdev);
 165
 166	return 0;
 167}
 168EXPORT_SYMBOL(dvb_generic_release);
 169
 170long dvb_generic_ioctl(struct file *file,
 171		       unsigned int cmd, unsigned long arg)
 172{
 173	struct dvb_device *dvbdev = file->private_data;
 174
 175	if (!dvbdev)
 176		return -ENODEV;
 177
 178	if (!dvbdev->kernel_ioctl)
 179		return -EINVAL;
 180
 181	return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
 182}
 183EXPORT_SYMBOL(dvb_generic_ioctl);
 184
 185static int dvbdev_get_free_id(struct dvb_adapter *adap, int type)
 186{
 187	u32 id = 0;
 188
 189	while (id < DVB_MAX_IDS) {
 190		struct dvb_device *dev;
 191
 192		list_for_each_entry(dev, &adap->device_list, list_head)
 193			if (dev->type == type && dev->id == id)
 194				goto skip;
 195		return id;
 196skip:
 197		id++;
 198	}
 199	return -ENFILE;
 200}
 201
 202static void dvb_media_device_free(struct dvb_device *dvbdev)
 203{
 204#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 205	if (dvbdev->entity) {
 206		media_device_unregister_entity(dvbdev->entity);
 207		kfree(dvbdev->entity);
 208		kfree(dvbdev->pads);
 209		dvbdev->entity = NULL;
 210		dvbdev->pads = NULL;
 211	}
 212
 213	if (dvbdev->tsout_entity) {
 214		int i;
 215
 216		for (i = 0; i < dvbdev->tsout_num_entities; i++) {
 217			media_device_unregister_entity(&dvbdev->tsout_entity[i]);
 218			kfree(dvbdev->tsout_entity[i].name);
 219		}
 220		kfree(dvbdev->tsout_entity);
 221		kfree(dvbdev->tsout_pads);
 222		dvbdev->tsout_entity = NULL;
 223		dvbdev->tsout_pads = NULL;
 224
 225		dvbdev->tsout_num_entities = 0;
 226	}
 227
 228	if (dvbdev->intf_devnode) {
 229		media_devnode_remove(dvbdev->intf_devnode);
 230		dvbdev->intf_devnode = NULL;
 231	}
 232
 233	if (dvbdev->adapter->conn) {
 234		media_device_unregister_entity(dvbdev->adapter->conn);
 235		kfree(dvbdev->adapter->conn);
 236		dvbdev->adapter->conn = NULL;
 237		kfree(dvbdev->adapter->conn_pads);
 238		dvbdev->adapter->conn_pads = NULL;
 239	}
 240#endif
 241}
 242
 243#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 244static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
 245				   const char *name, int npads)
 246{
 247	int i;
 248
 249	dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
 250				     GFP_KERNEL);
 251	if (!dvbdev->tsout_pads)
 252		return -ENOMEM;
 253
 254	dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
 255				       GFP_KERNEL);
 256	if (!dvbdev->tsout_entity)
 257		return -ENOMEM;
 258
 259	dvbdev->tsout_num_entities = npads;
 260
 261	for (i = 0; i < npads; i++) {
 262		struct media_pad *pads = &dvbdev->tsout_pads[i];
 263		struct media_entity *entity = &dvbdev->tsout_entity[i];
 264		int ret;
 265
 266		entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
 267		if (!entity->name)
 268			return -ENOMEM;
 269
 270		entity->function = MEDIA_ENT_F_IO_DTV;
 271		pads->flags = MEDIA_PAD_FL_SINK;
 272
 273		ret = media_entity_pads_init(entity, 1, pads);
 274		if (ret < 0)
 275			return ret;
 276
 277		ret = media_device_register_entity(dvbdev->adapter->mdev,
 278						   entity);
 279		if (ret < 0)
 280			return ret;
 281	}
 282	return 0;
 283}
 284
 285#define DEMUX_TSOUT	"demux-tsout"
 286#define DVR_TSOUT	"dvr-tsout"
 287
 288static int dvb_create_media_entity(struct dvb_device *dvbdev,
 289				   int type, int demux_sink_pads)
 290{
 291	int i, ret, npads;
 292
 293	switch (type) {
 294	case DVB_DEVICE_FRONTEND:
 295		npads = 2;
 296		break;
 297	case DVB_DEVICE_DVR:
 298		ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
 299					      demux_sink_pads);
 300		return ret;
 301	case DVB_DEVICE_DEMUX:
 302		npads = 1 + demux_sink_pads;
 303		ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
 304					      demux_sink_pads);
 305		if (ret < 0)
 306			return ret;
 307		break;
 308	case DVB_DEVICE_CA:
 309		npads = 2;
 310		break;
 311	case DVB_DEVICE_NET:
 312		/*
 313		 * We should be creating entities for the MPE/ULE
 314		 * decapsulation hardware (or software implementation).
 315		 *
 316		 * However, the number of for the MPE/ULE decaps may not be
 317		 * fixed. As we don't have yet dynamic support for PADs at
 318		 * the Media Controller, let's not create the decap
 319		 * entities yet.
 320		 */
 321		return 0;
 322	default:
 323		return 0;
 324	}
 325
 326	dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
 327	if (!dvbdev->entity)
 328		return -ENOMEM;
 329
 330	dvbdev->entity->name = dvbdev->name;
 331
 332	if (npads) {
 333		dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
 334				       GFP_KERNEL);
 335		if (!dvbdev->pads) {
 336			kfree(dvbdev->entity);
 337			dvbdev->entity = NULL;
 338			return -ENOMEM;
 339		}
 340	}
 341
 342	switch (type) {
 343	case DVB_DEVICE_FRONTEND:
 344		dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
 345		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 346		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
 347		break;
 348	case DVB_DEVICE_DEMUX:
 349		dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
 350		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 351		for (i = 1; i < npads; i++)
 352			dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
 353		break;
 354	case DVB_DEVICE_CA:
 355		dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
 356		dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
 357		dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
 358		break;
 359	default:
 360		/* Should never happen, as the first switch prevents it */
 361		kfree(dvbdev->entity);
 362		kfree(dvbdev->pads);
 363		dvbdev->entity = NULL;
 364		dvbdev->pads = NULL;
 365		return 0;
 366	}
 367
 368	if (npads) {
 369		ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
 370		if (ret)
 371			return ret;
 372	}
 373	ret = media_device_register_entity(dvbdev->adapter->mdev,
 374					   dvbdev->entity);
 375	if (ret)
 376		return ret;
 377
 378	pr_info("%s: media entity '%s' registered.\n",
 379		__func__, dvbdev->entity->name);
 380
 381	return 0;
 382}
 383#endif
 384
 385static int dvb_register_media_device(struct dvb_device *dvbdev,
 386				     int type, int minor,
 387				     unsigned int demux_sink_pads)
 388{
 389#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
 390	struct media_link *link;
 391	u32 intf_type;
 392	int ret;
 393
 394	if (!dvbdev->adapter->mdev)
 395		return 0;
 396
 397	ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
 398	if (ret)
 399		return ret;
 400
 401	switch (type) {
 402	case DVB_DEVICE_FRONTEND:
 403		intf_type = MEDIA_INTF_T_DVB_FE;
 404		break;
 405	case DVB_DEVICE_DEMUX:
 406		intf_type = MEDIA_INTF_T_DVB_DEMUX;
 407		break;
 408	case DVB_DEVICE_DVR:
 409		intf_type = MEDIA_INTF_T_DVB_DVR;
 410		break;
 411	case DVB_DEVICE_CA:
 412		intf_type = MEDIA_INTF_T_DVB_CA;
 413		break;
 414	case DVB_DEVICE_NET:
 415		intf_type = MEDIA_INTF_T_DVB_NET;
 416		break;
 417	default:
 418		return 0;
 419	}
 420
 421	dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
 422						    intf_type, 0,
 423						    DVB_MAJOR, minor);
 424
 425	if (!dvbdev->intf_devnode)
 426		return -ENOMEM;
 427
 428	/*
 429	 * Create the "obvious" link, e. g. the ones that represent
 430	 * a direct association between an interface and an entity.
 431	 * Other links should be created elsewhere, like:
 432	 *		DVB FE intf    -> tuner
 433	 *		DVB demux intf -> dvr
 434	 */
 435
 436	if (!dvbdev->entity)
 437		return 0;
 438
 439	link = media_create_intf_link(dvbdev->entity,
 440				      &dvbdev->intf_devnode->intf,
 441				      MEDIA_LNK_FL_ENABLED |
 442				      MEDIA_LNK_FL_IMMUTABLE);
 443	if (!link)
 444		return -ENOMEM;
 445#endif
 446	return 0;
 447}
 448
 449int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 450			const struct dvb_device *template, void *priv,
 451			enum dvb_device_type type, int demux_sink_pads)
 452{
 453	struct dvb_device *dvbdev;
 454	struct file_operations *dvbdevfops = NULL;
 455	struct dvbdevfops_node *node = NULL, *new_node = NULL;
 456	struct device *clsdev;
 457	int minor;
 458	int id, ret;
 459
 460	mutex_lock(&dvbdev_register_lock);
 461
 462	id = dvbdev_get_free_id(adap, type);
 463	if (id < 0) {
 464		mutex_unlock(&dvbdev_register_lock);
 465		*pdvbdev = NULL;
 466		pr_err("%s: couldn't find free device id\n", __func__);
 467		return -ENFILE;
 468	}
 469
 470	*pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
 471	if (!dvbdev) {
 472		mutex_unlock(&dvbdev_register_lock);
 473		return -ENOMEM;
 474	}
 475
 476	/*
 477	 * When a device of the same type is probe()d more than once,
 478	 * the first allocated fops are used. This prevents memory leaks
 479	 * that can occur when the same device is probe()d repeatedly.
 480	 */
 481	list_for_each_entry(node, &dvbdevfops_list, list_head) {
 482		if (node->fops->owner == adap->module &&
 483		    node->type == type && node->template == template) {
 484			dvbdevfops = node->fops;
 485			break;
 486		}
 487	}
 488
 489	if (!dvbdevfops) {
 490		dvbdevfops = kmemdup(template->fops, sizeof(*dvbdevfops), GFP_KERNEL);
 491		if (!dvbdevfops) {
 492			kfree(dvbdev);
 
 493			mutex_unlock(&dvbdev_register_lock);
 494			return -ENOMEM;
 495		}
 496
 497		new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
 498		if (!new_node) {
 499			kfree(dvbdevfops);
 500			kfree(dvbdev);
 
 501			mutex_unlock(&dvbdev_register_lock);
 502			return -ENOMEM;
 503		}
 504
 505		new_node->fops = dvbdevfops;
 506		new_node->type = type;
 507		new_node->template = template;
 508		list_add_tail(&new_node->list_head, &dvbdevfops_list);
 509	}
 510
 511	memcpy(dvbdev, template, sizeof(struct dvb_device));
 512	kref_init(&dvbdev->ref);
 513	dvbdev->type = type;
 514	dvbdev->id = id;
 515	dvbdev->adapter = adap;
 516	dvbdev->priv = priv;
 517	dvbdev->fops = dvbdevfops;
 518	init_waitqueue_head(&dvbdev->wait_queue);
 519	dvbdevfops->owner = adap->module;
 520	list_add_tail(&dvbdev->list_head, &adap->device_list);
 521	down_write(&minor_rwsem);
 522#ifdef CONFIG_DVB_DYNAMIC_MINORS
 523	for (minor = 0; minor < MAX_DVB_MINORS; minor++)
 524		if (!dvb_minors[minor])
 525			break;
 526	if (minor == MAX_DVB_MINORS) {
 
 
 
 527		if (new_node) {
 528			list_del(&new_node->list_head);
 529			kfree(dvbdevfops);
 530			kfree(new_node);
 531		}
 532		list_del(&dvbdev->list_head);
 533		kfree(dvbdev);
 
 534		up_write(&minor_rwsem);
 535		mutex_unlock(&dvbdev_register_lock);
 536		return -EINVAL;
 537	}
 538#else
 539	minor = nums2minor(adap->num, type, id);
 540#endif
 541	dvbdev->minor = minor;
 542	dvb_minors[minor] = dvb_device_get(dvbdev);
 543	up_write(&minor_rwsem);
 544	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
 545	if (ret) {
 546		pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
 547		       __func__);
 548		if (new_node) {
 549			list_del(&new_node->list_head);
 550			kfree(dvbdevfops);
 551			kfree(new_node);
 552		}
 553		dvb_media_device_free(dvbdev);
 554		list_del(&dvbdev->list_head);
 555		kfree(dvbdev);
 
 556		mutex_unlock(&dvbdev_register_lock);
 557		return ret;
 558	}
 559
 560	clsdev = device_create(dvb_class, adap->device,
 561			       MKDEV(DVB_MAJOR, minor),
 562			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
 563	if (IS_ERR(clsdev)) {
 564		pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
 565		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
 566		if (new_node) {
 567			list_del(&new_node->list_head);
 568			kfree(dvbdevfops);
 569			kfree(new_node);
 570		}
 571		dvb_media_device_free(dvbdev);
 572		list_del(&dvbdev->list_head);
 573		kfree(dvbdev);
 
 574		mutex_unlock(&dvbdev_register_lock);
 575		return PTR_ERR(clsdev);
 576	}
 577
 578	dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
 579		adap->num, dnames[type], id, minor, minor);
 580
 581	mutex_unlock(&dvbdev_register_lock);
 582	return 0;
 583}
 584EXPORT_SYMBOL(dvb_register_device);
 585
 586void dvb_remove_device(struct dvb_device *dvbdev)
 587{
 588	if (!dvbdev)
 589		return;
 590
 591	down_write(&minor_rwsem);
 592	dvb_minors[dvbdev->minor] = NULL;
 593	dvb_device_put(dvbdev);
 594	up_write(&minor_rwsem);
 595
 596	dvb_media_device_free(dvbdev);
 597
 598	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
 599
 600	list_del(&dvbdev->list_head);
 601}
 602EXPORT_SYMBOL(dvb_remove_device);
 603
 604static void dvb_free_device(struct kref *ref)
 605{
 606	struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref);
 607
 608	kfree(dvbdev);
 609}
 610
 611struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
 612{
 613	kref_get(&dvbdev->ref);
 614	return dvbdev;
 615}
 616EXPORT_SYMBOL(dvb_device_get);
 617
 618void dvb_device_put(struct dvb_device *dvbdev)
 619{
 620	if (dvbdev)
 621		kref_put(&dvbdev->ref, dvb_free_device);
 622}
 623
 624void dvb_unregister_device(struct dvb_device *dvbdev)
 625{
 626	dvb_remove_device(dvbdev);
 627	dvb_device_put(dvbdev);
 628}
 629EXPORT_SYMBOL(dvb_unregister_device);
 630
 631#ifdef CONFIG_MEDIA_CONTROLLER_DVB
 632
 633static int dvb_create_io_intf_links(struct dvb_adapter *adap,
 634				    struct media_interface *intf,
 635				    char *name)
 636{
 637	struct media_device *mdev = adap->mdev;
 638	struct media_entity *entity;
 639	struct media_link *link;
 640
 641	media_device_for_each_entity(entity, mdev) {
 642		if (entity->function == MEDIA_ENT_F_IO_DTV) {
 643			if (strncmp(entity->name, name, strlen(name)))
 644				continue;
 645			link = media_create_intf_link(entity, intf,
 646						      MEDIA_LNK_FL_ENABLED |
 647						      MEDIA_LNK_FL_IMMUTABLE);
 648			if (!link)
 649				return -ENOMEM;
 650		}
 651	}
 652	return 0;
 653}
 654
 655int dvb_create_media_graph(struct dvb_adapter *adap,
 656			   bool create_rf_connector)
 657{
 658	struct media_device *mdev = adap->mdev;
 659	struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
 660	struct media_entity *demux = NULL, *ca = NULL;
 661	struct media_link *link;
 662	struct media_interface *intf;
 663	unsigned int demux_pad = 0;
 664	unsigned int dvr_pad = 0;
 665	unsigned int ntuner = 0, ndemod = 0;
 666	int ret, pad_source, pad_sink;
 667	static const char *connector_name = "Television";
 668
 669	if (!mdev)
 670		return 0;
 671
 672	media_device_for_each_entity(entity, mdev) {
 673		switch (entity->function) {
 674		case MEDIA_ENT_F_TUNER:
 675			tuner = entity;
 676			ntuner++;
 677			break;
 678		case MEDIA_ENT_F_DTV_DEMOD:
 679			demod = entity;
 680			ndemod++;
 681			break;
 682		case MEDIA_ENT_F_TS_DEMUX:
 683			demux = entity;
 684			break;
 685		case MEDIA_ENT_F_DTV_CA:
 686			ca = entity;
 687			break;
 688		}
 689	}
 690
 691	/*
 692	 * Prepare to signalize to media_create_pad_links() that multiple
 693	 * entities of the same type exists and a 1:n or n:1 links need to be
 694	 * created.
 695	 * NOTE: if both tuner and demod have multiple instances, it is up
 696	 * to the caller driver to create such links.
 697	 */
 698	if (ntuner > 1)
 699		tuner = NULL;
 700	if (ndemod > 1)
 701		demod = NULL;
 702
 703	if (create_rf_connector) {
 704		conn = kzalloc(sizeof(*conn), GFP_KERNEL);
 705		if (!conn)
 706			return -ENOMEM;
 707		adap->conn = conn;
 708
 709		adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
 710		if (!adap->conn_pads)
 711			return -ENOMEM;
 712
 713		conn->flags = MEDIA_ENT_FL_CONNECTOR;
 714		conn->function = MEDIA_ENT_F_CONN_RF;
 715		conn->name = connector_name;
 716		adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
 717
 718		ret = media_entity_pads_init(conn, 1, adap->conn_pads);
 719		if (ret)
 720			return ret;
 721
 722		ret = media_device_register_entity(mdev, conn);
 723		if (ret)
 724			return ret;
 725
 726		if (!ntuner) {
 727			ret = media_create_pad_links(mdev,
 728						     MEDIA_ENT_F_CONN_RF,
 729						     conn, 0,
 730						     MEDIA_ENT_F_DTV_DEMOD,
 731						     demod, 0,
 732						     MEDIA_LNK_FL_ENABLED,
 733						     false);
 734		} else {
 735			pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
 736						       PAD_SIGNAL_ANALOG);
 737			if (pad_sink < 0)
 738				return -EINVAL;
 739			ret = media_create_pad_links(mdev,
 740						     MEDIA_ENT_F_CONN_RF,
 741						     conn, 0,
 742						     MEDIA_ENT_F_TUNER,
 743						     tuner, pad_sink,
 744						     MEDIA_LNK_FL_ENABLED,
 745						     false);
 746		}
 747		if (ret)
 748			return ret;
 749	}
 750
 751	if (ntuner && ndemod) {
 752		/* NOTE: first found tuner source pad presumed correct */
 753		pad_source = media_get_pad_index(tuner, MEDIA_PAD_FL_SOURCE,
 754						 PAD_SIGNAL_ANALOG);
 755		if (pad_source < 0)
 756			return -EINVAL;
 757		ret = media_create_pad_links(mdev,
 758					     MEDIA_ENT_F_TUNER,
 759					     tuner, pad_source,
 760					     MEDIA_ENT_F_DTV_DEMOD,
 761					     demod, 0, MEDIA_LNK_FL_ENABLED,
 762					     false);
 763		if (ret)
 764			return ret;
 765	}
 766
 767	if (ndemod && demux) {
 768		ret = media_create_pad_links(mdev,
 769					     MEDIA_ENT_F_DTV_DEMOD,
 770					     demod, 1,
 771					     MEDIA_ENT_F_TS_DEMUX,
 772					     demux, 0, MEDIA_LNK_FL_ENABLED,
 773					     false);
 774		if (ret)
 775			return ret;
 776	}
 777	if (demux && ca) {
 778		ret = media_create_pad_link(demux, 1, ca,
 779					    0, MEDIA_LNK_FL_ENABLED);
 780		if (ret)
 781			return ret;
 782	}
 783
 784	/* Create demux links for each ringbuffer/pad */
 785	if (demux) {
 786		media_device_for_each_entity(entity, mdev) {
 787			if (entity->function == MEDIA_ENT_F_IO_DTV) {
 788				if (!strncmp(entity->name, DVR_TSOUT,
 789					     strlen(DVR_TSOUT))) {
 790					ret = media_create_pad_link(demux,
 791								    ++dvr_pad,
 792								    entity, 0, 0);
 793					if (ret)
 794						return ret;
 795				}
 796				if (!strncmp(entity->name, DEMUX_TSOUT,
 797					     strlen(DEMUX_TSOUT))) {
 798					ret = media_create_pad_link(demux,
 799								    ++demux_pad,
 800								    entity, 0, 0);
 801					if (ret)
 802						return ret;
 803				}
 804			}
 805		}
 806	}
 807
 808	/* Create interface links for FE->tuner, DVR->demux and CA->ca */
 809	media_device_for_each_intf(intf, mdev) {
 810		if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
 811			link = media_create_intf_link(ca, intf,
 812						      MEDIA_LNK_FL_ENABLED |
 813						      MEDIA_LNK_FL_IMMUTABLE);
 814			if (!link)
 815				return -ENOMEM;
 816		}
 817
 818		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
 819			link = media_create_intf_link(tuner, intf,
 820						      MEDIA_LNK_FL_ENABLED |
 821						      MEDIA_LNK_FL_IMMUTABLE);
 822			if (!link)
 823				return -ENOMEM;
 824		}
 825#if 0
 826		/*
 827		 * Indirect link - let's not create yet, as we don't know how
 828		 *		   to handle indirect links, nor if this will
 829		 *		   actually be needed.
 830		 */
 831		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
 832			link = media_create_intf_link(demux, intf,
 833						      MEDIA_LNK_FL_ENABLED |
 834						      MEDIA_LNK_FL_IMMUTABLE);
 835			if (!link)
 836				return -ENOMEM;
 837		}
 838#endif
 839		if (intf->type == MEDIA_INTF_T_DVB_DVR) {
 840			ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
 841			if (ret)
 842				return ret;
 843		}
 844		if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
 845			ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
 846			if (ret)
 847				return ret;
 848		}
 849	}
 850	return 0;
 851}
 852EXPORT_SYMBOL_GPL(dvb_create_media_graph);
 853#endif
 854
 855static int dvbdev_check_free_adapter_num(int num)
 856{
 857	struct list_head *entry;
 858
 859	list_for_each(entry, &dvb_adapter_list) {
 860		struct dvb_adapter *adap;
 861
 862		adap = list_entry(entry, struct dvb_adapter, list_head);
 863		if (adap->num == num)
 864			return 0;
 865	}
 866	return 1;
 867}
 868
 869static int dvbdev_get_free_adapter_num(void)
 870{
 871	int num = 0;
 872
 873	while (num < DVB_MAX_ADAPTERS) {
 874		if (dvbdev_check_free_adapter_num(num))
 875			return num;
 876		num++;
 877	}
 878
 879	return -ENFILE;
 880}
 881
 882int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
 883			 struct module *module, struct device *device,
 884			 short *adapter_nums)
 885{
 886	int i, num;
 887
 888	mutex_lock(&dvbdev_register_lock);
 889
 890	for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
 891		num = adapter_nums[i];
 892		if (num >= 0  &&  num < DVB_MAX_ADAPTERS) {
 893		/* use the one the driver asked for */
 894			if (dvbdev_check_free_adapter_num(num))
 895				break;
 896		} else {
 897			num = dvbdev_get_free_adapter_num();
 898			break;
 899		}
 900		num = -1;
 901	}
 902
 903	if (num < 0) {
 904		mutex_unlock(&dvbdev_register_lock);
 905		return -ENFILE;
 906	}
 907
 908	memset(adap, 0, sizeof(struct dvb_adapter));
 909	INIT_LIST_HEAD(&adap->device_list);
 910
 911	pr_info("DVB: registering new adapter (%s)\n", name);
 912
 913	adap->num = num;
 914	adap->name = name;
 915	adap->module = module;
 916	adap->device = device;
 917	adap->mfe_shared = 0;
 918	adap->mfe_dvbdev = NULL;
 919	mutex_init(&adap->mfe_lock);
 920
 921#ifdef CONFIG_MEDIA_CONTROLLER_DVB
 922	mutex_init(&adap->mdev_lock);
 923#endif
 924
 925	list_add_tail(&adap->list_head, &dvb_adapter_list);
 926
 927	mutex_unlock(&dvbdev_register_lock);
 928
 929	return num;
 930}
 931EXPORT_SYMBOL(dvb_register_adapter);
 932
 933int dvb_unregister_adapter(struct dvb_adapter *adap)
 934{
 935	mutex_lock(&dvbdev_register_lock);
 936	list_del(&adap->list_head);
 937	mutex_unlock(&dvbdev_register_lock);
 938	return 0;
 939}
 940EXPORT_SYMBOL(dvb_unregister_adapter);
 941
 942/*
 943 * if the miracle happens and "generic_usercopy()" is included into
 944 * the kernel, then this can vanish. please don't make the mistake and
 945 * define this as video_usercopy(). this will introduce a dependency
 946 * to the v4l "videodev.o" module, which is unnecessary for some
 947 * cards (ie. the budget dvb-cards don't need the v4l module...)
 948 */
 949int dvb_usercopy(struct file *file,
 950		 unsigned int cmd, unsigned long arg,
 951		 int (*func)(struct file *file,
 952			     unsigned int cmd, void *arg))
 953{
 954	char    sbuf[128];
 955	void    *mbuf = NULL;
 956	void    *parg = NULL;
 957	int     err  = -EINVAL;
 958
 959	/*  Copy arguments into temp kernel buffer  */
 960	switch (_IOC_DIR(cmd)) {
 961	case _IOC_NONE:
 962		/*
 963		 * For this command, the pointer is actually an integer
 964		 * argument.
 965		 */
 966		parg = (void *)arg;
 967		break;
 968	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
 969	case _IOC_WRITE:
 970	case (_IOC_WRITE | _IOC_READ):
 971		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
 972			parg = sbuf;
 973		} else {
 974			/* too big to allocate from stack */
 975			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
 976			if (!mbuf)
 977				return -ENOMEM;
 978			parg = mbuf;
 979		}
 980
 981		err = -EFAULT;
 982		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
 983			goto out;
 984		break;
 985	}
 986
 987	/* call driver */
 988	err = func(file, cmd, parg);
 989	if (err == -ENOIOCTLCMD)
 990		err = -ENOTTY;
 991
 992	if (err < 0)
 993		goto out;
 994
 995	/*  Copy results into user buffer  */
 996	switch (_IOC_DIR(cmd)) {
 997	case _IOC_READ:
 998	case (_IOC_WRITE | _IOC_READ):
 999		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1000			err = -EFAULT;
1001		break;
1002	}
1003
1004out:
1005	kfree(mbuf);
1006	return err;
1007}
1008
1009#if IS_ENABLED(CONFIG_I2C)
1010struct i2c_client *dvb_module_probe(const char *module_name,
1011				    const char *name,
1012				    struct i2c_adapter *adap,
1013				    unsigned char addr,
1014				    void *platform_data)
1015{
1016	struct i2c_client *client;
1017	struct i2c_board_info *board_info;
1018
1019	board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
1020	if (!board_info)
1021		return NULL;
1022
1023	if (name)
1024		strscpy(board_info->type, name, I2C_NAME_SIZE);
1025	else
1026		strscpy(board_info->type, module_name, I2C_NAME_SIZE);
1027
1028	board_info->addr = addr;
1029	board_info->platform_data = platform_data;
1030	request_module(module_name);
1031	client = i2c_new_client_device(adap, board_info);
1032	if (!i2c_client_has_driver(client)) {
1033		kfree(board_info);
1034		return NULL;
1035	}
1036
1037	if (!try_module_get(client->dev.driver->owner)) {
1038		i2c_unregister_device(client);
1039		client = NULL;
1040	}
1041
1042	kfree(board_info);
1043	return client;
1044}
1045EXPORT_SYMBOL_GPL(dvb_module_probe);
1046
1047void dvb_module_release(struct i2c_client *client)
1048{
1049	if (!client)
1050		return;
1051
1052	module_put(client->dev.driver->owner);
1053	i2c_unregister_device(client);
1054}
1055EXPORT_SYMBOL_GPL(dvb_module_release);
1056#endif
1057
1058static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env)
1059{
1060	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1061
1062	add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1063	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1064	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1065	return 0;
1066}
1067
1068static char *dvb_devnode(const struct device *dev, umode_t *mode)
1069{
1070	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1071
1072	return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1073		dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1074}
1075
1076static int __init init_dvbdev(void)
1077{
1078	int retval;
1079	dev_t dev = MKDEV(DVB_MAJOR, 0);
1080
1081	retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB");
1082	if (retval != 0) {
1083		pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1084		return retval;
1085	}
1086
1087	cdev_init(&dvb_device_cdev, &dvb_device_fops);
1088	retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS);
1089	if (retval != 0) {
1090		pr_err("dvb-core: unable register character device\n");
1091		goto error;
1092	}
1093
1094	dvb_class = class_create("dvb");
1095	if (IS_ERR(dvb_class)) {
1096		retval = PTR_ERR(dvb_class);
1097		goto error;
1098	}
1099	dvb_class->dev_uevent = dvb_uevent;
1100	dvb_class->devnode = dvb_devnode;
1101	return 0;
1102
1103error:
1104	cdev_del(&dvb_device_cdev);
1105	unregister_chrdev_region(dev, MAX_DVB_MINORS);
1106	return retval;
1107}
1108
1109static void __exit exit_dvbdev(void)
1110{
1111	struct dvbdevfops_node *node, *next;
1112
1113	class_destroy(dvb_class);
1114	cdev_del(&dvb_device_cdev);
1115	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1116
1117	list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) {
1118		list_del(&node->list_head);
1119		kfree(node->fops);
1120		kfree(node);
1121	}
1122}
1123
1124subsys_initcall(init_dvbdev);
1125module_exit(exit_dvbdev);
1126
1127MODULE_DESCRIPTION("DVB Core Driver");
1128MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1129MODULE_LICENSE("GPL");