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