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>
54 #include <linux/highmem.h>
55 #include <linux/vmalloc.h>
56 #include <linux/module.h>
57 #include <linux/init.h>
58 #include <linux/spinlock.h>
60 #include <linux/videodev2.h>
61 #include <linux/i2c.h>
63 #include <media/saa7115.h>
64 #include <media/v4l2-common.h>
65 #include <media/v4l2-ioctl.h>
66 #include <media/tuner.h>
68 #include <linux/workqueue.h>
70 #include "usbvision.h"
71 #include "usbvision-cards.h"
73 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>,\
74 Dwaine Garden <DwaineGarden@rogers.com>"
75 #define DRIVER_NAME "usbvision"
76 #define DRIVER_ALIAS "USBVision"
77 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
78 #define DRIVER_LICENSE "GPL"
79 #define USBVISION_DRIVER_VERSION_MAJOR 0
80 #define USBVISION_DRIVER_VERSION_MINOR 9
81 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 10
82 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
83 USBVISION_DRIVER_VERSION_MINOR,\
84 USBVISION_DRIVER_VERSION_PATCHLEVEL)
85 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
86 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
87 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
89 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
92 #ifdef USBVISION_DEBUG
93 #define PDEBUG(level, fmt, args...) { \
94 if (video_debug & (level)) \
95 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
96 __func__, __LINE__ , ## args); \
99 #define PDEBUG(level, fmt, args...) do {} while(0)
103 #define DBG_PROBE 1<<2
104 #define DBG_MMAP 1<<3
107 #define rmspace(str) while(*str==' ') str++;
108 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
111 /* sequential number of usbvision device */
112 static int usbvision_nr
;
114 static struct usbvision_v4l2_format_st usbvision_v4l2_format
[] = {
115 { 1, 1, 8, V4L2_PIX_FMT_GREY
, "GREY" },
116 { 1, 2, 16, V4L2_PIX_FMT_RGB565
, "RGB565" },
117 { 1, 3, 24, V4L2_PIX_FMT_RGB24
, "RGB24" },
118 { 1, 4, 32, V4L2_PIX_FMT_RGB32
, "RGB32" },
119 { 1, 2, 16, V4L2_PIX_FMT_RGB555
, "RGB555" },
120 { 1, 2, 16, V4L2_PIX_FMT_YUYV
, "YUV422" },
121 { 1, 2, 12, V4L2_PIX_FMT_YVU420
, "YUV420P" }, // 1.5 !
122 { 1, 2, 16, V4L2_PIX_FMT_YUV422P
, "YUV422P" }
125 /* Function prototypes */
126 static void usbvision_release(struct usb_usbvision
*usbvision
);
128 /* Default initialization of device driver parameters */
129 /* Set the default format for ISOC endpoint */
130 static int isocMode
= ISOC_MODE_COMPRESS
;
131 /* Set the default Debug Mode of the device driver */
132 static int video_debug
;
133 /* Set the default device to power on at startup */
134 static int PowerOnAtOpen
= 1;
135 /* Sequential Number of Video Device */
136 static int video_nr
= -1;
137 /* Sequential Number of Radio Device */
138 static int radio_nr
= -1;
140 /* Grab parameters for the device driver */
142 /* Showing parameters under SYSFS */
143 module_param(isocMode
, int, 0444);
144 module_param(video_debug
, int, 0444);
145 module_param(PowerOnAtOpen
, int, 0444);
146 module_param(video_nr
, int, 0444);
147 module_param(radio_nr
, int, 0444);
149 MODULE_PARM_DESC(isocMode
, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
150 MODULE_PARM_DESC(video_debug
, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
151 MODULE_PARM_DESC(PowerOnAtOpen
, " Set the default device to power on when device is opened. Default: 1 (On)");
152 MODULE_PARM_DESC(video_nr
, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
153 MODULE_PARM_DESC(radio_nr
, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
157 MODULE_AUTHOR(DRIVER_AUTHOR
);
158 MODULE_DESCRIPTION(DRIVER_DESC
);
159 MODULE_LICENSE(DRIVER_LICENSE
);
160 MODULE_VERSION(USBVISION_VERSION_STRING
);
161 MODULE_ALIAS(DRIVER_ALIAS
);
164 /*****************************************************************************/
165 /* SYSFS Code - Copied from the stv680.c usb module. */
166 /* Device information is located at /sys/class/video4linux/video0 */
167 /* Device parameters information is located at /sys/module/usbvision */
168 /* Device USB Information is located at */
169 /* /sys/bus/usb/drivers/USBVision Video Grabber */
170 /*****************************************************************************/
172 #define YES_NO(x) ((x) ? "Yes" : "No")
174 static inline struct usb_usbvision
*cd_to_usbvision(struct device
*cd
)
176 struct video_device
*vdev
=
177 container_of(cd
, struct video_device
, dev
);
178 return video_get_drvdata(vdev
);
181 static ssize_t
show_version(struct device
*cd
,
182 struct device_attribute
*attr
, char *buf
)
184 return sprintf(buf
, "%s\n", USBVISION_VERSION_STRING
);
186 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
188 static ssize_t
show_model(struct device
*cd
,
189 struct device_attribute
*attr
, char *buf
)
191 struct video_device
*vdev
=
192 container_of(cd
, struct video_device
, dev
);
193 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
194 return sprintf(buf
, "%s\n",
195 usbvision_device_data
[usbvision
->DevModel
].ModelString
);
197 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
199 static ssize_t
show_hue(struct device
*cd
,
200 struct device_attribute
*attr
, char *buf
)
202 struct video_device
*vdev
=
203 container_of(cd
, struct video_device
, dev
);
204 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
205 struct v4l2_control ctrl
;
206 ctrl
.id
= V4L2_CID_HUE
;
209 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
210 return sprintf(buf
, "%d\n", ctrl
.value
);
212 static DEVICE_ATTR(hue
, S_IRUGO
, show_hue
, NULL
);
214 static ssize_t
show_contrast(struct device
*cd
,
215 struct device_attribute
*attr
, char *buf
)
217 struct video_device
*vdev
=
218 container_of(cd
, struct video_device
, dev
);
219 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
220 struct v4l2_control ctrl
;
221 ctrl
.id
= V4L2_CID_CONTRAST
;
224 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
225 return sprintf(buf
, "%d\n", ctrl
.value
);
227 static DEVICE_ATTR(contrast
, S_IRUGO
, show_contrast
, NULL
);
229 static ssize_t
show_brightness(struct device
*cd
,
230 struct device_attribute
*attr
, char *buf
)
232 struct video_device
*vdev
=
233 container_of(cd
, struct video_device
, dev
);
234 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
235 struct v4l2_control ctrl
;
236 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
239 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
240 return sprintf(buf
, "%d\n", ctrl
.value
);
242 static DEVICE_ATTR(brightness
, S_IRUGO
, show_brightness
, NULL
);
244 static ssize_t
show_saturation(struct device
*cd
,
245 struct device_attribute
*attr
, char *buf
)
247 struct video_device
*vdev
=
248 container_of(cd
, struct video_device
, dev
);
249 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
250 struct v4l2_control ctrl
;
251 ctrl
.id
= V4L2_CID_SATURATION
;
254 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
255 return sprintf(buf
, "%d\n", ctrl
.value
);
257 static DEVICE_ATTR(saturation
, S_IRUGO
, show_saturation
, NULL
);
259 static ssize_t
show_streaming(struct device
*cd
,
260 struct device_attribute
*attr
, char *buf
)
262 struct video_device
*vdev
=
263 container_of(cd
, struct video_device
, dev
);
264 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
265 return sprintf(buf
, "%s\n",
266 YES_NO(usbvision
->streaming
==Stream_On
?1:0));
268 static DEVICE_ATTR(streaming
, S_IRUGO
, show_streaming
, NULL
);
270 static ssize_t
show_compression(struct device
*cd
,
271 struct device_attribute
*attr
, char *buf
)
273 struct video_device
*vdev
=
274 container_of(cd
, struct video_device
, dev
);
275 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
276 return sprintf(buf
, "%s\n",
277 YES_NO(usbvision
->isocMode
==ISOC_MODE_COMPRESS
));
279 static DEVICE_ATTR(compression
, S_IRUGO
, show_compression
, NULL
);
281 static ssize_t
show_device_bridge(struct device
*cd
,
282 struct device_attribute
*attr
, char *buf
)
284 struct video_device
*vdev
=
285 container_of(cd
, struct video_device
, dev
);
286 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
287 return sprintf(buf
, "%d\n", usbvision
->bridgeType
);
289 static DEVICE_ATTR(bridge
, S_IRUGO
, show_device_bridge
, NULL
);
291 static void usbvision_create_sysfs(struct video_device
*vdev
)
297 res
= device_create_file(&vdev
->dev
, &dev_attr_version
);
300 res
= device_create_file(&vdev
->dev
, &dev_attr_model
);
303 res
= device_create_file(&vdev
->dev
, &dev_attr_hue
);
306 res
= device_create_file(&vdev
->dev
, &dev_attr_contrast
);
309 res
= device_create_file(&vdev
->dev
, &dev_attr_brightness
);
312 res
= device_create_file(&vdev
->dev
, &dev_attr_saturation
);
315 res
= device_create_file(&vdev
->dev
, &dev_attr_streaming
);
318 res
= device_create_file(&vdev
->dev
, &dev_attr_compression
);
321 res
= device_create_file(&vdev
->dev
, &dev_attr_bridge
);
326 dev_err(&vdev
->dev
, "%s error: %d\n", __func__
, res
);
329 static void usbvision_remove_sysfs(struct video_device
*vdev
)
332 device_remove_file(&vdev
->dev
, &dev_attr_version
);
333 device_remove_file(&vdev
->dev
, &dev_attr_model
);
334 device_remove_file(&vdev
->dev
, &dev_attr_hue
);
335 device_remove_file(&vdev
->dev
, &dev_attr_contrast
);
336 device_remove_file(&vdev
->dev
, &dev_attr_brightness
);
337 device_remove_file(&vdev
->dev
, &dev_attr_saturation
);
338 device_remove_file(&vdev
->dev
, &dev_attr_streaming
);
339 device_remove_file(&vdev
->dev
, &dev_attr_compression
);
340 device_remove_file(&vdev
->dev
, &dev_attr_bridge
);
347 * This is part of Video 4 Linux API. The driver can be opened by one
348 * client only (checks internal counter 'usbvision->user'). The procedure
349 * then allocates buffers needed for video processing.
352 static int usbvision_v4l2_open(struct file
*file
)
354 struct usb_usbvision
*usbvision
= video_drvdata(file
);
357 PDEBUG(DBG_IO
, "open");
359 usbvision_reset_powerOffTimer(usbvision
);
364 /* Allocate memory for the scratch ring buffer */
365 errCode
= usbvision_scratch_alloc(usbvision
);
366 if (isocMode
==ISOC_MODE_COMPRESS
) {
367 /* Allocate intermediate decompression buffers
369 errCode
= usbvision_decompress_alloc(usbvision
);
372 /* Deallocate all buffers if trouble */
373 usbvision_scratch_free(usbvision
);
374 usbvision_decompress_free(usbvision
);
378 /* If so far no errors then we shall start the camera */
380 if (usbvision
->power
== 0) {
381 usbvision_power_on(usbvision
);
382 usbvision_i2c_register(usbvision
);
385 /* Send init sequence only once, it's large! */
386 if (!usbvision
->initialized
) {
388 setup_ok
= usbvision_setup(usbvision
,isocMode
);
390 usbvision
->initialized
= 1;
396 usbvision_begin_streaming(usbvision
);
397 errCode
= usbvision_init_isoc(usbvision
);
398 /* device must be initialized before isoc transfer */
399 usbvision_muxsel(usbvision
,0);
403 usbvision_i2c_unregister(usbvision
);
404 usbvision_power_off(usbvision
);
405 usbvision
->initialized
= 0;
411 usbvision_empty_framequeues(usbvision
);
413 PDEBUG(DBG_IO
, "success");
418 * usbvision_v4l2_close()
420 * This is part of Video 4 Linux API. The procedure
421 * stops streaming and deallocates all buffers that were earlier
422 * allocated in usbvision_v4l2_open().
425 static int usbvision_v4l2_close(struct file
*file
)
427 struct usb_usbvision
*usbvision
= video_drvdata(file
);
429 PDEBUG(DBG_IO
, "close");
431 usbvision_audio_off(usbvision
);
432 usbvision_restart_isoc(usbvision
);
433 usbvision_stop_isoc(usbvision
);
435 usbvision_decompress_free(usbvision
);
436 usbvision_frames_free(usbvision
);
437 usbvision_empty_framequeues(usbvision
);
438 usbvision_scratch_free(usbvision
);
443 /* power off in a little while
444 to avoid off/on every close/open short sequences */
445 usbvision_set_powerOffTimer(usbvision
);
446 usbvision
->initialized
= 0;
449 if (usbvision
->remove_pending
) {
450 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
451 usbvision_release(usbvision
);
454 PDEBUG(DBG_IO
, "success");
462 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
465 #ifdef CONFIG_VIDEO_ADV_DEBUG
466 static int vidioc_g_register (struct file
*file
, void *priv
,
467 struct v4l2_dbg_register
*reg
)
469 struct usb_usbvision
*usbvision
= video_drvdata(file
);
472 if (!v4l2_chip_match_host(®
->match
))
474 /* NT100x has a 8-bit register space */
475 errCode
= usbvision_read_reg(usbvision
, reg
->reg
&0xff);
477 dev_err(&usbvision
->vdev
->dev
,
478 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
487 static int vidioc_s_register (struct file
*file
, void *priv
,
488 struct v4l2_dbg_register
*reg
)
490 struct usb_usbvision
*usbvision
= video_drvdata(file
);
493 if (!v4l2_chip_match_host(®
->match
))
495 /* NT100x has a 8-bit register space */
496 errCode
= usbvision_write_reg(usbvision
, reg
->reg
&0xff, reg
->val
);
498 dev_err(&usbvision
->vdev
->dev
,
499 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
507 static int vidioc_querycap (struct file
*file
, void *priv
,
508 struct v4l2_capability
*vc
)
510 struct usb_usbvision
*usbvision
= video_drvdata(file
);
512 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
514 usbvision_device_data
[usbvision
->DevModel
].ModelString
,
516 usb_make_path(usbvision
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
));
517 vc
->version
= USBVISION_DRIVER_VERSION
;
518 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
522 (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
526 static int vidioc_enum_input (struct file
*file
, void *priv
,
527 struct v4l2_input
*vi
)
529 struct usb_usbvision
*usbvision
= video_drvdata(file
);
532 if (vi
->index
>= usbvision
->video_inputs
)
534 if (usbvision
->have_tuner
) {
537 chan
= vi
->index
+ 1; /*skip Television string*/
539 /* Determine the requested input characteristics
540 specific for each usbvision card model */
543 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
544 strcpy(vi
->name
, "White Video Input");
546 strcpy(vi
->name
, "Television");
547 vi
->type
= V4L2_INPUT_TYPE_TUNER
;
550 vi
->std
= USBVISION_NORMS
;
554 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
555 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
556 strcpy(vi
->name
, "Green Video Input");
558 strcpy(vi
->name
, "Composite Video Input");
560 vi
->std
= V4L2_STD_PAL
;
563 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
564 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
565 strcpy(vi
->name
, "Yellow Video Input");
567 strcpy(vi
->name
, "S-Video Input");
569 vi
->std
= V4L2_STD_PAL
;
572 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
573 strcpy(vi
->name
, "Red Video Input");
574 vi
->std
= V4L2_STD_PAL
;
580 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *input
)
582 struct usb_usbvision
*usbvision
= video_drvdata(file
);
584 *input
= usbvision
->ctl_input
;
588 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int input
)
590 struct usb_usbvision
*usbvision
= video_drvdata(file
);
592 if (input
>= usbvision
->video_inputs
)
595 usbvision_muxsel(usbvision
, input
);
596 usbvision_set_input(usbvision
);
597 usbvision_set_output(usbvision
,
599 usbvision
->curheight
);
603 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*id
)
605 struct usb_usbvision
*usbvision
= video_drvdata(file
);
607 usbvision
->tvnormId
=*id
;
609 call_all(usbvision
, core
, s_std
, usbvision
->tvnormId
);
610 /* propagate the change to the decoder */
611 usbvision_muxsel(usbvision
, usbvision
->ctl_input
);
616 static int vidioc_g_tuner (struct file
*file
, void *priv
,
617 struct v4l2_tuner
*vt
)
619 struct usb_usbvision
*usbvision
= video_drvdata(file
);
621 if (!usbvision
->have_tuner
|| vt
->index
) // Only tuner 0
623 if(usbvision
->radio
) {
624 strcpy(vt
->name
, "Radio");
625 vt
->type
= V4L2_TUNER_RADIO
;
627 strcpy(vt
->name
, "Television");
629 /* Let clients fill in the remainder of this struct */
630 call_all(usbvision
, tuner
, g_tuner
, vt
);
635 static int vidioc_s_tuner (struct file
*file
, void *priv
,
636 struct v4l2_tuner
*vt
)
638 struct usb_usbvision
*usbvision
= video_drvdata(file
);
640 // Only no or one tuner for now
641 if (!usbvision
->have_tuner
|| vt
->index
)
643 /* let clients handle this */
644 call_all(usbvision
, tuner
, s_tuner
, vt
);
649 static int vidioc_g_frequency (struct file
*file
, void *priv
,
650 struct v4l2_frequency
*freq
)
652 struct usb_usbvision
*usbvision
= video_drvdata(file
);
654 freq
->tuner
= 0; // Only one tuner
655 if(usbvision
->radio
) {
656 freq
->type
= V4L2_TUNER_RADIO
;
658 freq
->type
= V4L2_TUNER_ANALOG_TV
;
660 freq
->frequency
= usbvision
->freq
;
665 static int vidioc_s_frequency (struct file
*file
, void *priv
,
666 struct v4l2_frequency
*freq
)
668 struct usb_usbvision
*usbvision
= video_drvdata(file
);
670 // Only no or one tuner for now
671 if (!usbvision
->have_tuner
|| freq
->tuner
)
674 usbvision
->freq
= freq
->frequency
;
675 call_all(usbvision
, tuner
, s_frequency
, freq
);
680 static int vidioc_g_audio (struct file
*file
, void *priv
, struct v4l2_audio
*a
)
682 struct usb_usbvision
*usbvision
= video_drvdata(file
);
684 if(usbvision
->radio
) {
685 strcpy(a
->name
,"Radio");
687 strcpy(a
->name
, "TV");
693 static int vidioc_s_audio (struct file
*file
, void *fh
,
694 struct v4l2_audio
*a
)
703 static int vidioc_queryctrl (struct file
*file
, void *priv
,
704 struct v4l2_queryctrl
*ctrl
)
706 struct usb_usbvision
*usbvision
= video_drvdata(file
);
708 call_all(usbvision
, core
, queryctrl
, ctrl
);
716 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
717 struct v4l2_control
*ctrl
)
719 struct usb_usbvision
*usbvision
= video_drvdata(file
);
720 call_all(usbvision
, core
, g_ctrl
, ctrl
);
725 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
726 struct v4l2_control
*ctrl
)
728 struct usb_usbvision
*usbvision
= video_drvdata(file
);
729 call_all(usbvision
, core
, s_ctrl
, ctrl
);
734 static int vidioc_reqbufs (struct file
*file
,
735 void *priv
, struct v4l2_requestbuffers
*vr
)
737 struct usb_usbvision
*usbvision
= video_drvdata(file
);
740 RESTRICT_TO_RANGE(vr
->count
,1,USBVISION_NUMFRAMES
);
742 /* Check input validity:
743 the user must do a VIDEO CAPTURE and MMAP method. */
744 if (vr
->memory
!= V4L2_MEMORY_MMAP
)
747 if(usbvision
->streaming
== Stream_On
) {
748 if ((ret
= usbvision_stream_interrupt(usbvision
)))
752 usbvision_frames_free(usbvision
);
753 usbvision_empty_framequeues(usbvision
);
754 vr
->count
= usbvision_frames_alloc(usbvision
,vr
->count
);
756 usbvision
->curFrame
= NULL
;
761 static int vidioc_querybuf (struct file
*file
,
762 void *priv
, struct v4l2_buffer
*vb
)
764 struct usb_usbvision
*usbvision
= video_drvdata(file
);
765 struct usbvision_frame
*frame
;
767 /* FIXME : must control
768 that buffers are mapped (VIDIOC_REQBUFS has been called) */
769 if(vb
->index
>=usbvision
->num_frames
) {
772 /* Updating the corresponding frame state */
774 frame
= &usbvision
->frame
[vb
->index
];
775 if(frame
->grabstate
>= FrameState_Ready
)
776 vb
->flags
|= V4L2_BUF_FLAG_QUEUED
;
777 if(frame
->grabstate
>= FrameState_Done
)
778 vb
->flags
|= V4L2_BUF_FLAG_DONE
;
779 if(frame
->grabstate
== FrameState_Unused
)
780 vb
->flags
|= V4L2_BUF_FLAG_MAPPED
;
781 vb
->memory
= V4L2_MEMORY_MMAP
;
783 vb
->m
.offset
= vb
->index
*PAGE_ALIGN(usbvision
->max_frame_size
);
785 vb
->memory
= V4L2_MEMORY_MMAP
;
786 vb
->field
= V4L2_FIELD_NONE
;
787 vb
->length
= usbvision
->curwidth
*
788 usbvision
->curheight
*
789 usbvision
->palette
.bytes_per_pixel
;
790 vb
->timestamp
= usbvision
->frame
[vb
->index
].timestamp
;
791 vb
->sequence
= usbvision
->frame
[vb
->index
].sequence
;
795 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
797 struct usb_usbvision
*usbvision
= video_drvdata(file
);
798 struct usbvision_frame
*frame
;
799 unsigned long lock_flags
;
801 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
802 if(vb
->index
>=usbvision
->num_frames
) {
806 frame
= &usbvision
->frame
[vb
->index
];
808 if (frame
->grabstate
!= FrameState_Unused
) {
812 /* Mark it as ready and enqueue frame */
813 frame
->grabstate
= FrameState_Ready
;
814 frame
->scanstate
= ScanState_Scanning
;
815 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
817 vb
->flags
&= ~V4L2_BUF_FLAG_DONE
;
819 /* set v4l2_format index */
820 frame
->v4l2_format
= usbvision
->palette
;
822 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
823 list_add_tail(&usbvision
->frame
[vb
->index
].frame
, &usbvision
->inqueue
);
824 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
829 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
831 struct usb_usbvision
*usbvision
= video_drvdata(file
);
833 struct usbvision_frame
*f
;
834 unsigned long lock_flags
;
836 if (list_empty(&(usbvision
->outqueue
))) {
837 if (usbvision
->streaming
== Stream_Idle
)
839 ret
= wait_event_interruptible
840 (usbvision
->wait_frame
,
841 !list_empty(&(usbvision
->outqueue
)));
846 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
847 f
= list_entry(usbvision
->outqueue
.next
,
848 struct usbvision_frame
, frame
);
849 list_del(usbvision
->outqueue
.next
);
850 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
852 f
->grabstate
= FrameState_Unused
;
854 vb
->memory
= V4L2_MEMORY_MMAP
;
855 vb
->flags
= V4L2_BUF_FLAG_MAPPED
|
856 V4L2_BUF_FLAG_QUEUED
|
858 vb
->index
= f
->index
;
859 vb
->sequence
= f
->sequence
;
860 vb
->timestamp
= f
->timestamp
;
861 vb
->field
= V4L2_FIELD_NONE
;
862 vb
->bytesused
= f
->scanlength
;
867 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
869 struct usb_usbvision
*usbvision
= video_drvdata(file
);
871 usbvision
->streaming
= Stream_On
;
872 call_all(usbvision
, video
, s_stream
, 1);
877 static int vidioc_streamoff(struct file
*file
,
878 void *priv
, enum v4l2_buf_type type
)
880 struct usb_usbvision
*usbvision
= video_drvdata(file
);
882 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
885 if(usbvision
->streaming
== Stream_On
) {
886 usbvision_stream_interrupt(usbvision
);
887 /* Stop all video streamings */
888 call_all(usbvision
, video
, s_stream
, 0);
890 usbvision_empty_framequeues(usbvision
);
895 static int vidioc_enum_fmt_vid_cap (struct file
*file
, void *priv
,
896 struct v4l2_fmtdesc
*vfd
)
898 if(vfd
->index
>=USBVISION_SUPPORTED_PALETTES
-1) {
901 strcpy(vfd
->description
,usbvision_v4l2_format
[vfd
->index
].desc
);
902 vfd
->pixelformat
= usbvision_v4l2_format
[vfd
->index
].format
;
906 static int vidioc_g_fmt_vid_cap (struct file
*file
, void *priv
,
907 struct v4l2_format
*vf
)
909 struct usb_usbvision
*usbvision
= video_drvdata(file
);
910 vf
->fmt
.pix
.width
= usbvision
->curwidth
;
911 vf
->fmt
.pix
.height
= usbvision
->curheight
;
912 vf
->fmt
.pix
.pixelformat
= usbvision
->palette
.format
;
913 vf
->fmt
.pix
.bytesperline
=
914 usbvision
->curwidth
*usbvision
->palette
.bytes_per_pixel
;
915 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*usbvision
->curheight
;
916 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
917 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
922 static int vidioc_try_fmt_vid_cap (struct file
*file
, void *priv
,
923 struct v4l2_format
*vf
)
925 struct usb_usbvision
*usbvision
= video_drvdata(file
);
928 /* Find requested format in available ones */
929 for(formatIdx
=0;formatIdx
<USBVISION_SUPPORTED_PALETTES
;formatIdx
++) {
930 if(vf
->fmt
.pix
.pixelformat
==
931 usbvision_v4l2_format
[formatIdx
].format
) {
932 usbvision
->palette
= usbvision_v4l2_format
[formatIdx
];
937 if(formatIdx
== USBVISION_SUPPORTED_PALETTES
) {
940 RESTRICT_TO_RANGE(vf
->fmt
.pix
.width
, MIN_FRAME_WIDTH
, MAX_FRAME_WIDTH
);
941 RESTRICT_TO_RANGE(vf
->fmt
.pix
.height
, MIN_FRAME_HEIGHT
, MAX_FRAME_HEIGHT
);
943 vf
->fmt
.pix
.bytesperline
= vf
->fmt
.pix
.width
*
944 usbvision
->palette
.bytes_per_pixel
;
945 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*vf
->fmt
.pix
.height
;
950 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
951 struct v4l2_format
*vf
)
953 struct usb_usbvision
*usbvision
= video_drvdata(file
);
956 if( 0 != (ret
=vidioc_try_fmt_vid_cap (file
, priv
, vf
)) ) {
960 /* stop io in case it is already in progress */
961 if(usbvision
->streaming
== Stream_On
) {
962 if ((ret
= usbvision_stream_interrupt(usbvision
)))
965 usbvision_frames_free(usbvision
);
966 usbvision_empty_framequeues(usbvision
);
968 usbvision
->curFrame
= NULL
;
970 /* by now we are committed to the new data... */
971 usbvision_set_output(usbvision
, vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
);
976 static ssize_t
usbvision_v4l2_read(struct file
*file
, char __user
*buf
,
977 size_t count
, loff_t
*ppos
)
979 struct usb_usbvision
*usbvision
= video_drvdata(file
);
980 int noblock
= file
->f_flags
& O_NONBLOCK
;
981 unsigned long lock_flags
;
984 struct usbvision_frame
*frame
;
986 PDEBUG(DBG_IO
, "%s: %ld bytes, noblock=%d", __func__
,
987 (unsigned long)count
, noblock
);
989 if (!USBVISION_IS_OPERATIONAL(usbvision
) || (buf
== NULL
))
992 /* This entry point is compatible with the mmap routines
993 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
994 to get frames or call read on the device. */
995 if(!usbvision
->num_frames
) {
996 /* First, allocate some frames to work with
997 if this has not been done with VIDIOC_REQBUF */
998 usbvision_frames_free(usbvision
);
999 usbvision_empty_framequeues(usbvision
);
1000 usbvision_frames_alloc(usbvision
,USBVISION_NUMFRAMES
);
1003 if(usbvision
->streaming
!= Stream_On
) {
1004 /* no stream is running, make it running ! */
1005 usbvision
->streaming
= Stream_On
;
1006 call_all(usbvision
, video
, s_stream
, 1);
1009 /* Then, enqueue as many frames as possible
1010 (like a user of VIDIOC_QBUF would do) */
1011 for(i
=0;i
<usbvision
->num_frames
;i
++) {
1012 frame
= &usbvision
->frame
[i
];
1013 if(frame
->grabstate
== FrameState_Unused
) {
1014 /* Mark it as ready and enqueue frame */
1015 frame
->grabstate
= FrameState_Ready
;
1016 frame
->scanstate
= ScanState_Scanning
;
1017 /* Accumulated in usbvision_parse_data() */
1018 frame
->scanlength
= 0;
1020 /* set v4l2_format index */
1021 frame
->v4l2_format
= usbvision
->palette
;
1023 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1024 list_add_tail(&frame
->frame
, &usbvision
->inqueue
);
1025 spin_unlock_irqrestore(&usbvision
->queue_lock
,
1030 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1031 if (list_empty(&(usbvision
->outqueue
))) {
1035 ret
= wait_event_interruptible
1036 (usbvision
->wait_frame
,
1037 !list_empty(&(usbvision
->outqueue
)));
1042 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1043 frame
= list_entry(usbvision
->outqueue
.next
,
1044 struct usbvision_frame
, frame
);
1045 list_del(usbvision
->outqueue
.next
);
1046 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1048 /* An error returns an empty frame */
1049 if (frame
->grabstate
== FrameState_Error
) {
1050 frame
->bytes_read
= 0;
1054 PDEBUG(DBG_IO
, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1056 frame
->index
, frame
->bytes_read
, frame
->scanlength
);
1058 /* copy bytes to user space; we allow for partials reads */
1059 if ((count
+ frame
->bytes_read
) > (unsigned long)frame
->scanlength
)
1060 count
= frame
->scanlength
- frame
->bytes_read
;
1062 if (copy_to_user(buf
, frame
->data
+ frame
->bytes_read
, count
)) {
1066 frame
->bytes_read
+= count
;
1067 PDEBUG(DBG_IO
, "%s: {copy} count used=%ld, new bytes_read=%ld",
1069 (unsigned long)count
, frame
->bytes_read
);
1071 /* For now, forget the frame if it has not been read in one shot. */
1072 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1073 frame
->bytes_read
= 0;
1075 /* Mark it as available to be used again. */
1076 frame
->grabstate
= FrameState_Unused
;
1082 static int usbvision_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1084 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1085 start
= vma
->vm_start
;
1088 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1090 PDEBUG(DBG_MMAP
, "mmap");
1092 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1096 if (!(vma
->vm_flags
& VM_WRITE
) ||
1097 size
!= PAGE_ALIGN(usbvision
->max_frame_size
)) {
1101 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
1102 if (((PAGE_ALIGN(usbvision
->max_frame_size
)*i
) >> PAGE_SHIFT
) ==
1106 if (i
== usbvision
->num_frames
) {
1108 "mmap: user supplied mapping address is out of range");
1112 /* VM_IO is eventually going to replace PageReserved altogether */
1113 vma
->vm_flags
|= VM_IO
;
1114 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1116 pos
= usbvision
->frame
[i
].data
;
1119 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1120 PDEBUG(DBG_MMAP
, "mmap: vm_insert_page failed");
1133 * Here comes the stuff for radio on usbvision based devices
1136 static int usbvision_radio_open(struct file
*file
)
1138 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1141 PDEBUG(DBG_IO
, "%s:", __func__
);
1143 if (usbvision
->user
) {
1144 dev_err(&usbvision
->rdev
->dev
,
1145 "%s: Someone tried to open an already opened USBVision Radio!\n",
1151 usbvision_reset_powerOffTimer(usbvision
);
1152 if (usbvision
->power
== 0) {
1153 usbvision_power_on(usbvision
);
1154 usbvision_i2c_register(usbvision
);
1158 /* Alternate interface 1 is is the biggest frame size */
1159 errCode
= usbvision_set_alternate(usbvision
);
1161 usbvision
->last_error
= errCode
;
1166 // If so far no errors then we shall start the radio
1167 usbvision
->radio
= 1;
1168 call_all(usbvision
, tuner
, s_radio
);
1169 usbvision_set_audio(usbvision
, USBVISION_AUDIO_RADIO
);
1174 if (PowerOnAtOpen
) {
1175 usbvision_i2c_unregister(usbvision
);
1176 usbvision_power_off(usbvision
);
1177 usbvision
->initialized
= 0;
1185 static int usbvision_radio_close(struct file
*file
)
1187 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1192 /* Set packet size to 0 */
1193 usbvision
->ifaceAlt
=0;
1194 errCode
= usb_set_interface(usbvision
->dev
, usbvision
->iface
,
1195 usbvision
->ifaceAlt
);
1197 usbvision_audio_off(usbvision
);
1201 if (PowerOnAtOpen
) {
1202 usbvision_set_powerOffTimer(usbvision
);
1203 usbvision
->initialized
= 0;
1206 if (usbvision
->remove_pending
) {
1207 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
1208 usbvision_release(usbvision
);
1211 PDEBUG(DBG_IO
, "success");
1216 // Video registration stuff
1220 static const struct v4l2_file_operations usbvision_fops
= {
1221 .owner
= THIS_MODULE
,
1222 .open
= usbvision_v4l2_open
,
1223 .release
= usbvision_v4l2_close
,
1224 .read
= usbvision_v4l2_read
,
1225 .mmap
= usbvision_v4l2_mmap
,
1226 .unlocked_ioctl
= video_ioctl2
,
1227 /* .poll = video_poll, */
1230 static const struct v4l2_ioctl_ops usbvision_ioctl_ops
= {
1231 .vidioc_querycap
= vidioc_querycap
,
1232 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1233 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1234 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1235 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1236 .vidioc_reqbufs
= vidioc_reqbufs
,
1237 .vidioc_querybuf
= vidioc_querybuf
,
1238 .vidioc_qbuf
= vidioc_qbuf
,
1239 .vidioc_dqbuf
= vidioc_dqbuf
,
1240 .vidioc_s_std
= vidioc_s_std
,
1241 .vidioc_enum_input
= vidioc_enum_input
,
1242 .vidioc_g_input
= vidioc_g_input
,
1243 .vidioc_s_input
= vidioc_s_input
,
1244 .vidioc_queryctrl
= vidioc_queryctrl
,
1245 .vidioc_g_audio
= vidioc_g_audio
,
1246 .vidioc_s_audio
= vidioc_s_audio
,
1247 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1248 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1249 .vidioc_streamon
= vidioc_streamon
,
1250 .vidioc_streamoff
= vidioc_streamoff
,
1251 .vidioc_g_tuner
= vidioc_g_tuner
,
1252 .vidioc_s_tuner
= vidioc_s_tuner
,
1253 .vidioc_g_frequency
= vidioc_g_frequency
,
1254 .vidioc_s_frequency
= vidioc_s_frequency
,
1255 #ifdef CONFIG_VIDEO_ADV_DEBUG
1256 .vidioc_g_register
= vidioc_g_register
,
1257 .vidioc_s_register
= vidioc_s_register
,
1261 static struct video_device usbvision_video_template
= {
1262 .fops
= &usbvision_fops
,
1263 .ioctl_ops
= &usbvision_ioctl_ops
,
1264 .name
= "usbvision-video",
1265 .release
= video_device_release
,
1266 .tvnorms
= USBVISION_NORMS
,
1267 .current_norm
= V4L2_STD_PAL
1272 static const struct v4l2_file_operations usbvision_radio_fops
= {
1273 .owner
= THIS_MODULE
,
1274 .open
= usbvision_radio_open
,
1275 .release
= usbvision_radio_close
,
1276 .unlocked_ioctl
= video_ioctl2
,
1279 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops
= {
1280 .vidioc_querycap
= vidioc_querycap
,
1281 .vidioc_enum_input
= vidioc_enum_input
,
1282 .vidioc_g_input
= vidioc_g_input
,
1283 .vidioc_s_input
= vidioc_s_input
,
1284 .vidioc_queryctrl
= vidioc_queryctrl
,
1285 .vidioc_g_audio
= vidioc_g_audio
,
1286 .vidioc_s_audio
= vidioc_s_audio
,
1287 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1288 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1289 .vidioc_g_tuner
= vidioc_g_tuner
,
1290 .vidioc_s_tuner
= vidioc_s_tuner
,
1291 .vidioc_g_frequency
= vidioc_g_frequency
,
1292 .vidioc_s_frequency
= vidioc_s_frequency
,
1295 static struct video_device usbvision_radio_template
= {
1296 .fops
= &usbvision_radio_fops
,
1297 .name
= "usbvision-radio",
1298 .release
= video_device_release
,
1299 .ioctl_ops
= &usbvision_radio_ioctl_ops
,
1301 .tvnorms
= USBVISION_NORMS
,
1302 .current_norm
= V4L2_STD_PAL
1306 static struct video_device
*usbvision_vdev_init(struct usb_usbvision
*usbvision
,
1307 struct video_device
*vdev_template
,
1310 struct usb_device
*usb_dev
= usbvision
->dev
;
1311 struct video_device
*vdev
;
1313 if (usb_dev
== NULL
) {
1314 dev_err(&usbvision
->dev
->dev
,
1315 "%s: usbvision->dev is not set\n", __func__
);
1319 vdev
= video_device_alloc();
1323 *vdev
= *vdev_template
;
1324 vdev
->lock
= &usbvision
->v4l2_lock
;
1325 vdev
->v4l2_dev
= &usbvision
->v4l2_dev
;
1326 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s", name
);
1327 video_set_drvdata(vdev
, usbvision
);
1331 // unregister video4linux devices
1332 static void usbvision_unregister_video(struct usb_usbvision
*usbvision
)
1335 if (usbvision
->rdev
) {
1336 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1337 video_device_node_name(usbvision
->rdev
));
1338 if (video_is_registered(usbvision
->rdev
)) {
1339 video_unregister_device(usbvision
->rdev
);
1341 video_device_release(usbvision
->rdev
);
1343 usbvision
->rdev
= NULL
;
1347 if (usbvision
->vdev
) {
1348 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1349 video_device_node_name(usbvision
->vdev
));
1350 if (video_is_registered(usbvision
->vdev
)) {
1351 video_unregister_device(usbvision
->vdev
);
1353 video_device_release(usbvision
->vdev
);
1355 usbvision
->vdev
= NULL
;
1359 // register video4linux devices
1360 static int __devinit
usbvision_register_video(struct usb_usbvision
*usbvision
)
1363 usbvision
->vdev
= usbvision_vdev_init(usbvision
,
1364 &usbvision_video_template
,
1366 if (usbvision
->vdev
== NULL
) {
1369 if (video_register_device(usbvision
->vdev
,
1374 printk(KERN_INFO
"USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1375 usbvision
->nr
, video_device_node_name(usbvision
->vdev
));
1378 if (usbvision_device_data
[usbvision
->DevModel
].Radio
) {
1379 // usbvision has radio
1380 usbvision
->rdev
= usbvision_vdev_init(usbvision
,
1381 &usbvision_radio_template
,
1383 if (usbvision
->rdev
== NULL
) {
1386 if (video_register_device(usbvision
->rdev
,
1391 printk(KERN_INFO
"USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1392 usbvision
->nr
, video_device_node_name(usbvision
->rdev
));
1398 dev_err(&usbvision
->dev
->dev
,
1399 "USBVision[%d]: video_register_device() failed\n",
1401 usbvision_unregister_video(usbvision
);
1408 * This code allocates the struct usb_usbvision.
1409 * It is filled with default values.
1411 * Returns NULL on error, a pointer to usb_usbvision else.
1414 static struct usb_usbvision
*usbvision_alloc(struct usb_device
*dev
,
1415 struct usb_interface
*intf
)
1417 struct usb_usbvision
*usbvision
;
1419 usbvision
= kzalloc(sizeof(struct usb_usbvision
), GFP_KERNEL
);
1420 if (usbvision
== NULL
)
1423 usbvision
->dev
= dev
;
1424 if (v4l2_device_register(&intf
->dev
, &usbvision
->v4l2_dev
))
1427 mutex_init(&usbvision
->v4l2_lock
);
1429 // prepare control urb for control messages during interrupts
1430 usbvision
->ctrlUrb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
1431 if (usbvision
->ctrlUrb
== NULL
)
1433 init_waitqueue_head(&usbvision
->ctrlUrb_wq
);
1435 usbvision_init_powerOffTimer(usbvision
);
1440 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1447 * usbvision_release()
1449 * This code does final release of struct usb_usbvision. This happens
1450 * after the device is disconnected -and- all clients closed their files.
1453 static void usbvision_release(struct usb_usbvision
*usbvision
)
1455 PDEBUG(DBG_PROBE
, "");
1457 usbvision_reset_powerOffTimer(usbvision
);
1459 usbvision
->initialized
= 0;
1461 usbvision_remove_sysfs(usbvision
->vdev
);
1462 usbvision_unregister_video(usbvision
);
1464 if (usbvision
->ctrlUrb
) {
1465 usb_free_urb(usbvision
->ctrlUrb
);
1468 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1471 PDEBUG(DBG_PROBE
, "success");
1475 /*********************** usb interface **********************************/
1477 static void usbvision_configure_video(struct usb_usbvision
*usbvision
)
1481 if (usbvision
== NULL
)
1484 model
= usbvision
->DevModel
;
1485 usbvision
->palette
= usbvision_v4l2_format
[2]; // V4L2_PIX_FMT_RGB24;
1487 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2_override
) {
1488 usbvision
->Vin_Reg2_Preset
=
1489 usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2
;
1491 usbvision
->Vin_Reg2_Preset
= 0;
1494 usbvision
->tvnormId
= usbvision_device_data
[model
].VideoNorm
;
1496 usbvision
->video_inputs
= usbvision_device_data
[model
].VideoChannels
;
1497 usbvision
->ctl_input
= 0;
1499 /* This should be here to make i2c clients to be able to register */
1500 /* first switch off audio */
1501 usbvision_audio_off(usbvision
);
1502 if (!PowerOnAtOpen
) {
1503 /* and then power up the noisy tuner */
1504 usbvision_power_on(usbvision
);
1505 usbvision_i2c_register(usbvision
);
1512 * This procedure queries device descriptor and accepts the interface
1513 * if it looks like USBVISION video device
1516 static int __devinit
usbvision_probe(struct usb_interface
*intf
,
1517 const struct usb_device_id
*devid
)
1519 struct usb_device
*dev
= usb_get_dev(interface_to_usbdev(intf
));
1520 struct usb_interface
*uif
;
1521 __u8 ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
1522 const struct usb_host_interface
*interface
;
1523 struct usb_usbvision
*usbvision
= NULL
;
1524 const struct usb_endpoint_descriptor
*endpoint
;
1527 PDEBUG(DBG_PROBE
, "VID=%#04x, PID=%#04x, ifnum=%u",
1528 dev
->descriptor
.idVendor
,
1529 dev
->descriptor
.idProduct
, ifnum
);
1531 model
= devid
->driver_info
;
1532 if ( (model
<0) || (model
>=usbvision_device_data_size
) ) {
1533 PDEBUG(DBG_PROBE
, "model out of bounds %d",model
);
1536 printk(KERN_INFO
"%s: %s found\n", __func__
,
1537 usbvision_device_data
[model
].ModelString
);
1539 if (usbvision_device_data
[model
].Interface
>= 0) {
1540 interface
= &dev
->actconfig
->interface
[usbvision_device_data
[model
].Interface
]->altsetting
[0];
1542 interface
= &dev
->actconfig
->interface
[ifnum
]->altsetting
[0];
1544 endpoint
= &interface
->endpoint
[1].desc
;
1545 if (!usb_endpoint_xfer_isoc(endpoint
)) {
1546 dev_err(&intf
->dev
, "%s: interface %d. has non-ISO endpoint!\n",
1548 dev_err(&intf
->dev
, "%s: Endpoint attributes %d",
1549 __func__
, endpoint
->bmAttributes
);
1552 if (usb_endpoint_dir_out(endpoint
)) {
1553 dev_err(&intf
->dev
, "%s: interface %d. has ISO OUT endpoint!\n",
1558 usbvision
= usbvision_alloc(dev
, intf
);
1559 if (usbvision
== NULL
) {
1560 dev_err(&intf
->dev
, "%s: couldn't allocate USBVision struct\n", __func__
);
1564 if (dev
->descriptor
.bNumConfigurations
> 1) {
1565 usbvision
->bridgeType
= BRIDGE_NT1004
;
1566 } else if (model
== DAZZLE_DVC_90_REV_1_SECAM
) {
1567 usbvision
->bridgeType
= BRIDGE_NT1005
;
1569 usbvision
->bridgeType
= BRIDGE_NT1003
;
1571 PDEBUG(DBG_PROBE
, "bridgeType %d", usbvision
->bridgeType
);
1573 /* compute alternate max packet sizes */
1574 uif
= dev
->actconfig
->interface
[0];
1576 usbvision
->num_alt
=uif
->num_altsetting
;
1577 PDEBUG(DBG_PROBE
, "Alternate settings: %i",usbvision
->num_alt
);
1578 usbvision
->alt_max_pkt_size
= kmalloc(32*
1579 usbvision
->num_alt
,GFP_KERNEL
);
1580 if (usbvision
->alt_max_pkt_size
== NULL
) {
1581 dev_err(&intf
->dev
, "usbvision: out of memory!\n");
1585 for (i
= 0; i
< usbvision
->num_alt
; i
++) {
1586 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1588 usbvision
->alt_max_pkt_size
[i
] =
1589 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1590 PDEBUG(DBG_PROBE
, "Alternate setting %i, max size= %i",i
,
1591 usbvision
->alt_max_pkt_size
[i
]);
1595 usbvision
->nr
= usbvision_nr
++;
1597 usbvision
->have_tuner
= usbvision_device_data
[model
].Tuner
;
1598 if (usbvision
->have_tuner
) {
1599 usbvision
->tuner_type
= usbvision_device_data
[model
].TunerType
;
1602 usbvision
->DevModel
= model
;
1603 usbvision
->remove_pending
= 0;
1604 usbvision
->iface
= ifnum
;
1605 usbvision
->ifaceAlt
= 0;
1606 usbvision
->video_endp
= endpoint
->bEndpointAddress
;
1607 usbvision
->isocPacketSize
= 0;
1608 usbvision
->usb_bandwidth
= 0;
1609 usbvision
->user
= 0;
1610 usbvision
->streaming
= Stream_Off
;
1611 usbvision_configure_video(usbvision
);
1612 usbvision_register_video(usbvision
);
1614 usbvision_create_sysfs(usbvision
->vdev
);
1616 PDEBUG(DBG_PROBE
, "success");
1622 * usbvision_disconnect()
1624 * This procedure stops all driver activity, deallocates interface-private
1625 * structure (pointed by 'ptr') and after that driver should be removable
1626 * with no ill consequences.
1629 static void __devexit
usbvision_disconnect(struct usb_interface
*intf
)
1631 struct usb_usbvision
*usbvision
= to_usbvision(usb_get_intfdata(intf
));
1633 PDEBUG(DBG_PROBE
, "");
1635 if (usbvision
== NULL
) {
1636 pr_err("%s: usb_get_intfdata() failed\n", __func__
);
1640 mutex_lock(&usbvision
->v4l2_lock
);
1642 // At this time we ask to cancel outstanding URBs
1643 usbvision_stop_isoc(usbvision
);
1645 v4l2_device_disconnect(&usbvision
->v4l2_dev
);
1647 if (usbvision
->power
) {
1648 usbvision_i2c_unregister(usbvision
);
1649 usbvision_power_off(usbvision
);
1651 usbvision
->remove_pending
= 1; // Now all ISO data will be ignored
1653 usb_put_dev(usbvision
->dev
);
1654 usbvision
->dev
= NULL
; // USB device is no more
1656 mutex_unlock(&usbvision
->v4l2_lock
);
1658 if (usbvision
->user
) {
1659 printk(KERN_INFO
"%s: In use, disconnect pending\n",
1661 wake_up_interruptible(&usbvision
->wait_frame
);
1662 wake_up_interruptible(&usbvision
->wait_stream
);
1664 usbvision_release(usbvision
);
1667 PDEBUG(DBG_PROBE
, "success");
1670 static struct usb_driver usbvision_driver
= {
1671 .name
= "usbvision",
1672 .id_table
= usbvision_table
,
1673 .probe
= usbvision_probe
,
1674 .disconnect
= __devexit_p(usbvision_disconnect
),
1680 * This code is run to initialize the driver.
1683 static int __init
usbvision_init(void)
1687 PDEBUG(DBG_PROBE
, "");
1689 PDEBUG(DBG_IO
, "IO debugging is enabled [video]");
1690 PDEBUG(DBG_PROBE
, "PROBE debugging is enabled [video]");
1691 PDEBUG(DBG_MMAP
, "MMAP debugging is enabled [video]");
1693 /* disable planar mode support unless compression enabled */
1694 if (isocMode
!= ISOC_MODE_COMPRESS
) {
1695 // FIXME : not the right way to set supported flag
1696 usbvision_v4l2_format
[6].supported
= 0; // V4L2_PIX_FMT_YVU420
1697 usbvision_v4l2_format
[7].supported
= 0; // V4L2_PIX_FMT_YUV422P
1700 errCode
= usb_register(&usbvision_driver
);
1703 printk(KERN_INFO DRIVER_DESC
" : " USBVISION_VERSION_STRING
"\n");
1704 PDEBUG(DBG_PROBE
, "success");
1709 static void __exit
usbvision_exit(void)
1711 PDEBUG(DBG_PROBE
, "");
1713 usb_deregister(&usbvision_driver
);
1714 PDEBUG(DBG_PROBE
, "success");
1717 module_init(usbvision_init
);
1718 module_exit(usbvision_exit
);
1721 * Overrides for Emacs so that we follow Linus's tabbing style.
1722 * ---------------------------------------------------------------------------