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