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