X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=vdummy;a=blobdiff_plain;f=vdummy.c;h=a4d05b827789a01ddd77181763bc48862825a113;hp=e01eba6e93406ad44497864dd297deee02a8c87a;hb=d427a056fc21f60f596ce3bbdd2b5930f4fa09c4;hpb=dfbabe3bf9b7ffefdf7ab48dd62cf506855ec3fb diff --git a/vdummy.c b/vdummy.c index e01eba6..a4d05b8 100644 --- a/vdummy.c +++ b/vdummy.c @@ -3,15 +3,19 @@ #include #include #include +#include +#include #include #include #include #include #include #include +#include +#include struct buffer { - struct vb2_buffer vb; + struct vb2_v4l2_buffer vb; struct buffer *next; }; @@ -20,14 +24,16 @@ static void shutdown(void); static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap); static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdesc); +static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz); static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt); static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt); +static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt); static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt); static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *fmt); static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ctl); static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf, - unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx); + unsigned int *nplanes, unsigned int *sizes, struct device **alloc_ctx); static int buf_prepare(struct vb2_buffer *vb); static void buf_queue(struct vb2_buffer *vb); static int start_streaming(struct vb2_queue *vbq, unsigned int count); @@ -35,6 +41,8 @@ static void stop_streaming(struct vb2_queue *vbq); static int update_frame(int xsz, int ysz); +static int gen_frame(void); +enum hrtimer_restart timer_func(struct hrtimer *timer); module_init(init); @@ -42,7 +50,7 @@ module_exit(shutdown); MODULE_LICENSE("GPL"); MODULE_AUTHOR("John Tsiombikas"); -MODULE_DESCRIPTION("v4l2 test module"); +MODULE_DESCRIPTION("Dummy V4L2 device"); static struct mutex mutex; static struct video_device *vdev; @@ -57,9 +65,12 @@ static unsigned char *frame; static int frame_size; static int streaming; -static struct buffer *buflist; +static spinlock_t blist_lock; +static struct buffer *buflist, *buflist_tail; static int seqno; +static int framerate = 1; +static struct hrtimer timer; static int init(void) { @@ -71,6 +82,7 @@ static int init(void) } mutex_init(&mutex); + spin_lock_init(&blist_lock); vbops.queue_setup = queue_setup; vbops.buf_prepare = buf_prepare; @@ -86,7 +98,7 @@ static int init(void) vbq.ops = &vbops; vbq.mem_ops = &vb2_vmalloc_memops; vbq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; - vbq.min_buffers_needed = 1; + vbq.min_buffers_needed = 2; vbq.lock = &mutex; vbq.gfp_flags = GFP_KERNEL; @@ -99,12 +111,16 @@ static int init(void) if(!(vdev = video_device_alloc())) { return -ENOMEM; } + memset(vdev, 0, sizeof *vdev); vdev->release = video_device_release; strcpy(vdev->name, KBUILD_MODNAME); vdev->fops = &fops; vdev->ioctl_ops = &iops; vdev->vfl_type = VFL_TYPE_GRABBER; + vdev->vfl_dir = VFL_DIR_RX; vdev->v4l2_dev = &v4l2_dev; + vdev->lock = &mutex; + vdev->queue = &vbq; fops.owner = THIS_MODULE; fops.open = v4l2_fh_open; @@ -116,8 +132,10 @@ static int init(void) iops.vidioc_querycap = ioctl_querycap; iops.vidioc_enum_fmt_vid_cap = ioctl_enum_fmt; + iops.vidioc_enum_framesizes = ioctl_enum_framesz; iops.vidioc_g_fmt_vid_cap = ioctl_get_fmt; iops.vidioc_s_fmt_vid_cap = ioctl_set_fmt; + iops.vidioc_try_fmt_vid_cap = ioctl_try_fmt; iops.vidioc_g_parm = ioctl_get_parm; iops.vidioc_s_parm = ioctl_set_parm; iops.vidioc_queryctrl = ioctl_queryctl; @@ -129,6 +147,9 @@ static int init(void) iops.vidioc_expbuf = vb2_ioctl_expbuf; iops.vidioc_streamon = vb2_ioctl_streamon; iops.vidioc_streamoff = vb2_ioctl_streamoff; + iops.vidioc_log_status = v4l2_ctrl_log_status; + iops.vidioc_subscribe_event = v4l2_ctrl_subscribe_event; + iops.vidioc_unsubscribe_event = v4l2_event_unsubscribe; if((res = video_register_device(vdev, VFL_TYPE_GRABBER, -1)) != 0) { printk(KERN_ALERT "vdummy: failed to register video device\n"); @@ -137,6 +158,9 @@ static int init(void) return res; } + hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + timer.function = timer_func; + if((res = update_frame(640, 480)) != 0) { video_unregister_device(vdev); v4l2_device_unregister(&v4l2_dev); @@ -150,6 +174,7 @@ static int init(void) static void shutdown(void) { + hrtimer_cancel(&timer); video_unregister_device(vdev); v4l2_device_unregister(&v4l2_dev); kfree(frame); @@ -158,7 +183,7 @@ static void shutdown(void) static int ioctl_querycap(struct file *file, void *fh, struct v4l2_capability *cap) { strcpy(cap->driver, KBUILD_MODNAME); - strcpy(cap->card, "dummy v4l2 dev"); + strcpy(cap->card, "Dummy V4L2 device"); strcpy(cap->bus_info, "nobus"); cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING; cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; @@ -176,6 +201,18 @@ static int ioctl_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *fdes return 0; } +static int ioctl_enum_framesz(struct file *file, void *fh, struct v4l2_frmsizeenum *fsz) +{ + if(fsz->index != 0) { + return -EINVAL; + } + fsz->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; + fsz->stepwise.min_width = fsz->stepwise.min_height = 1; + fsz->stepwise.max_width = fsz->stepwise.max_height = 8192; + fsz->stepwise.step_width = fsz->stepwise.step_height = 1; + return 0; +} + static int ioctl_get_fmt(struct file *file, void *fh, struct v4l2_format *fmt) { if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { @@ -209,6 +246,28 @@ static int ioctl_set_fmt(struct file *file, void *fh, struct v4l2_format *fmt) return ioctl_get_fmt(file, fh, fmt); } +static int ioctl_try_fmt(struct file *file, void *fh, struct v4l2_format *fmt) +{ + if(fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { + return -EINVAL; + } + if(fmt->fmt.pix.width <= 0) + fmt->fmt.pix.width = width; + if(fmt->fmt.pix.height <= 0) + fmt->fmt.pix.height = height; + fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; + fmt->fmt.pix.field = V4L2_FIELD_NONE; + fmt->fmt.pix.bytesperline = 0; + fmt->fmt.pix.sizeimage = frame_size; + fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; + fmt->fmt.pix.priv = 0; + fmt->fmt.pix.flags = 0; + fmt->fmt.pix.ycbcr_enc = 0; + fmt->fmt.pix.quantization = V4L2_QUANTIZATION_FULL_RANGE; + fmt->fmt.pix.xfer_func = V4L2_XFER_FUNC_NONE; + return 0; +} + static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *parm) { if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { @@ -217,7 +276,7 @@ static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *p parm->parm.capture.capability = 0; parm->parm.capture.capturemode = 0; parm->parm.capture.timeperframe.numerator = 1; - parm->parm.capture.timeperframe.denominator = 30; + parm->parm.capture.timeperframe.denominator = framerate; parm->parm.capture.extendedmode = 0; parm->parm.capture.readbuffers = 1; return 0; @@ -225,9 +284,14 @@ static int ioctl_get_parm(struct file *file, void *fh, struct v4l2_streamparm *p static int ioctl_set_parm(struct file *file, void *fh, struct v4l2_streamparm *parm) { + int new_framerate; if(parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) { return -EINVAL; } + + new_framerate = parm->parm.capture.timeperframe.denominator / parm->parm.capture.timeperframe.numerator; + framerate = new_framerate < 1 ? 1 : (new_framerate > 60 ? 60 : new_framerate); + return ioctl_get_parm(file, fh, parm); } @@ -237,7 +301,7 @@ static int ioctl_queryctl(struct file *file, void *fh, struct v4l2_queryctrl *ct } static int queue_setup(struct vb2_queue *vbq, unsigned int *nbuf, - unsigned int *nplanes, unsigned int *sizes, void **alloc_ctx) + unsigned int *nplanes, unsigned int *sizes, struct device **alloc_ctx) { if(vbq->num_buffers + *nbuf < 2) { *nbuf = 2 - vbq->num_buffers; @@ -255,26 +319,60 @@ static int buf_prepare(struct vb2_buffer *vb) printk(KERN_ALERT "vdummy: buffer too small\n"); return -EINVAL; } - vb2_set_plane_payload(vb, 0, frame_size); return 0; } static void buf_queue(struct vb2_buffer *vb) { - memcpy(vb2_plane_vaddr(vb, 0), frame, frame_size); - vb2_buffer_done(vb, VB2_BUF_STATE_DONE); + struct buffer *buf = (struct buffer*)((char*)vb - offsetof(struct buffer, vb)); + + spin_lock(&blist_lock); + if(buflist) { + buflist_tail->next = buf; + buflist_tail = buf; + } else { + buflist = buflist_tail = buf; + } + buf->next = 0; + spin_unlock(&blist_lock); +} + +static void clear_queue(enum vb2_buffer_state st) +{ + spin_lock(&blist_lock); + while(buflist) { + struct buffer *buf = buflist; + buflist = buflist->next; + vb2_buffer_done(&buf->vb.vb2_buf, st); + } + buflist = buflist_tail = 0; + spin_unlock(&blist_lock); } static int start_streaming(struct vb2_queue *vbq, unsigned int count) { + ktime_t frame_interval; + streaming = 1; seqno = 0; + + /* calculate frame interval in nanoseconds */ + if(framerate <= 1) { + frame_interval = ktime_set(1, 0); + } else { + frame_interval = ktime_set(0, 1000000000ul / (unsigned long)framerate); + } + + hrtimer_start(&timer, frame_interval, HRTIMER_MODE_REL); return 0; } static void stop_streaming(struct vb2_queue *vbq) { + hrtimer_cancel(&timer); + streaming = 0; + clear_queue(VB2_BUF_STATE_ERROR); } static int update_frame(int xsz, int ysz) @@ -309,5 +407,39 @@ static int update_frame(int xsz, int ysz) *tmp++ = (val << 2) & 0xff; } } + return 0; } + +static int gen_frame(void) +{ + struct buffer *buf = 0; + unsigned long flags; + + spin_lock_irqsave(&blist_lock, flags); + if(buflist) { + buf = buflist; + buflist = buflist->next; + if(!buflist) buflist_tail = 0; + } + spin_unlock_irqrestore(&blist_lock, flags); + + if(buf) { + memcpy(vb2_plane_vaddr(&buf->vb.vb2_buf, 0), frame, frame_size); + + buf->vb.vb2_buf.timestamp = ktime_get_ns(); + printk(KERN_INFO "vdummy: copying frame: %d ts: %lu\n", seqno, + (unsigned long)buf->vb.vb2_buf.timestamp); + buf->vb.sequence = seqno++; + buf->vb.field = V4L2_FIELD_NONE; + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE); + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, frame_size); + } + return 0; +} + +enum hrtimer_restart timer_func(struct hrtimer *timer) +{ + gen_frame(); + return streaming ? HRTIMER_RESTART : HRTIMER_NORESTART; +}