blob: f526712a64351acbcac783c8dfa3b99d56b5c83c [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
48#include <linux/kernel.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030049#include <linux/list.h>
50#include <linux/timer.h>
51#include <linux/slab.h>
52#include <linux/mm.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030053#include <linux/highmem.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030054#include <linux/vmalloc.h>
55#include <linux/module.h>
56#include <linux/init.h>
57#include <linux/spinlock.h>
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030058#include <linux/io.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030059#include <linux/videodev2.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030060#include <linux/i2c.h>
61
62#include <media/saa7115.h>
63#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030064#include <media/v4l2-ioctl.h>
Hans Verkuilfd958702015-07-20 09:59:29 -030065#include <media/v4l2-event.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
Joe Perches85ee7a12011-04-23 20:38:19 -070073#define DRIVER_AUTHOR \
74 "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"
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030080#define USBVISION_VERSION_STRING "0.9.11"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030081
82#define ENABLE_HEXDUMP 0 /* Enable if you need it */
83
84
Thierry MERLE483dfdb2006-12-04 08:31:45 -030085#ifdef USBVISION_DEBUG
Thierry MERLEdf18c312008-04-04 21:00:57 -030086 #define PDEBUG(level, fmt, args...) { \
Thierry MERLEc5f48362007-05-08 17:22:29 -030087 if (video_debug & (level)) \
Greg Kroah-Hartmana482f322008-10-10 05:08:23 -030088 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
89 __func__, __LINE__ , ## args); \
Thierry MERLEdf18c312008-04-04 21:00:57 -030090 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -030091#else
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030092 #define PDEBUG(level, fmt, args...) do {} while (0)
Thierry MERLE483dfdb2006-12-04 08:31:45 -030093#endif
94
Hans Verkuil6d6a48e2010-12-29 13:53:21 -030095#define DBG_IO (1 << 1)
96#define DBG_PROBE (1 << 2)
97#define DBG_MMAP (1 << 3)
Thierry MERLE483dfdb2006-12-04 08:31:45 -030098
Hans Verkuil52cb0bf2010-12-19 20:33:51 -030099/* String operations */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300100#define rmspace(str) while (*str == ' ') str++;
101#define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300102
103
Thierry MERLEc5f48362007-05-08 17:22:29 -0300104/* sequential number of usbvision device */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300105static int usbvision_nr;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300106
107static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
108 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
109 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
110 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
111 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
112 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
113 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300114 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300115 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
116};
117
Thierry MERLEc5f48362007-05-08 17:22:29 -0300118/* Function prototypes */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300119static void usbvision_release(struct usb_usbvision *usbvision);
120
Joe Perchesc84e6032008-02-03 17:18:59 +0200121/* Default initialization of device driver parameters */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300122/* Set the default format for ISOC endpoint */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300123static int isoc_mode = ISOC_MODE_COMPRESS;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300124/* Set the default Debug Mode of the device driver */
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -0300125static int video_debug;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300126/* Sequential Number of Video Device */
127static int video_nr = -1;
128/* Sequential Number of Radio Device */
129static int radio_nr = -1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300130
Thierry MERLEc5f48362007-05-08 17:22:29 -0300131/* Grab parameters for the device driver */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300132
Thierry MERLEc5f48362007-05-08 17:22:29 -0300133/* Showing parameters under SYSFS */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300134module_param(isoc_mode, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300135module_param(video_debug, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300136module_param(video_nr, int, 0444);
137module_param(radio_nr, int, 0444);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300138
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300139MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300140MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300141MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
142MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300143
144
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300145/* Misc stuff */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300146MODULE_AUTHOR(DRIVER_AUTHOR);
147MODULE_DESCRIPTION(DRIVER_DESC);
148MODULE_LICENSE(DRIVER_LICENSE);
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -0300149MODULE_VERSION(USBVISION_VERSION_STRING);
150MODULE_ALIAS(DRIVER_ALIAS);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300151
152
Thierry MERLEc5f48362007-05-08 17:22:29 -0300153/*****************************************************************************/
154/* SYSFS Code - Copied from the stv680.c usb module. */
155/* Device information is located at /sys/class/video4linux/video0 */
156/* Device parameters information is located at /sys/module/usbvision */
157/* Device USB Information is located at */
158/* /sys/bus/usb/drivers/USBVision Video Grabber */
159/*****************************************************************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300160
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300161#define YES_NO(x) ((x) ? "Yes" : "No")
162
Kay Sievers54bd5b62007-10-08 16:26:13 -0300163static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300164{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300165 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300166 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300167 return video_get_drvdata(vdev);
168}
169
Kay Sievers54bd5b62007-10-08 16:26:13 -0300170static ssize_t show_version(struct device *cd,
171 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300172{
173 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
174}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300175static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300176
Kay Sievers54bd5b62007-10-08 16:26:13 -0300177static ssize_t show_model(struct device *cd,
178 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300179{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300180 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300181 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300182 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300183 return sprintf(buf, "%s\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300184 usbvision_device_data[usbvision->dev_model].model_string);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300185}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300186static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300187
Kay Sievers54bd5b62007-10-08 16:26:13 -0300188static ssize_t show_hue(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);
194 struct v4l2_control ctrl;
195 ctrl.id = V4L2_CID_HUE;
196 ctrl.value = 0;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300197 if (usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300198 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300199 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300200}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300201static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300202
Kay Sievers54bd5b62007-10-08 16:26:13 -0300203static ssize_t show_contrast(struct device *cd,
204 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300205{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300206 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300207 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300208 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
209 struct v4l2_control ctrl;
210 ctrl.id = V4L2_CID_CONTRAST;
211 ctrl.value = 0;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300212 if (usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300213 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300214 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300215}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300216static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300217
Kay Sievers54bd5b62007-10-08 16:26:13 -0300218static ssize_t show_brightness(struct device *cd,
219 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300220{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300221 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300222 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300223 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
224 struct v4l2_control ctrl;
225 ctrl.id = V4L2_CID_BRIGHTNESS;
226 ctrl.value = 0;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300227 if (usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300228 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300229 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300230}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300231static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300232
Kay Sievers54bd5b62007-10-08 16:26:13 -0300233static ssize_t show_saturation(struct device *cd,
234 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300235{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300236 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300237 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300238 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
239 struct v4l2_control ctrl;
240 ctrl.id = V4L2_CID_SATURATION;
241 ctrl.value = 0;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300242 if (usbvision->user)
Hans Verkuil1df79532009-02-21 18:11:31 -0300243 call_all(usbvision, core, g_ctrl, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300244 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300245}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300246static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300247
Kay Sievers54bd5b62007-10-08 16:26:13 -0300248static ssize_t show_streaming(struct device *cd,
249 struct device_attribute *attr, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300250{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300251 struct video_device *vdev =
Hans Verkuil22a04f12008-07-20 06:35:02 -0300252 container_of(cd, struct video_device, dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300253 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300254 return sprintf(buf, "%s\n",
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300255 YES_NO(usbvision->streaming == stream_on ? 1 : 0));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300256}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300257static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300258
Kay Sievers54bd5b62007-10-08 16:26:13 -0300259static ssize_t show_compression(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 Verkuil6d6a48e2010-12-29 13:53:21 -0300266 YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300267}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300268static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300269
Kay Sievers54bd5b62007-10-08 16:26:13 -0300270static ssize_t show_device_bridge(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);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300276 return sprintf(buf, "%d\n", usbvision->bridge_type);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300277}
Kay Sievers54bd5b62007-10-08 16:26:13 -0300278static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300279
280static void usbvision_create_sysfs(struct video_device *vdev)
281{
282 int res;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300283
Dwaine Gardened00b412006-12-27 10:23:28 -0200284 if (!vdev)
285 return;
286 do {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300287 res = device_create_file(&vdev->dev, &dev_attr_version);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300288 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200289 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300290 res = device_create_file(&vdev->dev, &dev_attr_model);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300291 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200292 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300293 res = device_create_file(&vdev->dev, &dev_attr_hue);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300294 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200295 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300296 res = device_create_file(&vdev->dev, &dev_attr_contrast);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300297 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200298 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300299 res = device_create_file(&vdev->dev, &dev_attr_brightness);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300300 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200301 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300302 res = device_create_file(&vdev->dev, &dev_attr_saturation);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300303 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200304 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300305 res = device_create_file(&vdev->dev, &dev_attr_streaming);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300306 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200307 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300308 res = device_create_file(&vdev->dev, &dev_attr_compression);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300309 if (res < 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200310 break;
Hans Verkuil22a04f12008-07-20 06:35:02 -0300311 res = device_create_file(&vdev->dev, &dev_attr_bridge);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300312 if (res >= 0)
Dwaine Gardened00b412006-12-27 10:23:28 -0200313 return;
314 } while (0);
315
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300316 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300317}
318
319static void usbvision_remove_sysfs(struct video_device *vdev)
320{
321 if (vdev) {
Hans Verkuil22a04f12008-07-20 06:35:02 -0300322 device_remove_file(&vdev->dev, &dev_attr_version);
323 device_remove_file(&vdev->dev, &dev_attr_model);
324 device_remove_file(&vdev->dev, &dev_attr_hue);
325 device_remove_file(&vdev->dev, &dev_attr_contrast);
326 device_remove_file(&vdev->dev, &dev_attr_brightness);
327 device_remove_file(&vdev->dev, &dev_attr_saturation);
328 device_remove_file(&vdev->dev, &dev_attr_streaming);
329 device_remove_file(&vdev->dev, &dev_attr_compression);
330 device_remove_file(&vdev->dev, &dev_attr_bridge);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300331 }
332}
333
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300334/*
335 * usbvision_open()
336 *
337 * This is part of Video 4 Linux API. The driver can be opened by one
338 * client only (checks internal counter 'usbvision->user'). The procedure
339 * then allocates buffers needed for video processing.
340 *
341 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300342static int usbvision_v4l2_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300343{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300344 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300345 int err_code = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300346
347 PDEBUG(DBG_IO, "open");
348
Hans Verkuil4d345702012-06-23 08:03:50 -0300349 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
350 return -ERESTARTSYS;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300351
Hans Verkuil62e25942015-07-20 09:59:28 -0300352 if (usbvision->user) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300353 err_code = -EBUSY;
Hans Verkuil62e25942015-07-20 09:59:28 -0300354 } else {
Hans Verkuilfd958702015-07-20 09:59:29 -0300355 err_code = v4l2_fh_open(file);
356 if (err_code)
357 goto unlock;
358
Thierry MERLE6f78e182007-02-07 10:13:11 -0300359 /* Allocate memory for the scratch ring buffer */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300360 err_code = usbvision_scratch_alloc(usbvision);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300361 if (isoc_mode == ISOC_MODE_COMPRESS) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300362 /* Allocate intermediate decompression buffers
363 only if needed */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300364 err_code = usbvision_decompress_alloc(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300365 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300366 if (err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300367 /* Deallocate all buffers if trouble */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300368 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300369 usbvision_decompress_free(usbvision);
370 }
371 }
372
373 /* If so far no errors then we shall start the camera */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300374 if (!err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300375 /* Send init sequence only once, it's large! */
376 if (!usbvision->initialized) {
377 int setup_ok = 0;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300378 setup_ok = usbvision_setup(usbvision, isoc_mode);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300379 if (setup_ok)
380 usbvision->initialized = 1;
381 else
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300382 err_code = -EBUSY;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300383 }
384
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300385 if (!err_code) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300386 usbvision_begin_streaming(usbvision);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300387 err_code = usbvision_init_isoc(usbvision);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300388 /* device must be initialized before isoc transfer */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300389 usbvision_muxsel(usbvision, 0);
Hans Verkuil62e25942015-07-20 09:59:28 -0300390
391 /* prepare queues */
392 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300393 usbvision->user++;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300394 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300395 }
396
Hans Verkuilfd958702015-07-20 09:59:29 -0300397unlock:
Hans Verkuil4d345702012-06-23 08:03:50 -0300398 mutex_unlock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300399
400 PDEBUG(DBG_IO, "success");
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300401 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300402}
403
404/*
405 * usbvision_v4l2_close()
406 *
407 * This is part of Video 4 Linux API. The procedure
408 * stops streaming and deallocates all buffers that were earlier
409 * allocated in usbvision_v4l2_open().
410 *
411 */
Hans Verkuilbec43662008-12-30 06:58:20 -0300412static int usbvision_v4l2_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300413{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300414 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300415
416 PDEBUG(DBG_IO, "close");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300417
Hans Verkuil4d345702012-06-23 08:03:50 -0300418 mutex_lock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300419 usbvision_audio_off(usbvision);
420 usbvision_restart_isoc(usbvision);
421 usbvision_stop_isoc(usbvision);
422
423 usbvision_decompress_free(usbvision);
Thierry MERLE38284ba2006-12-15 16:46:53 -0300424 usbvision_frames_free(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300425 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300426 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300427
428 usbvision->user--;
429
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300430 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300431 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300432 usbvision_release(usbvision);
Dan Carpenter470a9142014-10-16 04:57:21 -0300433 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300434 }
Hans Verkuil4d345702012-06-23 08:03:50 -0300435 mutex_unlock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300436
437 PDEBUG(DBG_IO, "success");
Hans Verkuilfd958702015-07-20 09:59:29 -0300438 return v4l2_fh_release(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300439}
440
441
442/*
443 * usbvision_ioctl()
444 *
445 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
446 *
447 */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300448#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300449static int vidioc_g_register(struct file *file, void *priv,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300450 struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300451{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300452 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300453 int err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300454
Thierry MERLEc5f48362007-05-08 17:22:29 -0300455 /* NT100x has a 8-bit register space */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300456 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
457 if (err_code < 0) {
Hans Verkuil2aa689d2015-03-09 13:34:12 -0300458 dev_err(&usbvision->vdev.dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300459 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300460 __func__, err_code);
461 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300462 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300463 reg->val = err_code;
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300464 reg->size = 1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300465 return 0;
466}
467
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300468static int vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300469 const struct v4l2_dbg_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300470{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300471 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300472 int err_code;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300473
Thierry MERLEc5f48362007-05-08 17:22:29 -0300474 /* NT100x has a 8-bit register space */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300475 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300476 if (err_code < 0) {
Hans Verkuil2aa689d2015-03-09 13:34:12 -0300477 dev_err(&usbvision->vdev.dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -0300478 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300479 __func__, err_code);
480 return err_code;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300481 }
482 return 0;
483}
484#endif
485
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300486static int vidioc_querycap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300487 struct v4l2_capability *vc)
488{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300489 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuilcbe12cc2015-07-20 09:59:32 -0300490 struct video_device *vdev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300491
492 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
493 strlcpy(vc->card,
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300494 usbvision_device_data[usbvision->dev_model].model_string,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300495 sizeof(vc->card));
Thierry MERLE6f2a2782009-01-20 17:40:44 -0300496 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
Hans Verkuilcbe12cc2015-07-20 09:59:32 -0300497 vc->device_caps = usbvision->have_tuner ? V4L2_CAP_TUNER : 0;
498 if (vdev->vfl_type == VFL_TYPE_GRABBER)
499 vc->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
500 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
501 else
502 vc->device_caps |= V4L2_CAP_RADIO;
503
504 vc->capabilities = vc->device_caps | V4L2_CAP_VIDEO_CAPTURE |
505 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
506 if (usbvision_device_data[usbvision->dev_model].radio)
507 vc->capabilities |= V4L2_CAP_RADIO;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300508 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300509}
510
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300511static int vidioc_enum_input(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300512 struct v4l2_input *vi)
513{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300514 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300515 int chan;
516
Roel Kluin223ffe52009-05-02 16:38:47 -0300517 if (vi->index >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300518 return -EINVAL;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300519 if (usbvision->have_tuner)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300520 chan = vi->index;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300521 else
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300522 chan = vi->index + 1; /* skip Television string*/
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300523
Thierry MERLEc5f48362007-05-08 17:22:29 -0300524 /* Determine the requested input characteristics
525 specific for each usbvision card model */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300526 switch (chan) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300527 case 0:
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300528 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300529 strcpy(vi->name, "White Video Input");
530 } else {
531 strcpy(vi->name, "Television");
532 vi->type = V4L2_INPUT_TYPE_TUNER;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300533 vi->tuner = chan;
534 vi->std = USBVISION_NORMS;
535 }
536 break;
537 case 1:
538 vi->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300539 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300540 strcpy(vi->name, "Green Video Input");
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300541 else
Thierry MERLEc5f48362007-05-08 17:22:29 -0300542 strcpy(vi->name, "Composite Video Input");
Thierry MERLEc5f48362007-05-08 17:22:29 -0300543 vi->std = V4L2_STD_PAL;
544 break;
545 case 2:
546 vi->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300547 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300548 strcpy(vi->name, "Yellow Video Input");
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300549 else
Thierry MERLEc5f48362007-05-08 17:22:29 -0300550 strcpy(vi->name, "S-Video Input");
Thierry MERLEc5f48362007-05-08 17:22:29 -0300551 vi->std = V4L2_STD_PAL;
552 break;
553 case 3:
554 vi->type = V4L2_INPUT_TYPE_CAMERA;
555 strcpy(vi->name, "Red Video Input");
556 vi->std = V4L2_STD_PAL;
557 break;
558 }
559 return 0;
560}
561
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300562static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300563{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300564 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300565
566 *input = usbvision->ctl_input;
567 return 0;
568}
569
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300570static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300571{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300572 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300573
Roel Kluinf14a2972009-10-23 07:59:42 -0300574 if (input >= usbvision->video_inputs)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300575 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300576
Thierry MERLE66a17872007-06-26 16:35:30 -0300577 usbvision_muxsel(usbvision, input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300578 usbvision_set_input(usbvision);
579 usbvision_set_output(usbvision,
580 usbvision->curwidth,
581 usbvision->curheight);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300582 return 0;
583}
584
Hans Verkuil314527a2013-03-15 06:10:40 -0300585static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300586{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300587 struct usb_usbvision *usbvision = video_drvdata(file);
588
Hans Verkuil314527a2013-03-15 06:10:40 -0300589 usbvision->tvnorm_id = id;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300590
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300591 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
Thierry MERLE66a17872007-06-26 16:35:30 -0300592 /* propagate the change to the decoder */
593 usbvision_muxsel(usbvision, usbvision->ctl_input);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300594
595 return 0;
596}
597
Hans Verkuil3b8436d2013-06-03 05:36:47 -0300598static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
599{
600 struct usb_usbvision *usbvision = video_drvdata(file);
601
602 *id = usbvision->tvnorm_id;
603 return 0;
604}
605
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300606static int vidioc_g_tuner(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300607 struct v4l2_tuner *vt)
608{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300609 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300610
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300611 if (!usbvision->have_tuner || vt->index) /* Only tuner 0 */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300612 return -EINVAL;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300613 if (usbvision->radio) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300614 strcpy(vt->name, "Radio");
615 vt->type = V4L2_TUNER_RADIO;
616 } else {
617 strcpy(vt->name, "Television");
618 }
619 /* Let clients fill in the remainder of this struct */
Hans Verkuil1df79532009-02-21 18:11:31 -0300620 call_all(usbvision, tuner, g_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300621
622 return 0;
623}
624
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300625static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300626 const struct v4l2_tuner *vt)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300627{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300628 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300629
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300630 /* Only no or one tuner for now */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300631 if (!usbvision->have_tuner || vt->index)
632 return -EINVAL;
633 /* let clients handle this */
Hans Verkuil1df79532009-02-21 18:11:31 -0300634 call_all(usbvision, tuner, s_tuner, vt);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300635
636 return 0;
637}
638
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300639static int vidioc_g_frequency(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300640 struct v4l2_frequency *freq)
641{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300642 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300643
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300644 freq->tuner = 0; /* Only one tuner */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300645 if (usbvision->radio)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300646 freq->type = V4L2_TUNER_RADIO;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300647 else
Thierry MERLEc5f48362007-05-08 17:22:29 -0300648 freq->type = V4L2_TUNER_ANALOG_TV;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300649 freq->frequency = usbvision->freq;
650
651 return 0;
652}
653
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300654static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300655 const struct v4l2_frequency *freq)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300656{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300657 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300658
Hans Verkuil52cb0bf2010-12-19 20:33:51 -0300659 /* Only no or one tuner for now */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300660 if (!usbvision->have_tuner || freq->tuner)
661 return -EINVAL;
662
663 usbvision->freq = freq->frequency;
Hans Verkuil1df79532009-02-21 18:11:31 -0300664 call_all(usbvision, tuner, s_frequency, freq);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300665
666 return 0;
667}
668
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300669static int vidioc_reqbufs(struct file *file,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300670 void *priv, struct v4l2_requestbuffers *vr)
671{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300672 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300673 int ret;
674
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300675 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300676
677 /* Check input validity:
678 the user must do a VIDEO CAPTURE and MMAP method. */
Trent Piepho975f5762009-03-28 22:25:36 -0300679 if (vr->memory != V4L2_MEMORY_MMAP)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300680 return -EINVAL;
681
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300682 if (usbvision->streaming == stream_on) {
683 ret = usbvision_stream_interrupt(usbvision);
684 if (ret)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300685 return ret;
686 }
687
688 usbvision_frames_free(usbvision);
689 usbvision_empty_framequeues(usbvision);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300690 vr->count = usbvision_frames_alloc(usbvision, vr->count);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300691
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300692 usbvision->cur_frame = NULL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300693
694 return 0;
695}
696
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300697static int vidioc_querybuf(struct file *file,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300698 void *priv, struct v4l2_buffer *vb)
699{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300700 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300701 struct usbvision_frame *frame;
702
703 /* FIXME : must control
704 that buffers are mapped (VIDIOC_REQBUFS has been called) */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300705 if (vb->index >= usbvision->num_frames)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300706 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300707 /* Updating the corresponding frame state */
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300708 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300709 frame = &usbvision->frame[vb->index];
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300710 if (frame->grabstate >= frame_state_ready)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300711 vb->flags |= V4L2_BUF_FLAG_QUEUED;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300712 if (frame->grabstate >= frame_state_done)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300713 vb->flags |= V4L2_BUF_FLAG_DONE;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300714 if (frame->grabstate == frame_state_unused)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300715 vb->flags |= V4L2_BUF_FLAG_MAPPED;
716 vb->memory = V4L2_MEMORY_MMAP;
717
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300718 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300719
720 vb->memory = V4L2_MEMORY_MMAP;
721 vb->field = V4L2_FIELD_NONE;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300722 vb->length = usbvision->curwidth *
723 usbvision->curheight *
Thierry MERLEc5f48362007-05-08 17:22:29 -0300724 usbvision->palette.bytes_per_pixel;
725 vb->timestamp = usbvision->frame[vb->index].timestamp;
726 vb->sequence = usbvision->frame[vb->index].sequence;
727 return 0;
728}
729
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300730static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300731{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300732 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300733 struct usbvision_frame *frame;
734 unsigned long lock_flags;
735
736 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300737 if (vb->index >= usbvision->num_frames)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300738 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300739
740 frame = &usbvision->frame[vb->index];
741
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300742 if (frame->grabstate != frame_state_unused)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300743 return -EAGAIN;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300744
745 /* Mark it as ready and enqueue frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300746 frame->grabstate = frame_state_ready;
747 frame->scanstate = scan_state_scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300748 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
749
750 vb->flags &= ~V4L2_BUF_FLAG_DONE;
751
752 /* set v4l2_format index */
753 frame->v4l2_format = usbvision->palette;
754
755 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
756 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
757 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
758
759 return 0;
760}
761
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300762static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300763{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300764 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300765 int ret;
766 struct usbvision_frame *f;
767 unsigned long lock_flags;
768
Thierry MERLEc5f48362007-05-08 17:22:29 -0300769 if (list_empty(&(usbvision->outqueue))) {
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300770 if (usbvision->streaming == stream_idle)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300771 return -EINVAL;
772 ret = wait_event_interruptible
773 (usbvision->wait_frame,
774 !list_empty(&(usbvision->outqueue)));
775 if (ret)
776 return ret;
777 }
778
779 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
780 f = list_entry(usbvision->outqueue.next,
781 struct usbvision_frame, frame);
782 list_del(usbvision->outqueue.next);
783 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
784
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300785 f->grabstate = frame_state_unused;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300786
787 vb->memory = V4L2_MEMORY_MMAP;
788 vb->flags = V4L2_BUF_FLAG_MAPPED |
789 V4L2_BUF_FLAG_QUEUED |
Sakari Ailus1b18e7a2012-10-22 17:10:16 -0300790 V4L2_BUF_FLAG_DONE |
791 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300792 vb->index = f->index;
793 vb->sequence = f->sequence;
794 vb->timestamp = f->timestamp;
795 vb->field = V4L2_FIELD_NONE;
796 vb->bytesused = f->scanlength;
797
798 return 0;
799}
800
801static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
802{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300803 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300804
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300805 usbvision->streaming = stream_on;
Hans Verkuil1df79532009-02-21 18:11:31 -0300806 call_all(usbvision, video, s_stream, 1);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300807
808 return 0;
809}
810
811static int vidioc_streamoff(struct file *file,
812 void *priv, enum v4l2_buf_type type)
813{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300814 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300815
816 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
817 return -EINVAL;
818
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300819 if (usbvision->streaming == stream_on) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300820 usbvision_stream_interrupt(usbvision);
821 /* Stop all video streamings */
Hans Verkuil1df79532009-02-21 18:11:31 -0300822 call_all(usbvision, video, s_stream, 0);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300823 }
824 usbvision_empty_framequeues(usbvision);
825
826 return 0;
827}
828
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300829static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300830 struct v4l2_fmtdesc *vfd)
831{
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300832 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300833 return -EINVAL;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300834 strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300835 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300836 return 0;
837}
838
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300839static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300840 struct v4l2_format *vf)
841{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300842 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300843 vf->fmt.pix.width = usbvision->curwidth;
844 vf->fmt.pix.height = usbvision->curheight;
845 vf->fmt.pix.pixelformat = usbvision->palette.format;
846 vf->fmt.pix.bytesperline =
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300847 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
848 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300849 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
850 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
851
852 return 0;
853}
854
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300855static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300856 struct v4l2_format *vf)
857{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300858 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300859 int format_idx;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300860
861 /* Find requested format in available ones */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300862 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
863 if (vf->fmt.pix.pixelformat ==
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300864 usbvision_v4l2_format[format_idx].format) {
865 usbvision->palette = usbvision_v4l2_format[format_idx];
Thierry MERLEc5f48362007-05-08 17:22:29 -0300866 break;
867 }
868 }
869 /* robustness */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300870 if (format_idx == USBVISION_SUPPORTED_PALETTES)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300871 return -EINVAL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300872 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
873 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
874
875 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
876 usbvision->palette.bytes_per_pixel;
877 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
878
879 return 0;
880}
881
Hans Verkuil78b526a2008-05-28 12:16:41 -0300882static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300883 struct v4l2_format *vf)
884{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300885 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300886 int ret;
887
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300888 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
889 if (ret)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300890 return ret;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300891
892 /* stop io in case it is already in progress */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300893 if (usbvision->streaming == stream_on) {
894 ret = usbvision_stream_interrupt(usbvision);
895 if (ret)
Thierry MERLEc5f48362007-05-08 17:22:29 -0300896 return ret;
897 }
898 usbvision_frames_free(usbvision);
899 usbvision_empty_framequeues(usbvision);
900
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300901 usbvision->cur_frame = NULL;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300902
903 /* by now we are committed to the new data... */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300904 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300905
906 return 0;
907}
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300908
Hans Verkuil4d345702012-06-23 08:03:50 -0300909static ssize_t usbvision_read(struct file *file, char __user *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300910 size_t count, loff_t *ppos)
911{
Hans Verkuilc170ecf2008-08-23 08:32:09 -0300912 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300913 int noblock = file->f_flags & O_NONBLOCK;
914 unsigned long lock_flags;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300915 int ret, i;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300916 struct usbvision_frame *frame;
917
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300918 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300919 (unsigned long)count, noblock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300920
921 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
922 return -EFAULT;
923
Thierry MERLEc5f48362007-05-08 17:22:29 -0300924 /* This entry point is compatible with the mmap routines
925 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
926 to get frames or call read on the device. */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300927 if (!usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300928 /* First, allocate some frames to work with
929 if this has not been done with VIDIOC_REQBUF */
Thierry MERLE6f78e182007-02-07 10:13:11 -0300930 usbvision_frames_free(usbvision);
931 usbvision_empty_framequeues(usbvision);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300932 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300933 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300934
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300935 if (usbvision->streaming != stream_on) {
Thierry MERLE6f78e182007-02-07 10:13:11 -0300936 /* no stream is running, make it running ! */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300937 usbvision->streaming = stream_on;
Hans Verkuil1df79532009-02-21 18:11:31 -0300938 call_all(usbvision, video, s_stream, 1);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300939 }
940
Thierry MERLEc5f48362007-05-08 17:22:29 -0300941 /* Then, enqueue as many frames as possible
942 (like a user of VIDIOC_QBUF would do) */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300943 for (i = 0; i < usbvision->num_frames; i++) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300944 frame = &usbvision->frame[i];
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300945 if (frame->grabstate == frame_state_unused) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300946 /* Mark it as ready and enqueue frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300947 frame->grabstate = frame_state_ready;
948 frame->scanstate = scan_state_scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300949 /* Accumulated in usbvision_parse_data() */
950 frame->scanlength = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300951
952 /* set v4l2_format index */
953 frame->v4l2_format = usbvision->palette;
954
955 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
956 list_add_tail(&frame->frame, &usbvision->inqueue);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300957 spin_unlock_irqrestore(&usbvision->queue_lock,
958 lock_flags);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300959 }
960 }
961
962 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
963 if (list_empty(&(usbvision->outqueue))) {
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300964 if (noblock)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300965 return -EAGAIN;
966
967 ret = wait_event_interruptible
968 (usbvision->wait_frame,
969 !list_empty(&(usbvision->outqueue)));
970 if (ret)
971 return ret;
972 }
973
974 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
975 frame = list_entry(usbvision->outqueue.next,
976 struct usbvision_frame, frame);
977 list_del(usbvision->outqueue.next);
978 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
979
980 /* An error returns an empty frame */
Hans Verkuil5490a7c2010-12-19 20:21:36 -0300981 if (frame->grabstate == frame_state_error) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300982 frame->bytes_read = 0;
983 return 0;
984 }
985
Thierry MERLEc5f48362007-05-08 17:22:29 -0300986 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300987 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -0300988 frame->index, frame->bytes_read, frame->scanlength);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300989
990 /* copy bytes to user space; we allow for partials reads */
991 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
992 count = frame->scanlength - frame->bytes_read;
993
Hans Verkuil6d6a48e2010-12-29 13:53:21 -0300994 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300995 return -EFAULT;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300996
997 frame->bytes_read += count;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300998 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
Harvey Harrisond2db42d2008-04-04 20:50:07 -0300999 __func__,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001000 (unsigned long)count, frame->bytes_read);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001001
Mauro Carvalho Chehabb295e5c2015-06-05 10:47:36 -03001002#if 1
1003 /*
1004 * FIXME:
1005 * For now, forget the frame if it has not been read in one shot.
1006 */
1007 frame->bytes_read = 0;
1008
1009 /* Mark it as available to be used again. */
1010 frame->grabstate = frame_state_unused;
1011#else
1012 if (frame->bytes_read >= frame->scanlength) {
1013 /* All data has been read */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001014 frame->bytes_read = 0;
1015
1016 /* Mark it as available to be used again. */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001017 frame->grabstate = frame_state_unused;
Mauro Carvalho Chehabb295e5c2015-06-05 10:47:36 -03001018 }
1019#endif
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001020
1021 return count;
1022}
1023
Hans Verkuil4d345702012-06-23 08:03:50 -03001024static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1025 size_t count, loff_t *ppos)
1026{
1027 struct usb_usbvision *usbvision = video_drvdata(file);
1028 int res;
1029
1030 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1031 return -ERESTARTSYS;
1032 res = usbvision_read(file, buf, count, ppos);
1033 mutex_unlock(&usbvision->v4l2_lock);
1034 return res;
1035}
1036
1037static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001038{
1039 unsigned long size = vma->vm_end - vma->vm_start,
1040 start = vma->vm_start;
1041 void *pos;
1042 u32 i;
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001043 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001044
Thierry MERLE6f78e182007-02-07 10:13:11 -03001045 PDEBUG(DBG_MMAP, "mmap");
1046
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001047 if (!USBVISION_IS_OPERATIONAL(usbvision))
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001048 return -EFAULT;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001049
1050 if (!(vma->vm_flags & VM_WRITE) ||
Thierry MERLE6f78e182007-02-07 10:13:11 -03001051 size != PAGE_ALIGN(usbvision->max_frame_size)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001052 return -EINVAL;
1053 }
1054
Thierry MERLE6f78e182007-02-07 10:13:11 -03001055 for (i = 0; i < usbvision->num_frames; i++) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001056 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1057 vma->vm_pgoff)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001058 break;
1059 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001060 if (i == usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001061 PDEBUG(DBG_MMAP,
1062 "mmap: user supplied mapping address is out of range");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001063 return -EINVAL;
1064 }
1065
1066 /* VM_IO is eventually going to replace PageReserved altogether */
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07001067 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001068
1069 pos = usbvision->frame[i].data;
1070 while (size > 0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001071 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001072 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001073 return -EAGAIN;
1074 }
1075 start += PAGE_SIZE;
1076 pos += PAGE_SIZE;
1077 size -= PAGE_SIZE;
1078 }
1079
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001080 return 0;
1081}
1082
Hans Verkuil4d345702012-06-23 08:03:50 -03001083static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1084{
1085 struct usb_usbvision *usbvision = video_drvdata(file);
1086 int res;
1087
1088 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1089 return -ERESTARTSYS;
1090 res = usbvision_mmap(file, vma);
1091 mutex_unlock(&usbvision->v4l2_lock);
1092 return res;
1093}
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001094
1095/*
1096 * Here comes the stuff for radio on usbvision based devices
1097 *
1098 */
Hans Verkuilbec43662008-12-30 06:58:20 -03001099static int usbvision_radio_open(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001100{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001101 struct usb_usbvision *usbvision = video_drvdata(file);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001102 int err_code = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001103
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001104 PDEBUG(DBG_IO, "%s:", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001105
Hans Verkuil4d345702012-06-23 08:03:50 -03001106 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1107 return -ERESTARTSYS;
Hans Verkuilfd958702015-07-20 09:59:29 -03001108 err_code = v4l2_fh_open(file);
1109 if (err_code)
1110 goto out;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001111 if (usbvision->user) {
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001112 dev_err(&usbvision->rdev.dev,
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001113 "%s: Someone tried to open an already opened USBVision Radio!\n",
1114 __func__);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001115 err_code = -EBUSY;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001116 } else {
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001117 /* Alternate interface 1 is is the biggest frame size */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001118 err_code = usbvision_set_alternate(usbvision);
1119 if (err_code < 0) {
1120 usbvision->last_error = err_code;
1121 err_code = -EBUSY;
Andrew Morton544e6172007-12-12 18:25:23 -03001122 goto out;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001123 }
1124
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001125 /* If so far no errors then we shall start the radio */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001126 usbvision->radio = 1;
Hans Verkuil1df79532009-02-21 18:11:31 -03001127 call_all(usbvision, tuner, s_radio);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001128 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1129 usbvision->user++;
1130 }
Andrew Morton544e6172007-12-12 18:25:23 -03001131out:
Hans Verkuil4d345702012-06-23 08:03:50 -03001132 mutex_unlock(&usbvision->v4l2_lock);
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001133 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001134}
1135
1136
Hans Verkuilbec43662008-12-30 06:58:20 -03001137static int usbvision_radio_close(struct file *file)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001138{
Hans Verkuilc170ecf2008-08-23 08:32:09 -03001139 struct usb_usbvision *usbvision = video_drvdata(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001140
1141 PDEBUG(DBG_IO, "");
1142
Hans Verkuil4d345702012-06-23 08:03:50 -03001143 mutex_lock(&usbvision->v4l2_lock);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001144 /* Set packet size to 0 */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001145 usbvision->iface_alt = 0;
Hans Verkuilfd958702015-07-20 09:59:29 -03001146 usb_set_interface(usbvision->dev, usbvision->iface,
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001147 usbvision->iface_alt);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001148
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001149 usbvision_audio_off(usbvision);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001150 usbvision->radio = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001151 usbvision->user--;
1152
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001153 if (usbvision->remove_pending) {
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001154 printk(KERN_INFO "%s: Final disconnect\n", __func__);
Hans Verkuilfd958702015-07-20 09:59:29 -03001155 v4l2_fh_release(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001156 usbvision_release(usbvision);
Hans Verkuilfd958702015-07-20 09:59:29 -03001157 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001158 }
1159
Hans Verkuil4d345702012-06-23 08:03:50 -03001160 mutex_unlock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001161 PDEBUG(DBG_IO, "success");
Hans Verkuilfd958702015-07-20 09:59:29 -03001162 return v4l2_fh_release(file);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001163}
1164
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001165/* Video registration stuff */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001166
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001167/* Video template */
Hans Verkuilbec43662008-12-30 06:58:20 -03001168static const struct v4l2_file_operations usbvision_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001169 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001170 .open = usbvision_v4l2_open,
1171 .release = usbvision_v4l2_close,
1172 .read = usbvision_v4l2_read,
1173 .mmap = usbvision_v4l2_mmap,
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001174 .unlocked_ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001175};
Hans Verkuila3998102008-07-21 02:57:38 -03001176
1177static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001178 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001179 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1180 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1181 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1182 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001183 .vidioc_reqbufs = vidioc_reqbufs,
1184 .vidioc_querybuf = vidioc_querybuf,
1185 .vidioc_qbuf = vidioc_qbuf,
1186 .vidioc_dqbuf = vidioc_dqbuf,
1187 .vidioc_s_std = vidioc_s_std,
Hans Verkuil3b8436d2013-06-03 05:36:47 -03001188 .vidioc_g_std = vidioc_g_std,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001189 .vidioc_enum_input = vidioc_enum_input,
1190 .vidioc_g_input = vidioc_g_input,
1191 .vidioc_s_input = vidioc_s_input,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001192 .vidioc_streamon = vidioc_streamon,
1193 .vidioc_streamoff = vidioc_streamoff,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001194 .vidioc_g_tuner = vidioc_g_tuner,
1195 .vidioc_s_tuner = vidioc_s_tuner,
1196 .vidioc_g_frequency = vidioc_g_frequency,
1197 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilfd958702015-07-20 09:59:29 -03001198 .vidioc_log_status = v4l2_ctrl_log_status,
1199 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1200 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001201#ifdef CONFIG_VIDEO_ADV_DEBUG
1202 .vidioc_g_register = vidioc_g_register,
1203 .vidioc_s_register = vidioc_s_register,
1204#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001205};
1206
1207static struct video_device usbvision_video_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001208 .fops = &usbvision_fops,
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001209 .ioctl_ops = &usbvision_ioctl_ops,
Hans Verkuila3998102008-07-21 02:57:38 -03001210 .name = "usbvision-video",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001211 .release = video_device_release_empty,
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001212 .tvnorms = USBVISION_NORMS,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001213};
1214
1215
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001216/* Radio template */
Hans Verkuilbec43662008-12-30 06:58:20 -03001217static const struct v4l2_file_operations usbvision_radio_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001218 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001219 .open = usbvision_radio_open,
1220 .release = usbvision_radio_close,
Hans Verkuilfd958702015-07-20 09:59:29 -03001221 .poll = v4l2_ctrl_poll,
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001222 .unlocked_ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001223};
1224
Hans Verkuila3998102008-07-21 02:57:38 -03001225static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001226 .vidioc_querycap = vidioc_querycap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001227 .vidioc_g_tuner = vidioc_g_tuner,
1228 .vidioc_s_tuner = vidioc_s_tuner,
1229 .vidioc_g_frequency = vidioc_g_frequency,
1230 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuilfd958702015-07-20 09:59:29 -03001231 .vidioc_log_status = v4l2_ctrl_log_status,
1232 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1233 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuila3998102008-07-21 02:57:38 -03001234};
1235
1236static struct video_device usbvision_radio_template = {
Hans Verkuila3998102008-07-21 02:57:38 -03001237 .fops = &usbvision_radio_fops,
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001238 .name = "usbvision-radio",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001239 .release = video_device_release_empty,
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001240 .ioctl_ops = &usbvision_radio_ioctl_ops,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001241};
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001242
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001243
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001244static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1245 struct video_device *vdev,
1246 const struct video_device *vdev_template,
1247 const char *name)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001248{
1249 struct usb_device *usb_dev = usbvision->dev;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001250
1251 if (usb_dev == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001252 dev_err(&usbvision->dev->dev,
1253 "%s: usbvision->dev is not set\n", __func__);
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001254 return;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001255 }
1256
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001257 *vdev = *vdev_template;
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001258 vdev->lock = &usbvision->v4l2_lock;
Hans Verkuil1df79532009-02-21 18:11:31 -03001259 vdev->v4l2_dev = &usbvision->v4l2_dev;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001260 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1261 video_set_drvdata(vdev, usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001262}
1263
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001264/* unregister video4linux devices */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001265static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1266{
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001267 /* Radio Device: */
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001268 if (video_is_registered(&usbvision->rdev)) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001269 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001270 video_device_node_name(&usbvision->rdev));
1271 video_unregister_device(&usbvision->rdev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001272 }
1273
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001274 /* Video Device: */
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001275 if (video_is_registered(&usbvision->vdev)) {
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001276 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001277 video_device_node_name(&usbvision->vdev));
1278 video_unregister_device(&usbvision->vdev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001279 }
1280}
1281
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001282/* register video4linux devices */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001283static int usbvision_register_video(struct usb_usbvision *usbvision)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001284{
Hans Verkuil2b436652015-07-20 09:59:30 -03001285 int res = -ENOMEM;
1286
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001287 /* Video Device: */
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001288 usbvision_vdev_init(usbvision, &usbvision->vdev,
1289 &usbvision_video_template, "USBVision Video");
1290 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001291 goto err_exit;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001292 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001293 usbvision->nr, video_device_node_name(&usbvision->vdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001294
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001295 /* Radio Device: */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001296 if (usbvision_device_data[usbvision->dev_model].radio) {
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001297 /* usbvision has radio */
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001298 usbvision_vdev_init(usbvision, &usbvision->rdev,
1299 &usbvision_radio_template, "USBVision Radio");
1300 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001301 goto err_exit;
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001302 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001303 usbvision->nr, video_device_node_name(&usbvision->rdev));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001304 }
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001305 /* all done */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001306 return 0;
1307
1308 err_exit:
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001309 dev_err(&usbvision->dev->dev,
1310 "USBVision[%d]: video_register_device() failed\n",
1311 usbvision->nr);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001312 usbvision_unregister_video(usbvision);
Hans Verkuil2b436652015-07-20 09:59:30 -03001313 return res;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001314}
1315
1316/*
1317 * usbvision_alloc()
1318 *
Thierry MERLEc5f48362007-05-08 17:22:29 -03001319 * This code allocates the struct usb_usbvision.
1320 * It is filled with default values.
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001321 *
1322 * Returns NULL on error, a pointer to usb_usbvision else.
1323 *
1324 */
Janne Grunaua8784402009-04-01 08:46:00 -03001325static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1326 struct usb_interface *intf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001327{
1328 struct usb_usbvision *usbvision;
1329
Hans Verkuil1df79532009-02-21 18:11:31 -03001330 usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1331 if (usbvision == NULL)
1332 return NULL;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001333
1334 usbvision->dev = dev;
Janne Grunaua8784402009-04-01 08:46:00 -03001335 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
Hans Verkuil1df79532009-02-21 18:11:31 -03001336 goto err_free;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001337
Hans Verkuilfd958702015-07-20 09:59:29 -03001338 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1339 goto err_unreg;
1340 usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001341 mutex_init(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001342
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001343 /* prepare control urb for control messages during interrupts */
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001344 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1345 if (usbvision->ctrl_urb == NULL)
Hans Verkuil1df79532009-02-21 18:11:31 -03001346 goto err_unreg;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001347 init_waitqueue_head(&usbvision->ctrl_urb_wq);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001348
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001349 return usbvision;
1350
Hans Verkuil1df79532009-02-21 18:11:31 -03001351err_unreg:
Hans Verkuilfd958702015-07-20 09:59:29 -03001352 v4l2_ctrl_handler_free(&usbvision->hdl);
Hans Verkuil1df79532009-02-21 18:11:31 -03001353 v4l2_device_unregister(&usbvision->v4l2_dev);
1354err_free:
1355 kfree(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001356 return NULL;
1357}
1358
1359/*
1360 * usbvision_release()
1361 *
1362 * This code does final release of struct usb_usbvision. This happens
1363 * after the device is disconnected -and- all clients closed their files.
1364 *
1365 */
1366static void usbvision_release(struct usb_usbvision *usbvision)
1367{
1368 PDEBUG(DBG_PROBE, "");
1369
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001370 usbvision->initialized = 0;
1371
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001372 usbvision_remove_sysfs(&usbvision->vdev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001373 usbvision_unregister_video(usbvision);
Alexey Khoroshilov090c65b2013-06-10 17:32:29 -03001374 kfree(usbvision->alt_max_pkt_size);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001375
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001376 usb_free_urb(usbvision->ctrl_urb);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001377
Hans Verkuilfd958702015-07-20 09:59:29 -03001378 v4l2_ctrl_handler_free(&usbvision->hdl);
Hans Verkuil1df79532009-02-21 18:11:31 -03001379 v4l2_device_unregister(&usbvision->v4l2_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001380 kfree(usbvision);
1381
1382 PDEBUG(DBG_PROBE, "success");
1383}
1384
1385
Thierry MERLEc5f48362007-05-08 17:22:29 -03001386/*********************** usb interface **********************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001387
1388static void usbvision_configure_video(struct usb_usbvision *usbvision)
1389{
Thierry MERLEc5f48362007-05-08 17:22:29 -03001390 int model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001391
1392 if (usbvision == NULL)
1393 return;
1394
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001395 model = usbvision->dev_model;
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001396 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001397
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001398 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1399 usbvision->vin_reg2_preset =
1400 usbvision_device_data[usbvision->dev_model].vin_reg2;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001401 } else {
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001402 usbvision->vin_reg2_preset = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001403 }
1404
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001405 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001406
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001407 usbvision->video_inputs = usbvision_device_data[model].video_channels;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001408 usbvision->ctl_input = 0;
1409
1410 /* This should be here to make i2c clients to be able to register */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001411 /* first switch off audio */
Ondrej Zary240d57b2011-04-27 17:36:05 -03001412 if (usbvision_device_data[model].audio_channels > 0)
1413 usbvision_audio_off(usbvision);
Hans Verkuil62e25942015-07-20 09:59:28 -03001414 /* and then power up the tuner */
1415 usbvision_power_on(usbvision);
1416 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001417}
1418
1419/*
1420 * usbvision_probe()
1421 *
1422 * This procedure queries device descriptor and accepts the interface
1423 * if it looks like USBVISION video device
1424 *
1425 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001426static int usbvision_probe(struct usb_interface *intf,
1427 const struct usb_device_id *devid)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001428{
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001429 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1430 struct usb_interface *uif;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001431 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1432 const struct usb_host_interface *interface;
1433 struct usb_usbvision *usbvision = NULL;
1434 const struct usb_endpoint_descriptor *endpoint;
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001435 int model, i, ret;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001436
1437 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001438 dev->descriptor.idVendor,
1439 dev->descriptor.idProduct, ifnum);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001440
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001441 model = devid->driver_info;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001442 if (model < 0 || model >= usbvision_device_data_size) {
1443 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001444 ret = -ENODEV;
1445 goto err_usb;
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001446 }
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001447 printk(KERN_INFO "%s: %s found\n", __func__,
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001448 usbvision_device_data[model].model_string);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001449
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001450 if (usbvision_device_data[model].interface >= 0)
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001451 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001452 else
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001453 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001454 endpoint = &interface->endpoint[1].desc;
Julia Lawall2230c3c2009-01-03 16:53:10 -03001455 if (!usb_endpoint_xfer_isoc(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001456 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001457 __func__, ifnum);
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001458 dev_err(&intf->dev, "%s: Endpoint attributes %d",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001459 __func__, endpoint->bmAttributes);
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001460 ret = -ENODEV;
1461 goto err_usb;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001462 }
Julia Lawall13417982008-12-29 21:49:22 -03001463 if (usb_endpoint_dir_out(endpoint)) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001464 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001465 __func__, ifnum);
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001466 ret = -ENODEV;
1467 goto err_usb;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001468 }
1469
Janne Grunaua8784402009-04-01 08:46:00 -03001470 usbvision = usbvision_alloc(dev, intf);
1471 if (usbvision == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001472 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001473 ret = -ENOMEM;
1474 goto err_usb;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001475 }
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001476
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001477 if (dev->descriptor.bNumConfigurations > 1)
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001478 usbvision->bridge_type = BRIDGE_NT1004;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001479 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001480 usbvision->bridge_type = BRIDGE_NT1005;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001481 else
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001482 usbvision->bridge_type = BRIDGE_NT1003;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001483 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001484
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001485 /* compute alternate max packet sizes */
1486 uif = dev->actconfig->interface[0];
1487
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001488 usbvision->num_alt = uif->num_altsetting;
1489 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1490 usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001491 if (usbvision->alt_max_pkt_size == NULL) {
Mauro Carvalho Chehabbe9ed512009-01-08 09:13:42 -03001492 dev_err(&intf->dev, "usbvision: out of memory!\n");
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001493 ret = -ENOMEM;
1494 goto err_pkt;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001495 }
1496
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001497 for (i = 0; i < usbvision->num_alt; i++) {
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001498 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1499 wMaxPacketSize);
1500 usbvision->alt_max_pkt_size[i] =
1501 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001502 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001503 usbvision->alt_max_pkt_size[i]);
1504 }
1505
1506
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001507 usbvision->nr = usbvision_nr++;
1508
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001509 usbvision->have_tuner = usbvision_device_data[model].tuner;
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001510 if (usbvision->have_tuner)
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001511 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001512
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001513 usbvision->dev_model = model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001514 usbvision->remove_pending = 0;
1515 usbvision->iface = ifnum;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001516 usbvision->iface_alt = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001517 usbvision->video_endp = endpoint->bEndpointAddress;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001518 usbvision->isoc_packet_size = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001519 usbvision->usb_bandwidth = 0;
1520 usbvision->user = 0;
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001521 usbvision->streaming = stream_off;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001522 usbvision_configure_video(usbvision);
Hans Verkuil84034722010-09-17 15:07:28 -03001523 usbvision_register_video(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001524
Hans Verkuil2aa689d2015-03-09 13:34:12 -03001525 usbvision_create_sysfs(&usbvision->vdev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001526
1527 PDEBUG(DBG_PROBE, "success");
1528 return 0;
Alexey Khoroshilovafd270d2015-03-27 19:39:09 -03001529
1530err_pkt:
1531 usbvision_release(usbvision);
1532err_usb:
1533 usb_put_dev(dev);
1534 return ret;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001535}
1536
1537
1538/*
1539 * usbvision_disconnect()
1540 *
1541 * This procedure stops all driver activity, deallocates interface-private
1542 * structure (pointed by 'ptr') and after that driver should be removable
1543 * with no ill consequences.
1544 *
1545 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001546static void usbvision_disconnect(struct usb_interface *intf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001547{
Hans Verkuilaeb506a2010-05-02 09:00:13 -03001548 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001549
1550 PDEBUG(DBG_PROBE, "");
1551
1552 if (usbvision == NULL) {
Julia Lawall42020662010-05-27 09:36:45 -03001553 pr_err("%s: usb_get_intfdata() failed\n", __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001554 return;
1555 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001556
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001557 mutex_lock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001558
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001559 /* At this time we ask to cancel outstanding URBs */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001560 usbvision_stop_isoc(usbvision);
1561
Hans Verkuilf1ba28c2009-03-14 12:27:01 -03001562 v4l2_device_disconnect(&usbvision->v4l2_dev);
Hans Verkuil62e25942015-07-20 09:59:28 -03001563 usbvision_i2c_unregister(usbvision);
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001564 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001565
1566 usb_put_dev(usbvision->dev);
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001567 usbvision->dev = NULL; /* USB device is no more */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001568
Hans Verkuilc627b9d2010-12-18 11:06:09 -03001569 mutex_unlock(&usbvision->v4l2_lock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001570
1571 if (usbvision->user) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001572 printk(KERN_INFO "%s: In use, disconnect pending\n",
Harvey Harrisond2db42d2008-04-04 20:50:07 -03001573 __func__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001574 wake_up_interruptible(&usbvision->wait_frame);
1575 wake_up_interruptible(&usbvision->wait_stream);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001576 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001577 usbvision_release(usbvision);
1578 }
1579
1580 PDEBUG(DBG_PROBE, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001581}
1582
1583static struct usb_driver usbvision_driver = {
1584 .name = "usbvision",
1585 .id_table = usbvision_table,
1586 .probe = usbvision_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001587 .disconnect = usbvision_disconnect,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001588};
1589
1590/*
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001591 * usbvision_init()
1592 *
1593 * This code is run to initialize the driver.
1594 *
1595 */
1596static int __init usbvision_init(void)
1597{
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001598 int err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001599
1600 PDEBUG(DBG_PROBE, "");
1601
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001602 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1603 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
Thierry MERLEc876a342006-12-09 16:42:54 -03001604 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001605
1606 /* disable planar mode support unless compression enabled */
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001607 if (isoc_mode != ISOC_MODE_COMPRESS) {
Hans Verkuil52cb0bf2010-12-19 20:33:51 -03001608 /* FIXME : not the right way to set supported flag */
1609 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1610 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001611 }
1612
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001613 err_code = usb_register(&usbvision_driver);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001614
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001615 if (err_code == 0) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001616 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001617 PDEBUG(DBG_PROBE, "success");
1618 }
Hans Verkuil5490a7c2010-12-19 20:21:36 -03001619 return err_code;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001620}
1621
1622static void __exit usbvision_exit(void)
1623{
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001624 PDEBUG(DBG_PROBE, "");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001625
Hans Verkuil6d6a48e2010-12-29 13:53:21 -03001626 usb_deregister(&usbvision_driver);
1627 PDEBUG(DBG_PROBE, "success");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001628}
1629
1630module_init(usbvision_init);
1631module_exit(usbvision_exit);