| 1 | /* |
| 2 | * USB USBVISION Video device driver 0.9.8.3cvs (For Kernel 2.4.19-2.4.32 + 2.6.0-2.6.16) |
| 3 | * |
| 4 | * |
| 5 | * |
| 6 | * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de> |
| 7 | * |
| 8 | * This module is part of usbvision driver project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | * |
| 24 | * Let's call the version 0.... until compression decoding is completely |
| 25 | * implemented. |
| 26 | * |
| 27 | * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach. |
| 28 | * It was based on USB CPiA driver written by Peter Pregler, |
| 29 | * Scott J. Bertin and Johannes Erdfelt |
| 30 | * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler & |
| 31 | * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink |
| 32 | * Updates to driver completed by Dwaine P. Garden |
| 33 | * |
| 34 | * History: |
| 35 | * |
| 36 | * Mar. 2000 - 15.12.2000: (0.0.0 - 0.2.0) |
| 37 | * Several alpha drivers and the first beta. |
| 38 | * |
| 39 | * Since Dec. 2000: (0.2.1) or (v2.1) |
| 40 | * Code changes or updates by Dwaine Garden and every other person. |
| 41 | * |
| 42 | * Added: New Hauppauge TV device Vendor ID: 0x0573 |
| 43 | * Product ID: 0x4D01 |
| 44 | * (Thanks to Giovanni Garberoglio) |
| 45 | * |
| 46 | * Added: UK Hauppauge WinTV-USB Vendor ID: 0x0573 |
| 47 | * Product ID: 0x4D02 |
| 48 | * (Thanks to Derek Freeman-Jones) |
| 49 | * |
| 50 | * Feb, 2001 - Apr 08, 2001: (0.3.0) |
| 51 | * - Some fixes. Driver is now more stable. |
| 52 | * - Scratch is organized as ring-buffer now for better performance |
| 53 | * - DGA (overlay) is now supported. |
| 54 | * !!!!Danger!!!! Clipping is not yet implemented. Your system will |
| 55 | * crash if your video window leaves the screen!!! |
| 56 | * - Max. Framesize is set to 320x240. There isn't more memory on the |
| 57 | * nt1003 video device for the FIFO. |
| 58 | * - Supported video palettes: RGB565, RGB555, RGB24, RGB32 |
| 59 | * |
| 60 | * |
| 61 | * Apr 15, 2001: (0.3.1-test...) |
| 62 | * - Clipping is implemented |
| 63 | * - NTSC is now coloured (Thanks to Dwaine Garden) |
| 64 | * - Added SECAM colour detection in saa7111-new |
| 65 | * - Added: French Hauppauge WinTV USB Vendor ID: 0x0573 |
| 66 | * Product ID: 0x4D03 |
| 67 | * (Thanks to Julius Hrivnac) |
| 68 | * - Added: US Hauppauge WINTV USB Vendor ID: 0x0573 |
| 69 | * Product ID: 0x4D00 |
| 70 | * (Thanks to Derrick J Brashear) |
| 71 | * - Changes for adding new devices. There's now a table in usbvision.h. |
| 72 | * Adding your devices data to the usbvision_device_data table is all |
| 73 | * you need to add a new device. |
| 74 | * |
| 75 | * May 11, 2001: (0.3.2-test...) (Thanks to Derek Freeman-Jones) |
| 76 | * - Support YUV422 raw format for people with hardware scaling. |
| 77 | * - Only power on the device when opened (use option PowerOnAtOpen=0 to disable it). |
| 78 | * - Turn off audio so we can listen to Line In. |
| 79 | * |
| 80 | * July 5, 2001 - (Patch the driver to run with Kernel 2.4.6) |
| 81 | * - Fixed a problem with the number of parameters passed to video_register_device. |
| 82 | * |
| 83 | * July 6, 2001 - Added: HAUPPAUGE WINTV-USB FM USA Vendor ID: 0x0573 |
| 84 | * Product ID: 0x4D10 |
| 85 | * (Thanks to Braddock Gaskill) |
| 86 | * Added: USBGear USBG-V1 resp. HAMA USB |
| 87 | * Vendor ID: 0x0573 |
| 88 | * Product ID: 0x0003 |
| 89 | * (Thanks to Bradley A. Singletary and Juergen Weigert) |
| 90 | * |
| 91 | * Jan 24, 2002 - (0.3.3-test...) |
| 92 | * - Moved all global variables that are device specific the usb_usbvision struct |
| 93 | * - Fixed the 64x48 unchangable image in xawtv when starting it with overlay |
| 94 | * - Add VideoNorm and TunerType to the usb_device_data table |
| 95 | * - Checked the audio channels and mute for HAUPPAUGE WinTV USB FM |
| 96 | * - Implemented the power on when opening the device. But some software opens |
| 97 | * the device several times when starting. So the i2c parts are just registered |
| 98 | * by an open, when they become deregistered by the next close. You can speed |
| 99 | * up tuner detection, when adding "options tuner addr=your_addr" to /etc/modules.conf |
| 100 | * - Begin to resize the frame in width and height. So it will be possible to watch i.e. |
| 101 | * 384x288 pixels at 23 fps. |
| 102 | * |
| 103 | * Feb 10, 2002 |
| 104 | * - Added radio device |
| 105 | * |
| 106 | * |
| 107 | * Jul 30, 2002 - (Thanks Cameron Maxwell) |
| 108 | * - Changes to usbvision.h --fixed usbvision device data structure, incorrectly had (0x0573, 0x4d21) for WinTV-USB II, should be 0x4d20. |
| 109 | * - Changes for device WinTV-USB II (0x0573. 0x4D21). It does not have a FM tuner. |
| 110 | * - Added the real device HAUPPAUGE WINTV-USB II (PAL) to the device structure in usbvision.h. |
| 111 | * - Changes to saa7113-new, the video is 8 bit data for the Phillips SAA7113 not 16bit like SAA7111. |
| 112 | * - Tuned lots of setup registers for the Phillips SAA7113 video chipset. |
| 113 | * - Changes to the supplied makefile. (Dwaine Garden) Still needs to be fixed so it will compile modules on different distrubutions. |
| 114 | * |
| 115 | * |
| 116 | * Aug 10, 2002 - (Thanks Mike Klinke) |
| 117 | * - Changes to usbvision.txt -- Fixed instructions on the location to copy the contents of the tgz file. |
| 118 | * - 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. |
| 119 | * |
| 120 | * Aug 12, 2002 - Dwaine Garden |
| 121 | * - Added the ability to read the NT100x chip for the MaxISOPacketLength and USB Bandwidth |
| 122 | * Setting of the video device. |
| 123 | * - Adjustments to the SAA7113H code for proper video output. |
| 124 | * - Changes to usbvision.h, so all the devices with FM tuners are working. |
| 125 | * |
| 126 | * Feb 10, 2003 - Joerg Heckenbach |
| 127 | * - fixed endian bug for Motorola PPC |
| 128 | * |
| 129 | * Feb 13, 2003 - Joerg Heckenbach |
| 130 | * - fixed Vin_Reg setting and presetting from usbvision_device_data() |
| 131 | * |
| 132 | * Apr 19, 2003 - Dwaine Garden |
| 133 | * - Fixed compiling errors under RedHat v9.0. from uvirt_to_kva and usbvision_mmap. (Thanks Cameron Maxwell) |
| 134 | * - Changed pte_offset to pte_offset_kernel. |
| 135 | * - Changed remap_page_range and added additional parameter to function. |
| 136 | * - Change setup parameters for the D-Link V100 USB device |
| 137 | * - Added a new device to the usbvision driver. Pinnacle Studio PCTV USB (PAL) 0x2304 0x0110 |
| 138 | * - Screwed up the sourceforge.net cvs respository! 8*) |
| 139 | * |
| 140 | * Apr 22, 2002 - Dwaine Garden |
| 141 | * - Added a new device to the usbvision driver. Dazzle DVC-80 (PAL) 0x07d0 0x0004. (Thanks Carl Anderson) |
| 142 | * - Changes to some of the comments. |
| 143 | * |
| 144 | * June 06, 2002 - Ivan, Dwaine Garden |
| 145 | * - Ivan updates for fine tuning device parameters without driver recompiling. (Ivan) |
| 146 | * - Changes to some of the comments. (Dwaine Garden) |
| 147 | * - Changes to the makefile - Better CPU settings. (Ivan) |
| 148 | * - Changes to device Hauppauge WinTv-USB III (PAL) FM Model 568 - Fine tuning parameters (Ivan) |
| 149 | * |
| 150 | * |
| 151 | * Oct 16, 2003 - 0.9.0 - Joerg Heckenbach |
| 152 | * - Implementation of the first part of the decompression algorithm for intra frames. |
| 153 | * The resolution has to be 320x240. A dynamic adaption of compression deepth is |
| 154 | * missing yet. |
| 155 | * |
| 156 | * Oct 22, 2003 - 0.9.1 - Joerg Heckenbach |
| 157 | * - Implementation of the decompression algorithm for inter frames. |
| 158 | * The resolution still has to be 320x240. |
| 159 | * |
| 160 | * Nov 2003 - Feb 2004 - 0.9.2 to 0.9.3 - Joerg Heckenbach |
| 161 | * - Implement last unknown compressed block type. But color is still noisy. |
| 162 | * - Finding criteria for adaptive compression adjustment. |
| 163 | * - Porting to 2.6 kernels, but still working under 2.4 |
| 164 | * |
| 165 | * Feb 04, 2004 - 0.9.4 Joerg Heckenbach |
| 166 | * - Found bug in color decompression. Color is OK now. |
| 167 | * |
| 168 | * Feb 09, 2004 - 0.9.5 Joerg Heckenbach |
| 169 | * - Add auto-recognition of chip type NT1003 or NT1004. |
| 170 | * - Add adaptive compression adjustment. |
| 171 | * - Patched saa7113 multiplexer switching (Thanks to Orlando F.S. Bordoni) |
| 172 | * |
| 173 | * Feb 24, 2004 - 0.9.6 Joerg Heckenbach |
| 174 | * - Add a timer to wait before poweroff at close, to save start time in |
| 175 | * some video applications |
| 176 | * |
| 177 | * Mar 4, 2004 - 0.9.6 Dwaine Garden |
| 178 | * - Added device Global Village GV-007 (NTSC) to usbvision.h (Thanks to Abe Skolnik) |
| 179 | * - Forgot to add this device to the driver. 8*) |
| 180 | * |
| 181 | * June 2, 2004 - 0.9.6 Dwaine Garden |
| 182 | * - Fixed sourceforge.net cvs repository. |
| 183 | * - 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. |
| 184 | * - Added device Hauppauge WinTv-USB III (PAL) FM Model 597 to usbvision.h |
| 185 | * |
| 186 | * July 1, 2004 -0.9.6 Dwaine Garden |
| 187 | * - Patch was submitted by Hal Finkel to fix the problem with the tuner not working under kernel 2.6.7. |
| 188 | * - Thanks Hal..... |
| 189 | * |
| 190 | * July 30, 2004 - 0.9.6 Dwaine Garden |
| 191 | * - Patch was submitted by Tobias Diaz to fix Model ID mismatch in usbvision.h. |
| 192 | * - Thanks..... |
| 193 | * |
| 194 | * August 12, 2004 - 0.9.6 Dwaine Garden |
| 195 | * - 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! |
| 196 | * - Added new device "Camtel Technology Corp TVB330-USB FM". |
| 197 | * - Sourceforge.net CVS has been updated with all the changes. |
| 198 | * |
| 199 | * August 20, 2004 - 0.9.7 Dwaine Garden |
| 200 | * - Added Device "Hauppauge USB Live Model 600" |
| 201 | * - 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. |
| 202 | * - Sourceforge.net CVS has been updated with all the changes. |
| 203 | * - Clean up the driver. |
| 204 | * |
| 205 | * September 13, 2004 - 0.9.8 Dwaine Garden |
| 206 | * - 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. |
| 207 | * - 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. |
| 208 | * - Removed some commented out code. Clean up. |
| 209 | * - Tried to fix up the annoying empty directories in the sourceforge.net cvs. Fuck it up again. 8*( |
| 210 | * |
| 211 | * November 15, 2004 - 0.9.8 Dwaine Garden |
| 212 | * - Release new tar - 0.9.8 on sourceforge.net |
| 213 | * - Added some new devices to usbvision.h WinTV USB Model 602 40201 Rev B282, Hauppague WinTV USB Model 602 40201 Rev B285 |
| 214 | * - Added better compatibility for 2.6.x kernels. |
| 215 | * - Hardware full screen scaling in grabdisplay mode. |
| 216 | * - Better support for sysfs. More code to follow for both video device and radio device. Device information is located at /sys/class/video4linux/video0 |
| 217 | * - Added module_param so loaded module parameters are displayed in sysfs. Driver parameters should show up in /sys/module/usbvision |
| 218 | * - Adjusted the SAA7111 registers to match the 2.6.x kernel SAA7111 code. Thanks to the person which helped test. |
| 219 | * - Changed to wait_event_interruptible. For all the people running Fedora 2. |
| 220 | * - Added some screenshots of actual video captures on sourceforge.net. |
| 221 | * |
| 222 | * November 24, 2004 - 0.9.8.1cvs Dwaine Garden |
| 223 | * - Added patch to check for palette and format in VIDIOCSPICT. Helix Producer should work fine with the driver now. Thanks Jason Simpson |
| 224 | * - Two device description changes and two additions for the maintainer of usb.ids. |
| 225 | * |
| 226 | * December 2, 2004 - 0.9.8.1cvs Dwaine Garden |
| 227 | * - Added patch for YUV420P and YUV422P video output. Thanks to Alex Smith. |
| 228 | * - Better support for mythtv. |
| 229 | * |
| 230 | * January 2, 2005 - 0.9.8.1cvs Dwaine Garden |
| 231 | * - Setup that you can specify which device is used for video. Default is auto detect next available device number eg. /dev/videoX |
| 232 | * - Setup that you can specify which device is used for radio. Default is auto detect next available device number eg. /dev/radioX |
| 233 | * - usb_unlink_urb() is deprecated for synchronous unlinks. Using usb_kill_urb instead. |
| 234 | * - usbvision_kvirt_to_pa is deprecated. Removed. |
| 235 | * - Changes are related to kernel changes for 2.6.10. (Fedora 4) |
| 236 | * |
| 237 | * February 2, 2005 - 0.9.8.1cvs Dwaine Garden |
| 238 | * - Added a new device to usbvision.h Dazzle DVC 50. Thanks to Luiz S. |
| 239 | * |
| 240 | * March 29, 2005 - 0.9.8.1cvs Dwaine Garden |
| 241 | * - Fixed compile error with saa7113 under kernels 2.6.11+ |
| 242 | * - Added module parameter to help people with Black and White output with using s-video input. Some cables and input device are wired differently. |
| 243 | * - Removed the .id from the i2c usbvision template. There was a change to the i2c with kernels 2.6.11+. |
| 244 | * |
| 245 | * April 9, 2005 - 0.9.8.1cvs Dwaine Garden |
| 246 | * - 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. |
| 247 | * If your device experiences Black and White images with the S-Video Input. Set this parameter to 1 when loading the module. |
| 248 | * - Replaced the wrong 2.6 readme file. I lost the right version. Someone sent me the right version by e-mail. Thanks. |
| 249 | * - Released new module version on sourceforge.net. So everyone can enjoy all the fixes and additional device support. |
| 250 | * |
| 251 | * April 20, 2005 - 0.9.8.2cvs Dwaine Garden |
| 252 | * - Release lock in usbvision_v4l_read_done. -Thanks to nplanel for the patch. |
| 253 | * - Additional comments to the driver. |
| 254 | * - Fixed some spelling mistakes. 8*) |
| 255 | * |
| 256 | * April 23, 2005 - 0.9.8.2cvs Joerg Heckenbach |
| 257 | * - Found bug in usbvision line counting. Now there should be no spurious lines in the image any longer. |
| 258 | * - Swapped usbvision_register_video and usbvision_configure_video to fix problem with PowerOnAtOpen=0. |
| 259 | * Thanks to Erwan Velu |
| 260 | * |
| 261 | * April 26, 2005 - 0.9.8.2cvs Joerg Heckenbach |
| 262 | * - Fixed problem with rmmod module and oppses. Replaced vfree(usbvision->overlay_base) with iounmap(usbvision->overlay_base). |
| 263 | * - Added function usb_get_dev(dev) and ; To help with unloading the module multiple times without crashing. |
| 264 | * (Keep the reference count in kobjects correct) |
| 265 | * |
| 266 | * June 14, 2005 - 0.9.8.2cvs Dwaine |
| 267 | * - Missed a change in saa7113.c for checking kernel version. Added conditional if's. |
| 268 | * |
| 269 | * June 15, 2005 - 0.9.8.2cvs Dwaine |
| 270 | * - Added new device WinTV device VendorId 0573 and ProductId 4d29. |
| 271 | * - Hacked some support for newer NT1005 devices. This devices only seem to have one configuration, not multiple configurations like the NT1004. |
| 272 | * |
| 273 | * June 29, 2005 - 0.9.8.2cvs Dwaine |
| 274 | * - Added new device WinTV device VendorId 0573 and ProductId 4d37. |
| 275 | * - Because of the first empty entry in usbvision_table, modutils failed to create the necessary entries for modules.usbmap. |
| 276 | * This means hotplug won't work for usbvision. Thanks to Gary Ng. |
| 277 | * - Sent an e-mail to the maintainer of usb.ids. New devices identified need to be added. |
| 278 | * - Fixed compile error with saa7113 under kernel 2.6.12. |
| 279 | * |
| 280 | * July 6, 2005 - 0.9.8.2cvs Dwaine |
| 281 | * - Patch submitted by Gary Ng for two additional procfs entries. Device Input and Frequency setting. |
| 282 | * |
| 283 | * July 12, 2005 - 0.9.8.2cvs Dwaine |
| 284 | * - New tuner identified for some devices it's called TCL_MFPE05. This tuner uses the same API as tuner 38 in tuner.c. |
| 285 | * - Thanks to lynx31 for contacting Hauppage and asking them. |
| 286 | * - I have no clue as to which devices use this new tuner, so people will have to contact me and tell me. |
| 287 | * |
| 288 | * July 21, 2005 - 0.9.8.2cvs Dwaine |
| 289 | * - Patched usbvision.c with missing ifdef kernversion statement so the module will compile with older kernels and v4l. |
| 290 | * - Thanks to cipe007...... |
| 291 | * |
| 292 | * May 19, 2006 - 0.9.8.3cvs Dwaine |
| 293 | * - Patched usbvision.c and i2c-algo.c so they will compile with kernel 2.6.16 |
| 294 | * - Adjust device "Pinnacle Studio PCTV USB (PAL) FM" values in usbvision.h |
| 295 | * |
| 296 | * May 24, 2006 - 0.9.8.3cvs Dwaine |
| 297 | * -Pinnacle Studio PCTV USB (NTSC) FM uses saa7111, not saa7113 like first thought. |
| 298 | * -Updated usbvision.h |
| 299 | * |
| 300 | * Aug 15, 2006 - 0.9.8.3cvs Dwaine |
| 301 | * -Added saa711x module into cvs, since the newer saa7115 module in newer kernels is v4l2. The usbvision driver is only v4l. |
| 302 | * -Updated makefile to put compiled modules into correct location. |
| 303 | * |
| 304 | * Aug 21, 2006 - 0.9.8.3cvs Dwaine |
| 305 | * -Changed number of bytes for i2c write to 4 as per the NT100X spec sheet. Thanks to Merlum for finding it. |
| 306 | * -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. |
| 307 | * -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*) |
| 308 | * |
| 309 | * Aug 27, 2006 - 0.9.8.3cvs Dwaine |
| 310 | * -Changed ifdef statement so the usbvision driver will compile with kernels at 2.6.12. |
| 311 | * -Updated readme files for new updated tuner list for v4l devices. |
| 312 | * |
| 313 | * |
| 314 | * |
| 315 | * TODO: |
| 316 | * - use submit_urb for all setup packets |
| 317 | * - Fix memory settings for nt1004. It is 4 times as big as the |
| 318 | * nt1003 memory. |
| 319 | * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one? |
| 320 | * - Clean up the driver. |
| 321 | * - optimization for performance. |
| 322 | * - Add Videotext capability (VBI). Working on it..... |
| 323 | * - Check audio for other devices |
| 324 | * - Add v4l2 interface |
| 325 | * |
| 326 | */ |
| 327 | |
| 328 | #include <linux/kernel.h> |
| 329 | #include <linux/sched.h> |
| 330 | #include <linux/list.h> |
| 331 | #include <linux/timer.h> |
| 332 | #include <linux/slab.h> |
| 333 | #include <linux/mm.h> |
| 334 | #include <linux/utsname.h> |
| 335 | #include <linux/highmem.h> |
| 336 | #include <linux/smp_lock.h> |
| 337 | #include <linux/videodev.h> |
| 338 | #include <linux/vmalloc.h> |
| 339 | #include <linux/module.h> |
| 340 | #include <linux/init.h> |
| 341 | #include <linux/spinlock.h> |
| 342 | #include <asm/io.h> |
| 343 | #include <linux/videodev2.h> |
| 344 | #include <linux/video_decoder.h> |
| 345 | #include <linux/i2c.h> |
| 346 | |
| 347 | #define USBVISION_DRIVER_VERSION_MAJOR 0 |
| 348 | #define USBVISION_DRIVER_VERSION_MINOR 8 |
| 349 | #define USBVISION_DRIVER_VERSION_PATCHLEVEL 0 |
| 350 | |
| 351 | #define USBVISION_VERSION __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL) " " USBVISION_DRIVER_VERSION_COMMENT |
| 352 | #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL) |
| 353 | |
| 354 | #include <media/saa7115.h> |
| 355 | #include <media/v4l2-common.h> |
| 356 | #include <media/tuner.h> |
| 357 | #include <media/audiochip.h> |
| 358 | |
| 359 | #include <linux/moduleparam.h> |
| 360 | #include <linux/workqueue.h> |
| 361 | |
| 362 | #ifdef CONFIG_KMOD |
| 363 | #include <linux/kmod.h> |
| 364 | #endif |
| 365 | |
| 366 | #include "usbvision.h" |
| 367 | #include "usbvision_ioctl.h" |
| 368 | |
| 369 | |
| 370 | #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__ |
| 371 | #define EMAIL "joerg@heckenbach-aw.de" |
| 372 | #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>" |
| 373 | #define DRIVER_DESC "USBVision USB Video Device Driver for Linux" |
| 374 | #define DRIVER_LICENSE "GPL" |
| 375 | #define DRIVER_ALIAS "USBVision" |
| 376 | |
| 377 | #define ENABLE_HEXDUMP 0 /* Enable if you need it */ |
| 378 | |
| 379 | |
| 380 | #define USBVISION_DEBUG /* Turn on debug messages */ |
| 381 | |
| 382 | #ifdef USBVISION_DEBUG |
| 383 | #define PDEBUG(level, fmt, args...) \ |
| 384 | if (debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args) |
| 385 | #else |
| 386 | #define PDEBUG(level, fmt, args...) do {} while(0) |
| 387 | #endif |
| 388 | |
| 389 | #define DBG_IOCTL 1<<3 |
| 390 | #define DBG_IO 1<<4 |
| 391 | #define DBG_RIO 1<<5 |
| 392 | #define DBG_HEADER 1<<7 |
| 393 | #define DBG_PROBE 1<<8 |
| 394 | #define DBG_IRQ 1<<9 |
| 395 | #define DBG_ISOC 1<<10 |
| 396 | #define DBG_PARSE 1<<11 |
| 397 | #define DBG_SCRATCH 1<<12 |
| 398 | #define DBG_FUNC 1<<13 |
| 399 | #define DBG_I2C 1<<14 |
| 400 | |
| 401 | #define DEBUG(x...) /* General Debug */ |
| 402 | #define IODEBUG(x...) /* Debug IO */ |
| 403 | #define OVDEBUG(x...) /* Debug overlay */ |
| 404 | #define MDEBUG(x...) /* Debug memory management */ |
| 405 | |
| 406 | //String operations |
| 407 | #define rmspace(str) while(*str==' ') str++; |
| 408 | #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++; |
| 409 | |
| 410 | |
| 411 | static int usbvision_nr = 0; // sequential number of usbvision device |
| 412 | static unsigned long usbvision_timestamp = 0; // timestamp in jiffies of a hundred frame |
| 413 | static unsigned long usbvision_counter = 0; // frame counter |
| 414 | |
| 415 | static const int max_imgwidth = MAX_FRAME_WIDTH; |
| 416 | static const int max_imgheight = MAX_FRAME_HEIGHT; |
| 417 | static const int min_imgwidth = MIN_FRAME_WIDTH; |
| 418 | static const int min_imgheight = MIN_FRAME_HEIGHT; |
| 419 | |
| 420 | #define FRAMERATE_MIN 0 |
| 421 | #define FRAMERATE_MAX 31 |
| 422 | |
| 423 | |
| 424 | enum { |
| 425 | ISOC_MODE_YUV422 = 0x03, |
| 426 | ISOC_MODE_YUV420 = 0x14, |
| 427 | ISOC_MODE_COMPRESS = 0x60, |
| 428 | }; |
| 429 | |
| 430 | static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = { |
| 431 | { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" }, |
| 432 | { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" }, |
| 433 | { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" }, |
| 434 | { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" }, |
| 435 | { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" }, |
| 436 | { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" }, |
| 437 | { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 ! |
| 438 | { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" } |
| 439 | }; |
| 440 | |
| 441 | /* supported tv norms */ |
| 442 | static struct usbvision_tvnorm tvnorms[] = { |
| 443 | { |
| 444 | .name = "PAL", |
| 445 | .id = V4L2_STD_PAL, |
| 446 | }, { |
| 447 | .name = "NTSC", |
| 448 | .id = V4L2_STD_NTSC, |
| 449 | }, { |
| 450 | .name = "SECAM", |
| 451 | .id = V4L2_STD_SECAM, |
| 452 | }, { |
| 453 | .name = "PAL-M", |
| 454 | .id = V4L2_STD_PAL_M, |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | #define TVNORMS ARRAY_SIZE(tvnorms) |
| 459 | |
| 460 | |
| 461 | /* |
| 462 | * The value of 'scratch_buf_size' affects quality of the picture |
| 463 | * in many ways. Shorter buffers may cause loss of data when client |
| 464 | * is too slow. Larger buffers are memory-consuming and take longer |
| 465 | * to work with. This setting can be adjusted, but the default value |
| 466 | * should be OK for most desktop users. |
| 467 | */ |
| 468 | #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer |
| 469 | static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE; |
| 470 | |
| 471 | // Function prototypes |
| 472 | static int usbvision_restart_isoc(struct usb_usbvision *usbvision); |
| 473 | static int usbvision_begin_streaming(struct usb_usbvision *usbvision); |
| 474 | static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel); |
| 475 | static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, short len); |
| 476 | static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, short len); |
| 477 | static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg); |
| 478 | static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg, unsigned char value); |
| 479 | static int usbvision_request_intra (struct usb_usbvision *usbvision); |
| 480 | static int usbvision_unrequest_intra (struct usb_usbvision *usbvision); |
| 481 | static int usbvision_adjust_compression (struct usb_usbvision *usbvision); |
| 482 | static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision); |
| 483 | static void usbvision_release(struct usb_usbvision *usbvision); |
| 484 | static int usbvision_set_input(struct usb_usbvision *usbvision); |
| 485 | static int usbvision_set_output(struct usb_usbvision *usbvision, int width, int height); |
| 486 | static void usbvision_empty_framequeues(struct usb_usbvision *dev); |
| 487 | static int usbvision_stream_interrupt(struct usb_usbvision *dev); |
| 488 | static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, void *arg); |
| 489 | |
| 490 | |
| 491 | // Bit flags (options) |
| 492 | #define FLAGS_RETRY_VIDIOCSYNC (1 << 0) |
| 493 | #define FLAGS_MONOCHROME (1 << 1) |
| 494 | #define FLAGS_DISPLAY_HINTS (1 << 2) |
| 495 | #define FLAGS_OSD_STATS (1 << 3) |
| 496 | #define FLAGS_FORCE_TESTPATTERN (1 << 4) |
| 497 | #define FLAGS_SEPARATE_FRAMES (1 << 5) |
| 498 | #define FLAGS_CLEAN_FRAMES (1 << 6) |
| 499 | |
| 500 | // Default initalization of device driver parameters |
| 501 | static int flags = 0; // Set the default Overlay Display mode of the device driver |
| 502 | static int debug = 0; // Set the default Debug Mode of the device driver |
| 503 | static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint |
| 504 | static int adjustCompression = 1; // Set the compression to be adaptive |
| 505 | static int dga = 1; // Set the default Direct Graphic Access |
| 506 | static int PowerOnAtOpen = 1; // Set the default device to power on at startup |
| 507 | 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. |
| 508 | static int video_nr = -1; // Sequential Number of Video Device |
| 509 | static int radio_nr = -1; // Sequential Number of Radio Device |
| 510 | static int vbi_nr = -1; // Sequential Number of VBI Device |
| 511 | static char *CustomDevice=NULL; // Set as nothing.... |
| 512 | |
| 513 | // Grab parameters for the device driver |
| 514 | |
| 515 | #if defined(module_param) // Showing parameters under SYSFS |
| 516 | module_param(flags, int, 0444); |
| 517 | module_param(debug, int, 0444); |
| 518 | module_param(isocMode, int, 0444); |
| 519 | module_param(adjustCompression, int, 0444); |
| 520 | module_param(dga, int, 0444); |
| 521 | module_param(PowerOnAtOpen, int, 0444); |
| 522 | module_param(SwitchSVideoInput, int, 0444); |
| 523 | module_param(video_nr, int, 0444); |
| 524 | module_param(radio_nr, int, 0444); |
| 525 | module_param(vbi_nr, int, 0444); |
| 526 | module_param(CustomDevice, charp, 0444); |
| 527 | #else // Old Style |
| 528 | MODULE_PARM(flags, "i"); // Grab the Overlay Display mode of the device driver |
| 529 | MODULE_PARM(debug, "i"); // Grab the Debug Mode of the device driver |
| 530 | MODULE_PARM(isocMode, "i"); // Grab the video format of the video device |
| 531 | MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive |
| 532 | MODULE_PARM(dga, "i"); // Grab the Direct Graphic Access |
| 533 | MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup |
| 534 | 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. |
| 535 | MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...) |
| 536 | MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...) |
| 537 | MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...) |
| 538 | MODULE_PARM(CustomDevice, "s"); // .... CustomDevice |
| 539 | #endif |
| 540 | |
| 541 | MODULE_PARM_DESC(flags, " Set the default Overlay Display mode of the device driver. Default: 0 (Off)"); |
| 542 | MODULE_PARM_DESC(debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)"); |
| 543 | MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)"); |
| 544 | MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)"); |
| 545 | MODULE_PARM_DESC(dga, " Set the Direct Graphic Access for the device. Default: 1 (On)"); |
| 546 | MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)"); |
| 547 | MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)"); |
| 548 | MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)"); |
| 549 | MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)"); |
| 550 | MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)"); |
| 551 | MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null"); |
| 552 | |
| 553 | |
| 554 | // Misc stuff |
| 555 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 556 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 557 | MODULE_LICENSE(DRIVER_LICENSE); |
| 558 | MODULE_VERSION(DRIVER_VERSION); |
| 559 | MODULE_ALIAS(DRIVER_ALIAS); |
| 560 | |
| 561 | #ifdef MODULE |
| 562 | static unsigned int autoload = 1; |
| 563 | #else |
| 564 | static unsigned int autoload = 0; |
| 565 | #endif |
| 566 | |
| 567 | |
| 568 | /****************************************************************************************/ |
| 569 | /* SYSFS Code - Copied from the stv680.c usb module. */ |
| 570 | /* Device information is located at /sys/class/video4linux/video0 */ |
| 571 | /* Device parameters information is located at /sys/module/usbvision */ |
| 572 | /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */ |
| 573 | /****************************************************************************************/ |
| 574 | |
| 575 | |
| 576 | #define YES_NO(x) ((x) ? "Yes" : "No") |
| 577 | |
| 578 | static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd) |
| 579 | { |
| 580 | struct video_device *vdev = to_video_device(cd); |
| 581 | return video_get_drvdata(vdev); |
| 582 | } |
| 583 | |
| 584 | static ssize_t show_version(struct class_device *cd, char *buf) |
| 585 | { |
| 586 | return sprintf(buf, "%s\n", DRIVER_VERSION); |
| 587 | } |
| 588 | static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL); |
| 589 | |
| 590 | static ssize_t show_model(struct class_device *class_dev, char *buf) |
| 591 | { |
| 592 | struct video_device *vdev = to_video_device(class_dev); |
| 593 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 594 | return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString); |
| 595 | } |
| 596 | static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL); |
| 597 | |
| 598 | static ssize_t show_hue(struct class_device *class_dev, char *buf) |
| 599 | { |
| 600 | struct video_device *vdev = to_video_device(class_dev); |
| 601 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 602 | return sprintf(buf, "%d\n", usbvision->hue >> 8); |
| 603 | } |
| 604 | static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL); |
| 605 | |
| 606 | static ssize_t show_contrast(struct class_device *class_dev, char *buf) |
| 607 | { |
| 608 | struct video_device *vdev = to_video_device(class_dev); |
| 609 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 610 | return sprintf(buf, "%d\n", usbvision->contrast >> 8); |
| 611 | } |
| 612 | static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL); |
| 613 | |
| 614 | static ssize_t show_brightness(struct class_device *class_dev, char *buf) |
| 615 | { |
| 616 | struct video_device *vdev = to_video_device(class_dev); |
| 617 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 618 | return sprintf(buf, "%d\n", usbvision->brightness >> 8); |
| 619 | } |
| 620 | static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL); |
| 621 | |
| 622 | static ssize_t show_saturation(struct class_device *class_dev, char *buf) |
| 623 | { |
| 624 | struct video_device *vdev = to_video_device(class_dev); |
| 625 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 626 | return sprintf(buf, "%d\n", usbvision->saturation >> 8); |
| 627 | } |
| 628 | static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL); |
| 629 | |
| 630 | static ssize_t show_streaming(struct class_device *class_dev, char *buf) |
| 631 | { |
| 632 | struct video_device *vdev = to_video_device(class_dev); |
| 633 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 634 | return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0)); |
| 635 | } |
| 636 | static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL); |
| 637 | |
| 638 | static ssize_t show_overlay(struct class_device *class_dev, char *buf) |
| 639 | { |
| 640 | struct video_device *vdev = to_video_device(class_dev); |
| 641 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 642 | return sprintf(buf, "%s\n", YES_NO(usbvision->overlay)); |
| 643 | } |
| 644 | static CLASS_DEVICE_ATTR(overlay, S_IRUGO, show_overlay, NULL); |
| 645 | |
| 646 | static ssize_t show_compression(struct class_device *class_dev, char *buf) |
| 647 | { |
| 648 | struct video_device *vdev = to_video_device(class_dev); |
| 649 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 650 | return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS)); |
| 651 | } |
| 652 | static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL); |
| 653 | |
| 654 | static ssize_t show_device_bridge(struct class_device *class_dev, char *buf) |
| 655 | { |
| 656 | struct video_device *vdev = to_video_device(class_dev); |
| 657 | struct usb_usbvision *usbvision = video_get_drvdata(vdev); |
| 658 | return sprintf(buf, "%d\n", usbvision->bridgeType); |
| 659 | } |
| 660 | static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL); |
| 661 | |
| 662 | static void usbvision_create_sysfs(struct video_device *vdev) |
| 663 | { |
| 664 | int res; |
| 665 | if (vdev) { |
| 666 | res=video_device_create_file(vdev, &class_device_attr_version); |
| 667 | res=video_device_create_file(vdev, &class_device_attr_model); |
| 668 | res=video_device_create_file(vdev, &class_device_attr_hue); |
| 669 | res=video_device_create_file(vdev, &class_device_attr_contrast); |
| 670 | res=video_device_create_file(vdev, &class_device_attr_brightness); |
| 671 | res=video_device_create_file(vdev, &class_device_attr_saturation); |
| 672 | res=video_device_create_file(vdev, &class_device_attr_streaming); |
| 673 | res=video_device_create_file(vdev, &class_device_attr_overlay); |
| 674 | res=video_device_create_file(vdev, &class_device_attr_compression); |
| 675 | res=video_device_create_file(vdev, &class_device_attr_bridge); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static void usbvision_remove_sysfs(struct video_device *vdev) |
| 680 | { |
| 681 | if (vdev) { |
| 682 | video_device_remove_file(vdev, &class_device_attr_version); |
| 683 | video_device_remove_file(vdev, &class_device_attr_model); |
| 684 | video_device_remove_file(vdev, &class_device_attr_hue); |
| 685 | video_device_remove_file(vdev, &class_device_attr_contrast); |
| 686 | video_device_remove_file(vdev, &class_device_attr_brightness); |
| 687 | video_device_remove_file(vdev, &class_device_attr_saturation); |
| 688 | video_device_remove_file(vdev, &class_device_attr_streaming); |
| 689 | video_device_remove_file(vdev, &class_device_attr_overlay); |
| 690 | video_device_remove_file(vdev, &class_device_attr_compression); |
| 691 | video_device_remove_file(vdev, &class_device_attr_bridge); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | |
| 696 | /*******************************/ |
| 697 | /* Memory management functions */ |
| 698 | /*******************************/ |
| 699 | |
| 700 | /* |
| 701 | * Here we want the physical address of the memory. |
| 702 | * This is used when initializing the contents of the area. |
| 703 | */ |
| 704 | |
| 705 | |
| 706 | void *usbvision_rvmalloc(unsigned long size) |
| 707 | { |
| 708 | void *mem; |
| 709 | unsigned long adr; |
| 710 | |
| 711 | size = PAGE_ALIGN(size); |
| 712 | mem = vmalloc_32(size); |
| 713 | if (!mem) |
| 714 | return NULL; |
| 715 | |
| 716 | memset(mem, 0, size); /* Clear the ram out, no junk to the user */ |
| 717 | adr = (unsigned long) mem; |
| 718 | while (size > 0) { |
| 719 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 720 | mem_map_reserve(vmalloc_to_page((void *)adr)); |
| 721 | #else |
| 722 | SetPageReserved(vmalloc_to_page((void *)adr)); |
| 723 | #endif |
| 724 | adr += PAGE_SIZE; |
| 725 | size -= PAGE_SIZE; |
| 726 | } |
| 727 | |
| 728 | return mem; |
| 729 | } |
| 730 | |
| 731 | void usbvision_rvfree(void *mem, unsigned long size) |
| 732 | { |
| 733 | unsigned long adr; |
| 734 | |
| 735 | if (!mem) |
| 736 | return; |
| 737 | |
| 738 | adr = (unsigned long) mem; |
| 739 | while ((long) size > 0) { |
| 740 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 741 | mem_map_unreserve(vmalloc_to_page((void *)adr)); |
| 742 | #else |
| 743 | ClearPageReserved(vmalloc_to_page((void *)adr)); |
| 744 | #endif |
| 745 | adr += PAGE_SIZE; |
| 746 | size -= PAGE_SIZE; |
| 747 | } |
| 748 | vfree(mem); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | #if ENABLE_HEXDUMP |
| 757 | static void usbvision_hexdump(const unsigned char *data, int len) |
| 758 | { |
| 759 | char tmp[80]; |
| 760 | int i, k; |
| 761 | |
| 762 | for (i = k = 0; len > 0; i++, len--) { |
| 763 | if (i > 0 && (i % 16 == 0)) { |
| 764 | printk("%s\n", tmp); |
| 765 | k = 0; |
| 766 | } |
| 767 | k += sprintf(&tmp[k], "%02x ", data[i]); |
| 768 | } |
| 769 | if (k > 0) |
| 770 | printk("%s\n", tmp); |
| 771 | } |
| 772 | #endif |
| 773 | |
| 774 | |
| 775 | /* These procedures handle the scratch ring buffer */ |
| 776 | int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */ |
| 777 | { |
| 778 | int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr; |
| 779 | if (len < 0) { |
| 780 | len += scratch_buf_size; |
| 781 | } |
| 782 | PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len); |
| 783 | |
| 784 | return len; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /* This returns the free space left in the buffer */ |
| 789 | int scratch_free(struct usb_usbvision *usbvision) |
| 790 | { |
| 791 | int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr; |
| 792 | if (free <= 0) { |
| 793 | free += scratch_buf_size; |
| 794 | } |
| 795 | if (free) { |
| 796 | free -= 1; /* at least one byte in the buffer must */ |
| 797 | /* left blank, otherwise there is no chance to differ between full and empty */ |
| 798 | } |
| 799 | PDEBUG(DBG_SCRATCH, "return %d\n", free); |
| 800 | |
| 801 | return free; |
| 802 | } |
| 803 | |
| 804 | |
| 805 | void *debug_memcpy(void *dest, void *src, size_t len) |
| 806 | { |
| 807 | printk(KERN_DEBUG "memcpy(%p, %p, %d);\n", dest, src, len); |
| 808 | return memcpy(dest, src, len); |
| 809 | } |
| 810 | |
| 811 | |
| 812 | /* This puts data into the buffer */ |
| 813 | int scratch_put(struct usb_usbvision *usbvision, unsigned char *data, int len) |
| 814 | { |
| 815 | int len_part; |
| 816 | |
| 817 | if (usbvision->scratch_write_ptr + len < scratch_buf_size) { |
| 818 | memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len); |
| 819 | usbvision->scratch_write_ptr += len; |
| 820 | } |
| 821 | else { |
| 822 | len_part = scratch_buf_size - usbvision->scratch_write_ptr; |
| 823 | memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part); |
| 824 | if (len == len_part) { |
| 825 | usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */ |
| 826 | } |
| 827 | else { |
| 828 | memcpy(usbvision->scratch, data + len_part, len - len_part); |
| 829 | usbvision->scratch_write_ptr = len - len_part; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr); |
| 834 | |
| 835 | return len; |
| 836 | } |
| 837 | |
| 838 | /* This marks the write_ptr as position of new frame header */ |
| 839 | void scratch_mark_header(struct usb_usbvision *usbvision) |
| 840 | { |
| 841 | PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr); |
| 842 | |
| 843 | usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] = |
| 844 | usbvision->scratch_write_ptr; |
| 845 | usbvision->scratch_headermarker_write_ptr += 1; |
| 846 | usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER; |
| 847 | } |
| 848 | |
| 849 | /* This gets data from the buffer at the given "ptr" position */ |
| 850 | int scratch_get_extra(struct usb_usbvision *usbvision, unsigned char *data, int *ptr, int len) |
| 851 | { |
| 852 | int len_part; |
| 853 | if (*ptr + len < scratch_buf_size) { |
| 854 | memcpy(data, usbvision->scratch + *ptr, len); |
| 855 | *ptr += len; |
| 856 | } |
| 857 | else { |
| 858 | len_part = scratch_buf_size - *ptr; |
| 859 | memcpy(data, usbvision->scratch + *ptr, len_part); |
| 860 | if (len == len_part) { |
| 861 | *ptr = 0; /* just set the y_ptr to zero */ |
| 862 | } |
| 863 | else { |
| 864 | memcpy(data + len_part, usbvision->scratch, len - len_part); |
| 865 | *ptr = len - len_part; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr); |
| 870 | |
| 871 | return len; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | /* This sets the scratch extra read pointer */ |
| 876 | void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr, int len) |
| 877 | { |
| 878 | *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size; |
| 879 | |
| 880 | PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | /*This increments the scratch extra read pointer */ |
| 885 | void scratch_inc_extra_ptr(int *ptr, int len) |
| 886 | { |
| 887 | *ptr = (*ptr + len) % scratch_buf_size; |
| 888 | |
| 889 | PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr); |
| 890 | } |
| 891 | |
| 892 | |
| 893 | /* This gets data from the buffer */ |
| 894 | int scratch_get(struct usb_usbvision *usbvision, unsigned char *data, int len) |
| 895 | { |
| 896 | int len_part; |
| 897 | if (usbvision->scratch_read_ptr + len < scratch_buf_size) { |
| 898 | memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len); |
| 899 | usbvision->scratch_read_ptr += len; |
| 900 | } |
| 901 | else { |
| 902 | len_part = scratch_buf_size - usbvision->scratch_read_ptr; |
| 903 | memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part); |
| 904 | if (len == len_part) { |
| 905 | usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */ |
| 906 | } |
| 907 | else { |
| 908 | memcpy(data + len_part, usbvision->scratch, len - len_part); |
| 909 | usbvision->scratch_read_ptr = len - len_part; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr); |
| 914 | |
| 915 | return len; |
| 916 | } |
| 917 | |
| 918 | |
| 919 | /* This sets read pointer to next header and returns it */ |
| 920 | int scratch_get_header(struct usb_usbvision *usbvision,struct usbvision_frame_header *header) |
| 921 | { |
| 922 | int errCode = 0; |
| 923 | |
| 924 | PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr); |
| 925 | |
| 926 | while (usbvision->scratch_headermarker_write_ptr - |
| 927 | usbvision->scratch_headermarker_read_ptr != 0) { |
| 928 | usbvision->scratch_read_ptr = |
| 929 | usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr]; |
| 930 | usbvision->scratch_headermarker_read_ptr += 1; |
| 931 | usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER; |
| 932 | scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH); |
| 933 | if ((header->magic_1 == USBVISION_MAGIC_1) |
| 934 | && (header->magic_2 == USBVISION_MAGIC_2) |
| 935 | && (header->headerLength == USBVISION_HEADER_LENGTH)) { |
| 936 | errCode = USBVISION_HEADER_LENGTH; |
| 937 | header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8); |
| 938 | header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8); |
| 939 | break; |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return errCode; |
| 944 | } |
| 945 | |
| 946 | |
| 947 | /*This removes len bytes of old data from the buffer */ |
| 948 | void scratch_rm_old(struct usb_usbvision *usbvision, int len) |
| 949 | { |
| 950 | |
| 951 | usbvision->scratch_read_ptr += len; |
| 952 | usbvision->scratch_read_ptr %= scratch_buf_size; |
| 953 | PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr); |
| 954 | } |
| 955 | |
| 956 | |
| 957 | /*This resets the buffer - kills all data in it too */ |
| 958 | void scratch_reset(struct usb_usbvision *usbvision) |
| 959 | { |
| 960 | PDEBUG(DBG_SCRATCH, "\n"); |
| 961 | |
| 962 | usbvision->scratch_read_ptr = 0; |
| 963 | usbvision->scratch_write_ptr = 0; |
| 964 | usbvision->scratch_headermarker_read_ptr = 0; |
| 965 | usbvision->scratch_headermarker_write_ptr = 0; |
| 966 | usbvision->isocstate = IsocState_NoFrame; |
| 967 | } |
| 968 | |
| 969 | |
| 970 | |
| 971 | /* Here comes the OVERLAY stuff */ |
| 972 | |
| 973 | /* Tell the interrupt handler what to to. */ |
| 974 | static |
| 975 | void usbvision_cap(struct usb_usbvision* usbvision, int on) |
| 976 | { |
| 977 | DEBUG(printk(KERN_DEBUG "usbvision_cap: overlay was %d, set it to %d\n", usbvision->overlay, on);) |
| 978 | |
| 979 | if (on) { |
| 980 | usbvision->overlay = 1; |
| 981 | } |
| 982 | else { |
| 983 | usbvision->overlay = 0; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | /* append a new clipregion to the vector of video_clips */ |
| 991 | static |
| 992 | void usbvision_new_clip(struct v4l2_format* vf, struct v4l2_clip* vcp, int x, int y, int w, int h) |
| 993 | { |
| 994 | vcp[vf->fmt.win.clipcount].c.left = x; |
| 995 | vcp[vf->fmt.win.clipcount].c.top = y; |
| 996 | vcp[vf->fmt.win.clipcount].c.width = w; |
| 997 | vcp[vf->fmt.win.clipcount].c.height = h; |
| 998 | vf->fmt.win.clipcount++; |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | #define mark_pixel(x,y) usbvision->clipmask[((x) + (y) * MAX_FRAME_WIDTH)/32] |= 0x00000001<<((x)%32) |
| 1003 | #define clipped_pixel(index) usbvision->clipmask[(index)/32] & (0x00000001<<((index)%32)) |
| 1004 | |
| 1005 | static |
| 1006 | void usbvision_built_overlay(struct usb_usbvision* usbvision, int count, struct v4l2_clip *vcp) |
| 1007 | { |
| 1008 | usbvision->overlay_win = usbvision->overlay_base + |
| 1009 | (signed int)usbvision->vid_win.fmt.win.w.left * usbvision->depth / 8 + |
| 1010 | (signed int)usbvision->vid_win.fmt.win.w.top * usbvision->vid_buf.fmt.bytesperline; |
| 1011 | |
| 1012 | IODEBUG(printk(KERN_DEBUG "built_overlay base=%p, win=%p, bpl=%d, clips=%d, size=%dx%d\n", |
| 1013 | usbvision->overlay_base, usbvision->overlay_win, |
| 1014 | usbvision->vid_buf.fmt.bytesperline, count, |
| 1015 | usbvision->vid_win.fmt.win.w.width, usbvision->vid_win.fmt.win.w.height);) |
| 1016 | |
| 1017 | |
| 1018 | /* Add here generation of clipping mask */ |
| 1019 | { |
| 1020 | int x_start, x_end, y_start, y_end; |
| 1021 | int clip_index, x, y; |
| 1022 | |
| 1023 | memset(usbvision->clipmask, 0, USBVISION_CLIPMASK_SIZE); |
| 1024 | |
| 1025 | OVDEBUG(printk(KERN_DEBUG "clips = %d\n", count);) |
| 1026 | |
| 1027 | for(clip_index = 0; clip_index < count; clip_index++) { |
| 1028 | OVDEBUG(printk(KERN_DEBUG "clip: %d,%d,%d,%d\n", vcp[clip_index].x, |
| 1029 | vcp[clip_index].y, |
| 1030 | vcp[clip_index].width, |
| 1031 | vcp[clip_index].height);) |
| 1032 | |
| 1033 | x_start = vcp[clip_index].c.left; |
| 1034 | if(x_start >= (int)usbvision->vid_win.fmt.win.w.width) { |
| 1035 | OVDEBUG(printk(KERN_DEBUG "x_start=%d\n", x_start);) |
| 1036 | continue; //clipping window is right of overlay window |
| 1037 | } |
| 1038 | x_end = x_start + vcp[clip_index].c.width; |
| 1039 | if(x_end <= 0) { |
| 1040 | OVDEBUG(printk(KERN_DEBUG "x_end=%d\n", x_end);) |
| 1041 | continue; //clipping window is left of overlay window |
| 1042 | } |
| 1043 | |
| 1044 | y_start = vcp[clip_index].c.top; |
| 1045 | if(y_start >= (int)usbvision->vid_win.fmt.win.w.height) { |
| 1046 | OVDEBUG(printk(KERN_DEBUG "y_start=%d\n", y_start);) |
| 1047 | continue; //clipping window is below overlay window |
| 1048 | } |
| 1049 | y_end = y_start + vcp[clip_index].c.height; |
| 1050 | if(y_end <= 0) { |
| 1051 | OVDEBUG(printk(KERN_DEBUG "y_end=%d\n", y_end);) |
| 1052 | continue; //clipping window is above overlay window |
| 1053 | } |
| 1054 | |
| 1055 | //clip the clipping window |
| 1056 | if (x_start < 0) { |
| 1057 | x_start = 0; |
| 1058 | } |
| 1059 | if (x_end > (int)usbvision->vid_win.fmt.win.w.width) { |
| 1060 | x_end = (int)usbvision->vid_win.fmt.win.w.width; |
| 1061 | } |
| 1062 | if (y_start < 0) { |
| 1063 | y_start = 0; |
| 1064 | } |
| 1065 | if (y_end > (int)usbvision->vid_win.fmt.win.w.height) { |
| 1066 | y_end = (int)usbvision->vid_win.fmt.win.w.height; |
| 1067 | } |
| 1068 | |
| 1069 | OVDEBUG(printk(KERN_DEBUG "clip_o: %d,%d,%d,%d\n", x_start, y_start, x_end, y_end);) |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | for(y = y_start; y < y_end; y++) { |
| 1074 | for(x = x_start; x < x_end; x++) { |
| 1075 | mark_pixel(x,y); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | void usbvision_osd_char(struct usb_usbvision *usbvision, |
| 1086 | struct usbvision_frame *frame, int x, int y, int ch) |
| 1087 | { |
| 1088 | static const unsigned short digits[16] = { |
| 1089 | 0xF6DE, /* 0 */ |
| 1090 | 0x2492, /* 1 */ |
| 1091 | 0xE7CE, /* 2 */ |
| 1092 | 0xE79E, /* 3 */ |
| 1093 | 0xB792, /* 4 */ |
| 1094 | 0xF39E, /* 5 */ |
| 1095 | 0xF3DE, /* 6 */ |
| 1096 | 0xF492, /* 7 */ |
| 1097 | 0xF7DE, /* 8 */ |
| 1098 | 0xF79E, /* 9 */ |
| 1099 | 0x77DA, /* a */ |
| 1100 | 0xD75C, /* b */ |
| 1101 | 0xF24E, /* c */ |
| 1102 | 0xD6DC, /* d */ |
| 1103 | 0xF34E, /* e */ |
| 1104 | 0xF348 /* f */ |
| 1105 | }; |
| 1106 | unsigned short digit; |
| 1107 | int ix, iy; |
| 1108 | |
| 1109 | if ((usbvision == NULL) || (frame == NULL)) |
| 1110 | return; |
| 1111 | |
| 1112 | if (ch >= '0' && ch <= '9') |
| 1113 | ch -= '0'; |
| 1114 | else if (ch >= 'A' && ch <= 'F') |
| 1115 | ch = 10 + (ch - 'A'); |
| 1116 | else if (ch >= 'a' && ch <= 'f') |
| 1117 | ch = 10 + (ch - 'a'); |
| 1118 | else |
| 1119 | return; |
| 1120 | digit = digits[ch]; |
| 1121 | |
| 1122 | for (iy = 0; iy < 5; iy++) { |
| 1123 | for (ix = 0; ix < 3; ix++) { |
| 1124 | if (digit & 0x8000) { |
| 1125 | // USBVISION_PUTPIXEL(frame, x + ix, y + iy, |
| 1126 | // 0xFF, 0xFF, 0xFF); |
| 1127 | } |
| 1128 | digit = digit << 1; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | void usbvision_osd_string(struct usb_usbvision *usbvision, |
| 1135 | struct usbvision_frame *frame, |
| 1136 | int x, int y, const char *str) |
| 1137 | { |
| 1138 | while (*str) { |
| 1139 | usbvision_osd_char(usbvision, frame, x, y, *str); |
| 1140 | str++; |
| 1141 | x += 4; /* 3 pixels character + 1 space */ |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * usb_usbvision_osd_stats() |
| 1147 | * |
| 1148 | * On screen display of important debugging information. |
| 1149 | * |
| 1150 | */ |
| 1151 | void usbvision_osd_stats(struct usb_usbvision *usbvision, |
| 1152 | struct usbvision_frame *frame) |
| 1153 | { |
| 1154 | const int y_diff = 8; |
| 1155 | char tmp[16]; |
| 1156 | int x = 10; |
| 1157 | int y = 10; |
| 1158 | |
| 1159 | sprintf(tmp, "%8x", usbvision->frame_num); |
| 1160 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1161 | y += y_diff; |
| 1162 | |
| 1163 | sprintf(tmp, "%8lx", usbvision->isocUrbCount); |
| 1164 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1165 | y += y_diff; |
| 1166 | |
| 1167 | sprintf(tmp, "%8lx", usbvision->urb_length); |
| 1168 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1169 | y += y_diff; |
| 1170 | |
| 1171 | sprintf(tmp, "%8lx", usbvision->isocDataCount); |
| 1172 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1173 | y += y_diff; |
| 1174 | |
| 1175 | sprintf(tmp, "%8lx", usbvision->header_count); |
| 1176 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1177 | y += y_diff; |
| 1178 | |
| 1179 | sprintf(tmp, "%8lx", usbvision->scratch_ovf_count); |
| 1180 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1181 | y += y_diff; |
| 1182 | |
| 1183 | sprintf(tmp, "%8lx", usbvision->isocSkipCount); |
| 1184 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1185 | y += y_diff; |
| 1186 | |
| 1187 | sprintf(tmp, "%8lx", usbvision->isocErrCount); |
| 1188 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1189 | y += y_diff; |
| 1190 | |
| 1191 | sprintf(tmp, "%8x", usbvision->saturation); |
| 1192 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1193 | y += y_diff; |
| 1194 | |
| 1195 | sprintf(tmp, "%8x", usbvision->hue); |
| 1196 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1197 | y += y_diff; |
| 1198 | |
| 1199 | sprintf(tmp, "%8x", usbvision->brightness >> 8); |
| 1200 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1201 | y += y_diff; |
| 1202 | |
| 1203 | sprintf(tmp, "%8x", usbvision->contrast >> 12); |
| 1204 | usbvision_osd_string(usbvision, frame, x, y, tmp); |
| 1205 | y += y_diff; |
| 1206 | |
| 1207 | } |
| 1208 | |
| 1209 | /* |
| 1210 | * usbvision_testpattern() |
| 1211 | * |
| 1212 | * Procedure forms a test pattern (yellow grid on blue background). |
| 1213 | * |
| 1214 | * Parameters: |
| 1215 | * fullframe: if TRUE then entire frame is filled, otherwise the procedure |
| 1216 | * continues from the current scanline. |
| 1217 | * pmode 0: fill the frame with solid blue color (like on VCR or TV) |
| 1218 | * 1: Draw a colored grid |
| 1219 | * |
| 1220 | */ |
| 1221 | void usbvision_testpattern(struct usb_usbvision *usbvision, int fullframe, |
| 1222 | int pmode) |
| 1223 | { |
| 1224 | static const char proc[] = "usbvision_testpattern"; |
| 1225 | struct usbvision_frame *frame; |
| 1226 | unsigned char *f; |
| 1227 | int num_cell = 0; |
| 1228 | int scan_length = 0; |
| 1229 | static int num_pass = 0; |
| 1230 | |
| 1231 | if (usbvision == NULL) { |
| 1232 | printk(KERN_ERR "%s: usbvision == NULL\n", proc); |
| 1233 | return; |
| 1234 | } |
| 1235 | if (usbvision->curFrame == NULL) { |
| 1236 | printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | /* Grab the current frame */ |
| 1241 | frame = usbvision->curFrame; |
| 1242 | |
| 1243 | /* Optionally start at the beginning */ |
| 1244 | if (fullframe) { |
| 1245 | frame->curline = 0; |
| 1246 | frame->scanlength = 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* Form every scan line */ |
| 1250 | for (; frame->curline < frame->frmheight; frame->curline++) { |
| 1251 | int i; |
| 1252 | |
| 1253 | f = frame->data + (usbvision->curwidth * 3 * frame->curline); |
| 1254 | for (i = 0; i < usbvision->curwidth; i++) { |
| 1255 | unsigned char cb = 0x80; |
| 1256 | unsigned char cg = 0; |
| 1257 | unsigned char cr = 0; |
| 1258 | |
| 1259 | if (pmode == 1) { |
| 1260 | if (frame->curline % 32 == 0) |
| 1261 | cb = 0, cg = cr = 0xFF; |
| 1262 | else if (i % 32 == 0) { |
| 1263 | if (frame->curline % 32 == 1) |
| 1264 | num_cell++; |
| 1265 | cb = 0, cg = cr = 0xFF; |
| 1266 | } else { |
| 1267 | cb = |
| 1268 | ((num_cell * 7) + |
| 1269 | num_pass) & 0xFF; |
| 1270 | cg = |
| 1271 | ((num_cell * 5) + |
| 1272 | num_pass * 2) & 0xFF; |
| 1273 | cr = |
| 1274 | ((num_cell * 3) + |
| 1275 | num_pass * 3) & 0xFF; |
| 1276 | } |
| 1277 | } else { |
| 1278 | /* Just the blue screen */ |
| 1279 | } |
| 1280 | |
| 1281 | *f++ = cb; |
| 1282 | *f++ = cg; |
| 1283 | *f++ = cr; |
| 1284 | scan_length += 3; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | frame->grabstate = FrameState_Done; |
| 1289 | frame->scanlength += scan_length; |
| 1290 | ++num_pass; |
| 1291 | |
| 1292 | /* We do this unconditionally, regardless of FLAGS_OSD_STATS */ |
| 1293 | usbvision_osd_stats(usbvision, frame); |
| 1294 | } |
| 1295 | |
| 1296 | /* |
| 1297 | * Here comes the data parsing stuff that is run as interrupt |
| 1298 | */ |
| 1299 | |
| 1300 | /* |
| 1301 | * usbvision_find_header() |
| 1302 | * |
| 1303 | * Locate one of supported header markers in the scratch buffer. |
| 1304 | */ |
| 1305 | static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision) |
| 1306 | { |
| 1307 | struct usbvision_frame *frame; |
| 1308 | int foundHeader = 0; |
| 1309 | |
| 1310 | if (usbvision->overlay) { |
| 1311 | frame = &usbvision->overlay_frame; |
| 1312 | } |
| 1313 | else { |
| 1314 | frame = usbvision->curFrame; |
| 1315 | } |
| 1316 | |
| 1317 | while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) { |
| 1318 | // found header in scratch |
| 1319 | PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u", |
| 1320 | frame->isocHeader.magic_2, |
| 1321 | frame->isocHeader.magic_1, |
| 1322 | frame->isocHeader.headerLength, |
| 1323 | frame->isocHeader.frameNum, |
| 1324 | frame->isocHeader.framePhase, |
| 1325 | frame->isocHeader.frameLatency, |
| 1326 | frame->isocHeader.dataFormat, |
| 1327 | frame->isocHeader.formatParam, |
| 1328 | frame->isocHeader.frameWidth, |
| 1329 | frame->isocHeader.frameHeight); |
| 1330 | |
| 1331 | if (usbvision->requestIntra) { |
| 1332 | if (frame->isocHeader.formatParam & 0x80) { |
| 1333 | foundHeader = 1; |
| 1334 | usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time |
| 1335 | usbvision_unrequest_intra(usbvision); |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | else { |
| 1340 | foundHeader = 1; |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if (foundHeader) { |
| 1346 | frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width; |
| 1347 | frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height; |
| 1348 | frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3; |
| 1349 | } |
| 1350 | else { // no header found |
| 1351 | PDEBUG(DBG_HEADER, "skipping scratch data, no header"); |
| 1352 | scratch_reset(usbvision); |
| 1353 | return ParseState_EndParse; |
| 1354 | } |
| 1355 | |
| 1356 | // found header |
| 1357 | if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) { |
| 1358 | //check isocHeader.frameNum for lost frames |
| 1359 | if (usbvision->lastIsocFrameNum >= 0) { |
| 1360 | if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) { |
| 1361 | // unexpected frame drop: need to request new intra frame |
| 1362 | PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum); |
| 1363 | usbvision_request_intra(usbvision); |
| 1364 | return ParseState_NextFrame; |
| 1365 | } |
| 1366 | } |
| 1367 | usbvision->lastIsocFrameNum = frame->isocHeader.frameNum; |
| 1368 | } |
| 1369 | usbvision->header_count++; |
| 1370 | frame->scanstate = ScanState_Lines; |
| 1371 | frame->curline = 0; |
| 1372 | |
| 1373 | if (flags & FLAGS_FORCE_TESTPATTERN) { |
| 1374 | usbvision_testpattern(usbvision, 1, 1); |
| 1375 | return ParseState_NextFrame; |
| 1376 | } |
| 1377 | return ParseState_Continue; |
| 1378 | } |
| 1379 | |
| 1380 | static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision, |
| 1381 | long *pcopylen) |
| 1382 | { |
| 1383 | volatile struct usbvision_frame *frame; |
| 1384 | unsigned char *f; |
| 1385 | int len; |
| 1386 | int i; |
| 1387 | unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components |
| 1388 | unsigned char rv, gv, bv; // RGB components |
| 1389 | int clipmask_index, bytes_per_pixel; |
| 1390 | int overlay = usbvision->overlay; |
| 1391 | int stretch_bytes, clipmask_add; |
| 1392 | |
| 1393 | if (overlay) { |
| 1394 | frame = &usbvision->overlay_frame; |
| 1395 | if (usbvision->overlay_base == NULL) { |
| 1396 | //video_buffer is not set yet |
| 1397 | return ParseState_NextFrame; |
| 1398 | } |
| 1399 | f = usbvision->overlay_win + frame->curline * |
| 1400 | usbvision->vid_buf.fmt.bytesperline; |
| 1401 | } |
| 1402 | else { |
| 1403 | frame = usbvision->curFrame; |
| 1404 | f = frame->data + (frame->v4l2_linesize * frame->curline); |
| 1405 | } |
| 1406 | |
| 1407 | /* Make sure there's enough data for the entire line */ |
| 1408 | len = (frame->isocHeader.frameWidth * 2)+5; |
| 1409 | if (scratch_len(usbvision) < len) { |
| 1410 | PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len); |
| 1411 | return ParseState_Out; |
| 1412 | } |
| 1413 | |
| 1414 | if ((frame->curline + 1) >= frame->frmheight) { |
| 1415 | return ParseState_NextFrame; |
| 1416 | } |
| 1417 | |
| 1418 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; |
| 1419 | stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel; |
| 1420 | clipmask_index = frame->curline * MAX_FRAME_WIDTH; |
| 1421 | clipmask_add = usbvision->stretch_width; |
| 1422 | |
| 1423 | for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) { |
| 1424 | |
| 1425 | scratch_get(usbvision, &yuyv[0], 4); |
| 1426 | |
| 1427 | if((overlay) && (clipped_pixel(clipmask_index))) { |
| 1428 | f += bytes_per_pixel; |
| 1429 | } |
| 1430 | else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1431 | *f++ = yuyv[0]; // Y |
| 1432 | *f++ = yuyv[3]; // U |
| 1433 | } |
| 1434 | else { |
| 1435 | |
| 1436 | YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv); |
| 1437 | switch (frame->v4l2_format.format) { |
| 1438 | case V4L2_PIX_FMT_RGB565: |
| 1439 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); |
| 1440 | *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); |
| 1441 | break; |
| 1442 | case V4L2_PIX_FMT_RGB24: |
| 1443 | *f++ = bv; |
| 1444 | *f++ = gv; |
| 1445 | *f++ = rv; |
| 1446 | break; |
| 1447 | case V4L2_PIX_FMT_RGB32: |
| 1448 | *f++ = bv; |
| 1449 | *f++ = gv; |
| 1450 | *f++ = rv; |
| 1451 | f++; |
| 1452 | break; |
| 1453 | case V4L2_PIX_FMT_RGB555: |
| 1454 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); |
| 1455 | *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | clipmask_index += clipmask_add; |
| 1460 | f += stretch_bytes; |
| 1461 | |
| 1462 | if((overlay) && (clipped_pixel(clipmask_index))) { |
| 1463 | f += bytes_per_pixel; |
| 1464 | } |
| 1465 | else if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1466 | *f++ = yuyv[2]; // Y |
| 1467 | *f++ = yuyv[1]; // V |
| 1468 | } |
| 1469 | else { |
| 1470 | |
| 1471 | YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv); |
| 1472 | switch (frame->v4l2_format.format) { |
| 1473 | case V4L2_PIX_FMT_RGB565: |
| 1474 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); |
| 1475 | *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); |
| 1476 | break; |
| 1477 | case V4L2_PIX_FMT_RGB24: |
| 1478 | *f++ = bv; |
| 1479 | *f++ = gv; |
| 1480 | *f++ = rv; |
| 1481 | break; |
| 1482 | case V4L2_PIX_FMT_RGB32: |
| 1483 | *f++ = bv; |
| 1484 | *f++ = gv; |
| 1485 | *f++ = rv; |
| 1486 | f++; |
| 1487 | break; |
| 1488 | case V4L2_PIX_FMT_RGB555: |
| 1489 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); |
| 1490 | *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | clipmask_index += clipmask_add; |
| 1495 | f += stretch_bytes; |
| 1496 | } |
| 1497 | |
| 1498 | frame->curline += usbvision->stretch_height; |
| 1499 | *pcopylen += frame->v4l2_linesize * usbvision->stretch_height; |
| 1500 | |
| 1501 | if (frame->curline >= frame->frmheight) { |
| 1502 | return ParseState_NextFrame; |
| 1503 | } |
| 1504 | else { |
| 1505 | return ParseState_Continue; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | |
| 1510 | static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed, |
| 1511 | unsigned char *Decompressed, int *StartPos, |
| 1512 | int *BlockTypeStartPos, int Len) |
| 1513 | { |
| 1514 | int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen; |
| 1515 | unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator; |
| 1516 | |
| 1517 | Integrator = 0; |
| 1518 | Pos = *StartPos; |
| 1519 | BlockTypePos = *BlockTypeStartPos; |
| 1520 | MaxPos = 396; //Pos + Len; |
| 1521 | ExtraPos = Pos; |
| 1522 | BlockLen = 0; |
| 1523 | BlockByte = 0; |
| 1524 | BlockCode = 0; |
| 1525 | BlockType = 0; |
| 1526 | BlockTypeByte = 0; |
| 1527 | BlockTypeLen = 0; |
| 1528 | RestPixel = Len; |
| 1529 | |
| 1530 | for (Idx = 0; Idx < Len; Idx++) { |
| 1531 | |
| 1532 | if (BlockLen == 0) { |
| 1533 | if (BlockTypeLen==0) { |
| 1534 | BlockTypeByte = Compressed[BlockTypePos]; |
| 1535 | BlockTypePos++; |
| 1536 | BlockTypeLen = 4; |
| 1537 | } |
| 1538 | BlockType = (BlockTypeByte & 0xC0) >> 6; |
| 1539 | |
| 1540 | //statistic: |
| 1541 | usbvision->ComprBlockTypes[BlockType]++; |
| 1542 | |
| 1543 | Pos = ExtraPos; |
| 1544 | if (BlockType == 0) { |
| 1545 | if(RestPixel >= 24) { |
| 1546 | Idx += 23; |
| 1547 | RestPixel -= 24; |
| 1548 | Integrator = Decompressed[Idx]; |
| 1549 | } else { |
| 1550 | Idx += RestPixel - 1; |
| 1551 | RestPixel = 0; |
| 1552 | } |
| 1553 | } else { |
| 1554 | BlockCode = Compressed[Pos]; |
| 1555 | Pos++; |
| 1556 | if (RestPixel >= 24) { |
| 1557 | BlockLen = 24; |
| 1558 | } else { |
| 1559 | BlockLen = RestPixel; |
| 1560 | } |
| 1561 | RestPixel -= BlockLen; |
| 1562 | ExtraPos = Pos + (BlockLen / 4); |
| 1563 | } |
| 1564 | BlockTypeByte <<= 2; |
| 1565 | BlockTypeLen -= 1; |
| 1566 | } |
| 1567 | if (BlockLen > 0) { |
| 1568 | if ((BlockLen%4) == 0) { |
| 1569 | BlockByte = Compressed[Pos]; |
| 1570 | Pos++; |
| 1571 | } |
| 1572 | if (BlockType == 1) { //inter Block |
| 1573 | Integrator = Decompressed[Idx]; |
| 1574 | } |
| 1575 | switch (BlockByte & 0xC0) { |
| 1576 | case 0x03<<6: |
| 1577 | Integrator += Compressed[ExtraPos]; |
| 1578 | ExtraPos++; |
| 1579 | break; |
| 1580 | case 0x02<<6: |
| 1581 | Integrator += BlockCode; |
| 1582 | break; |
| 1583 | case 0x00: |
| 1584 | Integrator -= BlockCode; |
| 1585 | break; |
| 1586 | } |
| 1587 | Decompressed[Idx] = Integrator; |
| 1588 | BlockByte <<= 2; |
| 1589 | BlockLen -= 1; |
| 1590 | } |
| 1591 | } |
| 1592 | *StartPos = ExtraPos; |
| 1593 | *BlockTypeStartPos = BlockTypePos; |
| 1594 | return Idx; |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | /* |
| 1599 | * usbvision_parse_compress() |
| 1600 | * |
| 1601 | * Parse compressed frame from the scratch buffer, put |
| 1602 | * decoded RGB value into the current frame buffer and add the written |
| 1603 | * number of bytes (RGB) to the *pcopylen. |
| 1604 | * |
| 1605 | */ |
| 1606 | static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision, |
| 1607 | long *pcopylen) |
| 1608 | { |
| 1609 | #define USBVISION_STRIP_MAGIC 0x5A |
| 1610 | #define USBVISION_STRIP_LEN_MAX 400 |
| 1611 | #define USBVISION_STRIP_HEADER_LEN 3 |
| 1612 | |
| 1613 | struct usbvision_frame *frame; |
| 1614 | unsigned char *f,*u = NULL ,*v = NULL; |
| 1615 | unsigned char StripData[USBVISION_STRIP_LEN_MAX]; |
| 1616 | unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN]; |
| 1617 | int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos; |
| 1618 | int clipmask_index, bytes_per_pixel, rc; |
| 1619 | int overlay = usbvision->overlay; |
| 1620 | int imageSize; |
| 1621 | unsigned char rv, gv, bv; |
| 1622 | static unsigned char *Y, *U, *V; |
| 1623 | |
| 1624 | if (overlay) { |
| 1625 | frame = &usbvision->overlay_frame; |
| 1626 | imageSize = frame->frmwidth * frame->frmheight; |
| 1627 | if (usbvision->overlay_base == NULL) { |
| 1628 | //video_buffer is not set yet |
| 1629 | return ParseState_NextFrame; |
| 1630 | } |
| 1631 | f = usbvision->overlay_win + frame->curline * |
| 1632 | usbvision->vid_buf.fmt.bytesperline; |
| 1633 | } |
| 1634 | else { |
| 1635 | frame = usbvision->curFrame; |
| 1636 | imageSize = frame->frmwidth * frame->frmheight; |
| 1637 | if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) || |
| 1638 | (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) |
| 1639 | { // this is a planar format |
| 1640 | //... v4l2_linesize not used here. |
| 1641 | f = frame->data + (frame->width * frame->curline); |
| 1642 | } else |
| 1643 | f = frame->data + (frame->v4l2_linesize * frame->curline); |
| 1644 | |
| 1645 | if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers |
| 1646 | // get base of u and b planes add halfoffset |
| 1647 | |
| 1648 | u = frame->data |
| 1649 | + imageSize |
| 1650 | + (frame->frmwidth >>1) * frame->curline ; |
| 1651 | v = u + (imageSize >>1 ); |
| 1652 | |
| 1653 | } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){ |
| 1654 | |
| 1655 | v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ; |
| 1656 | u = v + (imageSize >>2) ; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | if (frame->curline == 0) { |
| 1661 | usbvision_adjust_compression(usbvision); |
| 1662 | } |
| 1663 | |
| 1664 | if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) { |
| 1665 | return ParseState_Out; |
| 1666 | } |
| 1667 | |
| 1668 | //get strip header without changing the scratch_read_ptr |
| 1669 | scratch_set_extra_ptr(usbvision, &StripPtr, 0); |
| 1670 | scratch_get_extra(usbvision, &StripHeader[0], &StripPtr, |
| 1671 | USBVISION_STRIP_HEADER_LEN); |
| 1672 | |
| 1673 | if (StripHeader[0] != USBVISION_STRIP_MAGIC) { |
| 1674 | // wrong strip magic |
| 1675 | usbvision->stripMagicErrors++; |
| 1676 | return ParseState_NextFrame; |
| 1677 | } |
| 1678 | |
| 1679 | if (frame->curline != (int)StripHeader[2]) { |
| 1680 | //line number missmatch error |
| 1681 | usbvision->stripLineNumberErrors++; |
| 1682 | } |
| 1683 | |
| 1684 | StripLen = 2 * (unsigned int)StripHeader[1]; |
| 1685 | if (StripLen > USBVISION_STRIP_LEN_MAX) { |
| 1686 | // strip overrun |
| 1687 | // I think this never happens |
| 1688 | usbvision_request_intra(usbvision); |
| 1689 | } |
| 1690 | |
| 1691 | if (scratch_len(usbvision) < StripLen) { |
| 1692 | //there is not enough data for the strip |
| 1693 | return ParseState_Out; |
| 1694 | } |
| 1695 | |
| 1696 | if (usbvision->IntraFrameBuffer) { |
| 1697 | Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline; |
| 1698 | U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2); |
| 1699 | V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2); |
| 1700 | } |
| 1701 | else { |
| 1702 | return ParseState_NextFrame; |
| 1703 | } |
| 1704 | |
| 1705 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; |
| 1706 | clipmask_index = frame->curline * MAX_FRAME_WIDTH; |
| 1707 | |
| 1708 | scratch_get(usbvision, StripData, StripLen); |
| 1709 | |
| 1710 | IdxEnd = frame->frmwidth; |
| 1711 | BlockTypePos = USBVISION_STRIP_HEADER_LEN; |
| 1712 | StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2; |
| 1713 | BlockPos = StartBlockPos; |
| 1714 | |
| 1715 | usbvision->BlockPos = BlockPos; |
| 1716 | |
| 1717 | if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) { |
| 1718 | //return ParseState_Continue; |
| 1719 | } |
| 1720 | if (StripLen > usbvision->maxStripLen) { |
| 1721 | usbvision->maxStripLen = StripLen; |
| 1722 | } |
| 1723 | |
| 1724 | if (frame->curline%2) { |
| 1725 | if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) { |
| 1726 | //return ParseState_Continue; |
| 1727 | } |
| 1728 | } |
| 1729 | else { |
| 1730 | if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) { |
| 1731 | //return ParseState_Continue; |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | if (BlockPos > usbvision->comprBlockPos) { |
| 1736 | usbvision->comprBlockPos = BlockPos; |
| 1737 | } |
| 1738 | if (BlockPos > StripLen) { |
| 1739 | usbvision->stripLenErrors++; |
| 1740 | } |
| 1741 | |
| 1742 | for (Idx = 0; Idx < IdxEnd; Idx++) { |
| 1743 | if((overlay) && (clipped_pixel(clipmask_index))) { |
| 1744 | f += bytes_per_pixel; |
| 1745 | } |
| 1746 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1747 | *f++ = Y[Idx]; |
| 1748 | *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2]; |
| 1749 | } |
| 1750 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) { |
| 1751 | *f++ = Y[Idx]; |
| 1752 | if ( Idx & 0x01) |
| 1753 | *u++ = U[Idx>>1] ; |
| 1754 | else |
| 1755 | *v++ = V[Idx>>1]; |
| 1756 | } |
| 1757 | else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) { |
| 1758 | *f++ = Y [Idx]; |
| 1759 | if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){ |
| 1760 | |
| 1761 | /* only need do this for 1 in 4 pixels */ |
| 1762 | /* intraframe buffer is YUV420 format */ |
| 1763 | |
| 1764 | *u++ = U[Idx >>1]; |
| 1765 | *v++ = V[Idx >>1]; |
| 1766 | } |
| 1767 | |
| 1768 | } |
| 1769 | else { |
| 1770 | YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv); |
| 1771 | switch (frame->v4l2_format.format) { |
| 1772 | case V4L2_PIX_FMT_GREY: |
| 1773 | *f++ = Y[Idx]; |
| 1774 | break; |
| 1775 | case V4L2_PIX_FMT_RGB555: |
| 1776 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 2)); |
| 1777 | *f++ = (0x03 & (gv >> 6)) | (0x7C & (rv >> 1)); |
| 1778 | break; |
| 1779 | case V4L2_PIX_FMT_RGB565: |
| 1780 | *f++ = (0x1F & (bv >> 3)) | (0xE0 & (gv << 3)); |
| 1781 | *f++ = (0x07 & (gv >> 5)) | (0xF8 & rv); |
| 1782 | break; |
| 1783 | case V4L2_PIX_FMT_RGB24: |
| 1784 | *f++ = bv; |
| 1785 | *f++ = gv; |
| 1786 | *f++ = rv; |
| 1787 | break; |
| 1788 | case V4L2_PIX_FMT_RGB32: |
| 1789 | *f++ = bv; |
| 1790 | *f++ = gv; |
| 1791 | *f++ = rv; |
| 1792 | f++; |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
| 1796 | clipmask_index++; |
| 1797 | } |
| 1798 | /* Deal with non-integer no. of bytes for YUV420P */ |
| 1799 | if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 ) |
| 1800 | *pcopylen += frame->v4l2_linesize; |
| 1801 | else |
| 1802 | *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1; |
| 1803 | |
| 1804 | frame->curline += 1; |
| 1805 | |
| 1806 | if (frame->curline >= frame->frmheight) { |
| 1807 | return ParseState_NextFrame; |
| 1808 | } |
| 1809 | else { |
| 1810 | return ParseState_Continue; |
| 1811 | } |
| 1812 | |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | /* |
| 1817 | * usbvision_parse_lines_420() |
| 1818 | * |
| 1819 | * Parse two lines from the scratch buffer, put |
| 1820 | * decoded RGB value into the current frame buffer and add the written |
| 1821 | * number of bytes (RGB) to the *pcopylen. |
| 1822 | * |
| 1823 | */ |
| 1824 | static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision, |
| 1825 | long *pcopylen) |
| 1826 | { |
| 1827 | struct usbvision_frame *frame; |
| 1828 | unsigned char *f_even = NULL, *f_odd = NULL; |
| 1829 | unsigned int pixel_per_line, block; |
| 1830 | int pixel, block_split; |
| 1831 | int y_ptr, u_ptr, v_ptr, y_odd_offset; |
| 1832 | const int y_block_size = 128; |
| 1833 | const int uv_block_size = 64; |
| 1834 | const int sub_block_size = 32; |
| 1835 | const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4; |
| 1836 | const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4; |
| 1837 | unsigned char y[2], u, v; /* YUV components */ |
| 1838 | int y_, u_, v_, vb, uvg, ur; |
| 1839 | int r_, g_, b_; /* RGB components */ |
| 1840 | unsigned char g; |
| 1841 | int clipmask_even_index, clipmask_odd_index, bytes_per_pixel; |
| 1842 | int clipmask_add, stretch_bytes; |
| 1843 | int overlay = usbvision->overlay; |
| 1844 | |
| 1845 | if (overlay) { |
| 1846 | frame = &usbvision->overlay_frame; |
| 1847 | if (usbvision->overlay_base == NULL) { |
| 1848 | //video_buffer is not set yet |
| 1849 | return ParseState_NextFrame; |
| 1850 | } |
| 1851 | f_even = usbvision->overlay_win + frame->curline * |
| 1852 | usbvision->vid_buf.fmt.bytesperline; |
| 1853 | f_odd = f_even + usbvision->vid_buf.fmt.bytesperline * usbvision->stretch_height; |
| 1854 | } |
| 1855 | else { |
| 1856 | frame = usbvision->curFrame; |
| 1857 | f_even = frame->data + (frame->v4l2_linesize * frame->curline); |
| 1858 | f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height; |
| 1859 | } |
| 1860 | |
| 1861 | /* Make sure there's enough data for the entire line */ |
| 1862 | /* In this mode usbvision transfer 3 bytes for every 2 pixels */ |
| 1863 | /* I need two lines to decode the color */ |
| 1864 | bytes_per_pixel = frame->v4l2_format.bytes_per_pixel; |
| 1865 | stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel; |
| 1866 | clipmask_even_index = frame->curline * MAX_FRAME_WIDTH; |
| 1867 | clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH; |
| 1868 | clipmask_add = usbvision->stretch_width; |
| 1869 | pixel_per_line = frame->isocHeader.frameWidth; |
| 1870 | |
| 1871 | if (scratch_len(usbvision) < (int)pixel_per_line * 3) { |
| 1872 | //printk(KERN_DEBUG "out of data, need %d\n", len); |
| 1873 | return ParseState_Out; |
| 1874 | } |
| 1875 | |
| 1876 | if ((frame->curline + 1) >= frame->frmheight) { |
| 1877 | return ParseState_NextFrame; |
| 1878 | } |
| 1879 | |
| 1880 | block_split = (pixel_per_line%y_block_size) ? 1 : 0; //are some blocks splitted into different lines? |
| 1881 | |
| 1882 | y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size) |
| 1883 | + block_split * uv_block_size; |
| 1884 | |
| 1885 | scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset); |
| 1886 | scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size); |
| 1887 | scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset |
| 1888 | + (4 - block_split) * sub_block_size); |
| 1889 | |
| 1890 | for (block = 0; block < (pixel_per_line / sub_block_size); |
| 1891 | block++) { |
| 1892 | |
| 1893 | |
| 1894 | for (pixel = 0; pixel < sub_block_size; pixel +=2) { |
| 1895 | scratch_get(usbvision, &y[0], 2); |
| 1896 | scratch_get_extra(usbvision, &u, &u_ptr, 1); |
| 1897 | scratch_get_extra(usbvision, &v, &v_ptr, 1); |
| 1898 | |
| 1899 | //I don't use the YUV_TO_RGB macro for better performance |
| 1900 | v_ = v - 128; |
| 1901 | u_ = u - 128; |
| 1902 | vb = 132252 * v_; |
| 1903 | uvg= -53281 * u_ - 25625 * v_; |
| 1904 | ur = 104595 * u_; |
| 1905 | |
| 1906 | if((overlay) && (clipped_pixel(clipmask_even_index))) { |
| 1907 | f_even += bytes_per_pixel; |
| 1908 | } |
| 1909 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1910 | *f_even++ = y[0]; |
| 1911 | *f_even++ = v; |
| 1912 | } |
| 1913 | else { |
| 1914 | y_ = 76284 * (y[0] - 16); |
| 1915 | |
| 1916 | b_ = (y_ + vb) >> 16; |
| 1917 | g_ = (y_ + uvg)>> 16; |
| 1918 | r_ = (y_ + ur) >> 16; |
| 1919 | |
| 1920 | switch (frame->v4l2_format.format) { |
| 1921 | case V4L2_PIX_FMT_RGB565: |
| 1922 | g = LIMIT_RGB(g_); |
| 1923 | *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); |
| 1924 | *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); |
| 1925 | break; |
| 1926 | case V4L2_PIX_FMT_RGB24: |
| 1927 | *f_even++ = LIMIT_RGB(b_); |
| 1928 | *f_even++ = LIMIT_RGB(g_); |
| 1929 | *f_even++ = LIMIT_RGB(r_); |
| 1930 | break; |
| 1931 | case V4L2_PIX_FMT_RGB32: |
| 1932 | *f_even++ = LIMIT_RGB(b_); |
| 1933 | *f_even++ = LIMIT_RGB(g_); |
| 1934 | *f_even++ = LIMIT_RGB(r_); |
| 1935 | f_even++; |
| 1936 | break; |
| 1937 | case V4L2_PIX_FMT_RGB555: |
| 1938 | g = LIMIT_RGB(g_); |
| 1939 | *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); |
| 1940 | *f_even++ = (0x03 & ( g >> 6)) | |
| 1941 | (0x7C & (LIMIT_RGB(r_) >> 1)); |
| 1942 | break; |
| 1943 | } |
| 1944 | } |
| 1945 | clipmask_even_index += clipmask_add; |
| 1946 | f_even += stretch_bytes; |
| 1947 | |
| 1948 | if((overlay) && (clipped_pixel(clipmask_even_index))) { |
| 1949 | f_even += bytes_per_pixel; |
| 1950 | } |
| 1951 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1952 | *f_even++ = y[1]; |
| 1953 | *f_even++ = u; |
| 1954 | } |
| 1955 | else { |
| 1956 | y_ = 76284 * (y[1] - 16); |
| 1957 | |
| 1958 | b_ = (y_ + vb) >> 16; |
| 1959 | g_ = (y_ + uvg)>> 16; |
| 1960 | r_ = (y_ + ur) >> 16; |
| 1961 | |
| 1962 | switch (frame->v4l2_format.format) { |
| 1963 | case V4L2_PIX_FMT_RGB565: |
| 1964 | g = LIMIT_RGB(g_); |
| 1965 | *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); |
| 1966 | *f_even++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); |
| 1967 | break; |
| 1968 | case V4L2_PIX_FMT_RGB24: |
| 1969 | *f_even++ = LIMIT_RGB(b_); |
| 1970 | *f_even++ = LIMIT_RGB(g_); |
| 1971 | *f_even++ = LIMIT_RGB(r_); |
| 1972 | break; |
| 1973 | case V4L2_PIX_FMT_RGB32: |
| 1974 | *f_even++ = LIMIT_RGB(b_); |
| 1975 | *f_even++ = LIMIT_RGB(g_); |
| 1976 | *f_even++ = LIMIT_RGB(r_); |
| 1977 | f_even++; |
| 1978 | break; |
| 1979 | case V4L2_PIX_FMT_RGB555: |
| 1980 | g = LIMIT_RGB(g_); |
| 1981 | *f_even++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); |
| 1982 | *f_even++ = (0x03 & ( g >> 6)) | |
| 1983 | (0x7C & (LIMIT_RGB(r_) >> 1)); |
| 1984 | break; |
| 1985 | } |
| 1986 | } |
| 1987 | clipmask_even_index += clipmask_add; |
| 1988 | f_even += stretch_bytes; |
| 1989 | |
| 1990 | scratch_get_extra(usbvision, &y[0], &y_ptr, 2); |
| 1991 | |
| 1992 | if ((overlay) && (clipped_pixel(clipmask_odd_index))) { |
| 1993 | f_odd += bytes_per_pixel; |
| 1994 | } |
| 1995 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 1996 | *f_odd++ = y[0]; |
| 1997 | *f_odd++ = v; |
| 1998 | } |
| 1999 | else { |
| 2000 | y_ = 76284 * (y[0] - 16); |
| 2001 | |
| 2002 | b_ = (y_ + vb) >> 16; |
| 2003 | g_ = (y_ + uvg)>> 16; |
| 2004 | r_ = (y_ + ur) >> 16; |
| 2005 | |
| 2006 | switch (frame->v4l2_format.format) { |
| 2007 | case V4L2_PIX_FMT_RGB565: |
| 2008 | g = LIMIT_RGB(g_); |
| 2009 | *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); |
| 2010 | *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); |
| 2011 | break; |
| 2012 | case V4L2_PIX_FMT_RGB24: |
| 2013 | *f_odd++ = LIMIT_RGB(b_); |
| 2014 | *f_odd++ = LIMIT_RGB(g_); |
| 2015 | *f_odd++ = LIMIT_RGB(r_); |
| 2016 | break; |
| 2017 | case V4L2_PIX_FMT_RGB32: |
| 2018 | *f_odd++ = LIMIT_RGB(b_); |
| 2019 | *f_odd++ = LIMIT_RGB(g_); |
| 2020 | *f_odd++ = LIMIT_RGB(r_); |
| 2021 | f_odd++; |
| 2022 | break; |
| 2023 | case V4L2_PIX_FMT_RGB555: |
| 2024 | g = LIMIT_RGB(g_); |
| 2025 | *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); |
| 2026 | *f_odd++ = (0x03 & ( g >> 6)) | |
| 2027 | (0x7C & (LIMIT_RGB(r_) >> 1)); |
| 2028 | break; |
| 2029 | } |
| 2030 | } |
| 2031 | clipmask_odd_index += clipmask_add; |
| 2032 | f_odd += stretch_bytes; |
| 2033 | |
| 2034 | if((overlay) && (clipped_pixel(clipmask_odd_index))) { |
| 2035 | f_odd += bytes_per_pixel; |
| 2036 | } |
| 2037 | else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) { |
| 2038 | *f_odd++ = y[1]; |
| 2039 | *f_odd++ = u; |
| 2040 | } |
| 2041 | else { |
| 2042 | y_ = 76284 * (y[1] - 16); |
| 2043 | |
| 2044 | b_ = (y_ + vb) >> 16; |
| 2045 | g_ = (y_ + uvg)>> 16; |
| 2046 | r_ = (y_ + ur) >> 16; |
| 2047 | |
| 2048 | switch (frame->v4l2_format.format) { |
| 2049 | case V4L2_PIX_FMT_RGB565: |
| 2050 | g = LIMIT_RGB(g_); |
| 2051 | *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 3)); |
| 2052 | *f_odd++ = (0x07 & ( g >> 5)) | (0xF8 & LIMIT_RGB(r_)); |
| 2053 | break; |
| 2054 | case V4L2_PIX_FMT_RGB24: |
| 2055 | *f_odd++ = LIMIT_RGB(b_); |
| 2056 | *f_odd++ = LIMIT_RGB(g_); |
| 2057 | *f_odd++ = LIMIT_RGB(r_); |
| 2058 | break; |
| 2059 | case V4L2_PIX_FMT_RGB32: |
| 2060 | *f_odd++ = LIMIT_RGB(b_); |
| 2061 | *f_odd++ = LIMIT_RGB(g_); |
| 2062 | *f_odd++ = LIMIT_RGB(r_); |
| 2063 | f_odd++; |
| 2064 | break; |
| 2065 | case V4L2_PIX_FMT_RGB555: |
| 2066 | g = LIMIT_RGB(g_); |
| 2067 | *f_odd++ = (0x1F & (LIMIT_RGB(b_) >> 3)) | (0xE0 & (g << 2)); |
| 2068 | *f_odd++ = (0x03 & ( g >> 6)) | |
| 2069 | (0x7C & (LIMIT_RGB(r_) >> 1)); |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | clipmask_odd_index += clipmask_add; |
| 2074 | f_odd += stretch_bytes; |
| 2075 | } |
| 2076 | |
| 2077 | scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size); |
| 2078 | scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size] |
| 2079 | * sub_block_size); |
| 2080 | scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size] |
| 2081 | * sub_block_size); |
| 2082 | scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size] |
| 2083 | * sub_block_size); |
| 2084 | } |
| 2085 | |
| 2086 | scratch_rm_old(usbvision, pixel_per_line * 3 / 2 |
| 2087 | + block_split * sub_block_size); |
| 2088 | |
| 2089 | frame->curline += 2 * usbvision->stretch_height; |
| 2090 | *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height; |
| 2091 | |
| 2092 | if (frame->curline >= frame->frmheight) |
| 2093 | return ParseState_NextFrame; |
| 2094 | else |
| 2095 | return ParseState_Continue; |
| 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * usbvision_parse_data() |
| 2100 | * |
| 2101 | * Generic routine to parse the scratch buffer. It employs either |
| 2102 | * usbvision_find_header() or usbvision_parse_lines() to do most |
| 2103 | * of work. |
| 2104 | * |
| 2105 | */ |
| 2106 | static void usbvision_parse_data(struct usb_usbvision *usbvision) |
| 2107 | { |
| 2108 | struct usbvision_frame *frame; |
| 2109 | enum ParseState newstate; |
| 2110 | long copylen = 0; |
| 2111 | unsigned long lock_flags; |
| 2112 | |
| 2113 | if (usbvision->overlay) { |
| 2114 | frame = &usbvision->overlay_frame; |
| 2115 | } |
| 2116 | else { |
| 2117 | frame = usbvision->curFrame; |
| 2118 | } |
| 2119 | |
| 2120 | PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision)); |
| 2121 | |
| 2122 | while (1) { |
| 2123 | |
| 2124 | newstate = ParseState_Out; |
| 2125 | if (scratch_len(usbvision)) { |
| 2126 | if (frame->scanstate == ScanState_Scanning) { |
| 2127 | newstate = usbvision_find_header(usbvision); |
| 2128 | } |
| 2129 | else if (frame->scanstate == ScanState_Lines) { |
| 2130 | if (usbvision->isocMode == ISOC_MODE_YUV420) { |
| 2131 | newstate = usbvision_parse_lines_420(usbvision, ©len); |
| 2132 | } |
| 2133 | else if (usbvision->isocMode == ISOC_MODE_YUV422) { |
| 2134 | newstate = usbvision_parse_lines_422(usbvision, ©len); |
| 2135 | } |
| 2136 | else if (usbvision->isocMode == ISOC_MODE_COMPRESS) { |
| 2137 | newstate = usbvision_parse_compress(usbvision, ©len); |
| 2138 | } |
| 2139 | |
| 2140 | } |
| 2141 | } |
| 2142 | if (newstate == ParseState_Continue) { |
| 2143 | continue; |
| 2144 | } |
| 2145 | else if ((newstate == ParseState_NextFrame) || (newstate == ParseState_Out)) { |
| 2146 | break; |
| 2147 | } |
| 2148 | else { |
| 2149 | return; /* ParseState_EndParse */ |
| 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | if (newstate == ParseState_NextFrame) { |
| 2154 | frame->grabstate = FrameState_Done; |
| 2155 | do_gettimeofday(&(frame->timestamp)); |
| 2156 | frame->sequence = usbvision->frame_num; |
| 2157 | if (usbvision->overlay) { |
| 2158 | frame->grabstate = FrameState_Grabbing; |
| 2159 | frame->scanstate = ScanState_Scanning; |
| 2160 | frame->scanlength = 0; |
| 2161 | copylen = 0; |
| 2162 | } |
| 2163 | else { |
| 2164 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
| 2165 | list_move_tail(&(frame->frame), &usbvision->outqueue); |
| 2166 | usbvision->curFrame = NULL; |
| 2167 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
| 2168 | } |
| 2169 | usbvision->frame_num++; |
| 2170 | |
| 2171 | /* Optionally display statistics on the screen */ |
| 2172 | if (flags & FLAGS_OSD_STATS) |
| 2173 | usbvision_osd_stats(usbvision, frame); |
| 2174 | |
| 2175 | /* This will cause the process to request another frame. */ |
| 2176 | if (waitqueue_active(&usbvision->wait_frame)) { |
| 2177 | PDEBUG(DBG_PARSE, "Wake up !"); |
| 2178 | wake_up_interruptible(&usbvision->wait_frame); |
| 2179 | } |
| 2180 | } |
| 2181 | else |
| 2182 | frame->grabstate = FrameState_Grabbing; |
| 2183 | |
| 2184 | |
| 2185 | /* Update the frame's uncompressed length. */ |
| 2186 | frame->scanlength += copylen; |
| 2187 | } |
| 2188 | |
| 2189 | |
| 2190 | /* |
| 2191 | * Make all of the blocks of data contiguous |
| 2192 | */ |
| 2193 | static int usbvision_compress_isochronous(struct usb_usbvision *usbvision, |
| 2194 | struct urb *urb) |
| 2195 | { |
| 2196 | unsigned char *packet_data; |
| 2197 | int i, totlen = 0; |
| 2198 | |
| 2199 | for (i = 0; i < urb->number_of_packets; i++) { |
| 2200 | int packet_len = urb->iso_frame_desc[i].actual_length; |
| 2201 | int packet_stat = urb->iso_frame_desc[i].status; |
| 2202 | |
| 2203 | packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
| 2204 | |
| 2205 | /* Detect and ignore errored packets */ |
| 2206 | if (packet_stat) { // packet_stat != 0 ????????????? |
| 2207 | PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat); |
| 2208 | usbvision->isocErrCount++; |
| 2209 | continue; |
| 2210 | } |
| 2211 | |
| 2212 | /* Detect and ignore empty packets */ |
| 2213 | if (packet_len < 0) { |
| 2214 | PDEBUG(DBG_ISOC, "error packet [%d]", i); |
| 2215 | usbvision->isocSkipCount++; |
| 2216 | continue; |
| 2217 | } |
| 2218 | else if (packet_len == 0) { /* Frame end ????? */ |
| 2219 | PDEBUG(DBG_ISOC, "null packet [%d]", i); |
| 2220 | usbvision->isocstate=IsocState_NoFrame; |
| 2221 | usbvision->isocSkipCount++; |
| 2222 | continue; |
| 2223 | } |
| 2224 | else if (packet_len > usbvision->isocPacketSize) { |
| 2225 | PDEBUG(DBG_ISOC, "packet[%d] > isocPacketSize", i); |
| 2226 | usbvision->isocSkipCount++; |
| 2227 | continue; |
| 2228 | } |
| 2229 | |
| 2230 | PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len); |
| 2231 | |
| 2232 | if (usbvision->isocstate==IsocState_NoFrame) { //new frame begins |
| 2233 | usbvision->isocstate=IsocState_InFrame; |
| 2234 | scratch_mark_header(usbvision); |
| 2235 | usbvision_measure_bandwidth(usbvision); |
| 2236 | PDEBUG(DBG_ISOC, "packet with header"); |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * If usbvision continues to feed us with data but there is no |
| 2241 | * consumption (if, for example, V4L client fell asleep) we |
| 2242 | * may overflow the buffer. We have to move old data over to |
| 2243 | * free room for new data. This is bad for old data. If we |
| 2244 | * just drop new data then it's bad for new data... choose |
| 2245 | * your favorite evil here. |
| 2246 | */ |
| 2247 | if (scratch_free(usbvision) < packet_len) { |
| 2248 | |
| 2249 | usbvision->scratch_ovf_count++; |
| 2250 | PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d", |
| 2251 | scratch_len(usbvision), packet_len); |
| 2252 | scratch_rm_old(usbvision, packet_len - scratch_free(usbvision)); |
| 2253 | } |
| 2254 | |
| 2255 | /* Now we know that there is enough room in scratch buffer */ |
| 2256 | scratch_put(usbvision, packet_data, packet_len); |
| 2257 | totlen += packet_len; |
| 2258 | usbvision->isocDataCount += packet_len; |
| 2259 | usbvision->isocPacketCount++; |
| 2260 | } |
| 2261 | #if ENABLE_HEXDUMP |
| 2262 | if (totlen > 0) { |
| 2263 | static int foo = 0; |
| 2264 | if (foo < 1) { |
| 2265 | printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen); |
| 2266 | usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen); |
| 2267 | ++foo; |
| 2268 | } |
| 2269 | } |
| 2270 | #endif |
| 2271 | return totlen; |
| 2272 | } |
| 2273 | |
| 2274 | static void usbvision_isocIrq(struct urb *urb, struct pt_regs *regs) |
| 2275 | { |
| 2276 | int errCode = 0; |
| 2277 | int len; |
| 2278 | struct usb_usbvision *usbvision = urb->context; |
| 2279 | int i; |
| 2280 | unsigned long startTime = jiffies; |
| 2281 | struct usbvision_frame **f; |
| 2282 | |
| 2283 | /* We don't want to do anything if we are about to be removed! */ |
| 2284 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 2285 | return; |
| 2286 | |
| 2287 | f = &usbvision->curFrame; |
| 2288 | |
| 2289 | /* Manage streaming interruption */ |
| 2290 | if (usbvision->streaming == Stream_Interrupt) { |
| 2291 | usbvision->streaming = Stream_Idle; |
| 2292 | if ((*f)) { |
| 2293 | (*f)->grabstate = FrameState_Ready; |
| 2294 | (*f)->scanstate = ScanState_Scanning; |
| 2295 | } |
| 2296 | PDEBUG(DBG_IRQ, "stream interrupted"); |
| 2297 | wake_up_interruptible(&usbvision->wait_stream); |
| 2298 | } |
| 2299 | |
| 2300 | /* Copy the data received into our scratch buffer */ |
| 2301 | len = usbvision_compress_isochronous(usbvision, urb); |
| 2302 | |
| 2303 | usbvision->isocUrbCount++; |
| 2304 | usbvision->urb_length = len; |
| 2305 | |
| 2306 | if (usbvision->streaming == Stream_On) { |
| 2307 | |
| 2308 | /* If we collected enough data let's parse! */ |
| 2309 | if (scratch_len(usbvision) > USBVISION_HEADER_LENGTH) { /* 12 == header_length */ |
| 2310 | /*If we don't have a frame we're current working on, complain */ |
| 2311 | if((!list_empty(&(usbvision->inqueue))) || (usbvision->overlay)) { |
| 2312 | if (!(*f)) { |
| 2313 | (*f) = list_entry(usbvision->inqueue.next,struct usbvision_frame, frame); |
| 2314 | } |
| 2315 | usbvision_parse_data(usbvision); |
| 2316 | } |
| 2317 | else { |
| 2318 | PDEBUG(DBG_IRQ, "received data, but no one needs it"); |
| 2319 | scratch_reset(usbvision); |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | usbvision->timeInIrq += jiffies - startTime; |
| 2325 | |
| 2326 | for (i = 0; i < USBVISION_URB_FRAMES; i++) { |
| 2327 | urb->iso_frame_desc[i].status = 0; |
| 2328 | urb->iso_frame_desc[i].actual_length = 0; |
| 2329 | } |
| 2330 | |
| 2331 | urb->status = 0; |
| 2332 | urb->dev = usbvision->dev; |
| 2333 | errCode = usb_submit_urb (urb, GFP_ATOMIC); |
| 2334 | |
| 2335 | /* Disable this warning. By design of the driver. */ |
| 2336 | // if(errCode) { |
| 2337 | // err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode); |
| 2338 | // } |
| 2339 | |
| 2340 | return; |
| 2341 | } |
| 2342 | |
| 2343 | /*************************************/ |
| 2344 | /* Low level usbvision access functions */ |
| 2345 | /*************************************/ |
| 2346 | |
| 2347 | /* |
| 2348 | * usbvision_read_reg() |
| 2349 | * |
| 2350 | * return < 0 -> Error |
| 2351 | * >= 0 -> Data |
| 2352 | */ |
| 2353 | |
| 2354 | static int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg) |
| 2355 | { |
| 2356 | int errCode = 0; |
| 2357 | unsigned char buffer[1]; |
| 2358 | |
| 2359 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 2360 | return -1; |
| 2361 | |
| 2362 | errCode = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1), |
| 2363 | USBVISION_OP_CODE, |
| 2364 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, |
| 2365 | 0, (__u16) reg, buffer, 1, HZ); |
| 2366 | |
| 2367 | if (errCode < 0) { |
| 2368 | err("%s: failed: error %d", __FUNCTION__, errCode); |
| 2369 | return errCode; |
| 2370 | } |
| 2371 | return buffer[0]; |
| 2372 | } |
| 2373 | |
| 2374 | /* |
| 2375 | * usbvision_write_reg() |
| 2376 | * |
| 2377 | * return 1 -> Reg written |
| 2378 | * 0 -> usbvision is not yet ready |
| 2379 | * -1 -> Something went wrong |
| 2380 | */ |
| 2381 | |
| 2382 | static int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg, |
| 2383 | unsigned char value) |
| 2384 | { |
| 2385 | int errCode = 0; |
| 2386 | |
| 2387 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 2388 | return 0; |
| 2389 | |
| 2390 | errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 2391 | USBVISION_OP_CODE, |
| 2392 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 2393 | USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ); |
| 2394 | |
| 2395 | if (errCode < 0) { |
| 2396 | err("%s: failed: error %d", __FUNCTION__, errCode); |
| 2397 | } |
| 2398 | return errCode; |
| 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | static void usbvision_ctrlUrb_complete(struct urb *urb, struct pt_regs *regs) |
| 2403 | { |
| 2404 | struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context; |
| 2405 | |
| 2406 | PDEBUG(DBG_IRQ, ""); |
| 2407 | usbvision->ctrlUrbBusy = 0; |
| 2408 | if (waitqueue_active(&usbvision->ctrlUrb_wq)) { |
| 2409 | wake_up_interruptible(&usbvision->ctrlUrb_wq); |
| 2410 | } |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address, |
| 2415 | unsigned char *data, int len) |
| 2416 | { |
| 2417 | int errCode = 0; |
| 2418 | |
| 2419 | PDEBUG(DBG_IRQ, ""); |
| 2420 | if (len > 8) { |
| 2421 | return -EFAULT; |
| 2422 | } |
| 2423 | // down(&usbvision->ctrlUrbLock); |
| 2424 | if (usbvision->ctrlUrbBusy) { |
| 2425 | // up(&usbvision->ctrlUrbLock); |
| 2426 | return -EBUSY; |
| 2427 | } |
| 2428 | usbvision->ctrlUrbBusy = 1; |
| 2429 | // up(&usbvision->ctrlUrbLock); |
| 2430 | |
| 2431 | usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT; |
| 2432 | usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE; |
| 2433 | usbvision->ctrlUrbSetup.wValue = 0; |
| 2434 | usbvision->ctrlUrbSetup.wIndex = cpu_to_le16(address); |
| 2435 | usbvision->ctrlUrbSetup.wLength = cpu_to_le16(len); |
| 2436 | usb_fill_control_urb (usbvision->ctrlUrb, usbvision->dev, |
| 2437 | usb_sndctrlpipe(usbvision->dev, 1), |
| 2438 | (unsigned char *)&usbvision->ctrlUrbSetup, |
| 2439 | (void *)usbvision->ctrlUrbBuffer, len, |
| 2440 | usbvision_ctrlUrb_complete, |
| 2441 | (void *)usbvision); |
| 2442 | |
| 2443 | memcpy(usbvision->ctrlUrbBuffer, data, len); |
| 2444 | |
| 2445 | errCode = usb_submit_urb(usbvision->ctrlUrb, GFP_ATOMIC); |
| 2446 | if (errCode < 0) { |
| 2447 | // error in usb_submit_urb() |
| 2448 | usbvision->ctrlUrbBusy = 0; |
| 2449 | } |
| 2450 | PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, errCode); |
| 2451 | return errCode; |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | |
| 2456 | |
| 2457 | static int usbvision_init_compression(struct usb_usbvision *usbvision) |
| 2458 | { |
| 2459 | int errCode = 0; |
| 2460 | |
| 2461 | usbvision->lastIsocFrameNum = -1; |
| 2462 | usbvision->isocDataCount = 0; |
| 2463 | usbvision->isocPacketCount = 0; |
| 2464 | usbvision->isocSkipCount = 0; |
| 2465 | usbvision->comprLevel = 50; |
| 2466 | usbvision->lastComprLevel = -1; |
| 2467 | usbvision->isocUrbCount = 0; |
| 2468 | usbvision->requestIntra = 1; |
| 2469 | usbvision->isocMeasureBandwidthCount = 0; |
| 2470 | |
| 2471 | return errCode; |
| 2472 | } |
| 2473 | |
| 2474 | /* this function measures the used bandwidth since last call |
| 2475 | * return: 0 : no error |
| 2476 | * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize |
| 2477 | */ |
| 2478 | static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision) |
| 2479 | { |
| 2480 | int errCode = 0; |
| 2481 | |
| 2482 | if (usbvision->isocMeasureBandwidthCount < 2) { // this gives an average bandwidth of 3 frames |
| 2483 | usbvision->isocMeasureBandwidthCount++; |
| 2484 | return errCode; |
| 2485 | } |
| 2486 | if ((usbvision->isocPacketSize > 0) && (usbvision->isocPacketCount > 0)) { |
| 2487 | usbvision->usedBandwidth = usbvision->isocDataCount / |
| 2488 | (usbvision->isocPacketCount + usbvision->isocSkipCount) * |
| 2489 | 100 / usbvision->isocPacketSize; |
| 2490 | } |
| 2491 | usbvision->isocMeasureBandwidthCount = 0; |
| 2492 | usbvision->isocDataCount = 0; |
| 2493 | usbvision->isocPacketCount = 0; |
| 2494 | usbvision->isocSkipCount = 0; |
| 2495 | return errCode; |
| 2496 | } |
| 2497 | |
| 2498 | static int usbvision_adjust_compression (struct usb_usbvision *usbvision) |
| 2499 | { |
| 2500 | int errCode = 0; |
| 2501 | unsigned char buffer[6]; |
| 2502 | |
| 2503 | PDEBUG(DBG_IRQ, ""); |
| 2504 | if ((adjustCompression) && (usbvision->usedBandwidth > 0)) { |
| 2505 | usbvision->comprLevel += (usbvision->usedBandwidth - 90) / 2; |
| 2506 | RESTRICT_TO_RANGE(usbvision->comprLevel, 0, 100); |
| 2507 | if (usbvision->comprLevel != usbvision->lastComprLevel) { |
| 2508 | int distorsion; |
| 2509 | if (usbvision->bridgeType == BRIDGE_NT1004 || usbvision->bridgeType == BRIDGE_NT1005) { |
| 2510 | buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM Threshold 1 |
| 2511 | buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM Threshold 2 |
| 2512 | distorsion = 7 + 248 * usbvision->comprLevel / 100; |
| 2513 | buffer[2] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (inter) |
| 2514 | buffer[3] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (intra) |
| 2515 | distorsion = 1 + 42 * usbvision->comprLevel / 100; |
| 2516 | buffer[4] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (inter) |
| 2517 | buffer[5] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (intra) |
| 2518 | } |
| 2519 | else { //BRIDGE_NT1003 |
| 2520 | buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM threshold 1 |
| 2521 | buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM threshold 2 |
| 2522 | distorsion = 2 + 253 * usbvision->comprLevel / 100; |
| 2523 | buffer[2] = (unsigned char)(distorsion & 0xFF); // distorsion threshold bit0-7 |
| 2524 | buffer[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11 |
| 2525 | distorsion = 0 + 43 * usbvision->comprLevel / 100; |
| 2526 | buffer[4] = (unsigned char)(distorsion & 0xFF); // maximum distorsion bit0-7 |
| 2527 | buffer[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8 |
| 2528 | } |
| 2529 | errCode = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6); |
| 2530 | if (errCode == 0){ |
| 2531 | PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0], |
| 2532 | buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]); |
| 2533 | usbvision->lastComprLevel = usbvision->comprLevel; |
| 2534 | } |
| 2535 | } |
| 2536 | } |
| 2537 | return errCode; |
| 2538 | } |
| 2539 | |
| 2540 | static int usbvision_request_intra (struct usb_usbvision *usbvision) |
| 2541 | { |
| 2542 | int errCode = 0; |
| 2543 | unsigned char buffer[1]; |
| 2544 | |
| 2545 | PDEBUG(DBG_IRQ, ""); |
| 2546 | usbvision->requestIntra = 1; |
| 2547 | buffer[0] = 1; |
| 2548 | usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1); |
| 2549 | return errCode; |
| 2550 | } |
| 2551 | |
| 2552 | static int usbvision_unrequest_intra (struct usb_usbvision *usbvision) |
| 2553 | { |
| 2554 | int errCode = 0; |
| 2555 | unsigned char buffer[1]; |
| 2556 | |
| 2557 | PDEBUG(DBG_IRQ, ""); |
| 2558 | usbvision->requestIntra = 0; |
| 2559 | buffer[0] = 0; |
| 2560 | usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1); |
| 2561 | return errCode; |
| 2562 | } |
| 2563 | |
| 2564 | /* ----------------------------------------------------------------------- */ |
| 2565 | /* I2C functions */ |
| 2566 | /* ----------------------------------------------------------------------- */ |
| 2567 | |
| 2568 | static void call_i2c_clients(struct usb_usbvision *usbvision, unsigned int cmd, |
| 2569 | void *arg) |
| 2570 | { |
| 2571 | |
| 2572 | int i; |
| 2573 | |
| 2574 | for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) { |
| 2575 | if (NULL == usbvision->i2c_clients[i]) |
| 2576 | continue; |
| 2577 | if (NULL == usbvision->i2c_clients[i]->driver->command) |
| 2578 | continue; |
| 2579 | usbvision->i2c_clients[i]->driver->command(usbvision->i2c_clients[i], cmd, arg); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | static int attach_inform(struct i2c_client *client) |
| 2584 | { |
| 2585 | struct usb_usbvision *usbvision; |
| 2586 | struct tuner_setup tun_addr; |
| 2587 | int i; |
| 2588 | |
| 2589 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 2590 | usbvision = (struct usb_usbvision *)client->adapter->data; |
| 2591 | #else |
| 2592 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); |
| 2593 | #endif |
| 2594 | |
| 2595 | for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) { |
| 2596 | if (usbvision->i2c_clients[i] == NULL || |
| 2597 | usbvision->i2c_clients[i]->driver->id == |
| 2598 | client->driver->id) { |
| 2599 | usbvision->i2c_clients[i] = client; |
| 2600 | break; |
| 2601 | } |
| 2602 | } |
| 2603 | if ((usbvision->have_tuner) && (usbvision->tuner_type != -1)) { |
| 2604 | tun_addr.mode_mask = T_ANALOG_TV; |
| 2605 | tun_addr.type = usbvision->tuner_type; |
| 2606 | tun_addr.addr = ADDR_UNSET; |
| 2607 | client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_addr); |
| 2608 | call_i2c_clients(usbvision, VIDIOC_INT_RESET, NULL); |
| 2609 | call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->ctl_input); |
| 2610 | call_i2c_clients(usbvision, VIDIOC_STREAMON, NULL); |
| 2611 | } |
| 2612 | call_i2c_clients(usbvision, VIDIOC_S_STD, &usbvision->tvnorm->id); |
| 2613 | |
| 2614 | PDEBUG(DBG_I2C, "usbvision[%d] attaches %s", usbvision->nr, client->name); |
| 2615 | |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | static int detach_inform(struct i2c_client *client) |
| 2620 | { |
| 2621 | struct usb_usbvision *usbvision; |
| 2622 | int i; |
| 2623 | |
| 2624 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 2625 | usbvision = (struct usb_usbvision *)client->adapter->data; |
| 2626 | #else |
| 2627 | usbvision = (struct usb_usbvision *)i2c_get_adapdata(client->adapter); |
| 2628 | #endif |
| 2629 | |
| 2630 | PDEBUG(DBG_I2C, "usbvision[%d] detaches %s", usbvision->nr, client->name); |
| 2631 | for (i = 0; i < USBVISION_I2C_CLIENTS_MAX; i++) { |
| 2632 | if (NULL != usbvision->i2c_clients[i] && |
| 2633 | usbvision->i2c_clients[i]->driver->id == |
| 2634 | client->driver->id) { |
| 2635 | usbvision->i2c_clients[i] = NULL; |
| 2636 | break; |
| 2637 | } |
| 2638 | } |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
| 2642 | static int |
| 2643 | usbvision_i2c_read_max4(struct usb_usbvision *usbvision, unsigned char addr, |
| 2644 | char *buf, short len) |
| 2645 | { |
| 2646 | int rc, retries; |
| 2647 | |
| 2648 | for (retries = 5;;) { |
| 2649 | rc = usbvision_write_reg(usbvision, USBVISION_SER_ADRS, addr); |
| 2650 | if (rc < 0) |
| 2651 | return rc; |
| 2652 | |
| 2653 | /* Initiate byte read cycle */ |
| 2654 | /* USBVISION_SER_CONT <- d0-d2 n. of bytes to r/w */ |
| 2655 | /* d3 0=Wr 1=Rd */ |
| 2656 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, |
| 2657 | (len & 0x07) | 0x18); |
| 2658 | if (rc < 0) |
| 2659 | return rc; |
| 2660 | |
| 2661 | /* Test for Busy and ACK */ |
| 2662 | do { |
| 2663 | /* USBVISION_SER_CONT -> d4 == 0 busy */ |
| 2664 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); |
| 2665 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ |
| 2666 | if (rc < 0) |
| 2667 | return rc; |
| 2668 | |
| 2669 | /* USBVISION_SER_CONT -> d5 == 1 Not ack */ |
| 2670 | if ((rc & 0x20) == 0) /* Ack? */ |
| 2671 | break; |
| 2672 | |
| 2673 | /* I2C abort */ |
| 2674 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 2675 | if (rc < 0) |
| 2676 | return rc; |
| 2677 | |
| 2678 | if (--retries < 0) |
| 2679 | return -1; |
| 2680 | } |
| 2681 | |
| 2682 | switch (len) { |
| 2683 | case 4: |
| 2684 | buf[3] = usbvision_read_reg(usbvision, USBVISION_SER_DAT4); |
| 2685 | case 3: |
| 2686 | buf[2] = usbvision_read_reg(usbvision, USBVISION_SER_DAT3); |
| 2687 | case 2: |
| 2688 | buf[1] = usbvision_read_reg(usbvision, USBVISION_SER_DAT2); |
| 2689 | case 1: |
| 2690 | buf[0] = usbvision_read_reg(usbvision, USBVISION_SER_DAT1); |
| 2691 | break; |
| 2692 | default: |
| 2693 | printk(KERN_ERR |
| 2694 | "usbvision_i2c_read_max4: buffer length > 4\n"); |
| 2695 | } |
| 2696 | |
| 2697 | if (debug & DBG_I2C) { |
| 2698 | int idx; |
| 2699 | for (idx = 0; idx < len; idx++) { |
| 2700 | PDEBUG(DBG_I2C, "read %x from address %x", (unsigned char)buf[idx], addr); |
| 2701 | } |
| 2702 | } |
| 2703 | return len; |
| 2704 | } |
| 2705 | |
| 2706 | |
| 2707 | static int usbvision_i2c_write_max4(struct usb_usbvision *usbvision, |
| 2708 | unsigned char addr, const char *buf, |
| 2709 | short len) |
| 2710 | { |
| 2711 | int rc, retries; |
| 2712 | int i; |
| 2713 | unsigned char value[6]; |
| 2714 | unsigned char ser_cont; |
| 2715 | |
| 2716 | ser_cont = (len & 0x07) | 0x10; |
| 2717 | |
| 2718 | value[0] = addr; |
| 2719 | value[1] = ser_cont; |
| 2720 | for (i = 0; i < len; i++) |
| 2721 | value[i + 2] = buf[i]; |
| 2722 | |
| 2723 | for (retries = 5;;) { |
| 2724 | rc = usb_control_msg(usbvision->dev, |
| 2725 | usb_sndctrlpipe(usbvision->dev, 1), |
| 2726 | USBVISION_OP_CODE, |
| 2727 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 2728 | USB_RECIP_ENDPOINT, 0, |
| 2729 | (__u16) USBVISION_SER_ADRS, value, |
| 2730 | len + 2, HZ); |
| 2731 | |
| 2732 | if (rc < 0) |
| 2733 | return rc; |
| 2734 | |
| 2735 | rc = usbvision_write_reg(usbvision, USBVISION_SER_CONT, |
| 2736 | (len & 0x07) | 0x10); |
| 2737 | if (rc < 0) |
| 2738 | return rc; |
| 2739 | |
| 2740 | /* Test for Busy and ACK */ |
| 2741 | do { |
| 2742 | rc = usbvision_read_reg(usbvision, USBVISION_SER_CONT); |
| 2743 | } while (rc > 0 && ((rc & 0x10) != 0)); /* Retry while busy */ |
| 2744 | if (rc < 0) |
| 2745 | return rc; |
| 2746 | |
| 2747 | if ((rc & 0x20) == 0) /* Ack? */ |
| 2748 | break; |
| 2749 | |
| 2750 | /* I2C abort */ |
| 2751 | usbvision_write_reg(usbvision, USBVISION_SER_CONT, 0x00); |
| 2752 | |
| 2753 | if (--retries < 0) |
| 2754 | return -1; |
| 2755 | |
| 2756 | } |
| 2757 | |
| 2758 | if (debug & DBG_I2C) { |
| 2759 | int idx; |
| 2760 | for (idx = 0; idx < len; idx++) { |
| 2761 | PDEBUG(DBG_I2C, "wrote %x at address %x", (unsigned char)buf[idx], addr); |
| 2762 | } |
| 2763 | } |
| 2764 | return len; |
| 2765 | } |
| 2766 | |
| 2767 | static int usbvision_i2c_write(void *data, unsigned char addr, char *buf, |
| 2768 | short len) |
| 2769 | { |
| 2770 | char *bufPtr = buf; |
| 2771 | int retval; |
| 2772 | int wrcount = 0; |
| 2773 | int count; |
| 2774 | int maxLen = 4; |
| 2775 | struct usb_usbvision *usbvision = (struct usb_usbvision *) data; |
| 2776 | |
| 2777 | while (len > 0) { |
| 2778 | count = (len > maxLen) ? maxLen : len; |
| 2779 | retval = usbvision_i2c_write_max4(usbvision, addr, bufPtr, count); |
| 2780 | if (retval > 0) { |
| 2781 | len -= count; |
| 2782 | bufPtr += count; |
| 2783 | wrcount += count; |
| 2784 | } else |
| 2785 | return (retval < 0) ? retval : -EFAULT; |
| 2786 | } |
| 2787 | return wrcount; |
| 2788 | } |
| 2789 | |
| 2790 | static int usbvision_i2c_read(void *data, unsigned char addr, char *buf, |
| 2791 | short len) |
| 2792 | { |
| 2793 | char temp[4]; |
| 2794 | int retval, i; |
| 2795 | int rdcount = 0; |
| 2796 | int count; |
| 2797 | struct usb_usbvision *usbvision = (struct usb_usbvision *) data; |
| 2798 | |
| 2799 | while (len > 0) { |
| 2800 | count = (len > 3) ? 4 : len; |
| 2801 | retval = usbvision_i2c_read_max4(usbvision, addr, temp, count); |
| 2802 | if (retval > 0) { |
| 2803 | for (i = 0; i < len; i++) |
| 2804 | buf[rdcount + i] = temp[i]; |
| 2805 | len -= count; |
| 2806 | rdcount += count; |
| 2807 | } else |
| 2808 | return (retval < 0) ? retval : -EFAULT; |
| 2809 | } |
| 2810 | return rdcount; |
| 2811 | } |
| 2812 | |
| 2813 | static struct i2c_algo_usb_data i2c_algo_template = { |
| 2814 | .data = NULL, |
| 2815 | .inb = usbvision_i2c_read, |
| 2816 | .outb = usbvision_i2c_write, |
| 2817 | .udelay = 10, |
| 2818 | .mdelay = 10, |
| 2819 | .timeout = 100, |
| 2820 | }; |
| 2821 | |
| 2822 | static struct i2c_adapter i2c_adap_template = { |
| 2823 | .owner = THIS_MODULE, |
| 2824 | .name = "usbvision", |
| 2825 | .id = I2C_HW_B_BT848, /* FIXME */ |
| 2826 | .algo = NULL, |
| 2827 | .algo_data = NULL, |
| 2828 | .client_register = attach_inform, |
| 2829 | .client_unregister = detach_inform, |
| 2830 | #if defined (I2C_ADAP_CLASS_TV_ANALOG) |
| 2831 | .class = I2C_ADAP_CLASS_TV_ANALOG, |
| 2832 | #elif defined (I2C_CLASS_TV_ANALOG) |
| 2833 | .class = I2C_CLASS_TV_ANALOG, |
| 2834 | #endif |
| 2835 | }; |
| 2836 | |
| 2837 | static struct i2c_client i2c_client_template = { |
| 2838 | .name = "usbvision internal", |
| 2839 | .flags = 0, |
| 2840 | .addr = 0, |
| 2841 | .adapter = NULL, |
| 2842 | .driver = NULL, |
| 2843 | }; |
| 2844 | |
| 2845 | static int usbvision_init_i2c(struct usb_usbvision *usbvision) |
| 2846 | { |
| 2847 | memcpy(&usbvision->i2c_adap, &i2c_adap_template, |
| 2848 | sizeof(struct i2c_adapter)); |
| 2849 | memcpy(&usbvision->i2c_algo, &i2c_algo_template, |
| 2850 | sizeof(struct i2c_algo_usb_data)); |
| 2851 | memcpy(&usbvision->i2c_client, &i2c_client_template, |
| 2852 | sizeof(struct i2c_client)); |
| 2853 | |
| 2854 | sprintf(usbvision->i2c_adap.name + strlen(usbvision->i2c_adap.name), |
| 2855 | " #%d", usbvision->vdev->minor & 0x1f); |
| 2856 | PDEBUG(DBG_I2C, "Adaptername: %s", usbvision->i2c_adap.name); |
| 2857 | |
| 2858 | i2c_set_adapdata(&usbvision->i2c_adap, usbvision); |
| 2859 | i2c_set_clientdata(&usbvision->i2c_client, usbvision); |
| 2860 | i2c_set_algo_usb_data(&usbvision->i2c_algo, usbvision); |
| 2861 | |
| 2862 | usbvision->i2c_adap.algo_data = &usbvision->i2c_algo; |
| 2863 | usbvision->i2c_client.adapter = &usbvision->i2c_adap; |
| 2864 | |
| 2865 | if (usbvision_write_reg(usbvision, USBVISION_SER_MODE, USBVISION_IIC_LRNACK) < 0) { |
| 2866 | printk(KERN_ERR "usbvision_init_i2c: can't wirte reg\n"); |
| 2867 | return -EBUSY; |
| 2868 | } |
| 2869 | |
| 2870 | #ifdef CONFIG_KMOD |
| 2871 | /* Request the load of the i2c modules we need */ |
| 2872 | if (autoload) { |
| 2873 | switch (usbvision_device_data[usbvision->DevModel].Codec) { |
| 2874 | case CODEC_SAA7113: |
| 2875 | request_module("saa7115"); |
| 2876 | break; |
| 2877 | case CODEC_SAA7111: |
| 2878 | request_module("saa7115"); |
| 2879 | break; |
| 2880 | } |
| 2881 | if (usbvision_device_data[usbvision->DevModel].Tuner == 1) { |
| 2882 | request_module("tuner"); |
| 2883 | } |
| 2884 | } |
| 2885 | #endif |
| 2886 | |
| 2887 | usbvision->i2c_ok = usbvision_i2c_usb_add_bus(&usbvision->i2c_adap); |
| 2888 | |
| 2889 | return usbvision->i2c_ok; |
| 2890 | |
| 2891 | } |
| 2892 | |
| 2893 | |
| 2894 | /****************************/ |
| 2895 | /* usbvision utility functions */ |
| 2896 | /****************************/ |
| 2897 | |
| 2898 | static int usbvision_power_off(struct usb_usbvision *usbvision) |
| 2899 | { |
| 2900 | int errCode = 0; |
| 2901 | |
| 2902 | PDEBUG(DBG_FUNC, ""); |
| 2903 | |
| 2904 | errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN); |
| 2905 | if (errCode == 1) { |
| 2906 | usbvision->power = 0; |
| 2907 | } |
| 2908 | PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode!=1)?"ERROR":"power is off", errCode); |
| 2909 | return errCode; |
| 2910 | } |
| 2911 | |
| 2912 | |
| 2913 | // to call usbvision_power_off from task queue |
| 2914 | static void call_usbvision_power_off(void *_usbvision) |
| 2915 | { |
| 2916 | struct usb_usbvision *usbvision = _usbvision; |
| 2917 | |
| 2918 | PDEBUG(DBG_FUNC, ""); |
| 2919 | down_interruptible(&usbvision->lock); |
| 2920 | if(usbvision->user == 0) { |
| 2921 | usbvision_i2c_usb_del_bus(&usbvision->i2c_adap); |
| 2922 | usbvision_power_off(usbvision); |
| 2923 | usbvision->initialized = 0; |
| 2924 | } |
| 2925 | up(&usbvision->lock); |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | /* |
| 2930 | * usbvision_set_video_format() |
| 2931 | * |
| 2932 | */ |
| 2933 | static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format) |
| 2934 | { |
| 2935 | static const char proc[] = "usbvision_set_video_format"; |
| 2936 | int rc; |
| 2937 | unsigned char value[2]; |
| 2938 | |
| 2939 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 2940 | return 0; |
| 2941 | |
| 2942 | PDEBUG(DBG_FUNC, "isocMode %#02x", format); |
| 2943 | |
| 2944 | if ((format != ISOC_MODE_YUV422) |
| 2945 | && (format != ISOC_MODE_YUV420) |
| 2946 | && (format != ISOC_MODE_COMPRESS)) { |
| 2947 | printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420", |
| 2948 | format); |
| 2949 | format = ISOC_MODE_YUV420; |
| 2950 | } |
| 2951 | value[0] = 0x0A; //TODO: See the effect of the filter |
| 2952 | value[1] = format; |
| 2953 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 2954 | USBVISION_OP_CODE, |
| 2955 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 2956 | USB_RECIP_ENDPOINT, 0, |
| 2957 | (__u16) USBVISION_FILT_CONT, value, 2, HZ); |
| 2958 | |
| 2959 | if (rc < 0) { |
| 2960 | printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - " |
| 2961 | "reconnect or reload driver.\n", proc, rc); |
| 2962 | } |
| 2963 | usbvision->isocMode = format; |
| 2964 | return rc; |
| 2965 | } |
| 2966 | |
| 2967 | /* |
| 2968 | * usbvision_set_output() |
| 2969 | * |
| 2970 | */ |
| 2971 | |
| 2972 | static int usbvision_set_output(struct usb_usbvision *usbvision, int width, |
| 2973 | int height) |
| 2974 | { |
| 2975 | int errCode = 0; |
| 2976 | int UsbWidth, UsbHeight; |
| 2977 | unsigned int frameRate=0, frameDrop=0; |
| 2978 | unsigned char value[4]; |
| 2979 | |
| 2980 | if (!USBVISION_IS_OPERATIONAL(usbvision)) { |
| 2981 | return 0; |
| 2982 | } |
| 2983 | |
| 2984 | if (width > MAX_USB_WIDTH) { |
| 2985 | UsbWidth = width / 2; |
| 2986 | usbvision->stretch_width = 2; |
| 2987 | } |
| 2988 | else { |
| 2989 | UsbWidth = width; |
| 2990 | usbvision->stretch_width = 1; |
| 2991 | } |
| 2992 | |
| 2993 | if (height > MAX_USB_HEIGHT) { |
| 2994 | UsbHeight = height / 2; |
| 2995 | usbvision->stretch_height = 2; |
| 2996 | } |
| 2997 | else { |
| 2998 | UsbHeight = height; |
| 2999 | usbvision->stretch_height = 1; |
| 3000 | } |
| 3001 | |
| 3002 | RESTRICT_TO_RANGE(UsbWidth, MIN_FRAME_WIDTH, MAX_USB_WIDTH); |
| 3003 | UsbWidth &= ~(MIN_FRAME_WIDTH-1); |
| 3004 | RESTRICT_TO_RANGE(UsbHeight, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT); |
| 3005 | UsbHeight &= ~(1); |
| 3006 | |
| 3007 | PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d", |
| 3008 | UsbWidth, UsbHeight, width, height, |
| 3009 | usbvision->stretch_width, usbvision->stretch_height); |
| 3010 | |
| 3011 | /* I'll not rewrite the same values */ |
| 3012 | if ((UsbWidth != usbvision->curwidth) || (UsbHeight != usbvision->curheight)) { |
| 3013 | value[0] = UsbWidth & 0xff; //LSB |
| 3014 | value[1] = (UsbWidth >> 8) & 0x03; //MSB |
| 3015 | value[2] = UsbHeight & 0xff; //LSB |
| 3016 | value[3] = (UsbHeight >> 8) & 0x03; //MSB |
| 3017 | |
| 3018 | errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 3019 | USBVISION_OP_CODE, |
| 3020 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, |
| 3021 | 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ); |
| 3022 | |
| 3023 | if (errCode < 0) { |
| 3024 | err("%s failed: error %d", __FUNCTION__, errCode); |
| 3025 | return errCode; |
| 3026 | } |
| 3027 | usbvision->curwidth = usbvision->stretch_width * UsbWidth; |
| 3028 | usbvision->curheight = usbvision->stretch_height * UsbHeight; |
| 3029 | } |
| 3030 | |
| 3031 | if (usbvision->isocMode == ISOC_MODE_YUV422) { |
| 3032 | frameRate = (usbvision->isocPacketSize * 1000) / (UsbWidth * UsbHeight * 2); |
| 3033 | } |
| 3034 | else if (usbvision->isocMode == ISOC_MODE_YUV420) { |
| 3035 | frameRate = (usbvision->isocPacketSize * 1000) / ((UsbWidth * UsbHeight * 12) / 8); |
| 3036 | } |
| 3037 | else { |
| 3038 | frameRate = FRAMERATE_MAX; |
| 3039 | } |
| 3040 | |
| 3041 | if (usbvision->tvnorm->id & V4L2_STD_625_50) { |
| 3042 | frameDrop = frameRate * 32 / 25 - 1; |
| 3043 | } |
| 3044 | else if (usbvision->tvnorm->id & V4L2_STD_525_60) { |
| 3045 | frameDrop = frameRate * 32 / 30 - 1; |
| 3046 | } |
| 3047 | |
| 3048 | RESTRICT_TO_RANGE(frameDrop, FRAMERATE_MIN, FRAMERATE_MAX); |
| 3049 | |
| 3050 | PDEBUG(DBG_FUNC, "frameRate %d fps, frameDrop %d", frameRate, frameDrop); |
| 3051 | |
| 3052 | frameDrop = FRAMERATE_MAX; // We can allow the maximum here, because dropping is controlled |
| 3053 | |
| 3054 | /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ... |
| 3055 | => frameSkip = 4; |
| 3056 | => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25; |
| 3057 | |
| 3058 | frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ... |
| 3059 | => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ... |
| 3060 | => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125; |
| 3061 | */ |
| 3062 | errCode = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frameDrop); |
| 3063 | return errCode; |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | /* |
| 3068 | * usbvision_empty_framequeues() |
| 3069 | * prepare queues for incoming and outgoing frames |
| 3070 | */ |
| 3071 | static void usbvision_empty_framequeues(struct usb_usbvision *usbvision) |
| 3072 | { |
| 3073 | u32 i; |
| 3074 | |
| 3075 | INIT_LIST_HEAD(&(usbvision->inqueue)); |
| 3076 | INIT_LIST_HEAD(&(usbvision->outqueue)); |
| 3077 | |
| 3078 | for (i = 0; i < USBVISION_NUMFRAMES; i++) { |
| 3079 | usbvision->frame[i].grabstate = FrameState_Unused; |
| 3080 | usbvision->frame[i].bytes_read = 0; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * usbvision_stream_interrupt() |
| 3086 | * stops streaming |
| 3087 | */ |
| 3088 | static int usbvision_stream_interrupt(struct usb_usbvision *usbvision) |
| 3089 | { |
| 3090 | int ret = 0; |
| 3091 | |
| 3092 | /* stop reading from the device */ |
| 3093 | |
| 3094 | usbvision->streaming = Stream_Interrupt; |
| 3095 | ret = wait_event_timeout(usbvision->wait_stream, |
| 3096 | (usbvision->streaming == Stream_Idle), |
| 3097 | msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES)); |
| 3098 | return ret; |
| 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * usbvision_set_compress_params() |
| 3103 | * |
| 3104 | */ |
| 3105 | |
| 3106 | static int usbvision_set_compress_params(struct usb_usbvision *usbvision) |
| 3107 | { |
| 3108 | static const char proc[] = "usbvision_set_compresion_params: "; |
| 3109 | int rc; |
| 3110 | unsigned char value[6]; |
| 3111 | |
| 3112 | value[0] = 0x0F; // Intra-Compression cycle |
| 3113 | value[1] = 0x01; // Reg.45 one line per strip |
| 3114 | value[2] = 0x00; // Reg.46 Force intra mode on all new frames |
| 3115 | value[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force) |
| 3116 | value[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode. |
| 3117 | value[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression |
| 3118 | |
| 3119 | //catched values for NT1004 |
| 3120 | // value[0] = 0xFF; // Never apply intra mode automatically |
| 3121 | // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip |
| 3122 | // value[2] = 0x01; // Force intra mode on all new frames |
| 3123 | // value[3] = 0x00; // Strip size 400 Bytes; do not force up |
| 3124 | // value[4] = 0xA2; // |
| 3125 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3126 | return 0; |
| 3127 | |
| 3128 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 3129 | USBVISION_OP_CODE, |
| 3130 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 3131 | USB_RECIP_ENDPOINT, 0, |
| 3132 | (__u16) USBVISION_INTRA_CYC, value, 5, HZ); |
| 3133 | |
| 3134 | if (rc < 0) { |
| 3135 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " |
| 3136 | "reconnect or reload driver.\n", proc, rc); |
| 3137 | return rc; |
| 3138 | } |
| 3139 | |
| 3140 | if (usbvision->bridgeType == BRIDGE_NT1004) { |
| 3141 | value[0] = 20; // PCM Threshold 1 |
| 3142 | value[1] = 12; // PCM Threshold 2 |
| 3143 | value[2] = 255; // Distorsion Threshold inter |
| 3144 | value[3] = 255; // Distorsion Threshold intra |
| 3145 | value[4] = 43; // Max Distorsion inter |
| 3146 | value[5] = 43; // Max Distorsion intra |
| 3147 | } |
| 3148 | else { |
| 3149 | value[0] = 20; // PCM Threshold 1 |
| 3150 | value[1] = 12; // PCM Threshold 2 |
| 3151 | value[2] = 255; // Distorsion Threshold d7-d0 |
| 3152 | value[3] = 0; // Distorsion Threshold d11-d8 |
| 3153 | value[4] = 43; // Max Distorsion d7-d0 |
| 3154 | value[5] = 0; // Max Distorsion d8 |
| 3155 | } |
| 3156 | |
| 3157 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3158 | return 0; |
| 3159 | |
| 3160 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 3161 | USBVISION_OP_CODE, |
| 3162 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 3163 | USB_RECIP_ENDPOINT, 0, |
| 3164 | (__u16) USBVISION_PCM_THR1, value, 6, HZ); |
| 3165 | |
| 3166 | if (rc < 0) { |
| 3167 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " |
| 3168 | "reconnect or reload driver.\n", proc, rc); |
| 3169 | return rc; |
| 3170 | } |
| 3171 | |
| 3172 | |
| 3173 | return rc; |
| 3174 | } |
| 3175 | |
| 3176 | |
| 3177 | /* |
| 3178 | * usbvision_set_input() |
| 3179 | * |
| 3180 | * Set the input (saa711x, ...) size x y and other misc input params |
| 3181 | * I've no idea if this parameters are right |
| 3182 | * |
| 3183 | */ |
| 3184 | static int usbvision_set_input(struct usb_usbvision *usbvision) |
| 3185 | { |
| 3186 | static const char proc[] = "usbvision_set_input: "; |
| 3187 | int rc; |
| 3188 | unsigned char value[8]; |
| 3189 | unsigned char dvi_yuv_value; |
| 3190 | |
| 3191 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3192 | return 0; |
| 3193 | |
| 3194 | /* Set input format expected from decoder*/ |
| 3195 | if (usbvision_device_data[usbvision->DevModel].Vin_Reg1 >= 0) { |
| 3196 | value[0] = usbvision_device_data[usbvision->DevModel].Vin_Reg1 & 0xff; |
| 3197 | } else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) { |
| 3198 | /* SAA7113 uses 8 bit output */ |
| 3199 | value[0] = USBVISION_8_422_SYNC; |
| 3200 | } else { |
| 3201 | /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses |
| 3202 | * as that is how saa7111 is configured */ |
| 3203 | value[0] = USBVISION_16_422_SYNC; |
| 3204 | /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/ |
| 3205 | } |
| 3206 | |
| 3207 | rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]); |
| 3208 | if (rc < 0) { |
| 3209 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " |
| 3210 | "reconnect or reload driver.\n", proc, rc); |
| 3211 | return rc; |
| 3212 | } |
| 3213 | |
| 3214 | |
| 3215 | if (usbvision->tvnorm->id & V4L2_STD_PAL) { |
| 3216 | value[0] = 0xC0; |
| 3217 | value[1] = 0x02; //0x02C0 -> 704 Input video line length |
| 3218 | value[2] = 0x20; |
| 3219 | value[3] = 0x01; //0x0120 -> 288 Input video n. of lines |
| 3220 | value[4] = 0x60; |
| 3221 | value[5] = 0x00; //0x0060 -> 96 Input video h offset |
| 3222 | value[6] = 0x16; |
| 3223 | value[7] = 0x00; //0x0016 -> 22 Input video v offset |
| 3224 | } else if (usbvision->tvnorm->id & V4L2_STD_SECAM) { |
| 3225 | value[0] = 0xC0; |
| 3226 | value[1] = 0x02; //0x02C0 -> 704 Input video line length |
| 3227 | value[2] = 0x20; |
| 3228 | value[3] = 0x01; //0x0120 -> 288 Input video n. of lines |
| 3229 | value[4] = 0x01; |
| 3230 | value[5] = 0x00; //0x0001 -> 01 Input video h offset |
| 3231 | value[6] = 0x01; |
| 3232 | value[7] = 0x00; //0x0001 -> 01 Input video v offset |
| 3233 | } else { /* V4L2_STD_NTSC */ |
| 3234 | value[0] = 0xD0; |
| 3235 | value[1] = 0x02; //0x02D0 -> 720 Input video line length |
| 3236 | value[2] = 0xF0; |
| 3237 | value[3] = 0x00; //0x00F0 -> 240 Input video number of lines |
| 3238 | value[4] = 0x50; |
| 3239 | value[5] = 0x00; //0x0050 -> 80 Input video h offset |
| 3240 | value[6] = 0x10; |
| 3241 | value[7] = 0x00; //0x0010 -> 16 Input video v offset |
| 3242 | } |
| 3243 | |
| 3244 | if (usbvision_device_data[usbvision->DevModel].X_Offset >= 0) { |
| 3245 | value[4]=usbvision_device_data[usbvision->DevModel].X_Offset & 0xff; |
| 3246 | value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8; |
| 3247 | } |
| 3248 | |
| 3249 | if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) { |
| 3250 | value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff; |
| 3251 | value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8; |
| 3252 | } |
| 3253 | |
| 3254 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 3255 | USBVISION_OP_CODE, /* USBVISION specific code */ |
| 3256 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0, |
| 3257 | (__u16) USBVISION_LXSIZE_I, value, 8, HZ); |
| 3258 | if (rc < 0) { |
| 3259 | printk(KERN_ERR "%sERROR=%d. USBVISION stopped - " |
| 3260 | "reconnect or reload driver.\n", proc, rc); |
| 3261 | return rc; |
| 3262 | } |
| 3263 | |
| 3264 | |
| 3265 | dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */ |
| 3266 | |
| 3267 | if(usbvision_device_data[usbvision->DevModel].Dvi_yuv >= 0){ |
| 3268 | dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv & 0xff; |
| 3269 | } |
| 3270 | else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) { |
| 3271 | /* This changes as the fine sync control changes. Further investigation necessary */ |
| 3272 | dvi_yuv_value = 0x06; |
| 3273 | } |
| 3274 | |
| 3275 | return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value)); |
| 3276 | } |
| 3277 | |
| 3278 | |
| 3279 | /* |
| 3280 | * usbvision_set_dram_settings() |
| 3281 | * |
| 3282 | * Set the buffer address needed by the usbvision dram to operate |
| 3283 | * This values has been taken with usbsnoop. |
| 3284 | * |
| 3285 | */ |
| 3286 | |
| 3287 | static int usbvision_set_dram_settings(struct usb_usbvision *usbvision) |
| 3288 | { |
| 3289 | int rc; |
| 3290 | unsigned char value[8]; |
| 3291 | |
| 3292 | if (usbvision->isocMode == ISOC_MODE_COMPRESS) { |
| 3293 | value[0] = 0x42; |
| 3294 | value[1] = 0x71; |
| 3295 | value[2] = 0xff; |
| 3296 | value[3] = 0x00; |
| 3297 | value[4] = 0x98; |
| 3298 | value[5] = 0xe0; |
| 3299 | value[6] = 0x71; |
| 3300 | value[7] = 0xff; |
| 3301 | // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte) |
| 3302 | // FDL: 0x00000-0x0E099 = 57498 Words |
| 3303 | // VDW: 0x0E3FF-0x3FFFF |
| 3304 | } |
| 3305 | else { |
| 3306 | value[0] = 0x42; |
| 3307 | value[1] = 0x00; |
| 3308 | value[2] = 0xff; |
| 3309 | value[3] = 0x00; |
| 3310 | value[4] = 0x00; |
| 3311 | value[5] = 0x00; |
| 3312 | value[6] = 0x00; |
| 3313 | value[7] = 0xff; |
| 3314 | } |
| 3315 | /* These are the values of the address of the video buffer, |
| 3316 | * they have to be loaded into the USBVISION_DRM_PRM1-8 |
| 3317 | * |
| 3318 | * Start address of video output buffer for read: drm_prm1-2 -> 0x00000 |
| 3319 | * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff |
| 3320 | * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000 |
| 3321 | * Only used in compressed mode |
| 3322 | * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff |
| 3323 | * Only used in compressed mode |
| 3324 | * Start address of video output buffer for write: drm_prm1-7 -> 0x00000 |
| 3325 | * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff |
| 3326 | */ |
| 3327 | |
| 3328 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3329 | return 0; |
| 3330 | |
| 3331 | rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1), |
| 3332 | USBVISION_OP_CODE, /* USBVISION specific code */ |
| 3333 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 3334 | USB_RECIP_ENDPOINT, 0, |
| 3335 | (__u16) USBVISION_DRM_PRM1, value, 8, HZ); |
| 3336 | |
| 3337 | if (rc < 0) { |
| 3338 | err("%sERROR=%d", __FUNCTION__, rc); |
| 3339 | return rc; |
| 3340 | } |
| 3341 | |
| 3342 | /* Restart the video buffer logic */ |
| 3343 | if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR | |
| 3344 | USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0) |
| 3345 | return rc; |
| 3346 | rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00); |
| 3347 | |
| 3348 | return rc; |
| 3349 | } |
| 3350 | |
| 3351 | /* |
| 3352 | * () |
| 3353 | * |
| 3354 | * Power on the device, enables suspend-resume logic |
| 3355 | * & reset the isoc End-Point |
| 3356 | * |
| 3357 | */ |
| 3358 | |
| 3359 | static int usbvision_power_on(struct usb_usbvision *usbvision) |
| 3360 | { |
| 3361 | int errCode = 0; |
| 3362 | |
| 3363 | PDEBUG(DBG_FUNC, ""); |
| 3364 | |
| 3365 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN); |
| 3366 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
| 3367 | USBVISION_SSPND_EN | USBVISION_RES2); |
| 3368 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
| 3369 | USBVISION_SSPND_EN | USBVISION_PWR_VID); |
| 3370 | errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
| 3371 | USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2); |
| 3372 | if (errCode == 1) { |
| 3373 | usbvision->power = 1; |
| 3374 | } |
| 3375 | PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode<0)?"ERROR":"power is on", errCode); |
| 3376 | return errCode; |
| 3377 | } |
| 3378 | |
| 3379 | |
| 3380 | static void usbvision_powerOffTimer(unsigned long data) |
| 3381 | { |
| 3382 | struct usb_usbvision *usbvision = (void *) data; |
| 3383 | |
| 3384 | PDEBUG(DBG_FUNC, ""); |
| 3385 | del_timer(&usbvision->powerOffTimer); |
| 3386 | INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off, usbvision); |
| 3387 | (void) schedule_work(&usbvision->powerOffWork); |
| 3388 | |
| 3389 | } |
| 3390 | |
| 3391 | |
| 3392 | /* |
| 3393 | * usbvision_begin_streaming() |
| 3394 | * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no |
| 3395 | * idea about the rest |
| 3396 | */ |
| 3397 | static int usbvision_begin_streaming(struct usb_usbvision *usbvision) |
| 3398 | { |
| 3399 | int errCode = 0; |
| 3400 | |
| 3401 | if (usbvision->isocMode == ISOC_MODE_COMPRESS) { |
| 3402 | usbvision_init_compression(usbvision); |
| 3403 | } |
| 3404 | errCode = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID | |
| 3405 | usbvision->Vin_Reg2_Preset); |
| 3406 | return errCode; |
| 3407 | } |
| 3408 | |
| 3409 | /* |
| 3410 | * usbvision_restart_isoc() |
| 3411 | * Not sure yet if touching here PWR_REG make loose the config |
| 3412 | */ |
| 3413 | |
| 3414 | static int usbvision_restart_isoc(struct usb_usbvision *usbvision) |
| 3415 | { |
| 3416 | int ret; |
| 3417 | |
| 3418 | if ( |
| 3419 | (ret = |
| 3420 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
| 3421 | USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0) |
| 3422 | return ret; |
| 3423 | if ( |
| 3424 | (ret = |
| 3425 | usbvision_write_reg(usbvision, USBVISION_PWR_REG, |
| 3426 | USBVISION_SSPND_EN | USBVISION_PWR_VID | |
| 3427 | USBVISION_RES2)) < 0) |
| 3428 | return ret; |
| 3429 | if ( |
| 3430 | (ret = |
| 3431 | usbvision_write_reg(usbvision, USBVISION_VIN_REG2, |
| 3432 | USBVISION_KEEP_BLANK | USBVISION_NOHVALID | |
| 3433 | usbvision->Vin_Reg2_Preset)) < 0) return ret; |
| 3434 | |
| 3435 | /* TODO: schedule timeout */ |
| 3436 | while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) && 0x01) != 1); |
| 3437 | |
| 3438 | return 0; |
| 3439 | } |
| 3440 | |
| 3441 | static int usbvision_audio_on(struct usb_usbvision *usbvision) |
| 3442 | { |
| 3443 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, usbvision->AudioChannel) < 0) { |
| 3444 | printk(KERN_ERR "usbvision_audio_on: can't wirte reg\n"); |
| 3445 | return -1; |
| 3446 | } |
| 3447 | DEBUG(printk(KERN_DEBUG "usbvision_audio_on: channel %d\n", usbvision->AudioChannel)); |
| 3448 | usbvision->AudioMute = 0; |
| 3449 | return 0; |
| 3450 | } |
| 3451 | |
| 3452 | static int usbvision_audio_mute(struct usb_usbvision *usbvision) |
| 3453 | { |
| 3454 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, 0x03) < 0) { |
| 3455 | printk(KERN_ERR "usbvision_audio_mute: can't wirte reg\n"); |
| 3456 | return -1; |
| 3457 | } |
| 3458 | DEBUG(printk(KERN_DEBUG "usbvision_audio_mute: audio mute\n")); |
| 3459 | usbvision->AudioMute = 1; |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
| 3463 | static int usbvision_audio_off(struct usb_usbvision *usbvision) |
| 3464 | { |
| 3465 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) { |
| 3466 | printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n"); |
| 3467 | return -1; |
| 3468 | } |
| 3469 | DEBUG(printk(KERN_DEBUG "usbvision_audio_off: audio off\n")); |
| 3470 | usbvision->AudioMute = 0; |
| 3471 | usbvision->AudioChannel = USBVISION_AUDIO_MUTE; |
| 3472 | return 0; |
| 3473 | } |
| 3474 | |
| 3475 | static int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel) |
| 3476 | { |
| 3477 | if (!usbvision->AudioMute) { |
| 3478 | if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) { |
| 3479 | printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n"); |
| 3480 | return -1; |
| 3481 | } |
| 3482 | } |
| 3483 | DEBUG(printk(KERN_DEBUG "usbvision_set_audio: channel %d\n", AudioChannel)); |
| 3484 | usbvision->AudioChannel = AudioChannel; |
| 3485 | return 0; |
| 3486 | } |
| 3487 | |
| 3488 | static int usbvision_setup(struct usb_usbvision *usbvision) |
| 3489 | { |
| 3490 | usbvision_set_video_format(usbvision, isocMode); |
| 3491 | usbvision_set_dram_settings(usbvision); |
| 3492 | usbvision_set_compress_params(usbvision); |
| 3493 | usbvision_set_input(usbvision); |
| 3494 | usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT); |
| 3495 | usbvision_restart_isoc(usbvision); |
| 3496 | |
| 3497 | /* cosas del PCM */ |
| 3498 | return USBVISION_IS_OPERATIONAL(usbvision); |
| 3499 | } |
| 3500 | |
| 3501 | |
| 3502 | /* |
| 3503 | * usbvision_init_isoc() |
| 3504 | * |
| 3505 | */ |
| 3506 | static int usbvision_init_isoc(struct usb_usbvision *usbvision) |
| 3507 | { |
| 3508 | struct usb_device *dev = usbvision->dev; |
| 3509 | int bufIdx, errCode, regValue; |
| 3510 | |
| 3511 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3512 | return -EFAULT; |
| 3513 | |
| 3514 | usbvision->curFrame = NULL; |
| 3515 | scratch_reset(usbvision); |
| 3516 | |
| 3517 | /* Alternate interface 1 is is the biggest frame size */ |
| 3518 | errCode = usb_set_interface(dev, usbvision->iface, usbvision->ifaceAltActive); |
| 3519 | if (errCode < 0) { |
| 3520 | usbvision->last_error = errCode; |
| 3521 | return -EBUSY; |
| 3522 | } |
| 3523 | |
| 3524 | regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F; |
| 3525 | usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1; |
| 3526 | PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize); |
| 3527 | |
| 3528 | usbvision->usb_bandwidth = regValue >> 1; |
| 3529 | PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth); |
| 3530 | |
| 3531 | |
| 3532 | |
| 3533 | /* We double buffer the Iso lists */ |
| 3534 | |
| 3535 | for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) { |
| 3536 | int j, k; |
| 3537 | struct urb *urb; |
| 3538 | |
| 3539 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 3540 | urb = usb_alloc_urb(USBVISION_URB_FRAMES); |
| 3541 | #else |
| 3542 | urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL); |
| 3543 | #endif |
| 3544 | if (urb == NULL) { |
| 3545 | err("%s: usb_alloc_urb() failed", __FUNCTION__); |
| 3546 | return -ENOMEM; |
| 3547 | } |
| 3548 | usbvision->sbuf[bufIdx].urb = urb; |
| 3549 | urb->dev = dev; |
| 3550 | urb->context = usbvision; |
| 3551 | urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp); |
| 3552 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 3553 | urb->transfer_flags = USB_ISO_ASAP; |
| 3554 | #else |
| 3555 | urb->transfer_flags = URB_ISO_ASAP; |
| 3556 | urb->interval = 1; |
| 3557 | #endif |
| 3558 | urb->transfer_buffer = usbvision->sbuf[bufIdx].data; |
| 3559 | urb->complete = usbvision_isocIrq; |
| 3560 | urb->number_of_packets = USBVISION_URB_FRAMES; |
| 3561 | urb->transfer_buffer_length = |
| 3562 | usbvision->isocPacketSize * USBVISION_URB_FRAMES; |
| 3563 | for (j = k = 0; j < USBVISION_URB_FRAMES; j++, |
| 3564 | k += usbvision->isocPacketSize) { |
| 3565 | urb->iso_frame_desc[j].offset = k; |
| 3566 | urb->iso_frame_desc[j].length = usbvision->isocPacketSize; |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | |
| 3571 | /* Submit all URBs */ |
| 3572 | for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) { |
| 3573 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0) |
| 3574 | errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb); |
| 3575 | #else |
| 3576 | errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb, GFP_KERNEL); |
| 3577 | #endif |
| 3578 | if (errCode) { |
| 3579 | err("%s: usb_submit_urb(%d) failed: error %d", __FUNCTION__, bufIdx, errCode); |
| 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | usbvision->streaming = Stream_Idle; |
| 3584 | PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x", __FUNCTION__, usbvision->video_endp); |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | /* |
| 3589 | * usbvision_stop_isoc() |
| 3590 | * |
| 3591 | * This procedure stops streaming and deallocates URBs. Then it |
| 3592 | * activates zero-bandwidth alt. setting of the video interface. |
| 3593 | * |
| 3594 | */ |
| 3595 | static void usbvision_stop_isoc(struct usb_usbvision *usbvision) |
| 3596 | { |
| 3597 | int bufIdx, errCode, regValue; |
| 3598 | |
| 3599 | if ((usbvision->streaming == Stream_Off) || (usbvision->dev == NULL)) |
| 3600 | return; |
| 3601 | |
| 3602 | /* Unschedule all of the iso td's */ |
| 3603 | for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) { |
| 3604 | usb_kill_urb(usbvision->sbuf[bufIdx].urb); |
| 3605 | usb_free_urb(usbvision->sbuf[bufIdx].urb); |
| 3606 | usbvision->sbuf[bufIdx].urb = NULL; |
| 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | PDEBUG(DBG_ISOC, "%s: streaming=Stream_Off\n", __FUNCTION__); |
| 3611 | usbvision->streaming = Stream_Off; |
| 3612 | |
| 3613 | if (!usbvision->remove_pending) { |
| 3614 | |
| 3615 | /* Set packet size to 0 */ |
| 3616 | errCode = usb_set_interface(usbvision->dev, usbvision->iface, |
| 3617 | usbvision->ifaceAltInactive); |
| 3618 | if (errCode < 0) { |
| 3619 | err("%s: usb_set_interface() failed: error %d", __FUNCTION__, errCode); |
| 3620 | usbvision->last_error = errCode; |
| 3621 | } |
| 3622 | regValue = (16 - usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F; |
| 3623 | usbvision->isocPacketSize = (regValue == 0) ? 0 : (regValue * 64) - 1; |
| 3624 | PDEBUG(DBG_ISOC, "ISO Packet Length:%d", usbvision->isocPacketSize); |
| 3625 | |
| 3626 | usbvision->usb_bandwidth = regValue >> 1; |
| 3627 | PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec", usbvision->usb_bandwidth); |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | static int usbvision_muxsel(struct usb_usbvision *usbvision, int channel) |
| 3632 | { |
| 3633 | int mode[4]; |
| 3634 | int audio[]= {1, 0, 0, 0}; |
| 3635 | struct v4l2_routing route; |
| 3636 | //channel 0 is TV with audiochannel 1 (tuner mono) |
| 3637 | //channel 1 is Composite with audio channel 0 (line in) |
| 3638 | //channel 2 is S-Video with audio channel 0 (line in) |
| 3639 | //channel 3 is additional video inputs to the device with audio channel 0 (line in) |
| 3640 | |
| 3641 | RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs); |
| 3642 | usbvision->ctl_input = channel; |
| 3643 | route.input = SAA7115_COMPOSITE1; |
| 3644 | call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route); |
| 3645 | call_i2c_clients(usbvision, VIDIOC_S_INPUT, &usbvision->ctl_input); |
| 3646 | |
| 3647 | // set the new channel |
| 3648 | // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video |
| 3649 | // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red |
| 3650 | |
| 3651 | switch (usbvision_device_data[usbvision->DevModel].Codec) { |
| 3652 | case CODEC_SAA7113: |
| 3653 | if (SwitchSVideoInput) { // To handle problems with S-Video Input for some devices. Use SwitchSVideoInput parameter when loading the module. |
| 3654 | mode[2] = 1; |
| 3655 | } |
| 3656 | else { |
| 3657 | mode[2] = 7; |
| 3658 | } |
| 3659 | if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) { |
| 3660 | mode[0] = 0; mode[1] = 2; mode[3] = 3; // Special for four input devices |
| 3661 | } |
| 3662 | else { |
| 3663 | mode[0] = 0; mode[1] = 2; //modes for regular saa7113 devices |
| 3664 | } |
| 3665 | break; |
| 3666 | case CODEC_SAA7111: |
| 3667 | mode[0] = 0; mode[1] = 1; mode[2] = 7; //modes for saa7111 |
| 3668 | break; |
| 3669 | default: |
| 3670 | mode[0] = 0; mode[1] = 1; mode[2] = 7; //default modes |
| 3671 | } |
| 3672 | route.input = mode[channel]; |
| 3673 | call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route); |
| 3674 | usbvision->channel = channel; |
| 3675 | usbvision_set_audio(usbvision, audio[channel]); |
| 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | |
| 3680 | /* |
| 3681 | * usbvision_open() |
| 3682 | * |
| 3683 | * This is part of Video 4 Linux API. The driver can be opened by one |
| 3684 | * client only (checks internal counter 'usbvision->user'). The procedure |
| 3685 | * then allocates buffers needed for video processing. |
| 3686 | * |
| 3687 | */ |
| 3688 | static int usbvision_v4l2_open(struct inode *inode, struct file *file) |
| 3689 | { |
| 3690 | struct video_device *dev = video_devdata(file); |
| 3691 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 3692 | const int sb_size = USBVISION_URB_FRAMES * USBVISION_MAX_ISOC_PACKET_SIZE; |
| 3693 | int i, errCode = 0; |
| 3694 | |
| 3695 | PDEBUG(DBG_IO, "open"); |
| 3696 | |
| 3697 | |
| 3698 | if (timer_pending(&usbvision->powerOffTimer)) { |
| 3699 | del_timer(&usbvision->powerOffTimer); |
| 3700 | } |
| 3701 | |
| 3702 | if (usbvision->user) |
| 3703 | errCode = -EBUSY; |
| 3704 | else { |
| 3705 | /* Clean pointers so we know if we allocated something */ |
| 3706 | for (i = 0; i < USBVISION_NUMSBUF; i++) |
| 3707 | usbvision->sbuf[i].data = NULL; |
| 3708 | |
| 3709 | /* Allocate memory for the frame buffers */ |
| 3710 | usbvision->max_frame_size = MAX_FRAME_SIZE; |
| 3711 | usbvision->fbuf_size = USBVISION_NUMFRAMES * usbvision->max_frame_size; |
| 3712 | usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size); |
| 3713 | usbvision->scratch = vmalloc(scratch_buf_size); |
| 3714 | scratch_reset(usbvision); |
| 3715 | if ((usbvision->fbuf == NULL) || (usbvision->scratch == NULL)) { |
| 3716 | err("%s: unable to allocate %d bytes for fbuf and %d bytes for scratch", |
| 3717 | __FUNCTION__, usbvision->fbuf_size, scratch_buf_size); |
| 3718 | errCode = -ENOMEM; |
| 3719 | } |
| 3720 | else { |
| 3721 | spin_lock_init(&usbvision->queue_lock); |
| 3722 | init_waitqueue_head(&usbvision->wait_frame); |
| 3723 | init_waitqueue_head(&usbvision->wait_stream); |
| 3724 | |
| 3725 | /* Allocate all buffers */ |
| 3726 | for (i = 0; i < USBVISION_NUMFRAMES; i++) { |
| 3727 | usbvision->frame[i].index = i; |
| 3728 | usbvision->frame[i].grabstate = FrameState_Unused; |
| 3729 | usbvision->frame[i].data = usbvision->fbuf + |
| 3730 | i * MAX_FRAME_SIZE; |
| 3731 | /* |
| 3732 | * Set default sizes in case IOCTL |
| 3733 | * (VIDIOCMCAPTURE) |
| 3734 | * is not used (using read() instead). |
| 3735 | */ |
| 3736 | usbvision->stretch_width = 1; |
| 3737 | usbvision->stretch_height = 1; |
| 3738 | usbvision->frame[i].width = usbvision->curwidth; |
| 3739 | usbvision->frame[i].height = usbvision->curheight; |
| 3740 | usbvision->frame[i].bytes_read = 0; |
| 3741 | } |
| 3742 | if (dga) { //set default for DGA |
| 3743 | usbvision->overlay_frame.grabstate = FrameState_Unused; |
| 3744 | usbvision->overlay_frame.scanstate = ScanState_Scanning; |
| 3745 | usbvision->overlay_frame.data = NULL; |
| 3746 | usbvision->overlay_frame.width = usbvision->curwidth; |
| 3747 | usbvision->overlay_frame.height = usbvision->curheight; |
| 3748 | usbvision->overlay_frame.bytes_read = 0; |
| 3749 | } |
| 3750 | for (i = 0; i < USBVISION_NUMSBUF; i++) { |
| 3751 | usbvision->sbuf[i].data = kzalloc(sb_size, GFP_KERNEL); |
| 3752 | if (usbvision->sbuf[i].data == NULL) { |
| 3753 | err("%s: unable to allocate %d bytes for sbuf", __FUNCTION__, sb_size); |
| 3754 | errCode = -ENOMEM; |
| 3755 | break; |
| 3756 | } |
| 3757 | } |
| 3758 | } |
| 3759 | if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) { |
| 3760 | int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2; |
| 3761 | usbvision->IntraFrameBuffer = vmalloc(IFB_size); |
| 3762 | if (usbvision->IntraFrameBuffer == NULL) { |
| 3763 | err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size); |
| 3764 | errCode = -ENOMEM; |
| 3765 | } |
| 3766 | |
| 3767 | } |
| 3768 | if (errCode) { |
| 3769 | /* Have to free all that memory */ |
| 3770 | if (usbvision->fbuf != NULL) { |
| 3771 | usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size); |
| 3772 | usbvision->fbuf = NULL; |
| 3773 | } |
| 3774 | if (usbvision->scratch != NULL) { |
| 3775 | vfree(usbvision->scratch); |
| 3776 | usbvision->scratch = NULL; |
| 3777 | } |
| 3778 | for (i = 0; i < USBVISION_NUMSBUF; i++) { |
| 3779 | if (usbvision->sbuf[i].data != NULL) { |
| 3780 | kfree(usbvision->sbuf[i].data); |
| 3781 | usbvision->sbuf[i].data = NULL; |
| 3782 | } |
| 3783 | } |
| 3784 | if (usbvision->IntraFrameBuffer != NULL) { |
| 3785 | vfree(usbvision->IntraFrameBuffer); |
| 3786 | usbvision->IntraFrameBuffer = NULL; |
| 3787 | } |
| 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | /* If so far no errors then we shall start the camera */ |
| 3792 | if (!errCode) { |
| 3793 | down(&usbvision->lock); |
| 3794 | if (usbvision->power == 0) { |
| 3795 | usbvision_power_on(usbvision); |
| 3796 | usbvision_init_i2c(usbvision); |
| 3797 | } |
| 3798 | |
| 3799 | /* Send init sequence only once, it's large! */ |
| 3800 | if (!usbvision->initialized) { |
| 3801 | int setup_ok = 0; |
| 3802 | setup_ok = usbvision_setup(usbvision); |
| 3803 | if (setup_ok) |
| 3804 | usbvision->initialized = 1; |
| 3805 | else |
| 3806 | errCode = -EBUSY; |
| 3807 | } |
| 3808 | |
| 3809 | if (!errCode) { |
| 3810 | usbvision_begin_streaming(usbvision); |
| 3811 | errCode = usbvision_init_isoc(usbvision); |
| 3812 | usbvision->user++; |
| 3813 | } |
| 3814 | else { |
| 3815 | if (PowerOnAtOpen) { |
| 3816 | usbvision_i2c_usb_del_bus(&usbvision->i2c_adap); |
| 3817 | usbvision_power_off(usbvision); |
| 3818 | usbvision->initialized = 0; |
| 3819 | } |
| 3820 | } |
| 3821 | up(&usbvision->lock); |
| 3822 | } |
| 3823 | |
| 3824 | if (errCode) { |
| 3825 | } |
| 3826 | |
| 3827 | /* prepare queues */ |
| 3828 | usbvision_empty_framequeues(usbvision); |
| 3829 | |
| 3830 | PDEBUG(DBG_IO, "success"); |
| 3831 | return errCode; |
| 3832 | } |
| 3833 | |
| 3834 | /* |
| 3835 | * usbvision_v4l2_close() |
| 3836 | * |
| 3837 | * This is part of Video 4 Linux API. The procedure |
| 3838 | * stops streaming and deallocates all buffers that were earlier |
| 3839 | * allocated in usbvision_v4l2_open(). |
| 3840 | * |
| 3841 | */ |
| 3842 | static int usbvision_v4l2_close(struct inode *inode, struct file *file) |
| 3843 | { |
| 3844 | struct video_device *dev = video_devdata(file); |
| 3845 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 3846 | int i; |
| 3847 | |
| 3848 | PDEBUG(DBG_IO, "close"); |
| 3849 | down(&usbvision->lock); |
| 3850 | |
| 3851 | usbvision_audio_off(usbvision); |
| 3852 | usbvision_restart_isoc(usbvision); |
| 3853 | usbvision_stop_isoc(usbvision); |
| 3854 | |
| 3855 | if (usbvision->IntraFrameBuffer != NULL) { |
| 3856 | vfree(usbvision->IntraFrameBuffer); |
| 3857 | usbvision->IntraFrameBuffer = NULL; |
| 3858 | } |
| 3859 | |
| 3860 | usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size); |
| 3861 | vfree(usbvision->scratch); |
| 3862 | for (i = 0; i < USBVISION_NUMSBUF; i++) |
| 3863 | kfree(usbvision->sbuf[i].data); |
| 3864 | |
| 3865 | usbvision->user--; |
| 3866 | |
| 3867 | if (PowerOnAtOpen) { |
| 3868 | mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME); |
| 3869 | usbvision->initialized = 0; |
| 3870 | } |
| 3871 | |
| 3872 | up(&usbvision->lock); |
| 3873 | |
| 3874 | if (usbvision->remove_pending) { |
| 3875 | info("%s: Final disconnect", __FUNCTION__); |
| 3876 | usbvision_release(usbvision); |
| 3877 | } |
| 3878 | |
| 3879 | PDEBUG(DBG_IO, "success"); |
| 3880 | |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
| 3885 | |
| 3886 | /* |
| 3887 | * usbvision_ioctl() |
| 3888 | * |
| 3889 | * This is part of Video 4 Linux API. The procedure handles ioctl() calls. |
| 3890 | * |
| 3891 | */ |
| 3892 | static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file, |
| 3893 | unsigned int cmd, void *arg) |
| 3894 | { |
| 3895 | struct video_device *dev = video_devdata(file); |
| 3896 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 3897 | |
| 3898 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 3899 | return -EFAULT; |
| 3900 | |
| 3901 | // if (debug & DBG_IOCTL) v4l_printk_ioctl(cmd); |
| 3902 | |
| 3903 | switch (cmd) { |
| 3904 | case UVIOCSREG: |
| 3905 | { |
| 3906 | struct usbvision_reg *usbvision_reg = arg; |
| 3907 | int errCode; |
| 3908 | |
| 3909 | errCode = usbvision_write_reg(usbvision, usbvision_reg->addr, usbvision_reg->value); |
| 3910 | |
| 3911 | if (errCode < 0) { |
| 3912 | err("%s: UVIOCSREG failed: error %d", __FUNCTION__, errCode); |
| 3913 | } |
| 3914 | else { |
| 3915 | PDEBUG(DBG_IOCTL, "UVIOCSREG addr=0x%02X, value=0x%02X", |
| 3916 | usbvision_reg->addr, usbvision_reg->value); |
| 3917 | errCode = 0; |
| 3918 | } |
| 3919 | return errCode; |
| 3920 | } |
| 3921 | case UVIOCGREG: |
| 3922 | { |
| 3923 | struct usbvision_reg *usbvision_reg = arg; |
| 3924 | int errCode; |
| 3925 | |
| 3926 | errCode = usbvision_read_reg(usbvision, usbvision_reg->addr); |
| 3927 | |
| 3928 | if (errCode < 0) { |
| 3929 | err("%s: UVIOCGREG failed: error %d", __FUNCTION__, errCode); |
| 3930 | } |
| 3931 | else { |
| 3932 | usbvision_reg->value=(unsigned char)errCode; |
| 3933 | PDEBUG(DBG_IOCTL, "UVIOCGREG addr=0x%02X, value=0x%02X", |
| 3934 | usbvision_reg->addr, usbvision_reg->value); |
| 3935 | errCode = 0; // No error |
| 3936 | } |
| 3937 | return errCode; |
| 3938 | } |
| 3939 | case VIDIOC_QUERYCAP: |
| 3940 | { |
| 3941 | struct v4l2_capability *vc=arg; |
| 3942 | |
| 3943 | memset(vc, 0, sizeof(*vc)); |
| 3944 | strlcpy(vc->driver, "USBVision", sizeof(vc->driver)); |
| 3945 | strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString, |
| 3946 | sizeof(vc->card)); |
| 3947 | strlcpy(vc->bus_info, usbvision->dev->dev.bus_id, |
| 3948 | sizeof(vc->bus_info)); |
| 3949 | vc->version = USBVISION_DRIVER_VERSION; |
| 3950 | vc->capabilities = V4L2_CAP_VIDEO_CAPTURE | |
| 3951 | V4L2_CAP_AUDIO | |
| 3952 | V4L2_CAP_READWRITE | |
| 3953 | V4L2_CAP_STREAMING | |
| 3954 | (dga ? (V4L2_FBUF_CAP_LIST_CLIPPING | V4L2_CAP_VIDEO_OVERLAY) : 0) | |
| 3955 | (usbvision->have_tuner ? V4L2_CAP_TUNER : 0); |
| 3956 | PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP"); |
| 3957 | return 0; |
| 3958 | } |
| 3959 | case VIDIOC_ENUMINPUT: |
| 3960 | { |
| 3961 | struct v4l2_input *vi = arg; |
| 3962 | int chan; |
| 3963 | |
| 3964 | if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) ) |
| 3965 | return -EINVAL; |
| 3966 | if (usbvision->have_tuner) { |
| 3967 | chan = vi->index; |
| 3968 | } |
| 3969 | else { |
| 3970 | chan = vi->index + 1; //skip Television string |
| 3971 | } |
| 3972 | switch(chan) { |
| 3973 | case 0: |
| 3974 | if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) { |
| 3975 | strcpy(vi->name, "White Video Input"); |
| 3976 | } |
| 3977 | else { |
| 3978 | strcpy(vi->name, "Television"); |
| 3979 | vi->type = V4L2_INPUT_TYPE_TUNER; |
| 3980 | vi->audioset = 1; |
| 3981 | vi->tuner = chan; |
| 3982 | vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM; |
| 3983 | } |
| 3984 | break; |
| 3985 | case 1: |
| 3986 | vi->type = V4L2_INPUT_TYPE_CAMERA; |
| 3987 | if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) { |
| 3988 | strcpy(vi->name, "Green Video Input"); |
| 3989 | } |
| 3990 | else { |
| 3991 | strcpy(vi->name, "Composite Video Input"); |
| 3992 | } |
| 3993 | vi->std = V4L2_STD_PAL; |
| 3994 | break; |
| 3995 | case 2: |
| 3996 | vi->type = V4L2_INPUT_TYPE_CAMERA; |
| 3997 | if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) { |
| 3998 | strcpy(vi->name, "Yellow Video Input"); |
| 3999 | } |
| 4000 | else { |
| 4001 | strcpy(vi->name, "S-Video Input"); |
| 4002 | } |
| 4003 | vi->std = V4L2_STD_PAL; |
| 4004 | break; |
| 4005 | case 3: |
| 4006 | vi->type = V4L2_INPUT_TYPE_CAMERA; |
| 4007 | strcpy(vi->name, "Red Video Input"); |
| 4008 | vi->std = V4L2_STD_PAL; |
| 4009 | break; |
| 4010 | } |
| 4011 | 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); |
| 4012 | return 0; |
| 4013 | } |
| 4014 | case VIDIOC_ENUMSTD: |
| 4015 | { |
| 4016 | struct v4l2_standard *e = arg; |
| 4017 | unsigned int i; |
| 4018 | int ret; |
| 4019 | |
| 4020 | i = e->index; |
| 4021 | if (i >= TVNORMS) |
| 4022 | return -EINVAL; |
| 4023 | ret = v4l2_video_std_construct(e, tvnorms[e->index].id, |
| 4024 | tvnorms[e->index].name); |
| 4025 | e->index = i; |
| 4026 | if (ret < 0) |
| 4027 | return ret; |
| 4028 | return 0; |
| 4029 | } |
| 4030 | case VIDIOC_G_INPUT: |
| 4031 | { |
| 4032 | int *input = arg; |
| 4033 | *input = usbvision->ctl_input; |
| 4034 | return 0; |
| 4035 | } |
| 4036 | case VIDIOC_S_INPUT: |
| 4037 | { |
| 4038 | int *input = arg; |
| 4039 | if ((*input >= usbvision->video_inputs) || (*input < 0) ) |
| 4040 | return -EINVAL; |
| 4041 | usbvision->ctl_input = *input; |
| 4042 | |
| 4043 | down(&usbvision->lock); |
| 4044 | usbvision_muxsel(usbvision, usbvision->ctl_input); |
| 4045 | usbvision_set_input(usbvision); |
| 4046 | usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight); |
| 4047 | up(&usbvision->lock); |
| 4048 | return 0; |
| 4049 | } |
| 4050 | case VIDIOC_G_STD: |
| 4051 | { |
| 4052 | v4l2_std_id *id = arg; |
| 4053 | |
| 4054 | *id = usbvision->tvnorm->id; |
| 4055 | |
| 4056 | PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name); |
| 4057 | return 0; |
| 4058 | } |
| 4059 | case VIDIOC_S_STD: |
| 4060 | { |
| 4061 | v4l2_std_id *id = arg; |
| 4062 | unsigned int i; |
| 4063 | |
| 4064 | for (i = 0; i < TVNORMS; i++) |
| 4065 | if (*id == tvnorms[i].id) |
| 4066 | break; |
| 4067 | if (i == TVNORMS) |
| 4068 | for (i = 0; i < TVNORMS; i++) |
| 4069 | if (*id & tvnorms[i].id) |
| 4070 | break; |
| 4071 | if (i == TVNORMS) |
| 4072 | return -EINVAL; |
| 4073 | |
| 4074 | down(&usbvision->lock); |
| 4075 | usbvision->tvnorm = &tvnorms[i]; |
| 4076 | |
| 4077 | call_i2c_clients(usbvision, VIDIOC_S_STD, |
| 4078 | &usbvision->tvnorm->id); |
| 4079 | |
| 4080 | up(&usbvision->lock); |
| 4081 | |
| 4082 | PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name); |
| 4083 | return 0; |
| 4084 | } |
| 4085 | case VIDIOC_G_TUNER: |
| 4086 | { |
| 4087 | struct v4l2_tuner *vt = arg; |
| 4088 | |
| 4089 | if (!usbvision->have_tuner || vt->index) // Only tuner 0 |
| 4090 | return -EINVAL; |
| 4091 | strcpy(vt->name, "Television"); |
| 4092 | /* Let clients fill in the remainder of this struct */ |
| 4093 | call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt); |
| 4094 | |
| 4095 | PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc); |
| 4096 | return 0; |
| 4097 | } |
| 4098 | case VIDIOC_S_TUNER: |
| 4099 | { |
| 4100 | struct v4l2_tuner *vt = arg; |
| 4101 | |
| 4102 | // Only no or one tuner for now |
| 4103 | if (!usbvision->have_tuner || vt->index) |
| 4104 | return -EINVAL; |
| 4105 | /* let clients handle this */ |
| 4106 | call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt); |
| 4107 | |
| 4108 | PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER"); |
| 4109 | return 0; |
| 4110 | } |
| 4111 | case VIDIOC_G_FREQUENCY: |
| 4112 | { |
| 4113 | struct v4l2_frequency *freq = arg; |
| 4114 | |
| 4115 | freq->tuner = 0; // Only one tuner |
| 4116 | freq->type = V4L2_TUNER_ANALOG_TV; |
| 4117 | freq->frequency = usbvision->freq; |
| 4118 | PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency); |
| 4119 | return 0; |
| 4120 | } |
| 4121 | case VIDIOC_S_FREQUENCY: |
| 4122 | { |
| 4123 | struct v4l2_frequency *freq = arg; |
| 4124 | |
| 4125 | // Only no or one tuner for now |
| 4126 | if (!usbvision->have_tuner || freq->tuner) |
| 4127 | return -EINVAL; |
| 4128 | |
| 4129 | usbvision->freq = freq->frequency; |
| 4130 | call_i2c_clients(usbvision, cmd, freq); |
| 4131 | PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency); |
| 4132 | return 0; |
| 4133 | } |
| 4134 | case VIDIOC_G_AUDIO: |
| 4135 | { |
| 4136 | struct v4l2_audio *v = arg; |
| 4137 | memset(v,0, sizeof(v)); |
| 4138 | strcpy(v->name, "TV"); |
| 4139 | PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO"); |
| 4140 | // FIXME: no more processings ??? |
| 4141 | return 0; |
| 4142 | } |
| 4143 | case VIDIOC_S_AUDIO: |
| 4144 | { |
| 4145 | struct v4l2_audio *v = arg; |
| 4146 | if(v->index) { |
| 4147 | return -EINVAL; |
| 4148 | } |
| 4149 | PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO"); |
| 4150 | // FIXME: void function ??? |
| 4151 | return 0; |
| 4152 | } |
| 4153 | case VIDIOC_QUERYCTRL: |
| 4154 | { |
| 4155 | struct v4l2_queryctrl *ctrl = arg; |
| 4156 | int id=ctrl->id; |
| 4157 | |
| 4158 | memset(ctrl,0,sizeof(*ctrl)); |
| 4159 | ctrl->id=id; |
| 4160 | |
| 4161 | i2c_clients_command(&usbvision->i2c_adap, cmd, arg); |
| 4162 | |
| 4163 | if (ctrl->type) |
| 4164 | return 0; |
| 4165 | else |
| 4166 | return -EINVAL; |
| 4167 | |
| 4168 | PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type); |
| 4169 | } |
| 4170 | case VIDIOC_G_CTRL: |
| 4171 | { |
| 4172 | struct v4l2_control *ctrl = arg; |
| 4173 | |
| 4174 | PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value); |
| 4175 | call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl); |
| 4176 | return 0; |
| 4177 | } |
| 4178 | case VIDIOC_S_CTRL: |
| 4179 | { |
| 4180 | struct v4l2_control *ctrl = arg; |
| 4181 | |
| 4182 | PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value); |
| 4183 | call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl); |
| 4184 | return 0; |
| 4185 | } |
| 4186 | case VIDIOC_REQBUFS: |
| 4187 | { |
| 4188 | struct v4l2_requestbuffers *vr = arg; |
| 4189 | int ret; |
| 4190 | |
| 4191 | RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES); |
| 4192 | |
| 4193 | // Check input validity : the user must do a VIDEO CAPTURE and MMAP method. |
| 4194 | if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) || |
| 4195 | (vr->memory != V4L2_MEMORY_MMAP)) |
| 4196 | return -EINVAL; |
| 4197 | |
| 4198 | // FIXME : before this, we must control if buffers are still mapped. |
| 4199 | // Then interrupt streaming if so... |
| 4200 | if(usbvision->streaming == Stream_On) { |
| 4201 | if ((ret = usbvision_stream_interrupt(usbvision))) |
| 4202 | return ret; |
| 4203 | } |
| 4204 | |
| 4205 | usbvision_empty_framequeues(usbvision); |
| 4206 | |
| 4207 | usbvision->curFrame = NULL; |
| 4208 | |
| 4209 | PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count); |
| 4210 | return 0; |
| 4211 | } |
| 4212 | case VIDIOC_QUERYBUF: |
| 4213 | { |
| 4214 | struct v4l2_buffer *vb = arg; |
| 4215 | struct usbvision_frame *frame; |
| 4216 | |
| 4217 | // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called) |
| 4218 | |
| 4219 | if(vb->type != V4L2_CAP_VIDEO_CAPTURE) { |
| 4220 | return -EINVAL; |
| 4221 | } |
| 4222 | if(vb->index>=USBVISION_NUMFRAMES) { |
| 4223 | return -EINVAL; |
| 4224 | } |
| 4225 | // Updating the corresponding frame state |
| 4226 | vb->flags = 0; |
| 4227 | frame = &usbvision->frame[vb->index]; |
| 4228 | if(frame->grabstate >= FrameState_Ready) |
| 4229 | vb->flags |= V4L2_BUF_FLAG_QUEUED; |
| 4230 | if(frame->grabstate >= FrameState_Done) |
| 4231 | vb->flags |= V4L2_BUF_FLAG_DONE; |
| 4232 | if(frame->grabstate == FrameState_Unused) |
| 4233 | vb->flags |= V4L2_BUF_FLAG_MAPPED; |
| 4234 | vb->memory = V4L2_MEMORY_MMAP; |
| 4235 | if(vb->index == 0) { |
| 4236 | vb->m.offset = 0; |
| 4237 | } |
| 4238 | else { |
| 4239 | vb->m.offset = MAX_FRAME_SIZE; |
| 4240 | } |
| 4241 | vb->memory = V4L2_MEMORY_MMAP; |
| 4242 | vb->field = V4L2_FIELD_NONE; |
| 4243 | vb->length = MAX_FRAME_SIZE; |
| 4244 | vb->timestamp = usbvision->frame[vb->index].timestamp; |
| 4245 | vb->sequence = usbvision->frame[vb->index].sequence; |
| 4246 | return 0; |
| 4247 | } |
| 4248 | case VIDIOC_QBUF: |
| 4249 | { |
| 4250 | struct v4l2_buffer *vb = arg; |
| 4251 | struct usbvision_frame *frame; |
| 4252 | unsigned long lock_flags; |
| 4253 | |
| 4254 | // FIXME : works only on VIDEO_CAPTURE MODE, MMAP. |
| 4255 | if(vb->type != V4L2_CAP_VIDEO_CAPTURE) { |
| 4256 | return -EINVAL; |
| 4257 | } |
| 4258 | if(vb->index>=USBVISION_NUMFRAMES) { |
| 4259 | return -EINVAL; |
| 4260 | } |
| 4261 | |
| 4262 | frame = &usbvision->frame[vb->index]; |
| 4263 | |
| 4264 | if (frame->grabstate != FrameState_Unused) { |
| 4265 | return -EAGAIN; |
| 4266 | } |
| 4267 | |
| 4268 | /* Mark it as ready and enqueue frame */ |
| 4269 | frame->grabstate = FrameState_Ready; |
| 4270 | frame->scanstate = ScanState_Scanning; |
| 4271 | frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */ |
| 4272 | |
| 4273 | vb->flags &= ~V4L2_BUF_FLAG_DONE; |
| 4274 | |
| 4275 | /* set v4l2_format index */ |
| 4276 | frame->v4l2_format = usbvision->palette; |
| 4277 | |
| 4278 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
| 4279 | list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue); |
| 4280 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
| 4281 | |
| 4282 | PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index); |
| 4283 | return 0; |
| 4284 | } |
| 4285 | case VIDIOC_DQBUF: |
| 4286 | { |
| 4287 | struct v4l2_buffer *vb = arg; |
| 4288 | int ret; |
| 4289 | struct usbvision_frame *f; |
| 4290 | unsigned long lock_flags; |
| 4291 | |
| 4292 | if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 4293 | return -EINVAL; |
| 4294 | |
| 4295 | if (list_empty(&(usbvision->outqueue))) { |
| 4296 | if (usbvision->streaming == Stream_Idle) |
| 4297 | return -EINVAL; |
| 4298 | ret = wait_event_interruptible |
| 4299 | (usbvision->wait_frame, |
| 4300 | !list_empty(&(usbvision->outqueue))); |
| 4301 | if (ret) |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
| 4305 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
| 4306 | f = list_entry(usbvision->outqueue.next, |
| 4307 | struct usbvision_frame, frame); |
| 4308 | list_del(usbvision->outqueue.next); |
| 4309 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
| 4310 | |
| 4311 | f->grabstate = FrameState_Unused; |
| 4312 | |
| 4313 | vb->memory = V4L2_MEMORY_MMAP; |
| 4314 | vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE; |
| 4315 | vb->index = f->index; |
| 4316 | vb->sequence = f->sequence; |
| 4317 | vb->timestamp = f->timestamp; |
| 4318 | vb->field = V4L2_FIELD_NONE; |
| 4319 | vb->bytesused = f->scanlength; |
| 4320 | |
| 4321 | if(debug & DBG_IOCTL) { // do not spend computing time for debug stuff if not needed ! |
| 4322 | if(usbvision_counter == 100) { |
| 4323 | PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF delta=%d",(unsigned)(jiffies-usbvision_timestamp)); |
| 4324 | usbvision_counter = 0; |
| 4325 | usbvision_timestamp = jiffies; |
| 4326 | } |
| 4327 | else { |
| 4328 | usbvision_counter++; |
| 4329 | } |
| 4330 | PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF frame #%d",vb->index); |
| 4331 | } |
| 4332 | return 0; |
| 4333 | } |
| 4334 | case VIDIOC_STREAMON: |
| 4335 | { |
| 4336 | int b=V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 4337 | |
| 4338 | usbvision->streaming = Stream_On; |
| 4339 | |
| 4340 | if(debug & DBG_IOCTL) usbvision_timestamp = jiffies; |
| 4341 | |
| 4342 | call_i2c_clients(usbvision,VIDIOC_STREAMON , &b); |
| 4343 | |
| 4344 | PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON"); |
| 4345 | |
| 4346 | return 0; |
| 4347 | } |
| 4348 | case VIDIOC_STREAMOFF: |
| 4349 | { |
| 4350 | int *type = arg; |
| 4351 | int b=V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 4352 | |
| 4353 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 4354 | return -EINVAL; |
| 4355 | |
| 4356 | if(usbvision->streaming == Stream_On) { |
| 4357 | usbvision_stream_interrupt(usbvision); |
| 4358 | // Stop all video streamings |
| 4359 | call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b); |
| 4360 | } |
| 4361 | usbvision_empty_framequeues(usbvision); |
| 4362 | |
| 4363 | PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF"); |
| 4364 | return 0; |
| 4365 | } |
| 4366 | case VIDIOC_G_FBUF: |
| 4367 | { |
| 4368 | struct v4l2_framebuffer *vb = arg; |
| 4369 | |
| 4370 | if (dga) { |
| 4371 | *vb = usbvision->vid_buf; |
| 4372 | } |
| 4373 | else { |
| 4374 | memset(vb, 0, sizeof(vb)); //dga not supported, not used |
| 4375 | } |
| 4376 | PDEBUG(DBG_IOCTL, "VIDIOC_G_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d", |
| 4377 | vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline); |
| 4378 | return 0; |
| 4379 | } |
| 4380 | case VIDIOC_S_FBUF: |
| 4381 | { |
| 4382 | struct v4l2_framebuffer *vb = arg; |
| 4383 | int formatIdx; |
| 4384 | |
| 4385 | if (dga == 0) { |
| 4386 | return -EINVAL; |
| 4387 | } |
| 4388 | |
| 4389 | if(!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_ADMIN)) { |
| 4390 | return -EPERM; |
| 4391 | } |
| 4392 | |
| 4393 | PDEBUG(DBG_IOCTL, "VIDIOC_S_FBUF base=%p, width=%d, height=%d, pixelformat=%d, bpl=%d", |
| 4394 | vb->base, vb->fmt.width, vb->fmt.height, vb->fmt.pixelformat,vb->fmt.bytesperline); |
| 4395 | |
| 4396 | for (formatIdx=0; formatIdx <= USBVISION_SUPPORTED_PALETTES; formatIdx++) { |
| 4397 | if (formatIdx == USBVISION_SUPPORTED_PALETTES) { |
| 4398 | return -EINVAL; // no matching video_format |
| 4399 | } |
| 4400 | if ((vb->fmt.pixelformat == usbvision_v4l2_format[formatIdx].format) && |
| 4401 | (usbvision_v4l2_format[formatIdx].supported)) { |
| 4402 | break; //found matching video_format |
| 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | if (vb->fmt.bytesperline<1) { |
| 4407 | return -EINVAL; |
| 4408 | } |
| 4409 | if (usbvision->overlay) { |
| 4410 | return -EBUSY; |
| 4411 | } |
| 4412 | down(&usbvision->lock); |
| 4413 | if (usbvision->overlay_base) { |
| 4414 | iounmap(usbvision->overlay_base); |
| 4415 | usbvision->vid_buf_valid = 0; |
| 4416 | } |
| 4417 | usbvision->overlay_base = ioremap((ulong)vb->base, vb->fmt.height * vb->fmt.bytesperline); |
| 4418 | if (usbvision->overlay_base) { |
| 4419 | usbvision->vid_buf_valid = 1; |
| 4420 | } |
| 4421 | usbvision->vid_buf = *vb; |
| 4422 | usbvision->overlay_frame.v4l2_format = usbvision_v4l2_format[formatIdx]; |
| 4423 | up(&usbvision->lock); |
| 4424 | return 0; |
| 4425 | } |
| 4426 | case VIDIOC_ENUM_FMT: |
| 4427 | { |
| 4428 | struct v4l2_fmtdesc *vfd = arg; |
| 4429 | |
| 4430 | if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) { |
| 4431 | return -EINVAL; |
| 4432 | } |
| 4433 | vfd->flags = 0; |
| 4434 | vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 4435 | strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc); |
| 4436 | vfd->pixelformat = usbvision_v4l2_format[vfd->index].format; |
| 4437 | memset(vfd->reserved, 0, sizeof(vfd->reserved)); |
| 4438 | return 0; |
| 4439 | } |
| 4440 | case VIDIOC_G_FMT: |
| 4441 | { |
| 4442 | struct v4l2_format *vf = arg; |
| 4443 | |
| 4444 | switch (vf->type) { |
| 4445 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 4446 | { |
| 4447 | vf->fmt.pix.width = usbvision->curwidth; |
| 4448 | vf->fmt.pix.height = usbvision->curheight; |
| 4449 | vf->fmt.pix.pixelformat = usbvision->palette.format; |
| 4450 | vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel; |
| 4451 | vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight; |
| 4452 | vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
| 4453 | vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */ |
| 4454 | } |
| 4455 | return 0; |
| 4456 | default: |
| 4457 | PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type); |
| 4458 | return -EINVAL; |
| 4459 | } |
| 4460 | PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d",vf->fmt.win.w.width, vf->fmt.win.w.height); |
| 4461 | return 0; |
| 4462 | } |
| 4463 | case VIDIOC_TRY_FMT: |
| 4464 | case VIDIOC_S_FMT: |
| 4465 | { |
| 4466 | struct v4l2_format *vf = arg; |
| 4467 | struct v4l2_clip *vc=NULL; |
| 4468 | int on,formatIdx; |
| 4469 | |
| 4470 | switch(vf->type) { |
| 4471 | case V4L2_BUF_TYPE_VIDEO_OVERLAY: |
| 4472 | { |
| 4473 | if (vf->fmt.win.clipcount>256) { |
| 4474 | return -EDOM; /* Too many clips! */ |
| 4475 | } |
| 4476 | // Do every clips. |
| 4477 | vc = vmalloc(sizeof(struct v4l2_clip)*(vf->fmt.win.clipcount+4)); |
| 4478 | if (vc == NULL) { |
| 4479 | return -ENOMEM; |
| 4480 | } |
| 4481 | if (vf->fmt.win.clipcount && copy_from_user(vc,vf->fmt.win.clips,sizeof(struct v4l2_clip)*vf->fmt.win.clipcount)) { |
| 4482 | return -EFAULT; |
| 4483 | } |
| 4484 | on = usbvision->overlay; // Save overlay state |
| 4485 | if (on) { |
| 4486 | usbvision_cap(usbvision, 0); |
| 4487 | } |
| 4488 | |
| 4489 | // strange, it seems xawtv sometimes calls us with 0 |
| 4490 | // width and/or height. Ignore these values |
| 4491 | if (vf->fmt.win.w.left == 0) { |
| 4492 | vf->fmt.win.w.left = usbvision->vid_win.fmt.win.w.left; |
| 4493 | } |
| 4494 | if (vf->fmt.win.w.top == 0) { |
| 4495 | vf->fmt.win.w.top = usbvision->vid_win.fmt.win.w.top; |
| 4496 | } |
| 4497 | |
| 4498 | // by now we are committed to the new data... |
| 4499 | down(&usbvision->lock); |
| 4500 | RESTRICT_TO_RANGE(vf->fmt.win.w.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH); |
| 4501 | RESTRICT_TO_RANGE(vf->fmt.win.w.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT); |
| 4502 | usbvision->vid_win = *vf; |
| 4503 | usbvision->overlay_frame.width = vf->fmt.win.w.width; |
| 4504 | usbvision->overlay_frame.height = vf->fmt.win.w.height; |
| 4505 | usbvision_set_output(usbvision, vf->fmt.win.w.width, vf->fmt.win.w.height); |
| 4506 | up(&usbvision->lock); |
| 4507 | |
| 4508 | // Impose display clips |
| 4509 | if (vf->fmt.win.w.left+vf->fmt.win.w.width > (unsigned int)usbvision->vid_buf.fmt.width) { |
| 4510 | 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); |
| 4511 | } |
| 4512 | if (vf->fmt.win.w.top+vf->fmt.win.w.height > (unsigned int)usbvision->vid_buf.fmt.height) { |
| 4513 | 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); |
| 4514 | } |
| 4515 | |
| 4516 | // built the requested clipping zones |
| 4517 | usbvision_built_overlay(usbvision, vf->fmt.win.clipcount, vc); |
| 4518 | vfree(vc); |
| 4519 | |
| 4520 | // restore overlay state |
| 4521 | if (on) { |
| 4522 | usbvision_cap(usbvision, 1); |
| 4523 | } |
| 4524 | usbvision->vid_win_valid = 1; |
| 4525 | PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT overlay x=%d, y=%d, w=%d, h=%d, chroma=%x, clips=%d", |
| 4526 | 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); |
| 4527 | return 0; |
| 4528 | } |
| 4529 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 4530 | { |
| 4531 | /* Find requested format in available ones */ |
| 4532 | for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) { |
| 4533 | if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) { |
| 4534 | usbvision->palette = usbvision_v4l2_format[formatIdx]; |
| 4535 | break; |
| 4536 | } |
| 4537 | } |
| 4538 | /* robustness */ |
| 4539 | if(formatIdx == USBVISION_SUPPORTED_PALETTES) { |
| 4540 | return -EINVAL; |
| 4541 | } |
| 4542 | RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH); |
| 4543 | RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT); |
| 4544 | // by now we are committed to the new data... |
| 4545 | down(&usbvision->lock); |
| 4546 | usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height); |
| 4547 | up(&usbvision->lock); |
| 4548 | |
| 4549 | PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s", |
| 4550 | vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc); |
| 4551 | return 0; |
| 4552 | } |
| 4553 | default: |
| 4554 | return -EINVAL; |
| 4555 | } |
| 4556 | } |
| 4557 | case VIDIOC_OVERLAY: |
| 4558 | { |
| 4559 | int *v = arg; |
| 4560 | |
| 4561 | if ( (dga == 0) && |
| 4562 | (usbvision->palette.format != V4L2_PIX_FMT_YVU420) && |
| 4563 | (usbvision->palette.format != V4L2_PIX_FMT_YUV422P) ) { |
| 4564 | PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY DGA disabled"); |
| 4565 | return -EINVAL; |
| 4566 | } |
| 4567 | |
| 4568 | if (*v == 0) { |
| 4569 | usbvision_cap(usbvision, 0); |
| 4570 | } |
| 4571 | else { |
| 4572 | // are VIDIOCSFBUF and VIDIOCSWIN done? |
| 4573 | if ((usbvision->vid_buf_valid == 0) || (usbvision->vid_win_valid == 0)) { |
| 4574 | PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY vid_buf_valid %d; vid_win_valid %d", |
| 4575 | usbvision->vid_buf_valid, usbvision->vid_win_valid); |
| 4576 | return -EINVAL; |
| 4577 | } |
| 4578 | usbvision_cap(usbvision, 1); |
| 4579 | } |
| 4580 | PDEBUG(DBG_IOCTL, "VIDIOC_OVERLAY %s", (*v)?"on":"off"); |
| 4581 | return 0; |
| 4582 | } |
| 4583 | default: |
| 4584 | return -ENOIOCTLCMD; |
| 4585 | } |
| 4586 | return 0; |
| 4587 | } |
| 4588 | |
| 4589 | static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file, |
| 4590 | unsigned int cmd, unsigned long arg) |
| 4591 | { |
| 4592 | return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl); |
| 4593 | } |
| 4594 | |
| 4595 | |
| 4596 | static ssize_t usbvision_v4l2_read(struct file *file, char *buf, |
| 4597 | size_t count, loff_t *ppos) |
| 4598 | { |
| 4599 | struct video_device *dev = video_devdata(file); |
| 4600 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 4601 | int noblock = file->f_flags & O_NONBLOCK; |
| 4602 | unsigned long lock_flags; |
| 4603 | |
| 4604 | int frmx = -1; |
| 4605 | int ret,i; |
| 4606 | struct usbvision_frame *frame; |
| 4607 | |
| 4608 | PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock); |
| 4609 | |
| 4610 | if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL)) |
| 4611 | return -EFAULT; |
| 4612 | |
| 4613 | /* no stream is running, make it running ! */ |
| 4614 | usbvision->streaming = Stream_On; |
| 4615 | call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL); |
| 4616 | |
| 4617 | /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */ |
| 4618 | for(i=0;i<USBVISION_NUMFRAMES;i++) { |
| 4619 | frame = &usbvision->frame[i]; |
| 4620 | if(frame->grabstate == FrameState_Unused) { |
| 4621 | /* Mark it as ready and enqueue frame */ |
| 4622 | frame->grabstate = FrameState_Ready; |
| 4623 | frame->scanstate = ScanState_Scanning; |
| 4624 | frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */ |
| 4625 | |
| 4626 | /* set v4l2_format index */ |
| 4627 | frame->v4l2_format = usbvision->palette; |
| 4628 | |
| 4629 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
| 4630 | list_add_tail(&frame->frame, &usbvision->inqueue); |
| 4631 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */ |
| 4636 | if (list_empty(&(usbvision->outqueue))) { |
| 4637 | if(noblock) |
| 4638 | return -EAGAIN; |
| 4639 | |
| 4640 | ret = wait_event_interruptible |
| 4641 | (usbvision->wait_frame, |
| 4642 | !list_empty(&(usbvision->outqueue))); |
| 4643 | if (ret) |
| 4644 | return ret; |
| 4645 | } |
| 4646 | |
| 4647 | spin_lock_irqsave(&usbvision->queue_lock, lock_flags); |
| 4648 | frame = list_entry(usbvision->outqueue.next, |
| 4649 | struct usbvision_frame, frame); |
| 4650 | list_del(usbvision->outqueue.next); |
| 4651 | spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags); |
| 4652 | |
| 4653 | if(debug & DBG_IOCTL) { // do not spend computing time for debug stuff if not needed ! |
| 4654 | if(usbvision_counter == 100) { |
| 4655 | PDEBUG(DBG_IOCTL, "VIDIOC_DQBUF delta=%d",(unsigned)(jiffies-usbvision_timestamp)); |
| 4656 | usbvision_counter = 0; |
| 4657 | usbvision_timestamp = jiffies; |
| 4658 | } |
| 4659 | else { |
| 4660 | usbvision_counter++; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | /* An error returns an empty frame */ |
| 4665 | if (frame->grabstate == FrameState_Error) { |
| 4666 | frame->bytes_read = 0; |
| 4667 | return 0; |
| 4668 | } |
| 4669 | |
| 4670 | PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__, |
| 4671 | frame->index, frame->bytes_read, frame->scanlength); |
| 4672 | |
| 4673 | /* copy bytes to user space; we allow for partials reads */ |
| 4674 | if ((count + frame->bytes_read) > (unsigned long)frame->scanlength) |
| 4675 | count = frame->scanlength - frame->bytes_read; |
| 4676 | |
| 4677 | if (copy_to_user(buf, frame->data + frame->bytes_read, count)) { |
| 4678 | return -EFAULT; |
| 4679 | } |
| 4680 | |
| 4681 | frame->bytes_read += count; |
| 4682 | PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__, |
| 4683 | (unsigned long)count, frame->bytes_read); |
| 4684 | |
| 4685 | // For now, forget the frame if it has not been read in one shot. |
| 4686 | /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */ |
| 4687 | frame->bytes_read = 0; |
| 4688 | |
| 4689 | /* Mark it as available to be used again. */ |
| 4690 | usbvision->frame[frmx].grabstate = FrameState_Unused; |
| 4691 | /* } */ |
| 4692 | |
| 4693 | return count; |
| 4694 | } |
| 4695 | |
| 4696 | static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma) |
| 4697 | { |
| 4698 | struct video_device *dev = video_devdata(file); |
| 4699 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 4700 | unsigned long start = vma->vm_start; |
| 4701 | unsigned long size = vma->vm_end-vma->vm_start; |
| 4702 | |
| 4703 | unsigned long page, pos; |
| 4704 | |
| 4705 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 4706 | return -EFAULT; |
| 4707 | |
| 4708 | if (size > (((USBVISION_NUMFRAMES * usbvision->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))) |
| 4709 | return -EINVAL; |
| 4710 | |
| 4711 | pos = (unsigned long) usbvision->fbuf; |
| 4712 | while (size > 0) { |
| 4713 | |
| 4714 | // Really ugly.... |
| 4715 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10) //Compatibility for 2.6.10+ kernels |
| 4716 | page = vmalloc_to_pfn((void *)pos); |
| 4717 | if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { |
| 4718 | return -EAGAIN; |
| 4719 | } |
| 4720 | #else //Compatibility for 2.6.0 - 2.6.9 kernels |
| 4721 | page = usbvision_kvirt_to_pa(pos); |
| 4722 | if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) { |
| 4723 | return -EAGAIN; |
| 4724 | } |
| 4725 | #endif |
| 4726 | start += PAGE_SIZE; |
| 4727 | pos += PAGE_SIZE; |
| 4728 | if (size > PAGE_SIZE) |
| 4729 | size -= PAGE_SIZE; |
| 4730 | else |
| 4731 | size = 0; |
| 4732 | } |
| 4733 | |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | |
| 4738 | /* |
| 4739 | * Here comes the stuff for radio on usbvision based devices |
| 4740 | * |
| 4741 | */ |
| 4742 | static int usbvision_radio_open(struct inode *inode, struct file *file) |
| 4743 | { |
| 4744 | struct video_device *dev = video_devdata(file); |
| 4745 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 4746 | struct v4l2_frequency freq; |
| 4747 | int errCode = 0; |
| 4748 | |
| 4749 | PDEBUG(DBG_RIO, "%s:", __FUNCTION__); |
| 4750 | |
| 4751 | down(&usbvision->lock); |
| 4752 | |
| 4753 | if (usbvision->user) { |
| 4754 | err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__); |
| 4755 | errCode = -EBUSY; |
| 4756 | } |
| 4757 | else { |
| 4758 | if(PowerOnAtOpen) { |
| 4759 | if (timer_pending(&usbvision->powerOffTimer)) { |
| 4760 | del_timer(&usbvision->powerOffTimer); |
| 4761 | } |
| 4762 | if (usbvision->power == 0) { |
| 4763 | usbvision_power_on(usbvision); |
| 4764 | usbvision_init_i2c(usbvision); |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | // If so far no errors then we shall start the radio |
| 4769 | usbvision->radio = 1; |
| 4770 | call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type); |
| 4771 | freq.frequency = 1517; //SWR3 @ 94.8MHz |
| 4772 | call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq); |
| 4773 | usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO); |
| 4774 | usbvision->user++; |
| 4775 | } |
| 4776 | |
| 4777 | if (errCode) { |
| 4778 | if (PowerOnAtOpen) { |
| 4779 | usbvision_i2c_usb_del_bus(&usbvision->i2c_adap); |
| 4780 | usbvision_power_off(usbvision); |
| 4781 | usbvision->initialized = 0; |
| 4782 | } |
| 4783 | } |
| 4784 | up(&usbvision->lock); |
| 4785 | return errCode; |
| 4786 | } |
| 4787 | |
| 4788 | |
| 4789 | static int usbvision_radio_close(struct inode *inode, struct file *file) |
| 4790 | { |
| 4791 | struct video_device *dev = video_devdata(file); |
| 4792 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 4793 | int errCode = 0; |
| 4794 | |
| 4795 | PDEBUG(DBG_RIO, ""); |
| 4796 | |
| 4797 | down(&usbvision->lock); |
| 4798 | |
| 4799 | usbvision_audio_off(usbvision); |
| 4800 | usbvision->radio=0; |
| 4801 | usbvision->user--; |
| 4802 | |
| 4803 | if (PowerOnAtOpen) { |
| 4804 | mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME); |
| 4805 | usbvision->initialized = 0; |
| 4806 | } |
| 4807 | |
| 4808 | up(&usbvision->lock); |
| 4809 | |
| 4810 | if (usbvision->remove_pending) { |
| 4811 | info("%s: Final disconnect", __FUNCTION__); |
| 4812 | usbvision_release(usbvision); |
| 4813 | } |
| 4814 | |
| 4815 | |
| 4816 | PDEBUG(DBG_RIO, "success"); |
| 4817 | |
| 4818 | return errCode; |
| 4819 | } |
| 4820 | |
| 4821 | static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file, |
| 4822 | unsigned int cmd, void *arg) |
| 4823 | { |
| 4824 | struct video_device *dev = video_devdata(file); |
| 4825 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 4826 | |
| 4827 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 4828 | return -EIO; |
| 4829 | |
| 4830 | switch (cmd) { |
| 4831 | /*************************** |
| 4832 | * V4L2 IOCTLs * |
| 4833 | ***************************/ |
| 4834 | case VIDIOC_QUERYCAP: |
| 4835 | { |
| 4836 | struct v4l2_capability *vc=arg; |
| 4837 | memset(vc, 0, sizeof(struct v4l2_capability)); |
| 4838 | strcpy(vc->driver,"usbvision radio"); |
| 4839 | strcpy(vc->card,usbvision->vcap.card); |
| 4840 | strcpy(vc->bus_info,"usb"); |
| 4841 | vc->version = USBVISION_DRIVER_VERSION; /* version */ |
| 4842 | vc->capabilities = V4L2_CAP_TUNER; /* capabilities */ |
| 4843 | PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__); |
| 4844 | return 0; |
| 4845 | } |
| 4846 | case VIDIOC_QUERYCTRL: |
| 4847 | { |
| 4848 | struct v4l2_queryctrl *qc = arg; |
| 4849 | switch(qc->id) |
| 4850 | { |
| 4851 | case V4L2_CID_AUDIO_VOLUME: |
| 4852 | case V4L2_CID_AUDIO_MUTE: |
| 4853 | return v4l2_ctrl_query_fill_std(qc); |
| 4854 | break; |
| 4855 | default: |
| 4856 | return -EINVAL; |
| 4857 | } |
| 4858 | return 0; |
| 4859 | } |
| 4860 | case VIDIOC_G_CTRL: |
| 4861 | { |
| 4862 | struct v4l2_control *ctrl = arg; |
| 4863 | PDEBUG(DBG_IOCTL, "VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value); |
| 4864 | switch(ctrl->id) { |
| 4865 | case V4L2_CID_AUDIO_VOLUME: |
| 4866 | /* ctrl->value = usbvision->volume; */ |
| 4867 | break; |
| 4868 | case V4L2_CID_AUDIO_MUTE: |
| 4869 | ctrl->value = usbvision->AudioMute; |
| 4870 | break; |
| 4871 | default: |
| 4872 | return -EINVAL; |
| 4873 | } |
| 4874 | return 0; |
| 4875 | } |
| 4876 | case VIDIOC_S_CTRL: |
| 4877 | { |
| 4878 | struct v4l2_control *ctrl = arg; |
| 4879 | call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl); |
| 4880 | |
| 4881 | PDEBUG(DBG_RIO, "%s: VIDIOC_S_CTRL id=%x value=%x", __FUNCTION__,ctrl->id,ctrl->value); |
| 4882 | return 0; |
| 4883 | } |
| 4884 | case VIDIOC_G_TUNER: |
| 4885 | { |
| 4886 | struct v4l2_tuner *vt = arg; |
| 4887 | |
| 4888 | if (!usbvision->have_tuner || vt->index) // Only tuner 0 |
| 4889 | return -EINVAL; |
| 4890 | strcpy(vt->name, "Radio"); |
| 4891 | vt->type = V4L2_TUNER_RADIO; |
| 4892 | vt->capability = V4L2_TUNER_CAP_STEREO; |
| 4893 | // japan: 76.0 MHz - 89.9 MHz |
| 4894 | // western europe: 87.5 MHz - 108.0 MHz |
| 4895 | // russia: 65.0 MHz - 108.0 MHz |
| 4896 | vt->rangelow = (int)(65*16);; |
| 4897 | vt->rangehigh = (int)(108*16); |
| 4898 | vt->audmode = V4L2_TUNER_MODE_STEREO; |
| 4899 | vt->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 4900 | call_i2c_clients(usbvision,VIDIOC_G_TUNER,&vt); |
| 4901 | |
| 4902 | PDEBUG(DBG_RIO, "%s: VIDIOC_G_TUNER signal=%d", __FUNCTION__, vt->signal); |
| 4903 | return 0; |
| 4904 | } |
| 4905 | case VIDIOC_S_TUNER: |
| 4906 | { |
| 4907 | struct v4l2_tuner *vt = arg; |
| 4908 | |
| 4909 | // Only channel 0 has a tuner |
| 4910 | if((vt->index) || (usbvision->channel)) { |
| 4911 | return -EINVAL; |
| 4912 | } |
| 4913 | PDEBUG(DBG_RIO, "%s: VIDIOC_S_TUNER", __FUNCTION__); |
| 4914 | return 0; |
| 4915 | } |
| 4916 | case VIDIOC_G_AUDIO: |
| 4917 | { |
| 4918 | struct v4l2_audio *va = arg; |
| 4919 | memset(va,0, sizeof(va)); |
| 4920 | va->capability = V4L2_AUDCAP_STEREO; |
| 4921 | strcpy(va->name, "Radio"); |
| 4922 | PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO"); |
| 4923 | return 0; |
| 4924 | } |
| 4925 | case VIDIOC_S_AUDIO: |
| 4926 | { |
| 4927 | struct v4l2_audio *v = arg; |
| 4928 | if(v->index) { |
| 4929 | return -EINVAL; |
| 4930 | } |
| 4931 | PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO"); |
| 4932 | // FIXME: void function ??? |
| 4933 | return 0; |
| 4934 | } |
| 4935 | case VIDIOC_G_FREQUENCY: |
| 4936 | { |
| 4937 | struct v4l2_frequency *freq = arg; |
| 4938 | freq->tuner = 0; // Only one tuner |
| 4939 | freq->type = V4L2_TUNER_RADIO; |
| 4940 | freq->frequency = usbvision->freq; |
| 4941 | PDEBUG(DBG_RIO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency); |
| 4942 | return 0; |
| 4943 | } |
| 4944 | case VIDIOC_S_FREQUENCY: |
| 4945 | { |
| 4946 | struct v4l2_frequency *freq = arg; |
| 4947 | usbvision->freq = freq->frequency; |
| 4948 | call_i2c_clients(usbvision, cmd, freq); |
| 4949 | PDEBUG(DBG_RIO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency); |
| 4950 | return 0; |
| 4951 | } |
| 4952 | |
| 4953 | /*************************** |
| 4954 | * V4L1 IOCTLs * |
| 4955 | ***************************/ |
| 4956 | case VIDIOCGCAP: |
| 4957 | { |
| 4958 | struct video_capability *vc = arg; |
| 4959 | |
| 4960 | memset(vc, 0, sizeof(struct video_capability)); |
| 4961 | strcpy(vc->name,usbvision->vcap.card); |
| 4962 | vc->type = VID_TYPE_TUNER; |
| 4963 | vc->channels = 1; |
| 4964 | vc->audios = 1; |
| 4965 | PDEBUG(DBG_RIO, "%s: VIDIOCGCAP", __FUNCTION__); |
| 4966 | return 0; |
| 4967 | } |
| 4968 | case VIDIOCGTUNER: |
| 4969 | { |
| 4970 | struct video_tuner *vt = arg; |
| 4971 | |
| 4972 | if((vt->tuner) || (usbvision->channel)) { /* Only tuner 0 */ |
| 4973 | return -EINVAL; |
| 4974 | } |
| 4975 | strcpy(vt->name, "Radio"); |
| 4976 | // japan: 76.0 MHz - 89.9 MHz |
| 4977 | // western europe: 87.5 MHz - 108.0 MHz |
| 4978 | // russia: 65.0 MHz - 108.0 MHz |
| 4979 | vt->rangelow=(int)(65*16); |
| 4980 | vt->rangehigh=(int)(108*16); |
| 4981 | vt->flags= 0; |
| 4982 | vt->mode = 0; |
| 4983 | call_i2c_clients(usbvision,cmd,vt); |
| 4984 | PDEBUG(DBG_RIO, "%s: VIDIOCGTUNER signal=%d", __FUNCTION__, vt->signal); |
| 4985 | return 0; |
| 4986 | } |
| 4987 | case VIDIOCSTUNER: |
| 4988 | { |
| 4989 | struct video_tuner *vt = arg; |
| 4990 | |
| 4991 | // Only channel 0 has a tuner |
| 4992 | if((vt->tuner) || (usbvision->channel)) { |
| 4993 | return -EINVAL; |
| 4994 | } |
| 4995 | PDEBUG(DBG_RIO, "%s: VIDIOCSTUNER", __FUNCTION__); |
| 4996 | return 0; |
| 4997 | } |
| 4998 | case VIDIOCGAUDIO: |
| 4999 | { |
| 5000 | struct video_audio *va = arg; |
| 5001 | memset(va,0, sizeof(struct video_audio)); |
| 5002 | call_i2c_clients(usbvision, cmd, va); |
| 5003 | va->flags|=VIDEO_AUDIO_MUTABLE; |
| 5004 | va->volume=1; |
| 5005 | va->step=1; |
| 5006 | strcpy(va->name, "Radio"); |
| 5007 | PDEBUG(DBG_RIO, "%s: VIDIOCGAUDIO", __FUNCTION__); |
| 5008 | return 0; |
| 5009 | } |
| 5010 | case VIDIOCSAUDIO: |
| 5011 | { |
| 5012 | struct video_audio *va = arg; |
| 5013 | if(va->audio) { |
| 5014 | return -EINVAL; |
| 5015 | } |
| 5016 | |
| 5017 | if(va->flags & VIDEO_AUDIO_MUTE) { |
| 5018 | if (usbvision_audio_mute(usbvision)) { |
| 5019 | return -EFAULT; |
| 5020 | } |
| 5021 | } |
| 5022 | else { |
| 5023 | if (usbvision_audio_on(usbvision)) { |
| 5024 | return -EFAULT; |
| 5025 | } |
| 5026 | } |
| 5027 | PDEBUG(DBG_RIO, "%s: VIDIOCSAUDIO flags=0x%x)", __FUNCTION__, va->flags); |
| 5028 | return 0; |
| 5029 | } |
| 5030 | case VIDIOCGFREQ: |
| 5031 | { |
| 5032 | unsigned long *freq = arg; |
| 5033 | |
| 5034 | *freq = usbvision->freq; |
| 5035 | PDEBUG(DBG_RIO, "%s: VIDIOCGFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | case VIDIOCSFREQ: |
| 5039 | { |
| 5040 | unsigned long *freq = arg; |
| 5041 | |
| 5042 | usbvision->freq = *freq; |
| 5043 | call_i2c_clients(usbvision, cmd, freq); |
| 5044 | PDEBUG(DBG_RIO, "%s: VIDIOCSFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4); |
| 5045 | return 0; |
| 5046 | } |
| 5047 | default: |
| 5048 | { |
| 5049 | PDEBUG(DBG_RIO, "%s: Unknown command %x", __FUNCTION__, cmd); |
| 5050 | return -ENOIOCTLCMD; |
| 5051 | } |
| 5052 | } |
| 5053 | return 0; |
| 5054 | } |
| 5055 | |
| 5056 | |
| 5057 | static int usbvision_radio_ioctl(struct inode *inode, struct file *file, |
| 5058 | unsigned int cmd, unsigned long arg) |
| 5059 | { |
| 5060 | return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl); |
| 5061 | } |
| 5062 | |
| 5063 | |
| 5064 | /* |
| 5065 | * Here comes the stuff for vbi on usbvision based devices |
| 5066 | * |
| 5067 | */ |
| 5068 | static int usbvision_vbi_open(struct inode *inode, struct file *file) |
| 5069 | { |
| 5070 | struct video_device *dev = video_devdata(file); |
| 5071 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 5072 | unsigned long freq; |
| 5073 | int errCode = 0; |
| 5074 | |
| 5075 | PDEBUG(DBG_RIO, "%s:", __FUNCTION__); |
| 5076 | |
| 5077 | down(&usbvision->lock); |
| 5078 | |
| 5079 | if (usbvision->user) { |
| 5080 | err("%s: Someone tried to open an already opened USBVision VBI!", __FUNCTION__); |
| 5081 | errCode = -EBUSY; |
| 5082 | } |
| 5083 | else { |
| 5084 | if(PowerOnAtOpen) { |
| 5085 | if (timer_pending(&usbvision->powerOffTimer)) { |
| 5086 | del_timer(&usbvision->powerOffTimer); |
| 5087 | } |
| 5088 | if (usbvision->power == 0) { |
| 5089 | usbvision_power_on(usbvision); |
| 5090 | usbvision_init_i2c(usbvision); |
| 5091 | } |
| 5092 | } |
| 5093 | |
| 5094 | // If so far no errors then we shall start the vbi device |
| 5095 | //usbvision->vbi = 1; |
| 5096 | call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type); |
| 5097 | freq = 1517; //SWR3 @ 94.8MHz |
| 5098 | call_i2c_clients(usbvision, VIDIOCSFREQ, &freq); |
| 5099 | usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO); |
| 5100 | usbvision->user++; |
| 5101 | } |
| 5102 | |
| 5103 | if (errCode) { |
| 5104 | if (PowerOnAtOpen) { |
| 5105 | usbvision_i2c_usb_del_bus(&usbvision->i2c_adap); |
| 5106 | usbvision_power_off(usbvision); |
| 5107 | usbvision->initialized = 0; |
| 5108 | } |
| 5109 | } |
| 5110 | up(&usbvision->lock); |
| 5111 | return errCode; |
| 5112 | } |
| 5113 | |
| 5114 | static int usbvision_vbi_close(struct inode *inode, struct file *file) |
| 5115 | { |
| 5116 | struct video_device *dev = video_devdata(file); |
| 5117 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 5118 | int errCode = 0; |
| 5119 | |
| 5120 | PDEBUG(DBG_RIO, ""); |
| 5121 | |
| 5122 | down(&usbvision->lock); |
| 5123 | |
| 5124 | usbvision_audio_off(usbvision); |
| 5125 | usbvision->vbi=0; |
| 5126 | usbvision->user--; |
| 5127 | |
| 5128 | if (PowerOnAtOpen) { |
| 5129 | mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME); |
| 5130 | usbvision->initialized = 0; |
| 5131 | } |
| 5132 | |
| 5133 | up(&usbvision->lock); |
| 5134 | |
| 5135 | if (usbvision->remove_pending) { |
| 5136 | info("%s: Final disconnect", __FUNCTION__); |
| 5137 | usbvision_release(usbvision); |
| 5138 | } |
| 5139 | |
| 5140 | |
| 5141 | PDEBUG(DBG_RIO, "success"); |
| 5142 | |
| 5143 | return errCode; |
| 5144 | } |
| 5145 | |
| 5146 | static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file, |
| 5147 | unsigned int cmd, void *arg) |
| 5148 | { |
| 5149 | struct video_device *dev = video_devdata(file); |
| 5150 | struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev); |
| 5151 | |
| 5152 | if (!USBVISION_IS_OPERATIONAL(usbvision)) |
| 5153 | return -EIO; |
| 5154 | |
| 5155 | switch (cmd) { |
| 5156 | case VIDIOC_QUERYCAP: |
| 5157 | { |
| 5158 | struct v4l2_capability *vc=arg; |
| 5159 | memset(vc, 0, sizeof(struct v4l2_capability)); |
| 5160 | strcpy(vc->driver,"usbvision vbi"); |
| 5161 | strcpy(vc->card,usbvision->vcap.card); |
| 5162 | strcpy(vc->bus_info,"usb"); |
| 5163 | vc->version = USBVISION_DRIVER_VERSION; /* version */ |
| 5164 | vc->capabilities = V4L2_CAP_VBI_CAPTURE; /* capabilities */ |
| 5165 | PDEBUG(DBG_RIO, "%s: VIDIOC_QUERYCAP", __FUNCTION__); |
| 5166 | return 0; |
| 5167 | } |
| 5168 | case VIDIOCGTUNER: |
| 5169 | { |
| 5170 | struct video_tuner *vt = arg; |
| 5171 | |
| 5172 | if((vt->tuner) || (usbvision->channel)) { /* Only tuner 0 */ |
| 5173 | return -EINVAL; |
| 5174 | } |
| 5175 | strcpy(vt->name, "vbi"); |
| 5176 | // japan: 76.0 MHz - 89.9 MHz |
| 5177 | // western europe: 87.5 MHz - 108.0 MHz |
| 5178 | // russia: 65.0 MHz - 108.0 MHz |
| 5179 | vt->rangelow=(int)(65*16); |
| 5180 | vt->rangehigh=(int)(108*16); |
| 5181 | vt->flags= 0; |
| 5182 | vt->mode = 0; |
| 5183 | call_i2c_clients(usbvision,cmd,vt); |
| 5184 | PDEBUG(DBG_RIO, "%s: VIDIOCGTUNER signal=%d", __FUNCTION__, vt->signal); |
| 5185 | return 0; |
| 5186 | } |
| 5187 | case VIDIOCSTUNER: |
| 5188 | { |
| 5189 | struct video_tuner *vt = arg; |
| 5190 | |
| 5191 | // Only channel 0 has a tuner |
| 5192 | if((vt->tuner) || (usbvision->channel)) { |
| 5193 | return -EINVAL; |
| 5194 | } |
| 5195 | PDEBUG(DBG_RIO, "%s: VIDIOCSTUNER", __FUNCTION__); |
| 5196 | return 0; |
| 5197 | } |
| 5198 | case VIDIOCGAUDIO: |
| 5199 | { |
| 5200 | struct video_audio *va = arg; |
| 5201 | memset(va,0, sizeof(struct video_audio)); |
| 5202 | call_i2c_clients(usbvision, cmd, va); |
| 5203 | va->flags|=VIDEO_AUDIO_MUTABLE; |
| 5204 | va->volume=1; |
| 5205 | va->step=1; |
| 5206 | strcpy(va->name, "vbi"); |
| 5207 | PDEBUG(DBG_RIO, "%s: VIDIOCGAUDIO", __FUNCTION__); |
| 5208 | return 0; |
| 5209 | } |
| 5210 | case VIDIOCSAUDIO: |
| 5211 | { |
| 5212 | struct video_audio *va = arg; |
| 5213 | if(va->audio) { |
| 5214 | return -EINVAL; |
| 5215 | } |
| 5216 | |
| 5217 | if(va->flags & VIDEO_AUDIO_MUTE) { |
| 5218 | if (usbvision_audio_mute(usbvision)) { |
| 5219 | return -EFAULT; |
| 5220 | } |
| 5221 | } |
| 5222 | else { |
| 5223 | if (usbvision_audio_on(usbvision)) { |
| 5224 | return -EFAULT; |
| 5225 | } |
| 5226 | } |
| 5227 | PDEBUG(DBG_RIO, "%s: VIDIOCSAUDIO flags=0x%x)", __FUNCTION__, va->flags); |
| 5228 | return 0; |
| 5229 | } |
| 5230 | case VIDIOCGFREQ: |
| 5231 | { |
| 5232 | unsigned long *freq = arg; |
| 5233 | |
| 5234 | *freq = usbvision->freq; |
| 5235 | PDEBUG(DBG_RIO, "%s: VIDIOCGFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4); |
| 5236 | return 0; |
| 5237 | } |
| 5238 | case VIDIOCSFREQ: |
| 5239 | { |
| 5240 | unsigned long *freq = arg; |
| 5241 | |
| 5242 | usbvision->freq = *freq; |
| 5243 | call_i2c_clients(usbvision, cmd, freq); |
| 5244 | PDEBUG(DBG_RIO, "%s: VIDIOCSFREQ freq = %ld00 kHz", __FUNCTION__, (*freq * 10)>>4); |
| 5245 | return 0; |
| 5246 | } |
| 5247 | default: |
| 5248 | { |
| 5249 | PDEBUG(DBG_RIO, "%s: Unknown command %d", __FUNCTION__, cmd); |
| 5250 | return -ENOIOCTLCMD; |
| 5251 | } |
| 5252 | } |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
| 5256 | static int usbvision_vbi_ioctl(struct inode *inode, struct file *file, |
| 5257 | unsigned int cmd, unsigned long arg) |
| 5258 | { |
| 5259 | return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl); |
| 5260 | } |
| 5261 | |
| 5262 | |
| 5263 | |
| 5264 | static void usbvision_configure_video(struct usb_usbvision *usbvision) |
| 5265 | { |
| 5266 | int model,i; |
| 5267 | |
| 5268 | if (usbvision == NULL) |
| 5269 | return; |
| 5270 | |
| 5271 | model = usbvision->DevModel; |
| 5272 | usbvision->depth = 24; |
| 5273 | usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24; |
| 5274 | |
| 5275 | if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) { |
| 5276 | usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff; |
| 5277 | } else { |
| 5278 | usbvision->Vin_Reg2_Preset = 0; |
| 5279 | } |
| 5280 | |
| 5281 | memset(&usbvision->vcap, 0, sizeof(usbvision->vcap)); |
| 5282 | strcpy(usbvision->vcap.driver, "USBVision"); |
| 5283 | strlcpy(usbvision->vcap.bus_info, usbvision->dev->dev.bus_id, |
| 5284 | sizeof(usbvision->vcap.bus_info)); |
| 5285 | usbvision->vcap.capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | |
| 5286 | (dga ? (V4L2_FBUF_CAP_LIST_CLIPPING | V4L2_CAP_VIDEO_OVERLAY) : 0) | |
| 5287 | (usbvision->have_tuner ? V4L2_CAP_TUNER : 0); |
| 5288 | usbvision->vcap.version = USBVISION_DRIVER_VERSION; /* version */ |
| 5289 | |
| 5290 | |
| 5291 | for (i = 0; i < TVNORMS; i++) |
| 5292 | if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode) |
| 5293 | break; |
| 5294 | if (i == TVNORMS) |
| 5295 | i = 0; |
| 5296 | usbvision->tvnorm = &tvnorms[i]; /* set default norm */ |
| 5297 | call_i2c_clients(usbvision, VIDIOC_S_STD, |
| 5298 | &usbvision->tvnorm->id); |
| 5299 | |
| 5300 | usbvision->video_inputs = usbvision_device_data[model].VideoChannels; |
| 5301 | usbvision->ctl_input = 0; |
| 5302 | /* usbvision_muxsel(usbvision, usbvision->ctl_input); */ |
| 5303 | |
| 5304 | /* This should be here to make i2c clients to be able to register */ |
| 5305 | usbvision_audio_off(usbvision); //first switch off audio |
| 5306 | if (!PowerOnAtOpen) { |
| 5307 | usbvision_power_on(usbvision); //and then power up the noisy tuner |
| 5308 | usbvision_init_i2c(usbvision); |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | // |
| 5313 | // Video registration stuff |
| 5314 | // |
| 5315 | |
| 5316 | // Video template |
| 5317 | static struct file_operations usbvision_fops = { |
| 5318 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5319 | .owner = THIS_MODULE, |
| 5320 | #endif |
| 5321 | .open = usbvision_v4l2_open, |
| 5322 | .release = usbvision_v4l2_close, |
| 5323 | .read = usbvision_v4l2_read, |
| 5324 | .mmap = usbvision_v4l2_mmap, |
| 5325 | .ioctl = usbvision_v4l2_ioctl, |
| 5326 | .llseek = no_llseek, |
| 5327 | }; |
| 5328 | static struct video_device usbvision_video_template = { |
| 5329 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5330 | .owner = THIS_MODULE, |
| 5331 | #endif |
| 5332 | .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE, |
| 5333 | .hardware = VID_HARDWARE_USBVISION, |
| 5334 | .fops = &usbvision_fops, |
| 5335 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
| 5336 | .name = "usbvision-video", |
| 5337 | .release = video_device_release, |
| 5338 | #endif |
| 5339 | .minor = -1, |
| 5340 | }; |
| 5341 | |
| 5342 | |
| 5343 | // Radio template |
| 5344 | static struct file_operations usbvision_radio_fops = { |
| 5345 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5346 | .owner = THIS_MODULE, |
| 5347 | #endif |
| 5348 | .open = usbvision_radio_open, |
| 5349 | .release = usbvision_radio_close, |
| 5350 | .ioctl = usbvision_radio_ioctl, |
| 5351 | .llseek = no_llseek, |
| 5352 | }; |
| 5353 | |
| 5354 | static struct video_device usbvision_radio_template= |
| 5355 | { |
| 5356 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5357 | .owner = THIS_MODULE, |
| 5358 | #endif |
| 5359 | .type = VID_TYPE_TUNER, |
| 5360 | .hardware = VID_HARDWARE_USBVISION, |
| 5361 | .fops = &usbvision_radio_fops, |
| 5362 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
| 5363 | .release = video_device_release, |
| 5364 | .name = "usbvision-radio", |
| 5365 | #endif |
| 5366 | .minor = -1, |
| 5367 | }; |
| 5368 | |
| 5369 | |
| 5370 | // vbi template |
| 5371 | static struct file_operations usbvision_vbi_fops = { |
| 5372 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5373 | .owner = THIS_MODULE, |
| 5374 | #endif |
| 5375 | .open = usbvision_vbi_open, |
| 5376 | .release = usbvision_vbi_close, |
| 5377 | .ioctl = usbvision_vbi_ioctl, |
| 5378 | .llseek = no_llseek, |
| 5379 | }; |
| 5380 | |
| 5381 | static struct video_device usbvision_vbi_template= |
| 5382 | { |
| 5383 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,31) |
| 5384 | .owner = THIS_MODULE, |
| 5385 | #endif |
| 5386 | .type = VID_TYPE_TUNER, |
| 5387 | .hardware = VID_HARDWARE_USBVISION, |
| 5388 | .fops = &usbvision_vbi_fops, |
| 5389 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
| 5390 | .release = video_device_release, |
| 5391 | .name = "usbvision-vbi", |
| 5392 | #endif |
| 5393 | .minor = -1, |
| 5394 | }; |
| 5395 | |
| 5396 | |
| 5397 | static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision, |
| 5398 | struct video_device *vdev_template, |
| 5399 | char *name) |
| 5400 | { |
| 5401 | struct usb_device *usb_dev = usbvision->dev; |
| 5402 | struct video_device *vdev; |
| 5403 | |
| 5404 | if (usb_dev == NULL) { |
| 5405 | err("%s: usbvision->dev is not set", __FUNCTION__); |
| 5406 | return NULL; |
| 5407 | } |
| 5408 | |
| 5409 | vdev = video_device_alloc(); |
| 5410 | if (NULL == vdev) { |
| 5411 | return NULL; |
| 5412 | } |
| 5413 | *vdev = *vdev_template; |
| 5414 | // vdev->minor = -1; |
| 5415 | vdev->dev = &usb_dev->dev; |
| 5416 | snprintf(vdev->name, sizeof(vdev->name), "%s", name); |
| 5417 | video_set_drvdata(vdev, usbvision); |
| 5418 | return vdev; |
| 5419 | } |
| 5420 | |
| 5421 | // unregister video4linux devices |
| 5422 | static void usbvision_unregister_video(struct usb_usbvision *usbvision) |
| 5423 | { |
| 5424 | // vbi Device: |
| 5425 | if (usbvision->vbi) { |
| 5426 | PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f); |
| 5427 | if (usbvision->vbi->minor != -1) { |
| 5428 | video_unregister_device(usbvision->vbi); |
| 5429 | } |
| 5430 | else { |
| 5431 | video_device_release(usbvision->vbi); |
| 5432 | } |
| 5433 | usbvision->vbi = NULL; |
| 5434 | } |
| 5435 | |
| 5436 | // Radio Device: |
| 5437 | if (usbvision->rdev) { |
| 5438 | PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f); |
| 5439 | if (usbvision->rdev->minor != -1) { |
| 5440 | video_unregister_device(usbvision->rdev); |
| 5441 | } |
| 5442 | else { |
| 5443 | video_device_release(usbvision->rdev); |
| 5444 | } |
| 5445 | usbvision->rdev = NULL; |
| 5446 | } |
| 5447 | |
| 5448 | // Video Device: |
| 5449 | if (usbvision->vdev) { |
| 5450 | PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f); |
| 5451 | if (usbvision->vdev->minor != -1) { |
| 5452 | video_unregister_device(usbvision->vdev); |
| 5453 | } |
| 5454 | else { |
| 5455 | video_device_release(usbvision->vdev); |
| 5456 | } |
| 5457 | usbvision->vdev = NULL; |
| 5458 | } |
| 5459 | } |
| 5460 | |
| 5461 | // register video4linux devices |
| 5462 | static int __devinit usbvision_register_video(struct usb_usbvision *usbvision) |
| 5463 | { |
| 5464 | // Video Device: |
| 5465 | usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video"); |
| 5466 | if (usbvision->vdev == NULL) { |
| 5467 | goto err_exit; |
| 5468 | } |
| 5469 | if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) { |
| 5470 | goto err_exit; |
| 5471 | } |
| 5472 | info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f); |
| 5473 | |
| 5474 | // Radio Device: |
| 5475 | if (usbvision_device_data[usbvision->DevModel].Radio) { |
| 5476 | // usbvision has radio |
| 5477 | usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio"); |
| 5478 | if (usbvision->rdev == NULL) { |
| 5479 | goto err_exit; |
| 5480 | } |
| 5481 | if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) { |
| 5482 | goto err_exit; |
| 5483 | } |
| 5484 | info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f); |
| 5485 | } |
| 5486 | // vbi Device: |
| 5487 | if (usbvision_device_data[usbvision->DevModel].vbi) { |
| 5488 | usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI"); |
| 5489 | if (usbvision->vdev == NULL) { |
| 5490 | goto err_exit; |
| 5491 | } |
| 5492 | if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) { |
| 5493 | goto err_exit; |
| 5494 | } |
| 5495 | info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f); |
| 5496 | } |
| 5497 | // all done |
| 5498 | return 0; |
| 5499 | |
| 5500 | err_exit: |
| 5501 | err("USBVision[%d]: video_register_device() failed", usbvision->nr); |
| 5502 | usbvision_unregister_video(usbvision); |
| 5503 | return -1; |
| 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | * usbvision_alloc() |
| 5508 | * |
| 5509 | * This code allocates the struct usb_usbvision. It is filled with default values. |
| 5510 | * |
| 5511 | * Returns NULL on error, a pointer to usb_usbvision else. |
| 5512 | * |
| 5513 | */ |
| 5514 | static struct usb_usbvision *usbvision_alloc(struct usb_device *dev) |
| 5515 | { |
| 5516 | struct usb_usbvision *usbvision; |
| 5517 | |
| 5518 | if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) { |
| 5519 | goto err_exit; |
| 5520 | } |
| 5521 | |
| 5522 | usbvision->dev = dev; |
| 5523 | |
| 5524 | init_MUTEX(&usbvision->lock); /* to 1 == available */ |
| 5525 | |
| 5526 | // prepare control urb for control messages during interrupts |
| 5527 | usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL); |
| 5528 | if (usbvision->ctrlUrb == NULL) { |
| 5529 | goto err_exit; |
| 5530 | } |
| 5531 | init_waitqueue_head(&usbvision->ctrlUrb_wq); |
| 5532 | init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */ |
| 5533 | |
| 5534 | init_timer(&usbvision->powerOffTimer); |
| 5535 | usbvision->powerOffTimer.data = (long) usbvision; |
| 5536 | usbvision->powerOffTimer.function = usbvision_powerOffTimer; |
| 5537 | |
| 5538 | return usbvision; |
| 5539 | |
| 5540 | err_exit: |
| 5541 | if (usbvision && usbvision->ctrlUrb) { |
| 5542 | usb_free_urb(usbvision->ctrlUrb); |
| 5543 | } |
| 5544 | if (usbvision) { |
| 5545 | kfree(usbvision); |
| 5546 | } |
| 5547 | return NULL; |
| 5548 | } |
| 5549 | |
| 5550 | /* |
| 5551 | * usbvision_release() |
| 5552 | * |
| 5553 | * This code does final release of struct usb_usbvision. This happens |
| 5554 | * after the device is disconnected -and- all clients closed their files. |
| 5555 | * |
| 5556 | */ |
| 5557 | static void usbvision_release(struct usb_usbvision *usbvision) |
| 5558 | { |
| 5559 | PDEBUG(DBG_PROBE, ""); |
| 5560 | |
| 5561 | down(&usbvision->lock); |
| 5562 | |
| 5563 | if (timer_pending(&usbvision->powerOffTimer)) { |
| 5564 | del_timer(&usbvision->powerOffTimer); |
| 5565 | } |
| 5566 | |
| 5567 | usbvision->usbvision_used = 0; |
| 5568 | usbvision->initialized = 0; |
| 5569 | |
| 5570 | up(&usbvision->lock); |
| 5571 | |
| 5572 | usbvision_remove_sysfs(usbvision->vdev); |
| 5573 | usbvision_unregister_video(usbvision); |
| 5574 | if(dga) { |
| 5575 | if (usbvision->overlay_base) { |
| 5576 | iounmap(usbvision->overlay_base); |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (usbvision->ctrlUrb) { |
| 5581 | usb_free_urb(usbvision->ctrlUrb); |
| 5582 | } |
| 5583 | |
| 5584 | kfree(usbvision); |
| 5585 | |
| 5586 | PDEBUG(DBG_PROBE, "success"); |
| 5587 | } |
| 5588 | |
| 5589 | |
| 5590 | /* |
| 5591 | * usbvision_probe() |
| 5592 | * |
| 5593 | * This procedure queries device descriptor and accepts the interface |
| 5594 | * if it looks like USBVISION video device |
| 5595 | * |
| 5596 | */ |
| 5597 | static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid) |
| 5598 | { |
| 5599 | struct usb_device *dev = interface_to_usbdev(intf); |
| 5600 | __u8 ifnum = intf->altsetting->desc.bInterfaceNumber; |
| 5601 | const struct usb_host_interface *interface; |
| 5602 | struct usb_usbvision *usbvision = NULL; |
| 5603 | const struct usb_endpoint_descriptor *endpoint; |
| 5604 | int model; |
| 5605 | |
| 5606 | PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u", |
| 5607 | dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum); |
| 5608 | /* Is it an USBVISION video dev? */ |
| 5609 | model = 0; |
| 5610 | for(model = 0; usbvision_device_data[model].idVendor; model++) { |
| 5611 | if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) { |
| 5612 | continue; |
| 5613 | } |
| 5614 | if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) { |
| 5615 | continue; |
| 5616 | } |
| 5617 | |
| 5618 | info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString); |
| 5619 | break; |
| 5620 | } |
| 5621 | |
| 5622 | if (usbvision_device_data[model].idVendor == 0) { |
| 5623 | return -ENODEV; //no matching device |
| 5624 | } |
| 5625 | if (usbvision_device_data[model].Interface >= 0) { |
| 5626 | interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0]; |
| 5627 | } |
| 5628 | else { |
| 5629 | interface = &dev->actconfig->interface[ifnum]->altsetting[0]; |
| 5630 | } |
| 5631 | endpoint = &interface->endpoint[1].desc; |
| 5632 | if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) { |
| 5633 | err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum); |
| 5634 | err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes); |
| 5635 | return -ENODEV; |
| 5636 | } |
| 5637 | if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) { |
| 5638 | err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum); |
| 5639 | return -ENODEV; |
| 5640 | } |
| 5641 | |
| 5642 | usb_get_dev(dev); |
| 5643 | |
| 5644 | if ((usbvision = usbvision_alloc(dev)) == NULL) { |
| 5645 | err("%s: couldn't allocate USBVision struct", __FUNCTION__); |
| 5646 | return -ENOMEM; |
| 5647 | } |
| 5648 | if (dev->descriptor.bNumConfigurations > 1) { |
| 5649 | usbvision->bridgeType = BRIDGE_NT1004; |
| 5650 | } |
| 5651 | else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") { |
| 5652 | usbvision->bridgeType = BRIDGE_NT1005; |
| 5653 | } |
| 5654 | else { |
| 5655 | usbvision->bridgeType = BRIDGE_NT1003; |
| 5656 | } |
| 5657 | PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType); |
| 5658 | |
| 5659 | down(&usbvision->lock); |
| 5660 | |
| 5661 | usbvision->nr = usbvision_nr++; |
| 5662 | |
| 5663 | usbvision->have_tuner = usbvision_device_data[model].Tuner; |
| 5664 | if (usbvision->have_tuner) { |
| 5665 | usbvision->tuner_type = usbvision_device_data[model].TunerType; |
| 5666 | } |
| 5667 | |
| 5668 | usbvision->DevModel = model; |
| 5669 | usbvision->remove_pending = 0; |
| 5670 | usbvision->last_error = 0; |
| 5671 | usbvision->iface = ifnum; |
| 5672 | usbvision->ifaceAltInactive = 0; |
| 5673 | usbvision->ifaceAltActive = 1; |
| 5674 | usbvision->video_endp = endpoint->bEndpointAddress; |
| 5675 | usbvision->isocPacketSize = 0; |
| 5676 | usbvision->usb_bandwidth = 0; |
| 5677 | usbvision->user = 0; |
| 5678 | usbvision->streaming = Stream_Off; |
| 5679 | |
| 5680 | usbvision_register_video(usbvision); |
| 5681 | usbvision_configure_video(usbvision); |
| 5682 | up(&usbvision->lock); |
| 5683 | |
| 5684 | |
| 5685 | usb_set_intfdata (intf, usbvision); |
| 5686 | usbvision_create_sysfs(usbvision->vdev); |
| 5687 | |
| 5688 | PDEBUG(DBG_PROBE, "success"); |
| 5689 | return 0; |
| 5690 | } |
| 5691 | |
| 5692 | |
| 5693 | /* |
| 5694 | * usbvision_disconnect() |
| 5695 | * |
| 5696 | * This procedure stops all driver activity, deallocates interface-private |
| 5697 | * structure (pointed by 'ptr') and after that driver should be removable |
| 5698 | * with no ill consequences. |
| 5699 | * |
| 5700 | */ |
| 5701 | static void __devexit usbvision_disconnect(struct usb_interface *intf) |
| 5702 | { |
| 5703 | struct usb_usbvision *usbvision = usb_get_intfdata(intf); |
| 5704 | |
| 5705 | PDEBUG(DBG_PROBE, ""); |
| 5706 | |
| 5707 | if (usbvision == NULL) { |
| 5708 | err("%s: usb_get_intfdata() failed", __FUNCTION__); |
| 5709 | return; |
| 5710 | } |
| 5711 | usb_set_intfdata (intf, NULL); |
| 5712 | |
| 5713 | down(&usbvision->lock); |
| 5714 | |
| 5715 | // At this time we ask to cancel outstanding URBs |
| 5716 | usbvision_stop_isoc(usbvision); |
| 5717 | |
| 5718 | if (usbvision->power) { |
| 5719 | usbvision_i2c_usb_del_bus(&usbvision->i2c_adap); |
| 5720 | usbvision_power_off(usbvision); |
| 5721 | } |
| 5722 | usbvision->remove_pending = 1; // Now all ISO data will be ignored |
| 5723 | |
| 5724 | usb_put_dev(usbvision->dev); |
| 5725 | usbvision->dev = NULL; // USB device is no more |
| 5726 | |
| 5727 | up(&usbvision->lock); |
| 5728 | |
| 5729 | if (usbvision->user) { |
| 5730 | info("%s: In use, disconnect pending", __FUNCTION__); |
| 5731 | wake_up_interruptible(&usbvision->wait_frame); |
| 5732 | wake_up_interruptible(&usbvision->wait_stream); |
| 5733 | } |
| 5734 | else { |
| 5735 | usbvision_release(usbvision); |
| 5736 | } |
| 5737 | |
| 5738 | PDEBUG(DBG_PROBE, "success"); |
| 5739 | |
| 5740 | } |
| 5741 | |
| 5742 | static struct usb_driver usbvision_driver = { |
| 5743 | .name = "usbvision", |
| 5744 | .id_table = usbvision_table, |
| 5745 | .probe = usbvision_probe, |
| 5746 | .disconnect = usbvision_disconnect |
| 5747 | }; |
| 5748 | |
| 5749 | /* |
| 5750 | * customdevice_process() |
| 5751 | * |
| 5752 | * This procedure preprocesses CustomDevice parameter if any |
| 5753 | * |
| 5754 | */ |
| 5755 | void customdevice_process(void) |
| 5756 | { |
| 5757 | usbvision_device_data[0]=usbvision_device_data[1]; |
| 5758 | usbvision_table[0]=usbvision_table[1]; |
| 5759 | |
| 5760 | if(CustomDevice) |
| 5761 | { |
| 5762 | char *parse=CustomDevice; |
| 5763 | |
| 5764 | PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice); |
| 5765 | |
| 5766 | /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1" |
| 5767 | usbvision_device_data[0].idVendor; |
| 5768 | usbvision_device_data[0].idProduct; |
| 5769 | usbvision_device_data[0].Interface; |
| 5770 | usbvision_device_data[0].Codec; |
| 5771 | usbvision_device_data[0].VideoChannels; |
| 5772 | usbvision_device_data[0].VideoNorm; |
| 5773 | usbvision_device_data[0].AudioChannels; |
| 5774 | usbvision_device_data[0].Radio; |
| 5775 | usbvision_device_data[0].Tuner; |
| 5776 | usbvision_device_data[0].TunerType; |
| 5777 | usbvision_device_data[0].Vin_Reg1; |
| 5778 | usbvision_device_data[0].Vin_Reg2; |
| 5779 | usbvision_device_data[0].X_Offset; |
| 5780 | usbvision_device_data[0].Y_Offset; |
| 5781 | usbvision_device_data[0].Dvi_yuv; |
| 5782 | usbvision_device_data[0].ModelString; |
| 5783 | */ |
| 5784 | |
| 5785 | rmspace(parse); |
| 5786 | usbvision_device_data[0].ModelString="USBVISION Custom Device"; |
| 5787 | |
| 5788 | parse+=2; |
| 5789 | sscanf(parse,"%x",&usbvision_device_data[0].idVendor); |
| 5790 | goto2next(parse); |
| 5791 | PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor); |
| 5792 | parse+=2; |
| 5793 | sscanf(parse,"%x",&usbvision_device_data[0].idProduct); |
| 5794 | goto2next(parse); |
| 5795 | PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct); |
| 5796 | sscanf(parse,"%d",&usbvision_device_data[0].Interface); |
| 5797 | goto2next(parse); |
| 5798 | PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface); |
| 5799 | sscanf(parse,"%d",&usbvision_device_data[0].Codec); |
| 5800 | goto2next(parse); |
| 5801 | PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec); |
| 5802 | sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels); |
| 5803 | goto2next(parse); |
| 5804 | PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels); |
| 5805 | |
| 5806 | switch(*parse) |
| 5807 | { |
| 5808 | case 'P': |
| 5809 | PDEBUG(DBG_PROBE, "VideoNorm=PAL"); |
| 5810 | usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL; |
| 5811 | break; |
| 5812 | |
| 5813 | case 'S': |
| 5814 | PDEBUG(DBG_PROBE, "VideoNorm=SECAM"); |
| 5815 | usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM; |
| 5816 | break; |
| 5817 | |
| 5818 | case 'N': |
| 5819 | PDEBUG(DBG_PROBE, "VideoNorm=NTSC"); |
| 5820 | usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC; |
| 5821 | break; |
| 5822 | |
| 5823 | default: |
| 5824 | PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)"); |
| 5825 | usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL; |
| 5826 | break; |
| 5827 | } |
| 5828 | goto2next(parse); |
| 5829 | |
| 5830 | sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels); |
| 5831 | goto2next(parse); |
| 5832 | PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels); |
| 5833 | sscanf(parse,"%d",&usbvision_device_data[0].Radio); |
| 5834 | goto2next(parse); |
| 5835 | PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio); |
| 5836 | sscanf(parse,"%d",&usbvision_device_data[0].Tuner); |
| 5837 | goto2next(parse); |
| 5838 | PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner); |
| 5839 | sscanf(parse,"%d",&usbvision_device_data[0].TunerType); |
| 5840 | goto2next(parse); |
| 5841 | PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType); |
| 5842 | sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1); |
| 5843 | goto2next(parse); |
| 5844 | PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1); |
| 5845 | sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2); |
| 5846 | goto2next(parse); |
| 5847 | PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2); |
| 5848 | sscanf(parse,"%d",&usbvision_device_data[0].X_Offset); |
| 5849 | goto2next(parse); |
| 5850 | PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset); |
| 5851 | sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset); |
| 5852 | goto2next(parse); |
| 5853 | PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset); |
| 5854 | sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv); |
| 5855 | PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv); |
| 5856 | |
| 5857 | //add to usbvision_table also |
| 5858 | usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE; |
| 5859 | usbvision_table[0].idVendor=usbvision_device_data[0].idVendor; |
| 5860 | usbvision_table[0].idProduct=usbvision_device_data[0].idProduct; |
| 5861 | |
| 5862 | } |
| 5863 | } |
| 5864 | |
| 5865 | |
| 5866 | |
| 5867 | /* |
| 5868 | * usbvision_init() |
| 5869 | * |
| 5870 | * This code is run to initialize the driver. |
| 5871 | * |
| 5872 | */ |
| 5873 | static int __init usbvision_init(void) |
| 5874 | { |
| 5875 | int errCode; |
| 5876 | |
| 5877 | PDEBUG(DBG_PROBE, ""); |
| 5878 | |
| 5879 | PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled"); |
| 5880 | PDEBUG(DBG_IO, "IO debugging is enabled"); |
| 5881 | PDEBUG(DBG_RIO, "RIO debugging is enabled"); |
| 5882 | PDEBUG(DBG_HEADER, "HEADER debugging is enabled"); |
| 5883 | PDEBUG(DBG_PROBE, "PROBE debugging is enabled"); |
| 5884 | PDEBUG(DBG_IRQ, "IRQ debugging is enabled"); |
| 5885 | PDEBUG(DBG_ISOC, "ISOC debugging is enabled"); |
| 5886 | PDEBUG(DBG_PARSE, "PARSE debugging is enabled"); |
| 5887 | PDEBUG(DBG_SCRATCH, "SCRATCH debugging is enabled"); |
| 5888 | PDEBUG(DBG_FUNC, "FUNC debugging is enabled"); |
| 5889 | PDEBUG(DBG_I2C, "I2C debugging is enabled"); |
| 5890 | |
| 5891 | /* disable planar mode support unless compression enabled */ |
| 5892 | if (isocMode != ISOC_MODE_COMPRESS ) { |
| 5893 | // FIXME : not the right way to set supported flag |
| 5894 | usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420 |
| 5895 | usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P |
| 5896 | } |
| 5897 | |
| 5898 | customdevice_process(); |
| 5899 | |
| 5900 | errCode = usb_register(&usbvision_driver); |
| 5901 | |
| 5902 | if (errCode == 0) { |
| 5903 | info(DRIVER_DESC " : " DRIVER_VERSION); |
| 5904 | PDEBUG(DBG_PROBE, "success"); |
| 5905 | } |
| 5906 | return errCode; |
| 5907 | } |
| 5908 | |
| 5909 | static void __exit usbvision_exit(void) |
| 5910 | { |
| 5911 | PDEBUG(DBG_PROBE, ""); |
| 5912 | |
| 5913 | usb_deregister(&usbvision_driver); |
| 5914 | PDEBUG(DBG_PROBE, "success"); |
| 5915 | } |
| 5916 | |
| 5917 | module_init(usbvision_init); |
| 5918 | module_exit(usbvision_exit); |
| 5919 | |
| 5920 | /* |
| 5921 | * Overrides for Emacs so that we follow Linus's tabbing style. |
| 5922 | * --------------------------------------------------------------------------- |
| 5923 | * Local variables: |
| 5924 | * c-basic-offset: 8 |
| 5925 | * End: |
| 5926 | */ |