2 * USB USBVISION Video device driver 0.9.9
6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
8 * This module is part of usbvision driver project.
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.
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.
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.
24 * Let's call the version 0.... until compression decoding is completely
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
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
39 * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one?
40 * - Clean up the driver.
41 * - optimization for performance.
42 * - Add Videotext capability (VBI). Working on it.....
43 * - Check audio for other devices
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/list.h>
50 #include <linux/timer.h>
51 #include <linux/slab.h>
53 #include <linux/utsname.h>
54 #include <linux/highmem.h>
55 #include <linux/smp_lock.h>
56 #include <linux/videodev.h>
57 #include <linux/vmalloc.h>
58 #include <linux/module.h>
59 #include <linux/init.h>
60 #include <linux/spinlock.h>
62 #include <linux/videodev2.h>
63 #include <linux/video_decoder.h>
64 #include <linux/i2c.h>
66 #include <media/saa7115.h>
67 #include <media/v4l2-common.h>
68 #include <media/tuner.h>
69 #include <media/audiochip.h>
71 #include <linux/moduleparam.h>
72 #include <linux/workqueue.h>
75 #include <linux/kmod.h>
78 #include "usbvision.h"
80 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
81 #define DRIVER_NAME "usbvision"
82 #define DRIVER_ALIAS "USBVision"
83 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
84 #define DRIVER_LICENSE "GPL"
85 #define USBVISION_DRIVER_VERSION_MAJOR 0
86 #define USBVISION_DRIVER_VERSION_MINOR 9
87 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
88 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
89 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
91 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
94 #define USBVISION_DEBUG /* Turn on debug messages */
96 #ifdef USBVISION_DEBUG
97 #define PDEBUG(level, fmt, args...) \
98 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
100 #define PDEBUG(level, fmt, args...) do {} while(0)
103 #define DBG_IOCTL 1<<0
105 #define DBG_PROBE 1<<2
106 #define DBG_FUNC 1<<3
109 #define rmspace(str) while(*str==' ') str++;
110 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
113 static int usbvision_nr = 0; // sequential number of usbvision device
115 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
116 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
117 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
118 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
119 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
120 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
121 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
122 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
123 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
126 /* supported tv norms */
127 static struct usbvision_tvnorm tvnorms[] = {
136 .id = V4L2_STD_SECAM,
139 .id = V4L2_STD_PAL_M,
143 #define TVNORMS ARRAY_SIZE(tvnorms)
145 // Function prototypes
146 static void usbvision_release(struct usb_usbvision *usbvision);
148 // Default initalization of device driver parameters
149 static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
150 static int video_debug = 0; // Set the default Debug Mode of the device driver
151 static int PowerOnAtOpen = 1; // Set the default device to power on at startup
152 static int video_nr = -1; // Sequential Number of Video Device
153 static int radio_nr = -1; // Sequential Number of Radio Device
154 static int vbi_nr = -1; // Sequential Number of VBI Device
155 static char *CustomDevice=NULL; // Set as nothing....
157 // Grab parameters for the device driver
159 #if defined(module_param) // Showing parameters under SYSFS
160 module_param(isocMode, int, 0444);
161 module_param(video_debug, int, 0444);
162 module_param(PowerOnAtOpen, int, 0444);
163 module_param(video_nr, int, 0444);
164 module_param(radio_nr, int, 0444);
165 module_param(vbi_nr, int, 0444);
166 module_param(CustomDevice, charp, 0444);
168 MODULE_PARAM(isocMode, "i");
169 MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
170 MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
171 MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
172 MODULE_PARM(SwitchSVideoInput, "i"); // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
173 MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
174 MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
175 MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
176 MODULE_PARM(CustomDevice, "s"); // .... CustomDevice
179 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
180 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
181 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
182 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
183 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
184 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
185 MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null");
189 MODULE_AUTHOR(DRIVER_AUTHOR);
190 MODULE_DESCRIPTION(DRIVER_DESC);
191 MODULE_LICENSE(DRIVER_LICENSE);
192 MODULE_VERSION(USBVISION_VERSION_STRING);
193 MODULE_ALIAS(DRIVER_ALIAS);
196 /****************************************************************************************/
197 /* SYSFS Code - Copied from the stv680.c usb module. */
198 /* Device information is located at /sys/class/video4linux/video0 */
199 /* Device parameters information is located at /sys/module/usbvision */
200 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
201 /****************************************************************************************/
204 #define YES_NO(x) ((x) ? "Yes" : "No")
206 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
208 struct video_device *vdev = to_video_device(cd);
209 return video_get_drvdata(vdev);
212 static ssize_t show_version(struct class_device *cd, char *buf)
214 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
216 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
218 static ssize_t show_model(struct class_device *class_dev, char *buf)
220 struct video_device *vdev = to_video_device(class_dev);
221 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
222 return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
224 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
226 static ssize_t show_hue(struct class_device *class_dev, char *buf)
228 struct video_device *vdev = to_video_device(class_dev);
229 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
230 struct v4l2_control ctrl;
231 ctrl.id = V4L2_CID_HUE;
233 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
234 return sprintf(buf, "%d\n", ctrl.value >> 8);
236 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
238 static ssize_t show_contrast(struct class_device *class_dev, char *buf)
240 struct video_device *vdev = to_video_device(class_dev);
241 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
242 struct v4l2_control ctrl;
243 ctrl.id = V4L2_CID_CONTRAST;
245 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
246 return sprintf(buf, "%d\n", ctrl.value >> 8);
248 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
250 static ssize_t show_brightness(struct class_device *class_dev, char *buf)
252 struct video_device *vdev = to_video_device(class_dev);
253 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
254 struct v4l2_control ctrl;
255 ctrl.id = V4L2_CID_BRIGHTNESS;
257 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
258 return sprintf(buf, "%d\n", ctrl.value >> 8);
260 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
262 static ssize_t show_saturation(struct class_device *class_dev, char *buf)
264 struct video_device *vdev = to_video_device(class_dev);
265 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
266 struct v4l2_control ctrl;
267 ctrl.id = V4L2_CID_SATURATION;
269 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
270 return sprintf(buf, "%d\n", ctrl.value >> 8);
272 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
274 static ssize_t show_streaming(struct class_device *class_dev, char *buf)
276 struct video_device *vdev = to_video_device(class_dev);
277 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
278 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
280 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
282 static ssize_t show_compression(struct class_device *class_dev, char *buf)
284 struct video_device *vdev = to_video_device(class_dev);
285 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
286 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
288 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
290 static ssize_t show_device_bridge(struct class_device *class_dev, char *buf)
292 struct video_device *vdev = to_video_device(class_dev);
293 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
294 return sprintf(buf, "%d\n", usbvision->bridgeType);
296 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
298 static void usbvision_create_sysfs(struct video_device *vdev)
302 res=video_device_create_file(vdev, &class_device_attr_version);
303 res=video_device_create_file(vdev, &class_device_attr_model);
304 res=video_device_create_file(vdev, &class_device_attr_hue);
305 res=video_device_create_file(vdev, &class_device_attr_contrast);
306 res=video_device_create_file(vdev, &class_device_attr_brightness);
307 res=video_device_create_file(vdev, &class_device_attr_saturation);
308 res=video_device_create_file(vdev, &class_device_attr_streaming);
309 res=video_device_create_file(vdev, &class_device_attr_compression);
310 res=video_device_create_file(vdev, &class_device_attr_bridge);
314 static void usbvision_remove_sysfs(struct video_device *vdev)
317 video_device_remove_file(vdev, &class_device_attr_version);
318 video_device_remove_file(vdev, &class_device_attr_model);
319 video_device_remove_file(vdev, &class_device_attr_hue);
320 video_device_remove_file(vdev, &class_device_attr_contrast);
321 video_device_remove_file(vdev, &class_device_attr_brightness);
322 video_device_remove_file(vdev, &class_device_attr_saturation);
323 video_device_remove_file(vdev, &class_device_attr_streaming);
324 video_device_remove_file(vdev, &class_device_attr_compression);
325 video_device_remove_file(vdev, &class_device_attr_bridge);
333 * This is part of Video 4 Linux API. The driver can be opened by one
334 * client only (checks internal counter 'usbvision->user'). The procedure
335 * then allocates buffers needed for video processing.
338 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
340 struct video_device *dev = video_devdata(file);
341 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
344 PDEBUG(DBG_IO, "open");
347 usbvision_reset_powerOffTimer(usbvision);
352 /* Allocate memory for the frame buffers */
353 errCode = usbvision_frames_alloc(usbvision);
355 /* Allocate memory for the scratch ring buffer */
356 errCode = usbvision_scratch_alloc(usbvision);
358 /* Allocate memory for the USB S buffers */
359 errCode = usbvision_sbuf_alloc(usbvision);
360 if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) {
361 /* Allocate intermediate decompression buffers only if needed */
362 errCode = usbvision_decompress_alloc(usbvision);
367 /* Deallocate all buffers if trouble */
368 usbvision_frames_free(usbvision);
369 usbvision_scratch_free(usbvision);
370 usbvision_sbuf_free(usbvision);
371 usbvision_decompress_free(usbvision);
375 /* If so far no errors then we shall start the camera */
377 down(&usbvision->lock);
378 if (usbvision->power == 0) {
379 usbvision_power_on(usbvision);
380 usbvision_init_i2c(usbvision);
383 /* Send init sequence only once, it's large! */
384 if (!usbvision->initialized) {
386 setup_ok = usbvision_setup(usbvision,isocMode);
388 usbvision->initialized = 1;
394 usbvision_begin_streaming(usbvision);
395 errCode = usbvision_init_isoc(usbvision);
396 /* device needs to be initialized before isoc transfer */
397 usbvision_muxsel(usbvision,0);
402 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
403 usbvision_power_off(usbvision);
404 usbvision->initialized = 0;
407 up(&usbvision->lock);
414 usbvision_empty_framequeues(usbvision);
416 PDEBUG(DBG_IO, "success");
421 * usbvision_v4l2_close()
423 * This is part of Video 4 Linux API. The procedure
424 * stops streaming and deallocates all buffers that were earlier
425 * allocated in usbvision_v4l2_open().
428 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
430 struct video_device *dev = video_devdata(file);
431 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
433 PDEBUG(DBG_IO, "close");
434 down(&usbvision->lock);
436 usbvision_audio_off(usbvision);
437 usbvision_restart_isoc(usbvision);
438 usbvision_stop_isoc(usbvision);
440 usbvision_decompress_free(usbvision);
441 usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
442 usbvision_scratch_free(usbvision);
443 usbvision_sbuf_free(usbvision);
448 /* power off in a little while to avoid off/on every close/open short sequences */
449 usbvision_set_powerOffTimer(usbvision);
450 usbvision->initialized = 0;
453 up(&usbvision->lock);
455 if (usbvision->remove_pending) {
456 info("%s: Final disconnect", __FUNCTION__);
457 usbvision_release(usbvision);
460 PDEBUG(DBG_IO, "success");
470 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
473 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
474 unsigned int cmd, void *arg)
476 struct video_device *dev = video_devdata(file);
477 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
479 if (!USBVISION_IS_OPERATIONAL(usbvision))
484 #ifdef CONFIG_VIDEO_ADV_DEBUG
485 /* ioctls to allow direct acces to the NT100x registers */
486 case VIDIOC_INT_G_REGISTER:
488 struct v4l2_register *reg = arg;
491 if (reg->i2c_id != 0)
493 /* NT100x has a 8-bit register space */
494 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
496 err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
499 reg->val=(unsigned char)errCode;
500 PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
501 (unsigned int)reg->reg, reg->val);
502 errCode = 0; // No error
506 case VIDIOC_INT_S_REGISTER:
508 struct v4l2_register *reg = arg;
511 if (reg->i2c_id != 0)
513 if (!capable(CAP_SYS_ADMIN))
515 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
517 err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
520 PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
521 (unsigned int)reg->reg, reg->val);
527 case VIDIOC_QUERYCAP:
529 struct v4l2_capability *vc=arg;
531 memset(vc, 0, sizeof(*vc));
532 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
533 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
535 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
536 sizeof(vc->bus_info));
537 vc->version = USBVISION_DRIVER_VERSION;
538 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
542 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
543 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
546 case VIDIOC_ENUMINPUT:
548 struct v4l2_input *vi = arg;
551 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
553 if (usbvision->have_tuner) {
557 chan = vi->index + 1; //skip Television string
561 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
562 strcpy(vi->name, "White Video Input");
565 strcpy(vi->name, "Television");
566 vi->type = V4L2_INPUT_TYPE_TUNER;
569 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
573 vi->type = V4L2_INPUT_TYPE_CAMERA;
574 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
575 strcpy(vi->name, "Green Video Input");
578 strcpy(vi->name, "Composite Video Input");
580 vi->std = V4L2_STD_PAL;
583 vi->type = V4L2_INPUT_TYPE_CAMERA;
584 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
585 strcpy(vi->name, "Yellow Video Input");
588 strcpy(vi->name, "S-Video Input");
590 vi->std = V4L2_STD_PAL;
593 vi->type = V4L2_INPUT_TYPE_CAMERA;
594 strcpy(vi->name, "Red Video Input");
595 vi->std = V4L2_STD_PAL;
598 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
599 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
604 struct v4l2_standard *e = arg;
611 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
612 tvnorms[e->index].name);
621 *input = usbvision->ctl_input;
627 if ((*input >= usbvision->video_inputs) || (*input < 0) )
629 usbvision->ctl_input = *input;
631 down(&usbvision->lock);
632 usbvision_muxsel(usbvision, usbvision->ctl_input);
633 usbvision_set_input(usbvision);
634 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
635 up(&usbvision->lock);
640 v4l2_std_id *id = arg;
642 *id = usbvision->tvnorm->id;
644 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
649 v4l2_std_id *id = arg;
652 for (i = 0; i < TVNORMS; i++)
653 if (*id == tvnorms[i].id)
656 for (i = 0; i < TVNORMS; i++)
657 if (*id & tvnorms[i].id)
662 down(&usbvision->lock);
663 usbvision->tvnorm = &tvnorms[i];
665 call_i2c_clients(usbvision, VIDIOC_S_STD,
666 &usbvision->tvnorm->id);
668 up(&usbvision->lock);
670 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
675 struct v4l2_tuner *vt = arg;
677 if (!usbvision->have_tuner || vt->index) // Only tuner 0
679 strcpy(vt->name, "Television");
680 /* Let clients fill in the remainder of this struct */
681 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
683 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
688 struct v4l2_tuner *vt = arg;
690 // Only no or one tuner for now
691 if (!usbvision->have_tuner || vt->index)
693 /* let clients handle this */
694 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
696 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
699 case VIDIOC_G_FREQUENCY:
701 struct v4l2_frequency *freq = arg;
703 freq->tuner = 0; // Only one tuner
704 freq->type = V4L2_TUNER_ANALOG_TV;
705 freq->frequency = usbvision->freq;
706 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
709 case VIDIOC_S_FREQUENCY:
711 struct v4l2_frequency *freq = arg;
713 // Only no or one tuner for now
714 if (!usbvision->have_tuner || freq->tuner)
717 usbvision->freq = freq->frequency;
718 call_i2c_clients(usbvision, cmd, freq);
719 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
724 struct v4l2_audio *v = arg;
725 memset(v,0, sizeof(v));
726 strcpy(v->name, "TV");
727 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
732 struct v4l2_audio *v = arg;
736 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
739 case VIDIOC_QUERYCTRL:
741 struct v4l2_queryctrl *ctrl = arg;
744 memset(ctrl,0,sizeof(*ctrl));
747 call_i2c_clients(usbvision, cmd, arg);
754 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
758 struct v4l2_control *ctrl = arg;
759 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
760 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
765 struct v4l2_control *ctrl = arg;
767 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
768 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
773 struct v4l2_requestbuffers *vr = arg;
776 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
778 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
779 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
780 (vr->memory != V4L2_MEMORY_MMAP))
783 if(usbvision->streaming == Stream_On) {
784 if ((ret = usbvision_stream_interrupt(usbvision)))
788 usbvision_empty_framequeues(usbvision);
790 usbvision->curFrame = NULL;
792 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
795 case VIDIOC_QUERYBUF:
797 struct v4l2_buffer *vb = arg;
798 struct usbvision_frame *frame;
800 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
802 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
805 if(vb->index>=USBVISION_NUMFRAMES) {
808 // Updating the corresponding frame state
810 frame = &usbvision->frame[vb->index];
811 if(frame->grabstate >= FrameState_Ready)
812 vb->flags |= V4L2_BUF_FLAG_QUEUED;
813 if(frame->grabstate >= FrameState_Done)
814 vb->flags |= V4L2_BUF_FLAG_DONE;
815 if(frame->grabstate == FrameState_Unused)
816 vb->flags |= V4L2_BUF_FLAG_MAPPED;
817 vb->memory = V4L2_MEMORY_MMAP;
819 vb->m.offset = vb->index*usbvision->max_frame_size;
821 vb->memory = V4L2_MEMORY_MMAP;
822 vb->field = V4L2_FIELD_NONE;
823 vb->length = usbvision->max_frame_size;
824 vb->timestamp = usbvision->frame[vb->index].timestamp;
825 vb->sequence = usbvision->frame[vb->index].sequence;
830 struct v4l2_buffer *vb = arg;
831 struct usbvision_frame *frame;
832 unsigned long lock_flags;
834 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
835 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
838 if(vb->index>=USBVISION_NUMFRAMES) {
842 frame = &usbvision->frame[vb->index];
844 if (frame->grabstate != FrameState_Unused) {
848 /* Mark it as ready and enqueue frame */
849 frame->grabstate = FrameState_Ready;
850 frame->scanstate = ScanState_Scanning;
851 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
853 vb->flags &= ~V4L2_BUF_FLAG_DONE;
855 /* set v4l2_format index */
856 frame->v4l2_format = usbvision->palette;
858 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
859 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
860 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
862 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
867 struct v4l2_buffer *vb = arg;
869 struct usbvision_frame *f;
870 unsigned long lock_flags;
872 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
875 if (list_empty(&(usbvision->outqueue))) {
876 if (usbvision->streaming == Stream_Idle)
878 ret = wait_event_interruptible
879 (usbvision->wait_frame,
880 !list_empty(&(usbvision->outqueue)));
885 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
886 f = list_entry(usbvision->outqueue.next,
887 struct usbvision_frame, frame);
888 list_del(usbvision->outqueue.next);
889 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
891 f->grabstate = FrameState_Unused;
893 vb->memory = V4L2_MEMORY_MMAP;
894 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
895 vb->index = f->index;
896 vb->sequence = f->sequence;
897 vb->timestamp = f->timestamp;
898 vb->field = V4L2_FIELD_NONE;
899 vb->bytesused = f->scanlength;
903 case VIDIOC_STREAMON:
905 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
907 usbvision->streaming = Stream_On;
909 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
911 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
915 case VIDIOC_STREAMOFF:
918 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
920 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
923 if(usbvision->streaming == Stream_On) {
924 usbvision_stream_interrupt(usbvision);
925 // Stop all video streamings
926 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
928 usbvision_empty_framequeues(usbvision);
930 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
933 case VIDIOC_ENUM_FMT:
935 struct v4l2_fmtdesc *vfd = arg;
937 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
941 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
942 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
943 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
944 memset(vfd->reserved, 0, sizeof(vfd->reserved));
949 struct v4l2_format *vf = arg;
952 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
954 vf->fmt.pix.width = usbvision->curwidth;
955 vf->fmt.pix.height = usbvision->curheight;
956 vf->fmt.pix.pixelformat = usbvision->palette.format;
957 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
958 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
959 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
960 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
964 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
967 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d",vf->fmt.win.w.width, vf->fmt.win.w.height);
973 struct v4l2_format *vf = arg;
977 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
979 /* Find requested format in available ones */
980 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
981 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
982 usbvision->palette = usbvision_v4l2_format[formatIdx];
987 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
990 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
991 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
993 /* stop io in case it is already in progress */
994 if(usbvision->streaming == Stream_On) {
995 if ((ret = usbvision_stream_interrupt(usbvision)))
998 usbvision_empty_framequeues(usbvision);
1000 usbvision->curFrame = NULL;
1002 // by now we are committed to the new data...
1003 down(&usbvision->lock);
1004 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1005 up(&usbvision->lock);
1007 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1008 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1016 return -ENOIOCTLCMD;
1021 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1022 unsigned int cmd, unsigned long arg)
1024 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1028 static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
1029 size_t count, loff_t *ppos)
1031 struct video_device *dev = video_devdata(file);
1032 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1033 int noblock = file->f_flags & O_NONBLOCK;
1034 unsigned long lock_flags;
1038 struct usbvision_frame *frame;
1040 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1042 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1045 /* no stream is running, make it running ! */
1046 usbvision->streaming = Stream_On;
1047 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1049 /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1050 for(i=0;i<USBVISION_NUMFRAMES;i++) {
1051 frame = &usbvision->frame[i];
1052 if(frame->grabstate == FrameState_Unused) {
1053 /* Mark it as ready and enqueue frame */
1054 frame->grabstate = FrameState_Ready;
1055 frame->scanstate = ScanState_Scanning;
1056 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1058 /* set v4l2_format index */
1059 frame->v4l2_format = usbvision->palette;
1061 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1062 list_add_tail(&frame->frame, &usbvision->inqueue);
1063 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1067 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1068 if (list_empty(&(usbvision->outqueue))) {
1072 ret = wait_event_interruptible
1073 (usbvision->wait_frame,
1074 !list_empty(&(usbvision->outqueue)));
1079 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1080 frame = list_entry(usbvision->outqueue.next,
1081 struct usbvision_frame, frame);
1082 list_del(usbvision->outqueue.next);
1083 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1085 /* An error returns an empty frame */
1086 if (frame->grabstate == FrameState_Error) {
1087 frame->bytes_read = 0;
1091 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1092 frame->index, frame->bytes_read, frame->scanlength);
1094 /* copy bytes to user space; we allow for partials reads */
1095 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1096 count = frame->scanlength - frame->bytes_read;
1098 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1102 frame->bytes_read += count;
1103 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1104 (unsigned long)count, frame->bytes_read);
1106 // For now, forget the frame if it has not been read in one shot.
1107 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1108 frame->bytes_read = 0;
1110 /* Mark it as available to be used again. */
1111 usbvision->frame[frmx].grabstate = FrameState_Unused;
1117 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1119 unsigned long size = vma->vm_end - vma->vm_start,
1120 start = vma->vm_start;
1124 struct video_device *dev = video_devdata(file);
1125 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1127 down(&usbvision->lock);
1129 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1130 up(&usbvision->lock);
1134 if (!(vma->vm_flags & VM_WRITE) ||
1135 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1136 up(&usbvision->lock);
1140 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1141 if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1144 if (i == USBVISION_NUMFRAMES) {
1145 PDEBUG(DBG_FUNC, "mmap: user supplied mapping address is out of range");
1146 up(&usbvision->lock);
1150 /* VM_IO is eventually going to replace PageReserved altogether */
1151 vma->vm_flags |= VM_IO;
1152 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1154 pos = usbvision->frame[i].data;
1157 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1158 PDEBUG(DBG_FUNC, "mmap: vm_insert_page failed");
1159 up(&usbvision->lock);
1167 up(&usbvision->lock);
1173 * Here comes the stuff for radio on usbvision based devices
1176 static int usbvision_radio_open(struct inode *inode, struct file *file)
1178 struct video_device *dev = video_devdata(file);
1179 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1180 struct v4l2_frequency freq;
1183 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1185 down(&usbvision->lock);
1187 if (usbvision->user) {
1188 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1193 usbvision_reset_powerOffTimer(usbvision);
1194 if (usbvision->power == 0) {
1195 usbvision_power_on(usbvision);
1196 usbvision_init_i2c(usbvision);
1200 // If so far no errors then we shall start the radio
1201 usbvision->radio = 1;
1202 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1203 freq.frequency = 1517; //SWR3 @ 94.8MHz
1204 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1205 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1210 if (PowerOnAtOpen) {
1211 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1212 usbvision_power_off(usbvision);
1213 usbvision->initialized = 0;
1216 up(&usbvision->lock);
1221 static int usbvision_radio_close(struct inode *inode, struct file *file)
1223 struct video_device *dev = video_devdata(file);
1224 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1229 down(&usbvision->lock);
1231 usbvision_audio_off(usbvision);
1235 if (PowerOnAtOpen) {
1236 usbvision_set_powerOffTimer(usbvision);
1237 usbvision->initialized = 0;
1240 up(&usbvision->lock);
1242 if (usbvision->remove_pending) {
1243 info("%s: Final disconnect", __FUNCTION__);
1244 usbvision_release(usbvision);
1248 PDEBUG(DBG_IO, "success");
1253 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1254 unsigned int cmd, void *arg)
1256 struct video_device *dev = video_devdata(file);
1257 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1259 if (!USBVISION_IS_OPERATIONAL(usbvision))
1263 case VIDIOC_QUERYCAP:
1265 struct v4l2_capability *vc=arg;
1267 memset(vc, 0, sizeof(*vc));
1268 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1269 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1271 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1272 sizeof(vc->bus_info));
1273 vc->version = USBVISION_DRIVER_VERSION;
1274 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1275 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1278 case VIDIOC_QUERYCTRL:
1280 struct v4l2_queryctrl *ctrl = arg;
1283 memset(ctrl,0,sizeof(*ctrl));
1286 call_i2c_clients(usbvision, cmd, arg);
1287 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1297 struct v4l2_control *ctrl = arg;
1299 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1300 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1305 struct v4l2_control *ctrl = arg;
1307 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1308 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1311 case VIDIOC_G_TUNER:
1313 struct v4l2_tuner *t = arg;
1318 memset(t,0,sizeof(*t));
1319 strcpy(t->name, "Radio");
1320 t->type = V4L2_TUNER_RADIO;
1322 /* Let clients fill in the remainder of this struct */
1323 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1324 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1327 case VIDIOC_S_TUNER:
1329 struct v4l2_tuner *vt = arg;
1331 // Only no or one tuner for now
1332 if (!usbvision->have_tuner || vt->index)
1334 /* let clients handle this */
1335 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1337 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1340 case VIDIOC_G_AUDIO:
1342 struct v4l2_audio *a = arg;
1344 memset(a,0,sizeof(*a));
1345 strcpy(a->name,"Radio");
1346 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1349 case VIDIOC_S_AUDIO:
1350 case VIDIOC_S_INPUT:
1354 case VIDIOC_G_FREQUENCY:
1356 struct v4l2_frequency *f = arg;
1358 memset(f,0,sizeof(*f));
1360 f->type = V4L2_TUNER_RADIO;
1361 f->frequency = usbvision->freq;
1362 call_i2c_clients(usbvision, cmd, f);
1363 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1367 case VIDIOC_S_FREQUENCY:
1369 struct v4l2_frequency *f = arg;
1373 usbvision->freq = f->frequency;
1374 call_i2c_clients(usbvision, cmd, f);
1375 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1381 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1382 return -ENOIOCTLCMD;
1389 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1390 unsigned int cmd, unsigned long arg)
1392 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1397 * Here comes the stuff for vbi on usbvision based devices
1400 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1407 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1413 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1414 unsigned int cmd, void *arg)
1420 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1421 unsigned int cmd, unsigned long arg)
1423 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1428 // Video registration stuff
1432 static struct file_operations usbvision_fops = {
1433 .owner = THIS_MODULE,
1434 .open = usbvision_v4l2_open,
1435 .release = usbvision_v4l2_close,
1436 .read = usbvision_v4l2_read,
1437 .mmap = usbvision_v4l2_mmap,
1438 .ioctl = usbvision_v4l2_ioctl,
1439 .llseek = no_llseek,
1441 static struct video_device usbvision_video_template = {
1442 .owner = THIS_MODULE,
1443 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1444 .hardware = VID_HARDWARE_USBVISION,
1445 .fops = &usbvision_fops,
1446 .name = "usbvision-video",
1447 .release = video_device_release,
1453 static struct file_operations usbvision_radio_fops = {
1454 .owner = THIS_MODULE,
1455 .open = usbvision_radio_open,
1456 .release = usbvision_radio_close,
1457 .ioctl = usbvision_radio_ioctl,
1458 .llseek = no_llseek,
1461 static struct video_device usbvision_radio_template=
1463 .owner = THIS_MODULE,
1464 .type = VID_TYPE_TUNER,
1465 .hardware = VID_HARDWARE_USBVISION,
1466 .fops = &usbvision_radio_fops,
1467 .release = video_device_release,
1468 .name = "usbvision-radio",
1474 static struct file_operations usbvision_vbi_fops = {
1475 .owner = THIS_MODULE,
1476 .open = usbvision_vbi_open,
1477 .release = usbvision_vbi_close,
1478 .ioctl = usbvision_vbi_ioctl,
1479 .llseek = no_llseek,
1482 static struct video_device usbvision_vbi_template=
1484 .owner = THIS_MODULE,
1485 .type = VID_TYPE_TUNER,
1486 .hardware = VID_HARDWARE_USBVISION,
1487 .fops = &usbvision_vbi_fops,
1488 .release = video_device_release,
1489 .name = "usbvision-vbi",
1494 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1495 struct video_device *vdev_template,
1498 struct usb_device *usb_dev = usbvision->dev;
1499 struct video_device *vdev;
1501 if (usb_dev == NULL) {
1502 err("%s: usbvision->dev is not set", __FUNCTION__);
1506 vdev = video_device_alloc();
1510 *vdev = *vdev_template;
1511 // vdev->minor = -1;
1512 vdev->dev = &usb_dev->dev;
1513 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1514 video_set_drvdata(vdev, usbvision);
1518 // unregister video4linux devices
1519 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1522 if (usbvision->vbi) {
1523 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1524 if (usbvision->vbi->minor != -1) {
1525 video_unregister_device(usbvision->vbi);
1528 video_device_release(usbvision->vbi);
1530 usbvision->vbi = NULL;
1534 if (usbvision->rdev) {
1535 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1536 if (usbvision->rdev->minor != -1) {
1537 video_unregister_device(usbvision->rdev);
1540 video_device_release(usbvision->rdev);
1542 usbvision->rdev = NULL;
1546 if (usbvision->vdev) {
1547 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1548 if (usbvision->vdev->minor != -1) {
1549 video_unregister_device(usbvision->vdev);
1552 video_device_release(usbvision->vdev);
1554 usbvision->vdev = NULL;
1558 // register video4linux devices
1559 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1562 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1563 if (usbvision->vdev == NULL) {
1566 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1569 info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
1572 if (usbvision_device_data[usbvision->DevModel].Radio) {
1573 // usbvision has radio
1574 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1575 if (usbvision->rdev == NULL) {
1578 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1581 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
1584 if (usbvision_device_data[usbvision->DevModel].vbi) {
1585 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1586 if (usbvision->vdev == NULL) {
1589 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1592 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
1598 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1599 usbvision_unregister_video(usbvision);
1606 * This code allocates the struct usb_usbvision. It is filled with default values.
1608 * Returns NULL on error, a pointer to usb_usbvision else.
1611 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1613 struct usb_usbvision *usbvision;
1615 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1619 usbvision->dev = dev;
1621 init_MUTEX(&usbvision->lock); /* to 1 == available */
1623 // prepare control urb for control messages during interrupts
1624 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1625 if (usbvision->ctrlUrb == NULL) {
1628 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1629 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1631 usbvision_init_powerOffTimer(usbvision);
1636 if (usbvision && usbvision->ctrlUrb) {
1637 usb_free_urb(usbvision->ctrlUrb);
1646 * usbvision_release()
1648 * This code does final release of struct usb_usbvision. This happens
1649 * after the device is disconnected -and- all clients closed their files.
1652 static void usbvision_release(struct usb_usbvision *usbvision)
1654 PDEBUG(DBG_PROBE, "");
1656 down(&usbvision->lock);
1658 usbvision_reset_powerOffTimer(usbvision);
1660 usbvision->initialized = 0;
1662 up(&usbvision->lock);
1664 usbvision_remove_sysfs(usbvision->vdev);
1665 usbvision_unregister_video(usbvision);
1667 if (usbvision->ctrlUrb) {
1668 usb_free_urb(usbvision->ctrlUrb);
1673 PDEBUG(DBG_PROBE, "success");
1677 /******************************** usb interface *****************************************/
1679 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1683 if (usbvision == NULL)
1686 model = usbvision->DevModel;
1687 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1689 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1690 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1692 usbvision->Vin_Reg2_Preset = 0;
1695 for (i = 0; i < TVNORMS; i++)
1696 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1700 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1702 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1703 usbvision->ctl_input = 0;
1705 /* This should be here to make i2c clients to be able to register */
1706 usbvision_audio_off(usbvision); //first switch off audio
1707 if (!PowerOnAtOpen) {
1708 usbvision_power_on(usbvision); //and then power up the noisy tuner
1709 usbvision_init_i2c(usbvision);
1716 * This procedure queries device descriptor and accepts the interface
1717 * if it looks like USBVISION video device
1720 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1722 struct usb_device *dev = interface_to_usbdev(intf);
1723 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1724 const struct usb_host_interface *interface;
1725 struct usb_usbvision *usbvision = NULL;
1726 const struct usb_endpoint_descriptor *endpoint;
1729 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1730 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1731 /* Is it an USBVISION video dev? */
1733 for(model = 0; usbvision_device_data[model].idVendor; model++) {
1734 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1737 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1741 info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
1745 if (usbvision_device_data[model].idVendor == 0) {
1746 return -ENODEV; //no matching device
1748 if (usbvision_device_data[model].Interface >= 0) {
1749 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1752 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1754 endpoint = &interface->endpoint[1].desc;
1755 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1756 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1757 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1760 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1761 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1767 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1768 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1771 if (dev->descriptor.bNumConfigurations > 1) {
1772 usbvision->bridgeType = BRIDGE_NT1004;
1774 else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1775 usbvision->bridgeType = BRIDGE_NT1005;
1778 usbvision->bridgeType = BRIDGE_NT1003;
1780 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1782 down(&usbvision->lock);
1784 usbvision->nr = usbvision_nr++;
1786 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1787 if (usbvision->have_tuner) {
1788 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1791 usbvision->tuner_addr = ADDR_UNSET;
1793 usbvision->DevModel = model;
1794 usbvision->remove_pending = 0;
1795 usbvision->iface = ifnum;
1796 usbvision->ifaceAltInactive = 0;
1797 usbvision->ifaceAltActive = 1;
1798 usbvision->video_endp = endpoint->bEndpointAddress;
1799 usbvision->isocPacketSize = 0;
1800 usbvision->usb_bandwidth = 0;
1801 usbvision->user = 0;
1802 usbvision->streaming = Stream_Off;
1803 usbvision_register_video(usbvision);
1804 usbvision_configure_video(usbvision);
1805 up(&usbvision->lock);
1808 usb_set_intfdata (intf, usbvision);
1809 usbvision_create_sysfs(usbvision->vdev);
1811 PDEBUG(DBG_PROBE, "success");
1817 * usbvision_disconnect()
1819 * This procedure stops all driver activity, deallocates interface-private
1820 * structure (pointed by 'ptr') and after that driver should be removable
1821 * with no ill consequences.
1824 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1826 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1828 PDEBUG(DBG_PROBE, "");
1830 if (usbvision == NULL) {
1831 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1834 usb_set_intfdata (intf, NULL);
1836 down(&usbvision->lock);
1838 // At this time we ask to cancel outstanding URBs
1839 usbvision_stop_isoc(usbvision);
1841 if (usbvision->power) {
1842 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1843 usbvision_power_off(usbvision);
1845 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1847 usb_put_dev(usbvision->dev);
1848 usbvision->dev = NULL; // USB device is no more
1850 up(&usbvision->lock);
1852 if (usbvision->user) {
1853 info("%s: In use, disconnect pending", __FUNCTION__);
1854 wake_up_interruptible(&usbvision->wait_frame);
1855 wake_up_interruptible(&usbvision->wait_stream);
1858 usbvision_release(usbvision);
1861 PDEBUG(DBG_PROBE, "success");
1865 static struct usb_driver usbvision_driver = {
1866 .name = "usbvision",
1867 .id_table = usbvision_table,
1868 .probe = usbvision_probe,
1869 .disconnect = usbvision_disconnect
1873 * customdevice_process()
1875 * This procedure preprocesses CustomDevice parameter if any
1878 void customdevice_process(void)
1880 usbvision_device_data[0]=usbvision_device_data[1];
1881 usbvision_table[0]=usbvision_table[1];
1885 char *parse=CustomDevice;
1887 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1889 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1890 usbvision_device_data[0].idVendor;
1891 usbvision_device_data[0].idProduct;
1892 usbvision_device_data[0].Interface;
1893 usbvision_device_data[0].Codec;
1894 usbvision_device_data[0].VideoChannels;
1895 usbvision_device_data[0].VideoNorm;
1896 usbvision_device_data[0].AudioChannels;
1897 usbvision_device_data[0].Radio;
1898 usbvision_device_data[0].Tuner;
1899 usbvision_device_data[0].TunerType;
1900 usbvision_device_data[0].Vin_Reg1;
1901 usbvision_device_data[0].Vin_Reg2;
1902 usbvision_device_data[0].X_Offset;
1903 usbvision_device_data[0].Y_Offset;
1904 usbvision_device_data[0].Dvi_yuv;
1905 usbvision_device_data[0].ModelString;
1909 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1912 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1914 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1916 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1918 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1919 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1921 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1922 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1924 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1925 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1927 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1932 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
1933 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1937 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
1938 usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
1942 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
1943 usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
1947 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
1948 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1953 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
1955 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
1956 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
1958 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
1959 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
1961 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
1962 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
1964 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
1965 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
1967 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
1968 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
1970 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
1971 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
1973 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
1974 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
1976 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
1977 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
1978 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
1980 //add to usbvision_table also
1981 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
1982 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
1983 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
1993 * This code is run to initialize the driver.
1996 static int __init usbvision_init(void)
2000 PDEBUG(DBG_PROBE, "");
2002 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
2003 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
2004 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
2005 PDEBUG(DBG_FUNC, "FUNC debugging is enabled [video]");
2007 /* disable planar mode support unless compression enabled */
2008 if (isocMode != ISOC_MODE_COMPRESS ) {
2009 // FIXME : not the right way to set supported flag
2010 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2011 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2014 customdevice_process();
2016 errCode = usb_register(&usbvision_driver);
2019 info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
2020 PDEBUG(DBG_PROBE, "success");
2025 static void __exit usbvision_exit(void)
2027 PDEBUG(DBG_PROBE, "");
2029 usb_deregister(&usbvision_driver);
2030 PDEBUG(DBG_PROBE, "success");
2033 module_init(usbvision_init);
2034 module_exit(usbvision_exit);
2037 * Overrides for Emacs so that we follow Linus's tabbing style.
2038 * ---------------------------------------------------------------------------