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/kernel.h>
49 #include <linux/list.h>
50 #include <linux/timer.h>
51 #include <linux/slab.h>
53 #include <linux/highmem.h>
54 #include <linux/vmalloc.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/spinlock.h>
59 #include <linux/videodev2.h>
60 #include <linux/i2c.h>
62 #include <media/i2c/saa7115.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include <media/v4l2-event.h>
66 #include <media/tuner.h>
68 #include <linux/workqueue.h>
70 #include "usbvision.h"
71 #include "usbvision-cards.h"
73 #define DRIVER_AUTHOR \
74 "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_VERSION_STRING "0.9.11"
82 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
85 #ifdef USBVISION_DEBUG
86 #define PDEBUG(level, fmt, args...) { \
87 if (video_debug & (level)) \
88 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
89 __func__, __LINE__ , ## args); \
92 #define PDEBUG(level, fmt, args...) do {} while (0)
95 #define DBG_IO (1 << 1)
96 #define DBG_PROBE (1 << 2)
97 #define DBG_MMAP (1 << 3)
99 /* String operations */
100 #define rmspace(str) while (*str == ' ') str++;
101 #define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
104 /* sequential number of usbvision device */
105 static int usbvision_nr
;
107 static struct usbvision_v4l2_format_st usbvision_v4l2_format
[] = {
108 { 1, 1, 8, V4L2_PIX_FMT_GREY
, "GREY" },
109 { 1, 2, 16, V4L2_PIX_FMT_RGB565
, "RGB565" },
110 { 1, 3, 24, V4L2_PIX_FMT_RGB24
, "RGB24" },
111 { 1, 4, 32, V4L2_PIX_FMT_RGB32
, "RGB32" },
112 { 1, 2, 16, V4L2_PIX_FMT_RGB555
, "RGB555" },
113 { 1, 2, 16, V4L2_PIX_FMT_YUYV
, "YUV422" },
114 { 1, 2, 12, V4L2_PIX_FMT_YVU420
, "YUV420P" }, /* 1.5 ! */
115 { 1, 2, 16, V4L2_PIX_FMT_YUV422P
, "YUV422P" }
118 /* Function prototypes */
119 static void usbvision_release(struct usb_usbvision
*usbvision
);
121 /* Default initialization of device driver parameters */
122 /* Set the default format for ISOC endpoint */
123 static int isoc_mode
= ISOC_MODE_COMPRESS
;
124 /* Set the default Debug Mode of the device driver */
125 static int video_debug
;
126 /* Sequential Number of Video Device */
127 static int video_nr
= -1;
128 /* Sequential Number of Radio Device */
129 static int radio_nr
= -1;
131 /* Grab parameters for the device driver */
133 /* Showing parameters under SYSFS */
134 module_param(isoc_mode
, int, 0444);
135 module_param(video_debug
, int, 0444);
136 module_param(video_nr
, int, 0444);
137 module_param(radio_nr
, int, 0444);
139 MODULE_PARM_DESC(isoc_mode
, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
140 MODULE_PARM_DESC(video_debug
, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
141 MODULE_PARM_DESC(video_nr
, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
142 MODULE_PARM_DESC(radio_nr
, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
146 MODULE_AUTHOR(DRIVER_AUTHOR
);
147 MODULE_DESCRIPTION(DRIVER_DESC
);
148 MODULE_LICENSE(DRIVER_LICENSE
);
149 MODULE_VERSION(USBVISION_VERSION_STRING
);
150 MODULE_ALIAS(DRIVER_ALIAS
);
153 /*****************************************************************************/
154 /* SYSFS Code - Copied from the stv680.c usb module. */
155 /* Device information is located at /sys/class/video4linux/video0 */
156 /* Device parameters information is located at /sys/module/usbvision */
157 /* Device USB Information is located at */
158 /* /sys/bus/usb/drivers/USBVision Video Grabber */
159 /*****************************************************************************/
161 #define YES_NO(x) ((x) ? "Yes" : "No")
163 static inline struct usb_usbvision
*cd_to_usbvision(struct device
*cd
)
165 struct video_device
*vdev
= to_video_device(cd
);
166 return video_get_drvdata(vdev
);
169 static ssize_t
show_version(struct device
*cd
,
170 struct device_attribute
*attr
, char *buf
)
172 return sprintf(buf
, "%s\n", USBVISION_VERSION_STRING
);
174 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
176 static ssize_t
show_model(struct device
*cd
,
177 struct device_attribute
*attr
, char *buf
)
179 struct video_device
*vdev
= to_video_device(cd
);
180 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
181 return sprintf(buf
, "%s\n",
182 usbvision_device_data
[usbvision
->dev_model
].model_string
);
184 static DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
186 static ssize_t
show_hue(struct device
*cd
,
187 struct device_attribute
*attr
, char *buf
)
189 struct video_device
*vdev
= to_video_device(cd
);
190 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
191 struct v4l2_control ctrl
;
192 ctrl
.id
= V4L2_CID_HUE
;
195 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
196 return sprintf(buf
, "%d\n", ctrl
.value
);
198 static DEVICE_ATTR(hue
, S_IRUGO
, show_hue
, NULL
);
200 static ssize_t
show_contrast(struct device
*cd
,
201 struct device_attribute
*attr
, char *buf
)
203 struct video_device
*vdev
= to_video_device(cd
);
204 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
205 struct v4l2_control ctrl
;
206 ctrl
.id
= V4L2_CID_CONTRAST
;
209 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
210 return sprintf(buf
, "%d\n", ctrl
.value
);
212 static DEVICE_ATTR(contrast
, S_IRUGO
, show_contrast
, NULL
);
214 static ssize_t
show_brightness(struct device
*cd
,
215 struct device_attribute
*attr
, char *buf
)
217 struct video_device
*vdev
= to_video_device(cd
);
218 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
219 struct v4l2_control ctrl
;
220 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
223 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
224 return sprintf(buf
, "%d\n", ctrl
.value
);
226 static DEVICE_ATTR(brightness
, S_IRUGO
, show_brightness
, NULL
);
228 static ssize_t
show_saturation(struct device
*cd
,
229 struct device_attribute
*attr
, char *buf
)
231 struct video_device
*vdev
= to_video_device(cd
);
232 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
233 struct v4l2_control ctrl
;
234 ctrl
.id
= V4L2_CID_SATURATION
;
237 call_all(usbvision
, core
, g_ctrl
, &ctrl
);
238 return sprintf(buf
, "%d\n", ctrl
.value
);
240 static DEVICE_ATTR(saturation
, S_IRUGO
, show_saturation
, NULL
);
242 static ssize_t
show_streaming(struct device
*cd
,
243 struct device_attribute
*attr
, char *buf
)
245 struct video_device
*vdev
= to_video_device(cd
);
246 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
247 return sprintf(buf
, "%s\n",
248 YES_NO(usbvision
->streaming
== stream_on
? 1 : 0));
250 static DEVICE_ATTR(streaming
, S_IRUGO
, show_streaming
, NULL
);
252 static ssize_t
show_compression(struct device
*cd
,
253 struct device_attribute
*attr
, char *buf
)
255 struct video_device
*vdev
= to_video_device(cd
);
256 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
257 return sprintf(buf
, "%s\n",
258 YES_NO(usbvision
->isoc_mode
== ISOC_MODE_COMPRESS
));
260 static DEVICE_ATTR(compression
, S_IRUGO
, show_compression
, NULL
);
262 static ssize_t
show_device_bridge(struct device
*cd
,
263 struct device_attribute
*attr
, char *buf
)
265 struct video_device
*vdev
= to_video_device(cd
);
266 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
267 return sprintf(buf
, "%d\n", usbvision
->bridge_type
);
269 static DEVICE_ATTR(bridge
, S_IRUGO
, show_device_bridge
, NULL
);
271 static void usbvision_create_sysfs(struct video_device
*vdev
)
278 res
= device_create_file(&vdev
->dev
, &dev_attr_version
);
281 res
= device_create_file(&vdev
->dev
, &dev_attr_model
);
284 res
= device_create_file(&vdev
->dev
, &dev_attr_hue
);
287 res
= device_create_file(&vdev
->dev
, &dev_attr_contrast
);
290 res
= device_create_file(&vdev
->dev
, &dev_attr_brightness
);
293 res
= device_create_file(&vdev
->dev
, &dev_attr_saturation
);
296 res
= device_create_file(&vdev
->dev
, &dev_attr_streaming
);
299 res
= device_create_file(&vdev
->dev
, &dev_attr_compression
);
302 res
= device_create_file(&vdev
->dev
, &dev_attr_bridge
);
307 dev_err(&vdev
->dev
, "%s error: %d\n", __func__
, res
);
310 static void usbvision_remove_sysfs(struct video_device
*vdev
)
313 device_remove_file(&vdev
->dev
, &dev_attr_version
);
314 device_remove_file(&vdev
->dev
, &dev_attr_model
);
315 device_remove_file(&vdev
->dev
, &dev_attr_hue
);
316 device_remove_file(&vdev
->dev
, &dev_attr_contrast
);
317 device_remove_file(&vdev
->dev
, &dev_attr_brightness
);
318 device_remove_file(&vdev
->dev
, &dev_attr_saturation
);
319 device_remove_file(&vdev
->dev
, &dev_attr_streaming
);
320 device_remove_file(&vdev
->dev
, &dev_attr_compression
);
321 device_remove_file(&vdev
->dev
, &dev_attr_bridge
);
328 * This is part of Video 4 Linux API. The driver can be opened by one
329 * client only (checks internal counter 'usbvision->user'). The procedure
330 * then allocates buffers needed for video processing.
333 static int usbvision_v4l2_open(struct file
*file
)
335 struct usb_usbvision
*usbvision
= video_drvdata(file
);
338 PDEBUG(DBG_IO
, "open");
340 if (mutex_lock_interruptible(&usbvision
->v4l2_lock
))
343 if (usbvision
->user
) {
346 err_code
= v4l2_fh_open(file
);
350 /* Allocate memory for the scratch ring buffer */
351 err_code
= usbvision_scratch_alloc(usbvision
);
352 if (isoc_mode
== ISOC_MODE_COMPRESS
) {
353 /* Allocate intermediate decompression buffers
355 err_code
= usbvision_decompress_alloc(usbvision
);
358 /* Deallocate all buffers if trouble */
359 usbvision_scratch_free(usbvision
);
360 usbvision_decompress_free(usbvision
);
364 /* If so far no errors then we shall start the camera */
366 /* Send init sequence only once, it's large! */
367 if (!usbvision
->initialized
) {
369 setup_ok
= usbvision_setup(usbvision
, isoc_mode
);
371 usbvision
->initialized
= 1;
377 usbvision_begin_streaming(usbvision
);
378 err_code
= usbvision_init_isoc(usbvision
);
379 /* device must be initialized before isoc transfer */
380 usbvision_muxsel(usbvision
, 0);
383 usbvision_empty_framequeues(usbvision
);
389 mutex_unlock(&usbvision
->v4l2_lock
);
391 PDEBUG(DBG_IO
, "success");
396 * usbvision_v4l2_close()
398 * This is part of Video 4 Linux API. The procedure
399 * stops streaming and deallocates all buffers that were earlier
400 * allocated in usbvision_v4l2_open().
403 static int usbvision_v4l2_close(struct file
*file
)
405 struct usb_usbvision
*usbvision
= video_drvdata(file
);
407 PDEBUG(DBG_IO
, "close");
409 mutex_lock(&usbvision
->v4l2_lock
);
410 usbvision_audio_off(usbvision
);
411 usbvision_restart_isoc(usbvision
);
412 usbvision_stop_isoc(usbvision
);
414 usbvision_decompress_free(usbvision
);
415 usbvision_frames_free(usbvision
);
416 usbvision_empty_framequeues(usbvision
);
417 usbvision_scratch_free(usbvision
);
420 mutex_unlock(&usbvision
->v4l2_lock
);
422 if (usbvision
->remove_pending
) {
423 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
424 usbvision_release(usbvision
);
428 PDEBUG(DBG_IO
, "success");
429 return v4l2_fh_release(file
);
436 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
439 #ifdef CONFIG_VIDEO_ADV_DEBUG
440 static int vidioc_g_register(struct file
*file
, void *priv
,
441 struct v4l2_dbg_register
*reg
)
443 struct usb_usbvision
*usbvision
= video_drvdata(file
);
446 /* NT100x has a 8-bit register space */
447 err_code
= usbvision_read_reg(usbvision
, reg
->reg
&0xff);
449 dev_err(&usbvision
->vdev
.dev
,
450 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
459 static int vidioc_s_register(struct file
*file
, void *priv
,
460 const struct v4l2_dbg_register
*reg
)
462 struct usb_usbvision
*usbvision
= video_drvdata(file
);
465 /* NT100x has a 8-bit register space */
466 err_code
= usbvision_write_reg(usbvision
, reg
->reg
& 0xff, reg
->val
);
468 dev_err(&usbvision
->vdev
.dev
,
469 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
477 static int vidioc_querycap(struct file
*file
, void *priv
,
478 struct v4l2_capability
*vc
)
480 struct usb_usbvision
*usbvision
= video_drvdata(file
);
481 struct video_device
*vdev
= video_devdata(file
);
483 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
485 usbvision_device_data
[usbvision
->dev_model
].model_string
,
487 usb_make_path(usbvision
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
));
488 vc
->device_caps
= usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0;
489 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
490 vc
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
|
491 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
493 vc
->device_caps
|= V4L2_CAP_RADIO
;
495 vc
->capabilities
= vc
->device_caps
| V4L2_CAP_VIDEO_CAPTURE
|
496 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
| V4L2_CAP_DEVICE_CAPS
;
497 if (usbvision_device_data
[usbvision
->dev_model
].radio
)
498 vc
->capabilities
|= V4L2_CAP_RADIO
;
502 static int vidioc_enum_input(struct file
*file
, void *priv
,
503 struct v4l2_input
*vi
)
505 struct usb_usbvision
*usbvision
= video_drvdata(file
);
508 if (vi
->index
>= usbvision
->video_inputs
)
510 if (usbvision
->have_tuner
)
513 chan
= vi
->index
+ 1; /* skip Television string*/
515 /* Determine the requested input characteristics
516 specific for each usbvision card model */
519 if (usbvision_device_data
[usbvision
->dev_model
].video_channels
== 4) {
520 strcpy(vi
->name
, "White Video Input");
522 strcpy(vi
->name
, "Television");
523 vi
->type
= V4L2_INPUT_TYPE_TUNER
;
525 vi
->std
= USBVISION_NORMS
;
529 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
530 if (usbvision_device_data
[usbvision
->dev_model
].video_channels
== 4)
531 strcpy(vi
->name
, "Green Video Input");
533 strcpy(vi
->name
, "Composite Video Input");
534 vi
->std
= USBVISION_NORMS
;
537 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
538 if (usbvision_device_data
[usbvision
->dev_model
].video_channels
== 4)
539 strcpy(vi
->name
, "Yellow Video Input");
541 strcpy(vi
->name
, "S-Video Input");
542 vi
->std
= USBVISION_NORMS
;
545 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
546 strcpy(vi
->name
, "Red Video Input");
547 vi
->std
= USBVISION_NORMS
;
553 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *input
)
555 struct usb_usbvision
*usbvision
= video_drvdata(file
);
557 *input
= usbvision
->ctl_input
;
561 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int input
)
563 struct usb_usbvision
*usbvision
= video_drvdata(file
);
565 if (input
>= usbvision
->video_inputs
)
568 usbvision_muxsel(usbvision
, input
);
569 usbvision_set_input(usbvision
);
570 usbvision_set_output(usbvision
,
572 usbvision
->curheight
);
576 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
578 struct usb_usbvision
*usbvision
= video_drvdata(file
);
580 usbvision
->tvnorm_id
= id
;
582 call_all(usbvision
, video
, s_std
, usbvision
->tvnorm_id
);
583 /* propagate the change to the decoder */
584 usbvision_muxsel(usbvision
, usbvision
->ctl_input
);
589 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
591 struct usb_usbvision
*usbvision
= video_drvdata(file
);
593 *id
= usbvision
->tvnorm_id
;
597 static int vidioc_g_tuner(struct file
*file
, void *priv
,
598 struct v4l2_tuner
*vt
)
600 struct usb_usbvision
*usbvision
= video_drvdata(file
);
602 if (vt
->index
) /* Only tuner 0 */
604 if (vt
->type
== V4L2_TUNER_RADIO
)
605 strcpy(vt
->name
, "Radio");
607 strcpy(vt
->name
, "Television");
609 /* Let clients fill in the remainder of this struct */
610 call_all(usbvision
, tuner
, g_tuner
, vt
);
615 static int vidioc_s_tuner(struct file
*file
, void *priv
,
616 const struct v4l2_tuner
*vt
)
618 struct usb_usbvision
*usbvision
= video_drvdata(file
);
620 /* Only one tuner for now */
623 /* let clients handle this */
624 call_all(usbvision
, tuner
, s_tuner
, vt
);
629 static int vidioc_g_frequency(struct file
*file
, void *priv
,
630 struct v4l2_frequency
*freq
)
632 struct usb_usbvision
*usbvision
= video_drvdata(file
);
637 if (freq
->type
== V4L2_TUNER_RADIO
)
638 freq
->frequency
= usbvision
->radio_freq
;
640 freq
->frequency
= usbvision
->tv_freq
;
645 static int vidioc_s_frequency(struct file
*file
, void *priv
,
646 const struct v4l2_frequency
*freq
)
648 struct usb_usbvision
*usbvision
= video_drvdata(file
);
649 struct v4l2_frequency new_freq
= *freq
;
651 /* Only one tuner for now */
655 call_all(usbvision
, tuner
, s_frequency
, freq
);
656 call_all(usbvision
, tuner
, g_frequency
, &new_freq
);
657 if (freq
->type
== V4L2_TUNER_RADIO
)
658 usbvision
->radio_freq
= new_freq
.frequency
;
660 usbvision
->tv_freq
= new_freq
.frequency
;
665 static int vidioc_reqbufs(struct file
*file
,
666 void *priv
, struct v4l2_requestbuffers
*vr
)
668 struct usb_usbvision
*usbvision
= video_drvdata(file
);
671 RESTRICT_TO_RANGE(vr
->count
, 1, USBVISION_NUMFRAMES
);
673 /* Check input validity:
674 the user must do a VIDEO CAPTURE and MMAP method. */
675 if (vr
->memory
!= V4L2_MEMORY_MMAP
)
678 if (usbvision
->streaming
== stream_on
) {
679 ret
= usbvision_stream_interrupt(usbvision
);
684 usbvision_frames_free(usbvision
);
685 usbvision_empty_framequeues(usbvision
);
686 vr
->count
= usbvision_frames_alloc(usbvision
, vr
->count
);
688 usbvision
->cur_frame
= NULL
;
693 static int vidioc_querybuf(struct file
*file
,
694 void *priv
, struct v4l2_buffer
*vb
)
696 struct usb_usbvision
*usbvision
= video_drvdata(file
);
697 struct usbvision_frame
*frame
;
699 /* FIXME : must control
700 that buffers are mapped (VIDIOC_REQBUFS has been called) */
701 if (vb
->index
>= usbvision
->num_frames
)
703 /* Updating the corresponding frame state */
704 vb
->flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
705 frame
= &usbvision
->frame
[vb
->index
];
706 if (frame
->grabstate
>= frame_state_ready
)
707 vb
->flags
|= V4L2_BUF_FLAG_QUEUED
;
708 if (frame
->grabstate
>= frame_state_done
)
709 vb
->flags
|= V4L2_BUF_FLAG_DONE
;
710 if (frame
->grabstate
== frame_state_unused
)
711 vb
->flags
|= V4L2_BUF_FLAG_MAPPED
;
712 vb
->memory
= V4L2_MEMORY_MMAP
;
714 vb
->m
.offset
= vb
->index
* PAGE_ALIGN(usbvision
->max_frame_size
);
716 vb
->memory
= V4L2_MEMORY_MMAP
;
717 vb
->field
= V4L2_FIELD_NONE
;
718 vb
->length
= usbvision
->curwidth
*
719 usbvision
->curheight
*
720 usbvision
->palette
.bytes_per_pixel
;
721 vb
->timestamp
= usbvision
->frame
[vb
->index
].timestamp
;
722 vb
->sequence
= usbvision
->frame
[vb
->index
].sequence
;
726 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
728 struct usb_usbvision
*usbvision
= video_drvdata(file
);
729 struct usbvision_frame
*frame
;
730 unsigned long lock_flags
;
732 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
733 if (vb
->index
>= usbvision
->num_frames
)
736 frame
= &usbvision
->frame
[vb
->index
];
738 if (frame
->grabstate
!= frame_state_unused
)
741 /* Mark it as ready and enqueue frame */
742 frame
->grabstate
= frame_state_ready
;
743 frame
->scanstate
= scan_state_scanning
;
744 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
746 vb
->flags
&= ~V4L2_BUF_FLAG_DONE
;
748 /* set v4l2_format index */
749 frame
->v4l2_format
= usbvision
->palette
;
751 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
752 list_add_tail(&usbvision
->frame
[vb
->index
].frame
, &usbvision
->inqueue
);
753 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
758 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*vb
)
760 struct usb_usbvision
*usbvision
= video_drvdata(file
);
762 struct usbvision_frame
*f
;
763 unsigned long lock_flags
;
765 if (list_empty(&(usbvision
->outqueue
))) {
766 if (usbvision
->streaming
== stream_idle
)
768 ret
= wait_event_interruptible
769 (usbvision
->wait_frame
,
770 !list_empty(&(usbvision
->outqueue
)));
775 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
776 f
= list_entry(usbvision
->outqueue
.next
,
777 struct usbvision_frame
, frame
);
778 list_del(usbvision
->outqueue
.next
);
779 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
781 f
->grabstate
= frame_state_unused
;
783 vb
->memory
= V4L2_MEMORY_MMAP
;
784 vb
->flags
= V4L2_BUF_FLAG_MAPPED
|
785 V4L2_BUF_FLAG_QUEUED
|
787 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
788 vb
->index
= f
->index
;
789 vb
->sequence
= f
->sequence
;
790 vb
->timestamp
= f
->timestamp
;
791 vb
->field
= V4L2_FIELD_NONE
;
792 vb
->bytesused
= f
->scanlength
;
797 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
799 struct usb_usbvision
*usbvision
= video_drvdata(file
);
801 usbvision
->streaming
= stream_on
;
802 call_all(usbvision
, video
, s_stream
, 1);
807 static int vidioc_streamoff(struct file
*file
,
808 void *priv
, enum v4l2_buf_type type
)
810 struct usb_usbvision
*usbvision
= video_drvdata(file
);
812 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
815 if (usbvision
->streaming
== stream_on
) {
816 usbvision_stream_interrupt(usbvision
);
817 /* Stop all video streamings */
818 call_all(usbvision
, video
, s_stream
, 0);
820 usbvision_empty_framequeues(usbvision
);
825 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
826 struct v4l2_fmtdesc
*vfd
)
828 if (vfd
->index
>= USBVISION_SUPPORTED_PALETTES
- 1)
830 strcpy(vfd
->description
, usbvision_v4l2_format
[vfd
->index
].desc
);
831 vfd
->pixelformat
= usbvision_v4l2_format
[vfd
->index
].format
;
835 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
836 struct v4l2_format
*vf
)
838 struct usb_usbvision
*usbvision
= video_drvdata(file
);
839 vf
->fmt
.pix
.width
= usbvision
->curwidth
;
840 vf
->fmt
.pix
.height
= usbvision
->curheight
;
841 vf
->fmt
.pix
.pixelformat
= usbvision
->palette
.format
;
842 vf
->fmt
.pix
.bytesperline
=
843 usbvision
->curwidth
* usbvision
->palette
.bytes_per_pixel
;
844 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
* usbvision
->curheight
;
845 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
846 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
851 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
852 struct v4l2_format
*vf
)
854 struct usb_usbvision
*usbvision
= video_drvdata(file
);
857 /* Find requested format in available ones */
858 for (format_idx
= 0; format_idx
< USBVISION_SUPPORTED_PALETTES
; format_idx
++) {
859 if (vf
->fmt
.pix
.pixelformat
==
860 usbvision_v4l2_format
[format_idx
].format
) {
861 usbvision
->palette
= usbvision_v4l2_format
[format_idx
];
866 if (format_idx
== USBVISION_SUPPORTED_PALETTES
)
868 RESTRICT_TO_RANGE(vf
->fmt
.pix
.width
, MIN_FRAME_WIDTH
, MAX_FRAME_WIDTH
);
869 RESTRICT_TO_RANGE(vf
->fmt
.pix
.height
, MIN_FRAME_HEIGHT
, MAX_FRAME_HEIGHT
);
871 vf
->fmt
.pix
.bytesperline
= vf
->fmt
.pix
.width
*
872 usbvision
->palette
.bytes_per_pixel
;
873 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*vf
->fmt
.pix
.height
;
874 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
875 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
880 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
881 struct v4l2_format
*vf
)
883 struct usb_usbvision
*usbvision
= video_drvdata(file
);
886 ret
= vidioc_try_fmt_vid_cap(file
, priv
, vf
);
890 /* stop io in case it is already in progress */
891 if (usbvision
->streaming
== stream_on
) {
892 ret
= usbvision_stream_interrupt(usbvision
);
896 usbvision_frames_free(usbvision
);
897 usbvision_empty_framequeues(usbvision
);
899 usbvision
->cur_frame
= NULL
;
901 /* by now we are committed to the new data... */
902 usbvision_set_output(usbvision
, vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
);
907 static ssize_t
usbvision_read(struct file
*file
, char __user
*buf
,
908 size_t count
, loff_t
*ppos
)
910 struct usb_usbvision
*usbvision
= video_drvdata(file
);
911 int noblock
= file
->f_flags
& O_NONBLOCK
;
912 unsigned long lock_flags
;
914 struct usbvision_frame
*frame
;
916 PDEBUG(DBG_IO
, "%s: %ld bytes, noblock=%d", __func__
,
917 (unsigned long)count
, noblock
);
919 if (!USBVISION_IS_OPERATIONAL(usbvision
) || (buf
== NULL
))
922 /* This entry point is compatible with the mmap routines
923 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
924 to get frames or call read on the device. */
925 if (!usbvision
->num_frames
) {
926 /* First, allocate some frames to work with
927 if this has not been done with VIDIOC_REQBUF */
928 usbvision_frames_free(usbvision
);
929 usbvision_empty_framequeues(usbvision
);
930 usbvision_frames_alloc(usbvision
, USBVISION_NUMFRAMES
);
933 if (usbvision
->streaming
!= stream_on
) {
934 /* no stream is running, make it running ! */
935 usbvision
->streaming
= stream_on
;
936 call_all(usbvision
, video
, s_stream
, 1);
939 /* Then, enqueue as many frames as possible
940 (like a user of VIDIOC_QBUF would do) */
941 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
942 frame
= &usbvision
->frame
[i
];
943 if (frame
->grabstate
== frame_state_unused
) {
944 /* Mark it as ready and enqueue frame */
945 frame
->grabstate
= frame_state_ready
;
946 frame
->scanstate
= scan_state_scanning
;
947 /* Accumulated in usbvision_parse_data() */
948 frame
->scanlength
= 0;
950 /* set v4l2_format index */
951 frame
->v4l2_format
= usbvision
->palette
;
953 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
954 list_add_tail(&frame
->frame
, &usbvision
->inqueue
);
955 spin_unlock_irqrestore(&usbvision
->queue_lock
,
960 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
961 if (list_empty(&(usbvision
->outqueue
))) {
965 ret
= wait_event_interruptible
966 (usbvision
->wait_frame
,
967 !list_empty(&(usbvision
->outqueue
)));
972 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
973 frame
= list_entry(usbvision
->outqueue
.next
,
974 struct usbvision_frame
, frame
);
975 list_del(usbvision
->outqueue
.next
);
976 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
978 /* An error returns an empty frame */
979 if (frame
->grabstate
== frame_state_error
) {
980 frame
->bytes_read
= 0;
984 PDEBUG(DBG_IO
, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
986 frame
->index
, frame
->bytes_read
, frame
->scanlength
);
988 /* copy bytes to user space; we allow for partials reads */
989 if ((count
+ frame
->bytes_read
) > (unsigned long)frame
->scanlength
)
990 count
= frame
->scanlength
- frame
->bytes_read
;
992 if (copy_to_user(buf
, frame
->data
+ frame
->bytes_read
, count
))
995 frame
->bytes_read
+= count
;
996 PDEBUG(DBG_IO
, "%s: {copy} count used=%ld, new bytes_read=%ld",
998 (unsigned long)count
, frame
->bytes_read
);
1003 * For now, forget the frame if it has not been read in one shot.
1005 frame
->bytes_read
= 0;
1007 /* Mark it as available to be used again. */
1008 frame
->grabstate
= frame_state_unused
;
1010 if (frame
->bytes_read
>= frame
->scanlength
) {
1011 /* All data has been read */
1012 frame
->bytes_read
= 0;
1014 /* Mark it as available to be used again. */
1015 frame
->grabstate
= frame_state_unused
;
1022 static ssize_t
usbvision_v4l2_read(struct file
*file
, char __user
*buf
,
1023 size_t count
, loff_t
*ppos
)
1025 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1028 if (mutex_lock_interruptible(&usbvision
->v4l2_lock
))
1029 return -ERESTARTSYS
;
1030 res
= usbvision_read(file
, buf
, count
, ppos
);
1031 mutex_unlock(&usbvision
->v4l2_lock
);
1035 static int usbvision_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1037 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1038 start
= vma
->vm_start
;
1041 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1043 PDEBUG(DBG_MMAP
, "mmap");
1045 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1048 if (!(vma
->vm_flags
& VM_WRITE
) ||
1049 size
!= PAGE_ALIGN(usbvision
->max_frame_size
)) {
1053 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
1054 if (((PAGE_ALIGN(usbvision
->max_frame_size
)*i
) >> PAGE_SHIFT
) ==
1058 if (i
== usbvision
->num_frames
) {
1060 "mmap: user supplied mapping address is out of range");
1064 /* VM_IO is eventually going to replace PageReserved altogether */
1065 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
1067 pos
= usbvision
->frame
[i
].data
;
1069 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1070 PDEBUG(DBG_MMAP
, "mmap: vm_insert_page failed");
1081 static int usbvision_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1083 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1086 if (mutex_lock_interruptible(&usbvision
->v4l2_lock
))
1087 return -ERESTARTSYS
;
1088 res
= usbvision_mmap(file
, vma
);
1089 mutex_unlock(&usbvision
->v4l2_lock
);
1094 * Here comes the stuff for radio on usbvision based devices
1097 static int usbvision_radio_open(struct file
*file
)
1099 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1102 PDEBUG(DBG_IO
, "%s:", __func__
);
1104 if (mutex_lock_interruptible(&usbvision
->v4l2_lock
))
1105 return -ERESTARTSYS
;
1106 err_code
= v4l2_fh_open(file
);
1109 if (usbvision
->user
) {
1110 dev_err(&usbvision
->rdev
.dev
,
1111 "%s: Someone tried to open an already opened USBVision Radio!\n",
1115 /* Alternate interface 1 is is the biggest frame size */
1116 err_code
= usbvision_set_alternate(usbvision
);
1118 usbvision
->last_error
= err_code
;
1123 /* If so far no errors then we shall start the radio */
1124 usbvision
->radio
= 1;
1125 call_all(usbvision
, tuner
, s_radio
);
1126 usbvision_set_audio(usbvision
, USBVISION_AUDIO_RADIO
);
1130 mutex_unlock(&usbvision
->v4l2_lock
);
1135 static int usbvision_radio_close(struct file
*file
)
1137 struct usb_usbvision
*usbvision
= video_drvdata(file
);
1141 mutex_lock(&usbvision
->v4l2_lock
);
1142 /* Set packet size to 0 */
1143 usbvision
->iface_alt
= 0;
1144 usb_set_interface(usbvision
->dev
, usbvision
->iface
,
1145 usbvision
->iface_alt
);
1147 usbvision_audio_off(usbvision
);
1148 usbvision
->radio
= 0;
1150 mutex_unlock(&usbvision
->v4l2_lock
);
1152 if (usbvision
->remove_pending
) {
1153 printk(KERN_INFO
"%s: Final disconnect\n", __func__
);
1154 v4l2_fh_release(file
);
1155 usbvision_release(usbvision
);
1159 PDEBUG(DBG_IO
, "success");
1160 return v4l2_fh_release(file
);
1163 /* Video registration stuff */
1165 /* Video template */
1166 static const struct v4l2_file_operations usbvision_fops
= {
1167 .owner
= THIS_MODULE
,
1168 .open
= usbvision_v4l2_open
,
1169 .release
= usbvision_v4l2_close
,
1170 .read
= usbvision_v4l2_read
,
1171 .mmap
= usbvision_v4l2_mmap
,
1172 .unlocked_ioctl
= video_ioctl2
,
1175 static const struct v4l2_ioctl_ops usbvision_ioctl_ops
= {
1176 .vidioc_querycap
= vidioc_querycap
,
1177 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1178 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1179 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1180 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1181 .vidioc_reqbufs
= vidioc_reqbufs
,
1182 .vidioc_querybuf
= vidioc_querybuf
,
1183 .vidioc_qbuf
= vidioc_qbuf
,
1184 .vidioc_dqbuf
= vidioc_dqbuf
,
1185 .vidioc_s_std
= vidioc_s_std
,
1186 .vidioc_g_std
= vidioc_g_std
,
1187 .vidioc_enum_input
= vidioc_enum_input
,
1188 .vidioc_g_input
= vidioc_g_input
,
1189 .vidioc_s_input
= vidioc_s_input
,
1190 .vidioc_streamon
= vidioc_streamon
,
1191 .vidioc_streamoff
= vidioc_streamoff
,
1192 .vidioc_g_tuner
= vidioc_g_tuner
,
1193 .vidioc_s_tuner
= vidioc_s_tuner
,
1194 .vidioc_g_frequency
= vidioc_g_frequency
,
1195 .vidioc_s_frequency
= vidioc_s_frequency
,
1196 .vidioc_log_status
= v4l2_ctrl_log_status
,
1197 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1198 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1199 #ifdef CONFIG_VIDEO_ADV_DEBUG
1200 .vidioc_g_register
= vidioc_g_register
,
1201 .vidioc_s_register
= vidioc_s_register
,
1205 static struct video_device usbvision_video_template
= {
1206 .fops
= &usbvision_fops
,
1207 .ioctl_ops
= &usbvision_ioctl_ops
,
1208 .name
= "usbvision-video",
1209 .release
= video_device_release_empty
,
1210 .tvnorms
= USBVISION_NORMS
,
1214 /* Radio template */
1215 static const struct v4l2_file_operations usbvision_radio_fops
= {
1216 .owner
= THIS_MODULE
,
1217 .open
= usbvision_radio_open
,
1218 .release
= usbvision_radio_close
,
1219 .poll
= v4l2_ctrl_poll
,
1220 .unlocked_ioctl
= video_ioctl2
,
1223 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops
= {
1224 .vidioc_querycap
= vidioc_querycap
,
1225 .vidioc_g_tuner
= vidioc_g_tuner
,
1226 .vidioc_s_tuner
= vidioc_s_tuner
,
1227 .vidioc_g_frequency
= vidioc_g_frequency
,
1228 .vidioc_s_frequency
= vidioc_s_frequency
,
1229 .vidioc_log_status
= v4l2_ctrl_log_status
,
1230 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1231 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1234 static struct video_device usbvision_radio_template
= {
1235 .fops
= &usbvision_radio_fops
,
1236 .name
= "usbvision-radio",
1237 .release
= video_device_release_empty
,
1238 .ioctl_ops
= &usbvision_radio_ioctl_ops
,
1242 static void usbvision_vdev_init(struct usb_usbvision
*usbvision
,
1243 struct video_device
*vdev
,
1244 const struct video_device
*vdev_template
,
1247 struct usb_device
*usb_dev
= usbvision
->dev
;
1249 if (usb_dev
== NULL
) {
1250 dev_err(&usbvision
->dev
->dev
,
1251 "%s: usbvision->dev is not set\n", __func__
);
1255 *vdev
= *vdev_template
;
1256 vdev
->lock
= &usbvision
->v4l2_lock
;
1257 vdev
->v4l2_dev
= &usbvision
->v4l2_dev
;
1258 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s", name
);
1259 video_set_drvdata(vdev
, usbvision
);
1262 /* unregister video4linux devices */
1263 static void usbvision_unregister_video(struct usb_usbvision
*usbvision
)
1266 if (video_is_registered(&usbvision
->rdev
)) {
1267 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1268 video_device_node_name(&usbvision
->rdev
));
1269 video_unregister_device(&usbvision
->rdev
);
1273 if (video_is_registered(&usbvision
->vdev
)) {
1274 PDEBUG(DBG_PROBE
, "unregister %s [v4l2]",
1275 video_device_node_name(&usbvision
->vdev
));
1276 video_unregister_device(&usbvision
->vdev
);
1280 /* register video4linux devices */
1281 static int usbvision_register_video(struct usb_usbvision
*usbvision
)
1286 usbvision_vdev_init(usbvision
, &usbvision
->vdev
,
1287 &usbvision_video_template
, "USBVision Video");
1288 if (!usbvision
->have_tuner
) {
1289 v4l2_disable_ioctl(&usbvision
->vdev
, VIDIOC_G_FREQUENCY
);
1290 v4l2_disable_ioctl(&usbvision
->vdev
, VIDIOC_S_TUNER
);
1291 v4l2_disable_ioctl(&usbvision
->vdev
, VIDIOC_G_FREQUENCY
);
1292 v4l2_disable_ioctl(&usbvision
->vdev
, VIDIOC_S_TUNER
);
1294 if (video_register_device(&usbvision
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0)
1296 printk(KERN_INFO
"USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1297 usbvision
->nr
, video_device_node_name(&usbvision
->vdev
));
1300 if (usbvision_device_data
[usbvision
->dev_model
].radio
) {
1301 /* usbvision has radio */
1302 usbvision_vdev_init(usbvision
, &usbvision
->rdev
,
1303 &usbvision_radio_template
, "USBVision Radio");
1304 if (video_register_device(&usbvision
->rdev
, VFL_TYPE_RADIO
, radio_nr
) < 0)
1306 printk(KERN_INFO
"USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1307 usbvision
->nr
, video_device_node_name(&usbvision
->rdev
));
1313 dev_err(&usbvision
->dev
->dev
,
1314 "USBVision[%d]: video_register_device() failed\n",
1316 usbvision_unregister_video(usbvision
);
1323 * This code allocates the struct usb_usbvision.
1324 * It is filled with default values.
1326 * Returns NULL on error, a pointer to usb_usbvision else.
1329 static struct usb_usbvision
*usbvision_alloc(struct usb_device
*dev
,
1330 struct usb_interface
*intf
)
1332 struct usb_usbvision
*usbvision
;
1334 usbvision
= kzalloc(sizeof(struct usb_usbvision
), GFP_KERNEL
);
1335 if (usbvision
== NULL
)
1338 usbvision
->dev
= dev
;
1339 if (v4l2_device_register(&intf
->dev
, &usbvision
->v4l2_dev
))
1342 if (v4l2_ctrl_handler_init(&usbvision
->hdl
, 4))
1344 usbvision
->v4l2_dev
.ctrl_handler
= &usbvision
->hdl
;
1345 mutex_init(&usbvision
->v4l2_lock
);
1347 /* prepare control urb for control messages during interrupts */
1348 usbvision
->ctrl_urb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
1349 if (usbvision
->ctrl_urb
== NULL
)
1351 init_waitqueue_head(&usbvision
->ctrl_urb_wq
);
1356 v4l2_ctrl_handler_free(&usbvision
->hdl
);
1357 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1364 * usbvision_release()
1366 * This code does final release of struct usb_usbvision. This happens
1367 * after the device is disconnected -and- all clients closed their files.
1370 static void usbvision_release(struct usb_usbvision
*usbvision
)
1372 PDEBUG(DBG_PROBE
, "");
1374 usbvision
->initialized
= 0;
1376 usbvision_remove_sysfs(&usbvision
->vdev
);
1377 usbvision_unregister_video(usbvision
);
1378 kfree(usbvision
->alt_max_pkt_size
);
1380 usb_free_urb(usbvision
->ctrl_urb
);
1382 v4l2_ctrl_handler_free(&usbvision
->hdl
);
1383 v4l2_device_unregister(&usbvision
->v4l2_dev
);
1386 PDEBUG(DBG_PROBE
, "success");
1390 /*********************** usb interface **********************************/
1392 static void usbvision_configure_video(struct usb_usbvision
*usbvision
)
1396 if (usbvision
== NULL
)
1399 model
= usbvision
->dev_model
;
1400 usbvision
->palette
= usbvision_v4l2_format
[2]; /* V4L2_PIX_FMT_RGB24; */
1402 if (usbvision_device_data
[usbvision
->dev_model
].vin_reg2_override
) {
1403 usbvision
->vin_reg2_preset
=
1404 usbvision_device_data
[usbvision
->dev_model
].vin_reg2
;
1406 usbvision
->vin_reg2_preset
= 0;
1409 usbvision
->tvnorm_id
= usbvision_device_data
[model
].video_norm
;
1410 usbvision
->video_inputs
= usbvision_device_data
[model
].video_channels
;
1411 usbvision
->ctl_input
= 0;
1412 usbvision
->radio_freq
= 87.5 * 16000;
1413 usbvision
->tv_freq
= 400 * 16;
1415 /* This should be here to make i2c clients to be able to register */
1416 /* first switch off audio */
1417 if (usbvision_device_data
[model
].audio_channels
> 0)
1418 usbvision_audio_off(usbvision
);
1419 /* and then power up the tuner */
1420 usbvision_power_on(usbvision
);
1421 usbvision_i2c_register(usbvision
);
1427 * This procedure queries device descriptor and accepts the interface
1428 * if it looks like USBVISION video device
1431 static int usbvision_probe(struct usb_interface
*intf
,
1432 const struct usb_device_id
*devid
)
1434 struct usb_device
*dev
= usb_get_dev(interface_to_usbdev(intf
));
1435 struct usb_interface
*uif
;
1436 __u8 ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
1437 const struct usb_host_interface
*interface
;
1438 struct usb_usbvision
*usbvision
= NULL
;
1439 const struct usb_endpoint_descriptor
*endpoint
;
1442 PDEBUG(DBG_PROBE
, "VID=%#04x, PID=%#04x, ifnum=%u",
1443 dev
->descriptor
.idVendor
,
1444 dev
->descriptor
.idProduct
, ifnum
);
1446 model
= devid
->driver_info
;
1447 if (model
< 0 || model
>= usbvision_device_data_size
) {
1448 PDEBUG(DBG_PROBE
, "model out of bounds %d", model
);
1452 printk(KERN_INFO
"%s: %s found\n", __func__
,
1453 usbvision_device_data
[model
].model_string
);
1456 * this is a security check.
1457 * an exploit using an incorrect bInterfaceNumber is known
1459 if (ifnum
>= USB_MAXINTERFACES
|| !dev
->actconfig
->interface
[ifnum
])
1462 if (usbvision_device_data
[model
].interface
>= 0)
1463 interface
= &dev
->actconfig
->interface
[usbvision_device_data
[model
].interface
]->altsetting
[0];
1464 else if (ifnum
< dev
->actconfig
->desc
.bNumInterfaces
)
1465 interface
= &dev
->actconfig
->interface
[ifnum
]->altsetting
[0];
1467 dev_err(&intf
->dev
, "interface %d is invalid, max is %d\n",
1468 ifnum
, dev
->actconfig
->desc
.bNumInterfaces
- 1);
1473 if (interface
->desc
.bNumEndpoints
< 2) {
1474 dev_err(&intf
->dev
, "interface %d has %d endpoints, but must"
1475 " have minimum 2\n", ifnum
, interface
->desc
.bNumEndpoints
);
1479 endpoint
= &interface
->endpoint
[1].desc
;
1481 if (!usb_endpoint_xfer_isoc(endpoint
)) {
1482 dev_err(&intf
->dev
, "%s: interface %d. has non-ISO endpoint!\n",
1484 dev_err(&intf
->dev
, "%s: Endpoint attributes %d",
1485 __func__
, endpoint
->bmAttributes
);
1489 if (usb_endpoint_dir_out(endpoint
)) {
1490 dev_err(&intf
->dev
, "%s: interface %d. has ISO OUT endpoint!\n",
1496 usbvision
= usbvision_alloc(dev
, intf
);
1497 if (usbvision
== NULL
) {
1498 dev_err(&intf
->dev
, "%s: couldn't allocate USBVision struct\n", __func__
);
1503 if (dev
->descriptor
.bNumConfigurations
> 1)
1504 usbvision
->bridge_type
= BRIDGE_NT1004
;
1505 else if (model
== DAZZLE_DVC_90_REV_1_SECAM
)
1506 usbvision
->bridge_type
= BRIDGE_NT1005
;
1508 usbvision
->bridge_type
= BRIDGE_NT1003
;
1509 PDEBUG(DBG_PROBE
, "bridge_type %d", usbvision
->bridge_type
);
1511 /* compute alternate max packet sizes */
1512 uif
= dev
->actconfig
->interface
[0];
1514 usbvision
->num_alt
= uif
->num_altsetting
;
1515 PDEBUG(DBG_PROBE
, "Alternate settings: %i", usbvision
->num_alt
);
1516 usbvision
->alt_max_pkt_size
= kmalloc(32 * usbvision
->num_alt
, GFP_KERNEL
);
1517 if (usbvision
->alt_max_pkt_size
== NULL
) {
1518 dev_err(&intf
->dev
, "usbvision: out of memory!\n");
1523 for (i
= 0; i
< usbvision
->num_alt
; i
++) {
1524 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1526 usbvision
->alt_max_pkt_size
[i
] =
1527 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1528 PDEBUG(DBG_PROBE
, "Alternate setting %i, max size= %i", i
,
1529 usbvision
->alt_max_pkt_size
[i
]);
1533 usbvision
->nr
= usbvision_nr
++;
1535 spin_lock_init(&usbvision
->queue_lock
);
1536 init_waitqueue_head(&usbvision
->wait_frame
);
1537 init_waitqueue_head(&usbvision
->wait_stream
);
1539 usbvision
->have_tuner
= usbvision_device_data
[model
].tuner
;
1540 if (usbvision
->have_tuner
)
1541 usbvision
->tuner_type
= usbvision_device_data
[model
].tuner_type
;
1543 usbvision
->dev_model
= model
;
1544 usbvision
->remove_pending
= 0;
1545 usbvision
->iface
= ifnum
;
1546 usbvision
->iface_alt
= 0;
1547 usbvision
->video_endp
= endpoint
->bEndpointAddress
;
1548 usbvision
->isoc_packet_size
= 0;
1549 usbvision
->usb_bandwidth
= 0;
1550 usbvision
->user
= 0;
1551 usbvision
->streaming
= stream_off
;
1552 usbvision_configure_video(usbvision
);
1553 usbvision_register_video(usbvision
);
1555 usbvision_create_sysfs(&usbvision
->vdev
);
1557 PDEBUG(DBG_PROBE
, "success");
1561 usbvision_release(usbvision
);
1569 * usbvision_disconnect()
1571 * This procedure stops all driver activity, deallocates interface-private
1572 * structure (pointed by 'ptr') and after that driver should be removable
1573 * with no ill consequences.
1576 static void usbvision_disconnect(struct usb_interface
*intf
)
1578 struct usb_usbvision
*usbvision
= to_usbvision(usb_get_intfdata(intf
));
1580 PDEBUG(DBG_PROBE
, "");
1582 if (usbvision
== NULL
) {
1583 pr_err("%s: usb_get_intfdata() failed\n", __func__
);
1587 mutex_lock(&usbvision
->v4l2_lock
);
1589 /* At this time we ask to cancel outstanding URBs */
1590 usbvision_stop_isoc(usbvision
);
1592 v4l2_device_disconnect(&usbvision
->v4l2_dev
);
1593 usbvision_i2c_unregister(usbvision
);
1594 usbvision
->remove_pending
= 1; /* Now all ISO data will be ignored */
1596 usb_put_dev(usbvision
->dev
);
1597 usbvision
->dev
= NULL
; /* USB device is no more */
1599 mutex_unlock(&usbvision
->v4l2_lock
);
1601 if (usbvision
->user
) {
1602 printk(KERN_INFO
"%s: In use, disconnect pending\n",
1604 wake_up_interruptible(&usbvision
->wait_frame
);
1605 wake_up_interruptible(&usbvision
->wait_stream
);
1607 usbvision_release(usbvision
);
1610 PDEBUG(DBG_PROBE
, "success");
1613 static struct usb_driver usbvision_driver
= {
1614 .name
= "usbvision",
1615 .id_table
= usbvision_table
,
1616 .probe
= usbvision_probe
,
1617 .disconnect
= usbvision_disconnect
,
1623 * This code is run to initialize the driver.
1626 static int __init
usbvision_init(void)
1630 PDEBUG(DBG_PROBE
, "");
1632 PDEBUG(DBG_IO
, "IO debugging is enabled [video]");
1633 PDEBUG(DBG_PROBE
, "PROBE debugging is enabled [video]");
1634 PDEBUG(DBG_MMAP
, "MMAP debugging is enabled [video]");
1636 /* disable planar mode support unless compression enabled */
1637 if (isoc_mode
!= ISOC_MODE_COMPRESS
) {
1638 /* FIXME : not the right way to set supported flag */
1639 usbvision_v4l2_format
[6].supported
= 0; /* V4L2_PIX_FMT_YVU420 */
1640 usbvision_v4l2_format
[7].supported
= 0; /* V4L2_PIX_FMT_YUV422P */
1643 err_code
= usb_register(&usbvision_driver
);
1645 if (err_code
== 0) {
1646 printk(KERN_INFO DRIVER_DESC
" : " USBVISION_VERSION_STRING
"\n");
1647 PDEBUG(DBG_PROBE
, "success");
1652 static void __exit
usbvision_exit(void)
1654 PDEBUG(DBG_PROBE
, "");
1656 usb_deregister(&usbvision_driver
);
1657 PDEBUG(DBG_PROBE
, "success");
1660 module_init(usbvision_init
);
1661 module_exit(usbvision_exit
);