blob: 4882ea087b92ed0f7dd7bb08d787182103894dc0 [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>
53#include <linux/mm.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030054#include <linux/highmem.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030055#include <linux/vmalloc.h>
56#include <linux/module.h>
57#include <linux/init.h>
58#include <linux/spinlock.h>
59#include <asm/io.h>
60#include <linux/videodev2.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030061#include <linux/i2c.h>
62
63#include <media/saa7115.h>
64#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030065#include <media/v4l2-ioctl.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030066#include <media/tuner.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030067
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -030068#include <linux/workqueue.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030069
Thierry MERLE483dfdb2006-12-04 08:31:45 -030070#include "usbvision.h"
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -030071#include "usbvision-cards.h"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030072
Thierry MERLEc5f48362007-05-08 17:22:29 -030073#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
74 Dwaine Garden <DwaineGarden@rogers.com>"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030075#define DRIVER_NAME "usbvision"
76#define DRIVER_ALIAS "USBVision"
77#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
78#define DRIVER_LICENSE "GPL"
79#define USBVISION_DRIVER_VERSION_MAJOR 0
80#define USBVISION_DRIVER_VERSION_MINOR 9
Hans Verkuil3ff4ad82009-04-01 03:15:52 -030081#define USBVISION_DRIVER_VERSION_PATCHLEVEL 10
Thierry MERLEc5f48362007-05-08 17:22:29 -030082#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
83USBVISION_DRIVER_VERSION_MINOR,\
84USBVISION_DRIVER_VERSION_PATCHLEVEL)
85#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
86 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
87 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
Thierry MERLE483dfdb2006-12-04 08:31:45 -030088
89#define ENABLE_HEXDUMP 0 /* Enable if you need it */
90
91
Thierry MERLE483dfdb2006-12-04 08:31:45 -030092#ifdef USBVISION_DEBUG
Thierry MERLEdf18c312008-04-04 21:00:57 -030093 #define PDEBUG(level, fmt, args...) { \
Thierry MERLEc5f48362007-05-08 17:22:29 -030094 if (video_debug & (level)) \
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -030095 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
96 __func__, __LINE__ , ## args); \
Thierry MERLEdf18c312008-04-04 21:00:57 -030097 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -030098#else
99 #define PDEBUG(level, fmt, args...) do {} while(0)
100#endif
101
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300102#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
Thierry MERLEc5f48362007-05-08 17:22:29 -0300111/* sequential number of usbvision device */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300112static int usbvision_nr;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300113
114static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
115 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
116 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
117 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
118 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
119 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
120 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
121 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
122 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
123};
124
Thierry MERLEc5f48362007-05-08 17:22:29 -0300125/* Function prototypes */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300126static void usbvision_release(struct usb_usbvision *usbvision);
127
Joe Perchesc84e6032008-02-03 17:18:59 +0200128/* Default initialization of device driver parameters */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300129/* Set the default format for ISOC endpoint */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300130static int isoc_mode = ISOC_MODE_COMPRESS;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300131/* Set the default Debug Mode of the device driver */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300132static int video_debug;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300133/* Set the default device to power on at startup */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300134static int power_on_at_open = 1;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300135/* Sequential Number of Video Device */
136static int video_nr = -1;
137/* Sequential Number of Radio Device */
138static int radio_nr = -1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300139
Thierry MERLEc5f48362007-05-08 17:22:29 -0300140/* Grab parameters for the device driver */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300141
Thierry MERLEc5f48362007-05-08 17:22:29 -0300142/* Showing parameters under SYSFS */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300143module_param(isoc_mode, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300144module_param(video_debug, int, 0444);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300145module_param(power_on_at_open, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300146module_param(video_nr, int, 0444);
147module_param(radio_nr, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300148
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300149MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300150MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300151MODULE_PARM_DESC(power_on_at_open, " Set the default device to power on when device is opened. Default: 1 (On)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300152MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
153MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300154
155
156// Misc stuff
157MODULE_AUTHOR(DRIVER_AUTHOR);
158MODULE_DESCRIPTION(DRIVER_DESC);
159MODULE_LICENSE(DRIVER_LICENSE);
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -0300160MODULE_VERSION(USBVISION_VERSION_STRING);
161MODULE_ALIAS(DRIVER_ALIAS);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300162
163
Thierry MERLEc5f48362007-05-08 17:22:29 -0300164/*****************************************************************************/
165/* SYSFS Code - Copied from the stv680.c usb module. */
166/* Device information is located at /sys/class/video4linux/video0 */
167/* Device parameters information is located at /sys/module/usbvision */
168/* Device USB Information is located at */
169/* /sys/bus/usb/drivers/USBVision Video Grabber */
170/*****************************************************************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300171
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300172#define YES_NO(x) ((x) ? "Yes" : "No")
173
Kay Sievers54bd5b62007-10-08 16:26:13 -0300174static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300175{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300176 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300177 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300178 return video_get_drvdata(vdev);
179}
180
Kay Sievers54bd5b62007-10-08 16:26:13 -0300181static ssize_t show_version(struct device *cd,
182 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300183{
184 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
185}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300186static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300187
Kay Sievers54bd5b62007-10-08 16:26:13 -0300188static ssize_t show_model(struct device *cd,
189 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300190{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300191 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300192 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300193 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300194 return sprintf(buf, "%s\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300195 usbvision_device_data[usbvision->dev_model].model_string);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300196}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300197static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300198
Kay Sievers54bd5b62007-10-08 16:26:13 -0300199static ssize_t show_hue(struct device *cd,
200 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300201{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300202 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300203 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300204 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
205 struct v4l2_control ctrl;
206 ctrl.id = V4L2_CID_HUE;
207 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200208 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300209 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300210 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300211}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300212static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300213
Kay Sievers54bd5b62007-10-08 16:26:13 -0300214static ssize_t show_contrast(struct device *cd,
215 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300216{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300217 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300218 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300219 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
220 struct v4l2_control ctrl;
221 ctrl.id = V4L2_CID_CONTRAST;
222 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200223 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300224 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300225 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300226}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300227static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300228
Kay Sievers54bd5b62007-10-08 16:26:13 -0300229static ssize_t show_brightness(struct device *cd,
230 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300231{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300232 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300233 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300234 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
235 struct v4l2_control ctrl;
236 ctrl.id = V4L2_CID_BRIGHTNESS;
237 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200238 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300239 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300240 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300241}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300242static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300243
Kay Sievers54bd5b62007-10-08 16:26:13 -0300244static ssize_t show_saturation(struct device *cd,
245 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300246{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300247 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300248 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300249 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
250 struct v4l2_control ctrl;
251 ctrl.id = V4L2_CID_SATURATION;
252 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200253 if(usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300254 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300255 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300256}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300257static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300258
Kay Sievers54bd5b62007-10-08 16:26:13 -0300259static ssize_t show_streaming(struct device *cd,
260 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300261{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300262 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300263 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300264 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300265 return sprintf(buf, "%s\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300266 YES_NO(usbvision->streaming==stream_on?1:0));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300267}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300268static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300269
Kay Sievers54bd5b62007-10-08 16:26:13 -0300270static ssize_t show_compression(struct device *cd,
271 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300272{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300273 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300274 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300276 return sprintf(buf, "%s\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300277 YES_NO(usbvision->isoc_mode==ISOC_MODE_COMPRESS));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300278}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300279static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300280
Kay Sievers54bd5b62007-10-08 16:26:13 -0300281static ssize_t show_device_bridge(struct device *cd,
282 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300283{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300284 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300285 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300286 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300287 return sprintf(buf, "%d\n", usbvision->bridge_type);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300288}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300289static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300290
291static void usbvision_create_sysfs(struct video_device *vdev)
292{
293 int res;
Dwaine Gardened00b412006-12-27 10:23:28 -0200294 if (!vdev)
295 return;
296 do {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300297 res = device_create_file(&vdev->dev, &dev_attr_version);
Dwaine Gardened00b412006-12-27 10:23:28 -0200298 if (res<0)
299 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300300 res = device_create_file(&vdev->dev, &dev_attr_model);
Dwaine Gardened00b412006-12-27 10:23:28 -0200301 if (res<0)
302 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300303 res = device_create_file(&vdev->dev, &dev_attr_hue);
Dwaine Gardened00b412006-12-27 10:23:28 -0200304 if (res<0)
305 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300306 res = device_create_file(&vdev->dev, &dev_attr_contrast);
Dwaine Gardened00b412006-12-27 10:23:28 -0200307 if (res<0)
308 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300309 res = device_create_file(&vdev->dev, &dev_attr_brightness);
Dwaine Gardened00b412006-12-27 10:23:28 -0200310 if (res<0)
311 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300312 res = device_create_file(&vdev->dev, &dev_attr_saturation);
Dwaine Gardened00b412006-12-27 10:23:28 -0200313 if (res<0)
314 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300315 res = device_create_file(&vdev->dev, &dev_attr_streaming);
Dwaine Gardened00b412006-12-27 10:23:28 -0200316 if (res<0)
317 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300318 res = device_create_file(&vdev->dev, &dev_attr_compression);
Dwaine Gardened00b412006-12-27 10:23:28 -0200319 if (res<0)
320 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300321 res = device_create_file(&vdev->dev, &dev_attr_bridge);
Dwaine Gardened00b412006-12-27 10:23:28 -0200322 if (res>=0)
323 return;
324 } while (0);
325
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300326 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300327}
328
329static void usbvision_remove_sysfs(struct video_device *vdev)
330{
331 if (vdev) {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300332 device_remove_file(&vdev->dev, &dev_attr_version);
333 device_remove_file(&vdev->dev, &dev_attr_model);
334 device_remove_file(&vdev->dev, &dev_attr_hue);
335 device_remove_file(&vdev->dev, &dev_attr_contrast);
336 device_remove_file(&vdev->dev, &dev_attr_brightness);
337 device_remove_file(&vdev->dev, &dev_attr_saturation);
338 device_remove_file(&vdev->dev, &dev_attr_streaming);
339 device_remove_file(&vdev->dev, &dev_attr_compression);
340 device_remove_file(&vdev->dev, &dev_attr_bridge);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300341 }
342}
343
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300344/*
345 * usbvision_open()
346 *
347 * This is part of Video 4 Linux API. The driver can be opened by one
348 * client only (checks internal counter 'usbvision->user'). The procedure
349 * then allocates buffers needed for video processing.
350 *
351 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300352static int usbvision_v4l2_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300353{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300354 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300355 int err_code = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300356
357 PDEBUG(DBG_IO, "open");
358
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300359 usbvision_reset_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300360
361 if (usbvision->user)
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300362 err_code = -EBUSY;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300363 else {
Thierry MERLE6f78e182007-02-07 10:13:11 -0300364 /* Allocate memory for the scratch ring buffer */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300365 err_code = usbvision_scratch_alloc(usbvision);
366 if (isoc_mode==ISOC_MODE_COMPRESS) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300367 /* Allocate intermediate decompression buffers
368 only if needed */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300369 err_code = usbvision_decompress_alloc(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300370 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300371 if (err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300372 /* Deallocate all buffers if trouble */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300373 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300374 usbvision_decompress_free(usbvision);
375 }
376 }
377
378 /* If so far no errors then we shall start the camera */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300379 if (!err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300380 if (usbvision->power == 0) {
381 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300382 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300383 }
384
385 /* Send init sequence only once, it's large! */
386 if (!usbvision->initialized) {
387 int setup_ok = 0;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300388 setup_ok = usbvision_setup(usbvision,isoc_mode);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300389 if (setup_ok)
390 usbvision->initialized = 1;
391 else
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300392 err_code = -EBUSY;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300393 }
394
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300395 if (!err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300396 usbvision_begin_streaming(usbvision);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300397 err_code = usbvision_init_isoc(usbvision);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300398 /* device must be initialized before isoc transfer */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300399 usbvision_muxsel(usbvision,0);
400 usbvision->user++;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300401 } else {
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300402 if (power_on_at_open) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300403 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300404 usbvision_power_off(usbvision);
405 usbvision->initialized = 0;
406 }
407 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300408 }
409
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300410 /* prepare queues */
411 usbvision_empty_framequeues(usbvision);
412
413 PDEBUG(DBG_IO, "success");
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300414 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300415}
416
417/*
418 * usbvision_v4l2_close()
419 *
420 * This is part of Video 4 Linux API. The procedure
421 * stops streaming and deallocates all buffers that were earlier
422 * allocated in usbvision_v4l2_open().
423 *
424 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300425static int usbvision_v4l2_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300426{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300427 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300428
429 PDEBUG(DBG_IO, "close");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300430
431 usbvision_audio_off(usbvision);
432 usbvision_restart_isoc(usbvision);
433 usbvision_stop_isoc(usbvision);
434
435 usbvision_decompress_free(usbvision);
Thierry MERLE38284ba2006-12-15 16:46:53 -0300436 usbvision_frames_free(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300437 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300438 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300439
440 usbvision->user--;
441
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300442 if (power_on_at_open) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300443 /* power off in a little while
444 to avoid off/on every close/open short sequences */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300445 usbvision_set_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300446 usbvision->initialized = 0;
447 }
448
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300449 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300450 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300451 usbvision_release(usbvision);
452 }
453
454 PDEBUG(DBG_IO, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300455 return 0;
456}
457
458
459/*
460 * usbvision_ioctl()
461 *
462 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
463 *
464 */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300465#ifdef CONFIG_VIDEO_ADV_DEBUG
466static int vidioc_g_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300467 struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300468{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300469 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300470 int err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300471
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300472 if (!v4l2_chip_match_host(&reg->match))
Thierry MERLEc5f48362007-05-08 17:22:29 -0300473 return -EINVAL;
474 /* NT100x has a 8-bit register space */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300475 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
476 if (err_code < 0) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300477 dev_err(&usbvision->vdev->dev,
478 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300479 __func__, err_code);
480 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300481 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300482 reg->val = err_code;
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300483 reg->size = 1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300484 return 0;
485}
486
Thierry MERLEc5f48362007-05-08 17:22:29 -0300487static int vidioc_s_register (struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300488 struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300489{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300490 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300491 int err_code;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300492
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300493 if (!v4l2_chip_match_host(&reg->match))
Thierry MERLEc5f48362007-05-08 17:22:29 -0300494 return -EINVAL;
495 /* NT100x has a 8-bit register space */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300496 err_code = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
497 if (err_code < 0) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300498 dev_err(&usbvision->vdev->dev,
499 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300500 __func__, err_code);
501 return err_code;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300502 }
503 return 0;
504}
505#endif
506
507static int vidioc_querycap (struct file *file, void *priv,
508 struct v4l2_capability *vc)
509{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300510 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300511
512 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
513 strlcpy(vc->card,
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300514 usbvision_device_data[usbvision->dev_model].model_string,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300515 sizeof(vc->card));
Thierry MERLE6f2a2782009-01-20 17:40:44 -0300516 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
Thierry MERLEc5f48362007-05-08 17:22:29 -0300517 vc->version = USBVISION_DRIVER_VERSION;
518 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
519 V4L2_CAP_AUDIO |
520 V4L2_CAP_READWRITE |
521 V4L2_CAP_STREAMING |
522 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
523 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300524}
525
Thierry MERLEc5f48362007-05-08 17:22:29 -0300526static int vidioc_enum_input (struct file *file, void *priv,
527 struct v4l2_input *vi)
528{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300529 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300530 int chan;
531
Roel Kluin223ffe52009-05-02 16:38:47 -0300532 if (vi->index >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300533 return -EINVAL;
534 if (usbvision->have_tuner) {
535 chan = vi->index;
536 } else {
537 chan = vi->index + 1; /*skip Television string*/
538 }
539 /* Determine the requested input characteristics
540 specific for each usbvision card model */
541 switch(chan) {
542 case 0:
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300543 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300544 strcpy(vi->name, "White Video Input");
545 } else {
546 strcpy(vi->name, "Television");
547 vi->type = V4L2_INPUT_TYPE_TUNER;
548 vi->audioset = 1;
549 vi->tuner = chan;
550 vi->std = USBVISION_NORMS;
551 }
552 break;
553 case 1:
554 vi->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300555 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300556 strcpy(vi->name, "Green Video Input");
557 } else {
558 strcpy(vi->name, "Composite Video Input");
559 }
560 vi->std = V4L2_STD_PAL;
561 break;
562 case 2:
563 vi->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300564 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300565 strcpy(vi->name, "Yellow Video Input");
566 } else {
567 strcpy(vi->name, "S-Video Input");
568 }
569 vi->std = V4L2_STD_PAL;
570 break;
571 case 3:
572 vi->type = V4L2_INPUT_TYPE_CAMERA;
573 strcpy(vi->name, "Red Video Input");
574 vi->std = V4L2_STD_PAL;
575 break;
576 }
577 return 0;
578}
579
580static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
581{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300582 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300583
584 *input = usbvision->ctl_input;
585 return 0;
586}
587
588static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
589{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300590 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300591
Roel Kluinf14a2972009-10-23 07:59:42 -0300592 if (input >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300593 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300594
Thierry MERLE66a17872007-06-26 16:35:30 -0300595 usbvision_muxsel(usbvision, input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300596 usbvision_set_input(usbvision);
597 usbvision_set_output(usbvision,
598 usbvision->curwidth,
599 usbvision->curheight);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300600 return 0;
601}
602
603static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
604{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300605 struct usb_usbvision *usbvision = video_drvdata(file);
606
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300607 usbvision->tvnorm_id=*id;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300608
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300609 call_all(usbvision, core, s_std, usbvision->tvnorm_id);
Thierry MERLE66a17872007-06-26 16:35:30 -0300610 /* propagate the change to the decoder */
611 usbvision_muxsel(usbvision, usbvision->ctl_input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300612
613 return 0;
614}
615
616static int vidioc_g_tuner (struct file *file, void *priv,
617 struct v4l2_tuner *vt)
618{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300619 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300620
621 if (!usbvision->have_tuner || vt->index) // Only tuner 0
622 return -EINVAL;
623 if(usbvision->radio) {
624 strcpy(vt->name, "Radio");
625 vt->type = V4L2_TUNER_RADIO;
626 } else {
627 strcpy(vt->name, "Television");
628 }
629 /* Let clients fill in the remainder of this struct */
Hans Verkuil1df79532009-02-21 18:11:31 -0300630 call_all(usbvision, tuner, g_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300631
632 return 0;
633}
634
635static int vidioc_s_tuner (struct file *file, void *priv,
636 struct v4l2_tuner *vt)
637{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300638 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300639
640 // Only no or one tuner for now
641 if (!usbvision->have_tuner || vt->index)
642 return -EINVAL;
643 /* let clients handle this */
Hans Verkuil1df79532009-02-21 18:11:31 -0300644 call_all(usbvision, tuner, s_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300645
646 return 0;
647}
648
649static int vidioc_g_frequency (struct file *file, void *priv,
650 struct v4l2_frequency *freq)
651{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300652 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300653
654 freq->tuner = 0; // Only one tuner
655 if(usbvision->radio) {
656 freq->type = V4L2_TUNER_RADIO;
657 } else {
658 freq->type = V4L2_TUNER_ANALOG_TV;
659 }
660 freq->frequency = usbvision->freq;
661
662 return 0;
663}
664
665static int vidioc_s_frequency (struct file *file, void *priv,
666 struct v4l2_frequency *freq)
667{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300668 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300669
670 // Only no or one tuner for now
671 if (!usbvision->have_tuner || freq->tuner)
672 return -EINVAL;
673
674 usbvision->freq = freq->frequency;
Hans Verkuil1df79532009-02-21 18:11:31 -0300675 call_all(usbvision, tuner, s_frequency, freq);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300676
677 return 0;
678}
679
680static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
681{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300682 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300683
Thierry MERLEc5f48362007-05-08 17:22:29 -0300684 if(usbvision->radio) {
685 strcpy(a->name,"Radio");
686 } else {
687 strcpy(a->name, "TV");
688 }
689
690 return 0;
691}
692
693static int vidioc_s_audio (struct file *file, void *fh,
694 struct v4l2_audio *a)
695{
696 if(a->index) {
697 return -EINVAL;
698 }
699
700 return 0;
701}
702
703static int vidioc_queryctrl (struct file *file, void *priv,
704 struct v4l2_queryctrl *ctrl)
705{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300706 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300707
Hans Verkuil1df79532009-02-21 18:11:31 -0300708 call_all(usbvision, core, queryctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300709
710 if (!ctrl->type)
711 return -EINVAL;
712
713 return 0;
714}
715
716static int vidioc_g_ctrl (struct file *file, void *priv,
717 struct v4l2_control *ctrl)
718{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300719 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil1df79532009-02-21 18:11:31 -0300720 call_all(usbvision, core, g_ctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300721
722 return 0;
723}
724
725static int vidioc_s_ctrl (struct file *file, void *priv,
726 struct v4l2_control *ctrl)
727{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300728 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil1df79532009-02-21 18:11:31 -0300729 call_all(usbvision, core, s_ctrl, ctrl);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300730
731 return 0;
732}
733
734static int vidioc_reqbufs (struct file *file,
735 void *priv, struct v4l2_requestbuffers *vr)
736{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300737 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300738 int ret;
739
740 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
741
742 /* Check input validity:
743 the user must do a VIDEO CAPTURE and MMAP method. */
Trent Piepho975f5762009-03-28 22:25:36 -0300744 if (vr->memory != V4L2_MEMORY_MMAP)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300745 return -EINVAL;
746
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300747 if(usbvision->streaming == stream_on) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300748 if ((ret = usbvision_stream_interrupt(usbvision)))
749 return ret;
750 }
751
752 usbvision_frames_free(usbvision);
753 usbvision_empty_framequeues(usbvision);
754 vr->count = usbvision_frames_alloc(usbvision,vr->count);
755
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300756 usbvision->cur_frame = NULL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300757
758 return 0;
759}
760
761static int vidioc_querybuf (struct file *file,
762 void *priv, struct v4l2_buffer *vb)
763{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300764 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300765 struct usbvision_frame *frame;
766
767 /* FIXME : must control
768 that buffers are mapped (VIDIOC_REQBUFS has been called) */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300769 if(vb->index>=usbvision->num_frames) {
770 return -EINVAL;
771 }
772 /* Updating the corresponding frame state */
773 vb->flags = 0;
774 frame = &usbvision->frame[vb->index];
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300775 if(frame->grabstate >= frame_state_ready)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300776 vb->flags |= V4L2_BUF_FLAG_QUEUED;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300777 if(frame->grabstate >= frame_state_done)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300778 vb->flags |= V4L2_BUF_FLAG_DONE;
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300779 if(frame->grabstate == frame_state_unused)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300780 vb->flags |= V4L2_BUF_FLAG_MAPPED;
781 vb->memory = V4L2_MEMORY_MMAP;
782
783 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
784
785 vb->memory = V4L2_MEMORY_MMAP;
786 vb->field = V4L2_FIELD_NONE;
787 vb->length = usbvision->curwidth*
788 usbvision->curheight*
789 usbvision->palette.bytes_per_pixel;
790 vb->timestamp = usbvision->frame[vb->index].timestamp;
791 vb->sequence = usbvision->frame[vb->index].sequence;
792 return 0;
793}
794
795static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
796{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300797 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300798 struct usbvision_frame *frame;
799 unsigned long lock_flags;
800
801 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300802 if(vb->index>=usbvision->num_frames) {
803 return -EINVAL;
804 }
805
806 frame = &usbvision->frame[vb->index];
807
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300808 if (frame->grabstate != frame_state_unused) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300809 return -EAGAIN;
810 }
811
812 /* Mark it as ready and enqueue frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300813 frame->grabstate = frame_state_ready;
814 frame->scanstate = scan_state_scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300815 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
816
817 vb->flags &= ~V4L2_BUF_FLAG_DONE;
818
819 /* set v4l2_format index */
820 frame->v4l2_format = usbvision->palette;
821
822 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
823 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
824 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
825
826 return 0;
827}
828
829static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
830{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300831 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300832 int ret;
833 struct usbvision_frame *f;
834 unsigned long lock_flags;
835
Thierry MERLEc5f48362007-05-08 17:22:29 -0300836 if (list_empty(&(usbvision->outqueue))) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300837 if (usbvision->streaming == stream_idle)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300838 return -EINVAL;
839 ret = wait_event_interruptible
840 (usbvision->wait_frame,
841 !list_empty(&(usbvision->outqueue)));
842 if (ret)
843 return ret;
844 }
845
846 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
847 f = list_entry(usbvision->outqueue.next,
848 struct usbvision_frame, frame);
849 list_del(usbvision->outqueue.next);
850 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
851
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300852 f->grabstate = frame_state_unused;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300853
854 vb->memory = V4L2_MEMORY_MMAP;
855 vb->flags = V4L2_BUF_FLAG_MAPPED |
856 V4L2_BUF_FLAG_QUEUED |
857 V4L2_BUF_FLAG_DONE;
858 vb->index = f->index;
859 vb->sequence = f->sequence;
860 vb->timestamp = f->timestamp;
861 vb->field = V4L2_FIELD_NONE;
862 vb->bytesused = f->scanlength;
863
864 return 0;
865}
866
867static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
868{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300869 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300870
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300871 usbvision->streaming = stream_on;
Hans Verkuil1df79532009-02-21 18:11:31 -0300872 call_all(usbvision, video, s_stream, 1);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300873
874 return 0;
875}
876
877static int vidioc_streamoff(struct file *file,
878 void *priv, enum v4l2_buf_type type)
879{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300880 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300881
882 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
883 return -EINVAL;
884
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300885 if(usbvision->streaming == stream_on) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300886 usbvision_stream_interrupt(usbvision);
887 /* Stop all video streamings */
Hans Verkuil1df79532009-02-21 18:11:31 -0300888 call_all(usbvision, video, s_stream, 0);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300889 }
890 usbvision_empty_framequeues(usbvision);
891
892 return 0;
893}
894
Hans Verkuil78b526a2008-05-28 12:16:41 -0300895static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300896 struct v4l2_fmtdesc *vfd)
897{
898 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
899 return -EINVAL;
900 }
Thierry MERLEc5f48362007-05-08 17:22:29 -0300901 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
902 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300903 return 0;
904}
905
Hans Verkuil78b526a2008-05-28 12:16:41 -0300906static int vidioc_g_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300907 struct v4l2_format *vf)
908{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300909 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300910 vf->fmt.pix.width = usbvision->curwidth;
911 vf->fmt.pix.height = usbvision->curheight;
912 vf->fmt.pix.pixelformat = usbvision->palette.format;
913 vf->fmt.pix.bytesperline =
914 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
915 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
916 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
917 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
918
919 return 0;
920}
921
Hans Verkuil78b526a2008-05-28 12:16:41 -0300922static int vidioc_try_fmt_vid_cap (struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300923 struct v4l2_format *vf)
924{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300925 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300926 int format_idx;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300927
928 /* Find requested format in available ones */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300929 for(format_idx=0;format_idx<USBVISION_SUPPORTED_PALETTES;format_idx++) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300930 if(vf->fmt.pix.pixelformat ==
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300931 usbvision_v4l2_format[format_idx].format) {
932 usbvision->palette = usbvision_v4l2_format[format_idx];
Thierry MERLEc5f48362007-05-08 17:22:29 -0300933 break;
934 }
935 }
936 /* robustness */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300937 if(format_idx == USBVISION_SUPPORTED_PALETTES) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300938 return -EINVAL;
939 }
940 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
941 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
942
943 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
944 usbvision->palette.bytes_per_pixel;
945 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
946
947 return 0;
948}
949
Hans Verkuil78b526a2008-05-28 12:16:41 -0300950static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300951 struct v4l2_format *vf)
952{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300953 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300954 int ret;
955
Hans Verkuil78b526a2008-05-28 12:16:41 -0300956 if( 0 != (ret=vidioc_try_fmt_vid_cap (file, priv, vf)) ) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300957 return ret;
958 }
959
960 /* stop io in case it is already in progress */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300961 if(usbvision->streaming == stream_on) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300962 if ((ret = usbvision_stream_interrupt(usbvision)))
963 return ret;
964 }
965 usbvision_frames_free(usbvision);
966 usbvision_empty_framequeues(usbvision);
967
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300968 usbvision->cur_frame = NULL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300969
970 /* by now we are committed to the new data... */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300971 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300972
973 return 0;
974}
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300975
Al Viro8a5ab412007-02-09 16:38:20 +0000976static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300977 size_t count, loff_t *ppos)
978{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300979 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300980 int noblock = file->f_flags & O_NONBLOCK;
981 unsigned long lock_flags;
982
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300983 int ret,i;
984 struct usbvision_frame *frame;
985
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300986 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300987 (unsigned long)count, noblock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300988
989 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
990 return -EFAULT;
991
Thierry MERLEc5f48362007-05-08 17:22:29 -0300992 /* This entry point is compatible with the mmap routines
993 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
994 to get frames or call read on the device. */
Thierry MERLE6f78e182007-02-07 10:13:11 -0300995 if(!usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300996 /* First, allocate some frames to work with
997 if this has not been done with VIDIOC_REQBUF */
Thierry MERLE6f78e182007-02-07 10:13:11 -0300998 usbvision_frames_free(usbvision);
999 usbvision_empty_framequeues(usbvision);
1000 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1001 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001002
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001003 if(usbvision->streaming != stream_on) {
Thierry MERLE6f78e182007-02-07 10:13:11 -03001004 /* no stream is running, make it running ! */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001005 usbvision->streaming = stream_on;
Hans Verkuil1df79532009-02-21 18:11:31 -03001006 call_all(usbvision, video, s_stream, 1);
Thierry MERLE6f78e182007-02-07 10:13:11 -03001007 }
1008
Thierry MERLEc5f48362007-05-08 17:22:29 -03001009 /* Then, enqueue as many frames as possible
1010 (like a user of VIDIOC_QBUF would do) */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001011 for(i=0;i<usbvision->num_frames;i++) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001012 frame = &usbvision->frame[i];
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001013 if(frame->grabstate == frame_state_unused) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001014 /* Mark it as ready and enqueue frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001015 frame->grabstate = frame_state_ready;
1016 frame->scanstate = scan_state_scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001017 /* Accumulated in usbvision_parse_data() */
1018 frame->scanlength = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001019
1020 /* set v4l2_format index */
1021 frame->v4l2_format = usbvision->palette;
1022
1023 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1024 list_add_tail(&frame->frame, &usbvision->inqueue);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001025 spin_unlock_irqrestore(&usbvision->queue_lock,
1026 lock_flags);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001027 }
1028 }
1029
1030 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1031 if (list_empty(&(usbvision->outqueue))) {
1032 if(noblock)
1033 return -EAGAIN;
1034
1035 ret = wait_event_interruptible
1036 (usbvision->wait_frame,
1037 !list_empty(&(usbvision->outqueue)));
1038 if (ret)
1039 return ret;
1040 }
1041
1042 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1043 frame = list_entry(usbvision->outqueue.next,
1044 struct usbvision_frame, frame);
1045 list_del(usbvision->outqueue.next);
1046 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1047
1048 /* An error returns an empty frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001049 if (frame->grabstate == frame_state_error) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001050 frame->bytes_read = 0;
1051 return 0;
1052 }
1053
Thierry MERLEc5f48362007-05-08 17:22:29 -03001054 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001055 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001056 frame->index, frame->bytes_read, frame->scanlength);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001057
1058 /* copy bytes to user space; we allow for partials reads */
1059 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1060 count = frame->scanlength - frame->bytes_read;
1061
1062 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1063 return -EFAULT;
1064 }
1065
1066 frame->bytes_read += count;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001067 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001068 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001069 (unsigned long)count, frame->bytes_read);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001070
Thierry MERLEc5f48362007-05-08 17:22:29 -03001071 /* For now, forget the frame if it has not been read in one shot. */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001072/* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1073 frame->bytes_read = 0;
1074
1075 /* Mark it as available to be used again. */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001076 frame->grabstate = frame_state_unused;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001077/* } */
1078
1079 return count;
1080}
1081
1082static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1083{
1084 unsigned long size = vma->vm_end - vma->vm_start,
1085 start = vma->vm_start;
1086 void *pos;
1087 u32 i;
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001088 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001089
Thierry MERLE6f78e182007-02-07 10:13:11 -03001090 PDEBUG(DBG_MMAP, "mmap");
1091
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001092 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001093 return -EFAULT;
1094 }
1095
1096 if (!(vma->vm_flags & VM_WRITE) ||
Thierry MERLE6f78e182007-02-07 10:13:11 -03001097 size != PAGE_ALIGN(usbvision->max_frame_size)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001098 return -EINVAL;
1099 }
1100
Thierry MERLE6f78e182007-02-07 10:13:11 -03001101 for (i = 0; i < usbvision->num_frames; i++) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001102 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1103 vma->vm_pgoff)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001104 break;
1105 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001106 if (i == usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001107 PDEBUG(DBG_MMAP,
1108 "mmap: user supplied mapping address is out of range");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001109 return -EINVAL;
1110 }
1111
1112 /* VM_IO is eventually going to replace PageReserved altogether */
1113 vma->vm_flags |= VM_IO;
1114 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1115
1116 pos = usbvision->frame[i].data;
1117 while (size > 0) {
1118
1119 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001120 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001121 return -EAGAIN;
1122 }
1123 start += PAGE_SIZE;
1124 pos += PAGE_SIZE;
1125 size -= PAGE_SIZE;
1126 }
1127
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001128 return 0;
1129}
1130
1131
1132/*
1133 * Here comes the stuff for radio on usbvision based devices
1134 *
1135 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001136static int usbvision_radio_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001137{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001138 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001139 int err_code = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001140
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001141 PDEBUG(DBG_IO, "%s:", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001142
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001143 if (usbvision->user) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001144 dev_err(&usbvision->rdev->dev,
1145 "%s: Someone tried to open an already opened USBVision Radio!\n",
1146 __func__);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001147 err_code = -EBUSY;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001148 }
1149 else {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001150 if(power_on_at_open) {
1151 usbvision_reset_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001152 if (usbvision->power == 0) {
1153 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001154 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001155 }
1156 }
1157
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001158 /* Alternate interface 1 is is the biggest frame size */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001159 err_code = usbvision_set_alternate(usbvision);
1160 if (err_code < 0) {
1161 usbvision->last_error = err_code;
1162 err_code = -EBUSY;
Andrew Morton544e6172007-12-12 18:25:23 -03001163 goto out;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001164 }
1165
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001166 // If so far no errors then we shall start the radio
1167 usbvision->radio = 1;
Hans Verkuil1df79532009-02-21 18:11:31 -03001168 call_all(usbvision, tuner, s_radio);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001169 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1170 usbvision->user++;
1171 }
1172
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001173 if (err_code) {
1174 if (power_on_at_open) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001175 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001176 usbvision_power_off(usbvision);
1177 usbvision->initialized = 0;
1178 }
1179 }
Andrew Morton544e6172007-12-12 18:25:23 -03001180out:
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001181 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001182}
1183
1184
Hans Verkuilbec43662008-12-30 06:58:20 -03001185static int usbvision_radio_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001186{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001187 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001188 int err_code = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001189
1190 PDEBUG(DBG_IO, "");
1191
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001192 /* Set packet size to 0 */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001193 usbvision->iface_alt=0;
1194 err_code = usb_set_interface(usbvision->dev, usbvision->iface,
1195 usbvision->iface_alt);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001196
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001197 usbvision_audio_off(usbvision);
1198 usbvision->radio=0;
1199 usbvision->user--;
1200
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001201 if (power_on_at_open) {
1202 usbvision_set_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001203 usbvision->initialized = 0;
1204 }
1205
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001206 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001207 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001208 usbvision_release(usbvision);
1209 }
1210
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001211 PDEBUG(DBG_IO, "success");
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001212 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001213}
1214
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001215//
1216// Video registration stuff
1217//
1218
1219// Video template
Hans Verkuilbec43662008-12-30 06:58:20 -03001220static const struct v4l2_file_operations usbvision_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001221 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001222 .open = usbvision_v4l2_open,
1223 .release = usbvision_v4l2_close,
1224 .read = usbvision_v4l2_read,
1225 .mmap = usbvision_v4l2_mmap,
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001226 .unlocked_ioctl = video_ioctl2,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001227/* .poll = video_poll, */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001228};
Hans Verkuila3998102008-07-21 02:57:38 -03001229
1230static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001231 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001232 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1233 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1234 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1235 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001236 .vidioc_reqbufs = vidioc_reqbufs,
1237 .vidioc_querybuf = vidioc_querybuf,
1238 .vidioc_qbuf = vidioc_qbuf,
1239 .vidioc_dqbuf = vidioc_dqbuf,
1240 .vidioc_s_std = vidioc_s_std,
1241 .vidioc_enum_input = vidioc_enum_input,
1242 .vidioc_g_input = vidioc_g_input,
1243 .vidioc_s_input = vidioc_s_input,
1244 .vidioc_queryctrl = vidioc_queryctrl,
1245 .vidioc_g_audio = vidioc_g_audio,
Al Viroed4d6372007-09-26 01:53:42 +01001246 .vidioc_s_audio = vidioc_s_audio,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001247 .vidioc_g_ctrl = vidioc_g_ctrl,
1248 .vidioc_s_ctrl = vidioc_s_ctrl,
1249 .vidioc_streamon = vidioc_streamon,
1250 .vidioc_streamoff = vidioc_streamoff,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001251 .vidioc_g_tuner = vidioc_g_tuner,
1252 .vidioc_s_tuner = vidioc_s_tuner,
1253 .vidioc_g_frequency = vidioc_g_frequency,
1254 .vidioc_s_frequency = vidioc_s_frequency,
1255#ifdef CONFIG_VIDEO_ADV_DEBUG
1256 .vidioc_g_register = vidioc_g_register,
1257 .vidioc_s_register = vidioc_s_register,
1258#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001259};
1260
1261static struct video_device usbvision_video_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001262 .fops = &usbvision_fops,
1263 .ioctl_ops = &usbvision_ioctl_ops,
1264 .name = "usbvision-video",
1265 .release = video_device_release,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001266 .tvnorms = USBVISION_NORMS,
1267 .current_norm = V4L2_STD_PAL
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001268};
1269
1270
1271// Radio template
Hans Verkuilbec43662008-12-30 06:58:20 -03001272static const struct v4l2_file_operations usbvision_radio_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001273 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001274 .open = usbvision_radio_open,
1275 .release = usbvision_radio_close,
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001276 .unlocked_ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001277};
1278
Hans Verkuila3998102008-07-21 02:57:38 -03001279static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001280 .vidioc_querycap = vidioc_querycap,
1281 .vidioc_enum_input = vidioc_enum_input,
1282 .vidioc_g_input = vidioc_g_input,
1283 .vidioc_s_input = vidioc_s_input,
1284 .vidioc_queryctrl = vidioc_queryctrl,
1285 .vidioc_g_audio = vidioc_g_audio,
Al Viroed4d6372007-09-26 01:53:42 +01001286 .vidioc_s_audio = vidioc_s_audio,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001287 .vidioc_g_ctrl = vidioc_g_ctrl,
1288 .vidioc_s_ctrl = vidioc_s_ctrl,
1289 .vidioc_g_tuner = vidioc_g_tuner,
1290 .vidioc_s_tuner = vidioc_s_tuner,
1291 .vidioc_g_frequency = vidioc_g_frequency,
1292 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuila3998102008-07-21 02:57:38 -03001293};
1294
1295static struct video_device usbvision_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001296 .fops = &usbvision_radio_fops,
1297 .name = "usbvision-radio",
1298 .release = video_device_release,
Hans Verkuila3998102008-07-21 02:57:38 -03001299 .ioctl_ops = &usbvision_radio_ioctl_ops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001300
Thierry MERLEc5f48362007-05-08 17:22:29 -03001301 .tvnorms = USBVISION_NORMS,
1302 .current_norm = V4L2_STD_PAL
1303};
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001304
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001305
1306static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1307 struct video_device *vdev_template,
1308 char *name)
1309{
1310 struct usb_device *usb_dev = usbvision->dev;
1311 struct video_device *vdev;
1312
1313 if (usb_dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001314 dev_err(&usbvision->dev->dev,
1315 "%s: usbvision->dev is not set\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001316 return NULL;
1317 }
1318
1319 vdev = video_device_alloc();
1320 if (NULL == vdev) {
1321 return NULL;
1322 }
1323 *vdev = *vdev_template;
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001324 vdev->lock = &usbvision->v4l2_lock;
Hans Verkuil1df79532009-02-21 18:11:31 -03001325 vdev->v4l2_dev = &usbvision->v4l2_dev;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001326 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1327 video_set_drvdata(vdev, usbvision);
1328 return vdev;
1329}
1330
1331// unregister video4linux devices
1332static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1333{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001334 // Radio Device:
1335 if (usbvision->rdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001336 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1337 video_device_node_name(usbvision->rdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001338 if (video_is_registered(usbvision->rdev)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001339 video_unregister_device(usbvision->rdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001340 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001341 video_device_release(usbvision->rdev);
1342 }
1343 usbvision->rdev = NULL;
1344 }
1345
1346 // Video Device:
1347 if (usbvision->vdev) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001348 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1349 video_device_node_name(usbvision->vdev));
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001350 if (video_is_registered(usbvision->vdev)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001351 video_unregister_device(usbvision->vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001352 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001353 video_device_release(usbvision->vdev);
1354 }
1355 usbvision->vdev = NULL;
1356 }
1357}
1358
1359// register video4linux devices
1360static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1361{
1362 // Video Device:
Thierry MERLEc5f48362007-05-08 17:22:29 -03001363 usbvision->vdev = usbvision_vdev_init(usbvision,
1364 &usbvision_video_template,
1365 "USBVision Video");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001366 if (usbvision->vdev == NULL) {
1367 goto err_exit;
1368 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001369 if (video_register_device(usbvision->vdev,
1370 VFL_TYPE_GRABBER,
1371 video_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001372 goto err_exit;
1373 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001374 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1375 usbvision->nr, video_device_node_name(usbvision->vdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001376
1377 // Radio Device:
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001378 if (usbvision_device_data[usbvision->dev_model].radio) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001379 // usbvision has radio
Thierry MERLEc5f48362007-05-08 17:22:29 -03001380 usbvision->rdev = usbvision_vdev_init(usbvision,
1381 &usbvision_radio_template,
1382 "USBVision Radio");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001383 if (usbvision->rdev == NULL) {
1384 goto err_exit;
1385 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001386 if (video_register_device(usbvision->rdev,
1387 VFL_TYPE_RADIO,
1388 radio_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001389 goto err_exit;
1390 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001391 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1392 usbvision->nr, video_device_node_name(usbvision->rdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001393 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001394 // all done
1395 return 0;
1396
1397 err_exit:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001398 dev_err(&usbvision->dev->dev,
1399 "USBVision[%d]: video_register_device() failed\n",
1400 usbvision->nr);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001401 usbvision_unregister_video(usbvision);
1402 return -1;
1403}
1404
1405/*
1406 * usbvision_alloc()
1407 *
Thierry MERLEc5f48362007-05-08 17:22:29 -03001408 * This code allocates the struct usb_usbvision.
1409 * It is filled with default values.
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001410 *
1411 * Returns NULL on error, a pointer to usb_usbvision else.
1412 *
1413 */
Janne Grunaua8784402009-04-01 08:46:00 -03001414static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1415 struct usb_interface *intf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001416{
1417 struct usb_usbvision *usbvision;
1418
Hans Verkuil1df79532009-02-21 18:11:31 -03001419 usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1420 if (usbvision == NULL)
1421 return NULL;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001422
1423 usbvision->dev = dev;
Janne Grunaua8784402009-04-01 08:46:00 -03001424 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
Hans Verkuil1df79532009-02-21 18:11:31 -03001425 goto err_free;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001426
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001427 mutex_init(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001428
1429 // prepare control urb for control messages during interrupts
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001430 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1431 if (usbvision->ctrl_urb == NULL)
Hans Verkuil1df79532009-02-21 18:11:31 -03001432 goto err_unreg;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001433 init_waitqueue_head(&usbvision->ctrl_urb_wq);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001434
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001435 usbvision_init_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001436
1437 return usbvision;
1438
Hans Verkuil1df79532009-02-21 18:11:31 -03001439err_unreg:
1440 v4l2_device_unregister(&usbvision->v4l2_dev);
1441err_free:
1442 kfree(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001443 return NULL;
1444}
1445
1446/*
1447 * usbvision_release()
1448 *
1449 * This code does final release of struct usb_usbvision. This happens
1450 * after the device is disconnected -and- all clients closed their files.
1451 *
1452 */
1453static void usbvision_release(struct usb_usbvision *usbvision)
1454{
1455 PDEBUG(DBG_PROBE, "");
1456
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001457 usbvision_reset_power_off_timer(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001458
1459 usbvision->initialized = 0;
1460
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001461 usbvision_remove_sysfs(usbvision->vdev);
1462 usbvision_unregister_video(usbvision);
1463
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001464 if (usbvision->ctrl_urb) {
1465 usb_free_urb(usbvision->ctrl_urb);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001466 }
1467
Hans Verkuil1df79532009-02-21 18:11:31 -03001468 v4l2_device_unregister(&usbvision->v4l2_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001469 kfree(usbvision);
1470
1471 PDEBUG(DBG_PROBE, "success");
1472}
1473
1474
Thierry MERLEc5f48362007-05-08 17:22:29 -03001475/*********************** usb interface **********************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001476
1477static void usbvision_configure_video(struct usb_usbvision *usbvision)
1478{
Thierry MERLEc5f48362007-05-08 17:22:29 -03001479 int model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001480
1481 if (usbvision == NULL)
1482 return;
1483
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001484 model = usbvision->dev_model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001485 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1486
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001487 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1488 usbvision->vin_reg2_preset =
1489 usbvision_device_data[usbvision->dev_model].vin_reg2;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001490 } else {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001491 usbvision->vin_reg2_preset = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001492 }
1493
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001494 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001495
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001496 usbvision->video_inputs = usbvision_device_data[model].video_channels;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001497 usbvision->ctl_input = 0;
1498
1499 /* This should be here to make i2c clients to be able to register */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001500 /* first switch off audio */
1501 usbvision_audio_off(usbvision);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001502 if (!power_on_at_open) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001503 /* and then power up the noisy tuner */
1504 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001505 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001506 }
1507}
1508
1509/*
1510 * usbvision_probe()
1511 *
1512 * This procedure queries device descriptor and accepts the interface
1513 * if it looks like USBVISION video device
1514 *
1515 */
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001516static int __devinit usbvision_probe(struct usb_interface *intf,
1517 const struct usb_device_id *devid)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001518{
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001519 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1520 struct usb_interface *uif;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001521 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1522 const struct usb_host_interface *interface;
1523 struct usb_usbvision *usbvision = NULL;
1524 const struct usb_endpoint_descriptor *endpoint;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001525 int model,i;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001526
1527 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001528 dev->descriptor.idVendor,
1529 dev->descriptor.idProduct, ifnum);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001530
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001531 model = devid->driver_info;
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001532 if ( (model<0) || (model>=usbvision_device_data_size) ) {
Thierry MERLE672d0132007-04-14 17:53:55 -03001533 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001534 return -ENODEV;
1535 }
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001536 printk(KERN_INFO "%s: %s found\n", __func__,
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001537 usbvision_device_data[model].model_string);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001538
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001539 if (usbvision_device_data[model].interface >= 0) {
1540 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
Thierry MERLEc5f48362007-05-08 17:22:29 -03001541 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001542 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1543 }
1544 endpoint = &interface->endpoint[1].desc;
Julia Lawall2230c3c2009-01-03 16:53:10 -03001545 if (!usb_endpoint_xfer_isoc(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001546 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001547 __func__, ifnum);
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001548 dev_err(&intf->dev, "%s: Endpoint attributes %d",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001549 __func__, endpoint->bmAttributes);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001550 return -ENODEV;
1551 }
Julia Lawall13417982008-12-29 21:49:22 -03001552 if (usb_endpoint_dir_out(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001553 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001554 __func__, ifnum);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001555 return -ENODEV;
1556 }
1557
Janne Grunaua8784402009-04-01 08:46:00 -03001558 usbvision = usbvision_alloc(dev, intf);
1559 if (usbvision == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001560 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001561 return -ENOMEM;
1562 }
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001563
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001564 if (dev->descriptor.bNumConfigurations > 1) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001565 usbvision->bridge_type = BRIDGE_NT1004;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001566 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001567 usbvision->bridge_type = BRIDGE_NT1005;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001568 } else {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001569 usbvision->bridge_type = BRIDGE_NT1003;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001570 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001571 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001572
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001573 /* compute alternate max packet sizes */
1574 uif = dev->actconfig->interface[0];
1575
1576 usbvision->num_alt=uif->num_altsetting;
1577 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1578 usbvision->alt_max_pkt_size = kmalloc(32*
1579 usbvision->num_alt,GFP_KERNEL);
1580 if (usbvision->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001581 dev_err(&intf->dev, "usbvision: out of memory!\n");
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001582 return -ENOMEM;
1583 }
1584
1585 for (i = 0; i < usbvision->num_alt ; i++) {
1586 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1587 wMaxPacketSize);
1588 usbvision->alt_max_pkt_size[i] =
1589 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1590 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1591 usbvision->alt_max_pkt_size[i]);
1592 }
1593
1594
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001595 usbvision->nr = usbvision_nr++;
1596
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001597 usbvision->have_tuner = usbvision_device_data[model].tuner;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001598 if (usbvision->have_tuner) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001599 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001600 }
1601
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001602 usbvision->dev_model = model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001603 usbvision->remove_pending = 0;
1604 usbvision->iface = ifnum;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001605 usbvision->iface_alt = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001606 usbvision->video_endp = endpoint->bEndpointAddress;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001607 usbvision->isoc_packet_size = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001608 usbvision->usb_bandwidth = 0;
1609 usbvision->user = 0;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001610 usbvision->streaming = stream_off;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001611 usbvision_configure_video(usbvision);
Hans Verkuil84034722010-09-17 15:07:28 -03001612 usbvision_register_video(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001613
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001614 usbvision_create_sysfs(usbvision->vdev);
1615
1616 PDEBUG(DBG_PROBE, "success");
1617 return 0;
1618}
1619
1620
1621/*
1622 * usbvision_disconnect()
1623 *
1624 * This procedure stops all driver activity, deallocates interface-private
1625 * structure (pointed by 'ptr') and after that driver should be removable
1626 * with no ill consequences.
1627 *
1628 */
1629static void __devexit usbvision_disconnect(struct usb_interface *intf)
1630{
Hans Verkuilaeb506a2010-05-02 09:00:13 -03001631 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001632
1633 PDEBUG(DBG_PROBE, "");
1634
1635 if (usbvision == NULL) {
Julia Lawall42020662010-05-27 09:36:45 -03001636 pr_err("%s: usb_get_intfdata() failed\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001637 return;
1638 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001639
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001640 mutex_lock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001641
1642 // At this time we ask to cancel outstanding URBs
1643 usbvision_stop_isoc(usbvision);
1644
Hans Verkuilf1ba28c2009-03-14 12:27:01 -03001645 v4l2_device_disconnect(&usbvision->v4l2_dev);
1646
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001647 if (usbvision->power) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001648 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001649 usbvision_power_off(usbvision);
1650 }
1651 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1652
1653 usb_put_dev(usbvision->dev);
1654 usbvision->dev = NULL; // USB device is no more
1655
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001656 mutex_unlock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001657
1658 if (usbvision->user) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001659 printk(KERN_INFO "%s: In use, disconnect pending\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001660 __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001661 wake_up_interruptible(&usbvision->wait_frame);
1662 wake_up_interruptible(&usbvision->wait_stream);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001663 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001664 usbvision_release(usbvision);
1665 }
1666
1667 PDEBUG(DBG_PROBE, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001668}
1669
1670static struct usb_driver usbvision_driver = {
1671 .name = "usbvision",
1672 .id_table = usbvision_table,
1673 .probe = usbvision_probe,
Jean Delvaree36bc312009-06-04 11:07:16 -03001674 .disconnect = __devexit_p(usbvision_disconnect),
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001675};
1676
1677/*
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001678 * usbvision_init()
1679 *
1680 * This code is run to initialize the driver.
1681 *
1682 */
1683static int __init usbvision_init(void)
1684{
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001685 int err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001686
1687 PDEBUG(DBG_PROBE, "");
1688
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001689 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1690 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
Thierry MERLEc876a342006-12-09 16:42:54 -03001691 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001692
1693 /* disable planar mode support unless compression enabled */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001694 if (isoc_mode != ISOC_MODE_COMPRESS ) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001695 // FIXME : not the right way to set supported flag
1696 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1697 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1698 }
1699
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001700 err_code = usb_register(&usbvision_driver);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001701
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001702 if (err_code == 0) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001703 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001704 PDEBUG(DBG_PROBE, "success");
1705 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001706 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001707}
1708
1709static void __exit usbvision_exit(void)
1710{
1711 PDEBUG(DBG_PROBE, "");
1712
1713 usb_deregister(&usbvision_driver);
1714 PDEBUG(DBG_PROBE, "success");
1715}
1716
1717module_init(usbvision_init);
1718module_exit(usbvision_exit);
1719
1720/*
1721 * Overrides for Emacs so that we follow Linus's tabbing style.
1722 * ---------------------------------------------------------------------------
1723 * Local variables:
1724 * c-basic-offset: 8
1725 * End:
1726 */