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