Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / dvb-core / dvbdev.c
blob04dc2f4bc7aafd4d40d6486f22cf9a79231307c2
1 /*
2 * dvbdev.c
4 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 * for convergence integrated media GmbH
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #define pr_fmt(fmt) "dvbdev: " fmt
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/string.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/i2c.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/device.h>
31 #include <linux/fs.h>
32 #include <linux/cdev.h>
33 #include <linux/mutex.h>
34 #include <media/dvbdev.h>
36 /* Due to enum tuner_pad_index */
37 #include <media/tuner.h>
39 static DEFINE_MUTEX(dvbdev_mutex);
40 static int dvbdev_debug;
42 module_param(dvbdev_debug, int, 0644);
43 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
45 #define dprintk(fmt, arg...) do { \
46 if (dvbdev_debug) \
47 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
48 __func__, ##arg); \
49 } while (0)
51 static LIST_HEAD(dvb_adapter_list);
52 static DEFINE_MUTEX(dvbdev_register_lock);
54 static const char * const dnames[] = {
55 [DVB_DEVICE_VIDEO] = "video",
56 [DVB_DEVICE_AUDIO] = "audio",
57 [DVB_DEVICE_SEC] = "sec",
58 [DVB_DEVICE_FRONTEND] = "frontend",
59 [DVB_DEVICE_DEMUX] = "demux",
60 [DVB_DEVICE_DVR] = "dvr",
61 [DVB_DEVICE_CA] = "ca",
62 [DVB_DEVICE_NET] = "net",
63 [DVB_DEVICE_OSD] = "osd"
66 #ifdef CONFIG_DVB_DYNAMIC_MINORS
67 #define MAX_DVB_MINORS 256
68 #define DVB_MAX_IDS MAX_DVB_MINORS
69 #else
70 #define DVB_MAX_IDS 4
72 static const u8 minor_type[] = {
73 [DVB_DEVICE_VIDEO] = 0,
74 [DVB_DEVICE_AUDIO] = 1,
75 [DVB_DEVICE_SEC] = 2,
76 [DVB_DEVICE_FRONTEND] = 3,
77 [DVB_DEVICE_DEMUX] = 4,
78 [DVB_DEVICE_DVR] = 5,
79 [DVB_DEVICE_CA] = 6,
80 [DVB_DEVICE_NET] = 7,
81 [DVB_DEVICE_OSD] = 8,
84 #define nums2minor(num, type, id) \
85 (((num) << 6) | ((id) << 4) | minor_type[type])
87 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
88 #endif
90 static struct class *dvb_class;
92 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
93 static DECLARE_RWSEM(minor_rwsem);
95 static int dvb_device_open(struct inode *inode, struct file *file)
97 struct dvb_device *dvbdev;
99 mutex_lock(&dvbdev_mutex);
100 down_read(&minor_rwsem);
101 dvbdev = dvb_minors[iminor(inode)];
103 if (dvbdev && dvbdev->fops) {
104 int err = 0;
105 const struct file_operations *new_fops;
107 new_fops = fops_get(dvbdev->fops);
108 if (!new_fops)
109 goto fail;
110 file->private_data = dvbdev;
111 replace_fops(file, new_fops);
112 if (file->f_op->open)
113 err = file->f_op->open(inode, file);
114 up_read(&minor_rwsem);
115 mutex_unlock(&dvbdev_mutex);
116 return err;
118 fail:
119 up_read(&minor_rwsem);
120 mutex_unlock(&dvbdev_mutex);
121 return -ENODEV;
125 static const struct file_operations dvb_device_fops =
127 .owner = THIS_MODULE,
128 .open = dvb_device_open,
129 .llseek = noop_llseek,
132 static struct cdev dvb_device_cdev;
134 int dvb_generic_open(struct inode *inode, struct file *file)
136 struct dvb_device *dvbdev = file->private_data;
138 if (!dvbdev)
139 return -ENODEV;
141 if (!dvbdev->users)
142 return -EBUSY;
144 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
145 if (!dvbdev->readers)
146 return -EBUSY;
147 dvbdev->readers--;
148 } else {
149 if (!dvbdev->writers)
150 return -EBUSY;
151 dvbdev->writers--;
154 dvbdev->users--;
155 return 0;
157 EXPORT_SYMBOL(dvb_generic_open);
160 int dvb_generic_release(struct inode *inode, struct file *file)
162 struct dvb_device *dvbdev = file->private_data;
164 if (!dvbdev)
165 return -ENODEV;
167 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
168 dvbdev->readers++;
169 } else {
170 dvbdev->writers++;
173 dvbdev->users++;
174 return 0;
176 EXPORT_SYMBOL(dvb_generic_release);
179 long dvb_generic_ioctl(struct file *file,
180 unsigned int cmd, unsigned long arg)
182 struct dvb_device *dvbdev = file->private_data;
184 if (!dvbdev)
185 return -ENODEV;
187 if (!dvbdev->kernel_ioctl)
188 return -EINVAL;
190 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
192 EXPORT_SYMBOL(dvb_generic_ioctl);
195 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
197 u32 id = 0;
199 while (id < DVB_MAX_IDS) {
200 struct dvb_device *dev;
201 list_for_each_entry(dev, &adap->device_list, list_head)
202 if (dev->type == type && dev->id == id)
203 goto skip;
204 return id;
205 skip:
206 id++;
208 return -ENFILE;
211 static void dvb_media_device_free(struct dvb_device *dvbdev)
213 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
214 if (dvbdev->entity) {
215 media_device_unregister_entity(dvbdev->entity);
216 kfree(dvbdev->entity);
217 kfree(dvbdev->pads);
218 dvbdev->entity = NULL;
219 dvbdev->pads = NULL;
222 if (dvbdev->tsout_entity) {
223 int i;
225 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
226 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
227 kfree(dvbdev->tsout_entity[i].name);
229 kfree(dvbdev->tsout_entity);
230 kfree(dvbdev->tsout_pads);
231 dvbdev->tsout_entity = NULL;
232 dvbdev->tsout_pads = NULL;
234 dvbdev->tsout_num_entities = 0;
237 if (dvbdev->intf_devnode) {
238 media_devnode_remove(dvbdev->intf_devnode);
239 dvbdev->intf_devnode = NULL;
242 if (dvbdev->adapter->conn) {
243 media_device_unregister_entity(dvbdev->adapter->conn);
244 dvbdev->adapter->conn = NULL;
245 kfree(dvbdev->adapter->conn_pads);
246 dvbdev->adapter->conn_pads = NULL;
248 #endif
251 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
252 static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
253 const char *name, int npads)
255 int i, ret = 0;
257 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
258 GFP_KERNEL);
259 if (!dvbdev->tsout_pads)
260 return -ENOMEM;
262 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
263 GFP_KERNEL);
264 if (!dvbdev->tsout_entity)
265 return -ENOMEM;
267 dvbdev->tsout_num_entities = npads;
269 for (i = 0; i < npads; i++) {
270 struct media_pad *pads = &dvbdev->tsout_pads[i];
271 struct media_entity *entity = &dvbdev->tsout_entity[i];
273 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
274 if (!entity->name)
275 return -ENOMEM;
277 entity->function = MEDIA_ENT_F_IO_DTV;
278 pads->flags = MEDIA_PAD_FL_SINK;
280 ret = media_entity_pads_init(entity, 1, pads);
281 if (ret < 0)
282 return ret;
284 ret = media_device_register_entity(dvbdev->adapter->mdev,
285 entity);
286 if (ret < 0)
287 return ret;
289 return 0;
292 #define DEMUX_TSOUT "demux-tsout"
293 #define DVR_TSOUT "dvr-tsout"
295 static int dvb_create_media_entity(struct dvb_device *dvbdev,
296 int type, int demux_sink_pads)
298 int i, ret, npads;
300 switch (type) {
301 case DVB_DEVICE_FRONTEND:
302 npads = 2;
303 break;
304 case DVB_DEVICE_DVR:
305 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
306 demux_sink_pads);
307 return ret;
308 case DVB_DEVICE_DEMUX:
309 npads = 1 + demux_sink_pads;
310 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
311 demux_sink_pads);
312 if (ret < 0)
313 return ret;
314 break;
315 case DVB_DEVICE_CA:
316 npads = 2;
317 break;
318 case DVB_DEVICE_NET:
320 * We should be creating entities for the MPE/ULE
321 * decapsulation hardware (or software implementation).
323 * However, the number of for the MPE/ULE decaps may not be
324 * fixed. As we don't have yet dynamic support for PADs at
325 * the Media Controller, let's not create the decap
326 * entities yet.
328 return 0;
329 default:
330 return 0;
333 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
334 if (!dvbdev->entity)
335 return -ENOMEM;
337 dvbdev->entity->name = dvbdev->name;
339 if (npads) {
340 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
341 GFP_KERNEL);
342 if (!dvbdev->pads) {
343 kfree(dvbdev->entity);
344 return -ENOMEM;
348 switch (type) {
349 case DVB_DEVICE_FRONTEND:
350 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
351 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
352 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
353 break;
354 case DVB_DEVICE_DEMUX:
355 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
356 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
357 for (i = 1; i < npads; i++)
358 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
359 break;
360 case DVB_DEVICE_CA:
361 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
362 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
363 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
364 break;
365 default:
366 /* Should never happen, as the first switch prevents it */
367 kfree(dvbdev->entity);
368 kfree(dvbdev->pads);
369 dvbdev->entity = NULL;
370 dvbdev->pads = NULL;
371 return 0;
374 if (npads) {
375 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
376 if (ret)
377 return ret;
379 ret = media_device_register_entity(dvbdev->adapter->mdev,
380 dvbdev->entity);
381 if (ret)
382 return ret;
384 pr_info("%s: media entity '%s' registered.\n",
385 __func__, dvbdev->entity->name);
387 return 0;
389 #endif
391 static int dvb_register_media_device(struct dvb_device *dvbdev,
392 int type, int minor,
393 unsigned demux_sink_pads)
395 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
396 struct media_link *link;
397 u32 intf_type;
398 int ret;
400 if (!dvbdev->adapter->mdev)
401 return 0;
403 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
404 if (ret)
405 return ret;
407 switch (type) {
408 case DVB_DEVICE_FRONTEND:
409 intf_type = MEDIA_INTF_T_DVB_FE;
410 break;
411 case DVB_DEVICE_DEMUX:
412 intf_type = MEDIA_INTF_T_DVB_DEMUX;
413 break;
414 case DVB_DEVICE_DVR:
415 intf_type = MEDIA_INTF_T_DVB_DVR;
416 break;
417 case DVB_DEVICE_CA:
418 intf_type = MEDIA_INTF_T_DVB_CA;
419 break;
420 case DVB_DEVICE_NET:
421 intf_type = MEDIA_INTF_T_DVB_NET;
422 break;
423 default:
424 return 0;
427 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
428 intf_type, 0,
429 DVB_MAJOR, minor);
431 if (!dvbdev->intf_devnode)
432 return -ENOMEM;
435 * Create the "obvious" link, e. g. the ones that represent
436 * a direct association between an interface and an entity.
437 * Other links should be created elsewhere, like:
438 * DVB FE intf -> tuner
439 * DVB demux intf -> dvr
442 if (!dvbdev->entity)
443 return 0;
445 link = media_create_intf_link(dvbdev->entity,
446 &dvbdev->intf_devnode->intf,
447 MEDIA_LNK_FL_ENABLED |
448 MEDIA_LNK_FL_IMMUTABLE);
449 if (!link)
450 return -ENOMEM;
451 #endif
452 return 0;
455 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
456 const struct dvb_device *template, void *priv,
457 enum dvb_device_type type, int demux_sink_pads)
459 struct dvb_device *dvbdev;
460 struct file_operations *dvbdevfops;
461 struct device *clsdev;
462 int minor;
463 int id, ret;
465 mutex_lock(&dvbdev_register_lock);
467 if ((id = dvbdev_get_free_id (adap, type)) < 0){
468 mutex_unlock(&dvbdev_register_lock);
469 *pdvbdev = NULL;
470 pr_err("%s: couldn't find free device id\n", __func__);
471 return -ENFILE;
474 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
476 if (!dvbdev){
477 mutex_unlock(&dvbdev_register_lock);
478 return -ENOMEM;
481 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
483 if (!dvbdevfops){
484 kfree (dvbdev);
485 mutex_unlock(&dvbdev_register_lock);
486 return -ENOMEM;
489 memcpy(dvbdev, template, sizeof(struct dvb_device));
490 dvbdev->type = type;
491 dvbdev->id = id;
492 dvbdev->adapter = adap;
493 dvbdev->priv = priv;
494 dvbdev->fops = dvbdevfops;
495 init_waitqueue_head (&dvbdev->wait_queue);
497 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
498 dvbdevfops->owner = adap->module;
500 list_add_tail (&dvbdev->list_head, &adap->device_list);
502 down_write(&minor_rwsem);
503 #ifdef CONFIG_DVB_DYNAMIC_MINORS
504 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
505 if (dvb_minors[minor] == NULL)
506 break;
508 if (minor == MAX_DVB_MINORS) {
509 kfree(dvbdevfops);
510 kfree(dvbdev);
511 up_write(&minor_rwsem);
512 mutex_unlock(&dvbdev_register_lock);
513 return -EINVAL;
515 #else
516 minor = nums2minor(adap->num, type, id);
517 #endif
519 dvbdev->minor = minor;
520 dvb_minors[minor] = dvbdev;
521 up_write(&minor_rwsem);
523 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
524 if (ret) {
525 pr_err("%s: dvb_register_media_device failed to create the mediagraph\n",
526 __func__);
528 dvb_media_device_free(dvbdev);
529 kfree(dvbdevfops);
530 kfree(dvbdev);
531 up_write(&minor_rwsem);
532 mutex_unlock(&dvbdev_register_lock);
533 return ret;
536 mutex_unlock(&dvbdev_register_lock);
538 clsdev = device_create(dvb_class, adap->device,
539 MKDEV(DVB_MAJOR, minor),
540 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
541 if (IS_ERR(clsdev)) {
542 pr_err("%s: failed to create device dvb%d.%s%d (%ld)\n",
543 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
544 return PTR_ERR(clsdev);
546 dprintk("DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
547 adap->num, dnames[type], id, minor, minor);
549 return 0;
551 EXPORT_SYMBOL(dvb_register_device);
554 void dvb_remove_device(struct dvb_device *dvbdev)
556 if (!dvbdev)
557 return;
559 down_write(&minor_rwsem);
560 dvb_minors[dvbdev->minor] = NULL;
561 up_write(&minor_rwsem);
563 dvb_media_device_free(dvbdev);
565 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
567 list_del (&dvbdev->list_head);
569 EXPORT_SYMBOL(dvb_remove_device);
572 void dvb_free_device(struct dvb_device *dvbdev)
574 if (!dvbdev)
575 return;
577 kfree (dvbdev->fops);
578 kfree (dvbdev);
580 EXPORT_SYMBOL(dvb_free_device);
583 void dvb_unregister_device(struct dvb_device *dvbdev)
585 dvb_remove_device(dvbdev);
586 dvb_free_device(dvbdev);
588 EXPORT_SYMBOL(dvb_unregister_device);
591 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
593 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
594 struct media_interface *intf,
595 char *name)
597 struct media_device *mdev = adap->mdev;
598 struct media_entity *entity;
599 struct media_link *link;
601 media_device_for_each_entity(entity, mdev) {
602 if (entity->function == MEDIA_ENT_F_IO_DTV) {
603 if (strncmp(entity->name, name, strlen(name)))
604 continue;
605 link = media_create_intf_link(entity, intf,
606 MEDIA_LNK_FL_ENABLED |
607 MEDIA_LNK_FL_IMMUTABLE);
608 if (!link)
609 return -ENOMEM;
612 return 0;
615 int dvb_create_media_graph(struct dvb_adapter *adap,
616 bool create_rf_connector)
618 struct media_device *mdev = adap->mdev;
619 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
620 struct media_entity *demux = NULL, *ca = NULL;
621 struct media_link *link;
622 struct media_interface *intf;
623 unsigned demux_pad = 0;
624 unsigned dvr_pad = 0;
625 unsigned ntuner = 0, ndemod = 0;
626 int ret;
627 static const char *connector_name = "Television";
629 if (!mdev)
630 return 0;
632 media_device_for_each_entity(entity, mdev) {
633 switch (entity->function) {
634 case MEDIA_ENT_F_TUNER:
635 tuner = entity;
636 ntuner++;
637 break;
638 case MEDIA_ENT_F_DTV_DEMOD:
639 demod = entity;
640 ndemod++;
641 break;
642 case MEDIA_ENT_F_TS_DEMUX:
643 demux = entity;
644 break;
645 case MEDIA_ENT_F_DTV_CA:
646 ca = entity;
647 break;
652 * Prepare to signalize to media_create_pad_links() that multiple
653 * entities of the same type exists and a 1:n or n:1 links need to be
654 * created.
655 * NOTE: if both tuner and demod have multiple instances, it is up
656 * to the caller driver to create such links.
658 if (ntuner > 1)
659 tuner = NULL;
660 if (ndemod > 1)
661 demod = NULL;
663 if (create_rf_connector) {
664 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
665 if (!conn)
666 return -ENOMEM;
667 adap->conn = conn;
669 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
670 if (!adap->conn_pads)
671 return -ENOMEM;
673 conn->flags = MEDIA_ENT_FL_CONNECTOR;
674 conn->function = MEDIA_ENT_F_CONN_RF;
675 conn->name = connector_name;
676 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
678 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
679 if (ret)
680 return ret;
682 ret = media_device_register_entity(mdev, conn);
683 if (ret)
684 return ret;
686 if (!ntuner)
687 ret = media_create_pad_links(mdev,
688 MEDIA_ENT_F_CONN_RF,
689 conn, 0,
690 MEDIA_ENT_F_DTV_DEMOD,
691 demod, 0,
692 MEDIA_LNK_FL_ENABLED,
693 false);
694 else
695 ret = media_create_pad_links(mdev,
696 MEDIA_ENT_F_CONN_RF,
697 conn, 0,
698 MEDIA_ENT_F_TUNER,
699 tuner, TUNER_PAD_RF_INPUT,
700 MEDIA_LNK_FL_ENABLED,
701 false);
702 if (ret)
703 return ret;
706 if (ntuner && ndemod) {
707 ret = media_create_pad_links(mdev,
708 MEDIA_ENT_F_TUNER,
709 tuner, TUNER_PAD_OUTPUT,
710 MEDIA_ENT_F_DTV_DEMOD,
711 demod, 0, MEDIA_LNK_FL_ENABLED,
712 false);
713 if (ret)
714 return ret;
717 if (ndemod && demux) {
718 ret = media_create_pad_links(mdev,
719 MEDIA_ENT_F_DTV_DEMOD,
720 demod, 1,
721 MEDIA_ENT_F_TS_DEMUX,
722 demux, 0, MEDIA_LNK_FL_ENABLED,
723 false);
724 if (ret)
725 return ret;
727 if (demux && ca) {
728 ret = media_create_pad_link(demux, 1, ca,
729 0, MEDIA_LNK_FL_ENABLED);
730 if (ret)
731 return ret;
734 /* Create demux links for each ringbuffer/pad */
735 if (demux) {
736 media_device_for_each_entity(entity, mdev) {
737 if (entity->function == MEDIA_ENT_F_IO_DTV) {
738 if (!strncmp(entity->name, DVR_TSOUT,
739 strlen(DVR_TSOUT))) {
740 ret = media_create_pad_link(demux,
741 ++dvr_pad,
742 entity, 0, 0);
743 if (ret)
744 return ret;
746 if (!strncmp(entity->name, DEMUX_TSOUT,
747 strlen(DEMUX_TSOUT))) {
748 ret = media_create_pad_link(demux,
749 ++demux_pad,
750 entity, 0, 0);
751 if (ret)
752 return ret;
758 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
759 media_device_for_each_intf(intf, mdev) {
760 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
761 link = media_create_intf_link(ca, intf,
762 MEDIA_LNK_FL_ENABLED |
763 MEDIA_LNK_FL_IMMUTABLE);
764 if (!link)
765 return -ENOMEM;
768 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
769 link = media_create_intf_link(tuner, intf,
770 MEDIA_LNK_FL_ENABLED |
771 MEDIA_LNK_FL_IMMUTABLE);
772 if (!link)
773 return -ENOMEM;
775 #if 0
777 * Indirect link - let's not create yet, as we don't know how
778 * to handle indirect links, nor if this will
779 * actually be needed.
781 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
782 link = media_create_intf_link(demux, intf,
783 MEDIA_LNK_FL_ENABLED |
784 MEDIA_LNK_FL_IMMUTABLE);
785 if (!link)
786 return -ENOMEM;
788 #endif
789 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
790 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
791 if (ret)
792 return ret;
794 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
795 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
796 if (ret)
797 return ret;
800 return 0;
802 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
803 #endif
805 static int dvbdev_check_free_adapter_num(int num)
807 struct list_head *entry;
808 list_for_each(entry, &dvb_adapter_list) {
809 struct dvb_adapter *adap;
810 adap = list_entry(entry, struct dvb_adapter, list_head);
811 if (adap->num == num)
812 return 0;
814 return 1;
817 static int dvbdev_get_free_adapter_num (void)
819 int num = 0;
821 while (num < DVB_MAX_ADAPTERS) {
822 if (dvbdev_check_free_adapter_num(num))
823 return num;
824 num++;
827 return -ENFILE;
831 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
832 struct module *module, struct device *device,
833 short *adapter_nums)
835 int i, num;
837 mutex_lock(&dvbdev_register_lock);
839 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
840 num = adapter_nums[i];
841 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
842 /* use the one the driver asked for */
843 if (dvbdev_check_free_adapter_num(num))
844 break;
845 } else {
846 num = dvbdev_get_free_adapter_num();
847 break;
849 num = -1;
852 if (num < 0) {
853 mutex_unlock(&dvbdev_register_lock);
854 return -ENFILE;
857 memset (adap, 0, sizeof(struct dvb_adapter));
858 INIT_LIST_HEAD (&adap->device_list);
860 pr_info("DVB: registering new adapter (%s)\n", name);
862 adap->num = num;
863 adap->name = name;
864 adap->module = module;
865 adap->device = device;
866 adap->mfe_shared = 0;
867 adap->mfe_dvbdev = NULL;
868 mutex_init (&adap->mfe_lock);
870 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
871 mutex_init(&adap->mdev_lock);
872 #endif
874 list_add_tail (&adap->list_head, &dvb_adapter_list);
876 mutex_unlock(&dvbdev_register_lock);
878 return num;
880 EXPORT_SYMBOL(dvb_register_adapter);
883 int dvb_unregister_adapter(struct dvb_adapter *adap)
885 mutex_lock(&dvbdev_register_lock);
886 list_del (&adap->list_head);
887 mutex_unlock(&dvbdev_register_lock);
888 return 0;
890 EXPORT_SYMBOL(dvb_unregister_adapter);
892 /* if the miracle happens and "generic_usercopy()" is included into
893 the kernel, then this can vanish. please don't make the mistake and
894 define this as video_usercopy(). this will introduce a dependecy
895 to the v4l "videodev.o" module, which is unnecessary for some
896 cards (ie. the budget dvb-cards don't need the v4l module...) */
897 int dvb_usercopy(struct file *file,
898 unsigned int cmd, unsigned long arg,
899 int (*func)(struct file *file,
900 unsigned int cmd, void *arg))
902 char sbuf[128];
903 void *mbuf = NULL;
904 void *parg = NULL;
905 int err = -EINVAL;
907 /* Copy arguments into temp kernel buffer */
908 switch (_IOC_DIR(cmd)) {
909 case _IOC_NONE:
911 * For this command, the pointer is actually an integer
912 * argument.
914 parg = (void *) arg;
915 break;
916 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
917 case _IOC_WRITE:
918 case (_IOC_WRITE | _IOC_READ):
919 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
920 parg = sbuf;
921 } else {
922 /* too big to allocate from stack */
923 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
924 if (NULL == mbuf)
925 return -ENOMEM;
926 parg = mbuf;
929 err = -EFAULT;
930 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
931 goto out;
932 break;
935 /* call driver */
936 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
937 err = -ENOTTY;
939 if (err < 0)
940 goto out;
942 /* Copy results into user buffer */
943 switch (_IOC_DIR(cmd))
945 case _IOC_READ:
946 case (_IOC_WRITE | _IOC_READ):
947 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
948 err = -EFAULT;
949 break;
952 out:
953 kfree(mbuf);
954 return err;
957 #if IS_ENABLED(CONFIG_I2C)
958 struct i2c_client *dvb_module_probe(const char *module_name,
959 const char *name,
960 struct i2c_adapter *adap,
961 unsigned char addr,
962 void *platform_data)
964 struct i2c_client *client;
965 struct i2c_board_info *board_info;
967 board_info = kzalloc(sizeof(*board_info), GFP_KERNEL);
968 if (!board_info)
969 return NULL;
971 if (name)
972 strlcpy(board_info->type, name, I2C_NAME_SIZE);
973 else
974 strlcpy(board_info->type, module_name, I2C_NAME_SIZE);
976 board_info->addr = addr;
977 board_info->platform_data = platform_data;
978 request_module(module_name);
979 client = i2c_new_device(adap, board_info);
980 if (client == NULL || client->dev.driver == NULL) {
981 kfree(board_info);
982 return NULL;
985 if (!try_module_get(client->dev.driver->owner)) {
986 i2c_unregister_device(client);
987 client = NULL;
990 kfree(board_info);
991 return client;
993 EXPORT_SYMBOL_GPL(dvb_module_probe);
995 void dvb_module_release(struct i2c_client *client)
997 if (!client)
998 return;
1000 module_put(client->dev.driver->owner);
1001 i2c_unregister_device(client);
1003 EXPORT_SYMBOL_GPL(dvb_module_release);
1004 #endif
1006 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
1008 struct dvb_device *dvbdev = dev_get_drvdata(dev);
1010 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
1011 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
1012 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
1013 return 0;
1016 static char *dvb_devnode(struct device *dev, umode_t *mode)
1018 struct dvb_device *dvbdev = dev_get_drvdata(dev);
1020 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
1021 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
1025 static int __init init_dvbdev(void)
1027 int retval;
1028 dev_t dev = MKDEV(DVB_MAJOR, 0);
1030 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
1031 pr_err("dvb-core: unable to get major %d\n", DVB_MAJOR);
1032 return retval;
1035 cdev_init(&dvb_device_cdev, &dvb_device_fops);
1036 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
1037 pr_err("dvb-core: unable register character device\n");
1038 goto error;
1041 dvb_class = class_create(THIS_MODULE, "dvb");
1042 if (IS_ERR(dvb_class)) {
1043 retval = PTR_ERR(dvb_class);
1044 goto error;
1046 dvb_class->dev_uevent = dvb_uevent;
1047 dvb_class->devnode = dvb_devnode;
1048 return 0;
1050 error:
1051 cdev_del(&dvb_device_cdev);
1052 unregister_chrdev_region(dev, MAX_DVB_MINORS);
1053 return retval;
1057 static void __exit exit_dvbdev(void)
1059 class_destroy(dvb_class);
1060 cdev_del(&dvb_device_cdev);
1061 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
1064 subsys_initcall(init_dvbdev);
1065 module_exit(exit_dvbdev);
1067 MODULE_DESCRIPTION("DVB Core Driver");
1068 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
1069 MODULE_LICENSE("GPL");