drivers/macintosh/: Spelling fixes
[linux-2.6-block.git] / drivers / media / video / usbvision / usbvision-video.c
CommitLineData
483dfdb6 1/*
7ca659e3 2 * USB USBVISION Video device driver 0.9.9
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
f30ebd43 48#include <linux/version.h>
483dfdb6 49#include <linux/kernel.h>
483dfdb6
TM
50#include <linux/list.h>
51#include <linux/timer.h>
52#include <linux/slab.h>
53#include <linux/mm.h>
54#include <linux/utsname.h>
55#include <linux/highmem.h>
483dfdb6
TM
56#include <linux/videodev.h>
57#include <linux/vmalloc.h>
58#include <linux/module.h>
59#include <linux/init.h>
60#include <linux/spinlock.h>
61#include <asm/io.h>
62#include <linux/videodev2.h>
63#include <linux/video_decoder.h>
64#include <linux/i2c.h>
65
66#include <media/saa7115.h>
67#include <media/v4l2-common.h>
68#include <media/tuner.h>
69#include <media/audiochip.h>
70
a1ed551c 71#include <linux/workqueue.h>
483dfdb6
TM
72
73#ifdef CONFIG_KMOD
74#include <linux/kmod.h>
75#endif
76
77#include "usbvision.h"
659ae56d 78#include "usbvision-cards.h"
483dfdb6 79
c5f48367
TM
80#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
81 Dwaine Garden <DwaineGarden@rogers.com>"
483dfdb6
TM
82#define DRIVER_NAME "usbvision"
83#define DRIVER_ALIAS "USBVision"
84#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85#define DRIVER_LICENSE "GPL"
86#define USBVISION_DRIVER_VERSION_MAJOR 0
87#define USBVISION_DRIVER_VERSION_MINOR 9
88#define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
c5f48367
TM
89#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
90USBVISION_DRIVER_VERSION_MINOR,\
91USBVISION_DRIVER_VERSION_PATCHLEVEL)
92#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
93 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
94 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
483dfdb6
TM
95
96#define ENABLE_HEXDUMP 0 /* Enable if you need it */
97
98
483dfdb6
TM
99#ifdef USBVISION_DEBUG
100 #define PDEBUG(level, fmt, args...) \
c5f48367
TM
101 if (video_debug & (level)) \
102 info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ ,\
103 ## args)
483dfdb6
TM
104#else
105 #define PDEBUG(level, fmt, args...) do {} while(0)
106#endif
107
483dfdb6
TM
108#define DBG_IO 1<<1
109#define DBG_PROBE 1<<2
c876a346 110#define DBG_MMAP 1<<3
483dfdb6
TM
111
112//String operations
113#define rmspace(str) while(*str==' ') str++;
114#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
115
116
c5f48367
TM
117/* sequential number of usbvision device */
118static int usbvision_nr = 0;
483dfdb6
TM
119
120static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
121 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
122 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
123 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
124 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
125 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
126 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
127 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
128 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
129};
130
c5f48367 131/* Function prototypes */
483dfdb6
TM
132static void usbvision_release(struct usb_usbvision *usbvision);
133
c5f48367
TM
134/* Default initalization of device driver parameters */
135/* Set the default format for ISOC endpoint */
136static int isocMode = ISOC_MODE_COMPRESS;
137/* Set the default Debug Mode of the device driver */
138static int video_debug = 0;
139/* Set the default device to power on at startup */
140static int PowerOnAtOpen = 1;
141/* Sequential Number of Video Device */
142static int video_nr = -1;
143/* Sequential Number of Radio Device */
144static int radio_nr = -1;
145/* Sequential Number of VBI Device */
146static int vbi_nr = -1;
147
148/* Grab parameters for the device driver */
149
150/* Showing parameters under SYSFS */
483dfdb6
TM
151module_param(isocMode, int, 0444);
152module_param(video_debug, int, 0444);
153module_param(PowerOnAtOpen, int, 0444);
154module_param(video_nr, int, 0444);
155module_param(radio_nr, int, 0444);
156module_param(vbi_nr, int, 0444);
483dfdb6
TM
157
158MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
159MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
160MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
161MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
162MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
163MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
483dfdb6
TM
164
165
166// Misc stuff
167MODULE_AUTHOR(DRIVER_AUTHOR);
168MODULE_DESCRIPTION(DRIVER_DESC);
169MODULE_LICENSE(DRIVER_LICENSE);
a1ed551c
MCC
170MODULE_VERSION(USBVISION_VERSION_STRING);
171MODULE_ALIAS(DRIVER_ALIAS);
483dfdb6
TM
172
173
c5f48367
TM
174/*****************************************************************************/
175/* SYSFS Code - Copied from the stv680.c usb module. */
176/* Device information is located at /sys/class/video4linux/video0 */
177/* Device parameters information is located at /sys/module/usbvision */
178/* Device USB Information is located at */
179/* /sys/bus/usb/drivers/USBVision Video Grabber */
180/*****************************************************************************/
483dfdb6
TM
181
182
183#define YES_NO(x) ((x) ? "Yes" : "No")
184
54bd5b66 185static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
483dfdb6 186{
c5f48367
TM
187 struct video_device *vdev =
188 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
189 return video_get_drvdata(vdev);
190}
191
54bd5b66
KS
192static ssize_t show_version(struct device *cd,
193 struct device_attribute *attr, char *buf)
483dfdb6
TM
194{
195 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
196}
54bd5b66 197static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
483dfdb6 198
54bd5b66
KS
199static ssize_t show_model(struct device *cd,
200 struct device_attribute *attr, char *buf)
483dfdb6 201{
c5f48367
TM
202 struct video_device *vdev =
203 container_of(cd, struct video_device, class_dev);
483dfdb6 204 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
c5f48367
TM
205 return sprintf(buf, "%s\n",
206 usbvision_device_data[usbvision->DevModel].ModelString);
483dfdb6 207}
54bd5b66 208static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
483dfdb6 209
54bd5b66
KS
210static ssize_t show_hue(struct device *cd,
211 struct device_attribute *attr, char *buf)
483dfdb6 212{
c5f48367
TM
213 struct video_device *vdev =
214 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
215 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
216 struct v4l2_control ctrl;
217 ctrl.id = V4L2_CID_HUE;
218 ctrl.value = 0;
ed00b41d
DG
219 if(usbvision->user)
220 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
2a9f8b5d 221 return sprintf(buf, "%d\n", ctrl.value);
483dfdb6 222}
54bd5b66 223static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
483dfdb6 224
54bd5b66
KS
225static ssize_t show_contrast(struct device *cd,
226 struct device_attribute *attr, char *buf)
483dfdb6 227{
c5f48367
TM
228 struct video_device *vdev =
229 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
230 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
231 struct v4l2_control ctrl;
232 ctrl.id = V4L2_CID_CONTRAST;
233 ctrl.value = 0;
ed00b41d
DG
234 if(usbvision->user)
235 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
2a9f8b5d 236 return sprintf(buf, "%d\n", ctrl.value);
483dfdb6 237}
54bd5b66 238static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
483dfdb6 239
54bd5b66
KS
240static ssize_t show_brightness(struct device *cd,
241 struct device_attribute *attr, char *buf)
483dfdb6 242{
c5f48367
TM
243 struct video_device *vdev =
244 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
245 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
246 struct v4l2_control ctrl;
247 ctrl.id = V4L2_CID_BRIGHTNESS;
248 ctrl.value = 0;
ed00b41d
DG
249 if(usbvision->user)
250 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
2a9f8b5d 251 return sprintf(buf, "%d\n", ctrl.value);
483dfdb6 252}
54bd5b66 253static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
483dfdb6 254
54bd5b66
KS
255static ssize_t show_saturation(struct device *cd,
256 struct device_attribute *attr, char *buf)
483dfdb6 257{
c5f48367
TM
258 struct video_device *vdev =
259 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
260 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
261 struct v4l2_control ctrl;
262 ctrl.id = V4L2_CID_SATURATION;
263 ctrl.value = 0;
ed00b41d
DG
264 if(usbvision->user)
265 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
2a9f8b5d 266 return sprintf(buf, "%d\n", ctrl.value);
483dfdb6 267}
54bd5b66 268static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
483dfdb6 269
54bd5b66
KS
270static ssize_t show_streaming(struct device *cd,
271 struct device_attribute *attr, char *buf)
483dfdb6 272{
c5f48367
TM
273 struct video_device *vdev =
274 container_of(cd, struct video_device, class_dev);
483dfdb6 275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
c5f48367
TM
276 return sprintf(buf, "%s\n",
277 YES_NO(usbvision->streaming==Stream_On?1:0));
483dfdb6 278}
54bd5b66 279static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
483dfdb6 280
54bd5b66
KS
281static ssize_t show_compression(struct device *cd,
282 struct device_attribute *attr, char *buf)
483dfdb6 283{
c5f48367
TM
284 struct video_device *vdev =
285 container_of(cd, struct video_device, class_dev);
483dfdb6 286 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
c5f48367
TM
287 return sprintf(buf, "%s\n",
288 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
483dfdb6 289}
54bd5b66 290static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
483dfdb6 291
54bd5b66
KS
292static ssize_t show_device_bridge(struct device *cd,
293 struct device_attribute *attr, char *buf)
483dfdb6 294{
c5f48367
TM
295 struct video_device *vdev =
296 container_of(cd, struct video_device, class_dev);
483dfdb6
TM
297 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
298 return sprintf(buf, "%d\n", usbvision->bridgeType);
299}
54bd5b66 300static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
483dfdb6
TM
301
302static void usbvision_create_sysfs(struct video_device *vdev)
303{
304 int res;
ed00b41d
DG
305 if (!vdev)
306 return;
307 do {
54bd5b66
KS
308 res = device_create_file(&vdev->class_dev,
309 &dev_attr_version);
ed00b41d
DG
310 if (res<0)
311 break;
54bd5b66
KS
312 res = device_create_file(&vdev->class_dev,
313 &dev_attr_model);
ed00b41d
DG
314 if (res<0)
315 break;
54bd5b66
KS
316 res = device_create_file(&vdev->class_dev,
317 &dev_attr_hue);
ed00b41d
DG
318 if (res<0)
319 break;
54bd5b66
KS
320 res = device_create_file(&vdev->class_dev,
321 &dev_attr_contrast);
ed00b41d
DG
322 if (res<0)
323 break;
54bd5b66
KS
324 res = device_create_file(&vdev->class_dev,
325 &dev_attr_brightness);
ed00b41d
DG
326 if (res<0)
327 break;
54bd5b66
KS
328 res = device_create_file(&vdev->class_dev,
329 &dev_attr_saturation);
ed00b41d
DG
330 if (res<0)
331 break;
54bd5b66
KS
332 res = device_create_file(&vdev->class_dev,
333 &dev_attr_streaming);
ed00b41d
DG
334 if (res<0)
335 break;
54bd5b66
KS
336 res = device_create_file(&vdev->class_dev,
337 &dev_attr_compression);
ed00b41d
DG
338 if (res<0)
339 break;
54bd5b66
KS
340 res = device_create_file(&vdev->class_dev,
341 &dev_attr_bridge);
ed00b41d
DG
342 if (res>=0)
343 return;
344 } while (0);
345
346 err("%s error: %d\n", __FUNCTION__, res);
483dfdb6
TM
347}
348
349static void usbvision_remove_sysfs(struct video_device *vdev)
350{
351 if (vdev) {
54bd5b66
KS
352 device_remove_file(&vdev->class_dev,
353 &dev_attr_version);
354 device_remove_file(&vdev->class_dev,
355 &dev_attr_model);
356 device_remove_file(&vdev->class_dev,
357 &dev_attr_hue);
358 device_remove_file(&vdev->class_dev,
359 &dev_attr_contrast);
360 device_remove_file(&vdev->class_dev,
361 &dev_attr_brightness);
362 device_remove_file(&vdev->class_dev,
363 &dev_attr_saturation);
364 device_remove_file(&vdev->class_dev,
365 &dev_attr_streaming);
366 device_remove_file(&vdev->class_dev,
367 &dev_attr_compression);
368 device_remove_file(&vdev->class_dev,
369 &dev_attr_bridge);
483dfdb6
TM
370 }
371}
372
373
374/*
375 * usbvision_open()
376 *
377 * This is part of Video 4 Linux API. The driver can be opened by one
378 * client only (checks internal counter 'usbvision->user'). The procedure
379 * then allocates buffers needed for video processing.
380 *
381 */
382static int usbvision_v4l2_open(struct inode *inode, struct file *file)
383{
384 struct video_device *dev = video_devdata(file);
c5f48367
TM
385 struct usb_usbvision *usbvision =
386 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6
TM
387 int errCode = 0;
388
389 PDEBUG(DBG_IO, "open");
390
391
392 usbvision_reset_powerOffTimer(usbvision);
393
394 if (usbvision->user)
395 errCode = -EBUSY;
396 else {
6f78e186
TM
397 /* Allocate memory for the scratch ring buffer */
398 errCode = usbvision_scratch_alloc(usbvision);
399 if (isocMode==ISOC_MODE_COMPRESS) {
c5f48367
TM
400 /* Allocate intermediate decompression buffers
401 only if needed */
6f78e186 402 errCode = usbvision_decompress_alloc(usbvision);
483dfdb6
TM
403 }
404 if (errCode) {
405 /* Deallocate all buffers if trouble */
483dfdb6 406 usbvision_scratch_free(usbvision);
483dfdb6
TM
407 usbvision_decompress_free(usbvision);
408 }
409 }
410
411 /* If so far no errors then we shall start the camera */
412 if (!errCode) {
289d4d2f 413 mutex_lock(&usbvision->lock);
483dfdb6
TM
414 if (usbvision->power == 0) {
415 usbvision_power_on(usbvision);
1ff16c20 416 usbvision_i2c_register(usbvision);
483dfdb6
TM
417 }
418
419 /* Send init sequence only once, it's large! */
420 if (!usbvision->initialized) {
421 int setup_ok = 0;
422 setup_ok = usbvision_setup(usbvision,isocMode);
423 if (setup_ok)
424 usbvision->initialized = 1;
425 else
426 errCode = -EBUSY;
427 }
428
429 if (!errCode) {
430 usbvision_begin_streaming(usbvision);
431 errCode = usbvision_init_isoc(usbvision);
c5f48367 432 /* device must be initialized before isoc transfer */
483dfdb6
TM
433 usbvision_muxsel(usbvision,0);
434 usbvision->user++;
c5f48367 435 } else {
483dfdb6 436 if (PowerOnAtOpen) {
1ff16c20 437 usbvision_i2c_unregister(usbvision);
483dfdb6
TM
438 usbvision_power_off(usbvision);
439 usbvision->initialized = 0;
440 }
441 }
289d4d2f 442 mutex_unlock(&usbvision->lock);
483dfdb6
TM
443 }
444
445 if (errCode) {
446 }
447
448 /* prepare queues */
449 usbvision_empty_framequeues(usbvision);
450
451 PDEBUG(DBG_IO, "success");
452 return errCode;
453}
454
455/*
456 * usbvision_v4l2_close()
457 *
458 * This is part of Video 4 Linux API. The procedure
459 * stops streaming and deallocates all buffers that were earlier
460 * allocated in usbvision_v4l2_open().
461 *
462 */
463static int usbvision_v4l2_close(struct inode *inode, struct file *file)
464{
465 struct video_device *dev = video_devdata(file);
c5f48367
TM
466 struct usb_usbvision *usbvision =
467 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6
TM
468
469 PDEBUG(DBG_IO, "close");
289d4d2f 470 mutex_lock(&usbvision->lock);
483dfdb6
TM
471
472 usbvision_audio_off(usbvision);
473 usbvision_restart_isoc(usbvision);
474 usbvision_stop_isoc(usbvision);
475
476 usbvision_decompress_free(usbvision);
38284ba3 477 usbvision_frames_free(usbvision);
6f78e186 478 usbvision_empty_framequeues(usbvision);
483dfdb6 479 usbvision_scratch_free(usbvision);
483dfdb6
TM
480
481 usbvision->user--;
482
483 if (PowerOnAtOpen) {
c5f48367
TM
484 /* power off in a little while
485 to avoid off/on every close/open short sequences */
483dfdb6
TM
486 usbvision_set_powerOffTimer(usbvision);
487 usbvision->initialized = 0;
488 }
489
289d4d2f 490 mutex_unlock(&usbvision->lock);
483dfdb6
TM
491
492 if (usbvision->remove_pending) {
de6a1b8e 493 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
483dfdb6
TM
494 usbvision_release(usbvision);
495 }
496
497 PDEBUG(DBG_IO, "success");
498
499
500 return 0;
501}
502
503
504/*
505 * usbvision_ioctl()
506 *
507 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
508 *
509 */
c5f48367
TM
510#ifdef CONFIG_VIDEO_ADV_DEBUG
511static int vidioc_g_register (struct file *file, void *priv,
512 struct v4l2_register *reg)
483dfdb6
TM
513{
514 struct video_device *dev = video_devdata(file);
c5f48367
TM
515 struct usb_usbvision *usbvision =
516 (struct usb_usbvision *) video_get_drvdata(dev);
517 int errCode;
483dfdb6 518
c5f48367
TM
519 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
520 return -EINVAL;
521 /* NT100x has a 8-bit register space */
522 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
523 if (errCode < 0) {
524 err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
525 __FUNCTION__, errCode);
526 return errCode;
527 }
bc147135 528 reg->val = errCode;
c5f48367
TM
529 return 0;
530}
483dfdb6 531
c5f48367
TM
532static int vidioc_s_register (struct file *file, void *priv,
533 struct v4l2_register *reg)
534{
535 struct video_device *dev = video_devdata(file);
536 struct usb_usbvision *usbvision =
537 (struct usb_usbvision *) video_get_drvdata(dev);
538 int errCode;
62d50add 539
c5f48367
TM
540 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
541 return -EINVAL;
542 /* NT100x has a 8-bit register space */
bc147135
TP
543 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
544 if (errCode < 0) {
c5f48367
TM
545 err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
546 __FUNCTION__, errCode);
547 return errCode;
548 }
549 return 0;
550}
483dfdb6 551#endif
c5f48367
TM
552
553static int vidioc_querycap (struct file *file, void *priv,
554 struct v4l2_capability *vc)
555{
556 struct video_device *dev = video_devdata(file);
557 struct usb_usbvision *usbvision =
558 (struct usb_usbvision *) video_get_drvdata(dev);
559
560 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
561 strlcpy(vc->card,
562 usbvision_device_data[usbvision->DevModel].ModelString,
563 sizeof(vc->card));
564 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
565 sizeof(vc->bus_info));
566 vc->version = USBVISION_DRIVER_VERSION;
567 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
568 V4L2_CAP_AUDIO |
569 V4L2_CAP_READWRITE |
570 V4L2_CAP_STREAMING |
571 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
572 return 0;
573}
574
575static int vidioc_enum_input (struct file *file, void *priv,
576 struct v4l2_input *vi)
577{
578 struct video_device *dev = video_devdata(file);
579 struct usb_usbvision *usbvision =
580 (struct usb_usbvision *) video_get_drvdata(dev);
581 int chan;
582
583 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
584 return -EINVAL;
585 if (usbvision->have_tuner) {
586 chan = vi->index;
587 } else {
588 chan = vi->index + 1; /*skip Television string*/
589 }
590 /* Determine the requested input characteristics
591 specific for each usbvision card model */
592 switch(chan) {
593 case 0:
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "White Video Input");
596 } else {
597 strcpy(vi->name, "Television");
598 vi->type = V4L2_INPUT_TYPE_TUNER;
599 vi->audioset = 1;
600 vi->tuner = chan;
601 vi->std = USBVISION_NORMS;
483dfdb6 602 }
c5f48367
TM
603 break;
604 case 1:
605 vi->type = V4L2_INPUT_TYPE_CAMERA;
606 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
607 strcpy(vi->name, "Green Video Input");
608 } else {
609 strcpy(vi->name, "Composite Video Input");
483dfdb6 610 }
c5f48367
TM
611 vi->std = V4L2_STD_PAL;
612 break;
613 case 2:
614 vi->type = V4L2_INPUT_TYPE_CAMERA;
615 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
616 strcpy(vi->name, "Yellow Video Input");
617 } else {
618 strcpy(vi->name, "S-Video Input");
483dfdb6 619 }
c5f48367
TM
620 vi->std = V4L2_STD_PAL;
621 break;
622 case 3:
623 vi->type = V4L2_INPUT_TYPE_CAMERA;
624 strcpy(vi->name, "Red Video Input");
625 vi->std = V4L2_STD_PAL;
626 break;
627 }
628 return 0;
629}
483dfdb6 630
c5f48367
TM
631static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
632{
633 struct video_device *dev = video_devdata(file);
634 struct usb_usbvision *usbvision =
635 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 636
c5f48367
TM
637 *input = usbvision->ctl_input;
638 return 0;
639}
483dfdb6 640
c5f48367
TM
641static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
642{
643 struct video_device *dev = video_devdata(file);
644 struct usb_usbvision *usbvision =
645 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 646
c5f48367
TM
647 if ((input >= usbvision->video_inputs) || (input < 0) )
648 return -EINVAL;
483dfdb6 649
289d4d2f 650 mutex_lock(&usbvision->lock);
66a17879 651 usbvision_muxsel(usbvision, input);
c5f48367
TM
652 usbvision_set_input(usbvision);
653 usbvision_set_output(usbvision,
654 usbvision->curwidth,
655 usbvision->curheight);
289d4d2f 656 mutex_unlock(&usbvision->lock);
c5f48367
TM
657 return 0;
658}
483dfdb6 659
c5f48367
TM
660static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
661{
662 struct video_device *dev = video_devdata(file);
663 struct usb_usbvision *usbvision =
664 (struct usb_usbvision *) video_get_drvdata(dev);
665 usbvision->tvnormId=*id;
483dfdb6 666
289d4d2f 667 mutex_lock(&usbvision->lock);
c5f48367
TM
668 call_i2c_clients(usbvision, VIDIOC_S_STD,
669 &usbvision->tvnormId);
289d4d2f 670 mutex_unlock(&usbvision->lock);
66a17879
TM
671 /* propagate the change to the decoder */
672 usbvision_muxsel(usbvision, usbvision->ctl_input);
483dfdb6 673
c5f48367
TM
674 return 0;
675}
483dfdb6 676
c5f48367
TM
677static int vidioc_g_tuner (struct file *file, void *priv,
678 struct v4l2_tuner *vt)
679{
680 struct video_device *dev = video_devdata(file);
681 struct usb_usbvision *usbvision =
682 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 683
c5f48367
TM
684 if (!usbvision->have_tuner || vt->index) // Only tuner 0
685 return -EINVAL;
686 if(usbvision->radio) {
687 strcpy(vt->name, "Radio");
688 vt->type = V4L2_TUNER_RADIO;
689 } else {
690 strcpy(vt->name, "Television");
691 }
692 /* Let clients fill in the remainder of this struct */
693 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
483dfdb6 694
c5f48367
TM
695 return 0;
696}
483dfdb6 697
c5f48367
TM
698static int vidioc_s_tuner (struct file *file, void *priv,
699 struct v4l2_tuner *vt)
700{
701 struct video_device *dev = video_devdata(file);
702 struct usb_usbvision *usbvision =
703 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 704
c5f48367
TM
705 // Only no or one tuner for now
706 if (!usbvision->have_tuner || vt->index)
707 return -EINVAL;
708 /* let clients handle this */
709 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
483dfdb6 710
c5f48367
TM
711 return 0;
712}
483dfdb6 713
c5f48367
TM
714static int vidioc_g_frequency (struct file *file, void *priv,
715 struct v4l2_frequency *freq)
716{
717 struct video_device *dev = video_devdata(file);
718 struct usb_usbvision *usbvision =
719 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 720
c5f48367
TM
721 freq->tuner = 0; // Only one tuner
722 if(usbvision->radio) {
723 freq->type = V4L2_TUNER_RADIO;
724 } else {
725 freq->type = V4L2_TUNER_ANALOG_TV;
726 }
727 freq->frequency = usbvision->freq;
483dfdb6 728
c5f48367
TM
729 return 0;
730}
483dfdb6 731
c5f48367
TM
732static int vidioc_s_frequency (struct file *file, void *priv,
733 struct v4l2_frequency *freq)
734{
735 struct video_device *dev = video_devdata(file);
736 struct usb_usbvision *usbvision =
737 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 738
c5f48367
TM
739 // Only no or one tuner for now
740 if (!usbvision->have_tuner || freq->tuner)
741 return -EINVAL;
483dfdb6 742
c5f48367
TM
743 usbvision->freq = freq->frequency;
744 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
483dfdb6 745
c5f48367
TM
746 return 0;
747}
483dfdb6 748
c5f48367
TM
749static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
750{
751 struct video_device *dev = video_devdata(file);
752 struct usb_usbvision *usbvision =
753 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 754
c5f48367
TM
755 memset(a,0,sizeof(*a));
756 if(usbvision->radio) {
757 strcpy(a->name,"Radio");
758 } else {
759 strcpy(a->name, "TV");
760 }
483dfdb6 761
c5f48367
TM
762 return 0;
763}
483dfdb6 764
c5f48367
TM
765static int vidioc_s_audio (struct file *file, void *fh,
766 struct v4l2_audio *a)
767{
768 if(a->index) {
769 return -EINVAL;
770 }
483dfdb6 771
c5f48367
TM
772 return 0;
773}
483dfdb6 774
c5f48367
TM
775static int vidioc_queryctrl (struct file *file, void *priv,
776 struct v4l2_queryctrl *ctrl)
777{
778 struct video_device *dev = video_devdata(file);
779 struct usb_usbvision *usbvision =
780 (struct usb_usbvision *) video_get_drvdata(dev);
781 int id=ctrl->id;
483dfdb6 782
c5f48367
TM
783 memset(ctrl,0,sizeof(*ctrl));
784 ctrl->id=id;
483dfdb6 785
c5f48367 786 call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
483dfdb6 787
c5f48367
TM
788 if (!ctrl->type)
789 return -EINVAL;
483dfdb6 790
c5f48367
TM
791 return 0;
792}
483dfdb6 793
c5f48367
TM
794static int vidioc_g_ctrl (struct file *file, void *priv,
795 struct v4l2_control *ctrl)
796{
797 struct video_device *dev = video_devdata(file);
798 struct usb_usbvision *usbvision =
799 (struct usb_usbvision *) video_get_drvdata(dev);
800 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
483dfdb6 801
c5f48367
TM
802 return 0;
803}
804
805static int vidioc_s_ctrl (struct file *file, void *priv,
806 struct v4l2_control *ctrl)
807{
808 struct video_device *dev = video_devdata(file);
809 struct usb_usbvision *usbvision =
810 (struct usb_usbvision *) video_get_drvdata(dev);
811 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
812
813 return 0;
814}
815
816static int vidioc_reqbufs (struct file *file,
817 void *priv, struct v4l2_requestbuffers *vr)
818{
819 struct video_device *dev = video_devdata(file);
820 struct usb_usbvision *usbvision =
821 (struct usb_usbvision *) video_get_drvdata(dev);
822 int ret;
823
824 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
825
826 /* Check input validity:
827 the user must do a VIDEO CAPTURE and MMAP method. */
828 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
829 (vr->memory != V4L2_MEMORY_MMAP))
830 return -EINVAL;
831
832 if(usbvision->streaming == Stream_On) {
833 if ((ret = usbvision_stream_interrupt(usbvision)))
834 return ret;
483dfdb6 835 }
c5f48367
TM
836
837 usbvision_frames_free(usbvision);
838 usbvision_empty_framequeues(usbvision);
839 vr->count = usbvision_frames_alloc(usbvision,vr->count);
840
841 usbvision->curFrame = NULL;
842
483dfdb6
TM
843 return 0;
844}
845
c5f48367
TM
846static int vidioc_querybuf (struct file *file,
847 void *priv, struct v4l2_buffer *vb)
483dfdb6 848{
c5f48367
TM
849 struct video_device *dev = video_devdata(file);
850 struct usb_usbvision *usbvision =
851 (struct usb_usbvision *) video_get_drvdata(dev);
852 struct usbvision_frame *frame;
853
854 /* FIXME : must control
855 that buffers are mapped (VIDIOC_REQBUFS has been called) */
856 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
857 return -EINVAL;
858 }
859 if(vb->index>=usbvision->num_frames) {
860 return -EINVAL;
861 }
862 /* Updating the corresponding frame state */
863 vb->flags = 0;
864 frame = &usbvision->frame[vb->index];
865 if(frame->grabstate >= FrameState_Ready)
866 vb->flags |= V4L2_BUF_FLAG_QUEUED;
867 if(frame->grabstate >= FrameState_Done)
868 vb->flags |= V4L2_BUF_FLAG_DONE;
869 if(frame->grabstate == FrameState_Unused)
870 vb->flags |= V4L2_BUF_FLAG_MAPPED;
871 vb->memory = V4L2_MEMORY_MMAP;
872
873 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
874
875 vb->memory = V4L2_MEMORY_MMAP;
876 vb->field = V4L2_FIELD_NONE;
877 vb->length = usbvision->curwidth*
878 usbvision->curheight*
879 usbvision->palette.bytes_per_pixel;
880 vb->timestamp = usbvision->frame[vb->index].timestamp;
881 vb->sequence = usbvision->frame[vb->index].sequence;
882 return 0;
483dfdb6
TM
883}
884
c5f48367
TM
885static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
886{
887 struct video_device *dev = video_devdata(file);
888 struct usb_usbvision *usbvision =
889 (struct usb_usbvision *) video_get_drvdata(dev);
890 struct usbvision_frame *frame;
891 unsigned long lock_flags;
892
893 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
894 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
895 return -EINVAL;
896 }
897 if(vb->index>=usbvision->num_frames) {
898 return -EINVAL;
899 }
900
901 frame = &usbvision->frame[vb->index];
902
903 if (frame->grabstate != FrameState_Unused) {
904 return -EAGAIN;
905 }
906
907 /* Mark it as ready and enqueue frame */
908 frame->grabstate = FrameState_Ready;
909 frame->scanstate = ScanState_Scanning;
910 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
911
912 vb->flags &= ~V4L2_BUF_FLAG_DONE;
913
914 /* set v4l2_format index */
915 frame->v4l2_format = usbvision->palette;
916
917 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
918 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
919 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
920
921 return 0;
922}
923
924static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
925{
926 struct video_device *dev = video_devdata(file);
927 struct usb_usbvision *usbvision =
928 (struct usb_usbvision *) video_get_drvdata(dev);
929 int ret;
930 struct usbvision_frame *f;
931 unsigned long lock_flags;
932
933 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
934 return -EINVAL;
935
936 if (list_empty(&(usbvision->outqueue))) {
937 if (usbvision->streaming == Stream_Idle)
938 return -EINVAL;
939 ret = wait_event_interruptible
940 (usbvision->wait_frame,
941 !list_empty(&(usbvision->outqueue)));
942 if (ret)
943 return ret;
944 }
945
946 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
947 f = list_entry(usbvision->outqueue.next,
948 struct usbvision_frame, frame);
949 list_del(usbvision->outqueue.next);
950 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
951
952 f->grabstate = FrameState_Unused;
953
954 vb->memory = V4L2_MEMORY_MMAP;
955 vb->flags = V4L2_BUF_FLAG_MAPPED |
956 V4L2_BUF_FLAG_QUEUED |
957 V4L2_BUF_FLAG_DONE;
958 vb->index = f->index;
959 vb->sequence = f->sequence;
960 vb->timestamp = f->timestamp;
961 vb->field = V4L2_FIELD_NONE;
962 vb->bytesused = f->scanlength;
963
964 return 0;
965}
966
967static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
968{
969 struct video_device *dev = video_devdata(file);
970 struct usb_usbvision *usbvision =
971 (struct usb_usbvision *) video_get_drvdata(dev);
972 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
973
974 usbvision->streaming = Stream_On;
975 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
976
977 return 0;
978}
979
980static int vidioc_streamoff(struct file *file,
981 void *priv, enum v4l2_buf_type type)
982{
983 struct video_device *dev = video_devdata(file);
984 struct usb_usbvision *usbvision =
985 (struct usb_usbvision *) video_get_drvdata(dev);
986 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
987
988 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
989 return -EINVAL;
990
991 if(usbvision->streaming == Stream_On) {
992 usbvision_stream_interrupt(usbvision);
993 /* Stop all video streamings */
994 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
995 }
996 usbvision_empty_framequeues(usbvision);
997
998 return 0;
999}
1000
1001static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1002 struct v4l2_fmtdesc *vfd)
1003{
1004 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
1005 return -EINVAL;
1006 }
1007 vfd->flags = 0;
1008 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
1010 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
1011 memset(vfd->reserved, 0, sizeof(vfd->reserved));
1012 return 0;
1013}
1014
1015static int vidioc_g_fmt_cap (struct file *file, void *priv,
1016 struct v4l2_format *vf)
1017{
1018 struct video_device *dev = video_devdata(file);
1019 struct usb_usbvision *usbvision =
1020 (struct usb_usbvision *) video_get_drvdata(dev);
1021 vf->fmt.pix.width = usbvision->curwidth;
1022 vf->fmt.pix.height = usbvision->curheight;
1023 vf->fmt.pix.pixelformat = usbvision->palette.format;
1024 vf->fmt.pix.bytesperline =
1025 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
1026 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
1027 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1028 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
1029
1030 return 0;
1031}
1032
1033static int vidioc_try_fmt_cap (struct file *file, void *priv,
1034 struct v4l2_format *vf)
1035{
1036 struct video_device *dev = video_devdata(file);
1037 struct usb_usbvision *usbvision =
1038 (struct usb_usbvision *) video_get_drvdata(dev);
1039 int formatIdx;
1040
1041 /* Find requested format in available ones */
1042 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1043 if(vf->fmt.pix.pixelformat ==
1044 usbvision_v4l2_format[formatIdx].format) {
1045 usbvision->palette = usbvision_v4l2_format[formatIdx];
1046 break;
1047 }
1048 }
1049 /* robustness */
1050 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1051 return -EINVAL;
1052 }
1053 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1054 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1055
1056 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
1057 usbvision->palette.bytes_per_pixel;
1058 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1059
1060 return 0;
1061}
1062
1063static int vidioc_s_fmt_cap(struct file *file, void *priv,
1064 struct v4l2_format *vf)
1065{
1066 struct video_device *dev = video_devdata(file);
1067 struct usb_usbvision *usbvision =
1068 (struct usb_usbvision *) video_get_drvdata(dev);
1069 int ret;
1070
1071 if( 0 != (ret=vidioc_try_fmt_cap (file, priv, vf)) ) {
1072 return ret;
1073 }
1074
1075 /* stop io in case it is already in progress */
1076 if(usbvision->streaming == Stream_On) {
1077 if ((ret = usbvision_stream_interrupt(usbvision)))
1078 return ret;
1079 }
1080 usbvision_frames_free(usbvision);
1081 usbvision_empty_framequeues(usbvision);
1082
1083 usbvision->curFrame = NULL;
1084
1085 /* by now we are committed to the new data... */
289d4d2f 1086 mutex_lock(&usbvision->lock);
c5f48367 1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
289d4d2f 1088 mutex_unlock(&usbvision->lock);
c5f48367
TM
1089
1090 return 0;
1091}
483dfdb6 1092
8a5ab415 1093static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
483dfdb6
TM
1094 size_t count, loff_t *ppos)
1095{
1096 struct video_device *dev = video_devdata(file);
c5f48367
TM
1097 struct usb_usbvision *usbvision =
1098 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6
TM
1099 int noblock = file->f_flags & O_NONBLOCK;
1100 unsigned long lock_flags;
1101
483dfdb6
TM
1102 int ret,i;
1103 struct usbvision_frame *frame;
1104
c5f48367
TM
1105 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__,
1106 (unsigned long)count, noblock);
483dfdb6
TM
1107
1108 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1109 return -EFAULT;
1110
c5f48367
TM
1111 /* This entry point is compatible with the mmap routines
1112 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1113 to get frames or call read on the device. */
6f78e186 1114 if(!usbvision->num_frames) {
c5f48367
TM
1115 /* First, allocate some frames to work with
1116 if this has not been done with VIDIOC_REQBUF */
6f78e186
TM
1117 usbvision_frames_free(usbvision);
1118 usbvision_empty_framequeues(usbvision);
1119 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1120 }
1121
1122 if(usbvision->streaming != Stream_On) {
1123 /* no stream is running, make it running ! */
1124 usbvision->streaming = Stream_On;
1125 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1126 }
483dfdb6 1127
c5f48367
TM
1128 /* Then, enqueue as many frames as possible
1129 (like a user of VIDIOC_QBUF would do) */
6f78e186 1130 for(i=0;i<usbvision->num_frames;i++) {
483dfdb6
TM
1131 frame = &usbvision->frame[i];
1132 if(frame->grabstate == FrameState_Unused) {
1133 /* Mark it as ready and enqueue frame */
1134 frame->grabstate = FrameState_Ready;
1135 frame->scanstate = ScanState_Scanning;
c5f48367
TM
1136 /* Accumulated in usbvision_parse_data() */
1137 frame->scanlength = 0;
483dfdb6
TM
1138
1139 /* set v4l2_format index */
1140 frame->v4l2_format = usbvision->palette;
1141
1142 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1143 list_add_tail(&frame->frame, &usbvision->inqueue);
c5f48367
TM
1144 spin_unlock_irqrestore(&usbvision->queue_lock,
1145 lock_flags);
483dfdb6
TM
1146 }
1147 }
1148
1149 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1150 if (list_empty(&(usbvision->outqueue))) {
1151 if(noblock)
1152 return -EAGAIN;
1153
1154 ret = wait_event_interruptible
1155 (usbvision->wait_frame,
1156 !list_empty(&(usbvision->outqueue)));
1157 if (ret)
1158 return ret;
1159 }
1160
1161 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1162 frame = list_entry(usbvision->outqueue.next,
1163 struct usbvision_frame, frame);
1164 list_del(usbvision->outqueue.next);
1165 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1166
1167 /* An error returns an empty frame */
1168 if (frame->grabstate == FrameState_Error) {
1169 frame->bytes_read = 0;
1170 return 0;
1171 }
1172
c5f48367
TM
1173 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1174 __FUNCTION__,
1175 frame->index, frame->bytes_read, frame->scanlength);
483dfdb6
TM
1176
1177 /* copy bytes to user space; we allow for partials reads */
1178 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1179 count = frame->scanlength - frame->bytes_read;
1180
1181 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1182 return -EFAULT;
1183 }
1184
1185 frame->bytes_read += count;
c5f48367
TM
1186 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1187 __FUNCTION__,
1188 (unsigned long)count, frame->bytes_read);
483dfdb6 1189
c5f48367 1190 /* For now, forget the frame if it has not been read in one shot. */
483dfdb6
TM
1191/* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1192 frame->bytes_read = 0;
1193
1194 /* Mark it as available to be used again. */
c430ca1e 1195 frame->grabstate = FrameState_Unused;
483dfdb6
TM
1196/* } */
1197
1198 return count;
1199}
1200
1201static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1202{
1203 unsigned long size = vma->vm_end - vma->vm_start,
1204 start = vma->vm_start;
1205 void *pos;
1206 u32 i;
1207
1208 struct video_device *dev = video_devdata(file);
c5f48367
TM
1209 struct usb_usbvision *usbvision =
1210 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6 1211
6f78e186
TM
1212 PDEBUG(DBG_MMAP, "mmap");
1213
289d4d2f 1214 mutex_lock(&usbvision->lock);
483dfdb6
TM
1215
1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
289d4d2f 1217 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1218 return -EFAULT;
1219 }
1220
1221 if (!(vma->vm_flags & VM_WRITE) ||
6f78e186 1222 size != PAGE_ALIGN(usbvision->max_frame_size)) {
289d4d2f 1223 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1224 return -EINVAL;
1225 }
1226
6f78e186 1227 for (i = 0; i < usbvision->num_frames; i++) {
c5f48367
TM
1228 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1229 vma->vm_pgoff)
483dfdb6
TM
1230 break;
1231 }
6f78e186 1232 if (i == usbvision->num_frames) {
c5f48367
TM
1233 PDEBUG(DBG_MMAP,
1234 "mmap: user supplied mapping address is out of range");
289d4d2f 1235 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1236 return -EINVAL;
1237 }
1238
1239 /* VM_IO is eventually going to replace PageReserved altogether */
1240 vma->vm_flags |= VM_IO;
1241 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1242
1243 pos = usbvision->frame[i].data;
1244 while (size > 0) {
1245
1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
c876a346 1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
289d4d2f 1248 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1249 return -EAGAIN;
1250 }
1251 start += PAGE_SIZE;
1252 pos += PAGE_SIZE;
1253 size -= PAGE_SIZE;
1254 }
1255
289d4d2f 1256 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1257 return 0;
1258}
1259
1260
1261/*
1262 * Here comes the stuff for radio on usbvision based devices
1263 *
1264 */
1265static int usbvision_radio_open(struct inode *inode, struct file *file)
1266{
1267 struct video_device *dev = video_devdata(file);
c5f48367
TM
1268 struct usb_usbvision *usbvision =
1269 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6
TM
1270 int errCode = 0;
1271
1272 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1273
289d4d2f 1274 mutex_lock(&usbvision->lock);
483dfdb6
TM
1275
1276 if (usbvision->user) {
1277 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1278 errCode = -EBUSY;
1279 }
1280 else {
1281 if(PowerOnAtOpen) {
1282 usbvision_reset_powerOffTimer(usbvision);
1283 if (usbvision->power == 0) {
1284 usbvision_power_on(usbvision);
1ff16c20 1285 usbvision_i2c_register(usbvision);
483dfdb6
TM
1286 }
1287 }
1288
2a9f8b5d
TM
1289 /* Alternate interface 1 is is the biggest frame size */
1290 errCode = usbvision_set_alternate(usbvision);
1291 if (errCode < 0) {
1292 usbvision->last_error = errCode;
544e6175
AM
1293 errCode = -EBUSY;
1294 goto out;
2a9f8b5d
TM
1295 }
1296
483dfdb6
TM
1297 // If so far no errors then we shall start the radio
1298 usbvision->radio = 1;
1299 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
483dfdb6
TM
1300 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1301 usbvision->user++;
1302 }
1303
1304 if (errCode) {
1305 if (PowerOnAtOpen) {
1ff16c20 1306 usbvision_i2c_unregister(usbvision);
483dfdb6
TM
1307 usbvision_power_off(usbvision);
1308 usbvision->initialized = 0;
1309 }
1310 }
544e6175 1311out:
289d4d2f 1312 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1313 return errCode;
1314}
1315
1316
1317static int usbvision_radio_close(struct inode *inode, struct file *file)
1318{
1319 struct video_device *dev = video_devdata(file);
c5f48367
TM
1320 struct usb_usbvision *usbvision =
1321 (struct usb_usbvision *) video_get_drvdata(dev);
483dfdb6
TM
1322 int errCode = 0;
1323
1324 PDEBUG(DBG_IO, "");
1325
289d4d2f 1326 mutex_lock(&usbvision->lock);
483dfdb6 1327
2a9f8b5d
TM
1328 /* Set packet size to 0 */
1329 usbvision->ifaceAlt=0;
1330 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1331 usbvision->ifaceAlt);
1332
483dfdb6
TM
1333 usbvision_audio_off(usbvision);
1334 usbvision->radio=0;
1335 usbvision->user--;
1336
1337 if (PowerOnAtOpen) {
1338 usbvision_set_powerOffTimer(usbvision);
1339 usbvision->initialized = 0;
1340 }
1341
289d4d2f 1342 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1343
1344 if (usbvision->remove_pending) {
de6a1b8e 1345 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
483dfdb6
TM
1346 usbvision_release(usbvision);
1347 }
1348
1349
1350 PDEBUG(DBG_IO, "success");
1351
1352 return errCode;
1353}
1354
483dfdb6
TM
1355/*
1356 * Here comes the stuff for vbi on usbvision based devices
1357 *
1358 */
1359static int usbvision_vbi_open(struct inode *inode, struct file *file)
1360{
1361 /* TODO */
c5f48367 1362 return -ENODEV;
483dfdb6
TM
1363
1364}
1365
1366static int usbvision_vbi_close(struct inode *inode, struct file *file)
1367{
1368 /* TODO */
c5f48367 1369 return -ENODEV;
483dfdb6
TM
1370}
1371
1372static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1373 unsigned int cmd, void *arg)
1374{
1375 /* TODO */
c5f48367 1376 return -ENOIOCTLCMD;
483dfdb6
TM
1377}
1378
1379static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1380 unsigned int cmd, unsigned long arg)
1381{
1382 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1383}
1384
1385
1386//
1387// Video registration stuff
1388//
1389
1390// Video template
fa027c2a 1391static const struct file_operations usbvision_fops = {
483dfdb6 1392 .owner = THIS_MODULE,
483dfdb6
TM
1393 .open = usbvision_v4l2_open,
1394 .release = usbvision_v4l2_close,
1395 .read = usbvision_v4l2_read,
1396 .mmap = usbvision_v4l2_mmap,
c5f48367 1397 .ioctl = video_ioctl2,
483dfdb6 1398 .llseek = no_llseek,
c5f48367 1399/* .poll = video_poll, */
c5f48367 1400 .compat_ioctl = v4l_compat_ioctl32,
483dfdb6
TM
1401};
1402static struct video_device usbvision_video_template = {
483dfdb6 1403 .owner = THIS_MODULE,
483dfdb6 1404 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
483dfdb6 1405 .fops = &usbvision_fops,
483dfdb6
TM
1406 .name = "usbvision-video",
1407 .release = video_device_release,
483dfdb6 1408 .minor = -1,
c5f48367
TM
1409 .vidioc_querycap = vidioc_querycap,
1410 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1411 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1412 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1413 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1414 .vidioc_reqbufs = vidioc_reqbufs,
1415 .vidioc_querybuf = vidioc_querybuf,
1416 .vidioc_qbuf = vidioc_qbuf,
1417 .vidioc_dqbuf = vidioc_dqbuf,
1418 .vidioc_s_std = vidioc_s_std,
1419 .vidioc_enum_input = vidioc_enum_input,
1420 .vidioc_g_input = vidioc_g_input,
1421 .vidioc_s_input = vidioc_s_input,
1422 .vidioc_queryctrl = vidioc_queryctrl,
1423 .vidioc_g_audio = vidioc_g_audio,
ed4d6375 1424 .vidioc_s_audio = vidioc_s_audio,
c5f48367
TM
1425 .vidioc_g_ctrl = vidioc_g_ctrl,
1426 .vidioc_s_ctrl = vidioc_s_ctrl,
1427 .vidioc_streamon = vidioc_streamon,
1428 .vidioc_streamoff = vidioc_streamoff,
1429#ifdef CONFIG_VIDEO_V4L1_COMPAT
1430/* .vidiocgmbuf = vidiocgmbuf, */
1431#endif
1432 .vidioc_g_tuner = vidioc_g_tuner,
1433 .vidioc_s_tuner = vidioc_s_tuner,
1434 .vidioc_g_frequency = vidioc_g_frequency,
1435 .vidioc_s_frequency = vidioc_s_frequency,
1436#ifdef CONFIG_VIDEO_ADV_DEBUG
1437 .vidioc_g_register = vidioc_g_register,
1438 .vidioc_s_register = vidioc_s_register,
1439#endif
1440 .tvnorms = USBVISION_NORMS,
1441 .current_norm = V4L2_STD_PAL
483dfdb6
TM
1442};
1443
1444
1445// Radio template
fa027c2a 1446static const struct file_operations usbvision_radio_fops = {
483dfdb6 1447 .owner = THIS_MODULE,
483dfdb6
TM
1448 .open = usbvision_radio_open,
1449 .release = usbvision_radio_close,
c5f48367 1450 .ioctl = video_ioctl2,
483dfdb6 1451 .llseek = no_llseek,
c5f48367 1452 .compat_ioctl = v4l_compat_ioctl32,
483dfdb6
TM
1453};
1454
1455static struct video_device usbvision_radio_template=
1456{
483dfdb6 1457 .owner = THIS_MODULE,
483dfdb6 1458 .type = VID_TYPE_TUNER,
483dfdb6 1459 .fops = &usbvision_radio_fops,
483dfdb6 1460 .name = "usbvision-radio",
c5f48367 1461 .release = video_device_release,
483dfdb6 1462 .minor = -1,
c5f48367
TM
1463 .vidioc_querycap = vidioc_querycap,
1464 .vidioc_enum_input = vidioc_enum_input,
1465 .vidioc_g_input = vidioc_g_input,
1466 .vidioc_s_input = vidioc_s_input,
1467 .vidioc_queryctrl = vidioc_queryctrl,
1468 .vidioc_g_audio = vidioc_g_audio,
ed4d6375 1469 .vidioc_s_audio = vidioc_s_audio,
c5f48367
TM
1470 .vidioc_g_ctrl = vidioc_g_ctrl,
1471 .vidioc_s_ctrl = vidioc_s_ctrl,
1472 .vidioc_g_tuner = vidioc_g_tuner,
1473 .vidioc_s_tuner = vidioc_s_tuner,
1474 .vidioc_g_frequency = vidioc_g_frequency,
1475 .vidioc_s_frequency = vidioc_s_frequency,
1476
1477 .tvnorms = USBVISION_NORMS,
1478 .current_norm = V4L2_STD_PAL
483dfdb6
TM
1479};
1480
483dfdb6 1481// vbi template
fa027c2a 1482static const struct file_operations usbvision_vbi_fops = {
483dfdb6 1483 .owner = THIS_MODULE,
483dfdb6
TM
1484 .open = usbvision_vbi_open,
1485 .release = usbvision_vbi_close,
1486 .ioctl = usbvision_vbi_ioctl,
1487 .llseek = no_llseek,
c5f48367 1488 .compat_ioctl = v4l_compat_ioctl32,
483dfdb6
TM
1489};
1490
1491static struct video_device usbvision_vbi_template=
1492{
483dfdb6 1493 .owner = THIS_MODULE,
483dfdb6 1494 .type = VID_TYPE_TUNER,
483dfdb6 1495 .fops = &usbvision_vbi_fops,
483dfdb6
TM
1496 .release = video_device_release,
1497 .name = "usbvision-vbi",
483dfdb6
TM
1498 .minor = -1,
1499};
1500
1501
1502static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1503 struct video_device *vdev_template,
1504 char *name)
1505{
1506 struct usb_device *usb_dev = usbvision->dev;
1507 struct video_device *vdev;
1508
1509 if (usb_dev == NULL) {
1510 err("%s: usbvision->dev is not set", __FUNCTION__);
1511 return NULL;
1512 }
1513
1514 vdev = video_device_alloc();
1515 if (NULL == vdev) {
1516 return NULL;
1517 }
1518 *vdev = *vdev_template;
1519// vdev->minor = -1;
1520 vdev->dev = &usb_dev->dev;
1521 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1522 video_set_drvdata(vdev, usbvision);
1523 return vdev;
1524}
1525
1526// unregister video4linux devices
1527static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1528{
1529 // vbi Device:
1530 if (usbvision->vbi) {
c5f48367
TM
1531 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
1532 usbvision->vbi->minor & 0x1f);
483dfdb6
TM
1533 if (usbvision->vbi->minor != -1) {
1534 video_unregister_device(usbvision->vbi);
c5f48367 1535 } else {
483dfdb6
TM
1536 video_device_release(usbvision->vbi);
1537 }
1538 usbvision->vbi = NULL;
1539 }
1540
1541 // Radio Device:
1542 if (usbvision->rdev) {
c5f48367
TM
1543 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
1544 usbvision->rdev->minor & 0x1f);
483dfdb6
TM
1545 if (usbvision->rdev->minor != -1) {
1546 video_unregister_device(usbvision->rdev);
c5f48367 1547 } else {
483dfdb6
TM
1548 video_device_release(usbvision->rdev);
1549 }
1550 usbvision->rdev = NULL;
1551 }
1552
1553 // Video Device:
1554 if (usbvision->vdev) {
c5f48367
TM
1555 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
1556 usbvision->vdev->minor & 0x1f);
483dfdb6
TM
1557 if (usbvision->vdev->minor != -1) {
1558 video_unregister_device(usbvision->vdev);
c5f48367 1559 } else {
483dfdb6
TM
1560 video_device_release(usbvision->vdev);
1561 }
1562 usbvision->vdev = NULL;
1563 }
1564}
1565
1566// register video4linux devices
1567static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1568{
1569 // Video Device:
c5f48367
TM
1570 usbvision->vdev = usbvision_vdev_init(usbvision,
1571 &usbvision_video_template,
1572 "USBVision Video");
483dfdb6
TM
1573 if (usbvision->vdev == NULL) {
1574 goto err_exit;
1575 }
c5f48367
TM
1576 if (video_register_device(usbvision->vdev,
1577 VFL_TYPE_GRABBER,
1578 video_nr)<0) {
483dfdb6
TM
1579 goto err_exit;
1580 }
c5f48367
TM
1581 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
1582 usbvision->nr,usbvision->vdev->minor & 0x1f);
483dfdb6
TM
1583
1584 // Radio Device:
1585 if (usbvision_device_data[usbvision->DevModel].Radio) {
1586 // usbvision has radio
c5f48367
TM
1587 usbvision->rdev = usbvision_vdev_init(usbvision,
1588 &usbvision_radio_template,
1589 "USBVision Radio");
483dfdb6
TM
1590 if (usbvision->rdev == NULL) {
1591 goto err_exit;
1592 }
c5f48367
TM
1593 if (video_register_device(usbvision->rdev,
1594 VFL_TYPE_RADIO,
1595 radio_nr)<0) {
483dfdb6
TM
1596 goto err_exit;
1597 }
c5f48367
TM
1598 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
1599 usbvision->nr, usbvision->rdev->minor & 0x1f);
483dfdb6
TM
1600 }
1601 // vbi Device:
1602 if (usbvision_device_data[usbvision->DevModel].vbi) {
c5f48367
TM
1603 usbvision->vbi = usbvision_vdev_init(usbvision,
1604 &usbvision_vbi_template,
1605 "USBVision VBI");
483dfdb6
TM
1606 if (usbvision->vdev == NULL) {
1607 goto err_exit;
1608 }
c5f48367
TM
1609 if (video_register_device(usbvision->vbi,
1610 VFL_TYPE_VBI,
1611 vbi_nr)<0) {
483dfdb6
TM
1612 goto err_exit;
1613 }
c5f48367
TM
1614 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
1615 usbvision->nr,usbvision->vbi->minor & 0x1f);
483dfdb6
TM
1616 }
1617 // all done
1618 return 0;
1619
1620 err_exit:
1621 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1622 usbvision_unregister_video(usbvision);
1623 return -1;
1624}
1625
1626/*
1627 * usbvision_alloc()
1628 *
c5f48367
TM
1629 * This code allocates the struct usb_usbvision.
1630 * It is filled with default values.
483dfdb6
TM
1631 *
1632 * Returns NULL on error, a pointer to usb_usbvision else.
1633 *
1634 */
1635static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1636{
1637 struct usb_usbvision *usbvision;
1638
c5f48367
TM
1639 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
1640 NULL) {
483dfdb6
TM
1641 goto err_exit;
1642 }
1643
1644 usbvision->dev = dev;
1645
289d4d2f 1646 mutex_init(&usbvision->lock); /* available */
483dfdb6
TM
1647
1648 // prepare control urb for control messages during interrupts
1649 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1650 if (usbvision->ctrlUrb == NULL) {
1651 goto err_exit;
1652 }
1653 init_waitqueue_head(&usbvision->ctrlUrb_wq);
483dfdb6
TM
1654
1655 usbvision_init_powerOffTimer(usbvision);
1656
1657 return usbvision;
1658
1659err_exit:
1660 if (usbvision && usbvision->ctrlUrb) {
1661 usb_free_urb(usbvision->ctrlUrb);
1662 }
1663 if (usbvision) {
1664 kfree(usbvision);
1665 }
1666 return NULL;
1667}
1668
1669/*
1670 * usbvision_release()
1671 *
1672 * This code does final release of struct usb_usbvision. This happens
1673 * after the device is disconnected -and- all clients closed their files.
1674 *
1675 */
1676static void usbvision_release(struct usb_usbvision *usbvision)
1677{
1678 PDEBUG(DBG_PROBE, "");
1679
289d4d2f 1680 mutex_lock(&usbvision->lock);
483dfdb6
TM
1681
1682 usbvision_reset_powerOffTimer(usbvision);
1683
1684 usbvision->initialized = 0;
1685
289d4d2f 1686 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1687
1688 usbvision_remove_sysfs(usbvision->vdev);
1689 usbvision_unregister_video(usbvision);
1690
1691 if (usbvision->ctrlUrb) {
1692 usb_free_urb(usbvision->ctrlUrb);
1693 }
1694
1695 kfree(usbvision);
1696
1697 PDEBUG(DBG_PROBE, "success");
1698}
1699
1700
c5f48367 1701/*********************** usb interface **********************************/
483dfdb6
TM
1702
1703static void usbvision_configure_video(struct usb_usbvision *usbvision)
1704{
c5f48367 1705 int model;
483dfdb6
TM
1706
1707 if (usbvision == NULL)
1708 return;
1709
1710 model = usbvision->DevModel;
1711 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1712
c682b3a7 1713 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
c5f48367
TM
1714 usbvision->Vin_Reg2_Preset =
1715 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
483dfdb6
TM
1716 } else {
1717 usbvision->Vin_Reg2_Preset = 0;
1718 }
1719
c5f48367 1720 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
483dfdb6
TM
1721
1722 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1723 usbvision->ctl_input = 0;
1724
1725 /* This should be here to make i2c clients to be able to register */
c5f48367
TM
1726 /* first switch off audio */
1727 usbvision_audio_off(usbvision);
483dfdb6 1728 if (!PowerOnAtOpen) {
c5f48367
TM
1729 /* and then power up the noisy tuner */
1730 usbvision_power_on(usbvision);
1ff16c20 1731 usbvision_i2c_register(usbvision);
483dfdb6
TM
1732 }
1733}
1734
1735/*
1736 * usbvision_probe()
1737 *
1738 * This procedure queries device descriptor and accepts the interface
1739 * if it looks like USBVISION video device
1740 *
1741 */
659ae56d
MCC
1742static int __devinit usbvision_probe(struct usb_interface *intf,
1743 const struct usb_device_id *devid)
483dfdb6 1744{
2a9f8b5d
TM
1745 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1746 struct usb_interface *uif;
483dfdb6
TM
1747 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1748 const struct usb_host_interface *interface;
1749 struct usb_usbvision *usbvision = NULL;
1750 const struct usb_endpoint_descriptor *endpoint;
2a9f8b5d 1751 int model,i;
483dfdb6
TM
1752
1753 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
659ae56d
MCC
1754 dev->descriptor.idVendor,
1755 dev->descriptor.idProduct, ifnum);
2a9f8b5d 1756
659ae56d 1757 model = devid->driver_info;
f8a389db 1758 if ( (model<0) || (model>=usbvision_device_data_size) ) {
672d013b 1759 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
f8a389db
MCC
1760 return -ENODEV;
1761 }
659ae56d
MCC
1762 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1763 usbvision_device_data[model].ModelString);
483dfdb6 1764
483dfdb6
TM
1765 if (usbvision_device_data[model].Interface >= 0) {
1766 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
c5f48367 1767 } else {
483dfdb6
TM
1768 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1769 }
1770 endpoint = &interface->endpoint[1].desc;
c5f48367
TM
1771 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1772 USB_ENDPOINT_XFER_ISOC) {
1773 err("%s: interface %d. has non-ISO endpoint!",
1774 __FUNCTION__, ifnum);
1775 err("%s: Endpoint attributes %d",
1776 __FUNCTION__, endpoint->bmAttributes);
483dfdb6
TM
1777 return -ENODEV;
1778 }
c5f48367
TM
1779 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1780 USB_DIR_OUT) {
1781 err("%s: interface %d. has ISO OUT endpoint!",
1782 __FUNCTION__, ifnum);
483dfdb6
TM
1783 return -ENODEV;
1784 }
1785
483dfdb6
TM
1786 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1787 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1788 return -ENOMEM;
1789 }
659ae56d 1790
483dfdb6
TM
1791 if (dev->descriptor.bNumConfigurations > 1) {
1792 usbvision->bridgeType = BRIDGE_NT1004;
c5f48367 1793 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
483dfdb6 1794 usbvision->bridgeType = BRIDGE_NT1005;
c5f48367 1795 } else {
483dfdb6
TM
1796 usbvision->bridgeType = BRIDGE_NT1003;
1797 }
1798 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1799
289d4d2f 1800 mutex_lock(&usbvision->lock);
483dfdb6 1801
2a9f8b5d
TM
1802 /* compute alternate max packet sizes */
1803 uif = dev->actconfig->interface[0];
1804
1805 usbvision->num_alt=uif->num_altsetting;
1806 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1807 usbvision->alt_max_pkt_size = kmalloc(32*
1808 usbvision->num_alt,GFP_KERNEL);
1809 if (usbvision->alt_max_pkt_size == NULL) {
1810 err("usbvision: out of memory!\n");
0991112c 1811 mutex_unlock(&usbvision->lock);
2a9f8b5d
TM
1812 return -ENOMEM;
1813 }
1814
1815 for (i = 0; i < usbvision->num_alt ; i++) {
1816 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1817 wMaxPacketSize);
1818 usbvision->alt_max_pkt_size[i] =
1819 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1820 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1821 usbvision->alt_max_pkt_size[i]);
1822 }
1823
1824
483dfdb6
TM
1825 usbvision->nr = usbvision_nr++;
1826
1827 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1828 if (usbvision->have_tuner) {
1829 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1830 }
1831
1832 usbvision->tuner_addr = ADDR_UNSET;
1833
1834 usbvision->DevModel = model;
1835 usbvision->remove_pending = 0;
1836 usbvision->iface = ifnum;
2a9f8b5d 1837 usbvision->ifaceAlt = 0;
483dfdb6
TM
1838 usbvision->video_endp = endpoint->bEndpointAddress;
1839 usbvision->isocPacketSize = 0;
1840 usbvision->usb_bandwidth = 0;
1841 usbvision->user = 0;
1842 usbvision->streaming = Stream_Off;
1843 usbvision_register_video(usbvision);
1844 usbvision_configure_video(usbvision);
289d4d2f 1845 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1846
1847
1848 usb_set_intfdata (intf, usbvision);
1849 usbvision_create_sysfs(usbvision->vdev);
1850
1851 PDEBUG(DBG_PROBE, "success");
1852 return 0;
1853}
1854
1855
1856/*
1857 * usbvision_disconnect()
1858 *
1859 * This procedure stops all driver activity, deallocates interface-private
1860 * structure (pointed by 'ptr') and after that driver should be removable
1861 * with no ill consequences.
1862 *
1863 */
1864static void __devexit usbvision_disconnect(struct usb_interface *intf)
1865{
1866 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1867
1868 PDEBUG(DBG_PROBE, "");
1869
1870 if (usbvision == NULL) {
1871 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1872 return;
1873 }
1874 usb_set_intfdata (intf, NULL);
1875
289d4d2f 1876 mutex_lock(&usbvision->lock);
483dfdb6
TM
1877
1878 // At this time we ask to cancel outstanding URBs
1879 usbvision_stop_isoc(usbvision);
1880
1881 if (usbvision->power) {
1ff16c20 1882 usbvision_i2c_unregister(usbvision);
483dfdb6
TM
1883 usbvision_power_off(usbvision);
1884 }
1885 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1886
1887 usb_put_dev(usbvision->dev);
1888 usbvision->dev = NULL; // USB device is no more
1889
289d4d2f 1890 mutex_unlock(&usbvision->lock);
483dfdb6
TM
1891
1892 if (usbvision->user) {
c5f48367
TM
1893 printk(KERN_INFO "%s: In use, disconnect pending\n",
1894 __FUNCTION__);
483dfdb6
TM
1895 wake_up_interruptible(&usbvision->wait_frame);
1896 wake_up_interruptible(&usbvision->wait_stream);
c5f48367 1897 } else {
483dfdb6
TM
1898 usbvision_release(usbvision);
1899 }
1900
1901 PDEBUG(DBG_PROBE, "success");
1902
1903}
1904
1905static struct usb_driver usbvision_driver = {
1906 .name = "usbvision",
1907 .id_table = usbvision_table,
1908 .probe = usbvision_probe,
1909 .disconnect = usbvision_disconnect
1910};
1911
483dfdb6
TM
1912/*
1913 * usbvision_init()
1914 *
1915 * This code is run to initialize the driver.
1916 *
1917 */
1918static int __init usbvision_init(void)
1919{
1920 int errCode;
1921
1922 PDEBUG(DBG_PROBE, "");
1923
483dfdb6
TM
1924 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1925 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
c876a346 1926 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
483dfdb6
TM
1927
1928 /* disable planar mode support unless compression enabled */
1929 if (isocMode != ISOC_MODE_COMPRESS ) {
1930 // FIXME : not the right way to set supported flag
1931 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1932 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1933 }
1934
483dfdb6
TM
1935 errCode = usb_register(&usbvision_driver);
1936
1937 if (errCode == 0) {
de6a1b8e 1938 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
483dfdb6
TM
1939 PDEBUG(DBG_PROBE, "success");
1940 }
1941 return errCode;
1942}
1943
1944static void __exit usbvision_exit(void)
1945{
1946 PDEBUG(DBG_PROBE, "");
1947
1948 usb_deregister(&usbvision_driver);
1949 PDEBUG(DBG_PROBE, "success");
1950}
1951
1952module_init(usbvision_init);
1953module_exit(usbvision_exit);
1954
1955/*
1956 * Overrides for Emacs so that we follow Linus's tabbing style.
1957 * ---------------------------------------------------------------------------
1958 * Local variables:
1959 * c-basic-offset: 8
1960 * End:
1961 */