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.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/device.h>
33 #include <linux/cdev.h>
34 #include <linux/mutex.h>
37 /* Due to enum tuner_pad_index */
38 #include <media/tuner.h>
40 static DEFINE_MUTEX(dvbdev_mutex);
41 static int dvbdev_debug;
43 module_param(dvbdev_debug, int, 0644);
44 MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
46 #define dprintk if (dvbdev_debug) printk
48 static LIST_HEAD(dvb_adapter_list);
49 static DEFINE_MUTEX(dvbdev_register_lock);
51 static const char * const dnames[] = {
52 "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
56 #ifdef CONFIG_DVB_DYNAMIC_MINORS
57 #define MAX_DVB_MINORS 256
58 #define DVB_MAX_IDS MAX_DVB_MINORS
61 #define nums2minor(num, type, id) ((num << 6) | (id << 4) | type)
62 #define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)
65 static struct class *dvb_class;
67 static struct dvb_device *dvb_minors[MAX_DVB_MINORS];
68 static DECLARE_RWSEM(minor_rwsem);
70 static int dvb_device_open(struct inode *inode, struct file *file)
72 struct dvb_device *dvbdev;
74 mutex_lock(&dvbdev_mutex);
75 down_read(&minor_rwsem);
76 dvbdev = dvb_minors[iminor(inode)];
78 if (dvbdev && dvbdev->fops) {
80 const struct file_operations *new_fops;
82 new_fops = fops_get(dvbdev->fops);
85 file->private_data = dvbdev;
86 replace_fops(file, new_fops);
88 err = file->f_op->open(inode, file);
89 up_read(&minor_rwsem);
90 mutex_unlock(&dvbdev_mutex);
94 up_read(&minor_rwsem);
95 mutex_unlock(&dvbdev_mutex);
100 static const struct file_operations dvb_device_fops =
102 .owner = THIS_MODULE,
103 .open = dvb_device_open,
104 .llseek = noop_llseek,
107 static struct cdev dvb_device_cdev;
109 int dvb_generic_open(struct inode *inode, struct file *file)
111 struct dvb_device *dvbdev = file->private_data;
119 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
120 if (!dvbdev->readers)
124 if (!dvbdev->writers)
132 EXPORT_SYMBOL(dvb_generic_open);
135 int dvb_generic_release(struct inode *inode, struct file *file)
137 struct dvb_device *dvbdev = file->private_data;
142 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
151 EXPORT_SYMBOL(dvb_generic_release);
154 long dvb_generic_ioctl(struct file *file,
155 unsigned int cmd, unsigned long arg)
157 struct dvb_device *dvbdev = file->private_data;
162 if (!dvbdev->kernel_ioctl)
165 return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);
167 EXPORT_SYMBOL(dvb_generic_ioctl);
170 static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
174 while (id < DVB_MAX_IDS) {
175 struct dvb_device *dev;
176 list_for_each_entry(dev, &adap->device_list, list_head)
177 if (dev->type == type && dev->id == id)
186 static void dvb_media_device_free(struct dvb_device *dvbdev)
188 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
189 if (dvbdev->entity) {
190 media_device_unregister_entity(dvbdev->entity);
191 kfree(dvbdev->entity);
193 dvbdev->entity = NULL;
197 if (dvbdev->tsout_entity) {
200 for (i = 0; i < dvbdev->tsout_num_entities; i++) {
201 media_device_unregister_entity(&dvbdev->tsout_entity[i]);
202 kfree(dvbdev->tsout_entity[i].name);
204 kfree(dvbdev->tsout_entity);
205 kfree(dvbdev->tsout_pads);
206 dvbdev->tsout_entity = NULL;
207 dvbdev->tsout_pads = NULL;
209 dvbdev->tsout_num_entities = 0;
212 if (dvbdev->intf_devnode) {
213 media_devnode_remove(dvbdev->intf_devnode);
214 dvbdev->intf_devnode = NULL;
217 if (dvbdev->adapter->conn) {
218 media_device_unregister_entity(dvbdev->adapter->conn);
219 kfree(dvbdev->adapter->conn);
220 dvbdev->adapter->conn = NULL;
221 kfree(dvbdev->adapter->conn_pads);
222 dvbdev->adapter->conn_pads = NULL;
227 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
228 static int dvb_create_tsout_entity(struct dvb_device *dvbdev,
229 const char *name, int npads)
233 dvbdev->tsout_pads = kcalloc(npads, sizeof(*dvbdev->tsout_pads),
235 if (!dvbdev->tsout_pads)
238 dvbdev->tsout_entity = kcalloc(npads, sizeof(*dvbdev->tsout_entity),
240 if (!dvbdev->tsout_entity)
243 dvbdev->tsout_num_entities = npads;
245 for (i = 0; i < npads; i++) {
246 struct media_pad *pads = &dvbdev->tsout_pads[i];
247 struct media_entity *entity = &dvbdev->tsout_entity[i];
249 entity->name = kasprintf(GFP_KERNEL, "%s #%d", name, i);
253 entity->function = MEDIA_ENT_F_IO_DTV;
254 pads->flags = MEDIA_PAD_FL_SINK;
256 ret = media_entity_pads_init(entity, 1, pads);
260 ret = media_device_register_entity(dvbdev->adapter->mdev,
268 #define DEMUX_TSOUT "demux-tsout"
269 #define DVR_TSOUT "dvr-tsout"
271 static int dvb_create_media_entity(struct dvb_device *dvbdev,
272 int type, int demux_sink_pads)
277 case DVB_DEVICE_FRONTEND:
281 ret = dvb_create_tsout_entity(dvbdev, DVR_TSOUT,
284 case DVB_DEVICE_DEMUX:
285 npads = 1 + demux_sink_pads;
286 ret = dvb_create_tsout_entity(dvbdev, DEMUX_TSOUT,
296 * We should be creating entities for the MPE/ULE
297 * decapsulation hardware (or software implementation).
299 * However, the number of for the MPE/ULE decaps may not be
300 * fixed. As we don't have yet dynamic support for PADs at
301 * the Media Controller, let's not create the decap
309 dvbdev->entity = kzalloc(sizeof(*dvbdev->entity), GFP_KERNEL);
313 dvbdev->entity->name = dvbdev->name;
316 dvbdev->pads = kcalloc(npads, sizeof(*dvbdev->pads),
319 kfree(dvbdev->entity);
325 case DVB_DEVICE_FRONTEND:
326 dvbdev->entity->function = MEDIA_ENT_F_DTV_DEMOD;
327 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
328 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
330 case DVB_DEVICE_DEMUX:
331 dvbdev->entity->function = MEDIA_ENT_F_TS_DEMUX;
332 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
333 for (i = 1; i < npads; i++)
334 dvbdev->pads[i].flags = MEDIA_PAD_FL_SOURCE;
337 dvbdev->entity->function = MEDIA_ENT_F_DTV_CA;
338 dvbdev->pads[0].flags = MEDIA_PAD_FL_SINK;
339 dvbdev->pads[1].flags = MEDIA_PAD_FL_SOURCE;
342 /* Should never happen, as the first switch prevents it */
343 kfree(dvbdev->entity);
345 dvbdev->entity = NULL;
351 ret = media_entity_pads_init(dvbdev->entity, npads, dvbdev->pads);
355 ret = media_device_register_entity(dvbdev->adapter->mdev,
360 printk(KERN_DEBUG "%s: media entity '%s' registered.\n",
361 __func__, dvbdev->entity->name);
367 static int dvb_register_media_device(struct dvb_device *dvbdev,
369 unsigned demux_sink_pads)
371 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
372 struct media_link *link;
376 if (!dvbdev->adapter->mdev)
379 ret = dvb_create_media_entity(dvbdev, type, demux_sink_pads);
384 case DVB_DEVICE_FRONTEND:
385 intf_type = MEDIA_INTF_T_DVB_FE;
387 case DVB_DEVICE_DEMUX:
388 intf_type = MEDIA_INTF_T_DVB_DEMUX;
391 intf_type = MEDIA_INTF_T_DVB_DVR;
394 intf_type = MEDIA_INTF_T_DVB_CA;
397 intf_type = MEDIA_INTF_T_DVB_NET;
403 dvbdev->intf_devnode = media_devnode_create(dvbdev->adapter->mdev,
407 if (!dvbdev->intf_devnode)
411 * Create the "obvious" link, e. g. the ones that represent
412 * a direct association between an interface and an entity.
413 * Other links should be created elsewhere, like:
414 * DVB FE intf -> tuner
415 * DVB demux intf -> dvr
421 link = media_create_intf_link(dvbdev->entity, &dvbdev->intf_devnode->intf,
422 MEDIA_LNK_FL_ENABLED);
429 int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
430 const struct dvb_device *template, void *priv, int type,
433 struct dvb_device *dvbdev;
434 struct file_operations *dvbdevfops;
435 struct device *clsdev;
439 mutex_lock(&dvbdev_register_lock);
441 if ((id = dvbdev_get_free_id (adap, type)) < 0){
442 mutex_unlock(&dvbdev_register_lock);
444 printk(KERN_ERR "%s: couldn't find free device id\n", __func__);
448 *pdvbdev = dvbdev = kzalloc(sizeof(*dvbdev), GFP_KERNEL);
451 mutex_unlock(&dvbdev_register_lock);
455 dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
459 mutex_unlock(&dvbdev_register_lock);
463 memcpy(dvbdev, template, sizeof(struct dvb_device));
466 dvbdev->adapter = adap;
468 dvbdev->fops = dvbdevfops;
469 init_waitqueue_head (&dvbdev->wait_queue);
471 memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));
472 dvbdevfops->owner = adap->module;
474 list_add_tail (&dvbdev->list_head, &adap->device_list);
476 down_write(&minor_rwsem);
477 #ifdef CONFIG_DVB_DYNAMIC_MINORS
478 for (minor = 0; minor < MAX_DVB_MINORS; minor++)
479 if (dvb_minors[minor] == NULL)
482 if (minor == MAX_DVB_MINORS) {
485 up_write(&minor_rwsem);
486 mutex_unlock(&dvbdev_register_lock);
490 minor = nums2minor(adap->num, type, id);
493 dvbdev->minor = minor;
494 dvb_minors[minor] = dvbdev;
495 up_write(&minor_rwsem);
497 ret = dvb_register_media_device(dvbdev, type, minor, demux_sink_pads);
500 "%s: dvb_register_media_device failed to create the mediagraph\n",
503 dvb_media_device_free(dvbdev);
506 up_write(&minor_rwsem);
507 mutex_unlock(&dvbdev_register_lock);
511 mutex_unlock(&dvbdev_register_lock);
513 clsdev = device_create(dvb_class, adap->device,
514 MKDEV(DVB_MAJOR, minor),
515 dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);
516 if (IS_ERR(clsdev)) {
517 printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
518 __func__, adap->num, dnames[type], id, PTR_ERR(clsdev));
519 return PTR_ERR(clsdev);
521 dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
522 adap->num, dnames[type], id, minor, minor);
526 EXPORT_SYMBOL(dvb_register_device);
529 void dvb_unregister_device(struct dvb_device *dvbdev)
534 down_write(&minor_rwsem);
535 dvb_minors[dvbdev->minor] = NULL;
536 up_write(&minor_rwsem);
538 dvb_media_device_free(dvbdev);
540 device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));
542 list_del (&dvbdev->list_head);
543 kfree (dvbdev->fops);
546 EXPORT_SYMBOL(dvb_unregister_device);
549 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
551 static int dvb_create_io_intf_links(struct dvb_adapter *adap,
552 struct media_interface *intf,
555 struct media_device *mdev = adap->mdev;
556 struct media_entity *entity;
557 struct media_link *link;
559 media_device_for_each_entity(entity, mdev) {
560 if (entity->function == MEDIA_ENT_F_IO_DTV) {
561 if (strncmp(entity->name, name, strlen(name)))
563 link = media_create_intf_link(entity, intf,
564 MEDIA_LNK_FL_ENABLED);
572 int dvb_create_media_graph(struct dvb_adapter *adap,
573 bool create_rf_connector)
575 struct media_device *mdev = adap->mdev;
576 struct media_entity *entity, *tuner = NULL, *demod = NULL, *conn;
577 struct media_entity *demux = NULL, *ca = NULL;
578 struct media_link *link;
579 struct media_interface *intf;
580 unsigned demux_pad = 0;
581 unsigned dvr_pad = 0;
582 unsigned ntuner = 0, ndemod = 0;
584 static const char *connector_name = "Television";
589 media_device_for_each_entity(entity, mdev) {
590 switch (entity->function) {
591 case MEDIA_ENT_F_TUNER:
595 case MEDIA_ENT_F_DTV_DEMOD:
599 case MEDIA_ENT_F_TS_DEMUX:
602 case MEDIA_ENT_F_DTV_CA:
609 * Prepare to signalize to media_create_pad_links() that multiple
610 * entities of the same type exists and a 1:n or n:1 links need to be
612 * NOTE: if both tuner and demod have multiple instances, it is up
613 * to the caller driver to create such links.
620 if (create_rf_connector) {
621 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
626 adap->conn_pads = kzalloc(sizeof(*adap->conn_pads), GFP_KERNEL);
627 if (!adap->conn_pads)
630 conn->flags = MEDIA_ENT_FL_CONNECTOR;
631 conn->function = MEDIA_ENT_F_CONN_RF;
632 conn->name = connector_name;
633 adap->conn_pads->flags = MEDIA_PAD_FL_SOURCE;
635 ret = media_entity_pads_init(conn, 1, adap->conn_pads);
639 ret = media_device_register_entity(mdev, conn);
644 ret = media_create_pad_links(mdev,
647 MEDIA_ENT_F_DTV_DEMOD,
649 MEDIA_LNK_FL_ENABLED,
652 ret = media_create_pad_links(mdev,
656 tuner, TUNER_PAD_RF_INPUT,
657 MEDIA_LNK_FL_ENABLED,
663 if (ntuner && ndemod) {
664 ret = media_create_pad_links(mdev,
666 tuner, TUNER_PAD_OUTPUT,
667 MEDIA_ENT_F_DTV_DEMOD,
668 demod, 0, MEDIA_LNK_FL_ENABLED,
674 if (ndemod && demux) {
675 ret = media_create_pad_links(mdev,
676 MEDIA_ENT_F_DTV_DEMOD,
678 MEDIA_ENT_F_TS_DEMUX,
679 demux, 0, MEDIA_LNK_FL_ENABLED,
685 ret = media_create_pad_link(demux, 1, ca,
686 0, MEDIA_LNK_FL_ENABLED);
691 /* Create demux links for each ringbuffer/pad */
693 media_device_for_each_entity(entity, mdev) {
694 if (entity->function == MEDIA_ENT_F_IO_DTV) {
695 if (!strncmp(entity->name, DVR_TSOUT,
696 strlen(DVR_TSOUT))) {
697 ret = media_create_pad_link(demux,
703 if (!strncmp(entity->name, DEMUX_TSOUT,
704 strlen(DEMUX_TSOUT))) {
705 ret = media_create_pad_link(demux,
715 /* Create interface links for FE->tuner, DVR->demux and CA->ca */
716 media_device_for_each_intf(intf, mdev) {
717 if (intf->type == MEDIA_INTF_T_DVB_CA && ca) {
718 link = media_create_intf_link(ca, intf,
719 MEDIA_LNK_FL_ENABLED);
724 if (intf->type == MEDIA_INTF_T_DVB_FE && tuner) {
725 link = media_create_intf_link(tuner, intf,
726 MEDIA_LNK_FL_ENABLED);
732 * Indirect link - let's not create yet, as we don't know how
733 * to handle indirect links, nor if this will
734 * actually be needed.
736 if (intf->type == MEDIA_INTF_T_DVB_DVR && demux) {
737 link = media_create_intf_link(demux, intf,
738 MEDIA_LNK_FL_ENABLED);
743 if (intf->type == MEDIA_INTF_T_DVB_DVR) {
744 ret = dvb_create_io_intf_links(adap, intf, DVR_TSOUT);
748 if (intf->type == MEDIA_INTF_T_DVB_DEMUX) {
749 ret = dvb_create_io_intf_links(adap, intf, DEMUX_TSOUT);
756 EXPORT_SYMBOL_GPL(dvb_create_media_graph);
759 static int dvbdev_check_free_adapter_num(int num)
761 struct list_head *entry;
762 list_for_each(entry, &dvb_adapter_list) {
763 struct dvb_adapter *adap;
764 adap = list_entry(entry, struct dvb_adapter, list_head);
765 if (adap->num == num)
771 static int dvbdev_get_free_adapter_num (void)
775 while (num < DVB_MAX_ADAPTERS) {
776 if (dvbdev_check_free_adapter_num(num))
785 int dvb_register_adapter(struct dvb_adapter *adap, const char *name,
786 struct module *module, struct device *device,
791 mutex_lock(&dvbdev_register_lock);
793 for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {
794 num = adapter_nums[i];
795 if (num >= 0 && num < DVB_MAX_ADAPTERS) {
796 /* use the one the driver asked for */
797 if (dvbdev_check_free_adapter_num(num))
800 num = dvbdev_get_free_adapter_num();
807 mutex_unlock(&dvbdev_register_lock);
811 memset (adap, 0, sizeof(struct dvb_adapter));
812 INIT_LIST_HEAD (&adap->device_list);
814 printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
818 adap->module = module;
819 adap->device = device;
820 adap->mfe_shared = 0;
821 adap->mfe_dvbdev = NULL;
822 mutex_init (&adap->mfe_lock);
824 list_add_tail (&adap->list_head, &dvb_adapter_list);
826 mutex_unlock(&dvbdev_register_lock);
830 EXPORT_SYMBOL(dvb_register_adapter);
833 int dvb_unregister_adapter(struct dvb_adapter *adap)
835 mutex_lock(&dvbdev_register_lock);
836 list_del (&adap->list_head);
837 mutex_unlock(&dvbdev_register_lock);
840 EXPORT_SYMBOL(dvb_unregister_adapter);
842 /* if the miracle happens and "generic_usercopy()" is included into
843 the kernel, then this can vanish. please don't make the mistake and
844 define this as video_usercopy(). this will introduce a dependecy
845 to the v4l "videodev.o" module, which is unnecessary for some
846 cards (ie. the budget dvb-cards don't need the v4l module...) */
847 int dvb_usercopy(struct file *file,
848 unsigned int cmd, unsigned long arg,
849 int (*func)(struct file *file,
850 unsigned int cmd, void *arg))
857 /* Copy arguments into temp kernel buffer */
858 switch (_IOC_DIR(cmd)) {
861 * For this command, the pointer is actually an integer
866 case _IOC_READ: /* some v4l ioctls are marked wrong ... */
868 case (_IOC_WRITE | _IOC_READ):
869 if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
872 /* too big to allocate from stack */
873 mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
880 if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
886 if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
892 /* Copy results into user buffer */
893 switch (_IOC_DIR(cmd))
896 case (_IOC_WRITE | _IOC_READ):
897 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
907 static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)
909 struct dvb_device *dvbdev = dev_get_drvdata(dev);
911 add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);
912 add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);
913 add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);
917 static char *dvb_devnode(struct device *dev, umode_t *mode)
919 struct dvb_device *dvbdev = dev_get_drvdata(dev);
921 return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",
922 dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);
926 static int __init init_dvbdev(void)
929 dev_t dev = MKDEV(DVB_MAJOR, 0);
931 if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
932 printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
936 cdev_init(&dvb_device_cdev, &dvb_device_fops);
937 if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
938 printk(KERN_ERR "dvb-core: unable register character device\n");
942 dvb_class = class_create(THIS_MODULE, "dvb");
943 if (IS_ERR(dvb_class)) {
944 retval = PTR_ERR(dvb_class);
947 dvb_class->dev_uevent = dvb_uevent;
948 dvb_class->devnode = dvb_devnode;
952 cdev_del(&dvb_device_cdev);
953 unregister_chrdev_region(dev, MAX_DVB_MINORS);
958 static void __exit exit_dvbdev(void)
960 class_destroy(dvb_class);
961 cdev_del(&dvb_device_cdev);
962 unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
965 subsys_initcall(init_dvbdev);
966 module_exit(exit_dvbdev);
968 MODULE_DESCRIPTION("DVB Core Driver");
969 MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
970 MODULE_LICENSE("GPL");