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