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