V4L/DVB (4937): Usbvision cleanup and code reorganization
authorThierry MERLE <thierry.merle@free.fr>
Mon, 4 Dec 2006 11:31:45 +0000 (08:31 -0300)
committerMauro Carvalho Chehab <mchehab@infradead.org>
Sun, 10 Dec 2006 11:22:49 +0000 (09:22 -0200)
- removal of overlay stuff
- reorganization of functions in 3 files:
    * usbvision-i2c for I2C-related stuff
    * usbvision-video for v4l2 entry points
    * usbvision-core for all peripheral controls and utilities

Signed-off-by: Thierry MERLE <thierry.merle@free.fr>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
drivers/media/video/usbvision/Makefile
drivers/media/video/usbvision/usbvision-cards.c
drivers/media/video/usbvision/usbvision-core.c
drivers/media/video/usbvision/usbvision-i2c.c
drivers/media/video/usbvision/usbvision-i2c.h [deleted file]
drivers/media/video/usbvision/usbvision-video.c [new file with mode: 0644]
drivers/media/video/usbvision/usbvision.h

index ebdf78a19280a33646adecbf9c21d75d1c6f9cfb..9ac92a80c645442ecba5d038e75b9479bebb05d6 100644 (file)
@@ -1,4 +1,4 @@
-usbvision-objs  := usbvision-core.o usbvision-i2c.o usbvision-cards.o
+usbvision-objs  := usbvision-core.o usbvision-video.o usbvision-i2c.o usbvision-cards.o
 
 obj-$(CONFIG_VIDEO_USBVISION) += usbvision.o
 
index 16c8890bf9bb3626fcc9be447cdcc8468ee7fda5..f1fb9e752b5d09b85d920b3e3a5aeceeb2cc696d 100644 (file)
@@ -30,7 +30,6 @@
 #include "usbvision.h"
 
 /* Supported Devices: A table for usbvision.c*/
-
 struct usbvision_device_data_st  usbvision_device_data[] = {
        {0xFFF0, 0xFFF0, -1, CODEC_SAA7111, 3, V4L2_STD_NTSC,  1, 1, 1, 1, TUNER_PHILIPS_NTSC_M,       -1, -1, -1, -1, -1, "Custom Dummy USBVision Device"},  
        {0x0A6F, 0x0400, -1, CODEC_SAA7113, 4, V4L2_STD_NTSC,  1, 0, 1, 0, 0,                          -1, -1, -1, -1, -1, "Xanboo"}, 
index 0eb3ddcd0f3ea58a4ee8fcf4e32bf109a2fac786..cf787d8ab6d344df5e60dab163dcc4edadb11e5a 100644 (file)
@@ -1,11 +1,12 @@
 /*
- * USB USBVISION Video device driver 0.9.8.3cvs (For Kernel 2.4.19-2.4.32 + 2.6.0-2.6.16)
- *
- *
+ * usbvision-core.c - driver for NT100x USB video capture devices
+ * 
  *
  * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
+ *                         Dwaine Garden <dwainegarden@rogers.com>
  *
  * This module is part of usbvision driver project.
+ * Updates to driver completed by Dwaine P. Garden
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * Let's call the version 0.... until compression decoding is completely
- * implemented.
- *
- * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
- * It was based on USB CPiA driver written by Peter Pregler,
- * Scott J. Bertin and Johannes Erdfelt
- * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
- * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
- * Updates to driver completed by Dwaine P. Garden
- *
- * History:
- *
- * Mar. 2000 - 15.12.2000:  (0.0.0 - 0.2.0)
- *     Several alpha drivers and the first beta.
- *
- * Since Dec. 2000:  (0.2.1) or (v2.1)
- *     Code changes or updates by Dwaine Garden and every other person.
- *
- *     Added: New Hauppauge TV device  Vendor  ID: 0x0573
- *                                     Product ID: 0x4D01
- *            (Thanks to Giovanni Garberoglio)
- *
- *     Added: UK Hauppauge WinTV-USB   Vendor  ID: 0x0573
- *                                     Product ID: 0x4D02
- *            (Thanks to Derek Freeman-Jones)
- *
- * Feb, 2001 - Apr 08, 2001:  (0.3.0)
- *     - Some fixes. Driver is now more stable.
- *     - Scratch is organized as ring-buffer now for better performance
- *     - DGA (overlay) is now supported.
- *       !!!!Danger!!!! Clipping is not yet implemented. Your system will
- *       crash if your video window leaves the screen!!!
- *     - Max. Framesize is set to 320x240. There isn't more memory on the
- *       nt1003 video device for the FIFO.
- *     - Supported video palettes: RGB565, RGB555, RGB24, RGB32
- *
- *
- * Apr 15, 2001:  (0.3.1-test...)
- *     - Clipping is implemented
- *     - NTSC is now coloured (Thanks to Dwaine Garden)
- *     - Added SECAM colour detection in saa7111-new
- *     - Added: French Hauppauge WinTV USB  Vendor ID: 0x0573
- *                                          Product ID: 0x4D03
- *              (Thanks to Julius Hrivnac)
- *     - Added: US Hauppauge WINTV USB  Vendor ID: 0x0573
- *                                      Product ID: 0x4D00
- *              (Thanks to  Derrick J Brashear)
- *     - Changes for adding new devices. There's now a table in usbvision.h.
- *       Adding your devices data to the usbvision_device_data table is all
- *       you need to add a new device.
- *
- * May 11, 2001: (0.3.2-test...) (Thanks to Derek Freeman-Jones)
- *     - Support YUV422 raw format for people with hardware scaling.
- *     - Only power on the device when opened (use option PowerOnAtOpen=0 to disable it).
- *     - Turn off audio so we can listen to Line In.
- *
- * July 5, 2001 - (Patch the driver to run with Kernel 2.4.6)
- *     - Fixed a problem with the number of parameters passed to video_register_device.
- *
- * July 6, 2001 - Added: HAUPPAUGE WINTV-USB FM USA Vendor  ID: 0x0573
- *                                                     Product ID: 0x4D10
- *       (Thanks to Braddock Gaskill)
- *                Added: USBGear USBG-V1 resp. HAMA USB
- *                                      Vendor  ID: 0x0573
- *                                      Product ID: 0x0003
- *       (Thanks to Bradley A. Singletary and Juergen Weigert)
- *
- * Jan 24, 2002 - (0.3.3-test...)
- *     - Moved all global variables that are device specific the usb_usbvision struct
- *     - Fixed the 64x48 unchangable image in xawtv when starting it with overlay
- *     - Add VideoNorm and TunerType to the usb_device_data table
- *     - Checked the audio channels and mute for HAUPPAUGE WinTV USB FM
- *     - Implemented the power on when opening the device. But some software opens
- *       the device several times when starting. So the i2c parts are just registered
- *       by an open, when they become deregistered by the next close. You can speed
- *       up tuner detection, when adding "options tuner addr=your_addr" to /etc/modules.conf
- *     - Begin to resize the frame in width and height. So it will be possible to watch i.e.
- *       384x288 pixels at 23 fps.
- *
- * Feb 10, 2002
- *     - Added radio device
- *
- *
- * Jul 30, 2002 - (Thanks Cameron Maxwell)
- *     - Changes to usbvision.h --fixed usbvision device data structure, incorrectly had (0x0573, 0x4d21) for WinTV-USB II, should be 0x4d20.
- *     - Changes for device WinTV-USB II (0x0573. 0x4D21).  It does not have a FM tuner.
- *     - Added the real device HAUPPAUGE WINTV-USB II (PAL) to the device structure in usbvision.h.
- *     - Changes to saa7113-new, the video is 8 bit data for the Phillips SAA7113 not 16bit like SAA7111.
- *     - Tuned lots of setup registers for the Phillips SAA7113 video chipset.
- *     - Changes to the supplied makefile. (Dwaine Garden) Still needs to be fixed so it will compile modules on different distrubutions.
- *
- *
- * Aug 10, 2002 - (Thanks Mike Klinke)
- *     - Changes to usbvision.txt -- Fixed instructions on the location to copy the contents of the tgz file.
- *     - Added device WinTV-USB FM Model 621 (0x0573. 0x4D30). There is another device which carries the same name. Kept that device in the device structure.
- *
- * Aug 12, 2002 - Dwaine Garden
- *     - Added the ability to read the NT100x chip for the MaxISOPacketLength and USB Bandwidth
- *       Setting of the video device.
- *     - Adjustments to the SAA7113H code for proper video output.
- *     - Changes to usbvision.h, so all the devices with FM tuners are working.
- *
- * Feb 10, 2003 - Joerg Heckenbach
- *     - fixed endian bug for Motorola PPC
- *
- * Feb 13, 2003 - Joerg Heckenbach
- *     - fixed Vin_Reg setting and presetting from usbvision_device_data()
- *
- * Apr 19, 2003 - Dwaine Garden
- *     - Fixed compiling errors under RedHat v9.0. from uvirt_to_kva and usbvision_mmap. (Thanks Cameron Maxwell)
- *     - Changed pte_offset to pte_offset_kernel.
- *     - Changed remap_page_range and added additional parameter to function.
- *     - Change setup parameters for the D-Link V100 USB device
- *     - Added a new device to the usbvision driver.  Pinnacle Studio PCTV USB (PAL) 0x2304 0x0110
- *     - Screwed up the sourceforge.net cvs respository!  8*)
- *
- * Apr 22, 2002 - Dwaine Garden
- *     - Added a new device to the usbvision driver. Dazzle DVC-80 (PAL) 0x07d0 0x0004. (Thanks Carl Anderson)
- *     - Changes to some of the comments.
- *
- * June 06, 2002 - Ivan, Dwaine Garden
- *     - Ivan updates for fine tuning device parameters without driver recompiling. (Ivan)
- *     - Changes to some of the comments. (Dwaine Garden)
- *     - Changes to the makefile - Better CPU settings. (Ivan)
- *     - Changes to device Hauppauge WinTv-USB III (PAL) FM Model 568 - Fine tuning parameters (Ivan)
- *
- *
- * Oct 16, 2003 - 0.9.0 - Joerg Heckenbach
- *     - Implementation of the first part of the decompression algorithm for intra frames.
- *       The resolution has to be 320x240. A dynamic adaption of compression deepth is
- *       missing yet.
- *
- * Oct 22, 2003 - 0.9.1 - Joerg Heckenbach
- *     - Implementation of the decompression algorithm for inter frames.
- *       The resolution still has to be 320x240.
- *
- * Nov 2003 - Feb 2004 - 0.9.2 to 0.9.3 - Joerg Heckenbach
- *     - Implement last unknown compressed block type. But color is still noisy.
- *     - Finding criteria for adaptive compression adjustment.
- *     - Porting to 2.6 kernels, but still working under 2.4
- *
- * Feb 04, 2004 - 0.9.4 Joerg Heckenbach
- *     - Found bug in color decompression. Color is OK now.
- *
- * Feb 09, 2004 - 0.9.5 Joerg Heckenbach
- *     - Add auto-recognition of chip type NT1003 or NT1004.
- *     - Add adaptive compression adjustment.
- *     - Patched saa7113 multiplexer switching (Thanks to Orlando F.S. Bordoni)
- *
- * Feb 24, 2004 - 0.9.6 Joerg Heckenbach
- *     - Add a timer to wait before poweroff at close, to save start time in
- *       some video applications
- *
- * Mar 4, 2004 - 0.9.6 Dwaine Garden
- *     - Added device Global Village GV-007 (NTSC) to usbvision.h (Thanks to Abe Skolnik)
- *     - Forgot to add this device to the driver. 8*)
- *
- * 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
- *
- * 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.
- *     - Thanks Hal.....
- *
- * July 30, 2004 - 0.9.6 Dwaine Garden
- *     - Patch was submitted by Tobias Diaz to fix Model ID mismatch in usbvision.h.
- *     - Thanks.....
- *
- * August 12, 2004 - 0.9.6 Dwaine Garden
- *     - Updated the readme file so people could install the driver under the configuration file for kernel 2.6.x recompiles.  Now people can use make xconfig!
- *     - Added new device "Camtel Technology Corp TVB330-USB FM".
- *     - Sourceforge.net CVS has been updated with all the changes.
- *
- * August 20, 2004 - 0.9.7 Dwaine Garden
- *     - Added Device "Hauppauge USB Live Model 600"
- *     - Fixed up all the devices which did not have a default tuner type in usbvision.h.  It's best guess, at least until someone with the device tells me otherwise.
- *     - Sourceforge.net CVS has been updated with all the changes.
- *     - Clean up the driver.
- *
- * September 13, 2004 - 0.9.8 Dwaine Garden
- *     - 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*(
- *
- * November 15, 2004 - 0.9.8 Dwaine Garden
- *     - Release new tar - 0.9.8 on sourceforge.net
- *     - Added some new devices to usbvision.h WinTV USB Model 602 40201 Rev B282, Hauppague WinTV USB Model 602 40201 Rev B285
- *     - Added better compatibility for 2.6.x kernels.
- *     - Hardware full screen scaling in grabdisplay mode.
- *     - Better support for sysfs.  More code to follow for both video device and radio device.        Device information is located at /sys/class/video4linux/video0
- *     - Added module_param so loaded module parameters are displayed in sysfs.  Driver parameters should show up in /sys/module/usbvision
- *     - Adjusted the SAA7111 registers to match the 2.6.x kernel SAA7111 code. Thanks to the person which helped test.
- *     - Changed to wait_event_interruptible. For all the people running Fedora 2.
- *     - Added some screenshots of actual video captures on sourceforge.net.
- *
- * November 24, 2004 - 0.9.8.1cvs Dwaine Garden
- *     - Added patch to check for palette and format in VIDIOCSPICT.  Helix Producer should work fine with the driver now.  Thanks Jason Simpson
- *     - Two device description changes and two additions for the maintainer of usb.ids.
- *
- * December 2, 2004 - 0.9.8.1cvs Dwaine Garden
- *     - Added patch for YUV420P and YUV422P video output.  Thanks to Alex Smith.
- *     - 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 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.
- *     - Changes are related to kernel changes for 2.6.10. (Fedora 4)
- *
- * February 2, 2005 - 0.9.8.1cvs Dwaine Garden
- *     - Added a new device to usbvision.h Dazzle DVC 50.  Thanks to Luiz S.
- *
- * March 29, 2005 - 0.9.8.1cvs Dwaine Garden
- *     - Fixed compile error with saa7113 under kernels 2.6.11+
- *     - Added module parameter to help people with Black and White output with using s-video input.  Some cables and input device are wired differently.
- *     - Removed the .id from the i2c usbvision template.  There was a change to the i2c with kernels 2.6.11+.
- *
- * April 9, 2005 - 0.9.8.1cvs Dwaine Garden
- *     - Added in the 2.4 and 2.6 readme files the SwitchSVideoInput parameter information.  This will help people setup the right values for the parameter.
- *       If your device experiences Black and White images with the S-Video Input.  Set this parameter to 1 when loading the module.
- *     - Replaced the wrong 2.6 readme file.  I lost the right version.  Someone sent me the right version by e-mail.  Thanks.
- *     - Released new module version on sourceforge.net.  So everyone can enjoy all the fixes and additional device support.
- *
- * 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*)
- *
- * 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.
- *       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.
- *       (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.
- *
- * June 15, 2005 - 0.9.8.2cvs Dwaine
- *     - 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.
- *     - 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.
- *
- * July 6, 2005 - 0.9.8.2cvs Dwaine
- *     - 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.
- *
- * 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.
- *     - Thanks to cipe007......
- *
- * May 19, 2006 - 0.9.8.3cvs Dwaine
- *     - Patched usbvision.c and i2c-algo.c so they will compile with kernel 2.6.16
- *     - Adjust device "Pinnacle Studio PCTV USB (PAL) FM" values in usbvision.h
- *
- * May 24, 2006 - 0.9.8.3cvs Dwaine
- *     -Pinnacle Studio PCTV USB (NTSC) FM uses saa7111, not saa7113 like first thought.
- *     -Updated usbvision.h
- *
- * Aug 15, 2006 - 0.9.8.3cvs Dwaine
- *     -Added saa711x module into cvs, since the newer saa7115 module in newer kernels is v4l2.  The usbvision driver is only v4l.
- *     -Updated makefile to put compiled modules into correct location.
- *
- * Aug 21, 2006 - 0.9.8.3cvs Dwaine
- *     -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:
- *     - use submit_urb for all setup packets
- *     - Fix memory settings for nt1004. It is 4 times as big as the
- *       nt1003 memory.
- *     - Add audio on endpoint 3 for nt1004 chip.  Seems impossible, needs a codec interface.  Which one?
- *     - Clean up the driver.
- *     - optimization for performance.
- *     - Add Videotext capability (VBI).  Working on it.....
- *     - Check audio for other devices
- *     - Add v4l2 interface
- *
  */
 
 #include <linux/kernel.h>
 #include <linux/video_decoder.h>
 #include <linux/i2c.h>
 
-#define USBVISION_DRIVER_VERSION_MAJOR 0
-#define USBVISION_DRIVER_VERSION_MINOR 8
-#define USBVISION_DRIVER_VERSION_PATCHLEVEL 0
-
-#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/tuner.h>
 
 #include "usbvision.h"
 
-#define DRIVER_VERSION "0.9.8.3cvs for Linux kernels 2.4.19-2.4.32 + 2.6.0-2.6.17, compiled at "__DATE__", "__TIME__
-#define EMAIL "joerg@heckenbach-aw.de"
-#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
-#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
-#define DRIVER_LICENSE "GPL"
-#define DRIVER_ALIAS "USBVision"
+static unsigned int core_debug = 0;
+module_param(core_debug,int,0644);
+MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
 
-#define        ENABLE_HEXDUMP  0       /* Enable if you need it */
+static unsigned int force_testpattern = 0;
+module_param(force_testpattern,int,0644);
+MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]");
+
+static int adjustCompression = 1;                      // Set the compression to be adaptive
+module_param(adjustCompression, int, 0444);
+MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device.  Default: 1 (On)");
+
+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.
+module_param(SwitchSVideoInput, int, 0444);
+MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input.  Some cables and input device are wired differently. Default: 0 (Off)");
 
+#define        ENABLE_HEXDUMP  0       /* Enable if you need it */
 
-#define USBVISION_DEBUG                /* Turn on debug messages */
 
 #ifdef USBVISION_DEBUG
        #define PDEBUG(level, fmt, args...) \
-               if (debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
+               if (core_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
 #else
        #define PDEBUG(level, fmt, args...) do {} while(0)
 #endif
 
-#define DBG_IOCTL      1<<3
-#define DBG_IO         1<<4
-#define DBG_RIO                1<<5
-#define DBG_HEADER     1<<7
-#define DBG_PROBE      1<<8
-#define DBG_IRQ                1<<9
-#define DBG_ISOC       1<<10
-#define DBG_PARSE      1<<11
-#define DBG_SCRATCH    1<<12
-#define DBG_FUNC       1<<13
-#define DBG_I2C                1<<14
-
-#define DEBUG(x...)                                                            /* General Debug */
-#define IODEBUG(x...)                                                          /* Debug IO */
-#define OVDEBUG(x...)                                                          /* Debug overlay */
-#define MDEBUG(x...)                                                           /* Debug memory management */
-
-//String operations
-#define rmspace(str)   while(*str==' ') str++;
-#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
-
-
-static int usbvision_nr = 0;                   // sequential number of usbvision device
-static unsigned long usbvision_timestamp = 0;          // timestamp in jiffies of a hundred frame
-static unsigned long usbvision_counter = 0;            // frame counter
+#define DBG_HEADER     1<<0
+#define DBG_IRQ                1<<1
+#define DBG_ISOC       1<<2
+#define DBG_PARSE      1<<3
+#define DBG_SCRATCH    1<<4
 
 static const int max_imgwidth = MAX_FRAME_WIDTH;
 static const int max_imgheight = MAX_FRAME_HEIGHT;
 static const int min_imgwidth = MIN_FRAME_WIDTH;
 static const int min_imgheight = MIN_FRAME_HEIGHT;
 
-#define FRAMERATE_MIN  0
-#define FRAMERATE_MAX  31
-
-
-enum {
-       ISOC_MODE_YUV422 = 0x03,
-       ISOC_MODE_YUV420 = 0x14,
-       ISOC_MODE_COMPRESS = 0x60,
-};
-
-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" }
-};
-
-/* 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
+/* The value of 'scratch_buf_size' affects quality of the picture
  * in many ways. Shorter buffers may cause loss of data when client
  * is too slow. Larger buffers are memory-consuming and take longer
  * to work with. This setting can be adjusted, but the default value
@@ -467,229 +103,10 @@ static struct usbvision_tvnorm tvnorms[] = {
 static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
 
 // 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);
-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);
-static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg, unsigned char value);
 static int usbvision_request_intra (struct usb_usbvision *usbvision);
 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 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)
-#define FLAGS_RETRY_VIDIOCSYNC         (1 << 0)
-#define        FLAGS_MONOCHROME                (1 << 1)
-#define FLAGS_DISPLAY_HINTS            (1 << 2)
-#define FLAGS_OSD_STATS                        (1 << 3)
-#define FLAGS_FORCE_TESTPATTERN                (1 << 4)
-#define FLAGS_SEPARATE_FRAMES          (1 << 5)
-#define FLAGS_CLEAN_FRAMES             (1 << 6)
-
-// Default initalization of device driver parameters
-static int flags = 0;                                  // Set the default Overlay Display mode of the device driver
-static int debug = 0;                                  // Set the default Debug Mode of the device driver
-static int isocMode = ISOC_MODE_COMPRESS;              // Set the default format for ISOC endpoint
-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 vbi_nr = -1;                                        // Sequential Number of VBI Device
-static char *CustomDevice=NULL;                                // Set as nothing....
-
-// Grab parameters for the device driver
-
-#if defined(module_param)                               // Showing parameters under SYSFS
-module_param(flags, int, 0444);
-module_param(debug, int, 0444);
-module_param(isocMode, int, 0444);
-module_param(adjustCompression, int, 0444);
-module_param(dga, int, 0444);
-module_param(PowerOnAtOpen, int, 0444);
-module_param(SwitchSVideoInput, int, 0444);
-module_param(video_nr, int, 0444);
-module_param(radio_nr, int, 0444);
-module_param(vbi_nr, int, 0444);
-module_param(CustomDevice, charp, 0444);
-#else                                                  // Old Style
-MODULE_PARM(flags, "i");                               // Grab the Overlay Display mode of the device driver
-MODULE_PARM(debug, "i");                               // Grab the Debug Mode of the device driver
-MODULE_PARM(isocMode, "i");                            // Grab the video format of the video device
-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(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
-#endif
-
-MODULE_PARM_DESC(flags,        " Set the default Overlay Display mode of the device driver.  Default: 0 (Off)");
-MODULE_PARM_DESC(debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
-MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
-MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device.  Default: 1 (On)");
-MODULE_PARM_DESC(dga, " Set the Direct Graphic Access for the device.  Default: 1 (On)");
-MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened.  Default: 1 (On)");
-MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input.  Some cables and input device are wired differently. Default: 0 (Off)");
-MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
-MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
-MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX).  Default: -1 (autodetect)");
-MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device.  Default: null");
-
-
-// Misc stuff
-MODULE_AUTHOR(DRIVER_AUTHOR);
-MODULE_DESCRIPTION(DRIVER_DESC);
-MODULE_LICENSE(DRIVER_LICENSE);
-       MODULE_VERSION(DRIVER_VERSION);
-       MODULE_ALIAS(DRIVER_ALIAS);
-
-#ifdef MODULE
-static unsigned int autoload = 1;
-#else
-static unsigned int autoload = 0;
-#endif
-
-
-/****************************************************************************************/
-/* 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    */
-/****************************************************************************************/
-
-
-#define YES_NO(x) ((x) ? "Yes" : "No")
-
-static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
-{
-       struct video_device *vdev = to_video_device(cd);
-       return video_get_drvdata(vdev);
-}
-
-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);
-       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);
-       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);
-       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);
-       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);
-       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==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);
-       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);
-       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);
-       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) {
-               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);
-       }
-}
-
-static void usbvision_remove_sysfs(struct video_device *vdev)
-{
-       if (vdev) {
-               video_device_remove_file(vdev, &class_device_attr_version);
-               video_device_remove_file(vdev, &class_device_attr_model);
-               video_device_remove_file(vdev, &class_device_attr_hue);
-               video_device_remove_file(vdev, &class_device_attr_contrast);
-               video_device_remove_file(vdev, &class_device_attr_brightness);
-               video_device_remove_file(vdev, &class_device_attr_saturation);
-               video_device_remove_file(vdev, &class_device_attr_streaming);
-               video_device_remove_file(vdev, &class_device_attr_overlay);
-               video_device_remove_file(vdev, &class_device_attr_compression);
-               video_device_remove_file(vdev, &class_device_attr_bridge);
-       }
-}
-
 
 /*******************************/
 /* Memory management functions */
@@ -700,7 +117,6 @@ static void usbvision_remove_sysfs(struct video_device *vdev)
  * This is used when initializing the contents of the area.
  */
 
-
 void *usbvision_rvmalloc(unsigned long size)
 {
        void *mem;
@@ -712,65 +128,58 @@ void *usbvision_rvmalloc(unsigned long size)
                return NULL;
 
        memset(mem, 0, size); /* Clear the ram out, no junk to the user */
- adr = (unsigned long) mem;
- while (size > 0) {
-  #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
-   mem_map_reserve(vmalloc_to_page((void *)adr));
-  #else
-   SetPageReserved(vmalloc_to_page((void *)adr));
-  #endif
-  adr += PAGE_SIZE;
-  size -= PAGE_SIZE;
- }
-
- return mem;
+       adr = (unsigned long) mem;
+       while (size > 0) {
+               SetPageReserved(vmalloc_to_page((void *)adr));
+               adr += PAGE_SIZE;
+               size -= PAGE_SIZE;
+       }
+
+       return mem;
 }
 
 void usbvision_rvfree(void *mem, unsigned long size)
 {
- unsigned long adr;
-
- if (!mem)
-  return;
-
- adr = (unsigned long) mem;
- while ((long) size > 0) {
-  #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
-   mem_map_unreserve(vmalloc_to_page((void *)adr));
-  #else
-   ClearPageReserved(vmalloc_to_page((void *)adr));
-  #endif
-  adr += PAGE_SIZE;
-  size -= PAGE_SIZE;
- }
- vfree(mem);
-}
+       unsigned long adr;
+
+       if (!mem)
+               return;
 
+       size = PAGE_ALIGN(size);
 
+       adr = (unsigned long) mem;
+       while ((long) size > 0) {
+               ClearPageReserved(vmalloc_to_page((void *)adr));
+               adr += PAGE_SIZE;
+               size -= PAGE_SIZE;
+       }
 
+       vfree(mem);
+}
 
 
 
 #if ENABLE_HEXDUMP
 static void usbvision_hexdump(const unsigned char *data, int len)
 {
- char tmp[80];
- int i, k;
-
- for (i = k = 0; len > 0; i++, len--) {
-  if (i > 0 && (i % 16 == 0)) {
-   printk("%s\n", tmp);
-   k = 0;
-  }
-  k += sprintf(&tmp[k], "%02x ", data[i]);
- }
- if (k > 0)
-  printk("%s\n", tmp);
      char tmp[80];
      int i, k;
+
      for (i = k = 0; len > 0; i++, len--) {
+               if (i > 0 && (i % 16 == 0)) {
+                       printk("%s\n", tmp);
+                       k = 0;
+               }
+               k += sprintf(&tmp[k], "%02x ", data[i]);
      }
      if (k > 0)
+               printk("%s\n", tmp);
 }
 #endif
 
-
-/* These procedures handle the scratch ring buffer */
+/********************************
+ * scratch ring buffer handling
+ ********************************/
 int scratch_len(struct usb_usbvision *usbvision)    /*This returns the amount of data actually in the buffer */
 {
        int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
@@ -800,13 +209,6 @@ int scratch_free(struct usb_usbvision *usbvision)
 }
 
 
-void *debug_memcpy(void *dest, void *src, size_t len)
-{
-       printk(KERN_DEBUG "memcpy(%p, %p, %d);\n", dest, src, len);
-       return memcpy(dest, src, len);
-}
-
-
 /* This puts data into the buffer */
 int scratch_put(struct usb_usbvision *usbvision, unsigned char *data, int len)
 {
@@ -964,246 +366,26 @@ void scratch_reset(struct usb_usbvision *usbvision)
        usbvision->isocstate = IsocState_NoFrame;
 }
 
-
-
-/* Here comes the OVERLAY stuff */
-
-/* Tell the interrupt handler what to to.  */
-static
-void usbvision_cap(struct usb_usbvision* usbvision, int on)
-{
-       DEBUG(printk(KERN_DEBUG "usbvision_cap: overlay was %d, set it to %d\n", usbvision->overlay, on);)
-
-       if (on) {
-               usbvision->overlay = 1;
-       }
-       else {
-               usbvision->overlay = 0;
-       }
-}
-
-
-
-
-/* append a new clipregion to the vector of video_clips */
-static
-void usbvision_new_clip(struct v4l2_format* vf, struct v4l2_clip* vcp, int x, int y, int w, int h)
-{
-       vcp[vf->fmt.win.clipcount].c.left = x;
-       vcp[vf->fmt.win.clipcount].c.top = y;
-       vcp[vf->fmt.win.clipcount].c.width = w;
-       vcp[vf->fmt.win.clipcount].c.height = h;
-       vf->fmt.win.clipcount++;
-}
-
-
-#define mark_pixel(x,y)  usbvision->clipmask[((x) + (y) * MAX_FRAME_WIDTH)/32] |= 0x00000001<<((x)%32)
-#define clipped_pixel(index) usbvision->clipmask[(index)/32] & (0x00000001<<((index)%32))
-
-static
-void usbvision_built_overlay(struct usb_usbvision* usbvision, int count, struct v4l2_clip *vcp)
-{
-       usbvision->overlay_win = usbvision->overlay_base +
-               (signed int)usbvision->vid_win.fmt.win.w.left * usbvision->depth / 8 +
-               (signed int)usbvision->vid_win.fmt.win.w.top * usbvision->vid_buf.fmt.bytesperline;
-
-               IODEBUG(printk(KERN_DEBUG "built_overlay base=%p, win=%p, bpl=%d, clips=%d, size=%dx%d\n",
-                                       usbvision->overlay_base, usbvision->overlay_win,
-                                       usbvision->vid_buf.fmt.bytesperline, count,
-                                       usbvision->vid_win.fmt.win.w.width, usbvision->vid_win.fmt.win.w.height);)
-
-
-       /* Add here generation of clipping mask */
-{
-       int x_start, x_end, y_start, y_end;
-       int clip_index, x, y;
-
-       memset(usbvision->clipmask, 0, USBVISION_CLIPMASK_SIZE);
-
-       OVDEBUG(printk(KERN_DEBUG "clips = %d\n", count);)
-
-       for(clip_index = 0; clip_index < count; clip_index++) {
-               OVDEBUG(printk(KERN_DEBUG "clip: %d,%d,%d,%d\n", vcp[clip_index].x,
-                               vcp[clip_index].y,
-                               vcp[clip_index].width,
-                               vcp[clip_index].height);)
-
-               x_start = vcp[clip_index].c.left;
-               if(x_start >= (int)usbvision->vid_win.fmt.win.w.width) {
-                       OVDEBUG(printk(KERN_DEBUG "x_start=%d\n", x_start);)
-                       continue; //clipping window is right of overlay window
-               }
-               x_end   = x_start + vcp[clip_index].c.width;
-               if(x_end <= 0) {
-                       OVDEBUG(printk(KERN_DEBUG "x_end=%d\n", x_end);)
-                       continue; //clipping window is left of overlay window
-               }
-
-               y_start = vcp[clip_index].c.top;
-               if(y_start >= (int)usbvision->vid_win.fmt.win.w.height) {
-                       OVDEBUG(printk(KERN_DEBUG "y_start=%d\n", y_start);)
-                       continue; //clipping window is below overlay window
-               }
-               y_end   = y_start + vcp[clip_index].c.height;
-               if(y_end <= 0) {
-                       OVDEBUG(printk(KERN_DEBUG "y_end=%d\n", y_end);)
-                       continue; //clipping window is above overlay window
-               }
-
-               //clip the clipping window
-               if (x_start < 0) {
-                       x_start = 0;
-               }
-               if (x_end > (int)usbvision->vid_win.fmt.win.w.width) {
-                       x_end = (int)usbvision->vid_win.fmt.win.w.width;
-               }
-               if (y_start < 0) {
-                       y_start = 0;
-               }
-               if (y_end > (int)usbvision->vid_win.fmt.win.w.height) {
-                       y_end = (int)usbvision->vid_win.fmt.win.w.height;
-               }
-
-               OVDEBUG(printk(KERN_DEBUG "clip_o: %d,%d,%d,%d\n", x_start,     y_start, x_end, y_end);)
-
-
-
-               for(y = y_start; y < y_end; y++) {
-                       for(x = x_start; x < x_end; x++) {
-                               mark_pixel(x,y);
-                       }
-               }
-       }
-}
-
-}
-
-
-
-void usbvision_osd_char(struct usb_usbvision *usbvision,
-                       struct usbvision_frame *frame, int x, int y, int ch)
+int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
 {
-       static const unsigned short digits[16] = {
-               0xF6DE,         /* 0 */
-               0x2492,         /* 1 */
-               0xE7CE,         /* 2 */
-               0xE79E,         /* 3 */
-               0xB792,         /* 4 */
-               0xF39E,         /* 5 */
-               0xF3DE,         /* 6 */
-               0xF492,         /* 7 */
-               0xF7DE,         /* 8 */
-               0xF79E,         /* 9 */
-               0x77DA,         /* a */
-               0xD75C,         /* b */
-               0xF24E,         /* c */
-               0xD6DC,         /* d */
-               0xF34E,         /* e */
-               0xF348          /* f */
-       };
-       unsigned short digit;
-       int ix, iy;
-
-       if ((usbvision == NULL) || (frame == NULL))
-               return;
-
-       if (ch >= '0' && ch <= '9')
-               ch -= '0';
-       else if (ch >= 'A' && ch <= 'F')
-               ch = 10 + (ch - 'A');
-       else if (ch >= 'a' && ch <= 'f')
-               ch = 10 + (ch - 'a');
-       else
-               return;
-       digit = digits[ch];
-
-       for (iy = 0; iy < 5; iy++) {
-               for (ix = 0; ix < 3; ix++) {
-                       if (digit & 0x8000) {
-                       //      USBVISION_PUTPIXEL(frame, x + ix, y + iy,
-                       //                      0xFF, 0xFF, 0xFF);
-                       }
-                       digit = digit << 1;
-               }
+       usbvision->scratch = vmalloc(scratch_buf_size);
+       scratch_reset(usbvision);
+       if(usbvision->scratch == NULL) {
+               err("%s: unable to allocate %d bytes for scratch",
+                   __FUNCTION__, scratch_buf_size);
+               return -ENOMEM;
        }
+       return 0;
 }
 
-
-void usbvision_osd_string(struct usb_usbvision *usbvision,
-                         struct usbvision_frame *frame,
-                         int x, int y, const char *str)
+void usbvision_scratch_free(struct usb_usbvision *usbvision)
 {
-       while (*str) {
-               usbvision_osd_char(usbvision, frame, x, y, *str);
-               str++;
-               x += 4;         /* 3 pixels character + 1 space */
+       if (usbvision->scratch != NULL) {
+               vfree(usbvision->scratch);
+               usbvision->scratch = NULL;
        }
 }
 
-/*
- * usb_usbvision_osd_stats()
- *
- * On screen display of important debugging information.
- *
- */
-void usbvision_osd_stats(struct usb_usbvision *usbvision,
-                        struct usbvision_frame *frame)
-{
-       const int y_diff = 8;
-       char tmp[16];
-       int x = 10;
-       int y = 10;
-
-       sprintf(tmp, "%8x", usbvision->frame_num);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->isocUrbCount);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->urb_length);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->isocDataCount);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->header_count);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->scratch_ovf_count);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->isocSkipCount);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8lx", usbvision->isocErrCount);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8x", usbvision->saturation);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8x", usbvision->hue);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8x", usbvision->brightness >> 8);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-       sprintf(tmp, "%8x", usbvision->contrast >> 12);
-       usbvision_osd_string(usbvision, frame, x, y, tmp);
-       y += y_diff;
-
-}
-
 /*
  * usbvision_testpattern()
  *
@@ -1287,14 +469,40 @@ void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe,
        frame->scanlength += scan_length;
        ++num_pass;
 
-       /* We do this unconditionally, regardless of FLAGS_OSD_STATS */
-       usbvision_osd_stats(usbvision, frame);
 }
 
 /*
- * Here comes the data parsing stuff that is run as interrupt
+ * usbvision_decompress_alloc()
+ *
+ * allocates intermediate buffer for decompression
  */
-
+int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
+{
+       int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
+       usbvision->IntraFrameBuffer = vmalloc(IFB_size);
+       if (usbvision->IntraFrameBuffer == NULL) {
+               err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
+               return -ENOMEM;
+       }
+       return 0;
+}
+
+/*
+ * usbvision_decompress_free()
+ *
+ * frees intermediate buffer for decompression
+ */
+void usbvision_decompress_free(struct usb_usbvision *usbvision)
+{
+       if (usbvision->IntraFrameBuffer != NULL) {
+               vfree(usbvision->IntraFrameBuffer);
+               usbvision->IntraFrameBuffer = NULL;
+       }
+}
+
+/************************************************************
+ * Here comes the data parsing stuff that is run as interrupt
+ ************************************************************/
 /*
  * usbvision_find_header()
  *
@@ -1305,12 +513,7 @@ static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
        struct usbvision_frame *frame;
        int foundHeader = 0;
 
-       if (usbvision->overlay) {
-               frame = &usbvision->overlay_frame;
-       }
-       else {
-               frame = usbvision->curFrame;
-       }
+       frame = usbvision->curFrame;
 
        while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
                // found header in scratch
@@ -1368,7 +571,7 @@ static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
        frame->scanstate = ScanState_Lines;
        frame->curline = 0;
 
-       if (flags & FLAGS_FORCE_TESTPATTERN) {
+       if (force_testpattern) {
                usbvision_testpattern(usbvision, 1, 1);
                return ParseState_NextFrame;
        }
@@ -1385,22 +588,10 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
        unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
        unsigned char rv, gv, bv;       // RGB components
        int clipmask_index, bytes_per_pixel;
-       int overlay = usbvision->overlay;
        int stretch_bytes, clipmask_add;
 
-       if (overlay) {
-               frame  = &usbvision->overlay_frame;
-               if (usbvision->overlay_base == NULL) {
-                       //video_buffer is not set yet
-                       return ParseState_NextFrame;
-               }
-               f = usbvision->overlay_win + frame->curline *
-                       usbvision->vid_buf.fmt.bytesperline;
-       }
-       else {
-               frame  = usbvision->curFrame;
-               f = frame->data + (frame->v4l2_linesize * frame->curline);
-       }
+       frame  = usbvision->curFrame;
+       f = frame->data + (frame->v4l2_linesize * frame->curline);
 
        /* Make sure there's enough data for the entire line */
        len = (frame->isocHeader.frameWidth * 2)+5;
@@ -1422,10 +613,7 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
 
                scratch_get(usbvision, &yuyv[0], 4);
 
-               if((overlay) && (clipped_pixel(clipmask_index))) {
-                       f += bytes_per_pixel;
-               }
-               else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+               if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                        *f++ = yuyv[0]; // Y
                        *f++ = yuyv[3]; // U
                }
@@ -1457,10 +645,7 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
                clipmask_index += clipmask_add;
                f += stretch_bytes;
 
-               if((overlay) && (clipped_pixel(clipmask_index))) {
-                       f += bytes_per_pixel;
-               }
-               else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+               if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                        *f++ = yuyv[2]; // Y
                        *f++ = yuyv[1]; // V
                }
@@ -1504,7 +689,7 @@ static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision
        }
 }
 
-
+/* The decompression routine  */
 static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
                                                                unsigned char *Decompressed, int *StartPos,
                                                                int *BlockTypeStartPos, int Len)
@@ -1614,45 +799,31 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
        unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
        int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
        int clipmask_index, bytes_per_pixel, rc;
-       int overlay = usbvision->overlay;
        int imageSize;
        unsigned char rv, gv, bv;
        static unsigned char *Y, *U, *V;
 
-       if (overlay) {
-               frame  = &usbvision->overlay_frame;
-               imageSize = frame->frmwidth * frame->frmheight;
-               if (usbvision->overlay_base == NULL) {
-                       //video_buffer is not set yet
-                       return ParseState_NextFrame;
-               }
-               f = usbvision->overlay_win + frame->curline *
-                       usbvision->vid_buf.fmt.bytesperline;
-       }
-       else {
-               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.
-                       f = frame->data + (frame->width * frame->curline);
-               } 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
-                               + (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) ;
-                       u = v + (imageSize >>2) ;
-               }
+       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.
+               f = frame->data + (frame->width * frame->curline);
+       } 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
+                       + (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) ;
+               u = v + (imageSize >>2) ;
        }
 
        if (frame->curline == 0) {
@@ -1738,10 +909,7 @@ static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
        }
 
        for (Idx = 0; Idx < IdxEnd; Idx++) {
-               if((overlay) && (clipped_pixel(clipmask_index))) {
-                       f += bytes_per_pixel;
-               }
-               else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+               if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                        *f++ = Y[Idx];
                        *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
                }
@@ -1838,23 +1006,10 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
        unsigned char g;
        int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
        int clipmask_add, stretch_bytes;
-       int overlay = usbvision->overlay;
 
-       if (overlay) {
-               frame  = &usbvision->overlay_frame;
-               if (usbvision->overlay_base == NULL) {
-                       //video_buffer is not set yet
-                       return ParseState_NextFrame;
-               }
-               f_even = usbvision->overlay_win + frame->curline *
-                        usbvision->vid_buf.fmt.bytesperline;
-               f_odd  = f_even + usbvision->vid_buf.fmt.bytesperline * usbvision->stretch_height;
-       }
-       else {
-               frame  = usbvision->curFrame;
-               f_even = frame->data + (frame->v4l2_linesize * frame->curline);
-               f_odd  = f_even + frame->v4l2_linesize * usbvision->stretch_height;
-       }
+       frame  = usbvision->curFrame;
+       f_even = frame->data + (frame->v4l2_linesize * frame->curline);
+       f_odd  = f_even + frame->v4l2_linesize * usbvision->stretch_height;
 
        /* Make sure there's enough data for the entire line */
        /* In this mode usbvision transfer 3 bytes for every 2 pixels */
@@ -1901,10 +1056,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
                        uvg= -53281 * u_ - 25625 * v_;
                        ur = 104595 * u_;
 
-                       if((overlay) && (clipped_pixel(clipmask_even_index))) {
-                               f_even += bytes_per_pixel;
-                       }
-                       else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+                       if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                                *f_even++ = y[0];
                                *f_even++ = v;
                        }
@@ -1943,10 +1095,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
                        clipmask_even_index += clipmask_add;
                        f_even += stretch_bytes;
 
-                       if((overlay) && (clipped_pixel(clipmask_even_index))) {
-                               f_even += bytes_per_pixel;
-                       }
-                       else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+                       if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                                *f_even++ = y[1];
                                *f_even++ = u;
                        }
@@ -1987,10 +1136,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
 
                        scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
 
-                       if ((overlay) && (clipped_pixel(clipmask_odd_index))) {
-                               f_odd += bytes_per_pixel;
-                       }
-                       else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+                       if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                                *f_odd++ = y[0];
                                *f_odd++ = v;
                        }
@@ -2029,10 +1175,7 @@ static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision
                        clipmask_odd_index += clipmask_add;
                        f_odd += stretch_bytes;
 
-                       if((overlay) && (clipped_pixel(clipmask_odd_index))) {
-                               f_odd += bytes_per_pixel;
-                       }
-                       else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
+                       if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
                                *f_odd++ = y[1];
                                *f_odd++ = u;
                        }
@@ -2108,12 +1251,7 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
        long copylen = 0;
        unsigned long lock_flags;
 
-       if (usbvision->overlay) {
-               frame = &usbvision->overlay_frame;
-       }
-       else {
-               frame = usbvision->curFrame;
-       }
+       frame = usbvision->curFrame;
 
        PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
 
@@ -2152,23 +1290,13 @@ static void usbvision_parse_data(struct usb_usbvision *usbvision)
                frame->grabstate = FrameState_Done;
                do_gettimeofday(&(frame->timestamp));
                frame->sequence = usbvision->frame_num;
-               if (usbvision->overlay) {
-                       frame->grabstate = FrameState_Grabbing;
-                       frame->scanstate = ScanState_Scanning;
-                       frame->scanlength = 0;
-                       copylen = 0;
-               }
-               else {
-                       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++;
 
-               /* Optionally display statistics on the screen */
-               if (flags & FLAGS_OSD_STATS)
-                       usbvision_osd_stats(usbvision, frame);
+               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++;
 
                /* This will cause the process to request another frame. */
                if (waitqueue_active(&usbvision->wait_frame)) {
@@ -2306,7 +1434,7 @@ static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs)
                /* 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(!list_empty(&(usbvision->inqueue))) {
                                if (!(*f)) {
                                        (*f) = list_entry(usbvision->inqueue.next,struct usbvision_frame, frame);
                                }
@@ -2353,7 +1481,7 @@ static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs)
  *        >= 0 -> Data
  */
 
-static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
+int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
 {
        int errCode = 0;
        unsigned char buffer[1];
@@ -2381,7 +1509,7 @@ static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg
  *       -1 -> Something went wrong
  */
 
-static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
+int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
                            unsigned char value)
 {
        int errCode = 0;
@@ -2454,8 +1582,6 @@ static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address,
 }
 
 
-
-
 static int usbvision_init_compression(struct usb_usbvision *usbvision)
 {
        int errCode = 0;
@@ -2563,334 +1689,11 @@ static int usbvision_unrequest_intra (struct usb_usbvision *usbvision)
        return errCode;
 }
 
-/* ----------------------------------------------------------------------- */
-/* I2C functions                                                           */
-/* ----------------------------------------------------------------------- */
-
-static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
-                            void *arg)
-{
-       BUG_ON(NULL == usbvision->i2c_adap.algo_data);
-       i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
-}
-
-static int attach_inform(struct i2c_client *client)
-{
-       struct usb_usbvision *usbvision;
-
-       usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
-
-       switch (client->addr << 1) {
-               case 0x43:
-               case 0x4b:
-               {
-                       struct tuner_setup tun_setup;
-
-                       tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
-                       tun_setup.type = TUNER_TDA9887;
-                       tun_setup.addr = client->addr;
-
-                       call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
-
-                       break;
-               }
-               case 0x42:
-                       PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.\n");
-                       break;
-               case 0x4a:
-                       PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.\n");
-                       break;
-               case 0xa0:
-                       PDEBUG(DBG_I2C,"attach_inform: eeprom detected.\n");
-                       break;
-
-               default:
-                       PDEBUG(DBG_I2C,"attach inform: detected I2C address %x\n", client->addr << 1);
-                       {
-                               struct tuner_setup tun_setup;
-
-                               usbvision->tuner_addr = client->addr;
-
-                               if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
-                                       tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
-                                       tun_setup.type = usbvision->tuner_type;
-                                       tun_setup.addr = usbvision->tuner_addr;
-                                       call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
-                               }
-                       }
-                       break;
-       }
-       return 0;
-}
-
-static int detach_inform(struct i2c_client *client)
-{
-       struct usb_usbvision *usbvision;
-
-       #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
-               usbvision = (struct usb_usbvision *)client->adapter->data;
-       #else
-               usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
-       #endif
-
-       PDEBUG(DBG_I2C, "usbvision[%d] detaches %s", usbvision->nr, client->name);
-       return 0;
-}
-
-static int
-usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
-                    char *buf, short len)
-{
-       int rc, retries;
-
-       for (retries = 5;;) {
-               rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
-               if (rc < 0)
-                       return rc;
-
-               /* Initiate byte read cycle                    */
-               /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
-               /*                    d3 0=Wr 1=Rd             */
-               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
-                                     (len & 0x07) | 0x18);
-               if (rc < 0)
-                       return rc;
-
-               /* Test for Busy and ACK */
-               do {
-                       /* USBVISION_SER_CONT -> d4 == 0 busy */
-                       rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
-               } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
-               if (rc < 0)
-                       return rc;
-
-               /* USBVISION_SER_CONT -> d5 == 1 Not ack */
-               if ((rc & 0x20) == 0)   /* Ack? */
-                       break;
-
-               /* I2C abort */
-               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
-               if (rc < 0)
-                       return rc;
-
-               if (--retries < 0)
-                       return -1;
-       }
-
-       switch (len) {
-       case 4:
-               buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
-       case 3:
-               buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
-       case 2:
-               buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
-       case 1:
-               buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
-               break;
-       default:
-               printk(KERN_ERR
-                      "usbvision_i2c_read_max4: buffer length > 4\n");
-       }
-
-       if (debug & DBG_I2C) {
-               int idx;
-               for (idx = 0; idx < len; idx++) {
-                       PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr);
-               }
-       }
-       return len;
-}
-
-
-static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
-                                unsigned char addr, const char *buf,
-                                short len)
-{
-       int rc, retries;
-       int i;
-       unsigned char value[6];
-       unsigned char ser_cont;
-
-       ser_cont = (len & 0x07) | 0x10;
-
-       value[0] = addr;
-       value[1] = ser_cont;
-       for (i = 0; i < len; i++)
-               value[i + 2] = buf[i];
-
-       for (retries = 5;;) {
-               rc = usb_control_msg(usbvision->dev,
-                                    usb_sndctrlpipe(usbvision->dev, 1),
-                                    USBVISION_OP_CODE,
-                                    USB_DIR_OUT | USB_TYPE_VENDOR |
-                                    USB_RECIP_ENDPOINT, 0,
-                                    (__u16) USBVISION_SER_ADRS, value,
-                                    len + 2, HZ);
-
-               if (rc < 0)
-                       return rc;
-
-               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
-                                     (len & 0x07) | 0x10);
-               if (rc < 0)
-                       return rc;
-
-               /* Test for Busy and ACK */
-               do {
-                       rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
-               } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
-               if (rc < 0)
-                       return rc;
-
-               if ((rc & 0x20) == 0)   /* Ack? */
-                       break;
-
-               /* I2C abort */
-               usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
-
-               if (--retries < 0)
-                       return -1;
-
-       }
-
-       if (debug & DBG_I2C) {
-               int idx;
-               for (idx = 0; idx < len; idx++) {
-                       PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr);
-               }
-       }
-       return len;
-}
-
-static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
-                           short len)
-{
-       char *bufPtr = buf;
-       int retval;
-       int wrcount = 0;
-       int count;
-       int maxLen = 4;
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
-
-       while (len > 0) {
-               count = (len > maxLen) ? maxLen : len;
-               retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
-               if (retval > 0) {
-                       len -= count;
-                       bufPtr += count;
-                       wrcount += count;
-               } else
-                       return (retval < 0) ? retval : -EFAULT;
-       }
-       return wrcount;
-}
-
-static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
-                          short len)
-{
-       char temp[4];
-       int retval, i;
-       int rdcount = 0;
-       int count;
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
-
-       while (len > 0) {
-               count = (len > 3) ? 4 : len;
-               retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
-               if (retval > 0) {
-                       for (i = 0; i < len; i++)
-                               buf[rdcount + i] = temp[i];
-                       len -= count;
-                       rdcount += count;
-               } else
-                       return (retval < 0) ? retval : -EFAULT;
-       }
-       return rdcount;
-}
-
-static struct i2c_algo_usb_data i2c_algo_template = {
-       .data           = NULL,
-       .inb            = usbvision_i2c_read,
-       .outb           = usbvision_i2c_write,
-       .udelay         = 10,
-       .mdelay         = 10,
-       .timeout        = 100,
-};
-
-static struct i2c_adapter i2c_adap_template = {
-       .owner             = THIS_MODULE,
-       .name              = "usbvision",
-       .id                = I2C_HW_B_BT848, /* FIXME */
-       .algo              = NULL,
-       .algo_data         = NULL,
-       .client_register   = attach_inform,
-       .client_unregister = detach_inform,
-#if defined (I2C_ADAP_CLASS_TV_ANALOG)
-       .class             = I2C_ADAP_CLASS_TV_ANALOG,
-#elif defined (I2C_CLASS_TV_ANALOG)
-       .class             = I2C_CLASS_TV_ANALOG,
-#endif
-};
-
-static struct i2c_client i2c_client_template = {
-       .name           = "usbvision internal",
-       .flags          = 0,
-       .addr           = 0,
-       .adapter        = NULL,
-       .driver         = NULL,
-};
-
-static int usbvision_init_i2c(struct usb_usbvision *usbvision)
-{
-       memcpy(&usbvision->i2c_adap, &i2c_adap_template,
-              sizeof(struct i2c_adapter));
-       memcpy(&usbvision->i2c_algo, &i2c_algo_template,
-              sizeof(struct i2c_algo_usb_data));
-       memcpy(&usbvision->i2c_client, &i2c_client_template,
-              sizeof(struct i2c_client));
-
-       sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
-               " #%d", usbvision->vdev->minor & 0x1f);
-       PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name);
-
-       i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
-       i2c_set_clientdata(&usbvision->i2c_client, usbvision);
-       i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision);
-
-       usbvision->i2c_adap.algo_data = &usbvision->i2c_algo;
-       usbvision->i2c_client.adapter = &usbvision->i2c_adap;
-
-       if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
-               printk(KERN_ERR "usbvision_init_i2c: can't wirte reg\n");
-               return -EBUSY;
-       }
-
-#ifdef CONFIG_KMOD
-       /* Request the load of the i2c modules we need */
-       if (autoload) {
-               switch (usbvision_device_data[usbvision->DevModel].Codec) {
-                       case CODEC_SAA7113:
-                               request_module("saa7115");
-                               break;
-                       case CODEC_SAA7111:
-                               request_module("saa7115");
-                               break;
-               }
-               if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
-                       request_module("tuner");
-               }
-       }
-#endif
-
-       return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
-}
+/*******************************
+ * usbvision utility functions
+ *******************************/
 
-
-/****************************/
-/* usbvision utility functions */
-/****************************/
-
-static int usbvision_power_off(struct usb_usbvision *usbvision)
+int usbvision_power_off(struct usb_usbvision *usbvision)
 {
        int errCode = 0;
 
@@ -2905,22 +1708,6 @@ static int usbvision_power_off(struct usb_usbvision *usbvision)
 }
 
 
-// to call usbvision_power_off from task queue
-static void call_usbvision_power_off(void *_usbvision)
-{
-       struct usb_usbvision *usbvision = _usbvision;
-
-       PDEBUG(DBG_FUNC, "");
-       down_interruptible(&usbvision->lock);
-       if(usbvision->user == 0) {
-               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
-               usbvision_power_off(usbvision);
-               usbvision->initialized = 0;
-       }
-       up(&usbvision->lock);
-}
-
-
 /*
  * usbvision_set_video_format()
  *
@@ -2964,8 +1751,8 @@ static int usbvision_set_video_format(struct usb_usbvision *usbvision, int forma
  *
  */
 
-static int usbvision_set_output(struct usb_usbvision *usbvision, int width,
-                                 int height)
+int usbvision_set_output(struct usb_usbvision *usbvision, int width,
+                        int height)
 {
        int errCode = 0;
        int UsbWidth, UsbHeight;
@@ -3059,11 +1846,63 @@ static int usbvision_set_output(struct usb_usbvision *usbvision, int width,
 }
 
 
+/*
+ * usbvision_frames_alloc
+ * allocate the maximum frames this driver can manage
+ */
+int usbvision_frames_alloc(struct usb_usbvision *usbvision)
+{
+       int i;
+
+       /* Allocate memory for the frame buffers */
+       usbvision->max_frame_size = MAX_FRAME_SIZE;
+       usbvision->fbuf_size = USBVISION_NUMFRAMES * usbvision->max_frame_size;
+       usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size);
+
+       if(usbvision->fbuf == NULL) {
+               err("%s: unable to allocate %d bytes for fbuf ",
+                   __FUNCTION__, usbvision->fbuf_size);
+               return -ENOMEM;
+       }
+       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 * usbvision->max_frame_size;
+               /*
+                * Set default sizes for read operation.
+                */
+               usbvision->stretch_width = 1;
+               usbvision->stretch_height = 1;
+               usbvision->frame[i].width = usbvision->curwidth;
+               usbvision->frame[i].height = usbvision->curheight;
+               usbvision->frame[i].bytes_read = 0;
+       }
+       return 0;
+}
+
+/*
+ * usbvision_frames_free
+ * frees memory allocated for the frames
+ */
+void usbvision_frames_free(struct usb_usbvision *usbvision)
+{
+       /* Have to free all that memory */
+       if (usbvision->fbuf != NULL) {
+               usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
+               usbvision->fbuf = NULL;
+       }
+}
 /*
  * usbvision_empty_framequeues()
  * prepare queues for incoming and outgoing frames
  */
-static void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
+void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
 {
        u32 i;
 
@@ -3080,7 +1919,7 @@ static void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
  * usbvision_stream_interrupt()
  * stops streaming
  */
-static int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
+int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
 {
        int ret = 0;
 
@@ -3176,7 +2015,7 @@ static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
  * I've no idea if this parameters are right
  *
  */
-static int usbvision_set_input(struct usb_usbvision *usbvision)
+int usbvision_set_input(struct usb_usbvision *usbvision)
 {
        static const char proc[] = "usbvision_set_input: ";
        int rc;
@@ -3351,7 +2190,7 @@ static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
  *
  */
 
-static int usbvision_power_on(struct usb_usbvision *usbvision)
+int usbvision_power_on(struct usb_usbvision *usbvision)
 {
        int errCode = 0;
 
@@ -3372,6 +2211,25 @@ static int usbvision_power_on(struct usb_usbvision *usbvision)
 }
 
 
+/*
+ * usbvision timer stuff
+ */
+
+// to call usbvision_power_off from task queue
+static void call_usbvision_power_off(void *_usbvision)
+{
+       struct usb_usbvision *usbvision = _usbvision;
+
+       PDEBUG(DBG_FUNC, "");
+       down_interruptible(&usbvision->lock);
+       if(usbvision->user == 0) {
+               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
+               usbvision_power_off(usbvision);
+               usbvision->initialized = 0;
+       }
+       up(&usbvision->lock);
+}
+
 static void usbvision_powerOffTimer(unsigned long data)
 {
        struct usb_usbvision *usbvision = (void *) data;
@@ -3383,13 +2241,31 @@ static void usbvision_powerOffTimer(unsigned long data)
 
 }
 
+void usbvision_init_powerOffTimer(struct usb_usbvision *usbvision)
+{
+       init_timer(&usbvision->powerOffTimer);
+       usbvision->powerOffTimer.data = (long) usbvision;
+       usbvision->powerOffTimer.function = usbvision_powerOffTimer;
+}
+
+void usbvision_set_powerOffTimer(struct usb_usbvision *usbvision)
+{
+       mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
+}
+
+void usbvision_reset_powerOffTimer(struct usb_usbvision *usbvision)
+{
+       if (timer_pending(&usbvision->powerOffTimer)) {
+               del_timer(&usbvision->powerOffTimer);
+       }
+}
 
 /*
  * usbvision_begin_streaming()
  * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
  * idea about the rest
  */
-static int usbvision_begin_streaming(struct usb_usbvision *usbvision)
+int usbvision_begin_streaming(struct usb_usbvision *usbvision)
 {
        int errCode = 0;
 
@@ -3406,7 +2282,7 @@ static int usbvision_begin_streaming(struct usb_usbvision *usbvision)
  * Not sure yet if touching here PWR_REG make loose the config
  */
 
-static int usbvision_restart_isoc(struct usb_usbvision *usbvision)
+int usbvision_restart_isoc(struct usb_usbvision *usbvision)
 {
        int ret;
 
@@ -3433,41 +2309,18 @@ static int usbvision_restart_isoc(struct usb_usbvision *usbvision)
        return 0;
 }
 
-static int usbvision_audio_on(struct usb_usbvision *usbvision)
-{
-       if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, usbvision->AudioChannel) < 0) {
-               printk(KERN_ERR "usbvision_audio_on: can't wirte reg\n");
-               return -1;
-       }
-       DEBUG(printk(KERN_DEBUG "usbvision_audio_on: channel %d\n", usbvision->AudioChannel));
-       usbvision->AudioMute = 0;
-       return 0;
-}
-
-static int usbvision_audio_mute(struct usb_usbvision *usbvision)
-{
-       if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, 0x03) < 0) {
-               printk(KERN_ERR "usbvision_audio_mute: can't wirte reg\n");
-               return -1;
-       }
-       DEBUG(printk(KERN_DEBUG "usbvision_audio_mute: audio mute\n"));
-       usbvision->AudioMute = 1;
-       return 0;
-}
-
-static int usbvision_audio_off(struct usb_usbvision *usbvision)
+int usbvision_audio_off(struct usb_usbvision *usbvision)
 {
        if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
                printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n");
                return -1;
        }
-       DEBUG(printk(KERN_DEBUG "usbvision_audio_off: audio off\n"));
        usbvision->AudioMute = 0;
        usbvision->AudioChannel = USBVISION_AUDIO_MUTE;
        return 0;
 }
 
-static int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
+int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
 {
        if (!usbvision->AudioMute) {
                if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) {
@@ -3475,14 +2328,13 @@ static int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel
                        return -1;
                }
        }
-       DEBUG(printk(KERN_DEBUG "usbvision_set_audio: channel %d\n", AudioChannel));
        usbvision->AudioChannel = AudioChannel;
        return 0;
 }
 
-static int usbvision_setup(struct usb_usbvision *usbvision)
+int usbvision_setup(struct usb_usbvision *usbvision,int format)
 {
-       usbvision_set_video_format(usbvision, isocMode);
+       usbvision_set_video_format(usbvision, format);
        usbvision_set_dram_settings(usbvision);
        usbvision_set_compress_params(usbvision);
        usbvision_set_input(usbvision);
@@ -3494,22 +2346,55 @@ static int usbvision_setup(struct usb_usbvision *usbvision)
 }
 
 
-/*
- * usbvision_init_isoc()
- *
- */
-static int usbvision_init_isoc(struct usb_usbvision *usbvision)
+int usbvision_sbuf_alloc(struct usb_usbvision *usbvision)
 {
-       struct usb_device *dev = usbvision->dev;
-       int bufIdx, errCode, regValue;
+       int i, errCode = 0;
+       const int sb_size = USBVISION_URB_FRAMES * USBVISION_MAX_ISOC_PACKET_SIZE;
 
-       if (!USBVISION_IS_OPERATIONAL(usbvision))
-               return -EFAULT;
+       /* Clean pointers so we know if we allocated something */
+       for (i = 0; i < USBVISION_NUMSBUF; i++)
+               usbvision->sbuf[i].data = NULL;
 
-       usbvision->curFrame = NULL;
-       scratch_reset(usbvision);
+       for (i = 0; i < USBVISION_NUMSBUF; i++) {
+               usbvision->sbuf[i].data = kzalloc(sb_size, GFP_KERNEL);
+               if (usbvision->sbuf[i].data == NULL) {
+                       err("%s: unable to allocate %d bytes for sbuf", __FUNCTION__, sb_size);
+                       errCode = -ENOMEM;
+                       break;
+               }
+       }
+       return errCode;
+}
 
-       /* Alternate interface 1 is is the biggest frame size */
+
+void usbvision_sbuf_free(struct usb_usbvision *usbvision)
+{
+       int i;
+
+       for (i = 0; i < USBVISION_NUMSBUF; i++) {
+               if (usbvision->sbuf[i].data != NULL) {
+                       kfree(usbvision->sbuf[i].data);
+                       usbvision->sbuf[i].data = NULL;
+               }
+       }
+}
+
+/*
+ * usbvision_init_isoc()
+ *
+ */
+int usbvision_init_isoc(struct usb_usbvision *usbvision)
+{
+       struct usb_device *dev = usbvision->dev;
+       int bufIdx, errCode, regValue;
+
+       if (!USBVISION_IS_OPERATIONAL(usbvision))
+               return -EFAULT;
+
+       usbvision->curFrame = NULL;
+       scratch_reset(usbvision);
+
+       /* Alternate interface 1 is is the biggest frame size */
        errCode = usb_set_interface(dev, usbvision->iface, usbvision->ifaceAltActive);
        if (errCode < 0) {
                usbvision->last_error = errCode;
@@ -3587,7 +2472,7 @@ static int usbvision_init_isoc(struct usb_usbvision *usbvision)
  * activates zero-bandwidth alt. setting of the video interface.
  *
  */
-static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
+void usbvision_stop_isoc(struct usb_usbvision *usbvision)
 {
        int bufIdx, errCode, regValue;
 
@@ -3623,7 +2508,7 @@ static void usbvision_stop_isoc(struct usb_usbvision *usbvision)
        }
 }
 
-static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
+int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
 {
        int mode[4];
        int audio[]= {1, 0, 0, 0};
@@ -3671,2157 +2556,6 @@ static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
        return 0;
 }
 
-
-/*
- * usbvision_open()
- *
- * This is part of Video 4 Linux API. The driver can be opened by one
- * client only (checks internal counter 'usbvision->user'). The procedure
- * then allocates buffers needed for video processing.
- *
- */
-static int usbvision_v4l2_open(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       const int sb_size = USBVISION_URB_FRAMES * USBVISION_MAX_ISOC_PACKET_SIZE;
-       int i, errCode = 0;
-
-       PDEBUG(DBG_IO, "open");
-
-
-       if (timer_pending(&usbvision->powerOffTimer)) {
-               del_timer(&usbvision->powerOffTimer);
-       }
-
-       if (usbvision->user)
-               errCode = -EBUSY;
-       else {
-               /* Clean pointers so we know if we allocated something */
-               for (i = 0; i < USBVISION_NUMSBUF; i++)
-                       usbvision->sbuf[i].data = NULL;
-
-               /* Allocate memory for the frame buffers */
-               usbvision->max_frame_size = MAX_FRAME_SIZE;
-               usbvision->fbuf_size = USBVISION_NUMFRAMES * usbvision->max_frame_size;
-               usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size);
-               usbvision->scratch = vmalloc(scratch_buf_size);
-               scratch_reset(usbvision);
-               if ((usbvision->fbuf == NULL) || (usbvision->scratch == NULL)) {
-                       err("%s: unable to allocate %d bytes for fbuf and %d bytes for scratch",
-                                       __FUNCTION__, usbvision->fbuf_size, scratch_buf_size);
-                       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;
-                               /*
-                                * Set default sizes in case IOCTL
-                                * (VIDIOCMCAPTURE)
-                                * is not used (using read() instead).
-                                */
-                               usbvision->stretch_width = 1;
-                               usbvision->stretch_height = 1;
-                               usbvision->frame[i].width = usbvision->curwidth;
-                               usbvision->frame[i].height = usbvision->curheight;
-                               usbvision->frame[i].bytes_read = 0;
-                       }
-                       if (dga) { //set default for DGA
-                               usbvision->overlay_frame.grabstate = FrameState_Unused;
-                               usbvision->overlay_frame.scanstate = ScanState_Scanning;
-                               usbvision->overlay_frame.data = NULL;
-                               usbvision->overlay_frame.width = usbvision->curwidth;
-                               usbvision->overlay_frame.height = usbvision->curheight;
-                               usbvision->overlay_frame.bytes_read = 0;
-                       }
-                       for (i = 0; i < USBVISION_NUMSBUF; i++) {
-                               usbvision->sbuf[i].data = kzalloc(sb_size, GFP_KERNEL);
-                               if (usbvision->sbuf[i].data == NULL) {
-                                       err("%s: unable to allocate %d bytes for sbuf", __FUNCTION__, sb_size);
-                                       errCode = -ENOMEM;
-                                       break;
-                               }
-                       }
-               }
-               if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) {
-                       int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
-                       usbvision->IntraFrameBuffer = vmalloc(IFB_size);
-                       if (usbvision->IntraFrameBuffer == NULL) {
-                               err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
-                               errCode = -ENOMEM;
-                       }
-
-               }
-               if (errCode) {
-                       /* Have to free all that memory */
-                       if (usbvision->fbuf != NULL) {
-                               usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
-                               usbvision->fbuf = NULL;
-                       }
-                       if (usbvision->scratch != NULL) {
-                               vfree(usbvision->scratch);
-                               usbvision->scratch = NULL;
-                       }
-                       for (i = 0; i < USBVISION_NUMSBUF; i++) {
-                               if (usbvision->sbuf[i].data != NULL) {
-                                       kfree(usbvision->sbuf[i].data);
-                                       usbvision->sbuf[i].data = NULL;
-                               }
-                       }
-                       if (usbvision->IntraFrameBuffer != NULL) {
-                               vfree(usbvision->IntraFrameBuffer);
-                               usbvision->IntraFrameBuffer = NULL;
-                       }
-               }
-       }
-
-       /* If so far no errors then we shall start the camera */
-       if (!errCode) {
-               down(&usbvision->lock);
-               if (usbvision->power == 0) {
-                       usbvision_power_on(usbvision);
-                       usbvision_init_i2c(usbvision);
-               }
-
-               /* Send init sequence only once, it's large! */
-               if (!usbvision->initialized) {
-                       int setup_ok = 0;
-                       setup_ok = usbvision_setup(usbvision);
-                       if (setup_ok)
-                               usbvision->initialized = 1;
-                       else
-                               errCode = -EBUSY;
-               }
-
-               if (!errCode) {
-                       usbvision_begin_streaming(usbvision);
-                       errCode = usbvision_init_isoc(usbvision);
-                       /* device needs to be initialized before isoc transfer */
-                       usbvision_muxsel(usbvision,0);
-                       usbvision->user++;
-               }
-               else {
-                       if (PowerOnAtOpen) {
-                               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
-                               usbvision_power_off(usbvision);
-                               usbvision->initialized = 0;
-                       }
-               }
-               up(&usbvision->lock);
-       }
-
-       if (errCode) {
-       }
-
-       /* prepare queues */
-       usbvision_empty_framequeues(usbvision);
-
-       PDEBUG(DBG_IO, "success");
-       return errCode;
-}
-
-/*
- * usbvision_v4l2_close()
- *
- * This is part of Video 4 Linux API. The procedure
- * stops streaming and deallocates all buffers that were earlier
- * allocated in usbvision_v4l2_open().
- *
- */
-static int usbvision_v4l2_close(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       int i;
-
-       PDEBUG(DBG_IO, "close");
-       down(&usbvision->lock);
-
-       usbvision_audio_off(usbvision);
-       usbvision_restart_isoc(usbvision);
-       usbvision_stop_isoc(usbvision);
-
-       if (usbvision->IntraFrameBuffer != NULL) {
-               vfree(usbvision->IntraFrameBuffer);
-               usbvision->IntraFrameBuffer = NULL;
-       }
-
-       usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
-       vfree(usbvision->scratch);
-       for (i = 0; i < USBVISION_NUMSBUF; i++)
-               kfree(usbvision->sbuf[i].data);
-
-       usbvision->user--;
-
-       if (PowerOnAtOpen) {
-               mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
-               usbvision->initialized = 0;
-       }
-
-       up(&usbvision->lock);
-
-       if (usbvision->remove_pending) {
-               info("%s: Final disconnect", __FUNCTION__);
-               usbvision_release(usbvision);
-       }
-
-       PDEBUG(DBG_IO, "success");
-
-
-       return 0;
-}
-
-
-/*
- * usbvision_ioctl()
- *
- * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
- *
- */
-static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
-                                unsigned int cmd, void *arg)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-
-       if (!USBVISION_IS_OPERATIONAL(usbvision))
-               return -EFAULT;
-
-       //      if (debug & DBG_IOCTL) v4l_printk_ioctl(cmd);
-
-       switch (cmd) {
-
-#ifdef CONFIG_VIDEO_ADV_DEBUG
-               /* ioctls to allow direct acces to the NT100x registers */
-               case VIDIOC_INT_G_REGISTER:
-               {
-                       struct v4l2_register *reg = arg;
-                       int errCode;
-
-                       if (reg->i2c_id != 0)
-                               return -EINVAL;
-                       /* NT100x has a 8-bit register space */
-                       errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
-                       if (errCode < 0) {
-                               err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
-                       }
-                       else {
-                               reg->val=(unsigned char)errCode;
-                               PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
-                                                       (unsigned int)reg->reg, reg->val);
-                               errCode = 0; // No error
-                       }
-                       return errCode;
-               }
-               case VIDIOC_INT_S_REGISTER:
-               {
-                       struct v4l2_register *reg = arg;
-                       int errCode;
-
-                       if (reg->i2c_id != 0)
-                               return -EINVAL;
-                       if (!capable(CAP_SYS_ADMIN))
-                               return -EPERM;
-                       errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
-                       if (errCode < 0) {
-                               err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
-                       }
-                       else {
-                               PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
-                                                       (unsigned int)reg->reg, reg->val);
-                               errCode = 0;
-                       }
-                       return 0;
-               }
-#endif
-               case VIDIOC_QUERYCAP:
-               {
-                       struct v4l2_capability *vc=arg;
-
-                       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;
-               }
-               case VIDIOC_ENUMINPUT:
-               {
-                       struct v4l2_input *vi = arg;
-                       int chan;
-
-                       if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
-                               return -EINVAL;
-                       if (usbvision->have_tuner) {
-                               chan = vi->index;
-                       }
-                       else {
-                               chan = vi->index + 1; //skip Television string
-                       }
-                       switch(chan) {
-                               case 0:
-                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
-                                               strcpy(vi->name, "White Video Input");
-                                       }
-                                       else {
-                                               strcpy(vi->name, "Television");
-                                               vi->type = V4L2_INPUT_TYPE_TUNER;
-                                               vi->audioset = 1;
-                                               vi->tuner = chan;
-                                               vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
-                                       }
-                                       break;
-                               case 1:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
-                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
-                                               strcpy(vi->name, "Green Video Input");
-                                       }
-                                       else {
-                                               strcpy(vi->name, "Composite Video Input");
-                                       }
-                                       vi->std = V4L2_STD_PAL;
-                                       break;
-                               case 2:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
-                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
-                                               strcpy(vi->name, "Yellow Video Input");
-                                       }
-                                       else {
-                                       strcpy(vi->name, "S-Video Input");
-                                       }
-                                       vi->std = V4L2_STD_PAL;
-                                       break;
-                               case 3:
-                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
-                                       strcpy(vi->name, "Red Video Input");
-                                       vi->std = V4L2_STD_PAL;
-                                       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 *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:
-               {
-                       int *input = arg;
-                       *input = usbvision->ctl_input;
-                       return 0;
-               }
-               case VIDIOC_S_INPUT:
-               {
-                       int *input = arg;
-                       if ((*input >= usbvision->video_inputs) || (*input < 0) )
-                               return -EINVAL;
-                       usbvision->ctl_input = *input;
-
-                       down(&usbvision->lock);
-                       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:
-               {
-                       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:
-               {
-                       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->tvnorm = &tvnorms[i];
-
-                       call_i2c_clients(usbvision, VIDIOC_S_STD,
-                                        &usbvision->tvnorm->id);
-
-                       up(&usbvision->lock);
-
-                       PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
-                       return 0;
-               }
-               case VIDIOC_G_TUNER:
-               {
-                       struct v4l2_tuner *vt = arg;
-
-                       if (!usbvision->have_tuner || vt->index)        // Only tuner 0
-                               return -EINVAL;
-                       strcpy(vt->name, "Television");
-                       /* 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:
-               {
-                       struct v4l2_tuner *vt = arg;
-
-                       // Only no or one tuner for now
-                       if (!usbvision->have_tuner || vt->index)
-                               return -EINVAL;
-                       /* 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;
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
-                       return 0;
-               }
-               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);
-                       return 0;
-               }
-               case VIDIOC_G_AUDIO:
-               {
-                       struct v4l2_audio *v = arg;
-                       memset(v,0, sizeof(v));
-                       strcpy(v->name, "TV");
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
-                       // FIXME: no more processings ???
-                       return 0;
-               }
-               case VIDIOC_S_AUDIO:
-               {
-                       struct v4l2_audio *v = arg;
-                       if(v->index) {
-                               return -EINVAL;
-                       }
-                       PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
-                       // FIXME: void function ???
-                       return 0;
-               }
-               case VIDIOC_QUERYCTRL:
-               {
-                       struct v4l2_queryctrl *ctrl = arg;
-                       int id=ctrl->id;
-
-                       memset(ctrl,0,sizeof(*ctrl));
-                       ctrl->id=id;
-
-                       call_i2c_clients(usbvision, cmd, arg);
-
-                       if (ctrl->type)
-                               return 0;
-                       else
-                               return -EINVAL;
-
-                       PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
-               }
-               case VIDIOC_G_CTRL:
-               {
-                       struct v4l2_control *ctrl = arg;
-
-                       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;
-               }
-               case VIDIOC_REQBUFS:
-               {
-                       struct v4l2_requestbuffers *vr = arg;
-                       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:
-               {
-                       struct v4l2_buffer *vb = arg;
-                       struct usbvision_frame *frame;
-
-                       // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
-
-                       if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
-                               return -EINVAL;
-                       }
-                       if(vb->index>=USBVISION_NUMFRAMES)  {
-                               return -EINVAL;
-                       }
-                       // Updating the corresponding frame state
-                       vb->flags = 0;
-                       frame = &usbvision->frame[vb->index];
-                       if(frame->grabstate >= FrameState_Ready)
-                               vb->flags |= V4L2_BUF_FLAG_QUEUED;
-                       if(frame->grabstate >= FrameState_Done)
-                               vb->flags |= V4L2_BUF_FLAG_DONE;
-                       if(frame->grabstate == FrameState_Unused)
-                               vb->flags |= V4L2_BUF_FLAG_MAPPED;
-                       vb->memory = V4L2_MEMORY_MMAP;
-
-                       vb->m.offset = vb->index*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[vb->index].sequence;
-                       return 0;
-               }
-               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>=USBVISION_NUMFRAMES)  {
-                               return -EINVAL;
-                       }
-
-                       frame = &usbvision->frame[vb->index];
-
-                       if (frame->grabstate != FrameState_Unused) {
-                               return -EAGAIN;
-                       }
-
-                       /* 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;
-
-                       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);
-
-                       PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
-                       return 0;
-               }
-               case VIDIOC_DQBUF:
-               {
-                       struct v4l2_buffer *vb = arg;
-                       int ret;
-                       struct usbvision_frame *f;
-                       unsigned long lock_flags;
-
-                       if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-                               return -EINVAL;
-
-                       if (list_empty(&(usbvision->outqueue))) {
-                               if (usbvision->streaming == Stream_Idle)
-                                       return -EINVAL;
-                               ret = wait_event_interruptible
-                                       (usbvision->wait_frame,
-                                        !list_empty(&(usbvision->outqueue)));
-                               if (ret)
-                                       return ret;
-                       }
-
-                       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;
-                       vb->timestamp = f->timestamp;
-                       vb->field = V4L2_FIELD_NONE;
-                       vb->bytesused = f->scanlength;
-
-                       if(debug & DBG_IOCTL) { // do not spend computing time for debug stuff if not needed !
-                               if(usbvision_counter == 100) {
-                                       PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF delta=%d",(unsigned)(jiffies-usbvision_timestamp));
-                                       usbvision_counter = 0;
-                                       usbvision_timestamp = jiffies;
-                               }
-                               else {
-                                       usbvision_counter++;
-                               }
-                               PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF frame #%d",vb->index);
-                       }
-                       return 0;
-               }
-               case VIDIOC_STREAMON:
-               {
-                       int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
-
-                       usbvision->streaming = Stream_On;
-
-                       if(debug & DBG_IOCTL) usbvision_timestamp = jiffies;
-
-                       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;
-
-                       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:
-               {
-                       struct v4l2_framebuffer *vb = arg;
-
-                       if (dga) {
-                               *vb = usbvision->vid_buf;
-                       }
-                       else {
-                               memset(vb, 0, sizeof(vb)); //dga not supported, not used
-                       }
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d",
-                              vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline);
-                       return 0;
-               }
-               case VIDIOC_S_FBUF:
-               {
-                       struct v4l2_framebuffer *vb = arg;
-                       int formatIdx;
-
-                       if (dga == 0) {
-                               return -EINVAL;
-                       }
-
-                       if(!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_ADMIN)) {
-                               return -EPERM;
-                       }
-
-                       PDEBUG(DBG_IOCTL, "VIDIOC_S_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d",
-                              vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline);
-
-                       for (formatIdx=0; formatIdx <= USBVISION_SUPPORTED_PALETTES; formatIdx++) {
-                               if (formatIdx == USBVISION_SUPPORTED_PALETTES) {
-                                       return -EINVAL; // no matching video_format
-                               }
-                               if ((vb->fmt.pixelformat == usbvision_v4l2_format[formatIdx].format) &&
-                                   (usbvision_v4l2_format[formatIdx].supported)) {
-                                       break; //found matching video_format
-                               }
-                       }
-
-                       if (vb->fmt.bytesperline<1) {
-                               return -EINVAL;
-                       }
-                       if (usbvision->overlay) {
-                               return -EBUSY;
-                       }
-                       down(&usbvision->lock);
-                       if (usbvision->overlay_base) {
-                               iounmap(usbvision->overlay_base);
-                               usbvision->vid_buf_valid = 0;
-                       }
-                       usbvision->overlay_base = ioremap((ulong)vb->base, vb->fmt.height * vb->fmt.bytesperline);
-                       if (usbvision->overlay_base) {
-                               usbvision->vid_buf_valid = 1;
-                       }
-                       usbvision->vid_buf = *vb;
-                       usbvision->overlay_frame.v4l2_format = usbvision_v4l2_format[formatIdx];
-                       up(&usbvision->lock);
-                       return 0;
-               }
-               case VIDIOC_ENUM_FMT:
-               {
-                       struct v4l2_fmtdesc *vfd = arg;
-
-                       if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
-                               return -EINVAL;
-                       }
-                       vfd->flags = 0;
-                       vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-                       strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
-                       vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
-                       memset(vfd->reserved, 0, sizeof(vfd->reserved));
-                       return 0;
-               }
-               case VIDIOC_G_FMT:
-               {
-                       struct v4l2_format *vf = arg;
-
-                       switch (vf->type) {
-                               case V4L2_BUF_TYPE_VIDEO_CAPTURE:
-                               {
-                                       vf->fmt.pix.width = usbvision->curwidth;
-                                       vf->fmt.pix.height = usbvision->curheight;
-                                       vf->fmt.pix.pixelformat = usbvision->palette.format;
-                                       vf->fmt.pix.bytesperline =  usbvision->curwidth*usbvision->palette.bytes_per_pixel;
-                                       vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
-                                       vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
-                                       vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
-                               }
-                               return 0;
-                               default:
-                                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
-                                       return -EINVAL;
-                       }
-                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d",vf->fmt.win.w.width, vf->fmt.win.w.height);
-                       return 0;
-               }
-               case VIDIOC_TRY_FMT:
-               case VIDIOC_S_FMT:
-               {
-                       struct v4l2_format *vf = arg;
-                       struct v4l2_clip *vc=NULL;
-                       int on,formatIdx;
-
-                       switch(vf->type) {
-                               case V4L2_BUF_TYPE_VIDEO_OVERLAY:
-                               {
-                                       if (vf->fmt.win.clipcount>256) {
-                                               return -EDOM;   /* Too many clips! */
-                                       }
-                                       // Do every clips.
-                                       vc = vmalloc(sizeof(struct v4l2_clip)*(vf->fmt.win.clipcount+4));
-                                       if (vc == NULL) {
-                                               return -ENOMEM;
-                                       }
-                                       if (vf->fmt.win.clipcount && copy_from_user(vc,vf->fmt.win.clips,sizeof(struct v4l2_clip)*vf->fmt.win.clipcount)) {
-                                               return -EFAULT;
-                                       }
-                                       on = usbvision->overlay;        // Save overlay state
-                                       if (on) {
-                                               usbvision_cap(usbvision, 0);
-                                       }
-
-                                       // strange, it seems xawtv sometimes calls us with 0
-                                       // width and/or height. Ignore these values
-                                       if (vf->fmt.win.w.left == 0) {
-                                               vf->fmt.win.w.left = usbvision->vid_win.fmt.win.w.left;
-                                       }
-                                       if (vf->fmt.win.w.top == 0) {
-                                               vf->fmt.win.w.top = usbvision->vid_win.fmt.win.w.top;
-                                       }
-
-                                       // by now we are committed to the new data...
-                                       down(&usbvision->lock);
-                                       RESTRICT_TO_RANGE(vf->fmt.win.w.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
-                                       RESTRICT_TO_RANGE(vf->fmt.win.w.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
-                                       usbvision->vid_win = *vf;
-                                       usbvision->overlay_frame.width = vf->fmt.win.w.width;
-                                       usbvision->overlay_frame.height = vf->fmt.win.w.height;
-                                       usbvision_set_output(usbvision, vf->fmt.win.w.width, vf->fmt.win.w.height);
-                                       up(&usbvision->lock);
-
-                                       // Impose display clips
-                                       if (vf->fmt.win.w.left+vf->fmt.win.w.width > (unsigned int)usbvision->vid_buf.fmt.width) {
-                                               usbvision_new_clip(vf, vc, usbvision->vid_buf.fmt.width-vf->fmt.win.w.left, 0, vf->fmt.win.w.width-1, vf->fmt.win.w.height-1);
-                                       }
-                                       if (vf->fmt.win.w.top+vf->fmt.win.w.height > (unsigned int)usbvision->vid_buf.fmt.height) {
-                                               usbvision_new_clip(vf, vc, 0, usbvision->vid_buf.fmt.height-vf->fmt.win.w.top, vf->fmt.win.w.width-1, vf->fmt.win.w.height-1);
-                                       }
-
-                                       // built the requested clipping zones
-                                       usbvision_built_overlay(usbvision, vf->fmt.win.clipcount, vc);
-                                       vfree(vc);
-
-                                       // restore overlay state
-                                       if (on) {
-                                               usbvision_cap(usbvision, 1);
-                                       }
-                                       usbvision->vid_win_valid = 1;
-                                       PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT overlay x=%d, y=%d, w=%d, h=%d, chroma=%x, clips=%d",
-                                              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 V4L2_BUF_TYPE_VIDEO_CAPTURE:
-                               {
-                                       /* Find requested format in available ones */
-                                       for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
-                                               if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
-                                                       usbvision->palette = usbvision_v4l2_format[formatIdx];
-                                                       break;
-                                               }
-                                       }
-                                       /* robustness */
-                                       if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
-                                               return -EINVAL;
-                                       }
-                                       RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
-                                       RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
-                                       // by now we are committed to the new data...
-                                       down(&usbvision->lock);
-                                       usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
-                                       up(&usbvision->lock);
-
-                                       PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
-                                              vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
-                                       return 0;
-                               }
-                               default:
-                                       return -EINVAL;
-                       }
-               }
-               case VIDIOC_OVERLAY:
-               {
-                       int *v = arg;
-
-                       if ( (dga == 0) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YVU420) &&
-                            (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) {
-                               PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY  DGA disabled");
-                               return -EINVAL;
-                       }
-
-                       if (*v == 0) {
-                               usbvision_cap(usbvision, 0);
-                       }
-                       else {
-                               // are VIDIOCSFBUF and VIDIOCSWIN done?
-                               if ((usbvision->vid_buf_valid == 0) || (usbvision->vid_win_valid == 0)) {
-                                       PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY  vid_buf_valid %d; vid_win_valid %d",
-                                              usbvision->vid_buf_valid, usbvision->vid_win_valid);
-                                       return -EINVAL;
-                               }
-                               usbvision_cap(usbvision, 1);
-                       }
-                       PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY %s", (*v)?"on":"off");
-                       return 0;
-               }
-               default:
-                       return -ENOIOCTLCMD;
-       }
-       return 0;
-}
-
-static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, unsigned long arg)
-{
-       return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
-}
-
-
-static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
-                     size_t count, loff_t *ppos)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       int noblock = file->f_flags & O_NONBLOCK;
-       unsigned long lock_flags;
-
-       int frmx = -1;
-       int ret,i;
-       struct usbvision_frame *frame;
-
-       PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
-
-       if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
-               return -EFAULT;
-
-       /* no stream is running, make it running ! */
-       usbvision->streaming = Stream_On;
-       call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
-
-       /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
-       for(i=0;i<USBVISION_NUMFRAMES;i++) {
-               frame = &usbvision->frame[i];
-               if(frame->grabstate == FrameState_Unused) {
-                       /* Mark it as ready and enqueue frame */
-                       frame->grabstate = FrameState_Ready;
-                       frame->scanstate = ScanState_Scanning;
-                       frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
-
-                       /* set v4l2_format index */
-                       frame->v4l2_format = usbvision->palette;
-
-                       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
-                       list_add_tail(&frame->frame, &usbvision->inqueue);
-                       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
-               }
-       }
-
-       /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
-       if (list_empty(&(usbvision->outqueue))) {
-               if(noblock)
-                       return -EAGAIN;
-
-               ret = wait_event_interruptible
-                       (usbvision->wait_frame,
-                        !list_empty(&(usbvision->outqueue)));
-               if (ret)
-                       return ret;
-       }
-
-       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
-       frame = list_entry(usbvision->outqueue.next,
-                          struct usbvision_frame, frame);
-       list_del(usbvision->outqueue.next);
-       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
-
-       if(debug & DBG_IOCTL) { // do not spend computing time for debug stuff if not needed !
-               if(usbvision_counter == 100) {
-                       PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF delta=%d",(unsigned)(jiffies-usbvision_timestamp));
-                       usbvision_counter = 0;
-                       usbvision_timestamp = jiffies;
-               }
-               else {
-                       usbvision_counter++;
-               }
-       }
-
-       /* An error returns an empty frame */
-       if (frame->grabstate == FrameState_Error) {
-               frame->bytes_read = 0;
-               return 0;
-       }
-
-       PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
-                      frame->index, frame->bytes_read, frame->scanlength);
-
-       /* copy bytes to user space; we allow for partials reads */
-       if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
-               count = frame->scanlength - frame->bytes_read;
-
-       if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
-               return -EFAULT;
-       }
-
-       frame->bytes_read += count;
-       PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
-                      (unsigned long)count, frame->bytes_read);
-
-       // For now, forget the frame if it has not been read in one shot.
-/*     if (frame->bytes_read >= frame->scanlength) {// All data has been read */
-               frame->bytes_read = 0;
-
-               /* Mark it as available to be used again. */
-               usbvision->frame[frmx].grabstate = FrameState_Unused;
-/*     } */
-
-       return count;
-}
-
-static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
-{
-       unsigned long size = vma->vm_end - vma->vm_start,
-               start = vma->vm_start;
-       void *pos;
-       u32 i;
-
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-
-       if (!USBVISION_IS_OPERATIONAL(usbvision))
-               return -EFAULT;
-
-       if (!(vma->vm_flags & VM_WRITE) ||
-           size != PAGE_ALIGN(usbvision->max_frame_size)) {
-               return -EINVAL;
-       }
-
-       for (i = 0; i < USBVISION_NUMFRAMES; i++) {
-               if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
-                       break;
-       }
-       if (i == USBVISION_NUMFRAMES) {
-               PDEBUG(DBG_FUNC, "mmap: user supplied mapping address is out of range");
-               return -EINVAL;
-       }
-
-       /* VM_IO is eventually going to replace PageReserved altogether */
-       vma->vm_flags |= VM_IO;
-       vma->vm_flags |= VM_RESERVED;   /* avoid to swap out this VMA */
-
-       pos = usbvision->frame[i].data;
-       while (size > 0) {
-
-               if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
-                       PDEBUG(DBG_FUNC, "mmap: vm_insert_page failed");
-                       return -EAGAIN;
-               }
-               start += PAGE_SIZE;
-               pos += PAGE_SIZE;
-               size -= PAGE_SIZE;
-       }
-
-       return 0;
-}
-
-
-/*
- * Here comes the stuff for radio on usbvision based devices
- *
- */
-static int usbvision_radio_open(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       struct v4l2_frequency freq;
-       int errCode = 0;
-
-       PDEBUG(DBG_RIO, "%s:", __FUNCTION__);
-
-       down(&usbvision->lock);
-
-       if (usbvision->user) {
-               err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
-               errCode = -EBUSY;
-       }
-       else {
-               if(PowerOnAtOpen) {
-                       if (timer_pending(&usbvision->powerOffTimer)) {
-                               del_timer(&usbvision->powerOffTimer);
-                       }
-                       if (usbvision->power == 0) {
-                               usbvision_power_on(usbvision);
-                               usbvision_init_i2c(usbvision);
-                       }
-               }
-
-               // If so far no errors then we shall start the radio
-               usbvision->radio = 1;
-               call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
-               freq.frequency = 1517; //SWR3 @ 94.8MHz
-               call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
-               usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
-               usbvision->user++;
-       }
-
-       if (errCode) {
-               if (PowerOnAtOpen) {
-                       usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
-                       usbvision_power_off(usbvision);
-                       usbvision->initialized = 0;
-               }
-       }
-       up(&usbvision->lock);
-       return errCode;
-}
-
-
-static int usbvision_radio_close(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       int errCode = 0;
-
-       PDEBUG(DBG_RIO, "");
-
-       down(&usbvision->lock);
-
-       usbvision_audio_off(usbvision);
-       usbvision->radio=0;
-       usbvision->user--;
-
-       if (PowerOnAtOpen) {
-               mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
-               usbvision->initialized = 0;
-       }
-
-       up(&usbvision->lock);
-
-       if (usbvision->remove_pending) {
-               info("%s: Final disconnect", __FUNCTION__);
-               usbvision_release(usbvision);
-       }
-
-
-       PDEBUG(DBG_RIO, "success");
-
-       return errCode;
-}
-
-static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
-                                unsigned int cmd, void *arg)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-
-       if (!USBVISION_IS_OPERATIONAL(usbvision))
-               return -EIO;
-
-       switch (cmd) {
-               case VIDIOC_QUERYCAP:
-               {
-                       struct v4l2_capability *vc=arg;
-
-                       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 = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
-                       PDEBUG(DBG_RIO, "VIDIOC_QUERYCAP");
-                       return 0;
-               }
-               case VIDIOC_QUERYCTRL:
-               {
-                       struct v4l2_queryctrl *ctrl = arg;
-                       int id=ctrl->id;
-
-                       memset(ctrl,0,sizeof(*ctrl));
-                       ctrl->id=id;
-
-                       call_i2c_clients(usbvision, cmd, arg);
-                       PDEBUG(DBG_RIO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
-
-                       if (ctrl->type)
-                               return 0;
-                       else
-                               return -EINVAL;
-
-               }
-               case VIDIOC_G_CTRL:
-               {
-                       struct v4l2_control *ctrl = arg;
-
-                       call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
-                       PDEBUG(DBG_RIO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
-                       return 0;
-               }
-               case VIDIOC_S_CTRL:
-               {
-                       struct v4l2_control *ctrl = arg;
-
-                       call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
-                       PDEBUG(DBG_RIO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
-                       return 0;
-               }
-               case VIDIOC_G_TUNER:
-               {
-                       struct v4l2_tuner *t = arg;
-
-                       if (t->index > 0)
-                               return -EINVAL;
-
-                       memset(t,0,sizeof(*t));
-                       strcpy(t->name, "Radio");
-                       t->type = V4L2_TUNER_RADIO;
-
-                       /* Let clients fill in the remainder of this struct */
-                       call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
-                       PDEBUG(DBG_RIO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
-                       return 0;
-               }
-               case VIDIOC_S_TUNER:
-               {
-                       struct v4l2_tuner *vt = arg;
-
-                       // Only no or one tuner for now
-                       if (!usbvision->have_tuner || vt->index)
-                               return -EINVAL;
-                       /* let clients handle this */
-                       call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
-
-                       PDEBUG(DBG_RIO, "VIDIOC_S_TUNER");
-                       return 0;
-               }
-               case VIDIOC_G_AUDIO:
-               {
-                       struct v4l2_audio *a = arg;
-
-                       memset(a,0,sizeof(*a));
-                       strcpy(a->name,"Radio");
-                       PDEBUG(DBG_RIO, "VIDIOC_G_AUDIO");
-                       return 0;
-               }
-               case VIDIOC_S_AUDIO:
-               case VIDIOC_S_INPUT:
-               case VIDIOC_S_STD:
-               return 0;
-
-               case VIDIOC_G_FREQUENCY:
-               {
-                       struct v4l2_frequency *f = arg;
-
-                       memset(f,0,sizeof(*f));
-
-                       f->type = V4L2_TUNER_RADIO;
-                       f->frequency = usbvision->freq;
-                       call_i2c_clients(usbvision, cmd, f);
-                       PDEBUG(DBG_RIO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
-
-                       return 0;
-               }
-               case VIDIOC_S_FREQUENCY:
-               {
-                       struct v4l2_frequency *f = arg;
-
-                       if (f->tuner != 0)
-                               return -EINVAL;
-                       usbvision->freq = f->frequency;
-                       call_i2c_clients(usbvision, cmd, f);
-                       PDEBUG(DBG_RIO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
-
-                       return 0;
-               }
-               default:
-               {
-                       PDEBUG(DBG_RIO, "%s: Unknown command %x", __FUNCTION__, cmd);
-                       return -ENOIOCTLCMD;
-               }
-       }
-       return 0;
-}
-
-
-static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, unsigned long arg)
-{
-       return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
-}
-
-
-/*
- * Here comes the stuff for vbi on usbvision based devices
- *
- */
-static int usbvision_vbi_open(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       unsigned long freq;
-       int errCode = 0;
-
-       PDEBUG(DBG_RIO, "%s:", __FUNCTION__);
-
-       down(&usbvision->lock);
-
-       if (usbvision->user) {
-               err("%s: Someone tried to open an already opened USBVision VBI!", __FUNCTION__);
-               errCode = -EBUSY;
-       }
-       else {
-               if(PowerOnAtOpen) {
-                       if (timer_pending(&usbvision->powerOffTimer)) {
-                               del_timer(&usbvision->powerOffTimer);
-                       }
-                       if (usbvision->power == 0) {
-                               usbvision_power_on(usbvision);
-                               usbvision_init_i2c(usbvision);
-                       }
-               }
-
-               // If so far no errors then we shall start the vbi device
-               //usbvision->vbi = 1;
-               call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
-               freq = 1517; //SWR3 @ 94.8MHz
-               call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
-               usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
-               usbvision->user++;
-       }
-
-       if (errCode) {
-               if (PowerOnAtOpen) {
-                       usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
-                       usbvision_power_off(usbvision);
-                       usbvision->initialized = 0;
-               }
-       }
-       up(&usbvision->lock);
-       return errCode;
-}
-
-static int usbvision_vbi_close(struct inode *inode, struct file *file)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-       int errCode = 0;
-
-       PDEBUG(DBG_RIO, "");
-
-       down(&usbvision->lock);
-
-       usbvision_audio_off(usbvision);
-       usbvision->vbi=0;
-       usbvision->user--;
-
-       if (PowerOnAtOpen) {
-               mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
-               usbvision->initialized = 0;
-       }
-
-       up(&usbvision->lock);
-
-       if (usbvision->remove_pending) {
-               info("%s: Final disconnect", __FUNCTION__);
-               usbvision_release(usbvision);
-       }
-
-
-       PDEBUG(DBG_RIO, "success");
-
-       return errCode;
-}
-
-static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
-                                unsigned int cmd, void *arg)
-{
-       struct video_device *dev = video_devdata(file);
-       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
-
-       if (!USBVISION_IS_OPERATIONAL(usbvision))
-               return -EIO;
-
-       switch (cmd) {
-               case VIDIOC_QUERYCAP:
-               {
-                       struct v4l2_capability *vc=arg;
-                       memset(vc, 0, sizeof(struct v4l2_capability));
-                       strcpy(vc->driver,"usbvision vbi");
-                       strcpy(vc->card,usbvision->vcap.card);
-                       strcpy(vc->bus_info,"usb");
-                       vc->version = USBVISION_DRIVER_VERSION;             /* version */
-                       vc->capabilities = V4L2_CAP_VBI_CAPTURE;            /* capabilities */
-                       PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__);
-                       return 0;
-               }
-               case VIDIOCGTUNER:
-               {
-                       struct video_tuner *vt = arg;
-
-                       if((vt->tuner) || (usbvision->channel)) {       /* Only tuner 0 */
-                               return -EINVAL;
-                       }
-                       strcpy(vt->name, "vbi");
-                       // japan:          76.0 MHz -  89.9 MHz
-                       // western europe: 87.5 MHz - 108.0 MHz
-                       // russia:         65.0 MHz - 108.0 MHz
-                       vt->rangelow=(int)(65*16);
-                       vt->rangehigh=(int)(108*16);
-                       vt->flags= 0;
-                       vt->mode = 0;
-                       call_i2c_clients(usbvision,cmd,vt);
-                       PDEBUG(DBG_RIO, "%s: VIDIOCGTUNER signal=%d", __FUNCTION__, vt->signal);
-                       return 0;
-               }
-               case VIDIOCSTUNER:
-               {
-                       struct video_tuner *vt = arg;
-
-                       // Only channel 0 has a tuner
-                       if((vt->tuner) || (usbvision->channel)) {
-                               return -EINVAL;
-                       }
-                       PDEBUG(DBG_RIO, "%s: VIDIOCSTUNER", __FUNCTION__);
-                       return 0;
-               }
-               case VIDIOCGAUDIO:
-               {
-                       struct video_audio *va = arg;
-                       memset(va,0, sizeof(struct video_audio));
-                       call_i2c_clients(usbvision, cmd, va);
-                       va->flags|=VIDEO_AUDIO_MUTABLE;
-                       va->volume=1;
-                       va->step=1;
-                       strcpy(va->name, "vbi");
-                       PDEBUG(DBG_RIO, "%s: VIDIOCGAUDIO", __FUNCTION__);
-                       return 0;
-               }
-               case VIDIOCSAUDIO:
-               {
-                       struct video_audio *va = arg;
-                       if(va->audio) {
-                               return -EINVAL;
-                       }
-
-                       if(va->flags & VIDEO_AUDIO_MUTE) {
-                               if (usbvision_audio_mute(usbvision)) {
-                                       return -EFAULT;
-                               }
-                       }
-                       else {
-                               if (usbvision_audio_on(usbvision)) {
-                                       return -EFAULT;
-                               }
-                       }
-                       PDEBUG(DBG_RIO, "%s: VIDIOCSAUDIO flags=0x%x)", __FUNCTION__, va->flags);
-                       return 0;
-               }
-               case VIDIOCGFREQ:
-               {
-                       unsigned long *freq = arg;
-
-                       *freq = usbvision->freq;
-                       PDEBUG(DBG_RIO, "%s: VIDIOCGFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
-                       return 0;
-               }
-               case VIDIOCSFREQ:
-               {
-                       unsigned long *freq = arg;
-
-                       usbvision->freq = *freq;
-                       call_i2c_clients(usbvision, cmd, freq);
-                       PDEBUG(DBG_RIO, "%s: VIDIOCSFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4);
-                       return 0;
-               }
-               default:
-               {
-                       PDEBUG(DBG_RIO, "%s: Unknown command %d", __FUNCTION__, cmd);
-                       return -ENOIOCTLCMD;
-               }
-       }
-       return 0;
-}
-
-static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, unsigned long arg)
-{
-       return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
-}
-
-
-
-static void usbvision_configure_video(struct usb_usbvision *usbvision)
-{
-       int model,i;
-
-       if (usbvision == NULL)
-               return;
-
-       model = usbvision->DevModel;
-       usbvision->depth = 24;
-       usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
-
-       if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
-               usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
-       } else {
-               usbvision->Vin_Reg2_Preset = 0;
-       }
-
-       memset(&usbvision->vcap, 0, sizeof(usbvision->vcap));
-       strcpy(usbvision->vcap.driver, "USBVision");
-       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 */
-
-       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 */
-
-       usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
-       usbvision->ctl_input = 0;
-
-       /* This should be here to make i2c clients to be able to register */
-       usbvision_audio_off(usbvision); //first switch off audio
-       if (!PowerOnAtOpen) {
-               usbvision_power_on(usbvision);  //and then power up the noisy tuner
-               usbvision_init_i2c(usbvision);
-       }
-}
-
-//
-// Video registration stuff
-//
-
-// Video template
-static struct file_operations usbvision_fops = {
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .open           = usbvision_v4l2_open,
-       .release        = usbvision_v4l2_close,
-       .read           = usbvision_v4l2_read,
-       .mmap           = usbvision_v4l2_mmap,
-       .ioctl          = usbvision_v4l2_ioctl,
-       .llseek         = no_llseek,
-};
-static struct video_device usbvision_video_template = {
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .type           = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
-       .hardware       = VID_HARDWARE_USBVISION,
-       .fops           = &usbvision_fops,
-  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
-       .name           = "usbvision-video",
-       .release        = video_device_release,
-  #endif
-       .minor          = -1,
-};
-
-
-// Radio template
-static struct file_operations usbvision_radio_fops = {
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .open           = usbvision_radio_open,
-       .release        = usbvision_radio_close,
-       .ioctl          = usbvision_radio_ioctl,
-       .llseek         = no_llseek,
-};
-
-static struct video_device usbvision_radio_template=
-{
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .type           = VID_TYPE_TUNER,
-       .hardware       = VID_HARDWARE_USBVISION,
-       .fops           = &usbvision_radio_fops,
-  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
-       .release        = video_device_release,
-       .name           = "usbvision-radio",
-  #endif
-       .minor          = -1,
-};
-
-
-// vbi template
-static struct file_operations usbvision_vbi_fops = {
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .open           = usbvision_vbi_open,
-       .release        = usbvision_vbi_close,
-       .ioctl          = usbvision_vbi_ioctl,
-       .llseek         = no_llseek,
-};
-
-static struct video_device usbvision_vbi_template=
-{
-  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
-       .owner             = THIS_MODULE,
-  #endif
-       .type           = VID_TYPE_TUNER,
-       .hardware       = VID_HARDWARE_USBVISION,
-       .fops           = &usbvision_vbi_fops,
-  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
-       .release        = video_device_release,
-       .name           = "usbvision-vbi",
-  #endif
-       .minor          = -1,
-};
-
-
-static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
-                                       struct video_device *vdev_template,
-                                       char *name)
-{
-       struct usb_device *usb_dev = usbvision->dev;
-       struct video_device *vdev;
-
-       if (usb_dev == NULL) {
-               err("%s: usbvision->dev is not set", __FUNCTION__);
-               return NULL;
-       }
-
-       vdev = video_device_alloc();
-       if (NULL == vdev) {
-               return NULL;
-       }
-       *vdev = *vdev_template;
-//     vdev->minor   = -1;
-       vdev->dev     = &usb_dev->dev;
-       snprintf(vdev->name, sizeof(vdev->name), "%s", name);
-       video_set_drvdata(vdev, usbvision);
-       return vdev;
-}
-
-// unregister video4linux devices
-static void usbvision_unregister_video(struct usb_usbvision *usbvision)
-{
-       // vbi Device:
-       if (usbvision->vbi) {
-               PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
-               if (usbvision->vbi->minor != -1) {
-                       video_unregister_device(usbvision->vbi);
-               }
-               else {
-                       video_device_release(usbvision->vbi);
-               }
-               usbvision->vbi = NULL;
-       }
-
-       // Radio Device:
-       if (usbvision->rdev) {
-               PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
-               if (usbvision->rdev->minor != -1) {
-                       video_unregister_device(usbvision->rdev);
-               }
-               else {
-                       video_device_release(usbvision->rdev);
-               }
-               usbvision->rdev = NULL;
-       }
-
-       // Video Device:
-       if (usbvision->vdev) {
-               PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
-               if (usbvision->vdev->minor != -1) {
-                       video_unregister_device(usbvision->vdev);
-               }
-               else {
-                       video_device_release(usbvision->vdev);
-               }
-               usbvision->vdev = NULL;
-       }
-}
-
-// register video4linux devices
-static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
-{
-       // Video Device:
-       usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
-       if (usbvision->vdev == NULL) {
-               goto err_exit;
-       }
-       if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
-               goto err_exit;
-       }
-       info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
-
-       // Radio Device:
-       if (usbvision_device_data[usbvision->DevModel].Radio) {
-               // usbvision has radio
-               usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
-               if (usbvision->rdev == NULL) {
-                       goto err_exit;
-               }
-               if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
-                       goto err_exit;
-               }
-               info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
-       }
-       // vbi Device:
-       if (usbvision_device_data[usbvision->DevModel].vbi) {
-               usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
-               if (usbvision->vdev == NULL) {
-                       goto err_exit;
-               }
-               if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
-                       goto err_exit;
-               }
-               info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
-       }
-       // all done
-       return 0;
-
- err_exit:
-       err("USBVision[%d]: video_register_device() failed", usbvision->nr);
-       usbvision_unregister_video(usbvision);
-       return -1;
-}
-
-/*
- * usbvision_alloc()
- *
- * This code allocates the struct usb_usbvision. It is filled with default values.
- *
- * Returns NULL on error, a pointer to usb_usbvision else.
- *
- */
-static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
-{
-       struct usb_usbvision *usbvision;
-
-       if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
-               goto err_exit;
-       }
-
-       usbvision->dev = dev;
-
-       init_MUTEX(&usbvision->lock);   /* to 1 == available */
-
-       // prepare control urb for control messages during interrupts
-       usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
-       if (usbvision->ctrlUrb == NULL) {
-               goto err_exit;
-       }
-       init_waitqueue_head(&usbvision->ctrlUrb_wq);
-       init_MUTEX(&usbvision->ctrlUrbLock);    /* to 1 == available */
-
-       init_timer(&usbvision->powerOffTimer);
-       usbvision->powerOffTimer.data = (long) usbvision;
-       usbvision->powerOffTimer.function = usbvision_powerOffTimer;
-
-       return usbvision;
-
-err_exit:
-       if (usbvision && usbvision->ctrlUrb) {
-               usb_free_urb(usbvision->ctrlUrb);
-       }
-       if (usbvision) {
-               kfree(usbvision);
-       }
-       return NULL;
-}
-
-/*
- * usbvision_release()
- *
- * This code does final release of struct usb_usbvision. This happens
- * after the device is disconnected -and- all clients closed their files.
- *
- */
-static void usbvision_release(struct usb_usbvision *usbvision)
-{
-       PDEBUG(DBG_PROBE, "");
-
-       down(&usbvision->lock);
-
-       if (timer_pending(&usbvision->powerOffTimer)) {
-               del_timer(&usbvision->powerOffTimer);
-       }
-
-       usbvision->usbvision_used = 0;
-       usbvision->initialized = 0;
-
-       up(&usbvision->lock);
-
-       usbvision_remove_sysfs(usbvision->vdev);
-       usbvision_unregister_video(usbvision);
-       if(dga) {
-               if (usbvision->overlay_base) {
-                       iounmap(usbvision->overlay_base);
-               }
-       }
-
-       if (usbvision->ctrlUrb) {
-               usb_free_urb(usbvision->ctrlUrb);
-       }
-
-       kfree(usbvision);
-
-       PDEBUG(DBG_PROBE, "success");
-}
-
-
-/*
- * usbvision_probe()
- *
- * This procedure queries device descriptor and accepts the interface
- * if it looks like USBVISION video device
- *
- */
-static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
-{
-       struct usb_device *dev = interface_to_usbdev(intf);
-       __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
-       const struct usb_host_interface *interface;
-       struct usb_usbvision *usbvision = NULL;
-       const struct usb_endpoint_descriptor *endpoint;
-       int model;
-
-       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) {
-                       continue;
-               }
-               if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
-                       continue;
-               }
-
-               info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
-               break;
-       }
-
-       if (usbvision_device_data[model].idVendor == 0) {
-               return -ENODEV; //no matching device
-       }
-       if (usbvision_device_data[model].Interface >= 0) {
-               interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
-       }
-       else {
-               interface = &dev->actconfig->interface[ifnum]->altsetting[0];
-       }
-       endpoint = &interface->endpoint[1].desc;
-       if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
-               err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
-               err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
-               return -ENODEV;
-       }
-       if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
-               err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
-               return -ENODEV;
-       }
-
-       usb_get_dev(dev);
-
-       if ((usbvision = usbvision_alloc(dev)) == NULL) {
-               err("%s: couldn't allocate USBVision struct", __FUNCTION__);
-               return -ENOMEM;
-       }
-       if (dev->descriptor.bNumConfigurations > 1) {
-               usbvision->bridgeType = BRIDGE_NT1004;
-       }
-       else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
-               usbvision->bridgeType = BRIDGE_NT1005;
-       }
-       else {
-               usbvision->bridgeType = BRIDGE_NT1003;
-       }
-       PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
-
-       down(&usbvision->lock);
-
-       usbvision->nr = usbvision_nr++;
-
-       usbvision->have_tuner = usbvision_device_data[model].Tuner;
-       if (usbvision->have_tuner) {
-               usbvision->tuner_type = usbvision_device_data[model].TunerType;
-       }
-
-       usbvision->tuner_addr = ADDR_UNSET;
-
-       usbvision->DevModel = model;
-       usbvision->remove_pending = 0;
-       usbvision->last_error = 0;
-       usbvision->iface = ifnum;
-       usbvision->ifaceAltInactive = 0;
-       usbvision->ifaceAltActive = 1;
-       usbvision->video_endp = endpoint->bEndpointAddress;
-       usbvision->isocPacketSize = 0;
-       usbvision->usb_bandwidth = 0;
-       usbvision->user = 0;
-       usbvision->streaming = Stream_Off;
-       usbvision_register_video(usbvision);
-       usbvision_configure_video(usbvision);
-       up(&usbvision->lock);
-
-
-       usb_set_intfdata (intf, usbvision);
-       usbvision_create_sysfs(usbvision->vdev);
-
-       PDEBUG(DBG_PROBE, "success");
-       return 0;
-}
-
-
-/*
- * usbvision_disconnect()
- *
- * This procedure stops all driver activity, deallocates interface-private
- * structure (pointed by 'ptr') and after that driver should be removable
- * with no ill consequences.
- *
- */
-static void __devexit usbvision_disconnect(struct usb_interface *intf)
-{
-       struct usb_usbvision *usbvision = usb_get_intfdata(intf);
-
-       PDEBUG(DBG_PROBE, "");
-
-       if (usbvision == NULL) {
-               err("%s: usb_get_intfdata() failed", __FUNCTION__);
-               return;
-       }
-       usb_set_intfdata (intf, NULL);
-
-       down(&usbvision->lock);
-
-       // At this time we ask to cancel outstanding URBs
-       usbvision_stop_isoc(usbvision);
-
-       if (usbvision->power) {
-               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
-               usbvision_power_off(usbvision);
-       }
-       usbvision->remove_pending = 1;  // Now all ISO data will be ignored
-
-       usb_put_dev(usbvision->dev);
-       usbvision->dev = NULL;  // USB device is no more
-
-       up(&usbvision->lock);
-
-       if (usbvision->user) {
-               info("%s: In use, disconnect pending", __FUNCTION__);
-               wake_up_interruptible(&usbvision->wait_frame);
-               wake_up_interruptible(&usbvision->wait_stream);
-       }
-       else {
-               usbvision_release(usbvision);
-       }
-
-       PDEBUG(DBG_PROBE, "success");
-
-}
-
-static struct usb_driver usbvision_driver = {
-       .name           = "usbvision",
-       .id_table       = usbvision_table,
-       .probe          = usbvision_probe,
-       .disconnect     = usbvision_disconnect
-};
-
-/*
- * customdevice_process()
- *
- * This procedure preprocesses CustomDevice parameter if any
- *
- */
-void customdevice_process(void)
-{
-       usbvision_device_data[0]=usbvision_device_data[1];
-       usbvision_table[0]=usbvision_table[1];
-
-       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"
-               usbvision_device_data[0].idVendor;
-               usbvision_device_data[0].idProduct;
-               usbvision_device_data[0].Interface;
-               usbvision_device_data[0].Codec;
-               usbvision_device_data[0].VideoChannels;
-               usbvision_device_data[0].VideoNorm;
-               usbvision_device_data[0].AudioChannels;
-               usbvision_device_data[0].Radio;
-               usbvision_device_data[0].Tuner;
-               usbvision_device_data[0].TunerType;
-               usbvision_device_data[0].Vin_Reg1;
-               usbvision_device_data[0].Vin_Reg2;
-               usbvision_device_data[0].X_Offset;
-               usbvision_device_data[0].Y_Offset;
-               usbvision_device_data[0].Dvi_yuv;
-               usbvision_device_data[0].ModelString;
-               */
-
-               rmspace(parse);
-               usbvision_device_data[0].ModelString="USBVISION Custom Device";
-
-               parse+=2;
-               sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
-               parse+=2;
-               sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
-               sscanf(parse,"%d",&usbvision_device_data[0].Interface);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
-               sscanf(parse,"%d",&usbvision_device_data[0].Codec);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
-               sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
-
-               switch(*parse)
-               {
-                       case 'P':
-                               PDEBUG(DBG_PROBE, "VideoNorm=PAL");
-                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
-                               break;
-
-                       case 'S':
-                               PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
-                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
-                               break;
-
-                       case 'N':
-                               PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
-                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
-                               break;
-
-                       default:
-                               PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
-                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
-                               break;
-               }
-               goto2next(parse);
-
-               sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
-               sscanf(parse,"%d",&usbvision_device_data[0].Radio);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
-               sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
-               sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
-               sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
-               sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
-               sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
-               sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
-               goto2next(parse);
-               PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
-               sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
-               PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
-
-               //add to usbvision_table also
-               usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
-               usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
-               usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
-
-       }
-}
-
-
-
-/*
- * usbvision_init()
- *
- * This code is run to initialize the driver.
- *
- */
-static int __init usbvision_init(void)
-{
-       int errCode;
-
-       PDEBUG(DBG_PROBE, "");
-
-       PDEBUG(DBG_IOCTL, "IOCTL   debugging is enabled");
-       PDEBUG(DBG_IO,  "IO      debugging is enabled");
-       PDEBUG(DBG_RIO,  "RIO     debugging is enabled");
-       PDEBUG(DBG_HEADER, "HEADER  debugging is enabled");
-       PDEBUG(DBG_PROBE, "PROBE   debugging is enabled");
-       PDEBUG(DBG_IRQ,  "IRQ     debugging is enabled");
-       PDEBUG(DBG_ISOC, "ISOC    debugging is enabled");
-       PDEBUG(DBG_PARSE, "PARSE   debugging is enabled");
-       PDEBUG(DBG_SCRATCH, "SCRATCH debugging is enabled");
-       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 ) {
-               // 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();
-
-       errCode = usb_register(&usbvision_driver);
-
-       if (errCode == 0) {
-               info(DRIVER_DESC " : " DRIVER_VERSION);
-               PDEBUG(DBG_PROBE, "success");
-       }
-       return errCode;
-}
-
-static void __exit usbvision_exit(void)
-{
- PDEBUG(DBG_PROBE, "");
-
- usb_deregister(&usbvision_driver);
- PDEBUG(DBG_PROBE, "success");
-}
-
-module_init(usbvision_init);
-module_exit(usbvision_exit);
-
 /*
  * Overrides for Emacs so that we follow Linus's tabbing style.
  * ---------------------------------------------------------------------------
index 48afcd2aca3f6efecaf1e0c9900595cd053db217..2de74e48cbe59f1f6d2ed9cdbcdcf04043de9d94 100644 (file)
 #include <linux/sched.h>
 #include <linux/usb.h>
 #include <linux/i2c.h>
-#include "usbvision-i2c.h"
+#include "usbvision.h"
 
-static int debug_i2c_usb = 0;
+#define DBG_I2C                1<<0
+#define DBG_ALGO       1<<1
 
-#if defined(module_param)                               // Showing parameters under SYSFS
-module_param (debug_i2c_usb, int, 0444);                       // debug_i2c_usb mode of the device driver
-#else
-MODULE_PARM(debug_i2c_usb, "i");                               // debug_i2c_usb mode of the device driver
-#endif
+static int i2c_debug = 0;
+
+module_param (i2c_debug, int, 0644);                   // debug_i2c_usb mode of the device driver
+MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
 
+#define PDEBUG(level, fmt, args...) \
+               if (i2c_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
 
 static inline int try_write_address(struct i2c_adapter *i2c_adap,
                                    unsigned char addr, int retries)
@@ -67,11 +69,9 @@ static inline int try_write_address(struct i2c_adapter *i2c_adap,
                        break;
                udelay(adap->udelay);
        }
-       if (debug_i2c_usb) {
-               if (i) {
-                       info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
-                       info("%s: Maybe there's no device at this address", __FUNCTION__);
-               }
+       if (i) {
+               PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
+               PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
        }
        return ret;
 }
@@ -94,11 +94,9 @@ static inline int try_read_address(struct i2c_adapter *i2c_adap,
                        break;
                udelay(adap->udelay);
        }
-       if (debug_i2c_usb) {
-               if (i) {
-                       info("%s: Needed %d retries for address %#2x", __FUNCTION__, i, addr);
-                       info("%s: Maybe there's no device at this address", __FUNCTION__);
-               }
+       if (i) {
+               PDEBUG(DBG_ALGO,"Needed %d retries for address %#2x", i, addr);
+               PDEBUG(DBG_ALGO,"Maybe there's no device at this address");
        }
        return ret;
 }
@@ -166,9 +164,7 @@ usb_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[], int num)
                pmsg = &msgs[i];
                ret = usb_find_address(i2c_adap, pmsg, i2c_adap->retries, &addr);
                if (ret != 0) {
-                       if (debug_i2c_usb) {
-                               info("%s: got NAK from device, message #%d\n", __FUNCTION__, i);
-                       }
+                       PDEBUG(DBG_ALGO,"got NAK from device, message #%d", i);
                        return (ret < 0) ? ret : -EREMOTEIO;
                }
 
@@ -215,6 +211,9 @@ static struct i2c_algorithm i2c_usb_algo = {
  */
 int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
 {
+       PDEBUG(DBG_I2C, "I2C   debugging is enabled [i2c]");
+       PDEBUG(DBG_ALGO, "ALGO   debugging is enabled [i2c]");
+
        /* register new adapter to i2c module... */
 
        adap->algo = &i2c_usb_algo;
@@ -222,17 +221,9 @@ int usbvision_i2c_usb_add_bus(struct i2c_adapter *adap)
        adap->timeout = 100;    /* default values, should       */
        adap->retries = 3;      /* be replaced by defines       */
 
-#ifdef MODULE
-       #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
-               MOD_INC_USE_COUNT;
-       #endif
-#endif
-
        i2c_add_adapter(adap);
 
-       if (debug_i2c_usb) {
-               info("i2c bus for %s registered", adap->name);
-       }
+       PDEBUG(DBG_ALGO,"i2c bus for %s registered", adap->name);
 
        return 0;
 }
@@ -243,17 +234,338 @@ int usbvision_i2c_usb_del_bus(struct i2c_adapter *adap)
 
        i2c_del_adapter(adap);
 
-       if (debug_i2c_usb) {
-               info("i2c bus for %s unregistered", adap->name);
+       PDEBUG(DBG_ALGO,"i2c bus for %s unregistered", adap->name);
+
+       return 0;
+}
+
+
+/* ----------------------------------------------------------------------- */
+/* usbvision specific I2C functions                                        */
+/* ----------------------------------------------------------------------- */
+static struct i2c_adapter i2c_adap_template;
+static struct i2c_algo_usb_data i2c_algo_template;
+static struct i2c_client i2c_client_template;
+
+int usbvision_init_i2c(struct usb_usbvision *usbvision)
+{
+       memcpy(&usbvision->i2c_adap, &i2c_adap_template,
+              sizeof(struct i2c_adapter));
+       memcpy(&usbvision->i2c_algo, &i2c_algo_template,
+              sizeof(struct i2c_algo_usb_data));
+       memcpy(&usbvision->i2c_client, &i2c_client_template,
+              sizeof(struct i2c_client));
+
+       sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name),
+               " #%d", usbvision->vdev->minor & 0x1f);
+       PDEBUG(DBG_I2C,"Adaptername: %s", usbvision->i2c_adap.name);
+
+       i2c_set_adapdata(&usbvision->i2c_adap, usbvision);
+       i2c_set_clientdata(&usbvision->i2c_client, usbvision);
+       i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision);
+
+       usbvision->i2c_adap.algo_data = &usbvision->i2c_algo;
+       usbvision->i2c_client.adapter = &usbvision->i2c_adap;
+
+       if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) {
+               printk(KERN_ERR "usbvision_init_i2c: can't write reg\n");
+               return -EBUSY;
+       }
+
+#ifdef CONFIG_MODULES
+       /* Request the load of the i2c modules we need */
+       switch (usbvision_device_data[usbvision->DevModel].Codec) {
+       case CODEC_SAA7113:
+               request_module("saa7115");
+               break;
+       case CODEC_SAA7111:
+               request_module("saa7115");
+               break;
+       }
+       if (usbvision_device_data[usbvision->DevModel].Tuner == 1) {
+               request_module("tuner");
        }
-#ifdef MODULE
-       #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 21)
-               MOD_DEC_USE_COUNT;
-       #endif
 #endif
 
+       return usbvision_i2c_usb_add_bus(&usbvision->i2c_adap);
+}
+
+void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,
+                     void *arg)
+{
+       BUG_ON(NULL == usbvision->i2c_adap.algo_data);
+       i2c_clients_command(&usbvision->i2c_adap, cmd, arg);
+}
+
+static int attach_inform(struct i2c_client *client)
+{
+       struct usb_usbvision *usbvision;
+
+       usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
+
+       switch (client->addr << 1) {
+               case 0x43:
+               case 0x4b:
+               {
+                       struct tuner_setup tun_setup;
+
+                       tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
+                       tun_setup.type = TUNER_TDA9887;
+                       tun_setup.addr = client->addr;
+
+                       call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
+
+                       break;
+               }
+               case 0x42:
+                       PDEBUG(DBG_I2C,"attach_inform: saa7114 detected.");
+                       break;
+               case 0x4a:
+                       PDEBUG(DBG_I2C,"attach_inform: saa7113 detected.");
+                       break;
+               case 0xa0:
+                       PDEBUG(DBG_I2C,"attach_inform: eeprom detected.");
+                       break;
+
+               default:
+                       {
+                               struct tuner_setup tun_setup;
+
+                               PDEBUG(DBG_I2C,"attach inform: detected I2C address %x", client->addr << 1);
+                               usbvision->tuner_addr = client->addr;
+
+                               if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) {
+                                       tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
+                                       tun_setup.type = usbvision->tuner_type;
+                                       tun_setup.addr = usbvision->tuner_addr;
+                                       call_i2c_clients(usbvision, TUNER_SET_TYPE_ADDR, &tun_setup);
+                               }
+                       }
+                       break;
+       }
        return 0;
 }
 
+static int detach_inform(struct i2c_client *client)
+{
+       struct usb_usbvision *usbvision;
+
+       #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
+               usbvision = (struct usb_usbvision *)client->adapter->data;
+       #else
+               usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter);
+       #endif
+
+               PDEBUG(DBG_I2C,"usbvision[%d] detaches %s", usbvision->nr, client->name);
+       return 0;
+}
+
+static int
+usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr,
+                    char *buf, short len)
+{
+       int rc, retries;
+
+       for (retries = 5;;) {
+               rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr);
+               if (rc < 0)
+                       return rc;
+
+               /* Initiate byte read cycle                    */
+               /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */
+               /*                    d3 0=Wr 1=Rd             */
+               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
+                                     (len & 0x07) | 0x18);
+               if (rc < 0)
+                       return rc;
+
+               /* Test for Busy and ACK */
+               do {
+                       /* USBVISION_SER_CONT -> d4 == 0 busy */
+                       rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
+               } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
+               if (rc < 0)
+                       return rc;
+
+               /* USBVISION_SER_CONT -> d5 == 1 Not ack */
+               if ((rc & 0x20) == 0)   /* Ack? */
+                       break;
+
+               /* I2C abort */
+               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
+               if (rc < 0)
+                       return rc;
+
+               if (--retries < 0)
+                       return -1;
+       }
+
+       switch (len) {
+       case 4:
+               buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4);
+       case 3:
+               buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3);
+       case 2:
+               buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2);
+       case 1:
+               buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1);
+               break;
+       default:
+               printk(KERN_ERR
+                      "usbvision_i2c_read_max4: buffer length > 4\n");
+       }
+
+       if (i2c_debug & DBG_I2C) {
+               int idx;
+               for (idx = 0; idx < len; idx++) {
+                       PDEBUG(DBG_I2C,"read %x from address %x", (unsigned char)buf[idx], addr);
+               }
+       }
+       return len;
+}
+
+
+static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision,
+                                unsigned char addr, const char *buf,
+                                short len)
+{
+       int rc, retries;
+       int i;
+       unsigned char value[6];
+       unsigned char ser_cont;
+
+       ser_cont = (len & 0x07) | 0x10;
+
+       value[0] = addr;
+       value[1] = ser_cont;
+       for (i = 0; i < len; i++)
+               value[i + 2] = buf[i];
+
+       for (retries = 5;;) {
+               rc = usb_control_msg(usbvision->dev,
+                                    usb_sndctrlpipe(usbvision->dev, 1),
+                                    USBVISION_OP_CODE,
+                                    USB_DIR_OUT | USB_TYPE_VENDOR |
+                                    USB_RECIP_ENDPOINT, 0,
+                                    (__u16) USBVISION_SER_ADRS, value,
+                                    len + 2, HZ);
+
+               if (rc < 0)
+                       return rc;
+
+               rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT,
+                                     (len & 0x07) | 0x10);
+               if (rc < 0)
+                       return rc;
+
+               /* Test for Busy and ACK */
+               do {
+                       rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT);
+               } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */
+               if (rc < 0)
+                       return rc;
+
+               if ((rc & 0x20) == 0)   /* Ack? */
+                       break;
+
+               /* I2C abort */
+               usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00);
+
+               if (--retries < 0)
+                       return -1;
+
+       }
+
+       if (i2c_debug & DBG_I2C) {
+               int idx;
+               for (idx = 0; idx < len; idx++) {
+                       PDEBUG(DBG_I2C,"wrote %x at address %x", (unsigned char)buf[idx], addr);
+               }
+       }
+       return len;
+}
+
+static int usbvision_i2c_write(void *data, unsigned char addr, char *buf,
+                           short len)
+{
+       char *bufPtr = buf;
+       int retval;
+       int wrcount = 0;
+       int count;
+       int maxLen = 4;
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
+
+       while (len > 0) {
+               count = (len > maxLen) ? maxLen : len;
+               retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count);
+               if (retval > 0) {
+                       len -= count;
+                       bufPtr += count;
+                       wrcount += count;
+               } else
+                       return (retval < 0) ? retval : -EFAULT;
+       }
+       return wrcount;
+}
+
+static int usbvision_i2c_read(void *data, unsigned char addr, char *buf,
+                          short len)
+{
+       char temp[4];
+       int retval, i;
+       int rdcount = 0;
+       int count;
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) data;
+
+       while (len > 0) {
+               count = (len > 3) ? 4 : len;
+               retval = usbvision_i2c_read_max4(usbvision, addr, temp, count);
+               if (retval > 0) {
+                       for (i = 0; i < len; i++)
+                               buf[rdcount + i] = temp[i];
+                       len -= count;
+                       rdcount += count;
+               } else
+                       return (retval < 0) ? retval : -EFAULT;
+       }
+       return rdcount;
+}
+
+static struct i2c_algo_usb_data i2c_algo_template = {
+       .data           = NULL,
+       .inb            = usbvision_i2c_read,
+       .outb           = usbvision_i2c_write,
+       .udelay         = 10,
+       .mdelay         = 10,
+       .timeout        = 100,
+};
+
+static struct i2c_adapter i2c_adap_template = {
+       .owner = THIS_MODULE,
+       .name              = "usbvision",
+       .id                = I2C_HW_B_BT848, /* FIXME */
+       .algo              = NULL,
+       .algo_data         = NULL,
+       .client_register   = attach_inform,
+       .client_unregister = detach_inform,
+#if defined (I2C_ADAP_CLASS_TV_ANALOG)
+       .class             = I2C_ADAP_CLASS_TV_ANALOG,
+#elif defined (I2C_CLASS_TV_ANALOG)
+       .class             = I2C_CLASS_TV_ANALOG,
+#endif
+};
+
+static struct i2c_client i2c_client_template = {
+       .name           = "usbvision internal",
+};
+
 EXPORT_SYMBOL(usbvision_i2c_usb_add_bus);
 EXPORT_SYMBOL(usbvision_i2c_usb_del_bus);
+
+/*
+ * Overrides for Emacs so that we follow Linus's tabbing style.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-basic-offset: 8
+ * End:
+ */
diff --git a/drivers/media/video/usbvision/usbvision-i2c.h b/drivers/media/video/usbvision/usbvision-i2c.h
deleted file mode 100644 (file)
index a2df8db..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/* 
- * I2C_ALGO_USB.H
- *  i2c algorithm for USB-I2C Bridges
- *
- * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
- *
- * This module is part of usbvision driver project.
- * Updates to driver completed by Dwaine P. Garden
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-
-#ifndef I2C_ALGO_USB_H
-#define I2C_ALGO_USB_H 1 
-
-#include <linux/i2c.h>
-
-struct i2c_algo_usb_data {
-       void *data;             /* private data for lowlevel routines */
-       int (*inb) (void *data, unsigned char addr, char *buf, short len);
-       int (*outb) (void *data, unsigned char addr, char *buf, short len);
-
-       /* local settings */
-       int udelay;
-       int mdelay;
-       int timeout;
-};
-
-#define I2C_USB_ADAP_MAX       16
-
-int usbvision_i2c_usb_add_bus(struct i2c_adapter *);
-int usbvision_i2c_usb_del_bus(struct i2c_adapter *);
-
-static inline void *i2c_get_algo_usb_data (struct i2c_algo_usb_data *dev)
-{
-       return dev->data;
-}
-
-static inline void i2c_set_algo_usb_data (struct i2c_algo_usb_data *dev, void *data)
-{
-       dev->data = data;
-}
-
-
-#endif //I2C_ALGO_USB_H
diff --git a/drivers/media/video/usbvision/usbvision-video.c b/drivers/media/video/usbvision/usbvision-video.c
new file mode 100644 (file)
index 0000000..7e26d83
--- /dev/null
@@ -0,0 +1,2060 @@
+/*
+ * USB USBVISION Video device driver 0.9.9 
+ *
+ *
+ *
+ * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
+ *
+ * This module is part of usbvision driver project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Let's call the version 0.... until compression decoding is completely
+ * implemented.
+ *
+ * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
+ * It was based on USB CPiA driver written by Peter Pregler,
+ * Scott J. Bertin and Johannes Erdfelt
+ * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
+ * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
+ * Updates to driver completed by Dwaine P. Garden
+ *
+ *
+ * TODO:
+ *     - use submit_urb for all setup packets
+ *     - Fix memory settings for nt1004. It is 4 times as big as the
+ *       nt1003 memory.
+ *     - Add audio on endpoint 3 for nt1004 chip.  Seems impossible, needs a codec interface.  Which one?
+ *     - Clean up the driver.
+ *     - optimization for performance.
+ *     - Add Videotext capability (VBI).  Working on it.....
+ *     - Check audio for other devices
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+#include <linux/timer.h>
+#include <linux/slab.h>
+#include <linux/mm.h>
+#include <linux/utsname.h>
+#include <linux/highmem.h>
+#include <linux/smp_lock.h>
+#include <linux/videodev.h>
+#include <linux/vmalloc.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/spinlock.h>
+#include <asm/io.h>
+#include <linux/videodev2.h>
+#include <linux/video_decoder.h>
+#include <linux/i2c.h>
+
+#include <media/saa7115.h>
+#include <media/v4l2-common.h>
+#include <media/tuner.h>
+#include <media/audiochip.h>
+
+       #include <linux/moduleparam.h>
+       #include <linux/workqueue.h>
+
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+
+#include "usbvision.h"
+
+#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
+#define DRIVER_NAME "usbvision"
+#define DRIVER_ALIAS "USBVision"
+#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
+#define DRIVER_LICENSE "GPL"
+#define USBVISION_DRIVER_VERSION_MAJOR 0
+#define USBVISION_DRIVER_VERSION_MINOR 9
+#define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
+#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
+#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
+
+#define        ENABLE_HEXDUMP  0       /* Enable if you need it */
+
+
+#define USBVISION_DEBUG                /* Turn on debug messages */
+
+#ifdef USBVISION_DEBUG
+       #define PDEBUG(level, fmt, args...) \
+               if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
+#else
+       #define PDEBUG(level, fmt, args...) do {} while(0)
+#endif
+
+#define DBG_IOCTL      1<<0
+#define DBG_IO         1<<1
+#define DBG_PROBE      1<<2
+#define DBG_FUNC       1<<3
+
+//String operations
+#define rmspace(str)   while(*str==' ') str++;
+#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
+
+
+static int usbvision_nr = 0;                   // sequential number of usbvision device
+
+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" }
+};
+
+/* 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)
+
+// Function prototypes
+static void usbvision_release(struct usb_usbvision *usbvision);
+
+// Default initalization of device driver parameters
+static int isocMode = ISOC_MODE_COMPRESS;              // Set the default format for ISOC endpoint
+static int video_debug = 0;                            // Set the default Debug Mode of the device driver
+static int PowerOnAtOpen = 1;                          // Set the default device to power on at startup
+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....
+
+// Grab parameters for the device driver
+
+#if defined(module_param)                               // Showing parameters under SYSFS
+module_param(isocMode, int, 0444);
+module_param(video_debug, int, 0444);
+module_param(PowerOnAtOpen, int, 0444);
+module_param(video_nr, int, 0444);
+module_param(radio_nr, int, 0444);
+module_param(vbi_nr, int, 0444);
+module_param(CustomDevice, charp, 0444);
+#else                                                  // Old Style
+MODULE_PARAM(isocMode, "i");
+MODULE_PARM(video_debug, "i");                         // Grab the Debug Mode of the device driver
+MODULE_PARM(adjustCompression, "i");                   // Grab the compression to be adaptive
+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(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
+#endif
+
+MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
+MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
+MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened.  Default: 1 (On)");
+MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
+MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
+MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX).  Default: -1 (autodetect)");
+MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device.  Default: null");
+
+
+// Misc stuff
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE(DRIVER_LICENSE);
+       MODULE_VERSION(USBVISION_VERSION_STRING);
+       MODULE_ALIAS(DRIVER_ALIAS);
+
+
+/****************************************************************************************/
+/* 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    */
+/****************************************************************************************/
+
+
+#define YES_NO(x) ((x) ? "Yes" : "No")
+
+static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
+{
+       struct video_device *vdev = to_video_device(cd);
+       return video_get_drvdata(vdev);
+}
+
+static ssize_t show_version(struct class_device *cd, char *buf)
+{
+       return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
+}
+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);
+       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 v4l2_control ctrl;
+       ctrl.id = V4L2_CID_HUE;
+       ctrl.value = 0;
+       call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
+       return sprintf(buf, "%d\n", ctrl.value >> 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 v4l2_control ctrl;
+       ctrl.id = V4L2_CID_CONTRAST;
+       ctrl.value = 0;
+       call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
+       return sprintf(buf, "%d\n", ctrl.value >> 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 v4l2_control ctrl;
+       ctrl.id = V4L2_CID_BRIGHTNESS;
+       ctrl.value = 0;
+       call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
+       return sprintf(buf, "%d\n", ctrl.value >> 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 v4l2_control ctrl;
+       ctrl.id = V4L2_CID_SATURATION;
+       ctrl.value = 0;
+       call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
+       return sprintf(buf, "%d\n", ctrl.value >> 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==Stream_On?1:0));
+}
+static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, 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);
+       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);
+       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) {
+               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_compression);
+               res=video_device_create_file(vdev, &class_device_attr_bridge);
+       }
+}
+
+static void usbvision_remove_sysfs(struct video_device *vdev)
+{
+       if (vdev) {
+               video_device_remove_file(vdev, &class_device_attr_version);
+               video_device_remove_file(vdev, &class_device_attr_model);
+               video_device_remove_file(vdev, &class_device_attr_hue);
+               video_device_remove_file(vdev, &class_device_attr_contrast);
+               video_device_remove_file(vdev, &class_device_attr_brightness);
+               video_device_remove_file(vdev, &class_device_attr_saturation);
+               video_device_remove_file(vdev, &class_device_attr_streaming);
+               video_device_remove_file(vdev, &class_device_attr_compression);
+               video_device_remove_file(vdev, &class_device_attr_bridge);
+       }
+}
+
+
+/*
+ * usbvision_open()
+ *
+ * This is part of Video 4 Linux API. The driver can be opened by one
+ * client only (checks internal counter 'usbvision->user'). The procedure
+ * then allocates buffers needed for video processing.
+ *
+ */
+static int usbvision_v4l2_open(struct inode *inode, struct file *file)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+       int errCode = 0;
+
+       PDEBUG(DBG_IO, "open");
+
+
+       usbvision_reset_powerOffTimer(usbvision);
+
+       if (usbvision->user)
+               errCode = -EBUSY;
+       else {
+               /* Allocate memory for the frame buffers */
+               errCode = usbvision_frames_alloc(usbvision);
+               if(!errCode) {
+                       /* Allocate memory for the scratch ring buffer */
+                       errCode = usbvision_scratch_alloc(usbvision);
+                       if(!errCode) {
+                               /* Allocate memory for the USB S buffers */
+                               errCode = usbvision_sbuf_alloc(usbvision);
+                               if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) {
+                                       /* Allocate intermediate decompression buffers only if needed */
+                                       errCode = usbvision_decompress_alloc(usbvision);
+                               }
+                       }
+               }
+               if (errCode) {
+                       /* Deallocate all buffers if trouble */
+                       usbvision_frames_free(usbvision);
+                       usbvision_scratch_free(usbvision);
+                       usbvision_sbuf_free(usbvision);
+                       usbvision_decompress_free(usbvision);
+               }
+       }
+
+       /* If so far no errors then we shall start the camera */
+       if (!errCode) {
+               down(&usbvision->lock);
+               if (usbvision->power == 0) {
+                       usbvision_power_on(usbvision);
+                       usbvision_init_i2c(usbvision);
+               }
+
+               /* Send init sequence only once, it's large! */
+               if (!usbvision->initialized) {
+                       int setup_ok = 0;
+                       setup_ok = usbvision_setup(usbvision,isocMode);
+                       if (setup_ok)
+                               usbvision->initialized = 1;
+                       else
+                               errCode = -EBUSY;
+               }
+
+               if (!errCode) {
+                       usbvision_begin_streaming(usbvision);
+                       errCode = usbvision_init_isoc(usbvision);
+                       /* device needs to be initialized before isoc transfer */
+                       usbvision_muxsel(usbvision,0);
+                       usbvision->user++;
+               }
+               else {
+                       if (PowerOnAtOpen) {
+                               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
+                               usbvision_power_off(usbvision);
+                               usbvision->initialized = 0;
+                       }
+               }
+               up(&usbvision->lock);
+       }
+
+       if (errCode) {
+       }
+
+       /* prepare queues */
+       usbvision_empty_framequeues(usbvision);
+
+       PDEBUG(DBG_IO, "success");
+       return errCode;
+}
+
+/*
+ * usbvision_v4l2_close()
+ *
+ * This is part of Video 4 Linux API. The procedure
+ * stops streaming and deallocates all buffers that were earlier
+ * allocated in usbvision_v4l2_open().
+ *
+ */
+static int usbvision_v4l2_close(struct inode *inode, struct file *file)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+
+       PDEBUG(DBG_IO, "close");
+       down(&usbvision->lock);
+
+       usbvision_audio_off(usbvision);
+       usbvision_restart_isoc(usbvision);
+       usbvision_stop_isoc(usbvision);
+
+       usbvision_decompress_free(usbvision);
+       usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
+       usbvision_scratch_free(usbvision);
+       usbvision_sbuf_free(usbvision);
+
+       usbvision->user--;
+
+       if (PowerOnAtOpen) {
+               /* power off in a little while to avoid off/on every close/open short sequences */
+               usbvision_set_powerOffTimer(usbvision);
+               usbvision->initialized = 0;
+       }
+
+       up(&usbvision->lock);
+
+       if (usbvision->remove_pending) {
+               info("%s: Final disconnect", __FUNCTION__);
+               usbvision_release(usbvision);
+       }
+
+       PDEBUG(DBG_IO, "success");
+
+
+       return 0;
+}
+
+
+/*
+ * usbvision_ioctl()
+ *
+ * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
+ *
+ */
+static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
+                                unsigned int cmd, void *arg)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+
+       if (!USBVISION_IS_OPERATIONAL(usbvision))
+               return -EFAULT;
+
+       switch (cmd) {
+
+#ifdef CONFIG_VIDEO_ADV_DEBUG
+               /* ioctls to allow direct acces to the NT100x registers */
+               case VIDIOC_INT_G_REGISTER:
+               {
+                       struct v4l2_register *reg = arg;
+                       int errCode;
+
+                       if (reg->i2c_id != 0)
+                               return -EINVAL;
+                       /* NT100x has a 8-bit register space */
+                       errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
+                       if (errCode < 0) {
+                               err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
+                       }
+                       else {
+                               reg->val=(unsigned char)errCode;
+                               PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
+                                                       (unsigned int)reg->reg, reg->val);
+                               errCode = 0; // No error
+                       }
+                       return errCode;
+               }
+               case VIDIOC_INT_S_REGISTER:
+               {
+                       struct v4l2_register *reg = arg;
+                       int errCode;
+
+                       if (reg->i2c_id != 0)
+                               return -EINVAL;
+                       if (!capable(CAP_SYS_ADMIN))
+                               return -EPERM;
+                       errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
+                       if (errCode < 0) {
+                               err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
+                       }
+                       else {
+                               PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
+                                                       (unsigned int)reg->reg, reg->val);
+                               errCode = 0;
+                       }
+                       return 0;
+               }
+#endif
+               case VIDIOC_QUERYCAP:
+               {
+                       struct v4l2_capability *vc=arg;
+
+                       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 |
+                               (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
+                       PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
+                       return 0;
+               }
+               case VIDIOC_ENUMINPUT:
+               {
+                       struct v4l2_input *vi = arg;
+                       int chan;
+
+                       if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
+                               return -EINVAL;
+                       if (usbvision->have_tuner) {
+                               chan = vi->index;
+                       }
+                       else {
+                               chan = vi->index + 1; //skip Television string
+                       }
+                       switch(chan) {
+                               case 0:
+                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
+                                               strcpy(vi->name, "White Video Input");
+                                       }
+                                       else {
+                                               strcpy(vi->name, "Television");
+                                               vi->type = V4L2_INPUT_TYPE_TUNER;
+                                               vi->audioset = 1;
+                                               vi->tuner = chan;
+                                               vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
+                                       }
+                                       break;
+                               case 1:
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
+                                               strcpy(vi->name, "Green Video Input");
+                                       }
+                                       else {
+                                               strcpy(vi->name, "Composite Video Input");
+                                       }
+                                       vi->std = V4L2_STD_PAL;
+                                       break;
+                               case 2:
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
+                                               strcpy(vi->name, "Yellow Video Input");
+                                       }
+                                       else {
+                                       strcpy(vi->name, "S-Video Input");
+                                       }
+                                       vi->std = V4L2_STD_PAL;
+                                       break;
+                               case 3:
+                                       vi->type = V4L2_INPUT_TYPE_CAMERA;
+                                       strcpy(vi->name, "Red Video Input");
+                                       vi->std = V4L2_STD_PAL;
+                                       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 *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:
+               {
+                       int *input = arg;
+                       *input = usbvision->ctl_input;
+                       return 0;
+               }
+               case VIDIOC_S_INPUT:
+               {
+                       int *input = arg;
+                       if ((*input >= usbvision->video_inputs) || (*input < 0) )
+                               return -EINVAL;
+                       usbvision->ctl_input = *input;
+
+                       down(&usbvision->lock);
+                       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:
+               {
+                       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:
+               {
+                       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->tvnorm = &tvnorms[i];
+
+                       call_i2c_clients(usbvision, VIDIOC_S_STD,
+                                        &usbvision->tvnorm->id);
+
+                       up(&usbvision->lock);
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
+                       return 0;
+               }
+               case VIDIOC_G_TUNER:
+               {
+                       struct v4l2_tuner *vt = arg;
+
+                       if (!usbvision->have_tuner || vt->index)        // Only tuner 0
+                               return -EINVAL;
+                       strcpy(vt->name, "Television");
+                       /* 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:
+               {
+                       struct v4l2_tuner *vt = arg;
+
+                       // Only no or one tuner for now
+                       if (!usbvision->have_tuner || vt->index)
+                               return -EINVAL;
+                       /* 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;
+                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
+                       return 0;
+               }
+               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);
+                       return 0;
+               }
+               case VIDIOC_G_AUDIO:
+               {
+                       struct v4l2_audio *v = arg;
+                       memset(v,0, sizeof(v));
+                       strcpy(v->name, "TV");
+                       PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
+                       return 0;
+               }
+               case VIDIOC_S_AUDIO:
+               {
+                       struct v4l2_audio *v = arg;
+                       if(v->index) {
+                               return -EINVAL;
+                       }
+                       PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
+                       return 0;
+               }
+               case VIDIOC_QUERYCTRL:
+               {
+                       struct v4l2_queryctrl *ctrl = arg;
+                       int id=ctrl->id;
+
+                       memset(ctrl,0,sizeof(*ctrl));
+                       ctrl->id=id;
+
+                       call_i2c_clients(usbvision, cmd, arg);
+
+                       if (ctrl->type)
+                               return 0;
+                       else
+                               return -EINVAL;
+
+                       PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
+               }
+               case VIDIOC_G_CTRL:
+               {
+                       struct v4l2_control *ctrl = arg;
+                       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;
+               }
+               case VIDIOC_REQBUFS:
+               {
+                       struct v4l2_requestbuffers *vr = arg;
+                       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;
+
+                       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:
+               {
+                       struct v4l2_buffer *vb = arg;
+                       struct usbvision_frame *frame;
+
+                       // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
+
+                       if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
+                               return -EINVAL;
+                       }
+                       if(vb->index>=USBVISION_NUMFRAMES)  {
+                               return -EINVAL;
+                       }
+                       // Updating the corresponding frame state
+                       vb->flags = 0;
+                       frame = &usbvision->frame[vb->index];
+                       if(frame->grabstate >= FrameState_Ready)
+                               vb->flags |= V4L2_BUF_FLAG_QUEUED;
+                       if(frame->grabstate >= FrameState_Done)
+                               vb->flags |= V4L2_BUF_FLAG_DONE;
+                       if(frame->grabstate == FrameState_Unused)
+                               vb->flags |= V4L2_BUF_FLAG_MAPPED;
+                       vb->memory = V4L2_MEMORY_MMAP;
+
+                       vb->m.offset = vb->index*usbvision->max_frame_size;
+
+                       vb->memory = V4L2_MEMORY_MMAP;
+                       vb->field = V4L2_FIELD_NONE;
+                       vb->length = usbvision->max_frame_size;
+                       vb->timestamp = usbvision->frame[vb->index].timestamp;
+                       vb->sequence = usbvision->frame[vb->index].sequence;
+                       return 0;
+               }
+               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>=USBVISION_NUMFRAMES)  {
+                               return -EINVAL;
+                       }
+
+                       frame = &usbvision->frame[vb->index];
+
+                       if (frame->grabstate != FrameState_Unused) {
+                               return -EAGAIN;
+                       }
+
+                       /* 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;
+
+                       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);
+
+                       PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
+                       return 0;
+               }
+               case VIDIOC_DQBUF:
+               {
+                       struct v4l2_buffer *vb = arg;
+                       int ret;
+                       struct usbvision_frame *f;
+                       unsigned long lock_flags;
+
+                       if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+                               return -EINVAL;
+
+                       if (list_empty(&(usbvision->outqueue))) {
+                               if (usbvision->streaming == Stream_Idle)
+                                       return -EINVAL;
+                               ret = wait_event_interruptible
+                                       (usbvision->wait_frame,
+                                        !list_empty(&(usbvision->outqueue)));
+                               if (ret)
+                                       return ret;
+                       }
+
+                       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;
+                       vb->timestamp = f->timestamp;
+                       vb->field = V4L2_FIELD_NONE;
+                       vb->bytesused = f->scanlength;
+
+                       return 0;
+               }
+               case VIDIOC_STREAMON:
+               {
+                       int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+                       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;
+
+                       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_ENUM_FMT:
+               {
+                       struct v4l2_fmtdesc *vfd = arg;
+
+                       if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
+                               return -EINVAL;
+                       }
+                       vfd->flags = 0;
+                       vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+                       strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
+                       vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
+                       memset(vfd->reserved, 0, sizeof(vfd->reserved));
+                       return 0;
+               }
+               case VIDIOC_G_FMT:
+               {
+                       struct v4l2_format *vf = arg;
+
+                       switch (vf->type) {
+                               case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+                               {
+                                       vf->fmt.pix.width = usbvision->curwidth;
+                                       vf->fmt.pix.height = usbvision->curheight;
+                                       vf->fmt.pix.pixelformat = usbvision->palette.format;
+                                       vf->fmt.pix.bytesperline =  usbvision->curwidth*usbvision->palette.bytes_per_pixel;
+                                       vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
+                                       vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
+                                       vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
+                               }
+                               return 0;
+                               default:
+                                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
+                                       return -EINVAL;
+                       }
+                       PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d",vf->fmt.win.w.width, vf->fmt.win.w.height);
+                       return 0;
+               }
+               case VIDIOC_TRY_FMT:
+               case VIDIOC_S_FMT:
+               {
+                       struct v4l2_format *vf = arg;
+                       int formatIdx,ret;
+
+                       switch(vf->type) {
+                               case V4L2_BUF_TYPE_VIDEO_CAPTURE:
+                               {
+                                       /* Find requested format in available ones */
+                                       for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
+                                               if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
+                                                       usbvision->palette = usbvision_v4l2_format[formatIdx];
+                                                       break;
+                                               }
+                                       }
+                                       /* robustness */
+                                       if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
+                                               return -EINVAL;
+                                       }
+                                       RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
+                                       RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
+
+                                       /* stop io in case it is already in progress */
+                                       if(usbvision->streaming == Stream_On) {
+                                               if ((ret = usbvision_stream_interrupt(usbvision)))
+                                                       return ret;
+                                       }
+                                       usbvision_empty_framequeues(usbvision);
+
+                                       usbvision->curFrame = NULL;
+
+                                       // by now we are committed to the new data...
+                                       down(&usbvision->lock);
+                                       usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
+                                       up(&usbvision->lock);
+
+                                       PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
+                                              vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
+                                       return 0;
+                               }
+                               default:
+                                       return -EINVAL;
+                       }
+               }
+               default:
+                       return -ENOIOCTLCMD;
+       }
+       return 0;
+}
+
+static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
+                      unsigned int cmd, unsigned long arg)
+{
+       return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
+}
+
+
+static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
+                     size_t count, loff_t *ppos)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+       int noblock = file->f_flags & O_NONBLOCK;
+       unsigned long lock_flags;
+
+       int frmx = -1;
+       int ret,i;
+       struct usbvision_frame *frame;
+
+       PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
+
+       if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
+               return -EFAULT;
+
+       /* no stream is running, make it running ! */
+       usbvision->streaming = Stream_On;
+       call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
+
+       /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
+       for(i=0;i<USBVISION_NUMFRAMES;i++) {
+               frame = &usbvision->frame[i];
+               if(frame->grabstate == FrameState_Unused) {
+                       /* Mark it as ready and enqueue frame */
+                       frame->grabstate = FrameState_Ready;
+                       frame->scanstate = ScanState_Scanning;
+                       frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
+
+                       /* set v4l2_format index */
+                       frame->v4l2_format = usbvision->palette;
+
+                       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
+                       list_add_tail(&frame->frame, &usbvision->inqueue);
+                       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
+               }
+       }
+
+       /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
+       if (list_empty(&(usbvision->outqueue))) {
+               if(noblock)
+                       return -EAGAIN;
+
+               ret = wait_event_interruptible
+                       (usbvision->wait_frame,
+                        !list_empty(&(usbvision->outqueue)));
+               if (ret)
+                       return ret;
+       }
+
+       spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
+       frame = list_entry(usbvision->outqueue.next,
+                          struct usbvision_frame, frame);
+       list_del(usbvision->outqueue.next);
+       spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
+
+       /* An error returns an empty frame */
+       if (frame->grabstate == FrameState_Error) {
+               frame->bytes_read = 0;
+               return 0;
+       }
+
+       PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
+                      frame->index, frame->bytes_read, frame->scanlength);
+
+       /* copy bytes to user space; we allow for partials reads */
+       if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
+               count = frame->scanlength - frame->bytes_read;
+
+       if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
+               return -EFAULT;
+       }
+
+       frame->bytes_read += count;
+       PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
+                      (unsigned long)count, frame->bytes_read);
+
+       // For now, forget the frame if it has not been read in one shot.
+/*     if (frame->bytes_read >= frame->scanlength) {// All data has been read */
+               frame->bytes_read = 0;
+
+               /* Mark it as available to be used again. */
+               usbvision->frame[frmx].grabstate = FrameState_Unused;
+/*     } */
+
+       return count;
+}
+
+static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       unsigned long size = vma->vm_end - vma->vm_start,
+               start = vma->vm_start;
+       void *pos;
+       u32 i;
+
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+
+       down(&usbvision->lock);
+
+       if (!USBVISION_IS_OPERATIONAL(usbvision)) {
+               up(&usbvision->lock);
+               return -EFAULT;
+       }
+
+       if (!(vma->vm_flags & VM_WRITE) ||
+           size != PAGE_ALIGN(usbvision->max_frame_size)) {
+               up(&usbvision->lock);
+               return -EINVAL;
+       }
+
+       for (i = 0; i < USBVISION_NUMFRAMES; i++) {
+               if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
+                       break;
+       }
+       if (i == USBVISION_NUMFRAMES) {
+               PDEBUG(DBG_FUNC, "mmap: user supplied mapping address is out of range");
+               up(&usbvision->lock);
+               return -EINVAL;
+       }
+
+       /* VM_IO is eventually going to replace PageReserved altogether */
+       vma->vm_flags |= VM_IO;
+       vma->vm_flags |= VM_RESERVED;   /* avoid to swap out this VMA */
+
+       pos = usbvision->frame[i].data;
+       while (size > 0) {
+
+               if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
+                       PDEBUG(DBG_FUNC, "mmap: vm_insert_page failed");
+                       up(&usbvision->lock);
+                       return -EAGAIN;
+               }
+               start += PAGE_SIZE;
+               pos += PAGE_SIZE;
+               size -= PAGE_SIZE;
+       }
+
+       up(&usbvision->lock);
+       return 0;
+}
+
+
+/*
+ * Here comes the stuff for radio on usbvision based devices
+ *
+ */
+static int usbvision_radio_open(struct inode *inode, struct file *file)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+       struct v4l2_frequency freq;
+       int errCode = 0;
+
+       PDEBUG(DBG_IO, "%s:", __FUNCTION__);
+
+       down(&usbvision->lock);
+
+       if (usbvision->user) {
+               err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
+               errCode = -EBUSY;
+       }
+       else {
+               if(PowerOnAtOpen) {
+                       usbvision_reset_powerOffTimer(usbvision);
+                       if (usbvision->power == 0) {
+                               usbvision_power_on(usbvision);
+                               usbvision_init_i2c(usbvision);
+                       }
+               }
+
+               // If so far no errors then we shall start the radio
+               usbvision->radio = 1;
+               call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
+               freq.frequency = 1517; //SWR3 @ 94.8MHz
+               call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
+               usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
+               usbvision->user++;
+       }
+
+       if (errCode) {
+               if (PowerOnAtOpen) {
+                       usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
+                       usbvision_power_off(usbvision);
+                       usbvision->initialized = 0;
+               }
+       }
+       up(&usbvision->lock);
+       return errCode;
+}
+
+
+static int usbvision_radio_close(struct inode *inode, struct file *file)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+       int errCode = 0;
+
+       PDEBUG(DBG_IO, "");
+
+       down(&usbvision->lock);
+
+       usbvision_audio_off(usbvision);
+       usbvision->radio=0;
+       usbvision->user--;
+
+       if (PowerOnAtOpen) {
+               usbvision_set_powerOffTimer(usbvision);
+               usbvision->initialized = 0;
+       }
+
+       up(&usbvision->lock);
+
+       if (usbvision->remove_pending) {
+               info("%s: Final disconnect", __FUNCTION__);
+               usbvision_release(usbvision);
+       }
+
+
+       PDEBUG(DBG_IO, "success");
+
+       return errCode;
+}
+
+static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
+                                unsigned int cmd, void *arg)
+{
+       struct video_device *dev = video_devdata(file);
+       struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
+
+       if (!USBVISION_IS_OPERATIONAL(usbvision))
+               return -EIO;
+
+       switch (cmd) {
+               case VIDIOC_QUERYCAP:
+               {
+                       struct v4l2_capability *vc=arg;
+
+                       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 = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
+                       PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
+                       return 0;
+               }
+               case VIDIOC_QUERYCTRL:
+               {
+                       struct v4l2_queryctrl *ctrl = arg;
+                       int id=ctrl->id;
+
+                       memset(ctrl,0,sizeof(*ctrl));
+                       ctrl->id=id;
+
+                       call_i2c_clients(usbvision, cmd, arg);
+                       PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
+
+                       if (ctrl->type)
+                               return 0;
+                       else
+                               return -EINVAL;
+
+               }
+               case VIDIOC_G_CTRL:
+               {
+                       struct v4l2_control *ctrl = arg;
+
+                       call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
+                       PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
+                       return 0;
+               }
+               case VIDIOC_S_CTRL:
+               {
+                       struct v4l2_control *ctrl = arg;
+
+                       call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
+                       PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
+                       return 0;
+               }
+               case VIDIOC_G_TUNER:
+               {
+                       struct v4l2_tuner *t = arg;
+
+                       if (t->index > 0)
+                               return -EINVAL;
+
+                       memset(t,0,sizeof(*t));
+                       strcpy(t->name, "Radio");
+                       t->type = V4L2_TUNER_RADIO;
+
+                       /* Let clients fill in the remainder of this struct */
+                       call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
+                       PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
+                       return 0;
+               }
+               case VIDIOC_S_TUNER:
+               {
+                       struct v4l2_tuner *vt = arg;
+
+                       // Only no or one tuner for now
+                       if (!usbvision->have_tuner || vt->index)
+                               return -EINVAL;
+                       /* let clients handle this */
+                       call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
+
+                       PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
+                       return 0;
+               }
+               case VIDIOC_G_AUDIO:
+               {
+                       struct v4l2_audio *a = arg;
+
+                       memset(a,0,sizeof(*a));
+                       strcpy(a->name,"Radio");
+                       PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
+                       return 0;
+               }
+               case VIDIOC_S_AUDIO:
+               case VIDIOC_S_INPUT:
+               case VIDIOC_S_STD:
+               return 0;
+
+               case VIDIOC_G_FREQUENCY:
+               {
+                       struct v4l2_frequency *f = arg;
+
+                       memset(f,0,sizeof(*f));
+
+                       f->type = V4L2_TUNER_RADIO;
+                       f->frequency = usbvision->freq;
+                       call_i2c_clients(usbvision, cmd, f);
+                       PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
+
+                       return 0;
+               }
+               case VIDIOC_S_FREQUENCY:
+               {
+                       struct v4l2_frequency *f = arg;
+
+                       if (f->tuner != 0)
+                               return -EINVAL;
+                       usbvision->freq = f->frequency;
+                       call_i2c_clients(usbvision, cmd, f);
+                       PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
+
+                       return 0;
+               }
+               default:
+               {
+                       PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
+                       return -ENOIOCTLCMD;
+               }
+       }
+       return 0;
+}
+
+
+static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
+                      unsigned int cmd, unsigned long arg)
+{
+       return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
+}
+
+
+/*
+ * Here comes the stuff for vbi on usbvision based devices
+ *
+ */
+static int usbvision_vbi_open(struct inode *inode, struct file *file)
+{
+       /* TODO */
+       return -EINVAL;
+
+}
+
+static int usbvision_vbi_close(struct inode *inode, struct file *file)
+{
+       /* TODO */
+       return -EINVAL;
+}
+
+static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
+                                unsigned int cmd, void *arg)
+{
+       /* TODO */
+       return -EINVAL;
+}
+
+static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
+                      unsigned int cmd, unsigned long arg)
+{
+       return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
+}
+
+
+//
+// Video registration stuff
+//
+
+// Video template
+static struct file_operations usbvision_fops = {
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .open           = usbvision_v4l2_open,
+       .release        = usbvision_v4l2_close,
+       .read           = usbvision_v4l2_read,
+       .mmap           = usbvision_v4l2_mmap,
+       .ioctl          = usbvision_v4l2_ioctl,
+       .llseek         = no_llseek,
+};
+static struct video_device usbvision_video_template = {
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .type           = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
+       .hardware       = VID_HARDWARE_USBVISION,
+       .fops           = &usbvision_fops,
+  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
+       .name           = "usbvision-video",
+       .release        = video_device_release,
+  #endif
+       .minor          = -1,
+};
+
+
+// Radio template
+static struct file_operations usbvision_radio_fops = {
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .open           = usbvision_radio_open,
+       .release        = usbvision_radio_close,
+       .ioctl          = usbvision_radio_ioctl,
+       .llseek         = no_llseek,
+};
+
+static struct video_device usbvision_radio_template=
+{
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .type           = VID_TYPE_TUNER,
+       .hardware       = VID_HARDWARE_USBVISION,
+       .fops           = &usbvision_radio_fops,
+  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
+       .release        = video_device_release,
+       .name           = "usbvision-radio",
+  #endif
+       .minor          = -1,
+};
+
+
+// vbi template
+static struct file_operations usbvision_vbi_fops = {
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .open           = usbvision_vbi_open,
+       .release        = usbvision_vbi_close,
+       .ioctl          = usbvision_vbi_ioctl,
+       .llseek         = no_llseek,
+};
+
+static struct video_device usbvision_vbi_template=
+{
+  #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31)
+       .owner             = THIS_MODULE,
+  #endif
+       .type           = VID_TYPE_TUNER,
+       .hardware       = VID_HARDWARE_USBVISION,
+       .fops           = &usbvision_vbi_fops,
+  #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
+       .release        = video_device_release,
+       .name           = "usbvision-vbi",
+  #endif
+       .minor          = -1,
+};
+
+
+static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
+                                       struct video_device *vdev_template,
+                                       char *name)
+{
+       struct usb_device *usb_dev = usbvision->dev;
+       struct video_device *vdev;
+
+       if (usb_dev == NULL) {
+               err("%s: usbvision->dev is not set", __FUNCTION__);
+               return NULL;
+       }
+
+       vdev = video_device_alloc();
+       if (NULL == vdev) {
+               return NULL;
+       }
+       *vdev = *vdev_template;
+//     vdev->minor   = -1;
+       vdev->dev     = &usb_dev->dev;
+       snprintf(vdev->name, sizeof(vdev->name), "%s", name);
+       video_set_drvdata(vdev, usbvision);
+       return vdev;
+}
+
+// unregister video4linux devices
+static void usbvision_unregister_video(struct usb_usbvision *usbvision)
+{
+       // vbi Device:
+       if (usbvision->vbi) {
+               PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
+               if (usbvision->vbi->minor != -1) {
+                       video_unregister_device(usbvision->vbi);
+               }
+               else {
+                       video_device_release(usbvision->vbi);
+               }
+               usbvision->vbi = NULL;
+       }
+
+       // Radio Device:
+       if (usbvision->rdev) {
+               PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
+               if (usbvision->rdev->minor != -1) {
+                       video_unregister_device(usbvision->rdev);
+               }
+               else {
+                       video_device_release(usbvision->rdev);
+               }
+               usbvision->rdev = NULL;
+       }
+
+       // Video Device:
+       if (usbvision->vdev) {
+               PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
+               if (usbvision->vdev->minor != -1) {
+                       video_unregister_device(usbvision->vdev);
+               }
+               else {
+                       video_device_release(usbvision->vdev);
+               }
+               usbvision->vdev = NULL;
+       }
+}
+
+// register video4linux devices
+static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
+{
+       // Video Device:
+       usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
+       if (usbvision->vdev == NULL) {
+               goto err_exit;
+       }
+       if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
+               goto err_exit;
+       }
+       info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
+
+       // Radio Device:
+       if (usbvision_device_data[usbvision->DevModel].Radio) {
+               // usbvision has radio
+               usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
+               if (usbvision->rdev == NULL) {
+                       goto err_exit;
+               }
+               if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
+                       goto err_exit;
+               }
+               info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
+       }
+       // vbi Device:
+       if (usbvision_device_data[usbvision->DevModel].vbi) {
+               usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
+               if (usbvision->vdev == NULL) {
+                       goto err_exit;
+               }
+               if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
+                       goto err_exit;
+               }
+               info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
+       }
+       // all done
+       return 0;
+
+ err_exit:
+       err("USBVision[%d]: video_register_device() failed", usbvision->nr);
+       usbvision_unregister_video(usbvision);
+       return -1;
+}
+
+/*
+ * usbvision_alloc()
+ *
+ * This code allocates the struct usb_usbvision. It is filled with default values.
+ *
+ * Returns NULL on error, a pointer to usb_usbvision else.
+ *
+ */
+static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
+{
+       struct usb_usbvision *usbvision;
+
+       if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
+               goto err_exit;
+       }
+
+       usbvision->dev = dev;
+
+       init_MUTEX(&usbvision->lock);   /* to 1 == available */
+
+       // prepare control urb for control messages during interrupts
+       usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
+       if (usbvision->ctrlUrb == NULL) {
+               goto err_exit;
+       }
+       init_waitqueue_head(&usbvision->ctrlUrb_wq);
+       init_MUTEX(&usbvision->ctrlUrbLock);    /* to 1 == available */
+
+       usbvision_init_powerOffTimer(usbvision);
+
+       return usbvision;
+
+err_exit:
+       if (usbvision && usbvision->ctrlUrb) {
+               usb_free_urb(usbvision->ctrlUrb);
+       }
+       if (usbvision) {
+               kfree(usbvision);
+       }
+       return NULL;
+}
+
+/*
+ * usbvision_release()
+ *
+ * This code does final release of struct usb_usbvision. This happens
+ * after the device is disconnected -and- all clients closed their files.
+ *
+ */
+static void usbvision_release(struct usb_usbvision *usbvision)
+{
+       PDEBUG(DBG_PROBE, "");
+
+       down(&usbvision->lock);
+
+       usbvision_reset_powerOffTimer(usbvision);
+
+       usbvision->initialized = 0;
+
+       up(&usbvision->lock);
+
+       usbvision_remove_sysfs(usbvision->vdev);
+       usbvision_unregister_video(usbvision);
+
+       if (usbvision->ctrlUrb) {
+               usb_free_urb(usbvision->ctrlUrb);
+       }
+
+       kfree(usbvision);
+
+       PDEBUG(DBG_PROBE, "success");
+}
+
+
+/******************************** usb interface *****************************************/
+
+static void usbvision_configure_video(struct usb_usbvision *usbvision)
+{
+       int model,i;
+
+       if (usbvision == NULL)
+               return;
+
+       model = usbvision->DevModel;
+       usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
+
+       if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
+               usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
+       } else {
+               usbvision->Vin_Reg2_Preset = 0;
+       }
+
+       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 */
+
+       usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
+       usbvision->ctl_input = 0;
+
+       /* This should be here to make i2c clients to be able to register */
+       usbvision_audio_off(usbvision); //first switch off audio
+       if (!PowerOnAtOpen) {
+               usbvision_power_on(usbvision);  //and then power up the noisy tuner
+               usbvision_init_i2c(usbvision);
+       }
+}
+
+/*
+ * usbvision_probe()
+ *
+ * This procedure queries device descriptor and accepts the interface
+ * if it looks like USBVISION video device
+ *
+ */
+static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
+{
+       struct usb_device *dev = interface_to_usbdev(intf);
+       __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
+       const struct usb_host_interface *interface;
+       struct usb_usbvision *usbvision = NULL;
+       const struct usb_endpoint_descriptor *endpoint;
+       int model;
+
+       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) {
+                       continue;
+               }
+               if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
+                       continue;
+               }
+
+               info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
+               break;
+       }
+
+       if (usbvision_device_data[model].idVendor == 0) {
+               return -ENODEV; //no matching device
+       }
+       if (usbvision_device_data[model].Interface >= 0) {
+               interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
+       }
+       else {
+               interface = &dev->actconfig->interface[ifnum]->altsetting[0];
+       }
+       endpoint = &interface->endpoint[1].desc;
+       if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
+               err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
+               err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
+               return -ENODEV;
+       }
+       if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
+               err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
+               return -ENODEV;
+       }
+
+       usb_get_dev(dev);
+
+       if ((usbvision = usbvision_alloc(dev)) == NULL) {
+               err("%s: couldn't allocate USBVision struct", __FUNCTION__);
+               return -ENOMEM;
+       }
+       if (dev->descriptor.bNumConfigurations > 1) {
+               usbvision->bridgeType = BRIDGE_NT1004;
+       }
+       else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
+               usbvision->bridgeType = BRIDGE_NT1005;
+       }
+       else {
+               usbvision->bridgeType = BRIDGE_NT1003;
+       }
+       PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
+
+       down(&usbvision->lock);
+
+       usbvision->nr = usbvision_nr++;
+
+       usbvision->have_tuner = usbvision_device_data[model].Tuner;
+       if (usbvision->have_tuner) {
+               usbvision->tuner_type = usbvision_device_data[model].TunerType;
+       }
+
+       usbvision->tuner_addr = ADDR_UNSET;
+
+       usbvision->DevModel = model;
+       usbvision->remove_pending = 0;
+       usbvision->iface = ifnum;
+       usbvision->ifaceAltInactive = 0;
+       usbvision->ifaceAltActive = 1;
+       usbvision->video_endp = endpoint->bEndpointAddress;
+       usbvision->isocPacketSize = 0;
+       usbvision->usb_bandwidth = 0;
+       usbvision->user = 0;
+       usbvision->streaming = Stream_Off;
+       usbvision_register_video(usbvision);
+       usbvision_configure_video(usbvision);
+       up(&usbvision->lock);
+
+
+       usb_set_intfdata (intf, usbvision);
+       usbvision_create_sysfs(usbvision->vdev);
+
+       PDEBUG(DBG_PROBE, "success");
+       return 0;
+}
+
+
+/*
+ * usbvision_disconnect()
+ *
+ * This procedure stops all driver activity, deallocates interface-private
+ * structure (pointed by 'ptr') and after that driver should be removable
+ * with no ill consequences.
+ *
+ */
+static void __devexit usbvision_disconnect(struct usb_interface *intf)
+{
+       struct usb_usbvision *usbvision = usb_get_intfdata(intf);
+
+       PDEBUG(DBG_PROBE, "");
+
+       if (usbvision == NULL) {
+               err("%s: usb_get_intfdata() failed", __FUNCTION__);
+               return;
+       }
+       usb_set_intfdata (intf, NULL);
+
+       down(&usbvision->lock);
+
+       // At this time we ask to cancel outstanding URBs
+       usbvision_stop_isoc(usbvision);
+
+       if (usbvision->power) {
+               usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
+               usbvision_power_off(usbvision);
+       }
+       usbvision->remove_pending = 1;  // Now all ISO data will be ignored
+
+       usb_put_dev(usbvision->dev);
+       usbvision->dev = NULL;  // USB device is no more
+
+       up(&usbvision->lock);
+
+       if (usbvision->user) {
+               info("%s: In use, disconnect pending", __FUNCTION__);
+               wake_up_interruptible(&usbvision->wait_frame);
+               wake_up_interruptible(&usbvision->wait_stream);
+       }
+       else {
+               usbvision_release(usbvision);
+       }
+
+       PDEBUG(DBG_PROBE, "success");
+
+}
+
+static struct usb_driver usbvision_driver = {
+       .name           = "usbvision",
+       .id_table       = usbvision_table,
+       .probe          = usbvision_probe,
+       .disconnect     = usbvision_disconnect
+};
+
+/*
+ * customdevice_process()
+ *
+ * This procedure preprocesses CustomDevice parameter if any
+ *
+ */
+void customdevice_process(void)
+{
+       usbvision_device_data[0]=usbvision_device_data[1];
+       usbvision_table[0]=usbvision_table[1];
+
+       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"
+               usbvision_device_data[0].idVendor;
+               usbvision_device_data[0].idProduct;
+               usbvision_device_data[0].Interface;
+               usbvision_device_data[0].Codec;
+               usbvision_device_data[0].VideoChannels;
+               usbvision_device_data[0].VideoNorm;
+               usbvision_device_data[0].AudioChannels;
+               usbvision_device_data[0].Radio;
+               usbvision_device_data[0].Tuner;
+               usbvision_device_data[0].TunerType;
+               usbvision_device_data[0].Vin_Reg1;
+               usbvision_device_data[0].Vin_Reg2;
+               usbvision_device_data[0].X_Offset;
+               usbvision_device_data[0].Y_Offset;
+               usbvision_device_data[0].Dvi_yuv;
+               usbvision_device_data[0].ModelString;
+               */
+
+               rmspace(parse);
+               usbvision_device_data[0].ModelString="USBVISION Custom Device";
+
+               parse+=2;
+               sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
+               parse+=2;
+               sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
+               sscanf(parse,"%d",&usbvision_device_data[0].Interface);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
+               sscanf(parse,"%d",&usbvision_device_data[0].Codec);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
+               sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
+
+               switch(*parse)
+               {
+                       case 'P':
+                               PDEBUG(DBG_PROBE, "VideoNorm=PAL");
+                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
+                               break;
+
+                       case 'S':
+                               PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
+                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
+                               break;
+
+                       case 'N':
+                               PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
+                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
+                               break;
+
+                       default:
+                               PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
+                               usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
+                               break;
+               }
+               goto2next(parse);
+
+               sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
+               sscanf(parse,"%d",&usbvision_device_data[0].Radio);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
+               sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
+               sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
+               sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
+               sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
+               sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
+               sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
+               goto2next(parse);
+               PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
+               sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
+               PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
+
+               //add to usbvision_table also
+               usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
+               usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
+               usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
+
+       }
+}
+
+
+
+/*
+ * usbvision_init()
+ *
+ * This code is run to initialize the driver.
+ *
+ */
+static int __init usbvision_init(void)
+{
+       int errCode;
+
+       PDEBUG(DBG_PROBE, "");
+
+       PDEBUG(DBG_IOCTL, "IOCTL   debugging is enabled [video]");
+       PDEBUG(DBG_IO,  "IO      debugging is enabled [video]");
+       PDEBUG(DBG_PROBE, "PROBE   debugging is enabled [video]");
+       PDEBUG(DBG_FUNC, "FUNC    debugging is enabled [video]");
+
+       /* 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();
+
+       errCode = usb_register(&usbvision_driver);
+
+       if (errCode == 0) {
+               info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
+               PDEBUG(DBG_PROBE, "success");
+       }
+       return errCode;
+}
+
+static void __exit usbvision_exit(void)
+{
+ PDEBUG(DBG_PROBE, "");
+
+ usb_deregister(&usbvision_driver);
+ PDEBUG(DBG_PROBE, "success");
+}
+
+module_init(usbvision_init);
+module_exit(usbvision_exit);
+
+/*
+ * Overrides for Emacs so that we follow Linus's tabbing style.
+ * ---------------------------------------------------------------------------
+ * Local variables:
+ * c-basic-offset: 8
+ * End:
+ */
index f304e66b03a672b0145542d11cceefaf83652e83..f2eeda8326ff08f5d1b8c55ac4715963834498d3 100644 (file)
@@ -28,7 +28,9 @@
 
 #include <linux/list.h>
 #include <linux/usb.h>
-#include "usbvision-i2c.h"
+#include <media/v4l2-common.h>
+#include <media/tuner.h>
+#include <linux/videodev2.h>
 
 #ifndef VID_HARDWARE_USBVISION
        #define VID_HARDWARE_USBVISION 34   /* USBVision Video Grabber */
 #define USBVISION_MAX_ISOC_PACKET_SIZE         959                     // NT1003 Specs Document says 1023
 
 #define USBVISION_NUM_HEADERMARKER     20
-#define USBVISION_NUMFRAMES            3
-#define USBVISION_NUMSBUF              2
+#define USBVISION_NUMFRAMES            3  /* Maximum number of frames an application can get */
+#define USBVISION_NUMSBUF              2 /* Dimensioning the USB S buffering */
 
 #define USBVISION_POWEROFF_TIME                3 * (HZ)                // 3 seconds
 
+
+#define FRAMERATE_MIN  0
+#define FRAMERATE_MAX  31
+
+enum {
+       ISOC_MODE_YUV422 = 0x03,
+       ISOC_MODE_YUV420 = 0x14,
+       ISOC_MODE_COMPRESS = 0x60,
+};
+
 /* This macro restricts an int variable to an inclusive range */
 #define RESTRICT_TO_RANGE(v,mi,ma) { if ((v) < (mi)) (v) = (mi); else if ((v) > (ma)) (v) = (ma); }
 
     mr = LIMIT_RGB(mm_r); \
 }
 
-
-
 /* Debugging aid */
 #define USBVISION_SAY_AND_WAIT(what) { \
        wait_queue_head_t wq; \
        ((udevice)->last_error == 0) && \
        (!(udevice)->remove_pending))
 
+/* I2C structures */
+struct i2c_algo_usb_data {
+       void *data;             /* private data for lowlevel routines */
+       int (*inb) (void *data, unsigned char addr, char *buf, short len);
+       int (*outb) (void *data, unsigned char addr, char *buf, short len);
+
+       /* local settings */
+       int udelay;
+       int mdelay;
+       int timeout;
+};
 
+#define I2C_USB_ADAP_MAX       16
+
+/* ----------------------------------------------------------------- */
+/* usbvision video structures                                        */
+/* ----------------------------------------------------------------- */
 enum ScanState {
        ScanState_Scanning,     /* Scanning for header */
        ScanState_Lines         /* Parsing lines */
@@ -347,7 +373,6 @@ struct usb_usbvision {
        struct video_device *vdev;                                      /* Video Device */
        struct video_device *rdev;                                      /* Radio Device */
        struct video_device *vbi;                                       /* VBI Device   */
-       struct video_audio audio_dev;                                   /* Current audio params */
 
        /* i2c Declaration Section*/
        struct i2c_adapter i2c_adap;
@@ -361,6 +386,7 @@ struct usb_usbvision {
        wait_queue_head_t ctrlUrb_wq;                                   // Processes waiting
        struct semaphore ctrlUrbLock;
 
+       /* configuration part */
        int have_tuner;
        int tuner_type;
        int tuner_addr;
@@ -372,7 +398,7 @@ struct usb_usbvision {
        int AudioMute;
        int AudioChannel;
        int isocMode;                                                   // format of video data for the usb isoc-transfer
-       unsigned int nr;                                                // Number of the device < MAX_USBVISION
+       unsigned int nr;                                                // Number of the device
 
        /* Device structure */
        struct usb_device *dev;
@@ -384,7 +410,6 @@ struct usb_usbvision {
        struct work_struct powerOffWork;
        int power;                                                      /* is the device powered on? */
        int user;                                                       /* user count for exclusive use */
-       int usbvision_used;                                             /* Is this structure in use? */
        int initialized;                                                /* Had we already sent init sequence? */
        int DevModel;                                                   /* What type of USBVISION device we got? */
        enum StreamState streaming;                                     /* Are we streaming Isochronous? */
@@ -402,7 +427,6 @@ struct usb_usbvision {
        wait_queue_head_t wait_stream;                                  /* Processes waiting */
        struct usbvision_frame *curFrame;                               // pointer to current frame, set by usbvision_find_header
        struct usbvision_frame frame[USBVISION_NUMFRAMES];              // frame buffer
-       int curSbufNum;                                                 // number of current receiving sbuf
        struct usbvision_sbuf sbuf[USBVISION_NUMSBUF];                  // S buffering
        volatile int remove_pending;                                    /* If set then about to exit */
 
@@ -413,13 +437,7 @@ struct usb_usbvision {
        int scratch_headermarker[USBVISION_NUM_HEADERMARKER];
        int scratch_headermarker_read_ptr;
        int scratch_headermarker_write_ptr;
-       int isocstate;
-       /* color controls */
-       int saturation;
-       int hue;
-       int brightness;
-       int contrast;
-       int depth;
+       enum IsocState isocstate;
        struct usbvision_v4l2_format_st palette;
 
        struct v4l2_capability vcap;                                    /* Video capabilities */
@@ -427,17 +445,6 @@ struct usb_usbvision {
        struct usbvision_tvnorm *tvnorm;                                /* selected tv norm */
        unsigned char video_endp;                                       /* 0x82 for USBVISION devices based */
 
-       // Overlay stuff:
-       struct v4l2_framebuffer vid_buf;
-       struct v4l2_format vid_win;
-       int vid_buf_valid;                                              // Status: video buffer is valid (set)
-       int vid_win_valid;                                              // Status: video window is valid (set)
-       int     overlay;                                                /*Status: Are we overlaying? */
-       unsigned int    clipmask[USBVISION_CLIPMASK_SIZE / 4];
-       unsigned char   *overlay_base;                                  /* Virtual base address of video buffer */
-       unsigned char   *overlay_win;                                   /* virt start address of overlay window */
-       struct usbvision_frame overlay_frame;
-
        // Decompression stuff:
        unsigned char *IntraFrameBuffer;                                /* Buffer for reference frame */
        int BlockPos;                                                   //for test only
@@ -449,16 +456,6 @@ struct usb_usbvision {
        int lastComprLevel;                                             // How strong (100) or weak (0) was compression
        int usb_bandwidth;                                              /* Mbit/s */
 
-       /* /proc entries, relative to /proc/video/usbvision/ */
-       struct proc_dir_entry *proc_devdir;             /* Per-device proc directory */
-       struct proc_dir_entry *proc_info;               /* <minor#>/info entry */
-       struct proc_dir_entry *proc_register;           /* <minor#>/register entry */
-       struct proc_dir_entry *proc_freq;               /* <minor#>/freq entry */
-       struct proc_dir_entry *proc_input;              /* <minor#>/input entry */
-       struct proc_dir_entry *proc_frame;              /* <minor#>/frame entry */
-       struct proc_dir_entry *proc_button;             /* <minor#>/button entry */
-       struct proc_dir_entry *proc_control;            /* <minor#>/control entry */
-
        /* Statistics that can be overlayed on the screen */
        unsigned long isocUrbCount;                     // How many URBs we received so far
        unsigned long urb_length;                       /* Length of last URB */
@@ -479,6 +476,70 @@ struct usb_usbvision {
        int ComprBlockTypes[4];
 };
 
+
+/* --------------------------------------------------------------- */
+/* defined in usbvision-i2c.c                                      */
+/* i2c-algo-usb declaration                                        */
+/* --------------------------------------------------------------- */
+
+int usbvision_i2c_usb_add_bus(struct i2c_adapter *);
+int usbvision_i2c_usb_del_bus(struct i2c_adapter *);
+
+static inline void *i2c_get_algo_usb_data (struct i2c_algo_usb_data *dev)
+{
+       return dev->data;
+}
+
+static inline void i2c_set_algo_usb_data (struct i2c_algo_usb_data *dev, void *data)
+{
+       dev->data = data;
+}
+
+
+/* ----------------------------------------------------------------------- */
+/* usbvision specific I2C functions                                        */
+/* ----------------------------------------------------------------------- */
+int usbvision_init_i2c(struct usb_usbvision *usbvision);
+void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd,void *arg);
+
+/* defined in usbvision-core.c                                      */
+void *usbvision_rvmalloc(unsigned long size);
+void usbvision_rvfree(void *mem, unsigned long size);
+int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg);
+int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
+                       unsigned char value);
+
+int usbvision_frames_alloc(struct usb_usbvision *usbvision);
+void usbvision_frames_free(struct usb_usbvision *usbvision);
+int usbvision_scratch_alloc(struct usb_usbvision *usbvision);
+void usbvision_scratch_free(struct usb_usbvision *usbvision);
+int usbvision_sbuf_alloc(struct usb_usbvision *usbvision);
+void usbvision_sbuf_free(struct usb_usbvision *usbvision);
+int usbvision_decompress_alloc(struct usb_usbvision *usbvision);
+void usbvision_decompress_free(struct usb_usbvision *usbvision);
+
+int usbvision_setup(struct usb_usbvision *usbvision,int format);
+int usbvision_init_isoc(struct usb_usbvision *usbvision);
+int usbvision_restart_isoc(struct usb_usbvision *usbvision);
+void usbvision_stop_isoc(struct usb_usbvision *usbvision);
+
+int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel);
+int usbvision_audio_off(struct usb_usbvision *usbvision);
+
+int usbvision_begin_streaming(struct usb_usbvision *usbvision);
+void usbvision_empty_framequeues(struct usb_usbvision *dev);
+int usbvision_stream_interrupt(struct usb_usbvision *dev);
+
+int usbvision_muxsel(struct usb_usbvision *usbvision, int channel);
+int usbvision_set_input(struct usb_usbvision *usbvision);
+int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height);
+
+void usbvision_init_powerOffTimer(struct usb_usbvision *usbvision);
+void usbvision_set_powerOffTimer(struct usb_usbvision *usbvision);
+void usbvision_reset_powerOffTimer(struct usb_usbvision *usbvision);
+int usbvision_power_off(struct usb_usbvision *usbvision);
+int usbvision_power_on(struct usb_usbvision *usbvision);
+
 #endif                                                                 /* __LINUX_USBVISION_H */
 
 /*