Linux Audio

Check our new training course

Loading...
   1/*
   2 * USB USBVISION Video device driver 0.9.10
   3 *
   4 *
   5 *
   6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
   7 *
   8 * This module is part of usbvision driver project.
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * (at your option) any later version.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23 *
  24 * Let's call the version 0.... until compression decoding is completely
  25 * implemented.
  26 *
  27 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
  28 * It was based on USB CPiA driver written by Peter Pregler,
  29 * Scott J. Bertin and Johannes Erdfelt
  30 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
  31 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
  32 * Updates to driver completed by Dwaine P. Garden
  33 *
  34 *
  35 * TODO:
  36 *     - use submit_urb for all setup packets
  37 *     - Fix memory settings for nt1004. It is 4 times as big as the
  38 *       nt1003 memory.
  39 *     - Add audio on endpoint 3 for nt1004 chip.
  40 *         Seems impossible, needs a codec interface.  Which one?
  41 *     - Clean up the driver.
  42 *     - optimization for performance.
  43 *     - Add Videotext capability (VBI).  Working on it.....
  44 *     - Check audio for other devices
  45 *
  46 */
  47
  48#include <linux/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/highmem.h>
  54#include <linux/vmalloc.h>
  55#include <linux/module.h>
  56#include <linux/init.h>
  57#include <linux/spinlock.h>
  58#include <linux/io.h>
  59#include <linux/videodev2.h>
  60#include <linux/i2c.h>
  61
  62#include <media/saa7115.h>
  63#include <media/v4l2-common.h>
  64#include <media/v4l2-ioctl.h>
  65#include <media/tuner.h>
  66
  67#include <linux/workqueue.h>
  68
  69#include "usbvision.h"
  70#include "usbvision-cards.h"
  71
  72#define DRIVER_AUTHOR					\
  73	"Joerg Heckenbach <joerg@heckenbach-aw.de>, "	\
  74	"Dwaine Garden <DwaineGarden@rogers.com>"
  75#define DRIVER_NAME "usbvision"
  76#define DRIVER_ALIAS "USBVision"
  77#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
  78#define DRIVER_LICENSE "GPL"
  79#define USBVISION_VERSION_STRING "0.9.11"
  80
  81#define	ENABLE_HEXDUMP	0	/* Enable if you need it */
  82
  83
  84#ifdef USBVISION_DEBUG
  85	#define PDEBUG(level, fmt, args...) { \
  86		if (video_debug & (level)) \
  87			printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
  88				__func__, __LINE__ , ## args); \
  89	}
  90#else
  91	#define PDEBUG(level, fmt, args...) do {} while (0)
  92#endif
  93
  94#define DBG_IO		(1 << 1)
  95#define DBG_PROBE	(1 << 2)
  96#define DBG_MMAP	(1 << 3)
  97
  98/* String operations */
  99#define rmspace(str)	while (*str == ' ') str++;
 100#define goto2next(str)	while (*str != ' ') str++; while (*str == ' ') str++;
 101
 102
 103/* sequential number of usbvision device */
 104static int usbvision_nr;
 105
 106static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
 107	{ 1, 1,  8, V4L2_PIX_FMT_GREY    , "GREY" },
 108	{ 1, 2, 16, V4L2_PIX_FMT_RGB565  , "RGB565" },
 109	{ 1, 3, 24, V4L2_PIX_FMT_RGB24   , "RGB24" },
 110	{ 1, 4, 32, V4L2_PIX_FMT_RGB32   , "RGB32" },
 111	{ 1, 2, 16, V4L2_PIX_FMT_RGB555  , "RGB555" },
 112	{ 1, 2, 16, V4L2_PIX_FMT_YUYV    , "YUV422" },
 113	{ 1, 2, 12, V4L2_PIX_FMT_YVU420  , "YUV420P" }, /* 1.5 ! */
 114	{ 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
 115};
 116
 117/* Function prototypes */
 118static void usbvision_release(struct usb_usbvision *usbvision);
 119
 120/* Default initialization of device driver parameters */
 121/* Set the default format for ISOC endpoint */
 122static int isoc_mode = ISOC_MODE_COMPRESS;
 123/* Set the default Debug Mode of the device driver */
 124static int video_debug;
 125/* Set the default device to power on at startup */
 126static int power_on_at_open = 1;
 127/* Sequential Number of Video Device */
 128static int video_nr = -1;
 129/* Sequential Number of Radio Device */
 130static int radio_nr = -1;
 131
 132/* Grab parameters for the device driver */
 133
 134/* Showing parameters under SYSFS */
 135module_param(isoc_mode, int, 0444);
 136module_param(video_debug, int, 0444);
 137module_param(power_on_at_open, int, 0444);
 138module_param(video_nr, int, 0444);
 139module_param(radio_nr, int, 0444);
 140
 141MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
 142MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
 143MODULE_PARM_DESC(power_on_at_open, " Set the default device to power on when device is opened.  Default: 1 (On)");
 144MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
 145MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
 146
 147
 148/* Misc stuff */
 149MODULE_AUTHOR(DRIVER_AUTHOR);
 150MODULE_DESCRIPTION(DRIVER_DESC);
 151MODULE_LICENSE(DRIVER_LICENSE);
 152MODULE_VERSION(USBVISION_VERSION_STRING);
 153MODULE_ALIAS(DRIVER_ALIAS);
 154
 155
 156/*****************************************************************************/
 157/* SYSFS Code - Copied from the stv680.c usb module.			     */
 158/* Device information is located at /sys/class/video4linux/video0            */
 159/* Device parameters information is located at /sys/module/usbvision         */
 160/* Device USB Information is located at                                      */
 161/*   /sys/bus/usb/drivers/USBVision Video Grabber                            */
 162/*****************************************************************************/
 163
 164#define YES_NO(x) ((x) ? "Yes" : "No")
 165
 166static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
 167{
 168	struct video_device *vdev =
 169		container_of(cd, struct video_device, dev);
 170	return video_get_drvdata(vdev);
 171}
 172
 173static ssize_t show_version(struct device *cd,
 174			    struct device_attribute *attr, char *buf)
 175{
 176	return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
 177}
 178static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
 179
 180static ssize_t show_model(struct device *cd,
 181			  struct device_attribute *attr, char *buf)
 182{
 183	struct video_device *vdev =
 184		container_of(cd, struct video_device, dev);
 185	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 186	return sprintf(buf, "%s\n",
 187		       usbvision_device_data[usbvision->dev_model].model_string);
 188}
 189static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
 190
 191static ssize_t show_hue(struct device *cd,
 192			struct device_attribute *attr, char *buf)
 193{
 194	struct video_device *vdev =
 195		container_of(cd, struct video_device, dev);
 196	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 197	struct v4l2_control ctrl;
 198	ctrl.id = V4L2_CID_HUE;
 199	ctrl.value = 0;
 200	if (usbvision->user)
 201		call_all(usbvision, core, g_ctrl, &ctrl);
 202	return sprintf(buf, "%d\n", ctrl.value);
 203}
 204static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
 205
 206static ssize_t show_contrast(struct device *cd,
 207			     struct device_attribute *attr, char *buf)
 208{
 209	struct video_device *vdev =
 210		container_of(cd, struct video_device, dev);
 211	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 212	struct v4l2_control ctrl;
 213	ctrl.id = V4L2_CID_CONTRAST;
 214	ctrl.value = 0;
 215	if (usbvision->user)
 216		call_all(usbvision, core, g_ctrl, &ctrl);
 217	return sprintf(buf, "%d\n", ctrl.value);
 218}
 219static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
 220
 221static ssize_t show_brightness(struct device *cd,
 222			       struct device_attribute *attr, char *buf)
 223{
 224	struct video_device *vdev =
 225		container_of(cd, struct video_device, dev);
 226	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 227	struct v4l2_control ctrl;
 228	ctrl.id = V4L2_CID_BRIGHTNESS;
 229	ctrl.value = 0;
 230	if (usbvision->user)
 231		call_all(usbvision, core, g_ctrl, &ctrl);
 232	return sprintf(buf, "%d\n", ctrl.value);
 233}
 234static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
 235
 236static ssize_t show_saturation(struct device *cd,
 237			       struct device_attribute *attr, char *buf)
 238{
 239	struct video_device *vdev =
 240		container_of(cd, struct video_device, dev);
 241	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 242	struct v4l2_control ctrl;
 243	ctrl.id = V4L2_CID_SATURATION;
 244	ctrl.value = 0;
 245	if (usbvision->user)
 246		call_all(usbvision, core, g_ctrl, &ctrl);
 247	return sprintf(buf, "%d\n", ctrl.value);
 248}
 249static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
 250
 251static ssize_t show_streaming(struct device *cd,
 252			      struct device_attribute *attr, char *buf)
 253{
 254	struct video_device *vdev =
 255		container_of(cd, struct video_device, dev);
 256	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 257	return sprintf(buf, "%s\n",
 258		       YES_NO(usbvision->streaming == stream_on ? 1 : 0));
 259}
 260static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
 261
 262static ssize_t show_compression(struct device *cd,
 263				struct device_attribute *attr, char *buf)
 264{
 265	struct video_device *vdev =
 266		container_of(cd, struct video_device, dev);
 267	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 268	return sprintf(buf, "%s\n",
 269		       YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
 270}
 271static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
 272
 273static ssize_t show_device_bridge(struct device *cd,
 274				  struct device_attribute *attr, char *buf)
 275{
 276	struct video_device *vdev =
 277		container_of(cd, struct video_device, dev);
 278	struct usb_usbvision *usbvision = video_get_drvdata(vdev);
 279	return sprintf(buf, "%d\n", usbvision->bridge_type);
 280}
 281static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
 282
 283static void usbvision_create_sysfs(struct video_device *vdev)
 284{
 285	int res;
 286
 287	if (!vdev)
 288		return;
 289	do {
 290		res = device_create_file(&vdev->dev, &dev_attr_version);
 291		if (res < 0)
 292			break;
 293		res = device_create_file(&vdev->dev, &dev_attr_model);
 294		if (res < 0)
 295			break;
 296		res = device_create_file(&vdev->dev, &dev_attr_hue);
 297		if (res < 0)
 298			break;
 299		res = device_create_file(&vdev->dev, &dev_attr_contrast);
 300		if (res < 0)
 301			break;
 302		res = device_create_file(&vdev->dev, &dev_attr_brightness);
 303		if (res < 0)
 304			break;
 305		res = device_create_file(&vdev->dev, &dev_attr_saturation);
 306		if (res < 0)
 307			break;
 308		res = device_create_file(&vdev->dev, &dev_attr_streaming);
 309		if (res < 0)
 310			break;
 311		res = device_create_file(&vdev->dev, &dev_attr_compression);
 312		if (res < 0)
 313			break;
 314		res = device_create_file(&vdev->dev, &dev_attr_bridge);
 315		if (res >= 0)
 316			return;
 317	} while (0);
 318
 319	dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
 320}
 321
 322static void usbvision_remove_sysfs(struct video_device *vdev)
 323{
 324	if (vdev) {
 325		device_remove_file(&vdev->dev, &dev_attr_version);
 326		device_remove_file(&vdev->dev, &dev_attr_model);
 327		device_remove_file(&vdev->dev, &dev_attr_hue);
 328		device_remove_file(&vdev->dev, &dev_attr_contrast);
 329		device_remove_file(&vdev->dev, &dev_attr_brightness);
 330		device_remove_file(&vdev->dev, &dev_attr_saturation);
 331		device_remove_file(&vdev->dev, &dev_attr_streaming);
 332		device_remove_file(&vdev->dev, &dev_attr_compression);
 333		device_remove_file(&vdev->dev, &dev_attr_bridge);
 334	}
 335}
 336
 337/*
 338 * usbvision_open()
 339 *
 340 * This is part of Video 4 Linux API. The driver can be opened by one
 341 * client only (checks internal counter 'usbvision->user'). The procedure
 342 * then allocates buffers needed for video processing.
 343 *
 344 */
 345static int usbvision_v4l2_open(struct file *file)
 346{
 347	struct usb_usbvision *usbvision = video_drvdata(file);
 348	int err_code = 0;
 349
 350	PDEBUG(DBG_IO, "open");
 351
 352	usbvision_reset_power_off_timer(usbvision);
 353
 354	if (usbvision->user)
 355		err_code = -EBUSY;
 356	else {
 357		/* Allocate memory for the scratch ring buffer */
 358		err_code = usbvision_scratch_alloc(usbvision);
 359		if (isoc_mode == ISOC_MODE_COMPRESS) {
 360			/* Allocate intermediate decompression buffers
 361			   only if needed */
 362			err_code = usbvision_decompress_alloc(usbvision);
 363		}
 364		if (err_code) {
 365			/* Deallocate all buffers if trouble */
 366			usbvision_scratch_free(usbvision);
 367			usbvision_decompress_free(usbvision);
 368		}
 369	}
 370
 371	/* If so far no errors then we shall start the camera */
 372	if (!err_code) {
 373		if (usbvision->power == 0) {
 374			usbvision_power_on(usbvision);
 375			usbvision_i2c_register(usbvision);
 376		}
 377
 378		/* Send init sequence only once, it's large! */
 379		if (!usbvision->initialized) {
 380			int setup_ok = 0;
 381			setup_ok = usbvision_setup(usbvision, isoc_mode);
 382			if (setup_ok)
 383				usbvision->initialized = 1;
 384			else
 385				err_code = -EBUSY;
 386		}
 387
 388		if (!err_code) {
 389			usbvision_begin_streaming(usbvision);
 390			err_code = usbvision_init_isoc(usbvision);
 391			/* device must be initialized before isoc transfer */
 392			usbvision_muxsel(usbvision, 0);
 393			usbvision->user++;
 394		} else {
 395			if (power_on_at_open) {
 396				usbvision_i2c_unregister(usbvision);
 397				usbvision_power_off(usbvision);
 398				usbvision->initialized = 0;
 399			}
 400		}
 401	}
 402
 403	/* prepare queues */
 404	usbvision_empty_framequeues(usbvision);
 405
 406	PDEBUG(DBG_IO, "success");
 407	return err_code;
 408}
 409
 410/*
 411 * usbvision_v4l2_close()
 412 *
 413 * This is part of Video 4 Linux API. The procedure
 414 * stops streaming and deallocates all buffers that were earlier
 415 * allocated in usbvision_v4l2_open().
 416 *
 417 */
 418static int usbvision_v4l2_close(struct file *file)
 419{
 420	struct usb_usbvision *usbvision = video_drvdata(file);
 421
 422	PDEBUG(DBG_IO, "close");
 423
 424	usbvision_audio_off(usbvision);
 425	usbvision_restart_isoc(usbvision);
 426	usbvision_stop_isoc(usbvision);
 427
 428	usbvision_decompress_free(usbvision);
 429	usbvision_frames_free(usbvision);
 430	usbvision_empty_framequeues(usbvision);
 431	usbvision_scratch_free(usbvision);
 432
 433	usbvision->user--;
 434
 435	if (power_on_at_open) {
 436		/* power off in a little while
 437		   to avoid off/on every close/open short sequences */
 438		usbvision_set_power_off_timer(usbvision);
 439		usbvision->initialized = 0;
 440	}
 441
 442	if (usbvision->remove_pending) {
 443		printk(KERN_INFO "%s: Final disconnect\n", __func__);
 444		usbvision_release(usbvision);
 445	}
 446
 447	PDEBUG(DBG_IO, "success");
 448	return 0;
 449}
 450
 451
 452/*
 453 * usbvision_ioctl()
 454 *
 455 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
 456 *
 457 */
 458#ifdef CONFIG_VIDEO_ADV_DEBUG
 459static int vidioc_g_register(struct file *file, void *priv,
 460				struct v4l2_dbg_register *reg)
 461{
 462	struct usb_usbvision *usbvision = video_drvdata(file);
 463	int err_code;
 464
 465	if (!v4l2_chip_match_host(&reg->match))
 466		return -EINVAL;
 467	/* NT100x has a 8-bit register space */
 468	err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
 469	if (err_code < 0) {
 470		dev_err(&usbvision->vdev->dev,
 471			"%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
 472				__func__, err_code);
 473		return err_code;
 474	}
 475	reg->val = err_code;
 476	reg->size = 1;
 477	return 0;
 478}
 479
 480static int vidioc_s_register(struct file *file, void *priv,
 481				struct v4l2_dbg_register *reg)
 482{
 483	struct usb_usbvision *usbvision = video_drvdata(file);
 484	int err_code;
 485
 486	if (!v4l2_chip_match_host(&reg->match))
 487		return -EINVAL;
 488	/* NT100x has a 8-bit register space */
 489	err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
 490	if (err_code < 0) {
 491		dev_err(&usbvision->vdev->dev,
 492			"%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
 493				__func__, err_code);
 494		return err_code;
 495	}
 496	return 0;
 497}
 498#endif
 499
 500static int vidioc_querycap(struct file *file, void  *priv,
 501					struct v4l2_capability *vc)
 502{
 503	struct usb_usbvision *usbvision = video_drvdata(file);
 504
 505	strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
 506	strlcpy(vc->card,
 507		usbvision_device_data[usbvision->dev_model].model_string,
 508		sizeof(vc->card));
 509	usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
 510	vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
 511		V4L2_CAP_AUDIO |
 512		V4L2_CAP_READWRITE |
 513		V4L2_CAP_STREAMING |
 514		(usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
 515	return 0;
 516}
 517
 518static int vidioc_enum_input(struct file *file, void *priv,
 519				struct v4l2_input *vi)
 520{
 521	struct usb_usbvision *usbvision = video_drvdata(file);
 522	int chan;
 523
 524	if (vi->index >= usbvision->video_inputs)
 525		return -EINVAL;
 526	if (usbvision->have_tuner)
 527		chan = vi->index;
 528	else
 529		chan = vi->index + 1; /* skip Television string*/
 530
 531	/* Determine the requested input characteristics
 532	   specific for each usbvision card model */
 533	switch (chan) {
 534	case 0:
 535		if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
 536			strcpy(vi->name, "White Video Input");
 537		} else {
 538			strcpy(vi->name, "Television");
 539			vi->type = V4L2_INPUT_TYPE_TUNER;
 540			vi->audioset = 1;
 541			vi->tuner = chan;
 542			vi->std = USBVISION_NORMS;
 543		}
 544		break;
 545	case 1:
 546		vi->type = V4L2_INPUT_TYPE_CAMERA;
 547		if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
 548			strcpy(vi->name, "Green Video Input");
 549		else
 550			strcpy(vi->name, "Composite Video Input");
 551		vi->std = V4L2_STD_PAL;
 552		break;
 553	case 2:
 554		vi->type = V4L2_INPUT_TYPE_CAMERA;
 555		if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
 556			strcpy(vi->name, "Yellow Video Input");
 557		else
 558			strcpy(vi->name, "S-Video Input");
 559		vi->std = V4L2_STD_PAL;
 560		break;
 561	case 3:
 562		vi->type = V4L2_INPUT_TYPE_CAMERA;
 563		strcpy(vi->name, "Red Video Input");
 564		vi->std = V4L2_STD_PAL;
 565		break;
 566	}
 567	return 0;
 568}
 569
 570static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
 571{
 572	struct usb_usbvision *usbvision = video_drvdata(file);
 573
 574	*input = usbvision->ctl_input;
 575	return 0;
 576}
 577
 578static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
 579{
 580	struct usb_usbvision *usbvision = video_drvdata(file);
 581
 582	if (input >= usbvision->video_inputs)
 583		return -EINVAL;
 584
 585	usbvision_muxsel(usbvision, input);
 586	usbvision_set_input(usbvision);
 587	usbvision_set_output(usbvision,
 588			     usbvision->curwidth,
 589			     usbvision->curheight);
 590	return 0;
 591}
 592
 593static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
 594{
 595	struct usb_usbvision *usbvision = video_drvdata(file);
 596
 597	usbvision->tvnorm_id = *id;
 598
 599	call_all(usbvision, core, s_std, usbvision->tvnorm_id);
 600	/* propagate the change to the decoder */
 601	usbvision_muxsel(usbvision, usbvision->ctl_input);
 602
 603	return 0;
 604}
 605
 606static int vidioc_g_tuner(struct file *file, void *priv,
 607				struct v4l2_tuner *vt)
 608{
 609	struct usb_usbvision *usbvision = video_drvdata(file);
 610
 611	if (!usbvision->have_tuner || vt->index)	/* Only tuner 0 */
 612		return -EINVAL;
 613	if (usbvision->radio) {
 614		strcpy(vt->name, "Radio");
 615		vt->type = V4L2_TUNER_RADIO;
 616	} else {
 617		strcpy(vt->name, "Television");
 618	}
 619	/* Let clients fill in the remainder of this struct */
 620	call_all(usbvision, tuner, g_tuner, vt);
 621
 622	return 0;
 623}
 624
 625static int vidioc_s_tuner(struct file *file, void *priv,
 626				struct v4l2_tuner *vt)
 627{
 628	struct usb_usbvision *usbvision = video_drvdata(file);
 629
 630	/* Only no or one tuner for now */
 631	if (!usbvision->have_tuner || vt->index)
 632		return -EINVAL;
 633	/* let clients handle this */
 634	call_all(usbvision, tuner, s_tuner, vt);
 635
 636	return 0;
 637}
 638
 639static int vidioc_g_frequency(struct file *file, void *priv,
 640				struct v4l2_frequency *freq)
 641{
 642	struct usb_usbvision *usbvision = video_drvdata(file);
 643
 644	freq->tuner = 0; /* Only one tuner */
 645	if (usbvision->radio)
 646		freq->type = V4L2_TUNER_RADIO;
 647	else
 648		freq->type = V4L2_TUNER_ANALOG_TV;
 649	freq->frequency = usbvision->freq;
 650
 651	return 0;
 652}
 653
 654static int vidioc_s_frequency(struct file *file, void *priv,
 655				struct v4l2_frequency *freq)
 656{
 657	struct usb_usbvision *usbvision = video_drvdata(file);
 658
 659	/* Only no or one tuner for now */
 660	if (!usbvision->have_tuner || freq->tuner)
 661		return -EINVAL;
 662
 663	usbvision->freq = freq->frequency;
 664	call_all(usbvision, tuner, s_frequency, freq);
 665
 666	return 0;
 667}
 668
 669static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
 670{
 671	struct usb_usbvision *usbvision = video_drvdata(file);
 672
 673	if (usbvision->radio)
 674		strcpy(a->name, "Radio");
 675	else
 676		strcpy(a->name, "TV");
 677
 678	return 0;
 679}
 680
 681static int vidioc_s_audio(struct file *file, void *fh,
 682			  struct v4l2_audio *a)
 683{
 684	if (a->index)
 685		return -EINVAL;
 686	return 0;
 687}
 688
 689static int vidioc_queryctrl(struct file *file, void *priv,
 690			    struct v4l2_queryctrl *ctrl)
 691{
 692	struct usb_usbvision *usbvision = video_drvdata(file);
 693
 694	call_all(usbvision, core, queryctrl, ctrl);
 695
 696	if (!ctrl->type)
 697		return -EINVAL;
 698
 699	return 0;
 700}
 701
 702static int vidioc_g_ctrl(struct file *file, void *priv,
 703				struct v4l2_control *ctrl)
 704{
 705	struct usb_usbvision *usbvision = video_drvdata(file);
 706
 707	call_all(usbvision, core, g_ctrl, ctrl);
 708	return 0;
 709}
 710
 711static int vidioc_s_ctrl(struct file *file, void *priv,
 712				struct v4l2_control *ctrl)
 713{
 714	struct usb_usbvision *usbvision = video_drvdata(file);
 715
 716	call_all(usbvision, core, s_ctrl, ctrl);
 717	return 0;
 718}
 719
 720static int vidioc_reqbufs(struct file *file,
 721			   void *priv, struct v4l2_requestbuffers *vr)
 722{
 723	struct usb_usbvision *usbvision = video_drvdata(file);
 724	int ret;
 725
 726	RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
 727
 728	/* Check input validity:
 729	   the user must do a VIDEO CAPTURE and MMAP method. */
 730	if (vr->memory != V4L2_MEMORY_MMAP)
 731		return -EINVAL;
 732
 733	if (usbvision->streaming == stream_on) {
 734		ret = usbvision_stream_interrupt(usbvision);
 735		if (ret)
 736			return ret;
 737	}
 738
 739	usbvision_frames_free(usbvision);
 740	usbvision_empty_framequeues(usbvision);
 741	vr->count = usbvision_frames_alloc(usbvision, vr->count);
 742
 743	usbvision->cur_frame = NULL;
 744
 745	return 0;
 746}
 747
 748static int vidioc_querybuf(struct file *file,
 749			    void *priv, struct v4l2_buffer *vb)
 750{
 751	struct usb_usbvision *usbvision = video_drvdata(file);
 752	struct usbvision_frame *frame;
 753
 754	/* FIXME : must control
 755	   that buffers are mapped (VIDIOC_REQBUFS has been called) */
 756	if (vb->index >= usbvision->num_frames)
 757		return -EINVAL;
 758	/* Updating the corresponding frame state */
 759	vb->flags = 0;
 760	frame = &usbvision->frame[vb->index];
 761	if (frame->grabstate >= frame_state_ready)
 762		vb->flags |= V4L2_BUF_FLAG_QUEUED;
 763	if (frame->grabstate >= frame_state_done)
 764		vb->flags |= V4L2_BUF_FLAG_DONE;
 765	if (frame->grabstate == frame_state_unused)
 766		vb->flags |= V4L2_BUF_FLAG_MAPPED;
 767	vb->memory = V4L2_MEMORY_MMAP;
 768
 769	vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
 770
 771	vb->memory = V4L2_MEMORY_MMAP;
 772	vb->field = V4L2_FIELD_NONE;
 773	vb->length = usbvision->curwidth *
 774		usbvision->curheight *
 775		usbvision->palette.bytes_per_pixel;
 776	vb->timestamp = usbvision->frame[vb->index].timestamp;
 777	vb->sequence = usbvision->frame[vb->index].sequence;
 778	return 0;
 779}
 780
 781static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 782{
 783	struct usb_usbvision *usbvision = video_drvdata(file);
 784	struct usbvision_frame *frame;
 785	unsigned long lock_flags;
 786
 787	/* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
 788	if (vb->index >= usbvision->num_frames)
 789		return -EINVAL;
 790
 791	frame = &usbvision->frame[vb->index];
 792
 793	if (frame->grabstate != frame_state_unused)
 794		return -EAGAIN;
 795
 796	/* Mark it as ready and enqueue frame */
 797	frame->grabstate = frame_state_ready;
 798	frame->scanstate = scan_state_scanning;
 799	frame->scanlength = 0;	/* Accumulated in usbvision_parse_data() */
 800
 801	vb->flags &= ~V4L2_BUF_FLAG_DONE;
 802
 803	/* set v4l2_format index */
 804	frame->v4l2_format = usbvision->palette;
 805
 806	spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 807	list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
 808	spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
 809
 810	return 0;
 811}
 812
 813static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 814{
 815	struct usb_usbvision *usbvision = video_drvdata(file);
 816	int ret;
 817	struct usbvision_frame *f;
 818	unsigned long lock_flags;
 819
 820	if (list_empty(&(usbvision->outqueue))) {
 821		if (usbvision->streaming == stream_idle)
 822			return -EINVAL;
 823		ret = wait_event_interruptible
 824			(usbvision->wait_frame,
 825			 !list_empty(&(usbvision->outqueue)));
 826		if (ret)
 827			return ret;
 828	}
 829
 830	spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 831	f = list_entry(usbvision->outqueue.next,
 832		       struct usbvision_frame, frame);
 833	list_del(usbvision->outqueue.next);
 834	spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
 835
 836	f->grabstate = frame_state_unused;
 837
 838	vb->memory = V4L2_MEMORY_MMAP;
 839	vb->flags = V4L2_BUF_FLAG_MAPPED |
 840		V4L2_BUF_FLAG_QUEUED |
 841		V4L2_BUF_FLAG_DONE;
 842	vb->index = f->index;
 843	vb->sequence = f->sequence;
 844	vb->timestamp = f->timestamp;
 845	vb->field = V4L2_FIELD_NONE;
 846	vb->bytesused = f->scanlength;
 847
 848	return 0;
 849}
 850
 851static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 852{
 853	struct usb_usbvision *usbvision = video_drvdata(file);
 854
 855	usbvision->streaming = stream_on;
 856	call_all(usbvision, video, s_stream, 1);
 857
 858	return 0;
 859}
 860
 861static int vidioc_streamoff(struct file *file,
 862			    void *priv, enum v4l2_buf_type type)
 863{
 864	struct usb_usbvision *usbvision = video_drvdata(file);
 865
 866	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 867		return -EINVAL;
 868
 869	if (usbvision->streaming == stream_on) {
 870		usbvision_stream_interrupt(usbvision);
 871		/* Stop all video streamings */
 872		call_all(usbvision, video, s_stream, 0);
 873	}
 874	usbvision_empty_framequeues(usbvision);
 875
 876	return 0;
 877}
 878
 879static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 880					struct v4l2_fmtdesc *vfd)
 881{
 882	if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
 883		return -EINVAL;
 884	strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
 885	vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
 886	return 0;
 887}
 888
 889static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 890					struct v4l2_format *vf)
 891{
 892	struct usb_usbvision *usbvision = video_drvdata(file);
 893	vf->fmt.pix.width = usbvision->curwidth;
 894	vf->fmt.pix.height = usbvision->curheight;
 895	vf->fmt.pix.pixelformat = usbvision->palette.format;
 896	vf->fmt.pix.bytesperline =
 897		usbvision->curwidth * usbvision->palette.bytes_per_pixel;
 898	vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
 899	vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 900	vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
 901
 902	return 0;
 903}
 904
 905static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 906			       struct v4l2_format *vf)
 907{
 908	struct usb_usbvision *usbvision = video_drvdata(file);
 909	int format_idx;
 910
 911	/* Find requested format in available ones */
 912	for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
 913		if (vf->fmt.pix.pixelformat ==
 914		   usbvision_v4l2_format[format_idx].format) {
 915			usbvision->palette = usbvision_v4l2_format[format_idx];
 916			break;
 917		}
 918	}
 919	/* robustness */
 920	if (format_idx == USBVISION_SUPPORTED_PALETTES)
 921		return -EINVAL;
 922	RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
 923	RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
 924
 925	vf->fmt.pix.bytesperline = vf->fmt.pix.width*
 926		usbvision->palette.bytes_per_pixel;
 927	vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
 928
 929	return 0;
 930}
 931
 932static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 933			       struct v4l2_format *vf)
 934{
 935	struct usb_usbvision *usbvision = video_drvdata(file);
 936	int ret;
 937
 938	ret = vidioc_try_fmt_vid_cap(file, priv, vf);
 939	if (ret)
 940		return ret;
 941
 942	/* stop io in case it is already in progress */
 943	if (usbvision->streaming == stream_on) {
 944		ret = usbvision_stream_interrupt(usbvision);
 945		if (ret)
 946			return ret;
 947	}
 948	usbvision_frames_free(usbvision);
 949	usbvision_empty_framequeues(usbvision);
 950
 951	usbvision->cur_frame = NULL;
 952
 953	/* by now we are committed to the new data... */
 954	usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
 955
 956	return 0;
 957}
 958
 959static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
 960		      size_t count, loff_t *ppos)
 961{
 962	struct usb_usbvision *usbvision = video_drvdata(file);
 963	int noblock = file->f_flags & O_NONBLOCK;
 964	unsigned long lock_flags;
 965	int ret, i;
 966	struct usbvision_frame *frame;
 967
 968	PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
 969	       (unsigned long)count, noblock);
 970
 971	if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
 972		return -EFAULT;
 973
 974	/* This entry point is compatible with the mmap routines
 975	   so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
 976	   to get frames or call read on the device. */
 977	if (!usbvision->num_frames) {
 978		/* First, allocate some frames to work with
 979		   if this has not been done with VIDIOC_REQBUF */
 980		usbvision_frames_free(usbvision);
 981		usbvision_empty_framequeues(usbvision);
 982		usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
 983	}
 984
 985	if (usbvision->streaming != stream_on) {
 986		/* no stream is running, make it running ! */
 987		usbvision->streaming = stream_on;
 988		call_all(usbvision, video, s_stream, 1);
 989	}
 990
 991	/* Then, enqueue as many frames as possible
 992	   (like a user of VIDIOC_QBUF would do) */
 993	for (i = 0; i < usbvision->num_frames; i++) {
 994		frame = &usbvision->frame[i];
 995		if (frame->grabstate == frame_state_unused) {
 996			/* Mark it as ready and enqueue frame */
 997			frame->grabstate = frame_state_ready;
 998			frame->scanstate = scan_state_scanning;
 999			/* Accumulated in usbvision_parse_data() */
1000			frame->scanlength = 0;
1001
1002			/* set v4l2_format index */
1003			frame->v4l2_format = usbvision->palette;
1004
1005			spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1006			list_add_tail(&frame->frame, &usbvision->inqueue);
1007			spin_unlock_irqrestore(&usbvision->queue_lock,
1008					       lock_flags);
1009		}
1010	}
1011
1012	/* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1013	if (list_empty(&(usbvision->outqueue))) {
1014		if (noblock)
1015			return -EAGAIN;
1016
1017		ret = wait_event_interruptible
1018			(usbvision->wait_frame,
1019			 !list_empty(&(usbvision->outqueue)));
1020		if (ret)
1021			return ret;
1022	}
1023
1024	spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1025	frame = list_entry(usbvision->outqueue.next,
1026			   struct usbvision_frame, frame);
1027	list_del(usbvision->outqueue.next);
1028	spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1029
1030	/* An error returns an empty frame */
1031	if (frame->grabstate == frame_state_error) {
1032		frame->bytes_read = 0;
1033		return 0;
1034	}
1035
1036	PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1037	       __func__,
1038	       frame->index, frame->bytes_read, frame->scanlength);
1039
1040	/* copy bytes to user space; we allow for partials reads */
1041	if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1042		count = frame->scanlength - frame->bytes_read;
1043
1044	if (copy_to_user(buf, frame->data + frame->bytes_read, count))
1045		return -EFAULT;
1046
1047	frame->bytes_read += count;
1048	PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1049	       __func__,
1050	       (unsigned long)count, frame->bytes_read);
1051
1052	/* For now, forget the frame if it has not been read in one shot. */
1053/*	if (frame->bytes_read >= frame->scanlength) {*/ /* All data has been read */
1054		frame->bytes_read = 0;
1055
1056		/* Mark it as available to be used again. */
1057		frame->grabstate = frame_state_unused;
1058/*	} */
1059
1060	return count;
1061}
1062
1063static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1064{
1065	unsigned long size = vma->vm_end - vma->vm_start,
1066		start = vma->vm_start;
1067	void *pos;
1068	u32 i;
1069	struct usb_usbvision *usbvision = video_drvdata(file);
1070
1071	PDEBUG(DBG_MMAP, "mmap");
1072
1073	if (!USBVISION_IS_OPERATIONAL(usbvision))
1074		return -EFAULT;
1075
1076	if (!(vma->vm_flags & VM_WRITE) ||
1077	    size != PAGE_ALIGN(usbvision->max_frame_size)) {
1078		return -EINVAL;
1079	}
1080
1081	for (i = 0; i < usbvision->num_frames; i++) {
1082		if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1083		    vma->vm_pgoff)
1084			break;
1085	}
1086	if (i == usbvision->num_frames) {
1087		PDEBUG(DBG_MMAP,
1088		       "mmap: user supplied mapping address is out of range");
1089		return -EINVAL;
1090	}
1091
1092	/* VM_IO is eventually going to replace PageReserved altogether */
1093	vma->vm_flags |= VM_IO;
1094	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
1095
1096	pos = usbvision->frame[i].data;
1097	while (size > 0) {
1098		if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1099			PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1100			return -EAGAIN;
1101		}
1102		start += PAGE_SIZE;
1103		pos += PAGE_SIZE;
1104		size -= PAGE_SIZE;
1105	}
1106
1107	return 0;
1108}
1109
1110
1111/*
1112 * Here comes the stuff for radio on usbvision based devices
1113 *
1114 */
1115static int usbvision_radio_open(struct file *file)
1116{
1117	struct usb_usbvision *usbvision = video_drvdata(file);
1118	int err_code = 0;
1119
1120	PDEBUG(DBG_IO, "%s:", __func__);
1121
1122	if (usbvision->user) {
1123		dev_err(&usbvision->rdev->dev,
1124			"%s: Someone tried to open an already opened USBVision Radio!\n",
1125				__func__);
1126		err_code = -EBUSY;
1127	} else {
1128		if (power_on_at_open) {
1129			usbvision_reset_power_off_timer(usbvision);
1130			if (usbvision->power == 0) {
1131				usbvision_power_on(usbvision);
1132				usbvision_i2c_register(usbvision);
1133			}
1134		}
1135
1136		/* Alternate interface 1 is is the biggest frame size */
1137		err_code = usbvision_set_alternate(usbvision);
1138		if (err_code < 0) {
1139			usbvision->last_error = err_code;
1140			err_code = -EBUSY;
1141			goto out;
1142		}
1143
1144		/* If so far no errors then we shall start the radio */
1145		usbvision->radio = 1;
1146		call_all(usbvision, tuner, s_radio);
1147		usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1148		usbvision->user++;
1149	}
1150
1151	if (err_code) {
1152		if (power_on_at_open) {
1153			usbvision_i2c_unregister(usbvision);
1154			usbvision_power_off(usbvision);
1155			usbvision->initialized = 0;
1156		}
1157	}
1158out:
1159	return err_code;
1160}
1161
1162
1163static int usbvision_radio_close(struct file *file)
1164{
1165	struct usb_usbvision *usbvision = video_drvdata(file);
1166	int err_code = 0;
1167
1168	PDEBUG(DBG_IO, "");
1169
1170	/* Set packet size to 0 */
1171	usbvision->iface_alt = 0;
1172	err_code = usb_set_interface(usbvision->dev, usbvision->iface,
1173				    usbvision->iface_alt);
1174
1175	usbvision_audio_off(usbvision);
1176	usbvision->radio = 0;
1177	usbvision->user--;
1178
1179	if (power_on_at_open) {
1180		usbvision_set_power_off_timer(usbvision);
1181		usbvision->initialized = 0;
1182	}
1183
1184	if (usbvision->remove_pending) {
1185		printk(KERN_INFO "%s: Final disconnect\n", __func__);
1186		usbvision_release(usbvision);
1187	}
1188
1189	PDEBUG(DBG_IO, "success");
1190	return err_code;
1191}
1192
1193/* Video registration stuff */
1194
1195/* Video template */
1196static const struct v4l2_file_operations usbvision_fops = {
1197	.owner             = THIS_MODULE,
1198	.open		= usbvision_v4l2_open,
1199	.release	= usbvision_v4l2_close,
1200	.read		= usbvision_v4l2_read,
1201	.mmap		= usbvision_v4l2_mmap,
1202	.unlocked_ioctl	= video_ioctl2,
1203/*	.poll		= video_poll, */
1204};
1205
1206static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1207	.vidioc_querycap      = vidioc_querycap,
1208	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1209	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1210	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1211	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1212	.vidioc_reqbufs       = vidioc_reqbufs,
1213	.vidioc_querybuf      = vidioc_querybuf,
1214	.vidioc_qbuf          = vidioc_qbuf,
1215	.vidioc_dqbuf         = vidioc_dqbuf,
1216	.vidioc_s_std         = vidioc_s_std,
1217	.vidioc_enum_input    = vidioc_enum_input,
1218	.vidioc_g_input       = vidioc_g_input,
1219	.vidioc_s_input       = vidioc_s_input,
1220	.vidioc_queryctrl     = vidioc_queryctrl,
1221	.vidioc_g_audio       = vidioc_g_audio,
1222	.vidioc_s_audio       = vidioc_s_audio,
1223	.vidioc_g_ctrl        = vidioc_g_ctrl,
1224	.vidioc_s_ctrl        = vidioc_s_ctrl,
1225	.vidioc_streamon      = vidioc_streamon,
1226	.vidioc_streamoff     = vidioc_streamoff,
1227	.vidioc_g_tuner       = vidioc_g_tuner,
1228	.vidioc_s_tuner       = vidioc_s_tuner,
1229	.vidioc_g_frequency   = vidioc_g_frequency,
1230	.vidioc_s_frequency   = vidioc_s_frequency,
1231#ifdef CONFIG_VIDEO_ADV_DEBUG
1232	.vidioc_g_register    = vidioc_g_register,
1233	.vidioc_s_register    = vidioc_s_register,
1234#endif
1235};
1236
1237static struct video_device usbvision_video_template = {
1238	.fops		= &usbvision_fops,
1239	.ioctl_ops	= &usbvision_ioctl_ops,
1240	.name           = "usbvision-video",
1241	.release	= video_device_release,
1242	.tvnorms        = USBVISION_NORMS,
1243	.current_norm   = V4L2_STD_PAL
1244};
1245
1246
1247/* Radio template */
1248static const struct v4l2_file_operations usbvision_radio_fops = {
1249	.owner             = THIS_MODULE,
1250	.open		= usbvision_radio_open,
1251	.release	= usbvision_radio_close,
1252	.unlocked_ioctl	= video_ioctl2,
1253};
1254
1255static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1256	.vidioc_querycap      = vidioc_querycap,
1257	.vidioc_enum_input    = vidioc_enum_input,
1258	.vidioc_g_input       = vidioc_g_input,
1259	.vidioc_s_input       = vidioc_s_input,
1260	.vidioc_queryctrl     = vidioc_queryctrl,
1261	.vidioc_g_audio       = vidioc_g_audio,
1262	.vidioc_s_audio       = vidioc_s_audio,
1263	.vidioc_g_ctrl        = vidioc_g_ctrl,
1264	.vidioc_s_ctrl        = vidioc_s_ctrl,
1265	.vidioc_g_tuner       = vidioc_g_tuner,
1266	.vidioc_s_tuner       = vidioc_s_tuner,
1267	.vidioc_g_frequency   = vidioc_g_frequency,
1268	.vidioc_s_frequency   = vidioc_s_frequency,
1269};
1270
1271static struct video_device usbvision_radio_template = {
1272	.fops		= &usbvision_radio_fops,
1273	.name		= "usbvision-radio",
1274	.release	= video_device_release,
1275	.ioctl_ops	= &usbvision_radio_ioctl_ops,
1276
1277	.tvnorms              = USBVISION_NORMS,
1278	.current_norm         = V4L2_STD_PAL
1279};
1280
1281
1282static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1283					struct video_device *vdev_template,
1284					char *name)
1285{
1286	struct usb_device *usb_dev = usbvision->dev;
1287	struct video_device *vdev;
1288
1289	if (usb_dev == NULL) {
1290		dev_err(&usbvision->dev->dev,
1291			"%s: usbvision->dev is not set\n", __func__);
1292		return NULL;
1293	}
1294
1295	vdev = video_device_alloc();
1296	if (NULL == vdev)
1297		return NULL;
1298	*vdev = *vdev_template;
1299	/* Locking in file operations other than ioctl should be done
1300	   by the driver, not the V4L2 core.
1301	   This driver needs auditing so that this flag can be removed. */
1302	set_bit(V4L2_FL_LOCK_ALL_FOPS, &vdev->flags);
1303	vdev->lock = &usbvision->v4l2_lock;
1304	vdev->v4l2_dev = &usbvision->v4l2_dev;
1305	snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1306	video_set_drvdata(vdev, usbvision);
1307	return vdev;
1308}
1309
1310/* unregister video4linux devices */
1311static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1312{
1313	/* Radio Device: */
1314	if (usbvision->rdev) {
1315		PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1316		       video_device_node_name(usbvision->rdev));
1317		if (video_is_registered(usbvision->rdev))
1318			video_unregister_device(usbvision->rdev);
1319		else
1320			video_device_release(usbvision->rdev);
1321		usbvision->rdev = NULL;
1322	}
1323
1324	/* Video Device: */
1325	if (usbvision->vdev) {
1326		PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1327		       video_device_node_name(usbvision->vdev));
1328		if (video_is_registered(usbvision->vdev))
1329			video_unregister_device(usbvision->vdev);
1330		else
1331			video_device_release(usbvision->vdev);
1332		usbvision->vdev = NULL;
1333	}
1334}
1335
1336/* register video4linux devices */
1337static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1338{
1339	/* Video Device: */
1340	usbvision->vdev = usbvision_vdev_init(usbvision,
1341					      &usbvision_video_template,
1342					      "USBVision Video");
1343	if (usbvision->vdev == NULL)
1344		goto err_exit;
1345	if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1346		goto err_exit;
1347	printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1348	       usbvision->nr, video_device_node_name(usbvision->vdev));
1349
1350	/* Radio Device: */
1351	if (usbvision_device_data[usbvision->dev_model].radio) {
1352		/* usbvision has radio */
1353		usbvision->rdev = usbvision_vdev_init(usbvision,
1354						      &usbvision_radio_template,
1355						      "USBVision Radio");
1356		if (usbvision->rdev == NULL)
1357			goto err_exit;
1358		if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1359			goto err_exit;
1360		printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1361		       usbvision->nr, video_device_node_name(usbvision->rdev));
1362	}
1363	/* all done */
1364	return 0;
1365
1366 err_exit:
1367	dev_err(&usbvision->dev->dev,
1368		"USBVision[%d]: video_register_device() failed\n",
1369			usbvision->nr);
1370	usbvision_unregister_video(usbvision);
1371	return -1;
1372}
1373
1374/*
1375 * usbvision_alloc()
1376 *
1377 * This code allocates the struct usb_usbvision.
1378 * It is filled with default values.
1379 *
1380 * Returns NULL on error, a pointer to usb_usbvision else.
1381 *
1382 */
1383static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1384					     struct usb_interface *intf)
1385{
1386	struct usb_usbvision *usbvision;
1387
1388	usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1389	if (usbvision == NULL)
1390		return NULL;
1391
1392	usbvision->dev = dev;
1393	if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1394		goto err_free;
1395
1396	mutex_init(&usbvision->v4l2_lock);
1397
1398	/* prepare control urb for control messages during interrupts */
1399	usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1400	if (usbvision->ctrl_urb == NULL)
1401		goto err_unreg;
1402	init_waitqueue_head(&usbvision->ctrl_urb_wq);
1403
1404	usbvision_init_power_off_timer(usbvision);
1405
1406	return usbvision;
1407
1408err_unreg:
1409	v4l2_device_unregister(&usbvision->v4l2_dev);
1410err_free:
1411	kfree(usbvision);
1412	return NULL;
1413}
1414
1415/*
1416 * usbvision_release()
1417 *
1418 * This code does final release of struct usb_usbvision. This happens
1419 * after the device is disconnected -and- all clients closed their files.
1420 *
1421 */
1422static void usbvision_release(struct usb_usbvision *usbvision)
1423{
1424	PDEBUG(DBG_PROBE, "");
1425
1426	usbvision_reset_power_off_timer(usbvision);
1427
1428	usbvision->initialized = 0;
1429
1430	usbvision_remove_sysfs(usbvision->vdev);
1431	usbvision_unregister_video(usbvision);
1432
1433	usb_free_urb(usbvision->ctrl_urb);
1434
1435	v4l2_device_unregister(&usbvision->v4l2_dev);
1436	kfree(usbvision);
1437
1438	PDEBUG(DBG_PROBE, "success");
1439}
1440
1441
1442/*********************** usb interface **********************************/
1443
1444static void usbvision_configure_video(struct usb_usbvision *usbvision)
1445{
1446	int model;
1447
1448	if (usbvision == NULL)
1449		return;
1450
1451	model = usbvision->dev_model;
1452	usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1453
1454	if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1455		usbvision->vin_reg2_preset =
1456			usbvision_device_data[usbvision->dev_model].vin_reg2;
1457	} else {
1458		usbvision->vin_reg2_preset = 0;
1459	}
1460
1461	usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1462
1463	usbvision->video_inputs = usbvision_device_data[model].video_channels;
1464	usbvision->ctl_input = 0;
1465
1466	/* This should be here to make i2c clients to be able to register */
1467	/* first switch off audio */
1468	if (usbvision_device_data[model].audio_channels > 0)
1469		usbvision_audio_off(usbvision);
1470	if (!power_on_at_open) {
1471		/* and then power up the noisy tuner */
1472		usbvision_power_on(usbvision);
1473		usbvision_i2c_register(usbvision);
1474	}
1475}
1476
1477/*
1478 * usbvision_probe()
1479 *
1480 * This procedure queries device descriptor and accepts the interface
1481 * if it looks like USBVISION video device
1482 *
1483 */
1484static int __devinit usbvision_probe(struct usb_interface *intf,
1485				     const struct usb_device_id *devid)
1486{
1487	struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1488	struct usb_interface *uif;
1489	__u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1490	const struct usb_host_interface *interface;
1491	struct usb_usbvision *usbvision = NULL;
1492	const struct usb_endpoint_descriptor *endpoint;
1493	int model, i;
1494
1495	PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1496				dev->descriptor.idVendor,
1497				dev->descriptor.idProduct, ifnum);
1498
1499	model = devid->driver_info;
1500	if (model < 0 || model >= usbvision_device_data_size) {
1501		PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1502		return -ENODEV;
1503	}
1504	printk(KERN_INFO "%s: %s found\n", __func__,
1505				usbvision_device_data[model].model_string);
1506
1507	if (usbvision_device_data[model].interface >= 0)
1508		interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1509	else
1510		interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1511	endpoint = &interface->endpoint[1].desc;
1512	if (!usb_endpoint_xfer_isoc(endpoint)) {
1513		dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1514		    __func__, ifnum);
1515		dev_err(&intf->dev, "%s: Endpoint attributes %d",
1516		    __func__, endpoint->bmAttributes);
1517		return -ENODEV;
1518	}
1519	if (usb_endpoint_dir_out(endpoint)) {
1520		dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1521		    __func__, ifnum);
1522		return -ENODEV;
1523	}
1524
1525	usbvision = usbvision_alloc(dev, intf);
1526	if (usbvision == NULL) {
1527		dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1528		return -ENOMEM;
1529	}
1530
1531	if (dev->descriptor.bNumConfigurations > 1)
1532		usbvision->bridge_type = BRIDGE_NT1004;
1533	else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1534		usbvision->bridge_type = BRIDGE_NT1005;
1535	else
1536		usbvision->bridge_type = BRIDGE_NT1003;
1537	PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1538
1539	/* compute alternate max packet sizes */
1540	uif = dev->actconfig->interface[0];
1541
1542	usbvision->num_alt = uif->num_altsetting;
1543	PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1544	usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
1545	if (usbvision->alt_max_pkt_size == NULL) {
1546		dev_err(&intf->dev, "usbvision: out of memory!\n");
1547		return -ENOMEM;
1548	}
1549
1550	for (i = 0; i < usbvision->num_alt; i++) {
1551		u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1552				      wMaxPacketSize);
1553		usbvision->alt_max_pkt_size[i] =
1554			(tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1555		PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1556		       usbvision->alt_max_pkt_size[i]);
1557	}
1558
1559
1560	usbvision->nr = usbvision_nr++;
1561
1562	usbvision->have_tuner = usbvision_device_data[model].tuner;
1563	if (usbvision->have_tuner)
1564		usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1565
1566	usbvision->dev_model = model;
1567	usbvision->remove_pending = 0;
1568	usbvision->iface = ifnum;
1569	usbvision->iface_alt = 0;
1570	usbvision->video_endp = endpoint->bEndpointAddress;
1571	usbvision->isoc_packet_size = 0;
1572	usbvision->usb_bandwidth = 0;
1573	usbvision->user = 0;
1574	usbvision->streaming = stream_off;
1575	usbvision_configure_video(usbvision);
1576	usbvision_register_video(usbvision);
1577
1578	usbvision_create_sysfs(usbvision->vdev);
1579
1580	PDEBUG(DBG_PROBE, "success");
1581	return 0;
1582}
1583
1584
1585/*
1586 * usbvision_disconnect()
1587 *
1588 * This procedure stops all driver activity, deallocates interface-private
1589 * structure (pointed by 'ptr') and after that driver should be removable
1590 * with no ill consequences.
1591 *
1592 */
1593static void __devexit usbvision_disconnect(struct usb_interface *intf)
1594{
1595	struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1596
1597	PDEBUG(DBG_PROBE, "");
1598
1599	if (usbvision == NULL) {
1600		pr_err("%s: usb_get_intfdata() failed\n", __func__);
1601		return;
1602	}
1603
1604	mutex_lock(&usbvision->v4l2_lock);
1605
1606	/* At this time we ask to cancel outstanding URBs */
1607	usbvision_stop_isoc(usbvision);
1608
1609	v4l2_device_disconnect(&usbvision->v4l2_dev);
1610
1611	if (usbvision->power) {
1612		usbvision_i2c_unregister(usbvision);
1613		usbvision_power_off(usbvision);
1614	}
1615	usbvision->remove_pending = 1;	/* Now all ISO data will be ignored */
1616
1617	usb_put_dev(usbvision->dev);
1618	usbvision->dev = NULL;	/* USB device is no more */
1619
1620	mutex_unlock(&usbvision->v4l2_lock);
1621
1622	if (usbvision->user) {
1623		printk(KERN_INFO "%s: In use, disconnect pending\n",
1624		       __func__);
1625		wake_up_interruptible(&usbvision->wait_frame);
1626		wake_up_interruptible(&usbvision->wait_stream);
1627	} else {
1628		usbvision_release(usbvision);
1629	}
1630
1631	PDEBUG(DBG_PROBE, "success");
1632}
1633
1634static struct usb_driver usbvision_driver = {
1635	.name		= "usbvision",
1636	.id_table	= usbvision_table,
1637	.probe		= usbvision_probe,
1638	.disconnect	= __devexit_p(usbvision_disconnect),
1639};
1640
1641/*
1642 * usbvision_init()
1643 *
1644 * This code is run to initialize the driver.
1645 *
1646 */
1647static int __init usbvision_init(void)
1648{
1649	int err_code;
1650
1651	PDEBUG(DBG_PROBE, "");
1652
1653	PDEBUG(DBG_IO,  "IO      debugging is enabled [video]");
1654	PDEBUG(DBG_PROBE, "PROBE   debugging is enabled [video]");
1655	PDEBUG(DBG_MMAP, "MMAP    debugging is enabled [video]");
1656
1657	/* disable planar mode support unless compression enabled */
1658	if (isoc_mode != ISOC_MODE_COMPRESS) {
1659		/* FIXME : not the right way to set supported flag */
1660		usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1661		usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1662	}
1663
1664	err_code = usb_register(&usbvision_driver);
1665
1666	if (err_code == 0) {
1667		printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1668		PDEBUG(DBG_PROBE, "success");
1669	}
1670	return err_code;
1671}
1672
1673static void __exit usbvision_exit(void)
1674{
1675	PDEBUG(DBG_PROBE, "");
1676
1677	usb_deregister(&usbvision_driver);
1678	PDEBUG(DBG_PROBE, "success");
1679}
1680
1681module_init(usbvision_init);
1682module_exit(usbvision_exit);
1683
1684/*
1685 * Overrides for Emacs so that we follow Linus's tabbing style.
1686 * ---------------------------------------------------------------------------
1687 * Local variables:
1688 * c-basic-offset: 8
1689 * End:
1690 */