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