📄 videodev.c
字号:
/* * Video capture interface for Linux * * A generic video device interface for the LINUX operating system * using a set of device structures/vectors for low level operations. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Author: Alan Cox, <alan@redhat.com> * * Fixes: 20000516 Claudio Matsuoka <claudio@conectiva.com> * - Added procfs support */#include <linux/config.h>#include <linux/version.h>#include <linux/module.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/smp_lock.h>#include <linux/mm.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/kmod.h>#include <linux/slab.h>#include <asm/uaccess.h>#include <asm/system.h>#include <asm/semaphore.h>#include <linux/videodev.h>#define VIDEO_NUM_DEVICES 256 /* * Active devices */ static struct video_device *video_device[VIDEO_NUM_DEVICES];static DECLARE_MUTEX(videodev_lock);#if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)#include <linux/proc_fs.h>struct videodev_proc_data { struct list_head proc_list; char name[16]; struct video_device *vdev; struct proc_dir_entry *proc_entry;};static struct proc_dir_entry *video_dev_proc_entry = NULL;struct proc_dir_entry *video_proc_entry = NULL;EXPORT_SYMBOL(video_proc_entry);LIST_HEAD(videodev_proc_list);#endif /* CONFIG_PROC_FS && CONFIG_VIDEO_PROC_FS */struct video_device *video_device_alloc(void){ struct video_device *vfd; vfd = kmalloc(sizeof(*vfd),GFP_KERNEL); if (NULL == vfd) return NULL; memset(vfd,0,sizeof(*vfd)); return vfd;}void video_device_release(struct video_device *vfd){ kfree(vfd);}/* * Read will do some smarts later on. Buffer pin etc. */ static ssize_t video_read(struct file *file, char *buf, size_t count, loff_t *ppos){ struct video_device *vfl = video_devdata(file); if(vfl->read) return vfl->read(vfl, buf, count, file->f_flags&O_NONBLOCK); else return -EINVAL;}/* * Write for now does nothing. No reason it shouldnt do overlay setting * for some boards I guess.. */static ssize_t video_write(struct file *file, const char *buf, size_t count, loff_t *ppos){ struct video_device *vfl = video_devdata(file); if(vfl->write) return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK); else return 0;}struct video_device* video_devdata(struct file *file){ return video_device[minor(file->f_dentry->d_inode->i_rdev)];}/* * Poll to see if we're readable, can probably be used for timing on incoming * frames, etc.. */static unsigned int video_poll(struct file *file, poll_table * wait){ struct video_device *vfl = video_devdata(file); if(vfl->poll) return vfl->poll(vfl, file, wait); else return 0;}/* * Open a video device. */static int video_open(struct inode *inode, struct file *file){ unsigned int minor = minor(inode->i_rdev); int err = 0; struct video_device *vfl; if(minor>=VIDEO_NUM_DEVICES) return -ENODEV; down(&videodev_lock); vfl=video_device[minor]; if(vfl==NULL) { char modname[20]; up(&videodev_lock); sprintf (modname, "char-major-%d-%d", VIDEO_MAJOR, minor); request_module(modname); down(&videodev_lock); vfl=video_device[minor]; if (vfl==NULL) { err = -ENODEV; goto unlock_out; } } if (vfl->fops) { struct file_operations *old_fops; old_fops = file->f_op; file->f_op = fops_get(vfl->fops); if(file->f_op->open) err = file->f_op->open(inode,file); if (err) { fops_put(file->f_op); file->f_op = fops_get(old_fops); } fops_put(old_fops); goto unlock_out; } if(vfl->users) { err = -EBUSY; goto unlock_out; } vfl->users++; /* In case vfl->open sleeps */ if(vfl->owner) __MOD_INC_USE_COUNT(vfl->owner); if (vfl->open) { err=vfl->open(vfl,0); /* Tell the device it is open */ if (err) { vfl->users--; if(vfl->owner) __MOD_DEC_USE_COUNT(vfl->owner); goto unlock_out; } } err = 0; unlock_out: up(&videodev_lock); return err;}/* * Last close of a video for Linux device */ static int video_release(struct inode *inode, struct file *file){ struct video_device *vfl; struct module *owner; vfl = video_devdata(file); owner = vfl->owner; if (vfl->close) vfl->close(vfl); down(&videodev_lock); /* ->close() might have called video_device_unregister() in case of a hot unplug, thus we have to get vfl again */ vfl = video_devdata(file); if (NULL != vfl) vfl->users--; if (owner) __MOD_DEC_USE_COUNT(owner); up(&videodev_lock); return 0;}static int video_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct video_device *vfl = video_devdata(file); int err=vfl->ioctl(vfl, cmd, (void *)arg); if(err!=-ENOIOCTLCMD) return err; switch(cmd) { default: return -EINVAL; }}/* * We need to do MMAP support */ int video_mmap(struct file *file, struct vm_area_struct *vma){ int ret = -EINVAL; struct video_device *vfl = video_devdata(file); if(vfl->mmap) { lock_kernel(); ret = vfl->mmap(vfl, (char *)vma->vm_start, (unsigned long)(vma->vm_end-vma->vm_start)); unlock_kernel(); } return ret;}/* * helper function -- handles userspace copying for ioctl arguments */intvideo_usercopy(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg, int (*func)(struct inode *inode, struct file *file, unsigned int cmd, void *arg)){ char sbuf[128]; void *mbuf = NULL; void *parg = NULL; int err = -EINVAL; /* Copy arguments into temp kernel buffer */ switch (_IOC_DIR(cmd)) { case _IOC_NONE: parg = (void *)arg; break; case _IOC_READ: /* some v4l ioctls are marked wrong ... */ case _IOC_WRITE: case (_IOC_WRITE | _IOC_READ): if (_IOC_SIZE(cmd) <= sizeof(sbuf)) { parg = sbuf; } else { /* too big to allocate from stack */ mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL); if (NULL == mbuf) return -ENOMEM; parg = mbuf; } err = -EFAULT; if (copy_from_user(parg, (void *)arg, _IOC_SIZE(cmd))) goto out; break; } /* call driver */ err = func(inode, file, cmd, parg); if (err == -ENOIOCTLCMD) err = -EINVAL; if (err < 0) goto out; /* Copy results into user buffer */ switch (_IOC_DIR(cmd)) { case _IOC_READ: case (_IOC_WRITE | _IOC_READ): if (copy_to_user((void *)arg, parg, _IOC_SIZE(cmd))) err = -EFAULT; break; }out: if (mbuf) kfree(mbuf); return err;}/* * open/release helper functions -- handle exclusive opens */extern int video_exclusive_open(struct inode *inode, struct file *file){ struct video_device *vfl = video_devdata(file); int retval = 0; down(&vfl->lock); if (vfl->users) { retval = -EBUSY; } else { vfl->users++; } up(&vfl->lock); return retval;}extern int video_exclusive_release(struct inode *inode, struct file *file){ struct video_device *vfl = video_devdata(file); vfl->users--; return 0;}/* * /proc support */#if defined(CONFIG_PROC_FS) && defined(CONFIG_VIDEO_PROC_FS)/* Hmm... i'd like to see video_capability information here, but * how can I access it (without changing the other drivers? -claudio */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -