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