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 = container_of(cd, struct video_device, class_dev);
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 *cd, char *buf)
219 struct video_device *vdev = container_of(cd, struct 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 *cd, char *buf)
227 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
228 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
229 struct v4l2_control ctrl;
230 ctrl.id = V4L2_CID_HUE;
233 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
234 return sprintf(buf, "%d\n", ctrl.value >> 8);
236 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
238 static ssize_t show_contrast(struct class_device *cd, char *buf)
240 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
241 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
242 struct v4l2_control ctrl;
243 ctrl.id = V4L2_CID_CONTRAST;
246 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
247 return sprintf(buf, "%d\n", ctrl.value >> 8);
249 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
251 static ssize_t show_brightness(struct class_device *cd, char *buf)
253 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
254 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
255 struct v4l2_control ctrl;
256 ctrl.id = V4L2_CID_BRIGHTNESS;
259 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
260 return sprintf(buf, "%d\n", ctrl.value >> 8);
262 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
264 static ssize_t show_saturation(struct class_device *cd, char *buf)
266 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
267 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
268 struct v4l2_control ctrl;
269 ctrl.id = V4L2_CID_SATURATION;
272 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
273 return sprintf(buf, "%d\n", ctrl.value >> 8);
275 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
277 static ssize_t show_streaming(struct class_device *cd, char *buf)
279 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
280 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
281 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
283 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
285 static ssize_t show_compression(struct class_device *cd, char *buf)
287 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
288 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
289 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
291 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
293 static ssize_t show_device_bridge(struct class_device *cd, char *buf)
295 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
296 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
297 return sprintf(buf, "%d\n", usbvision->bridgeType);
299 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
301 static void usbvision_create_sysfs(struct video_device *vdev)
307 res=class_device_create_file(&vdev->class_dev,
308 &class_device_attr_version);
311 res=class_device_create_file(&vdev->class_dev,
312 &class_device_attr_model);
315 res=class_device_create_file(&vdev->class_dev,
316 &class_device_attr_hue);
319 res=class_device_create_file(&vdev->class_dev,
320 &class_device_attr_contrast);
323 res=class_device_create_file(&vdev->class_dev,
324 &class_device_attr_brightness);
327 res=class_device_create_file(&vdev->class_dev,
328 &class_device_attr_saturation);
331 res=class_device_create_file(&vdev->class_dev,
332 &class_device_attr_streaming);
335 res=class_device_create_file(&vdev->class_dev,
336 &class_device_attr_compression);
339 res=class_device_create_file(&vdev->class_dev,
340 &class_device_attr_bridge);
345 err("%s error: %d\n", __FUNCTION__, res);
348 static void usbvision_remove_sysfs(struct video_device *vdev)
351 class_device_remove_file(&vdev->class_dev,
352 &class_device_attr_version);
353 class_device_remove_file(&vdev->class_dev,
354 &class_device_attr_model);
355 class_device_remove_file(&vdev->class_dev,
356 &class_device_attr_hue);
357 class_device_remove_file(&vdev->class_dev,
358 &class_device_attr_contrast);
359 class_device_remove_file(&vdev->class_dev,
360 &class_device_attr_brightness);
361 class_device_remove_file(&vdev->class_dev,
362 &class_device_attr_saturation);
363 class_device_remove_file(&vdev->class_dev,
364 &class_device_attr_streaming);
365 class_device_remove_file(&vdev->class_dev,
366 &class_device_attr_compression);
367 class_device_remove_file(&vdev->class_dev,
368 &class_device_attr_bridge);
376 * This is part of Video 4 Linux API. The driver can be opened by one
377 * client only (checks internal counter 'usbvision->user'). The procedure
378 * then allocates buffers needed for video processing.
381 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
383 struct video_device *dev = video_devdata(file);
384 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
387 PDEBUG(DBG_IO, "open");
390 usbvision_reset_powerOffTimer(usbvision);
395 /* Allocate memory for the frame buffers */
396 errCode = usbvision_frames_alloc(usbvision);
398 /* Allocate memory for the scratch ring buffer */
399 errCode = usbvision_scratch_alloc(usbvision);
400 if ((!errCode) && (isocMode==ISOC_MODE_COMPRESS)) {
401 /* Allocate intermediate decompression buffers only if needed */
402 errCode = usbvision_decompress_alloc(usbvision);
406 /* Deallocate all buffers if trouble */
407 usbvision_frames_free(usbvision);
408 usbvision_scratch_free(usbvision);
409 usbvision_decompress_free(usbvision);
413 /* If so far no errors then we shall start the camera */
415 down(&usbvision->lock);
416 if (usbvision->power == 0) {
417 usbvision_power_on(usbvision);
418 usbvision_init_i2c(usbvision);
421 /* Send init sequence only once, it's large! */
422 if (!usbvision->initialized) {
424 setup_ok = usbvision_setup(usbvision,isocMode);
426 usbvision->initialized = 1;
432 usbvision_begin_streaming(usbvision);
433 errCode = usbvision_init_isoc(usbvision);
434 /* device needs to be initialized before isoc transfer */
435 usbvision_muxsel(usbvision,0);
440 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
441 usbvision_power_off(usbvision);
442 usbvision->initialized = 0;
445 up(&usbvision->lock);
452 usbvision_empty_framequeues(usbvision);
454 PDEBUG(DBG_IO, "success");
459 * usbvision_v4l2_close()
461 * This is part of Video 4 Linux API. The procedure
462 * stops streaming and deallocates all buffers that were earlier
463 * allocated in usbvision_v4l2_open().
466 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
468 struct video_device *dev = video_devdata(file);
469 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
471 PDEBUG(DBG_IO, "close");
472 down(&usbvision->lock);
474 usbvision_audio_off(usbvision);
475 usbvision_restart_isoc(usbvision);
476 usbvision_stop_isoc(usbvision);
478 usbvision_decompress_free(usbvision);
479 usbvision_frames_free(usbvision);
480 usbvision_scratch_free(usbvision);
485 /* power off in a little while to avoid off/on every close/open short sequences */
486 usbvision_set_powerOffTimer(usbvision);
487 usbvision->initialized = 0;
490 up(&usbvision->lock);
492 if (usbvision->remove_pending) {
493 info("%s: Final disconnect", __FUNCTION__);
494 usbvision_release(usbvision);
497 PDEBUG(DBG_IO, "success");
507 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
510 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
511 unsigned int cmd, void *arg)
513 struct video_device *dev = video_devdata(file);
514 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
516 if (!USBVISION_IS_OPERATIONAL(usbvision))
521 #ifdef CONFIG_VIDEO_ADV_DEBUG
522 /* ioctls to allow direct acces to the NT100x registers */
523 case VIDIOC_INT_G_REGISTER:
525 struct v4l2_register *reg = arg;
528 if (reg->i2c_id != 0)
530 /* NT100x has a 8-bit register space */
531 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
533 err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
536 reg->val=(unsigned char)errCode;
537 PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
538 (unsigned int)reg->reg, reg->val);
539 errCode = 0; // No error
543 case VIDIOC_INT_S_REGISTER:
545 struct v4l2_register *reg = arg;
548 if (reg->i2c_id != 0)
550 if (!capable(CAP_SYS_ADMIN))
552 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
554 err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
557 PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
558 (unsigned int)reg->reg, reg->val);
564 case VIDIOC_QUERYCAP:
566 struct v4l2_capability *vc=arg;
568 memset(vc, 0, sizeof(*vc));
569 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
570 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
572 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
573 sizeof(vc->bus_info));
574 vc->version = USBVISION_DRIVER_VERSION;
575 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
579 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
580 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
583 case VIDIOC_ENUMINPUT:
585 struct v4l2_input *vi = arg;
588 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
590 if (usbvision->have_tuner) {
594 chan = vi->index + 1; //skip Television string
598 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
599 strcpy(vi->name, "White Video Input");
602 strcpy(vi->name, "Television");
603 vi->type = V4L2_INPUT_TYPE_TUNER;
606 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
610 vi->type = V4L2_INPUT_TYPE_CAMERA;
611 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
612 strcpy(vi->name, "Green Video Input");
615 strcpy(vi->name, "Composite Video Input");
617 vi->std = V4L2_STD_PAL;
620 vi->type = V4L2_INPUT_TYPE_CAMERA;
621 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
622 strcpy(vi->name, "Yellow Video Input");
625 strcpy(vi->name, "S-Video Input");
627 vi->std = V4L2_STD_PAL;
630 vi->type = V4L2_INPUT_TYPE_CAMERA;
631 strcpy(vi->name, "Red Video Input");
632 vi->std = V4L2_STD_PAL;
635 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
636 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
641 struct v4l2_standard *e = arg;
648 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
649 tvnorms[e->index].name);
658 *input = usbvision->ctl_input;
664 if ((*input >= usbvision->video_inputs) || (*input < 0) )
666 usbvision->ctl_input = *input;
668 down(&usbvision->lock);
669 usbvision_muxsel(usbvision, usbvision->ctl_input);
670 usbvision_set_input(usbvision);
671 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
672 up(&usbvision->lock);
677 v4l2_std_id *id = arg;
679 *id = usbvision->tvnorm->id;
681 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
686 v4l2_std_id *id = arg;
689 for (i = 0; i < TVNORMS; i++)
690 if (*id == tvnorms[i].id)
693 for (i = 0; i < TVNORMS; i++)
694 if (*id & tvnorms[i].id)
699 down(&usbvision->lock);
700 usbvision->tvnorm = &tvnorms[i];
702 call_i2c_clients(usbvision, VIDIOC_S_STD,
703 &usbvision->tvnorm->id);
705 up(&usbvision->lock);
707 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
712 struct v4l2_tuner *vt = arg;
714 if (!usbvision->have_tuner || vt->index) // Only tuner 0
716 strcpy(vt->name, "Television");
717 /* Let clients fill in the remainder of this struct */
718 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
720 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
725 struct v4l2_tuner *vt = arg;
727 // Only no or one tuner for now
728 if (!usbvision->have_tuner || vt->index)
730 /* let clients handle this */
731 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
733 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
736 case VIDIOC_G_FREQUENCY:
738 struct v4l2_frequency *freq = arg;
740 freq->tuner = 0; // Only one tuner
741 freq->type = V4L2_TUNER_ANALOG_TV;
742 freq->frequency = usbvision->freq;
743 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
746 case VIDIOC_S_FREQUENCY:
748 struct v4l2_frequency *freq = arg;
750 // Only no or one tuner for now
751 if (!usbvision->have_tuner || freq->tuner)
754 usbvision->freq = freq->frequency;
755 call_i2c_clients(usbvision, cmd, freq);
756 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
761 struct v4l2_audio *v = arg;
762 memset(v,0, sizeof(v));
763 strcpy(v->name, "TV");
764 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
769 struct v4l2_audio *v = arg;
773 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
776 case VIDIOC_QUERYCTRL:
778 struct v4l2_queryctrl *ctrl = arg;
781 memset(ctrl,0,sizeof(*ctrl));
784 call_i2c_clients(usbvision, cmd, arg);
791 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
795 struct v4l2_control *ctrl = arg;
796 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
797 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
802 struct v4l2_control *ctrl = arg;
804 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
805 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
810 struct v4l2_requestbuffers *vr = arg;
813 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
815 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
816 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
817 (vr->memory != V4L2_MEMORY_MMAP))
820 if(usbvision->streaming == Stream_On) {
821 if ((ret = usbvision_stream_interrupt(usbvision)))
825 usbvision_empty_framequeues(usbvision);
827 usbvision->curFrame = NULL;
829 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
832 case VIDIOC_QUERYBUF:
834 struct v4l2_buffer *vb = arg;
835 struct usbvision_frame *frame;
837 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
839 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
842 if(vb->index>=USBVISION_NUMFRAMES) {
845 // Updating the corresponding frame state
847 frame = &usbvision->frame[vb->index];
848 if(frame->grabstate >= FrameState_Ready)
849 vb->flags |= V4L2_BUF_FLAG_QUEUED;
850 if(frame->grabstate >= FrameState_Done)
851 vb->flags |= V4L2_BUF_FLAG_DONE;
852 if(frame->grabstate == FrameState_Unused)
853 vb->flags |= V4L2_BUF_FLAG_MAPPED;
854 vb->memory = V4L2_MEMORY_MMAP;
856 vb->m.offset = vb->index*usbvision->max_frame_size;
858 vb->memory = V4L2_MEMORY_MMAP;
859 vb->field = V4L2_FIELD_NONE;
860 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
861 vb->timestamp = usbvision->frame[vb->index].timestamp;
862 vb->sequence = usbvision->frame[vb->index].sequence;
867 struct v4l2_buffer *vb = arg;
868 struct usbvision_frame *frame;
869 unsigned long lock_flags;
871 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
872 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
875 if(vb->index>=USBVISION_NUMFRAMES) {
879 frame = &usbvision->frame[vb->index];
881 if (frame->grabstate != FrameState_Unused) {
885 /* Mark it as ready and enqueue frame */
886 frame->grabstate = FrameState_Ready;
887 frame->scanstate = ScanState_Scanning;
888 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
890 vb->flags &= ~V4L2_BUF_FLAG_DONE;
892 /* set v4l2_format index */
893 frame->v4l2_format = usbvision->palette;
895 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
896 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
897 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
899 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
904 struct v4l2_buffer *vb = arg;
906 struct usbvision_frame *f;
907 unsigned long lock_flags;
909 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
912 if (list_empty(&(usbvision->outqueue))) {
913 if (usbvision->streaming == Stream_Idle)
915 ret = wait_event_interruptible
916 (usbvision->wait_frame,
917 !list_empty(&(usbvision->outqueue)));
922 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
923 f = list_entry(usbvision->outqueue.next,
924 struct usbvision_frame, frame);
925 list_del(usbvision->outqueue.next);
926 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
928 f->grabstate = FrameState_Unused;
930 vb->memory = V4L2_MEMORY_MMAP;
931 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
932 vb->index = f->index;
933 vb->sequence = f->sequence;
934 vb->timestamp = f->timestamp;
935 vb->field = V4L2_FIELD_NONE;
936 vb->bytesused = f->scanlength;
940 case VIDIOC_STREAMON:
942 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
944 usbvision->streaming = Stream_On;
946 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
948 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
952 case VIDIOC_STREAMOFF:
955 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
957 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
960 if(usbvision->streaming == Stream_On) {
961 usbvision_stream_interrupt(usbvision);
962 // Stop all video streamings
963 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
965 usbvision_empty_framequeues(usbvision);
967 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
970 case VIDIOC_ENUM_FMT:
972 struct v4l2_fmtdesc *vfd = arg;
974 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
978 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
979 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
980 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
981 memset(vfd->reserved, 0, sizeof(vfd->reserved));
986 struct v4l2_format *vf = arg;
989 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
991 vf->fmt.pix.width = usbvision->curwidth;
992 vf->fmt.pix.height = usbvision->curheight;
993 vf->fmt.pix.pixelformat = usbvision->palette.format;
994 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
995 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
996 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
997 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
998 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
999 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1003 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
1008 case VIDIOC_TRY_FMT:
1011 struct v4l2_format *vf = arg;
1015 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1017 /* Find requested format in available ones */
1018 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1019 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
1020 usbvision->palette = usbvision_v4l2_format[formatIdx];
1025 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1028 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1029 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1031 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
1032 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1034 if(cmd == VIDIOC_TRY_FMT) {
1035 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1036 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1040 /* stop io in case it is already in progress */
1041 if(usbvision->streaming == Stream_On) {
1042 if ((ret = usbvision_stream_interrupt(usbvision)))
1045 usbvision_empty_framequeues(usbvision);
1047 usbvision->curFrame = NULL;
1049 // by now we are committed to the new data...
1050 down(&usbvision->lock);
1051 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1052 up(&usbvision->lock);
1054 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1055 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1063 return -ENOIOCTLCMD;
1068 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1069 unsigned int cmd, unsigned long arg)
1071 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1075 static ssize_t usbvision_v4l2_read(struct file *file, char *buf,
1076 size_t count, loff_t *ppos)
1078 struct video_device *dev = video_devdata(file);
1079 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1080 int noblock = file->f_flags & O_NONBLOCK;
1081 unsigned long lock_flags;
1085 struct usbvision_frame *frame;
1087 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1089 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1092 /* no stream is running, make it running ! */
1093 usbvision->streaming = Stream_On;
1094 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1096 /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1097 for(i=0;i<USBVISION_NUMFRAMES;i++) {
1098 frame = &usbvision->frame[i];
1099 if(frame->grabstate == FrameState_Unused) {
1100 /* Mark it as ready and enqueue frame */
1101 frame->grabstate = FrameState_Ready;
1102 frame->scanstate = ScanState_Scanning;
1103 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1105 /* set v4l2_format index */
1106 frame->v4l2_format = usbvision->palette;
1108 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1109 list_add_tail(&frame->frame, &usbvision->inqueue);
1110 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1114 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1115 if (list_empty(&(usbvision->outqueue))) {
1119 ret = wait_event_interruptible
1120 (usbvision->wait_frame,
1121 !list_empty(&(usbvision->outqueue)));
1126 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1127 frame = list_entry(usbvision->outqueue.next,
1128 struct usbvision_frame, frame);
1129 list_del(usbvision->outqueue.next);
1130 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1132 /* An error returns an empty frame */
1133 if (frame->grabstate == FrameState_Error) {
1134 frame->bytes_read = 0;
1138 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1139 frame->index, frame->bytes_read, frame->scanlength);
1141 /* copy bytes to user space; we allow for partials reads */
1142 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1143 count = frame->scanlength - frame->bytes_read;
1145 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1149 frame->bytes_read += count;
1150 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1151 (unsigned long)count, frame->bytes_read);
1153 // For now, forget the frame if it has not been read in one shot.
1154 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1155 frame->bytes_read = 0;
1157 /* Mark it as available to be used again. */
1158 usbvision->frame[frmx].grabstate = FrameState_Unused;
1164 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1166 unsigned long size = vma->vm_end - vma->vm_start,
1167 start = vma->vm_start;
1171 struct video_device *dev = video_devdata(file);
1172 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1174 down(&usbvision->lock);
1176 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1177 up(&usbvision->lock);
1181 if (!(vma->vm_flags & VM_WRITE) ||
1182 size != PAGE_ALIGN(usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel)) {
1183 up(&usbvision->lock);
1187 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1188 if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1191 if (i == USBVISION_NUMFRAMES) {
1192 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1193 up(&usbvision->lock);
1197 /* VM_IO is eventually going to replace PageReserved altogether */
1198 vma->vm_flags |= VM_IO;
1199 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1201 pos = usbvision->frame[i].data;
1204 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1205 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1206 up(&usbvision->lock);
1214 up(&usbvision->lock);
1220 * Here comes the stuff for radio on usbvision based devices
1223 static int usbvision_radio_open(struct inode *inode, struct file *file)
1225 struct video_device *dev = video_devdata(file);
1226 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1227 struct v4l2_frequency freq;
1230 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1232 down(&usbvision->lock);
1234 if (usbvision->user) {
1235 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1240 usbvision_reset_powerOffTimer(usbvision);
1241 if (usbvision->power == 0) {
1242 usbvision_power_on(usbvision);
1243 usbvision_init_i2c(usbvision);
1247 // If so far no errors then we shall start the radio
1248 usbvision->radio = 1;
1249 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1250 freq.frequency = 1517; //SWR3 @ 94.8MHz
1251 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1252 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1257 if (PowerOnAtOpen) {
1258 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1259 usbvision_power_off(usbvision);
1260 usbvision->initialized = 0;
1263 up(&usbvision->lock);
1268 static int usbvision_radio_close(struct inode *inode, struct file *file)
1270 struct video_device *dev = video_devdata(file);
1271 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1276 down(&usbvision->lock);
1278 usbvision_audio_off(usbvision);
1282 if (PowerOnAtOpen) {
1283 usbvision_set_powerOffTimer(usbvision);
1284 usbvision->initialized = 0;
1287 up(&usbvision->lock);
1289 if (usbvision->remove_pending) {
1290 info("%s: Final disconnect", __FUNCTION__);
1291 usbvision_release(usbvision);
1295 PDEBUG(DBG_IO, "success");
1300 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1301 unsigned int cmd, void *arg)
1303 struct video_device *dev = video_devdata(file);
1304 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1306 if (!USBVISION_IS_OPERATIONAL(usbvision))
1310 case VIDIOC_QUERYCAP:
1312 struct v4l2_capability *vc=arg;
1314 memset(vc, 0, sizeof(*vc));
1315 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1316 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1318 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1319 sizeof(vc->bus_info));
1320 vc->version = USBVISION_DRIVER_VERSION;
1321 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1322 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1325 case VIDIOC_QUERYCTRL:
1327 struct v4l2_queryctrl *ctrl = arg;
1330 memset(ctrl,0,sizeof(*ctrl));
1333 call_i2c_clients(usbvision, cmd, arg);
1334 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1344 struct v4l2_control *ctrl = arg;
1346 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1347 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1352 struct v4l2_control *ctrl = arg;
1354 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1355 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1358 case VIDIOC_G_TUNER:
1360 struct v4l2_tuner *t = arg;
1365 memset(t,0,sizeof(*t));
1366 strcpy(t->name, "Radio");
1367 t->type = V4L2_TUNER_RADIO;
1369 /* Let clients fill in the remainder of this struct */
1370 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1371 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1374 case VIDIOC_S_TUNER:
1376 struct v4l2_tuner *vt = arg;
1378 // Only no or one tuner for now
1379 if (!usbvision->have_tuner || vt->index)
1381 /* let clients handle this */
1382 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1384 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1387 case VIDIOC_G_AUDIO:
1389 struct v4l2_audio *a = arg;
1391 memset(a,0,sizeof(*a));
1392 strcpy(a->name,"Radio");
1393 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1396 case VIDIOC_S_AUDIO:
1397 case VIDIOC_S_INPUT:
1401 case VIDIOC_G_FREQUENCY:
1403 struct v4l2_frequency *f = arg;
1405 memset(f,0,sizeof(*f));
1407 f->type = V4L2_TUNER_RADIO;
1408 f->frequency = usbvision->freq;
1409 call_i2c_clients(usbvision, cmd, f);
1410 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1414 case VIDIOC_S_FREQUENCY:
1416 struct v4l2_frequency *f = arg;
1420 usbvision->freq = f->frequency;
1421 call_i2c_clients(usbvision, cmd, f);
1422 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1428 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1429 return -ENOIOCTLCMD;
1436 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1437 unsigned int cmd, unsigned long arg)
1439 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1444 * Here comes the stuff for vbi on usbvision based devices
1447 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1454 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1460 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1461 unsigned int cmd, void *arg)
1467 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1468 unsigned int cmd, unsigned long arg)
1470 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1475 // Video registration stuff
1479 static struct file_operations usbvision_fops = {
1480 .owner = THIS_MODULE,
1481 .open = usbvision_v4l2_open,
1482 .release = usbvision_v4l2_close,
1483 .read = usbvision_v4l2_read,
1484 .mmap = usbvision_v4l2_mmap,
1485 .ioctl = usbvision_v4l2_ioctl,
1486 .llseek = no_llseek,
1488 static struct video_device usbvision_video_template = {
1489 .owner = THIS_MODULE,
1490 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1491 .hardware = VID_HARDWARE_USBVISION,
1492 .fops = &usbvision_fops,
1493 .name = "usbvision-video",
1494 .release = video_device_release,
1500 static struct file_operations usbvision_radio_fops = {
1501 .owner = THIS_MODULE,
1502 .open = usbvision_radio_open,
1503 .release = usbvision_radio_close,
1504 .ioctl = usbvision_radio_ioctl,
1505 .llseek = no_llseek,
1508 static struct video_device usbvision_radio_template=
1510 .owner = THIS_MODULE,
1511 .type = VID_TYPE_TUNER,
1512 .hardware = VID_HARDWARE_USBVISION,
1513 .fops = &usbvision_radio_fops,
1514 .release = video_device_release,
1515 .name = "usbvision-radio",
1521 static struct file_operations usbvision_vbi_fops = {
1522 .owner = THIS_MODULE,
1523 .open = usbvision_vbi_open,
1524 .release = usbvision_vbi_close,
1525 .ioctl = usbvision_vbi_ioctl,
1526 .llseek = no_llseek,
1529 static struct video_device usbvision_vbi_template=
1531 .owner = THIS_MODULE,
1532 .type = VID_TYPE_TUNER,
1533 .hardware = VID_HARDWARE_USBVISION,
1534 .fops = &usbvision_vbi_fops,
1535 .release = video_device_release,
1536 .name = "usbvision-vbi",
1541 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1542 struct video_device *vdev_template,
1545 struct usb_device *usb_dev = usbvision->dev;
1546 struct video_device *vdev;
1548 if (usb_dev == NULL) {
1549 err("%s: usbvision->dev is not set", __FUNCTION__);
1553 vdev = video_device_alloc();
1557 *vdev = *vdev_template;
1558 // vdev->minor = -1;
1559 vdev->dev = &usb_dev->dev;
1560 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1561 video_set_drvdata(vdev, usbvision);
1565 // unregister video4linux devices
1566 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1569 if (usbvision->vbi) {
1570 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1571 if (usbvision->vbi->minor != -1) {
1572 video_unregister_device(usbvision->vbi);
1575 video_device_release(usbvision->vbi);
1577 usbvision->vbi = NULL;
1581 if (usbvision->rdev) {
1582 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1583 if (usbvision->rdev->minor != -1) {
1584 video_unregister_device(usbvision->rdev);
1587 video_device_release(usbvision->rdev);
1589 usbvision->rdev = NULL;
1593 if (usbvision->vdev) {
1594 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1595 if (usbvision->vdev->minor != -1) {
1596 video_unregister_device(usbvision->vdev);
1599 video_device_release(usbvision->vdev);
1601 usbvision->vdev = NULL;
1605 // register video4linux devices
1606 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1609 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1610 if (usbvision->vdev == NULL) {
1613 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1616 info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
1619 if (usbvision_device_data[usbvision->DevModel].Radio) {
1620 // usbvision has radio
1621 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1622 if (usbvision->rdev == NULL) {
1625 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1628 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
1631 if (usbvision_device_data[usbvision->DevModel].vbi) {
1632 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1633 if (usbvision->vdev == NULL) {
1636 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1639 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
1645 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1646 usbvision_unregister_video(usbvision);
1653 * This code allocates the struct usb_usbvision. It is filled with default values.
1655 * Returns NULL on error, a pointer to usb_usbvision else.
1658 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1660 struct usb_usbvision *usbvision;
1662 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1666 usbvision->dev = dev;
1668 init_MUTEX(&usbvision->lock); /* to 1 == available */
1670 // prepare control urb for control messages during interrupts
1671 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1672 if (usbvision->ctrlUrb == NULL) {
1675 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1676 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1678 usbvision_init_powerOffTimer(usbvision);
1683 if (usbvision && usbvision->ctrlUrb) {
1684 usb_free_urb(usbvision->ctrlUrb);
1693 * usbvision_release()
1695 * This code does final release of struct usb_usbvision. This happens
1696 * after the device is disconnected -and- all clients closed their files.
1699 static void usbvision_release(struct usb_usbvision *usbvision)
1701 PDEBUG(DBG_PROBE, "");
1703 down(&usbvision->lock);
1705 usbvision_reset_powerOffTimer(usbvision);
1707 usbvision->initialized = 0;
1709 up(&usbvision->lock);
1711 usbvision_remove_sysfs(usbvision->vdev);
1712 usbvision_unregister_video(usbvision);
1714 if (usbvision->ctrlUrb) {
1715 usb_free_urb(usbvision->ctrlUrb);
1720 PDEBUG(DBG_PROBE, "success");
1724 /******************************** usb interface *****************************************/
1726 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1730 if (usbvision == NULL)
1733 model = usbvision->DevModel;
1734 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1736 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
1737 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
1739 usbvision->Vin_Reg2_Preset = 0;
1742 for (i = 0; i < TVNORMS; i++)
1743 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1747 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1749 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1750 usbvision->ctl_input = 0;
1752 /* This should be here to make i2c clients to be able to register */
1753 usbvision_audio_off(usbvision); //first switch off audio
1754 if (!PowerOnAtOpen) {
1755 usbvision_power_on(usbvision); //and then power up the noisy tuner
1756 usbvision_init_i2c(usbvision);
1763 * This procedure queries device descriptor and accepts the interface
1764 * if it looks like USBVISION video device
1767 static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
1769 struct usb_device *dev = interface_to_usbdev(intf);
1770 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1771 const struct usb_host_interface *interface;
1772 struct usb_usbvision *usbvision = NULL;
1773 const struct usb_endpoint_descriptor *endpoint;
1776 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1777 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
1778 /* Is it an USBVISION video dev? */
1780 for(model = 0; usbvision_device_data[model].idVendor; model++) {
1781 if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
1784 if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
1788 info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
1792 if (usbvision_device_data[model].idVendor == 0) {
1793 return -ENODEV; //no matching device
1795 if (usbvision_device_data[model].Interface >= 0) {
1796 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1799 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1801 endpoint = &interface->endpoint[1].desc;
1802 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1803 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1804 err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
1807 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1808 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1814 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1815 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1818 if (dev->descriptor.bNumConfigurations > 1) {
1819 usbvision->bridgeType = BRIDGE_NT1004;
1821 else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1822 usbvision->bridgeType = BRIDGE_NT1005;
1825 usbvision->bridgeType = BRIDGE_NT1003;
1827 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1829 down(&usbvision->lock);
1831 usbvision->nr = usbvision_nr++;
1833 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1834 if (usbvision->have_tuner) {
1835 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1838 usbvision->tuner_addr = ADDR_UNSET;
1840 usbvision->DevModel = model;
1841 usbvision->remove_pending = 0;
1842 usbvision->iface = ifnum;
1843 usbvision->ifaceAltInactive = 0;
1844 usbvision->ifaceAltActive = 1;
1845 usbvision->video_endp = endpoint->bEndpointAddress;
1846 usbvision->isocPacketSize = 0;
1847 usbvision->usb_bandwidth = 0;
1848 usbvision->user = 0;
1849 usbvision->streaming = Stream_Off;
1850 usbvision_register_video(usbvision);
1851 usbvision_configure_video(usbvision);
1852 up(&usbvision->lock);
1855 usb_set_intfdata (intf, usbvision);
1856 usbvision_create_sysfs(usbvision->vdev);
1858 PDEBUG(DBG_PROBE, "success");
1864 * usbvision_disconnect()
1866 * This procedure stops all driver activity, deallocates interface-private
1867 * structure (pointed by 'ptr') and after that driver should be removable
1868 * with no ill consequences.
1871 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1873 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1875 PDEBUG(DBG_PROBE, "");
1877 if (usbvision == NULL) {
1878 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1881 usb_set_intfdata (intf, NULL);
1883 down(&usbvision->lock);
1885 // At this time we ask to cancel outstanding URBs
1886 usbvision_stop_isoc(usbvision);
1888 if (usbvision->power) {
1889 usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
1890 usbvision_power_off(usbvision);
1892 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1894 usb_put_dev(usbvision->dev);
1895 usbvision->dev = NULL; // USB device is no more
1897 up(&usbvision->lock);
1899 if (usbvision->user) {
1900 info("%s: In use, disconnect pending", __FUNCTION__);
1901 wake_up_interruptible(&usbvision->wait_frame);
1902 wake_up_interruptible(&usbvision->wait_stream);
1905 usbvision_release(usbvision);
1908 PDEBUG(DBG_PROBE, "success");
1912 static struct usb_driver usbvision_driver = {
1913 .name = "usbvision",
1914 .id_table = usbvision_table,
1915 .probe = usbvision_probe,
1916 .disconnect = usbvision_disconnect
1920 * customdevice_process()
1922 * This procedure preprocesses CustomDevice parameter if any
1925 static void customdevice_process(void)
1927 usbvision_device_data[0]=usbvision_device_data[1];
1928 usbvision_table[0]=usbvision_table[1];
1932 char *parse=CustomDevice;
1934 PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
1936 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1937 usbvision_device_data[0].idVendor;
1938 usbvision_device_data[0].idProduct;
1939 usbvision_device_data[0].Interface;
1940 usbvision_device_data[0].Codec;
1941 usbvision_device_data[0].VideoChannels;
1942 usbvision_device_data[0].VideoNorm;
1943 usbvision_device_data[0].AudioChannels;
1944 usbvision_device_data[0].Radio;
1945 usbvision_device_data[0].Tuner;
1946 usbvision_device_data[0].TunerType;
1947 usbvision_device_data[0].Vin_Reg1;
1948 usbvision_device_data[0].Vin_Reg2;
1949 usbvision_device_data[0].X_Offset;
1950 usbvision_device_data[0].Y_Offset;
1951 usbvision_device_data[0].Dvi_yuv;
1952 usbvision_device_data[0].ModelString;
1956 usbvision_device_data[0].ModelString="USBVISION Custom Device";
1959 sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
1961 PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
1963 sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
1965 PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
1966 sscanf(parse,"%d",&usbvision_device_data[0].Interface);
1968 PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
1969 sscanf(parse,"%d",&usbvision_device_data[0].Codec);
1971 PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
1972 sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
1974 PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
1979 PDEBUG(DBG_PROBE, "VideoNorm=PAL");
1980 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
1984 PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
1985 usbvision_device_data[0].VideoNorm=V4L2_STD_SECAM;
1989 PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
1990 usbvision_device_data[0].VideoNorm=V4L2_STD_NTSC;
1994 PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
1995 usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
2000 sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
2002 PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
2003 sscanf(parse,"%d",&usbvision_device_data[0].Radio);
2005 PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
2006 sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
2008 PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
2009 sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
2011 PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
2012 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
2014 PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
2015 sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
2017 PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
2018 sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
2020 PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
2021 sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
2023 PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
2024 sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
2025 PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
2027 //add to usbvision_table also
2028 usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
2029 usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
2030 usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
2040 * This code is run to initialize the driver.
2043 static int __init usbvision_init(void)
2047 PDEBUG(DBG_PROBE, "");
2049 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
2050 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
2051 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
2052 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
2054 /* disable planar mode support unless compression enabled */
2055 if (isocMode != ISOC_MODE_COMPRESS ) {
2056 // FIXME : not the right way to set supported flag
2057 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
2058 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
2061 customdevice_process();
2063 errCode = usb_register(&usbvision_driver);
2066 info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
2067 PDEBUG(DBG_PROBE, "success");
2072 static void __exit usbvision_exit(void)
2074 PDEBUG(DBG_PROBE, "");
2076 usb_deregister(&usbvision_driver);
2077 PDEBUG(DBG_PROBE, "success");
2080 module_init(usbvision_init);
2081 module_exit(usbvision_exit);
2084 * Overrides for Emacs so that we follow Linus's tabbing style.
2085 * ---------------------------------------------------------------------------