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