2 * USB USBVISION Video device driver 0.9.10
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>
53 #include <linux/smp_lock.h>
55 #include <linux/highmem.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/i2c.h>
64 #include <media/saa7115.h>
65 #include <media/v4l2-common.h>
66 #include <media/v4l2-ioctl.h>
67 #include <media/tuner.h>
69 #include <linux/workqueue.h>
71 #include "usbvision.h"
72 #include "usbvision-cards.h"
74 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
75 Dwaine Garden <DwaineGarden@rogers.com>"
76 #define DRIVER_NAME "usbvision"
77 #define DRIVER_ALIAS "USBVision"
78 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
79 #define DRIVER_LICENSE "GPL"
80 #define USBVISION_DRIVER_VERSION_MAJOR 0
81 #define USBVISION_DRIVER_VERSION_MINOR 9
82 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 10
83 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
84 USBVISION_DRIVER_VERSION_MINOR,\
85 USBVISION_DRIVER_VERSION_PATCHLEVEL)
86 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
87 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
88 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
90 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
93 #ifdef USBVISION_DEBUG
94 #define PDEBUG(level, fmt, args...) { \
95 if (video_debug & (level)) \
96 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
97 __func__, __LINE__ , ## args); \
100 #define PDEBUG(level, fmt, args...) do {} while(0)
104 #define DBG_PROBE 1<<2
105 #define DBG_MMAP 1<<3
108 #define rmspace(str) while(*str==' ') str++;
109 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
112 /* sequential number of usbvision device */
113 static int usbvision_nr
;
115 static struct usbvision_v4l2_format_st usbvision_v4l2_format
[] = {
116 { 1, 1, 8, V4L2_PIX_FMT_GREY
, "GREY" },
117 { 1, 2, 16, V4L2_PIX_FMT_RGB565
, "RGB565" },
118 { 1, 3, 24, V4L2_PIX_FMT_RGB24
, "RGB24" },
119 { 1, 4, 32, V4L2_PIX_FMT_RGB32
, "RGB32" },
120 { 1, 2, 16, V4L2_PIX_FMT_RGB555
, "RGB555" },
121 { 1, 2, 16, V4L2_PIX_FMT_YUYV
, "YUV422" },
122 { 1, 2, 12, V4L2_PIX_FMT_YVU420
, "YUV420P" }, // 1.5 !
123 { 1, 2, 16, V4L2_PIX_FMT_YUV422P
, "YUV422P" }
126 /* Function prototypes */
127 static void usbvision_release(struct usb_usbvision
*usbvision
);
129 /* Default initialization of device driver parameters */
130 /* Set the default format for ISOC endpoint */
131 static int isocMode
= ISOC_MODE_COMPRESS
;
132 /* Set the default Debug Mode of the device driver */
133 static int video_debug
;
134 /* Set the default device to power on at startup */
135 static int PowerOnAtOpen
= 1;
136 /* Sequential Number of Video Device */
137 static int video_nr
= -1;
138 /* Sequential Number of Radio Device */
139 static int radio_nr
= -1;
141 /* Grab parameters for the device driver */
143 /* Showing parameters under SYSFS */
144 module_param(isocMode
, int, 0444);
145 module_param(video_debug
, int, 0444);
146 module_param(PowerOnAtOpen
, int, 0444);
147 module_param(video_nr
, int, 0444);
148 module_param(radio_nr
, int, 0444);
150 MODULE_PARM_DESC(isocMode
, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
151 MODULE_PARM_DESC(video_debug
, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
152 MODULE_PARM_DESC(PowerOnAtOpen
, " Set the default device to power on when device is opened. Default: 1 (On)");
153 MODULE_PARM_DESC(video_nr
, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
154 MODULE_PARM_DESC(radio_nr
, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
158 MODULE_AUTHOR(DRIVER_AUTHOR
);
159 MODULE_DESCRIPTION(DRIVER_DESC
);
160 MODULE_LICENSE(DRIVER_LICENSE
);
161 MODULE_VERSION(USBVISION_VERSION_STRING
);
162 MODULE_ALIAS(DRIVER_ALIAS
);
165 /*****************************************************************************/
166 /* SYSFS Code - Copied from the stv680.c usb module. */
167 /* Device information is located at /sys/class/video4linux/video0 */
168 /* Device parameters information is located at /sys/module/usbvision */
169 /* Device USB Information is located at */
170 /* /sys/bus/usb/drivers/USBVision Video Grabber */
171 /*****************************************************************************/
173 #define YES_NO(x) ((x) ? "Yes" : "No")
175 static inline struct usb_usbvision
*cd_to_usbvision(struct device
*cd
)
177 struct video_device
*vdev
=
178 container_of(cd
, struct video_device
, dev
);
179 return video_get_drvdata(vdev
);
182 static ssize_t
show_version(struct device
*cd
,
183 struct device_attribute
*attr
, char *buf
)
185 return sprintf(buf
, "%s\n", USBVISION_VERSION_STRING
);
187 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
189 static ssize_t
show_model(struct device
*cd
,
190 struct device_attribute
*attr
, char *buf
)
192 struct video_device
*vdev
=
193 container_of(cd
, struct video_device
, dev
);
194 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
195 return sprintf(buf
, "%s\n",
196 usbvision_device_data
[usbvision
->DevModel
].ModelString
);
198 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
200 static ssize_t
show_hue(struct device
*cd
,
201 struct device_attribute
*attr
, char *buf
)
203 struct video_device
*vdev
=
204 container_of(cd
, struct video_device
, dev
);
205 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
206 struct v4l2_control ctrl
;
207 ctrl
.id
= V4L2_CID_HUE
;
210 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
211 return sprintf(buf
, "%d\n", ctrl
.value
);
213 static DEVICE_ATTR(hue
, S_IRUGO
, show_hue
, NULL
);
215 static ssize_t
show_contrast(struct device
*cd
,
216 struct device_attribute
*attr
, char *buf
)
218 struct video_device
*vdev
=
219 container_of(cd
, struct video_device
, dev
);
220 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
221 struct v4l2_control ctrl
;
222 ctrl
.id
= V4L2_CID_CONTRAST
;
225 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
226 return sprintf(buf
, "%d\n", ctrl
.value
);
228 static DEVICE_ATTR(contrast
, S_IRUGO
, show_contrast
, NULL
);
230 static ssize_t
show_brightness(struct device
*cd
,
231 struct device_attribute
*attr
, char *buf
)
233 struct video_device
*vdev
=
234 container_of(cd
, struct video_device
, dev
);
235 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
236 struct v4l2_control ctrl
;
237 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
240 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
241 return sprintf(buf
, "%d\n", ctrl
.value
);
243 static DEVICE_ATTR(brightness
, S_IRUGO
, show_brightness
, NULL
);
245 static ssize_t
show_saturation(struct device
*cd
,
246 struct device_attribute
*attr
, char *buf
)
248 struct video_device
*vdev
=
249 container_of(cd
, struct video_device
, dev
);
250 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
251 struct v4l2_control ctrl
;
252 ctrl
.id
= V4L2_CID_SATURATION
;
255 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
256 return sprintf(buf
, "%d\n", ctrl
.value
);
258 static DEVICE_ATTR(saturation
, S_IRUGO
, show_saturation
, NULL
);
260 static ssize_t
show_streaming(struct device
*cd
,
261 struct device_attribute
*attr
, char *buf
)
263 struct video_device
*vdev
=
264 container_of(cd
, struct video_device
, dev
);
265 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
266 return sprintf(buf
, "%s\n",
267 YES_NO(usbvision
->streaming
==Stream_On
?1:0));
269 static DEVICE_ATTR(streaming
, S_IRUGO
, show_streaming
, NULL
);
271 static ssize_t
show_compression(struct device
*cd
,
272 struct device_attribute
*attr
, char *buf
)
274 struct video_device
*vdev
=
275 container_of(cd
, struct video_device
, dev
);
276 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
277 return sprintf(buf
, "%s\n",
278 YES_NO(usbvision
->isocMode
==ISOC_MODE_COMPRESS
));
280 static DEVICE_ATTR(compression
, S_IRUGO
, show_compression
, NULL
);
282 static ssize_t
show_device_bridge(struct device
*cd
,
283 struct device_attribute
*attr
, char *buf
)
285 struct video_device
*vdev
=
286 container_of(cd
, struct video_device
, dev
);
287 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
288 return sprintf(buf
, "%d\n", usbvision
->bridgeType
);
290 static DEVICE_ATTR(bridge
, S_IRUGO
, show_device_bridge
, NULL
);
292 static void usbvision_create_sysfs(struct video_device
*vdev
)
298 res
= device_create_file(&vdev
->dev
, &dev_attr_version
);
301 res
= device_create_file(&vdev
->dev
, &dev_attr_model
);
304 res
= device_create_file(&vdev
->dev
, &dev_attr_hue
);
307 res
= device_create_file(&vdev
->dev
, &dev_attr_contrast
);
310 res
= device_create_file(&vdev
->dev
, &dev_attr_brightness
);
313 res
= device_create_file(&vdev
->dev
, &dev_attr_saturation
);
316 res
= device_create_file(&vdev
->dev
, &dev_attr_streaming
);
319 res
= device_create_file(&vdev
->dev
, &dev_attr_compression
);
322 res
= device_create_file(&vdev
->dev
, &dev_attr_bridge
);
327 dev_err(&vdev
->dev
, "%s error: %d\n", __func__
, res
);
330 static void usbvision_remove_sysfs(struct video_device
*vdev
)
333 device_remove_file(&vdev
->dev
, &dev_attr_version
);
334 device_remove_file(&vdev
->dev
, &dev_attr_model
);
335 device_remove_file(&vdev
->dev
, &dev_attr_hue
);
336 device_remove_file(&vdev
->dev
, &dev_attr_contrast
);
337 device_remove_file(&vdev
->dev
, &dev_attr_brightness
);
338 device_remove_file(&vdev
->dev
, &dev_attr_saturation
);
339 device_remove_file(&vdev
->dev
, &dev_attr_streaming
);
340 device_remove_file(&vdev
->dev
, &dev_attr_compression
);
341 device_remove_file(&vdev
->dev
, &dev_attr_bridge
);
348 * This is part of Video 4 Linux API. The driver can be opened by one
349 * client only (checks internal counter 'usbvision->user'). The procedure
350 * then allocates buffers needed for video processing.
353 static int usbvision_v4l2_open(struct file
*file
)
355 struct usb_usbvision
*usbvision
= video_drvdata(file
);
358 PDEBUG(DBG_IO
, "open");
361 usbvision_reset_powerOffTimer(usbvision
);
366 /* Allocate memory for the scratch ring buffer */
367 errCode
= usbvision_scratch_alloc(usbvision
);
368 if (isocMode
==ISOC_MODE_COMPRESS
) {
369 /* Allocate intermediate decompression buffers
371 errCode
= usbvision_decompress_alloc(usbvision
);
374 /* Deallocate all buffers if trouble */
375 usbvision_scratch_free(usbvision
);
376 usbvision_decompress_free(usbvision
);
380 /* If so far no errors then we shall start the camera */
382 mutex_lock(&usbvision
->lock
);
383 if (usbvision
->power
== 0) {
384 usbvision_power_on(usbvision
);
385 usbvision_i2c_register(usbvision
);
388 /* Send init sequence only once, it's large! */
389 if (!usbvision
->initialized
) {
391 setup_ok
= usbvision_setup(usbvision
,isocMode
);
393 usbvision
->initialized
= 1;
399 usbvision_begin_streaming(usbvision
);
400 errCode
= usbvision_init_isoc(usbvision
);
401 /* device must be initialized before isoc transfer */
402 usbvision_muxsel(usbvision
,0);
406 usbvision_i2c_unregister(usbvision
);
407 usbvision_power_off(usbvision
);
408 usbvision
->initialized
= 0;
411 mutex_unlock(&usbvision
->lock
);
415 usbvision_empty_framequeues(usbvision
);
417 PDEBUG(DBG_IO
, "success");
423 * usbvision_v4l2_close()
425 * This is part of Video 4 Linux API. The procedure
426 * stops streaming and deallocates all buffers that were earlier
427 * allocated in usbvision_v4l2_open().
430 static int usbvision_v4l2_close(struct file
*file
)
432 struct usb_usbvision
*usbvision
= video_drvdata(file
);
434 PDEBUG(DBG_IO
, "close");
435 mutex_lock(&usbvision
->lock
);
437 usbvision_audio_off(usbvision
);
438 usbvision_restart_isoc(usbvision
);
439 usbvision_stop_isoc(usbvision
);
441 usbvision_decompress_free(usbvision
);
442 usbvision_frames_free(usbvision
);
443 usbvision_empty_framequeues(usbvision
);
444 usbvision_scratch_free(usbvision
);
449 /* power off in a little while
450 to avoid off/on every close/open short sequences */
451 usbvision_set_powerOffTimer(usbvision
);
452 usbvision
->initialized
= 0;
455 mutex_unlock(&usbvision
->lock
);
457 if (usbvision
->remove_pending
) {
458 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
459 usbvision_release(usbvision
);
462 PDEBUG(DBG_IO
, "success");
470 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
473 #ifdef CONFIG_VIDEO_ADV_DEBUG
474 static int vidioc_g_register (struct file
*file
, void *priv
,
475 struct v4l2_dbg_register
*reg
)
477 struct usb_usbvision
*usbvision
= video_drvdata(file
);
480 if (!v4l2_chip_match_host(®
->match
))
482 /* NT100x has a 8-bit register space */
483 errCode
= usbvision_read_reg(usbvision
, reg
->reg
&0xff);
485 dev_err(&usbvision
->vdev
->dev
,
486 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
495 static int vidioc_s_register (struct file
*file
, void *priv
,
496 struct v4l2_dbg_register
*reg
)
498 struct usb_usbvision
*usbvision
= video_drvdata(file
);
501 if (!v4l2_chip_match_host(®
->match
))
503 /* NT100x has a 8-bit register space */
504 errCode
= usbvision_write_reg(usbvision
, reg
->reg
&0xff, reg
->val
);
506 dev_err(&usbvision
->vdev
->dev
,
507 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
515 static int vidioc_querycap (struct file
*file
, void *priv
,
516 struct v4l2_capability
*vc
)
518 struct usb_usbvision
*usbvision
= video_drvdata(file
);
520 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
522 usbvision_device_data
[usbvision
->DevModel
].ModelString
,
524 usb_make_path(usbvision
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
));
525 vc
->version
= USBVISION_DRIVER_VERSION
;
526 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
530 (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
534 static int vidioc_enum_input (struct file
*file
, void *priv
,
535 struct v4l2_input
*vi
)
537 struct usb_usbvision
*usbvision
= video_drvdata(file
);
540 if (vi
->index
>= usbvision
->video_inputs
)
542 if (usbvision
->have_tuner
) {
545 chan
= vi
->index
+ 1; /*skip Television string*/
547 /* Determine the requested input characteristics
548 specific for each usbvision card model */
551 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
552 strcpy(vi
->name
, "White Video Input");
554 strcpy(vi
->name
, "Television");
555 vi
->type
= V4L2_INPUT_TYPE_TUNER
;
558 vi
->std
= USBVISION_NORMS
;
562 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
563 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
564 strcpy(vi
->name
, "Green Video Input");
566 strcpy(vi
->name
, "Composite Video Input");
568 vi
->std
= V4L2_STD_PAL
;
571 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
572 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
573 strcpy(vi
->name
, "Yellow Video Input");
575 strcpy(vi
->name
, "S-Video Input");
577 vi
->std
= V4L2_STD_PAL
;
580 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
581 strcpy(vi
->name
, "Red Video Input");
582 vi
->std
= V4L2_STD_PAL
;
588 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *input
)
590 struct usb_usbvision
*usbvision
= video_drvdata(file
);
592 *input
= usbvision
->ctl_input
;
596 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int input
)
598 struct usb_usbvision
*usbvision
= video_drvdata(file
);
600 if (input
>= usbvision
->video_inputs
)
603 mutex_lock(&usbvision
->lock
);
604 usbvision_muxsel(usbvision
, input
);
605 usbvision_set_input(usbvision
);
606 usbvision_set_output(usbvision
,
608 usbvision
->curheight
);
609 mutex_unlock(&usbvision
->lock
);
613 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*id
)
615 struct usb_usbvision
*usbvision
= video_drvdata(file
);
617 usbvision
->tvnormId
=*id
;
619 mutex_lock(&usbvision
->lock
);
620 call_all(usbvision
, core
, s_std
, usbvision
->tvnormId
);
621 mutex_unlock(&usbvision
->lock
);
622 /* propagate the change to the decoder */
623 usbvision_muxsel(usbvision
, usbvision
->ctl_input
);
628 static int vidioc_g_tuner (struct file
*file
, void *priv
,
629 struct v4l2_tuner
*vt
)
631 struct usb_usbvision
*usbvision
= video_drvdata(file
);
633 if (!usbvision
->have_tuner
|| vt
->index
) // Only tuner 0
635 if(usbvision
->radio
) {
636 strcpy(vt
->name
, "Radio");
637 vt
->type
= V4L2_TUNER_RADIO
;
639 strcpy(vt
->name
, "Television");
641 /* Let clients fill in the remainder of this struct */
642 call_all(usbvision
, tuner
, g_tuner
, vt
);
647 static int vidioc_s_tuner (struct file
*file
, void *priv
,
648 struct v4l2_tuner
*vt
)
650 struct usb_usbvision
*usbvision
= video_drvdata(file
);
652 // Only no or one tuner for now
653 if (!usbvision
->have_tuner
|| vt
->index
)
655 /* let clients handle this */
656 call_all(usbvision
, tuner
, s_tuner
, vt
);
661 static int vidioc_g_frequency (struct file
*file
, void *priv
,
662 struct v4l2_frequency
*freq
)
664 struct usb_usbvision
*usbvision
= video_drvdata(file
);
666 freq
->tuner
= 0; // Only one tuner
667 if(usbvision
->radio
) {
668 freq
->type
= V4L2_TUNER_RADIO
;
670 freq
->type
= V4L2_TUNER_ANALOG_TV
;
672 freq
->frequency
= usbvision
->freq
;
677 static int vidioc_s_frequency (struct file
*file
, void *priv
,
678 struct v4l2_frequency
*freq
)
680 struct usb_usbvision
*usbvision
= video_drvdata(file
);
682 // Only no or one tuner for now
683 if (!usbvision
->have_tuner
|| freq
->tuner
)
686 usbvision
->freq
= freq
->frequency
;
687 call_all(usbvision
, tuner
, s_frequency
, freq
);
692 static int vidioc_g_audio (struct file
*file
, void *priv
, struct v4l2_audio
*a
)
694 struct usb_usbvision
*usbvision
= video_drvdata(file
);
696 if(usbvision
->radio
) {
697 strcpy(a
->name
,"Radio");
699 strcpy(a
->name
, "TV");
705 static int vidioc_s_audio (struct file
*file
, void *fh
,
706 struct v4l2_audio
*a
)
715 static int vidioc_queryctrl (struct file
*file
, void *priv
,
716 struct v4l2_queryctrl
*ctrl
)
718 struct usb_usbvision
*usbvision
= video_drvdata(file
);
720 call_all(usbvision
, core
, queryctrl
, ctrl
);
728 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
729 struct v4l2_control
*ctrl
)
731 struct usb_usbvision
*usbvision
= video_drvdata(file
);
732 call_all(usbvision
, core
, g_ctrl
, ctrl
);
737 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
738 struct v4l2_control
*ctrl
)
740 struct usb_usbvision
*usbvision
= video_drvdata(file
);
741 call_all(usbvision
, core
, s_ctrl
, ctrl
);
746 static int vidioc_reqbufs (struct file
*file
,
747 void *priv
, struct v4l2_requestbuffers
*vr
)
749 struct usb_usbvision
*usbvision
= video_drvdata(file
);
752 RESTRICT_TO_RANGE(vr
->count
,1,USBVISION_NUMFRAMES
);
754 /* Check input validity:
755 the user must do a VIDEO CAPTURE and MMAP method. */
756 if (vr
->memory
!= V4L2_MEMORY_MMAP
)
759 if(usbvision
->streaming
== Stream_On
) {
760 if ((ret
= usbvision_stream_interrupt(usbvision
)))
764 usbvision_frames_free(usbvision
);
765 usbvision_empty_framequeues(usbvision
);
766 vr
->count
= usbvision_frames_alloc(usbvision
,vr
->count
);
768 usbvision
->curFrame
= NULL
;
773 static int vidioc_querybuf (struct file
*file
,
774 void *priv
, struct v4l2_buffer
*vb
)
776 struct usb_usbvision
*usbvision
= video_drvdata(file
);
777 struct usbvision_frame
*frame
;
779 /* FIXME : must control
780 that buffers are mapped (VIDIOC_REQBUFS has been called) */
781 if(vb
->index
>=usbvision
->num_frames
) {
784 /* Updating the corresponding frame state */
786 frame
= &usbvision
->frame
[vb
->index
];
787 if(frame
->grabstate
>= FrameState_Ready
)
788 vb
->flags
|= V4L2_BUF_FLAG_QUEUED
;
789 if(frame
->grabstate
>= FrameState_Done
)
790 vb
->flags
|= V4L2_BUF_FLAG_DONE
;
791 if(frame
->grabstate
== FrameState_Unused
)
792 vb
->flags
|= V4L2_BUF_FLAG_MAPPED
;
793 vb
->memory
= V4L2_MEMORY_MMAP
;
795 vb
->m
.offset
= vb
->index
*PAGE_ALIGN(usbvision
->max_frame_size
);
797 vb
->memory
= V4L2_MEMORY_MMAP
;
798 vb
->field
= V4L2_FIELD_NONE
;
799 vb
->length
= usbvision
->curwidth
*
800 usbvision
->curheight
*
801 usbvision
->palette
.bytes_per_pixel
;
802 vb
->timestamp
= usbvision
->frame
[vb
->index
].timestamp
;
803 vb
->sequence
= usbvision
->frame
[vb
->index
].sequence
;
807 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
809 struct usb_usbvision
*usbvision
= video_drvdata(file
);
810 struct usbvision_frame
*frame
;
811 unsigned long lock_flags
;
813 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
814 if(vb
->index
>=usbvision
->num_frames
) {
818 frame
= &usbvision
->frame
[vb
->index
];
820 if (frame
->grabstate
!= FrameState_Unused
) {
824 /* Mark it as ready and enqueue frame */
825 frame
->grabstate
= FrameState_Ready
;
826 frame
->scanstate
= ScanState_Scanning
;
827 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
829 vb
->flags
&= ~V4L2_BUF_FLAG_DONE
;
831 /* set v4l2_format index */
832 frame
->v4l2_format
= usbvision
->palette
;
834 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
835 list_add_tail(&usbvision
->frame
[vb
->index
].frame
, &usbvision
->inqueue
);
836 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
841 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
843 struct usb_usbvision
*usbvision
= video_drvdata(file
);
845 struct usbvision_frame
*f
;
846 unsigned long lock_flags
;
848 if (list_empty(&(usbvision
->outqueue
))) {
849 if (usbvision
->streaming
== Stream_Idle
)
851 ret
= wait_event_interruptible
852 (usbvision
->wait_frame
,
853 !list_empty(&(usbvision
->outqueue
)));
858 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
859 f
= list_entry(usbvision
->outqueue
.next
,
860 struct usbvision_frame
, frame
);
861 list_del(usbvision
->outqueue
.next
);
862 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
864 f
->grabstate
= FrameState_Unused
;
866 vb
->memory
= V4L2_MEMORY_MMAP
;
867 vb
->flags
= V4L2_BUF_FLAG_MAPPED
|
868 V4L2_BUF_FLAG_QUEUED
|
870 vb
->index
= f
->index
;
871 vb
->sequence
= f
->sequence
;
872 vb
->timestamp
= f
->timestamp
;
873 vb
->field
= V4L2_FIELD_NONE
;
874 vb
->bytesused
= f
->scanlength
;
879 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
881 struct usb_usbvision
*usbvision
= video_drvdata(file
);
883 usbvision
->streaming
= Stream_On
;
884 call_all(usbvision
, video
, s_stream
, 1);
889 static int vidioc_streamoff(struct file
*file
,
890 void *priv
, enum v4l2_buf_type type
)
892 struct usb_usbvision
*usbvision
= video_drvdata(file
);
894 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
897 if(usbvision
->streaming
== Stream_On
) {
898 usbvision_stream_interrupt(usbvision
);
899 /* Stop all video streamings */
900 call_all(usbvision
, video
, s_stream
, 0);
902 usbvision_empty_framequeues(usbvision
);
907 static int vidioc_enum_fmt_vid_cap (struct file
*file
, void *priv
,
908 struct v4l2_fmtdesc
*vfd
)
910 if(vfd
->index
>=USBVISION_SUPPORTED_PALETTES
-1) {
913 strcpy(vfd
->description
,usbvision_v4l2_format
[vfd
->index
].desc
);
914 vfd
->pixelformat
= usbvision_v4l2_format
[vfd
->index
].format
;
918 static int vidioc_g_fmt_vid_cap (struct file
*file
, void *priv
,
919 struct v4l2_format
*vf
)
921 struct usb_usbvision
*usbvision
= video_drvdata(file
);
922 vf
->fmt
.pix
.width
= usbvision
->curwidth
;
923 vf
->fmt
.pix
.height
= usbvision
->curheight
;
924 vf
->fmt
.pix
.pixelformat
= usbvision
->palette
.format
;
925 vf
->fmt
.pix
.bytesperline
=
926 usbvision
->curwidth
*usbvision
->palette
.bytes_per_pixel
;
927 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*usbvision
->curheight
;
928 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
929 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
934 static int vidioc_try_fmt_vid_cap (struct file
*file
, void *priv
,
935 struct v4l2_format
*vf
)
937 struct usb_usbvision
*usbvision
= video_drvdata(file
);
940 /* Find requested format in available ones */
941 for(formatIdx
=0;formatIdx
<USBVISION_SUPPORTED_PALETTES
;formatIdx
++) {
942 if(vf
->fmt
.pix
.pixelformat
==
943 usbvision_v4l2_format
[formatIdx
].format
) {
944 usbvision
->palette
= usbvision_v4l2_format
[formatIdx
];
949 if(formatIdx
== USBVISION_SUPPORTED_PALETTES
) {
952 RESTRICT_TO_RANGE(vf
->fmt
.pix
.width
, MIN_FRAME_WIDTH
, MAX_FRAME_WIDTH
);
953 RESTRICT_TO_RANGE(vf
->fmt
.pix
.height
, MIN_FRAME_HEIGHT
, MAX_FRAME_HEIGHT
);
955 vf
->fmt
.pix
.bytesperline
= vf
->fmt
.pix
.width
*
956 usbvision
->palette
.bytes_per_pixel
;
957 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*vf
->fmt
.pix
.height
;
962 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
963 struct v4l2_format
*vf
)
965 struct usb_usbvision
*usbvision
= video_drvdata(file
);
968 if( 0 != (ret
=vidioc_try_fmt_vid_cap (file
, priv
, vf
)) ) {
972 /* stop io in case it is already in progress */
973 if(usbvision
->streaming
== Stream_On
) {
974 if ((ret
= usbvision_stream_interrupt(usbvision
)))
977 usbvision_frames_free(usbvision
);
978 usbvision_empty_framequeues(usbvision
);
980 usbvision
->curFrame
= NULL
;
982 /* by now we are committed to the new data... */
983 mutex_lock(&usbvision
->lock
);
984 usbvision_set_output(usbvision
, vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
);
985 mutex_unlock(&usbvision
->lock
);
990 static ssize_t
usbvision_v4l2_read(struct file
*file
, char __user
*buf
,
991 size_t count
, loff_t
*ppos
)
993 struct usb_usbvision
*usbvision
= video_drvdata(file
);
994 int noblock
= file
->f_flags
& O_NONBLOCK
;
995 unsigned long lock_flags
;
998 struct usbvision_frame
*frame
;
1000 PDEBUG(DBG_IO
, "%s: %ld bytes, noblock=%d", __func__
,
1001 (unsigned long)count
, noblock
);
1003 if (!USBVISION_IS_OPERATIONAL(usbvision
) || (buf
== NULL
))
1006 /* This entry point is compatible with the mmap routines
1007 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1008 to get frames or call read on the device. */
1009 if(!usbvision
->num_frames
) {
1010 /* First, allocate some frames to work with
1011 if this has not been done with VIDIOC_REQBUF */
1012 usbvision_frames_free(usbvision
);
1013 usbvision_empty_framequeues(usbvision
);
1014 usbvision_frames_alloc(usbvision
,USBVISION_NUMFRAMES
);
1017 if(usbvision
->streaming
!= Stream_On
) {
1018 /* no stream is running, make it running ! */
1019 usbvision
->streaming
= Stream_On
;
1020 call_all(usbvision
, video
, s_stream
, 1);
1023 /* Then, enqueue as many frames as possible
1024 (like a user of VIDIOC_QBUF would do) */
1025 for(i
=0;i
<usbvision
->num_frames
;i
++) {
1026 frame
= &usbvision
->frame
[i
];
1027 if(frame
->grabstate
== FrameState_Unused
) {
1028 /* Mark it as ready and enqueue frame */
1029 frame
->grabstate
= FrameState_Ready
;
1030 frame
->scanstate
= ScanState_Scanning
;
1031 /* Accumulated in usbvision_parse_data() */
1032 frame
->scanlength
= 0;
1034 /* set v4l2_format index */
1035 frame
->v4l2_format
= usbvision
->palette
;
1037 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1038 list_add_tail(&frame
->frame
, &usbvision
->inqueue
);
1039 spin_unlock_irqrestore(&usbvision
->queue_lock
,
1044 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1045 if (list_empty(&(usbvision
->outqueue
))) {
1049 ret
= wait_event_interruptible
1050 (usbvision
->wait_frame
,
1051 !list_empty(&(usbvision
->outqueue
)));
1056 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1057 frame
= list_entry(usbvision
->outqueue
.next
,
1058 struct usbvision_frame
, frame
);
1059 list_del(usbvision
->outqueue
.next
);
1060 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1062 /* An error returns an empty frame */
1063 if (frame
->grabstate
== FrameState_Error
) {
1064 frame
->bytes_read
= 0;
1068 PDEBUG(DBG_IO
, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1070 frame
->index
, frame
->bytes_read
, frame
->scanlength
);
1072 /* copy bytes to user space; we allow for partials reads */
1073 if ((count
+ frame
->bytes_read
) > (unsigned long)frame
->scanlength
)
1074 count
= frame
->scanlength
- frame
->bytes_read
;
1076 if (copy_to_user(buf
, frame
->data
+ frame
->bytes_read
, count
)) {
1080 frame
->bytes_read
+= count
;
1081 PDEBUG(DBG_IO
, "%s: {copy} count used=%ld, new bytes_read=%ld",
1083 (unsigned long)count
, frame
->bytes_read
);
1085 /* For now, forget the frame if it has not been read in one shot. */
1086 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1087 frame
->bytes_read
= 0;
1089 /* Mark it as available to be used again. */
1090 frame
->grabstate
= FrameState_Unused
;
1096 static int usbvision_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1098 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1099 start
= vma
->vm_start
;
1102 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1104 PDEBUG(DBG_MMAP
, "mmap");
1106 mutex_lock(&usbvision
->lock
);
1108 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1109 mutex_unlock(&usbvision
->lock
);
1113 if (!(vma
->vm_flags
& VM_WRITE
) ||
1114 size
!= PAGE_ALIGN(usbvision
->max_frame_size
)) {
1115 mutex_unlock(&usbvision
->lock
);
1119 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
1120 if (((PAGE_ALIGN(usbvision
->max_frame_size
)*i
) >> PAGE_SHIFT
) ==
1124 if (i
== usbvision
->num_frames
) {
1126 "mmap: user supplied mapping address is out of range");
1127 mutex_unlock(&usbvision
->lock
);
1131 /* VM_IO is eventually going to replace PageReserved altogether */
1132 vma
->vm_flags
|= VM_IO
;
1133 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1135 pos
= usbvision
->frame
[i
].data
;
1138 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1139 PDEBUG(DBG_MMAP
, "mmap: vm_insert_page failed");
1140 mutex_unlock(&usbvision
->lock
);
1148 mutex_unlock(&usbvision
->lock
);
1154 * Here comes the stuff for radio on usbvision based devices
1157 static int usbvision_radio_open(struct file
*file
)
1159 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1162 PDEBUG(DBG_IO
, "%s:", __func__
);
1164 mutex_lock(&usbvision
->lock
);
1166 if (usbvision
->user
) {
1167 dev_err(&usbvision
->rdev
->dev
,
1168 "%s: Someone tried to open an already opened USBVision Radio!\n",
1174 usbvision_reset_powerOffTimer(usbvision
);
1175 if (usbvision
->power
== 0) {
1176 usbvision_power_on(usbvision
);
1177 usbvision_i2c_register(usbvision
);
1181 /* Alternate interface 1 is is the biggest frame size */
1182 errCode
= usbvision_set_alternate(usbvision
);
1184 usbvision
->last_error
= errCode
;
1189 // If so far no errors then we shall start the radio
1190 usbvision
->radio
= 1;
1191 call_all(usbvision
, tuner
, s_radio
);
1192 usbvision_set_audio(usbvision
, USBVISION_AUDIO_RADIO
);
1197 if (PowerOnAtOpen
) {
1198 usbvision_i2c_unregister(usbvision
);
1199 usbvision_power_off(usbvision
);
1200 usbvision
->initialized
= 0;
1204 mutex_unlock(&usbvision
->lock
);
1209 static int usbvision_radio_close(struct file
*file
)
1211 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1216 mutex_lock(&usbvision
->lock
);
1218 /* Set packet size to 0 */
1219 usbvision
->ifaceAlt
=0;
1220 errCode
= usb_set_interface(usbvision
->dev
, usbvision
->iface
,
1221 usbvision
->ifaceAlt
);
1223 usbvision_audio_off(usbvision
);
1227 if (PowerOnAtOpen
) {
1228 usbvision_set_powerOffTimer(usbvision
);
1229 usbvision
->initialized
= 0;
1232 mutex_unlock(&usbvision
->lock
);
1234 if (usbvision
->remove_pending
) {
1235 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
1236 usbvision_release(usbvision
);
1239 PDEBUG(DBG_IO
, "success");
1244 // Video registration stuff
1248 static const struct v4l2_file_operations usbvision_fops
= {
1249 .owner
= THIS_MODULE
,
1250 .open
= usbvision_v4l2_open
,
1251 .release
= usbvision_v4l2_close
,
1252 .read
= usbvision_v4l2_read
,
1253 .mmap
= usbvision_v4l2_mmap
,
1254 .ioctl
= video_ioctl2
,
1255 /* .poll = video_poll, */
1258 static const struct v4l2_ioctl_ops usbvision_ioctl_ops
= {
1259 .vidioc_querycap
= vidioc_querycap
,
1260 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1261 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1262 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1263 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1264 .vidioc_reqbufs
= vidioc_reqbufs
,
1265 .vidioc_querybuf
= vidioc_querybuf
,
1266 .vidioc_qbuf
= vidioc_qbuf
,
1267 .vidioc_dqbuf
= vidioc_dqbuf
,
1268 .vidioc_s_std
= vidioc_s_std
,
1269 .vidioc_enum_input
= vidioc_enum_input
,
1270 .vidioc_g_input
= vidioc_g_input
,
1271 .vidioc_s_input
= vidioc_s_input
,
1272 .vidioc_queryctrl
= vidioc_queryctrl
,
1273 .vidioc_g_audio
= vidioc_g_audio
,
1274 .vidioc_s_audio
= vidioc_s_audio
,
1275 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1276 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1277 .vidioc_streamon
= vidioc_streamon
,
1278 .vidioc_streamoff
= vidioc_streamoff
,
1279 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1280 /* .vidiocgmbuf = vidiocgmbuf, */
1282 .vidioc_g_tuner
= vidioc_g_tuner
,
1283 .vidioc_s_tuner
= vidioc_s_tuner
,
1284 .vidioc_g_frequency
= vidioc_g_frequency
,
1285 .vidioc_s_frequency
= vidioc_s_frequency
,
1286 #ifdef CONFIG_VIDEO_ADV_DEBUG
1287 .vidioc_g_register
= vidioc_g_register
,
1288 .vidioc_s_register
= vidioc_s_register
,
1292 static struct video_device usbvision_video_template
= {
1293 .fops
= &usbvision_fops
,
1294 .ioctl_ops
= &usbvision_ioctl_ops
,
1295 .name
= "usbvision-video",
1296 .release
= video_device_release
,
1297 .tvnorms
= USBVISION_NORMS
,
1298 .current_norm
= V4L2_STD_PAL
1303 static const struct v4l2_file_operations usbvision_radio_fops
= {
1304 .owner
= THIS_MODULE
,
1305 .open
= usbvision_radio_open
,
1306 .release
= usbvision_radio_close
,
1307 .ioctl
= video_ioctl2
,
1310 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops
= {
1311 .vidioc_querycap
= vidioc_querycap
,
1312 .vidioc_enum_input
= vidioc_enum_input
,
1313 .vidioc_g_input
= vidioc_g_input
,
1314 .vidioc_s_input
= vidioc_s_input
,
1315 .vidioc_queryctrl
= vidioc_queryctrl
,
1316 .vidioc_g_audio
= vidioc_g_audio
,
1317 .vidioc_s_audio
= vidioc_s_audio
,
1318 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1319 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1320 .vidioc_g_tuner
= vidioc_g_tuner
,
1321 .vidioc_s_tuner
= vidioc_s_tuner
,
1322 .vidioc_g_frequency
= vidioc_g_frequency
,
1323 .vidioc_s_frequency
= vidioc_s_frequency
,
1326 static struct video_device usbvision_radio_template
= {
1327 .fops
= &usbvision_radio_fops
,
1328 .name
= "usbvision-radio",
1329 .release
= video_device_release
,
1330 .ioctl_ops
= &usbvision_radio_ioctl_ops
,
1332 .tvnorms
= USBVISION_NORMS
,
1333 .current_norm
= V4L2_STD_PAL
1337 static struct video_device
*usbvision_vdev_init(struct usb_usbvision
*usbvision
,
1338 struct video_device
*vdev_template
,
1341 struct usb_device
*usb_dev
= usbvision
->dev
;
1342 struct video_device
*vdev
;
1344 if (usb_dev
== NULL
) {
1345 dev_err(&usbvision
->dev
->dev
,
1346 "%s: usbvision->dev is not set\n", __func__
);
1350 vdev
= video_device_alloc();
1354 *vdev
= *vdev_template
;
1355 vdev
->v4l2_dev
= &usbvision
->v4l2_dev
;
1356 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s", name
);
1357 video_set_drvdata(vdev
, usbvision
);
1361 // unregister video4linux devices
1362 static void usbvision_unregister_video(struct usb_usbvision
*usbvision
)
1365 if (usbvision
->rdev
) {
1366 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1367 video_device_node_name(usbvision
->rdev
));
1368 if (video_is_registered(usbvision
->rdev
)) {
1369 video_unregister_device(usbvision
->rdev
);
1371 video_device_release(usbvision
->rdev
);
1373 usbvision
->rdev
= NULL
;
1377 if (usbvision
->vdev
) {
1378 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1379 video_device_node_name(usbvision
->vdev
));
1380 if (video_is_registered(usbvision
->vdev
)) {
1381 video_unregister_device(usbvision
->vdev
);
1383 video_device_release(usbvision
->vdev
);
1385 usbvision
->vdev
= NULL
;
1389 // register video4linux devices
1390 static int __devinit
usbvision_register_video(struct usb_usbvision
*usbvision
)
1393 usbvision
->vdev
= usbvision_vdev_init(usbvision
,
1394 &usbvision_video_template
,
1396 if (usbvision
->vdev
== NULL
) {
1399 if (video_register_device(usbvision
->vdev
,
1404 printk(KERN_INFO
"USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1405 usbvision
->nr
, video_device_node_name(usbvision
->vdev
));
1408 if (usbvision_device_data
[usbvision
->DevModel
].Radio
) {
1409 // usbvision has radio
1410 usbvision
->rdev
= usbvision_vdev_init(usbvision
,
1411 &usbvision_radio_template
,
1413 if (usbvision
->rdev
== NULL
) {
1416 if (video_register_device(usbvision
->rdev
,
1421 printk(KERN_INFO
"USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1422 usbvision
->nr
, video_device_node_name(usbvision
->rdev
));
1428 dev_err(&usbvision
->dev
->dev
,
1429 "USBVision[%d]: video_register_device() failed\n",
1431 usbvision_unregister_video(usbvision
);
1438 * This code allocates the struct usb_usbvision.
1439 * It is filled with default values.
1441 * Returns NULL on error, a pointer to usb_usbvision else.
1444 static struct usb_usbvision
*usbvision_alloc(struct usb_device
*dev
,
1445 struct usb_interface
*intf
)
1447 struct usb_usbvision
*usbvision
;
1449 usbvision
= kzalloc(sizeof(struct usb_usbvision
), GFP_KERNEL
);
1450 if (usbvision
== NULL
)
1453 usbvision
->dev
= dev
;
1454 if (v4l2_device_register(&intf
->dev
, &usbvision
->v4l2_dev
))
1457 mutex_init(&usbvision
->lock
); /* available */
1459 // prepare control urb for control messages during interrupts
1460 usbvision
->ctrlUrb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
1461 if (usbvision
->ctrlUrb
== NULL
)
1463 init_waitqueue_head(&usbvision
->ctrlUrb_wq
);
1465 usbvision_init_powerOffTimer(usbvision
);
1470 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1477 * usbvision_release()
1479 * This code does final release of struct usb_usbvision. This happens
1480 * after the device is disconnected -and- all clients closed their files.
1483 static void usbvision_release(struct usb_usbvision
*usbvision
)
1485 PDEBUG(DBG_PROBE
, "");
1487 mutex_lock(&usbvision
->lock
);
1489 usbvision_reset_powerOffTimer(usbvision
);
1491 usbvision
->initialized
= 0;
1493 mutex_unlock(&usbvision
->lock
);
1495 usbvision_remove_sysfs(usbvision
->vdev
);
1496 usbvision_unregister_video(usbvision
);
1498 if (usbvision
->ctrlUrb
) {
1499 usb_free_urb(usbvision
->ctrlUrb
);
1502 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1505 PDEBUG(DBG_PROBE
, "success");
1509 /*********************** usb interface **********************************/
1511 static void usbvision_configure_video(struct usb_usbvision
*usbvision
)
1515 if (usbvision
== NULL
)
1518 model
= usbvision
->DevModel
;
1519 usbvision
->palette
= usbvision_v4l2_format
[2]; // V4L2_PIX_FMT_RGB24;
1521 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2_override
) {
1522 usbvision
->Vin_Reg2_Preset
=
1523 usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2
;
1525 usbvision
->Vin_Reg2_Preset
= 0;
1528 usbvision
->tvnormId
= usbvision_device_data
[model
].VideoNorm
;
1530 usbvision
->video_inputs
= usbvision_device_data
[model
].VideoChannels
;
1531 usbvision
->ctl_input
= 0;
1533 /* This should be here to make i2c clients to be able to register */
1534 /* first switch off audio */
1535 usbvision_audio_off(usbvision
);
1536 if (!PowerOnAtOpen
) {
1537 /* and then power up the noisy tuner */
1538 usbvision_power_on(usbvision
);
1539 usbvision_i2c_register(usbvision
);
1546 * This procedure queries device descriptor and accepts the interface
1547 * if it looks like USBVISION video device
1550 static int __devinit
usbvision_probe(struct usb_interface
*intf
,
1551 const struct usb_device_id
*devid
)
1553 struct usb_device
*dev
= usb_get_dev(interface_to_usbdev(intf
));
1554 struct usb_interface
*uif
;
1555 __u8 ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
1556 const struct usb_host_interface
*interface
;
1557 struct usb_usbvision
*usbvision
= NULL
;
1558 const struct usb_endpoint_descriptor
*endpoint
;
1561 PDEBUG(DBG_PROBE
, "VID=%#04x, PID=%#04x, ifnum=%u",
1562 dev
->descriptor
.idVendor
,
1563 dev
->descriptor
.idProduct
, ifnum
);
1565 model
= devid
->driver_info
;
1566 if ( (model
<0) || (model
>=usbvision_device_data_size
) ) {
1567 PDEBUG(DBG_PROBE
, "model out of bounds %d",model
);
1570 printk(KERN_INFO
"%s: %s found\n", __func__
,
1571 usbvision_device_data
[model
].ModelString
);
1573 if (usbvision_device_data
[model
].Interface
>= 0) {
1574 interface
= &dev
->actconfig
->interface
[usbvision_device_data
[model
].Interface
]->altsetting
[0];
1576 interface
= &dev
->actconfig
->interface
[ifnum
]->altsetting
[0];
1578 endpoint
= &interface
->endpoint
[1].desc
;
1579 if (!usb_endpoint_xfer_isoc(endpoint
)) {
1580 dev_err(&intf
->dev
, "%s: interface %d. has non-ISO endpoint!\n",
1582 dev_err(&intf
->dev
, "%s: Endpoint attributes %d",
1583 __func__
, endpoint
->bmAttributes
);
1586 if (usb_endpoint_dir_out(endpoint
)) {
1587 dev_err(&intf
->dev
, "%s: interface %d. has ISO OUT endpoint!\n",
1592 usbvision
= usbvision_alloc(dev
, intf
);
1593 if (usbvision
== NULL
) {
1594 dev_err(&intf
->dev
, "%s: couldn't allocate USBVision struct\n", __func__
);
1598 if (dev
->descriptor
.bNumConfigurations
> 1) {
1599 usbvision
->bridgeType
= BRIDGE_NT1004
;
1600 } else if (model
== DAZZLE_DVC_90_REV_1_SECAM
) {
1601 usbvision
->bridgeType
= BRIDGE_NT1005
;
1603 usbvision
->bridgeType
= BRIDGE_NT1003
;
1605 PDEBUG(DBG_PROBE
, "bridgeType %d", usbvision
->bridgeType
);
1607 mutex_lock(&usbvision
->lock
);
1609 /* compute alternate max packet sizes */
1610 uif
= dev
->actconfig
->interface
[0];
1612 usbvision
->num_alt
=uif
->num_altsetting
;
1613 PDEBUG(DBG_PROBE
, "Alternate settings: %i",usbvision
->num_alt
);
1614 usbvision
->alt_max_pkt_size
= kmalloc(32*
1615 usbvision
->num_alt
,GFP_KERNEL
);
1616 if (usbvision
->alt_max_pkt_size
== NULL
) {
1617 dev_err(&intf
->dev
, "usbvision: out of memory!\n");
1618 mutex_unlock(&usbvision
->lock
);
1622 for (i
= 0; i
< usbvision
->num_alt
; i
++) {
1623 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1625 usbvision
->alt_max_pkt_size
[i
] =
1626 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1627 PDEBUG(DBG_PROBE
, "Alternate setting %i, max size= %i",i
,
1628 usbvision
->alt_max_pkt_size
[i
]);
1632 usbvision
->nr
= usbvision_nr
++;
1634 usbvision
->have_tuner
= usbvision_device_data
[model
].Tuner
;
1635 if (usbvision
->have_tuner
) {
1636 usbvision
->tuner_type
= usbvision_device_data
[model
].TunerType
;
1639 usbvision
->DevModel
= model
;
1640 usbvision
->remove_pending
= 0;
1641 usbvision
->iface
= ifnum
;
1642 usbvision
->ifaceAlt
= 0;
1643 usbvision
->video_endp
= endpoint
->bEndpointAddress
;
1644 usbvision
->isocPacketSize
= 0;
1645 usbvision
->usb_bandwidth
= 0;
1646 usbvision
->user
= 0;
1647 usbvision
->streaming
= Stream_Off
;
1648 usbvision_register_video(usbvision
);
1649 usbvision_configure_video(usbvision
);
1650 mutex_unlock(&usbvision
->lock
);
1652 usbvision_create_sysfs(usbvision
->vdev
);
1654 PDEBUG(DBG_PROBE
, "success");
1660 * usbvision_disconnect()
1662 * This procedure stops all driver activity, deallocates interface-private
1663 * structure (pointed by 'ptr') and after that driver should be removable
1664 * with no ill consequences.
1667 static void __devexit
usbvision_disconnect(struct usb_interface
*intf
)
1669 struct usb_usbvision
*usbvision
= to_usbvision(usb_get_intfdata(intf
));
1671 PDEBUG(DBG_PROBE
, "");
1673 if (usbvision
== NULL
) {
1674 dev_err(&usbvision
->dev
->dev
,
1675 "%s: usb_get_intfdata() failed\n", __func__
);
1679 mutex_lock(&usbvision
->lock
);
1681 // At this time we ask to cancel outstanding URBs
1682 usbvision_stop_isoc(usbvision
);
1684 v4l2_device_disconnect(&usbvision
->v4l2_dev
);
1686 if (usbvision
->power
) {
1687 usbvision_i2c_unregister(usbvision
);
1688 usbvision_power_off(usbvision
);
1690 usbvision
->remove_pending
= 1; // Now all ISO data will be ignored
1692 usb_put_dev(usbvision
->dev
);
1693 usbvision
->dev
= NULL
; // USB device is no more
1695 mutex_unlock(&usbvision
->lock
);
1697 if (usbvision
->user
) {
1698 printk(KERN_INFO
"%s: In use, disconnect pending\n",
1700 wake_up_interruptible(&usbvision
->wait_frame
);
1701 wake_up_interruptible(&usbvision
->wait_stream
);
1703 usbvision_release(usbvision
);
1706 PDEBUG(DBG_PROBE
, "success");
1709 static struct usb_driver usbvision_driver
= {
1710 .name
= "usbvision",
1711 .id_table
= usbvision_table
,
1712 .probe
= usbvision_probe
,
1713 .disconnect
= __devexit_p(usbvision_disconnect
),
1719 * This code is run to initialize the driver.
1722 static int __init
usbvision_init(void)
1726 PDEBUG(DBG_PROBE
, "");
1728 PDEBUG(DBG_IO
, "IO debugging is enabled [video]");
1729 PDEBUG(DBG_PROBE
, "PROBE debugging is enabled [video]");
1730 PDEBUG(DBG_MMAP
, "MMAP debugging is enabled [video]");
1732 /* disable planar mode support unless compression enabled */
1733 if (isocMode
!= ISOC_MODE_COMPRESS
) {
1734 // FIXME : not the right way to set supported flag
1735 usbvision_v4l2_format
[6].supported
= 0; // V4L2_PIX_FMT_YVU420
1736 usbvision_v4l2_format
[7].supported
= 0; // V4L2_PIX_FMT_YUV422P
1739 errCode
= usb_register(&usbvision_driver
);
1742 printk(KERN_INFO DRIVER_DESC
" : " USBVISION_VERSION_STRING
"\n");
1743 PDEBUG(DBG_PROBE
, "success");
1748 static void __exit
usbvision_exit(void)
1750 PDEBUG(DBG_PROBE
, "");
1752 usb_deregister(&usbvision_driver
);
1753 PDEBUG(DBG_PROBE
, "success");
1756 module_init(usbvision_init
);
1757 module_exit(usbvision_exit
);
1760 * Overrides for Emacs so that we follow Linus's tabbing style.
1761 * ---------------------------------------------------------------------------