V4L/DVB (4970): Usbvision memory fixes
[pandora-kernel.git] / drivers / media / video / usbvision / usbvision-video.c
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/version.h>
48 #include <linux/kernel.h>
49 #include <linux/sched.h>
50 #include <linux/list.h>
51 #include <linux/timer.h>
52 #include <linux/slab.h>
53 #include <linux/mm.h>
54 #include <linux/utsname.h>
55 #include <linux/highmem.h>
56 #include <linux/smp_lock.h>
57 #include <linux/videodev.h>
58 #include <linux/vmalloc.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/spinlock.h>
62 #include <asm/io.h>
63 #include <linux/videodev2.h>
64 #include <linux/video_decoder.h>
65 #include <linux/i2c.h>
66
67 #include <media/saa7115.h>
68 #include <media/v4l2-common.h>
69 #include <media/tuner.h>
70 #include <media/audiochip.h>
71
72 #include <linux/moduleparam.h>
73 #include <linux/workqueue.h>
74
75 #ifdef CONFIG_KMOD
76 #include <linux/kmod.h>
77 #endif
78
79 #include "usbvision.h"
80
81 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
82 #define DRIVER_NAME "usbvision"
83 #define DRIVER_ALIAS "USBVision"
84 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85 #define DRIVER_LICENSE "GPL"
86 #define USBVISION_DRIVER_VERSION_MAJOR 0
87 #define USBVISION_DRIVER_VERSION_MINOR 9
88 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
90 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
91
92 #define ENABLE_HEXDUMP  0       /* Enable if you need it */
93
94
95 #ifdef USBVISION_DEBUG
96         #define PDEBUG(level, fmt, args...) \
97                 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
98 #else
99         #define PDEBUG(level, fmt, args...) do {} while(0)
100 #endif
101
102 #define DBG_IOCTL       1<<0
103 #define DBG_IO          1<<1
104 #define DBG_PROBE       1<<2
105 #define DBG_MMAP        1<<3
106
107 //String operations
108 #define rmspace(str)    while(*str==' ') str++;
109 #define goto2next(str)  while(*str!=' ') str++; while(*str==' ') str++;
110
111
112 static int usbvision_nr = 0;                    // sequential number of usbvision device
113
114 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
115         { 1, 1,  8, V4L2_PIX_FMT_GREY    , "GREY" },
116         { 1, 2, 16, V4L2_PIX_FMT_RGB565  , "RGB565" },
117         { 1, 3, 24, V4L2_PIX_FMT_RGB24   , "RGB24" },
118         { 1, 4, 32, V4L2_PIX_FMT_RGB32   , "RGB32" },
119         { 1, 2, 16, V4L2_PIX_FMT_RGB555  , "RGB555" },
120         { 1, 2, 16, V4L2_PIX_FMT_YUYV    , "YUV422" },
121         { 1, 2, 12, V4L2_PIX_FMT_YVU420  , "YUV420P" }, // 1.5 !
122         { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
123 };
124
125 /* supported tv norms */
126 static struct usbvision_tvnorm tvnorms[] = {
127         {
128                 .name = "PAL",
129                 .id = V4L2_STD_PAL,
130         }, {
131                 .name = "NTSC",
132                 .id = V4L2_STD_NTSC,
133         }, {
134                  .name = "SECAM",
135                  .id = V4L2_STD_SECAM,
136         }, {
137                 .name = "PAL-M",
138                 .id = V4L2_STD_PAL_M,
139         }
140 };
141
142 #define TVNORMS ARRAY_SIZE(tvnorms)
143
144 // Function prototypes
145 static void usbvision_release(struct usb_usbvision *usbvision);
146
147 // Default initalization of device driver parameters
148 static int isocMode = ISOC_MODE_COMPRESS;               // Set the default format for ISOC endpoint
149 static int video_debug = 0;                             // Set the default Debug Mode of the device driver
150 static int PowerOnAtOpen = 1;                           // Set the default device to power on at startup
151 static int video_nr = -1;                               // Sequential Number of Video Device
152 static int radio_nr = -1;                               // Sequential Number of Radio Device
153 static int vbi_nr = -1;                                 // Sequential Number of VBI Device
154 static char *CustomDevice=NULL;                         // Set as nothing....
155
156 // Grab parameters for the device driver
157
158 #if defined(module_param)                               // Showing parameters under SYSFS
159 module_param(isocMode, int, 0444);
160 module_param(video_debug, int, 0444);
161 module_param(PowerOnAtOpen, int, 0444);
162 module_param(video_nr, int, 0444);
163 module_param(radio_nr, int, 0444);
164 module_param(vbi_nr, int, 0444);
165 module_param(CustomDevice, charp, 0444);
166 #else                                                   // Old Style
167 MODULE_PARAM(isocMode, "i");
168 MODULE_PARM(video_debug, "i");                          // Grab the Debug Mode of the device driver
169 MODULE_PARM(adjustCompression, "i");                    // Grab the compression to be adaptive
170 MODULE_PARM(PowerOnAtOpen, "i");                        // Grab the device to power on at startup
171 MODULE_PARM(SwitchSVideoInput, "i");                    // To help people with Black and White output with using s-video input.  Some cables and input device are wired differently.
172 MODULE_PARM(video_nr, "i");                             // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
173 MODULE_PARM(radio_nr, "i");                             // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
174 MODULE_PARM(vbi_nr, "i");                               // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
175 MODULE_PARM(CustomDevice, "s");                         // .... CustomDevice
176 #endif
177
178 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
179 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
180 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened.  Default: 1 (On)");
181 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
182 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
183 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX).  Default: -1 (autodetect)");
184 MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device.  Default: null");
185
186
187 // Misc stuff
188 MODULE_AUTHOR(DRIVER_AUTHOR);
189 MODULE_DESCRIPTION(DRIVER_DESC);
190 MODULE_LICENSE(DRIVER_LICENSE);
191 MODULE_VERSION(USBVISION_VERSION_STRING);
192 MODULE_ALIAS(DRIVER_ALIAS);
193
194
195 /****************************************************************************************/
196 /* SYSFS Code - Copied from the stv680.c usb module.                                    */
197 /* Device information is located at /sys/class/video4linux/video0                       */
198 /* Device parameters information is located at /sys/module/usbvision                    */
199 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber    */
200 /****************************************************************************************/
201
202
203 #define YES_NO(x) ((x) ? "Yes" : "No")
204
205 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
206 {
207         struct video_device *vdev = to_video_device(cd);
208         return video_get_drvdata(vdev);
209 }
210
211 static ssize_t show_version(struct class_device *cd, char *buf)
212 {
213         return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
214 }
215 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
216
217 static ssize_t show_model(struct class_device *class_dev, char *buf)
218 {
219         struct video_device *vdev = to_video_device(class_dev);
220         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
221         return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
222 }
223 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
224
225 static ssize_t show_hue(struct class_device *class_dev, char *buf)
226 {
227         struct video_device *vdev = to_video_device(class_dev);
228         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
229         struct v4l2_control ctrl;
230         ctrl.id = V4L2_CID_HUE;
231         ctrl.value = 0;
232         call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
233         return sprintf(buf, "%d\n", ctrl.value >> 8);
234 }
235 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
236
237 static ssize_t show_contrast(struct class_device *class_dev, char *buf)
238 {
239         struct video_device *vdev = to_video_device(class_dev);
240         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
241         struct v4l2_control ctrl;
242         ctrl.id = V4L2_CID_CONTRAST;
243         ctrl.value = 0;
244         call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
245         return sprintf(buf, "%d\n", ctrl.value >> 8);
246 }
247 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
248
249 static ssize_t show_brightness(struct class_device *class_dev, char *buf)
250 {
251         struct video_device *vdev = to_video_device(class_dev);
252         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
253         struct v4l2_control ctrl;
254         ctrl.id = V4L2_CID_BRIGHTNESS;
255         ctrl.value = 0;
256         call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
257         return sprintf(buf, "%d\n", ctrl.value >> 8);
258 }
259 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
260
261 static ssize_t show_saturation(struct class_device *class_dev, char *buf)
262 {
263         struct video_device *vdev = to_video_device(class_dev);
264         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
265         struct v4l2_control ctrl;
266         ctrl.id = V4L2_CID_SATURATION;
267         ctrl.value = 0;
268         call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
269         return sprintf(buf, "%d\n", ctrl.value >> 8);
270 }
271 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
272
273 static ssize_t show_streaming(struct class_device *class_dev, char *buf)
274 {
275         struct video_device *vdev = to_video_device(class_dev);
276         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
277         return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
278 }
279 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
280
281 static ssize_t show_compression(struct class_device *class_dev, char *buf)
282 {
283         struct video_device *vdev = to_video_device(class_dev);
284         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
285         return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
286 }
287 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
288
289 static ssize_t show_device_bridge(struct class_device *class_dev, char *buf)
290 {
291         struct video_device *vdev = to_video_device(class_dev);
292         struct usb_usbvision *usbvision = video_get_drvdata(vdev);
293         return sprintf(buf, "%d\n", usbvision->bridgeType);
294 }
295 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
296
297 static void usbvision_create_sysfs(struct video_device *vdev)
298 {
299         int res;
300         if (vdev) {
301                 res=video_device_create_file(vdev, &class_device_attr_version);
302                 res=video_device_create_file(vdev, &class_device_attr_model);
303                 res=video_device_create_file(vdev, &class_device_attr_hue);
304                 res=video_device_create_file(vdev, &class_device_attr_contrast);
305                 res=video_device_create_file(vdev, &class_device_attr_brightness);
306                 res=video_device_create_file(vdev, &class_device_attr_saturation);
307                 res=video_device_create_file(vdev, &class_device_attr_streaming);
308                 res=video_device_create_file(vdev, &class_device_attr_compression);
309                 res=video_device_create_file(vdev, &class_device_attr_bridge);
310         }
311 }
312
313 static void usbvision_remove_sysfs(struct video_device *vdev)
314 {
315         if (vdev) {
316                 video_device_remove_file(vdev, &class_device_attr_version);
317                 video_device_remove_file(vdev, &class_device_attr_model);
318                 video_device_remove_file(vdev, &class_device_attr_hue);
319                 video_device_remove_file(vdev, &class_device_attr_contrast);
320                 video_device_remove_file(vdev, &class_device_attr_brightness);
321                 video_device_remove_file(vdev, &class_device_attr_saturation);
322                 video_device_remove_file(vdev, &class_device_attr_streaming);
323                 video_device_remove_file(vdev, &class_device_attr_compression);
324                 video_device_remove_file(vdev, &class_device_attr_bridge);
325         }
326 }
327
328
329 /*
330  * usbvision_open()
331  *
332  * This is part of Video 4 Linux API. The driver can be opened by one
333  * client only (checks internal counter 'usbvision->user'). The procedure
334  * then allocates buffers needed for video processing.
335  *
336  */
337 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
338 {
339         struct video_device *dev = video_devdata(file);
340         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
341         int errCode = 0;
342
343         PDEBUG(DBG_IO, "open");
344
345
346         usbvision_reset_powerOffTimer(usbvision);
347
348         if (usbvision->user)
349                 errCode = -EBUSY;
350         else {
351                 /* Allocate memory for the frame buffers */
352                 errCode = usbvision_frames_alloc(usbvision);
353                 if(!errCode) {
354                         /* Allocate memory for the scratch ring buffer */
355                         errCode = usbvision_scratch_alloc(usbvision);
356                         if ((!errCode) && (isocMode==ISOC_MODE_COMPRESS)) {
357                                 /* Allocate intermediate decompression buffers only if needed */
358                                 errCode = usbvision_decompress_alloc(usbvision);
359                         }
360                 }
361                 if (errCode) {
362                         /* Deallocate all buffers if trouble */
363                         usbvision_frames_free(usbvision);
364                         usbvision_scratch_free(usbvision);
365                         usbvision_decompress_free(usbvision);
366                 }
367         }
368
369         /* If so far no errors then we shall start the camera */
370         if (!errCode) {
371                 down(&usbvision->lock);
372                 if (usbvision->power == 0) {
373                         usbvision_power_on(usbvision);
374                         usbvision_init_i2c(usbvision);
375                 }
376
377                 /* Send init sequence only once, it's large! */
378                 if (!usbvision->initialized) {
379                         int setup_ok = 0;
380                         setup_ok = usbvision_setup(usbvision,isocMode);
381                         if (setup_ok)
382                                 usbvision->initialized = 1;
383                         else
384                                 errCode = -EBUSY;
385                 }
386
387                 if (!errCode) {
388                         usbvision_begin_streaming(usbvision);
389                         errCode = usbvision_init_isoc(usbvision);
390                         /* device needs to be initialized before isoc transfer */
391                         usbvision_muxsel(usbvision,0);
392                         usbvision->user++;
393                 }
394                 else {
395                         if (PowerOnAtOpen) {
396                                 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
397                                 usbvision_power_off(usbvision);
398                                 usbvision->initialized = 0;
399                         }
400                 }
401                 up(&usbvision->lock);
402         }
403
404         if (errCode) {
405         }
406
407         /* prepare queues */
408         usbvision_empty_framequeues(usbvision);
409
410         PDEBUG(DBG_IO, "success");
411         return errCode;
412 }
413
414 /*
415  * usbvision_v4l2_close()
416  *
417  * This is part of Video 4 Linux API. The procedure
418  * stops streaming and deallocates all buffers that were earlier
419  * allocated in usbvision_v4l2_open().
420  *
421  */
422 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
423 {
424         struct video_device *dev = video_devdata(file);
425         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
426
427         PDEBUG(DBG_IO, "close");
428         down(&usbvision->lock);
429
430         usbvision_audio_off(usbvision);
431         usbvision_restart_isoc(usbvision);
432         usbvision_stop_isoc(usbvision);
433
434         usbvision_decompress_free(usbvision);
435         usbvision_frames_free(usbvision);
436         usbvision_scratch_free(usbvision);
437
438         usbvision->user--;
439
440         if (PowerOnAtOpen) {
441                 /* power off in a little while to avoid off/on every close/open short sequences */
442                 usbvision_set_powerOffTimer(usbvision);
443                 usbvision->initialized = 0;
444         }
445
446         up(&usbvision->lock);
447
448         if (usbvision->remove_pending) {
449                 info("%s: Final disconnect", __FUNCTION__);
450                 usbvision_release(usbvision);
451         }
452
453         PDEBUG(DBG_IO, "success");
454
455
456         return 0;
457 }
458
459
460 /*
461  * usbvision_ioctl()
462  *
463  * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
464  *
465  */
466 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
467                                  unsigned int cmd, void *arg)
468 {
469         struct video_device *dev = video_devdata(file);
470         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
471
472         if (!USBVISION_IS_OPERATIONAL(usbvision))
473                 return -EFAULT;
474
475         switch (cmd) {
476
477 #ifdef CONFIG_VIDEO_ADV_DEBUG
478                 /* ioctls to allow direct acces to the NT100x registers */
479                 case VIDIOC_INT_G_REGISTER:
480                 {
481                         struct v4l2_register *reg = arg;
482                         int errCode;
483
484                         if (reg->i2c_id != 0)
485                                 return -EINVAL;
486                         /* NT100x has a 8-bit register space */
487                         errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
488                         if (errCode < 0) {
489                                 err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
490                         }
491                         else {
492                                 reg->val=(unsigned char)errCode;
493                                 PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
494                                                         (unsigned int)reg->reg, reg->val);
495                                 errCode = 0; // No error
496                         }
497                         return errCode;
498                 }
499                 case VIDIOC_INT_S_REGISTER:
500                 {
501                         struct v4l2_register *reg = arg;
502                         int errCode;
503
504                         if (reg->i2c_id != 0)
505                                 return -EINVAL;
506                         if (!capable(CAP_SYS_ADMIN))
507                                 return -EPERM;
508                         errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
509                         if (errCode < 0) {
510                                 err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
511                         }
512                         else {
513                                 PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
514                                                         (unsigned int)reg->reg, reg->val);
515                                 errCode = 0;
516                         }
517                         return 0;
518                 }
519 #endif
520                 case VIDIOC_QUERYCAP:
521                 {
522                         struct v4l2_capability *vc=arg;
523
524                         memset(vc, 0, sizeof(*vc));
525                         strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
526                         strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
527                                 sizeof(vc->card));
528                         strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
529                                 sizeof(vc->bus_info));
530                         vc->version = USBVISION_DRIVER_VERSION;
531                         vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
532                                 V4L2_CAP_AUDIO |
533                                 V4L2_CAP_READWRITE |
534                                 V4L2_CAP_STREAMING |
535                                 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
536                         PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
537                         return 0;
538                 }
539                 case VIDIOC_ENUMINPUT:
540                 {
541                         struct v4l2_input *vi = arg;
542                         int chan;
543
544                         if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
545                                 return -EINVAL;
546                         if (usbvision->have_tuner) {
547                                 chan = vi->index;
548                         }
549                         else {
550                                 chan = vi->index + 1; //skip Television string
551                         }
552                         switch(chan) {
553                                 case 0:
554                                         if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
555                                                 strcpy(vi->name, "White Video Input");
556                                         }
557                                         else {
558                                                 strcpy(vi->name, "Television");
559                                                 vi->type = V4L2_INPUT_TYPE_TUNER;
560                                                 vi->audioset = 1;
561                                                 vi->tuner = chan;
562                                                 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
563                                         }
564                                         break;
565                                 case 1:
566                                         vi->type = V4L2_INPUT_TYPE_CAMERA;
567                                         if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
568                                                 strcpy(vi->name, "Green Video Input");
569                                         }
570                                         else {
571                                                 strcpy(vi->name, "Composite Video Input");
572                                         }
573                                         vi->std = V4L2_STD_PAL;
574                                         break;
575                                 case 2:
576                                         vi->type = V4L2_INPUT_TYPE_CAMERA;
577                                         if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
578                                                 strcpy(vi->name, "Yellow Video Input");
579                                         }
580                                         else {
581                                         strcpy(vi->name, "S-Video Input");
582                                         }
583                                         vi->std = V4L2_STD_PAL;
584                                         break;
585                                 case 3:
586                                         vi->type = V4L2_INPUT_TYPE_CAMERA;
587                                         strcpy(vi->name, "Red Video Input");
588                                         vi->std = V4L2_STD_PAL;
589                                         break;
590                         }
591                         PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
592                                vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
593                         return 0;
594                 }
595                 case VIDIOC_ENUMSTD:
596                 {
597                         struct v4l2_standard *e = arg;
598                         unsigned int i;
599                         int ret;
600
601                         i = e->index;
602                         if (i >= TVNORMS)
603                                 return -EINVAL;
604                         ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
605                                                        tvnorms[e->index].name);
606                         e->index = i;
607                         if (ret < 0)
608                                 return ret;
609                         return 0;
610                 }
611                 case VIDIOC_G_INPUT:
612                 {
613                         int *input = arg;
614                         *input = usbvision->ctl_input;
615                         return 0;
616                 }
617                 case VIDIOC_S_INPUT:
618                 {
619                         int *input = arg;
620                         if ((*input >= usbvision->video_inputs) || (*input < 0) )
621                                 return -EINVAL;
622                         usbvision->ctl_input = *input;
623
624                         down(&usbvision->lock);
625                         usbvision_muxsel(usbvision, usbvision->ctl_input);
626                         usbvision_set_input(usbvision);
627                         usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
628                         up(&usbvision->lock);
629                         return 0;
630                 }
631                 case VIDIOC_G_STD:
632                 {
633                         v4l2_std_id *id = arg;
634
635                         *id = usbvision->tvnorm->id;
636
637                         PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
638                         return 0;
639                 }
640                 case VIDIOC_S_STD:
641                 {
642                         v4l2_std_id *id = arg;
643                         unsigned int i;
644
645                         for (i = 0; i < TVNORMS; i++)
646                                 if (*id == tvnorms[i].id)
647                                         break;
648                         if (i == TVNORMS)
649                                 for (i = 0; i < TVNORMS; i++)
650                                         if (*id & tvnorms[i].id)
651                                                 break;
652                         if (i == TVNORMS)
653                                 return -EINVAL;
654
655                         down(&usbvision->lock);
656                         usbvision->tvnorm = &tvnorms[i];
657
658                         call_i2c_clients(usbvision, VIDIOC_S_STD,
659                                          &usbvision->tvnorm->id);
660
661                         up(&usbvision->lock);
662
663                         PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
664                         return 0;
665                 }
666                 case VIDIOC_G_TUNER:
667                 {
668                         struct v4l2_tuner *vt = arg;
669
670                         if (!usbvision->have_tuner || vt->index)        // Only tuner 0
671                                 return -EINVAL;
672                         strcpy(vt->name, "Television");
673                         /* Let clients fill in the remainder of this struct */
674                         call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
675
676                         PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
677                         return 0;
678                 }
679                 case VIDIOC_S_TUNER:
680                 {
681                         struct v4l2_tuner *vt = arg;
682
683                         // Only no or one tuner for now
684                         if (!usbvision->have_tuner || vt->index)
685                                 return -EINVAL;
686                         /* let clients handle this */
687                         call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
688
689                         PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
690                         return 0;
691                 }
692                 case VIDIOC_G_FREQUENCY:
693                 {
694                         struct v4l2_frequency *freq = arg;
695
696                         freq->tuner = 0; // Only one tuner
697                         freq->type = V4L2_TUNER_ANALOG_TV;
698                         freq->frequency = usbvision->freq;
699                         PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
700                         return 0;
701                 }
702                 case VIDIOC_S_FREQUENCY:
703                 {
704                         struct v4l2_frequency *freq = arg;
705
706                         // Only no or one tuner for now
707                         if (!usbvision->have_tuner || freq->tuner)
708                                 return -EINVAL;
709
710                         usbvision->freq = freq->frequency;
711                         call_i2c_clients(usbvision, cmd, freq);
712                         PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
713                         return 0;
714                 }
715                 case VIDIOC_G_AUDIO:
716                 {
717                         struct v4l2_audio *v = arg;
718                         memset(v,0, sizeof(v));
719                         strcpy(v->name, "TV");
720                         PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
721                         return 0;
722                 }
723                 case VIDIOC_S_AUDIO:
724                 {
725                         struct v4l2_audio *v = arg;
726                         if(v->index) {
727                                 return -EINVAL;
728                         }
729                         PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
730                         return 0;
731                 }
732                 case VIDIOC_QUERYCTRL:
733                 {
734                         struct v4l2_queryctrl *ctrl = arg;
735                         int id=ctrl->id;
736
737                         memset(ctrl,0,sizeof(*ctrl));
738                         ctrl->id=id;
739
740                         call_i2c_clients(usbvision, cmd, arg);
741
742                         if (ctrl->type)
743                                 return 0;
744                         else
745                                 return -EINVAL;
746
747                         PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
748                 }
749                 case VIDIOC_G_CTRL:
750                 {
751                         struct v4l2_control *ctrl = arg;
752                         PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
753                         call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
754                         return 0;
755                 }
756                 case VIDIOC_S_CTRL:
757                 {
758                         struct v4l2_control *ctrl = arg;
759
760                         PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
761                         call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
762                         return 0;
763                 }
764                 case VIDIOC_REQBUFS:
765                 {
766                         struct v4l2_requestbuffers *vr = arg;
767                         int ret;
768
769                         RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
770
771                         // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
772                         if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
773                            (vr->memory != V4L2_MEMORY_MMAP))
774                                 return -EINVAL;
775
776                         if(usbvision->streaming == Stream_On) {
777                                 if ((ret = usbvision_stream_interrupt(usbvision)))
778                                     return ret;
779                         }
780
781                         usbvision_empty_framequeues(usbvision);
782
783                         usbvision->curFrame = NULL;
784
785                         PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
786                         return 0;
787                 }
788                 case VIDIOC_QUERYBUF:
789                 {
790                         struct v4l2_buffer *vb = arg;
791                         struct usbvision_frame *frame;
792
793                         // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
794
795                         if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
796                                 return -EINVAL;
797                         }
798                         if(vb->index>=USBVISION_NUMFRAMES)  {
799                                 return -EINVAL;
800                         }
801                         // Updating the corresponding frame state
802                         vb->flags = 0;
803                         frame = &usbvision->frame[vb->index];
804                         if(frame->grabstate >= FrameState_Ready)
805                                 vb->flags |= V4L2_BUF_FLAG_QUEUED;
806                         if(frame->grabstate >= FrameState_Done)
807                                 vb->flags |= V4L2_BUF_FLAG_DONE;
808                         if(frame->grabstate == FrameState_Unused)
809                                 vb->flags |= V4L2_BUF_FLAG_MAPPED;
810                         vb->memory = V4L2_MEMORY_MMAP;
811
812                         vb->m.offset = vb->index*usbvision->max_frame_size;
813
814                         vb->memory = V4L2_MEMORY_MMAP;
815                         vb->field = V4L2_FIELD_NONE;
816                         vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
817                         vb->timestamp = usbvision->frame[vb->index].timestamp;
818                         vb->sequence = usbvision->frame[vb->index].sequence;
819                         return 0;
820                 }
821                 case VIDIOC_QBUF:
822                 {
823                         struct v4l2_buffer *vb = arg;
824                         struct usbvision_frame *frame;
825                         unsigned long lock_flags;
826
827                         // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
828                         if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
829                                 return -EINVAL;
830                         }
831                         if(vb->index>=USBVISION_NUMFRAMES)  {
832                                 return -EINVAL;
833                         }
834
835                         frame = &usbvision->frame[vb->index];
836
837                         if (frame->grabstate != FrameState_Unused) {
838                                 return -EAGAIN;
839                         }
840
841                         /* Mark it as ready and enqueue frame */
842                         frame->grabstate = FrameState_Ready;
843                         frame->scanstate = ScanState_Scanning;
844                         frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
845
846                         vb->flags &= ~V4L2_BUF_FLAG_DONE;
847
848                         /* set v4l2_format index */
849                         frame->v4l2_format = usbvision->palette;
850
851                         spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
852                         list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
853                         spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
854
855                         PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
856                         return 0;
857                 }
858                 case VIDIOC_DQBUF:
859                 {
860                         struct v4l2_buffer *vb = arg;
861                         int ret;
862                         struct usbvision_frame *f;
863                         unsigned long lock_flags;
864
865                         if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
866                                 return -EINVAL;
867
868                         if (list_empty(&(usbvision->outqueue))) {
869                                 if (usbvision->streaming == Stream_Idle)
870                                         return -EINVAL;
871                                 ret = wait_event_interruptible
872                                         (usbvision->wait_frame,
873                                          !list_empty(&(usbvision->outqueue)));
874                                 if (ret)
875                                         return ret;
876                         }
877
878                         spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
879                         f = list_entry(usbvision->outqueue.next,
880                                        struct usbvision_frame, frame);
881                         list_del(usbvision->outqueue.next);
882                         spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
883
884                         f->grabstate = FrameState_Unused;
885
886                         vb->memory = V4L2_MEMORY_MMAP;
887                         vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
888                         vb->index = f->index;
889                         vb->sequence = f->sequence;
890                         vb->timestamp = f->timestamp;
891                         vb->field = V4L2_FIELD_NONE;
892                         vb->bytesused = f->scanlength;
893
894                         return 0;
895                 }
896                 case VIDIOC_STREAMON:
897                 {
898                         int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
899
900                         usbvision->streaming = Stream_On;
901
902                         call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
903
904                         PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
905
906                         return 0;
907                 }
908                 case VIDIOC_STREAMOFF:
909                 {
910                         int *type = arg;
911                         int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
912
913                         if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
914                                 return -EINVAL;
915
916                         if(usbvision->streaming == Stream_On) {
917                                 usbvision_stream_interrupt(usbvision);
918                                 // Stop all video streamings
919                                 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
920                         }
921                         usbvision_empty_framequeues(usbvision);
922
923                         PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
924                         return 0;
925                 }
926                 case VIDIOC_ENUM_FMT:
927                 {
928                         struct v4l2_fmtdesc *vfd = arg;
929
930                         if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
931                                 return -EINVAL;
932                         }
933                         vfd->flags = 0;
934                         vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
935                         strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
936                         vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
937                         memset(vfd->reserved, 0, sizeof(vfd->reserved));
938                         return 0;
939                 }
940                 case VIDIOC_G_FMT:
941                 {
942                         struct v4l2_format *vf = arg;
943
944                         switch (vf->type) {
945                                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
946                                 {
947                                         vf->fmt.pix.width = usbvision->curwidth;
948                                         vf->fmt.pix.height = usbvision->curheight;
949                                         vf->fmt.pix.pixelformat = usbvision->palette.format;
950                                         vf->fmt.pix.bytesperline =  usbvision->curwidth*usbvision->palette.bytes_per_pixel;
951                                         vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
952                                         vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
953                                         vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
954                                         PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
955                                                vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
956                                         return 0;
957                                 }
958                                 default:
959                                         PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
960                                         return -EINVAL;
961                         }
962                         return 0;
963                 }
964                 case VIDIOC_TRY_FMT:
965                 case VIDIOC_S_FMT:
966                 {
967                         struct v4l2_format *vf = arg;
968                         int formatIdx,ret;
969
970                         switch(vf->type) {
971                                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
972                                 {
973                                         /* Find requested format in available ones */
974                                         for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
975                                                 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
976                                                         usbvision->palette = usbvision_v4l2_format[formatIdx];
977                                                         break;
978                                                 }
979                                         }
980                                         /* robustness */
981                                         if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
982                                                 return -EINVAL;
983                                         }
984                                         RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
985                                         RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
986
987                                         vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
988                                         vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
989
990                                         if(cmd == VIDIOC_TRY_FMT) {
991                                                 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
992                                                vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
993                                                 return 0;
994                                         }
995
996                                         /* stop io in case it is already in progress */
997                                         if(usbvision->streaming == Stream_On) {
998                                                 if ((ret = usbvision_stream_interrupt(usbvision)))
999                                                         return ret;
1000                                         }
1001                                         usbvision_empty_framequeues(usbvision);
1002
1003                                         usbvision->curFrame = NULL;
1004
1005                                         // by now we are committed to the new data...
1006                                         down(&usbvision->lock);
1007                                         usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1008                                         up(&usbvision->lock);
1009
1010                                         PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1011                                                vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1012                                         return 0;
1013                                 }
1014                                 default:
1015                                         return -EINVAL;
1016                         }
1017                 }
1018                 default:
1019                         return -ENOIOCTLCMD;
1020         }
1021         return 0;
1022 }
1023
1024 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1025                        unsigned int cmd, unsigned long arg)
1026 {
1027         return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1028 }
1029
1030
1031 static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
1032                       size_t count, loff_t *ppos)
1033 {
1034         struct video_device *dev = video_devdata(file);
1035         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1036         int noblock = file->f_flags & O_NONBLOCK;
1037         unsigned long lock_flags;
1038
1039         int frmx = -1;
1040         int ret,i;
1041         struct usbvision_frame *frame;
1042
1043         PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1044
1045         if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1046                 return -EFAULT;
1047
1048         /* no stream is running, make it running ! */
1049         usbvision->streaming = Stream_On;
1050         call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1051
1052         /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1053         for(i=0;i<USBVISION_NUMFRAMES;i++) {
1054                 frame = &usbvision->frame[i];
1055                 if(frame->grabstate == FrameState_Unused) {
1056                         /* Mark it as ready and enqueue frame */
1057                         frame->grabstate = FrameState_Ready;
1058                         frame->scanstate = ScanState_Scanning;
1059                         frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
1060
1061                         /* set v4l2_format index */
1062                         frame->v4l2_format = usbvision->palette;
1063
1064                         spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1065                         list_add_tail(&frame->frame, &usbvision->inqueue);
1066                         spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1067                 }
1068         }
1069
1070         /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1071         if (list_empty(&(usbvision->outqueue))) {
1072                 if(noblock)
1073                         return -EAGAIN;
1074
1075                 ret = wait_event_interruptible
1076                         (usbvision->wait_frame,
1077                          !list_empty(&(usbvision->outqueue)));
1078                 if (ret)
1079                         return ret;
1080         }
1081
1082         spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1083         frame = list_entry(usbvision->outqueue.next,
1084                            struct usbvision_frame, frame);
1085         list_del(usbvision->outqueue.next);
1086         spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1087
1088         /* An error returns an empty frame */
1089         if (frame->grabstate == FrameState_Error) {
1090                 frame->bytes_read = 0;
1091                 return 0;
1092         }
1093
1094         PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1095                        frame->index, frame->bytes_read, frame->scanlength);
1096
1097         /* copy bytes to user space; we allow for partials reads */
1098         if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1099                 count = frame->scanlength - frame->bytes_read;
1100
1101         if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1102                 return -EFAULT;
1103         }
1104
1105         frame->bytes_read += count;
1106         PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1107                        (unsigned long)count, frame->bytes_read);
1108
1109         // For now, forget the frame if it has not been read in one shot.
1110 /*      if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1111                 frame->bytes_read = 0;
1112
1113                 /* Mark it as available to be used again. */
1114                 usbvision->frame[frmx].grabstate = FrameState_Unused;
1115 /*      } */
1116
1117         return count;
1118 }
1119
1120 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1121 {
1122         unsigned long size = vma->vm_end - vma->vm_start,
1123                 start = vma->vm_start;
1124         void *pos;
1125         u32 i;
1126
1127         struct video_device *dev = video_devdata(file);
1128         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1129
1130         down(&usbvision->lock);
1131
1132         if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1133                 up(&usbvision->lock);
1134                 return -EFAULT;
1135         }
1136
1137         if (!(vma->vm_flags & VM_WRITE) ||
1138             size != PAGE_ALIGN(usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel)) {
1139                 up(&usbvision->lock);
1140                 return -EINVAL;
1141         }
1142
1143         for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1144                 if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1145                         break;
1146         }
1147         if (i == USBVISION_NUMFRAMES) {
1148                 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1149                 up(&usbvision->lock);
1150                 return -EINVAL;
1151         }
1152
1153         /* VM_IO is eventually going to replace PageReserved altogether */
1154         vma->vm_flags |= VM_IO;
1155         vma->vm_flags |= VM_RESERVED;   /* avoid to swap out this VMA */
1156
1157         pos = usbvision->frame[i].data;
1158         while (size > 0) {
1159
1160                 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1161                         PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1162                         up(&usbvision->lock);
1163                         return -EAGAIN;
1164                 }
1165                 start += PAGE_SIZE;
1166                 pos += PAGE_SIZE;
1167                 size -= PAGE_SIZE;
1168         }
1169
1170         up(&usbvision->lock);
1171         return 0;
1172 }
1173
1174
1175 /*
1176  * Here comes the stuff for radio on usbvision based devices
1177  *
1178  */
1179 static int usbvision_radio_open(struct inode *inode, struct file *file)
1180 {
1181         struct video_device *dev = video_devdata(file);
1182         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1183         struct v4l2_frequency freq;
1184         int errCode = 0;
1185
1186         PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1187
1188         down(&usbvision->lock);
1189
1190         if (usbvision->user) {
1191                 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1192                 errCode = -EBUSY;
1193         }
1194         else {
1195                 if(PowerOnAtOpen) {
1196                         usbvision_reset_powerOffTimer(usbvision);
1197                         if (usbvision->power == 0) {
1198                                 usbvision_power_on(usbvision);
1199                                 usbvision_init_i2c(usbvision);
1200                         }
1201                 }
1202
1203                 // If so far no errors then we shall start the radio
1204                 usbvision->radio = 1;
1205                 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1206                 freq.frequency = 1517; //SWR3 @ 94.8MHz
1207                 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1208                 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1209                 usbvision->user++;
1210         }
1211
1212         if (errCode) {
1213                 if (PowerOnAtOpen) {
1214                         usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1215                         usbvision_power_off(usbvision);
1216                         usbvision->initialized = 0;
1217                 }
1218         }
1219         up(&usbvision->lock);
1220         return errCode;
1221 }
1222
1223
1224 static int usbvision_radio_close(struct inode *inode, struct file *file)
1225 {
1226         struct video_device *dev = video_devdata(file);
1227         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1228         int errCode = 0;
1229
1230         PDEBUG(DBG_IO, "");
1231
1232         down(&usbvision->lock);
1233
1234         usbvision_audio_off(usbvision);
1235         usbvision->radio=0;
1236         usbvision->user--;
1237
1238         if (PowerOnAtOpen) {
1239                 usbvision_set_powerOffTimer(usbvision);
1240                 usbvision->initialized = 0;
1241         }
1242
1243         up(&usbvision->lock);
1244
1245         if (usbvision->remove_pending) {
1246                 info("%s: Final disconnect", __FUNCTION__);
1247                 usbvision_release(usbvision);
1248         }
1249
1250
1251         PDEBUG(DBG_IO, "success");
1252
1253         return errCode;
1254 }
1255
1256 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1257                                  unsigned int cmd, void *arg)
1258 {
1259         struct video_device *dev = video_devdata(file);
1260         struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1261
1262         if (!USBVISION_IS_OPERATIONAL(usbvision))
1263                 return -EIO;
1264
1265         switch (cmd) {
1266                 case VIDIOC_QUERYCAP:
1267                 {
1268                         struct v4l2_capability *vc=arg;
1269
1270                         memset(vc, 0, sizeof(*vc));
1271                         strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1272                         strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1273                                 sizeof(vc->card));
1274                         strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1275                                 sizeof(vc->bus_info));
1276                         vc->version = USBVISION_DRIVER_VERSION;
1277                         vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1278                         PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1279                         return 0;
1280                 }
1281                 case VIDIOC_QUERYCTRL:
1282                 {
1283                         struct v4l2_queryctrl *ctrl = arg;
1284                         int id=ctrl->id;
1285
1286                         memset(ctrl,0,sizeof(*ctrl));
1287                         ctrl->id=id;
1288
1289                         call_i2c_clients(usbvision, cmd, arg);
1290                         PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1291
1292                         if (ctrl->type)
1293                                 return 0;
1294                         else
1295                                 return -EINVAL;
1296
1297                 }
1298                 case VIDIOC_G_CTRL:
1299                 {
1300                         struct v4l2_control *ctrl = arg;
1301
1302                         call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1303                         PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1304                         return 0;
1305                 }
1306                 case VIDIOC_S_CTRL:
1307                 {
1308                         struct v4l2_control *ctrl = arg;
1309
1310                         call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1311                         PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1312                         return 0;
1313                 }
1314                 case VIDIOC_G_TUNER:
1315                 {
1316                         struct v4l2_tuner *t = arg;
1317
1318                         if (t->index > 0)
1319                                 return -EINVAL;
1320
1321                         memset(t,0,sizeof(*t));
1322                         strcpy(t->name, "Radio");
1323                         t->type = V4L2_TUNER_RADIO;
1324
1325                         /* Let clients fill in the remainder of this struct */
1326                         call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1327                         PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1328                         return 0;
1329                 }
1330                 case VIDIOC_S_TUNER:
1331                 {
1332                         struct v4l2_tuner *vt = arg;
1333
1334                         // Only no or one tuner for now
1335                         if (!usbvision->have_tuner || vt->index)
1336                                 return -EINVAL;
1337                         /* let clients handle this */
1338                         call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1339
1340                         PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1341                         return 0;
1342                 }
1343                 case VIDIOC_G_AUDIO:
1344                 {
1345                         struct v4l2_audio *a = arg;
1346
1347                         memset(a,0,sizeof(*a));
1348                         strcpy(a->name,"Radio");
1349                         PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1350                         return 0;
1351                 }
1352                 case VIDIOC_S_AUDIO:
1353                 case VIDIOC_S_INPUT:
1354                 case VIDIOC_S_STD:
1355                 return 0;
1356
1357                 case VIDIOC_G_FREQUENCY:
1358                 {
1359                         struct v4l2_frequency *f = arg;
1360
1361                         memset(f,0,sizeof(*f));
1362
1363                         f->type = V4L2_TUNER_RADIO;
1364                         f->frequency = usbvision->freq;
1365                         call_i2c_clients(usbvision, cmd, f);
1366                         PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1367
1368                         return 0;
1369                 }
1370                 case VIDIOC_S_FREQUENCY:
1371                 {
1372                         struct v4l2_frequency *f = arg;
1373
1374                         if (f->tuner != 0)
1375                                 return -EINVAL;
1376                         usbvision->freq = f->frequency;
1377                         call_i2c_clients(usbvision, cmd, f);
1378                         PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1379
1380                         return 0;
1381                 }
1382                 default:
1383                 {
1384                         PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1385                         return -ENOIOCTLCMD;
1386                 }
1387         }
1388         return 0;
1389 }
1390
1391
1392 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1393                        unsigned int cmd, unsigned long arg)
1394 {
1395         return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1396 }
1397
1398
1399 /*
1400  * Here comes the stuff for vbi on usbvision based devices
1401  *
1402  */
1403 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1404 {
1405         /* TODO */
1406         return -EINVAL;
1407
1408 }
1409
1410 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1411 {
1412         /* TODO */
1413         return -EINVAL;
1414 }
1415
1416 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1417                                  unsigned int cmd, void *arg)
1418 {
1419         /* TODO */
1420         return -EINVAL;
1421 }
1422
1423 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1424                        unsigned int cmd, unsigned long arg)
1425 {
1426         return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1427 }
1428
1429
1430 //
1431 // Video registration stuff
1432 //
1433
1434 // Video template
1435 static struct file_operations usbvision_fops = {
1436         .owner             = THIS_MODULE,
1437         .open           = usbvision_v4l2_open,
1438         .release        = usbvision_v4l2_close,
1439         .read           = usbvision_v4l2_read,
1440         .mmap           = usbvision_v4l2_mmap,
1441         .ioctl          = usbvision_v4l2_ioctl,
1442         .llseek         = no_llseek,
1443 };
1444 static struct video_device usbvision_video_template = {
1445         .owner             = THIS_MODULE,
1446         .type           = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1447         .hardware       = VID_HARDWARE_USBVISION,
1448         .fops           = &usbvision_fops,
1449         .name           = "usbvision-video",
1450         .release        = video_device_release,
1451         .minor          = -1,
1452 };
1453
1454
1455 // Radio template
1456 static struct file_operations usbvision_radio_fops = {
1457         .owner             = THIS_MODULE,
1458         .open           = usbvision_radio_open,
1459         .release        = usbvision_radio_close,
1460         .ioctl          = usbvision_radio_ioctl,
1461         .llseek         = no_llseek,
1462 };
1463
1464 static struct video_device usbvision_radio_template=
1465 {
1466         .owner             = THIS_MODULE,
1467         .type           = VID_TYPE_TUNER,
1468         .hardware       = VID_HARDWARE_USBVISION,
1469         .fops           = &usbvision_radio_fops,
1470         .release        = video_device_release,
1471         .name           = "usbvision-radio",
1472         .minor          = -1,
1473 };
1474
1475
1476 // vbi template
1477 static struct file_operations usbvision_vbi_fops = {
1478         .owner             = THIS_MODULE,
1479         .open           = usbvision_vbi_open,
1480         .release        = usbvision_vbi_close,
1481         .ioctl          = usbvision_vbi_ioctl,
1482         .llseek         = no_llseek,
1483 };
1484
1485 static struct video_device usbvision_vbi_template=
1486 {
1487         .owner             = THIS_MODULE,
1488         .type           = VID_TYPE_TUNER,
1489         .hardware       = VID_HARDWARE_USBVISION,
1490         .fops           = &usbvision_vbi_fops,
1491         .release        = video_device_release,
1492         .name           = "usbvision-vbi",
1493         .minor          = -1,
1494 };
1495
1496
1497 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1498                                         struct video_device *vdev_template,
1499                                         char *name)
1500 {
1501         struct usb_device *usb_dev = usbvision->dev;
1502         struct video_device *vdev;
1503
1504         if (usb_dev == NULL) {
1505                 err("%s: usbvision->dev is not set", __FUNCTION__);
1506                 return NULL;
1507         }
1508
1509         vdev = video_device_alloc();
1510         if (NULL == vdev) {
1511                 return NULL;
1512         }
1513         *vdev = *vdev_template;
1514 //      vdev->minor   = -1;
1515         vdev->dev     = &usb_dev->dev;
1516         snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1517         video_set_drvdata(vdev, usbvision);
1518         return vdev;
1519 }
1520
1521 // unregister video4linux devices
1522 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1523 {
1524         // vbi Device:
1525         if (usbvision->vbi) {
1526                 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1527                 if (usbvision->vbi->minor != -1) {
1528                         video_unregister_device(usbvision->vbi);
1529                 }
1530                 else {
1531                         video_device_release(usbvision->vbi);
1532                 }
1533                 usbvision->vbi = NULL;
1534         }
1535
1536         // Radio Device:
1537         if (usbvision->rdev) {
1538                 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1539                 if (usbvision->rdev->minor != -1) {
1540                         video_unregister_device(usbvision->rdev);
1541                 }
1542                 else {
1543                         video_device_release(usbvision->rdev);
1544                 }
1545                 usbvision->rdev = NULL;
1546         }
1547
1548         // Video Device:
1549         if (usbvision->vdev) {
1550                 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1551                 if (usbvision->vdev->minor != -1) {
1552                         video_unregister_device(usbvision->vdev);
1553                 }
1554                 else {
1555                         video_device_release(usbvision->vdev);
1556                 }
1557                 usbvision->vdev = NULL;
1558         }
1559 }
1560
1561 // register video4linux devices
1562 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1563 {
1564         // Video Device:
1565         usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1566         if (usbvision->vdev == NULL) {
1567                 goto err_exit;
1568         }
1569         if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1570                 goto err_exit;
1571         }
1572         info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
1573
1574         // Radio Device:
1575         if (usbvision_device_data[usbvision->DevModel].Radio) {
1576                 // usbvision has radio
1577                 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1578                 if (usbvision->rdev == NULL) {
1579                         goto err_exit;
1580                 }
1581                 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1582                         goto err_exit;
1583                 }
1584                 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
1585         }
1586         // vbi Device:
1587         if (usbvision_device_data[usbvision->DevModel].vbi) {
1588                 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1589                 if (usbvision->vdev == NULL) {
1590                         goto err_exit;
1591                 }
1592                 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1593                         goto err_exit;
1594                 }
1595                 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
1596         }
1597         // all done
1598         return 0;
1599
1600  err_exit:
1601         err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1602         usbvision_unregister_video(usbvision);
1603         return -1;
1604 }
1605
1606 /*
1607  * usbvision_alloc()
1608  *
1609  * This code allocates the struct usb_usbvision. It is filled with default values.
1610  *
1611  * Returns NULL on error, a pointer to usb_usbvision else.
1612  *
1613  */
1614 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1615 {
1616         struct usb_usbvision *usbvision;
1617
1618         if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1619                 goto err_exit;
1620         }
1621
1622         usbvision->dev = dev;
1623
1624         init_MUTEX(&usbvision->lock);   /* to 1 == available */
1625
1626         // prepare control urb for control messages during interrupts
1627         usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1628         if (usbvision->ctrlUrb == NULL) {
1629                 goto err_exit;
1630         }
1631         init_waitqueue_head(&usbvision->ctrlUrb_wq);
1632         init_MUTEX(&usbvision->ctrlUrbLock);    /* to 1 == available */
1633
1634         usbvision_init_powerOffTimer(usbvision);
1635
1636         return usbvision;
1637
1638 err_exit:
1639         if (usbvision && usbvision->ctrlUrb) {
1640                 usb_free_urb(usbvision->ctrlUrb);
1641         }
1642         if (usbvision) {
1643                 kfree(usbvision);
1644         }
1645         return NULL;
1646 }
1647
1648 /*
1649  * usbvision_release()
1650  *
1651  * This code does final release of struct usb_usbvision. This happens
1652  * after the device is disconnected -and- all clients closed their files.
1653  *
1654  */
1655 static void usbvision_release(struct usb_usbvision *usbvision)
1656 {
1657         PDEBUG(DBG_PROBE, "");
1658
1659         down(&usbvision->lock);
1660
1661         usbvision_reset_powerOffTimer(usbvision);
1662
1663         usbvision->initialized = 0;
1664
1665         up(&usbvision->lock);
1666
1667         usbvision_remove_sysfs(usbvision->vdev);
1668         usbvision_unregister_video(usbvision);
1669
1670         if (usbvision->ctrlUrb) {
1671                 usb_free_urb(usbvision->ctrlUrb);
1672         }
1673
1674         kfree(usbvision);
1675
1676         PDEBUG(DBG_PROBE, "success");
1677 }
1678
1679
1680 /******************************** usb interface *****************************************/
1681
1682 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1683 {
1684         int model,i;
1685
1686         if (usbvision == NULL)
1687                 return;
1688
1689         model = usbvision->DevModel;
1690         usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1691
1692         if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1693                 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1694         } else {
1695                 usbvision->Vin_Reg2_Preset = 0;
1696         }
1697
1698         for (i = 0; i < TVNORMS; i++)
1699                 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1700                         break;
1701         if (i == TVNORMS)
1702                 i = 0;
1703         usbvision->tvnorm = &tvnorms[i];        /* set default norm */
1704
1705         usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1706         usbvision->ctl_input = 0;
1707
1708         /* This should be here to make i2c clients to be able to register */
1709         usbvision_audio_off(usbvision); //first switch off audio
1710         if (!PowerOnAtOpen) {
1711                 usbvision_power_on(usbvision);  //and then power up the noisy tuner
1712                 usbvision_init_i2c(usbvision);
1713         }
1714 }
1715
1716 /*
1717  * usbvision_probe()
1718  *
1719  * This procedure queries device descriptor and accepts the interface
1720  * if it looks like USBVISION video device
1721  *
1722  */
1723 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1724 {
1725         struct usb_device *dev = interface_to_usbdev(intf);
1726         __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1727         const struct usb_host_interface *interface;
1728         struct usb_usbvision *usbvision = NULL;
1729         const struct usb_endpoint_descriptor *endpoint;
1730         int model;
1731
1732         PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1733                                         dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1734         /* Is it an USBVISION video dev? */
1735         model = 0;
1736         for(model = 0; usbvision_device_data[model].idVendor; model++) {
1737                 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1738                         continue;
1739                 }
1740                 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1741                         continue;
1742                 }
1743
1744                 info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
1745                 break;
1746         }
1747
1748         if (usbvision_device_data[model].idVendor == 0) {
1749                 return -ENODEV; //no matching device
1750         }
1751         if (usbvision_device_data[model].Interface >= 0) {
1752                 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1753         }
1754         else {
1755                 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1756         }
1757         endpoint = &interface->endpoint[1].desc;
1758         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1759                 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1760                 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1761                 return -ENODEV;
1762         }
1763         if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1764                 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1765                 return -ENODEV;
1766         }
1767
1768         usb_get_dev(dev);
1769
1770         if ((usbvision = usbvision_alloc(dev)) == NULL) {
1771                 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1772                 return -ENOMEM;
1773         }
1774         if (dev->descriptor.bNumConfigurations > 1) {
1775                 usbvision->bridgeType = BRIDGE_NT1004;
1776         }
1777         else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1778                 usbvision->bridgeType = BRIDGE_NT1005;
1779         }
1780         else {
1781                 usbvision->bridgeType = BRIDGE_NT1003;
1782         }
1783         PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1784
1785         down(&usbvision->lock);
1786
1787         usbvision->nr = usbvision_nr++;
1788
1789         usbvision->have_tuner = usbvision_device_data[model].Tuner;
1790         if (usbvision->have_tuner) {
1791                 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1792         }
1793
1794         usbvision->tuner_addr = ADDR_UNSET;
1795
1796         usbvision->DevModel = model;
1797         usbvision->remove_pending = 0;
1798         usbvision->iface = ifnum;
1799         usbvision->ifaceAltInactive = 0;
1800         usbvision->ifaceAltActive = 1;
1801         usbvision->video_endp = endpoint->bEndpointAddress;
1802         usbvision->isocPacketSize = 0;
1803         usbvision->usb_bandwidth = 0;
1804         usbvision->user = 0;
1805         usbvision->streaming = Stream_Off;
1806         usbvision_register_video(usbvision);
1807         usbvision_configure_video(usbvision);
1808         up(&usbvision->lock);
1809
1810
1811         usb_set_intfdata (intf, usbvision);
1812         usbvision_create_sysfs(usbvision->vdev);
1813
1814         PDEBUG(DBG_PROBE, "success");
1815         return 0;
1816 }
1817
1818
1819 /*
1820  * usbvision_disconnect()
1821  *
1822  * This procedure stops all driver activity, deallocates interface-private
1823  * structure (pointed by 'ptr') and after that driver should be removable
1824  * with no ill consequences.
1825  *
1826  */
1827 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1828 {
1829         struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1830
1831         PDEBUG(DBG_PROBE, "");
1832
1833         if (usbvision == NULL) {
1834                 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1835                 return;
1836         }
1837         usb_set_intfdata (intf, NULL);
1838
1839         down(&usbvision->lock);
1840
1841         // At this time we ask to cancel outstanding URBs
1842         usbvision_stop_isoc(usbvision);
1843
1844         if (usbvision->power) {
1845                 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1846                 usbvision_power_off(usbvision);
1847         }
1848         usbvision->remove_pending = 1;  // Now all ISO data will be ignored
1849
1850         usb_put_dev(usbvision->dev);
1851         usbvision->dev = NULL;  // USB device is no more
1852
1853         up(&usbvision->lock);
1854
1855         if (usbvision->user) {
1856                 info("%s: In use, disconnect pending", __FUNCTION__);
1857                 wake_up_interruptible(&usbvision->wait_frame);
1858                 wake_up_interruptible(&usbvision->wait_stream);
1859         }
1860         else {
1861                 usbvision_release(usbvision);
1862         }
1863
1864         PDEBUG(DBG_PROBE, "success");
1865
1866 }
1867
1868 static struct usb_driver usbvision_driver = {
1869         .name           = "usbvision",
1870         .id_table       = usbvision_table,
1871         .probe          = usbvision_probe,
1872         .disconnect     = usbvision_disconnect
1873 };
1874
1875 /*
1876  * customdevice_process()
1877  *
1878  * This procedure preprocesses CustomDevice parameter if any
1879  *
1880  */
1881 static void customdevice_process(void)
1882 {
1883         usbvision_device_data[0]=usbvision_device_data[1];
1884         usbvision_table[0]=usbvision_table[1];
1885
1886         if(CustomDevice)
1887         {
1888                 char *parse=CustomDevice;
1889
1890                 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1891
1892                 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1893                 usbvision_device_data[0].idVendor;
1894                 usbvision_device_data[0].idProduct;
1895                 usbvision_device_data[0].Interface;
1896                 usbvision_device_data[0].Codec;
1897                 usbvision_device_data[0].VideoChannels;
1898                 usbvision_device_data[0].VideoNorm;
1899                 usbvision_device_data[0].AudioChannels;
1900                 usbvision_device_data[0].Radio;
1901                 usbvision_device_data[0].Tuner;
1902                 usbvision_device_data[0].TunerType;
1903                 usbvision_device_data[0].Vin_Reg1;
1904                 usbvision_device_data[0].Vin_Reg2;
1905                 usbvision_device_data[0].X_Offset;
1906                 usbvision_device_data[0].Y_Offset;
1907                 usbvision_device_data[0].Dvi_yuv;
1908                 usbvision_device_data[0].ModelString;
1909                 */
1910
1911                 rmspace(parse);
1912                 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1913
1914                 parse+=2;
1915                 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1916                 goto2next(parse);
1917                 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1918                 parse+=2;
1919                 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1920                 goto2next(parse);
1921                 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1922                 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1923                 goto2next(parse);
1924                 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1925                 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1926                 goto2next(parse);
1927                 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1928                 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1929                 goto2next(parse);
1930                 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1931
1932                 switch(*parse)
1933                 {
1934                         case 'P':
1935                                 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
1936                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1937                                 break;
1938
1939                         case 'S':
1940                                 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
1941                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
1942                                 break;
1943
1944                         case 'N':
1945                                 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
1946                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
1947                                 break;
1948
1949                         default:
1950                                 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
1951                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1952                                 break;
1953                 }
1954                 goto2next(parse);
1955
1956                 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
1957                 goto2next(parse);
1958                 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
1959                 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
1960                 goto2next(parse);
1961                 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
1962                 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
1963                 goto2next(parse);
1964                 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
1965                 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
1966                 goto2next(parse);
1967                 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
1968                 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
1969                 goto2next(parse);
1970                 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
1971                 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
1972                 goto2next(parse);
1973                 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
1974                 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
1975                 goto2next(parse);
1976                 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
1977                 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
1978                 goto2next(parse);
1979                 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
1980                 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
1981                 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
1982
1983                 //add to usbvision_table also
1984                 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
1985                 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
1986                 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
1987
1988         }
1989 }
1990
1991
1992
1993 /*
1994  * usbvision_init()
1995  *
1996  * This code is run to initialize the driver.
1997  *
1998  */
1999 static int __init usbvision_init(void)
2000 {
2001         int errCode;
2002
2003         PDEBUG(DBG_PROBE, "");
2004
2005         PDEBUG(DBG_IOCTL, "IOCTL   debugging is enabled [video]");
2006         PDEBUG(DBG_IO,  "IO      debugging is enabled [video]");
2007         PDEBUG(DBG_PROBE, "PROBE   debugging is enabled [video]");
2008         PDEBUG(DBG_MMAP, "MMAP    debugging is enabled [video]");
2009
2010         /* disable planar mode support unless compression enabled */
2011         if (isocMode != ISOC_MODE_COMPRESS ) {
2012                 // FIXME : not the right way to set supported flag
2013                 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2014                 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2015         }
2016
2017         customdevice_process();
2018
2019         errCode = usb_register(&usbvision_driver);
2020
2021         if (errCode == 0) {
2022                 info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
2023                 PDEBUG(DBG_PROBE, "success");
2024         }
2025         return errCode;
2026 }
2027
2028 static void __exit usbvision_exit(void)
2029 {
2030  PDEBUG(DBG_PROBE, "");
2031
2032  usb_deregister(&usbvision_driver);
2033  PDEBUG(DBG_PROBE, "success");
2034 }
2035
2036 module_init(usbvision_init);
2037 module_exit(usbvision_exit);
2038
2039 /*
2040  * Overrides for Emacs so that we follow Linus's tabbing style.
2041  * ---------------------------------------------------------------------------
2042  * Local variables:
2043  * c-basic-offset: 8
2044  * End:
2045  */