xref: /openbmc/linux/drivers/media/dvb-core/dvbdev.c (revision 2984f26a)
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 			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 }
584 EXPORT_SYMBOL(dvb_register_device);
585 
586 void 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 }
602 EXPORT_SYMBOL(dvb_remove_device);
603 
604 static 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 
611 struct dvb_device *dvb_device_get(struct dvb_device *dvbdev)
612 {
613 	kref_get(&dvbdev->ref);
614 	return dvbdev;
615 }
616 EXPORT_SYMBOL(dvb_device_get);
617 
618 void dvb_device_put(struct dvb_device *dvbdev)
619 {
620 	if (dvbdev)
621 		kref_put(&dvbdev->ref, dvb_free_device);
622 }
623 
624 void dvb_unregister_device(struct dvb_device *dvbdev)
625 {
626 	dvb_remove_device(dvbdev);
627 	dvb_device_put(dvbdev);
628 }
629 EXPORT_SYMBOL(dvb_unregister_device);
630 
631 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
632 
633 static 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 
655 int 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 }
852 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
853 #endif
854 
855 static 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 
869 static 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 
882 int 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 }
931 EXPORT_SYMBOL(dvb_register_adapter);
932 
933 int 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 }
940 EXPORT_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  */
949 int 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 
1004 out:
1005 	kfree(mbuf);
1006 	return err;
1007 }
1008 
1009 #if IS_ENABLED(CONFIG_I2C)
1010 struct 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 }
1045 EXPORT_SYMBOL_GPL(dvb_module_probe);
1046 
1047 void 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 }
1055 EXPORT_SYMBOL_GPL(dvb_module_release);
1056 #endif
1057 
1058 static 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 
1068 static 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 
1076 static 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 
1103 error:
1104 	cdev_del(&dvb_device_cdev);
1105 	unregister_chrdev_region(dev, MAX_DVB_MINORS);
1106 	return retval;
1107 }
1108 
1109 static 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 
1124 subsys_initcall(init_dvbdev);
1125 module_exit(exit_dvbdev);
1126 
1127 MODULE_DESCRIPTION("DVB Core Driver");
1128 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1129 MODULE_LICENSE("GPL");
1130