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