Move VIDIOC_G/S_PRIORITY handling to the V4L core
cover Move VIDIOC_G/S_PRIORITY handling to the V4L core
Few drivers implement VIDIOC_G/S_PRIORITY and those that do often implement it incorrectly.
Now that we have a v4l2_fh struct it is easy to add support for priority handling to the v4l core framework.
There are three types of drivers:
1) Those that use v4l2_fh. There the local priority can be stored in the v4l2_fh struct.
2) Those that do not have an open or release function defined in v4l2_file_ops. That means that file->private_data will never be filled and so we can use that to store the local priority in.
3) Others. In all other cases we leave it to the driver. Of course, the goal is to eventually move the 'others' into type 1 or 2.
This patch series shows how it is done and converts ivtv to rely on the core framework instead of doing it manually.
Comments?
Regards,
Hans
Hans Verkuil (7):
v4l2_prio: move from v4l2-common to v4l2-device.
v4l2-device: add v4l2_prio_state to v4l2_device.
v4l2-fh: implement v4l2_priority support.
v4l2-dev: add and support flag V4L2_FH_USE_PRIO.
v4l2-ioctl: add priority handling support.
ivtv: drop priority handling, use core framework for this.
ivtv: add priority checks for the non-standard commands.
drivers/media/video/cpia2/cpia2.h | 1 + drivers/media/video/ivtv/ivtv-driver.h | 2 - drivers/media/video/ivtv/ivtv-fileops.c | 2 - drivers/media/video/ivtv/ivtv-ioctl.c | 61 ++++++++------------------ drivers/media/video/pvrusb2/pvrusb2-v4l2.c | 1 + drivers/media/video/v4l2-common.c | 59 ------------------------- drivers/media/video/v4l2-dev.c | 23 +++++++++- drivers/media/video/v4l2-device.c | 62 +++++++++++++++++++++++++++ drivers/media/video/v4l2-fh.c | 4 ++ drivers/media/video/v4l2-ioctl.c | 64 +++++++++++++++++++++++++--- include/media/v4l2-common.h | 15 ------- include/media/v4l2-dev.h | 6 +++ include/media/v4l2-device.h | 17 +++++++ include/media/v4l2-fh.h | 1 + 14 files changed, 190 insertions(+), 128 deletions(-)
RFC PATCH 1/7 v4l2_prio: move from v4l2-common to v4l2-device.
We are going to move priority handling into the v4l2 core. As a consequence the v4l2_prio helper functions need to be moved into the core videodev module as well to prevent circular dependencies.
--- drivers/media/video/cpia2/cpia2.h | 1 + drivers/media/video/pvrusb2/pvrusb2-v4l2.c | 1 + drivers/media/video/v4l2-common.c | 59 --------------------------- drivers/media/video/v4l2-device.c | 61 ++++++++++++++++++++++++++++ include/media/v4l2-common.h | 15 ------- include/media/v4l2-device.h | 15 +++++++ 6 files changed, 78 insertions(+), 74 deletions(-)
drivers/media/video/cpia2/cpia2.h +1 -0
@@ -33,6 +33,7 @@#include <linux/version.h> #include <linux/videodev.h> +#include <media/v4l2-device.h> #include <media/v4l2-common.h> #include <linux/usb.h> #include <linux/poll.h>
drivers/media/video/pvrusb2/pvrusb2-v4l2.c +1 -0
@@ -29,6 +29,7 @@#include "pvrusb2-ioread.h" #include <linux/videodev2.h> #include <media/v4l2-dev.h> +#include <media/v4l2-device.h> #include <media/v4l2-common.h> #include <media/v4l2-ioctl.h>
drivers/media/video/v4l2-common.c +0 -59
65 lines not on this page · raw message
drivers/media/video/v4l2-device.c +61 -0
64 lines not on this page · raw message
include/media/v4l2-common.h +0 -15
@@ -80,21 +80,6 @@/* ------------------------------------------------------------------------- */ -/* Priority helper functions */ - -struct v4l2_prio_state { -atomic_t prios[4 ]; -}; -void v4l2_prio_init (struct v4l2_prio_state *global); -int v4l2_prio_change (struct v4l2_prio_state *global,enum v4l2_priority *local, -enum v4l2_priority new); -void v4l2_prio_open (struct v4l2_prio_state *global,enum v4l2_priority *local); -void v4l2_prio_close (struct v4l2_prio_state *global,enum v4l2_priority local); -enum v4l2_priority v4l2_prio_max (struct v4l2_prio_state *global); -int v4l2_prio_check (struct v4l2_prio_state *global,enum v4l2_priority local); - -/* ------------------------------------------------------------------------- */ -/* Control helper functions */ int v4l2_ctrl_check(struct v4l2_ext_control *ctrl, struct v4l2_queryctrl *qctrl,
include/media/v4l2-device.h +15 -0
@@ -23,6 +23,11 @@#include <media/v4l2-subdev.h> +/* struct to keep track of the access priority state */ +struct v4l2_prio_state { +atomic_t prios[4 ]; +}; +/* Each instance of a V4L2 device should create the v4l2_device struct, either stand-alone or embedded in a larger struct.
@@ -49,6 +54,16 @@ struct v4l2_device { unsigned int notification, void *arg); }; +/* Priority helper functions */ + +void v4l2_prio_init (struct v4l2_prio_state *global); +int v4l2_prio_change (struct v4l2_prio_state *global,enum v4l2_priority *local, +enum v4l2_priority new); +void v4l2_prio_open (struct v4l2_prio_state *global,enum v4l2_priority *local); +void v4l2_prio_close (struct v4l2_prio_state *global,enum v4l2_priority local); +enum v4l2_priority v4l2_prio_max (struct v4l2_prio_state *global); +int v4l2_prio_check (struct v4l2_prio_state *global,enum v4l2_priority local); +/* Initialize v4l2_dev and make dev->driver_data point to v4l2_dev. dev may be NULL in rare cases (ISA devices). In that case you must fill in the v4l2_dev->name field before calling this function. */
Signature · 1 line
1.6.4.2
RFC PATCH 2/7 v4l2-device: add v4l2_prio_state to v4l2_device.
In reply to Hans Verkuil--- drivers/media/video/v4l2-device.c | 1 + include/media/v4l2-device.h | 2 ++ 2 files changed, 3 insertions(+), 0 deletions(-)
drivers/media/video/v4l2-device.c +1 -0
@@ -34,6 +34,7 @@ int v4l2_device_register(struct device *dev, struct v4l2_device *v4l2_dev)INIT_LIST_HEAD (&v4l2_dev->subdevs);spin_lock_init (&v4l2_dev->lock); +v4l2_prio_init (&v4l2_dev->prio); v4l2_dev->dev = dev;if (dev ==NULL ) {/* If dev == NULL, then name must be filled in by the caller */
include/media/v4l2-device.h +2 -0
@@ -49,6 +49,8 @@ struct v4l2_device {spinlock_t lock;/* unique device name, by default the driver name + bus ID */ char name[V4L2_DEVICE_NAME_SIZE ]; +/* Device's priority state */ +struct v4l2_prio_state prio;/* notify callback called by some sub-devices. */ void (*notify)(struct v4l2_subdev *sd,unsigned int notification,void *arg);
Signature · 1 line
1.6.4.2
RFC PATCH 3/7 v4l2-fh: implement v4l2_priority support.
In reply to Hans Verkuil--- drivers/media/video/v4l2-fh.c | 4 ++++ include/media/v4l2-fh.h | 1 + 2 files changed, 5 insertions(+), 0 deletions(-)
drivers/media/video/v4l2-fh.c +4 -0
@@ -27,12 +27,14 @@#include <media/v4l2-fh.h> #include <media/v4l2-event.h> #include <media/v4l2-ioctl.h> +#include <media/v4l2-device.h> int v4l2_fh_init (struct v4l2_fh *fh,struct video_device *vdev) { fh->vdev = vdev;INIT_LIST_HEAD (&fh->list);set_bit (V4L2_FL_USES_V4L2_FH , &fh->vdev->flags); + fh->prio =V4L2_PRIORITY_UNSET ;/* * fh->events only needs to be initialized if the driver
@@ -51,6 +53,7 @@ void v4l2_fh_add(struct v4l2_fh *fh) {unsigned long flags; +v4l2_prio_open (&fh->vdev->v4l2_dev->prio, &fh->prio);spin_lock_irqsave (&fh->vdev->fh_lock, flags);list_add (&fh->list, &fh->vdev->fh_list);spin_unlock_irqrestore (&fh->vdev->fh_lock, flags);
@@ -64,6 +67,7 @@ void v4l2_fh_del(struct v4l2_fh *fh)spin_lock_irqsave (&fh->vdev->fh_lock, flags);list_del_init (&fh->list);spin_unlock_irqrestore (&fh->vdev->fh_lock, flags); +v4l2_prio_close (&fh->vdev->v4l2_dev->prio, fh->prio); }EXPORT_SYMBOL_GPL (v4l2_fh_del);
include/media/v4l2-fh.h +1 -0
@@ -35,6 +35,7 @@ struct v4l2_fh {struct list_head list;struct video_device *vdev;struct v4l2_events *events;/* events, pending and subscribed */ +enum v4l2_priority prio; };/*
Signature · 1 line
1.6.4.2
RFC PATCH 4/7 v4l2-dev: add and support flag V4L2_FH_USE_PRIO.
In reply to Hans VerkuilFlag V4L2_FH_USE_PRIO is set if we can safely store the priority level in file->private_data. Support this flag for the open and release file operations.
--- drivers/media/video/v4l2-dev.c | 23 +++++++++++++++++++++-- include/media/v4l2-dev.h | 6 ++++++ 2 files changed, 27 insertions(+), 2 deletions(-)
drivers/media/video/v4l2-dev.c +21 -2
@@ -283,6 +283,15 @@ static int v4l2_open(struct inode *inode, struct file *filp)/* and increase the device refcount */ video_get (vdev);mutex_unlock (&videodev_lock); + +if (test_bit (V4L2_FL_USES_V4L2_PRIO , &vdev->flags)) { +enum v4l2_priority prio = + (enum v4l2_priority )filp->private_data; + +v4l2_prio_open (&vdev->v4l2_dev->prio, &prio); + filp->private_data = (void *)prio; + } +if (vdev->fops->open) ret = vdev->fops->open (filp);
@@ -296,15 +305,20 @@ static int v4l2_open(struct inode *inode, struct file *filp)static int v4l2_release (struct inode *inode,struct file *filp) {struct video_device *vdev =video_devdata (filp); -int ret =0 ; +if (test_bit (V4L2_FL_USES_V4L2_PRIO , &vdev->flags)) { +enum v4l2_priority prio = + (enum v4l2_priority )filp->private_data; + +v4l2_prio_close (&vdev->v4l2_dev->prio, prio); + }if (vdev->fops->release) vdev->fops->release (filp);/* decrease the refcount unconditionally since the release() return value is ignored. */ video_put (vdev); -return ret; +return 0 ; }static const struct file_operations v4l2_unlocked_fops = {
@@ -515,6 +529,11 @@ static int __video_register_device(struct video_device *vdev, int type, int nr, vdev->index =get_index (vdev);mutex_unlock (&videodev_lock); +/* Determine if we can store v4l2_priority in file->private_data */ +if (vdev->fops->open ==NULL && vdev->fops->release ==NULL && + vdev->v4l2_dev !=NULL ) +set_bit (V4L2_FL_USES_V4L2_PRIO , &vdev->flags); +/* Part 3: Initialize the character device */ vdev->cdev =cdev_alloc ();if (vdev->cdev ==NULL ) {
include/media/v4l2-dev.h +6 -0
@@ -32,7 +32,13 @@ struct v4l2_device;Drivers can clear this flag if they want to block all future device access. It is cleared by video_unregister_device. */ #define V4L2_FL_REGISTERED (0) +/* Flag to mark that the driver stores a pointer to struct v4l2_fh in +file->private_data. Is automatically detected. */ #define V4L2_FL_USES_V4L2_FH (1) +/* Flag to mark that the driver stores the local v4l2_priority in +file->private_data. Will be used if open and release are both NULL. +Drivers can also set it manually. */ +#define V4L2_FL_USES_V4L2_PRIO (2)struct v4l2_file_operations {struct module *owner;
Signature · 1 line
1.6.4.2
RFC PATCH 5/7 v4l2-ioctl: add priority handling support.
In reply to Hans VerkuilDrivers that use either v4l2_fh or don't use filehandles at all can now use the core framework support of g/s_priority.
--- drivers/media/video/v4l2-ioctl.c | 64 ++++++++++++++++++++++++++++++++++--- 1 files changed, 58 insertions(+), 6 deletions(-)
drivers/media/video/v4l2-ioctl.c +58 -6
@@ -27,6 +27,7 @@#include <media/v4l2-ioctl.h> #include <media/v4l2-fh.h> #include <media/v4l2-event.h> +#include <media/v4l2-device.h> #include <media/v4l2-chip-ident.h> #define dbgarg(cmd, fmt, arg...) \
@@ -611,6 +612,7 @@ static long __video_do_ioctl(struct file *file, {struct video_device *vfd =video_devdata (file);const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops; +enum v4l2_priority prio =V4L2_PRIORITY_UNSET ;void *fh = file->private_data;long ret = -EINVAL ;
@@ -640,6 +642,40 @@ static long __video_do_ioctl(struct file *file,printk (KERN_CONT "\n" ); } +if (test_bit (V4L2_FL_USES_V4L2_FH , &vfd->flags)) + prio = ((struct v4l2_fh *)fh)->prio; +else if (test_bit (V4L2_FL_USES_V4L2_PRIO , &vfd->flags)) + prio = (enum v4l2_priority )fh; +if (prio !=V4L2_PRIORITY_UNSET ) { +switch (cmd) { +case VIDIOC_S_CTRL : +case VIDIOC_S_STD : +case VIDIOC_S_INPUT : +case VIDIOC_S_OUTPUT : +case VIDIOC_S_TUNER : +case VIDIOC_S_FREQUENCY : +case VIDIOC_S_FMT : +case VIDIOC_S_CROP : +case VIDIOC_S_AUDIO : +case VIDIOC_S_AUDOUT : +case VIDIOC_S_EXT_CTRLS : +case VIDIOC_S_FBUF : +case VIDIOC_S_PRIORITY : +case VIDIOC_S_DV_PRESET : +case VIDIOC_S_DV_TIMINGS : +case VIDIOC_S_JPEGCOMP : +case VIDIOC_S_MODULATOR : +case VIDIOC_S_PARM : +case VIDIOC_S_HW_FREQ_SEEK : +case VIDIOC_ENCODER_CMD : +case VIDIOC_OVERLAY : + ret =v4l2_prio_check (&vfd->v4l2_dev->prio, prio); +if (ret) + goto exit_prio; +break ; + } + } +switch (cmd) {#ifdef CONFIG_VIDEO_V4L1_COMPAT
@@ -689,9 +725,14 @@ static long __video_do_ioctl(struct file *file, {enum v4l2_priority *p = arg; -if (!ops->vidioc_g_priority) -break ; - ret = ops->vidioc_g_priority (file, fh, p); +if (!ops->vidioc_g_priority) { +if (prio ==V4L2_PRIORITY_UNSET ) +break ; + *p =v4l2_prio_max (&vfd->v4l2_dev->prio); + ret =0 ; + }else { + ret = ops->vidioc_g_priority (file, fh, p); + }if (!ret)dbgarg (cmd,"priority is %d\n" , *p);break ;
@@ -700,10 +741,20 @@ static long __video_do_ioctl(struct file *file, {enum v4l2_priority *p = arg; -if (!ops->vidioc_s_priority) -break ; +if (!ops->vidioc_s_priority && prio ==V4L2_PRIORITY_UNSET ) +break ;dbgarg (cmd,"setting priority to %d\n" , *p); - ret = ops->vidioc_s_priority (file, fh, *p); +if (ops->vidioc_s_priority) { + ret = ops->vidioc_s_priority (file, fh, *p); + }else { + ret =v4l2_prio_change (&vfd->v4l2_dev->prio, &prio, *p); +if (!ret) { +if (test_bit (V4L2_FL_USES_V4L2_FH , &vfd->flags)) + ((struct v4l2_fh *)fh)->prio = prio; +else + file->private_data = (void *)prio; + } + }break ; }
@@ -2010,6 +2061,7 @@ static long __video_do_ioctl(struct file *file, } }/* switch */ +exit_prio:if (vfd->debug &V4L2_DEBUG_IOCTL_ARG ) {if (ret <0 ) {v4l_print_ioctl (vfd->name, cmd);
Signature · 1 line
1.6.4.2
RFC PATCH 6/7 ivtv: drop priority handling, use core framework for this.
In reply to Hans Verkuil--- drivers/media/video/ivtv/ivtv-driver.h | 2 - drivers/media/video/ivtv/ivtv-fileops.c | 2 - drivers/media/video/ivtv/ivtv-ioctl.c | 41 ------------------------------- 3 files changed, 0 insertions(+), 45 deletions(-)
drivers/media/video/ivtv/ivtv-driver.h +0 -2
@@ -379,7 +379,6 @@ struct ivtv_open_id {u32 open_id;/* unique ID for this file descriptor */ int type;/* stream type */ int yuv_frames;/* 1: started OUT_UDMA_YUV output mode */ -enum v4l2_priority prio;/* priority */ struct ivtv *itv; };
@@ -704,7 +703,6 @@ struct ivtv {/* Miscellaneous */ u32 open_id;/* incremented each time an open occurs, is >= 1 */ -struct v4l2_prio_state prio;/* priority state */ int search_pack_header;/* 1 if ivtv_copy_buf_to_user() is scanning for a pack header (0xba) */ int speed;/* current playback speed setting */ u8 speed_mute_audio;/* 1 if audio should be muted when fast forward */
drivers/media/video/ivtv/ivtv-fileops.c +0 -2
@@ -853,7 +853,6 @@ int ivtv_v4l2_close(struct file *filp)IVTV_DEBUG_FILE ("close %s\n" , s->name); -v4l2_prio_close (&itv->prio, id->prio);v4l2_fh_del (fh);v4l2_fh_exit (fh);
@@ -949,7 +948,6 @@ static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp) } item->itv = itv; item->type = s->type; -v4l2_prio_open (&itv->prio, &item->prio); item->open_id = itv->open_id++; filp->private_data = &item->fh;
drivers/media/video/ivtv/ivtv-ioctl.c +0 -41
61 lines not on this page · raw message
Signature · 1 line
1.6.4.2
RFC PATCH 7/7 ivtv: add priority checks for the non-standard commands.
In reply to Hans Verkuil--- drivers/media/video/ivtv/ivtv-ioctl.c | 20 +++++++++++++++++++- 1 files changed, 19 insertions(+), 1 deletions(-)
drivers/media/video/ivtv/ivtv-ioctl.c +19 -1
@@ -1777,7 +1777,25 @@ static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)static long ivtv_default (struct file *file,void *fh,int cmd,void *arg) { -struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv; +struct ivtv_open_id *id =fh2id (fh); +struct ivtv *itv = id->itv; +int ret; + +switch (cmd) { +case VIDEO_PLAY : +case VIDEO_STOP : +case VIDEO_FREEZE : +case VIDEO_CONTINUE : +case VIDEO_COMMAND : +case VIDEO_SELECT_SOURCE : +case AUDIO_SET_MUTE : +case AUDIO_CHANNEL_SELECT : +case AUDIO_BILINGUAL_CHANNEL_SELECT : + ret =v4l2_prio_check (&itv->v4l2_dev.prio, id->fh.prio); +if (ret) +return ret; +break ; + }switch (cmd) {case VIDIOC_INT_RESET : {
Signature · 1 line
1.6.4.2
Hi Hans,
Quoted 15 lines · On Sunday 09 May 2010 21:29:05 Hans Verkuil wrote:
Few drivers implement VIDIOC_G/S_PRIORITY and those that do often implement it incorrectly.
Now that we have a
v4l2_fhstruct it is easy to add support for priority handling to the v4l core framework.There are three types of drivers:
1) Those that use
v4l2_fh. There the local priority can be stored in thev4l2_fhstruct.2) Those that do not have an open or release function defined in
v4l2_file_ops. That means thatfile->private_datawill never be filled and so we can use that to store the local priority in.
Quoted 7 lines
3) Others. In all other cases we leave it to the driver. Of course, the goal is to eventually move the 'others' into type 1 or 2.
This patch series shows how it is done and converts ivtv to rely on the core framework instead of doing it manually.
Comments?
I don't think this is right. You're moving the priority ioctls support to v4l2_device, while it would make more sense to move it to video_device. If a device can capture two independent video streams simultaneously, your patches would prevent it from working at all.
Signature · 3 lines
Regards, Laurent Pinchart