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