Path: blob/master/drivers/media/dvb/dvb-core/dvbdev.c
15112 views
/*1* dvbdev.c2*3* Copyright (C) 2000 Ralph Metzler <[email protected]>4* & Marcus Metzler <[email protected]>5* for convergence integrated media GmbH6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU Lesser General Public License9* as published by the Free Software Foundation; either version 2.110* of the License, or (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU Lesser General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.20*21*/2223#include <linux/types.h>24#include <linux/errno.h>25#include <linux/string.h>26#include <linux/module.h>27#include <linux/kernel.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 "dvbdev.h"3536static DEFINE_MUTEX(dvbdev_mutex);37static int dvbdev_debug;3839module_param(dvbdev_debug, int, 0644);40MODULE_PARM_DESC(dvbdev_debug, "Turn on/off device debugging (default:off).");4142#define dprintk if (dvbdev_debug) printk4344static LIST_HEAD(dvb_adapter_list);45static DEFINE_MUTEX(dvbdev_register_lock);4647static const char * const dnames[] = {48"video", "audio", "sec", "frontend", "demux", "dvr", "ca",49"net", "osd"50};5152#ifdef CONFIG_DVB_DYNAMIC_MINORS53#define MAX_DVB_MINORS 25654#define DVB_MAX_IDS MAX_DVB_MINORS55#else56#define DVB_MAX_IDS 457#define nums2minor(num,type,id) ((num << 6) | (id << 4) | type)58#define MAX_DVB_MINORS (DVB_MAX_ADAPTERS*64)59#endif6061static struct class *dvb_class;6263static struct dvb_device *dvb_minors[MAX_DVB_MINORS];64static DECLARE_RWSEM(minor_rwsem);6566static int dvb_device_open(struct inode *inode, struct file *file)67{68struct dvb_device *dvbdev;6970mutex_lock(&dvbdev_mutex);71down_read(&minor_rwsem);72dvbdev = dvb_minors[iminor(inode)];7374if (dvbdev && dvbdev->fops) {75int err = 0;76const struct file_operations *old_fops;7778file->private_data = dvbdev;79old_fops = file->f_op;80file->f_op = fops_get(dvbdev->fops);81if (file->f_op == NULL) {82file->f_op = old_fops;83goto fail;84}85if(file->f_op->open)86err = file->f_op->open(inode,file);87if (err) {88fops_put(file->f_op);89file->f_op = fops_get(old_fops);90}91fops_put(old_fops);92up_read(&minor_rwsem);93mutex_unlock(&dvbdev_mutex);94return err;95}96fail:97up_read(&minor_rwsem);98mutex_unlock(&dvbdev_mutex);99return -ENODEV;100}101102103static const struct file_operations dvb_device_fops =104{105.owner = THIS_MODULE,106.open = dvb_device_open,107.llseek = noop_llseek,108};109110static struct cdev dvb_device_cdev;111112int dvb_generic_open(struct inode *inode, struct file *file)113{114struct dvb_device *dvbdev = file->private_data;115116if (!dvbdev)117return -ENODEV;118119if (!dvbdev->users)120return -EBUSY;121122if ((file->f_flags & O_ACCMODE) == O_RDONLY) {123if (!dvbdev->readers)124return -EBUSY;125dvbdev->readers--;126} else {127if (!dvbdev->writers)128return -EBUSY;129dvbdev->writers--;130}131132dvbdev->users--;133return 0;134}135EXPORT_SYMBOL(dvb_generic_open);136137138int dvb_generic_release(struct inode *inode, struct file *file)139{140struct dvb_device *dvbdev = file->private_data;141142if (!dvbdev)143return -ENODEV;144145if ((file->f_flags & O_ACCMODE) == O_RDONLY) {146dvbdev->readers++;147} else {148dvbdev->writers++;149}150151dvbdev->users++;152return 0;153}154EXPORT_SYMBOL(dvb_generic_release);155156157long dvb_generic_ioctl(struct file *file,158unsigned int cmd, unsigned long arg)159{160struct dvb_device *dvbdev = file->private_data;161162if (!dvbdev)163return -ENODEV;164165if (!dvbdev->kernel_ioctl)166return -EINVAL;167168return dvb_usercopy(file, cmd, arg, dvbdev->kernel_ioctl);169}170EXPORT_SYMBOL(dvb_generic_ioctl);171172173static int dvbdev_get_free_id (struct dvb_adapter *adap, int type)174{175u32 id = 0;176177while (id < DVB_MAX_IDS) {178struct dvb_device *dev;179list_for_each_entry(dev, &adap->device_list, list_head)180if (dev->type == type && dev->id == id)181goto skip;182return id;183skip:184id++;185}186return -ENFILE;187}188189190int dvb_register_device(struct dvb_adapter *adap, struct dvb_device **pdvbdev,191const struct dvb_device *template, void *priv, int type)192{193struct dvb_device *dvbdev;194struct file_operations *dvbdevfops;195struct device *clsdev;196int minor;197int id;198199mutex_lock(&dvbdev_register_lock);200201if ((id = dvbdev_get_free_id (adap, type)) < 0){202mutex_unlock(&dvbdev_register_lock);203*pdvbdev = NULL;204printk(KERN_ERR "%s: couldn't find free device id\n", __func__);205return -ENFILE;206}207208*pdvbdev = dvbdev = kmalloc(sizeof(struct dvb_device), GFP_KERNEL);209210if (!dvbdev){211mutex_unlock(&dvbdev_register_lock);212return -ENOMEM;213}214215dvbdevfops = kzalloc(sizeof(struct file_operations), GFP_KERNEL);216217if (!dvbdevfops){218kfree (dvbdev);219mutex_unlock(&dvbdev_register_lock);220return -ENOMEM;221}222223memcpy(dvbdev, template, sizeof(struct dvb_device));224dvbdev->type = type;225dvbdev->id = id;226dvbdev->adapter = adap;227dvbdev->priv = priv;228dvbdev->fops = dvbdevfops;229init_waitqueue_head (&dvbdev->wait_queue);230231memcpy(dvbdevfops, template->fops, sizeof(struct file_operations));232dvbdevfops->owner = adap->module;233234list_add_tail (&dvbdev->list_head, &adap->device_list);235236down_write(&minor_rwsem);237#ifdef CONFIG_DVB_DYNAMIC_MINORS238for (minor = 0; minor < MAX_DVB_MINORS; minor++)239if (dvb_minors[minor] == NULL)240break;241242if (minor == MAX_DVB_MINORS) {243kfree(dvbdevfops);244kfree(dvbdev);245mutex_unlock(&dvbdev_register_lock);246return -EINVAL;247}248#else249minor = nums2minor(adap->num, type, id);250#endif251252dvbdev->minor = minor;253dvb_minors[minor] = dvbdev;254up_write(&minor_rwsem);255256mutex_unlock(&dvbdev_register_lock);257258clsdev = device_create(dvb_class, adap->device,259MKDEV(DVB_MAJOR, minor),260dvbdev, "dvb%d.%s%d", adap->num, dnames[type], id);261if (IS_ERR(clsdev)) {262printk(KERN_ERR "%s: failed to create device dvb%d.%s%d (%ld)\n",263__func__, adap->num, dnames[type], id, PTR_ERR(clsdev));264return PTR_ERR(clsdev);265}266267dprintk(KERN_DEBUG "DVB: register adapter%d/%s%d @ minor: %i (0x%02x)\n",268adap->num, dnames[type], id, minor, minor);269270return 0;271}272EXPORT_SYMBOL(dvb_register_device);273274275void dvb_unregister_device(struct dvb_device *dvbdev)276{277if (!dvbdev)278return;279280down_write(&minor_rwsem);281dvb_minors[dvbdev->minor] = NULL;282up_write(&minor_rwsem);283284device_destroy(dvb_class, MKDEV(DVB_MAJOR, dvbdev->minor));285286list_del (&dvbdev->list_head);287kfree (dvbdev->fops);288kfree (dvbdev);289}290EXPORT_SYMBOL(dvb_unregister_device);291292static int dvbdev_check_free_adapter_num(int num)293{294struct list_head *entry;295list_for_each(entry, &dvb_adapter_list) {296struct dvb_adapter *adap;297adap = list_entry(entry, struct dvb_adapter, list_head);298if (adap->num == num)299return 0;300}301return 1;302}303304static int dvbdev_get_free_adapter_num (void)305{306int num = 0;307308while (num < DVB_MAX_ADAPTERS) {309if (dvbdev_check_free_adapter_num(num))310return num;311num++;312}313314return -ENFILE;315}316317318int dvb_register_adapter(struct dvb_adapter *adap, const char *name,319struct module *module, struct device *device,320short *adapter_nums)321{322int i, num;323324mutex_lock(&dvbdev_register_lock);325326for (i = 0; i < DVB_MAX_ADAPTERS; ++i) {327num = adapter_nums[i];328if (num >= 0 && num < DVB_MAX_ADAPTERS) {329/* use the one the driver asked for */330if (dvbdev_check_free_adapter_num(num))331break;332} else {333num = dvbdev_get_free_adapter_num();334break;335}336num = -1;337}338339if (num < 0) {340mutex_unlock(&dvbdev_register_lock);341return -ENFILE;342}343344memset (adap, 0, sizeof(struct dvb_adapter));345INIT_LIST_HEAD (&adap->device_list);346347printk(KERN_INFO "DVB: registering new adapter (%s)\n", name);348349adap->num = num;350adap->name = name;351adap->module = module;352adap->device = device;353adap->mfe_shared = 0;354adap->mfe_dvbdev = NULL;355mutex_init (&adap->mfe_lock);356357list_add_tail (&adap->list_head, &dvb_adapter_list);358359mutex_unlock(&dvbdev_register_lock);360361return num;362}363EXPORT_SYMBOL(dvb_register_adapter);364365366int dvb_unregister_adapter(struct dvb_adapter *adap)367{368mutex_lock(&dvbdev_register_lock);369list_del (&adap->list_head);370mutex_unlock(&dvbdev_register_lock);371return 0;372}373EXPORT_SYMBOL(dvb_unregister_adapter);374375/* if the miracle happens and "generic_usercopy()" is included into376the kernel, then this can vanish. please don't make the mistake and377define this as video_usercopy(). this will introduce a dependecy378to the v4l "videodev.o" module, which is unnecessary for some379cards (ie. the budget dvb-cards don't need the v4l module...) */380int dvb_usercopy(struct file *file,381unsigned int cmd, unsigned long arg,382int (*func)(struct file *file,383unsigned int cmd, void *arg))384{385char sbuf[128];386void *mbuf = NULL;387void *parg = NULL;388int err = -EINVAL;389390/* Copy arguments into temp kernel buffer */391switch (_IOC_DIR(cmd)) {392case _IOC_NONE:393/*394* For this command, the pointer is actually an integer395* argument.396*/397parg = (void *) arg;398break;399case _IOC_READ: /* some v4l ioctls are marked wrong ... */400case _IOC_WRITE:401case (_IOC_WRITE | _IOC_READ):402if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {403parg = sbuf;404} else {405/* too big to allocate from stack */406mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);407if (NULL == mbuf)408return -ENOMEM;409parg = mbuf;410}411412err = -EFAULT;413if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))414goto out;415break;416}417418/* call driver */419mutex_lock(&dvbdev_mutex);420if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)421err = -EINVAL;422mutex_unlock(&dvbdev_mutex);423424if (err < 0)425goto out;426427/* Copy results into user buffer */428switch (_IOC_DIR(cmd))429{430case _IOC_READ:431case (_IOC_WRITE | _IOC_READ):432if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))433err = -EFAULT;434break;435}436437out:438kfree(mbuf);439return err;440}441442static int dvb_uevent(struct device *dev, struct kobj_uevent_env *env)443{444struct dvb_device *dvbdev = dev_get_drvdata(dev);445446add_uevent_var(env, "DVB_ADAPTER_NUM=%d", dvbdev->adapter->num);447add_uevent_var(env, "DVB_DEVICE_TYPE=%s", dnames[dvbdev->type]);448add_uevent_var(env, "DVB_DEVICE_NUM=%d", dvbdev->id);449return 0;450}451452static char *dvb_devnode(struct device *dev, mode_t *mode)453{454struct dvb_device *dvbdev = dev_get_drvdata(dev);455456return kasprintf(GFP_KERNEL, "dvb/adapter%d/%s%d",457dvbdev->adapter->num, dnames[dvbdev->type], dvbdev->id);458}459460461static int __init init_dvbdev(void)462{463int retval;464dev_t dev = MKDEV(DVB_MAJOR, 0);465466if ((retval = register_chrdev_region(dev, MAX_DVB_MINORS, "DVB")) != 0) {467printk(KERN_ERR "dvb-core: unable to get major %d\n", DVB_MAJOR);468return retval;469}470471cdev_init(&dvb_device_cdev, &dvb_device_fops);472if ((retval = cdev_add(&dvb_device_cdev, dev, MAX_DVB_MINORS)) != 0) {473printk(KERN_ERR "dvb-core: unable register character device\n");474goto error;475}476477dvb_class = class_create(THIS_MODULE, "dvb");478if (IS_ERR(dvb_class)) {479retval = PTR_ERR(dvb_class);480goto error;481}482dvb_class->dev_uevent = dvb_uevent;483dvb_class->devnode = dvb_devnode;484return 0;485486error:487cdev_del(&dvb_device_cdev);488unregister_chrdev_region(dev, MAX_DVB_MINORS);489return retval;490}491492493static void __exit exit_dvbdev(void)494{495class_destroy(dvb_class);496cdev_del(&dvb_device_cdev);497unregister_chrdev_region(MKDEV(DVB_MAJOR, 0), MAX_DVB_MINORS);498}499500subsys_initcall(init_dvbdev);501module_exit(exit_dvbdev);502503MODULE_DESCRIPTION("DVB Core Driver");504MODULE_AUTHOR("Marcus Metzler, Ralph Metzler, Holger Waechtler");505MODULE_LICENSE("GPL");506507508