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