📄 videodevx.c
字号:
/* * Video for Linux Two * * A generic video device interface for the LINUX operating system * using a set of device structures/vectors for low level operations. * * This file replaces the videodev.c file that comes with the * regular kernel distribution. * * 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: Bill Dirks <bdirks@pacbell.net> * based on code by Alan Cox, <alan@cymru.net> * *//* * 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: *//* * Video4linux 1/2 integration by Justin Schoeman * <justin@suntiger.ee.up.ac.za> * 2.4 PROCFS support ported from 2.4 kernels by * I馻ki Garc韆 Etxebarria <garetxe@euskalnet.net> * Makefile fix by "W. Michael Petullo" <mike@flyn.org> * 2.4 devfs support ported from 2.4 kernels by * Dan Merillat <dan@merillat.org> * Added Gerd Knorrs v4l1 enhancements (Justin Schoeman) */#ifndef __KERNEL__#define __KERNEL__#endif#include <linux/config.h>#ifndef EXPORT_SYMTAB#define EXPORT_SYMTAB#endif#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 <asm/div64.h>#include "videodev.h"#if LINUX_VERSION_CODE >= 0x020100#include <asm/uaccess.h>#endif#include <asm/system.h>#include <asm/pgtable.h>#include <asm/io.h>#ifdef CONFIG_KMOD#include <linux/kmod.h>#endif#include <linux/proc_fs.h>#if defined(CONFIG_UST) || defined(CONFIG_UST_MODULE)#include <linux/ust.h>#endif#ifdef MODULE_LICENSEMODULE_LICENSE("GPL");#endif#define V4L2_NUM_DEVICES 256 static int v4l2_major = 81;MODULE_PARM(v4l2_major, "i");#define VIDEO_NUM_DEVICES 256 /* * Active devices */static struct v4l2_device *v4l2_device[V4L2_NUM_DEVICES];static struct file_operations v4l2_fops; static struct video_device *video_device[VIDEO_NUM_DEVICES];static struct file_operations video_fops;#ifndef MODULE#ifdef CONFIG_VIDEO_BWQCAMextern int init_bw_qcams(struct video_init *);#endif#ifdef CONFIG_VIDEO_CPIAextern int cpia_init(struct video_init *);#endif#ifdef CONFIG_VIDEO_PLANBextern int init_planbs(struct video_init *);#endif#ifdef CONFIG_VIDEO_ZORANextern int init_zoran_cards(struct video_init *);#endif#if LINUX_VERSION_CODE < 0x020400#ifdef CONFIG_VIDEO_BT848extern int init_bttv_cards(struct video_init *);extern int i2c_tuner_init(struct video_init *);#endif#ifdef CONFIG_VIDEO_SAA5249extern int init_saa_5249(struct video_init *);#endif #ifdef CONFIG_VIDEO_CQCAMextern int init_colour_qcams(struct video_init *);#endif#ifdef CONFIG_RADIO_AZTECHextern int aztech_init(struct video_init *);#endif#ifdef CONFIG_RADIO_RTRACKextern int rtrack_init(struct video_init *);#endif#ifdef CONFIG_RADIO_RTRACK2extern int rtrack2_init(struct video_init *);#endif#ifdef CONFIG_RADIO_SF16FMIextern int fmi_init(struct video_init *);#endif#ifdef CONFIG_RADIO_MIROPCM20extern int pcm20_init(struct video_init *);#endif#ifdef CONFIG_RADIO_GEMTEKextern int gemtek_init(struct video_init *);#endif#ifdef CONFIG_RADIO_TYPHOONextern int typhoon_init(struct video_init *);#endif#ifdef CONFIG_RADIO_CADETextern int cadet_init(struct video_init *);#endif#ifdef CONFIG_RADIO_TRUSTextern int trust_init(struct video_init *);#endif#ifdef CONFIG_VIDEO_PMSextern int init_pms_cards(struct video_init *);#endif#endif /* not 2.4.x */#endif /* compiled into kernel (not module) */static struct video_init video_init_list[]={#ifndef MODULE#ifdef CONFIG_VIDEO_BWQCAM {"bw-qcam", init_bw_qcams},#endif #ifdef CONFIG_VIDEO_CPIA {"cpia", cpia_init},#endif #ifdef CONFIG_VIDEO_PLANB {"planb", init_planbs},#endif#ifdef CONFIG_VIDEO_ZORAN {"zoran", init_zoran_cards},#endif #if LINUX_VERSION_CODE < 0x020400#ifdef CONFIG_VIDEO_BT848 {"i2c-tuner", i2c_tuner_init}, {"bttv", init_bttv_cards},#endif #ifdef CONFIG_VIDEO_SAA5249 {"saa5249", init_saa_5249},#endif #ifdef CONFIG_VIDEO_CQCAM {"c-qcam", init_colour_qcams},#endif #ifdef CONFIG_VIDEO_PMS {"PMS", init_pms_cards}, #endif #ifdef CONFIG_RADIO_AZTECH {"Aztech", aztech_init}, #endif #ifdef CONFIG_RADIO_RTRACK {"RTrack", rtrack_init}, #endif #ifdef CONFIG_RADIO_RTRACK2 {"RTrack2", rtrack2_init}, #endif#ifdef CONFIG_RADIO_SF16FMI {"SF16FMI", fmi_init}, #endif #ifdef CONFIG_RADIO_MIROPCM20 {"PCM20", pcm20_init}, #endif#ifdef CONFIG_RADIO_CADET {"Cadet", cadet_init},#endif#ifdef CONFIG_RADIO_GEMTEK {"GemTek", gemtek_init},#endif#ifdef CONFIG_RADIO_TYPHOON {"radio-typhoon", typhoon_init},#endif#ifdef CONFIG_RADIO_TRUST {"Trust", trust_init}, #endif #endif /* not 2.4.x */#endif /* compiled into kernel (not module) */ {"end", NULL}};#if LINUX_VERSION_CODE >= 0x020100/* * Read will do some smarts later on. Buffer pin etc. */ inline ssize_t video_read(struct file *file, char *buf, size_t count, loff_t *ppos){ struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { if (vfl->fops->read) return vfl->fops->read(file,buf,count,ppos); else return -EINVAL; }#endif 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.. */inline ssize_t video_write(struct file *file, const char *buf, size_t count, loff_t *ppos){ struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { if (vfl->fops->write) return vfl->fops->write(file,buf,count,ppos); else return 0; }#endif if(vfl->write) return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK); else return 0;}/* * Poll to see if we're readable, can probably be used for timing on incoming * frames, etc.. */inline unsigned int video_poll(struct file *file, poll_table * wait){ struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { if (vfl->fops->poll) return vfl->fops->poll(file,wait); else return 0; }#endif if(vfl->poll) return vfl->poll(vfl, file, wait); else return 0;}#else#warning This version of videodevX does not fully support pre 2.2.x kernelsinline int video_read(struct inode *ino,struct file *file, char *buf, int count){ int err; struct video_device *vfl=video_device[MINOR(ino->i_rdev)]; if (vfl->read) return vfl->read(vfl, buf, count, file->f_flags&O_NONBLOCK); else return -EINVAL;}inline int video_write(struct inode *ino,struct file *file, const char *buf, int count){ int err; struct video_device *vfl=video_device[MINOR(ino->i_rdev)]; if (vfl->write) return vfl->write(vfl, buf, count, file->f_flags&O_NONBLOCK); else return 0;}#endif/* * Open a video device. */inline int video_open(struct inode *inode, struct file *file){ unsigned int minor = MINOR(inode->i_rdev); int err, retval = 0; struct video_device *vfl; if(minor>=VIDEO_NUM_DEVICES) return -ENODEV; lock_kernel(); vfl=video_device[minor]; if(vfl==NULL) { retval = -ENODEV; goto error_out; }#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { unlock_kernel(); if (vfl->fops->open) return vfl->fops->open(inode,file); else return 0; }#endif if(vfl->busy) { retval = -EBUSY; goto error_out; } vfl->busy=1; /* In case vfl->open sleeps */#if LINUX_VERSION_CODE >= 0x020403 if (vfl->owner) __MOD_INC_USE_COUNT(vfl->owner);#endif unlock_kernel(); if(vfl->open) { err=vfl->open(vfl,0); /* Tell the device it is open */ if(err) { vfl->busy=0;#if LINUX_VERSION_CODE >= 0x020403 if (vfl->owner) __MOD_DEC_USE_COUNT(vfl->owner);#endif return err; } } return 0;error_out: unlock_kernel(); return retval;}/* * Last close of a video for Linux device */ inline int video_release(struct inode *inode, struct file *file){ struct video_device *vfl; lock_kernel(); vfl=video_device[MINOR(inode->i_rdev)];#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { unlock_kernel(); if (vfl->fops->release) return vfl->fops->release(inode,file); else return 0; }#endif if(vfl->close) vfl->close(vfl); vfl->busy=0;#if LINUX_VERSION_CODE >= 0x020403 if (vfl->owner) __MOD_DEC_USE_COUNT(vfl->owner);#endif unlock_kernel(); return 0;}inline int video_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct video_device *vfl=video_device[MINOR(inode->i_rdev)]; int err;#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { if (vfl->fops->ioctl) return vfl->fops->ioctl(inode,file,cmd,arg); else return -EINVAL; }#endif err=vfl->ioctl(vfl, cmd, (void *)arg); if(err!=-ENOIOCTLCMD) return err; switch(cmd) { default: return -EINVAL; }}/* * We need to do MMAP support */ #if LINUX_VERSION_CODE >= 0x020100inline int video_mmap(struct file *file, struct vm_area_struct *vma){ int ret = -EINVAL; struct video_device *vfl=video_device[MINOR(file->f_dentry->d_inode->i_rdev)];#if LINUX_VERSION_CODE >= 0x020300 if (vfl->fops) { if (vfl->fops->mmap) return vfl->fops->mmap(file,vma); else return -EINVAL; }#endif /* 2.3.x and up */#else /* pre 2.1.x */inline int video_mmap(struct inode * ino, struct file * file, struct vm_area_struct * vma){ int ret = -EINVAL; struct video_device *vfl=video_device[MINOR(ino->i_rdev)];#endif 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;}#if (LINUX_VERSION_CODE >= 0x020300) && defined(CONFIG_PROC_FS) static void videodev_proc_create_dev (struct video_device *vfd, char *name); static void videodev_proc_destroy_dev (struct video_device *vfd); static void v4l2_proc_create_dev (struct v4l2_device *vfd, char *name); static void v4l2_proc_destroy_dev (struct v4l2_device *vfd);#endif /* * Video For Linux device drivers request registration here. */ #if LINUX_VERSION_CODE >= 0x020405int video_register_device(struct video_device *vfd, int type, int nr)#elseint video_register_device(struct video_device *vfd, int type)#endif{ int i=0; int base; int err; int end; char *name_base; switch(type) { case VFL_TYPE_GRABBER: base=0; end=64; name_base = "video"; break; case VFL_TYPE_VTX: base=192; end=224; name_base = "vtx"; break; case VFL_TYPE_VBI: base=224; end=240; name_base = "vbi"; break; case VFL_TYPE_RADIO: base=64; end=128; name_base = "radio"; break; default: return -1; } for(i=base;i<end;i++) { if((video_device[i]==NULL)&&(v4l2_device[i]==NULL)) {#if (defined(CONFIG_PROC_FS) && (LINUX_VERSION_CODE >= 0x020300)) \ || defined(CONFIG_DEVFS_FS) char name[16];#endif video_device[i]=vfd; vfd->minor=i; /* The init call may sleep so we book the slot out then call */ MOD_INC_USE_COUNT; if(vfd->initialize) { err=vfd->initialize(vfd); if(err<0) { video_device[i]=NULL; MOD_DEC_USE_COUNT; return err; } }#ifdef CONFIG_DEVFS_FS sprintf (name, "v4l/%s%d", name_base, i - base); /* * Start the device root only. Anything else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -