2 * USB USBVISION Video device driver 0.9.9
6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
8 * This module is part of usbvision driver project.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Let's call the version 0.... until compression decoding is completely
27 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
28 * It was based on USB CPiA driver written by Peter Pregler,
29 * Scott J. Bertin and Johannes Erdfelt
30 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
31 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
32 * Updates to driver completed by Dwaine P. Garden
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
39 * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one?
40 * - Clean up the driver.
41 * - optimization for performance.
42 * - Add Videotext capability (VBI). Working on it.....
43 * - Check audio for other devices
47 #include <linux/version.h>
48 #include <linux/kernel.h>
49 #include <linux/list.h>
50 #include <linux/timer.h>
51 #include <linux/slab.h>
53 #include <linux/utsname.h>
54 #include <linux/highmem.h>
55 #include <linux/videodev.h>
56 #include <linux/vmalloc.h>
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/spinlock.h>
61 #include <linux/videodev2.h>
62 #include <linux/video_decoder.h>
63 #include <linux/i2c.h>
65 #include <media/saa7115.h>
66 #include <media/v4l2-common.h>
67 #include <media/tuner.h>
68 #include <media/audiochip.h>
70 #include <linux/moduleparam.h>
71 #include <linux/workqueue.h>
74 #include <linux/kmod.h>
77 #include "usbvision.h"
78 #include "usbvision-cards.h"
80 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
81 #define DRIVER_NAME "usbvision"
82 #define DRIVER_ALIAS "USBVision"
83 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
84 #define DRIVER_LICENSE "GPL"
85 #define USBVISION_DRIVER_VERSION_MAJOR 0
86 #define USBVISION_DRIVER_VERSION_MINOR 9
87 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
88 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
89 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
91 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
94 #ifdef USBVISION_DEBUG
95 #define PDEBUG(level, fmt, args...) \
96 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
98 #define PDEBUG(level, fmt, args...) do {} while(0)
101 #define DBG_IOCTL 1<<0
103 #define DBG_PROBE 1<<2
104 #define DBG_MMAP 1<<3
107 #define rmspace(str) while(*str==' ') str++;
108 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
111 static int usbvision_nr = 0; // sequential number of usbvision device
113 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
114 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
115 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
116 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
117 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
118 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
119 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
120 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
121 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
124 /* supported tv norms */
125 static struct usbvision_tvnorm tvnorms[] = {
134 .id = V4L2_STD_SECAM,
137 .id = V4L2_STD_PAL_M,
141 #define TVNORMS ARRAY_SIZE(tvnorms)
143 // Function prototypes
144 static void usbvision_release(struct usb_usbvision *usbvision);
146 // Default initalization of device driver parameters
147 static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
148 static int video_debug = 0; // Set the default Debug Mode of the device driver
149 static int PowerOnAtOpen = 1; // Set the default device to power on at startup
150 static int video_nr = -1; // Sequential Number of Video Device
151 static int radio_nr = -1; // Sequential Number of Radio Device
152 static int vbi_nr = -1; // Sequential Number of VBI Device
154 // Grab parameters for the device driver
156 #if defined(module_param) // Showing parameters under SYSFS
157 module_param(isocMode, int, 0444);
158 module_param(video_debug, int, 0444);
159 module_param(PowerOnAtOpen, int, 0444);
160 module_param(video_nr, int, 0444);
161 module_param(radio_nr, int, 0444);
162 module_param(vbi_nr, int, 0444);
164 MODULE_PARAM(isocMode, "i");
165 MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
166 MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
167 MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
168 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.
169 MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
170 MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
171 MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
174 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
175 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
176 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
177 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
178 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
179 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
183 MODULE_AUTHOR(DRIVER_AUTHOR);
184 MODULE_DESCRIPTION(DRIVER_DESC);
185 MODULE_LICENSE(DRIVER_LICENSE);
186 MODULE_VERSION(USBVISION_VERSION_STRING);
187 MODULE_ALIAS(DRIVER_ALIAS);
190 /****************************************************************************************/
191 /* SYSFS Code - Copied from the stv680.c usb module. */
192 /* Device information is located at /sys/class/video4linux/video0 */
193 /* Device parameters information is located at /sys/module/usbvision */
194 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
195 /****************************************************************************************/
198 #define YES_NO(x) ((x) ? "Yes" : "No")
200 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
202 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
203 return video_get_drvdata(vdev);
206 static ssize_t show_version(struct class_device *cd, char *buf)
208 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
210 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
212 static ssize_t show_model(struct class_device *cd, char *buf)
214 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
215 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
216 return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
218 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
220 static ssize_t show_hue(struct class_device *cd, char *buf)
222 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
223 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
224 struct v4l2_control ctrl;
225 ctrl.id = V4L2_CID_HUE;
228 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
229 return sprintf(buf, "%d\n", ctrl.value);
231 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
233 static ssize_t show_contrast(struct class_device *cd, char *buf)
235 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
236 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
237 struct v4l2_control ctrl;
238 ctrl.id = V4L2_CID_CONTRAST;
241 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
242 return sprintf(buf, "%d\n", ctrl.value);
244 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
246 static ssize_t show_brightness(struct class_device *cd, char *buf)
248 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
249 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
250 struct v4l2_control ctrl;
251 ctrl.id = V4L2_CID_BRIGHTNESS;
254 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
255 return sprintf(buf, "%d\n", ctrl.value);
257 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
259 static ssize_t show_saturation(struct class_device *cd, char *buf)
261 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
262 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
263 struct v4l2_control ctrl;
264 ctrl.id = V4L2_CID_SATURATION;
267 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
268 return sprintf(buf, "%d\n", ctrl.value);
270 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
272 static ssize_t show_streaming(struct class_device *cd, char *buf)
274 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
276 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
278 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
280 static ssize_t show_compression(struct class_device *cd, char *buf)
282 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
283 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
284 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
286 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
288 static ssize_t show_device_bridge(struct class_device *cd, char *buf)
290 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
291 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
292 return sprintf(buf, "%d\n", usbvision->bridgeType);
294 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
296 static void usbvision_create_sysfs(struct video_device *vdev)
302 res=class_device_create_file(&vdev->class_dev,
303 &class_device_attr_version);
306 res=class_device_create_file(&vdev->class_dev,
307 &class_device_attr_model);
310 res=class_device_create_file(&vdev->class_dev,
311 &class_device_attr_hue);
314 res=class_device_create_file(&vdev->class_dev,
315 &class_device_attr_contrast);
318 res=class_device_create_file(&vdev->class_dev,
319 &class_device_attr_brightness);
322 res=class_device_create_file(&vdev->class_dev,
323 &class_device_attr_saturation);
326 res=class_device_create_file(&vdev->class_dev,
327 &class_device_attr_streaming);
330 res=class_device_create_file(&vdev->class_dev,
331 &class_device_attr_compression);
334 res=class_device_create_file(&vdev->class_dev,
335 &class_device_attr_bridge);
340 err("%s error: %d\n", __FUNCTION__, res);
343 static void usbvision_remove_sysfs(struct video_device *vdev)
346 class_device_remove_file(&vdev->class_dev,
347 &class_device_attr_version);
348 class_device_remove_file(&vdev->class_dev,
349 &class_device_attr_model);
350 class_device_remove_file(&vdev->class_dev,
351 &class_device_attr_hue);
352 class_device_remove_file(&vdev->class_dev,
353 &class_device_attr_contrast);
354 class_device_remove_file(&vdev->class_dev,
355 &class_device_attr_brightness);
356 class_device_remove_file(&vdev->class_dev,
357 &class_device_attr_saturation);
358 class_device_remove_file(&vdev->class_dev,
359 &class_device_attr_streaming);
360 class_device_remove_file(&vdev->class_dev,
361 &class_device_attr_compression);
362 class_device_remove_file(&vdev->class_dev,
363 &class_device_attr_bridge);
371 * This is part of Video 4 Linux API. The driver can be opened by one
372 * client only (checks internal counter 'usbvision->user'). The procedure
373 * then allocates buffers needed for video processing.
376 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
378 struct video_device *dev = video_devdata(file);
379 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
382 PDEBUG(DBG_IO, "open");
385 usbvision_reset_powerOffTimer(usbvision);
390 /* Allocate memory for the scratch ring buffer */
391 errCode = usbvision_scratch_alloc(usbvision);
392 if (isocMode==ISOC_MODE_COMPRESS) {
393 /* Allocate intermediate decompression buffers only if needed */
394 errCode = usbvision_decompress_alloc(usbvision);
397 /* Deallocate all buffers if trouble */
398 usbvision_scratch_free(usbvision);
399 usbvision_decompress_free(usbvision);
403 /* If so far no errors then we shall start the camera */
405 down(&usbvision->lock);
406 if (usbvision->power == 0) {
407 usbvision_power_on(usbvision);
408 usbvision_i2c_register(usbvision);
411 /* Send init sequence only once, it's large! */
412 if (!usbvision->initialized) {
414 setup_ok = usbvision_setup(usbvision,isocMode);
416 usbvision->initialized = 1;
422 usbvision_begin_streaming(usbvision);
423 errCode = usbvision_init_isoc(usbvision);
424 /* device needs to be initialized before isoc transfer */
425 usbvision_muxsel(usbvision,0);
430 usbvision_i2c_unregister(usbvision);
431 usbvision_power_off(usbvision);
432 usbvision->initialized = 0;
435 up(&usbvision->lock);
442 usbvision_empty_framequeues(usbvision);
444 PDEBUG(DBG_IO, "success");
449 * usbvision_v4l2_close()
451 * This is part of Video 4 Linux API. The procedure
452 * stops streaming and deallocates all buffers that were earlier
453 * allocated in usbvision_v4l2_open().
456 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
458 struct video_device *dev = video_devdata(file);
459 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
461 PDEBUG(DBG_IO, "close");
462 down(&usbvision->lock);
464 usbvision_audio_off(usbvision);
465 usbvision_restart_isoc(usbvision);
466 usbvision_stop_isoc(usbvision);
468 usbvision_decompress_free(usbvision);
469 usbvision_frames_free(usbvision);
470 usbvision_empty_framequeues(usbvision);
471 usbvision_scratch_free(usbvision);
476 /* power off in a little while to avoid off/on every close/open short sequences */
477 usbvision_set_powerOffTimer(usbvision);
478 usbvision->initialized = 0;
481 up(&usbvision->lock);
483 if (usbvision->remove_pending) {
484 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
485 usbvision_release(usbvision);
488 PDEBUG(DBG_IO, "success");
498 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
501 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
502 unsigned int cmd, void *arg)
504 struct video_device *dev = video_devdata(file);
505 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
507 if (!USBVISION_IS_OPERATIONAL(usbvision))
512 #ifdef CONFIG_VIDEO_ADV_DEBUG
513 /* ioctls to allow direct acces to the NT100x registers */
514 case VIDIOC_DBG_G_REGISTER:
515 case VIDIOC_DBG_S_REGISTER:
517 struct v4l2_register *reg = arg;
520 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
522 if (!capable(CAP_SYS_ADMIN))
524 /* NT100x has a 8-bit register space */
525 if (cmd == VIDIOC_DBG_G_REGISTER)
526 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
528 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
530 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__,
531 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S', errCode);
534 if (cmd == VIDIOC_DBG_S_REGISTER)
535 reg->val = (u8)errCode;
537 PDEBUG(DBG_IOCTL, "VIDIOC_DBG_%c_REGISTER reg=0x%02X, value=0x%02X",
538 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S',
539 (unsigned int)reg->reg, (unsigned int)reg->val);
543 case VIDIOC_QUERYCAP:
545 struct v4l2_capability *vc=arg;
547 memset(vc, 0, sizeof(*vc));
548 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
549 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
551 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
552 sizeof(vc->bus_info));
553 vc->version = USBVISION_DRIVER_VERSION;
554 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
558 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
559 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
562 case VIDIOC_ENUMINPUT:
564 struct v4l2_input *vi = arg;
567 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
569 if (usbvision->have_tuner) {
573 chan = vi->index + 1; //skip Television string
577 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
578 strcpy(vi->name, "White Video Input");
581 strcpy(vi->name, "Television");
582 vi->type = V4L2_INPUT_TYPE_TUNER;
585 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
589 vi->type = V4L2_INPUT_TYPE_CAMERA;
590 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
591 strcpy(vi->name, "Green Video Input");
594 strcpy(vi->name, "Composite Video Input");
596 vi->std = V4L2_STD_PAL;
599 vi->type = V4L2_INPUT_TYPE_CAMERA;
600 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
601 strcpy(vi->name, "Yellow Video Input");
604 strcpy(vi->name, "S-Video Input");
606 vi->std = V4L2_STD_PAL;
609 vi->type = V4L2_INPUT_TYPE_CAMERA;
610 strcpy(vi->name, "Red Video Input");
611 vi->std = V4L2_STD_PAL;
614 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
615 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
620 struct v4l2_standard *e = arg;
627 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
628 tvnorms[e->index].name);
637 *input = usbvision->ctl_input;
643 if ((*input >= usbvision->video_inputs) || (*input < 0) )
645 usbvision->ctl_input = *input;
647 down(&usbvision->lock);
648 usbvision_muxsel(usbvision, usbvision->ctl_input);
649 usbvision_set_input(usbvision);
650 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
651 up(&usbvision->lock);
656 v4l2_std_id *id = arg;
658 *id = usbvision->tvnorm->id;
660 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
665 v4l2_std_id *id = arg;
668 for (i = 0; i < TVNORMS; i++)
669 if (*id == tvnorms[i].id)
672 for (i = 0; i < TVNORMS; i++)
673 if (*id & tvnorms[i].id)
678 down(&usbvision->lock);
679 usbvision->tvnorm = &tvnorms[i];
681 call_i2c_clients(usbvision, VIDIOC_S_STD,
682 &usbvision->tvnorm->id);
684 up(&usbvision->lock);
686 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
691 struct v4l2_tuner *vt = arg;
693 if (!usbvision->have_tuner || vt->index) // Only tuner 0
695 if(usbvision->radio) {
696 strcpy(vt->name, "Radio");
697 vt->type = V4L2_TUNER_RADIO;
700 strcpy(vt->name, "Television");
702 /* Let clients fill in the remainder of this struct */
703 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
705 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER for %s signal=%x, afc=%x",
706 vt->name, vt->signal,vt->afc);
711 struct v4l2_tuner *vt = arg;
713 // Only no or one tuner for now
714 if (!usbvision->have_tuner || vt->index)
716 /* let clients handle this */
717 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
719 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
722 case VIDIOC_G_FREQUENCY:
724 struct v4l2_frequency *freq = arg;
726 freq->tuner = 0; // Only one tuner
727 if(usbvision->radio) {
728 freq->type = V4L2_TUNER_RADIO;
731 freq->type = V4L2_TUNER_ANALOG_TV;
733 freq->frequency = usbvision->freq;
734 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
737 case VIDIOC_S_FREQUENCY:
739 struct v4l2_frequency *freq = arg;
741 // Only no or one tuner for now
742 if (!usbvision->have_tuner || freq->tuner)
745 usbvision->freq = freq->frequency;
746 call_i2c_clients(usbvision, cmd, freq);
747 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
752 struct v4l2_audio *v = arg;
753 memset(v,0, sizeof(v));
754 if(usbvision->radio) {
755 strcpy(v->name,"Radio");
758 strcpy(v->name, "TV");
760 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
765 struct v4l2_audio *v = arg;
769 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
772 case VIDIOC_QUERYCTRL:
774 struct v4l2_queryctrl *ctrl = arg;
777 memset(ctrl,0,sizeof(*ctrl));
780 call_i2c_clients(usbvision, cmd, arg);
787 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
791 struct v4l2_control *ctrl = arg;
792 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
793 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
798 struct v4l2_control *ctrl = arg;
800 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
801 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
806 struct v4l2_requestbuffers *vr = arg;
809 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
811 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
812 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
813 (vr->memory != V4L2_MEMORY_MMAP))
816 if(usbvision->streaming == Stream_On) {
817 if ((ret = usbvision_stream_interrupt(usbvision)))
821 usbvision_frames_free(usbvision);
822 usbvision_empty_framequeues(usbvision);
823 vr->count = usbvision_frames_alloc(usbvision,vr->count);
825 usbvision->curFrame = NULL;
827 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
830 case VIDIOC_QUERYBUF:
832 struct v4l2_buffer *vb = arg;
833 struct usbvision_frame *frame;
835 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
837 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
840 if(vb->index>=usbvision->num_frames) {
843 // Updating the corresponding frame state
845 frame = &usbvision->frame[vb->index];
846 if(frame->grabstate >= FrameState_Ready)
847 vb->flags |= V4L2_BUF_FLAG_QUEUED;
848 if(frame->grabstate >= FrameState_Done)
849 vb->flags |= V4L2_BUF_FLAG_DONE;
850 if(frame->grabstate == FrameState_Unused)
851 vb->flags |= V4L2_BUF_FLAG_MAPPED;
852 vb->memory = V4L2_MEMORY_MMAP;
854 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
856 vb->memory = V4L2_MEMORY_MMAP;
857 vb->field = V4L2_FIELD_NONE;
858 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
859 vb->timestamp = usbvision->frame[vb->index].timestamp;
860 vb->sequence = usbvision->frame[vb->index].sequence;
865 struct v4l2_buffer *vb = arg;
866 struct usbvision_frame *frame;
867 unsigned long lock_flags;
869 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
870 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
873 if(vb->index>=usbvision->num_frames) {
877 frame = &usbvision->frame[vb->index];
879 if (frame->grabstate != FrameState_Unused) {
883 /* Mark it as ready and enqueue frame */
884 frame->grabstate = FrameState_Ready;
885 frame->scanstate = ScanState_Scanning;
886 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
888 vb->flags &= ~V4L2_BUF_FLAG_DONE;
890 /* set v4l2_format index */
891 frame->v4l2_format = usbvision->palette;
893 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
894 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
895 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
897 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
902 struct v4l2_buffer *vb = arg;
904 struct usbvision_frame *f;
905 unsigned long lock_flags;
907 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
910 if (list_empty(&(usbvision->outqueue))) {
911 if (usbvision->streaming == Stream_Idle)
913 ret = wait_event_interruptible
914 (usbvision->wait_frame,
915 !list_empty(&(usbvision->outqueue)));
920 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
921 f = list_entry(usbvision->outqueue.next,
922 struct usbvision_frame, frame);
923 list_del(usbvision->outqueue.next);
924 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
926 f->grabstate = FrameState_Unused;
928 vb->memory = V4L2_MEMORY_MMAP;
929 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
930 vb->index = f->index;
931 vb->sequence = f->sequence;
932 vb->timestamp = f->timestamp;
933 vb->field = V4L2_FIELD_NONE;
934 vb->bytesused = f->scanlength;
938 case VIDIOC_STREAMON:
940 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
942 usbvision->streaming = Stream_On;
944 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
946 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
950 case VIDIOC_STREAMOFF:
953 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
955 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
958 if(usbvision->streaming == Stream_On) {
959 usbvision_stream_interrupt(usbvision);
960 // Stop all video streamings
961 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
963 usbvision_empty_framequeues(usbvision);
965 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
968 case VIDIOC_ENUM_FMT:
970 struct v4l2_fmtdesc *vfd = arg;
972 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
976 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
977 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
978 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
979 memset(vfd->reserved, 0, sizeof(vfd->reserved));
984 struct v4l2_format *vf = arg;
987 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
989 vf->fmt.pix.width = usbvision->curwidth;
990 vf->fmt.pix.height = usbvision->curheight;
991 vf->fmt.pix.pixelformat = usbvision->palette.format;
992 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
993 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
994 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
995 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
996 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
997 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1001 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
1006 case VIDIOC_TRY_FMT:
1009 struct v4l2_format *vf = arg;
1013 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1015 /* Find requested format in available ones */
1016 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1017 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
1018 usbvision->palette = usbvision_v4l2_format[formatIdx];
1023 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1026 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1027 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1029 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
1030 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1032 if(cmd == VIDIOC_TRY_FMT) {
1033 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1034 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1038 /* stop io in case it is already in progress */
1039 if(usbvision->streaming == Stream_On) {
1040 if ((ret = usbvision_stream_interrupt(usbvision)))
1043 usbvision_frames_free(usbvision);
1044 usbvision_empty_framequeues(usbvision);
1046 usbvision->curFrame = NULL;
1048 // by now we are committed to the new data...
1049 down(&usbvision->lock);
1050 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1051 up(&usbvision->lock);
1053 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1054 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1062 return -ENOIOCTLCMD;
1067 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1068 unsigned int cmd, unsigned long arg)
1070 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1074 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1075 size_t count, loff_t *ppos)
1077 struct video_device *dev = video_devdata(file);
1078 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1079 int noblock = file->f_flags & O_NONBLOCK;
1080 unsigned long lock_flags;
1083 struct usbvision_frame *frame;
1085 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1087 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1090 /* This entry point is compatible with the mmap routines so that a user can do either
1091 VIDIOC_QBUF/VIDIOC_DQBUF to get frames or call read on the device. */
1092 if(!usbvision->num_frames) {
1093 /* First, allocate some frames to work with if this has not been done with
1095 usbvision_frames_free(usbvision);
1096 usbvision_empty_framequeues(usbvision);
1097 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1100 if(usbvision->streaming != Stream_On) {
1101 /* no stream is running, make it running ! */
1102 usbvision->streaming = Stream_On;
1103 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1106 /* Then, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1107 for(i=0;i<usbvision->num_frames;i++) {
1108 frame = &usbvision->frame[i];
1109 if(frame->grabstate == FrameState_Unused) {
1110 /* Mark it as ready and enqueue frame */
1111 frame->grabstate = FrameState_Ready;
1112 frame->scanstate = ScanState_Scanning;
1113 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1115 /* set v4l2_format index */
1116 frame->v4l2_format = usbvision->palette;
1118 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1119 list_add_tail(&frame->frame, &usbvision->inqueue);
1120 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1124 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1125 if (list_empty(&(usbvision->outqueue))) {
1129 ret = wait_event_interruptible
1130 (usbvision->wait_frame,
1131 !list_empty(&(usbvision->outqueue)));
1136 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1137 frame = list_entry(usbvision->outqueue.next,
1138 struct usbvision_frame, frame);
1139 list_del(usbvision->outqueue.next);
1140 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1142 /* An error returns an empty frame */
1143 if (frame->grabstate == FrameState_Error) {
1144 frame->bytes_read = 0;
1148 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1149 frame->index, frame->bytes_read, frame->scanlength);
1151 /* copy bytes to user space; we allow for partials reads */
1152 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1153 count = frame->scanlength - frame->bytes_read;
1155 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1159 frame->bytes_read += count;
1160 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1161 (unsigned long)count, frame->bytes_read);
1163 // For now, forget the frame if it has not been read in one shot.
1164 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1165 frame->bytes_read = 0;
1167 /* Mark it as available to be used again. */
1168 frame->grabstate = FrameState_Unused;
1174 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1176 unsigned long size = vma->vm_end - vma->vm_start,
1177 start = vma->vm_start;
1181 struct video_device *dev = video_devdata(file);
1182 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1184 PDEBUG(DBG_MMAP, "mmap");
1186 down(&usbvision->lock);
1188 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1189 up(&usbvision->lock);
1193 if (!(vma->vm_flags & VM_WRITE) ||
1194 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1195 up(&usbvision->lock);
1199 for (i = 0; i < usbvision->num_frames; i++) {
1200 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1203 if (i == usbvision->num_frames) {
1204 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1205 up(&usbvision->lock);
1209 /* VM_IO is eventually going to replace PageReserved altogether */
1210 vma->vm_flags |= VM_IO;
1211 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1213 pos = usbvision->frame[i].data;
1216 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1217 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1218 up(&usbvision->lock);
1226 up(&usbvision->lock);
1232 * Here comes the stuff for radio on usbvision based devices
1235 static int usbvision_radio_open(struct inode *inode, struct file *file)
1237 struct video_device *dev = video_devdata(file);
1238 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1241 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1243 down(&usbvision->lock);
1245 if (usbvision->user) {
1246 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1251 usbvision_reset_powerOffTimer(usbvision);
1252 if (usbvision->power == 0) {
1253 usbvision_power_on(usbvision);
1254 usbvision_i2c_register(usbvision);
1258 /* Alternate interface 1 is is the biggest frame size */
1259 errCode = usbvision_set_alternate(usbvision);
1261 usbvision->last_error = errCode;
1265 // If so far no errors then we shall start the radio
1266 usbvision->radio = 1;
1267 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1268 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1273 if (PowerOnAtOpen) {
1274 usbvision_i2c_unregister(usbvision);
1275 usbvision_power_off(usbvision);
1276 usbvision->initialized = 0;
1279 up(&usbvision->lock);
1284 static int usbvision_radio_close(struct inode *inode, struct file *file)
1286 struct video_device *dev = video_devdata(file);
1287 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1292 down(&usbvision->lock);
1294 /* Set packet size to 0 */
1295 usbvision->ifaceAlt=0;
1296 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1297 usbvision->ifaceAlt);
1299 usbvision_audio_off(usbvision);
1303 if (PowerOnAtOpen) {
1304 usbvision_set_powerOffTimer(usbvision);
1305 usbvision->initialized = 0;
1308 up(&usbvision->lock);
1310 if (usbvision->remove_pending) {
1311 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
1312 usbvision_release(usbvision);
1316 PDEBUG(DBG_IO, "success");
1321 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1322 unsigned int cmd, unsigned long arg)
1324 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1329 * Here comes the stuff for vbi on usbvision based devices
1332 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1339 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1345 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1346 unsigned int cmd, void *arg)
1352 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1353 unsigned int cmd, unsigned long arg)
1355 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1360 // Video registration stuff
1364 static const struct file_operations usbvision_fops = {
1365 .owner = THIS_MODULE,
1366 .open = usbvision_v4l2_open,
1367 .release = usbvision_v4l2_close,
1368 .read = usbvision_v4l2_read,
1369 .mmap = usbvision_v4l2_mmap,
1370 .ioctl = usbvision_v4l2_ioctl,
1371 .llseek = no_llseek,
1373 static struct video_device usbvision_video_template = {
1374 .owner = THIS_MODULE,
1375 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1376 .hardware = VID_HARDWARE_USBVISION,
1377 .fops = &usbvision_fops,
1378 .name = "usbvision-video",
1379 .release = video_device_release,
1385 static const struct file_operations usbvision_radio_fops = {
1386 .owner = THIS_MODULE,
1387 .open = usbvision_radio_open,
1388 .release = usbvision_radio_close,
1389 .ioctl = usbvision_radio_ioctl,
1390 .llseek = no_llseek,
1393 static struct video_device usbvision_radio_template=
1395 .owner = THIS_MODULE,
1396 .type = VID_TYPE_TUNER,
1397 .hardware = VID_HARDWARE_USBVISION,
1398 .fops = &usbvision_radio_fops,
1399 .release = video_device_release,
1400 .name = "usbvision-radio",
1406 static const struct file_operations usbvision_vbi_fops = {
1407 .owner = THIS_MODULE,
1408 .open = usbvision_vbi_open,
1409 .release = usbvision_vbi_close,
1410 .ioctl = usbvision_vbi_ioctl,
1411 .llseek = no_llseek,
1414 static struct video_device usbvision_vbi_template=
1416 .owner = THIS_MODULE,
1417 .type = VID_TYPE_TUNER,
1418 .hardware = VID_HARDWARE_USBVISION,
1419 .fops = &usbvision_vbi_fops,
1420 .release = video_device_release,
1421 .name = "usbvision-vbi",
1426 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1427 struct video_device *vdev_template,
1430 struct usb_device *usb_dev = usbvision->dev;
1431 struct video_device *vdev;
1433 if (usb_dev == NULL) {
1434 err("%s: usbvision->dev is not set", __FUNCTION__);
1438 vdev = video_device_alloc();
1442 *vdev = *vdev_template;
1443 // vdev->minor = -1;
1444 vdev->dev = &usb_dev->dev;
1445 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1446 video_set_drvdata(vdev, usbvision);
1450 // unregister video4linux devices
1451 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1454 if (usbvision->vbi) {
1455 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1456 if (usbvision->vbi->minor != -1) {
1457 video_unregister_device(usbvision->vbi);
1460 video_device_release(usbvision->vbi);
1462 usbvision->vbi = NULL;
1466 if (usbvision->rdev) {
1467 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1468 if (usbvision->rdev->minor != -1) {
1469 video_unregister_device(usbvision->rdev);
1472 video_device_release(usbvision->rdev);
1474 usbvision->rdev = NULL;
1478 if (usbvision->vdev) {
1479 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1480 if (usbvision->vdev->minor != -1) {
1481 video_unregister_device(usbvision->vdev);
1484 video_device_release(usbvision->vdev);
1486 usbvision->vdev = NULL;
1490 // register video4linux devices
1491 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1494 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1495 if (usbvision->vdev == NULL) {
1498 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1501 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision->nr,usbvision->vdev->minor & 0x1f);
1504 if (usbvision_device_data[usbvision->DevModel].Radio) {
1505 // usbvision has radio
1506 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1507 if (usbvision->rdev == NULL) {
1510 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1513 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision->nr, usbvision->rdev->minor & 0x1f);
1516 if (usbvision_device_data[usbvision->DevModel].vbi) {
1517 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1518 if (usbvision->vdev == NULL) {
1521 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1524 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision->nr,usbvision->vbi->minor & 0x1f);
1530 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1531 usbvision_unregister_video(usbvision);
1538 * This code allocates the struct usb_usbvision. It is filled with default values.
1540 * Returns NULL on error, a pointer to usb_usbvision else.
1543 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1545 struct usb_usbvision *usbvision;
1547 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1551 usbvision->dev = dev;
1553 init_MUTEX(&usbvision->lock); /* to 1 == available */
1555 // prepare control urb for control messages during interrupts
1556 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1557 if (usbvision->ctrlUrb == NULL) {
1560 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1561 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1563 usbvision_init_powerOffTimer(usbvision);
1568 if (usbvision && usbvision->ctrlUrb) {
1569 usb_free_urb(usbvision->ctrlUrb);
1578 * usbvision_release()
1580 * This code does final release of struct usb_usbvision. This happens
1581 * after the device is disconnected -and- all clients closed their files.
1584 static void usbvision_release(struct usb_usbvision *usbvision)
1586 PDEBUG(DBG_PROBE, "");
1588 down(&usbvision->lock);
1590 usbvision_reset_powerOffTimer(usbvision);
1592 usbvision->initialized = 0;
1594 up(&usbvision->lock);
1596 usbvision_remove_sysfs(usbvision->vdev);
1597 usbvision_unregister_video(usbvision);
1599 if (usbvision->ctrlUrb) {
1600 usb_free_urb(usbvision->ctrlUrb);
1605 PDEBUG(DBG_PROBE, "success");
1609 /******************************** usb interface *****************************************/
1611 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1615 if (usbvision == NULL)
1618 model = usbvision->DevModel;
1619 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1621 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
1622 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2;
1624 usbvision->Vin_Reg2_Preset = 0;
1627 for (i = 0; i < TVNORMS; i++)
1628 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1632 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1634 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1635 usbvision->ctl_input = 0;
1637 /* This should be here to make i2c clients to be able to register */
1638 usbvision_audio_off(usbvision); //first switch off audio
1639 if (!PowerOnAtOpen) {
1640 usbvision_power_on(usbvision); //and then power up the noisy tuner
1641 usbvision_i2c_register(usbvision);
1648 * This procedure queries device descriptor and accepts the interface
1649 * if it looks like USBVISION video device
1652 static int __devinit usbvision_probe(struct usb_interface *intf,
1653 const struct usb_device_id *devid)
1655 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1656 struct usb_interface *uif;
1657 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1658 const struct usb_host_interface *interface;
1659 struct usb_usbvision *usbvision = NULL;
1660 const struct usb_endpoint_descriptor *endpoint;
1663 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1664 dev->descriptor.idVendor,
1665 dev->descriptor.idProduct, ifnum);
1667 model = devid->driver_info;
1668 if ( (model<0) || (model>=usbvision_device_data_size) ) {
1669 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
1672 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1673 usbvision_device_data[model].ModelString);
1675 if (usbvision_device_data[model].Interface >= 0) {
1676 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1679 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1681 endpoint = &interface->endpoint[1].desc;
1682 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1683 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1684 err("%s: Endpoint attributes %d", __FUNCTION__, endpoint->bmAttributes);
1687 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1688 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1692 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1693 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1697 if (dev->descriptor.bNumConfigurations > 1) {
1698 usbvision->bridgeType = BRIDGE_NT1004;
1700 else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
1701 usbvision->bridgeType = BRIDGE_NT1005;
1704 usbvision->bridgeType = BRIDGE_NT1003;
1706 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1708 down(&usbvision->lock);
1710 /* compute alternate max packet sizes */
1711 uif = dev->actconfig->interface[0];
1713 usbvision->num_alt=uif->num_altsetting;
1714 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1715 usbvision->alt_max_pkt_size = kmalloc(32*
1716 usbvision->num_alt,GFP_KERNEL);
1717 if (usbvision->alt_max_pkt_size == NULL) {
1718 err("usbvision: out of memory!\n");
1722 for (i = 0; i < usbvision->num_alt ; i++) {
1723 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1725 usbvision->alt_max_pkt_size[i] =
1726 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1727 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1728 usbvision->alt_max_pkt_size[i]);
1732 usbvision->nr = usbvision_nr++;
1734 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1735 if (usbvision->have_tuner) {
1736 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1739 usbvision->tuner_addr = ADDR_UNSET;
1741 usbvision->DevModel = model;
1742 usbvision->remove_pending = 0;
1743 usbvision->iface = ifnum;
1744 usbvision->ifaceAlt = 0;
1745 usbvision->video_endp = endpoint->bEndpointAddress;
1746 usbvision->isocPacketSize = 0;
1747 usbvision->usb_bandwidth = 0;
1748 usbvision->user = 0;
1749 usbvision->streaming = Stream_Off;
1750 usbvision_register_video(usbvision);
1751 usbvision_configure_video(usbvision);
1752 up(&usbvision->lock);
1755 usb_set_intfdata (intf, usbvision);
1756 usbvision_create_sysfs(usbvision->vdev);
1758 PDEBUG(DBG_PROBE, "success");
1764 * usbvision_disconnect()
1766 * This procedure stops all driver activity, deallocates interface-private
1767 * structure (pointed by 'ptr') and after that driver should be removable
1768 * with no ill consequences.
1771 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1773 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1775 PDEBUG(DBG_PROBE, "");
1777 if (usbvision == NULL) {
1778 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1781 usb_set_intfdata (intf, NULL);
1783 down(&usbvision->lock);
1785 // At this time we ask to cancel outstanding URBs
1786 usbvision_stop_isoc(usbvision);
1788 if (usbvision->power) {
1789 usbvision_i2c_unregister(usbvision);
1790 usbvision_power_off(usbvision);
1792 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1794 usb_put_dev(usbvision->dev);
1795 usbvision->dev = NULL; // USB device is no more
1797 up(&usbvision->lock);
1799 if (usbvision->user) {
1800 printk(KERN_INFO "%s: In use, disconnect pending\n", __FUNCTION__);
1801 wake_up_interruptible(&usbvision->wait_frame);
1802 wake_up_interruptible(&usbvision->wait_stream);
1805 usbvision_release(usbvision);
1808 PDEBUG(DBG_PROBE, "success");
1812 static struct usb_driver usbvision_driver = {
1813 .name = "usbvision",
1814 .id_table = usbvision_table,
1815 .probe = usbvision_probe,
1816 .disconnect = usbvision_disconnect
1822 * This code is run to initialize the driver.
1825 static int __init usbvision_init(void)
1829 PDEBUG(DBG_PROBE, "");
1831 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
1832 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1833 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1834 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1836 /* disable planar mode support unless compression enabled */
1837 if (isocMode != ISOC_MODE_COMPRESS ) {
1838 // FIXME : not the right way to set supported flag
1839 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1840 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1843 errCode = usb_register(&usbvision_driver);
1846 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1847 PDEBUG(DBG_PROBE, "success");
1852 static void __exit usbvision_exit(void)
1854 PDEBUG(DBG_PROBE, "");
1856 usb_deregister(&usbvision_driver);
1857 PDEBUG(DBG_PROBE, "success");
1860 module_init(usbvision_init);
1861 module_exit(usbvision_exit);
1864 * Overrides for Emacs so that we follow Linus's tabbing style.
1865 * ---------------------------------------------------------------------------