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