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