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/sched.h>
50 #include <linux/list.h>
51 #include <linux/timer.h>
52 #include <linux/slab.h>
54 #include <linux/utsname.h>
55 #include <linux/highmem.h>
56 #include <linux/smp_lock.h>
57 #include <linux/videodev.h>
58 #include <linux/vmalloc.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/spinlock.h>
63 #include <linux/videodev2.h>
64 #include <linux/video_decoder.h>
65 #include <linux/i2c.h>
67 #include <media/saa7115.h>
68 #include <media/v4l2-common.h>
69 #include <media/tuner.h>
70 #include <media/audiochip.h>
72 #include <linux/moduleparam.h>
73 #include <linux/workqueue.h>
76 #include <linux/kmod.h>
79 #include "usbvision.h"
81 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
82 #define DRIVER_NAME "usbvision"
83 #define DRIVER_ALIAS "USBVision"
84 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85 #define DRIVER_LICENSE "GPL"
86 #define USBVISION_DRIVER_VERSION_MAJOR 0
87 #define USBVISION_DRIVER_VERSION_MINOR 9
88 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
90 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
92 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
95 #ifdef USBVISION_DEBUG
96 #define PDEBUG(level, fmt, args...) \
97 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
99 #define PDEBUG(level, fmt, args...) do {} while(0)
102 #define DBG_IOCTL 1<<0
104 #define DBG_PROBE 1<<2
105 #define DBG_MMAP 1<<3
108 #define rmspace(str) while(*str==' ') str++;
109 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
112 static int usbvision_nr = 0; // sequential number of usbvision device
114 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
115 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
116 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
117 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
118 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
119 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
120 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
121 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
122 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
125 /* supported tv norms */
126 static struct usbvision_tvnorm tvnorms[] = {
135 .id = V4L2_STD_SECAM,
138 .id = V4L2_STD_PAL_M,
142 #define TVNORMS ARRAY_SIZE(tvnorms)
144 // Function prototypes
145 static void usbvision_release(struct usb_usbvision *usbvision);
147 // Default initalization of device driver parameters
148 static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
149 static int video_debug = 0; // Set the default Debug Mode of the device driver
150 static int PowerOnAtOpen = 1; // Set the default device to power on at startup
151 static int video_nr = -1; // Sequential Number of Video Device
152 static int radio_nr = -1; // Sequential Number of Radio Device
153 static int vbi_nr = -1; // Sequential Number of VBI Device
154 static char *CustomDevice=NULL; // Set as nothing....
156 // Grab parameters for the device driver
158 #if defined(module_param) // Showing parameters under SYSFS
159 module_param(isocMode, int, 0444);
160 module_param(video_debug, int, 0444);
161 module_param(PowerOnAtOpen, int, 0444);
162 module_param(video_nr, int, 0444);
163 module_param(radio_nr, int, 0444);
164 module_param(vbi_nr, int, 0444);
165 module_param(CustomDevice, charp, 0444);
167 MODULE_PARAM(isocMode, "i");
168 MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
169 MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
170 MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
171 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.
172 MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
173 MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
174 MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
175 MODULE_PARM(CustomDevice, "s"); // .... CustomDevice
178 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
179 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
180 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
181 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
182 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
183 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
184 MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null");
188 MODULE_AUTHOR(DRIVER_AUTHOR);
189 MODULE_DESCRIPTION(DRIVER_DESC);
190 MODULE_LICENSE(DRIVER_LICENSE);
191 MODULE_VERSION(USBVISION_VERSION_STRING);
192 MODULE_ALIAS(DRIVER_ALIAS);
195 /****************************************************************************************/
196 /* SYSFS Code - Copied from the stv680.c usb module. */
197 /* Device information is located at /sys/class/video4linux/video0 */
198 /* Device parameters information is located at /sys/module/usbvision */
199 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
200 /****************************************************************************************/
203 #define YES_NO(x) ((x) ? "Yes" : "No")
205 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
207 struct video_device *vdev = to_video_device(cd);
208 return video_get_drvdata(vdev);
211 static ssize_t show_version(struct class_device *cd, char *buf)
213 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
215 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
217 static ssize_t show_model(struct class_device *class_dev, char *buf)
219 struct video_device *vdev = to_video_device(class_dev);
220 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
221 return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
223 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
225 static ssize_t show_hue(struct class_device *class_dev, char *buf)
227 struct video_device *vdev = to_video_device(class_dev);
228 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
229 struct v4l2_control ctrl;
230 ctrl.id = V4L2_CID_HUE;
232 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
233 return sprintf(buf, "%d\n", ctrl.value >> 8);
235 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
237 static ssize_t show_contrast(struct class_device *class_dev, char *buf)
239 struct video_device *vdev = to_video_device(class_dev);
240 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
241 struct v4l2_control ctrl;
242 ctrl.id = V4L2_CID_CONTRAST;
244 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
245 return sprintf(buf, "%d\n", ctrl.value >> 8);
247 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
249 static ssize_t show_brightness(struct class_device *class_dev, char *buf)
251 struct video_device *vdev = to_video_device(class_dev);
252 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
253 struct v4l2_control ctrl;
254 ctrl.id = V4L2_CID_BRIGHTNESS;
256 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
257 return sprintf(buf, "%d\n", ctrl.value >> 8);
259 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
261 static ssize_t show_saturation(struct class_device *class_dev, char *buf)
263 struct video_device *vdev = to_video_device(class_dev);
264 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
265 struct v4l2_control ctrl;
266 ctrl.id = V4L2_CID_SATURATION;
268 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
269 return sprintf(buf, "%d\n", ctrl.value >> 8);
271 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
273 static ssize_t show_streaming(struct class_device *class_dev, char *buf)
275 struct video_device *vdev = to_video_device(class_dev);
276 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
277 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
279 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
281 static ssize_t show_compression(struct class_device *class_dev, char *buf)
283 struct video_device *vdev = to_video_device(class_dev);
284 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
285 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
287 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
289 static ssize_t show_device_bridge(struct class_device *class_dev, char *buf)
291 struct video_device *vdev = to_video_device(class_dev);
292 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
293 return sprintf(buf, "%d\n", usbvision->bridgeType);
295 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
297 static void usbvision_create_sysfs(struct video_device *vdev)
301 res=video_device_create_file(vdev, &class_device_attr_version);
302 res=video_device_create_file(vdev, &class_device_attr_model);
303 res=video_device_create_file(vdev, &class_device_attr_hue);
304 res=video_device_create_file(vdev, &class_device_attr_contrast);
305 res=video_device_create_file(vdev, &class_device_attr_brightness);
306 res=video_device_create_file(vdev, &class_device_attr_saturation);
307 res=video_device_create_file(vdev, &class_device_attr_streaming);
308 res=video_device_create_file(vdev, &class_device_attr_compression);
309 res=video_device_create_file(vdev, &class_device_attr_bridge);
313 static void usbvision_remove_sysfs(struct video_device *vdev)
316 video_device_remove_file(vdev, &class_device_attr_version);
317 video_device_remove_file(vdev, &class_device_attr_model);
318 video_device_remove_file(vdev, &class_device_attr_hue);
319 video_device_remove_file(vdev, &class_device_attr_contrast);
320 video_device_remove_file(vdev, &class_device_attr_brightness);
321 video_device_remove_file(vdev, &class_device_attr_saturation);
322 video_device_remove_file(vdev, &class_device_attr_streaming);
323 video_device_remove_file(vdev, &class_device_attr_compression);
324 video_device_remove_file(vdev, &class_device_attr_bridge);
332 * This is part of Video 4 Linux API. The driver can be opened by one
333 * client only (checks internal counter 'usbvision->user'). The procedure
334 * then allocates buffers needed for video processing.
337 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
339 struct video_device *dev = video_devdata(file);
340 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
343 PDEBUG(DBG_IO, "open");
346 usbvision_reset_powerOffTimer(usbvision);
351 /* Allocate memory for the frame buffers */
352 errCode = usbvision_frames_alloc(usbvision);
354 /* Allocate memory for the scratch ring buffer */
355 errCode = usbvision_scratch_alloc(usbvision);
357 /* Allocate memory for the USB S buffers */
358 errCode = usbvision_sbuf_alloc(usbvision);
359 if ((!errCode) && (usbvision->isocMode==ISOC_MODE_COMPRESS)) {
360 /* Allocate intermediate decompression buffers only if needed */
361 errCode = usbvision_decompress_alloc(usbvision);
366 /* Deallocate all buffers if trouble */
367 usbvision_frames_free(usbvision);
368 usbvision_scratch_free(usbvision);
369 usbvision_sbuf_free(usbvision);
370 usbvision_decompress_free(usbvision);
374 /* If so far no errors then we shall start the camera */
376 down(&usbvision->lock);
377 if (usbvision->power == 0) {
378 usbvision_power_on(usbvision);
379 usbvision_init_i2c(usbvision);
382 /* Send init sequence only once, it's large! */
383 if (!usbvision->initialized) {
385 setup_ok = usbvision_setup(usbvision,isocMode);
387 usbvision->initialized = 1;
393 usbvision_begin_streaming(usbvision);
394 errCode = usbvision_init_isoc(usbvision);
395 /* device needs to be initialized before isoc transfer */
396 usbvision_muxsel(usbvision,0);
401 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
402 usbvision_power_off(usbvision);
403 usbvision->initialized = 0;
406 up(&usbvision->lock);
413 usbvision_empty_framequeues(usbvision);
415 PDEBUG(DBG_IO, "success");
420 * usbvision_v4l2_close()
422 * This is part of Video 4 Linux API. The procedure
423 * stops streaming and deallocates all buffers that were earlier
424 * allocated in usbvision_v4l2_open().
427 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
429 struct video_device *dev = video_devdata(file);
430 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
432 PDEBUG(DBG_IO, "close");
433 down(&usbvision->lock);
435 usbvision_audio_off(usbvision);
436 usbvision_restart_isoc(usbvision);
437 usbvision_stop_isoc(usbvision);
439 usbvision_decompress_free(usbvision);
440 usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
441 usbvision_scratch_free(usbvision);
442 usbvision_sbuf_free(usbvision);
447 /* power off in a little while to avoid off/on every close/open short sequences */
448 usbvision_set_powerOffTimer(usbvision);
449 usbvision->initialized = 0;
452 up(&usbvision->lock);
454 if (usbvision->remove_pending) {
455 info("%s: Final disconnect", __FUNCTION__);
456 usbvision_release(usbvision);
459 PDEBUG(DBG_IO, "success");
469 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
472 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
473 unsigned int cmd, void *arg)
475 struct video_device *dev = video_devdata(file);
476 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
478 if (!USBVISION_IS_OPERATIONAL(usbvision))
483 #ifdef CONFIG_VIDEO_ADV_DEBUG
484 /* ioctls to allow direct acces to the NT100x registers */
485 case VIDIOC_INT_G_REGISTER:
487 struct v4l2_register *reg = arg;
490 if (reg->i2c_id != 0)
492 /* NT100x has a 8-bit register space */
493 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
495 err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
498 reg->val=(unsigned char)errCode;
499 PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
500 (unsigned int)reg->reg, reg->val);
501 errCode = 0; // No error
505 case VIDIOC_INT_S_REGISTER:
507 struct v4l2_register *reg = arg;
510 if (reg->i2c_id != 0)
512 if (!capable(CAP_SYS_ADMIN))
514 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
516 err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
519 PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
520 (unsigned int)reg->reg, reg->val);
526 case VIDIOC_QUERYCAP:
528 struct v4l2_capability *vc=arg;
530 memset(vc, 0, sizeof(*vc));
531 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
532 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
534 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
535 sizeof(vc->bus_info));
536 vc->version = USBVISION_DRIVER_VERSION;
537 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
541 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
542 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
545 case VIDIOC_ENUMINPUT:
547 struct v4l2_input *vi = arg;
550 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
552 if (usbvision->have_tuner) {
556 chan = vi->index + 1; //skip Television string
560 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
561 strcpy(vi->name, "White Video Input");
564 strcpy(vi->name, "Television");
565 vi->type = V4L2_INPUT_TYPE_TUNER;
568 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
572 vi->type = V4L2_INPUT_TYPE_CAMERA;
573 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
574 strcpy(vi->name, "Green Video Input");
577 strcpy(vi->name, "Composite Video Input");
579 vi->std = V4L2_STD_PAL;
582 vi->type = V4L2_INPUT_TYPE_CAMERA;
583 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
584 strcpy(vi->name, "Yellow Video Input");
587 strcpy(vi->name, "S-Video Input");
589 vi->std = V4L2_STD_PAL;
592 vi->type = V4L2_INPUT_TYPE_CAMERA;
593 strcpy(vi->name, "Red Video Input");
594 vi->std = V4L2_STD_PAL;
597 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
598 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
603 struct v4l2_standard *e = arg;
610 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
611 tvnorms[e->index].name);
620 *input = usbvision->ctl_input;
626 if ((*input >= usbvision->video_inputs) || (*input < 0) )
628 usbvision->ctl_input = *input;
630 down(&usbvision->lock);
631 usbvision_muxsel(usbvision, usbvision->ctl_input);
632 usbvision_set_input(usbvision);
633 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
634 up(&usbvision->lock);
639 v4l2_std_id *id = arg;
641 *id = usbvision->tvnorm->id;
643 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
648 v4l2_std_id *id = arg;
651 for (i = 0; i < TVNORMS; i++)
652 if (*id == tvnorms[i].id)
655 for (i = 0; i < TVNORMS; i++)
656 if (*id & tvnorms[i].id)
661 down(&usbvision->lock);
662 usbvision->tvnorm = &tvnorms[i];
664 call_i2c_clients(usbvision, VIDIOC_S_STD,
665 &usbvision->tvnorm->id);
667 up(&usbvision->lock);
669 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
674 struct v4l2_tuner *vt = arg;
676 if (!usbvision->have_tuner || vt->index) // Only tuner 0
678 strcpy(vt->name, "Television");
679 /* Let clients fill in the remainder of this struct */
680 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
682 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
687 struct v4l2_tuner *vt = arg;
689 // Only no or one tuner for now
690 if (!usbvision->have_tuner || vt->index)
692 /* let clients handle this */
693 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
695 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
698 case VIDIOC_G_FREQUENCY:
700 struct v4l2_frequency *freq = arg;
702 freq->tuner = 0; // Only one tuner
703 freq->type = V4L2_TUNER_ANALOG_TV;
704 freq->frequency = usbvision->freq;
705 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
708 case VIDIOC_S_FREQUENCY:
710 struct v4l2_frequency *freq = arg;
712 // Only no or one tuner for now
713 if (!usbvision->have_tuner || freq->tuner)
716 usbvision->freq = freq->frequency;
717 call_i2c_clients(usbvision, cmd, freq);
718 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
723 struct v4l2_audio *v = arg;
724 memset(v,0, sizeof(v));
725 strcpy(v->name, "TV");
726 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
731 struct v4l2_audio *v = arg;
735 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
738 case VIDIOC_QUERYCTRL:
740 struct v4l2_queryctrl *ctrl = arg;
743 memset(ctrl,0,sizeof(*ctrl));
746 call_i2c_clients(usbvision, cmd, arg);
753 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
757 struct v4l2_control *ctrl = arg;
758 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
759 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
764 struct v4l2_control *ctrl = arg;
766 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
767 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
772 struct v4l2_requestbuffers *vr = arg;
775 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
777 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
778 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
779 (vr->memory != V4L2_MEMORY_MMAP))
782 if(usbvision->streaming == Stream_On) {
783 if ((ret = usbvision_stream_interrupt(usbvision)))
787 usbvision_empty_framequeues(usbvision);
789 usbvision->curFrame = NULL;
791 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
794 case VIDIOC_QUERYBUF:
796 struct v4l2_buffer *vb = arg;
797 struct usbvision_frame *frame;
799 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
801 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
804 if(vb->index>=USBVISION_NUMFRAMES) {
807 // Updating the corresponding frame state
809 frame = &usbvision->frame[vb->index];
810 if(frame->grabstate >= FrameState_Ready)
811 vb->flags |= V4L2_BUF_FLAG_QUEUED;
812 if(frame->grabstate >= FrameState_Done)
813 vb->flags |= V4L2_BUF_FLAG_DONE;
814 if(frame->grabstate == FrameState_Unused)
815 vb->flags |= V4L2_BUF_FLAG_MAPPED;
816 vb->memory = V4L2_MEMORY_MMAP;
818 vb->m.offset = vb->index*usbvision->max_frame_size;
820 vb->memory = V4L2_MEMORY_MMAP;
821 vb->field = V4L2_FIELD_NONE;
822 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
823 vb->timestamp = usbvision->frame[vb->index].timestamp;
824 vb->sequence = usbvision->frame[vb->index].sequence;
829 struct v4l2_buffer *vb = arg;
830 struct usbvision_frame *frame;
831 unsigned long lock_flags;
833 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
834 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
837 if(vb->index>=USBVISION_NUMFRAMES) {
841 frame = &usbvision->frame[vb->index];
843 if (frame->grabstate != FrameState_Unused) {
847 /* Mark it as ready and enqueue frame */
848 frame->grabstate = FrameState_Ready;
849 frame->scanstate = ScanState_Scanning;
850 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
852 vb->flags &= ~V4L2_BUF_FLAG_DONE;
854 /* set v4l2_format index */
855 frame->v4l2_format = usbvision->palette;
857 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
858 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
859 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
861 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
866 struct v4l2_buffer *vb = arg;
868 struct usbvision_frame *f;
869 unsigned long lock_flags;
871 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
874 if (list_empty(&(usbvision->outqueue))) {
875 if (usbvision->streaming == Stream_Idle)
877 ret = wait_event_interruptible
878 (usbvision->wait_frame,
879 !list_empty(&(usbvision->outqueue)));
884 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
885 f = list_entry(usbvision->outqueue.next,
886 struct usbvision_frame, frame);
887 list_del(usbvision->outqueue.next);
888 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
890 f->grabstate = FrameState_Unused;
892 vb->memory = V4L2_MEMORY_MMAP;
893 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
894 vb->index = f->index;
895 vb->sequence = f->sequence;
896 vb->timestamp = f->timestamp;
897 vb->field = V4L2_FIELD_NONE;
898 vb->bytesused = f->scanlength;
902 case VIDIOC_STREAMON:
904 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
906 usbvision->streaming = Stream_On;
908 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
910 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
914 case VIDIOC_STREAMOFF:
917 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
919 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
922 if(usbvision->streaming == Stream_On) {
923 usbvision_stream_interrupt(usbvision);
924 // Stop all video streamings
925 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
927 usbvision_empty_framequeues(usbvision);
929 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
932 case VIDIOC_ENUM_FMT:
934 struct v4l2_fmtdesc *vfd = arg;
936 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
940 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
941 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
942 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
943 memset(vfd->reserved, 0, sizeof(vfd->reserved));
948 struct v4l2_format *vf = arg;
951 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
953 vf->fmt.pix.width = usbvision->curwidth;
954 vf->fmt.pix.height = usbvision->curheight;
955 vf->fmt.pix.pixelformat = usbvision->palette.format;
956 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
957 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
958 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
959 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
960 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
961 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
965 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
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 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
994 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
996 if(cmd == VIDIOC_TRY_FMT) {
997 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
998 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1002 /* stop io in case it is already in progress */
1003 if(usbvision->streaming == Stream_On) {
1004 if ((ret = usbvision_stream_interrupt(usbvision)))
1007 usbvision_empty_framequeues(usbvision);
1009 usbvision->curFrame = NULL;
1011 // by now we are committed to the new data...
1012 down(&usbvision->lock);
1013 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1014 up(&usbvision->lock);
1016 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1017 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1025 return -ENOIOCTLCMD;
1030 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1031 unsigned int cmd, unsigned long arg)
1033 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1037 static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
1038 size_t count, loff_t *ppos)
1040 struct video_device *dev = video_devdata(file);
1041 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1042 int noblock = file->f_flags & O_NONBLOCK;
1043 unsigned long lock_flags;
1047 struct usbvision_frame *frame;
1049 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1051 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1054 /* no stream is running, make it running ! */
1055 usbvision->streaming = Stream_On;
1056 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1058 /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1059 for(i=0;i<USBVISION_NUMFRAMES;i++) {
1060 frame = &usbvision->frame[i];
1061 if(frame->grabstate == FrameState_Unused) {
1062 /* Mark it as ready and enqueue frame */
1063 frame->grabstate = FrameState_Ready;
1064 frame->scanstate = ScanState_Scanning;
1065 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1067 /* set v4l2_format index */
1068 frame->v4l2_format = usbvision->palette;
1070 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1071 list_add_tail(&frame->frame, &usbvision->inqueue);
1072 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1076 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1077 if (list_empty(&(usbvision->outqueue))) {
1081 ret = wait_event_interruptible
1082 (usbvision->wait_frame,
1083 !list_empty(&(usbvision->outqueue)));
1088 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1089 frame = list_entry(usbvision->outqueue.next,
1090 struct usbvision_frame, frame);
1091 list_del(usbvision->outqueue.next);
1092 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1094 /* An error returns an empty frame */
1095 if (frame->grabstate == FrameState_Error) {
1096 frame->bytes_read = 0;
1100 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1101 frame->index, frame->bytes_read, frame->scanlength);
1103 /* copy bytes to user space; we allow for partials reads */
1104 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1105 count = frame->scanlength - frame->bytes_read;
1107 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1111 frame->bytes_read += count;
1112 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1113 (unsigned long)count, frame->bytes_read);
1115 // For now, forget the frame if it has not been read in one shot.
1116 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1117 frame->bytes_read = 0;
1119 /* Mark it as available to be used again. */
1120 usbvision->frame[frmx].grabstate = FrameState_Unused;
1126 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1128 unsigned long size = vma->vm_end - vma->vm_start,
1129 start = vma->vm_start;
1133 struct video_device *dev = video_devdata(file);
1134 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1136 down(&usbvision->lock);
1138 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1139 up(&usbvision->lock);
1143 if (!(vma->vm_flags & VM_WRITE) ||
1144 size != PAGE_ALIGN(usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel)) {
1145 up(&usbvision->lock);
1149 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1150 if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1153 if (i == USBVISION_NUMFRAMES) {
1154 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1155 up(&usbvision->lock);
1159 /* VM_IO is eventually going to replace PageReserved altogether */
1160 vma->vm_flags |= VM_IO;
1161 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1163 pos = usbvision->frame[i].data;
1166 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1167 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1168 up(&usbvision->lock);
1176 up(&usbvision->lock);
1182 * Here comes the stuff for radio on usbvision based devices
1185 static int usbvision_radio_open(struct inode *inode, struct file *file)
1187 struct video_device *dev = video_devdata(file);
1188 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1189 struct v4l2_frequency freq;
1192 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1194 down(&usbvision->lock);
1196 if (usbvision->user) {
1197 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1202 usbvision_reset_powerOffTimer(usbvision);
1203 if (usbvision->power == 0) {
1204 usbvision_power_on(usbvision);
1205 usbvision_init_i2c(usbvision);
1209 // If so far no errors then we shall start the radio
1210 usbvision->radio = 1;
1211 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1212 freq.frequency = 1517; //SWR3 @ 94.8MHz
1213 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1214 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1219 if (PowerOnAtOpen) {
1220 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1221 usbvision_power_off(usbvision);
1222 usbvision->initialized = 0;
1225 up(&usbvision->lock);
1230 static int usbvision_radio_close(struct inode *inode, struct file *file)
1232 struct video_device *dev = video_devdata(file);
1233 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1238 down(&usbvision->lock);
1240 usbvision_audio_off(usbvision);
1244 if (PowerOnAtOpen) {
1245 usbvision_set_powerOffTimer(usbvision);
1246 usbvision->initialized = 0;
1249 up(&usbvision->lock);
1251 if (usbvision->remove_pending) {
1252 info("%s: Final disconnect", __FUNCTION__);
1253 usbvision_release(usbvision);
1257 PDEBUG(DBG_IO, "success");
1262 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1263 unsigned int cmd, void *arg)
1265 struct video_device *dev = video_devdata(file);
1266 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1268 if (!USBVISION_IS_OPERATIONAL(usbvision))
1272 case VIDIOC_QUERYCAP:
1274 struct v4l2_capability *vc=arg;
1276 memset(vc, 0, sizeof(*vc));
1277 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1278 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1280 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1281 sizeof(vc->bus_info));
1282 vc->version = USBVISION_DRIVER_VERSION;
1283 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1284 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1287 case VIDIOC_QUERYCTRL:
1289 struct v4l2_queryctrl *ctrl = arg;
1292 memset(ctrl,0,sizeof(*ctrl));
1295 call_i2c_clients(usbvision, cmd, arg);
1296 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1306 struct v4l2_control *ctrl = arg;
1308 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1309 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1314 struct v4l2_control *ctrl = arg;
1316 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1317 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1320 case VIDIOC_G_TUNER:
1322 struct v4l2_tuner *t = arg;
1327 memset(t,0,sizeof(*t));
1328 strcpy(t->name, "Radio");
1329 t->type = V4L2_TUNER_RADIO;
1331 /* Let clients fill in the remainder of this struct */
1332 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1333 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1336 case VIDIOC_S_TUNER:
1338 struct v4l2_tuner *vt = arg;
1340 // Only no or one tuner for now
1341 if (!usbvision->have_tuner || vt->index)
1343 /* let clients handle this */
1344 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1346 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1349 case VIDIOC_G_AUDIO:
1351 struct v4l2_audio *a = arg;
1353 memset(a,0,sizeof(*a));
1354 strcpy(a->name,"Radio");
1355 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1358 case VIDIOC_S_AUDIO:
1359 case VIDIOC_S_INPUT:
1363 case VIDIOC_G_FREQUENCY:
1365 struct v4l2_frequency *f = arg;
1367 memset(f,0,sizeof(*f));
1369 f->type = V4L2_TUNER_RADIO;
1370 f->frequency = usbvision->freq;
1371 call_i2c_clients(usbvision, cmd, f);
1372 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1376 case VIDIOC_S_FREQUENCY:
1378 struct v4l2_frequency *f = arg;
1382 usbvision->freq = f->frequency;
1383 call_i2c_clients(usbvision, cmd, f);
1384 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1390 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1391 return -ENOIOCTLCMD;
1398 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1399 unsigned int cmd, unsigned long arg)
1401 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1406 * Here comes the stuff for vbi on usbvision based devices
1409 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1416 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1422 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1423 unsigned int cmd, void *arg)
1429 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1430 unsigned int cmd, unsigned long arg)
1432 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1437 // Video registration stuff
1441 static struct file_operations usbvision_fops = {
1442 .owner = THIS_MODULE,
1443 .open = usbvision_v4l2_open,
1444 .release = usbvision_v4l2_close,
1445 .read = usbvision_v4l2_read,
1446 .mmap = usbvision_v4l2_mmap,
1447 .ioctl = usbvision_v4l2_ioctl,
1448 .llseek = no_llseek,
1450 static struct video_device usbvision_video_template = {
1451 .owner = THIS_MODULE,
1452 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1453 .hardware = VID_HARDWARE_USBVISION,
1454 .fops = &usbvision_fops,
1455 .name = "usbvision-video",
1456 .release = video_device_release,
1462 static struct file_operations usbvision_radio_fops = {
1463 .owner = THIS_MODULE,
1464 .open = usbvision_radio_open,
1465 .release = usbvision_radio_close,
1466 .ioctl = usbvision_radio_ioctl,
1467 .llseek = no_llseek,
1470 static struct video_device usbvision_radio_template=
1472 .owner = THIS_MODULE,
1473 .type = VID_TYPE_TUNER,
1474 .hardware = VID_HARDWARE_USBVISION,
1475 .fops = &usbvision_radio_fops,
1476 .release = video_device_release,
1477 .name = "usbvision-radio",
1483 static struct file_operations usbvision_vbi_fops = {
1484 .owner = THIS_MODULE,
1485 .open = usbvision_vbi_open,
1486 .release = usbvision_vbi_close,
1487 .ioctl = usbvision_vbi_ioctl,
1488 .llseek = no_llseek,
1491 static struct video_device usbvision_vbi_template=
1493 .owner = THIS_MODULE,
1494 .type = VID_TYPE_TUNER,
1495 .hardware = VID_HARDWARE_USBVISION,
1496 .fops = &usbvision_vbi_fops,
1497 .release = video_device_release,
1498 .name = "usbvision-vbi",
1503 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1504 struct video_device *vdev_template,
1507 struct usb_device *usb_dev = usbvision->dev;
1508 struct video_device *vdev;
1510 if (usb_dev == NULL) {
1511 err("%s: usbvision->dev is not set", __FUNCTION__);
1515 vdev = video_device_alloc();
1519 *vdev = *vdev_template;
1520 // vdev->minor = -1;
1521 vdev->dev = &usb_dev->dev;
1522 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1523 video_set_drvdata(vdev, usbvision);
1527 // unregister video4linux devices
1528 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1531 if (usbvision->vbi) {
1532 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1533 if (usbvision->vbi->minor != -1) {
1534 video_unregister_device(usbvision->vbi);
1537 video_device_release(usbvision->vbi);
1539 usbvision->vbi = NULL;
1543 if (usbvision->rdev) {
1544 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1545 if (usbvision->rdev->minor != -1) {
1546 video_unregister_device(usbvision->rdev);
1549 video_device_release(usbvision->rdev);
1551 usbvision->rdev = NULL;
1555 if (usbvision->vdev) {
1556 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1557 if (usbvision->vdev->minor != -1) {
1558 video_unregister_device(usbvision->vdev);
1561 video_device_release(usbvision->vdev);
1563 usbvision->vdev = NULL;
1567 // register video4linux devices
1568 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1571 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1572 if (usbvision->vdev == NULL) {
1575 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1578 info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
1581 if (usbvision_device_data[usbvision->DevModel].Radio) {
1582 // usbvision has radio
1583 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1584 if (usbvision->rdev == NULL) {
1587 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1590 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
1593 if (usbvision_device_data[usbvision->DevModel].vbi) {
1594 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1595 if (usbvision->vdev == NULL) {
1598 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1601 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
1607 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1608 usbvision_unregister_video(usbvision);
1615 * This code allocates the struct usb_usbvision. It is filled with default values.
1617 * Returns NULL on error, a pointer to usb_usbvision else.
1620 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1622 struct usb_usbvision *usbvision;
1624 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1628 usbvision->dev = dev;
1630 init_MUTEX(&usbvision->lock); /* to 1 == available */
1632 // prepare control urb for control messages during interrupts
1633 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1634 if (usbvision->ctrlUrb == NULL) {
1637 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1638 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1640 usbvision_init_powerOffTimer(usbvision);
1645 if (usbvision && usbvision->ctrlUrb) {
1646 usb_free_urb(usbvision->ctrlUrb);
1655 * usbvision_release()
1657 * This code does final release of struct usb_usbvision. This happens
1658 * after the device is disconnected -and- all clients closed their files.
1661 static void usbvision_release(struct usb_usbvision *usbvision)
1663 PDEBUG(DBG_PROBE, "");
1665 down(&usbvision->lock);
1667 usbvision_reset_powerOffTimer(usbvision);
1669 usbvision->initialized = 0;
1671 up(&usbvision->lock);
1673 usbvision_remove_sysfs(usbvision->vdev);
1674 usbvision_unregister_video(usbvision);
1676 if (usbvision->ctrlUrb) {
1677 usb_free_urb(usbvision->ctrlUrb);
1682 PDEBUG(DBG_PROBE, "success");
1686 /******************************** usb interface *****************************************/
1688 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1692 if (usbvision == NULL)
1695 model = usbvision->DevModel;
1696 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1698 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1699 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1701 usbvision->Vin_Reg2_Preset = 0;
1704 for (i = 0; i < TVNORMS; i++)
1705 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1709 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1711 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1712 usbvision->ctl_input = 0;
1714 /* This should be here to make i2c clients to be able to register */
1715 usbvision_audio_off(usbvision); //first switch off audio
1716 if (!PowerOnAtOpen) {
1717 usbvision_power_on(usbvision); //and then power up the noisy tuner
1718 usbvision_init_i2c(usbvision);
1725 * This procedure queries device descriptor and accepts the interface
1726 * if it looks like USBVISION video device
1729 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1731 struct usb_device *dev = interface_to_usbdev(intf);
1732 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1733 const struct usb_host_interface *interface;
1734 struct usb_usbvision *usbvision = NULL;
1735 const struct usb_endpoint_descriptor *endpoint;
1738 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1739 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1740 /* Is it an USBVISION video dev? */
1742 for(model = 0; usbvision_device_data[model].idVendor; model++) {
1743 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1746 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1750 info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
1754 if (usbvision_device_data[model].idVendor == 0) {
1755 return -ENODEV; //no matching device
1757 if (usbvision_device_data[model].Interface >= 0) {
1758 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1761 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1763 endpoint = &interface->endpoint[1].desc;
1764 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1765 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1766 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1769 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1770 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1776 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1777 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1780 if (dev->descriptor.bNumConfigurations > 1) {
1781 usbvision->bridgeType = BRIDGE_NT1004;
1783 else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1784 usbvision->bridgeType = BRIDGE_NT1005;
1787 usbvision->bridgeType = BRIDGE_NT1003;
1789 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1791 down(&usbvision->lock);
1793 usbvision->nr = usbvision_nr++;
1795 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1796 if (usbvision->have_tuner) {
1797 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1800 usbvision->tuner_addr = ADDR_UNSET;
1802 usbvision->DevModel = model;
1803 usbvision->remove_pending = 0;
1804 usbvision->iface = ifnum;
1805 usbvision->ifaceAltInactive = 0;
1806 usbvision->ifaceAltActive = 1;
1807 usbvision->video_endp = endpoint->bEndpointAddress;
1808 usbvision->isocPacketSize = 0;
1809 usbvision->usb_bandwidth = 0;
1810 usbvision->user = 0;
1811 usbvision->streaming = Stream_Off;
1812 usbvision_register_video(usbvision);
1813 usbvision_configure_video(usbvision);
1814 up(&usbvision->lock);
1817 usb_set_intfdata (intf, usbvision);
1818 usbvision_create_sysfs(usbvision->vdev);
1820 PDEBUG(DBG_PROBE, "success");
1826 * usbvision_disconnect()
1828 * This procedure stops all driver activity, deallocates interface-private
1829 * structure (pointed by 'ptr') and after that driver should be removable
1830 * with no ill consequences.
1833 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1835 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1837 PDEBUG(DBG_PROBE, "");
1839 if (usbvision == NULL) {
1840 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1843 usb_set_intfdata (intf, NULL);
1845 down(&usbvision->lock);
1847 // At this time we ask to cancel outstanding URBs
1848 usbvision_stop_isoc(usbvision);
1850 if (usbvision->power) {
1851 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1852 usbvision_power_off(usbvision);
1854 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1856 usb_put_dev(usbvision->dev);
1857 usbvision->dev = NULL; // USB device is no more
1859 up(&usbvision->lock);
1861 if (usbvision->user) {
1862 info("%s: In use, disconnect pending", __FUNCTION__);
1863 wake_up_interruptible(&usbvision->wait_frame);
1864 wake_up_interruptible(&usbvision->wait_stream);
1867 usbvision_release(usbvision);
1870 PDEBUG(DBG_PROBE, "success");
1874 static struct usb_driver usbvision_driver = {
1875 .name = "usbvision",
1876 .id_table = usbvision_table,
1877 .probe = usbvision_probe,
1878 .disconnect = usbvision_disconnect
1882 * customdevice_process()
1884 * This procedure preprocesses CustomDevice parameter if any
1887 void customdevice_process(void)
1889 usbvision_device_data[0]=usbvision_device_data[1];
1890 usbvision_table[0]=usbvision_table[1];
1894 char *parse=CustomDevice;
1896 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1898 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1899 usbvision_device_data[0].idVendor;
1900 usbvision_device_data[0].idProduct;
1901 usbvision_device_data[0].Interface;
1902 usbvision_device_data[0].Codec;
1903 usbvision_device_data[0].VideoChannels;
1904 usbvision_device_data[0].VideoNorm;
1905 usbvision_device_data[0].AudioChannels;
1906 usbvision_device_data[0].Radio;
1907 usbvision_device_data[0].Tuner;
1908 usbvision_device_data[0].TunerType;
1909 usbvision_device_data[0].Vin_Reg1;
1910 usbvision_device_data[0].Vin_Reg2;
1911 usbvision_device_data[0].X_Offset;
1912 usbvision_device_data[0].Y_Offset;
1913 usbvision_device_data[0].Dvi_yuv;
1914 usbvision_device_data[0].ModelString;
1918 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1921 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1923 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1925 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1927 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1928 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1930 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1931 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1933 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1934 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1936 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1941 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
1942 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1946 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
1947 usbvision_device_data[0].VideoNorm=VIDEO_MODE_SECAM;
1951 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
1952 usbvision_device_data[0].VideoNorm=VIDEO_MODE_NTSC;
1956 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
1957 usbvision_device_data[0].VideoNorm=VIDEO_MODE_PAL;
1962 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
1964 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
1965 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
1967 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
1968 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
1970 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
1971 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
1973 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
1974 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
1976 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
1977 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
1979 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
1980 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
1982 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
1983 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
1985 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
1986 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
1987 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
1989 //add to usbvision_table also
1990 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
1991 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
1992 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
2002 * This code is run to initialize the driver.
2005 static int __init usbvision_init(void)
2009 PDEBUG(DBG_PROBE, "");
2011 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
2012 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
2013 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
2014 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
2016 /* disable planar mode support unless compression enabled */
2017 if (isocMode != ISOC_MODE_COMPRESS ) {
2018 // FIXME : not the right way to set supported flag
2019 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2020 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2023 customdevice_process();
2025 errCode = usb_register(&usbvision_driver);
2028 info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
2029 PDEBUG(DBG_PROBE, "success");
2034 static void __exit usbvision_exit(void)
2036 PDEBUG(DBG_PROBE, "");
2038 usb_deregister(&usbvision_driver);
2039 PDEBUG(DBG_PROBE, "success");
2042 module_init(usbvision_init);
2043 module_exit(usbvision_exit);
2046 * Overrides for Emacs so that we follow Linus's tabbing style.
2047 * ---------------------------------------------------------------------------