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