vivi.c
来自「linux 内核源代码」· C语言 代码 · 共 1,211 行 · 第 1/2 页
C
1,211 行
if (in_interrupt()) BUG(); videobuf_waiton(&buf->vb,0,0); videobuf_vmalloc_free(&buf->vb); buf->vb.state = STATE_NEEDS_INIT;}#define norm_maxw() 1024#define norm_maxh() 768static intbuffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, enum v4l2_field field){ struct vivi_fh *fh = vq->priv_data; struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); int rc, init_buffer = 0; dprintk(1,"%s, field=%d\n",__FUNCTION__,field); BUG_ON(NULL == fh->fmt); if (fh->width < 48 || fh->width > norm_maxw() || fh->height < 32 || fh->height > norm_maxh()) return -EINVAL; buf->vb.size = fh->width*fh->height*2; if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) return -EINVAL; if (buf->fmt != fh->fmt || buf->vb.width != fh->width || buf->vb.height != fh->height || buf->vb.field != field) { buf->fmt = fh->fmt; buf->vb.width = fh->width; buf->vb.height = fh->height; buf->vb.field = field; init_buffer = 1; } if (STATE_NEEDS_INIT == buf->vb.state) { if (0 != (rc = videobuf_iolock(vq,&buf->vb,NULL))) goto fail; } buf->vb.state = STATE_PREPARED; return 0;fail: free_buffer(vq,buf); return rc;}static voidbuffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb){ struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); struct vivi_fh *fh = vq->priv_data; struct vivi_dev *dev = fh->dev; struct vivi_dmaqueue *vidq = &dev->vidq; struct vivi_buffer *prev; if (!list_empty(&vidq->queued)) { dprintk(1,"adding vb queue=0x%08lx\n",(unsigned long)&buf->vb.queue); list_add_tail(&buf->vb.queue,&vidq->queued); buf->vb.state = STATE_QUEUED; dprintk(2,"[%p/%d] buffer_queue - append to queued\n", buf, buf->vb.i); } else if (list_empty(&vidq->active)) { list_add_tail(&buf->vb.queue,&vidq->active); buf->vb.state = STATE_ACTIVE; mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT); dprintk(2,"[%p/%d] buffer_queue - first active\n", buf, buf->vb.i); vivi_start_thread(vidq); } else { prev = list_entry(vidq->active.prev, struct vivi_buffer, vb.queue); if (prev->vb.width == buf->vb.width && prev->vb.height == buf->vb.height && prev->fmt == buf->fmt) { list_add_tail(&buf->vb.queue,&vidq->active); buf->vb.state = STATE_ACTIVE; dprintk(2,"[%p/%d] buffer_queue - append to active\n", buf, buf->vb.i); } else { list_add_tail(&buf->vb.queue,&vidq->queued); buf->vb.state = STATE_QUEUED; dprintk(2,"[%p/%d] buffer_queue - first queued\n", buf, buf->vb.i); } }}static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb){ struct vivi_buffer *buf = container_of(vb,struct vivi_buffer,vb); struct vivi_fh *fh = vq->priv_data; struct vivi_dev *dev = (struct vivi_dev*)fh->dev; struct vivi_dmaqueue *vidq = &dev->vidq; dprintk(1,"%s\n",__FUNCTION__); vivi_stop_thread(vidq); free_buffer(vq,buf);}static struct videobuf_queue_ops vivi_video_qops = { .buf_setup = buffer_setup, .buf_prepare = buffer_prepare, .buf_queue = buffer_queue, .buf_release = buffer_release,};/* ------------------------------------------------------------------ IOCTL vidioc handling ------------------------------------------------------------------*/static int vidioc_querycap (struct file *file, void *priv, struct v4l2_capability *cap){ strcpy(cap->driver, "vivi"); strcpy(cap->card, "vivi"); cap->version = VIVI_VERSION; cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; return 0;}static int vidioc_enum_fmt_cap (struct file *file, void *priv, struct v4l2_fmtdesc *f){ if (f->index > 0) return -EINVAL; strlcpy(f->description,format.name,sizeof(f->description)); f->pixelformat = format.fourcc; return 0;}static int vidioc_g_fmt_cap (struct file *file, void *priv, struct v4l2_format *f){ struct vivi_fh *fh=priv; f->fmt.pix.width = fh->width; f->fmt.pix.height = fh->height; f->fmt.pix.field = fh->vb_vidq.field; f->fmt.pix.pixelformat = fh->fmt->fourcc; f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3; f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; return (0);}static int vidioc_try_fmt_cap (struct file *file, void *priv, struct v4l2_format *f){ struct vivi_fmt *fmt; enum v4l2_field field; unsigned int maxw, maxh; if (format.fourcc != f->fmt.pix.pixelformat) { dprintk(1,"Fourcc format (0x%08x) invalid. Driver accepts " "only 0x%08x\n",f->fmt.pix.pixelformat,format.fourcc); return -EINVAL; } fmt=&format; field = f->fmt.pix.field; if (field == V4L2_FIELD_ANY) { field=V4L2_FIELD_INTERLACED; } else if (V4L2_FIELD_INTERLACED != field) { dprintk(1,"Field type invalid.\n"); return -EINVAL; } maxw = norm_maxw(); maxh = norm_maxh(); f->fmt.pix.field = field; if (f->fmt.pix.height < 32) f->fmt.pix.height = 32; if (f->fmt.pix.height > maxh) f->fmt.pix.height = maxh; if (f->fmt.pix.width < 48) f->fmt.pix.width = 48; if (f->fmt.pix.width > maxw) f->fmt.pix.width = maxw; f->fmt.pix.width &= ~0x03; f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; return 0;}/*FIXME: This seems to be generic enough to be at videodev2 */static int vidioc_s_fmt_cap (struct file *file, void *priv, struct v4l2_format *f){ struct vivi_fh *fh=priv; int ret = vidioc_try_fmt_cap(file,fh,f); if (ret < 0) return (ret); fh->fmt = &format; fh->width = f->fmt.pix.width; fh->height = f->fmt.pix.height; fh->vb_vidq.field = f->fmt.pix.field; fh->type = f->type; return (0);}static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p){ struct vivi_fh *fh=priv; return (videobuf_reqbufs(&fh->vb_vidq, p));}static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p){ struct vivi_fh *fh=priv; return (videobuf_querybuf(&fh->vb_vidq, p));}static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p){ struct vivi_fh *fh=priv; return (videobuf_qbuf(&fh->vb_vidq, p));}static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p){ struct vivi_fh *fh=priv; return (videobuf_dqbuf(&fh->vb_vidq, p, file->f_flags & O_NONBLOCK));}#ifdef CONFIG_VIDEO_V4L1_COMPATstatic int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf){ struct vivi_fh *fh=priv; return videobuf_cgmbuf (&fh->vb_vidq, mbuf, 8);}#endifstatic int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i){ struct vivi_fh *fh=priv; if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; if (i != fh->type) return -EINVAL; return videobuf_streamon(&fh->vb_vidq);}static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i){ struct vivi_fh *fh=priv; if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; if (i != fh->type) return -EINVAL; return videobuf_streamoff(&fh->vb_vidq);}static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *i){ return 0;}/* only one input in this sample driver */static int vidioc_enum_input (struct file *file, void *priv, struct v4l2_input *inp){ if (inp->index != 0) return -EINVAL; inp->type = V4L2_INPUT_TYPE_CAMERA; inp->std = V4L2_STD_NTSC_M; strcpy(inp->name,"Camera"); return (0);}static int vidioc_g_input (struct file *file, void *priv, unsigned int *i){ *i = 0; return (0);}static int vidioc_s_input (struct file *file, void *priv, unsigned int i){ if (i > 0) return -EINVAL; return (0);} /* --- controls ---------------------------------------------- */static int vidioc_queryctrl (struct file *file, void *priv, struct v4l2_queryctrl *qc){ int i; for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) if (qc->id && qc->id == vivi_qctrl[i].id) { memcpy(qc, &(vivi_qctrl[i]), sizeof(*qc)); return (0); } return -EINVAL;}static int vidioc_g_ctrl (struct file *file, void *priv, struct v4l2_control *ctrl){ int i; for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) if (ctrl->id == vivi_qctrl[i].id) { ctrl->value=qctl_regs[i]; return (0); } return -EINVAL;}static int vidioc_s_ctrl (struct file *file, void *priv, struct v4l2_control *ctrl){ int i; for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) if (ctrl->id == vivi_qctrl[i].id) { if (ctrl->value < vivi_qctrl[i].minimum || ctrl->value > vivi_qctrl[i].maximum) { return (-ERANGE); } qctl_regs[i]=ctrl->value; return (0); } return -EINVAL;}/* ------------------------------------------------------------------ File operations for the device ------------------------------------------------------------------*/#define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)static int vivi_open(struct inode *inode, struct file *file){ int minor = iminor(inode); struct vivi_dev *dev; struct vivi_fh *fh; int i; printk(KERN_DEBUG "vivi: open called (minor=%d)\n",minor); list_for_each_entry(dev, &vivi_devlist, vivi_devlist) if (dev->vfd.minor == minor) goto found; return -ENODEV;found: /* If more than one user, mutex should be added */ dev->users++; dprintk(1, "open minor=%d type=%s users=%d\n", minor, v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users); /* allocate + initialize per filehandle data */ fh = kzalloc(sizeof(*fh),GFP_KERNEL); if (NULL == fh) { dev->users--; return -ENOMEM; } file->private_data = fh; fh->dev = dev; fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fh->fmt = &format; fh->width = 640; fh->height = 480; /* Put all controls at a sane state */ for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++) qctl_regs[i] =vivi_qctrl[i].default_value; dprintk(1,"Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n", (unsigned long)fh,(unsigned long)dev,(unsigned long)&dev->vidq); dprintk(1,"Open: list_empty queued=%d\n",list_empty(&dev->vidq.queued)); dprintk(1,"Open: list_empty active=%d\n",list_empty(&dev->vidq.active)); /* Resets frame counters */ dev->h=0; dev->m=0; dev->s=0; dev->us=0; dev->jiffies=jiffies; sprintf(dev->timestr,"%02d:%02d:%02d:%03d", dev->h,dev->m,dev->s,(dev->us+500)/1000); videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops, NULL, NULL, fh->type, V4L2_FIELD_INTERLACED, sizeof(struct vivi_buffer),fh); return 0;}static ssize_tvivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos){ struct vivi_fh *fh = file->private_data; if (fh->type==V4L2_BUF_TYPE_VIDEO_CAPTURE) { return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0, file->f_flags & O_NONBLOCK); } return 0;}static unsigned intvivi_poll(struct file *file, struct poll_table_struct *wait){ struct vivi_fh *fh = file->private_data; struct videobuf_queue *q = &fh->vb_vidq; dprintk(1,"%s\n",__FUNCTION__); if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) return POLLERR; return videobuf_poll_stream(file, q, wait);}static int vivi_release(struct inode *inode, struct file *file){ struct vivi_fh *fh = file->private_data; struct vivi_dev *dev = fh->dev; struct vivi_dmaqueue *vidq = &dev->vidq; int minor = iminor(inode); vivi_stop_thread(vidq); videobuf_stop(&fh->vb_vidq); videobuf_mmap_free(&fh->vb_vidq); kfree (fh); dev->users--; printk(KERN_DEBUG "vivi: close called (minor=%d, users=%d)\n",minor,dev->users); return 0;}static intvivi_mmap(struct file *file, struct vm_area_struct * vma){ struct vivi_fh *fh = file->private_data; int ret; dprintk (1,"mmap called, vma=0x%08lx\n",(unsigned long)vma); ret=videobuf_mmap_mapper(&fh->vb_vidq, vma); dprintk (1,"vma start=0x%08lx, size=%ld, ret=%d\n", (unsigned long)vma->vm_start, (unsigned long)vma->vm_end-(unsigned long)vma->vm_start, ret); return ret;}static const struct file_operations vivi_fops = { .owner = THIS_MODULE, .open = vivi_open, .release = vivi_release, .read = vivi_read, .poll = vivi_poll, .ioctl = video_ioctl2, /* V4L2 ioctl handler */ .mmap = vivi_mmap, .llseek = no_llseek,};static struct video_device vivi = { .name = "vivi", .type = VID_TYPE_CAPTURE, .fops = &vivi_fops, .minor = -1,// .release = video_device_release, .vidioc_querycap = vidioc_querycap, .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, .vidioc_g_fmt_cap = vidioc_g_fmt_cap, .vidioc_try_fmt_cap = vidioc_try_fmt_cap, .vidioc_s_fmt_cap = vidioc_s_fmt_cap, .vidioc_reqbufs = vidioc_reqbufs, .vidioc_querybuf = vidioc_querybuf, .vidioc_qbuf = vidioc_qbuf, .vidioc_dqbuf = vidioc_dqbuf, .vidioc_s_std = vidioc_s_std, .vidioc_enum_input = vidioc_enum_input, .vidioc_g_input = vidioc_g_input, .vidioc_s_input = vidioc_s_input, .vidioc_queryctrl = vidioc_queryctrl, .vidioc_g_ctrl = vidioc_g_ctrl, .vidioc_s_ctrl = vidioc_s_ctrl, .vidioc_streamon = vidioc_streamon, .vidioc_streamoff = vidioc_streamoff,#ifdef CONFIG_VIDEO_V4L1_COMPAT .vidiocgmbuf = vidiocgmbuf,#endif .tvnorms = V4L2_STD_NTSC_M, .current_norm = V4L2_STD_NTSC_M,};/* ----------------------------------------------------------------- Initialization and module stuff ------------------------------------------------------------------*/static int __init vivi_init(void){ int ret; struct vivi_dev *dev; dev = kzalloc(sizeof(*dev),GFP_KERNEL); if (NULL == dev) return -ENOMEM; list_add_tail(&dev->vivi_devlist,&vivi_devlist); /* init video dma queues */ INIT_LIST_HEAD(&dev->vidq.active); INIT_LIST_HEAD(&dev->vidq.queued); init_waitqueue_head(&dev->vidq.wq); /* initialize locks */ mutex_init(&dev->lock); dev->vidq.timeout.function = vivi_vid_timeout; dev->vidq.timeout.data = (unsigned long)dev; init_timer(&dev->vidq.timeout); ret = video_register_device(&vivi, VFL_TYPE_GRABBER, video_nr); printk(KERN_INFO "Video Technology Magazine Virtual Video Capture Board (Load status: %d)\n", ret); return ret;}static void __exit vivi_exit(void){ struct vivi_dev *h; struct list_head *list; while (!list_empty(&vivi_devlist)) { list = vivi_devlist.next; list_del(list); h = list_entry(list, struct vivi_dev, vivi_devlist); kfree (h); } video_unregister_device(&vivi);}module_init(vivi_init);module_exit(vivi_exit);MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");MODULE_LICENSE("Dual BSD/GPL");module_param(video_nr, int, 0);module_param_named(debug,vivi.debug, int, 0644);MODULE_PARM_DESC(debug,"activates debug info");module_param(vid_limit,int,0644);MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?