]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/media/video/usbvision/usbvision-core.c
V4L/DVB (4927): Enhancements on usbvision driver
[mv-sheeva.git] / drivers / media / video / usbvision / usbvision-core.c
index a23fcd15a1d051936227024e28909b25fd63e264..06e8e67da362346447a5537f303836039f9e5054 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * USB USBVISION Video device driver 0.9.8.3cvs (For Kernel 2.4.19-2.4.32 + 2.6.0-2.6.16)
  *
- * 
+ *
  *
  * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  *
  * June 2, 2004 - 0.9.6 Dwaine Garden
  *     - Fixed sourceforge.net cvs repository.
  *     - Added #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,26) for .owner to help compiling under kernels 2.4.x which do not have the i2c v2.8.x updates.
- *     - Added device Hauppauge WinTv-USB III (PAL) FM Model 597 to usbvision.h        
+ *     - Added device Hauppauge WinTv-USB III (PAL) FM Model 597 to usbvision.h
  *
  * July 1, 2004 -0.9.6 Dwaine Garden
  *     - Patch was submitted by Hal Finkel to fix the problem with the tuner not working under kernel 2.6.7.
  *     - Changed usbvision_muxsel to address the problem with black & white s-video output for NT1004 devices with saa7114 video decoder.  Thanks to Emmanuel for the patch and testing.
  *     - Fixed up SECAM devices which could not properly output video.  Changes to usbmuxsel.  Thanks to Emmanuel for the patch and everyone with a SECAM device which help test.
  *     - Removed some commented out code.  Clean up.
- *     - Tried to fix up the annoying empty directories in the sourceforge.net cvs.   Fuck it up again.  8*(   
+ *     - Tried to fix up the annoying empty directories in the sourceforge.net cvs.   Fuck it up again.  8*(
  *
  * November 15, 2004 - 0.9.8 Dwaine Garden
  *     - Release new tar - 0.9.8 on sourceforge.net
  *     - Better support for mythtv.
  *
  * January 2, 2005 - 0.9.8.1cvs Dwaine Garden
- *     - Setup that you can specify which device is used for video.  Default is auto detect next available device number eg.  /dev/videoX  
+ *     - Setup that you can specify which device is used for video.  Default is auto detect next available device number eg.  /dev/videoX
  *     - Setup that you can specify which device is used for radio.  Default is auto detect next available device number eg.  /dev/radioX
  *     - usb_unlink_urb() is deprecated for synchronous unlinks.  Using usb_kill_urb instead.
  *     - usbvision_kvirt_to_pa is deprecated.  Removed.
  * April 20, 2005 - 0.9.8.2cvs Dwaine Garden
  *     - Release lock in usbvision_v4l_read_done.  -Thanks to nplanel for the patch.
  *     - Additional comments to the driver.
- *     - Fixed some spelling mistakes.  8*)    
+ *     - Fixed some spelling mistakes.  8*)
  *
  * April 23, 2005 - 0.9.8.2cvs Joerg Heckenbach
  *     - Found bug in usbvision line counting. Now there should be no spurious lines in the image any longer.
- *     - Swapped usbvision_register_video and usbvision_configure_video to fix problem with PowerOnAtOpen=0. 
+ *     - Swapped usbvision_register_video and usbvision_configure_video to fix problem with PowerOnAtOpen=0.
  *       Thanks to Erwan Velu
  *
  * April 26, 2005 - 0.9.8.2cvs Joerg Heckenbach
  *     - Fixed problem with rmmod module and oppses. Replaced vfree(usbvision->overlay_base) with iounmap(usbvision->overlay_base).
- *     - Added function usb_get_dev(dev) and ; To help with unloading the module multiple times without crashing. 
+ *     - Added function usb_get_dev(dev) and ; To help with unloading the module multiple times without crashing.
  *       (Keep the reference count in kobjects correct)
  *
  * June 14, 2005 - 0.9.8.2cvs Dwaine
- *     - Missed a change in saa7113.c for checking kernel version.  Added conditional if's.    
+ *     - Missed a change in saa7113.c for checking kernel version.  Added conditional if's.
  *
  * June 15, 2005 - 0.9.8.2cvs Dwaine
- *     - Added new device WinTV device VendorId 0573 and ProductId 4d29.    
+ *     - Added new device WinTV device VendorId 0573 and ProductId 4d29.
  *     - Hacked some support for newer NT1005 devices.  This devices only seem to have one configuration, not multiple configurations like the NT1004.
  *
  * June 29, 2005 - 0.9.8.2cvs Dwaine
- *     - Added new device WinTV device VendorId 0573 and ProductId 4d37.   
- *     - Because of the first empty entry in usbvision_table, modutils failed to create the necessary entries for modules.usbmap. 
- *       This means hotplug won't work for usbvision.  Thanks to Gary Ng.  
+ *     - Added new device WinTV device VendorId 0573 and ProductId 4d37.
+ *     - Because of the first empty entry in usbvision_table, modutils failed to create the necessary entries for modules.usbmap.
+ *       This means hotplug won't work for usbvision.  Thanks to Gary Ng.
  *     - Sent an e-mail to the maintainer of usb.ids.  New devices identified need to be added.
- *     - Fixed compile error with saa7113 under kernel 2.6.12. 
+ *     - Fixed compile error with saa7113 under kernel 2.6.12.
  *
  * July 6, 2005 - 0.9.8.2cvs Dwaine
- *     - Patch submitted by Gary Ng for two additional procfs entries.  Device Input and Frequency setting.  
+ *     - Patch submitted by Gary Ng for two additional procfs entries.  Device Input and Frequency setting.
  *
  * July 12, 2005 - 0.9.8.2cvs Dwaine
  *     - New tuner identified for some devices it's called TCL_MFPE05.  This tuner uses the same API as tuner 38 in tuner.c.
  *     - Thanks to lynx31 for contacting Hauppage and asking them.
- *     - I have no clue as to which devices use this new tuner, so people will have to contact me and tell me.  
+ *     - I have no clue as to which devices use this new tuner, so people will have to contact me and tell me.
  *
  * July 21, 2005 - 0.9.8.2cvs Dwaine
  *     - Patched usbvision.c with missing ifdef kernversion statement so the module will compile with older kernels and v4l.
  *     -Changed number of bytes for i2c write to 4 as per the NT100X spec sheet.  Thanks to Merlum for finding it.
  *     -Remove the radio option for device Hauppauge WinTV USB device Model 40219 Rev E189.  This device does not have a FM radio.  Thanks to Shadwell.
  *     -Added radio option for device Hauppauge WinTV USB device Model 40219 Rev E189 again.  Just got an e-mail indicating their device has one.  8*)
- *   
+ *
  * Aug 27, 2006 - 0.9.8.3cvs Dwaine
  *     -Changed ifdef statement so the usbvision driver will compile with kernels at 2.6.12.
  *     -Updated readme files for new updated tuner list for v4l devices.
- *      
+ *
  *
  *
  * TODO:
 #define USBVISION_VERSION __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL) " " USBVISION_DRIVER_VERSION_COMMENT
 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
 
-#include <media/saa7115.h> 
-#include <media/v4l2-common.h> 
+#include <media/saa7115.h>
+#include <media/v4l2-common.h>
 #include <media/tuner.h>
 #include <media/audiochip.h>
 
@@ -427,16 +427,35 @@ enum {
 };
 
 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
-        { 1, 1,  8, V4L2_PIX_FMT_GREY    , "GREY" },
-        { 1, 2, 16, V4L2_PIX_FMT_RGB565  , "RGB565" },
-        { 1, 3, 24, V4L2_PIX_FMT_RGB24   , "RGB24" },
-        { 1, 4, 32, V4L2_PIX_FMT_RGB32   , "RGB32" },
-        { 1, 2, 16, V4L2_PIX_FMT_RGB555  , "RGB555" },
-        { 1, 2, 16, V4L2_PIX_FMT_YUYV    , "YUV422" },
-        { 1, 2, 12, V4L2_PIX_FMT_YVU420  , "YUV420P" }, // 1.5 !
-        { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
+       { 1, 1,  8, V4L2_PIX_FMT_GREY    , "GREY" },
+       { 1, 2, 16, V4L2_PIX_FMT_RGB565  , "RGB565" },
+       { 1, 3, 24, V4L2_PIX_FMT_RGB24   , "RGB24" },
+       { 1, 4, 32, V4L2_PIX_FMT_RGB32   , "RGB32" },
+       { 1, 2, 16, V4L2_PIX_FMT_RGB555  , "RGB555" },
+       { 1, 2, 16, V4L2_PIX_FMT_YUYV    , "YUV422" },
+       { 1, 2, 12, V4L2_PIX_FMT_YVU420  , "YUV420P" }, // 1.5 !
+       { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
+};
+
+/* supported tv norms */
+static struct usbvision_tvnorm tvnorms[] = {
+       {
+               .name = "PAL",
+               .id = V4L2_STD_PAL,
+       }, {
+               .name = "NTSC",
+               .id = V4L2_STD_NTSC,
+       }, {
+                .name = "SECAM",
+                .id = V4L2_STD_SECAM,
+       }, {
+               .name = "PAL-M",
+               .id = V4L2_STD_PAL_M,
+       }
 };
 
+#define TVNORMS ARRAY_SIZE(tvnorms)
+
 
 /*
  * The value of 'scratch_buf_size' affects quality of the picture
@@ -448,15 +467,10 @@ static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
 #define DEFAULT_SCRATCH_BUF_SIZE       (0x20000)               // 128kB memory scratch buffer
 static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
 
-static int init_brightness = 128;      // Initalize the brightness of the video device
-static int init_contrast = 192;                // Initalize the contrast of the video device
-static int init_saturation = 128;      // Initalize the staturation mode of the video device
-static int init_hue = 128;             // Initalize the Hue settings of the video device
-
 // Function prototypes
 static int usbvision_restart_isoc(struct usb_usbvision *usbvision);
 static int usbvision_begin_streaming(struct usb_usbvision *usbvision);
-static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int norm);
+static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel);
 static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, short len);
 static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, short len);
 static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg);
@@ -466,9 +480,11 @@ static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
 static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
 static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
 static void usbvision_release(struct usb_usbvision *usbvision);
-static int usbvision_set_input(struct usb_usbvision *usbvision); 
-static int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height); 
-static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, void *arg); 
+static int usbvision_set_input(struct usb_usbvision *usbvision);
+static int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height);
+static void usbvision_empty_framequeues(struct usb_usbvision *dev);
+static int usbvision_stream_interrupt(struct usb_usbvision *dev);
+static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, void *arg);
 
 
 // Bit flags (options)
@@ -488,8 +504,8 @@ static int adjustCompression = 1;                   // Set the compression to be adaptive
 static int dga = 1;                                    // Set the default Direct Graphic Access
 static int PowerOnAtOpen = 1;                          // Set the default device to power on at startup
 static int SwitchSVideoInput = 0;                      // To help people with Black and White output with using s-video input.  Some cables and input device are wired differently.
-static int video_nr = -1;                              // Sequential Number of Video Device
-static int radio_nr = -1;                              // Sequential Number of Radio Device
+static int video_nr = -1;                              // Sequential Number of Video Device
+static int radio_nr = -1;                              // Sequential Number of Radio Device
 static int vbi_nr = -1;                                        // Sequential Number of VBI Device
 static char *CustomDevice=NULL;                                // Set as nothing....
 
@@ -515,10 +531,10 @@ MODULE_PARM(adjustCompression, "i");                      // Grab the compression to be adaptive
 MODULE_PARM(dga, "i");                                 // Grab the Direct Graphic Access
 MODULE_PARM(PowerOnAtOpen, "i");                       // Grab the device to power on at startup
 MODULE_PARM(SwitchSVideoInput, "i");                   // To help people with Black and White output with using s-video input.  Some cables and input device are wired differently.
-MODULE_PARM(video_nr, "i");                            // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)                 
+MODULE_PARM(video_nr, "i");                            // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
 MODULE_PARM(radio_nr, "i");                            // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
 MODULE_PARM(vbi_nr, "i");                              // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
-MODULE_PARM(CustomDevice, "s");                                // .... CustomDevice
+MODULE_PARM(CustomDevice, "s");                                // .... CustomDevice
 #endif
 
 MODULE_PARM_DESC(flags,        " Set the default Overlay Display mode of the device driver.  Default: 0 (Off)");
@@ -539,7 +555,7 @@ MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE(DRIVER_LICENSE);
        MODULE_VERSION(DRIVER_VERSION);
-       MODULE_ALIAS(DRIVER_ALIAS); 
+       MODULE_ALIAS(DRIVER_ALIAS);
 
 #ifdef MODULE
 static unsigned int autoload = 1;
@@ -549,8 +565,8 @@ static unsigned int autoload = 0;
 
 
 /****************************************************************************************/
-/* SYSFS Code - Copied from the stv680.c usb module.                                   */
-/* Device information is located at /sys/class/video4linux/video0                      */
+/* SYSFS Code - Copied from the stv680.c usb module.                                   */
+/* Device information is located at /sys/class/video4linux/video0                      */
 /* Device parameters information is located at /sys/module/usbvision                    */
 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber    */
 /****************************************************************************************/
@@ -567,94 +583,95 @@ static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
 static ssize_t show_version(struct class_device *cd, char *buf)
 {
        return sprintf(buf, "%s\n", DRIVER_VERSION);
-} 
+}
 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
 
 static ssize_t show_model(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
-} 
+}
 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
 
 static ssize_t show_hue(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%d\n", usbvision->hue >> 8);
-} 
+}
 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
 
 static ssize_t show_contrast(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%d\n", usbvision->contrast >> 8);
-} 
+}
 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
 
 static ssize_t show_brightness(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%d\n", usbvision->brightness >> 8);
-} 
+}
 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
 
 static ssize_t show_saturation(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%d\n", usbvision->saturation >> 8);
-} 
+}
 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
 
 static ssize_t show_streaming(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
-       return sprintf(buf, "%s\n", YES_NO(usbvision->streaming));
-} 
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
+       return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
+}
 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
 
 static ssize_t show_overlay(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%s\n", YES_NO(usbvision->overlay));
-} 
+}
 static CLASS_DEVICE_ATTR(overlay, S_IRUGO, show_overlay, NULL);
 
 static ssize_t show_compression(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
-} 
+}
 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
 
 static ssize_t show_device_bridge(struct class_device *class_dev, char *buf)
 {
-       struct video_device *vdev = to_video_device(class_dev); 
-       struct usb_usbvision *usbvision = video_get_drvdata(vdev);      
+       struct video_device *vdev = to_video_device(class_dev);
+       struct usb_usbvision *usbvision = video_get_drvdata(vdev);
        return sprintf(buf, "%d\n", usbvision->bridgeType);
-} 
+}
 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
 
 static void usbvision_create_sysfs(struct video_device *vdev)
 {
+       int res;
        if (vdev) {
-               video_device_create_file(vdev, &class_device_attr_version);
-               video_device_create_file(vdev, &class_device_attr_model);
-               video_device_create_file(vdev, &class_device_attr_hue);
-               video_device_create_file(vdev, &class_device_attr_contrast);
-               video_device_create_file(vdev, &class_device_attr_brightness);
-               video_device_create_file(vdev, &class_device_attr_saturation);
-               video_device_create_file(vdev, &class_device_attr_streaming);
-               video_device_create_file(vdev, &class_device_attr_overlay);
-               video_device_create_file(vdev, &class_device_attr_compression);
-               video_device_create_file(vdev, &class_device_attr_bridge);
+               res=video_device_create_file(vdev, &class_device_attr_version);
+               res=video_device_create_file(vdev, &class_device_attr_model);
+               res=video_device_create_file(vdev, &class_device_attr_hue);
+               res=video_device_create_file(vdev, &class_device_attr_contrast);
+               res=video_device_create_file(vdev, &class_device_attr_brightness);
+               res=video_device_create_file(vdev, &class_device_attr_saturation);
+               res=video_device_create_file(vdev, &class_device_attr_streaming);
+               res=video_device_create_file(vdev, &class_device_attr_overlay);
+               res=video_device_create_file(vdev, &class_device_attr_compression);
+               res=video_device_create_file(vdev, &class_device_attr_bridge);
        }
 }
 
@@ -1195,9 +1212,9 @@ void usbvision_osd_stats(struct usb_usbvision *usbvision,
  *
  * Parameters:
  * fullframe:   if TRUE then entire frame is filled, otherwise the procedure
- *             continues from the current scanline.
+ *             continues from the current scanline.
  * pmode       0: fill the frame with solid blue color (like on VCR or TV)
- *             1: Draw a colored grid
+ *             1: Draw a colored grid
  *
  */
 void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe,
@@ -1214,15 +1231,13 @@ void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe,
                printk(KERN_ERR "%s: usbvision == NULL\n", proc);
                return;
        }
-       if ((usbvision->curFrameNum < 0)
-           || (usbvision->curFrameNum >= USBVISION_NUMFRAMES)) {
-               printk(KERN_ERR "%s: usbvision->curFrameNum=%d.\n", proc,
-                      usbvision->curFrameNum);
+       if (usbvision->curFrame == NULL) {
+               printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc);
                return;
        }
 
        /* Grab the current frame */
-       frame = &usbvision->frame[usbvision->curFrameNum];
+       frame = usbvision->curFrame;
 
        /* Optionally start at the beginning */
        if (fullframe) {
@@ -1295,7 +1310,7 @@ static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
                frame = &usbvision->overlay_frame;
        }
        else {
-               frame = &usbvision->frame[usbvision->curFrameNum];
+               frame = usbvision->curFrame;
        }
 
        while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
@@ -1330,7 +1345,6 @@ static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
                frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
                frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
                frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
-               usbvision->curFrame = frame;
        }
        else { // no header found
                PDEBUG(DBG_HEADER, "skipping scratch data, no header");
@@ -1385,7 +1399,7 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
                        usbvision->vid_buf.fmt.bytesperline;
        }
        else {
-               frame  = &usbvision->frame[usbvision->curFrameNum];
+               frame  = usbvision->curFrame;
                f = frame->data + (frame->v4l2_linesize * frame->curline);
        }
 
@@ -1608,7 +1622,7 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
 
        if (overlay) {
                frame  = &usbvision->overlay_frame;
-               imageSize = frame->frmwidth * frame->frmheight; 
+               imageSize = frame->frmwidth * frame->frmheight;
                if (usbvision->overlay_base == NULL) {
                        //video_buffer is not set yet
                        return ParseState_NextFrame;
@@ -1617,27 +1631,27 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
                        usbvision->vid_buf.fmt.bytesperline;
        }
        else {
-               frame  = &usbvision->frame[usbvision->curFrameNum];
-               imageSize = frame->frmwidth * frame->frmheight;                                 
+               frame  = usbvision->curFrame;
+               imageSize = frame->frmwidth * frame->frmheight;
                if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
                     (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) )
-               {       // this is a planar format 
-                                                                      //... v4l2_linesize not used here.
+               {       // this is a planar format
+                                                                      //... v4l2_linesize not used here.
                        f = frame->data + (frame->width * frame->curline);
-               } else 
+               } else
                        f = frame->data + (frame->v4l2_linesize * frame->curline);
-               
+
                if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
-                                                                // get base of u and b planes add halfoffset
-                       
-                       u = frame->data 
-                               + imageSize     
+                                                                // get base of u and b planes add halfoffset
+
+                       u = frame->data
+                               + imageSize
                                + (frame->frmwidth >>1) * frame->curline ;
                        v = u + (imageSize >>1 );
-                       
+
                } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
-                       
-                       v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ; 
+
+                       v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
                        u = v + (imageSize >>2) ;
                }
        }
@@ -1687,7 +1701,7 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
                return ParseState_NextFrame;
        }
 
-       bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
+       bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
        clipmask_index = frame->curline * MAX_FRAME_WIDTH;
 
        scratch_get(usbvision, StripData, StripLen);
@@ -1723,7 +1737,7 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
        if (BlockPos > StripLen) {
                usbvision->stripLenErrors++;
        }
-       
+
        for (Idx = 0; Idx < IdxEnd; Idx++) {
                if((overlay) && (clipped_pixel(clipmask_index))) {
                        f += bytes_per_pixel;
@@ -1741,15 +1755,15 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
                }
                else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
                        *f++ = Y [Idx];
-                       if ( !((  Idx & 0x01  ) | (  frame->curline & 0x01  )) ){ 
-                           
+                       if ( !((  Idx & 0x01  ) | (  frame->curline & 0x01  )) ){
+
 /*                              only need do this for 1 in 4 pixels */
 /*                              intraframe buffer is YUV420 format */
 
                                *u++ = U[Idx >>1];
                                *v++ = V[Idx >>1];
                        }
-                       
+
                }
                else {
                        YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
@@ -1780,12 +1794,12 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
                }
                clipmask_index++;
        }
-       /* Deal with non-integer no. of bytes for YUV420P */ 
-       if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 ) 
-               *pcopylen += frame->v4l2_linesize; 
-       else 
-               *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;  
-  
+       /* Deal with non-integer no. of bytes for YUV420P */
+       if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
+               *pcopylen += frame->v4l2_linesize;
+       else
+               *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
+
        frame->curline += 1;
 
        if (frame->curline >= frame->frmheight) {
@@ -1838,7 +1852,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
                f_odd  = f_even + usbvision->vid_buf.fmt.bytesperline * usbvision->stretch_height;
        }
        else {
-               frame  = &usbvision->frame[usbvision->curFrameNum];
+               frame  = usbvision->curFrame;
                f_even = frame->data + (frame->v4l2_linesize * frame->curline);
                f_odd  = f_even + frame->v4l2_linesize * usbvision->stretch_height;
        }
@@ -1846,7 +1860,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
        /* Make sure there's enough data for the entire line */
        /* In this mode usbvision transfer 3 bytes for every 2 pixels */
        /* I need two lines to decode the color */
-       bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
+       bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
        stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
        clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
        clipmask_odd_index  = clipmask_even_index + MAX_FRAME_WIDTH;
@@ -2093,17 +2107,17 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
        struct usbvision_frame *frame;
        enum ParseState newstate;
        long copylen = 0;
+       unsigned long lock_flags;
 
        if (usbvision->overlay) {
                frame = &usbvision->overlay_frame;
        }
        else {
-               frame = &usbvision->frame[usbvision->curFrameNum];
+               frame = usbvision->curFrame;
        }
 
        PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
 
-
        while (1) {
 
                newstate = ParseState_Out;
@@ -2146,7 +2160,10 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
                        copylen = 0;
                }
                else {
-                       usbvision->curFrameNum = -1;
+                       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
+                       list_move_tail(&(frame->frame), &usbvision->outqueue);
+                       usbvision->curFrame = NULL;
+                       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
                }
                usbvision->frame_num++;
 
@@ -2155,10 +2172,14 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
                        usbvision_osd_stats(usbvision, frame);
 
                /* This will cause the process to request another frame. */
-               if (waitqueue_active(&frame->wq)) {
-                       wake_up_interruptible(&frame->wq);
+               if (waitqueue_active(&usbvision->wait_frame)) {
+                       PDEBUG(DBG_PARSE, "Wake up !");
+                       wake_up_interruptible(&usbvision->wait_frame);
                }
        }
+       else
+               frame->grabstate = FrameState_Grabbing;
+
 
        /* Update the frame's uncompressed length. */
        frame->scanlength += copylen;
@@ -2169,7 +2190,7 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
  * Make all of the blocks of data contiguous
  */
 static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
-                                      struct urb *urb)
+                                         struct urb *urb)
 {
        unsigned char *packet_data;
        int i, totlen = 0;
@@ -2251,51 +2272,70 @@ static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
 
 static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs)
 {
- int errCode = 0;
- int len;
- struct usb_usbvision *usbvision = urb->context;
- int i;
- unsigned long startTime = jiffies;
-
- /* We don't want to do anything if we are about to be removed! */
- if (!USBVISION_IS_OPERATIONAL(usbvision))
-  return;
+       int errCode = 0;
+       int len;
+       struct usb_usbvision *usbvision = urb->context;
+       int i;
+       unsigned long startTime = jiffies;
+       struct usbvision_frame **f;
 
- if (!usbvision->streaming) {
-  PDEBUG(DBG_IRQ, "oops, not streaming, but interrupt");
-  return;
- }
+       /* We don't want to do anything if we are about to be removed! */
+       if (!USBVISION_IS_OPERATIONAL(usbvision))
+               return;
 
- /* Copy the data received into our scratch buffer */
- len = usbvision_compress_isochronous(usbvision, urb);
+       f = &usbvision->curFrame;
 
- usbvision->isocUrbCount++;
- usbvision->urb_length = len;
+       /* Manage streaming interruption */
+       if (usbvision->streaming == Stream_Interrupt) {
+               usbvision->streaming = Stream_Off;
+               if ((*f)) {
+                       (*f)->grabstate = FrameState_Ready;
+                       (*f)->scanstate = ScanState_Scanning;
+               }
+               PDEBUG(DBG_IRQ, "stream interrupted");
+               wake_up_interruptible(&usbvision->wait_stream);
+       }
 
- for (i = 0; i < USBVISION_URB_FRAMES; i++) {
-  urb->iso_frame_desc[i].status = 0;
-  urb->iso_frame_desc[i].actual_length = 0;
- }
-       urb->status = 0;
-       urb->dev = usbvision->dev;
-       errCode = usb_submit_urb (urb, GFP_ATOMIC);
+       /* Copy the data received into our scratch buffer */
+       len = usbvision_compress_isochronous(usbvision, urb);
 
-/* Disable this warning.  By design of the driver. */
-//     if(errCode) {
-//             err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode);
-//     }
+       usbvision->isocUrbCount++;
+       usbvision->urb_length = len;
 
-       /* If we collected enough data let's parse! */
-       if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH) { /* 12 == header_length */
-               /*If we don't have a frame we're current working on, complain */
-               if ((usbvision->curFrameNum >= 0) || (usbvision->overlay))
-                       usbvision_parse_data(usbvision);
-               else {
-                       PDEBUG(DBG_IRQ, "received data, but no one needs it");
-                       scratch_reset(usbvision);
+       if (usbvision->streaming == Stream_On) {
+
+               /* If we collected enough data let's parse! */
+               if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH) { /* 12 == header_length */
+                       /*If we don't have a frame we're current working on, complain */
+                       if((!list_empty(&(usbvision->inqueue))) || (usbvision->overlay)) {
+                               if (!(*f)) {
+                                       (*f) = list_entry(usbvision->inqueue.next,struct usbvision_frame, frame);
+                               }
+                               usbvision_parse_data(usbvision);
+                       }
+                       else {
+                               PDEBUG(DBG_IRQ, "received data, but no one needs it");
+                               scratch_reset(usbvision);
+                       }
                }
        }
+
        usbvision->timeInIrq += jiffies - startTime;
+
+       for (i = 0; i < USBVISION_URB_FRAMES; i++) {
+               urb->iso_frame_desc[i].status = 0;
+               urb->iso_frame_desc[i].actual_length = 0;
+       }
+
+       urb->status = 0;
+       urb->dev = usbvision->dev;
+       errCode = usb_submit_urb (urb, GFP_ATOMIC);
+
+       /* Disable this warning.  By design of the driver. */
+       //      if(errCode) {
+       //              err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode);
+       //      }
+
        return;
 }
 
@@ -2544,7 +2584,6 @@ static int attach_inform(struct i2c_client *client)
        struct usb_usbvision *usbvision;
        struct tuner_setup tun_addr;
        int i;
-       v4l2_std_id stdId;
 
        #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
                usbvision = (struct usb_usbvision *)client->adapter->data;
@@ -2562,16 +2601,14 @@ static int attach_inform(struct i2c_client *client)
        }
        if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
                tun_addr.mode_mask = T_ANALOG_TV;
-                tun_addr.type = usbvision->tuner_type;
-                tun_addr.addr = ADDR_UNSET;
-                client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_addr);
-                
-               call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->input.index);
+               tun_addr.type = usbvision->tuner_type;
+               tun_addr.addr = ADDR_UNSET;
+               client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_addr);
+               call_i2c_clients(usbvision, VIDIOC_INT_RESET, NULL);
+               call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->ctl_input);
+               call_i2c_clients(usbvision, VIDIOC_STREAMON, NULL);
        }
-       // FIXME : need to add a call VIDIOC_S_CTRL for each control
-/*     call_i2c_clients(usbvision, DECODER_SET_PICTURE, &usbvision->vpic); */
-       stdId = usbvision->input.std;
-       call_i2c_clients(usbvision, VIDIOC_S_STD, &stdId);
+       call_i2c_clients(usbvision, VIDIOC_S_STD, &usbvision->tvnorm->id);
 
        PDEBUG(DBG_I2C, "usbvision[%d] attaches %s", usbvision->nr, client->name);
 
@@ -2733,7 +2770,7 @@ static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
        int retval;
        int wrcount = 0;
        int count;
-       int maxLen = 4;
+       int maxLen = 4;
        struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
 
        while (len > 0) {
@@ -2841,7 +2878,7 @@ static int usbvision_init_i2c(struct usb_usbvision *usbvision)
                                break;
                }
                if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
-                       request_module("tuner");
+                       request_module("tuner");
                }
        }
 #endif
@@ -3000,10 +3037,10 @@ static int usbvision_set_output(struct usb_usbvision *usbvision, int width,
                frameRate = FRAMERATE_MAX;
        }
 
-       if (usbvision->input.std & V4L2_STD_625_50) {
-               frameDrop = frameRate * 32 / 25 - 1;
+       if (usbvision->tvnorm->id & V4L2_STD_625_50) {
+               frameDrop = frameRate * 32 / 25 - 1;
        }
-       else if (usbvision->input.std & V4L2_STD_525_60) {
+       else if (usbvision->tvnorm->id & V4L2_STD_525_60) {
                frameDrop = frameRate * 32 / 30 - 1;
        }
 
@@ -3026,6 +3063,40 @@ static int usbvision_set_output(struct usb_usbvision *usbvision, int width,
 }
 
 
+/*
+ * usbvision_empty_framequeues()
+ * prepare queues for incoming and outgoing frames
+ */
+static void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
+{
+       u32 i;
+
+       INIT_LIST_HEAD(&(usbvision->inqueue));
+       INIT_LIST_HEAD(&(usbvision->outqueue));
+
+       for (i = 0; i < USBVISION_NUMFRAMES; i++) {
+               usbvision->frame[i].grabstate = FrameState_Unused;
+               usbvision->frame[i].bytes_read = 0;
+       }
+}
+
+/*
+ * usbvision_stream_interrupt()
+ * stops streaming
+ */
+static int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
+{
+       int ret = 0;
+
+       /* stop reading from the device */
+
+       usbvision->streaming = Stream_Interrupt;
+       ret = wait_event_timeout(usbvision->wait_stream,
+                                (usbvision->streaming == Stream_Off),
+                                msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES));
+       return ret;
+}
+
 /*
  * usbvision_set_compress_params()
  *
@@ -3042,7 +3113,7 @@ static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
        value[2] = 0x00;    // Reg.46 Force intra mode on all new frames
        value[3] = 0x00;    // Reg.47 FORCE_UP <- 0 normal operation (not force)
        value[4] = 0xA2;    // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
-        value[5] = 0x00;    // Reg.49 DVI_YUV This has nothing to do with compression
+       value[5] = 0x00;    // Reg.49 DVI_YUV This has nothing to do with compression
 
        //catched values for NT1004
        // value[0] = 0xFF; // Never apply intra mode automatically
@@ -3140,7 +3211,7 @@ static int usbvision_set_input(struct usb_usbvision *usbvision)
        }
 
 
-       if (usbvision->input.std & V4L2_STD_PAL) {
+       if (usbvision->tvnorm->id & V4L2_STD_PAL) {
                value[0] = 0xC0;
                value[1] = 0x02;        //0x02C0 -> 704 Input video line length
                value[2] = 0x20;
@@ -3149,7 +3220,7 @@ static int usbvision_set_input(struct usb_usbvision *usbvision)
                value[5] = 0x00;        //0x0060 -> 96 Input video h offset
                value[6] = 0x16;
                value[7] = 0x00;        //0x0016 -> 22 Input video v offset
-       } else if (usbvision->input.std & V4L2_STD_SECAM) {
+       } else if (usbvision->tvnorm->id & V4L2_STD_SECAM) {
                value[0] = 0xC0;
                value[1] = 0x02;        //0x02C0 -> 704 Input video line length
                value[2] = 0x20;
@@ -3196,7 +3267,7 @@ static int usbvision_set_input(struct usb_usbvision *usbvision)
                dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv & 0xff;
        }
        else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
-        /* This changes as the fine sync control changes. Further investigation necessary */
+       /* This changes as the fine sync control changes. Further investigation necessary */
                dvi_yuv_value = 0x06;
        }
 
@@ -3313,7 +3384,7 @@ static void usbvision_powerOffTimer(unsigned long data)
        del_timer(&usbvision->powerOffTimer);
        INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off, usbvision);
        (void) schedule_work(&usbvision->powerOffWork);
-       
+
 }
 
 
@@ -3421,7 +3492,7 @@ static int usbvision_setup(struct usb_usbvision *usbvision)
        usbvision_set_input(usbvision);
        usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
        usbvision_restart_isoc(usbvision);
-       
+
        /* cosas del PCM */
        return USBVISION_IS_OPERATIONAL(usbvision);
 }
@@ -3439,7 +3510,7 @@ static int usbvision_init_isoc(struct usb_usbvision *usbvision)
        if (!USBVISION_IS_OPERATIONAL(usbvision))
                return -EFAULT;
 
-       usbvision->curFrameNum = -1;
+       usbvision->curFrame = NULL;
        scratch_reset(usbvision);
 
        /* Alternate interface 1 is is the biggest frame size */
@@ -3508,7 +3579,7 @@ static int usbvision_init_isoc(struct usb_usbvision *usbvision)
                }
        }
 
-       usbvision->streaming = 1;
+       usbvision->streaming = Stream_On;
        PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x", __FUNCTION__, usbvision->video_endp);
        return 0;
 }
@@ -3524,7 +3595,8 @@ static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
 {
        int bufIdx, errCode, regValue;
 
-       if (!usbvision->streaming || (usbvision->dev == NULL))
+       // FIXME : removed the streaming==Stream_Off. This field has not the same signification than before !
+       if (usbvision->dev == NULL)
                return;
 
        /* Unschedule all of the iso td's */
@@ -3532,12 +3604,11 @@ static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
                usb_kill_urb(usbvision->sbuf[bufIdx].urb);
                usb_free_urb(usbvision->sbuf[bufIdx].urb);
                usbvision->sbuf[bufIdx].urb = NULL;
-        }
+       }
 
 
-       PDEBUG(DBG_ISOC, "%s: streaming=0\n", __FUNCTION__);
-       usbvision->streaming = 0;
-                       
+       PDEBUG(DBG_ISOC, "%s: streaming=Stream_Off\n", __FUNCTION__);
+       usbvision->streaming = Stream_Off;
 
        if (!usbvision->remove_pending) {
 
@@ -3557,65 +3628,10 @@ static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
        }
 }
 
-/*
- * usbvision_new_frame()
- *
- */
-static int usbvision_new_frame(struct usb_usbvision *usbvision, int framenum)
-{
-       struct usbvision_frame *frame;
-       int n; //byhec  , width, height;
-
-       /* If we're not grabbing a frame right now and the other frame is */
-       /*  ready to be grabbed into, then use it instead */
-       if (usbvision->curFrameNum != -1)
-               return 0;
-
-       n = (framenum - 1 + USBVISION_NUMFRAMES) % USBVISION_NUMFRAMES;
-       if (usbvision->frame[n].grabstate == FrameState_Ready)
-               framenum = n;
-
-       frame = &usbvision->frame[framenum];
-
-       frame->grabstate = FrameState_Grabbing;
-       frame->scanstate = ScanState_Scanning;
-       frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
-       usbvision->curFrameNum = framenum;
-
-       /*
-        * Normally we would want to copy previous frame into the current one
-        * before we even start filling it with data; this allows us to stop
-        * filling at any moment; top portion of the frame will be new and
-        * bottom portion will stay as it was in previous frame. If we don't
-        * do that then missing chunks of video stream will result in flickering
-        * portions of old data whatever it was before.
-        *
-        * If we choose not to copy previous frame (to, for example, save few
-        * bus cycles - the frame can be pretty large!) then we have an option
-        * to clear the frame before using. If we experience losses in this
-        * mode then missing picture will be black (flickering).
-        *
-        * Finally, if user chooses not to clean the current frame before
-        * filling it with data then the old data will be visible if we fail
-        * to refill entire frame with new data.
-        */
-       if (!(flags & FLAGS_SEPARATE_FRAMES)) {
-               /* This copies previous frame into this one to mask losses */
-               memmove(frame->data, usbvision->frame[1 - framenum].data,
-                       MAX_FRAME_SIZE);
-       } else {
-               if (flags & FLAGS_CLEAN_FRAMES) {
-                       /*This provides a "clean" frame but slows things down */
-                       memset(frame->data, 0, MAX_FRAME_SIZE);
-               }
-       }
-       return 0;
-}
-
-static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int norm)
+static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
 {
        int mode[4];
-       int audio[]= {1, 0, 0, 0};      
+       int audio[]= {1, 0, 0, 0};
        struct v4l2_routing route;
        //channel 0 is TV with audiochannel 1 (tuner mono)
        //channel 1 is Composite with audio channel 0 (line in)
@@ -3623,20 +3639,14 @@ static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int no
        //channel 3 is additional video inputs to the device with audio channel 0 (line in)
 
        RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
-       /* set the new video norm */
-       if (usbvision->input.std != norm) {
-         v4l2_std_id video_command = norm;
-
+       usbvision->ctl_input = channel;
          route.input = SAA7115_COMPOSITE1;
          call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
-         call_i2c_clients(usbvision, VIDIOC_S_STD, &video_command);
-         usbvision->input.std = norm;
-         call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->input.index); //set norm in tuner
-       }
+         call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->ctl_input);
 
        // set the new channel
        // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
-       // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red 
+       // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
 
        switch (usbvision_device_data[usbvision->DevModel].Codec) {
                case CODEC_SAA7113:
@@ -3646,10 +3656,10 @@ static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel, int no
                        else {
                                mode[2] = 7;
                        }
-                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) { 
+                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
                                mode[0] = 0; mode[1] = 2; mode[3] = 3;  // Special for four input devices
                        }
-                       else {  
+                       else {
                                mode[0] = 0; mode[1] = 2; //modes for regular saa7113 devices
                        }
                        break;
@@ -3708,8 +3718,13 @@ static int usbvision_v4l2_open(struct inode *inode, struct file *file)
                        errCode = -ENOMEM;
                }
                else {
+                       spin_lock_init(&usbvision->queue_lock);
+                       init_waitqueue_head(&usbvision->wait_frame);
+                       init_waitqueue_head(&usbvision->wait_stream);
+
                        /* Allocate all buffers */
                        for (i = 0; i < USBVISION_NUMFRAMES; i++) {
+                               usbvision->frame[i].index = i;
                                usbvision->frame[i].grabstate = FrameState_Unused;
                                usbvision->frame[i].data = usbvision->fbuf +
                                    i * MAX_FRAME_SIZE;
@@ -3748,7 +3763,7 @@ static int usbvision_v4l2_open(struct inode *inode, struct file *file)
                                err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
                                errCode = -ENOMEM;
                        }
-                       
+
                }
                if (errCode) {
                        /* Have to free all that memory */
@@ -3809,6 +3824,9 @@ static int usbvision_v4l2_open(struct inode *inode, struct file *file)
        if (errCode) {
        }
 
+       /* prepare queues */
+       usbvision_empty_framequeues(usbvision);
+
        PDEBUG(DBG_IO, "success");
        return errCode;
 }
@@ -3880,6 +3898,8 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
        if (!USBVISION_IS_OPERATIONAL(usbvision))
                return -EFAULT;
 
+       //      if (debug & DBG_IOCTL) v4l_printk_ioctl(cmd);
+
        switch (cmd) {
                case UVIOCSREG:
                {
@@ -3919,7 +3939,20 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_QUERYCAP:
                {
                        struct v4l2_capability *vc=arg;
-                       *vc = usbvision->vcap;
+
+                       memset(vc, 0, sizeof(*vc));
+                       strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
+                       strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
+                               sizeof(vc->card));
+                       strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
+                               sizeof(vc->bus_info));
+                       vc->version = USBVISION_DRIVER_VERSION;
+                       vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
+                               V4L2_CAP_AUDIO |
+                               V4L2_CAP_READWRITE |
+                               V4L2_CAP_STREAMING |
+                               (dga ? (V4L2_FBUF_CAP_LIST_CLIPPING | V4L2_CAP_VIDEO_OVERLAY) : 0) |
+                               (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
                        PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
                        return 0;
                }
@@ -3940,8 +3973,8 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                                case 0:
                                        if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
                                                strcpy(vi->name, "White Video Input");
-                                       } 
-                                       else {  
+                                       }
+                                       else {
                                                strcpy(vi->name, "Television");
                                                vi->type = V4L2_INPUT_TYPE_TUNER;
                                                vi->audioset = 1;
@@ -3950,123 +3983,116 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                                        }
                                        break;
                                case 1:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
                                        if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
                                                strcpy(vi->name, "Green Video Input");
-                                       } 
-                                       else {  
+                                       }
+                                       else {
                                                strcpy(vi->name, "Composite Video Input");
                                        }
                                        vi->std = V4L2_STD_PAL;
                                        break;
                                case 2:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
                                        if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
                                                strcpy(vi->name, "Yellow Video Input");
-                                       } 
-                                       else {  
+                                       }
+                                       else {
                                        strcpy(vi->name, "S-Video Input");
                                        }
                                        vi->std = V4L2_STD_PAL;
                                        break;
                                case 3:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
                                        strcpy(vi->name, "Red Video Input");
                                        vi->std = V4L2_STD_PAL;
-                                       break; 
+                                       break;
                        }
                        PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x", vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
                        return 0;
                }
                case VIDIOC_ENUMSTD:
                {
-                       struct v4l2_standard *vs = arg;
-                       switch(vs->index) {
-                       case 0:
-                               vs->id = V4L2_STD_PAL;
-                               strcpy(vs->name,"PAL");
-                               vs->frameperiod.numerator   = 1;
-                               vs->frameperiod.denominator = 25;
-                               vs->framelines              = 625;
-                               break;
-                       case 1:
-                               vs->id = V4L2_STD_NTSC;
-                               strcpy(vs->name,"NTSC");
-                               vs->frameperiod.numerator   = 1001;
-                               vs->frameperiod.denominator = 30000;
-                               vs->framelines              = 525;
-                               break;
-                       case 2:
-                               vs->id = V4L2_STD_SECAM;
-                               strcpy(vs->name,"SECAM");
-                               vs->frameperiod.numerator   = 1;
-                               vs->frameperiod.denominator = 25;
-                               vs->framelines              = 625;
-                               break;
-                       default:
+                       struct v4l2_standard *e = arg;
+                       unsigned int i;
+                       int ret;
+
+                       i = e->index;
+                       if (i >= TVNORMS)
                                return -EINVAL;
-                       }
+                       ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
+                                                      tvnorms[e->index].name);
+                       e->index = i;
+                       if (ret < 0)
+                               return ret;
                        return 0;
                }
-               case VIDIOC_G_INPUT:
+               case VIDIOC_G_INPUT:
                {
                        int *input = arg;
-                       *input = usbvision->input.index;
+                       *input = usbvision->ctl_input;
                        return 0;
                }
-               case VIDIOC_S_INPUT:
+               case VIDIOC_S_INPUT:
                {
                        int *input = arg;
                        if ((*input >= usbvision->video_inputs) || (*input < 0) )
                                return -EINVAL;
-                       usbvision->input.index = *input;
+                       usbvision->ctl_input = *input;
 
                        down(&usbvision->lock);
-                       usbvision_muxsel(usbvision, usbvision->input.index, usbvision->input.std);
+                       usbvision_muxsel(usbvision, usbvision->ctl_input);
                        usbvision_set_input(usbvision);
                        usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
                        up(&usbvision->lock);
                        return 0;
                }
-               case VIDIOC_G_STD:
+               case VIDIOC_G_STD:
                {
-                       v4l2_std_id *std = arg;
-                       *std = usbvision->input.std;
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%x", (unsigned)*std);
+                       v4l2_std_id *id = arg;
+
+                       *id = usbvision->tvnorm->id;
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
                        return 0;
                }
-               case VIDIOC_S_STD:
+               case VIDIOC_S_STD:
                {
-                       v4l2_std_id *std = arg;
+                       v4l2_std_id *id = arg;
+                       unsigned int i;
+
+                       for (i = 0; i < TVNORMS; i++)
+                               if (*id == tvnorms[i].id)
+                                       break;
+                       if (i == TVNORMS)
+                               for (i = 0; i < TVNORMS; i++)
+                                       if (*id & tvnorms[i].id)
+                                               break;
+                       if (i == TVNORMS)
+                               return -EINVAL;
 
                        down(&usbvision->lock);
-                       usbvision_muxsel(usbvision, usbvision->input.index, *std);
-                       usbvision_set_input(usbvision);
-                       usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
+                       usbvision->tvnorm = &tvnorms[i];
+
+                       call_i2c_clients(usbvision, VIDIOC_S_STD,
+                                        &usbvision->tvnorm->id);
+
                        up(&usbvision->lock);
 
-                       usbvision->input.std = *std;
-                       PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%x", (unsigned)*std);
+                       PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
                        return 0;
                }
-               case VIDIOC_G_TUNER:
+               case VIDIOC_G_TUNER:
                {
                        struct v4l2_tuner *vt = arg;
-                       struct v4l2_tuner status;
 
                        if (!usbvision->have_tuner || vt->index)        // Only tuner 0
                                return -EINVAL;
                        strcpy(vt->name, "Television");
-                       vt->type = V4L2_TUNER_ANALOG_TV;
-                       vt->capability = V4L2_TUNER_CAP_NORM;
-                       vt->rangelow = 0;
-                       vt->rangehigh = ~0;
-                       vt->audmode = V4L2_TUNER_MODE_MONO;
-                       vt->rxsubchans = V4L2_TUNER_SUB_MONO;
-                       call_i2c_clients(usbvision,VIDIOC_G_TUNER,&status);
-                       vt->signal = status.signal;
-
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER");
+                       /* Let clients fill in the remainder of this struct */
+                       call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
                        return 0;
                }
                case VIDIOC_S_TUNER:
@@ -4076,15 +4102,16 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        // Only no or one tuner for now
                        if (!usbvision->have_tuner || vt->index)
                                return -EINVAL;
-                       // FIXME     vt->audmode Radio mode (STEREO/MONO/...)
-                       // vt->reserved Radio freq
-                       // usbvision_muxsel(usbvision, vt->index, vt->mode);
+                       /* let clients handle this */
+                       call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
+
                        PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
                        return 0;
                }
                case VIDIOC_G_FREQUENCY:
                {
                        struct v4l2_frequency *freq = arg;
+
                        freq->tuner = 0; // Only one tuner
                        freq->type = V4L2_TUNER_ANALOG_TV;
                        freq->frequency = usbvision->freq;
@@ -4094,6 +4121,11 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_S_FREQUENCY:
                {
                        struct v4l2_frequency *freq = arg;
+
+                       // Only no or one tuner for now
+                       if (!usbvision->have_tuner || freq->tuner)
+                               return -EINVAL;
+
                        usbvision->freq = freq->frequency;
                        call_i2c_clients(usbvision, cmd, freq);
                        PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
@@ -4118,55 +4150,35 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        // FIXME: void function ???
                        return 0;
                }
-               case VIDIOC_QUERYCTRL:
+               case VIDIOC_QUERYCTRL:
                {
-                       struct v4l2_queryctrl *qc = arg;
-                       switch(qc->id) {
-                       case V4L2_CID_BRIGHTNESS:
-                       case V4L2_CID_HUE:
-                       case V4L2_CID_SATURATION:
-                       case V4L2_CID_CONTRAST:
-                       case V4L2_CID_AUDIO_VOLUME:
-                       case V4L2_CID_AUDIO_MUTE:
-                               return v4l2_ctrl_query_fill_std(qc);
-                               break;
-                       default:
+                       struct v4l2_queryctrl *ctrl = arg;
+                       int id=ctrl->id;
+
+                       memset(ctrl,0,sizeof(*ctrl));
+                       ctrl->id=id;
+
+                       i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
+
+                       if (ctrl->type)
+                               return 0;
+                       else
                                return -EINVAL;
-                       }
-                       return 0;
+
+                       PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
                }
                case VIDIOC_G_CTRL:
                {
                        struct v4l2_control *ctrl = arg;
 
-                       switch (ctrl->id) {
-                       case V4L2_CID_BRIGHTNESS:
-                               ctrl->value = usbvision->brightness;
-                               break;
-                       case V4L2_CID_CONTRAST:
-                               ctrl->value = usbvision->contrast;
-                               break;
-                       case V4L2_CID_SATURATION:
-                               ctrl->value = usbvision->saturation;
-                               break;
-                       case V4L2_CID_HUE:
-                               ctrl->value = usbvision->hue;
-                               break;
-                       case V4L2_CID_AUDIO_VOLUME:
-                               /*      ctrl->value = usbvision->volume; */
-                               break;
-                       case V4L2_CID_AUDIO_MUTE:
-                               ctrl->value = usbvision->AudioMute;
-                               break;
-                       default:
-                               return -EINVAL;
-                       }
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
+                       PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
+                       call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
                        return 0;
                }
                case VIDIOC_S_CTRL:
                {
                        struct v4l2_control *ctrl = arg;
+
                        PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
                        call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
                        return 0;
@@ -4174,11 +4186,27 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_REQBUFS:
                {
                        struct v4l2_requestbuffers *vr = arg;
-                       // FIXME : normally we allocate the requested number of buffers.
-                       // this driver allocates statically the buffers.
-                       vr->count = 2;
-                       if(vr->memory != V4L2_MEMORY_MMAP)
+                       int ret;
+
+                       RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
+
+                       // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
+                       if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
+                          (vr->memory != V4L2_MEMORY_MMAP))
                                return -EINVAL;
+
+                       // FIXME : before this, we must control if buffers are still mapped.
+                       // Then interrupt streaming if so...
+                       if(usbvision->streaming == Stream_On) {
+                               if ((ret = usbvision_stream_interrupt(usbvision)))
+                                   return ret;
+                       }
+
+                       usbvision_empty_framequeues(usbvision);
+
+                       usbvision->curFrame = NULL;
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
                        return 0;
                }
                case VIDIOC_QUERYBUF:
@@ -4186,16 +4214,18 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        struct v4l2_buffer *vb = arg;
                        struct usbvision_frame *frame;
 
-                       // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
+                       // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
+
                        if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
                                return -EINVAL;
                        }
-                       if(vb->index>1)  {
+                       if(vb->index>=USBVISION_NUMFRAMES)  {
                                return -EINVAL;
                        }
+                       // Updating the corresponding frame state
                        vb->flags = 0;
                        frame = &usbvision->frame[vb->index];
-                       if(frame->grabstate == FrameState_Grabbing)
+                       if(frame->grabstate >= FrameState_Ready)
                                vb->flags |= V4L2_BUF_FLAG_QUEUED;
                        if(frame->grabstate >= FrameState_Done)
                                vb->flags |= V4L2_BUF_FLAG_DONE;
@@ -4208,120 +4238,118 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        else {
                                vb->m.offset = MAX_FRAME_SIZE;
                        }
+                       vb->memory = V4L2_MEMORY_MMAP;
+                       vb->field = V4L2_FIELD_NONE;
                        vb->length = MAX_FRAME_SIZE;
                        vb->timestamp = usbvision->frame[vb->index].timestamp;
-                       vb->sequence = usbvision->frame_num;
+                       vb->sequence = usbvision->frame[vb->index].sequence;
                        return 0;
                }
-               case VIDIOC_QBUF: // VIDIOCMCAPTURE + VIDIOCSYNC
+               case VIDIOC_QBUF:
                {
                        struct v4l2_buffer *vb = arg;
                        struct usbvision_frame *frame;
+                       unsigned long lock_flags;
 
                        // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
                        if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
                                return -EINVAL;
                        }
-                       if(vb->index>1)  {
+                       if(vb->index>=USBVISION_NUMFRAMES)  {
                                return -EINVAL;
                        }
 
                        frame = &usbvision->frame[vb->index];
 
-                       if (frame->grabstate == FrameState_Grabbing) {
-                               return -EBUSY;
+                       if (frame->grabstate != FrameState_Unused) {
+                               return -EAGAIN;
                        }
 
-                       usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
-
-                       /* Mark it as ready */
+                       /* Mark it as ready and enqueue frame */
                        frame->grabstate = FrameState_Ready;
+                       frame->scanstate = ScanState_Scanning;
+                       frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
+
                        vb->flags &= ~V4L2_BUF_FLAG_DONE;
 
                        /* set v4l2_format index */
                        frame->v4l2_format = usbvision->palette;
 
-                       return usbvision_new_frame(usbvision, vb->index);
+                       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
+                       list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
+                       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
+
+                       return 0;
                }
                case VIDIOC_DQBUF:
                {
                        struct v4l2_buffer *vb = arg;
-                       int errCode = 0;
+                       int ret;
+                       struct usbvision_frame *f;
+                       unsigned long lock_flags;
 
-                       DECLARE_WAITQUEUE(wait, current);
-                       // FIXME : not the proper way to get the last filled frame
-                       vb->index=-1;
-                       if(usbvision->curFrameNum != -1) vb->index=usbvision->curFrameNum;
-                       else {
-                               if(usbvision->frame[1].grabstate >= FrameState_Done)
-                                       vb->index = 1;
-                               else if(usbvision->frame[0].grabstate >= FrameState_Done)
-                                       vb->index = 0;
-                               // If no FRAME_DONE, look for a FRAME_GRABBING state.
-                               // See if a frame is in process (grabbing), then use it.
-                               if (vb->index == -1) {
-                                       if (usbvision->frame[1].grabstate == FrameState_Grabbing)
-                                               vb->index = 1;
-                                       else if (usbvision->frame[0].grabstate == FrameState_Grabbing)
-                                               vb->index = 0;
-                               }
-                       }
-                       if (vb->index == -1)
+                       if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
                                return -EINVAL;
 
-                       PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF frame=%d, grabstate=%d",
-                                      vb->index, usbvision->frame[vb->index].grabstate);
-
-                       switch (usbvision->frame[vb->index].grabstate) {
-                       case FrameState_Unused:
-                               errCode = -EINVAL;
-                               break;
-                       case FrameState_Grabbing:
-                               add_wait_queue(&usbvision->frame[vb->index].wq, &wait);
-                               current->state = TASK_INTERRUPTIBLE;
-                               while (usbvision->frame[vb->index].grabstate == FrameState_Grabbing) {
-                                       schedule();
-                                       if (signal_pending(current)) {
-                                               remove_wait_queue(&usbvision->frame[vb->index].wq, &wait);
-                                               current->state = TASK_RUNNING;
-                                               return -EINTR;
-                                       }
-                               }
-                               remove_wait_queue(&usbvision->frame[vb->index].wq, &wait);
-                               current->state = TASK_RUNNING;
-                       case FrameState_Ready:
-                       case FrameState_Error:
-                       case FrameState_Done:
-                               errCode = (usbvision->frame[vb->index].grabstate == FrameState_Error) ? -EIO : 0;
-                               vb->memory = V4L2_MEMORY_MMAP;
-                               vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
-                               vb->sequence = usbvision->frame[vb->index].sequence;
-                               usbvision->frame[vb->index].grabstate = FrameState_Unused;
-                               break;
+                       if (list_empty(&(usbvision->outqueue))) {
+                               if (usbvision->streaming == Stream_Off)
+                                       return -EINVAL;
+                               ret = wait_event_interruptible
+                                       (usbvision->wait_frame,
+                                        !list_empty(&(usbvision->outqueue)));
+                               if (ret)
+                                       return ret;
                        }
 
-                       usbvision->frame[vb->index].grabstate = FrameState_Unused;
-                       return errCode;
+                       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
+                       f = list_entry(usbvision->outqueue.next,
+                                      struct usbvision_frame, frame);
+                       list_del(usbvision->outqueue.next);
+                       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
+
+                       f->grabstate = FrameState_Unused;
+
+                       vb->memory = V4L2_MEMORY_MMAP;
+                       vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
+                       vb->index = f->index;
+                       vb->sequence = f->sequence;
+
+                       return 0;
                }
                case VIDIOC_STREAMON:
                {
                        int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
-                       call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);      
+
+                       if (list_empty(&usbvision->inqueue))
+                               return -EINVAL;
+
+                       usbvision->streaming = Stream_On;
+
+                       call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
+
                        return 0;
                }
                case VIDIOC_STREAMOFF:
                {
+                       int *type = arg;
                        int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
-                       down(&usbvision->lock);
-                       // Stop all video streamings
-                       call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
-                       usbvision->frame_num = -1;
-                       usbvision->frame[0].grabstate = FrameState_Unused;
-                       usbvision->frame[1].grabstate = FrameState_Unused;
-                       up(&usbvision->lock);
+
+                       if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+                               return -EINVAL;
+
+                       if(usbvision->streaming == Stream_On) {
+                               usbvision_stream_interrupt(usbvision);
+                               // Stop all video streamings
+                               call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
+                       }
+                       usbvision_empty_framequeues(usbvision);
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
                        return 0;
                }
-               case VIDIOC_G_FBUF:
+               case VIDIOC_G_FBUF:
                {
                        struct v4l2_framebuffer *vb = arg;
 
@@ -4384,10 +4412,11 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_ENUM_FMT:
                {
                        struct v4l2_fmtdesc *vfd = arg;
+
                        if ( (dga == 0) &&
                             (vfd->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
                             (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) { 
+                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
                                return -EINVAL;
                        }
                        if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
@@ -4401,11 +4430,11 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_G_FMT:
                {
                        struct v4l2_format *vf = arg;
-      
+
                        if ( (dga == 0) &&
                             (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
                             (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) { 
+                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
                                return -EINVAL;
                        }
                        down(&usbvision->lock);
@@ -4415,7 +4444,7 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                               vf->fmt.win.w.left, vf->fmt.win.w.top, vf->fmt.win.w.width, vf->fmt.win.w.height, vf->fmt.win.chromakey, vf->fmt.win.clipcount);
                        return 0;
                }
-               case VIDIOC_S_FMT:
+               case VIDIOC_S_FMT:
                {
                        struct v4l2_format *vf = arg;
                        struct v4l2_clip *vc=NULL;
@@ -4424,7 +4453,7 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                        if ( (dga == 0) &&
                             (vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) &&
                             (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) { 
+                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
                                return -EINVAL;
                        }
                        if(vf->type == V4L2_BUF_TYPE_VIDEO_OVERLAY) {
@@ -4516,10 +4545,10 @@ static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
                case VIDIOC_OVERLAY:
                {
                        int *v = arg;
-      
+
                        if ( (dga == 0) &&
                             (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) { 
+                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
                                PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY  DGA disabled");
                                return -EINVAL;
                        }
@@ -4562,7 +4591,7 @@ static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
        int frmx = -1;
        int rc = 0;
        struct usbvision_frame *frame;
-
+return -EINVAL;
        PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
 
        if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
@@ -4597,7 +4626,8 @@ static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
 
        // If no frame is active, start one.
        if (frmx == -1)
-               usbvision_new_frame(usbvision, frmx = 0);
+       // FIXME: enqueue all inqueue...
+       /*              usbvision_new_frame(usbvision, frmx = 0); */
 
        frame = &usbvision->frame[frmx];
 
@@ -4607,7 +4637,7 @@ static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
                goto usbvision_v4l2_read_done;
        }
        PDEBUG(DBG_IO, "Waiting frame grabbing");
-       rc = wait_event_interruptible(frame->wq, (frame->grabstate == FrameState_Done) || 
+       rc = wait_event_interruptible(usbvision->wait_frame, (frame->grabstate == FrameState_Done) ||
                                                 (frame->grabstate == FrameState_Error));
        if (rc) {
                goto usbvision_v4l2_read_done;
@@ -4615,9 +4645,10 @@ static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
 
        if (frame->grabstate == FrameState_Error) {
                frame->bytes_read = 0;
-               if (usbvision_new_frame(usbvision, frmx)) {
-                       err("%s: usbvision_new_frame() failed", __FUNCTION__);
-               }
+               // FIXME: enqueue all inqueue...
+/*             if (usbvision_new_frame(usbvision, frmx)) { */
+/*                     err("%s: usbvision_new_frame() failed", __FUNCTION__); */
+/*             } */
                goto restart;
        }
 
@@ -4642,8 +4673,9 @@ static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
 
                /* Mark it as available to be used again. */
                usbvision->frame[frmx].grabstate = FrameState_Unused;
-               if (usbvision_new_frame(usbvision, frmx ? 0 : 1))
-                       err("%s: usbvision_new_frame() failed", __FUNCTION__);
+               // FIXME enqueue another frame
+/*             if (usbvision_new_frame(usbvision, frmx ? 0 : 1)) */
+/*                     err("%s: usbvision_new_frame() failed", __FUNCTION__); */
        }
 
 usbvision_v4l2_read_done:
@@ -4668,11 +4700,11 @@ static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
 
        pos = (unsigned long) usbvision->fbuf;
        while (size > 0) {
-               
+
 // Really ugly....
-       #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)                           //Compatibility for 2.6.10+ kernels
+       #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)                           //Compatibility for 2.6.10+ kernels
                page = vmalloc_to_pfn((void *)pos);
-               if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { 
+               if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
                        return -EAGAIN;
                }
        #else                                                                      //Compatibility for 2.6.0 - 2.6.9 kernels
@@ -4789,7 +4821,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                /***************************
                 * V4L2 IOCTLs             *
                 ***************************/
-               case VIDIOC_QUERYCAP:
+               case VIDIOC_QUERYCAP:
                {
                        struct v4l2_capability *vc=arg;
                        memset(vc, 0, sizeof(struct v4l2_capability));
@@ -4801,7 +4833,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__);
                        return 0;
                }
-               case VIDIOC_QUERYCTRL:
+               case VIDIOC_QUERYCTRL:
                {
                        struct v4l2_queryctrl *qc = arg;
                        switch(qc->id)
@@ -4815,7 +4847,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                                }
                        return 0;
                }
-                       case VIDIOC_G_CTRL:
+               case VIDIOC_G_CTRL:
                {
                        struct v4l2_control *ctrl = arg;
                        PDEBUG(DBG_IOCTL, "VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
@@ -4831,7 +4863,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        }
                        return 0;
                }
-               case VIDIOC_S_CTRL:
+               case VIDIOC_S_CTRL:
                {
                        struct v4l2_control *ctrl = arg;
                        call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
@@ -4839,7 +4871,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_RIO, "%s: VIDIOC_S_CTRL id=%x value=%x", __FUNCTION__,ctrl->id,ctrl->value);
                        return 0;
                }
-               case VIDIOC_G_TUNER:
+               case VIDIOC_G_TUNER:
                {
                        struct v4l2_tuner *vt = arg;
 
@@ -4860,7 +4892,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_RIO, "%s: VIDIOC_G_TUNER signal=%d", __FUNCTION__, vt->signal);
                        return 0;
                }
-               case VIDIOC_S_TUNER:
+               case VIDIOC_S_TUNER:
                {
                        struct v4l2_tuner *vt = arg;
 
@@ -4871,7 +4903,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_RIO, "%s: VIDIOC_S_TUNER", __FUNCTION__);
                        return 0;
                }
-               case VIDIOC_G_AUDIO:
+               case VIDIOC_G_AUDIO:
                {
                        struct v4l2_audio *va = arg;
                        memset(va,0, sizeof(va));
@@ -4880,7 +4912,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
                        return 0;
                }
-               case VIDIOC_S_AUDIO:
+               case VIDIOC_S_AUDIO:
                {
                        struct v4l2_audio *v = arg;
                        if(v->index) {
@@ -4890,7 +4922,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        // FIXME: void function ???
                        return 0;
                }
-               case VIDIOC_G_FREQUENCY:
+               case VIDIOC_G_FREQUENCY:
                {
                        struct v4l2_frequency *freq = arg;
                        freq->tuner = 0; // Only one tuner
@@ -4899,7 +4931,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                        PDEBUG(DBG_RIO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
                        return 0;
                }
-               case VIDIOC_S_FREQUENCY:
+               case VIDIOC_S_FREQUENCY:
                {
                        struct v4l2_frequency *freq = arg;
                        usbvision->freq = freq->frequency;
@@ -4911,7 +4943,7 @@ static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
                /***************************
                 * V4L1 IOCTLs             *
                 ***************************/
-               case VIDIOCGCAP:
+               case VIDIOCGCAP:
                {
                        struct video_capability *vc = arg;
 
@@ -5221,22 +5253,12 @@ static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
 
 static void usbvision_configure_video(struct usb_usbvision *usbvision)
 {
-       int model;
+       int model,i;
 
        if (usbvision == NULL)
                return;
-               
-       model = usbvision->DevModel;
 
-       RESTRICT_TO_RANGE(init_brightness, 0, 255);
-       RESTRICT_TO_RANGE(init_contrast, 0, 255);
-       RESTRICT_TO_RANGE(init_saturation, 0, 255);
-       RESTRICT_TO_RANGE(init_hue, 0, 255);
-       
-       usbvision->saturation = init_saturation << 8;
-       usbvision->hue = init_hue << 8;
-       usbvision->brightness = init_brightness << 8;
-       usbvision->contrast = init_contrast << 8;
+       model = usbvision->DevModel;
        usbvision->depth = 24;
        usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
 
@@ -5248,17 +5270,26 @@ static void usbvision_configure_video(struct usb_usbvision *usbvision)
 
        memset(&usbvision->vcap, 0, sizeof(usbvision->vcap));
        strcpy(usbvision->vcap.driver, "USBVision");
-       usbvision->vcap.capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
+       strlcpy(usbvision->vcap.bus_info, usbvision->dev->dev.bus_id,
+               sizeof(usbvision->vcap.bus_info));
+       usbvision->vcap.capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
                (dga ? (V4L2_FBUF_CAP_LIST_CLIPPING | V4L2_CAP_VIDEO_OVERLAY) : 0) |
                (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
        usbvision->vcap.version = USBVISION_DRIVER_VERSION;         /* version */
 
-       usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
 
-       memset(&usbvision->input, 0, sizeof(usbvision->input));
-       usbvision->input.tuner = 0;
-       usbvision->input.type = V4L2_INPUT_TYPE_TUNER|VIDEO_TYPE_CAMERA;
-       usbvision->input.std = usbvision_device_data[model].VideoNorm;
+       for (i = 0; i < TVNORMS; i++)
+               if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
+                       break;
+       if (i == TVNORMS)
+               i = 0;
+       usbvision->tvnorm = &tvnorms[i];        /* set default norm */
+       call_i2c_clients(usbvision, VIDIOC_S_STD,
+                        &usbvision->tvnorm->id);
+
+       usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
+       usbvision->ctl_input = 0;
+/*     usbvision_muxsel(usbvision, usbvision->ctl_input); */
 
        /* This should be here to make i2c clients to be able to register */
        usbvision_audio_off(usbvision); //first switch off audio
@@ -5391,7 +5422,7 @@ static void usbvision_unregister_video(struct usb_usbvision *usbvision)
                }
                usbvision->vbi = NULL;
        }
-       
+
        // Radio Device:
        if (usbvision->rdev) {
                PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
@@ -5442,7 +5473,7 @@ static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
                }
                info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
        }
-       // vbi Device: 
+       // vbi Device:
        if (usbvision_device_data[usbvision->DevModel].vbi) {
                usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
                if (usbvision->vdev == NULL) {
@@ -5473,7 +5504,6 @@ static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
 {
        struct usb_usbvision *usbvision;
-       int FrameIdx;
 
        if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
                goto err_exit;
@@ -5481,10 +5511,6 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
 
        usbvision->dev = dev;
 
-       for (FrameIdx = 0; FrameIdx < USBVISION_NUMFRAMES; FrameIdx++) {
-               init_waitqueue_head(&usbvision->frame[FrameIdx].wq);
-       }
-       init_waitqueue_head(&usbvision->overlay_frame.wq);
        init_MUTEX(&usbvision->lock);   /* to 1 == available */
 
        // prepare control urb for control messages during interrupts
@@ -5493,7 +5519,7 @@ static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
                goto err_exit;
        }
        init_waitqueue_head(&usbvision->ctrlUrb_wq);
-       init_MUTEX(&usbvision->ctrlUrbLock);
+       init_MUTEX(&usbvision->ctrlUrbLock);    /* to 1 == available */
 
        init_timer(&usbvision->powerOffTimer);
        usbvision->powerOffTimer.data = (long) usbvision;
@@ -5569,14 +5595,13 @@ static int __devinit usbvision_probe(struct usb_interface *intf, const struct us
 
        PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
                                        dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
-
        /* Is it an USBVISION video dev? */
        model = 0;
        for(model = 0; usbvision_device_data[model].idVendor; model++) {
-               if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) { 
+               if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
                        continue;
                }
-               if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) { 
+               if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
                        continue;
                }
 
@@ -5605,7 +5630,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf, const struct us
        }
 
        usb_get_dev(dev);
-       
+
        if ((usbvision = usbvision_alloc(dev)) == NULL) {
                err("%s: couldn't allocate USBVision struct", __FUNCTION__);
                return -ENOMEM;
@@ -5615,7 +5640,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf, const struct us
        }
        else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
                usbvision->bridgeType = BRIDGE_NT1005;
-       }               
+       }
        else {
                usbvision->bridgeType = BRIDGE_NT1003;
        }
@@ -5647,7 +5672,7 @@ static int __devinit usbvision_probe(struct usb_interface *intf, const struct us
 
 
        usb_set_intfdata (intf, usbvision);
-       usbvision_create_sysfs(usbvision->vdev);        
+       usbvision_create_sysfs(usbvision->vdev);
 
        PDEBUG(DBG_PROBE, "success");
        return 0;
@@ -5688,6 +5713,9 @@ static void __devexit usbvision_disconnect(struct usb_interface *intf)
        usb_put_dev(usbvision->dev);
        usbvision->dev = NULL;  // USB device is no more
 
+       wake_up_interruptible(&usbvision->wait_frame);
+       wake_up_interruptible(&usbvision->wait_stream);
+
        up(&usbvision->lock);
 
        if (usbvision->user) {
@@ -5722,7 +5750,7 @@ void customdevice_process(void)
        if(CustomDevice)
        {
                char *parse=CustomDevice;
-       
+
                PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
 
                /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
@@ -5741,9 +5769,9 @@ void customdevice_process(void)
                usbvision_device_data[0].X_Offset;
                usbvision_device_data[0].Y_Offset;
                usbvision_device_data[0].Dvi_yuv;
-               usbvision_device_data[0].ModelString;
+               usbvision_device_data[0].ModelString;
                */
-               
+
                rmspace(parse);
                usbvision_device_data[0].ModelString="USBVISION Custom Device";
 
@@ -5850,12 +5878,12 @@ static int __init usbvision_init(void)
        PDEBUG(DBG_FUNC, "FUNC    debugging is enabled");
        PDEBUG(DBG_I2C,  "I2C     debugging is enabled");
 
-       /* disable planar mode support unless compression enabled */ 
-       if (isocMode != ISOC_MODE_COMPRESS ) { 
+       /* disable planar mode support unless compression enabled */
+       if (isocMode != ISOC_MODE_COMPRESS ) {
                // FIXME : not the right way to set supported flag
                usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
                usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
-       } 
+       }
 
        customdevice_process();