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