linux/drivers/media/dvb/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
  37static int dvbdev_debug;
  38
  39module_param(dvbdev_debug, int, 0644);
  40MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");
  41
  42#define dprintk if (dvbdev_debug) printk
  43
  44static LIST_HEAD(dvb_adapter_list);
  45static DEFINE_MUTEX(dvbdev_register_lock);
  46
  47static const char * const dnames[] = {
  48        "video", "audio", "sec", "frontend", "demux", "dvr", "ca",
  49        "net", "osd"
  50};
  51
  52#define DVB_MAX_ADAPTERS        8
  53#define DVB_MAX_IDS             4
  54#define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)
  55#define MAX_DVB_MINORS          (DVB_MAX_ADAPTERS*64)
  56
  57static struct class *dvb_class;
  58
  59static struct dvb_device* dvbdev_find_device (int minor)
  60{
  61        struct dvb_adapter *adap;
  62
  63        list_for_each_entry(adap, &dvb_adapter_list, list_head) {
  64                struct dvb_device *dev;
  65                list_for_each_entry(dev, &adap->device_list, list_head)
  66                        if (nums2minor(adap->num, dev->type, dev->id) == minor)
  67                                return dev;
  68        }
  69
  70        return NULL;
  71}
  72
  73
  74static int dvb_device_open(struct inode *inode, struct file *file)
  75{
  76        struct dvb_device *dvbdev;
  77
  78        dvbdev = dvbdev_find_device (iminor(inode));
  79
  80        if (dvbdev && dvbdev->fops) {
  81                int err = 0;
  82                const struct file_operations *old_fops;
  83
  84                file->private_data = dvbdev;
  85                old_fops = file->f_op;
  86                file->f_op = fops_get(dvbdev->fops);
  87                if(file->f_op->open)
  88                        err = file->f_op->open(inode,file);
  89                if (err) {
  90                        fops_put(file->f_op);
  91                        file->f_op = fops_get(old_fops);
  92                }
  93                fops_put(old_fops);
  94                return err;
  95        }
  96        return -ENODEV;
  97}
  98
  99
 100static struct file_operations dvb_device_fops =
 101{
 102        .owner =        THIS_MODULE,
 103        .open =         dvb_device_open,
 104};
 105
 106static struct cdev dvb_device_cdev;
 107
 108int dvb_generic_open(struct inode *inode, struct file *file)
 109{
 110        struct dvb_device *dvbdev = file->private_data;
 111
 112        if (!dvbdev)
 113                return -ENODEV;
 114
 115        if (!dvbdev->users)
 116                return -EBUSY;
 117
 118        if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
 119                if (!dvbdev->readers)
 120                        return -EBUSY;
 121                dvbdev->readers--;
 122        } else {
 123                if (!dvbdev->writers)
 124                        return -EBUSY;
 125                dvbdev->writers--;
 126        }
 127
 128        dvbdev->users--;
 129        return 0;
 130}
 131EXPORT_SYMBOL(dvb_generic_open);
 132
 133
 134int dvb_generic_release(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 ((file->f_flags & O_ACCMODE) == O_RDONLY) {
 142                dvbdev->readers++;
 143        } else {
 144                dvbdev->writers++;
 145        }
 146
 147        dvbdev->users++;
 148        return 0;
 149}
 150EXPORT_SYMBOL(dvb_generic_release);
 151
 152
 153int dvb_generic_ioctl(struct inode *inode, struct file *file,
 154                      unsigned int cmd, unsigned long arg)
 155{
 156        struct dvb_device *dvbdev = file->private_data;
 157
 158        if (!dvbdev)
 159                return -ENODEV;
 160
 161        if (!dvbdev->kernel_ioctl)
 162                return -EINVAL;
 163
 164        return dvb_usercopy (inode, file, cmd, arg, dvbdev->kernel_ioctl);
 165}
 166EXPORT_SYMBOL(dvb_generic_ioctl);
 167
 168
 169static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)
 170{
 171        u32 id = 0;
 172
 173        while (id < DVB_MAX_IDS) {
 174                struct dvb_device *dev;
 175                list_for_each_entry(dev, &adap->device_list, list_head)
 176                        if (dev->type == type && dev->id == id)
 177                                goto skip;
 178                return id;
 179skip:
 180                id++;
 181        }
 182        return -ENFILE;
 183}
 184
 185
 186int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,
 187                        const struct dvb_device *template, void *priv, int type)
 188{
 189        struct dvb_device *dvbdev;
 190        struct file_operations *dvbdevfops;
 191        struct device *clsdev;
 192        int id;
 193
 194        mutex_lock(&dvbdev_register_lock);
 195
 196        if ((id = dvbdev_get_free_id (adap, type)) < 0){
 197                mutex_unlock(&dvbdev_register_lock);
 198                *pdvbdev = NULL;
 199                printk(KERN_ERR "%s: couldn't find free device id\n", __FUNCTION__);
 200                return -ENFILE;
 201        }
 202
 203        *pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);
 204
 205        if (!dvbdev){
 206                mutex_unlock(&dvbdev_register_lock);
 207                return -ENOMEM;
 208        }
 209
 210        dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
 211
 212        if (!dvbdevfops){
 213                kfree (dvbdev);
 214                mutex_unlock(&dvbdev_register_lock);
 215                return -ENOMEM;
 216        }
 217
 218        memcpy(dvbdev, template, sizeof(struct dvb_device));
 219        dvbdev->type = type;
 220        dvbdev->id = id;
 221        dvbdev->adapter = adap;
 222        dvbdev->priv = priv;
 223        dvbdev->fops = dvbdevfops;
 224        init_waitqueue_head (&dvbdev->wait_queue);
 225
 226        memcpy(dvbdev->fops, template->fops, sizeof(struct file_operations));
 227        dvbdev->fops->owner = adap->module;
 228
 229        list_add_tail (&dvbdev->list_head, &adap->device_list);
 230
 231        mutex_unlock(&dvbdev_register_lock);
 232
 233        clsdev = device_create(dvb_class, adap->device,
 234                               MKDEV(DVB_MAJOR, nums2minor(adap->num, type, id)),
 235                               "dvb%d.%s%d", adap->num, dnames[type], id);
 236        if (IS_ERR(clsdev)) {
 237                printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",
 238                       __FUNCTION__, adap->num, dnames[type], id, PTR_ERR(clsdev));
 239                return PTR_ERR(clsdev);
 240        }
 241
 242        dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",
 243                adap->num, dnames[type], id, nums2minor(adap->num, type, id),
 244                nums2minor(adap->num, type, id));
 245
 246        return 0;
 247}
 248EXPORT_SYMBOL(dvb_register_device);
 249
 250
 251void dvb_unregister_device(struct dvb_device *dvbdev)
 252{
 253        if (!dvbdev)
 254                return;
 255
 256        device_destroy(dvb_class, MKDEV(DVB_MAJOR, nums2minor(dvbdev->adapter->num,
 257                       dvbdev->type, dvbdev->id)));
 258
 259        list_del (&dvbdev->list_head);
 260        kfree (dvbdev->fops);
 261        kfree (dvbdev);
 262}
 263EXPORT_SYMBOL(dvb_unregister_device);
 264
 265
 266static int dvbdev_get_free_adapter_num (void)
 267{
 268        int num = 0;
 269
 270        while (num < DVB_MAX_ADAPTERS) {
 271                struct dvb_adapter *adap;
 272                list_for_each_entry(adap, &dvb_adapter_list, list_head)
 273                        if (adap->num == num)
 274                                goto skip;
 275                return num;
 276skip:
 277                num++;
 278        }
 279
 280        return -ENFILE;
 281}
 282
 283
 284int dvb_register_adapter(struct dvb_adapter *adap, const char *name, struct module *module, struct device *device)
 285{
 286        int num;
 287
 288        mutex_lock(&dvbdev_register_lock);
 289
 290        if ((num = dvbdev_get_free_adapter_num ()) < 0) {
 291                mutex_unlock(&dvbdev_register_lock);
 292                return -ENFILE;
 293        }
 294
 295        memset (adap, 0, sizeof(struct dvb_adapter));
 296        INIT_LIST_HEAD (&adap->device_list);
 297
 298        printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);
 299
 300        adap->num = num;
 301        adap->name = name;
 302        adap->module = module;
 303        adap->device = device;
 304
 305        list_add_tail (&adap->list_head, &dvb_adapter_list);
 306
 307        mutex_unlock(&dvbdev_register_lock);
 308
 309        return num;
 310}
 311EXPORT_SYMBOL(dvb_register_adapter);
 312
 313
 314int dvb_unregister_adapter(struct dvb_adapter *adap)
 315{
 316        mutex_lock(&dvbdev_register_lock);
 317        list_del (&adap->list_head);
 318        mutex_unlock(&dvbdev_register_lock);
 319        return 0;
 320}
 321EXPORT_SYMBOL(dvb_unregister_adapter);
 322
 323/* if the miracle happens and "generic_usercopy()" is included into
 324   the kernel, then this can vanish. please don't make the mistake and
 325   define this as video_usercopy(). this will introduce a dependecy
 326   to the v4l "videodev.o" module, which is unnecessary for some
 327   cards (ie. the budget dvb-cards don't need the v4l module...) */
 328int dvb_usercopy(struct inode *inode, struct file *file,
 329                     unsigned int cmd, unsigned long arg,
 330                     int (*func)(struct inode *inode, struct file *file,
 331                     unsigned int cmd, void *arg))
 332{
 333        char    sbuf[128];
 334        void    *mbuf = NULL;
 335        void    *parg = NULL;
 336        int     err  = -EINVAL;
 337
 338        /*  Copy arguments into temp kernel buffer  */
 339        switch (_IOC_DIR(cmd)) {
 340        case _IOC_NONE:
 341                /*
 342                 * For this command, the pointer is actually an integer
 343                 * argument.
 344                 */
 345                parg = (void *) arg;
 346                break;
 347        case _IOC_READ: /* some v4l ioctls are marked wrong ... */
 348        case _IOC_WRITE:
 349        case (_IOC_WRITE | _IOC_READ):
 350                if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
 351                        parg = sbuf;
 352                } else {
 353                        /* too big to allocate from stack */
 354                        mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
 355                        if (NULL == mbuf)
 356                                return -ENOMEM;
 357                        parg = mbuf;
 358                }
 359
 360                err = -EFAULT;
 361                if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
 362                        goto out;
 363                break;
 364        }
 365
 366        /* call driver */
 367        if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
 368                err = -EINVAL;
 369
 370        if (err < 0)
 371                goto out;
 372
 373        /*  Copy results into user buffer  */
 374        switch (_IOC_DIR(cmd))
 375        {
 376        case _IOC_READ:
 377        case (_IOC_WRITE | _IOC_READ):
 378                if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
 379                        err = -EFAULT;
 380                break;
 381        }
 382
 383out:
 384        kfree(mbuf);
 385        return err;
 386}
 387
 388static int __init init_dvbdev(void)
 389{
 390        int retval;
 391        dev_t dev = MKDEV(DVB_MAJOR, 0);
 392
 393        if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {
 394                printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);
 395                return retval;
 396        }
 397
 398        cdev_init(&dvb_device_cdev, &dvb_device_fops);
 399        if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {
 400                printk(KERN_ERR "dvb-core: unable register character device\n");
 401                goto error;
 402        }
 403
 404        dvb_class = class_create(THIS_MODULE, "dvb");
 405        if (IS_ERR(dvb_class)) {
 406                retval = PTR_ERR(dvb_class);
 407                goto error;
 408        }
 409        return 0;
 410
 411error:
 412        cdev_del(&dvb_device_cdev);
 413        unregister_chrdev_region(dev, MAX_DVB_MINORS);
 414        return retval;
 415}
 416
 417
 418static void __exit exit_dvbdev(void)
 419{
 420        class_destroy(dvb_class);
 421        cdev_del(&dvb_device_cdev);
 422        unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);
 423}
 424
 425subsys_initcall(init_dvbdev);
 426module_exit(exit_dvbdev);
 427
 428MODULE_DESCRIPTION("DVB Core Driver");
 429MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");
 430MODULE_LICENSE("GPL");
 431
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.