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