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.
40 * Seems impossible, needs a codec interface. Which one?
41 * - Clean up the driver.
42 * - optimization for performance.
43 * - Add Videotext capability (VBI). Working on it.....
44 * - Check audio for other devices
48 #include <linux/version.h>
49 #include <linux/kernel.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/videodev.h>
57 #include <linux/vmalloc.h>
58 #include <linux/module.h>
59 #include <linux/init.h>
60 #include <linux/spinlock.h>
62 #include <linux/videodev2.h>
63 #include <linux/video_decoder.h>
64 #include <linux/i2c.h>
66 #include <media/saa7115.h>
67 #include <media/v4l2-common.h>
68 #include <media/tuner.h>
69 #include <media/audiochip.h>
71 #include <linux/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>,\
81 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,\
90 USBVISION_DRIVER_VERSION_MINOR,\
91 USBVISION_DRIVER_VERSION_PATCHLEVEL)
92 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
93 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
94 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
96 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
99 #ifdef USBVISION_DEBUG
100 #define PDEBUG(level, fmt, args...) \
101 if (video_debug & (level)) \
102 info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ ,\
105 #define PDEBUG(level, fmt, args...) do {} while(0)
109 #define DBG_PROBE 1<<2
110 #define DBG_MMAP 1<<3
113 #define rmspace(str) while(*str==' ') str++;
114 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
117 /* sequential number of usbvision device */
118 static int usbvision_nr = 0;
120 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
121 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
122 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
123 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
124 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
125 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
126 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
127 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
128 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
131 /* Function prototypes */
132 static void usbvision_release(struct usb_usbvision *usbvision);
134 /* Default initalization of device driver parameters */
135 /* Set the default format for ISOC endpoint */
136 static int isocMode = ISOC_MODE_COMPRESS;
137 /* Set the default Debug Mode of the device driver */
138 static int video_debug = 0;
139 /* Set the default device to power on at startup */
140 static int PowerOnAtOpen = 1;
141 /* Sequential Number of Video Device */
142 static int video_nr = -1;
143 /* Sequential Number of Radio Device */
144 static int radio_nr = -1;
145 /* Sequential Number of VBI Device */
146 static int vbi_nr = -1;
148 /* Grab parameters for the device driver */
150 /* Showing parameters under SYSFS */
151 module_param(isocMode, int, 0444);
152 module_param(video_debug, int, 0444);
153 module_param(PowerOnAtOpen, int, 0444);
154 module_param(video_nr, int, 0444);
155 module_param(radio_nr, int, 0444);
156 module_param(vbi_nr, int, 0444);
158 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
159 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
160 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
161 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
162 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
163 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
167 MODULE_AUTHOR(DRIVER_AUTHOR);
168 MODULE_DESCRIPTION(DRIVER_DESC);
169 MODULE_LICENSE(DRIVER_LICENSE);
170 MODULE_VERSION(USBVISION_VERSION_STRING);
171 MODULE_ALIAS(DRIVER_ALIAS);
174 /*****************************************************************************/
175 /* SYSFS Code - Copied from the stv680.c usb module. */
176 /* Device information is located at /sys/class/video4linux/video0 */
177 /* Device parameters information is located at /sys/module/usbvision */
178 /* Device USB Information is located at */
179 /* /sys/bus/usb/drivers/USBVision Video Grabber */
180 /*****************************************************************************/
183 #define YES_NO(x) ((x) ? "Yes" : "No")
185 static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
187 struct video_device *vdev =
188 container_of(cd, struct video_device, class_dev);
189 return video_get_drvdata(vdev);
192 static ssize_t show_version(struct device *cd,
193 struct device_attribute *attr, char *buf)
195 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
197 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
199 static ssize_t show_model(struct device *cd,
200 struct device_attribute *attr, char *buf)
202 struct video_device *vdev =
203 container_of(cd, struct video_device, class_dev);
204 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
205 return sprintf(buf, "%s\n",
206 usbvision_device_data[usbvision->DevModel].ModelString);
208 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
210 static ssize_t show_hue(struct device *cd,
211 struct device_attribute *attr, char *buf)
213 struct video_device *vdev =
214 container_of(cd, struct video_device, class_dev);
215 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
216 struct v4l2_control ctrl;
217 ctrl.id = V4L2_CID_HUE;
220 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
221 return sprintf(buf, "%d\n", ctrl.value);
223 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
225 static ssize_t show_contrast(struct device *cd,
226 struct device_attribute *attr, char *buf)
228 struct video_device *vdev =
229 container_of(cd, struct video_device, class_dev);
230 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
231 struct v4l2_control ctrl;
232 ctrl.id = V4L2_CID_CONTRAST;
235 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
236 return sprintf(buf, "%d\n", ctrl.value);
238 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
240 static ssize_t show_brightness(struct device *cd,
241 struct device_attribute *attr, char *buf)
243 struct video_device *vdev =
244 container_of(cd, struct video_device, class_dev);
245 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
246 struct v4l2_control ctrl;
247 ctrl.id = V4L2_CID_BRIGHTNESS;
250 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
251 return sprintf(buf, "%d\n", ctrl.value);
253 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
255 static ssize_t show_saturation(struct device *cd,
256 struct device_attribute *attr, char *buf)
258 struct video_device *vdev =
259 container_of(cd, struct video_device, class_dev);
260 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
261 struct v4l2_control ctrl;
262 ctrl.id = V4L2_CID_SATURATION;
265 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
266 return sprintf(buf, "%d\n", ctrl.value);
268 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
270 static ssize_t show_streaming(struct device *cd,
271 struct device_attribute *attr, char *buf)
273 struct video_device *vdev =
274 container_of(cd, struct video_device, class_dev);
275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
276 return sprintf(buf, "%s\n",
277 YES_NO(usbvision->streaming==Stream_On?1:0));
279 static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
281 static ssize_t show_compression(struct device *cd,
282 struct device_attribute *attr, char *buf)
284 struct video_device *vdev =
285 container_of(cd, struct video_device, class_dev);
286 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
287 return sprintf(buf, "%s\n",
288 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
290 static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
292 static ssize_t show_device_bridge(struct device *cd,
293 struct device_attribute *attr, char *buf)
295 struct video_device *vdev =
296 container_of(cd, struct video_device, class_dev);
297 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
298 return sprintf(buf, "%d\n", usbvision->bridgeType);
300 static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
302 static void usbvision_create_sysfs(struct video_device *vdev)
308 res = device_create_file(&vdev->class_dev,
312 res = device_create_file(&vdev->class_dev,
316 res = device_create_file(&vdev->class_dev,
320 res = device_create_file(&vdev->class_dev,
324 res = device_create_file(&vdev->class_dev,
325 &dev_attr_brightness);
328 res = device_create_file(&vdev->class_dev,
329 &dev_attr_saturation);
332 res = device_create_file(&vdev->class_dev,
333 &dev_attr_streaming);
336 res = device_create_file(&vdev->class_dev,
337 &dev_attr_compression);
340 res = device_create_file(&vdev->class_dev,
346 err("%s error: %d\n", __FUNCTION__, res);
349 static void usbvision_remove_sysfs(struct video_device *vdev)
352 device_remove_file(&vdev->class_dev,
354 device_remove_file(&vdev->class_dev,
356 device_remove_file(&vdev->class_dev,
358 device_remove_file(&vdev->class_dev,
360 device_remove_file(&vdev->class_dev,
361 &dev_attr_brightness);
362 device_remove_file(&vdev->class_dev,
363 &dev_attr_saturation);
364 device_remove_file(&vdev->class_dev,
365 &dev_attr_streaming);
366 device_remove_file(&vdev->class_dev,
367 &dev_attr_compression);
368 device_remove_file(&vdev->class_dev,
377 * This is part of Video 4 Linux API. The driver can be opened by one
378 * client only (checks internal counter 'usbvision->user'). The procedure
379 * then allocates buffers needed for video processing.
382 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
384 struct video_device *dev = video_devdata(file);
385 struct usb_usbvision *usbvision =
386 (struct usb_usbvision *) video_get_drvdata(dev);
389 PDEBUG(DBG_IO, "open");
392 usbvision_reset_powerOffTimer(usbvision);
397 /* Allocate memory for the scratch ring buffer */
398 errCode = usbvision_scratch_alloc(usbvision);
399 if (isocMode==ISOC_MODE_COMPRESS) {
400 /* Allocate intermediate decompression buffers
402 errCode = usbvision_decompress_alloc(usbvision);
405 /* Deallocate all buffers if trouble */
406 usbvision_scratch_free(usbvision);
407 usbvision_decompress_free(usbvision);
411 /* If so far no errors then we shall start the camera */
413 mutex_lock(&usbvision->lock);
414 if (usbvision->power == 0) {
415 usbvision_power_on(usbvision);
416 usbvision_i2c_register(usbvision);
419 /* Send init sequence only once, it's large! */
420 if (!usbvision->initialized) {
422 setup_ok = usbvision_setup(usbvision,isocMode);
424 usbvision->initialized = 1;
430 usbvision_begin_streaming(usbvision);
431 errCode = usbvision_init_isoc(usbvision);
432 /* device must be initialized before isoc transfer */
433 usbvision_muxsel(usbvision,0);
437 usbvision_i2c_unregister(usbvision);
438 usbvision_power_off(usbvision);
439 usbvision->initialized = 0;
442 mutex_unlock(&usbvision->lock);
449 usbvision_empty_framequeues(usbvision);
451 PDEBUG(DBG_IO, "success");
456 * usbvision_v4l2_close()
458 * This is part of Video 4 Linux API. The procedure
459 * stops streaming and deallocates all buffers that were earlier
460 * allocated in usbvision_v4l2_open().
463 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
465 struct video_device *dev = video_devdata(file);
466 struct usb_usbvision *usbvision =
467 (struct usb_usbvision *) video_get_drvdata(dev);
469 PDEBUG(DBG_IO, "close");
470 mutex_lock(&usbvision->lock);
472 usbvision_audio_off(usbvision);
473 usbvision_restart_isoc(usbvision);
474 usbvision_stop_isoc(usbvision);
476 usbvision_decompress_free(usbvision);
477 usbvision_frames_free(usbvision);
478 usbvision_empty_framequeues(usbvision);
479 usbvision_scratch_free(usbvision);
484 /* power off in a little while
485 to avoid off/on every close/open short sequences */
486 usbvision_set_powerOffTimer(usbvision);
487 usbvision->initialized = 0;
490 mutex_unlock(&usbvision->lock);
492 if (usbvision->remove_pending) {
493 printk(KERN_INFO "%s: Final disconnect\n", __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 #ifdef CONFIG_VIDEO_ADV_DEBUG
511 static int vidioc_g_register (struct file *file, void *priv,
512 struct v4l2_register *reg)
514 struct video_device *dev = video_devdata(file);
515 struct usb_usbvision *usbvision =
516 (struct usb_usbvision *) video_get_drvdata(dev);
519 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
521 /* NT100x has a 8-bit register space */
522 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
524 err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
525 __FUNCTION__, errCode);
532 static int vidioc_s_register (struct file *file, void *priv,
533 struct v4l2_register *reg)
535 struct video_device *dev = video_devdata(file);
536 struct usb_usbvision *usbvision =
537 (struct usb_usbvision *) video_get_drvdata(dev);
540 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
542 /* NT100x has a 8-bit register space */
543 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
545 err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
546 __FUNCTION__, errCode);
553 static int vidioc_querycap (struct file *file, void *priv,
554 struct v4l2_capability *vc)
556 struct video_device *dev = video_devdata(file);
557 struct usb_usbvision *usbvision =
558 (struct usb_usbvision *) video_get_drvdata(dev);
560 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
562 usbvision_device_data[usbvision->DevModel].ModelString,
564 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
565 sizeof(vc->bus_info));
566 vc->version = USBVISION_DRIVER_VERSION;
567 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
571 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
575 static int vidioc_enum_input (struct file *file, void *priv,
576 struct v4l2_input *vi)
578 struct video_device *dev = video_devdata(file);
579 struct usb_usbvision *usbvision =
580 (struct usb_usbvision *) video_get_drvdata(dev);
583 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
585 if (usbvision->have_tuner) {
588 chan = vi->index + 1; /*skip Television string*/
590 /* Determine the requested input characteristics
591 specific for each usbvision card model */
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "White Video Input");
597 strcpy(vi->name, "Television");
598 vi->type = V4L2_INPUT_TYPE_TUNER;
601 vi->std = USBVISION_NORMS;
605 vi->type = V4L2_INPUT_TYPE_CAMERA;
606 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
607 strcpy(vi->name, "Green Video Input");
609 strcpy(vi->name, "Composite Video Input");
611 vi->std = V4L2_STD_PAL;
614 vi->type = V4L2_INPUT_TYPE_CAMERA;
615 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
616 strcpy(vi->name, "Yellow Video Input");
618 strcpy(vi->name, "S-Video Input");
620 vi->std = V4L2_STD_PAL;
623 vi->type = V4L2_INPUT_TYPE_CAMERA;
624 strcpy(vi->name, "Red Video Input");
625 vi->std = V4L2_STD_PAL;
631 static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
633 struct video_device *dev = video_devdata(file);
634 struct usb_usbvision *usbvision =
635 (struct usb_usbvision *) video_get_drvdata(dev);
637 *input = usbvision->ctl_input;
641 static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
643 struct video_device *dev = video_devdata(file);
644 struct usb_usbvision *usbvision =
645 (struct usb_usbvision *) video_get_drvdata(dev);
647 if ((input >= usbvision->video_inputs) || (input < 0) )
650 mutex_lock(&usbvision->lock);
651 usbvision_muxsel(usbvision, input);
652 usbvision_set_input(usbvision);
653 usbvision_set_output(usbvision,
655 usbvision->curheight);
656 mutex_unlock(&usbvision->lock);
660 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
662 struct video_device *dev = video_devdata(file);
663 struct usb_usbvision *usbvision =
664 (struct usb_usbvision *) video_get_drvdata(dev);
665 usbvision->tvnormId=*id;
667 mutex_lock(&usbvision->lock);
668 call_i2c_clients(usbvision, VIDIOC_S_STD,
669 &usbvision->tvnormId);
670 mutex_unlock(&usbvision->lock);
671 /* propagate the change to the decoder */
672 usbvision_muxsel(usbvision, usbvision->ctl_input);
677 static int vidioc_g_tuner (struct file *file, void *priv,
678 struct v4l2_tuner *vt)
680 struct video_device *dev = video_devdata(file);
681 struct usb_usbvision *usbvision =
682 (struct usb_usbvision *) video_get_drvdata(dev);
684 if (!usbvision->have_tuner || vt->index) // Only tuner 0
686 if(usbvision->radio) {
687 strcpy(vt->name, "Radio");
688 vt->type = V4L2_TUNER_RADIO;
690 strcpy(vt->name, "Television");
692 /* Let clients fill in the remainder of this struct */
693 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
698 static int vidioc_s_tuner (struct file *file, void *priv,
699 struct v4l2_tuner *vt)
701 struct video_device *dev = video_devdata(file);
702 struct usb_usbvision *usbvision =
703 (struct usb_usbvision *) video_get_drvdata(dev);
705 // Only no or one tuner for now
706 if (!usbvision->have_tuner || vt->index)
708 /* let clients handle this */
709 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
714 static int vidioc_g_frequency (struct file *file, void *priv,
715 struct v4l2_frequency *freq)
717 struct video_device *dev = video_devdata(file);
718 struct usb_usbvision *usbvision =
719 (struct usb_usbvision *) video_get_drvdata(dev);
721 freq->tuner = 0; // Only one tuner
722 if(usbvision->radio) {
723 freq->type = V4L2_TUNER_RADIO;
725 freq->type = V4L2_TUNER_ANALOG_TV;
727 freq->frequency = usbvision->freq;
732 static int vidioc_s_frequency (struct file *file, void *priv,
733 struct v4l2_frequency *freq)
735 struct video_device *dev = video_devdata(file);
736 struct usb_usbvision *usbvision =
737 (struct usb_usbvision *) video_get_drvdata(dev);
739 // Only no or one tuner for now
740 if (!usbvision->have_tuner || freq->tuner)
743 usbvision->freq = freq->frequency;
744 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
749 static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
751 struct video_device *dev = video_devdata(file);
752 struct usb_usbvision *usbvision =
753 (struct usb_usbvision *) video_get_drvdata(dev);
755 memset(a,0,sizeof(*a));
756 if(usbvision->radio) {
757 strcpy(a->name,"Radio");
759 strcpy(a->name, "TV");
765 static int vidioc_s_audio (struct file *file, void *fh,
766 struct v4l2_audio *a)
775 static int vidioc_queryctrl (struct file *file, void *priv,
776 struct v4l2_queryctrl *ctrl)
778 struct video_device *dev = video_devdata(file);
779 struct usb_usbvision *usbvision =
780 (struct usb_usbvision *) video_get_drvdata(dev);
783 memset(ctrl,0,sizeof(*ctrl));
786 call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
794 static int vidioc_g_ctrl (struct file *file, void *priv,
795 struct v4l2_control *ctrl)
797 struct video_device *dev = video_devdata(file);
798 struct usb_usbvision *usbvision =
799 (struct usb_usbvision *) video_get_drvdata(dev);
800 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
805 static int vidioc_s_ctrl (struct file *file, void *priv,
806 struct v4l2_control *ctrl)
808 struct video_device *dev = video_devdata(file);
809 struct usb_usbvision *usbvision =
810 (struct usb_usbvision *) video_get_drvdata(dev);
811 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
816 static int vidioc_reqbufs (struct file *file,
817 void *priv, struct v4l2_requestbuffers *vr)
819 struct video_device *dev = video_devdata(file);
820 struct usb_usbvision *usbvision =
821 (struct usb_usbvision *) video_get_drvdata(dev);
824 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
826 /* Check input validity:
827 the user must do a VIDEO CAPTURE and MMAP method. */
828 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
829 (vr->memory != V4L2_MEMORY_MMAP))
832 if(usbvision->streaming == Stream_On) {
833 if ((ret = usbvision_stream_interrupt(usbvision)))
837 usbvision_frames_free(usbvision);
838 usbvision_empty_framequeues(usbvision);
839 vr->count = usbvision_frames_alloc(usbvision,vr->count);
841 usbvision->curFrame = NULL;
846 static int vidioc_querybuf (struct file *file,
847 void *priv, struct v4l2_buffer *vb)
849 struct video_device *dev = video_devdata(file);
850 struct usb_usbvision *usbvision =
851 (struct usb_usbvision *) video_get_drvdata(dev);
852 struct usbvision_frame *frame;
854 /* FIXME : must control
855 that buffers are mapped (VIDIOC_REQBUFS has been called) */
856 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
859 if(vb->index>=usbvision->num_frames) {
862 /* Updating the corresponding frame state */
864 frame = &usbvision->frame[vb->index];
865 if(frame->grabstate >= FrameState_Ready)
866 vb->flags |= V4L2_BUF_FLAG_QUEUED;
867 if(frame->grabstate >= FrameState_Done)
868 vb->flags |= V4L2_BUF_FLAG_DONE;
869 if(frame->grabstate == FrameState_Unused)
870 vb->flags |= V4L2_BUF_FLAG_MAPPED;
871 vb->memory = V4L2_MEMORY_MMAP;
873 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
875 vb->memory = V4L2_MEMORY_MMAP;
876 vb->field = V4L2_FIELD_NONE;
877 vb->length = usbvision->curwidth*
878 usbvision->curheight*
879 usbvision->palette.bytes_per_pixel;
880 vb->timestamp = usbvision->frame[vb->index].timestamp;
881 vb->sequence = usbvision->frame[vb->index].sequence;
885 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
887 struct video_device *dev = video_devdata(file);
888 struct usb_usbvision *usbvision =
889 (struct usb_usbvision *) video_get_drvdata(dev);
890 struct usbvision_frame *frame;
891 unsigned long lock_flags;
893 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
894 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
897 if(vb->index>=usbvision->num_frames) {
901 frame = &usbvision->frame[vb->index];
903 if (frame->grabstate != FrameState_Unused) {
907 /* Mark it as ready and enqueue frame */
908 frame->grabstate = FrameState_Ready;
909 frame->scanstate = ScanState_Scanning;
910 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
912 vb->flags &= ~V4L2_BUF_FLAG_DONE;
914 /* set v4l2_format index */
915 frame->v4l2_format = usbvision->palette;
917 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
918 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
919 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
924 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
926 struct video_device *dev = video_devdata(file);
927 struct usb_usbvision *usbvision =
928 (struct usb_usbvision *) video_get_drvdata(dev);
930 struct usbvision_frame *f;
931 unsigned long lock_flags;
933 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
936 if (list_empty(&(usbvision->outqueue))) {
937 if (usbvision->streaming == Stream_Idle)
939 ret = wait_event_interruptible
940 (usbvision->wait_frame,
941 !list_empty(&(usbvision->outqueue)));
946 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
947 f = list_entry(usbvision->outqueue.next,
948 struct usbvision_frame, frame);
949 list_del(usbvision->outqueue.next);
950 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
952 f->grabstate = FrameState_Unused;
954 vb->memory = V4L2_MEMORY_MMAP;
955 vb->flags = V4L2_BUF_FLAG_MAPPED |
956 V4L2_BUF_FLAG_QUEUED |
958 vb->index = f->index;
959 vb->sequence = f->sequence;
960 vb->timestamp = f->timestamp;
961 vb->field = V4L2_FIELD_NONE;
962 vb->bytesused = f->scanlength;
967 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
969 struct video_device *dev = video_devdata(file);
970 struct usb_usbvision *usbvision =
971 (struct usb_usbvision *) video_get_drvdata(dev);
972 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
974 usbvision->streaming = Stream_On;
975 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
980 static int vidioc_streamoff(struct file *file,
981 void *priv, enum v4l2_buf_type type)
983 struct video_device *dev = video_devdata(file);
984 struct usb_usbvision *usbvision =
985 (struct usb_usbvision *) video_get_drvdata(dev);
986 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
988 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
991 if(usbvision->streaming == Stream_On) {
992 usbvision_stream_interrupt(usbvision);
993 /* Stop all video streamings */
994 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
996 usbvision_empty_framequeues(usbvision);
1001 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1002 struct v4l2_fmtdesc *vfd)
1004 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
1008 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
1010 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
1011 memset(vfd->reserved, 0, sizeof(vfd->reserved));
1015 static int vidioc_g_fmt_cap (struct file *file, void *priv,
1016 struct v4l2_format *vf)
1018 struct video_device *dev = video_devdata(file);
1019 struct usb_usbvision *usbvision =
1020 (struct usb_usbvision *) video_get_drvdata(dev);
1021 vf->fmt.pix.width = usbvision->curwidth;
1022 vf->fmt.pix.height = usbvision->curheight;
1023 vf->fmt.pix.pixelformat = usbvision->palette.format;
1024 vf->fmt.pix.bytesperline =
1025 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
1026 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
1027 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1028 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
1033 static int vidioc_try_fmt_cap (struct file *file, void *priv,
1034 struct v4l2_format *vf)
1036 struct video_device *dev = video_devdata(file);
1037 struct usb_usbvision *usbvision =
1038 (struct usb_usbvision *) video_get_drvdata(dev);
1041 /* Find requested format in available ones */
1042 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1043 if(vf->fmt.pix.pixelformat ==
1044 usbvision_v4l2_format[formatIdx].format) {
1045 usbvision->palette = usbvision_v4l2_format[formatIdx];
1050 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1053 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1054 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1056 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
1057 usbvision->palette.bytes_per_pixel;
1058 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1063 static int vidioc_s_fmt_cap(struct file *file, void *priv,
1064 struct v4l2_format *vf)
1066 struct video_device *dev = video_devdata(file);
1067 struct usb_usbvision *usbvision =
1068 (struct usb_usbvision *) video_get_drvdata(dev);
1071 if( 0 != (ret=vidioc_try_fmt_cap (file, priv, vf)) ) {
1075 /* stop io in case it is already in progress */
1076 if(usbvision->streaming == Stream_On) {
1077 if ((ret = usbvision_stream_interrupt(usbvision)))
1080 usbvision_frames_free(usbvision);
1081 usbvision_empty_framequeues(usbvision);
1083 usbvision->curFrame = NULL;
1085 /* by now we are committed to the new data... */
1086 mutex_lock(&usbvision->lock);
1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1088 mutex_unlock(&usbvision->lock);
1093 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1094 size_t count, loff_t *ppos)
1096 struct video_device *dev = video_devdata(file);
1097 struct usb_usbvision *usbvision =
1098 (struct usb_usbvision *) video_get_drvdata(dev);
1099 int noblock = file->f_flags & O_NONBLOCK;
1100 unsigned long lock_flags;
1103 struct usbvision_frame *frame;
1105 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__,
1106 (unsigned long)count, noblock);
1108 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1111 /* This entry point is compatible with the mmap routines
1112 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1113 to get frames or call read on the device. */
1114 if(!usbvision->num_frames) {
1115 /* First, allocate some frames to work with
1116 if this has not been done with VIDIOC_REQBUF */
1117 usbvision_frames_free(usbvision);
1118 usbvision_empty_framequeues(usbvision);
1119 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1122 if(usbvision->streaming != Stream_On) {
1123 /* no stream is running, make it running ! */
1124 usbvision->streaming = Stream_On;
1125 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1128 /* Then, enqueue as many frames as possible
1129 (like a user of VIDIOC_QBUF would do) */
1130 for(i=0;i<usbvision->num_frames;i++) {
1131 frame = &usbvision->frame[i];
1132 if(frame->grabstate == FrameState_Unused) {
1133 /* Mark it as ready and enqueue frame */
1134 frame->grabstate = FrameState_Ready;
1135 frame->scanstate = ScanState_Scanning;
1136 /* Accumulated in usbvision_parse_data() */
1137 frame->scanlength = 0;
1139 /* set v4l2_format index */
1140 frame->v4l2_format = usbvision->palette;
1142 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1143 list_add_tail(&frame->frame, &usbvision->inqueue);
1144 spin_unlock_irqrestore(&usbvision->queue_lock,
1149 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1150 if (list_empty(&(usbvision->outqueue))) {
1154 ret = wait_event_interruptible
1155 (usbvision->wait_frame,
1156 !list_empty(&(usbvision->outqueue)));
1161 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1162 frame = list_entry(usbvision->outqueue.next,
1163 struct usbvision_frame, frame);
1164 list_del(usbvision->outqueue.next);
1165 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1167 /* An error returns an empty frame */
1168 if (frame->grabstate == FrameState_Error) {
1169 frame->bytes_read = 0;
1173 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1175 frame->index, frame->bytes_read, frame->scanlength);
1177 /* copy bytes to user space; we allow for partials reads */
1178 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1179 count = frame->scanlength - frame->bytes_read;
1181 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1185 frame->bytes_read += count;
1186 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1188 (unsigned long)count, frame->bytes_read);
1190 /* For now, forget the frame if it has not been read in one shot. */
1191 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1192 frame->bytes_read = 0;
1194 /* Mark it as available to be used again. */
1195 frame->grabstate = FrameState_Unused;
1201 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1203 unsigned long size = vma->vm_end - vma->vm_start,
1204 start = vma->vm_start;
1208 struct video_device *dev = video_devdata(file);
1209 struct usb_usbvision *usbvision =
1210 (struct usb_usbvision *) video_get_drvdata(dev);
1212 PDEBUG(DBG_MMAP, "mmap");
1214 mutex_lock(&usbvision->lock);
1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1217 mutex_unlock(&usbvision->lock);
1221 if (!(vma->vm_flags & VM_WRITE) ||
1222 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1223 mutex_unlock(&usbvision->lock);
1227 for (i = 0; i < usbvision->num_frames; i++) {
1228 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1232 if (i == usbvision->num_frames) {
1234 "mmap: user supplied mapping address is out of range");
1235 mutex_unlock(&usbvision->lock);
1239 /* VM_IO is eventually going to replace PageReserved altogether */
1240 vma->vm_flags |= VM_IO;
1241 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1243 pos = usbvision->frame[i].data;
1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1248 mutex_unlock(&usbvision->lock);
1256 mutex_unlock(&usbvision->lock);
1262 * Here comes the stuff for radio on usbvision based devices
1265 static int usbvision_radio_open(struct inode *inode, struct file *file)
1267 struct video_device *dev = video_devdata(file);
1268 struct usb_usbvision *usbvision =
1269 (struct usb_usbvision *) video_get_drvdata(dev);
1272 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1274 mutex_lock(&usbvision->lock);
1276 if (usbvision->user) {
1277 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1282 usbvision_reset_powerOffTimer(usbvision);
1283 if (usbvision->power == 0) {
1284 usbvision_power_on(usbvision);
1285 usbvision_i2c_register(usbvision);
1289 /* Alternate interface 1 is is the biggest frame size */
1290 errCode = usbvision_set_alternate(usbvision);
1292 usbvision->last_error = errCode;
1297 // If so far no errors then we shall start the radio
1298 usbvision->radio = 1;
1299 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1300 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1305 if (PowerOnAtOpen) {
1306 usbvision_i2c_unregister(usbvision);
1307 usbvision_power_off(usbvision);
1308 usbvision->initialized = 0;
1312 mutex_unlock(&usbvision->lock);
1317 static int usbvision_radio_close(struct inode *inode, struct file *file)
1319 struct video_device *dev = video_devdata(file);
1320 struct usb_usbvision *usbvision =
1321 (struct usb_usbvision *) video_get_drvdata(dev);
1326 mutex_lock(&usbvision->lock);
1328 /* Set packet size to 0 */
1329 usbvision->ifaceAlt=0;
1330 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1331 usbvision->ifaceAlt);
1333 usbvision_audio_off(usbvision);
1337 if (PowerOnAtOpen) {
1338 usbvision_set_powerOffTimer(usbvision);
1339 usbvision->initialized = 0;
1342 mutex_unlock(&usbvision->lock);
1344 if (usbvision->remove_pending) {
1345 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
1346 usbvision_release(usbvision);
1350 PDEBUG(DBG_IO, "success");
1356 * Here comes the stuff for vbi on usbvision based devices
1359 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1366 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1372 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1373 unsigned int cmd, void *arg)
1376 return -ENOIOCTLCMD;
1379 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1380 unsigned int cmd, unsigned long arg)
1382 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1387 // Video registration stuff
1391 static const struct file_operations usbvision_fops = {
1392 .owner = THIS_MODULE,
1393 .open = usbvision_v4l2_open,
1394 .release = usbvision_v4l2_close,
1395 .read = usbvision_v4l2_read,
1396 .mmap = usbvision_v4l2_mmap,
1397 .ioctl = video_ioctl2,
1398 .llseek = no_llseek,
1399 /* .poll = video_poll, */
1400 .compat_ioctl = v4l_compat_ioctl32,
1402 static struct video_device usbvision_video_template = {
1403 .owner = THIS_MODULE,
1404 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1405 .fops = &usbvision_fops,
1406 .name = "usbvision-video",
1407 .release = video_device_release,
1409 .vidioc_querycap = vidioc_querycap,
1410 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1411 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1412 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1413 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1414 .vidioc_reqbufs = vidioc_reqbufs,
1415 .vidioc_querybuf = vidioc_querybuf,
1416 .vidioc_qbuf = vidioc_qbuf,
1417 .vidioc_dqbuf = vidioc_dqbuf,
1418 .vidioc_s_std = vidioc_s_std,
1419 .vidioc_enum_input = vidioc_enum_input,
1420 .vidioc_g_input = vidioc_g_input,
1421 .vidioc_s_input = vidioc_s_input,
1422 .vidioc_queryctrl = vidioc_queryctrl,
1423 .vidioc_g_audio = vidioc_g_audio,
1424 .vidioc_s_audio = vidioc_s_audio,
1425 .vidioc_g_ctrl = vidioc_g_ctrl,
1426 .vidioc_s_ctrl = vidioc_s_ctrl,
1427 .vidioc_streamon = vidioc_streamon,
1428 .vidioc_streamoff = vidioc_streamoff,
1429 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1430 /* .vidiocgmbuf = vidiocgmbuf, */
1432 .vidioc_g_tuner = vidioc_g_tuner,
1433 .vidioc_s_tuner = vidioc_s_tuner,
1434 .vidioc_g_frequency = vidioc_g_frequency,
1435 .vidioc_s_frequency = vidioc_s_frequency,
1436 #ifdef CONFIG_VIDEO_ADV_DEBUG
1437 .vidioc_g_register = vidioc_g_register,
1438 .vidioc_s_register = vidioc_s_register,
1440 .tvnorms = USBVISION_NORMS,
1441 .current_norm = V4L2_STD_PAL
1446 static const struct file_operations usbvision_radio_fops = {
1447 .owner = THIS_MODULE,
1448 .open = usbvision_radio_open,
1449 .release = usbvision_radio_close,
1450 .ioctl = video_ioctl2,
1451 .llseek = no_llseek,
1452 .compat_ioctl = v4l_compat_ioctl32,
1455 static struct video_device usbvision_radio_template=
1457 .owner = THIS_MODULE,
1458 .type = VID_TYPE_TUNER,
1459 .fops = &usbvision_radio_fops,
1460 .name = "usbvision-radio",
1461 .release = video_device_release,
1463 .vidioc_querycap = vidioc_querycap,
1464 .vidioc_enum_input = vidioc_enum_input,
1465 .vidioc_g_input = vidioc_g_input,
1466 .vidioc_s_input = vidioc_s_input,
1467 .vidioc_queryctrl = vidioc_queryctrl,
1468 .vidioc_g_audio = vidioc_g_audio,
1469 .vidioc_s_audio = vidioc_s_audio,
1470 .vidioc_g_ctrl = vidioc_g_ctrl,
1471 .vidioc_s_ctrl = vidioc_s_ctrl,
1472 .vidioc_g_tuner = vidioc_g_tuner,
1473 .vidioc_s_tuner = vidioc_s_tuner,
1474 .vidioc_g_frequency = vidioc_g_frequency,
1475 .vidioc_s_frequency = vidioc_s_frequency,
1477 .tvnorms = USBVISION_NORMS,
1478 .current_norm = V4L2_STD_PAL
1482 static const struct file_operations usbvision_vbi_fops = {
1483 .owner = THIS_MODULE,
1484 .open = usbvision_vbi_open,
1485 .release = usbvision_vbi_close,
1486 .ioctl = usbvision_vbi_ioctl,
1487 .llseek = no_llseek,
1488 .compat_ioctl = v4l_compat_ioctl32,
1491 static struct video_device usbvision_vbi_template=
1493 .owner = THIS_MODULE,
1494 .type = VID_TYPE_TUNER,
1495 .fops = &usbvision_vbi_fops,
1496 .release = video_device_release,
1497 .name = "usbvision-vbi",
1502 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1503 struct video_device *vdev_template,
1506 struct usb_device *usb_dev = usbvision->dev;
1507 struct video_device *vdev;
1509 if (usb_dev == NULL) {
1510 err("%s: usbvision->dev is not set", __FUNCTION__);
1514 vdev = video_device_alloc();
1518 *vdev = *vdev_template;
1519 // vdev->minor = -1;
1520 vdev->dev = &usb_dev->dev;
1521 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1522 video_set_drvdata(vdev, usbvision);
1526 // unregister video4linux devices
1527 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1530 if (usbvision->vbi) {
1531 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
1532 usbvision->vbi->minor & 0x1f);
1533 if (usbvision->vbi->minor != -1) {
1534 video_unregister_device(usbvision->vbi);
1536 video_device_release(usbvision->vbi);
1538 usbvision->vbi = NULL;
1542 if (usbvision->rdev) {
1543 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
1544 usbvision->rdev->minor & 0x1f);
1545 if (usbvision->rdev->minor != -1) {
1546 video_unregister_device(usbvision->rdev);
1548 video_device_release(usbvision->rdev);
1550 usbvision->rdev = NULL;
1554 if (usbvision->vdev) {
1555 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
1556 usbvision->vdev->minor & 0x1f);
1557 if (usbvision->vdev->minor != -1) {
1558 video_unregister_device(usbvision->vdev);
1560 video_device_release(usbvision->vdev);
1562 usbvision->vdev = NULL;
1566 // register video4linux devices
1567 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1570 usbvision->vdev = usbvision_vdev_init(usbvision,
1571 &usbvision_video_template,
1573 if (usbvision->vdev == NULL) {
1576 if (video_register_device(usbvision->vdev,
1581 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
1582 usbvision->nr,usbvision->vdev->minor & 0x1f);
1585 if (usbvision_device_data[usbvision->DevModel].Radio) {
1586 // usbvision has radio
1587 usbvision->rdev = usbvision_vdev_init(usbvision,
1588 &usbvision_radio_template,
1590 if (usbvision->rdev == NULL) {
1593 if (video_register_device(usbvision->rdev,
1598 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
1599 usbvision->nr, usbvision->rdev->minor & 0x1f);
1602 if (usbvision_device_data[usbvision->DevModel].vbi) {
1603 usbvision->vbi = usbvision_vdev_init(usbvision,
1604 &usbvision_vbi_template,
1606 if (usbvision->vdev == NULL) {
1609 if (video_register_device(usbvision->vbi,
1614 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
1615 usbvision->nr,usbvision->vbi->minor & 0x1f);
1621 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1622 usbvision_unregister_video(usbvision);
1629 * This code allocates the struct usb_usbvision.
1630 * It is filled with default values.
1632 * Returns NULL on error, a pointer to usb_usbvision else.
1635 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1637 struct usb_usbvision *usbvision;
1639 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
1644 usbvision->dev = dev;
1646 mutex_init(&usbvision->lock); /* available */
1648 // prepare control urb for control messages during interrupts
1649 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1650 if (usbvision->ctrlUrb == NULL) {
1653 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1654 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1656 usbvision_init_powerOffTimer(usbvision);
1661 if (usbvision && usbvision->ctrlUrb) {
1662 usb_free_urb(usbvision->ctrlUrb);
1671 * usbvision_release()
1673 * This code does final release of struct usb_usbvision. This happens
1674 * after the device is disconnected -and- all clients closed their files.
1677 static void usbvision_release(struct usb_usbvision *usbvision)
1679 PDEBUG(DBG_PROBE, "");
1681 mutex_lock(&usbvision->lock);
1683 usbvision_reset_powerOffTimer(usbvision);
1685 usbvision->initialized = 0;
1687 mutex_unlock(&usbvision->lock);
1689 usbvision_remove_sysfs(usbvision->vdev);
1690 usbvision_unregister_video(usbvision);
1692 if (usbvision->ctrlUrb) {
1693 usb_free_urb(usbvision->ctrlUrb);
1698 PDEBUG(DBG_PROBE, "success");
1702 /*********************** usb interface **********************************/
1704 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1708 if (usbvision == NULL)
1711 model = usbvision->DevModel;
1712 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1714 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
1715 usbvision->Vin_Reg2_Preset =
1716 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
1718 usbvision->Vin_Reg2_Preset = 0;
1721 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
1723 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1724 usbvision->ctl_input = 0;
1726 /* This should be here to make i2c clients to be able to register */
1727 /* first switch off audio */
1728 usbvision_audio_off(usbvision);
1729 if (!PowerOnAtOpen) {
1730 /* and then power up the noisy tuner */
1731 usbvision_power_on(usbvision);
1732 usbvision_i2c_register(usbvision);
1739 * This procedure queries device descriptor and accepts the interface
1740 * if it looks like USBVISION video device
1743 static int __devinit usbvision_probe(struct usb_interface *intf,
1744 const struct usb_device_id *devid)
1746 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1747 struct usb_interface *uif;
1748 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1749 const struct usb_host_interface *interface;
1750 struct usb_usbvision *usbvision = NULL;
1751 const struct usb_endpoint_descriptor *endpoint;
1754 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1755 dev->descriptor.idVendor,
1756 dev->descriptor.idProduct, ifnum);
1758 model = devid->driver_info;
1759 if ( (model<0) || (model>=usbvision_device_data_size) ) {
1760 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
1763 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1764 usbvision_device_data[model].ModelString);
1766 if (usbvision_device_data[model].Interface >= 0) {
1767 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1769 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1771 endpoint = &interface->endpoint[1].desc;
1772 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1773 USB_ENDPOINT_XFER_ISOC) {
1774 err("%s: interface %d. has non-ISO endpoint!",
1775 __FUNCTION__, ifnum);
1776 err("%s: Endpoint attributes %d",
1777 __FUNCTION__, endpoint->bmAttributes);
1780 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1782 err("%s: interface %d. has ISO OUT endpoint!",
1783 __FUNCTION__, ifnum);
1787 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1788 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1792 if (dev->descriptor.bNumConfigurations > 1) {
1793 usbvision->bridgeType = BRIDGE_NT1004;
1794 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
1795 usbvision->bridgeType = BRIDGE_NT1005;
1797 usbvision->bridgeType = BRIDGE_NT1003;
1799 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1801 mutex_lock(&usbvision->lock);
1803 /* compute alternate max packet sizes */
1804 uif = dev->actconfig->interface[0];
1806 usbvision->num_alt=uif->num_altsetting;
1807 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1808 usbvision->alt_max_pkt_size = kmalloc(32*
1809 usbvision->num_alt,GFP_KERNEL);
1810 if (usbvision->alt_max_pkt_size == NULL) {
1811 err("usbvision: out of memory!\n");
1812 mutex_unlock(&usbvision->lock);
1816 for (i = 0; i < usbvision->num_alt ; i++) {
1817 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1819 usbvision->alt_max_pkt_size[i] =
1820 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1821 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1822 usbvision->alt_max_pkt_size[i]);
1826 usbvision->nr = usbvision_nr++;
1828 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1829 if (usbvision->have_tuner) {
1830 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1833 usbvision->tuner_addr = ADDR_UNSET;
1835 usbvision->DevModel = model;
1836 usbvision->remove_pending = 0;
1837 usbvision->iface = ifnum;
1838 usbvision->ifaceAlt = 0;
1839 usbvision->video_endp = endpoint->bEndpointAddress;
1840 usbvision->isocPacketSize = 0;
1841 usbvision->usb_bandwidth = 0;
1842 usbvision->user = 0;
1843 usbvision->streaming = Stream_Off;
1844 usbvision_register_video(usbvision);
1845 usbvision_configure_video(usbvision);
1846 mutex_unlock(&usbvision->lock);
1849 usb_set_intfdata (intf, usbvision);
1850 usbvision_create_sysfs(usbvision->vdev);
1852 PDEBUG(DBG_PROBE, "success");
1858 * usbvision_disconnect()
1860 * This procedure stops all driver activity, deallocates interface-private
1861 * structure (pointed by 'ptr') and after that driver should be removable
1862 * with no ill consequences.
1865 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1867 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1869 PDEBUG(DBG_PROBE, "");
1871 if (usbvision == NULL) {
1872 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1875 usb_set_intfdata (intf, NULL);
1877 mutex_lock(&usbvision->lock);
1879 // At this time we ask to cancel outstanding URBs
1880 usbvision_stop_isoc(usbvision);
1882 if (usbvision->power) {
1883 usbvision_i2c_unregister(usbvision);
1884 usbvision_power_off(usbvision);
1886 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1888 usb_put_dev(usbvision->dev);
1889 usbvision->dev = NULL; // USB device is no more
1891 mutex_unlock(&usbvision->lock);
1893 if (usbvision->user) {
1894 printk(KERN_INFO "%s: In use, disconnect pending\n",
1896 wake_up_interruptible(&usbvision->wait_frame);
1897 wake_up_interruptible(&usbvision->wait_stream);
1899 usbvision_release(usbvision);
1902 PDEBUG(DBG_PROBE, "success");
1906 static struct usb_driver usbvision_driver = {
1907 .name = "usbvision",
1908 .id_table = usbvision_table,
1909 .probe = usbvision_probe,
1910 .disconnect = usbvision_disconnect
1916 * This code is run to initialize the driver.
1919 static int __init usbvision_init(void)
1923 PDEBUG(DBG_PROBE, "");
1925 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1926 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1927 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1929 /* disable planar mode support unless compression enabled */
1930 if (isocMode != ISOC_MODE_COMPRESS ) {
1931 // FIXME : not the right way to set supported flag
1932 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1933 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1936 errCode = usb_register(&usbvision_driver);
1939 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1940 PDEBUG(DBG_PROBE, "success");
1945 static void __exit usbvision_exit(void)
1947 PDEBUG(DBG_PROBE, "");
1949 usb_deregister(&usbvision_driver);
1950 PDEBUG(DBG_PROBE, "success");
1953 module_init(usbvision_init);
1954 module_exit(usbvision_exit);
1957 * Overrides for Emacs so that we follow Linus's tabbing style.
1958 * ---------------------------------------------------------------------------