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