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