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