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/version.h>
48 #include <linux/kernel.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 #ifdef USBVISION_DEBUG
95 #define PDEBUG(level, fmt, args...) \
96 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
98 #define PDEBUG(level, fmt, args...) do {} while(0)
101 #define DBG_IOCTL 1<<0
103 #define DBG_PROBE 1<<2
104 #define DBG_MMAP 1<<3
107 #define rmspace(str) while(*str==' ') str++;
108 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
111 static int usbvision_nr = 0; // sequential number of usbvision device
113 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
114 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
115 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
116 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
117 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
118 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
119 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
120 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
121 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
124 /* supported tv norms */
125 static struct usbvision_tvnorm tvnorms[] = {
134 .id = V4L2_STD_SECAM,
137 .id = V4L2_STD_PAL_M,
141 #define TVNORMS ARRAY_SIZE(tvnorms)
143 // Function prototypes
144 static void usbvision_release(struct usb_usbvision *usbvision);
146 // Default initalization of device driver parameters
147 static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
148 static int video_debug = 0; // Set the default Debug Mode of the device driver
149 static int PowerOnAtOpen = 1; // Set the default device to power on at startup
150 static int video_nr = -1; // Sequential Number of Video Device
151 static int radio_nr = -1; // Sequential Number of Radio Device
152 static int vbi_nr = -1; // Sequential Number of VBI Device
153 static char *CustomDevice=NULL; // Set as nothing....
155 // Grab parameters for the device driver
157 #if defined(module_param) // Showing parameters under SYSFS
158 module_param(isocMode, int, 0444);
159 module_param(video_debug, int, 0444);
160 module_param(PowerOnAtOpen, int, 0444);
161 module_param(video_nr, int, 0444);
162 module_param(radio_nr, int, 0444);
163 module_param(vbi_nr, int, 0444);
164 module_param(CustomDevice, charp, 0444);
166 MODULE_PARAM(isocMode, "i");
167 MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
168 MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
169 MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
170 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.
171 MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
172 MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
173 MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
174 MODULE_PARM(CustomDevice, "s"); // .... CustomDevice
177 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
178 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
179 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
180 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
181 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
182 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
183 MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null");
187 MODULE_AUTHOR(DRIVER_AUTHOR);
188 MODULE_DESCRIPTION(DRIVER_DESC);
189 MODULE_LICENSE(DRIVER_LICENSE);
190 MODULE_VERSION(USBVISION_VERSION_STRING);
191 MODULE_ALIAS(DRIVER_ALIAS);
194 /****************************************************************************************/
195 /* SYSFS Code - Copied from the stv680.c usb module. */
196 /* Device information is located at /sys/class/video4linux/video0 */
197 /* Device parameters information is located at /sys/module/usbvision */
198 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
199 /****************************************************************************************/
202 #define YES_NO(x) ((x) ? "Yes" : "No")
204 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
206 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
207 return video_get_drvdata(vdev);
210 static ssize_t show_version(struct class_device *cd, char *buf)
212 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
214 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
216 static ssize_t show_model(struct class_device *cd, char *buf)
218 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
219 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
220 return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
222 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
224 static ssize_t show_hue(struct class_device *cd, char *buf)
226 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
227 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
228 struct v4l2_control ctrl;
229 ctrl.id = V4L2_CID_HUE;
232 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
233 return sprintf(buf, "%d\n", ctrl.value);
235 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
237 static ssize_t show_contrast(struct class_device *cd, char *buf)
239 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
240 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
241 struct v4l2_control ctrl;
242 ctrl.id = V4L2_CID_CONTRAST;
245 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
246 return sprintf(buf, "%d\n", ctrl.value);
248 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
250 static ssize_t show_brightness(struct class_device *cd, char *buf)
252 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
253 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
254 struct v4l2_control ctrl;
255 ctrl.id = V4L2_CID_BRIGHTNESS;
258 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
259 return sprintf(buf, "%d\n", ctrl.value);
261 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
263 static ssize_t show_saturation(struct class_device *cd, char *buf)
265 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
266 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
267 struct v4l2_control ctrl;
268 ctrl.id = V4L2_CID_SATURATION;
271 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
272 return sprintf(buf, "%d\n", ctrl.value);
274 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
276 static ssize_t show_streaming(struct class_device *cd, char *buf)
278 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
279 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
280 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
282 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
284 static ssize_t show_compression(struct class_device *cd, char *buf)
286 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
287 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
288 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
290 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
292 static ssize_t show_device_bridge(struct class_device *cd, char *buf)
294 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
295 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
296 return sprintf(buf, "%d\n", usbvision->bridgeType);
298 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
300 static void usbvision_create_sysfs(struct video_device *vdev)
306 res=class_device_create_file(&vdev->class_dev,
307 &class_device_attr_version);
310 res=class_device_create_file(&vdev->class_dev,
311 &class_device_attr_model);
314 res=class_device_create_file(&vdev->class_dev,
315 &class_device_attr_hue);
318 res=class_device_create_file(&vdev->class_dev,
319 &class_device_attr_contrast);
322 res=class_device_create_file(&vdev->class_dev,
323 &class_device_attr_brightness);
326 res=class_device_create_file(&vdev->class_dev,
327 &class_device_attr_saturation);
330 res=class_device_create_file(&vdev->class_dev,
331 &class_device_attr_streaming);
334 res=class_device_create_file(&vdev->class_dev,
335 &class_device_attr_compression);
338 res=class_device_create_file(&vdev->class_dev,
339 &class_device_attr_bridge);
344 err("%s error: %d\n", __FUNCTION__, res);
347 static void usbvision_remove_sysfs(struct video_device *vdev)
350 class_device_remove_file(&vdev->class_dev,
351 &class_device_attr_version);
352 class_device_remove_file(&vdev->class_dev,
353 &class_device_attr_model);
354 class_device_remove_file(&vdev->class_dev,
355 &class_device_attr_hue);
356 class_device_remove_file(&vdev->class_dev,
357 &class_device_attr_contrast);
358 class_device_remove_file(&vdev->class_dev,
359 &class_device_attr_brightness);
360 class_device_remove_file(&vdev->class_dev,
361 &class_device_attr_saturation);
362 class_device_remove_file(&vdev->class_dev,
363 &class_device_attr_streaming);
364 class_device_remove_file(&vdev->class_dev,
365 &class_device_attr_compression);
366 class_device_remove_file(&vdev->class_dev,
367 &class_device_attr_bridge);
375 * This is part of Video 4 Linux API. The driver can be opened by one
376 * client only (checks internal counter 'usbvision->user'). The procedure
377 * then allocates buffers needed for video processing.
380 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
382 struct video_device *dev = video_devdata(file);
383 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
386 PDEBUG(DBG_IO, "open");
389 usbvision_reset_powerOffTimer(usbvision);
394 /* Allocate memory for the scratch ring buffer */
395 errCode = usbvision_scratch_alloc(usbvision);
396 if (isocMode==ISOC_MODE_COMPRESS) {
397 /* Allocate intermediate decompression buffers only if needed */
398 errCode = usbvision_decompress_alloc(usbvision);
401 /* Deallocate all buffers if trouble */
402 usbvision_scratch_free(usbvision);
403 usbvision_decompress_free(usbvision);
407 /* If so far no errors then we shall start the camera */
409 down(&usbvision->lock);
410 if (usbvision->power == 0) {
411 usbvision_power_on(usbvision);
412 usbvision_init_i2c(usbvision);
415 /* Send init sequence only once, it's large! */
416 if (!usbvision->initialized) {
418 setup_ok = usbvision_setup(usbvision,isocMode);
420 usbvision->initialized = 1;
426 usbvision_begin_streaming(usbvision);
427 errCode = usbvision_init_isoc(usbvision);
428 /* device needs to be initialized before isoc transfer */
429 usbvision_muxsel(usbvision,0);
434 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
435 usbvision_power_off(usbvision);
436 usbvision->initialized = 0;
439 up(&usbvision->lock);
446 usbvision_empty_framequeues(usbvision);
448 PDEBUG(DBG_IO, "success");
453 * usbvision_v4l2_close()
455 * This is part of Video 4 Linux API. The procedure
456 * stops streaming and deallocates all buffers that were earlier
457 * allocated in usbvision_v4l2_open().
460 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
462 struct video_device *dev = video_devdata(file);
463 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
465 PDEBUG(DBG_IO, "close");
466 down(&usbvision->lock);
468 usbvision_audio_off(usbvision);
469 usbvision_restart_isoc(usbvision);
470 usbvision_stop_isoc(usbvision);
472 usbvision_decompress_free(usbvision);
473 usbvision_frames_free(usbvision);
474 usbvision_empty_framequeues(usbvision);
475 usbvision_scratch_free(usbvision);
480 /* power off in a little while to avoid off/on every close/open short sequences */
481 usbvision_set_powerOffTimer(usbvision);
482 usbvision->initialized = 0;
485 up(&usbvision->lock);
487 if (usbvision->remove_pending) {
488 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
489 usbvision_release(usbvision);
492 PDEBUG(DBG_IO, "success");
502 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
505 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
506 unsigned int cmd, void *arg)
508 struct video_device *dev = video_devdata(file);
509 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
511 if (!USBVISION_IS_OPERATIONAL(usbvision))
516 #ifdef CONFIG_VIDEO_ADV_DEBUG
517 /* ioctls to allow direct acces to the NT100x registers */
518 case VIDIOC_DBG_G_REGISTER:
519 case VIDIOC_DBG_S_REGISTER:
521 struct v4l2_register *reg = arg;
524 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
526 if (!capable(CAP_SYS_ADMIN))
528 /* NT100x has a 8-bit register space */
529 if (cmd == VIDIOC_DBG_G_REGISTER)
530 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
532 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
534 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__,
535 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S', errCode);
538 if (cmd == VIDIOC_DBG_S_REGISTER)
539 reg->val = (u8)errCode;
541 PDEBUG(DBG_IOCTL, "VIDIOC_DBG_%c_REGISTER reg=0x%02X, value=0x%02X",
542 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S',
543 (unsigned int)reg->reg, (unsigned int)reg->val);
547 case VIDIOC_QUERYCAP:
549 struct v4l2_capability *vc=arg;
551 memset(vc, 0, sizeof(*vc));
552 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
553 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
555 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
556 sizeof(vc->bus_info));
557 vc->version = USBVISION_DRIVER_VERSION;
558 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
562 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
563 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
566 case VIDIOC_ENUMINPUT:
568 struct v4l2_input *vi = arg;
571 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
573 if (usbvision->have_tuner) {
577 chan = vi->index + 1; //skip Television string
581 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
582 strcpy(vi->name, "White Video Input");
585 strcpy(vi->name, "Television");
586 vi->type = V4L2_INPUT_TYPE_TUNER;
589 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
593 vi->type = V4L2_INPUT_TYPE_CAMERA;
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "Green Video Input");
598 strcpy(vi->name, "Composite Video Input");
600 vi->std = V4L2_STD_PAL;
603 vi->type = V4L2_INPUT_TYPE_CAMERA;
604 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
605 strcpy(vi->name, "Yellow Video Input");
608 strcpy(vi->name, "S-Video Input");
610 vi->std = V4L2_STD_PAL;
613 vi->type = V4L2_INPUT_TYPE_CAMERA;
614 strcpy(vi->name, "Red Video Input");
615 vi->std = V4L2_STD_PAL;
618 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
619 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
624 struct v4l2_standard *e = arg;
631 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
632 tvnorms[e->index].name);
641 *input = usbvision->ctl_input;
647 if ((*input >= usbvision->video_inputs) || (*input < 0) )
649 usbvision->ctl_input = *input;
651 down(&usbvision->lock);
652 usbvision_muxsel(usbvision, usbvision->ctl_input);
653 usbvision_set_input(usbvision);
654 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
655 up(&usbvision->lock);
660 v4l2_std_id *id = arg;
662 *id = usbvision->tvnorm->id;
664 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
669 v4l2_std_id *id = arg;
672 for (i = 0; i < TVNORMS; i++)
673 if (*id == tvnorms[i].id)
676 for (i = 0; i < TVNORMS; i++)
677 if (*id & tvnorms[i].id)
682 down(&usbvision->lock);
683 usbvision->tvnorm = &tvnorms[i];
685 call_i2c_clients(usbvision, VIDIOC_S_STD,
686 &usbvision->tvnorm->id);
688 up(&usbvision->lock);
690 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
695 struct v4l2_tuner *vt = arg;
697 if (!usbvision->have_tuner || vt->index) // Only tuner 0
699 strcpy(vt->name, "Television");
700 /* Let clients fill in the remainder of this struct */
701 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
703 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
708 struct v4l2_tuner *vt = arg;
710 // Only no or one tuner for now
711 if (!usbvision->have_tuner || vt->index)
713 /* let clients handle this */
714 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
716 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
719 case VIDIOC_G_FREQUENCY:
721 struct v4l2_frequency *freq = arg;
723 freq->tuner = 0; // Only one tuner
724 freq->type = V4L2_TUNER_ANALOG_TV;
725 freq->frequency = usbvision->freq;
726 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
729 case VIDIOC_S_FREQUENCY:
731 struct v4l2_frequency *freq = arg;
733 // Only no or one tuner for now
734 if (!usbvision->have_tuner || freq->tuner)
737 usbvision->freq = freq->frequency;
738 call_i2c_clients(usbvision, cmd, freq);
739 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
744 struct v4l2_audio *v = arg;
745 memset(v,0, sizeof(v));
746 strcpy(v->name, "TV");
747 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
752 struct v4l2_audio *v = arg;
756 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
759 case VIDIOC_QUERYCTRL:
761 struct v4l2_queryctrl *ctrl = arg;
764 memset(ctrl,0,sizeof(*ctrl));
767 call_i2c_clients(usbvision, cmd, arg);
774 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
778 struct v4l2_control *ctrl = arg;
779 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
780 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
785 struct v4l2_control *ctrl = arg;
787 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
788 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
793 struct v4l2_requestbuffers *vr = arg;
796 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
798 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
799 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
800 (vr->memory != V4L2_MEMORY_MMAP))
803 if(usbvision->streaming == Stream_On) {
804 if ((ret = usbvision_stream_interrupt(usbvision)))
808 usbvision_frames_free(usbvision);
809 usbvision_empty_framequeues(usbvision);
810 vr->count = usbvision_frames_alloc(usbvision,vr->count);
812 usbvision->curFrame = NULL;
814 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
817 case VIDIOC_QUERYBUF:
819 struct v4l2_buffer *vb = arg;
820 struct usbvision_frame *frame;
822 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
824 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
827 if(vb->index>=usbvision->num_frames) {
830 // Updating the corresponding frame state
832 frame = &usbvision->frame[vb->index];
833 if(frame->grabstate >= FrameState_Ready)
834 vb->flags |= V4L2_BUF_FLAG_QUEUED;
835 if(frame->grabstate >= FrameState_Done)
836 vb->flags |= V4L2_BUF_FLAG_DONE;
837 if(frame->grabstate == FrameState_Unused)
838 vb->flags |= V4L2_BUF_FLAG_MAPPED;
839 vb->memory = V4L2_MEMORY_MMAP;
841 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
843 vb->memory = V4L2_MEMORY_MMAP;
844 vb->field = V4L2_FIELD_NONE;
845 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
846 vb->timestamp = usbvision->frame[vb->index].timestamp;
847 vb->sequence = usbvision->frame[vb->index].sequence;
852 struct v4l2_buffer *vb = arg;
853 struct usbvision_frame *frame;
854 unsigned long lock_flags;
856 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
857 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
860 if(vb->index>=usbvision->num_frames) {
864 frame = &usbvision->frame[vb->index];
866 if (frame->grabstate != FrameState_Unused) {
870 /* Mark it as ready and enqueue frame */
871 frame->grabstate = FrameState_Ready;
872 frame->scanstate = ScanState_Scanning;
873 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
875 vb->flags &= ~V4L2_BUF_FLAG_DONE;
877 /* set v4l2_format index */
878 frame->v4l2_format = usbvision->palette;
880 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
881 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
882 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
884 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
889 struct v4l2_buffer *vb = arg;
891 struct usbvision_frame *f;
892 unsigned long lock_flags;
894 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
897 if (list_empty(&(usbvision->outqueue))) {
898 if (usbvision->streaming == Stream_Idle)
900 ret = wait_event_interruptible
901 (usbvision->wait_frame,
902 !list_empty(&(usbvision->outqueue)));
907 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
908 f = list_entry(usbvision->outqueue.next,
909 struct usbvision_frame, frame);
910 list_del(usbvision->outqueue.next);
911 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
913 f->grabstate = FrameState_Unused;
915 vb->memory = V4L2_MEMORY_MMAP;
916 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
917 vb->index = f->index;
918 vb->sequence = f->sequence;
919 vb->timestamp = f->timestamp;
920 vb->field = V4L2_FIELD_NONE;
921 vb->bytesused = f->scanlength;
925 case VIDIOC_STREAMON:
927 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
929 usbvision->streaming = Stream_On;
931 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
933 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
937 case VIDIOC_STREAMOFF:
940 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
942 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945 if(usbvision->streaming == Stream_On) {
946 usbvision_stream_interrupt(usbvision);
947 // Stop all video streamings
948 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
950 usbvision_empty_framequeues(usbvision);
952 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
955 case VIDIOC_ENUM_FMT:
957 struct v4l2_fmtdesc *vfd = arg;
959 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
963 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
964 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
965 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
966 memset(vfd->reserved, 0, sizeof(vfd->reserved));
971 struct v4l2_format *vf = arg;
974 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
976 vf->fmt.pix.width = usbvision->curwidth;
977 vf->fmt.pix.height = usbvision->curheight;
978 vf->fmt.pix.pixelformat = usbvision->palette.format;
979 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
980 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
981 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
982 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
983 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
984 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
988 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
996 struct v4l2_format *vf = arg;
1000 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1002 /* Find requested format in available ones */
1003 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1004 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
1005 usbvision->palette = usbvision_v4l2_format[formatIdx];
1010 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1013 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1014 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1016 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
1017 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1019 if(cmd == VIDIOC_TRY_FMT) {
1020 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1021 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1025 /* stop io in case it is already in progress */
1026 if(usbvision->streaming == Stream_On) {
1027 if ((ret = usbvision_stream_interrupt(usbvision)))
1030 usbvision_frames_free(usbvision);
1031 usbvision_empty_framequeues(usbvision);
1033 usbvision->curFrame = NULL;
1035 // by now we are committed to the new data...
1036 down(&usbvision->lock);
1037 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1038 up(&usbvision->lock);
1040 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1041 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1049 return -ENOIOCTLCMD;
1054 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1055 unsigned int cmd, unsigned long arg)
1057 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1061 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1062 size_t count, loff_t *ppos)
1064 struct video_device *dev = video_devdata(file);
1065 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1066 int noblock = file->f_flags & O_NONBLOCK;
1067 unsigned long lock_flags;
1070 struct usbvision_frame *frame;
1072 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1074 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1077 /* This entry point is compatible with the mmap routines so that a user can do either
1078 VIDIOC_QBUF/VIDIOC_DQBUF to get frames or call read on the device. */
1079 if(!usbvision->num_frames) {
1080 /* First, allocate some frames to work with if this has not been done with
1082 usbvision_frames_free(usbvision);
1083 usbvision_empty_framequeues(usbvision);
1084 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1087 if(usbvision->streaming != Stream_On) {
1088 /* no stream is running, make it running ! */
1089 usbvision->streaming = Stream_On;
1090 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1093 /* Then, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1094 for(i=0;i<usbvision->num_frames;i++) {
1095 frame = &usbvision->frame[i];
1096 if(frame->grabstate == FrameState_Unused) {
1097 /* Mark it as ready and enqueue frame */
1098 frame->grabstate = FrameState_Ready;
1099 frame->scanstate = ScanState_Scanning;
1100 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1102 /* set v4l2_format index */
1103 frame->v4l2_format = usbvision->palette;
1105 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1106 list_add_tail(&frame->frame, &usbvision->inqueue);
1107 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1111 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1112 if (list_empty(&(usbvision->outqueue))) {
1116 ret = wait_event_interruptible
1117 (usbvision->wait_frame,
1118 !list_empty(&(usbvision->outqueue)));
1123 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1124 frame = list_entry(usbvision->outqueue.next,
1125 struct usbvision_frame, frame);
1126 list_del(usbvision->outqueue.next);
1127 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1129 /* An error returns an empty frame */
1130 if (frame->grabstate == FrameState_Error) {
1131 frame->bytes_read = 0;
1135 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1136 frame->index, frame->bytes_read, frame->scanlength);
1138 /* copy bytes to user space; we allow for partials reads */
1139 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1140 count = frame->scanlength - frame->bytes_read;
1142 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1146 frame->bytes_read += count;
1147 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1148 (unsigned long)count, frame->bytes_read);
1150 // For now, forget the frame if it has not been read in one shot.
1151 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1152 frame->bytes_read = 0;
1154 /* Mark it as available to be used again. */
1155 frame->grabstate = FrameState_Unused;
1161 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1163 unsigned long size = vma->vm_end - vma->vm_start,
1164 start = vma->vm_start;
1168 struct video_device *dev = video_devdata(file);
1169 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1171 PDEBUG(DBG_MMAP, "mmap");
1173 down(&usbvision->lock);
1175 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1176 up(&usbvision->lock);
1180 if (!(vma->vm_flags & VM_WRITE) ||
1181 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1182 up(&usbvision->lock);
1186 for (i = 0; i < usbvision->num_frames; i++) {
1187 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1190 if (i == usbvision->num_frames) {
1191 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1192 up(&usbvision->lock);
1196 /* VM_IO is eventually going to replace PageReserved altogether */
1197 vma->vm_flags |= VM_IO;
1198 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1200 pos = usbvision->frame[i].data;
1203 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1204 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1205 up(&usbvision->lock);
1213 up(&usbvision->lock);
1219 * Here comes the stuff for radio on usbvision based devices
1222 static int usbvision_radio_open(struct inode *inode, struct file *file)
1224 struct video_device *dev = video_devdata(file);
1225 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1226 struct v4l2_frequency freq;
1229 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1231 down(&usbvision->lock);
1233 if (usbvision->user) {
1234 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1239 usbvision_reset_powerOffTimer(usbvision);
1240 if (usbvision->power == 0) {
1241 usbvision_power_on(usbvision);
1242 usbvision_init_i2c(usbvision);
1246 /* Alternate interface 1 is is the biggest frame size */
1247 errCode = usbvision_set_alternate(usbvision);
1249 usbvision->last_error = errCode;
1253 // If so far no errors then we shall start the radio
1254 usbvision->radio = 1;
1255 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1256 freq.frequency = 1517; //SWR3 @ 94.8MHz
1257 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1258 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1263 if (PowerOnAtOpen) {
1264 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1265 usbvision_power_off(usbvision);
1266 usbvision->initialized = 0;
1269 up(&usbvision->lock);
1274 static int usbvision_radio_close(struct inode *inode, struct file *file)
1276 struct video_device *dev = video_devdata(file);
1277 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1282 down(&usbvision->lock);
1284 /* Set packet size to 0 */
1285 usbvision->ifaceAlt=0;
1286 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1287 usbvision->ifaceAlt);
1289 usbvision_audio_off(usbvision);
1293 if (PowerOnAtOpen) {
1294 usbvision_set_powerOffTimer(usbvision);
1295 usbvision->initialized = 0;
1298 up(&usbvision->lock);
1300 if (usbvision->remove_pending) {
1301 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
1302 usbvision_release(usbvision);
1306 PDEBUG(DBG_IO, "success");
1311 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1312 unsigned int cmd, void *arg)
1314 struct video_device *dev = video_devdata(file);
1315 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1317 if (!USBVISION_IS_OPERATIONAL(usbvision))
1321 case VIDIOC_QUERYCAP:
1323 struct v4l2_capability *vc=arg;
1325 memset(vc, 0, sizeof(*vc));
1326 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1327 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1329 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1330 sizeof(vc->bus_info));
1331 vc->version = USBVISION_DRIVER_VERSION;
1332 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1333 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1336 case VIDIOC_QUERYCTRL:
1338 struct v4l2_queryctrl *ctrl = arg;
1341 memset(ctrl,0,sizeof(*ctrl));
1344 call_i2c_clients(usbvision, cmd, arg);
1345 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1355 struct v4l2_control *ctrl = arg;
1357 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1358 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1363 struct v4l2_control *ctrl = arg;
1365 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1366 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1369 case VIDIOC_G_TUNER:
1371 struct v4l2_tuner *t = arg;
1376 memset(t,0,sizeof(*t));
1377 strcpy(t->name, "Radio");
1378 t->type = V4L2_TUNER_RADIO;
1380 /* Let clients fill in the remainder of this struct */
1381 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1382 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1385 case VIDIOC_S_TUNER:
1387 struct v4l2_tuner *vt = arg;
1389 // Only no or one tuner for now
1390 if (!usbvision->have_tuner || vt->index)
1392 /* let clients handle this */
1393 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1395 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1398 case VIDIOC_G_AUDIO:
1400 struct v4l2_audio *a = arg;
1402 memset(a,0,sizeof(*a));
1403 strcpy(a->name,"Radio");
1404 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1407 case VIDIOC_S_AUDIO:
1408 case VIDIOC_S_INPUT:
1412 case VIDIOC_G_FREQUENCY:
1414 struct v4l2_frequency *f = arg;
1416 memset(f,0,sizeof(*f));
1418 f->type = V4L2_TUNER_RADIO;
1419 f->frequency = usbvision->freq;
1420 call_i2c_clients(usbvision, cmd, f);
1421 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1425 case VIDIOC_S_FREQUENCY:
1427 struct v4l2_frequency *f = arg;
1431 usbvision->freq = f->frequency;
1432 call_i2c_clients(usbvision, cmd, f);
1433 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1439 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1440 return -ENOIOCTLCMD;
1447 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1448 unsigned int cmd, unsigned long arg)
1450 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1455 * Here comes the stuff for vbi on usbvision based devices
1458 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1465 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1471 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1472 unsigned int cmd, void *arg)
1478 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1479 unsigned int cmd, unsigned long arg)
1481 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1486 // Video registration stuff
1490 static const struct file_operations usbvision_fops = {
1491 .owner = THIS_MODULE,
1492 .open = usbvision_v4l2_open,
1493 .release = usbvision_v4l2_close,
1494 .read = usbvision_v4l2_read,
1495 .mmap = usbvision_v4l2_mmap,
1496 .ioctl = usbvision_v4l2_ioctl,
1497 .llseek = no_llseek,
1499 static struct video_device usbvision_video_template = {
1500 .owner = THIS_MODULE,
1501 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1502 .hardware = VID_HARDWARE_USBVISION,
1503 .fops = &usbvision_fops,
1504 .name = "usbvision-video",
1505 .release = video_device_release,
1511 static const struct file_operations usbvision_radio_fops = {
1512 .owner = THIS_MODULE,
1513 .open = usbvision_radio_open,
1514 .release = usbvision_radio_close,
1515 .ioctl = usbvision_radio_ioctl,
1516 .llseek = no_llseek,
1519 static struct video_device usbvision_radio_template=
1521 .owner = THIS_MODULE,
1522 .type = VID_TYPE_TUNER,
1523 .hardware = VID_HARDWARE_USBVISION,
1524 .fops = &usbvision_radio_fops,
1525 .release = video_device_release,
1526 .name = "usbvision-radio",
1532 static const struct file_operations usbvision_vbi_fops = {
1533 .owner = THIS_MODULE,
1534 .open = usbvision_vbi_open,
1535 .release = usbvision_vbi_close,
1536 .ioctl = usbvision_vbi_ioctl,
1537 .llseek = no_llseek,
1540 static struct video_device usbvision_vbi_template=
1542 .owner = THIS_MODULE,
1543 .type = VID_TYPE_TUNER,
1544 .hardware = VID_HARDWARE_USBVISION,
1545 .fops = &usbvision_vbi_fops,
1546 .release = video_device_release,
1547 .name = "usbvision-vbi",
1552 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1553 struct video_device *vdev_template,
1556 struct usb_device *usb_dev = usbvision->dev;
1557 struct video_device *vdev;
1559 if (usb_dev == NULL) {
1560 err("%s: usbvision->dev is not set", __FUNCTION__);
1564 vdev = video_device_alloc();
1568 *vdev = *vdev_template;
1569 // vdev->minor = -1;
1570 vdev->dev = &usb_dev->dev;
1571 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1572 video_set_drvdata(vdev, usbvision);
1576 // unregister video4linux devices
1577 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1580 if (usbvision->vbi) {
1581 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1582 if (usbvision->vbi->minor != -1) {
1583 video_unregister_device(usbvision->vbi);
1586 video_device_release(usbvision->vbi);
1588 usbvision->vbi = NULL;
1592 if (usbvision->rdev) {
1593 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1594 if (usbvision->rdev->minor != -1) {
1595 video_unregister_device(usbvision->rdev);
1598 video_device_release(usbvision->rdev);
1600 usbvision->rdev = NULL;
1604 if (usbvision->vdev) {
1605 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1606 if (usbvision->vdev->minor != -1) {
1607 video_unregister_device(usbvision->vdev);
1610 video_device_release(usbvision->vdev);
1612 usbvision->vdev = NULL;
1616 // register video4linux devices
1617 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1620 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1621 if (usbvision->vdev == NULL) {
1624 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1627 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision->nr,usbvision->vdev->minor & 0x1f);
1630 if (usbvision_device_data[usbvision->DevModel].Radio) {
1631 // usbvision has radio
1632 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1633 if (usbvision->rdev == NULL) {
1636 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1639 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision->nr, usbvision->rdev->minor & 0x1f);
1642 if (usbvision_device_data[usbvision->DevModel].vbi) {
1643 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1644 if (usbvision->vdev == NULL) {
1647 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1650 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision->nr,usbvision->vbi->minor & 0x1f);
1656 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1657 usbvision_unregister_video(usbvision);
1664 * This code allocates the struct usb_usbvision. It is filled with default values.
1666 * Returns NULL on error, a pointer to usb_usbvision else.
1669 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1671 struct usb_usbvision *usbvision;
1673 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1677 usbvision->dev = dev;
1679 init_MUTEX(&usbvision->lock); /* to 1 == available */
1681 // prepare control urb for control messages during interrupts
1682 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1683 if (usbvision->ctrlUrb == NULL) {
1686 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1687 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1689 usbvision_init_powerOffTimer(usbvision);
1694 if (usbvision && usbvision->ctrlUrb) {
1695 usb_free_urb(usbvision->ctrlUrb);
1704 * usbvision_release()
1706 * This code does final release of struct usb_usbvision. This happens
1707 * after the device is disconnected -and- all clients closed their files.
1710 static void usbvision_release(struct usb_usbvision *usbvision)
1712 PDEBUG(DBG_PROBE, "");
1714 down(&usbvision->lock);
1716 usbvision_reset_powerOffTimer(usbvision);
1718 usbvision->initialized = 0;
1720 up(&usbvision->lock);
1722 usbvision_remove_sysfs(usbvision->vdev);
1723 usbvision_unregister_video(usbvision);
1725 if (usbvision->ctrlUrb) {
1726 usb_free_urb(usbvision->ctrlUrb);
1731 PDEBUG(DBG_PROBE, "success");
1735 /******************************** usb interface *****************************************/
1737 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1741 if (usbvision == NULL)
1744 model = usbvision->DevModel;
1745 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1747 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1748 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1750 usbvision->Vin_Reg2_Preset = 0;
1753 for (i = 0; i < TVNORMS; i++)
1754 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1758 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1760 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1761 usbvision->ctl_input = 0;
1763 /* This should be here to make i2c clients to be able to register */
1764 usbvision_audio_off(usbvision); //first switch off audio
1765 if (!PowerOnAtOpen) {
1766 usbvision_power_on(usbvision); //and then power up the noisy tuner
1767 usbvision_init_i2c(usbvision);
1774 * This procedure queries device descriptor and accepts the interface
1775 * if it looks like USBVISION video device
1778 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1780 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1781 struct usb_interface *uif;
1782 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1783 const struct usb_host_interface *interface;
1784 struct usb_usbvision *usbvision = NULL;
1785 const struct usb_endpoint_descriptor *endpoint;
1788 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1789 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1791 /* Is it an USBVISION video dev? */
1793 for(model = 0; usbvision_device_data[model].idVendor; model++) {
1794 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1797 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1801 printk(KERN_INFO "%s: %s found\n", __FUNCTION__, usbvision_device_data[model].ModelString);
1805 if (usbvision_device_data[model].idVendor == 0) {
1806 return -ENODEV; //no matching device
1808 if (usbvision_device_data[model].Interface >= 0) {
1809 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1812 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1814 endpoint = &interface->endpoint[1].desc;
1815 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1816 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1817 err("%s: Endpoint attributes %d", __FUNCTION__, endpoint->bmAttributes);
1820 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1821 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1827 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1828 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1831 if (dev->descriptor.bNumConfigurations > 1) {
1832 usbvision->bridgeType = BRIDGE_NT1004;
1834 else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1835 usbvision->bridgeType = BRIDGE_NT1005;
1838 usbvision->bridgeType = BRIDGE_NT1003;
1840 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1842 down(&usbvision->lock);
1844 /* compute alternate max packet sizes */
1845 uif = dev->actconfig->interface[0];
1847 usbvision->num_alt=uif->num_altsetting;
1848 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1849 usbvision->alt_max_pkt_size = kmalloc(32*
1850 usbvision->num_alt,GFP_KERNEL);
1851 if (usbvision->alt_max_pkt_size == NULL) {
1852 err("usbvision: out of memory!\n");
1856 for (i = 0; i < usbvision->num_alt ; i++) {
1857 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1859 usbvision->alt_max_pkt_size[i] =
1860 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1861 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1862 usbvision->alt_max_pkt_size[i]);
1866 usbvision->nr = usbvision_nr++;
1868 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1869 if (usbvision->have_tuner) {
1870 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1873 usbvision->tuner_addr = ADDR_UNSET;
1875 usbvision->DevModel = model;
1876 usbvision->remove_pending = 0;
1877 usbvision->iface = ifnum;
1878 usbvision->ifaceAlt = 0;
1879 usbvision->video_endp = endpoint->bEndpointAddress;
1880 usbvision->isocPacketSize = 0;
1881 usbvision->usb_bandwidth = 0;
1882 usbvision->user = 0;
1883 usbvision->streaming = Stream_Off;
1884 usbvision_register_video(usbvision);
1885 usbvision_configure_video(usbvision);
1886 up(&usbvision->lock);
1889 usb_set_intfdata (intf, usbvision);
1890 usbvision_create_sysfs(usbvision->vdev);
1892 PDEBUG(DBG_PROBE, "success");
1898 * usbvision_disconnect()
1900 * This procedure stops all driver activity, deallocates interface-private
1901 * structure (pointed by 'ptr') and after that driver should be removable
1902 * with no ill consequences.
1905 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1907 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1909 PDEBUG(DBG_PROBE, "");
1911 if (usbvision == NULL) {
1912 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1915 usb_set_intfdata (intf, NULL);
1917 down(&usbvision->lock);
1919 // At this time we ask to cancel outstanding URBs
1920 usbvision_stop_isoc(usbvision);
1922 if (usbvision->power) {
1923 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1924 usbvision_power_off(usbvision);
1926 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1928 usb_put_dev(usbvision->dev);
1929 usbvision->dev = NULL; // USB device is no more
1931 up(&usbvision->lock);
1933 if (usbvision->user) {
1934 printk(KERN_INFO "%s: In use, disconnect pending\n", __FUNCTION__);
1935 wake_up_interruptible(&usbvision->wait_frame);
1936 wake_up_interruptible(&usbvision->wait_stream);
1939 usbvision_release(usbvision);
1942 PDEBUG(DBG_PROBE, "success");
1946 static struct usb_driver usbvision_driver = {
1947 .name = "usbvision",
1948 .id_table = usbvision_table,
1949 .probe = usbvision_probe,
1950 .disconnect = usbvision_disconnect
1954 * customdevice_process()
1956 * This procedure preprocesses CustomDevice parameter if any
1959 static void customdevice_process(void)
1961 usbvision_device_data[0]=usbvision_device_data[1];
1962 usbvision_table[0]=usbvision_table[1];
1966 char *parse=CustomDevice;
1968 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1970 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1971 usbvision_device_data[0].idVendor;
1972 usbvision_device_data[0].idProduct;
1973 usbvision_device_data[0].Interface;
1974 usbvision_device_data[0].Codec;
1975 usbvision_device_data[0].VideoChannels;
1976 usbvision_device_data[0].VideoNorm;
1977 usbvision_device_data[0].AudioChannels;
1978 usbvision_device_data[0].Radio;
1979 usbvision_device_data[0].Tuner;
1980 usbvision_device_data[0].TunerType;
1981 usbvision_device_data[0].Vin_Reg1;
1982 usbvision_device_data[0].Vin_Reg2;
1983 usbvision_device_data[0].X_Offset;
1984 usbvision_device_data[0].Y_Offset;
1985 usbvision_device_data[0].Dvi_yuv;
1986 usbvision_device_data[0].ModelString;
1990 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1993 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1995 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1997 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1999 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
2000 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
2002 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
2003 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
2005 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
2006 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
2008 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
2013 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
2014 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
2018 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
2019 usbvision_device_data[0].VideoNorm=V4L2_STD_SECAM;
2023 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
2024 usbvision_device_data[0].VideoNorm=V4L2_STD_NTSC;
2028 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
2029 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
2034 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
2036 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
2037 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
2039 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
2040 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
2042 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
2043 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
2045 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
2046 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
2048 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
2049 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
2051 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
2052 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
2054 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
2055 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
2057 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
2058 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
2059 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
2061 //add to usbvision_table also
2062 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
2063 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
2064 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
2074 * This code is run to initialize the driver.
2077 static int __init usbvision_init(void)
2081 PDEBUG(DBG_PROBE, "");
2083 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
2084 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
2085 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
2086 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
2088 /* disable planar mode support unless compression enabled */
2089 if (isocMode != ISOC_MODE_COMPRESS ) {
2090 // FIXME : not the right way to set supported flag
2091 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2092 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2095 customdevice_process();
2097 errCode = usb_register(&usbvision_driver);
2100 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
2101 PDEBUG(DBG_PROBE, "success");
2106 static void __exit usbvision_exit(void)
2108 PDEBUG(DBG_PROBE, "");
2110 usb_deregister(&usbvision_driver);
2111 PDEBUG(DBG_PROBE, "success");
2114 module_init(usbvision_init);
2115 module_exit(usbvision_exit);
2118 * Overrides for Emacs so that we follow Linus's tabbing style.
2119 * ---------------------------------------------------------------------------