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