xref: /openbmc/linux/drivers/media/dvb-core/dvbdev.c (revision fe4549b1)
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 		if (err)
108 			dvb_device_put(dvbdev);
109 		return err;
110 	}
111 fail:
112 	up_read(&minor_rwsem);
113 	mutex_unlock(&dvbdev_mutex);
114 	return -ENODEV;
115 }
116 
117 static const struct file_operations dvb_device_fops = {
118 	.owner =	THIS_MODULE,
119 	.open =		dvb_device_open,
120 	.llseek =	noop_llseek,
121 };
122 
123 static struct cdev dvb_device_cdev;
124 
125 int 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 }
148 EXPORT_SYMBOL(dvb_generic_open);
149 
150 int 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 }
168 EXPORT_SYMBOL(dvb_generic_release);
169 
170 long 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 }
183 EXPORT_SYMBOL(dvb_generic_ioctl);
184 
185 static 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;
196 skip:
197 		id++;
198 	}
199 	return -ENFILE;
200 }
201 
202 static 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)
244 static 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 
288 static 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 
385 static 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 
449 int 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 			*pdvbdev = NULL;
494 			mutex_unlock(&dvbdev_register_lock);
495 			return -ENOMEM;
496 		}
497 
498 		new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
499 		if (!new_node) {
500 			kfree(dvbdevfops);
501 			kfree(dvbdev);
502 			*pdvbdev = NULL;
503 			mutex_unlock(&dvbdev_register_lock);
504 			return -ENOMEM;
505 		}
506 
507 		new_node->fops = dvbdevfops;
508 		new_node->type = type;
509 		new_node->template = template;
510 		list_add_tail(&new_node->list_head, &dvbdevfops_list);
511 	}
512 
513 	memcpy(dvbdev, template, sizeof(struct dvb_device));
514 	kref_init(&dvbdev->ref);
515 	dvbdev->type = type;
516 	dvbdev->id = id;
517 	dvbdev->adapter = adap;
518 	dvbdev->priv = priv;
519 	dvbdev->fops = dvbdevfops;
520 	init_waitqueue_head(&dvbdev->wait_queue);
521 	dvbdevfops->owner = adap->module;
522 	list_add_tail(&dvbdev->list_head, &adap->device_list);
523 	down_write(&minor_rwsem);
524 #ifdef CONFIG_DVB_DYNAMIC_MINORS
525 	for (minor = 0; minor < MAX_DVB_MINORS; minor++)
526 		if (!dvb_minors[minor])
527 			break;
528 	if (minor == MAX_DVB_MINORS) {
529 		if (new_node) {
530 			list_del(&new_node->list_head);
531 			kfree(dvbdevfops);
532 			kfree(new_node);
533 		}
534 		list_del(&dvbdev->list_head);
535 		kfree(dvbdev);
536 		*pdvbdev = NULL;
537 		up_write(&minor_rwsem);
538 		mutex_unlock(&dvbdev_register_lock);
539 		return -EINVAL;
540 	}
541 #else
542 	minor = nums2minor(adap->num, type, id);
543 #endif
544 	dvbdev->minor = minor;
545 	dvb_minors[minor] = dvb_device_get(dvbdev);
546 	up_write(&minor_rwsem);
547 	ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
548 	if (ret) {
549 		pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
550 		       __func__);
551 		if (new_node) {
552 			list_del(&new_node->list_head);
553 			kfree(dvbdevfops);
554 			kfree(new_node);
555 		}
556 		dvb_media_device_free(dvbdev);
557 		list_del(&dvbdev->list_head);
558 		kfree(dvbdev);
559 		*pdvbdev = NULL;
560 		mutex_unlock(&dvbdev_register_lock);
561 		return ret;
562 	}
563 
564 	clsdev = device_create(dvb_class, adap->device,
565 			       MKDEV(DVB_MAJOR, minor),
566 			       dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
567 	if (IS_ERR(clsdev)) {
568 		pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
569 		       __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
570 		if (new_node) {
571 			list_del(&new_node->list_head);
572 			kfree(dvbdevfops);
573 			kfree(new_node);
574 		}
575 		dvb_media_device_free(dvbdev);
576 		list_del(&dvbdev->list_head);
577 		kfree(dvbdev);
578 		*pdvbdev = NULL;
579 		mutex_unlock(&dvbdev_register_lock);
580 		return PTR_ERR(clsdev);
581 	}
582 
583 	dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
584 		adap->num, dnames[type], id, minor, minor);
585 
586 	mutex_unlock(&dvbdev_register_lock);
587 	return 0;
588 }
589 EXPORT_SYMBOL(dvb_register_device);
590 
591 void dvb_remove_device(struct dvb_device *dvbdev)
592 {
593 	if (!dvbdev)
594 		return;
595 
596 	down_write(&minor_rwsem);
597 	dvb_minors[dvbdev->minor] = NULL;
598 	dvb_device_put(dvbdev);
599 	up_write(&minor_rwsem);
600 
601 	dvb_media_device_free(dvbdev);
602 
603 	device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
604 
605 	list_del(&dvbdev->list_head);
606 }
607 EXPORT_SYMBOL(dvb_remove_device);
608 
609 static void dvb_free_device(struct kref *ref)
610 {
611 	struct dvb_device *dvbdev = container_of(ref, struct dvb_device, ref);
612 
613 	kfree(dvbdev);
614 }
615 
616 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
617 {
618 	kref_get(&dvbdev->ref);
619 	return dvbdev;
620 }
621 EXPORT_SYMBOL(dvb_device_get);
622 
623 void dvb_device_put(struct dvb_device *dvbdev)
624 {
625 	if (dvbdev)
626 		kref_put(&dvbdev->ref, dvb_free_device);
627 }
628 
629 void dvb_unregister_device(struct dvb_device *dvbdev)
630 {
631 	dvb_remove_device(dvbdev);
632 	dvb_device_put(dvbdev);
633 }
634 EXPORT_SYMBOL(dvb_unregister_device);
635 
636 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
637 
638 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
639 				    struct media_interface *intf,
640 				    char *name)
641 {
642 	struct media_device *mdev = adap->mdev;
643 	struct media_entity *entity;
644 	struct media_link *link;
645 
646 	media_device_for_each_entity(entity, mdev) {
647 		if (entity->function == MEDIA_ENT_F_IO_DTV) {
648 			if (strncmp(entity->name, name, strlen(name)))
649 				continue;
650 			link = media_create_intf_link(entity, intf,
651 						      MEDIA_LNK_FL_ENABLED |
652 						      MEDIA_LNK_FL_IMMUTABLE);
653 			if (!link)
654 				return -ENOMEM;
655 		}
656 	}
657 	return 0;
658 }
659 
660 int dvb_create_media_graph(struct dvb_adapter *adap,
661 			   bool create_rf_connector)
662 {
663 	struct media_device *mdev = adap->mdev;
664 	struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
665 	struct media_entity *demux = NULL, *ca = NULL;
666 	struct media_link *link;
667 	struct media_interface *intf;
668 	unsigned int demux_pad = 0;
669 	unsigned int dvr_pad = 0;
670 	unsigned int ntuner = 0, ndemod = 0;
671 	int ret, pad_source, pad_sink;
672 	static const char *connector_name = "Television";
673 
674 	if (!mdev)
675 		return 0;
676 
677 	media_device_for_each_entity(entity, mdev) {
678 		switch (entity->function) {
679 		case MEDIA_ENT_F_TUNER:
680 			tuner = entity;
681 			ntuner++;
682 			break;
683 		case MEDIA_ENT_F_DTV_DEMOD:
684 			demod = entity;
685 			ndemod++;
686 			break;
687 		case MEDIA_ENT_F_TS_DEMUX:
688 			demux = entity;
689 			break;
690 		case MEDIA_ENT_F_DTV_CA:
691 			ca = entity;
692 			break;
693 		}
694 	}
695 
696 	/*
697 	 * Prepare to signalize to media_create_pad_links() that multiple
698 	 * entities of the same type exists and a 1:n or n:1 links need to be
699 	 * created.
700 	 * NOTE: if both tuner and demod have multiple instances, it is up
701 	 * to the caller driver to create such links.
702 	 */
703 	if (ntuner > 1)
704 		tuner = NULL;
705 	if (ndemod > 1)
706 		demod = NULL;
707 
708 	if (create_rf_connector) {
709 		conn = kzalloc(sizeof(*conn), GFP_KERNEL);
710 		if (!conn)
711 			return -ENOMEM;
712 		adap->conn = conn;
713 
714 		adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
715 		if (!adap->conn_pads)
716 			return -ENOMEM;
717 
718 		conn->flags = MEDIA_ENT_FL_CONNECTOR;
719 		conn->function = MEDIA_ENT_F_CONN_RF;
720 		conn->name = connector_name;
721 		adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
722 
723 		ret = media_entity_pads_init(conn, 1, adap->conn_pads);
724 		if (ret)
725 			return ret;
726 
727 		ret = media_device_register_entity(mdev, conn);
728 		if (ret)
729 			return ret;
730 
731 		if (!ntuner) {
732 			ret = media_create_pad_links(mdev,
733 						     MEDIA_ENT_F_CONN_RF,
734 						     conn, 0,
735 						     MEDIA_ENT_F_DTV_DEMOD,
736 						     demod, 0,
737 						     MEDIA_LNK_FL_ENABLED,
738 						     false);
739 		} else {
740 			pad_sink = media_get_pad_index(tuner, MEDIA_PAD_FL_SINK,
741 						       PAD_SIGNAL_ANALOG);
742 			if (pad_sink < 0)
743 				return -EINVAL;
744 			ret = media_create_pad_links(mdev,
745 						     MEDIA_ENT_F_CONN_RF,
746 						     conn, 0,
747 						     MEDIA_ENT_F_TUNER,
748 						     tuner, pad_sink,
749 						     MEDIA_LNK_FL_ENABLED,
750 						     false);
751 		}
752 		if (ret)
753 			return ret;
754 	}
755 
756 	if (ntuner && ndemod) {
757 		/* NOTE: first found tuner source pad presumed correct */
758 		pad_source = media_get_pad_index(tuner, MEDIA_PAD_FL_SOURCE,
759 						 PAD_SIGNAL_ANALOG);
760 		if (pad_source < 0)
761 			return -EINVAL;
762 		ret = media_create_pad_links(mdev,
763 					     MEDIA_ENT_F_TUNER,
764 					     tuner, pad_source,
765 					     MEDIA_ENT_F_DTV_DEMOD,
766 					     demod, 0, MEDIA_LNK_FL_ENABLED,
767 					     false);
768 		if (ret)
769 			return ret;
770 	}
771 
772 	if (ndemod && demux) {
773 		ret = media_create_pad_links(mdev,
774 					     MEDIA_ENT_F_DTV_DEMOD,
775 					     demod, 1,
776 					     MEDIA_ENT_F_TS_DEMUX,
777 					     demux, 0, MEDIA_LNK_FL_ENABLED,
778 					     false);
779 		if (ret)
780 			return ret;
781 	}
782 	if (demux && ca) {
783 		ret = media_create_pad_link(demux, 1, ca,
784 					    0, MEDIA_LNK_FL_ENABLED);
785 		if (ret)
786 			return ret;
787 	}
788 
789 	/* Create demux links for each ringbuffer/pad */
790 	if (demux) {
791 		media_device_for_each_entity(entity, mdev) {
792 			if (entity->function == MEDIA_ENT_F_IO_DTV) {
793 				if (!strncmp(entity->name, DVR_TSOUT,
794 					     strlen(DVR_TSOUT))) {
795 					ret = media_create_pad_link(demux,
796 								    ++dvr_pad,
797 								    entity, 0, 0);
798 					if (ret)
799 						return ret;
800 				}
801 				if (!strncmp(entity->name, DEMUX_TSOUT,
802 					     strlen(DEMUX_TSOUT))) {
803 					ret = media_create_pad_link(demux,
804 								    ++demux_pad,
805 								    entity, 0, 0);
806 					if (ret)
807 						return ret;
808 				}
809 			}
810 		}
811 	}
812 
813 	/* Create interface links for FE->tuner, DVR->demux and CA->ca */
814 	media_device_for_each_intf(intf, mdev) {
815 		if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
816 			link = media_create_intf_link(ca, intf,
817 						      MEDIA_LNK_FL_ENABLED |
818 						      MEDIA_LNK_FL_IMMUTABLE);
819 			if (!link)
820 				return -ENOMEM;
821 		}
822 
823 		if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
824 			link = media_create_intf_link(tuner, intf,
825 						      MEDIA_LNK_FL_ENABLED |
826 						      MEDIA_LNK_FL_IMMUTABLE);
827 			if (!link)
828 				return -ENOMEM;
829 		}
830 #if 0
831 		/*
832 		 * Indirect link - let's not create yet, as we don't know how
833 		 *		   to handle indirect links, nor if this will
834 		 *		   actually be needed.
835 		 */
836 		if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
837 			link = media_create_intf_link(demux, intf,
838 						      MEDIA_LNK_FL_ENABLED |
839 						      MEDIA_LNK_FL_IMMUTABLE);
840 			if (!link)
841 				return -ENOMEM;
842 		}
843 #endif
844 		if (intf->type == MEDIA_INTF_T_DVB_DVR) {
845 			ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
846 			if (ret)
847 				return ret;
848 		}
849 		if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
850 			ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
851 			if (ret)
852 				return ret;
853 		}
854 	}
855 	return 0;
856 }
857 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
858 #endif
859 
860 static int dvbdev_check_free_adapter_num(int num)
861 {
862 	struct list_head *entry;
863 
864 	list_for_each(entry, &dvb_adapter_list) {
865 		struct dvb_adapter *adap;
866 
867 		adap = list_entry(entry, struct dvb_adapter, list_head);
868 		if (adap->num == num)
869 			return 0;
870 	}
871 	return 1;
872 }
873 
874 static int dvbdev_get_free_adapter_num(void)
875 {
876 	int num = 0;
877 
878 	while (num < DVB_MAX_ADAPTERS) {
879 		if (dvbdev_check_free_adapter_num(num))
880 			return num;
881 		num++;
882 	}
883 
884 	return -ENFILE;
885 }
886 
887 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
888 			 struct module *module, struct device *device,
889 			 short *adapter_nums)
890 {
891 	int i, num;
892 
893 	mutex_lock(&dvbdev_register_lock);
894 
895 	for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
896 		num = adapter_nums[i];
897 		if (num >= 0  &&  num < DVB_MAX_ADAPTERS) {
898 		/* use the one the driver asked for */
899 			if (dvbdev_check_free_adapter_num(num))
900 				break;
901 		} else {
902 			num = dvbdev_get_free_adapter_num();
903 			break;
904 		}
905 		num = -1;
906 	}
907 
908 	if (num < 0) {
909 		mutex_unlock(&dvbdev_register_lock);
910 		return -ENFILE;
911 	}
912 
913 	memset(adap, 0, sizeof(struct dvb_adapter));
914 	INIT_LIST_HEAD(&adap->device_list);
915 
916 	pr_info("DVB: registering new adapter (%s)\n", name);
917 
918 	adap->num = num;
919 	adap->name = name;
920 	adap->module = module;
921 	adap->device = device;
922 	adap->mfe_shared = 0;
923 	adap->mfe_dvbdev = NULL;
924 	mutex_init(&adap->mfe_lock);
925 
926 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
927 	mutex_init(&adap->mdev_lock);
928 #endif
929 
930 	list_add_tail(&adap->list_head, &dvb_adapter_list);
931 
932 	mutex_unlock(&dvbdev_register_lock);
933 
934 	return num;
935 }
936 EXPORT_SYMBOL(dvb_register_adapter);
937 
938 int dvb_unregister_adapter(struct dvb_adapter *adap)
939 {
940 	mutex_lock(&dvbdev_register_lock);
941 	list_del(&adap->list_head);
942 	mutex_unlock(&dvbdev_register_lock);
943 	return 0;
944 }
945 EXPORT_SYMBOL(dvb_unregister_adapter);
946 
947 /*
948  * if the miracle happens and "generic_usercopy()" is included into
949  * the kernel, then this can vanish. please don't make the mistake and
950  * define this as video_usercopy(). this will introduce a dependency
951  * to the v4l "videodev.o" module, which is unnecessary for some
952  * cards (ie. the budget dvb-cards don't need the v4l module...)
953  */
954 int dvb_usercopy(struct file *file,
955 		 unsigned int cmd, unsigned long arg,
956 		 int (*func)(struct file *file,
957 			     unsigned int cmd, void *arg))
958 {
959 	char    sbuf[128];
960 	void    *mbuf = NULL;
961 	void    *parg = NULL;
962 	int     err  = -EINVAL;
963 
964 	/*  Copy arguments into temp kernel buffer  */
965 	switch (_IOC_DIR(cmd)) {
966 	case _IOC_NONE:
967 		/*
968 		 * For this command, the pointer is actually an integer
969 		 * argument.
970 		 */
971 		parg = (void *)arg;
972 		break;
973 	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
974 	case _IOC_WRITE:
975 	case (_IOC_WRITE | _IOC_READ):
976 		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
977 			parg = sbuf;
978 		} else {
979 			/* too big to allocate from stack */
980 			mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
981 			if (!mbuf)
982 				return -ENOMEM;
983 			parg = mbuf;
984 		}
985 
986 		err = -EFAULT;
987 		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
988 			goto out;
989 		break;
990 	}
991 
992 	/* call driver */
993 	err = func(file, cmd, parg);
994 	if (err == -ENOIOCTLCMD)
995 		err = -ENOTTY;
996 
997 	if (err < 0)
998 		goto out;
999 
1000 	/*  Copy results into user buffer  */
1001 	switch (_IOC_DIR(cmd)) {
1002 	case _IOC_READ:
1003 	case (_IOC_WRITE | _IOC_READ):
1004 		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
1005 			err = -EFAULT;
1006 		break;
1007 	}
1008 
1009 out:
1010 	kfree(mbuf);
1011 	return err;
1012 }
1013 
1014 #if IS_ENABLED(CONFIG_I2C)
1015 struct i2c_client *dvb_module_probe(const char *module_name,
1016 				    const char *name,
1017 				    struct i2c_adapter *adap,
1018 				    unsigned char addr,
1019 				    void *platform_data)
1020 {
1021 	struct i2c_client *client;
1022 	struct i2c_board_info *board_info;
1023 
1024 	board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
1025 	if (!board_info)
1026 		return NULL;
1027 
1028 	if (name)
1029 		strscpy(board_info->type, name, I2C_NAME_SIZE);
1030 	else
1031 		strscpy(board_info->type, module_name, I2C_NAME_SIZE);
1032 
1033 	board_info->addr = addr;
1034 	board_info->platform_data = platform_data;
1035 	request_module(module_name);
1036 	client = i2c_new_client_device(adap, board_info);
1037 	if (!i2c_client_has_driver(client)) {
1038 		kfree(board_info);
1039 		return NULL;
1040 	}
1041 
1042 	if (!try_module_get(client->dev.driver->owner)) {
1043 		i2c_unregister_device(client);
1044 		client = NULL;
1045 	}
1046 
1047 	kfree(board_info);
1048 	return client;
1049 }
1050 EXPORT_SYMBOL_GPL(dvb_module_probe);
1051 
1052 void dvb_module_release(struct i2c_client *client)
1053 {
1054 	if (!client)
1055 		return;
1056 
1057 	module_put(client->dev.driver->owner);
1058 	i2c_unregister_device(client);
1059 }
1060 EXPORT_SYMBOL_GPL(dvb_module_release);
1061 #endif
1062 
1063 static int dvb_uevent(const struct device *dev, struct kobj_uevent_env *env)
1064 {
1065 	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1066 
1067 	add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1068 	add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1069 	add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1070 	return 0;
1071 }
1072 
1073 static char *dvb_devnode(const struct device *dev, umode_t *mode)
1074 {
1075 	const struct dvb_device *dvbdev = dev_get_drvdata(dev);
1076 
1077 	return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1078 		dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1079 }
1080 
1081 static int __init init_dvbdev(void)
1082 {
1083 	int retval;
1084 	dev_t dev = MKDEV(DVB_MAJOR, 0);
1085 
1086 	retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB");
1087 	if (retval != 0) {
1088 		pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1089 		return retval;
1090 	}
1091 
1092 	cdev_init(&dvb_device_cdev, &dvb_device_fops);
1093 	retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS);
1094 	if (retval != 0) {
1095 		pr_err("dvb-core: unable register character device\n");
1096 		goto error;
1097 	}
1098 
1099 	dvb_class = class_create("dvb");
1100 	if (IS_ERR(dvb_class)) {
1101 		retval = PTR_ERR(dvb_class);
1102 		goto error;
1103 	}
1104 	dvb_class->dev_uevent = dvb_uevent;
1105 	dvb_class->devnode = dvb_devnode;
1106 	return 0;
1107 
1108 error:
1109 	cdev_del(&dvb_device_cdev);
1110 	unregister_chrdev_region(dev, MAX_DVB_MINORS);
1111 	return retval;
1112 }
1113 
1114 static void __exit exit_dvbdev(void)
1115 {
1116 	struct dvbdevfops_node *node, *next;
1117 
1118 	class_destroy(dvb_class);
1119 	cdev_del(&dvb_device_cdev);
1120 	unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1121 
1122 	list_for_each_entry_safe(node, next, &dvbdevfops_list, list_head) {
1123 		list_del(&node->list_head);
1124 		kfree(node->fops);
1125 		kfree(node);
1126 	}
1127 }
1128 
1129 subsys_initcall(init_dvbdev);
1130 module_exit(exit_dvbdev);
1131 
1132 MODULE_DESCRIPTION("DVB Core Driver");
1133 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1134 MODULE_LICENSE("GPL");
1135