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