V4L/DVB (4941): Remove LINUX_VERSION_CODE and fix identations
[linux-2.6] / 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/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
113 static int usbvision_nr = 0;                    // sequential number of usbvision device
114
115 static 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 */
127 static 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
146 static void usbvision_release(struct usb_usbvision *usbvision);
147
148 // Default initalization of device driver parameters
149 static int isocMode = ISOC_MODE_COMPRESS;               // Set the default format for ISOC endpoint
150 static int video_debug = 0;                             // Set the default Debug Mode of the device driver
151 static int PowerOnAtOpen = 1;                           // Set the default device to power on at startup
152 static int video_nr = -1;                               // Sequential Number of Video Device
153 static int radio_nr = -1;                               // Sequential Number of Radio Device
154 static int vbi_nr = -1;                                 // Sequential Number of VBI Device
155 static char *CustomDevice=NULL;                         // Set as nothing....
156
157 // Grab parameters for the device driver
158
159 #if defined(module_param)                               // Showing parameters under SYSFS
160 module_param(isocMode, int, 0444);
161 module_param(video_debug, int, 0444);
162 module_param(PowerOnAtOpen, int, 0444);
163 module_param(video_nr, int, 0444);
164 module_param(radio_nr, int, 0444);
165 module_param(vbi_nr, int, 0444);
166 module_param(CustomDevice, charp, 0444);
167 #else                                                   // Old Style
168 MODULE_PARAM(isocMode, "i");
169 MODULE_PARM(video_debug, "i");                          // Grab the Debug Mode of the device driver
170 MODULE_PARM(adjustCompression, "i");                    // Grab the compression to be adaptive
171 MODULE_PARM(PowerOnAtOpen, "i");                        // Grab the device to power on at startup
172 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.
173 MODULE_PARM(video_nr, "i");                             // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
174 MODULE_PARM(radio_nr, "i");                             // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
175 MODULE_PARM(vbi_nr, "i");                               // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
176 MODULE_PARM(CustomDevice, "s");                         // .... CustomDevice
177 #endif
178
179 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
180 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
181 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened.  Default: 1 (On)");
182 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
183 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
184 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX).  Default: -1 (autodetect)");
185 MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device.  Default: null");
186
187
188 // Misc stuff
189 MODULE_AUTHOR(DRIVER_AUTHOR);
190 MODULE_DESCRIPTION(DRIVER_DESC);
191 MODULE_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
206 static 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
212 static ssize_t show_version(struct class_device *cd, char *buf)
213 {
214         return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
215 }
216 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
217
218 static 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 }
224 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
225
226 static 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 }
236 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
237
238 static 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 }
248 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
249
250 static 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 }
260 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
261
262 static 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 }
272 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
273
274 static 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 }
280 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
281
282 static 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 }
288 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
289
290 static 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 }
296 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
297
298 static 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
314 static 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  */
338 static 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  */
428 static 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  */
473 static 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
1021 static 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
1028 static 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
1117 static 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  */
1176 static 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
1221 static 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
1253 static 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
1389 static 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  */
1400 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1401 {
1402         /* TODO */
1403         return -EINVAL;
1404
1405 }
1406
1407 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1408 {
1409         /* TODO */
1410         return -EINVAL;
1411 }
1412
1413 static 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
1420 static 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
1432 static struct file_operations usbvision_fops = {
1433         .owner             = THIS_MODULE,
1434         .open           = usbvision_v4l2_open,
1435         .release        = usbvision_v4l2_close,
1436         .read           = usbvision_v4l2_read,
1437         .mmap           = usbvision_v4l2_mmap,
1438         .ioctl          = usbvision_v4l2_ioctl,
1439         .llseek         = no_llseek,
1440 };
1441 static struct video_device usbvision_video_template = {
1442         .owner             = THIS_MODULE,
1443         .type           = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1444         .hardware       = VID_HARDWARE_USBVISION,
1445         .fops           = &usbvision_fops,
1446         .name           = "usbvision-video",
1447         .release        = video_device_release,
1448         .minor          = -1,
1449 };
1450
1451
1452 // Radio template
1453 static struct file_operations usbvision_radio_fops = {
1454         .owner             = THIS_MODULE,
1455         .open           = usbvision_radio_open,
1456         .release        = usbvision_radio_close,
1457         .ioctl          = usbvision_radio_ioctl,
1458         .llseek         = no_llseek,
1459 };
1460
1461 static struct video_device usbvision_radio_template=
1462 {
1463         .owner             = THIS_MODULE,
1464         .type           = VID_TYPE_TUNER,
1465         .hardware       = VID_HARDWARE_USBVISION,
1466         .fops           = &usbvision_radio_fops,
1467         .release        = video_device_release,
1468         .name           = "usbvision-radio",
1469         .minor          = -1,
1470 };
1471
1472
1473 // vbi template
1474 static struct file_operations usbvision_vbi_fops = {
1475         .owner             = THIS_MODULE,
1476         .open           = usbvision_vbi_open,
1477         .release        = usbvision_vbi_close,
1478         .ioctl          = usbvision_vbi_ioctl,
1479         .llseek         = no_llseek,
1480 };
1481
1482 static struct video_device usbvision_vbi_template=
1483 {
1484         .owner             = THIS_MODULE,
1485         .type           = VID_TYPE_TUNER,
1486         .hardware       = VID_HARDWARE_USBVISION,
1487         .fops           = &usbvision_vbi_fops,
1488         .release        = video_device_release,
1489         .name           = "usbvision-vbi",
1490         .minor          = -1,
1491 };
1492
1493
1494 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1495                                         struct video_device *vdev_template,
1496                                         char *name)
1497 {
1498         struct usb_device *usb_dev = usbvision->dev;
1499         struct video_device *vdev;
1500
1501         if (usb_dev == NULL) {
1502                 err("%s: usbvision->dev is not set", __FUNCTION__);
1503                 return NULL;
1504         }
1505
1506         vdev = video_device_alloc();
1507         if (NULL == vdev) {
1508                 return NULL;
1509         }
1510         *vdev = *vdev_template;
1511 //      vdev->minor   = -1;
1512         vdev->dev     = &usb_dev->dev;
1513         snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1514         video_set_drvdata(vdev, usbvision);
1515         return vdev;
1516 }
1517
1518 // unregister video4linux devices
1519 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1520 {
1521         // vbi Device:
1522         if (usbvision->vbi) {
1523                 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1524                 if (usbvision->vbi->minor != -1) {
1525                         video_unregister_device(usbvision->vbi);
1526                 }
1527                 else {
1528                         video_device_release(usbvision->vbi);
1529                 }
1530                 usbvision->vbi = NULL;
1531         }
1532
1533         // Radio Device:
1534         if (usbvision->rdev) {
1535                 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1536                 if (usbvision->rdev->minor != -1) {
1537                         video_unregister_device(usbvision->rdev);
1538                 }
1539                 else {
1540                         video_device_release(usbvision->rdev);
1541                 }
1542                 usbvision->rdev = NULL;
1543         }
1544
1545         // Video Device:
1546         if (usbvision->vdev) {
1547                 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1548                 if (usbvision->vdev->minor != -1) {
1549                         video_unregister_device(usbvision->vdev);
1550                 }
1551                 else {
1552                         video_device_release(usbvision->vdev);
1553                 }
1554                 usbvision->vdev = NULL;
1555         }
1556 }
1557
1558 // register video4linux devices
1559 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1560 {
1561         // Video Device:
1562         usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1563         if (usbvision->vdev == NULL) {
1564                 goto err_exit;
1565         }
1566         if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1567                 goto err_exit;
1568         }
1569         info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
1570
1571         // Radio Device:
1572         if (usbvision_device_data[usbvision->DevModel].Radio) {
1573                 // usbvision has radio
1574                 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1575                 if (usbvision->rdev == NULL) {
1576                         goto err_exit;
1577                 }
1578                 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1579                         goto err_exit;
1580                 }
1581                 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
1582         }
1583         // vbi Device:
1584         if (usbvision_device_data[usbvision->DevModel].vbi) {
1585                 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1586                 if (usbvision->vdev == NULL) {
1587                         goto err_exit;
1588                 }
1589                 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1590                         goto err_exit;
1591                 }
1592                 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
1593         }
1594         // all done
1595         return 0;
1596
1597  err_exit:
1598         err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1599         usbvision_unregister_video(usbvision);
1600         return -1;
1601 }
1602
1603 /*
1604  * usbvision_alloc()
1605  *
1606  * This code allocates the struct usb_usbvision. It is filled with default values.
1607  *
1608  * Returns NULL on error, a pointer to usb_usbvision else.
1609  *
1610  */
1611 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1612 {
1613         struct usb_usbvision *usbvision;
1614
1615         if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1616                 goto err_exit;
1617         }
1618
1619         usbvision->dev = dev;
1620
1621         init_MUTEX(&usbvision->lock);   /* to 1 == available */
1622
1623         // prepare control urb for control messages during interrupts
1624         usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1625         if (usbvision->ctrlUrb == NULL) {
1626                 goto err_exit;
1627         }
1628         init_waitqueue_head(&usbvision->ctrlUrb_wq);
1629         init_MUTEX(&usbvision->ctrlUrbLock);    /* to 1 == available */
1630
1631         usbvision_init_powerOffTimer(usbvision);
1632
1633         return usbvision;
1634
1635 err_exit:
1636         if (usbvision && usbvision->ctrlUrb) {
1637                 usb_free_urb(usbvision->ctrlUrb);
1638         }
1639         if (usbvision) {
1640                 kfree(usbvision);
1641         }
1642         return NULL;
1643 }
1644
1645 /*
1646  * usbvision_release()
1647  *
1648  * This code does final release of struct usb_usbvision. This happens
1649  * after the device is disconnected -and- all clients closed their files.
1650  *
1651  */
1652 static void usbvision_release(struct usb_usbvision *usbvision)
1653 {
1654         PDEBUG(DBG_PROBE, "");
1655
1656         down(&usbvision->lock);
1657
1658         usbvision_reset_powerOffTimer(usbvision);
1659
1660         usbvision->initialized = 0;
1661
1662         up(&usbvision->lock);
1663
1664         usbvision_remove_sysfs(usbvision->vdev);
1665         usbvision_unregister_video(usbvision);
1666
1667         if (usbvision->ctrlUrb) {
1668                 usb_free_urb(usbvision->ctrlUrb);
1669         }
1670
1671         kfree(usbvision);
1672
1673         PDEBUG(DBG_PROBE, "success");
1674 }
1675
1676
1677 /******************************** usb interface *****************************************/
1678
1679 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1680 {
1681         int model,i;
1682
1683         if (usbvision == NULL)
1684                 return;
1685
1686         model = usbvision->DevModel;
1687         usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1688
1689         if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1690                 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1691         } else {
1692                 usbvision->Vin_Reg2_Preset = 0;
1693         }
1694
1695         for (i = 0; i < TVNORMS; i++)
1696                 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1697                         break;
1698         if (i == TVNORMS)
1699                 i = 0;
1700         usbvision->tvnorm = &tvnorms[i];        /* set default norm */
1701
1702         usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1703         usbvision->ctl_input = 0;
1704
1705         /* This should be here to make i2c clients to be able to register */
1706         usbvision_audio_off(usbvision); //first switch off audio
1707         if (!PowerOnAtOpen) {
1708                 usbvision_power_on(usbvision);  //and then power up the noisy tuner
1709                 usbvision_init_i2c(usbvision);
1710         }
1711 }
1712
1713 /*
1714  * usbvision_probe()
1715  *
1716  * This procedure queries device descriptor and accepts the interface
1717  * if it looks like USBVISION video device
1718  *
1719  */
1720 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1721 {
1722         struct usb_device *dev = interface_to_usbdev(intf);
1723         __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1724         const struct usb_host_interface *interface;
1725         struct usb_usbvision *usbvision = NULL;
1726         const struct usb_endpoint_descriptor *endpoint;
1727         int model;
1728
1729         PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1730                                         dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1731         /* Is it an USBVISION video dev? */
1732         model = 0;
1733         for(model = 0; usbvision_device_data[model].idVendor; model++) {
1734                 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1735                         continue;
1736                 }
1737                 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1738                         continue;
1739                 }
1740
1741                 info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
1742                 break;
1743         }
1744
1745         if (usbvision_device_data[model].idVendor == 0) {
1746                 return -ENODEV; //no matching device
1747         }
1748         if (usbvision_device_data[model].Interface >= 0) {
1749                 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1750         }
1751         else {
1752                 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1753         }
1754         endpoint = &interface->endpoint[1].desc;
1755         if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1756                 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1757                 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1758                 return -ENODEV;
1759         }
1760         if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1761                 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1762                 return -ENODEV;
1763         }
1764
1765         usb_get_dev(dev);
1766
1767         if ((usbvision = usbvision_alloc(dev)) == NULL) {
1768                 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1769                 return -ENOMEM;
1770         }
1771         if (dev->descriptor.bNumConfigurations > 1) {
1772                 usbvision->bridgeType = BRIDGE_NT1004;
1773         }
1774         else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1775                 usbvision->bridgeType = BRIDGE_NT1005;
1776         }
1777         else {
1778                 usbvision->bridgeType = BRIDGE_NT1003;
1779         }
1780         PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1781
1782         down(&usbvision->lock);
1783
1784         usbvision->nr = usbvision_nr++;
1785
1786         usbvision->have_tuner = usbvision_device_data[model].Tuner;
1787         if (usbvision->have_tuner) {
1788                 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1789         }
1790
1791         usbvision->tuner_addr = ADDR_UNSET;
1792
1793         usbvision->DevModel = model;
1794         usbvision->remove_pending = 0;
1795         usbvision->iface = ifnum;
1796         usbvision->ifaceAltInactive = 0;
1797         usbvision->ifaceAltActive = 1;
1798         usbvision->video_endp = endpoint->bEndpointAddress;
1799         usbvision->isocPacketSize = 0;
1800         usbvision->usb_bandwidth = 0;
1801         usbvision->user = 0;
1802         usbvision->streaming = Stream_Off;
1803         usbvision_register_video(usbvision);
1804         usbvision_configure_video(usbvision);
1805         up(&usbvision->lock);
1806
1807
1808         usb_set_intfdata (intf, usbvision);
1809         usbvision_create_sysfs(usbvision->vdev);
1810
1811         PDEBUG(DBG_PROBE, "success");
1812         return 0;
1813 }
1814
1815
1816 /*
1817  * usbvision_disconnect()
1818  *
1819  * This procedure stops all driver activity, deallocates interface-private
1820  * structure (pointed by 'ptr') and after that driver should be removable
1821  * with no ill consequences.
1822  *
1823  */
1824 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1825 {
1826         struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1827
1828         PDEBUG(DBG_PROBE, "");
1829
1830         if (usbvision == NULL) {
1831                 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1832                 return;
1833         }
1834         usb_set_intfdata (intf, NULL);
1835
1836         down(&usbvision->lock);
1837
1838         // At this time we ask to cancel outstanding URBs
1839         usbvision_stop_isoc(usbvision);
1840
1841         if (usbvision->power) {
1842                 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1843                 usbvision_power_off(usbvision);
1844         }
1845         usbvision->remove_pending = 1;  // Now all ISO data will be ignored
1846
1847         usb_put_dev(usbvision->dev);
1848         usbvision->dev = NULL;  // USB device is no more
1849
1850         up(&usbvision->lock);
1851
1852         if (usbvision->user) {
1853                 info("%s: In use, disconnect pending", __FUNCTION__);
1854                 wake_up_interruptible(&usbvision->wait_frame);
1855                 wake_up_interruptible(&usbvision->wait_stream);
1856         }
1857         else {
1858                 usbvision_release(usbvision);
1859         }
1860
1861         PDEBUG(DBG_PROBE, "success");
1862
1863 }
1864
1865 static struct usb_driver usbvision_driver = {
1866         .name           = "usbvision",
1867         .id_table       = usbvision_table,
1868         .probe          = usbvision_probe,
1869         .disconnect     = usbvision_disconnect
1870 };
1871
1872 /*
1873  * customdevice_process()
1874  *
1875  * This procedure preprocesses CustomDevice parameter if any
1876  *
1877  */
1878 void customdevice_process(void)
1879 {
1880         usbvision_device_data[0]=usbvision_device_data[1];
1881         usbvision_table[0]=usbvision_table[1];
1882
1883         if(CustomDevice)
1884         {
1885                 char *parse=CustomDevice;
1886
1887                 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1888
1889                 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1890                 usbvision_device_data[0].idVendor;
1891                 usbvision_device_data[0].idProduct;
1892                 usbvision_device_data[0].Interface;
1893                 usbvision_device_data[0].Codec;
1894                 usbvision_device_data[0].VideoChannels;
1895                 usbvision_device_data[0].VideoNorm;
1896                 usbvision_device_data[0].AudioChannels;
1897                 usbvision_device_data[0].Radio;
1898                 usbvision_device_data[0].Tuner;
1899                 usbvision_device_data[0].TunerType;
1900                 usbvision_device_data[0].Vin_Reg1;
1901                 usbvision_device_data[0].Vin_Reg2;
1902                 usbvision_device_data[0].X_Offset;
1903                 usbvision_device_data[0].Y_Offset;
1904                 usbvision_device_data[0].Dvi_yuv;
1905                 usbvision_device_data[0].ModelString;
1906                 */
1907
1908                 rmspace(parse);
1909                 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1910
1911                 parse+=2;
1912                 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1913                 goto2next(parse);
1914                 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1915                 parse+=2;
1916                 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1917                 goto2next(parse);
1918                 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1919                 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1920                 goto2next(parse);
1921                 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1922                 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1923                 goto2next(parse);
1924                 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1925                 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1926                 goto2next(parse);
1927                 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1928
1929                 switch(*parse)
1930                 {
1931                         case 'P':
1932                                 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
1933                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1934                                 break;
1935
1936                         case 'S':
1937                                 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
1938                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
1939                                 break;
1940
1941                         case 'N':
1942                                 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
1943                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
1944                                 break;
1945
1946                         default:
1947                                 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
1948                                 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1949                                 break;
1950                 }
1951                 goto2next(parse);
1952
1953                 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
1954                 goto2next(parse);
1955                 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
1956                 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
1957                 goto2next(parse);
1958                 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
1959                 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
1960                 goto2next(parse);
1961                 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
1962                 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
1963                 goto2next(parse);
1964                 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
1965                 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
1966                 goto2next(parse);
1967                 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
1968                 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
1969                 goto2next(parse);
1970                 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
1971                 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
1972                 goto2next(parse);
1973                 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
1974                 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
1975                 goto2next(parse);
1976                 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
1977                 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
1978                 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
1979
1980                 //add to usbvision_table also
1981                 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
1982                 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
1983                 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
1984
1985         }
1986 }
1987
1988
1989
1990 /*
1991  * usbvision_init()
1992  *
1993  * This code is run to initialize the driver.
1994  *
1995  */
1996 static int __init usbvision_init(void)
1997 {
1998         int errCode;
1999
2000         PDEBUG(DBG_PROBE, "");
2001
2002         PDEBUG(DBG_IOCTL, "IOCTL   debugging is enabled [video]");
2003         PDEBUG(DBG_IO,  "IO      debugging is enabled [video]");
2004         PDEBUG(DBG_PROBE, "PROBE   debugging is enabled [video]");
2005         PDEBUG(DBG_FUNC, "FUNC    debugging is enabled [video]");
2006
2007         /* disable planar mode support unless compression enabled */
2008         if (isocMode != ISOC_MODE_COMPRESS ) {
2009                 // FIXME : not the right way to set supported flag
2010                 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2011                 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2012         }
2013
2014         customdevice_process();
2015
2016         errCode = usb_register(&usbvision_driver);
2017
2018         if (errCode == 0) {
2019                 info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
2020                 PDEBUG(DBG_PROBE, "success");
2021         }
2022         return errCode;
2023 }
2024
2025 static void __exit usbvision_exit(void)
2026 {
2027  PDEBUG(DBG_PROBE, "");
2028
2029  usb_deregister(&usbvision_driver);
2030  PDEBUG(DBG_PROBE, "success");
2031 }
2032
2033 module_init(usbvision_init);
2034 module_exit(usbvision_exit);
2035
2036 /*
2037  * Overrides for Emacs so that we follow Linus's tabbing style.
2038  * ---------------------------------------------------------------------------
2039  * Local variables:
2040  * c-basic-offset: 8
2041  * End:
2042  */