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