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