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