blob: cfbfda47ec4fae0188b90187c457025942a43060 [file] [log] [blame]
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001/*
Mauro Carvalho Chehab7ca659e2006-12-09 11:43:31 -03002 * USB USBVISION Video device driver 0.9.9
Thierry MERLE483dfdb2006-12-04 08:31:45 -03003 *
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
Mauro Carvalho Chehabf30ebd42006-12-09 12:32:18 -030047#include <linux/version.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030048#include <linux/kernel.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030049#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
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -030071#include <linux/moduleparam.h>
72#include <linux/workqueue.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030073
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
Thierry MERLE483dfdb2006-12-04 08:31:45 -030094#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
Thierry MERLEc876a342006-12-09 16:42:54 -0300104#define DBG_MMAP 1<<3
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300105
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);
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -0300190MODULE_VERSION(USBVISION_VERSION_STRING);
191MODULE_ALIAS(DRIVER_ALIAS);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300192
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{
Dwaine Gardened00b412006-12-27 10:23:28 -0200206 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300207 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
Dwaine Gardened00b412006-12-27 10:23:28 -0200216static ssize_t show_model(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300217{
Dwaine Gardened00b412006-12-27 10:23:28 -0200218 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300219 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
Dwaine Gardened00b412006-12-27 10:23:28 -0200224static ssize_t show_hue(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300225{
Dwaine Gardened00b412006-12-27 10:23:28 -0200226 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300227 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
228 struct v4l2_control ctrl;
229 ctrl.id = V4L2_CID_HUE;
230 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200231 if(usbvision->user)
232 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300233 return sprintf(buf, "%d\n", ctrl.value >> 8);
234}
235static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
236
Dwaine Gardened00b412006-12-27 10:23:28 -0200237static ssize_t show_contrast(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300238{
Dwaine Gardened00b412006-12-27 10:23:28 -0200239 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300240 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
241 struct v4l2_control ctrl;
242 ctrl.id = V4L2_CID_CONTRAST;
243 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200244 if(usbvision->user)
245 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300246 return sprintf(buf, "%d\n", ctrl.value >> 8);
247}
248static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
249
Dwaine Gardened00b412006-12-27 10:23:28 -0200250static ssize_t show_brightness(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300251{
Dwaine Gardened00b412006-12-27 10:23:28 -0200252 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300253 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
254 struct v4l2_control ctrl;
255 ctrl.id = V4L2_CID_BRIGHTNESS;
256 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200257 if(usbvision->user)
258 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300259 return sprintf(buf, "%d\n", ctrl.value >> 8);
260}
261static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
262
Dwaine Gardened00b412006-12-27 10:23:28 -0200263static ssize_t show_saturation(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300264{
Dwaine Gardened00b412006-12-27 10:23:28 -0200265 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300266 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
267 struct v4l2_control ctrl;
268 ctrl.id = V4L2_CID_SATURATION;
269 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200270 if(usbvision->user)
271 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300272 return sprintf(buf, "%d\n", ctrl.value >> 8);
273}
274static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
275
Dwaine Gardened00b412006-12-27 10:23:28 -0200276static ssize_t show_streaming(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300277{
Dwaine Gardened00b412006-12-27 10:23:28 -0200278 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300279 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
Dwaine Gardened00b412006-12-27 10:23:28 -0200284static ssize_t show_compression(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300285{
Dwaine Gardened00b412006-12-27 10:23:28 -0200286 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300287 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
Dwaine Gardened00b412006-12-27 10:23:28 -0200292static ssize_t show_device_bridge(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300293{
Dwaine Gardened00b412006-12-27 10:23:28 -0200294 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300295 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;
Dwaine Gardened00b412006-12-27 10:23:28 -0200303 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);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300345}
346
347static void usbvision_remove_sysfs(struct video_device *vdev)
348{
349 if (vdev) {
Dwaine Gardened00b412006-12-27 10:23:28 -0200350 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);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300368 }
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 {
Thierry MERLE6f78e182007-02-07 10:13:11 -0300394 /* Allocate memory for the scratch ring buffer */
395 errCode = usbvision_scratch_alloc(usbvision);
396 if (isocMode==ISOC_MODE_COMPRESS) {
397 /* Allocate intermediate decompression buffers only if needed */
398 errCode = usbvision_decompress_alloc(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300399 }
400 if (errCode) {
401 /* Deallocate all buffers if trouble */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300402 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300403 usbvision_decompress_free(usbvision);
404 }
405 }
406
407 /* If so far no errors then we shall start the camera */
408 if (!errCode) {
409 down(&usbvision->lock);
410 if (usbvision->power == 0) {
411 usbvision_power_on(usbvision);
412 usbvision_init_i2c(usbvision);
413 }
414
415 /* Send init sequence only once, it's large! */
416 if (!usbvision->initialized) {
417 int setup_ok = 0;
418 setup_ok = usbvision_setup(usbvision,isocMode);
419 if (setup_ok)
420 usbvision->initialized = 1;
421 else
422 errCode = -EBUSY;
423 }
424
425 if (!errCode) {
426 usbvision_begin_streaming(usbvision);
427 errCode = usbvision_init_isoc(usbvision);
428 /* device needs to be initialized before isoc transfer */
429 usbvision_muxsel(usbvision,0);
430 usbvision->user++;
431 }
432 else {
433 if (PowerOnAtOpen) {
434 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
435 usbvision_power_off(usbvision);
436 usbvision->initialized = 0;
437 }
438 }
439 up(&usbvision->lock);
440 }
441
442 if (errCode) {
443 }
444
445 /* prepare queues */
446 usbvision_empty_framequeues(usbvision);
447
448 PDEBUG(DBG_IO, "success");
449 return errCode;
450}
451
452/*
453 * usbvision_v4l2_close()
454 *
455 * This is part of Video 4 Linux API. The procedure
456 * stops streaming and deallocates all buffers that were earlier
457 * allocated in usbvision_v4l2_open().
458 *
459 */
460static int usbvision_v4l2_close(struct inode *inode, struct file *file)
461{
462 struct video_device *dev = video_devdata(file);
463 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
464
465 PDEBUG(DBG_IO, "close");
466 down(&usbvision->lock);
467
468 usbvision_audio_off(usbvision);
469 usbvision_restart_isoc(usbvision);
470 usbvision_stop_isoc(usbvision);
471
472 usbvision_decompress_free(usbvision);
Thierry MERLE38284ba2006-12-15 16:46:53 -0300473 usbvision_frames_free(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300474 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300475 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300476
477 usbvision->user--;
478
479 if (PowerOnAtOpen) {
480 /* power off in a little while to avoid off/on every close/open short sequences */
481 usbvision_set_powerOffTimer(usbvision);
482 usbvision->initialized = 0;
483 }
484
485 up(&usbvision->lock);
486
487 if (usbvision->remove_pending) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -0300488 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300489 usbvision_release(usbvision);
490 }
491
492 PDEBUG(DBG_IO, "success");
493
494
495 return 0;
496}
497
498
499/*
500 * usbvision_ioctl()
501 *
502 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
503 *
504 */
505static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
506 unsigned int cmd, void *arg)
507{
508 struct video_device *dev = video_devdata(file);
509 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
510
511 if (!USBVISION_IS_OPERATIONAL(usbvision))
512 return -EFAULT;
513
514 switch (cmd) {
515
516#ifdef CONFIG_VIDEO_ADV_DEBUG
517 /* ioctls to allow direct acces to the NT100x registers */
Trent Piepho52ebc762007-01-23 22:38:13 -0300518 case VIDIOC_DBG_G_REGISTER:
Trent Piepho52ebc762007-01-23 22:38:13 -0300519 case VIDIOC_DBG_S_REGISTER:
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300520 {
521 struct v4l2_register *reg = arg;
522 int errCode;
523
524 if (reg->i2c_id != 0)
525 return -EINVAL;
526 if (!capable(CAP_SYS_ADMIN))
527 return -EPERM;
Trent Piepho62d50ad2007-01-30 23:25:41 -0300528 /* NT100x has a 8-bit register space */
529 if (cmd == VIDIOC_DBG_G_REGISTER)
530 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
531 else
532 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300533 if (errCode < 0) {
Trent Piepho62d50ad2007-01-30 23:25:41 -0300534 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__,
535 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S', errCode);
536 return errCode;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300537 }
Trent Piepho62d50ad2007-01-30 23:25:41 -0300538 if (cmd == VIDIOC_DBG_S_REGISTER)
539 reg->val = (u8)errCode;
540
541 PDEBUG(DBG_IOCTL, "VIDIOC_DBG_%c_REGISTER reg=0x%02X, value=0x%02X",
542 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S',
543 (unsigned int)reg->reg, reg->val);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300544 return 0;
545 }
546#endif
547 case VIDIOC_QUERYCAP:
548 {
549 struct v4l2_capability *vc=arg;
550
551 memset(vc, 0, sizeof(*vc));
552 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
553 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
554 sizeof(vc->card));
555 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
556 sizeof(vc->bus_info));
557 vc->version = USBVISION_DRIVER_VERSION;
558 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
559 V4L2_CAP_AUDIO |
560 V4L2_CAP_READWRITE |
561 V4L2_CAP_STREAMING |
562 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
563 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
564 return 0;
565 }
566 case VIDIOC_ENUMINPUT:
567 {
568 struct v4l2_input *vi = arg;
569 int chan;
570
571 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
572 return -EINVAL;
573 if (usbvision->have_tuner) {
574 chan = vi->index;
575 }
576 else {
577 chan = vi->index + 1; //skip Television string
578 }
579 switch(chan) {
580 case 0:
581 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
582 strcpy(vi->name, "White Video Input");
583 }
584 else {
585 strcpy(vi->name, "Television");
586 vi->type = V4L2_INPUT_TYPE_TUNER;
587 vi->audioset = 1;
588 vi->tuner = chan;
589 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
590 }
591 break;
592 case 1:
593 vi->type = V4L2_INPUT_TYPE_CAMERA;
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "Green Video Input");
596 }
597 else {
598 strcpy(vi->name, "Composite Video Input");
599 }
600 vi->std = V4L2_STD_PAL;
601 break;
602 case 2:
603 vi->type = V4L2_INPUT_TYPE_CAMERA;
604 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
605 strcpy(vi->name, "Yellow Video Input");
606 }
607 else {
608 strcpy(vi->name, "S-Video Input");
609 }
610 vi->std = V4L2_STD_PAL;
611 break;
612 case 3:
613 vi->type = V4L2_INPUT_TYPE_CAMERA;
614 strcpy(vi->name, "Red Video Input");
615 vi->std = V4L2_STD_PAL;
616 break;
617 }
618 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
619 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
620 return 0;
621 }
622 case VIDIOC_ENUMSTD:
623 {
624 struct v4l2_standard *e = arg;
625 unsigned int i;
626 int ret;
627
628 i = e->index;
629 if (i >= TVNORMS)
630 return -EINVAL;
631 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
632 tvnorms[e->index].name);
633 e->index = i;
634 if (ret < 0)
635 return ret;
636 return 0;
637 }
638 case VIDIOC_G_INPUT:
639 {
640 int *input = arg;
641 *input = usbvision->ctl_input;
642 return 0;
643 }
644 case VIDIOC_S_INPUT:
645 {
646 int *input = arg;
647 if ((*input >= usbvision->video_inputs) || (*input < 0) )
648 return -EINVAL;
649 usbvision->ctl_input = *input;
650
651 down(&usbvision->lock);
652 usbvision_muxsel(usbvision, usbvision->ctl_input);
653 usbvision_set_input(usbvision);
654 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
655 up(&usbvision->lock);
656 return 0;
657 }
658 case VIDIOC_G_STD:
659 {
660 v4l2_std_id *id = arg;
661
662 *id = usbvision->tvnorm->id;
663
664 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
665 return 0;
666 }
667 case VIDIOC_S_STD:
668 {
669 v4l2_std_id *id = arg;
670 unsigned int i;
671
672 for (i = 0; i < TVNORMS; i++)
673 if (*id == tvnorms[i].id)
674 break;
675 if (i == TVNORMS)
676 for (i = 0; i < TVNORMS; i++)
677 if (*id & tvnorms[i].id)
678 break;
679 if (i == TVNORMS)
680 return -EINVAL;
681
682 down(&usbvision->lock);
683 usbvision->tvnorm = &tvnorms[i];
684
685 call_i2c_clients(usbvision, VIDIOC_S_STD,
686 &usbvision->tvnorm->id);
687
688 up(&usbvision->lock);
689
690 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
691 return 0;
692 }
693 case VIDIOC_G_TUNER:
694 {
695 struct v4l2_tuner *vt = arg;
696
697 if (!usbvision->have_tuner || vt->index) // Only tuner 0
698 return -EINVAL;
699 strcpy(vt->name, "Television");
700 /* Let clients fill in the remainder of this struct */
701 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
702
703 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
704 return 0;
705 }
706 case VIDIOC_S_TUNER:
707 {
708 struct v4l2_tuner *vt = arg;
709
710 // Only no or one tuner for now
711 if (!usbvision->have_tuner || vt->index)
712 return -EINVAL;
713 /* let clients handle this */
714 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
715
716 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
717 return 0;
718 }
719 case VIDIOC_G_FREQUENCY:
720 {
721 struct v4l2_frequency *freq = arg;
722
723 freq->tuner = 0; // Only one tuner
724 freq->type = V4L2_TUNER_ANALOG_TV;
725 freq->frequency = usbvision->freq;
726 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
727 return 0;
728 }
729 case VIDIOC_S_FREQUENCY:
730 {
731 struct v4l2_frequency *freq = arg;
732
733 // Only no or one tuner for now
734 if (!usbvision->have_tuner || freq->tuner)
735 return -EINVAL;
736
737 usbvision->freq = freq->frequency;
738 call_i2c_clients(usbvision, cmd, freq);
739 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
740 return 0;
741 }
742 case VIDIOC_G_AUDIO:
743 {
744 struct v4l2_audio *v = arg;
745 memset(v,0, sizeof(v));
746 strcpy(v->name, "TV");
747 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
748 return 0;
749 }
750 case VIDIOC_S_AUDIO:
751 {
752 struct v4l2_audio *v = arg;
753 if(v->index) {
754 return -EINVAL;
755 }
756 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
757 return 0;
758 }
759 case VIDIOC_QUERYCTRL:
760 {
761 struct v4l2_queryctrl *ctrl = arg;
762 int id=ctrl->id;
763
764 memset(ctrl,0,sizeof(*ctrl));
765 ctrl->id=id;
766
767 call_i2c_clients(usbvision, cmd, arg);
768
769 if (ctrl->type)
770 return 0;
771 else
772 return -EINVAL;
773
774 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
775 }
776 case VIDIOC_G_CTRL:
777 {
778 struct v4l2_control *ctrl = arg;
779 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
780 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
781 return 0;
782 }
783 case VIDIOC_S_CTRL:
784 {
785 struct v4l2_control *ctrl = arg;
786
787 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
788 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
789 return 0;
790 }
791 case VIDIOC_REQBUFS:
792 {
793 struct v4l2_requestbuffers *vr = arg;
794 int ret;
795
796 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
797
798 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
799 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
800 (vr->memory != V4L2_MEMORY_MMAP))
801 return -EINVAL;
802
803 if(usbvision->streaming == Stream_On) {
804 if ((ret = usbvision_stream_interrupt(usbvision)))
805 return ret;
806 }
807
Thierry MERLE6f78e182007-02-07 10:13:11 -0300808 usbvision_frames_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300809 usbvision_empty_framequeues(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300810 vr->count = usbvision_frames_alloc(usbvision,vr->count);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300811
812 usbvision->curFrame = NULL;
813
814 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
815 return 0;
816 }
817 case VIDIOC_QUERYBUF:
818 {
819 struct v4l2_buffer *vb = arg;
820 struct usbvision_frame *frame;
821
822 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
823
824 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
825 return -EINVAL;
826 }
Thierry MERLE6f78e182007-02-07 10:13:11 -0300827 if(vb->index>=usbvision->num_frames) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300828 return -EINVAL;
829 }
830 // Updating the corresponding frame state
831 vb->flags = 0;
832 frame = &usbvision->frame[vb->index];
833 if(frame->grabstate >= FrameState_Ready)
834 vb->flags |= V4L2_BUF_FLAG_QUEUED;
835 if(frame->grabstate >= FrameState_Done)
836 vb->flags |= V4L2_BUF_FLAG_DONE;
837 if(frame->grabstate == FrameState_Unused)
838 vb->flags |= V4L2_BUF_FLAG_MAPPED;
839 vb->memory = V4L2_MEMORY_MMAP;
840
Thierry MERLE6f78e182007-02-07 10:13:11 -0300841 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300842
843 vb->memory = V4L2_MEMORY_MMAP;
844 vb->field = V4L2_FIELD_NONE;
Thierry MERLEc876a342006-12-09 16:42:54 -0300845 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300846 vb->timestamp = usbvision->frame[vb->index].timestamp;
847 vb->sequence = usbvision->frame[vb->index].sequence;
848 return 0;
849 }
850 case VIDIOC_QBUF:
851 {
852 struct v4l2_buffer *vb = arg;
853 struct usbvision_frame *frame;
854 unsigned long lock_flags;
855
856 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
857 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
858 return -EINVAL;
859 }
Thierry MERLE6f78e182007-02-07 10:13:11 -0300860 if(vb->index>=usbvision->num_frames) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300861 return -EINVAL;
862 }
863
864 frame = &usbvision->frame[vb->index];
865
866 if (frame->grabstate != FrameState_Unused) {
867 return -EAGAIN;
868 }
869
870 /* Mark it as ready and enqueue frame */
871 frame->grabstate = FrameState_Ready;
872 frame->scanstate = ScanState_Scanning;
873 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
874
875 vb->flags &= ~V4L2_BUF_FLAG_DONE;
876
877 /* set v4l2_format index */
878 frame->v4l2_format = usbvision->palette;
879
880 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
881 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
882 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
883
884 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
885 return 0;
886 }
887 case VIDIOC_DQBUF:
888 {
889 struct v4l2_buffer *vb = arg;
890 int ret;
891 struct usbvision_frame *f;
892 unsigned long lock_flags;
893
894 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895 return -EINVAL;
896
897 if (list_empty(&(usbvision->outqueue))) {
898 if (usbvision->streaming == Stream_Idle)
899 return -EINVAL;
900 ret = wait_event_interruptible
901 (usbvision->wait_frame,
902 !list_empty(&(usbvision->outqueue)));
903 if (ret)
904 return ret;
905 }
906
907 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
908 f = list_entry(usbvision->outqueue.next,
909 struct usbvision_frame, frame);
910 list_del(usbvision->outqueue.next);
911 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
912
913 f->grabstate = FrameState_Unused;
914
915 vb->memory = V4L2_MEMORY_MMAP;
916 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
917 vb->index = f->index;
918 vb->sequence = f->sequence;
919 vb->timestamp = f->timestamp;
920 vb->field = V4L2_FIELD_NONE;
921 vb->bytesused = f->scanlength;
922
923 return 0;
924 }
925 case VIDIOC_STREAMON:
926 {
927 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
928
929 usbvision->streaming = Stream_On;
930
931 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
932
933 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
934
935 return 0;
936 }
937 case VIDIOC_STREAMOFF:
938 {
939 int *type = arg;
940 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
941
942 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
943 return -EINVAL;
944
945 if(usbvision->streaming == Stream_On) {
946 usbvision_stream_interrupt(usbvision);
947 // Stop all video streamings
948 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
949 }
950 usbvision_empty_framequeues(usbvision);
951
952 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
953 return 0;
954 }
955 case VIDIOC_ENUM_FMT:
956 {
957 struct v4l2_fmtdesc *vfd = arg;
958
959 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
960 return -EINVAL;
961 }
962 vfd->flags = 0;
963 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
964 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
965 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
966 memset(vfd->reserved, 0, sizeof(vfd->reserved));
967 return 0;
968 }
969 case VIDIOC_G_FMT:
970 {
971 struct v4l2_format *vf = arg;
972
973 switch (vf->type) {
974 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
975 {
976 vf->fmt.pix.width = usbvision->curwidth;
977 vf->fmt.pix.height = usbvision->curheight;
978 vf->fmt.pix.pixelformat = usbvision->palette.format;
979 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
980 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
981 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
982 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
Thierry MERLEc876a342006-12-09 16:42:54 -0300983 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
984 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
985 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300986 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300987 default:
988 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
989 return -EINVAL;
990 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300991 return 0;
992 }
993 case VIDIOC_TRY_FMT:
994 case VIDIOC_S_FMT:
995 {
996 struct v4l2_format *vf = arg;
997 int formatIdx,ret;
998
999 switch(vf->type) {
1000 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1001 {
1002 /* Find requested format in available ones */
1003 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1004 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
1005 usbvision->palette = usbvision_v4l2_format[formatIdx];
1006 break;
1007 }
1008 }
1009 /* robustness */
1010 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1011 return -EINVAL;
1012 }
1013 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1014 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1015
Thierry MERLEc876a342006-12-09 16:42:54 -03001016 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
1017 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1018
1019 if(cmd == VIDIOC_TRY_FMT) {
1020 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1021 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1022 return 0;
1023 }
1024
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001025 /* stop io in case it is already in progress */
1026 if(usbvision->streaming == Stream_On) {
1027 if ((ret = usbvision_stream_interrupt(usbvision)))
1028 return ret;
1029 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001030 usbvision_frames_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001031 usbvision_empty_framequeues(usbvision);
1032
1033 usbvision->curFrame = NULL;
1034
1035 // by now we are committed to the new data...
1036 down(&usbvision->lock);
1037 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1038 up(&usbvision->lock);
1039
1040 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1041 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1042 return 0;
1043 }
1044 default:
1045 return -EINVAL;
1046 }
1047 }
1048 default:
1049 return -ENOIOCTLCMD;
1050 }
1051 return 0;
1052}
1053
1054static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1055 unsigned int cmd, unsigned long arg)
1056{
1057 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1058}
1059
1060
Al Viro8a5ab412007-02-09 16:38:20 +00001061static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001062 size_t count, loff_t *ppos)
1063{
1064 struct video_device *dev = video_devdata(file);
1065 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1066 int noblock = file->f_flags & O_NONBLOCK;
1067 unsigned long lock_flags;
1068
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001069 int ret,i;
1070 struct usbvision_frame *frame;
1071
1072 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1073
1074 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1075 return -EFAULT;
1076
Thierry MERLE6f78e182007-02-07 10:13:11 -03001077 /* This entry point is compatible with the mmap routines so that a user can do either
1078 VIDIOC_QBUF/VIDIOC_DQBUF to get frames or call read on the device. */
1079 if(!usbvision->num_frames) {
1080 /* First, allocate some frames to work with if this has not been done with
1081 VIDIOC_REQBUF */
1082 usbvision_frames_free(usbvision);
1083 usbvision_empty_framequeues(usbvision);
1084 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1085 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001086
Thierry MERLE6f78e182007-02-07 10:13:11 -03001087 if(usbvision->streaming != Stream_On) {
1088 /* no stream is running, make it running ! */
1089 usbvision->streaming = Stream_On;
1090 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1091 }
1092
1093 /* Then, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1094 for(i=0;i<usbvision->num_frames;i++) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001095 frame = &usbvision->frame[i];
1096 if(frame->grabstate == FrameState_Unused) {
1097 /* Mark it as ready and enqueue frame */
1098 frame->grabstate = FrameState_Ready;
1099 frame->scanstate = ScanState_Scanning;
1100 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1101
1102 /* set v4l2_format index */
1103 frame->v4l2_format = usbvision->palette;
1104
1105 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1106 list_add_tail(&frame->frame, &usbvision->inqueue);
1107 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1108 }
1109 }
1110
1111 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1112 if (list_empty(&(usbvision->outqueue))) {
1113 if(noblock)
1114 return -EAGAIN;
1115
1116 ret = wait_event_interruptible
1117 (usbvision->wait_frame,
1118 !list_empty(&(usbvision->outqueue)));
1119 if (ret)
1120 return ret;
1121 }
1122
1123 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1124 frame = list_entry(usbvision->outqueue.next,
1125 struct usbvision_frame, frame);
1126 list_del(usbvision->outqueue.next);
1127 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1128
1129 /* An error returns an empty frame */
1130 if (frame->grabstate == FrameState_Error) {
1131 frame->bytes_read = 0;
1132 return 0;
1133 }
1134
1135 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1136 frame->index, frame->bytes_read, frame->scanlength);
1137
1138 /* copy bytes to user space; we allow for partials reads */
1139 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1140 count = frame->scanlength - frame->bytes_read;
1141
1142 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1143 return -EFAULT;
1144 }
1145
1146 frame->bytes_read += count;
1147 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1148 (unsigned long)count, frame->bytes_read);
1149
1150 // For now, forget the frame if it has not been read in one shot.
1151/* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1152 frame->bytes_read = 0;
1153
1154 /* Mark it as available to be used again. */
Thierry MERLEc430ca12006-12-29 20:32:08 -03001155 frame->grabstate = FrameState_Unused;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001156/* } */
1157
1158 return count;
1159}
1160
1161static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1162{
1163 unsigned long size = vma->vm_end - vma->vm_start,
1164 start = vma->vm_start;
1165 void *pos;
1166 u32 i;
1167
1168 struct video_device *dev = video_devdata(file);
1169 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1170
Thierry MERLE6f78e182007-02-07 10:13:11 -03001171 PDEBUG(DBG_MMAP, "mmap");
1172
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001173 down(&usbvision->lock);
1174
1175 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1176 up(&usbvision->lock);
1177 return -EFAULT;
1178 }
1179
1180 if (!(vma->vm_flags & VM_WRITE) ||
Thierry MERLE6f78e182007-02-07 10:13:11 -03001181 size != PAGE_ALIGN(usbvision->max_frame_size)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001182 up(&usbvision->lock);
1183 return -EINVAL;
1184 }
1185
Thierry MERLE6f78e182007-02-07 10:13:11 -03001186 for (i = 0; i < usbvision->num_frames; i++) {
1187 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) == vma->vm_pgoff)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001188 break;
1189 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001190 if (i == usbvision->num_frames) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001191 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001192 up(&usbvision->lock);
1193 return -EINVAL;
1194 }
1195
1196 /* VM_IO is eventually going to replace PageReserved altogether */
1197 vma->vm_flags |= VM_IO;
1198 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1199
1200 pos = usbvision->frame[i].data;
1201 while (size > 0) {
1202
1203 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001204 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001205 up(&usbvision->lock);
1206 return -EAGAIN;
1207 }
1208 start += PAGE_SIZE;
1209 pos += PAGE_SIZE;
1210 size -= PAGE_SIZE;
1211 }
1212
1213 up(&usbvision->lock);
1214 return 0;
1215}
1216
1217
1218/*
1219 * Here comes the stuff for radio on usbvision based devices
1220 *
1221 */
1222static int usbvision_radio_open(struct inode *inode, struct file *file)
1223{
1224 struct video_device *dev = video_devdata(file);
1225 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1226 struct v4l2_frequency freq;
1227 int errCode = 0;
1228
1229 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1230
1231 down(&usbvision->lock);
1232
1233 if (usbvision->user) {
1234 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1235 errCode = -EBUSY;
1236 }
1237 else {
1238 if(PowerOnAtOpen) {
1239 usbvision_reset_powerOffTimer(usbvision);
1240 if (usbvision->power == 0) {
1241 usbvision_power_on(usbvision);
1242 usbvision_init_i2c(usbvision);
1243 }
1244 }
1245
1246 // If so far no errors then we shall start the radio
1247 usbvision->radio = 1;
1248 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1249 freq.frequency = 1517; //SWR3 @ 94.8MHz
1250 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1251 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1252 usbvision->user++;
1253 }
1254
1255 if (errCode) {
1256 if (PowerOnAtOpen) {
1257 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1258 usbvision_power_off(usbvision);
1259 usbvision->initialized = 0;
1260 }
1261 }
1262 up(&usbvision->lock);
1263 return errCode;
1264}
1265
1266
1267static int usbvision_radio_close(struct inode *inode, struct file *file)
1268{
1269 struct video_device *dev = video_devdata(file);
1270 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1271 int errCode = 0;
1272
1273 PDEBUG(DBG_IO, "");
1274
1275 down(&usbvision->lock);
1276
1277 usbvision_audio_off(usbvision);
1278 usbvision->radio=0;
1279 usbvision->user--;
1280
1281 if (PowerOnAtOpen) {
1282 usbvision_set_powerOffTimer(usbvision);
1283 usbvision->initialized = 0;
1284 }
1285
1286 up(&usbvision->lock);
1287
1288 if (usbvision->remove_pending) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001289 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001290 usbvision_release(usbvision);
1291 }
1292
1293
1294 PDEBUG(DBG_IO, "success");
1295
1296 return errCode;
1297}
1298
1299static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1300 unsigned int cmd, void *arg)
1301{
1302 struct video_device *dev = video_devdata(file);
1303 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1304
1305 if (!USBVISION_IS_OPERATIONAL(usbvision))
1306 return -EIO;
1307
1308 switch (cmd) {
1309 case VIDIOC_QUERYCAP:
1310 {
1311 struct v4l2_capability *vc=arg;
1312
1313 memset(vc, 0, sizeof(*vc));
1314 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1315 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1316 sizeof(vc->card));
1317 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1318 sizeof(vc->bus_info));
1319 vc->version = USBVISION_DRIVER_VERSION;
1320 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1321 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1322 return 0;
1323 }
1324 case VIDIOC_QUERYCTRL:
1325 {
1326 struct v4l2_queryctrl *ctrl = arg;
1327 int id=ctrl->id;
1328
1329 memset(ctrl,0,sizeof(*ctrl));
1330 ctrl->id=id;
1331
1332 call_i2c_clients(usbvision, cmd, arg);
1333 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1334
1335 if (ctrl->type)
1336 return 0;
1337 else
1338 return -EINVAL;
1339
1340 }
1341 case VIDIOC_G_CTRL:
1342 {
1343 struct v4l2_control *ctrl = arg;
1344
1345 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1346 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1347 return 0;
1348 }
1349 case VIDIOC_S_CTRL:
1350 {
1351 struct v4l2_control *ctrl = arg;
1352
1353 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1354 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1355 return 0;
1356 }
1357 case VIDIOC_G_TUNER:
1358 {
1359 struct v4l2_tuner *t = arg;
1360
1361 if (t->index > 0)
1362 return -EINVAL;
1363
1364 memset(t,0,sizeof(*t));
1365 strcpy(t->name, "Radio");
1366 t->type = V4L2_TUNER_RADIO;
1367
1368 /* Let clients fill in the remainder of this struct */
1369 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1370 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1371 return 0;
1372 }
1373 case VIDIOC_S_TUNER:
1374 {
1375 struct v4l2_tuner *vt = arg;
1376
1377 // Only no or one tuner for now
1378 if (!usbvision->have_tuner || vt->index)
1379 return -EINVAL;
1380 /* let clients handle this */
1381 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1382
1383 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1384 return 0;
1385 }
1386 case VIDIOC_G_AUDIO:
1387 {
1388 struct v4l2_audio *a = arg;
1389
1390 memset(a,0,sizeof(*a));
1391 strcpy(a->name,"Radio");
1392 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1393 return 0;
1394 }
1395 case VIDIOC_S_AUDIO:
1396 case VIDIOC_S_INPUT:
1397 case VIDIOC_S_STD:
1398 return 0;
1399
1400 case VIDIOC_G_FREQUENCY:
1401 {
1402 struct v4l2_frequency *f = arg;
1403
1404 memset(f,0,sizeof(*f));
1405
1406 f->type = V4L2_TUNER_RADIO;
1407 f->frequency = usbvision->freq;
1408 call_i2c_clients(usbvision, cmd, f);
1409 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1410
1411 return 0;
1412 }
1413 case VIDIOC_S_FREQUENCY:
1414 {
1415 struct v4l2_frequency *f = arg;
1416
1417 if (f->tuner != 0)
1418 return -EINVAL;
1419 usbvision->freq = f->frequency;
1420 call_i2c_clients(usbvision, cmd, f);
1421 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1422
1423 return 0;
1424 }
1425 default:
1426 {
1427 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1428 return -ENOIOCTLCMD;
1429 }
1430 }
1431 return 0;
1432}
1433
1434
1435static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1436 unsigned int cmd, unsigned long arg)
1437{
1438 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1439}
1440
1441
1442/*
1443 * Here comes the stuff for vbi on usbvision based devices
1444 *
1445 */
1446static int usbvision_vbi_open(struct inode *inode, struct file *file)
1447{
1448 /* TODO */
1449 return -EINVAL;
1450
1451}
1452
1453static int usbvision_vbi_close(struct inode *inode, struct file *file)
1454{
1455 /* TODO */
1456 return -EINVAL;
1457}
1458
1459static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1460 unsigned int cmd, void *arg)
1461{
1462 /* TODO */
1463 return -EINVAL;
1464}
1465
1466static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1467 unsigned int cmd, unsigned long arg)
1468{
1469 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1470}
1471
1472
1473//
1474// Video registration stuff
1475//
1476
1477// Video template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001478static const struct file_operations usbvision_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001479 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001480 .open = usbvision_v4l2_open,
1481 .release = usbvision_v4l2_close,
1482 .read = usbvision_v4l2_read,
1483 .mmap = usbvision_v4l2_mmap,
1484 .ioctl = usbvision_v4l2_ioctl,
1485 .llseek = no_llseek,
1486};
1487static struct video_device usbvision_video_template = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001488 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001489 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1490 .hardware = VID_HARDWARE_USBVISION,
1491 .fops = &usbvision_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001492 .name = "usbvision-video",
1493 .release = video_device_release,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001494 .minor = -1,
1495};
1496
1497
1498// Radio template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001499static const struct file_operations usbvision_radio_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001500 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001501 .open = usbvision_radio_open,
1502 .release = usbvision_radio_close,
1503 .ioctl = usbvision_radio_ioctl,
1504 .llseek = no_llseek,
1505};
1506
1507static struct video_device usbvision_radio_template=
1508{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001509 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001510 .type = VID_TYPE_TUNER,
1511 .hardware = VID_HARDWARE_USBVISION,
1512 .fops = &usbvision_radio_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001513 .release = video_device_release,
1514 .name = "usbvision-radio",
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001515 .minor = -1,
1516};
1517
1518
1519// vbi template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001520static const struct file_operations usbvision_vbi_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001521 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001522 .open = usbvision_vbi_open,
1523 .release = usbvision_vbi_close,
1524 .ioctl = usbvision_vbi_ioctl,
1525 .llseek = no_llseek,
1526};
1527
1528static struct video_device usbvision_vbi_template=
1529{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001530 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001531 .type = VID_TYPE_TUNER,
1532 .hardware = VID_HARDWARE_USBVISION,
1533 .fops = &usbvision_vbi_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001534 .release = video_device_release,
1535 .name = "usbvision-vbi",
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001536 .minor = -1,
1537};
1538
1539
1540static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1541 struct video_device *vdev_template,
1542 char *name)
1543{
1544 struct usb_device *usb_dev = usbvision->dev;
1545 struct video_device *vdev;
1546
1547 if (usb_dev == NULL) {
1548 err("%s: usbvision->dev is not set", __FUNCTION__);
1549 return NULL;
1550 }
1551
1552 vdev = video_device_alloc();
1553 if (NULL == vdev) {
1554 return NULL;
1555 }
1556 *vdev = *vdev_template;
1557// vdev->minor = -1;
1558 vdev->dev = &usb_dev->dev;
1559 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1560 video_set_drvdata(vdev, usbvision);
1561 return vdev;
1562}
1563
1564// unregister video4linux devices
1565static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1566{
1567 // vbi Device:
1568 if (usbvision->vbi) {
1569 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1570 if (usbvision->vbi->minor != -1) {
1571 video_unregister_device(usbvision->vbi);
1572 }
1573 else {
1574 video_device_release(usbvision->vbi);
1575 }
1576 usbvision->vbi = NULL;
1577 }
1578
1579 // Radio Device:
1580 if (usbvision->rdev) {
1581 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1582 if (usbvision->rdev->minor != -1) {
1583 video_unregister_device(usbvision->rdev);
1584 }
1585 else {
1586 video_device_release(usbvision->rdev);
1587 }
1588 usbvision->rdev = NULL;
1589 }
1590
1591 // Video Device:
1592 if (usbvision->vdev) {
1593 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1594 if (usbvision->vdev->minor != -1) {
1595 video_unregister_device(usbvision->vdev);
1596 }
1597 else {
1598 video_device_release(usbvision->vdev);
1599 }
1600 usbvision->vdev = NULL;
1601 }
1602}
1603
1604// register video4linux devices
1605static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1606{
1607 // Video Device:
1608 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1609 if (usbvision->vdev == NULL) {
1610 goto err_exit;
1611 }
1612 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1613 goto err_exit;
1614 }
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001615 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision->nr,usbvision->vdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001616
1617 // Radio Device:
1618 if (usbvision_device_data[usbvision->DevModel].Radio) {
1619 // usbvision has radio
1620 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1621 if (usbvision->rdev == NULL) {
1622 goto err_exit;
1623 }
1624 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1625 goto err_exit;
1626 }
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001627 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision->nr, usbvision->rdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001628 }
1629 // vbi Device:
1630 if (usbvision_device_data[usbvision->DevModel].vbi) {
1631 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1632 if (usbvision->vdev == NULL) {
1633 goto err_exit;
1634 }
1635 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1636 goto err_exit;
1637 }
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001638 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision->nr,usbvision->vbi->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001639 }
1640 // all done
1641 return 0;
1642
1643 err_exit:
1644 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1645 usbvision_unregister_video(usbvision);
1646 return -1;
1647}
1648
1649/*
1650 * usbvision_alloc()
1651 *
1652 * This code allocates the struct usb_usbvision. It is filled with default values.
1653 *
1654 * Returns NULL on error, a pointer to usb_usbvision else.
1655 *
1656 */
1657static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1658{
1659 struct usb_usbvision *usbvision;
1660
1661 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1662 goto err_exit;
1663 }
1664
1665 usbvision->dev = dev;
1666
1667 init_MUTEX(&usbvision->lock); /* to 1 == available */
1668
1669 // prepare control urb for control messages during interrupts
1670 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1671 if (usbvision->ctrlUrb == NULL) {
1672 goto err_exit;
1673 }
1674 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1675 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1676
1677 usbvision_init_powerOffTimer(usbvision);
1678
1679 return usbvision;
1680
1681err_exit:
1682 if (usbvision && usbvision->ctrlUrb) {
1683 usb_free_urb(usbvision->ctrlUrb);
1684 }
1685 if (usbvision) {
1686 kfree(usbvision);
1687 }
1688 return NULL;
1689}
1690
1691/*
1692 * usbvision_release()
1693 *
1694 * This code does final release of struct usb_usbvision. This happens
1695 * after the device is disconnected -and- all clients closed their files.
1696 *
1697 */
1698static void usbvision_release(struct usb_usbvision *usbvision)
1699{
1700 PDEBUG(DBG_PROBE, "");
1701
1702 down(&usbvision->lock);
1703
1704 usbvision_reset_powerOffTimer(usbvision);
1705
1706 usbvision->initialized = 0;
1707
1708 up(&usbvision->lock);
1709
1710 usbvision_remove_sysfs(usbvision->vdev);
1711 usbvision_unregister_video(usbvision);
1712
1713 if (usbvision->ctrlUrb) {
1714 usb_free_urb(usbvision->ctrlUrb);
1715 }
1716
1717 kfree(usbvision);
1718
1719 PDEBUG(DBG_PROBE, "success");
1720}
1721
1722
1723/******************************** usb interface *****************************************/
1724
1725static void usbvision_configure_video(struct usb_usbvision *usbvision)
1726{
1727 int model,i;
1728
1729 if (usbvision == NULL)
1730 return;
1731
1732 model = usbvision->DevModel;
1733 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1734
1735 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1736 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1737 } else {
1738 usbvision->Vin_Reg2_Preset = 0;
1739 }
1740
1741 for (i = 0; i < TVNORMS; i++)
1742 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1743 break;
1744 if (i == TVNORMS)
1745 i = 0;
1746 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1747
1748 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1749 usbvision->ctl_input = 0;
1750
1751 /* This should be here to make i2c clients to be able to register */
1752 usbvision_audio_off(usbvision); //first switch off audio
1753 if (!PowerOnAtOpen) {
1754 usbvision_power_on(usbvision); //and then power up the noisy tuner
1755 usbvision_init_i2c(usbvision);
1756 }
1757}
1758
1759/*
1760 * usbvision_probe()
1761 *
1762 * This procedure queries device descriptor and accepts the interface
1763 * if it looks like USBVISION video device
1764 *
1765 */
1766static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1767{
1768 struct usb_device *dev = interface_to_usbdev(intf);
1769 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1770 const struct usb_host_interface *interface;
1771 struct usb_usbvision *usbvision = NULL;
1772 const struct usb_endpoint_descriptor *endpoint;
1773 int model;
1774
1775 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1776 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1777 /* Is it an USBVISION video dev? */
1778 model = 0;
1779 for(model = 0; usbvision_device_data[model].idVendor; model++) {
1780 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1781 continue;
1782 }
1783 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1784 continue;
1785 }
1786
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001787 printk(KERN_INFO "%s: %s found\n", __FUNCTION__, usbvision_device_data[model].ModelString);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001788 break;
1789 }
1790
1791 if (usbvision_device_data[model].idVendor == 0) {
1792 return -ENODEV; //no matching device
1793 }
1794 if (usbvision_device_data[model].Interface >= 0) {
1795 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1796 }
1797 else {
1798 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1799 }
1800 endpoint = &interface->endpoint[1].desc;
1801 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1802 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1803 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1804 return -ENODEV;
1805 }
1806 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1807 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1808 return -ENODEV;
1809 }
1810
1811 usb_get_dev(dev);
1812
1813 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1814 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1815 return -ENOMEM;
1816 }
1817 if (dev->descriptor.bNumConfigurations > 1) {
1818 usbvision->bridgeType = BRIDGE_NT1004;
1819 }
1820 else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1821 usbvision->bridgeType = BRIDGE_NT1005;
1822 }
1823 else {
1824 usbvision->bridgeType = BRIDGE_NT1003;
1825 }
1826 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1827
1828 down(&usbvision->lock);
1829
1830 usbvision->nr = usbvision_nr++;
1831
1832 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1833 if (usbvision->have_tuner) {
1834 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1835 }
1836
1837 usbvision->tuner_addr = ADDR_UNSET;
1838
1839 usbvision->DevModel = model;
1840 usbvision->remove_pending = 0;
1841 usbvision->iface = ifnum;
1842 usbvision->ifaceAltInactive = 0;
1843 usbvision->ifaceAltActive = 1;
1844 usbvision->video_endp = endpoint->bEndpointAddress;
1845 usbvision->isocPacketSize = 0;
1846 usbvision->usb_bandwidth = 0;
1847 usbvision->user = 0;
1848 usbvision->streaming = Stream_Off;
1849 usbvision_register_video(usbvision);
1850 usbvision_configure_video(usbvision);
1851 up(&usbvision->lock);
1852
1853
1854 usb_set_intfdata (intf, usbvision);
1855 usbvision_create_sysfs(usbvision->vdev);
1856
1857 PDEBUG(DBG_PROBE, "success");
1858 return 0;
1859}
1860
1861
1862/*
1863 * usbvision_disconnect()
1864 *
1865 * This procedure stops all driver activity, deallocates interface-private
1866 * structure (pointed by 'ptr') and after that driver should be removable
1867 * with no ill consequences.
1868 *
1869 */
1870static void __devexit usbvision_disconnect(struct usb_interface *intf)
1871{
1872 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1873
1874 PDEBUG(DBG_PROBE, "");
1875
1876 if (usbvision == NULL) {
1877 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1878 return;
1879 }
1880 usb_set_intfdata (intf, NULL);
1881
1882 down(&usbvision->lock);
1883
1884 // At this time we ask to cancel outstanding URBs
1885 usbvision_stop_isoc(usbvision);
1886
1887 if (usbvision->power) {
1888 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1889 usbvision_power_off(usbvision);
1890 }
1891 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1892
1893 usb_put_dev(usbvision->dev);
1894 usbvision->dev = NULL; // USB device is no more
1895
1896 up(&usbvision->lock);
1897
1898 if (usbvision->user) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001899 printk(KERN_INFO "%s: In use, disconnect pending\n", __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001900 wake_up_interruptible(&usbvision->wait_frame);
1901 wake_up_interruptible(&usbvision->wait_stream);
1902 }
1903 else {
1904 usbvision_release(usbvision);
1905 }
1906
1907 PDEBUG(DBG_PROBE, "success");
1908
1909}
1910
1911static struct usb_driver usbvision_driver = {
1912 .name = "usbvision",
1913 .id_table = usbvision_table,
1914 .probe = usbvision_probe,
1915 .disconnect = usbvision_disconnect
1916};
1917
1918/*
1919 * customdevice_process()
1920 *
1921 * This procedure preprocesses CustomDevice parameter if any
1922 *
1923 */
Adrian Bunk0a0cead2006-12-11 15:51:36 -03001924static void customdevice_process(void)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001925{
1926 usbvision_device_data[0]=usbvision_device_data[1];
1927 usbvision_table[0]=usbvision_table[1];
1928
1929 if(CustomDevice)
1930 {
1931 char *parse=CustomDevice;
1932
1933 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1934
1935 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1936 usbvision_device_data[0].idVendor;
1937 usbvision_device_data[0].idProduct;
1938 usbvision_device_data[0].Interface;
1939 usbvision_device_data[0].Codec;
1940 usbvision_device_data[0].VideoChannels;
1941 usbvision_device_data[0].VideoNorm;
1942 usbvision_device_data[0].AudioChannels;
1943 usbvision_device_data[0].Radio;
1944 usbvision_device_data[0].Tuner;
1945 usbvision_device_data[0].TunerType;
1946 usbvision_device_data[0].Vin_Reg1;
1947 usbvision_device_data[0].Vin_Reg2;
1948 usbvision_device_data[0].X_Offset;
1949 usbvision_device_data[0].Y_Offset;
1950 usbvision_device_data[0].Dvi_yuv;
1951 usbvision_device_data[0].ModelString;
1952 */
1953
1954 rmspace(parse);
1955 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1956
1957 parse+=2;
1958 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1959 goto2next(parse);
1960 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1961 parse+=2;
1962 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1963 goto2next(parse);
1964 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1965 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1966 goto2next(parse);
1967 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1968 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1969 goto2next(parse);
1970 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1971 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1972 goto2next(parse);
1973 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1974
1975 switch(*parse)
1976 {
1977 case 'P':
1978 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
Dwaine Gardened00b412006-12-27 10:23:28 -02001979 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001980 break;
1981
1982 case 'S':
1983 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
Dwaine Gardened00b412006-12-27 10:23:28 -02001984 usbvision_device_data[0].VideoNorm=V4L2_STD_SECAM;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001985 break;
1986
1987 case 'N':
1988 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
Dwaine Gardened00b412006-12-27 10:23:28 -02001989 usbvision_device_data[0].VideoNorm=V4L2_STD_NTSC;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001990 break;
1991
1992 default:
1993 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
Dwaine Gardened00b412006-12-27 10:23:28 -02001994 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001995 break;
1996 }
1997 goto2next(parse);
1998
1999 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
2000 goto2next(parse);
2001 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
2002 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
2003 goto2next(parse);
2004 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
2005 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
2006 goto2next(parse);
2007 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
2008 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
2009 goto2next(parse);
2010 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
2011 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
2012 goto2next(parse);
2013 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
2014 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
2015 goto2next(parse);
2016 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
2017 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
2018 goto2next(parse);
2019 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
2020 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
2021 goto2next(parse);
2022 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
2023 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
2024 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
2025
2026 //add to usbvision_table also
2027 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
2028 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
2029 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
2030
2031 }
2032}
2033
2034
2035
2036/*
2037 * usbvision_init()
2038 *
2039 * This code is run to initialize the driver.
2040 *
2041 */
2042static int __init usbvision_init(void)
2043{
2044 int errCode;
2045
2046 PDEBUG(DBG_PROBE, "");
2047
2048 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
2049 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
2050 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
Thierry MERLEc876a342006-12-09 16:42:54 -03002051 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03002052
2053 /* disable planar mode support unless compression enabled */
2054 if (isocMode != ISOC_MODE_COMPRESS ) {
2055 // FIXME : not the right way to set supported flag
2056 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2057 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2058 }
2059
2060 customdevice_process();
2061
2062 errCode = usb_register(&usbvision_driver);
2063
2064 if (errCode == 0) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03002065 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03002066 PDEBUG(DBG_PROBE, "success");
2067 }
2068 return errCode;
2069}
2070
2071static void __exit usbvision_exit(void)
2072{
2073 PDEBUG(DBG_PROBE, "");
2074
2075 usb_deregister(&usbvision_driver);
2076 PDEBUG(DBG_PROBE, "success");
2077}
2078
2079module_init(usbvision_init);
2080module_exit(usbvision_exit);
2081
2082/*
2083 * Overrides for Emacs so that we follow Linus's tabbing style.
2084 * ---------------------------------------------------------------------------
2085 * Local variables:
2086 * c-basic-offset: 8
2087 * End:
2088 */