2 * USB USBVISION Video device driver 0.9.9
6 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
8 * This module is part of usbvision driver project.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Let's call the version 0.... until compression decoding is completely
27 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
28 * It was based on USB CPiA driver written by Peter Pregler,
29 * Scott J. Bertin and Johannes Erdfelt
30 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
31 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
32 * Updates to driver completed by Dwaine P. Garden
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
39 * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one?
40 * - Clean up the driver.
41 * - optimization for performance.
42 * - Add Videotext capability (VBI). Working on it.....
43 * - Check audio for other devices
47 #include <linux/version.h>
48 #include <linux/kernel.h>
49 #include <linux/sched.h>
50 #include <linux/list.h>
51 #include <linux/timer.h>
52 #include <linux/slab.h>
54 #include <linux/utsname.h>
55 #include <linux/highmem.h>
56 #include <linux/smp_lock.h>
57 #include <linux/videodev.h>
58 #include <linux/vmalloc.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/spinlock.h>
63 #include <linux/videodev2.h>
64 #include <linux/video_decoder.h>
65 #include <linux/i2c.h>
67 #include <media/saa7115.h>
68 #include <media/v4l2-common.h>
69 #include <media/tuner.h>
70 #include <media/audiochip.h>
72 #include <linux/moduleparam.h>
73 #include <linux/workqueue.h>
76 #include <linux/kmod.h>
79 #include "usbvision.h"
81 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
82 #define DRIVER_NAME "usbvision"
83 #define DRIVER_ALIAS "USBVision"
84 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85 #define DRIVER_LICENSE "GPL"
86 #define USBVISION_DRIVER_VERSION_MAJOR 0
87 #define USBVISION_DRIVER_VERSION_MINOR 9
88 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
90 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
92 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
95 #define USBVISION_DEBUG /* Turn on debug messages */
97 #ifdef USBVISION_DEBUG
98 #define PDEBUG(level, fmt, args...) \
99 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
101 #define PDEBUG(level, fmt, args...) do {} while(0)
104 #define DBG_IOCTL 1<<0
106 #define DBG_PROBE 1<<2
107 #define DBG_FUNC 1<<3
110 #define rmspace(str) while(*str==' ') str++;
111 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
114 static int usbvision_nr
= 0; // sequential number of usbvision device
116 static struct usbvision_v4l2_format_st usbvision_v4l2_format
[] = {
117 { 1, 1, 8, V4L2_PIX_FMT_GREY
, "GREY" },
118 { 1, 2, 16, V4L2_PIX_FMT_RGB565
, "RGB565" },
119 { 1, 3, 24, V4L2_PIX_FMT_RGB24
, "RGB24" },
120 { 1, 4, 32, V4L2_PIX_FMT_RGB32
, "RGB32" },
121 { 1, 2, 16, V4L2_PIX_FMT_RGB555
, "RGB555" },
122 { 1, 2, 16, V4L2_PIX_FMT_YUYV
, "YUV422" },
123 { 1, 2, 12, V4L2_PIX_FMT_YVU420
, "YUV420P" }, // 1.5 !
124 { 1, 2, 16, V4L2_PIX_FMT_YUV422P
, "YUV422P" }
127 /* supported tv norms */
128 static struct usbvision_tvnorm tvnorms
[] = {
137 .id
= V4L2_STD_SECAM
,
140 .id
= V4L2_STD_PAL_M
,
144 #define TVNORMS ARRAY_SIZE(tvnorms)
146 // Function prototypes
147 static void usbvision_release(struct usb_usbvision
*usbvision
);
149 // Default initalization of device driver parameters
150 static int isocMode
= ISOC_MODE_COMPRESS
; // Set the default format for ISOC endpoint
151 static int video_debug
= 0; // Set the default Debug Mode of the device driver
152 static int PowerOnAtOpen
= 1; // Set the default device to power on at startup
153 static int video_nr
= -1; // Sequential Number of Video Device
154 static int radio_nr
= -1; // Sequential Number of Radio Device
155 static int vbi_nr
= -1; // Sequential Number of VBI Device
156 static char *CustomDevice
=NULL
; // Set as nothing....
158 // Grab parameters for the device driver
160 #if defined(module_param) // Showing parameters under SYSFS
161 module_param(isocMode
, int, 0444);
162 module_param(video_debug
, int, 0444);
163 module_param(PowerOnAtOpen
, int, 0444);
164 module_param(video_nr
, int, 0444);
165 module_param(radio_nr
, int, 0444);
166 module_param(vbi_nr
, int, 0444);
167 module_param(CustomDevice
, charp
, 0444);
169 MODULE_PARAM(isocMode
, "i");
170 MODULE_PARM(video_debug
, "i"); // Grab the Debug Mode of the device driver
171 MODULE_PARM(adjustCompression
, "i"); // Grab the compression to be adaptive
172 MODULE_PARM(PowerOnAtOpen
, "i"); // Grab the device to power on at startup
173 MODULE_PARM(SwitchSVideoInput
, "i"); // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
174 MODULE_PARM(video_nr
, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
175 MODULE_PARM(radio_nr
, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
176 MODULE_PARM(vbi_nr
, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
177 MODULE_PARM(CustomDevice
, "s"); // .... CustomDevice
180 MODULE_PARM_DESC(isocMode
, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
181 MODULE_PARM_DESC(video_debug
, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
182 MODULE_PARM_DESC(PowerOnAtOpen
, " Set the default device to power on when device is opened. Default: 1 (On)");
183 MODULE_PARM_DESC(video_nr
, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
184 MODULE_PARM_DESC(radio_nr
, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
185 MODULE_PARM_DESC(vbi_nr
, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
186 MODULE_PARM_DESC(CustomDevice
, " Define the fine tuning parameters for the device. Default: null");
190 MODULE_AUTHOR(DRIVER_AUTHOR
);
191 MODULE_DESCRIPTION(DRIVER_DESC
);
192 MODULE_LICENSE(DRIVER_LICENSE
);
193 MODULE_VERSION(USBVISION_VERSION_STRING
);
194 MODULE_ALIAS(DRIVER_ALIAS
);
197 /****************************************************************************************/
198 /* SYSFS Code - Copied from the stv680.c usb module. */
199 /* Device information is located at /sys/class/video4linux/video0 */
200 /* Device parameters information is located at /sys/module/usbvision */
201 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
202 /****************************************************************************************/
205 #define YES_NO(x) ((x) ? "Yes" : "No")
207 static inline struct usb_usbvision
*cd_to_usbvision(struct class_device
*cd
)
209 struct video_device
*vdev
= to_video_device(cd
);
210 return video_get_drvdata(vdev
);
213 static ssize_t
show_version(struct class_device
*cd
, char *buf
)
215 return sprintf(buf
, "%s\n", USBVISION_VERSION_STRING
);
217 static CLASS_DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
219 static ssize_t
show_model(struct class_device
*class_dev
, char *buf
)
221 struct video_device
*vdev
= to_video_device(class_dev
);
222 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
223 return sprintf(buf
, "%s\n", usbvision_device_data
[usbvision
->DevModel
].ModelString
);
225 static CLASS_DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
227 static ssize_t
show_hue(struct class_device
*class_dev
, char *buf
)
229 struct video_device
*vdev
= to_video_device(class_dev
);
230 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
231 struct v4l2_control ctrl
;
232 ctrl
.id
= V4L2_CID_HUE
;
234 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
235 return sprintf(buf
, "%d\n", ctrl
.value
>> 8);
237 static CLASS_DEVICE_ATTR(hue
, S_IRUGO
, show_hue
, NULL
);
239 static ssize_t
show_contrast(struct class_device
*class_dev
, char *buf
)
241 struct video_device
*vdev
= to_video_device(class_dev
);
242 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
243 struct v4l2_control ctrl
;
244 ctrl
.id
= V4L2_CID_CONTRAST
;
246 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
247 return sprintf(buf
, "%d\n", ctrl
.value
>> 8);
249 static CLASS_DEVICE_ATTR(contrast
, S_IRUGO
, show_contrast
, NULL
);
251 static ssize_t
show_brightness(struct class_device
*class_dev
, char *buf
)
253 struct video_device
*vdev
= to_video_device(class_dev
);
254 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
255 struct v4l2_control ctrl
;
256 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
258 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
259 return sprintf(buf
, "%d\n", ctrl
.value
>> 8);
261 static CLASS_DEVICE_ATTR(brightness
, S_IRUGO
, show_brightness
, NULL
);
263 static ssize_t
show_saturation(struct class_device
*class_dev
, char *buf
)
265 struct video_device
*vdev
= to_video_device(class_dev
);
266 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
267 struct v4l2_control ctrl
;
268 ctrl
.id
= V4L2_CID_SATURATION
;
270 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
271 return sprintf(buf
, "%d\n", ctrl
.value
>> 8);
273 static CLASS_DEVICE_ATTR(saturation
, S_IRUGO
, show_saturation
, NULL
);
275 static ssize_t
show_streaming(struct class_device
*class_dev
, char *buf
)
277 struct video_device
*vdev
= to_video_device(class_dev
);
278 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
279 return sprintf(buf
, "%s\n", YES_NO(usbvision
->streaming
==Stream_On
?1:0));
281 static CLASS_DEVICE_ATTR(streaming
, S_IRUGO
, show_streaming
, NULL
);
283 static ssize_t
show_compression(struct class_device
*class_dev
, char *buf
)
285 struct video_device
*vdev
= to_video_device(class_dev
);
286 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
287 return sprintf(buf
, "%s\n", YES_NO(usbvision
->isocMode
==ISOC_MODE_COMPRESS
));
289 static CLASS_DEVICE_ATTR(compression
, S_IRUGO
, show_compression
, NULL
);
291 static ssize_t
show_device_bridge(struct class_device
*class_dev
, char *buf
)
293 struct video_device
*vdev
= to_video_device(class_dev
);
294 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
295 return sprintf(buf
, "%d\n", usbvision
->bridgeType
);
297 static CLASS_DEVICE_ATTR(bridge
, S_IRUGO
, show_device_bridge
, NULL
);
299 static void usbvision_create_sysfs(struct video_device
*vdev
)
303 res
=video_device_create_file(vdev
, &class_device_attr_version
);
304 res
=video_device_create_file(vdev
, &class_device_attr_model
);
305 res
=video_device_create_file(vdev
, &class_device_attr_hue
);
306 res
=video_device_create_file(vdev
, &class_device_attr_contrast
);
307 res
=video_device_create_file(vdev
, &class_device_attr_brightness
);
308 res
=video_device_create_file(vdev
, &class_device_attr_saturation
);
309 res
=video_device_create_file(vdev
, &class_device_attr_streaming
);
310 res
=video_device_create_file(vdev
, &class_device_attr_compression
);
311 res
=video_device_create_file(vdev
, &class_device_attr_bridge
);
315 static void usbvision_remove_sysfs(struct video_device
*vdev
)
318 video_device_remove_file(vdev
, &class_device_attr_version
);
319 video_device_remove_file(vdev
, &class_device_attr_model
);
320 video_device_remove_file(vdev
, &class_device_attr_hue
);
321 video_device_remove_file(vdev
, &class_device_attr_contrast
);
322 video_device_remove_file(vdev
, &class_device_attr_brightness
);
323 video_device_remove_file(vdev
, &class_device_attr_saturation
);
324 video_device_remove_file(vdev
, &class_device_attr_streaming
);
325 video_device_remove_file(vdev
, &class_device_attr_compression
);
326 video_device_remove_file(vdev
, &class_device_attr_bridge
);
334 * This is part of Video 4 Linux API. The driver can be opened by one
335 * client only (checks internal counter 'usbvision->user'). The procedure
336 * then allocates buffers needed for video processing.
339 static int usbvision_v4l2_open(struct inode
*inode
, struct file
*file
)
341 struct video_device
*dev
= video_devdata(file
);
342 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
345 PDEBUG(DBG_IO
, "open");
348 usbvision_reset_powerOffTimer(usbvision
);
353 /* Allocate memory for the frame buffers */
354 errCode
= usbvision_frames_alloc(usbvision
);
356 /* Allocate memory for the scratch ring buffer */
357 errCode
= usbvision_scratch_alloc(usbvision
);
359 /* Allocate memory for the USB S buffers */
360 errCode
= usbvision_sbuf_alloc(usbvision
);
361 if ((!errCode
) && (usbvision
->isocMode
==ISOC_MODE_COMPRESS
)) {
362 /* Allocate intermediate decompression buffers only if needed */
363 errCode
= usbvision_decompress_alloc(usbvision
);
368 /* Deallocate all buffers if trouble */
369 usbvision_frames_free(usbvision
);
370 usbvision_scratch_free(usbvision
);
371 usbvision_sbuf_free(usbvision
);
372 usbvision_decompress_free(usbvision
);
376 /* If so far no errors then we shall start the camera */
378 down(&usbvision
->lock
);
379 if (usbvision
->power
== 0) {
380 usbvision_power_on(usbvision
);
381 usbvision_init_i2c(usbvision
);
384 /* Send init sequence only once, it's large! */
385 if (!usbvision
->initialized
) {
387 setup_ok
= usbvision_setup(usbvision
,isocMode
);
389 usbvision
->initialized
= 1;
395 usbvision_begin_streaming(usbvision
);
396 errCode
= usbvision_init_isoc(usbvision
);
397 /* device needs to be initialized before isoc transfer */
398 usbvision_muxsel(usbvision
,0);
403 usbvision_i2c_usb_del_bus(&usbvision
->i2c_adap
);
404 usbvision_power_off(usbvision
);
405 usbvision
->initialized
= 0;
408 up(&usbvision
->lock
);
415 usbvision_empty_framequeues(usbvision
);
417 PDEBUG(DBG_IO
, "success");
422 * usbvision_v4l2_close()
424 * This is part of Video 4 Linux API. The procedure
425 * stops streaming and deallocates all buffers that were earlier
426 * allocated in usbvision_v4l2_open().
429 static int usbvision_v4l2_close(struct inode
*inode
, struct file
*file
)
431 struct video_device
*dev
= video_devdata(file
);
432 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
434 PDEBUG(DBG_IO
, "close");
435 down(&usbvision
->lock
);
437 usbvision_audio_off(usbvision
);
438 usbvision_restart_isoc(usbvision
);
439 usbvision_stop_isoc(usbvision
);
441 usbvision_decompress_free(usbvision
);
442 usbvision_rvfree(usbvision
->fbuf
, usbvision
->fbuf_size
);
443 usbvision_scratch_free(usbvision
);
444 usbvision_sbuf_free(usbvision
);
449 /* power off in a little while to avoid off/on every close/open short sequences */
450 usbvision_set_powerOffTimer(usbvision
);
451 usbvision
->initialized
= 0;
454 up(&usbvision
->lock
);
456 if (usbvision
->remove_pending
) {
457 info("%s: Final disconnect", __FUNCTION__
);
458 usbvision_release(usbvision
);
461 PDEBUG(DBG_IO
, "success");
471 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
474 static int usbvision_v4l2_do_ioctl(struct inode
*inode
, struct file
*file
,
475 unsigned int cmd
, void *arg
)
477 struct video_device
*dev
= video_devdata(file
);
478 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
480 if (!USBVISION_IS_OPERATIONAL(usbvision
))
485 #ifdef CONFIG_VIDEO_ADV_DEBUG
486 /* ioctls to allow direct acces to the NT100x registers */
487 case VIDIOC_INT_G_REGISTER
:
489 struct v4l2_register
*reg
= arg
;
492 if (reg
->i2c_id
!= 0)
494 /* NT100x has a 8-bit register space */
495 errCode
= usbvision_read_reg(usbvision
, reg
->reg
&0xff);
497 err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__
, errCode
);
500 reg
->val
=(unsigned char)errCode
;
501 PDEBUG(DBG_IOCTL
, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
502 (unsigned int)reg
->reg
, reg
->val
);
503 errCode
= 0; // No error
507 case VIDIOC_INT_S_REGISTER
:
509 struct v4l2_register
*reg
= arg
;
512 if (reg
->i2c_id
!= 0)
514 if (!capable(CAP_SYS_ADMIN
))
516 errCode
= usbvision_write_reg(usbvision
, reg
->reg
&0xff, reg
->val
);
518 err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__
, errCode
);
521 PDEBUG(DBG_IOCTL
, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
522 (unsigned int)reg
->reg
, reg
->val
);
528 case VIDIOC_QUERYCAP
:
530 struct v4l2_capability
*vc
=arg
;
532 memset(vc
, 0, sizeof(*vc
));
533 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
534 strlcpy(vc
->card
, usbvision_device_data
[usbvision
->DevModel
].ModelString
,
536 strlcpy(vc
->bus_info
, usbvision
->dev
->dev
.bus_id
,
537 sizeof(vc
->bus_info
));
538 vc
->version
= USBVISION_DRIVER_VERSION
;
539 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
543 (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
544 PDEBUG(DBG_IOCTL
, "VIDIOC_QUERYCAP");
547 case VIDIOC_ENUMINPUT
:
549 struct v4l2_input
*vi
= arg
;
552 if ((vi
->index
>= usbvision
->video_inputs
) || (vi
->index
< 0) )
554 if (usbvision
->have_tuner
) {
558 chan
= vi
->index
+ 1; //skip Television string
562 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
563 strcpy(vi
->name
, "White Video Input");
566 strcpy(vi
->name
, "Television");
567 vi
->type
= V4L2_INPUT_TYPE_TUNER
;
570 vi
->std
= V4L2_STD_PAL
| V4L2_STD_NTSC
| V4L2_STD_SECAM
;
574 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
575 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
576 strcpy(vi
->name
, "Green Video Input");
579 strcpy(vi
->name
, "Composite Video Input");
581 vi
->std
= V4L2_STD_PAL
;
584 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
585 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
586 strcpy(vi
->name
, "Yellow Video Input");
589 strcpy(vi
->name
, "S-Video Input");
591 vi
->std
= V4L2_STD_PAL
;
594 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
595 strcpy(vi
->name
, "Red Video Input");
596 vi
->std
= V4L2_STD_PAL
;
599 PDEBUG(DBG_IOCTL
, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
600 vi
->name
, vi
->index
, vi
->tuner
,vi
->type
,(int)vi
->std
);
605 struct v4l2_standard
*e
= arg
;
612 ret
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
613 tvnorms
[e
->index
].name
);
622 *input
= usbvision
->ctl_input
;
628 if ((*input
>= usbvision
->video_inputs
) || (*input
< 0) )
630 usbvision
->ctl_input
= *input
;
632 down(&usbvision
->lock
);
633 usbvision_muxsel(usbvision
, usbvision
->ctl_input
);
634 usbvision_set_input(usbvision
);
635 usbvision_set_output(usbvision
, usbvision
->curwidth
, usbvision
->curheight
);
636 up(&usbvision
->lock
);
641 v4l2_std_id
*id
= arg
;
643 *id
= usbvision
->tvnorm
->id
;
645 PDEBUG(DBG_IOCTL
, "VIDIOC_G_STD std_id=%s", usbvision
->tvnorm
->name
);
650 v4l2_std_id
*id
= arg
;
653 for (i
= 0; i
< TVNORMS
; i
++)
654 if (*id
== tvnorms
[i
].id
)
657 for (i
= 0; i
< TVNORMS
; i
++)
658 if (*id
& tvnorms
[i
].id
)
663 down(&usbvision
->lock
);
664 usbvision
->tvnorm
= &tvnorms
[i
];
666 call_i2c_clients(usbvision
, VIDIOC_S_STD
,
667 &usbvision
->tvnorm
->id
);
669 up(&usbvision
->lock
);
671 PDEBUG(DBG_IOCTL
, "VIDIOC_S_STD std_id=%s", usbvision
->tvnorm
->name
);
676 struct v4l2_tuner
*vt
= arg
;
678 if (!usbvision
->have_tuner
|| vt
->index
) // Only tuner 0
680 strcpy(vt
->name
, "Television");
681 /* Let clients fill in the remainder of this struct */
682 call_i2c_clients(usbvision
,VIDIOC_G_TUNER
,vt
);
684 PDEBUG(DBG_IOCTL
, "VIDIOC_G_TUNER signal=%x, afc=%x",vt
->signal
,vt
->afc
);
689 struct v4l2_tuner
*vt
= arg
;
691 // Only no or one tuner for now
692 if (!usbvision
->have_tuner
|| vt
->index
)
694 /* let clients handle this */
695 call_i2c_clients(usbvision
,VIDIOC_S_TUNER
,vt
);
697 PDEBUG(DBG_IOCTL
, "VIDIOC_S_TUNER");
700 case VIDIOC_G_FREQUENCY
:
702 struct v4l2_frequency
*freq
= arg
;
704 freq
->tuner
= 0; // Only one tuner
705 freq
->type
= V4L2_TUNER_ANALOG_TV
;
706 freq
->frequency
= usbvision
->freq
;
707 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq
->frequency
);
710 case VIDIOC_S_FREQUENCY
:
712 struct v4l2_frequency
*freq
= arg
;
714 // Only no or one tuner for now
715 if (!usbvision
->have_tuner
|| freq
->tuner
)
718 usbvision
->freq
= freq
->frequency
;
719 call_i2c_clients(usbvision
, cmd
, freq
);
720 PDEBUG(DBG_IOCTL
, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq
->frequency
);
725 struct v4l2_audio
*v
= arg
;
726 memset(v
,0, sizeof(v
));
727 strcpy(v
->name
, "TV");
728 PDEBUG(DBG_IOCTL
, "VIDIOC_G_AUDIO");
733 struct v4l2_audio
*v
= arg
;
737 PDEBUG(DBG_IOCTL
, "VIDIOC_S_AUDIO");
740 case VIDIOC_QUERYCTRL
:
742 struct v4l2_queryctrl
*ctrl
= arg
;
745 memset(ctrl
,0,sizeof(*ctrl
));
748 call_i2c_clients(usbvision
, cmd
, arg
);
755 PDEBUG(DBG_IOCTL
,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl
->id
,ctrl
->type
);
759 struct v4l2_control
*ctrl
= arg
;
760 PDEBUG(DBG_IOCTL
,"VIDIOC_G_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
761 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, ctrl
);
766 struct v4l2_control
*ctrl
= arg
;
768 PDEBUG(DBG_IOCTL
, "VIDIOC_S_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
769 call_i2c_clients(usbvision
, VIDIOC_S_CTRL
, ctrl
);
774 struct v4l2_requestbuffers
*vr
= arg
;
777 RESTRICT_TO_RANGE(vr
->count
,1,USBVISION_NUMFRAMES
);
779 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
780 if((vr
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) ||
781 (vr
->memory
!= V4L2_MEMORY_MMAP
))
784 if(usbvision
->streaming
== Stream_On
) {
785 if ((ret
= usbvision_stream_interrupt(usbvision
)))
789 usbvision_empty_framequeues(usbvision
);
791 usbvision
->curFrame
= NULL
;
793 PDEBUG(DBG_IOCTL
, "VIDIOC_REQBUFS count=%d",vr
->count
);
796 case VIDIOC_QUERYBUF
:
798 struct v4l2_buffer
*vb
= arg
;
799 struct usbvision_frame
*frame
;
801 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
803 if(vb
->type
!= V4L2_CAP_VIDEO_CAPTURE
) {
806 if(vb
->index
>=USBVISION_NUMFRAMES
) {
809 // Updating the corresponding frame state
811 frame
= &usbvision
->frame
[vb
->index
];
812 if(frame
->grabstate
>= FrameState_Ready
)
813 vb
->flags
|= V4L2_BUF_FLAG_QUEUED
;
814 if(frame
->grabstate
>= FrameState_Done
)
815 vb
->flags
|= V4L2_BUF_FLAG_DONE
;
816 if(frame
->grabstate
== FrameState_Unused
)
817 vb
->flags
|= V4L2_BUF_FLAG_MAPPED
;
818 vb
->memory
= V4L2_MEMORY_MMAP
;
820 vb
->m
.offset
= vb
->index
*usbvision
->max_frame_size
;
822 vb
->memory
= V4L2_MEMORY_MMAP
;
823 vb
->field
= V4L2_FIELD_NONE
;
824 vb
->length
= usbvision
->max_frame_size
;
825 vb
->timestamp
= usbvision
->frame
[vb
->index
].timestamp
;
826 vb
->sequence
= usbvision
->frame
[vb
->index
].sequence
;
831 struct v4l2_buffer
*vb
= arg
;
832 struct usbvision_frame
*frame
;
833 unsigned long lock_flags
;
835 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
836 if(vb
->type
!= V4L2_CAP_VIDEO_CAPTURE
) {
839 if(vb
->index
>=USBVISION_NUMFRAMES
) {
843 frame
= &usbvision
->frame
[vb
->index
];
845 if (frame
->grabstate
!= FrameState_Unused
) {
849 /* Mark it as ready and enqueue frame */
850 frame
->grabstate
= FrameState_Ready
;
851 frame
->scanstate
= ScanState_Scanning
;
852 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
854 vb
->flags
&= ~V4L2_BUF_FLAG_DONE
;
856 /* set v4l2_format index */
857 frame
->v4l2_format
= usbvision
->palette
;
859 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
860 list_add_tail(&usbvision
->frame
[vb
->index
].frame
, &usbvision
->inqueue
);
861 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
863 PDEBUG(DBG_IOCTL
, "VIDIOC_QBUF frame #%d",vb
->index
);
868 struct v4l2_buffer
*vb
= arg
;
870 struct usbvision_frame
*f
;
871 unsigned long lock_flags
;
873 if (vb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
876 if (list_empty(&(usbvision
->outqueue
))) {
877 if (usbvision
->streaming
== Stream_Idle
)
879 ret
= wait_event_interruptible
880 (usbvision
->wait_frame
,
881 !list_empty(&(usbvision
->outqueue
)));
886 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
887 f
= list_entry(usbvision
->outqueue
.next
,
888 struct usbvision_frame
, frame
);
889 list_del(usbvision
->outqueue
.next
);
890 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
892 f
->grabstate
= FrameState_Unused
;
894 vb
->memory
= V4L2_MEMORY_MMAP
;
895 vb
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_QUEUED
| V4L2_BUF_FLAG_DONE
;
896 vb
->index
= f
->index
;
897 vb
->sequence
= f
->sequence
;
898 vb
->timestamp
= f
->timestamp
;
899 vb
->field
= V4L2_FIELD_NONE
;
900 vb
->bytesused
= f
->scanlength
;
904 case VIDIOC_STREAMON
:
906 int b
=V4L2_BUF_TYPE_VIDEO_CAPTURE
;
908 usbvision
->streaming
= Stream_On
;
910 call_i2c_clients(usbvision
,VIDIOC_STREAMON
, &b
);
912 PDEBUG(DBG_IOCTL
, "VIDIOC_STREAMON");
916 case VIDIOC_STREAMOFF
:
919 int b
=V4L2_BUF_TYPE_VIDEO_CAPTURE
;
921 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
924 if(usbvision
->streaming
== Stream_On
) {
925 usbvision_stream_interrupt(usbvision
);
926 // Stop all video streamings
927 call_i2c_clients(usbvision
,VIDIOC_STREAMOFF
, &b
);
929 usbvision_empty_framequeues(usbvision
);
931 PDEBUG(DBG_IOCTL
, "VIDIOC_STREAMOFF");
934 case VIDIOC_ENUM_FMT
:
936 struct v4l2_fmtdesc
*vfd
= arg
;
938 if(vfd
->index
>=USBVISION_SUPPORTED_PALETTES
-1) {
942 vfd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
943 strcpy(vfd
->description
,usbvision_v4l2_format
[vfd
->index
].desc
);
944 vfd
->pixelformat
= usbvision_v4l2_format
[vfd
->index
].format
;
945 memset(vfd
->reserved
, 0, sizeof(vfd
->reserved
));
950 struct v4l2_format
*vf
= arg
;
953 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
955 vf
->fmt
.pix
.width
= usbvision
->curwidth
;
956 vf
->fmt
.pix
.height
= usbvision
->curheight
;
957 vf
->fmt
.pix
.pixelformat
= usbvision
->palette
.format
;
958 vf
->fmt
.pix
.bytesperline
= usbvision
->curwidth
*usbvision
->palette
.bytes_per_pixel
;
959 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*usbvision
->curheight
;
960 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
961 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
965 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FMT invalid type %d",vf
->type
);
968 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FMT w=%d, h=%d",vf
->fmt
.win
.w
.width
, vf
->fmt
.win
.w
.height
);
974 struct v4l2_format
*vf
= arg
;
978 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
980 /* Find requested format in available ones */
981 for(formatIdx
=0;formatIdx
<USBVISION_SUPPORTED_PALETTES
;formatIdx
++) {
982 if(vf
->fmt
.pix
.pixelformat
== usbvision_v4l2_format
[formatIdx
].format
) {
983 usbvision
->palette
= usbvision_v4l2_format
[formatIdx
];
988 if(formatIdx
== USBVISION_SUPPORTED_PALETTES
) {
991 RESTRICT_TO_RANGE(vf
->fmt
.pix
.width
, MIN_FRAME_WIDTH
, MAX_FRAME_WIDTH
);
992 RESTRICT_TO_RANGE(vf
->fmt
.pix
.height
, MIN_FRAME_HEIGHT
, MAX_FRAME_HEIGHT
);
994 /* stop io in case it is already in progress */
995 if(usbvision
->streaming
== Stream_On
) {
996 if ((ret
= usbvision_stream_interrupt(usbvision
)))
999 usbvision_empty_framequeues(usbvision
);
1001 usbvision
->curFrame
= NULL
;
1003 // by now we are committed to the new data...
1004 down(&usbvision
->lock
);
1005 usbvision_set_output(usbvision
, vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
);
1006 up(&usbvision
->lock
);
1008 PDEBUG(DBG_IOCTL
, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1009 vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
,usbvision
->palette
.desc
);
1017 return -ENOIOCTLCMD
;
1022 static int usbvision_v4l2_ioctl(struct inode
*inode
, struct file
*file
,
1023 unsigned int cmd
, unsigned long arg
)
1025 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_v4l2_do_ioctl
);
1029 static ssize_t
usbvision_v4l2_read(struct file
*file
, char *buf
,
1030 size_t count
, loff_t
*ppos
)
1032 struct video_device
*dev
= video_devdata(file
);
1033 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1034 int noblock
= file
->f_flags
& O_NONBLOCK
;
1035 unsigned long lock_flags
;
1039 struct usbvision_frame
*frame
;
1041 PDEBUG(DBG_IO
, "%s: %ld bytes, noblock=%d", __FUNCTION__
, (unsigned long)count
, noblock
);
1043 if (!USBVISION_IS_OPERATIONAL(usbvision
) || (buf
== NULL
))
1046 /* no stream is running, make it running ! */
1047 usbvision
->streaming
= Stream_On
;
1048 call_i2c_clients(usbvision
,VIDIOC_STREAMON
, NULL
);
1050 /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1051 for(i
=0;i
<USBVISION_NUMFRAMES
;i
++) {
1052 frame
= &usbvision
->frame
[i
];
1053 if(frame
->grabstate
== FrameState_Unused
) {
1054 /* Mark it as ready and enqueue frame */
1055 frame
->grabstate
= FrameState_Ready
;
1056 frame
->scanstate
= ScanState_Scanning
;
1057 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
1059 /* set v4l2_format index */
1060 frame
->v4l2_format
= usbvision
->palette
;
1062 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1063 list_add_tail(&frame
->frame
, &usbvision
->inqueue
);
1064 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1068 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1069 if (list_empty(&(usbvision
->outqueue
))) {
1073 ret
= wait_event_interruptible
1074 (usbvision
->wait_frame
,
1075 !list_empty(&(usbvision
->outqueue
)));
1080 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1081 frame
= list_entry(usbvision
->outqueue
.next
,
1082 struct usbvision_frame
, frame
);
1083 list_del(usbvision
->outqueue
.next
);
1084 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1086 /* An error returns an empty frame */
1087 if (frame
->grabstate
== FrameState_Error
) {
1088 frame
->bytes_read
= 0;
1092 PDEBUG(DBG_IO
, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__
,
1093 frame
->index
, frame
->bytes_read
, frame
->scanlength
);
1095 /* copy bytes to user space; we allow for partials reads */
1096 if ((count
+ frame
->bytes_read
) > (unsigned long)frame
->scanlength
)
1097 count
= frame
->scanlength
- frame
->bytes_read
;
1099 if (copy_to_user(buf
, frame
->data
+ frame
->bytes_read
, count
)) {
1103 frame
->bytes_read
+= count
;
1104 PDEBUG(DBG_IO
, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__
,
1105 (unsigned long)count
, frame
->bytes_read
);
1107 // For now, forget the frame if it has not been read in one shot.
1108 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1109 frame
->bytes_read
= 0;
1111 /* Mark it as available to be used again. */
1112 usbvision
->frame
[frmx
].grabstate
= FrameState_Unused
;
1118 static int usbvision_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1120 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1121 start
= vma
->vm_start
;
1125 struct video_device
*dev
= video_devdata(file
);
1126 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1128 down(&usbvision
->lock
);
1130 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1131 up(&usbvision
->lock
);
1135 if (!(vma
->vm_flags
& VM_WRITE
) ||
1136 size
!= PAGE_ALIGN(usbvision
->max_frame_size
)) {
1137 up(&usbvision
->lock
);
1141 for (i
= 0; i
< USBVISION_NUMFRAMES
; i
++) {
1142 if (((usbvision
->max_frame_size
*i
) >> PAGE_SHIFT
) == vma
->vm_pgoff
)
1145 if (i
== USBVISION_NUMFRAMES
) {
1146 PDEBUG(DBG_FUNC
, "mmap: user supplied mapping address is out of range");
1147 up(&usbvision
->lock
);
1151 /* VM_IO is eventually going to replace PageReserved altogether */
1152 vma
->vm_flags
|= VM_IO
;
1153 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1155 pos
= usbvision
->frame
[i
].data
;
1158 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1159 PDEBUG(DBG_FUNC
, "mmap: vm_insert_page failed");
1160 up(&usbvision
->lock
);
1168 up(&usbvision
->lock
);
1174 * Here comes the stuff for radio on usbvision based devices
1177 static int usbvision_radio_open(struct inode
*inode
, struct file
*file
)
1179 struct video_device
*dev
= video_devdata(file
);
1180 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1181 struct v4l2_frequency freq
;
1184 PDEBUG(DBG_IO
, "%s:", __FUNCTION__
);
1186 down(&usbvision
->lock
);
1188 if (usbvision
->user
) {
1189 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__
);
1194 usbvision_reset_powerOffTimer(usbvision
);
1195 if (usbvision
->power
== 0) {
1196 usbvision_power_on(usbvision
);
1197 usbvision_init_i2c(usbvision
);
1201 // If so far no errors then we shall start the radio
1202 usbvision
->radio
= 1;
1203 call_i2c_clients(usbvision
,AUDC_SET_RADIO
,&usbvision
->tuner_type
);
1204 freq
.frequency
= 1517; //SWR3 @ 94.8MHz
1205 call_i2c_clients(usbvision
, VIDIOC_S_FREQUENCY
, &freq
);
1206 usbvision_set_audio(usbvision
, USBVISION_AUDIO_RADIO
);
1211 if (PowerOnAtOpen
) {
1212 usbvision_i2c_usb_del_bus(&usbvision
->i2c_adap
);
1213 usbvision_power_off(usbvision
);
1214 usbvision
->initialized
= 0;
1217 up(&usbvision
->lock
);
1222 static int usbvision_radio_close(struct inode
*inode
, struct file
*file
)
1224 struct video_device
*dev
= video_devdata(file
);
1225 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1230 down(&usbvision
->lock
);
1232 usbvision_audio_off(usbvision
);
1236 if (PowerOnAtOpen
) {
1237 usbvision_set_powerOffTimer(usbvision
);
1238 usbvision
->initialized
= 0;
1241 up(&usbvision
->lock
);
1243 if (usbvision
->remove_pending
) {
1244 info("%s: Final disconnect", __FUNCTION__
);
1245 usbvision_release(usbvision
);
1249 PDEBUG(DBG_IO
, "success");
1254 static int usbvision_do_radio_ioctl(struct inode
*inode
, struct file
*file
,
1255 unsigned int cmd
, void *arg
)
1257 struct video_device
*dev
= video_devdata(file
);
1258 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1260 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1264 case VIDIOC_QUERYCAP
:
1266 struct v4l2_capability
*vc
=arg
;
1268 memset(vc
, 0, sizeof(*vc
));
1269 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
1270 strlcpy(vc
->card
, usbvision_device_data
[usbvision
->DevModel
].ModelString
,
1272 strlcpy(vc
->bus_info
, usbvision
->dev
->dev
.bus_id
,
1273 sizeof(vc
->bus_info
));
1274 vc
->version
= USBVISION_DRIVER_VERSION
;
1275 vc
->capabilities
= (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
1276 PDEBUG(DBG_IO
, "VIDIOC_QUERYCAP");
1279 case VIDIOC_QUERYCTRL
:
1281 struct v4l2_queryctrl
*ctrl
= arg
;
1284 memset(ctrl
,0,sizeof(*ctrl
));
1287 call_i2c_clients(usbvision
, cmd
, arg
);
1288 PDEBUG(DBG_IO
,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl
->id
,ctrl
->type
);
1298 struct v4l2_control
*ctrl
= arg
;
1300 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, ctrl
);
1301 PDEBUG(DBG_IO
,"VIDIOC_G_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
1306 struct v4l2_control
*ctrl
= arg
;
1308 call_i2c_clients(usbvision
, VIDIOC_S_CTRL
, ctrl
);
1309 PDEBUG(DBG_IO
, "VIDIOC_S_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
1312 case VIDIOC_G_TUNER
:
1314 struct v4l2_tuner
*t
= arg
;
1319 memset(t
,0,sizeof(*t
));
1320 strcpy(t
->name
, "Radio");
1321 t
->type
= V4L2_TUNER_RADIO
;
1323 /* Let clients fill in the remainder of this struct */
1324 call_i2c_clients(usbvision
,VIDIOC_G_TUNER
,t
);
1325 PDEBUG(DBG_IO
, "VIDIOC_G_TUNER signal=%x, afc=%x",t
->signal
,t
->afc
);
1328 case VIDIOC_S_TUNER
:
1330 struct v4l2_tuner
*vt
= arg
;
1332 // Only no or one tuner for now
1333 if (!usbvision
->have_tuner
|| vt
->index
)
1335 /* let clients handle this */
1336 call_i2c_clients(usbvision
,VIDIOC_S_TUNER
,vt
);
1338 PDEBUG(DBG_IO
, "VIDIOC_S_TUNER");
1341 case VIDIOC_G_AUDIO
:
1343 struct v4l2_audio
*a
= arg
;
1345 memset(a
,0,sizeof(*a
));
1346 strcpy(a
->name
,"Radio");
1347 PDEBUG(DBG_IO
, "VIDIOC_G_AUDIO");
1350 case VIDIOC_S_AUDIO
:
1351 case VIDIOC_S_INPUT
:
1355 case VIDIOC_G_FREQUENCY
:
1357 struct v4l2_frequency
*f
= arg
;
1359 memset(f
,0,sizeof(*f
));
1361 f
->type
= V4L2_TUNER_RADIO
;
1362 f
->frequency
= usbvision
->freq
;
1363 call_i2c_clients(usbvision
, cmd
, f
);
1364 PDEBUG(DBG_IO
, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f
->frequency
);
1368 case VIDIOC_S_FREQUENCY
:
1370 struct v4l2_frequency
*f
= arg
;
1374 usbvision
->freq
= f
->frequency
;
1375 call_i2c_clients(usbvision
, cmd
, f
);
1376 PDEBUG(DBG_IO
, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f
->frequency
);
1382 PDEBUG(DBG_IO
, "%s: Unknown command %x", __FUNCTION__
, cmd
);
1383 return -ENOIOCTLCMD
;
1390 static int usbvision_radio_ioctl(struct inode
*inode
, struct file
*file
,
1391 unsigned int cmd
, unsigned long arg
)
1393 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_do_radio_ioctl
);
1398 * Here comes the stuff for vbi on usbvision based devices
1401 static int usbvision_vbi_open(struct inode
*inode
, struct file
*file
)
1408 static int usbvision_vbi_close(struct inode
*inode
, struct file
*file
)
1414 static int usbvision_do_vbi_ioctl(struct inode
*inode
, struct file
*file
,
1415 unsigned int cmd
, void *arg
)
1421 static int usbvision_vbi_ioctl(struct inode
*inode
, struct file
*file
,
1422 unsigned int cmd
, unsigned long arg
)
1424 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_do_vbi_ioctl
);
1429 // Video registration stuff
1433 static struct file_operations usbvision_fops
= {
1434 .owner
= THIS_MODULE
,
1435 .open
= usbvision_v4l2_open
,
1436 .release
= usbvision_v4l2_close
,
1437 .read
= usbvision_v4l2_read
,
1438 .mmap
= usbvision_v4l2_mmap
,
1439 .ioctl
= usbvision_v4l2_ioctl
,
1440 .llseek
= no_llseek
,
1442 static struct video_device usbvision_video_template
= {
1443 .owner
= THIS_MODULE
,
1444 .type
= VID_TYPE_TUNER
| VID_TYPE_CAPTURE
,
1445 .hardware
= VID_HARDWARE_USBVISION
,
1446 .fops
= &usbvision_fops
,
1447 .name
= "usbvision-video",
1448 .release
= video_device_release
,
1454 static struct file_operations usbvision_radio_fops
= {
1455 .owner
= THIS_MODULE
,
1456 .open
= usbvision_radio_open
,
1457 .release
= usbvision_radio_close
,
1458 .ioctl
= usbvision_radio_ioctl
,
1459 .llseek
= no_llseek
,
1462 static struct video_device usbvision_radio_template
=
1464 .owner
= THIS_MODULE
,
1465 .type
= VID_TYPE_TUNER
,
1466 .hardware
= VID_HARDWARE_USBVISION
,
1467 .fops
= &usbvision_radio_fops
,
1468 .release
= video_device_release
,
1469 .name
= "usbvision-radio",
1475 static struct file_operations usbvision_vbi_fops
= {
1476 .owner
= THIS_MODULE
,
1477 .open
= usbvision_vbi_open
,
1478 .release
= usbvision_vbi_close
,
1479 .ioctl
= usbvision_vbi_ioctl
,
1480 .llseek
= no_llseek
,
1483 static struct video_device usbvision_vbi_template
=
1485 .owner
= THIS_MODULE
,
1486 .type
= VID_TYPE_TUNER
,
1487 .hardware
= VID_HARDWARE_USBVISION
,
1488 .fops
= &usbvision_vbi_fops
,
1489 .release
= video_device_release
,
1490 .name
= "usbvision-vbi",
1495 static struct video_device
*usbvision_vdev_init(struct usb_usbvision
*usbvision
,
1496 struct video_device
*vdev_template
,
1499 struct usb_device
*usb_dev
= usbvision
->dev
;
1500 struct video_device
*vdev
;
1502 if (usb_dev
== NULL
) {
1503 err("%s: usbvision->dev is not set", __FUNCTION__
);
1507 vdev
= video_device_alloc();
1511 *vdev
= *vdev_template
;
1512 // vdev->minor = -1;
1513 vdev
->dev
= &usb_dev
->dev
;
1514 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s", name
);
1515 video_set_drvdata(vdev
, usbvision
);
1519 // unregister video4linux devices
1520 static void usbvision_unregister_video(struct usb_usbvision
*usbvision
)
1523 if (usbvision
->vbi
) {
1524 PDEBUG(DBG_PROBE
, "unregister /dev/vbi%d [v4l2]", usbvision
->vbi
->minor
& 0x1f);
1525 if (usbvision
->vbi
->minor
!= -1) {
1526 video_unregister_device(usbvision
->vbi
);
1529 video_device_release(usbvision
->vbi
);
1531 usbvision
->vbi
= NULL
;
1535 if (usbvision
->rdev
) {
1536 PDEBUG(DBG_PROBE
, "unregister /dev/radio%d [v4l2]", usbvision
->rdev
->minor
& 0x1f);
1537 if (usbvision
->rdev
->minor
!= -1) {
1538 video_unregister_device(usbvision
->rdev
);
1541 video_device_release(usbvision
->rdev
);
1543 usbvision
->rdev
= NULL
;
1547 if (usbvision
->vdev
) {
1548 PDEBUG(DBG_PROBE
, "unregister /dev/video%d [v4l2]", usbvision
->vdev
->minor
& 0x1f);
1549 if (usbvision
->vdev
->minor
!= -1) {
1550 video_unregister_device(usbvision
->vdev
);
1553 video_device_release(usbvision
->vdev
);
1555 usbvision
->vdev
= NULL
;
1559 // register video4linux devices
1560 static int __devinit
usbvision_register_video(struct usb_usbvision
*usbvision
)
1563 usbvision
->vdev
= usbvision_vdev_init(usbvision
, &usbvision_video_template
, "USBVision Video");
1564 if (usbvision
->vdev
== NULL
) {
1567 if (video_register_device(usbvision
->vdev
, VFL_TYPE_GRABBER
, video_nr
)<0) {
1570 info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision
->nr
,usbvision
->vdev
->minor
& 0x1f);
1573 if (usbvision_device_data
[usbvision
->DevModel
].Radio
) {
1574 // usbvision has radio
1575 usbvision
->rdev
= usbvision_vdev_init(usbvision
, &usbvision_radio_template
, "USBVision Radio");
1576 if (usbvision
->rdev
== NULL
) {
1579 if (video_register_device(usbvision
->rdev
, VFL_TYPE_RADIO
, radio_nr
)<0) {
1582 info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision
->nr
, usbvision
->rdev
->minor
& 0x1f);
1585 if (usbvision_device_data
[usbvision
->DevModel
].vbi
) {
1586 usbvision
->vbi
= usbvision_vdev_init(usbvision
, &usbvision_vbi_template
, "USBVision VBI");
1587 if (usbvision
->vdev
== NULL
) {
1590 if (video_register_device(usbvision
->vbi
, VFL_TYPE_VBI
, vbi_nr
)<0) {
1593 info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision
->nr
,usbvision
->vbi
->minor
& 0x1f);
1599 err("USBVision[%d]: video_register_device() failed", usbvision
->nr
);
1600 usbvision_unregister_video(usbvision
);
1607 * This code allocates the struct usb_usbvision. It is filled with default values.
1609 * Returns NULL on error, a pointer to usb_usbvision else.
1612 static struct usb_usbvision
*usbvision_alloc(struct usb_device
*dev
)
1614 struct usb_usbvision
*usbvision
;
1616 if ((usbvision
= kzalloc(sizeof(struct usb_usbvision
), GFP_KERNEL
)) == NULL
) {
1620 usbvision
->dev
= dev
;
1622 init_MUTEX(&usbvision
->lock
); /* to 1 == available */
1624 // prepare control urb for control messages during interrupts
1625 usbvision
->ctrlUrb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
1626 if (usbvision
->ctrlUrb
== NULL
) {
1629 init_waitqueue_head(&usbvision
->ctrlUrb_wq
);
1630 init_MUTEX(&usbvision
->ctrlUrbLock
); /* to 1 == available */
1632 usbvision_init_powerOffTimer(usbvision
);
1637 if (usbvision
&& usbvision
->ctrlUrb
) {
1638 usb_free_urb(usbvision
->ctrlUrb
);
1647 * usbvision_release()
1649 * This code does final release of struct usb_usbvision. This happens
1650 * after the device is disconnected -and- all clients closed their files.
1653 static void usbvision_release(struct usb_usbvision
*usbvision
)
1655 PDEBUG(DBG_PROBE
, "");
1657 down(&usbvision
->lock
);
1659 usbvision_reset_powerOffTimer(usbvision
);
1661 usbvision
->initialized
= 0;
1663 up(&usbvision
->lock
);
1665 usbvision_remove_sysfs(usbvision
->vdev
);
1666 usbvision_unregister_video(usbvision
);
1668 if (usbvision
->ctrlUrb
) {
1669 usb_free_urb(usbvision
->ctrlUrb
);
1674 PDEBUG(DBG_PROBE
, "success");
1678 /******************************** usb interface *****************************************/
1680 static void usbvision_configure_video(struct usb_usbvision
*usbvision
)
1684 if (usbvision
== NULL
)
1687 model
= usbvision
->DevModel
;
1688 usbvision
->palette
= usbvision_v4l2_format
[2]; // V4L2_PIX_FMT_RGB24;
1690 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2
>= 0) {
1691 usbvision
->Vin_Reg2_Preset
= usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2
& 0xff;
1693 usbvision
->Vin_Reg2_Preset
= 0;
1696 for (i
= 0; i
< TVNORMS
; i
++)
1697 if (usbvision_device_data
[model
].VideoNorm
== tvnorms
[i
].mode
)
1701 usbvision
->tvnorm
= &tvnorms
[i
]; /* set default norm */
1703 usbvision
->video_inputs
= usbvision_device_data
[model
].VideoChannels
;
1704 usbvision
->ctl_input
= 0;
1706 /* This should be here to make i2c clients to be able to register */
1707 usbvision_audio_off(usbvision
); //first switch off audio
1708 if (!PowerOnAtOpen
) {
1709 usbvision_power_on(usbvision
); //and then power up the noisy tuner
1710 usbvision_init_i2c(usbvision
);
1717 * This procedure queries device descriptor and accepts the interface
1718 * if it looks like USBVISION video device
1721 static int __devinit
usbvision_probe(struct usb_interface
*intf
, const struct usb_device_id
*devid
)
1723 struct usb_device
*dev
= interface_to_usbdev(intf
);
1724 __u8 ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
1725 const struct usb_host_interface
*interface
;
1726 struct usb_usbvision
*usbvision
= NULL
;
1727 const struct usb_endpoint_descriptor
*endpoint
;
1730 PDEBUG(DBG_PROBE
, "VID=%#04x, PID=%#04x, ifnum=%u",
1731 dev
->descriptor
.idVendor
, dev
->descriptor
.idProduct
, ifnum
);
1732 /* Is it an USBVISION video dev? */
1734 for(model
= 0; usbvision_device_data
[model
].idVendor
; model
++) {
1735 if (le16_to_cpu(dev
->descriptor
.idVendor
) != usbvision_device_data
[model
].idVendor
) {
1738 if (le16_to_cpu(dev
->descriptor
.idProduct
) != usbvision_device_data
[model
].idProduct
) {
1742 info("%s: %s found", __FUNCTION__
, usbvision_device_data
[model
].ModelString
);
1746 if (usbvision_device_data
[model
].idVendor
== 0) {
1747 return -ENODEV
; //no matching device
1749 if (usbvision_device_data
[model
].Interface
>= 0) {
1750 interface
= &dev
->actconfig
->interface
[usbvision_device_data
[model
].Interface
]->altsetting
[0];
1753 interface
= &dev
->actconfig
->interface
[ifnum
]->altsetting
[0];
1755 endpoint
= &interface
->endpoint
[1].desc
;
1756 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_ISOC
) {
1757 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__
, ifnum
);
1758 err("%s: Endpoint attribures %d", __FUNCTION__
, endpoint
->bmAttributes
);
1761 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1762 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__
, ifnum
);
1768 if ((usbvision
= usbvision_alloc(dev
)) == NULL
) {
1769 err("%s: couldn't allocate USBVision struct", __FUNCTION__
);
1772 if (dev
->descriptor
.bNumConfigurations
> 1) {
1773 usbvision
->bridgeType
= BRIDGE_NT1004
;
1775 else if (usbvision_device_data
[model
].ModelString
== "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
1776 usbvision
->bridgeType
= BRIDGE_NT1005
;
1779 usbvision
->bridgeType
= BRIDGE_NT1003
;
1781 PDEBUG(DBG_PROBE
, "bridgeType %d", usbvision
->bridgeType
);
1783 down(&usbvision
->lock
);
1785 usbvision
->nr
= usbvision_nr
++;
1787 usbvision
->have_tuner
= usbvision_device_data
[model
].Tuner
;
1788 if (usbvision
->have_tuner
) {
1789 usbvision
->tuner_type
= usbvision_device_data
[model
].TunerType
;
1792 usbvision
->tuner_addr
= ADDR_UNSET
;
1794 usbvision
->DevModel
= model
;
1795 usbvision
->remove_pending
= 0;
1796 usbvision
->iface
= ifnum
;
1797 usbvision
->ifaceAltInactive
= 0;
1798 usbvision
->ifaceAltActive
= 1;
1799 usbvision
->video_endp
= endpoint
->bEndpointAddress
;
1800 usbvision
->isocPacketSize
= 0;
1801 usbvision
->usb_bandwidth
= 0;
1802 usbvision
->user
= 0;
1803 usbvision
->streaming
= Stream_Off
;
1804 usbvision_register_video(usbvision
);
1805 usbvision_configure_video(usbvision
);
1806 up(&usbvision
->lock
);
1809 usb_set_intfdata (intf
, usbvision
);
1810 usbvision_create_sysfs(usbvision
->vdev
);
1812 PDEBUG(DBG_PROBE
, "success");
1818 * usbvision_disconnect()
1820 * This procedure stops all driver activity, deallocates interface-private
1821 * structure (pointed by 'ptr') and after that driver should be removable
1822 * with no ill consequences.
1825 static void __devexit
usbvision_disconnect(struct usb_interface
*intf
)
1827 struct usb_usbvision
*usbvision
= usb_get_intfdata(intf
);
1829 PDEBUG(DBG_PROBE
, "");
1831 if (usbvision
== NULL
) {
1832 err("%s: usb_get_intfdata() failed", __FUNCTION__
);
1835 usb_set_intfdata (intf
, NULL
);
1837 down(&usbvision
->lock
);
1839 // At this time we ask to cancel outstanding URBs
1840 usbvision_stop_isoc(usbvision
);
1842 if (usbvision
->power
) {
1843 usbvision_i2c_usb_del_bus(&usbvision
->i2c_adap
);
1844 usbvision_power_off(usbvision
);
1846 usbvision
->remove_pending
= 1; // Now all ISO data will be ignored
1848 usb_put_dev(usbvision
->dev
);
1849 usbvision
->dev
= NULL
; // USB device is no more
1851 up(&usbvision
->lock
);
1853 if (usbvision
->user
) {
1854 info("%s: In use, disconnect pending", __FUNCTION__
);
1855 wake_up_interruptible(&usbvision
->wait_frame
);
1856 wake_up_interruptible(&usbvision
->wait_stream
);
1859 usbvision_release(usbvision
);
1862 PDEBUG(DBG_PROBE
, "success");
1866 static struct usb_driver usbvision_driver
= {
1867 .name
= "usbvision",
1868 .id_table
= usbvision_table
,
1869 .probe
= usbvision_probe
,
1870 .disconnect
= usbvision_disconnect
1874 * customdevice_process()
1876 * This procedure preprocesses CustomDevice parameter if any
1879 void customdevice_process(void)
1881 usbvision_device_data
[0]=usbvision_device_data
[1];
1882 usbvision_table
[0]=usbvision_table
[1];
1886 char *parse
=CustomDevice
;
1888 PDEBUG(DBG_PROBE
, "CustomDevide=%s", CustomDevice
);
1890 /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
1891 usbvision_device_data[0].idVendor;
1892 usbvision_device_data[0].idProduct;
1893 usbvision_device_data[0].Interface;
1894 usbvision_device_data[0].Codec;
1895 usbvision_device_data[0].VideoChannels;
1896 usbvision_device_data[0].VideoNorm;
1897 usbvision_device_data[0].AudioChannels;
1898 usbvision_device_data[0].Radio;
1899 usbvision_device_data[0].Tuner;
1900 usbvision_device_data[0].TunerType;
1901 usbvision_device_data[0].Vin_Reg1;
1902 usbvision_device_data[0].Vin_Reg2;
1903 usbvision_device_data[0].X_Offset;
1904 usbvision_device_data[0].Y_Offset;
1905 usbvision_device_data[0].Dvi_yuv;
1906 usbvision_device_data[0].ModelString;
1910 usbvision_device_data
[0].ModelString
="USBVISION Custom Device";
1913 sscanf(parse
,"%x",&usbvision_device_data
[0].idVendor
);
1915 PDEBUG(DBG_PROBE
, "idVendor=0x%.4X", usbvision_device_data
[0].idVendor
);
1917 sscanf(parse
,"%x",&usbvision_device_data
[0].idProduct
);
1919 PDEBUG(DBG_PROBE
, "idProduct=0x%.4X", usbvision_device_data
[0].idProduct
);
1920 sscanf(parse
,"%d",&usbvision_device_data
[0].Interface
);
1922 PDEBUG(DBG_PROBE
, "Interface=%d", usbvision_device_data
[0].Interface
);
1923 sscanf(parse
,"%d",&usbvision_device_data
[0].Codec
);
1925 PDEBUG(DBG_PROBE
, "Codec=%d", usbvision_device_data
[0].Codec
);
1926 sscanf(parse
,"%d",&usbvision_device_data
[0].VideoChannels
);
1928 PDEBUG(DBG_PROBE
, "VideoChannels=%d", usbvision_device_data
[0].VideoChannels
);
1933 PDEBUG(DBG_PROBE
, "VideoNorm=PAL");
1934 usbvision_device_data
[0].VideoNorm
=VIDEO_MODE_PAL
;
1938 PDEBUG(DBG_PROBE
, "VideoNorm=SECAM");
1939 usbvision_device_data
[0].VideoNorm
=VIDEO_MODE_SECAM
;
1943 PDEBUG(DBG_PROBE
, "VideoNorm=NTSC");
1944 usbvision_device_data
[0].VideoNorm
=VIDEO_MODE_NTSC
;
1948 PDEBUG(DBG_PROBE
, "VideoNorm=PAL (by default)");
1949 usbvision_device_data
[0].VideoNorm
=VIDEO_MODE_PAL
;
1954 sscanf(parse
,"%d",&usbvision_device_data
[0].AudioChannels
);
1956 PDEBUG(DBG_PROBE
, "AudioChannels=%d", usbvision_device_data
[0].AudioChannels
);
1957 sscanf(parse
,"%d",&usbvision_device_data
[0].Radio
);
1959 PDEBUG(DBG_PROBE
, "Radio=%d", usbvision_device_data
[0].Radio
);
1960 sscanf(parse
,"%d",&usbvision_device_data
[0].Tuner
);
1962 PDEBUG(DBG_PROBE
, "Tuner=%d", usbvision_device_data
[0].Tuner
);
1963 sscanf(parse
,"%d",&usbvision_device_data
[0].TunerType
);
1965 PDEBUG(DBG_PROBE
, "TunerType=%d", usbvision_device_data
[0].TunerType
);
1966 sscanf(parse
,"%d",&usbvision_device_data
[0].Vin_Reg1
);
1968 PDEBUG(DBG_PROBE
, "Vin_Reg1=%d", usbvision_device_data
[0].Vin_Reg1
);
1969 sscanf(parse
,"%d",&usbvision_device_data
[0].Vin_Reg2
);
1971 PDEBUG(DBG_PROBE
, "Vin_Reg2=%d", usbvision_device_data
[0].Vin_Reg2
);
1972 sscanf(parse
,"%d",&usbvision_device_data
[0].X_Offset
);
1974 PDEBUG(DBG_PROBE
, "X_Offset=%d", usbvision_device_data
[0].X_Offset
);
1975 sscanf(parse
,"%d",&usbvision_device_data
[0].Y_Offset
);
1977 PDEBUG(DBG_PROBE
, "Y_Offset=%d", usbvision_device_data
[0].Y_Offset
);
1978 sscanf(parse
,"%d",&usbvision_device_data
[0].Dvi_yuv
);
1979 PDEBUG(DBG_PROBE
, "Dvi_yuv=%d", usbvision_device_data
[0].Dvi_yuv
);
1981 //add to usbvision_table also
1982 usbvision_table
[0].match_flags
=USB_DEVICE_ID_MATCH_DEVICE
;
1983 usbvision_table
[0].idVendor
=usbvision_device_data
[0].idVendor
;
1984 usbvision_table
[0].idProduct
=usbvision_device_data
[0].idProduct
;
1994 * This code is run to initialize the driver.
1997 static int __init
usbvision_init(void)
2001 PDEBUG(DBG_PROBE
, "");
2003 PDEBUG(DBG_IOCTL
, "IOCTL debugging is enabled [video]");
2004 PDEBUG(DBG_IO
, "IO debugging is enabled [video]");
2005 PDEBUG(DBG_PROBE
, "PROBE debugging is enabled [video]");
2006 PDEBUG(DBG_FUNC
, "FUNC debugging is enabled [video]");
2008 /* disable planar mode support unless compression enabled */
2009 if (isocMode
!= ISOC_MODE_COMPRESS
) {
2010 // FIXME : not the right way to set supported flag
2011 usbvision_v4l2_format
[6].supported
= 0; // V4L2_PIX_FMT_YVU420
2012 usbvision_v4l2_format
[7].supported
= 0; // V4L2_PIX_FMT_YUV422P
2015 customdevice_process();
2017 errCode
= usb_register(&usbvision_driver
);
2020 info(DRIVER_DESC
" : " USBVISION_VERSION_STRING
);
2021 PDEBUG(DBG_PROBE
, "success");
2026 static void __exit
usbvision_exit(void)
2028 PDEBUG(DBG_PROBE
, "");
2030 usb_deregister(&usbvision_driver
);
2031 PDEBUG(DBG_PROBE
, "success");
2034 module_init(usbvision_init
);
2035 module_exit(usbvision_exit
);
2038 * Overrides for Emacs so that we follow Linus's tabbing style.
2039 * ---------------------------------------------------------------------------