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