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