blob: 6b185a9a37e8d040f2bc744d6e5ed09c3a65ae61 [file] [log] [blame]
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001/*
Mauro Carvalho Chehab7ca659e2006-12-09 11:43:31 -03002 * USB USBVISION Video device driver 0.9.9
Thierry MERLE483dfdb2006-12-04 08:31:45 -03003 *
4 *
5 *
6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
7 *
8 * This module is part of usbvision driver project.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 * Let's call the version 0.... until compression decoding is completely
25 * implemented.
26 *
27 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
28 * It was based on USB CPiA driver written by Peter Pregler,
29 * Scott J. Bertin and Johannes Erdfelt
30 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
31 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
32 * Updates to driver completed by Dwaine P. Garden
33 *
34 *
35 * TODO:
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
38 * nt1003 memory.
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>
54#include <linux/utsname.h>
55#include <linux/highmem.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030056#include <linux/videodev.h>
57#include <linux/vmalloc.h>
58#include <linux/module.h>
59#include <linux/init.h>
60#include <linux/spinlock.h>
61#include <asm/io.h>
62#include <linux/videodev2.h>
63#include <linux/video_decoder.h>
64#include <linux/i2c.h>
65
66#include <media/saa7115.h>
67#include <media/v4l2-common.h>
68#include <media/tuner.h>
69#include <media/audiochip.h>
70
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -030071#include <linux/moduleparam.h>
72#include <linux/workqueue.h>
Thierry MERLE483dfdb2006-12-04 08:31:45 -030073
74#ifdef CONFIG_KMOD
75#include <linux/kmod.h>
76#endif
77
78#include "usbvision.h"
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -030079#include "usbvision-cards.h"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030080
Thierry MERLEc5f48362007-05-08 17:22:29 -030081#define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
82 Dwaine Garden <DwaineGarden@rogers.com>"
Thierry MERLE483dfdb2006-12-04 08:31:45 -030083#define DRIVER_NAME "usbvision"
84#define DRIVER_ALIAS "USBVision"
85#define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
86#define DRIVER_LICENSE "GPL"
87#define USBVISION_DRIVER_VERSION_MAJOR 0
88#define USBVISION_DRIVER_VERSION_MINOR 9
89#define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
Thierry MERLEc5f48362007-05-08 17:22:29 -030090#define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
91USBVISION_DRIVER_VERSION_MINOR,\
92USBVISION_DRIVER_VERSION_PATCHLEVEL)
93#define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
94 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
95 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
Thierry MERLE483dfdb2006-12-04 08:31:45 -030096
97#define ENABLE_HEXDUMP 0 /* Enable if you need it */
98
99
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300100#ifdef USBVISION_DEBUG
101 #define PDEBUG(level, fmt, args...) \
Thierry MERLEc5f48362007-05-08 17:22:29 -0300102 if (video_debug & (level)) \
103 info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ ,\
104 ## args)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300105#else
106 #define PDEBUG(level, fmt, args...) do {} while(0)
107#endif
108
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300109#define DBG_IO 1<<1
110#define DBG_PROBE 1<<2
Thierry MERLEc876a342006-12-09 16:42:54 -0300111#define DBG_MMAP 1<<3
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300112
113//String operations
114#define rmspace(str) while(*str==' ') str++;
115#define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
116
117
Thierry MERLEc5f48362007-05-08 17:22:29 -0300118/* sequential number of usbvision device */
119static int usbvision_nr = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300120
121static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
122 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
123 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
124 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
125 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
126 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
127 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
128 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
129 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
130};
131
Thierry MERLEc5f48362007-05-08 17:22:29 -0300132/* Function prototypes */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300133static void usbvision_release(struct usb_usbvision *usbvision);
134
Thierry MERLEc5f48362007-05-08 17:22:29 -0300135/* Default initalization of device driver parameters */
136/* Set the default format for ISOC endpoint */
137static int isocMode = ISOC_MODE_COMPRESS;
138/* Set the default Debug Mode of the device driver */
139static int video_debug = 0;
140/* Set the default device to power on at startup */
141static int PowerOnAtOpen = 1;
142/* Sequential Number of Video Device */
143static int video_nr = -1;
144/* Sequential Number of Radio Device */
145static int radio_nr = -1;
146/* Sequential Number of VBI Device */
147static int vbi_nr = -1;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300148
Thierry MERLEc5f48362007-05-08 17:22:29 -0300149/* Grab parameters for the device driver */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300150
Thierry MERLEc5f48362007-05-08 17:22:29 -0300151/* Showing parameters under SYSFS */
152#if defined(module_param)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300153module_param(isocMode, int, 0444);
154module_param(video_debug, int, 0444);
155module_param(PowerOnAtOpen, int, 0444);
156module_param(video_nr, int, 0444);
157module_param(radio_nr, int, 0444);
158module_param(vbi_nr, int, 0444);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300159#else
160/* Old Style */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300161MODULE_PARAM(isocMode, "i");
Thierry MERLEc5f48362007-05-08 17:22:29 -0300162/* Grab the Debug Mode of the device driver */
163MODULE_PARM(video_debug, "i");
164/* Grab the compression to be adaptive */
165MODULE_PARM(adjustCompression, "i");
166/* Grab the device to power on at startup */
167MODULE_PARM(PowerOnAtOpen, "i");
168/* To help people with Black and White output with using s-video input.
169 Some cables and input device are wired differently. */
170MODULE_PARM(SwitchSVideoInput, "i");
171/* video_nr option allows to specify a certain /dev/videoX device
172 (like /dev/video0 or /dev/video1 ...) */
173MODULE_PARM(video_nr, "i");
174/* radio_nr option allows to specify a certain /dev/radioX device
175 (like /dev/radio0 or /dev/radio1 ...) */
176MODULE_PARM(radio_nr, "i");
177/* vbi_nr option allows to specify a certain /dev/vbiX device
178 (like /dev/vbi0 or /dev/vbi1 ...) */
179MODULE_PARM(vbi_nr, "i");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300180#endif
181
182MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
183MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
184MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
185MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
186MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
187MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300188
189
190// Misc stuff
191MODULE_AUTHOR(DRIVER_AUTHOR);
192MODULE_DESCRIPTION(DRIVER_DESC);
193MODULE_LICENSE(DRIVER_LICENSE);
Mauro Carvalho Chehaba1ed5512006-12-09 11:41:59 -0300194MODULE_VERSION(USBVISION_VERSION_STRING);
195MODULE_ALIAS(DRIVER_ALIAS);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300196
197
Thierry MERLEc5f48362007-05-08 17:22:29 -0300198/*****************************************************************************/
199/* SYSFS Code - Copied from the stv680.c usb module. */
200/* Device information is located at /sys/class/video4linux/video0 */
201/* Device parameters information is located at /sys/module/usbvision */
202/* Device USB Information is located at */
203/* /sys/bus/usb/drivers/USBVision Video Grabber */
204/*****************************************************************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300205
206
207#define YES_NO(x) ((x) ? "Yes" : "No")
208
209static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
210{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300211 struct video_device *vdev =
212 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300213 return video_get_drvdata(vdev);
214}
215
216static ssize_t show_version(struct class_device *cd, char *buf)
217{
218 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
219}
220static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
221
Dwaine Gardened00b412006-12-27 10:23:28 -0200222static ssize_t show_model(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300223{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300224 struct video_device *vdev =
225 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300226 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300227 return sprintf(buf, "%s\n",
228 usbvision_device_data[usbvision->DevModel].ModelString);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300229}
230static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
231
Dwaine Gardened00b412006-12-27 10:23:28 -0200232static ssize_t show_hue(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300233{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300234 struct video_device *vdev =
235 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300236 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
237 struct v4l2_control ctrl;
238 ctrl.id = V4L2_CID_HUE;
239 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200240 if(usbvision->user)
241 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300242 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300243}
244static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
245
Dwaine Gardened00b412006-12-27 10:23:28 -0200246static ssize_t show_contrast(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300247{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300248 struct video_device *vdev =
249 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300250 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
251 struct v4l2_control ctrl;
252 ctrl.id = V4L2_CID_CONTRAST;
253 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200254 if(usbvision->user)
255 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300256 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300257}
258static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
259
Dwaine Gardened00b412006-12-27 10:23:28 -0200260static ssize_t show_brightness(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300261{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300262 struct video_device *vdev =
263 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300264 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
265 struct v4l2_control ctrl;
266 ctrl.id = V4L2_CID_BRIGHTNESS;
267 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200268 if(usbvision->user)
269 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300270 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300271}
272static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
273
Dwaine Gardened00b412006-12-27 10:23:28 -0200274static ssize_t show_saturation(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300275{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300276 struct video_device *vdev =
277 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300278 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
279 struct v4l2_control ctrl;
280 ctrl.id = V4L2_CID_SATURATION;
281 ctrl.value = 0;
Dwaine Gardened00b412006-12-27 10:23:28 -0200282 if(usbvision->user)
283 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -0300284 return sprintf(buf, "%d\n", ctrl.value);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300285}
286static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
287
Dwaine Gardened00b412006-12-27 10:23:28 -0200288static ssize_t show_streaming(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300289{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300290 struct video_device *vdev =
291 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300292 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300293 return sprintf(buf, "%s\n",
294 YES_NO(usbvision->streaming==Stream_On?1:0));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300295}
296static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
297
Dwaine Gardened00b412006-12-27 10:23:28 -0200298static ssize_t show_compression(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300299{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300300 struct video_device *vdev =
301 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300302 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300303 return sprintf(buf, "%s\n",
304 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300305}
306static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
307
Dwaine Gardened00b412006-12-27 10:23:28 -0200308static ssize_t show_device_bridge(struct class_device *cd, char *buf)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300309{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300310 struct video_device *vdev =
311 container_of(cd, struct video_device, class_dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300312 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
313 return sprintf(buf, "%d\n", usbvision->bridgeType);
314}
315static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
316
317static void usbvision_create_sysfs(struct video_device *vdev)
318{
319 int res;
Dwaine Gardened00b412006-12-27 10:23:28 -0200320 if (!vdev)
321 return;
322 do {
323 res=class_device_create_file(&vdev->class_dev,
324 &class_device_attr_version);
325 if (res<0)
326 break;
327 res=class_device_create_file(&vdev->class_dev,
328 &class_device_attr_model);
329 if (res<0)
330 break;
331 res=class_device_create_file(&vdev->class_dev,
332 &class_device_attr_hue);
333 if (res<0)
334 break;
335 res=class_device_create_file(&vdev->class_dev,
336 &class_device_attr_contrast);
337 if (res<0)
338 break;
339 res=class_device_create_file(&vdev->class_dev,
340 &class_device_attr_brightness);
341 if (res<0)
342 break;
343 res=class_device_create_file(&vdev->class_dev,
344 &class_device_attr_saturation);
345 if (res<0)
346 break;
347 res=class_device_create_file(&vdev->class_dev,
348 &class_device_attr_streaming);
349 if (res<0)
350 break;
351 res=class_device_create_file(&vdev->class_dev,
352 &class_device_attr_compression);
353 if (res<0)
354 break;
355 res=class_device_create_file(&vdev->class_dev,
356 &class_device_attr_bridge);
357 if (res>=0)
358 return;
359 } while (0);
360
361 err("%s error: %d\n", __FUNCTION__, res);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300362}
363
364static void usbvision_remove_sysfs(struct video_device *vdev)
365{
366 if (vdev) {
Dwaine Gardened00b412006-12-27 10:23:28 -0200367 class_device_remove_file(&vdev->class_dev,
368 &class_device_attr_version);
369 class_device_remove_file(&vdev->class_dev,
370 &class_device_attr_model);
371 class_device_remove_file(&vdev->class_dev,
372 &class_device_attr_hue);
373 class_device_remove_file(&vdev->class_dev,
374 &class_device_attr_contrast);
375 class_device_remove_file(&vdev->class_dev,
376 &class_device_attr_brightness);
377 class_device_remove_file(&vdev->class_dev,
378 &class_device_attr_saturation);
379 class_device_remove_file(&vdev->class_dev,
380 &class_device_attr_streaming);
381 class_device_remove_file(&vdev->class_dev,
382 &class_device_attr_compression);
383 class_device_remove_file(&vdev->class_dev,
384 &class_device_attr_bridge);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300385 }
386}
387
388
389/*
390 * usbvision_open()
391 *
392 * This is part of Video 4 Linux API. The driver can be opened by one
393 * client only (checks internal counter 'usbvision->user'). The procedure
394 * then allocates buffers needed for video processing.
395 *
396 */
397static int usbvision_v4l2_open(struct inode *inode, struct file *file)
398{
399 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300400 struct usb_usbvision *usbvision =
401 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300402 int errCode = 0;
403
404 PDEBUG(DBG_IO, "open");
405
406
407 usbvision_reset_powerOffTimer(usbvision);
408
409 if (usbvision->user)
410 errCode = -EBUSY;
411 else {
Thierry MERLE6f78e182007-02-07 10:13:11 -0300412 /* Allocate memory for the scratch ring buffer */
413 errCode = usbvision_scratch_alloc(usbvision);
414 if (isocMode==ISOC_MODE_COMPRESS) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300415 /* Allocate intermediate decompression buffers
416 only if needed */
Thierry MERLE6f78e182007-02-07 10:13:11 -0300417 errCode = usbvision_decompress_alloc(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300418 }
419 if (errCode) {
420 /* Deallocate all buffers if trouble */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300421 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300422 usbvision_decompress_free(usbvision);
423 }
424 }
425
426 /* If so far no errors then we shall start the camera */
427 if (!errCode) {
428 down(&usbvision->lock);
429 if (usbvision->power == 0) {
430 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300431 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300432 }
433
434 /* Send init sequence only once, it's large! */
435 if (!usbvision->initialized) {
436 int setup_ok = 0;
437 setup_ok = usbvision_setup(usbvision,isocMode);
438 if (setup_ok)
439 usbvision->initialized = 1;
440 else
441 errCode = -EBUSY;
442 }
443
444 if (!errCode) {
445 usbvision_begin_streaming(usbvision);
446 errCode = usbvision_init_isoc(usbvision);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300447 /* device must be initialized before isoc transfer */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300448 usbvision_muxsel(usbvision,0);
449 usbvision->user++;
Thierry MERLEc5f48362007-05-08 17:22:29 -0300450 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300451 if (PowerOnAtOpen) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -0300452 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300453 usbvision_power_off(usbvision);
454 usbvision->initialized = 0;
455 }
456 }
457 up(&usbvision->lock);
458 }
459
460 if (errCode) {
461 }
462
463 /* prepare queues */
464 usbvision_empty_framequeues(usbvision);
465
466 PDEBUG(DBG_IO, "success");
467 return errCode;
468}
469
470/*
471 * usbvision_v4l2_close()
472 *
473 * This is part of Video 4 Linux API. The procedure
474 * stops streaming and deallocates all buffers that were earlier
475 * allocated in usbvision_v4l2_open().
476 *
477 */
478static int usbvision_v4l2_close(struct inode *inode, struct file *file)
479{
480 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300481 struct usb_usbvision *usbvision =
482 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300483
484 PDEBUG(DBG_IO, "close");
485 down(&usbvision->lock);
486
487 usbvision_audio_off(usbvision);
488 usbvision_restart_isoc(usbvision);
489 usbvision_stop_isoc(usbvision);
490
491 usbvision_decompress_free(usbvision);
Thierry MERLE38284ba2006-12-15 16:46:53 -0300492 usbvision_frames_free(usbvision);
Thierry MERLE6f78e182007-02-07 10:13:11 -0300493 usbvision_empty_framequeues(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300494 usbvision_scratch_free(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300495
496 usbvision->user--;
497
498 if (PowerOnAtOpen) {
Thierry MERLEc5f48362007-05-08 17:22:29 -0300499 /* power off in a little while
500 to avoid off/on every close/open short sequences */
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300501 usbvision_set_powerOffTimer(usbvision);
502 usbvision->initialized = 0;
503 }
504
505 up(&usbvision->lock);
506
507 if (usbvision->remove_pending) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -0300508 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300509 usbvision_release(usbvision);
510 }
511
512 PDEBUG(DBG_IO, "success");
513
514
515 return 0;
516}
517
518
519/*
520 * usbvision_ioctl()
521 *
522 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
523 *
524 */
Thierry MERLEc5f48362007-05-08 17:22:29 -0300525#ifdef CONFIG_VIDEO_ADV_DEBUG
526static int vidioc_g_register (struct file *file, void *priv,
527 struct v4l2_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300528{
529 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -0300530 struct usb_usbvision *usbvision =
531 (struct usb_usbvision *) video_get_drvdata(dev);
532 int errCode;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300533
Thierry MERLEc5f48362007-05-08 17:22:29 -0300534 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
535 return -EINVAL;
536 /* NT100x has a 8-bit register space */
537 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
538 if (errCode < 0) {
539 err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
540 __FUNCTION__, errCode);
541 return errCode;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300542 }
543 return 0;
544}
545
Thierry MERLEc5f48362007-05-08 17:22:29 -0300546static int vidioc_s_register (struct file *file, void *priv,
547 struct v4l2_register *reg)
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300548{
Thierry MERLEc5f48362007-05-08 17:22:29 -0300549 struct video_device *dev = video_devdata(file);
550 struct usb_usbvision *usbvision =
551 (struct usb_usbvision *) video_get_drvdata(dev);
552 int errCode;
553
554 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
555 return -EINVAL;
556 /* NT100x has a 8-bit register space */
557 reg->val = (u8)usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
558 if (reg->val < 0) {
559 err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
560 __FUNCTION__, errCode);
561 return errCode;
562 }
563 return 0;
564}
565#endif
566
567static int vidioc_querycap (struct file *file, void *priv,
568 struct v4l2_capability *vc)
569{
570 struct video_device *dev = video_devdata(file);
571 struct usb_usbvision *usbvision =
572 (struct usb_usbvision *) video_get_drvdata(dev);
573
574 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
575 strlcpy(vc->card,
576 usbvision_device_data[usbvision->DevModel].ModelString,
577 sizeof(vc->card));
578 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
579 sizeof(vc->bus_info));
580 vc->version = USBVISION_DRIVER_VERSION;
581 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
582 V4L2_CAP_AUDIO |
583 V4L2_CAP_READWRITE |
584 V4L2_CAP_STREAMING |
585 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
586 return 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -0300587}
588
Thierry MERLEc5f48362007-05-08 17:22:29 -0300589static int vidioc_enum_input (struct file *file, void *priv,
590 struct v4l2_input *vi)
591{
592 struct video_device *dev = video_devdata(file);
593 struct usb_usbvision *usbvision =
594 (struct usb_usbvision *) video_get_drvdata(dev);
595 int chan;
596
597 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
598 return -EINVAL;
599 if (usbvision->have_tuner) {
600 chan = vi->index;
601 } else {
602 chan = vi->index + 1; /*skip Television string*/
603 }
604 /* Determine the requested input characteristics
605 specific for each usbvision card model */
606 switch(chan) {
607 case 0:
608 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
609 strcpy(vi->name, "White Video Input");
610 } else {
611 strcpy(vi->name, "Television");
612 vi->type = V4L2_INPUT_TYPE_TUNER;
613 vi->audioset = 1;
614 vi->tuner = chan;
615 vi->std = USBVISION_NORMS;
616 }
617 break;
618 case 1:
619 vi->type = V4L2_INPUT_TYPE_CAMERA;
620 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
621 strcpy(vi->name, "Green Video Input");
622 } else {
623 strcpy(vi->name, "Composite Video Input");
624 }
625 vi->std = V4L2_STD_PAL;
626 break;
627 case 2:
628 vi->type = V4L2_INPUT_TYPE_CAMERA;
629 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
630 strcpy(vi->name, "Yellow Video Input");
631 } else {
632 strcpy(vi->name, "S-Video Input");
633 }
634 vi->std = V4L2_STD_PAL;
635 break;
636 case 3:
637 vi->type = V4L2_INPUT_TYPE_CAMERA;
638 strcpy(vi->name, "Red Video Input");
639 vi->std = V4L2_STD_PAL;
640 break;
641 }
642 return 0;
643}
644
645static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
646{
647 struct video_device *dev = video_devdata(file);
648 struct usb_usbvision *usbvision =
649 (struct usb_usbvision *) video_get_drvdata(dev);
650
651 *input = usbvision->ctl_input;
652 return 0;
653}
654
655static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
656{
657 struct video_device *dev = video_devdata(file);
658 struct usb_usbvision *usbvision =
659 (struct usb_usbvision *) video_get_drvdata(dev);
660
661 if ((input >= usbvision->video_inputs) || (input < 0) )
662 return -EINVAL;
663 usbvision->ctl_input = input;
664
665 down(&usbvision->lock);
666 usbvision_muxsel(usbvision, usbvision->ctl_input);
667 usbvision_set_input(usbvision);
668 usbvision_set_output(usbvision,
669 usbvision->curwidth,
670 usbvision->curheight);
671 up(&usbvision->lock);
672 return 0;
673}
674
675static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
676{
677 struct video_device *dev = video_devdata(file);
678 struct usb_usbvision *usbvision =
679 (struct usb_usbvision *) video_get_drvdata(dev);
680 usbvision->tvnormId=*id;
681
682 down(&usbvision->lock);
683 call_i2c_clients(usbvision, VIDIOC_S_STD,
684 &usbvision->tvnormId);
685 up(&usbvision->lock);
686
687 return 0;
688}
689
690static int vidioc_g_tuner (struct file *file, void *priv,
691 struct v4l2_tuner *vt)
692{
693 struct video_device *dev = video_devdata(file);
694 struct usb_usbvision *usbvision =
695 (struct usb_usbvision *) video_get_drvdata(dev);
696
697 if (!usbvision->have_tuner || vt->index) // Only tuner 0
698 return -EINVAL;
699 if(usbvision->radio) {
700 strcpy(vt->name, "Radio");
701 vt->type = V4L2_TUNER_RADIO;
702 } else {
703 strcpy(vt->name, "Television");
704 }
705 /* Let clients fill in the remainder of this struct */
706 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
707
708 return 0;
709}
710
711static int vidioc_s_tuner (struct file *file, void *priv,
712 struct v4l2_tuner *vt)
713{
714 struct video_device *dev = video_devdata(file);
715 struct usb_usbvision *usbvision =
716 (struct usb_usbvision *) video_get_drvdata(dev);
717
718 // Only no or one tuner for now
719 if (!usbvision->have_tuner || vt->index)
720 return -EINVAL;
721 /* let clients handle this */
722 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
723
724 return 0;
725}
726
727static int vidioc_g_frequency (struct file *file, void *priv,
728 struct v4l2_frequency *freq)
729{
730 struct video_device *dev = video_devdata(file);
731 struct usb_usbvision *usbvision =
732 (struct usb_usbvision *) video_get_drvdata(dev);
733
734 freq->tuner = 0; // Only one tuner
735 if(usbvision->radio) {
736 freq->type = V4L2_TUNER_RADIO;
737 } else {
738 freq->type = V4L2_TUNER_ANALOG_TV;
739 }
740 freq->frequency = usbvision->freq;
741
742 return 0;
743}
744
745static int vidioc_s_frequency (struct file *file, void *priv,
746 struct v4l2_frequency *freq)
747{
748 struct video_device *dev = video_devdata(file);
749 struct usb_usbvision *usbvision =
750 (struct usb_usbvision *) video_get_drvdata(dev);
751
752 // Only no or one tuner for now
753 if (!usbvision->have_tuner || freq->tuner)
754 return -EINVAL;
755
756 usbvision->freq = freq->frequency;
757 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
758
759 return 0;
760}
761
762static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
763{
764 struct video_device *dev = video_devdata(file);
765 struct usb_usbvision *usbvision =
766 (struct usb_usbvision *) video_get_drvdata(dev);
767
768 memset(a,0,sizeof(*a));
769 if(usbvision->radio) {
770 strcpy(a->name,"Radio");
771 } else {
772 strcpy(a->name, "TV");
773 }
774
775 return 0;
776}
777
778static int vidioc_s_audio (struct file *file, void *fh,
779 struct v4l2_audio *a)
780{
781 if(a->index) {
782 return -EINVAL;
783 }
784
785 return 0;
786}
787
788static int vidioc_queryctrl (struct file *file, void *priv,
789 struct v4l2_queryctrl *ctrl)
790{
791 struct video_device *dev = video_devdata(file);
792 struct usb_usbvision *usbvision =
793 (struct usb_usbvision *) video_get_drvdata(dev);
794 int id=ctrl->id;
795
796 memset(ctrl,0,sizeof(*ctrl));
797 ctrl->id=id;
798
799 call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
800
801 if (!ctrl->type)
802 return -EINVAL;
803
804 return 0;
805}
806
807static int vidioc_g_ctrl (struct file *file, void *priv,
808 struct v4l2_control *ctrl)
809{
810 struct video_device *dev = video_devdata(file);
811 struct usb_usbvision *usbvision =
812 (struct usb_usbvision *) video_get_drvdata(dev);
813 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
814
815 return 0;
816}
817
818static int vidioc_s_ctrl (struct file *file, void *priv,
819 struct v4l2_control *ctrl)
820{
821 struct video_device *dev = video_devdata(file);
822 struct usb_usbvision *usbvision =
823 (struct usb_usbvision *) video_get_drvdata(dev);
824 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
825
826 return 0;
827}
828
829static int vidioc_reqbufs (struct file *file,
830 void *priv, struct v4l2_requestbuffers *vr)
831{
832 struct video_device *dev = video_devdata(file);
833 struct usb_usbvision *usbvision =
834 (struct usb_usbvision *) video_get_drvdata(dev);
835 int ret;
836
837 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
838
839 /* Check input validity:
840 the user must do a VIDEO CAPTURE and MMAP method. */
841 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
842 (vr->memory != V4L2_MEMORY_MMAP))
843 return -EINVAL;
844
845 if(usbvision->streaming == Stream_On) {
846 if ((ret = usbvision_stream_interrupt(usbvision)))
847 return ret;
848 }
849
850 usbvision_frames_free(usbvision);
851 usbvision_empty_framequeues(usbvision);
852 vr->count = usbvision_frames_alloc(usbvision,vr->count);
853
854 usbvision->curFrame = NULL;
855
856 return 0;
857}
858
859static int vidioc_querybuf (struct file *file,
860 void *priv, struct v4l2_buffer *vb)
861{
862 struct video_device *dev = video_devdata(file);
863 struct usb_usbvision *usbvision =
864 (struct usb_usbvision *) video_get_drvdata(dev);
865 struct usbvision_frame *frame;
866
867 /* FIXME : must control
868 that buffers are mapped (VIDIOC_REQBUFS has been called) */
869 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
870 return -EINVAL;
871 }
872 if(vb->index>=usbvision->num_frames) {
873 return -EINVAL;
874 }
875 /* Updating the corresponding frame state */
876 vb->flags = 0;
877 frame = &usbvision->frame[vb->index];
878 if(frame->grabstate >= FrameState_Ready)
879 vb->flags |= V4L2_BUF_FLAG_QUEUED;
880 if(frame->grabstate >= FrameState_Done)
881 vb->flags |= V4L2_BUF_FLAG_DONE;
882 if(frame->grabstate == FrameState_Unused)
883 vb->flags |= V4L2_BUF_FLAG_MAPPED;
884 vb->memory = V4L2_MEMORY_MMAP;
885
886 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
887
888 vb->memory = V4L2_MEMORY_MMAP;
889 vb->field = V4L2_FIELD_NONE;
890 vb->length = usbvision->curwidth*
891 usbvision->curheight*
892 usbvision->palette.bytes_per_pixel;
893 vb->timestamp = usbvision->frame[vb->index].timestamp;
894 vb->sequence = usbvision->frame[vb->index].sequence;
895 return 0;
896}
897
898static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
899{
900 struct video_device *dev = video_devdata(file);
901 struct usb_usbvision *usbvision =
902 (struct usb_usbvision *) video_get_drvdata(dev);
903 struct usbvision_frame *frame;
904 unsigned long lock_flags;
905
906 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
907 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
908 return -EINVAL;
909 }
910 if(vb->index>=usbvision->num_frames) {
911 return -EINVAL;
912 }
913
914 frame = &usbvision->frame[vb->index];
915
916 if (frame->grabstate != FrameState_Unused) {
917 return -EAGAIN;
918 }
919
920 /* Mark it as ready and enqueue frame */
921 frame->grabstate = FrameState_Ready;
922 frame->scanstate = ScanState_Scanning;
923 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
924
925 vb->flags &= ~V4L2_BUF_FLAG_DONE;
926
927 /* set v4l2_format index */
928 frame->v4l2_format = usbvision->palette;
929
930 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
931 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
932 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
933
934 return 0;
935}
936
937static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
938{
939 struct video_device *dev = video_devdata(file);
940 struct usb_usbvision *usbvision =
941 (struct usb_usbvision *) video_get_drvdata(dev);
942 int ret;
943 struct usbvision_frame *f;
944 unsigned long lock_flags;
945
946 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
947 return -EINVAL;
948
949 if (list_empty(&(usbvision->outqueue))) {
950 if (usbvision->streaming == Stream_Idle)
951 return -EINVAL;
952 ret = wait_event_interruptible
953 (usbvision->wait_frame,
954 !list_empty(&(usbvision->outqueue)));
955 if (ret)
956 return ret;
957 }
958
959 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
960 f = list_entry(usbvision->outqueue.next,
961 struct usbvision_frame, frame);
962 list_del(usbvision->outqueue.next);
963 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
964
965 f->grabstate = FrameState_Unused;
966
967 vb->memory = V4L2_MEMORY_MMAP;
968 vb->flags = V4L2_BUF_FLAG_MAPPED |
969 V4L2_BUF_FLAG_QUEUED |
970 V4L2_BUF_FLAG_DONE;
971 vb->index = f->index;
972 vb->sequence = f->sequence;
973 vb->timestamp = f->timestamp;
974 vb->field = V4L2_FIELD_NONE;
975 vb->bytesused = f->scanlength;
976
977 return 0;
978}
979
980static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
981{
982 struct video_device *dev = video_devdata(file);
983 struct usb_usbvision *usbvision =
984 (struct usb_usbvision *) video_get_drvdata(dev);
985 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
986
987 usbvision->streaming = Stream_On;
988 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
989
990 return 0;
991}
992
993static int vidioc_streamoff(struct file *file,
994 void *priv, enum v4l2_buf_type type)
995{
996 struct video_device *dev = video_devdata(file);
997 struct usb_usbvision *usbvision =
998 (struct usb_usbvision *) video_get_drvdata(dev);
999 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
1000
1001 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1002 return -EINVAL;
1003
1004 if(usbvision->streaming == Stream_On) {
1005 usbvision_stream_interrupt(usbvision);
1006 /* Stop all video streamings */
1007 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
1008 }
1009 usbvision_empty_framequeues(usbvision);
1010
1011 return 0;
1012}
1013
1014static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1015 struct v4l2_fmtdesc *vfd)
1016{
1017 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
1018 return -EINVAL;
1019 }
1020 vfd->flags = 0;
1021 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1022 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
1023 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
1024 memset(vfd->reserved, 0, sizeof(vfd->reserved));
1025 return 0;
1026}
1027
1028static int vidioc_g_fmt_cap (struct file *file, void *priv,
1029 struct v4l2_format *vf)
1030{
1031 struct video_device *dev = video_devdata(file);
1032 struct usb_usbvision *usbvision =
1033 (struct usb_usbvision *) video_get_drvdata(dev);
1034 vf->fmt.pix.width = usbvision->curwidth;
1035 vf->fmt.pix.height = usbvision->curheight;
1036 vf->fmt.pix.pixelformat = usbvision->palette.format;
1037 vf->fmt.pix.bytesperline =
1038 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
1039 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
1040 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1041 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
1042
1043 return 0;
1044}
1045
1046static int vidioc_try_fmt_cap (struct file *file, void *priv,
1047 struct v4l2_format *vf)
1048{
1049 struct video_device *dev = video_devdata(file);
1050 struct usb_usbvision *usbvision =
1051 (struct usb_usbvision *) video_get_drvdata(dev);
1052 int formatIdx;
1053
1054 /* Find requested format in available ones */
1055 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1056 if(vf->fmt.pix.pixelformat ==
1057 usbvision_v4l2_format[formatIdx].format) {
1058 usbvision->palette = usbvision_v4l2_format[formatIdx];
1059 break;
1060 }
1061 }
1062 /* robustness */
1063 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1064 return -EINVAL;
1065 }
1066 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1067 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1068
1069 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
1070 usbvision->palette.bytes_per_pixel;
1071 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1072
1073 return 0;
1074}
1075
1076static int vidioc_s_fmt_cap(struct file *file, void *priv,
1077 struct v4l2_format *vf)
1078{
1079 struct video_device *dev = video_devdata(file);
1080 struct usb_usbvision *usbvision =
1081 (struct usb_usbvision *) video_get_drvdata(dev);
1082 int ret;
1083
1084 if( 0 != (ret=vidioc_try_fmt_cap (file, priv, vf)) ) {
1085 return ret;
1086 }
1087
1088 /* stop io in case it is already in progress */
1089 if(usbvision->streaming == Stream_On) {
1090 if ((ret = usbvision_stream_interrupt(usbvision)))
1091 return ret;
1092 }
1093 usbvision_frames_free(usbvision);
1094 usbvision_empty_framequeues(usbvision);
1095
1096 usbvision->curFrame = NULL;
1097
1098 /* by now we are committed to the new data... */
1099 down(&usbvision->lock);
1100 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1101 up(&usbvision->lock);
1102
1103 return 0;
1104}
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001105
Al Viro8a5ab412007-02-09 16:38:20 +00001106static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001107 size_t count, loff_t *ppos)
1108{
1109 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001110 struct usb_usbvision *usbvision =
1111 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001112 int noblock = file->f_flags & O_NONBLOCK;
1113 unsigned long lock_flags;
1114
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001115 int ret,i;
1116 struct usbvision_frame *frame;
1117
Thierry MERLEc5f48362007-05-08 17:22:29 -03001118 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__,
1119 (unsigned long)count, noblock);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001120
1121 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1122 return -EFAULT;
1123
Thierry MERLEc5f48362007-05-08 17:22:29 -03001124 /* This entry point is compatible with the mmap routines
1125 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1126 to get frames or call read on the device. */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001127 if(!usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001128 /* First, allocate some frames to work with
1129 if this has not been done with VIDIOC_REQBUF */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001130 usbvision_frames_free(usbvision);
1131 usbvision_empty_framequeues(usbvision);
1132 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1133 }
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001134
Thierry MERLE6f78e182007-02-07 10:13:11 -03001135 if(usbvision->streaming != Stream_On) {
1136 /* no stream is running, make it running ! */
1137 usbvision->streaming = Stream_On;
1138 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1139 }
1140
Thierry MERLEc5f48362007-05-08 17:22:29 -03001141 /* Then, enqueue as many frames as possible
1142 (like a user of VIDIOC_QBUF would do) */
Thierry MERLE6f78e182007-02-07 10:13:11 -03001143 for(i=0;i<usbvision->num_frames;i++) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001144 frame = &usbvision->frame[i];
1145 if(frame->grabstate == FrameState_Unused) {
1146 /* Mark it as ready and enqueue frame */
1147 frame->grabstate = FrameState_Ready;
1148 frame->scanstate = ScanState_Scanning;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001149 /* Accumulated in usbvision_parse_data() */
1150 frame->scanlength = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001151
1152 /* set v4l2_format index */
1153 frame->v4l2_format = usbvision->palette;
1154
1155 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1156 list_add_tail(&frame->frame, &usbvision->inqueue);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001157 spin_unlock_irqrestore(&usbvision->queue_lock,
1158 lock_flags);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001159 }
1160 }
1161
1162 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1163 if (list_empty(&(usbvision->outqueue))) {
1164 if(noblock)
1165 return -EAGAIN;
1166
1167 ret = wait_event_interruptible
1168 (usbvision->wait_frame,
1169 !list_empty(&(usbvision->outqueue)));
1170 if (ret)
1171 return ret;
1172 }
1173
1174 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1175 frame = list_entry(usbvision->outqueue.next,
1176 struct usbvision_frame, frame);
1177 list_del(usbvision->outqueue.next);
1178 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1179
1180 /* An error returns an empty frame */
1181 if (frame->grabstate == FrameState_Error) {
1182 frame->bytes_read = 0;
1183 return 0;
1184 }
1185
Thierry MERLEc5f48362007-05-08 17:22:29 -03001186 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1187 __FUNCTION__,
1188 frame->index, frame->bytes_read, frame->scanlength);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001189
1190 /* copy bytes to user space; we allow for partials reads */
1191 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1192 count = frame->scanlength - frame->bytes_read;
1193
1194 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1195 return -EFAULT;
1196 }
1197
1198 frame->bytes_read += count;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001199 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1200 __FUNCTION__,
1201 (unsigned long)count, frame->bytes_read);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001202
Thierry MERLEc5f48362007-05-08 17:22:29 -03001203 /* For now, forget the frame if it has not been read in one shot. */
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001204/* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1205 frame->bytes_read = 0;
1206
1207 /* Mark it as available to be used again. */
Thierry MERLEc430ca12006-12-29 20:32:08 -03001208 frame->grabstate = FrameState_Unused;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001209/* } */
1210
1211 return count;
1212}
1213
1214static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1215{
1216 unsigned long size = vma->vm_end - vma->vm_start,
1217 start = vma->vm_start;
1218 void *pos;
1219 u32 i;
1220
1221 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001222 struct usb_usbvision *usbvision =
1223 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001224
Thierry MERLE6f78e182007-02-07 10:13:11 -03001225 PDEBUG(DBG_MMAP, "mmap");
1226
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001227 down(&usbvision->lock);
1228
1229 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1230 up(&usbvision->lock);
1231 return -EFAULT;
1232 }
1233
1234 if (!(vma->vm_flags & VM_WRITE) ||
Thierry MERLE6f78e182007-02-07 10:13:11 -03001235 size != PAGE_ALIGN(usbvision->max_frame_size)) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001236 up(&usbvision->lock);
1237 return -EINVAL;
1238 }
1239
Thierry MERLE6f78e182007-02-07 10:13:11 -03001240 for (i = 0; i < usbvision->num_frames; i++) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001241 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1242 vma->vm_pgoff)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001243 break;
1244 }
Thierry MERLE6f78e182007-02-07 10:13:11 -03001245 if (i == usbvision->num_frames) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001246 PDEBUG(DBG_MMAP,
1247 "mmap: user supplied mapping address is out of range");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001248 up(&usbvision->lock);
1249 return -EINVAL;
1250 }
1251
1252 /* VM_IO is eventually going to replace PageReserved altogether */
1253 vma->vm_flags |= VM_IO;
1254 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1255
1256 pos = usbvision->frame[i].data;
1257 while (size > 0) {
1258
1259 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
Thierry MERLEc876a342006-12-09 16:42:54 -03001260 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001261 up(&usbvision->lock);
1262 return -EAGAIN;
1263 }
1264 start += PAGE_SIZE;
1265 pos += PAGE_SIZE;
1266 size -= PAGE_SIZE;
1267 }
1268
1269 up(&usbvision->lock);
1270 return 0;
1271}
1272
1273
1274/*
1275 * Here comes the stuff for radio on usbvision based devices
1276 *
1277 */
1278static int usbvision_radio_open(struct inode *inode, struct file *file)
1279{
1280 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001281 struct usb_usbvision *usbvision =
1282 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001283 int errCode = 0;
1284
1285 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1286
1287 down(&usbvision->lock);
1288
1289 if (usbvision->user) {
1290 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1291 errCode = -EBUSY;
1292 }
1293 else {
1294 if(PowerOnAtOpen) {
1295 usbvision_reset_powerOffTimer(usbvision);
1296 if (usbvision->power == 0) {
1297 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001298 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001299 }
1300 }
1301
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001302 /* Alternate interface 1 is is the biggest frame size */
1303 errCode = usbvision_set_alternate(usbvision);
1304 if (errCode < 0) {
1305 usbvision->last_error = errCode;
1306 return -EBUSY;
1307 }
1308
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001309 // If so far no errors then we shall start the radio
1310 usbvision->radio = 1;
1311 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001312 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1313 usbvision->user++;
1314 }
1315
1316 if (errCode) {
1317 if (PowerOnAtOpen) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001318 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001319 usbvision_power_off(usbvision);
1320 usbvision->initialized = 0;
1321 }
1322 }
1323 up(&usbvision->lock);
1324 return errCode;
1325}
1326
1327
1328static int usbvision_radio_close(struct inode *inode, struct file *file)
1329{
1330 struct video_device *dev = video_devdata(file);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001331 struct usb_usbvision *usbvision =
1332 (struct usb_usbvision *) video_get_drvdata(dev);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001333 int errCode = 0;
1334
1335 PDEBUG(DBG_IO, "");
1336
1337 down(&usbvision->lock);
1338
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001339 /* Set packet size to 0 */
1340 usbvision->ifaceAlt=0;
1341 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1342 usbvision->ifaceAlt);
1343
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001344 usbvision_audio_off(usbvision);
1345 usbvision->radio=0;
1346 usbvision->user--;
1347
1348 if (PowerOnAtOpen) {
1349 usbvision_set_powerOffTimer(usbvision);
1350 usbvision->initialized = 0;
1351 }
1352
1353 up(&usbvision->lock);
1354
1355 if (usbvision->remove_pending) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001356 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001357 usbvision_release(usbvision);
1358 }
1359
1360
1361 PDEBUG(DBG_IO, "success");
1362
1363 return errCode;
1364}
1365
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001366/*
1367 * Here comes the stuff for vbi on usbvision based devices
1368 *
1369 */
1370static int usbvision_vbi_open(struct inode *inode, struct file *file)
1371{
1372 /* TODO */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001373 return -ENODEV;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001374
1375}
1376
1377static int usbvision_vbi_close(struct inode *inode, struct file *file)
1378{
1379 /* TODO */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001380 return -ENODEV;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001381}
1382
1383static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1384 unsigned int cmd, void *arg)
1385{
1386 /* TODO */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001387 return -ENOIOCTLCMD;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001388}
1389
1390static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1391 unsigned int cmd, unsigned long arg)
1392{
1393 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1394}
1395
1396
1397//
1398// Video registration stuff
1399//
1400
1401// Video template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001402static const struct file_operations usbvision_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001403 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001404 .open = usbvision_v4l2_open,
1405 .release = usbvision_v4l2_close,
1406 .read = usbvision_v4l2_read,
1407 .mmap = usbvision_v4l2_mmap,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001408 .ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001409 .llseek = no_llseek,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001410/* .poll = video_poll, */
1411 .mmap = usbvision_v4l2_mmap,
1412 .compat_ioctl = v4l_compat_ioctl32,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001413};
1414static struct video_device usbvision_video_template = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001415 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001416 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1417 .hardware = VID_HARDWARE_USBVISION,
1418 .fops = &usbvision_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001419 .name = "usbvision-video",
1420 .release = video_device_release,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001421 .minor = -1,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001422 .vidioc_querycap = vidioc_querycap,
1423 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1424 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1425 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1426 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1427 .vidioc_reqbufs = vidioc_reqbufs,
1428 .vidioc_querybuf = vidioc_querybuf,
1429 .vidioc_qbuf = vidioc_qbuf,
1430 .vidioc_dqbuf = vidioc_dqbuf,
1431 .vidioc_s_std = vidioc_s_std,
1432 .vidioc_enum_input = vidioc_enum_input,
1433 .vidioc_g_input = vidioc_g_input,
1434 .vidioc_s_input = vidioc_s_input,
1435 .vidioc_queryctrl = vidioc_queryctrl,
1436 .vidioc_g_audio = vidioc_g_audio,
1437 .vidioc_g_audio = vidioc_s_audio,
1438 .vidioc_g_ctrl = vidioc_g_ctrl,
1439 .vidioc_s_ctrl = vidioc_s_ctrl,
1440 .vidioc_streamon = vidioc_streamon,
1441 .vidioc_streamoff = vidioc_streamoff,
1442#ifdef CONFIG_VIDEO_V4L1_COMPAT
1443/* .vidiocgmbuf = vidiocgmbuf, */
1444#endif
1445 .vidioc_g_tuner = vidioc_g_tuner,
1446 .vidioc_s_tuner = vidioc_s_tuner,
1447 .vidioc_g_frequency = vidioc_g_frequency,
1448 .vidioc_s_frequency = vidioc_s_frequency,
1449#ifdef CONFIG_VIDEO_ADV_DEBUG
1450 .vidioc_g_register = vidioc_g_register,
1451 .vidioc_s_register = vidioc_s_register,
1452#endif
1453 .tvnorms = USBVISION_NORMS,
1454 .current_norm = V4L2_STD_PAL
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001455};
1456
1457
1458// Radio template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001459static const struct file_operations usbvision_radio_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001460 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001461 .open = usbvision_radio_open,
1462 .release = usbvision_radio_close,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001463 .ioctl = video_ioctl2,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001464 .llseek = no_llseek,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001465 .compat_ioctl = v4l_compat_ioctl32,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001466};
1467
1468static struct video_device usbvision_radio_template=
1469{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001470 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001471 .type = VID_TYPE_TUNER,
1472 .hardware = VID_HARDWARE_USBVISION,
1473 .fops = &usbvision_radio_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001474 .name = "usbvision-radio",
Thierry MERLEc5f48362007-05-08 17:22:29 -03001475 .release = video_device_release,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001476 .minor = -1,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001477 .vidioc_querycap = vidioc_querycap,
1478 .vidioc_enum_input = vidioc_enum_input,
1479 .vidioc_g_input = vidioc_g_input,
1480 .vidioc_s_input = vidioc_s_input,
1481 .vidioc_queryctrl = vidioc_queryctrl,
1482 .vidioc_g_audio = vidioc_g_audio,
1483 .vidioc_g_audio = vidioc_s_audio,
1484 .vidioc_g_ctrl = vidioc_g_ctrl,
1485 .vidioc_s_ctrl = vidioc_s_ctrl,
1486 .vidioc_g_tuner = vidioc_g_tuner,
1487 .vidioc_s_tuner = vidioc_s_tuner,
1488 .vidioc_g_frequency = vidioc_g_frequency,
1489 .vidioc_s_frequency = vidioc_s_frequency,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001490
Thierry MERLEc5f48362007-05-08 17:22:29 -03001491 .tvnorms = USBVISION_NORMS,
1492 .current_norm = V4L2_STD_PAL
1493};
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001494
1495// vbi template
Arjan van de Venfa027c22007-02-12 00:55:33 -08001496static const struct file_operations usbvision_vbi_fops = {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001497 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001498 .open = usbvision_vbi_open,
1499 .release = usbvision_vbi_close,
1500 .ioctl = usbvision_vbi_ioctl,
1501 .llseek = no_llseek,
Thierry MERLEc5f48362007-05-08 17:22:29 -03001502 .compat_ioctl = v4l_compat_ioctl32,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001503};
1504
1505static struct video_device usbvision_vbi_template=
1506{
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001507 .owner = THIS_MODULE,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001508 .type = VID_TYPE_TUNER,
1509 .hardware = VID_HARDWARE_USBVISION,
1510 .fops = &usbvision_vbi_fops,
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001511 .release = video_device_release,
1512 .name = "usbvision-vbi",
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001513 .minor = -1,
1514};
1515
1516
1517static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1518 struct video_device *vdev_template,
1519 char *name)
1520{
1521 struct usb_device *usb_dev = usbvision->dev;
1522 struct video_device *vdev;
1523
1524 if (usb_dev == NULL) {
1525 err("%s: usbvision->dev is not set", __FUNCTION__);
1526 return NULL;
1527 }
1528
1529 vdev = video_device_alloc();
1530 if (NULL == vdev) {
1531 return NULL;
1532 }
1533 *vdev = *vdev_template;
1534// vdev->minor = -1;
1535 vdev->dev = &usb_dev->dev;
1536 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1537 video_set_drvdata(vdev, usbvision);
1538 return vdev;
1539}
1540
1541// unregister video4linux devices
1542static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1543{
1544 // vbi Device:
1545 if (usbvision->vbi) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001546 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
1547 usbvision->vbi->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001548 if (usbvision->vbi->minor != -1) {
1549 video_unregister_device(usbvision->vbi);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001550 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001551 video_device_release(usbvision->vbi);
1552 }
1553 usbvision->vbi = NULL;
1554 }
1555
1556 // Radio Device:
1557 if (usbvision->rdev) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001558 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
1559 usbvision->rdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001560 if (usbvision->rdev->minor != -1) {
1561 video_unregister_device(usbvision->rdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001562 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001563 video_device_release(usbvision->rdev);
1564 }
1565 usbvision->rdev = NULL;
1566 }
1567
1568 // Video Device:
1569 if (usbvision->vdev) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001570 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
1571 usbvision->vdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001572 if (usbvision->vdev->minor != -1) {
1573 video_unregister_device(usbvision->vdev);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001574 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001575 video_device_release(usbvision->vdev);
1576 }
1577 usbvision->vdev = NULL;
1578 }
1579}
1580
1581// register video4linux devices
1582static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1583{
1584 // Video Device:
Thierry MERLEc5f48362007-05-08 17:22:29 -03001585 usbvision->vdev = usbvision_vdev_init(usbvision,
1586 &usbvision_video_template,
1587 "USBVision Video");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001588 if (usbvision->vdev == NULL) {
1589 goto err_exit;
1590 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001591 if (video_register_device(usbvision->vdev,
1592 VFL_TYPE_GRABBER,
1593 video_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001594 goto err_exit;
1595 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001596 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
1597 usbvision->nr,usbvision->vdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001598
1599 // Radio Device:
1600 if (usbvision_device_data[usbvision->DevModel].Radio) {
1601 // usbvision has radio
Thierry MERLEc5f48362007-05-08 17:22:29 -03001602 usbvision->rdev = usbvision_vdev_init(usbvision,
1603 &usbvision_radio_template,
1604 "USBVision Radio");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001605 if (usbvision->rdev == NULL) {
1606 goto err_exit;
1607 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001608 if (video_register_device(usbvision->rdev,
1609 VFL_TYPE_RADIO,
1610 radio_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001611 goto err_exit;
1612 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001613 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
1614 usbvision->nr, usbvision->rdev->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001615 }
1616 // vbi Device:
1617 if (usbvision_device_data[usbvision->DevModel].vbi) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001618 usbvision->vbi = usbvision_vdev_init(usbvision,
1619 &usbvision_vbi_template,
1620 "USBVision VBI");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001621 if (usbvision->vdev == NULL) {
1622 goto err_exit;
1623 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001624 if (video_register_device(usbvision->vbi,
1625 VFL_TYPE_VBI,
1626 vbi_nr)<0) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001627 goto err_exit;
1628 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001629 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
1630 usbvision->nr,usbvision->vbi->minor & 0x1f);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001631 }
1632 // all done
1633 return 0;
1634
1635 err_exit:
1636 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1637 usbvision_unregister_video(usbvision);
1638 return -1;
1639}
1640
1641/*
1642 * usbvision_alloc()
1643 *
Thierry MERLEc5f48362007-05-08 17:22:29 -03001644 * This code allocates the struct usb_usbvision.
1645 * It is filled with default values.
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001646 *
1647 * Returns NULL on error, a pointer to usb_usbvision else.
1648 *
1649 */
1650static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1651{
1652 struct usb_usbvision *usbvision;
1653
Thierry MERLEc5f48362007-05-08 17:22:29 -03001654 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
1655 NULL) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001656 goto err_exit;
1657 }
1658
1659 usbvision->dev = dev;
1660
1661 init_MUTEX(&usbvision->lock); /* to 1 == available */
1662
1663 // prepare control urb for control messages during interrupts
1664 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1665 if (usbvision->ctrlUrb == NULL) {
1666 goto err_exit;
1667 }
1668 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1669 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1670
1671 usbvision_init_powerOffTimer(usbvision);
1672
1673 return usbvision;
1674
1675err_exit:
1676 if (usbvision && usbvision->ctrlUrb) {
1677 usb_free_urb(usbvision->ctrlUrb);
1678 }
1679 if (usbvision) {
1680 kfree(usbvision);
1681 }
1682 return NULL;
1683}
1684
1685/*
1686 * usbvision_release()
1687 *
1688 * This code does final release of struct usb_usbvision. This happens
1689 * after the device is disconnected -and- all clients closed their files.
1690 *
1691 */
1692static void usbvision_release(struct usb_usbvision *usbvision)
1693{
1694 PDEBUG(DBG_PROBE, "");
1695
1696 down(&usbvision->lock);
1697
1698 usbvision_reset_powerOffTimer(usbvision);
1699
1700 usbvision->initialized = 0;
1701
1702 up(&usbvision->lock);
1703
1704 usbvision_remove_sysfs(usbvision->vdev);
1705 usbvision_unregister_video(usbvision);
1706
1707 if (usbvision->ctrlUrb) {
1708 usb_free_urb(usbvision->ctrlUrb);
1709 }
1710
1711 kfree(usbvision);
1712
1713 PDEBUG(DBG_PROBE, "success");
1714}
1715
1716
Thierry MERLEc5f48362007-05-08 17:22:29 -03001717/*********************** usb interface **********************************/
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001718
1719static void usbvision_configure_video(struct usb_usbvision *usbvision)
1720{
Thierry MERLEc5f48362007-05-08 17:22:29 -03001721 int model;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001722
1723 if (usbvision == NULL)
1724 return;
1725
1726 model = usbvision->DevModel;
1727 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1728
Trent Piephoc682b3a2007-04-14 15:16:26 -03001729 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001730 usbvision->Vin_Reg2_Preset =
1731 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001732 } else {
1733 usbvision->Vin_Reg2_Preset = 0;
1734 }
1735
Thierry MERLEc5f48362007-05-08 17:22:29 -03001736 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001737
1738 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1739 usbvision->ctl_input = 0;
1740
1741 /* This should be here to make i2c clients to be able to register */
Thierry MERLEc5f48362007-05-08 17:22:29 -03001742 /* first switch off audio */
1743 usbvision_audio_off(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001744 if (!PowerOnAtOpen) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001745 /* and then power up the noisy tuner */
1746 usbvision_power_on(usbvision);
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001747 usbvision_i2c_register(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001748 }
1749}
1750
1751/*
1752 * usbvision_probe()
1753 *
1754 * This procedure queries device descriptor and accepts the interface
1755 * if it looks like USBVISION video device
1756 *
1757 */
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001758static int __devinit usbvision_probe(struct usb_interface *intf,
1759 const struct usb_device_id *devid)
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001760{
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001761 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1762 struct usb_interface *uif;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001763 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1764 const struct usb_host_interface *interface;
1765 struct usb_usbvision *usbvision = NULL;
1766 const struct usb_endpoint_descriptor *endpoint;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001767 int model,i;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001768
1769 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001770 dev->descriptor.idVendor,
1771 dev->descriptor.idProduct, ifnum);
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001772
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001773 model = devid->driver_info;
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001774 if ( (model<0) || (model>=usbvision_device_data_size) ) {
Thierry MERLE672d0132007-04-14 17:53:55 -03001775 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
Mauro Carvalho Chehabf8a389d2007-04-14 15:17:35 -03001776 return -ENODEV;
1777 }
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001778 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1779 usbvision_device_data[model].ModelString);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001780
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001781 if (usbvision_device_data[model].Interface >= 0) {
1782 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
Thierry MERLEc5f48362007-05-08 17:22:29 -03001783 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001784 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1785 }
1786 endpoint = &interface->endpoint[1].desc;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001787 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1788 USB_ENDPOINT_XFER_ISOC) {
1789 err("%s: interface %d. has non-ISO endpoint!",
1790 __FUNCTION__, ifnum);
1791 err("%s: Endpoint attributes %d",
1792 __FUNCTION__, endpoint->bmAttributes);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001793 return -ENODEV;
1794 }
Thierry MERLEc5f48362007-05-08 17:22:29 -03001795 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1796 USB_DIR_OUT) {
1797 err("%s: interface %d. has ISO OUT endpoint!",
1798 __FUNCTION__, ifnum);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001799 return -ENODEV;
1800 }
1801
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001802 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1803 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1804 return -ENOMEM;
1805 }
Mauro Carvalho Chehab659ae562007-04-14 15:09:59 -03001806
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001807 if (dev->descriptor.bNumConfigurations > 1) {
1808 usbvision->bridgeType = BRIDGE_NT1004;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001809 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001810 usbvision->bridgeType = BRIDGE_NT1005;
Thierry MERLEc5f48362007-05-08 17:22:29 -03001811 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001812 usbvision->bridgeType = BRIDGE_NT1003;
1813 }
1814 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1815
1816 down(&usbvision->lock);
1817
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001818 /* compute alternate max packet sizes */
1819 uif = dev->actconfig->interface[0];
1820
1821 usbvision->num_alt=uif->num_altsetting;
1822 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1823 usbvision->alt_max_pkt_size = kmalloc(32*
1824 usbvision->num_alt,GFP_KERNEL);
1825 if (usbvision->alt_max_pkt_size == NULL) {
1826 err("usbvision: out of memory!\n");
1827 return -ENOMEM;
1828 }
1829
1830 for (i = 0; i < usbvision->num_alt ; i++) {
1831 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1832 wMaxPacketSize);
1833 usbvision->alt_max_pkt_size[i] =
1834 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1835 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1836 usbvision->alt_max_pkt_size[i]);
1837 }
1838
1839
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001840 usbvision->nr = usbvision_nr++;
1841
1842 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1843 if (usbvision->have_tuner) {
1844 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1845 }
1846
1847 usbvision->tuner_addr = ADDR_UNSET;
1848
1849 usbvision->DevModel = model;
1850 usbvision->remove_pending = 0;
1851 usbvision->iface = ifnum;
Thierry MERLE2a9f8b52007-02-07 10:14:38 -03001852 usbvision->ifaceAlt = 0;
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001853 usbvision->video_endp = endpoint->bEndpointAddress;
1854 usbvision->isocPacketSize = 0;
1855 usbvision->usb_bandwidth = 0;
1856 usbvision->user = 0;
1857 usbvision->streaming = Stream_Off;
1858 usbvision_register_video(usbvision);
1859 usbvision_configure_video(usbvision);
1860 up(&usbvision->lock);
1861
1862
1863 usb_set_intfdata (intf, usbvision);
1864 usbvision_create_sysfs(usbvision->vdev);
1865
1866 PDEBUG(DBG_PROBE, "success");
1867 return 0;
1868}
1869
1870
1871/*
1872 * usbvision_disconnect()
1873 *
1874 * This procedure stops all driver activity, deallocates interface-private
1875 * structure (pointed by 'ptr') and after that driver should be removable
1876 * with no ill consequences.
1877 *
1878 */
1879static void __devexit usbvision_disconnect(struct usb_interface *intf)
1880{
1881 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1882
1883 PDEBUG(DBG_PROBE, "");
1884
1885 if (usbvision == NULL) {
1886 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1887 return;
1888 }
1889 usb_set_intfdata (intf, NULL);
1890
1891 down(&usbvision->lock);
1892
1893 // At this time we ask to cancel outstanding URBs
1894 usbvision_stop_isoc(usbvision);
1895
1896 if (usbvision->power) {
Thierry MERLE1ff16c22007-04-14 16:23:49 -03001897 usbvision_i2c_unregister(usbvision);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001898 usbvision_power_off(usbvision);
1899 }
1900 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1901
1902 usb_put_dev(usbvision->dev);
1903 usbvision->dev = NULL; // USB device is no more
1904
1905 up(&usbvision->lock);
1906
1907 if (usbvision->user) {
Thierry MERLEc5f48362007-05-08 17:22:29 -03001908 printk(KERN_INFO "%s: In use, disconnect pending\n",
1909 __FUNCTION__);
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001910 wake_up_interruptible(&usbvision->wait_frame);
1911 wake_up_interruptible(&usbvision->wait_stream);
Thierry MERLEc5f48362007-05-08 17:22:29 -03001912 } else {
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001913 usbvision_release(usbvision);
1914 }
1915
1916 PDEBUG(DBG_PROBE, "success");
1917
1918}
1919
1920static struct usb_driver usbvision_driver = {
1921 .name = "usbvision",
1922 .id_table = usbvision_table,
1923 .probe = usbvision_probe,
1924 .disconnect = usbvision_disconnect
1925};
1926
1927/*
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001928 * usbvision_init()
1929 *
1930 * This code is run to initialize the driver.
1931 *
1932 */
1933static int __init usbvision_init(void)
1934{
1935 int errCode;
1936
1937 PDEBUG(DBG_PROBE, "");
1938
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001939 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1940 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
Thierry MERLEc876a342006-12-09 16:42:54 -03001941 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001942
1943 /* disable planar mode support unless compression enabled */
1944 if (isocMode != ISOC_MODE_COMPRESS ) {
1945 // FIXME : not the right way to set supported flag
1946 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1947 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1948 }
1949
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001950 errCode = usb_register(&usbvision_driver);
1951
1952 if (errCode == 0) {
Dwaine Gardende6a1b82007-01-07 21:13:55 -03001953 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
Thierry MERLE483dfdb2006-12-04 08:31:45 -03001954 PDEBUG(DBG_PROBE, "success");
1955 }
1956 return errCode;
1957}
1958
1959static void __exit usbvision_exit(void)
1960{
1961 PDEBUG(DBG_PROBE, "");
1962
1963 usb_deregister(&usbvision_driver);
1964 PDEBUG(DBG_PROBE, "success");
1965}
1966
1967module_init(usbvision_init);
1968module_exit(usbvision_exit);
1969
1970/*
1971 * Overrides for Emacs so that we follow Linus's tabbing style.
1972 * ---------------------------------------------------------------------------
1973 * Local variables:
1974 * c-basic-offset: 8
1975 * End:
1976 */