blob: db6b828594f513ac562485692e918f63798e41dc [file] [log] [blame]
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001/*
Hans Verkuil3ff4ad82009-04-01 03:15:52 -03002 * USB USBVISION Video device driver 0.9.10
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.
Thierry MERLEc5f48362007-05-08 17:22:29 -030039 * - Add audio on endpoint 3 for nt1004 chip.
40 * Seems impossible, needs a codec interface. Which one?
Thierry MERLE483dfdb2006-12-04 08:31:45 -030041 * - Clean up the driver.
42 * - optimization for performance.
43 * - Add Videotext capability (VBI). Working on it.....
44 * - Check audio for other devices
45 *
46 */
47
Mauro Carvalho Chehabf30ebd42006-12-09 12:32:18 -030048#include <linux/version.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030049#include <linux/kernel.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030050#include <linux/list.h>
51#include <linux/timer.h>
52#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040053#include <linux/smp_lock.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030054#include <linux/mm.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030055#include <linux/highmem.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030056#include <linux/vmalloc.h>
57#include <linux/module.h>
58#include <linux/init.h>
59#include <linux/spinlock.h>
60#include <asm/io.h>
61#include <linux/videodev2.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030062#include <linux/i2c.h>
63
64#include <media/saa7115.h>
65#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030066#include <media/v4l2-ioctl.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030067#include <media/tuner.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030068
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -030069#include <linux/workqueue.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030070
Thierry MERLE483dfdb2006-12-04 08:31:45 -030071#include "usbvision.h"
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -030072#include "usbvision-cards.h"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030073
Thierry MERLEc5f48362007-05-08 17:22:29 -030074#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
75 Dwaine Garden <DwaineGarden@rogers.com>"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030076#define DRIVER_NAME "usbvision"
77#define DRIVER_ALIAS "USBVision"
78#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
79#define DRIVER_LICENSE "GPL"
80#define USBVISION_DRIVER_VERSION_MAJOR 0
81#define USBVISION_DRIVER_VERSION_MINOR 9
Hans Verkuil3ff4ad82009-04-01 03:15:52 -030082#define USBVISION_DRIVER_VERSION_PATCHLEVEL 10
Thierry MERLEc5f48362007-05-08 17:22:29 -030083#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
84USBVISION_DRIVER_VERSION_MINOR,\
85USBVISION_DRIVER_VERSION_PATCHLEVEL)
86#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
87 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
88 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
Thierry MERLE483dfdb2006-12-04 08:31:45 -030089
90#define ENABLE_HEXDUMP 0 /* Enable if you need it */
91
92
Thierry MERLE483dfdb2006-12-04 08:31:45 -030093#ifdef USBVISION_DEBUG
Thierry MERLEdf18c312008-04-04 21:00:57 -030094 #define PDEBUG(level, fmt, args...) { \
Thierry MERLEc5f48362007-05-08 17:22:29 -030095 if (video_debug & (level)) \
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -030096 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
97 __func__, __LINE__ , ## args); \
Thierry MERLEdf18c312008-04-04 21:00:57 -030098 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -030099#else
100 #define PDEBUG(level, fmt, args...) do {} while(0)
101#endif
102
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300103#define DBG_IO 1<<1
104#define DBG_PROBE 1<<2
Thierry MERLEc876a342006-12-09 16:42:54 -0300105#define DBG_MMAP 1<<3
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300106
107//String operations
108#define rmspace(str) while(*str==' ') str++;
109#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
110
111
Thierry MERLEc5f48362007-05-08 17:22:29 -0300112/* sequential number of usbvision device */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300113static int usbvision_nr;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300114
115static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
116 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
117 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
118 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
119 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
120 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
121 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
122 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
123 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
124};
125
Thierry MERLEc5f48362007-05-08 17:22:29 -0300126/* Function prototypes */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300127static void usbvision_release(struct usb_usbvision *usbvision);
128
Joe Perchesc84e6032008-02-03 17:18:59 +0200129/* Default initialization of device driver parameters */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300130/* Set the default format for ISOC endpoint */
131static int isocMode = ISOC_MODE_COMPRESS;
132/* Set the default Debug Mode of the device driver */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300133static int video_debug;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300134/* Set the default device to power on at startup */
135static int PowerOnAtOpen = 1;
136/* Sequential Number of Video Device */
137static int video_nr = -1;
138/* Sequential Number of Radio Device */
139static int radio_nr = -1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300140
Thierry MERLEc5f48362007-05-08 17:22:29 -0300141/* Grab parameters for the device driver */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300142
Thierry MERLEc5f48362007-05-08 17:22:29 -0300143/* Showing parameters under SYSFS */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300144module_param(isocMode, int, 0444);
145module_param(video_debug, int, 0444);
146module_param(PowerOnAtOpen, int, 0444);
147module_param(video_nr, int, 0444);
148module_param(radio_nr, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300149
150MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
151MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
152MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
153MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
154MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300155
156
157// Misc stuff
158MODULE_AUTHOR(DRIVER_AUTHOR);
159MODULE_DESCRIPTION(DRIVER_DESC);
160MODULE_LICENSE(DRIVER_LICENSE);
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -0300161MODULE_VERSION(USBVISION_VERSION_STRING);
162MODULE_ALIAS(DRIVER_ALIAS);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300163
164
Thierry MERLEc5f48362007-05-08 17:22:29 -0300165/*****************************************************************************/
166/* SYSFS Code - Copied from the stv680.c usb module. */
167/* Device information is located at /sys/class/video4linux/video0 */
168/* Device parameters information is located at /sys/module/usbvision */
169/* Device USB Information is located at */
170/* /sys/bus/usb/drivers/USBVision Video Grabber */
171/*****************************************************************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300172
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300173#define YES_NO(x) ((x) ? "Yes" : "No")
174
Kay Sievers54bd5b62007-10-08 16:26:13 -0300175static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300176{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300177 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300178 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300179 return video_get_drvdata(vdev);
180}
181
Kay Sievers54bd5b62007-10-08 16:26:13 -0300182static ssize_t show_version(struct device *cd,
183 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300184{
185 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
186}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300187static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300188
Kay Sievers54bd5b62007-10-08 16:26:13 -0300189static ssize_t show_model(struct device *cd,
190 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300191{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300192 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300193 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300194 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300195 return sprintf(buf, "%s\n",
196 usbvision_device_data[usbvision->DevModel].ModelString);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300197}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300198static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300199
Kay Sievers54bd5b62007-10-08 16:26:13 -0300200static ssize_t show_hue(struct device *cd,
201 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300202{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300203 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300204 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300205 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
206 struct v4l2_control ctrl;
207 ctrl.id = V4L2_CID_HUE;
208 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200209 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300210 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300211 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300212}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300213static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300214
Kay Sievers54bd5b62007-10-08 16:26:13 -0300215static ssize_t show_contrast(struct device *cd,
216 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300217{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300218 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300219 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300220 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
221 struct v4l2_control ctrl;
222 ctrl.id = V4L2_CID_CONTRAST;
223 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200224 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300225 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300226 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300227}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300228static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300229
Kay Sievers54bd5b62007-10-08 16:26:13 -0300230static ssize_t show_brightness(struct device *cd,
231 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300232{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300233 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300234 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300235 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
236 struct v4l2_control ctrl;
237 ctrl.id = V4L2_CID_BRIGHTNESS;
238 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200239 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300240 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300241 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300242}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300243static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300244
Kay Sievers54bd5b62007-10-08 16:26:13 -0300245static ssize_t show_saturation(struct device *cd,
246 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300247{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300248 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300249 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300250 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
251 struct v4l2_control ctrl;
252 ctrl.id = V4L2_CID_SATURATION;
253 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200254 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300255 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300256 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300257}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300258static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300259
Kay Sievers54bd5b62007-10-08 16:26:13 -0300260static ssize_t show_streaming(struct device *cd,
261 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300262{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300263 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300264 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300265 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300266 return sprintf(buf, "%s\n",
267 YES_NO(usbvision->streaming==Stream_On?1:0));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300268}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300269static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300270
Kay Sievers54bd5b62007-10-08 16:26:13 -0300271static ssize_t show_compression(struct device *cd,
272 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300273{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300274 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300275 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300276 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300277 return sprintf(buf, "%s\n",
278 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300279}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300280static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300281
Kay Sievers54bd5b62007-10-08 16:26:13 -0300282static ssize_t show_device_bridge(struct device *cd,
283 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300284{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300285 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300286 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300287 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
288 return sprintf(buf, "%d\n", usbvision->bridgeType);
289}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300290static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300291
292static void usbvision_create_sysfs(struct video_device *vdev)
293{
294 int res;
Dwaine Gardened00b412006-12-27 10:23:28 -0200295 if (!vdev)
296 return;
297 do {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300298 res = device_create_file(&vdev->dev, &dev_attr_version);
Dwaine Gardened00b412006-12-27 10:23:28 -0200299 if (res<0)
300 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300301 res = device_create_file(&vdev->dev, &dev_attr_model);
Dwaine Gardened00b412006-12-27 10:23:28 -0200302 if (res<0)
303 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300304 res = device_create_file(&vdev->dev, &dev_attr_hue);
Dwaine Gardened00b412006-12-27 10:23:28 -0200305 if (res<0)
306 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300307 res = device_create_file(&vdev->dev, &dev_attr_contrast);
Dwaine Gardened00b412006-12-27 10:23:28 -0200308 if (res<0)
309 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300310 res = device_create_file(&vdev->dev, &dev_attr_brightness);
Dwaine Gardened00b412006-12-27 10:23:28 -0200311 if (res<0)
312 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300313 res = device_create_file(&vdev->dev, &dev_attr_saturation);
Dwaine Gardened00b412006-12-27 10:23:28 -0200314 if (res<0)
315 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300316 res = device_create_file(&vdev->dev, &dev_attr_streaming);
Dwaine Gardened00b412006-12-27 10:23:28 -0200317 if (res<0)
318 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300319 res = device_create_file(&vdev->dev, &dev_attr_compression);
Dwaine Gardened00b412006-12-27 10:23:28 -0200320 if (res<0)
321 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300322 res = device_create_file(&vdev->dev, &dev_attr_bridge);
Dwaine Gardened00b412006-12-27 10:23:28 -0200323 if (res>=0)
324 return;
325 } while (0);
326
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300327 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300328}
329
330static void usbvision_remove_sysfs(struct video_device *vdev)
331{
332 if (vdev) {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300333 device_remove_file(&vdev->dev, &dev_attr_version);
334 device_remove_file(&vdev->dev, &dev_attr_model);
335 device_remove_file(&vdev->dev, &dev_attr_hue);
336 device_remove_file(&vdev->dev, &dev_attr_contrast);
337 device_remove_file(&vdev->dev, &dev_attr_brightness);
338 device_remove_file(&vdev->dev, &dev_attr_saturation);
339 device_remove_file(&vdev->dev, &dev_attr_streaming);
340 device_remove_file(&vdev->dev, &dev_attr_compression);
341 device_remove_file(&vdev->dev, &dev_attr_bridge);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300342 }
343}
344
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300345/*
346 * usbvision_open()
347 *
348 * This is part of Video 4 Linux API. The driver can be opened by one
349 * client only (checks internal counter 'usbvision->user'). The procedure
350 * then allocates buffers needed for video processing.
351 *
352 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300353static int usbvision_v4l2_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300354{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300355 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300356 int errCode = 0;
357
358 PDEBUG(DBG_IO, "open");
359
Hans Verkuil84034722010-09-17 15:07:28 -0300360 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300361 usbvision_reset_powerOffTimer(usbvision);
362
363 if (usbvision->user)
364 errCode = -EBUSY;
365 else {
Thierry MERLE6f78e182007-02-07 10:13:11 -0300366 /* Allocate memory for the scratch ring buffer */
367 errCode = usbvision_scratch_alloc(usbvision);
368 if (isocMode==ISOC_MODE_COMPRESS) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300369 /* Allocate intermediate decompression buffers
370 only if needed */
Thierry MERLE6f78e182007-02-07 10:13:11 -0300371 errCode = usbvision_decompress_alloc(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300372 }
373 if (errCode) {
374 /* Deallocate all buffers if trouble */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300375 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300376 usbvision_decompress_free(usbvision);
377 }
378 }
379
380 /* If so far no errors then we shall start the camera */
381 if (!errCode) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300382 if (usbvision->power == 0) {
383 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300384 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300385 }
386
387 /* Send init sequence only once, it's large! */
388 if (!usbvision->initialized) {
389 int setup_ok = 0;
390 setup_ok = usbvision_setup(usbvision,isocMode);
391 if (setup_ok)
392 usbvision->initialized = 1;
393 else
394 errCode = -EBUSY;
395 }
396
397 if (!errCode) {
398 usbvision_begin_streaming(usbvision);
399 errCode = usbvision_init_isoc(usbvision);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300400 /* device must be initialized before isoc transfer */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300401 usbvision_muxsel(usbvision,0);
402 usbvision->user++;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300403 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300404 if (PowerOnAtOpen) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300405 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300406 usbvision_power_off(usbvision);
407 usbvision->initialized = 0;
408 }
409 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300410 }
411
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300412 /* prepare queues */
413 usbvision_empty_framequeues(usbvision);
414
415 PDEBUG(DBG_IO, "success");
Hans Verkuil84034722010-09-17 15:07:28 -0300416 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300417 return errCode;
418}
419
420/*
421 * usbvision_v4l2_close()
422 *
423 * This is part of Video 4 Linux API. The procedure
424 * stops streaming and deallocates all buffers that were earlier
425 * allocated in usbvision_v4l2_open().
426 *
427 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300428static int usbvision_v4l2_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300429{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300430 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300431
432 PDEBUG(DBG_IO, "close");
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300433 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300434
435 usbvision_audio_off(usbvision);
436 usbvision_restart_isoc(usbvision);
437 usbvision_stop_isoc(usbvision);
438
439 usbvision_decompress_free(usbvision);
Thierry MERLE38284ba2006-12-15 16:46:53 -0300440 usbvision_frames_free(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300441 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300442 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300443
444 usbvision->user--;
445
446 if (PowerOnAtOpen) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300447 /* power off in a little while
448 to avoid off/on every close/open short sequences */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300449 usbvision_set_powerOffTimer(usbvision);
450 usbvision->initialized = 0;
451 }
452
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300453 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300454
455 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300456 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300457 usbvision_release(usbvision);
458 }
459
460 PDEBUG(DBG_IO, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300461 return 0;
462}
463
464
465/*
466 * usbvision_ioctl()
467 *
468 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
469 *
470 */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300471#ifdef CONFIG_VIDEO_ADV_DEBUG
472static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300473 struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300474{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300475 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300476 int errCode;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300477
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300478 if (!v4l2_chip_match_host(&reg->match))
Thierry MERLEc5f48362007-05-08 17:22:29 -0300479 return -EINVAL;
480 /* NT100x has a 8-bit register space */
481 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
482 if (errCode < 0) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300483 dev_err(&usbvision->vdev->dev,
484 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
485 __func__, errCode);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300486 return errCode;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300487 }
Trent Piephobc147132007-07-23 06:58:31 -0300488 reg->val = errCode;
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300489 reg->size = 1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300490 return 0;
491}
492
Thierry MERLEc5f48362007-05-08 17:22:29 -0300493static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300494 struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300495{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300496 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300497 int errCode;
498
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300499 if (!v4l2_chip_match_host(&reg->match))
Thierry MERLEc5f48362007-05-08 17:22:29 -0300500 return -EINVAL;
501 /* NT100x has a 8-bit register space */
Trent Piephobc147132007-07-23 06:58:31 -0300502 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
503 if (errCode < 0) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300504 dev_err(&usbvision->vdev->dev,
505 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
506 __func__, errCode);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300507 return errCode;
508 }
509 return 0;
510}
511#endif
512
513static int vidioc_querycap (struct file *file, void *priv,
514 struct v4l2_capability *vc)
515{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300516 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300517
518 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
519 strlcpy(vc->card,
520 usbvision_device_data[usbvision->DevModel].ModelString,
521 sizeof(vc->card));
Thierry MERLE6f2a2782009-01-20 17:40:44 -0300522 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
Thierry MERLEc5f48362007-05-08 17:22:29 -0300523 vc->version = USBVISION_DRIVER_VERSION;
524 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
525 V4L2_CAP_AUDIO |
526 V4L2_CAP_READWRITE |
527 V4L2_CAP_STREAMING |
528 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
529 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300530}
531
Thierry MERLEc5f48362007-05-08 17:22:29 -0300532static int vidioc_enum_input (struct file *file, void *priv,
533 struct v4l2_input *vi)
534{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300535 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300536 int chan;
537
Roel Kluin223ffe52009-05-02 16:38:47 -0300538 if (vi->index >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300539 return -EINVAL;
540 if (usbvision->have_tuner) {
541 chan = vi->index;
542 } else {
543 chan = vi->index + 1; /*skip Television string*/
544 }
545 /* Determine the requested input characteristics
546 specific for each usbvision card model */
547 switch(chan) {
548 case 0:
549 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
550 strcpy(vi->name, "White Video Input");
551 } else {
552 strcpy(vi->name, "Television");
553 vi->type = V4L2_INPUT_TYPE_TUNER;
554 vi->audioset = 1;
555 vi->tuner = chan;
556 vi->std = USBVISION_NORMS;
557 }
558 break;
559 case 1:
560 vi->type = V4L2_INPUT_TYPE_CAMERA;
561 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
562 strcpy(vi->name, "Green Video Input");
563 } else {
564 strcpy(vi->name, "Composite Video Input");
565 }
566 vi->std = V4L2_STD_PAL;
567 break;
568 case 2:
569 vi->type = V4L2_INPUT_TYPE_CAMERA;
570 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
571 strcpy(vi->name, "Yellow Video Input");
572 } else {
573 strcpy(vi->name, "S-Video Input");
574 }
575 vi->std = V4L2_STD_PAL;
576 break;
577 case 3:
578 vi->type = V4L2_INPUT_TYPE_CAMERA;
579 strcpy(vi->name, "Red Video Input");
580 vi->std = V4L2_STD_PAL;
581 break;
582 }
583 return 0;
584}
585
586static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
587{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300588 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300589
590 *input = usbvision->ctl_input;
591 return 0;
592}
593
594static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
595{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300596 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300597
Roel Kluinf14a2972009-10-23 07:59:42 -0300598 if (input >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300599 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300600
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300601 mutex_lock(&usbvision->lock);
Thierry MERLE66a17872007-06-26 16:35:30 -0300602 usbvision_muxsel(usbvision, input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300603 usbvision_set_input(usbvision);
604 usbvision_set_output(usbvision,
605 usbvision->curwidth,
606 usbvision->curheight);
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300607 mutex_unlock(&usbvision->lock);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300608 return 0;
609}
610
611static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
612{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300613 struct usb_usbvision *usbvision = video_drvdata(file);
614
Thierry MERLEc5f48362007-05-08 17:22:29 -0300615 usbvision->tvnormId=*id;
616
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300617 mutex_lock(&usbvision->lock);
Hans Verkuilf41737e2009-04-01 03:52:39 -0300618 call_all(usbvision, core, s_std, usbvision->tvnormId);
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300619 mutex_unlock(&usbvision->lock);
Thierry MERLE66a17872007-06-26 16:35:30 -0300620 /* propagate the change to the decoder */
621 usbvision_muxsel(usbvision, usbvision->ctl_input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300622
623 return 0;
624}
625
626static int vidioc_g_tuner (struct file *file, void *priv,
627 struct v4l2_tuner *vt)
628{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300629 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300630
631 if (!usbvision->have_tuner || vt->index) // Only tuner 0
632 return -EINVAL;
633 if(usbvision->radio) {
634 strcpy(vt->name, "Radio");
635 vt->type = V4L2_TUNER_RADIO;
636 } else {
637 strcpy(vt->name, "Television");
638 }
639 /* Let clients fill in the remainder of this struct */
Hans Verkuil1df79532009-02-21 18:11:31 -0300640 call_all(usbvision, tuner, g_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300641
642 return 0;
643}
644
645static int vidioc_s_tuner (struct file *file, void *priv,
646 struct v4l2_tuner *vt)
647{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300648 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300649
650 // Only no or one tuner for now
651 if (!usbvision->have_tuner || vt->index)
652 return -EINVAL;
653 /* let clients handle this */
Hans Verkuil1df79532009-02-21 18:11:31 -0300654 call_all(usbvision, tuner, s_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300655
656 return 0;
657}
658
659static int vidioc_g_frequency (struct file *file, void *priv,
660 struct v4l2_frequency *freq)
661{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300662 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300663
664 freq->tuner = 0; // Only one tuner
665 if(usbvision->radio) {
666 freq->type = V4L2_TUNER_RADIO;
667 } else {
668 freq->type = V4L2_TUNER_ANALOG_TV;
669 }
670 freq->frequency = usbvision->freq;
671
672 return 0;
673}
674
675static int vidioc_s_frequency (struct file *file, void *priv,
676 struct v4l2_frequency *freq)
677{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300678 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300679
680 // Only no or one tuner for now
681 if (!usbvision->have_tuner || freq->tuner)
682 return -EINVAL;
683
684 usbvision->freq = freq->frequency;
Hans Verkuil1df79532009-02-21 18:11:31 -0300685 call_all(usbvision, tuner, s_frequency, freq);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300686
687 return 0;
688}
689
690static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
691{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300692 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300693
Thierry MERLEc5f48362007-05-08 17:22:29 -0300694 if(usbvision->radio) {
695 strcpy(a->name,"Radio");
696 } else {
697 strcpy(a->name, "TV");
698 }
699
700 return 0;
701}
702
703static int vidioc_s_audio (struct file *file, void *fh,
704 struct v4l2_audio *a)
705{
706 if(a->index) {
707 return -EINVAL;
708 }
709
710 return 0;
711}
712
713static int vidioc_queryctrl (struct file *file, void *priv,
714 struct v4l2_queryctrl *ctrl)
715{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300716 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300717
Hans Verkuil1df79532009-02-21 18:11:31 -0300718 call_all(usbvision, core, queryctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300719
720 if (!ctrl->type)
721 return -EINVAL;
722
723 return 0;
724}
725
726static int vidioc_g_ctrl (struct file *file, void *priv,
727 struct v4l2_control *ctrl)
728{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300729 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil1df79532009-02-21 18:11:31 -0300730 call_all(usbvision, core, g_ctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300731
732 return 0;
733}
734
735static int vidioc_s_ctrl (struct file *file, void *priv,
736 struct v4l2_control *ctrl)
737{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300738 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil1df79532009-02-21 18:11:31 -0300739 call_all(usbvision, core, s_ctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300740
741 return 0;
742}
743
744static int vidioc_reqbufs (struct file *file,
745 void *priv, struct v4l2_requestbuffers *vr)
746{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300747 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300748 int ret;
749
750 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
751
752 /* Check input validity:
753 the user must do a VIDEO CAPTURE and MMAP method. */
Trent Piepho975f5762009-03-28 22:25:36 -0300754 if (vr->memory != V4L2_MEMORY_MMAP)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300755 return -EINVAL;
756
757 if(usbvision->streaming == Stream_On) {
758 if ((ret = usbvision_stream_interrupt(usbvision)))
759 return ret;
760 }
761
762 usbvision_frames_free(usbvision);
763 usbvision_empty_framequeues(usbvision);
764 vr->count = usbvision_frames_alloc(usbvision,vr->count);
765
766 usbvision->curFrame = NULL;
767
768 return 0;
769}
770
771static int vidioc_querybuf (struct file *file,
772 void *priv, struct v4l2_buffer *vb)
773{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300774 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300775 struct usbvision_frame *frame;
776
777 /* FIXME : must control
778 that buffers are mapped (VIDIOC_REQBUFS has been called) */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300779 if(vb->index>=usbvision->num_frames) {
780 return -EINVAL;
781 }
782 /* Updating the corresponding frame state */
783 vb->flags = 0;
784 frame = &usbvision->frame[vb->index];
785 if(frame->grabstate >= FrameState_Ready)
786 vb->flags |= V4L2_BUF_FLAG_QUEUED;
787 if(frame->grabstate >= FrameState_Done)
788 vb->flags |= V4L2_BUF_FLAG_DONE;
789 if(frame->grabstate == FrameState_Unused)
790 vb->flags |= V4L2_BUF_FLAG_MAPPED;
791 vb->memory = V4L2_MEMORY_MMAP;
792
793 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
794
795 vb->memory = V4L2_MEMORY_MMAP;
796 vb->field = V4L2_FIELD_NONE;
797 vb->length = usbvision->curwidth*
798 usbvision->curheight*
799 usbvision->palette.bytes_per_pixel;
800 vb->timestamp = usbvision->frame[vb->index].timestamp;
801 vb->sequence = usbvision->frame[vb->index].sequence;
802 return 0;
803}
804
805static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
806{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300807 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300808 struct usbvision_frame *frame;
809 unsigned long lock_flags;
810
811 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300812 if(vb->index>=usbvision->num_frames) {
813 return -EINVAL;
814 }
815
816 frame = &usbvision->frame[vb->index];
817
818 if (frame->grabstate != FrameState_Unused) {
819 return -EAGAIN;
820 }
821
822 /* Mark it as ready and enqueue frame */
823 frame->grabstate = FrameState_Ready;
824 frame->scanstate = ScanState_Scanning;
825 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
826
827 vb->flags &= ~V4L2_BUF_FLAG_DONE;
828
829 /* set v4l2_format index */
830 frame->v4l2_format = usbvision->palette;
831
832 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
833 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
834 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
835
836 return 0;
837}
838
839static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
840{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300841 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300842 int ret;
843 struct usbvision_frame *f;
844 unsigned long lock_flags;
845
Thierry MERLEc5f48362007-05-08 17:22:29 -0300846 if (list_empty(&(usbvision->outqueue))) {
847 if (usbvision->streaming == Stream_Idle)
848 return -EINVAL;
849 ret = wait_event_interruptible
850 (usbvision->wait_frame,
851 !list_empty(&(usbvision->outqueue)));
852 if (ret)
853 return ret;
854 }
855
856 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
857 f = list_entry(usbvision->outqueue.next,
858 struct usbvision_frame, frame);
859 list_del(usbvision->outqueue.next);
860 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
861
862 f->grabstate = FrameState_Unused;
863
864 vb->memory = V4L2_MEMORY_MMAP;
865 vb->flags = V4L2_BUF_FLAG_MAPPED |
866 V4L2_BUF_FLAG_QUEUED |
867 V4L2_BUF_FLAG_DONE;
868 vb->index = f->index;
869 vb->sequence = f->sequence;
870 vb->timestamp = f->timestamp;
871 vb->field = V4L2_FIELD_NONE;
872 vb->bytesused = f->scanlength;
873
874 return 0;
875}
876
877static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
878{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300879 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300880
881 usbvision->streaming = Stream_On;
Hans Verkuil1df79532009-02-21 18:11:31 -0300882 call_all(usbvision, video, s_stream, 1);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300883
884 return 0;
885}
886
887static int vidioc_streamoff(struct file *file,
888 void *priv, enum v4l2_buf_type type)
889{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300890 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300891
892 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
893 return -EINVAL;
894
895 if(usbvision->streaming == Stream_On) {
896 usbvision_stream_interrupt(usbvision);
897 /* Stop all video streamings */
Hans Verkuil1df79532009-02-21 18:11:31 -0300898 call_all(usbvision, video, s_stream, 0);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300899 }
900 usbvision_empty_framequeues(usbvision);
901
902 return 0;
903}
904
Hans Verkuil78b526a2008-05-28 12:16:41 -0300905static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300906 struct v4l2_fmtdesc *vfd)
907{
908 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
909 return -EINVAL;
910 }
Thierry MERLEc5f48362007-05-08 17:22:29 -0300911 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
912 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300913 return 0;
914}
915
Hans Verkuil78b526a2008-05-28 12:16:41 -0300916static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300917 struct v4l2_format *vf)
918{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300919 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300920 vf->fmt.pix.width = usbvision->curwidth;
921 vf->fmt.pix.height = usbvision->curheight;
922 vf->fmt.pix.pixelformat = usbvision->palette.format;
923 vf->fmt.pix.bytesperline =
924 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
925 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
926 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
927 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
928
929 return 0;
930}
931
Hans Verkuil78b526a2008-05-28 12:16:41 -0300932static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300933 struct v4l2_format *vf)
934{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300935 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300936 int formatIdx;
937
938 /* Find requested format in available ones */
939 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
940 if(vf->fmt.pix.pixelformat ==
941 usbvision_v4l2_format[formatIdx].format) {
942 usbvision->palette = usbvision_v4l2_format[formatIdx];
943 break;
944 }
945 }
946 /* robustness */
947 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
948 return -EINVAL;
949 }
950 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
951 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
952
953 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
954 usbvision->palette.bytes_per_pixel;
955 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
956
957 return 0;
958}
959
Hans Verkuil78b526a2008-05-28 12:16:41 -0300960static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300961 struct v4l2_format *vf)
962{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300963 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300964 int ret;
965
Hans Verkuil78b526a2008-05-28 12:16:41 -0300966 if( 0 != (ret=vidioc_try_fmt_vid_cap (file, priv, vf)) ) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300967 return ret;
968 }
969
970 /* stop io in case it is already in progress */
971 if(usbvision->streaming == Stream_On) {
972 if ((ret = usbvision_stream_interrupt(usbvision)))
973 return ret;
974 }
975 usbvision_frames_free(usbvision);
976 usbvision_empty_framequeues(usbvision);
977
978 usbvision->curFrame = NULL;
979
980 /* by now we are committed to the new data... */
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300981 mutex_lock(&usbvision->lock);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300982 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -0300983 mutex_unlock(&usbvision->lock);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300984
985 return 0;
986}
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300987
Al Viro8a5ab412007-02-09 16:38:20 +0000988static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300989 size_t count, loff_t *ppos)
990{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300991 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300992 int noblock = file->f_flags & O_NONBLOCK;
993 unsigned long lock_flags;
994
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300995 int ret,i;
996 struct usbvision_frame *frame;
997
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300998 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300999 (unsigned long)count, noblock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001000
1001 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1002 return -EFAULT;
1003
Thierry MERLEc5f48362007-05-08 17:22:29 -03001004 /* This entry point is compatible with the mmap routines
1005 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1006 to get frames or call read on the device. */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001007 if(!usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001008 /* First, allocate some frames to work with
1009 if this has not been done with VIDIOC_REQBUF */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001010 usbvision_frames_free(usbvision);
1011 usbvision_empty_framequeues(usbvision);
1012 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1013 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001014
Thierry MERLE6f78e182007-02-07 10:13:11 -03001015 if(usbvision->streaming != Stream_On) {
1016 /* no stream is running, make it running ! */
1017 usbvision->streaming = Stream_On;
Hans Verkuil1df79532009-02-21 18:11:31 -03001018 call_all(usbvision, video, s_stream, 1);
Thierry MERLE6f78e182007-02-07 10:13:11 -03001019 }
1020
Thierry MERLEc5f48362007-05-08 17:22:29 -03001021 /* Then, enqueue as many frames as possible
1022 (like a user of VIDIOC_QBUF would do) */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001023 for(i=0;i<usbvision->num_frames;i++) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001024 frame = &usbvision->frame[i];
1025 if(frame->grabstate == FrameState_Unused) {
1026 /* Mark it as ready and enqueue frame */
1027 frame->grabstate = FrameState_Ready;
1028 frame->scanstate = ScanState_Scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001029 /* Accumulated in usbvision_parse_data() */
1030 frame->scanlength = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001031
1032 /* set v4l2_format index */
1033 frame->v4l2_format = usbvision->palette;
1034
1035 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1036 list_add_tail(&frame->frame, &usbvision->inqueue);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001037 spin_unlock_irqrestore(&usbvision->queue_lock,
1038 lock_flags);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001039 }
1040 }
1041
1042 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1043 if (list_empty(&(usbvision->outqueue))) {
1044 if(noblock)
1045 return -EAGAIN;
1046
1047 ret = wait_event_interruptible
1048 (usbvision->wait_frame,
1049 !list_empty(&(usbvision->outqueue)));
1050 if (ret)
1051 return ret;
1052 }
1053
1054 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1055 frame = list_entry(usbvision->outqueue.next,
1056 struct usbvision_frame, frame);
1057 list_del(usbvision->outqueue.next);
1058 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1059
1060 /* An error returns an empty frame */
1061 if (frame->grabstate == FrameState_Error) {
1062 frame->bytes_read = 0;
1063 return 0;
1064 }
1065
Thierry MERLEc5f48362007-05-08 17:22:29 -03001066 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001067 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001068 frame->index, frame->bytes_read, frame->scanlength);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001069
1070 /* copy bytes to user space; we allow for partials reads */
1071 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1072 count = frame->scanlength - frame->bytes_read;
1073
1074 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1075 return -EFAULT;
1076 }
1077
1078 frame->bytes_read += count;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001079 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001080 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001081 (unsigned long)count, frame->bytes_read);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001082
Thierry MERLEc5f48362007-05-08 17:22:29 -03001083 /* For now, forget the frame if it has not been read in one shot. */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001084/* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1085 frame->bytes_read = 0;
1086
1087 /* Mark it as available to be used again. */
Thierry MERLEc430ca12006-12-29 20:32:08 -03001088 frame->grabstate = FrameState_Unused;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001089/* } */
1090
1091 return count;
1092}
1093
1094static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1095{
1096 unsigned long size = vma->vm_end - vma->vm_start,
1097 start = vma->vm_start;
1098 void *pos;
1099 u32 i;
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001100 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001101
Thierry MERLE6f78e182007-02-07 10:13:11 -03001102 PDEBUG(DBG_MMAP, "mmap");
1103
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001104 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001105
1106 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001107 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001108 return -EFAULT;
1109 }
1110
1111 if (!(vma->vm_flags & VM_WRITE) ||
Thierry MERLE6f78e182007-02-07 10:13:11 -03001112 size != PAGE_ALIGN(usbvision->max_frame_size)) {
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001113 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001114 return -EINVAL;
1115 }
1116
Thierry MERLE6f78e182007-02-07 10:13:11 -03001117 for (i = 0; i < usbvision->num_frames; i++) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001118 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1119 vma->vm_pgoff)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001120 break;
1121 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001122 if (i == usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001123 PDEBUG(DBG_MMAP,
1124 "mmap: user supplied mapping address is out of range");
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001125 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001126 return -EINVAL;
1127 }
1128
1129 /* VM_IO is eventually going to replace PageReserved altogether */
1130 vma->vm_flags |= VM_IO;
1131 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1132
1133 pos = usbvision->frame[i].data;
1134 while (size > 0) {
1135
1136 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001137 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001138 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001139 return -EAGAIN;
1140 }
1141 start += PAGE_SIZE;
1142 pos += PAGE_SIZE;
1143 size -= PAGE_SIZE;
1144 }
1145
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001146 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001147 return 0;
1148}
1149
1150
1151/*
1152 * Here comes the stuff for radio on usbvision based devices
1153 *
1154 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001155static int usbvision_radio_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001156{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001157 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001158 int errCode = 0;
1159
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001160 PDEBUG(DBG_IO, "%s:", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001161
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001162 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001163
1164 if (usbvision->user) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001165 dev_err(&usbvision->rdev->dev,
1166 "%s: Someone tried to open an already opened USBVision Radio!\n",
1167 __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001168 errCode = -EBUSY;
1169 }
1170 else {
1171 if(PowerOnAtOpen) {
1172 usbvision_reset_powerOffTimer(usbvision);
1173 if (usbvision->power == 0) {
1174 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001175 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001176 }
1177 }
1178
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001179 /* Alternate interface 1 is is the biggest frame size */
1180 errCode = usbvision_set_alternate(usbvision);
1181 if (errCode < 0) {
1182 usbvision->last_error = errCode;
Andrew Morton544e6172007-12-12 18:25:23 -03001183 errCode = -EBUSY;
1184 goto out;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001185 }
1186
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001187 // If so far no errors then we shall start the radio
1188 usbvision->radio = 1;
Hans Verkuil1df79532009-02-21 18:11:31 -03001189 call_all(usbvision, tuner, s_radio);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001190 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1191 usbvision->user++;
1192 }
1193
1194 if (errCode) {
1195 if (PowerOnAtOpen) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001196 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001197 usbvision_power_off(usbvision);
1198 usbvision->initialized = 0;
1199 }
1200 }
Andrew Morton544e6172007-12-12 18:25:23 -03001201out:
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001202 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001203 return errCode;
1204}
1205
1206
Hans Verkuilbec43662008-12-30 06:58:20 -03001207static int usbvision_radio_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001208{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001209 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001210 int errCode = 0;
1211
1212 PDEBUG(DBG_IO, "");
1213
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001214 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001215
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001216 /* Set packet size to 0 */
1217 usbvision->ifaceAlt=0;
1218 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1219 usbvision->ifaceAlt);
1220
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001221 usbvision_audio_off(usbvision);
1222 usbvision->radio=0;
1223 usbvision->user--;
1224
1225 if (PowerOnAtOpen) {
1226 usbvision_set_powerOffTimer(usbvision);
1227 usbvision->initialized = 0;
1228 }
1229
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001230 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001231
1232 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001233 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001234 usbvision_release(usbvision);
1235 }
1236
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001237 PDEBUG(DBG_IO, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001238 return errCode;
1239}
1240
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001241//
1242// Video registration stuff
1243//
1244
1245// Video template
Hans Verkuilbec43662008-12-30 06:58:20 -03001246static const struct v4l2_file_operations usbvision_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001247 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001248 .open = usbvision_v4l2_open,
1249 .release = usbvision_v4l2_close,
1250 .read = usbvision_v4l2_read,
1251 .mmap = usbvision_v4l2_mmap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001252 .ioctl = video_ioctl2,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001253/* .poll = video_poll, */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001254};
Hans Verkuila3998102008-07-21 02:57:38 -03001255
1256static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001257 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001258 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1259 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1260 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1261 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001262 .vidioc_reqbufs = vidioc_reqbufs,
1263 .vidioc_querybuf = vidioc_querybuf,
1264 .vidioc_qbuf = vidioc_qbuf,
1265 .vidioc_dqbuf = vidioc_dqbuf,
1266 .vidioc_s_std = vidioc_s_std,
1267 .vidioc_enum_input = vidioc_enum_input,
1268 .vidioc_g_input = vidioc_g_input,
1269 .vidioc_s_input = vidioc_s_input,
1270 .vidioc_queryctrl = vidioc_queryctrl,
1271 .vidioc_g_audio = vidioc_g_audio,
Al Viroed4d6372007-09-26 01:53:42 +01001272 .vidioc_s_audio = vidioc_s_audio,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001273 .vidioc_g_ctrl = vidioc_g_ctrl,
1274 .vidioc_s_ctrl = vidioc_s_ctrl,
1275 .vidioc_streamon = vidioc_streamon,
1276 .vidioc_streamoff = vidioc_streamoff,
1277#ifdef CONFIG_VIDEO_V4L1_COMPAT
1278/* .vidiocgmbuf = vidiocgmbuf, */
1279#endif
1280 .vidioc_g_tuner = vidioc_g_tuner,
1281 .vidioc_s_tuner = vidioc_s_tuner,
1282 .vidioc_g_frequency = vidioc_g_frequency,
1283 .vidioc_s_frequency = vidioc_s_frequency,
1284#ifdef CONFIG_VIDEO_ADV_DEBUG
1285 .vidioc_g_register = vidioc_g_register,
1286 .vidioc_s_register = vidioc_s_register,
1287#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001288};
1289
1290static struct video_device usbvision_video_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001291 .fops = &usbvision_fops,
1292 .ioctl_ops = &usbvision_ioctl_ops,
1293 .name = "usbvision-video",
1294 .release = video_device_release,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001295 .tvnorms = USBVISION_NORMS,
1296 .current_norm = V4L2_STD_PAL
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001297};
1298
1299
1300// Radio template
Hans Verkuilbec43662008-12-30 06:58:20 -03001301static const struct v4l2_file_operations usbvision_radio_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001302 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001303 .open = usbvision_radio_open,
1304 .release = usbvision_radio_close,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001305 .ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001306};
1307
Hans Verkuila3998102008-07-21 02:57:38 -03001308static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001309 .vidioc_querycap = vidioc_querycap,
1310 .vidioc_enum_input = vidioc_enum_input,
1311 .vidioc_g_input = vidioc_g_input,
1312 .vidioc_s_input = vidioc_s_input,
1313 .vidioc_queryctrl = vidioc_queryctrl,
1314 .vidioc_g_audio = vidioc_g_audio,
Al Viroed4d6372007-09-26 01:53:42 +01001315 .vidioc_s_audio = vidioc_s_audio,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001316 .vidioc_g_ctrl = vidioc_g_ctrl,
1317 .vidioc_s_ctrl = vidioc_s_ctrl,
1318 .vidioc_g_tuner = vidioc_g_tuner,
1319 .vidioc_s_tuner = vidioc_s_tuner,
1320 .vidioc_g_frequency = vidioc_g_frequency,
1321 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuila3998102008-07-21 02:57:38 -03001322};
1323
1324static struct video_device usbvision_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001325 .fops = &usbvision_radio_fops,
1326 .name = "usbvision-radio",
1327 .release = video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001328 .ioctl_ops = &usbvision_radio_ioctl_ops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001329
Thierry MERLEc5f48362007-05-08 17:22:29 -03001330 .tvnorms = USBVISION_NORMS,
1331 .current_norm = V4L2_STD_PAL
1332};
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001333
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001334
1335static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1336 struct video_device *vdev_template,
1337 char *name)
1338{
1339 struct usb_device *usb_dev = usbvision->dev;
1340 struct video_device *vdev;
1341
1342 if (usb_dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001343 dev_err(&usbvision->dev->dev,
1344 "%s: usbvision->dev is not set\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001345 return NULL;
1346 }
1347
1348 vdev = video_device_alloc();
1349 if (NULL == vdev) {
1350 return NULL;
1351 }
1352 *vdev = *vdev_template;
Hans Verkuil1df79532009-02-21 18:11:31 -03001353 vdev->v4l2_dev = &usbvision->v4l2_dev;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001354 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1355 video_set_drvdata(vdev, usbvision);
1356 return vdev;
1357}
1358
1359// unregister video4linux devices
1360static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1361{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001362 // Radio Device:
1363 if (usbvision->rdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001364 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1365 video_device_node_name(usbvision->rdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001366 if (video_is_registered(usbvision->rdev)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001367 video_unregister_device(usbvision->rdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001368 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001369 video_device_release(usbvision->rdev);
1370 }
1371 usbvision->rdev = NULL;
1372 }
1373
1374 // Video Device:
1375 if (usbvision->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001376 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1377 video_device_node_name(usbvision->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001378 if (video_is_registered(usbvision->vdev)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001379 video_unregister_device(usbvision->vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001380 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001381 video_device_release(usbvision->vdev);
1382 }
1383 usbvision->vdev = NULL;
1384 }
1385}
1386
1387// register video4linux devices
1388static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1389{
1390 // Video Device:
Thierry MERLEc5f48362007-05-08 17:22:29 -03001391 usbvision->vdev = usbvision_vdev_init(usbvision,
1392 &usbvision_video_template,
1393 "USBVision Video");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001394 if (usbvision->vdev == NULL) {
1395 goto err_exit;
1396 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001397 if (video_register_device(usbvision->vdev,
1398 VFL_TYPE_GRABBER,
1399 video_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001400 goto err_exit;
1401 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001402 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1403 usbvision->nr, video_device_node_name(usbvision->vdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001404
1405 // Radio Device:
1406 if (usbvision_device_data[usbvision->DevModel].Radio) {
1407 // usbvision has radio
Thierry MERLEc5f48362007-05-08 17:22:29 -03001408 usbvision->rdev = usbvision_vdev_init(usbvision,
1409 &usbvision_radio_template,
1410 "USBVision Radio");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001411 if (usbvision->rdev == NULL) {
1412 goto err_exit;
1413 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001414 if (video_register_device(usbvision->rdev,
1415 VFL_TYPE_RADIO,
1416 radio_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001417 goto err_exit;
1418 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001419 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1420 usbvision->nr, video_device_node_name(usbvision->rdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001421 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001422 // all done
1423 return 0;
1424
1425 err_exit:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001426 dev_err(&usbvision->dev->dev,
1427 "USBVision[%d]: video_register_device() failed\n",
1428 usbvision->nr);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001429 usbvision_unregister_video(usbvision);
1430 return -1;
1431}
1432
1433/*
1434 * usbvision_alloc()
1435 *
Thierry MERLEc5f48362007-05-08 17:22:29 -03001436 * This code allocates the struct usb_usbvision.
1437 * It is filled with default values.
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001438 *
1439 * Returns NULL on error, a pointer to usb_usbvision else.
1440 *
1441 */
Janne Grunaua8784402009-04-01 08:46:00 -03001442static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1443 struct usb_interface *intf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001444{
1445 struct usb_usbvision *usbvision;
1446
Hans Verkuil1df79532009-02-21 18:11:31 -03001447 usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1448 if (usbvision == NULL)
1449 return NULL;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001450
1451 usbvision->dev = dev;
Janne Grunaua8784402009-04-01 08:46:00 -03001452 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
Hans Verkuil1df79532009-02-21 18:11:31 -03001453 goto err_free;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001454
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001455 mutex_init(&usbvision->lock); /* available */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001456
1457 // prepare control urb for control messages during interrupts
1458 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
Hans Verkuil1df79532009-02-21 18:11:31 -03001459 if (usbvision->ctrlUrb == NULL)
1460 goto err_unreg;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001461 init_waitqueue_head(&usbvision->ctrlUrb_wq);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001462
1463 usbvision_init_powerOffTimer(usbvision);
1464
1465 return usbvision;
1466
Hans Verkuil1df79532009-02-21 18:11:31 -03001467err_unreg:
1468 v4l2_device_unregister(&usbvision->v4l2_dev);
1469err_free:
1470 kfree(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001471 return NULL;
1472}
1473
1474/*
1475 * usbvision_release()
1476 *
1477 * This code does final release of struct usb_usbvision. This happens
1478 * after the device is disconnected -and- all clients closed their files.
1479 *
1480 */
1481static void usbvision_release(struct usb_usbvision *usbvision)
1482{
1483 PDEBUG(DBG_PROBE, "");
1484
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001485 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001486
1487 usbvision_reset_powerOffTimer(usbvision);
1488
1489 usbvision->initialized = 0;
1490
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001491 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001492
1493 usbvision_remove_sysfs(usbvision->vdev);
1494 usbvision_unregister_video(usbvision);
1495
1496 if (usbvision->ctrlUrb) {
1497 usb_free_urb(usbvision->ctrlUrb);
1498 }
1499
Hans Verkuil1df79532009-02-21 18:11:31 -03001500 v4l2_device_unregister(&usbvision->v4l2_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001501 kfree(usbvision);
1502
1503 PDEBUG(DBG_PROBE, "success");
1504}
1505
1506
Thierry MERLEc5f48362007-05-08 17:22:29 -03001507/*********************** usb interface **********************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001508
1509static void usbvision_configure_video(struct usb_usbvision *usbvision)
1510{
Thierry MERLEc5f48362007-05-08 17:22:29 -03001511 int model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001512
1513 if (usbvision == NULL)
1514 return;
1515
1516 model = usbvision->DevModel;
1517 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1518
Trent Piephoc682b3a2007-04-14 15:16:26 -03001519 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001520 usbvision->Vin_Reg2_Preset =
1521 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001522 } else {
1523 usbvision->Vin_Reg2_Preset = 0;
1524 }
1525
Thierry MERLEc5f48362007-05-08 17:22:29 -03001526 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001527
1528 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1529 usbvision->ctl_input = 0;
1530
1531 /* This should be here to make i2c clients to be able to register */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001532 /* first switch off audio */
1533 usbvision_audio_off(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001534 if (!PowerOnAtOpen) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001535 /* and then power up the noisy tuner */
1536 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001537 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001538 }
1539}
1540
1541/*
1542 * usbvision_probe()
1543 *
1544 * This procedure queries device descriptor and accepts the interface
1545 * if it looks like USBVISION video device
1546 *
1547 */
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001548static int __devinit usbvision_probe(struct usb_interface *intf,
1549 const struct usb_device_id *devid)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001550{
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001551 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1552 struct usb_interface *uif;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001553 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1554 const struct usb_host_interface *interface;
1555 struct usb_usbvision *usbvision = NULL;
1556 const struct usb_endpoint_descriptor *endpoint;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001557 int model,i;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001558
1559 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001560 dev->descriptor.idVendor,
1561 dev->descriptor.idProduct, ifnum);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001562
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001563 model = devid->driver_info;
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001564 if ( (model<0) || (model>=usbvision_device_data_size) ) {
Thierry MERLE672d0132007-04-14 17:53:55 -03001565 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001566 return -ENODEV;
1567 }
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001568 printk(KERN_INFO "%s: %s found\n", __func__,
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001569 usbvision_device_data[model].ModelString);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001570
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001571 if (usbvision_device_data[model].Interface >= 0) {
1572 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
Thierry MERLEc5f48362007-05-08 17:22:29 -03001573 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001574 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1575 }
1576 endpoint = &interface->endpoint[1].desc;
Julia Lawall2230c3c2009-01-03 16:53:10 -03001577 if (!usb_endpoint_xfer_isoc(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001578 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001579 __func__, ifnum);
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001580 dev_err(&intf->dev, "%s: Endpoint attributes %d",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001581 __func__, endpoint->bmAttributes);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001582 return -ENODEV;
1583 }
Julia Lawall13417982008-12-29 21:49:22 -03001584 if (usb_endpoint_dir_out(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001585 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001586 __func__, ifnum);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001587 return -ENODEV;
1588 }
1589
Janne Grunaua8784402009-04-01 08:46:00 -03001590 usbvision = usbvision_alloc(dev, intf);
1591 if (usbvision == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001592 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001593 return -ENOMEM;
1594 }
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001595
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001596 if (dev->descriptor.bNumConfigurations > 1) {
1597 usbvision->bridgeType = BRIDGE_NT1004;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001598 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001599 usbvision->bridgeType = BRIDGE_NT1005;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001600 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001601 usbvision->bridgeType = BRIDGE_NT1003;
1602 }
1603 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1604
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001605 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001606
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001607 /* compute alternate max packet sizes */
1608 uif = dev->actconfig->interface[0];
1609
1610 usbvision->num_alt=uif->num_altsetting;
1611 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1612 usbvision->alt_max_pkt_size = kmalloc(32*
1613 usbvision->num_alt,GFP_KERNEL);
1614 if (usbvision->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001615 dev_err(&intf->dev, "usbvision: out of memory!\n");
Daniel Walker09911122007-12-12 18:22:48 -03001616 mutex_unlock(&usbvision->lock);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001617 return -ENOMEM;
1618 }
1619
1620 for (i = 0; i < usbvision->num_alt ; i++) {
1621 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1622 wMaxPacketSize);
1623 usbvision->alt_max_pkt_size[i] =
1624 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1625 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1626 usbvision->alt_max_pkt_size[i]);
1627 }
1628
1629
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001630 usbvision->nr = usbvision_nr++;
1631
1632 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1633 if (usbvision->have_tuner) {
1634 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1635 }
1636
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001637 usbvision->DevModel = model;
1638 usbvision->remove_pending = 0;
1639 usbvision->iface = ifnum;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001640 usbvision->ifaceAlt = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001641 usbvision->video_endp = endpoint->bEndpointAddress;
1642 usbvision->isocPacketSize = 0;
1643 usbvision->usb_bandwidth = 0;
1644 usbvision->user = 0;
1645 usbvision->streaming = Stream_Off;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001646 usbvision_configure_video(usbvision);
Hans Verkuil84034722010-09-17 15:07:28 -03001647 usbvision_register_video(usbvision);
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001648 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001649
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001650 usbvision_create_sysfs(usbvision->vdev);
1651
1652 PDEBUG(DBG_PROBE, "success");
1653 return 0;
1654}
1655
1656
1657/*
1658 * usbvision_disconnect()
1659 *
1660 * This procedure stops all driver activity, deallocates interface-private
1661 * structure (pointed by 'ptr') and after that driver should be removable
1662 * with no ill consequences.
1663 *
1664 */
1665static void __devexit usbvision_disconnect(struct usb_interface *intf)
1666{
Hans Verkuilaeb506a2010-05-02 09:00:13 -03001667 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001668
1669 PDEBUG(DBG_PROBE, "");
1670
1671 if (usbvision == NULL) {
Julia Lawall42020662010-05-27 09:36:45 -03001672 pr_err("%s: usb_get_intfdata() failed\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001673 return;
1674 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001675
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001676 mutex_lock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001677
1678 // At this time we ask to cancel outstanding URBs
1679 usbvision_stop_isoc(usbvision);
1680
Hans Verkuilf1ba28c2009-03-14 12:27:01 -03001681 v4l2_device_disconnect(&usbvision->v4l2_dev);
1682
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001683 if (usbvision->power) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001684 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001685 usbvision_power_off(usbvision);
1686 }
1687 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1688
1689 usb_put_dev(usbvision->dev);
1690 usbvision->dev = NULL; // USB device is no more
1691
Matthias Kaehlcke289d4d22007-10-24 17:31:15 -03001692 mutex_unlock(&usbvision->lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001693
1694 if (usbvision->user) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001695 printk(KERN_INFO "%s: In use, disconnect pending\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001696 __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001697 wake_up_interruptible(&usbvision->wait_frame);
1698 wake_up_interruptible(&usbvision->wait_stream);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001699 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001700 usbvision_release(usbvision);
1701 }
1702
1703 PDEBUG(DBG_PROBE, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001704}
1705
1706static struct usb_driver usbvision_driver = {
1707 .name = "usbvision",
1708 .id_table = usbvision_table,
1709 .probe = usbvision_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001710 .disconnect = __devexit_p(usbvision_disconnect),
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001711};
1712
1713/*
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001714 * usbvision_init()
1715 *
1716 * This code is run to initialize the driver.
1717 *
1718 */
1719static int __init usbvision_init(void)
1720{
1721 int errCode;
1722
1723 PDEBUG(DBG_PROBE, "");
1724
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001725 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1726 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
Thierry MERLEc876a342006-12-09 16:42:54 -03001727 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001728
1729 /* disable planar mode support unless compression enabled */
1730 if (isocMode != ISOC_MODE_COMPRESS ) {
1731 // FIXME : not the right way to set supported flag
1732 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1733 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1734 }
1735
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001736 errCode = usb_register(&usbvision_driver);
1737
1738 if (errCode == 0) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001739 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001740 PDEBUG(DBG_PROBE, "success");
1741 }
1742 return errCode;
1743}
1744
1745static void __exit usbvision_exit(void)
1746{
1747 PDEBUG(DBG_PROBE, "");
1748
1749 usb_deregister(&usbvision_driver);
1750 PDEBUG(DBG_PROBE, "success");
1751}
1752
1753module_init(usbvision_init);
1754module_exit(usbvision_exit);
1755
1756/*
1757 * Overrides for Emacs so that we follow Linus's tabbing style.
1758 * ---------------------------------------------------------------------------
1759 * Local variables:
1760 * c-basic-offset: 8
1761 * End:
1762 */