videodev.[ch] redesign
Subsystems (2)
Hi,
In 2.5.x I want to drop the open/read/write/poll/mmap/... function pointers from struct video_device and all the useless video_read/write/poll/mmap/... wrapper functions from videodev.c.
Instead, I'd like to see the video4linux drivers use struct file_operations directly and handle the device registration like soundcore does it for the sound drivers: simply swap file->f_op in video_open().
The patch below does part one of the plan -- for 2.4.x kernels. It adds the fops pointer to struct video_device and makes video_open use it if available, so both old + new style drivers will work.
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
Comments?
Gerd
[mailed to both lkml + video4linux list]
-------------------------- cut here -------------------------
include/linux/videodev.h +40 -9
@@ -4,6 +4,18 @@#include <linux/types.h> #include <linux/version.h> +#if 0 +/* +* v4l2 is still work-in-progress, integration planed for 2.5.x +* v4l2 project homepage: http://www.thedirks.org/v4l2/ +* patches available from: http://bytesex.org/patches/ +*/ +# define HAVE_V4L2 1 +# include <linux/videodev2.h> +#else +# undef HAVE_V4L2 +#endif +#ifdef __KERNEL__#include <linux/poll.h>
@@ -12,22 +24,31 @@struct video_device {struct module *owner; -char name[32 ]; -int type; +char name[32 ]; +int type;/* v4l1 */ +int type2;/* v4l2 */ int hardware; +/* old, obsolete interface -- to be removed some day (2.5.x ?) */ int (*open)(struct video_device *,int mode);void (*close)(struct video_device *);long (*read)(struct video_device *,char *,unsigned long ,int noblock); -/* Do we need a write method ? */ long (*write)(struct video_device *,const char *,unsigned long ,int noblock); -#if LINUX_VERSION_CODE >=0x020100 unsigned int (*poll)(struct video_device *,struct file *,poll_table *); -#endif int (*ioctl)(struct video_device *,unsigned int ,void *);int (*mmap)(struct video_device *,const char *,unsigned long ); -int (*initialize)(struct video_device *); -void *priv;/* Used to be 'private' but that upsets C++ */ +int (*initialize)(struct video_device *); + +/* new interface -- we will use file_operations directly +* like soundcore does. +* kernel_ioctl() will be called by video_generic_ioctl. +* video_generic_ioctl() does the userspace copying of the +* ioctl arguments */ +struct file_operations *fops; +int (*kernel_ioctl)(struct inode *inode,struct file *file, +unsigned int cmd,void *arg); + +void *priv;/* Used to be 'private' but that upsets C++ */ int busy;int minor;devfs_handle_t devfs_handle;
@@ -42,8 +63,11 @@#define VFL_TYPE_VTX 3extern void video_unregister_device (struct video_device *); -#endif +extern struct video_device *video_devdata (struct file *); +extern int video_generic_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg); +#endif /* __KERNEL__ */ #define VID_TYPE_CAPTURE 1/* Can capture */ #define VID_TYPE_TUNER 2/* Can tune */
@@ -149,6 +173,7 @@#define VIDEO_AUDIO_VOLUME 4#define VIDEO_AUDIO_BASS 8#define VIDEO_AUDIO_TREBLE 16 +#define VIDEO_AUDIO_BALANCE 32char name[16 ];#define VIDEO_SOUND_MONO 1#define VIDEO_SOUND_STEREO 2
@@ -378,4 +403,10 @@#define VID_HARDWARE_MEYE 32/* Sony Vaio MotionEye cameras */ #define VID_HARDWARE_CPIA2 33 -#endif +#endif /* __LINUX_VIDEODEV_H */ + +/* +* Local variables: +* c-basic-offset: 8 +* End: +*/
drivers/media/video/Config.in +1 -0
@@ -8,6 +8,7 @@ dep_tristate ' I2C on parallel port' CONFIG_I2C_PARPORT $CONFIG_PARPORT $CONFIG_I2C comment 'Video Adapters' +dep_tristate ' Skeleton Driver Video For Linux' CONFIG_VIDEO_SKELETON $CONFIG_VIDEO_DEV if [ "$CONFIG_I2C_ALGOBIT" = "y" -o "$CONFIG_I2C_ALGOBIT" = "m" ]; then dep_tristate ' BT848 Video For Linux' CONFIG_VIDEO_BT848 $CONFIG_VIDEO_DEV $CONFIG_PCI $CONFIG_I2C_ALGOBIT fi
drivers/media/video/Makefile +1 -0
@@ -33,6 +33,7 @@ obj-$(CONFIG_VIDEO_DEV) += videodev.o +obj-$(CONFIG_VIDEO_SKELETON) += skeleton.o obj-$(CONFIG_BUS_I2C) += i2c-old.o obj-$(CONFIG_VIDEO_BT848) += bttv.o msp3400.o tvaudio.o \ tda7432.o tda9875.o tuner.o
drivers/media/video/skeleton.c +249 -0
249 lines not on this page · raw message
drivers/media/video/videodev.c +94 -10
175 lines not on this page · raw message
Documentation/Configure.help +13 -0
@@ -21269,6 +21269,19 @@ To use this option, you have to check, that the "/proc file system support" (CONFIG_PROC_FS) is enabled too. +Skeleton Video Capture Driver +CONFIG_VIDEO_SKELETON + This code is a skeleton driver that only illustrates how V4L + drivers register and communicate with the Video for Linux subsystem. + It does nothing. + + This driver is also available as a module called skeleton.o ( = code + which can be inserted in and removed from the running kernel + whenever you want). If you want to compile it as a module, say M + here and read Documentation/modules.txt. + + If unsure, say N, otherwise say N anyway. + AIMSlab RadioTrack (aka RadioReveal) support CONFIG_RADIO_RTRACK Choose Y here if you have one of these FM radio cards, and then fill
Quoted 10 lines · Gerd Knorr
Hi,
The patch below does part one of the plan -- for 2.4.x kernels. It adds the fops pointer to struct
video_deviceand makesvideo_openuse it if available, so both old + new style drivers will work.It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
That is a large improvement.
But you don't include a lock against reentry, which is bad.
Quoted 1 line
Comments?
Could you make a helper for open like for ioctl ?
And please don't use a pointer to the device descriptor in the file structure. It makes live for USB devices much harder.
Regards
Oliver
Quoted 10 lines · Gerd Knorr
Hi,
The patch below does part one of the plan -- for 2.4.x kernels. It adds the fops pointer to struct
video_deviceand makesvideo_openuse it if available, so both old + new style drivers will work.It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
That is a large improvement.
But you don't include a lock against reentry, which is bad.
Quoted 1 line
Comments?
Could you make a helper for open like for ioctl ?
And please don't use a pointer to the device descriptor in the file structure. It makes live for USB devices much harder.
Regards
Oliver
Quoted 6 lines
Quoted 3 lines
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
That is a large improvement.
But you don't include a lock against reentry, which is bad.
I don't want to handle the wrapper function too much. IMHO it is the job of the driver to do locking if needed. For some read-only ioctls like VIDIOCGCAP you don't need locking at all.
Quoted 3 lines
Quoted 1 line
Comments?
Could you make a helper for open like for ioctl ?
video_open does call video_device[minor]->fops->open(), isn't that enought?
Quoted 2 lines
And please don't use a pointer to the device descriptor in the file structure. It makes live for USB devices much harder.
Sorry, I don't understand. What exactly do you mean? file->private_data? videodev.c doesn't touch it ...
Gerd
Signature · 1 line
#define ENOCLUE 125 /* userland programmer induced race condition */
Quoted 11 lines · Gerd Knorr
Quoted 7 lines
Quoted 4 lines
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
That is a large improvement.
But you don't include a lock against reentry, which is bad.I don't want to handle the wrapper function too much. IMHO it is the job of the driver to do locking if needed. For some read-only ioctls like VIDIOCGCAP you don't need locking at all.
OK.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Comments?
Could you make a helper for open like for ioctl ?
video_opendoes call video_device[minor]->fops->open(), isn't that enought?
I'd prefer seeing exclusive opening handeled in the helper initially.
Quoted 5 lines
Quoted 2 lines
And please don't use a pointer to the device descriptor in the file structure. It makes live for USB devices much harder.
Sorry, I don't understand. What exactly do you mean?
file->private_data?videodev.cdoesn't touch it ...
But the skeleton driver you provide does so.
Regards
Oliver
Quoted 5 lines
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
Comments?
I'm not sure 2.4 should change but for 2.5 this is absolutely bang on perfect
Quoted 4 lines · Gerd Knorr
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
Excellent work. I have no complaints, just a few questions:
1. Would it be better to memset the temp buffer in video_generic_ioctl() rather than in the driver? I've seen so many drivers forget to do this, and it's a potential (albeit very small) security hole.
2. In skeleton_open(), couldn't the device_data lookup code be replaced with:
struct video_device *vdev = video_devdata(file); struct device_data *dev = vdev->priv;
3. In skeleton_initdev(), shouldn't...
dev->vdev = skeleton_template;
...be...
memcpy(&dev->vdev, &skeleton_template, sizeof(skeleton_template);
4. Is it safe to keep even 128 bytes on the stack in video_generic_ioctl()? Consider that devices might spend a relatively long time blocking on VIDIOCSYNC. With 32 devices in use at once, you'd be coming dangerously close to a stack overflow. IMHO it would be better to only allocate as much as MCAPTURE and SYNC need, and fall back on kmalloc for the less time-critical ones (if necessary).
Other than that, I extremely happy with what you've done!
Signature · 2 lines
Mark McClelland [email protected]
Quoted 5 lines
Excellent work. I have no complaints, just a few questions:
1. Would it be better to memset the temp buffer in
video_generic_ioctl()rather than in the driver? I've seen so many drivers forget to do this, and it's a potential (albeit very small) security hole.
The wrapper fills the buffer using copy_from_user() -- even for _IOR ioctls because some are labeled wrong -- and the driver needs that data. I can't zero the buffer ...
Quoted 5 lines
2. In
skeleton_open(), couldn't thedevice_datalookup code be replaced with:struct video_device *vdev = video_devdata(file); struct device_data *dev = vdev->priv;
Good point. Yes, that should work.
Quoted 7 lines
3. In
skeleton_initdev(), shouldn't...dev->vdev = skeleton_template;...be...
memcpy(&dev->vdev, &skeleton_template, sizeof(skeleton_template);
No. It does the same.
Quoted 4 lines
4. Is it safe to keep even 128 bytes on the stack in
video_generic_ioctl()? Consider that devices might spend a relatively long time blocking on VIDIOCSYNC. With 32 devices in use at once, you'd be coming dangerously close to a stack overflow.
I don't see a overflow can easily happen here. There is one kernel strack _per process_. Calling schedule() will also switch the stack.
Quoted 3 lines
IMHO it would be better
to only allocate as much as MCAPTURE and SYNC need, and fall back on kmalloc for the less time-critical ones (if necessary).
struct v4l2_buffer should fit onto the stack too.
Gerd
Signature · 1 line
#define ENOCLUE 125 /* userland programmer induced race condition */
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Could you make a helper for open like for ioctl ?
video_opendoes call video_device[minor]->fops->open(), isn't that enought?I'd prefer seeing exclusive opening handeled in the helper initially.
Ok, I see what you mean. That was another issue I was thinking about. Current videodev.c allows one open at a time only, the new code removes that restriction (intentionally).
Should videodev.c provide a way to ask for exclusive opens to maintain backward compatibility, using some flag in struct video_device maybe? Is there some sane way to do this without having to hook into fops->release? Checking inode->i_count maybe?
Quoted 4 lines
Quoted 2 lines
Sorry, I don't understand. What exactly do you mean?
file->private_data?videodev.cdoesn't touch it ...But the skeleton driver you provide does so.
Thats just some sample code, a dummy driver which does nothing but print some messages to the log. If you want allow multiple applications access the driver at the same you need some way to disturgish the file handles, and skeleton.c demonstrates how this can be done using file->private_data.
usb drivers are free to do with file->private_data whatever they want (like skeleton.c does), videodev.c doesn't use it.
Gerd
Signature · 1 line
#define ENOCLUE 125 /* userland programmer induced race condition */
Quoted 8 lines · Alan Cox
Quoted 5 lines
It also provides a ioctl wrapper function which handles copying the ioctl args from/to userspace, so we have this at one place can drop all the copy_from/to_user calls within the v4l device driver ioctl handlers.
Comments?
I'm not sure 2.4 should change but for 2.5 this is absolutely bang on perfect
For 2.5 I want switch over to using file_operations completely and drop the old stuff. Thus 2.4 should provide both old+new way to handle
stuff: The old one for backward compatibility and the new one to allow backporting 2.5.x drivers to 2.4 ...
Yesterday I've booted a 2.5.x kernel the first time and managed to make bttv work there. 2.5.x version of the will follow ...
Gerd
Signature · 1 line
#define ENOCLUE 125 /* userland programmer induced race condition */
OK, agreed on all points. Thanks for the clarification.
BTW, is there any chance for vmalloc() and pals to be moved to videodev.c, or something higher-up? I realize that vmalloc() can be used in instead in most cases, but at the expense of a more complex and potentially slower mmap().
Signature · 2 lines
Mark McClelland [email protected]
Quoted 4 lines · Mark McClelland
OK, agreed on all points. Thanks for the clarification.
BTW, is there any chance for
vmalloc()and pals to be moved tovideodev.c, or something higher-up?
What do you mean exactly? bttv's memory management code, which has been copied to various places, and which is now broken in 2.5.x due to virt_to_bus() being gone finally?
Some of this is work-in-progress. I'm talking to Dave to put some helper functions to handle DMA to vmalloced memory blocks to some sensible place within the kernel. If someone wants to have a look (not final yet): http://bytesex.org/patches/15_pci-2.4.18-pre8.diff
Gerd
Signature · 1 line
#define ENOCLUE 125 /* userland programmer induced race condition */
Quoted 9 lines · Gerd Knorr
Quoted 2 lines · Mark McClelland
BTW, is there any chance for
vmalloc()and pals to be moved tovideodev.c, or something higher-up?What do you mean exactly? bttv's memory management code, which has been copied to various places, and which is now broken in 2.5.x due to
virt_to_bus()being gone finally?
Sorry, I meant to type rvmalloc().
Many drivers (eg. USB webcam drivers), don't need virt_to_bus(). They only need a way to allocate reserved pages that they can safely do remap_page_range() on, for mmap().
Quoted 5 lines
Some of this is work-in-progress. I'm talking to Dave to put some helper functions to handle DMA to vmalloced memory blocks to some sensible place within the kernel. If someone wants to have a look (not final yet): http://bytesex.org/patches/15_pci-2.4.18-pre8.diff
Thanks, that's exactly what I was looking for. pci_vmalloc_to_page() should satisfy all of the USB drivers, if they override vma->vm_ops->nopage().
Signature · 2 lines
Mark McClelland [email protected]
[PATCH/RFC] videodev.[ch] redesign -- take #2
In reply to Gerd KnorrHi,
Ok, next batch of patches. Below are the videodev.c patches for both 2.5.x and 2.4.x. The 2.5.x patches drop the old function pointers from struct video_device and switch over to the new, struct file_operations based setup completely (which breaks almost all existing v4l drivers). The 2.4.x version of the patch supports both old (for backward compatibility) and new (for 2.5.x compatibility) way to register v4l drivers.
Related stuff:
http://bytesex.org/patches/11_v4l_skel-2.4.18-pre9.diff
The skeleton driver, if you want some sample code how to use the new-style v4l registration. Last patch version had this included, I've separated it now.
http://bytesex.org/bttv/
bttv 0.8.x uses the new-style v4l registration too.
http://bytesex.org/patches/2.5/
Patches for 2.5.x, fix drivers to use the new-style v4l registration. Right now there are a few patches for some of the radio drivers, more patches will follow next days. Testers are very welcome ...
Gerd
========== [ 2.5.x patch ] ==================================
include/linux/videodev.h +36 -15
@@ -4,6 +4,18 @@#include <linux/types.h> #include <linux/version.h> +#if 0 +/* +* v4l2 is still work-in-progress, integration planed for 2.5.x +* v4l2 project homepage: http://www.thedirks.org/v4l2/ +* patches available from: http://bytesex.org/patches/ +*/ +# define HAVE_V4L2 1 +# include <linux/videodev2.h> +#else +# undef HAVE_V4L2 +#endif +#ifdef __KERNEL__#include <linux/poll.h>
@@ -13,21 +25,20 @@struct video_device {struct module *owner; -char name[32 ]; -int type; +char name[32 ]; +int type;/* v4l1 */ +int type2;/* v4l2 */ int hardware; -int (*open)(struct video_device *,int mode); -void (*close)(struct video_device *); -long (*read)(struct video_device *,char *,unsigned long ,int noblock); -/* Do we need a write method ? */ -long (*write)(struct video_device *,const char *,unsigned long ,int noblock); -#if LINUX_VERSION_CODE >=0x020100 -unsigned int (*poll)(struct video_device *,struct file *,poll_table *); -#endif -int (*ioctl)(struct video_device *,unsigned int ,void *); -int (*mmap)(struct vm_area_struct *vma,struct video_device *,const char *,unsigned long ); -int (*initialize)(struct video_device *); +/* new interface -- we will use file_operations directly +* like soundcore does. +* kernel_ioctl() will be called by video_generic_ioctl. +* video_generic_ioctl() does the userspace copying of the +* ioctl arguments */ +struct file_operations *fops; +int (*kernel_ioctl)(struct inode *inode,struct file *file, +unsigned int cmd,void *arg); +void *priv;/* Used to be 'private' but that upsets C++ */ int busy;int minor;
@@ -43,8 +54,11 @@#define VFL_TYPE_VTX 3extern void video_unregister_device (struct video_device *); -#endif +extern struct video_device *video_devdata (struct file *); +extern int video_generic_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg); +#endif /* __KERNEL__ */ #define VID_TYPE_CAPTURE 1/* Can capture */ #define VID_TYPE_TUNER 2/* Can tune */
@@ -150,6 +164,7 @@#define VIDEO_AUDIO_VOLUME 4#define VIDEO_AUDIO_BASS 8#define VIDEO_AUDIO_TREBLE 16 +#define VIDEO_AUDIO_BALANCE 32char name[16 ];#define VIDEO_SOUND_MONO 1#define VIDEO_SOUND_STEREO 2
@@ -379,4 +394,10 @@#define VID_HARDWARE_MEYE 32/* Sony Vaio MotionEye cameras */ #define VID_HARDWARE_CPIA2 33 -#endif +#endif /* __LINUX_VIDEODEV_H */ + +/* +* Local variables: +* c-basic-offset: 8 +* End: +*/
drivers/media/video/videodev.c +74 -134
293 lines not on this page · raw message
========== [ 2.4.x patch ] ==================================
include/linux/videodev.h +40 -9
86 lines not on this page · raw message
drivers/media/video/videodev.c +94 -13
187 lines not on this page · raw message