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