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