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