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/list.h>
50 #include <linux/timer.h>
51 #include <linux/slab.h>
53 #include <linux/utsname.h>
54 #include <linux/highmem.h>
55 #include <linux/videodev.h>
56 #include <linux/vmalloc.h>
57 #include <linux/module.h>
58 #include <linux/init.h>
59 #include <linux/spinlock.h>
61 #include <linux/videodev2.h>
62 #include <linux/video_decoder.h>
63 #include <linux/i2c.h>
65 #include <media/saa7115.h>
66 #include <media/v4l2-common.h>
67 #include <media/tuner.h>
68 #include <media/audiochip.h>
70 #include <linux/moduleparam.h>
71 #include <linux/workqueue.h>
74 #include <linux/kmod.h>
77 #include "usbvision.h"
78 #include "usbvision-cards.h"
80 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
81 #define DRIVER_NAME "usbvision"
82 #define DRIVER_ALIAS "USBVision"
83 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
84 #define DRIVER_LICENSE "GPL"
85 #define USBVISION_DRIVER_VERSION_MAJOR 0
86 #define USBVISION_DRIVER_VERSION_MINOR 9
87 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
88 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
89 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
91 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
94 #ifdef USBVISION_DEBUG
95 #define PDEBUG(level, fmt, args...) \
96 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
98 #define PDEBUG(level, fmt, args...) do {} while(0)
101 #define DBG_IOCTL 1<<0
103 #define DBG_PROBE 1<<2
104 #define DBG_MMAP 1<<3
107 #define rmspace(str) while(*str==' ') str++;
108 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
111 static int usbvision_nr
= 0; // sequential number of usbvision device
113 static struct usbvision_v4l2_format_st usbvision_v4l2_format
[] = {
114 { 1, 1, 8, V4L2_PIX_FMT_GREY
, "GREY" },
115 { 1, 2, 16, V4L2_PIX_FMT_RGB565
, "RGB565" },
116 { 1, 3, 24, V4L2_PIX_FMT_RGB24
, "RGB24" },
117 { 1, 4, 32, V4L2_PIX_FMT_RGB32
, "RGB32" },
118 { 1, 2, 16, V4L2_PIX_FMT_RGB555
, "RGB555" },
119 { 1, 2, 16, V4L2_PIX_FMT_YUYV
, "YUV422" },
120 { 1, 2, 12, V4L2_PIX_FMT_YVU420
, "YUV420P" }, // 1.5 !
121 { 1, 2, 16, V4L2_PIX_FMT_YUV422P
, "YUV422P" }
124 /* supported tv norms */
125 static struct usbvision_tvnorm tvnorms
[] = {
134 .id
= V4L2_STD_SECAM
,
137 .id
= V4L2_STD_PAL_M
,
141 #define TVNORMS ARRAY_SIZE(tvnorms)
143 // Function prototypes
144 static void usbvision_release(struct usb_usbvision
*usbvision
);
146 // Default initalization of device driver parameters
147 static int isocMode
= ISOC_MODE_COMPRESS
; // Set the default format for ISOC endpoint
148 static int video_debug
= 0; // Set the default Debug Mode of the device driver
149 static int PowerOnAtOpen
= 1; // Set the default device to power on at startup
150 static int video_nr
= -1; // Sequential Number of Video Device
151 static int radio_nr
= -1; // Sequential Number of Radio Device
152 static int vbi_nr
= -1; // Sequential Number of VBI Device
154 // Grab parameters for the device driver
156 #if defined(module_param) // Showing parameters under SYSFS
157 module_param(isocMode
, int, 0444);
158 module_param(video_debug
, int, 0444);
159 module_param(PowerOnAtOpen
, int, 0444);
160 module_param(video_nr
, int, 0444);
161 module_param(radio_nr
, int, 0444);
162 module_param(vbi_nr
, int, 0444);
164 MODULE_PARAM(isocMode
, "i");
165 MODULE_PARM(video_debug
, "i"); // Grab the Debug Mode of the device driver
166 MODULE_PARM(adjustCompression
, "i"); // Grab the compression to be adaptive
167 MODULE_PARM(PowerOnAtOpen
, "i"); // Grab the device to power on at startup
168 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.
169 MODULE_PARM(video_nr
, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
170 MODULE_PARM(radio_nr
, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
171 MODULE_PARM(vbi_nr
, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
174 MODULE_PARM_DESC(isocMode
, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
175 MODULE_PARM_DESC(video_debug
, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
176 MODULE_PARM_DESC(PowerOnAtOpen
, " Set the default device to power on when device is opened. Default: 1 (On)");
177 MODULE_PARM_DESC(video_nr
, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
178 MODULE_PARM_DESC(radio_nr
, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
179 MODULE_PARM_DESC(vbi_nr
, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
183 MODULE_AUTHOR(DRIVER_AUTHOR
);
184 MODULE_DESCRIPTION(DRIVER_DESC
);
185 MODULE_LICENSE(DRIVER_LICENSE
);
186 MODULE_VERSION(USBVISION_VERSION_STRING
);
187 MODULE_ALIAS(DRIVER_ALIAS
);
190 /****************************************************************************************/
191 /* SYSFS Code - Copied from the stv680.c usb module. */
192 /* Device information is located at /sys/class/video4linux/video0 */
193 /* Device parameters information is located at /sys/module/usbvision */
194 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
195 /****************************************************************************************/
198 #define YES_NO(x) ((x) ? "Yes" : "No")
200 static inline struct usb_usbvision
*cd_to_usbvision(struct class_device
*cd
)
202 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
203 return video_get_drvdata(vdev
);
206 static ssize_t
show_version(struct class_device
*cd
, char *buf
)
208 return sprintf(buf
, "%s\n", USBVISION_VERSION_STRING
);
210 static CLASS_DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
212 static ssize_t
show_model(struct class_device
*cd
, char *buf
)
214 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
215 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
216 return sprintf(buf
, "%s\n", usbvision_device_data
[usbvision
->DevModel
].ModelString
);
218 static CLASS_DEVICE_ATTR(model
, S_IRUGO
, show_model
, NULL
);
220 static ssize_t
show_hue(struct class_device
*cd
, char *buf
)
222 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
223 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
224 struct v4l2_control ctrl
;
225 ctrl
.id
= V4L2_CID_HUE
;
228 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
229 return sprintf(buf
, "%d\n", ctrl
.value
);
231 static CLASS_DEVICE_ATTR(hue
, S_IRUGO
, show_hue
, NULL
);
233 static ssize_t
show_contrast(struct class_device
*cd
, char *buf
)
235 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
236 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
237 struct v4l2_control ctrl
;
238 ctrl
.id
= V4L2_CID_CONTRAST
;
241 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
242 return sprintf(buf
, "%d\n", ctrl
.value
);
244 static CLASS_DEVICE_ATTR(contrast
, S_IRUGO
, show_contrast
, NULL
);
246 static ssize_t
show_brightness(struct class_device
*cd
, char *buf
)
248 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
249 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
250 struct v4l2_control ctrl
;
251 ctrl
.id
= V4L2_CID_BRIGHTNESS
;
254 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
255 return sprintf(buf
, "%d\n", ctrl
.value
);
257 static CLASS_DEVICE_ATTR(brightness
, S_IRUGO
, show_brightness
, NULL
);
259 static ssize_t
show_saturation(struct class_device
*cd
, char *buf
)
261 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
262 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
263 struct v4l2_control ctrl
;
264 ctrl
.id
= V4L2_CID_SATURATION
;
267 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, &ctrl
);
268 return sprintf(buf
, "%d\n", ctrl
.value
);
270 static CLASS_DEVICE_ATTR(saturation
, S_IRUGO
, show_saturation
, NULL
);
272 static ssize_t
show_streaming(struct class_device
*cd
, char *buf
)
274 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
275 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
276 return sprintf(buf
, "%s\n", YES_NO(usbvision
->streaming
==Stream_On
?1:0));
278 static CLASS_DEVICE_ATTR(streaming
, S_IRUGO
, show_streaming
, NULL
);
280 static ssize_t
show_compression(struct class_device
*cd
, char *buf
)
282 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
283 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
284 return sprintf(buf
, "%s\n", YES_NO(usbvision
->isocMode
==ISOC_MODE_COMPRESS
));
286 static CLASS_DEVICE_ATTR(compression
, S_IRUGO
, show_compression
, NULL
);
288 static ssize_t
show_device_bridge(struct class_device
*cd
, char *buf
)
290 struct video_device
*vdev
= container_of(cd
, struct video_device
, class_dev
);
291 struct usb_usbvision
*usbvision
= video_get_drvdata(vdev
);
292 return sprintf(buf
, "%d\n", usbvision
->bridgeType
);
294 static CLASS_DEVICE_ATTR(bridge
, S_IRUGO
, show_device_bridge
, NULL
);
296 static void usbvision_create_sysfs(struct video_device
*vdev
)
302 res
=class_device_create_file(&vdev
->class_dev
,
303 &class_device_attr_version
);
306 res
=class_device_create_file(&vdev
->class_dev
,
307 &class_device_attr_model
);
310 res
=class_device_create_file(&vdev
->class_dev
,
311 &class_device_attr_hue
);
314 res
=class_device_create_file(&vdev
->class_dev
,
315 &class_device_attr_contrast
);
318 res
=class_device_create_file(&vdev
->class_dev
,
319 &class_device_attr_brightness
);
322 res
=class_device_create_file(&vdev
->class_dev
,
323 &class_device_attr_saturation
);
326 res
=class_device_create_file(&vdev
->class_dev
,
327 &class_device_attr_streaming
);
330 res
=class_device_create_file(&vdev
->class_dev
,
331 &class_device_attr_compression
);
334 res
=class_device_create_file(&vdev
->class_dev
,
335 &class_device_attr_bridge
);
340 err("%s error: %d\n", __FUNCTION__
, res
);
343 static void usbvision_remove_sysfs(struct video_device
*vdev
)
346 class_device_remove_file(&vdev
->class_dev
,
347 &class_device_attr_version
);
348 class_device_remove_file(&vdev
->class_dev
,
349 &class_device_attr_model
);
350 class_device_remove_file(&vdev
->class_dev
,
351 &class_device_attr_hue
);
352 class_device_remove_file(&vdev
->class_dev
,
353 &class_device_attr_contrast
);
354 class_device_remove_file(&vdev
->class_dev
,
355 &class_device_attr_brightness
);
356 class_device_remove_file(&vdev
->class_dev
,
357 &class_device_attr_saturation
);
358 class_device_remove_file(&vdev
->class_dev
,
359 &class_device_attr_streaming
);
360 class_device_remove_file(&vdev
->class_dev
,
361 &class_device_attr_compression
);
362 class_device_remove_file(&vdev
->class_dev
,
363 &class_device_attr_bridge
);
371 * This is part of Video 4 Linux API. The driver can be opened by one
372 * client only (checks internal counter 'usbvision->user'). The procedure
373 * then allocates buffers needed for video processing.
376 static int usbvision_v4l2_open(struct inode
*inode
, struct file
*file
)
378 struct video_device
*dev
= video_devdata(file
);
379 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
382 PDEBUG(DBG_IO
, "open");
385 usbvision_reset_powerOffTimer(usbvision
);
390 /* Allocate memory for the scratch ring buffer */
391 errCode
= usbvision_scratch_alloc(usbvision
);
392 if (isocMode
==ISOC_MODE_COMPRESS
) {
393 /* Allocate intermediate decompression buffers only if needed */
394 errCode
= usbvision_decompress_alloc(usbvision
);
397 /* Deallocate all buffers if trouble */
398 usbvision_scratch_free(usbvision
);
399 usbvision_decompress_free(usbvision
);
403 /* If so far no errors then we shall start the camera */
405 down(&usbvision
->lock
);
406 if (usbvision
->power
== 0) {
407 usbvision_power_on(usbvision
);
408 usbvision_i2c_register(usbvision
);
411 /* Send init sequence only once, it's large! */
412 if (!usbvision
->initialized
) {
414 setup_ok
= usbvision_setup(usbvision
,isocMode
);
416 usbvision
->initialized
= 1;
422 usbvision_begin_streaming(usbvision
);
423 errCode
= usbvision_init_isoc(usbvision
);
424 /* device needs to be initialized before isoc transfer */
425 usbvision_muxsel(usbvision
,0);
430 usbvision_i2c_unregister(usbvision
);
431 usbvision_power_off(usbvision
);
432 usbvision
->initialized
= 0;
435 up(&usbvision
->lock
);
442 usbvision_empty_framequeues(usbvision
);
444 PDEBUG(DBG_IO
, "success");
449 * usbvision_v4l2_close()
451 * This is part of Video 4 Linux API. The procedure
452 * stops streaming and deallocates all buffers that were earlier
453 * allocated in usbvision_v4l2_open().
456 static int usbvision_v4l2_close(struct inode
*inode
, struct file
*file
)
458 struct video_device
*dev
= video_devdata(file
);
459 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
461 PDEBUG(DBG_IO
, "close");
462 down(&usbvision
->lock
);
464 usbvision_audio_off(usbvision
);
465 usbvision_restart_isoc(usbvision
);
466 usbvision_stop_isoc(usbvision
);
468 usbvision_decompress_free(usbvision
);
469 usbvision_frames_free(usbvision
);
470 usbvision_empty_framequeues(usbvision
);
471 usbvision_scratch_free(usbvision
);
476 /* power off in a little while to avoid off/on every close/open short sequences */
477 usbvision_set_powerOffTimer(usbvision
);
478 usbvision
->initialized
= 0;
481 up(&usbvision
->lock
);
483 if (usbvision
->remove_pending
) {
484 printk(KERN_INFO
"%s: Final disconnect\n", __FUNCTION__
);
485 usbvision_release(usbvision
);
488 PDEBUG(DBG_IO
, "success");
498 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
501 static int usbvision_v4l2_do_ioctl(struct inode
*inode
, struct file
*file
,
502 unsigned int cmd
, void *arg
)
504 struct video_device
*dev
= video_devdata(file
);
505 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
507 if (!USBVISION_IS_OPERATIONAL(usbvision
))
512 #ifdef CONFIG_VIDEO_ADV_DEBUG
513 /* ioctls to allow direct acces to the NT100x registers */
514 case VIDIOC_DBG_G_REGISTER
:
515 case VIDIOC_DBG_S_REGISTER
:
517 struct v4l2_register
*reg
= arg
;
520 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
522 if (!capable(CAP_SYS_ADMIN
))
524 /* NT100x has a 8-bit register space */
525 if (cmd
== VIDIOC_DBG_G_REGISTER
)
526 errCode
= usbvision_read_reg(usbvision
, reg
->reg
&0xff);
528 errCode
= usbvision_write_reg(usbvision
, reg
->reg
&0xff, reg
->val
);
530 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__
,
531 cmd
== VIDIOC_DBG_G_REGISTER
? 'G' : 'S', errCode
);
534 if (cmd
== VIDIOC_DBG_S_REGISTER
)
535 reg
->val
= (u8
)errCode
;
537 PDEBUG(DBG_IOCTL
, "VIDIOC_DBG_%c_REGISTER reg=0x%02X, value=0x%02X",
538 cmd
== VIDIOC_DBG_G_REGISTER
? 'G' : 'S',
539 (unsigned int)reg
->reg
, (unsigned int)reg
->val
);
543 case VIDIOC_QUERYCAP
:
545 struct v4l2_capability
*vc
=arg
;
547 memset(vc
, 0, sizeof(*vc
));
548 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
549 strlcpy(vc
->card
, usbvision_device_data
[usbvision
->DevModel
].ModelString
,
551 strlcpy(vc
->bus_info
, usbvision
->dev
->dev
.bus_id
,
552 sizeof(vc
->bus_info
));
553 vc
->version
= USBVISION_DRIVER_VERSION
;
554 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
558 (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
559 PDEBUG(DBG_IOCTL
, "VIDIOC_QUERYCAP");
562 case VIDIOC_ENUMINPUT
:
564 struct v4l2_input
*vi
= arg
;
567 if ((vi
->index
>= usbvision
->video_inputs
) || (vi
->index
< 0) )
569 if (usbvision
->have_tuner
) {
573 chan
= vi
->index
+ 1; //skip Television string
577 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
578 strcpy(vi
->name
, "White Video Input");
581 strcpy(vi
->name
, "Television");
582 vi
->type
= V4L2_INPUT_TYPE_TUNER
;
585 vi
->std
= V4L2_STD_PAL
| V4L2_STD_NTSC
| V4L2_STD_SECAM
;
589 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
590 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
591 strcpy(vi
->name
, "Green Video Input");
594 strcpy(vi
->name
, "Composite Video Input");
596 vi
->std
= V4L2_STD_PAL
;
599 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
600 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
601 strcpy(vi
->name
, "Yellow Video Input");
604 strcpy(vi
->name
, "S-Video Input");
606 vi
->std
= V4L2_STD_PAL
;
609 vi
->type
= V4L2_INPUT_TYPE_CAMERA
;
610 strcpy(vi
->name
, "Red Video Input");
611 vi
->std
= V4L2_STD_PAL
;
614 PDEBUG(DBG_IOCTL
, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
615 vi
->name
, vi
->index
, vi
->tuner
,vi
->type
,(int)vi
->std
);
620 struct v4l2_standard
*e
= arg
;
627 ret
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
628 tvnorms
[e
->index
].name
);
637 *input
= usbvision
->ctl_input
;
643 if ((*input
>= usbvision
->video_inputs
) || (*input
< 0) )
645 usbvision
->ctl_input
= *input
;
647 down(&usbvision
->lock
);
648 usbvision_muxsel(usbvision
, usbvision
->ctl_input
);
649 usbvision_set_input(usbvision
);
650 usbvision_set_output(usbvision
, usbvision
->curwidth
, usbvision
->curheight
);
651 up(&usbvision
->lock
);
656 v4l2_std_id
*id
= arg
;
658 *id
= usbvision
->tvnorm
->id
;
660 PDEBUG(DBG_IOCTL
, "VIDIOC_G_STD std_id=%s", usbvision
->tvnorm
->name
);
665 v4l2_std_id
*id
= arg
;
668 for (i
= 0; i
< TVNORMS
; i
++)
669 if (*id
== tvnorms
[i
].id
)
672 for (i
= 0; i
< TVNORMS
; i
++)
673 if (*id
& tvnorms
[i
].id
)
678 down(&usbvision
->lock
);
679 usbvision
->tvnorm
= &tvnorms
[i
];
681 call_i2c_clients(usbvision
, VIDIOC_S_STD
,
682 &usbvision
->tvnorm
->id
);
684 up(&usbvision
->lock
);
686 PDEBUG(DBG_IOCTL
, "VIDIOC_S_STD std_id=%s", usbvision
->tvnorm
->name
);
691 struct v4l2_tuner
*vt
= arg
;
693 if (!usbvision
->have_tuner
|| vt
->index
) // Only tuner 0
695 strcpy(vt
->name
, "Television");
696 /* Let clients fill in the remainder of this struct */
697 call_i2c_clients(usbvision
,VIDIOC_G_TUNER
,vt
);
699 PDEBUG(DBG_IOCTL
, "VIDIOC_G_TUNER signal=%x, afc=%x",vt
->signal
,vt
->afc
);
704 struct v4l2_tuner
*vt
= arg
;
706 // Only no or one tuner for now
707 if (!usbvision
->have_tuner
|| vt
->index
)
709 /* let clients handle this */
710 call_i2c_clients(usbvision
,VIDIOC_S_TUNER
,vt
);
712 PDEBUG(DBG_IOCTL
, "VIDIOC_S_TUNER");
715 case VIDIOC_G_FREQUENCY
:
717 struct v4l2_frequency
*freq
= arg
;
719 freq
->tuner
= 0; // Only one tuner
720 freq
->type
= V4L2_TUNER_ANALOG_TV
;
721 freq
->frequency
= usbvision
->freq
;
722 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq
->frequency
);
725 case VIDIOC_S_FREQUENCY
:
727 struct v4l2_frequency
*freq
= arg
;
729 // Only no or one tuner for now
730 if (!usbvision
->have_tuner
|| freq
->tuner
)
733 usbvision
->freq
= freq
->frequency
;
734 call_i2c_clients(usbvision
, cmd
, freq
);
735 PDEBUG(DBG_IOCTL
, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq
->frequency
);
740 struct v4l2_audio
*v
= arg
;
741 memset(v
,0, sizeof(v
));
742 strcpy(v
->name
, "TV");
743 PDEBUG(DBG_IOCTL
, "VIDIOC_G_AUDIO");
748 struct v4l2_audio
*v
= arg
;
752 PDEBUG(DBG_IOCTL
, "VIDIOC_S_AUDIO");
755 case VIDIOC_QUERYCTRL
:
757 struct v4l2_queryctrl
*ctrl
= arg
;
760 memset(ctrl
,0,sizeof(*ctrl
));
763 call_i2c_clients(usbvision
, cmd
, arg
);
770 PDEBUG(DBG_IOCTL
,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl
->id
,ctrl
->type
);
774 struct v4l2_control
*ctrl
= arg
;
775 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, ctrl
);
776 PDEBUG(DBG_IOCTL
,"VIDIOC_G_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
781 struct v4l2_control
*ctrl
= arg
;
783 PDEBUG(DBG_IOCTL
, "VIDIOC_S_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
784 call_i2c_clients(usbvision
, VIDIOC_S_CTRL
, ctrl
);
789 struct v4l2_requestbuffers
*vr
= arg
;
792 RESTRICT_TO_RANGE(vr
->count
,1,USBVISION_NUMFRAMES
);
794 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
795 if((vr
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) ||
796 (vr
->memory
!= V4L2_MEMORY_MMAP
))
799 if(usbvision
->streaming
== Stream_On
) {
800 if ((ret
= usbvision_stream_interrupt(usbvision
)))
804 usbvision_frames_free(usbvision
);
805 usbvision_empty_framequeues(usbvision
);
806 vr
->count
= usbvision_frames_alloc(usbvision
,vr
->count
);
808 usbvision
->curFrame
= NULL
;
810 PDEBUG(DBG_IOCTL
, "VIDIOC_REQBUFS count=%d",vr
->count
);
813 case VIDIOC_QUERYBUF
:
815 struct v4l2_buffer
*vb
= arg
;
816 struct usbvision_frame
*frame
;
818 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
820 if(vb
->type
!= V4L2_CAP_VIDEO_CAPTURE
) {
823 if(vb
->index
>=usbvision
->num_frames
) {
826 // Updating the corresponding frame state
828 frame
= &usbvision
->frame
[vb
->index
];
829 if(frame
->grabstate
>= FrameState_Ready
)
830 vb
->flags
|= V4L2_BUF_FLAG_QUEUED
;
831 if(frame
->grabstate
>= FrameState_Done
)
832 vb
->flags
|= V4L2_BUF_FLAG_DONE
;
833 if(frame
->grabstate
== FrameState_Unused
)
834 vb
->flags
|= V4L2_BUF_FLAG_MAPPED
;
835 vb
->memory
= V4L2_MEMORY_MMAP
;
837 vb
->m
.offset
= vb
->index
*PAGE_ALIGN(usbvision
->max_frame_size
);
839 vb
->memory
= V4L2_MEMORY_MMAP
;
840 vb
->field
= V4L2_FIELD_NONE
;
841 vb
->length
= usbvision
->curwidth
*usbvision
->curheight
*usbvision
->palette
.bytes_per_pixel
;
842 vb
->timestamp
= usbvision
->frame
[vb
->index
].timestamp
;
843 vb
->sequence
= usbvision
->frame
[vb
->index
].sequence
;
848 struct v4l2_buffer
*vb
= arg
;
849 struct usbvision_frame
*frame
;
850 unsigned long lock_flags
;
852 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
853 if(vb
->type
!= V4L2_CAP_VIDEO_CAPTURE
) {
856 if(vb
->index
>=usbvision
->num_frames
) {
860 frame
= &usbvision
->frame
[vb
->index
];
862 if (frame
->grabstate
!= FrameState_Unused
) {
866 /* Mark it as ready and enqueue frame */
867 frame
->grabstate
= FrameState_Ready
;
868 frame
->scanstate
= ScanState_Scanning
;
869 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
871 vb
->flags
&= ~V4L2_BUF_FLAG_DONE
;
873 /* set v4l2_format index */
874 frame
->v4l2_format
= usbvision
->palette
;
876 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
877 list_add_tail(&usbvision
->frame
[vb
->index
].frame
, &usbvision
->inqueue
);
878 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
880 PDEBUG(DBG_IOCTL
, "VIDIOC_QBUF frame #%d",vb
->index
);
885 struct v4l2_buffer
*vb
= arg
;
887 struct usbvision_frame
*f
;
888 unsigned long lock_flags
;
890 if (vb
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
893 if (list_empty(&(usbvision
->outqueue
))) {
894 if (usbvision
->streaming
== Stream_Idle
)
896 ret
= wait_event_interruptible
897 (usbvision
->wait_frame
,
898 !list_empty(&(usbvision
->outqueue
)));
903 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
904 f
= list_entry(usbvision
->outqueue
.next
,
905 struct usbvision_frame
, frame
);
906 list_del(usbvision
->outqueue
.next
);
907 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
909 f
->grabstate
= FrameState_Unused
;
911 vb
->memory
= V4L2_MEMORY_MMAP
;
912 vb
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_QUEUED
| V4L2_BUF_FLAG_DONE
;
913 vb
->index
= f
->index
;
914 vb
->sequence
= f
->sequence
;
915 vb
->timestamp
= f
->timestamp
;
916 vb
->field
= V4L2_FIELD_NONE
;
917 vb
->bytesused
= f
->scanlength
;
921 case VIDIOC_STREAMON
:
923 int b
=V4L2_BUF_TYPE_VIDEO_CAPTURE
;
925 usbvision
->streaming
= Stream_On
;
927 call_i2c_clients(usbvision
,VIDIOC_STREAMON
, &b
);
929 PDEBUG(DBG_IOCTL
, "VIDIOC_STREAMON");
933 case VIDIOC_STREAMOFF
:
936 int b
=V4L2_BUF_TYPE_VIDEO_CAPTURE
;
938 if (*type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
941 if(usbvision
->streaming
== Stream_On
) {
942 usbvision_stream_interrupt(usbvision
);
943 // Stop all video streamings
944 call_i2c_clients(usbvision
,VIDIOC_STREAMOFF
, &b
);
946 usbvision_empty_framequeues(usbvision
);
948 PDEBUG(DBG_IOCTL
, "VIDIOC_STREAMOFF");
951 case VIDIOC_ENUM_FMT
:
953 struct v4l2_fmtdesc
*vfd
= arg
;
955 if(vfd
->index
>=USBVISION_SUPPORTED_PALETTES
-1) {
959 vfd
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
960 strcpy(vfd
->description
,usbvision_v4l2_format
[vfd
->index
].desc
);
961 vfd
->pixelformat
= usbvision_v4l2_format
[vfd
->index
].format
;
962 memset(vfd
->reserved
, 0, sizeof(vfd
->reserved
));
967 struct v4l2_format
*vf
= arg
;
970 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
972 vf
->fmt
.pix
.width
= usbvision
->curwidth
;
973 vf
->fmt
.pix
.height
= usbvision
->curheight
;
974 vf
->fmt
.pix
.pixelformat
= usbvision
->palette
.format
;
975 vf
->fmt
.pix
.bytesperline
= usbvision
->curwidth
*usbvision
->palette
.bytes_per_pixel
;
976 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*usbvision
->curheight
;
977 vf
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
978 vf
->fmt
.pix
.field
= V4L2_FIELD_NONE
; /* Always progressive image */
979 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
980 vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
,usbvision
->palette
.desc
);
984 PDEBUG(DBG_IOCTL
, "VIDIOC_G_FMT invalid type %d",vf
->type
);
992 struct v4l2_format
*vf
= arg
;
996 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
998 /* Find requested format in available ones */
999 for(formatIdx
=0;formatIdx
<USBVISION_SUPPORTED_PALETTES
;formatIdx
++) {
1000 if(vf
->fmt
.pix
.pixelformat
== usbvision_v4l2_format
[formatIdx
].format
) {
1001 usbvision
->palette
= usbvision_v4l2_format
[formatIdx
];
1006 if(formatIdx
== USBVISION_SUPPORTED_PALETTES
) {
1009 RESTRICT_TO_RANGE(vf
->fmt
.pix
.width
, MIN_FRAME_WIDTH
, MAX_FRAME_WIDTH
);
1010 RESTRICT_TO_RANGE(vf
->fmt
.pix
.height
, MIN_FRAME_HEIGHT
, MAX_FRAME_HEIGHT
);
1012 vf
->fmt
.pix
.bytesperline
= vf
->fmt
.pix
.width
*usbvision
->palette
.bytes_per_pixel
;
1013 vf
->fmt
.pix
.sizeimage
= vf
->fmt
.pix
.bytesperline
*vf
->fmt
.pix
.height
;
1015 if(cmd
== VIDIOC_TRY_FMT
) {
1016 PDEBUG(DBG_IOCTL
, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1017 vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
,usbvision
->palette
.desc
);
1021 /* stop io in case it is already in progress */
1022 if(usbvision
->streaming
== Stream_On
) {
1023 if ((ret
= usbvision_stream_interrupt(usbvision
)))
1026 usbvision_frames_free(usbvision
);
1027 usbvision_empty_framequeues(usbvision
);
1029 usbvision
->curFrame
= NULL
;
1031 // by now we are committed to the new data...
1032 down(&usbvision
->lock
);
1033 usbvision_set_output(usbvision
, vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
);
1034 up(&usbvision
->lock
);
1036 PDEBUG(DBG_IOCTL
, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1037 vf
->fmt
.pix
.width
, vf
->fmt
.pix
.height
,usbvision
->palette
.desc
);
1045 return -ENOIOCTLCMD
;
1050 static int usbvision_v4l2_ioctl(struct inode
*inode
, struct file
*file
,
1051 unsigned int cmd
, unsigned long arg
)
1053 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_v4l2_do_ioctl
);
1057 static ssize_t
usbvision_v4l2_read(struct file
*file
, char __user
*buf
,
1058 size_t count
, loff_t
*ppos
)
1060 struct video_device
*dev
= video_devdata(file
);
1061 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1062 int noblock
= file
->f_flags
& O_NONBLOCK
;
1063 unsigned long lock_flags
;
1066 struct usbvision_frame
*frame
;
1068 PDEBUG(DBG_IO
, "%s: %ld bytes, noblock=%d", __FUNCTION__
, (unsigned long)count
, noblock
);
1070 if (!USBVISION_IS_OPERATIONAL(usbvision
) || (buf
== NULL
))
1073 /* This entry point is compatible with the mmap routines so that a user can do either
1074 VIDIOC_QBUF/VIDIOC_DQBUF to get frames or call read on the device. */
1075 if(!usbvision
->num_frames
) {
1076 /* First, allocate some frames to work with if this has not been done with
1078 usbvision_frames_free(usbvision
);
1079 usbvision_empty_framequeues(usbvision
);
1080 usbvision_frames_alloc(usbvision
,USBVISION_NUMFRAMES
);
1083 if(usbvision
->streaming
!= Stream_On
) {
1084 /* no stream is running, make it running ! */
1085 usbvision
->streaming
= Stream_On
;
1086 call_i2c_clients(usbvision
,VIDIOC_STREAMON
, NULL
);
1089 /* Then, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1090 for(i
=0;i
<usbvision
->num_frames
;i
++) {
1091 frame
= &usbvision
->frame
[i
];
1092 if(frame
->grabstate
== FrameState_Unused
) {
1093 /* Mark it as ready and enqueue frame */
1094 frame
->grabstate
= FrameState_Ready
;
1095 frame
->scanstate
= ScanState_Scanning
;
1096 frame
->scanlength
= 0; /* Accumulated in usbvision_parse_data() */
1098 /* set v4l2_format index */
1099 frame
->v4l2_format
= usbvision
->palette
;
1101 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1102 list_add_tail(&frame
->frame
, &usbvision
->inqueue
);
1103 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1107 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1108 if (list_empty(&(usbvision
->outqueue
))) {
1112 ret
= wait_event_interruptible
1113 (usbvision
->wait_frame
,
1114 !list_empty(&(usbvision
->outqueue
)));
1119 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1120 frame
= list_entry(usbvision
->outqueue
.next
,
1121 struct usbvision_frame
, frame
);
1122 list_del(usbvision
->outqueue
.next
);
1123 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1125 /* An error returns an empty frame */
1126 if (frame
->grabstate
== FrameState_Error
) {
1127 frame
->bytes_read
= 0;
1131 PDEBUG(DBG_IO
, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__
,
1132 frame
->index
, frame
->bytes_read
, frame
->scanlength
);
1134 /* copy bytes to user space; we allow for partials reads */
1135 if ((count
+ frame
->bytes_read
) > (unsigned long)frame
->scanlength
)
1136 count
= frame
->scanlength
- frame
->bytes_read
;
1138 if (copy_to_user(buf
, frame
->data
+ frame
->bytes_read
, count
)) {
1142 frame
->bytes_read
+= count
;
1143 PDEBUG(DBG_IO
, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__
,
1144 (unsigned long)count
, frame
->bytes_read
);
1146 // For now, forget the frame if it has not been read in one shot.
1147 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1148 frame
->bytes_read
= 0;
1150 /* Mark it as available to be used again. */
1151 frame
->grabstate
= FrameState_Unused
;
1157 static int usbvision_v4l2_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1159 unsigned long size
= vma
->vm_end
- vma
->vm_start
,
1160 start
= vma
->vm_start
;
1164 struct video_device
*dev
= video_devdata(file
);
1165 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1167 PDEBUG(DBG_MMAP
, "mmap");
1169 down(&usbvision
->lock
);
1171 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1172 up(&usbvision
->lock
);
1176 if (!(vma
->vm_flags
& VM_WRITE
) ||
1177 size
!= PAGE_ALIGN(usbvision
->max_frame_size
)) {
1178 up(&usbvision
->lock
);
1182 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
1183 if (((PAGE_ALIGN(usbvision
->max_frame_size
)*i
) >> PAGE_SHIFT
) == vma
->vm_pgoff
)
1186 if (i
== usbvision
->num_frames
) {
1187 PDEBUG(DBG_MMAP
, "mmap: user supplied mapping address is out of range");
1188 up(&usbvision
->lock
);
1192 /* VM_IO is eventually going to replace PageReserved altogether */
1193 vma
->vm_flags
|= VM_IO
;
1194 vma
->vm_flags
|= VM_RESERVED
; /* avoid to swap out this VMA */
1196 pos
= usbvision
->frame
[i
].data
;
1199 if (vm_insert_page(vma
, start
, vmalloc_to_page(pos
))) {
1200 PDEBUG(DBG_MMAP
, "mmap: vm_insert_page failed");
1201 up(&usbvision
->lock
);
1209 up(&usbvision
->lock
);
1215 * Here comes the stuff for radio on usbvision based devices
1218 static int usbvision_radio_open(struct inode
*inode
, struct file
*file
)
1220 struct video_device
*dev
= video_devdata(file
);
1221 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1222 struct v4l2_frequency freq
;
1225 PDEBUG(DBG_IO
, "%s:", __FUNCTION__
);
1227 down(&usbvision
->lock
);
1229 if (usbvision
->user
) {
1230 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__
);
1235 usbvision_reset_powerOffTimer(usbvision
);
1236 if (usbvision
->power
== 0) {
1237 usbvision_power_on(usbvision
);
1238 usbvision_i2c_register(usbvision
);
1242 /* Alternate interface 1 is is the biggest frame size */
1243 errCode
= usbvision_set_alternate(usbvision
);
1245 usbvision
->last_error
= errCode
;
1249 // If so far no errors then we shall start the radio
1250 usbvision
->radio
= 1;
1251 call_i2c_clients(usbvision
,AUDC_SET_RADIO
,&usbvision
->tuner_type
);
1252 freq
.frequency
= 1517; //SWR3 @ 94.8MHz
1253 call_i2c_clients(usbvision
, VIDIOC_S_FREQUENCY
, &freq
);
1254 usbvision_set_audio(usbvision
, USBVISION_AUDIO_RADIO
);
1259 if (PowerOnAtOpen
) {
1260 usbvision_i2c_unregister(usbvision
);
1261 usbvision_power_off(usbvision
);
1262 usbvision
->initialized
= 0;
1265 up(&usbvision
->lock
);
1270 static int usbvision_radio_close(struct inode
*inode
, struct file
*file
)
1272 struct video_device
*dev
= video_devdata(file
);
1273 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1278 down(&usbvision
->lock
);
1280 /* Set packet size to 0 */
1281 usbvision
->ifaceAlt
=0;
1282 errCode
= usb_set_interface(usbvision
->dev
, usbvision
->iface
,
1283 usbvision
->ifaceAlt
);
1285 usbvision_audio_off(usbvision
);
1289 if (PowerOnAtOpen
) {
1290 usbvision_set_powerOffTimer(usbvision
);
1291 usbvision
->initialized
= 0;
1294 up(&usbvision
->lock
);
1296 if (usbvision
->remove_pending
) {
1297 printk(KERN_INFO
"%s: Final disconnect\n", __FUNCTION__
);
1298 usbvision_release(usbvision
);
1302 PDEBUG(DBG_IO
, "success");
1307 static int usbvision_do_radio_ioctl(struct inode
*inode
, struct file
*file
,
1308 unsigned int cmd
, void *arg
)
1310 struct video_device
*dev
= video_devdata(file
);
1311 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*) video_get_drvdata(dev
);
1313 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1317 case VIDIOC_QUERYCAP
:
1319 struct v4l2_capability
*vc
=arg
;
1321 memset(vc
, 0, sizeof(*vc
));
1322 strlcpy(vc
->driver
, "USBVision", sizeof(vc
->driver
));
1323 strlcpy(vc
->card
, usbvision_device_data
[usbvision
->DevModel
].ModelString
,
1325 strlcpy(vc
->bus_info
, usbvision
->dev
->dev
.bus_id
,
1326 sizeof(vc
->bus_info
));
1327 vc
->version
= USBVISION_DRIVER_VERSION
;
1328 vc
->capabilities
= (usbvision
->have_tuner
? V4L2_CAP_TUNER
: 0);
1329 PDEBUG(DBG_IO
, "VIDIOC_QUERYCAP");
1332 case VIDIOC_QUERYCTRL
:
1334 struct v4l2_queryctrl
*ctrl
= arg
;
1337 memset(ctrl
,0,sizeof(*ctrl
));
1340 call_i2c_clients(usbvision
, cmd
, arg
);
1341 PDEBUG(DBG_IO
,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl
->id
,ctrl
->type
);
1351 struct v4l2_control
*ctrl
= arg
;
1353 call_i2c_clients(usbvision
, VIDIOC_G_CTRL
, ctrl
);
1354 PDEBUG(DBG_IO
,"VIDIOC_G_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
1359 struct v4l2_control
*ctrl
= arg
;
1361 call_i2c_clients(usbvision
, VIDIOC_S_CTRL
, ctrl
);
1362 PDEBUG(DBG_IO
, "VIDIOC_S_CTRL id=%x value=%x",ctrl
->id
,ctrl
->value
);
1365 case VIDIOC_G_TUNER
:
1367 struct v4l2_tuner
*t
= arg
;
1372 memset(t
,0,sizeof(*t
));
1373 strcpy(t
->name
, "Radio");
1374 t
->type
= V4L2_TUNER_RADIO
;
1376 /* Let clients fill in the remainder of this struct */
1377 call_i2c_clients(usbvision
,VIDIOC_G_TUNER
,t
);
1378 PDEBUG(DBG_IO
, "VIDIOC_G_TUNER signal=%x, afc=%x",t
->signal
,t
->afc
);
1381 case VIDIOC_S_TUNER
:
1383 struct v4l2_tuner
*vt
= arg
;
1385 // Only no or one tuner for now
1386 if (!usbvision
->have_tuner
|| vt
->index
)
1388 /* let clients handle this */
1389 call_i2c_clients(usbvision
,VIDIOC_S_TUNER
,vt
);
1391 PDEBUG(DBG_IO
, "VIDIOC_S_TUNER");
1394 case VIDIOC_G_AUDIO
:
1396 struct v4l2_audio
*a
= arg
;
1398 memset(a
,0,sizeof(*a
));
1399 strcpy(a
->name
,"Radio");
1400 PDEBUG(DBG_IO
, "VIDIOC_G_AUDIO");
1403 case VIDIOC_S_AUDIO
:
1404 case VIDIOC_S_INPUT
:
1408 case VIDIOC_G_FREQUENCY
:
1410 struct v4l2_frequency
*f
= arg
;
1412 memset(f
,0,sizeof(*f
));
1414 f
->type
= V4L2_TUNER_RADIO
;
1415 f
->frequency
= usbvision
->freq
;
1416 call_i2c_clients(usbvision
, cmd
, f
);
1417 PDEBUG(DBG_IO
, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f
->frequency
);
1421 case VIDIOC_S_FREQUENCY
:
1423 struct v4l2_frequency
*f
= arg
;
1427 usbvision
->freq
= f
->frequency
;
1428 call_i2c_clients(usbvision
, cmd
, f
);
1429 PDEBUG(DBG_IO
, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f
->frequency
);
1435 PDEBUG(DBG_IO
, "%s: Unknown command %x", __FUNCTION__
, cmd
);
1436 return -ENOIOCTLCMD
;
1443 static int usbvision_radio_ioctl(struct inode
*inode
, struct file
*file
,
1444 unsigned int cmd
, unsigned long arg
)
1446 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_do_radio_ioctl
);
1451 * Here comes the stuff for vbi on usbvision based devices
1454 static int usbvision_vbi_open(struct inode
*inode
, struct file
*file
)
1461 static int usbvision_vbi_close(struct inode
*inode
, struct file
*file
)
1467 static int usbvision_do_vbi_ioctl(struct inode
*inode
, struct file
*file
,
1468 unsigned int cmd
, void *arg
)
1474 static int usbvision_vbi_ioctl(struct inode
*inode
, struct file
*file
,
1475 unsigned int cmd
, unsigned long arg
)
1477 return video_usercopy(inode
, file
, cmd
, arg
, usbvision_do_vbi_ioctl
);
1482 // Video registration stuff
1486 static const struct file_operations usbvision_fops
= {
1487 .owner
= THIS_MODULE
,
1488 .open
= usbvision_v4l2_open
,
1489 .release
= usbvision_v4l2_close
,
1490 .read
= usbvision_v4l2_read
,
1491 .mmap
= usbvision_v4l2_mmap
,
1492 .ioctl
= usbvision_v4l2_ioctl
,
1493 .llseek
= no_llseek
,
1495 static struct video_device usbvision_video_template
= {
1496 .owner
= THIS_MODULE
,
1497 .type
= VID_TYPE_TUNER
| VID_TYPE_CAPTURE
,
1498 .hardware
= VID_HARDWARE_USBVISION
,
1499 .fops
= &usbvision_fops
,
1500 .name
= "usbvision-video",
1501 .release
= video_device_release
,
1507 static const struct file_operations usbvision_radio_fops
= {
1508 .owner
= THIS_MODULE
,
1509 .open
= usbvision_radio_open
,
1510 .release
= usbvision_radio_close
,
1511 .ioctl
= usbvision_radio_ioctl
,
1512 .llseek
= no_llseek
,
1515 static struct video_device usbvision_radio_template
=
1517 .owner
= THIS_MODULE
,
1518 .type
= VID_TYPE_TUNER
,
1519 .hardware
= VID_HARDWARE_USBVISION
,
1520 .fops
= &usbvision_radio_fops
,
1521 .release
= video_device_release
,
1522 .name
= "usbvision-radio",
1528 static const struct file_operations usbvision_vbi_fops
= {
1529 .owner
= THIS_MODULE
,
1530 .open
= usbvision_vbi_open
,
1531 .release
= usbvision_vbi_close
,
1532 .ioctl
= usbvision_vbi_ioctl
,
1533 .llseek
= no_llseek
,
1536 static struct video_device usbvision_vbi_template
=
1538 .owner
= THIS_MODULE
,
1539 .type
= VID_TYPE_TUNER
,
1540 .hardware
= VID_HARDWARE_USBVISION
,
1541 .fops
= &usbvision_vbi_fops
,
1542 .release
= video_device_release
,
1543 .name
= "usbvision-vbi",
1548 static struct video_device
*usbvision_vdev_init(struct usb_usbvision
*usbvision
,
1549 struct video_device
*vdev_template
,
1552 struct usb_device
*usb_dev
= usbvision
->dev
;
1553 struct video_device
*vdev
;
1555 if (usb_dev
== NULL
) {
1556 err("%s: usbvision->dev is not set", __FUNCTION__
);
1560 vdev
= video_device_alloc();
1564 *vdev
= *vdev_template
;
1565 // vdev->minor = -1;
1566 vdev
->dev
= &usb_dev
->dev
;
1567 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s", name
);
1568 video_set_drvdata(vdev
, usbvision
);
1572 // unregister video4linux devices
1573 static void usbvision_unregister_video(struct usb_usbvision
*usbvision
)
1576 if (usbvision
->vbi
) {
1577 PDEBUG(DBG_PROBE
, "unregister /dev/vbi%d [v4l2]", usbvision
->vbi
->minor
& 0x1f);
1578 if (usbvision
->vbi
->minor
!= -1) {
1579 video_unregister_device(usbvision
->vbi
);
1582 video_device_release(usbvision
->vbi
);
1584 usbvision
->vbi
= NULL
;
1588 if (usbvision
->rdev
) {
1589 PDEBUG(DBG_PROBE
, "unregister /dev/radio%d [v4l2]", usbvision
->rdev
->minor
& 0x1f);
1590 if (usbvision
->rdev
->minor
!= -1) {
1591 video_unregister_device(usbvision
->rdev
);
1594 video_device_release(usbvision
->rdev
);
1596 usbvision
->rdev
= NULL
;
1600 if (usbvision
->vdev
) {
1601 PDEBUG(DBG_PROBE
, "unregister /dev/video%d [v4l2]", usbvision
->vdev
->minor
& 0x1f);
1602 if (usbvision
->vdev
->minor
!= -1) {
1603 video_unregister_device(usbvision
->vdev
);
1606 video_device_release(usbvision
->vdev
);
1608 usbvision
->vdev
= NULL
;
1612 // register video4linux devices
1613 static int __devinit
usbvision_register_video(struct usb_usbvision
*usbvision
)
1616 usbvision
->vdev
= usbvision_vdev_init(usbvision
, &usbvision_video_template
, "USBVision Video");
1617 if (usbvision
->vdev
== NULL
) {
1620 if (video_register_device(usbvision
->vdev
, VFL_TYPE_GRABBER
, video_nr
)<0) {
1623 printk(KERN_INFO
"USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision
->nr
,usbvision
->vdev
->minor
& 0x1f);
1626 if (usbvision_device_data
[usbvision
->DevModel
].Radio
) {
1627 // usbvision has radio
1628 usbvision
->rdev
= usbvision_vdev_init(usbvision
, &usbvision_radio_template
, "USBVision Radio");
1629 if (usbvision
->rdev
== NULL
) {
1632 if (video_register_device(usbvision
->rdev
, VFL_TYPE_RADIO
, radio_nr
)<0) {
1635 printk(KERN_INFO
"USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision
->nr
, usbvision
->rdev
->minor
& 0x1f);
1638 if (usbvision_device_data
[usbvision
->DevModel
].vbi
) {
1639 usbvision
->vbi
= usbvision_vdev_init(usbvision
, &usbvision_vbi_template
, "USBVision VBI");
1640 if (usbvision
->vdev
== NULL
) {
1643 if (video_register_device(usbvision
->vbi
, VFL_TYPE_VBI
, vbi_nr
)<0) {
1646 printk(KERN_INFO
"USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision
->nr
,usbvision
->vbi
->minor
& 0x1f);
1652 err("USBVision[%d]: video_register_device() failed", usbvision
->nr
);
1653 usbvision_unregister_video(usbvision
);
1660 * This code allocates the struct usb_usbvision. It is filled with default values.
1662 * Returns NULL on error, a pointer to usb_usbvision else.
1665 static struct usb_usbvision
*usbvision_alloc(struct usb_device
*dev
)
1667 struct usb_usbvision
*usbvision
;
1669 if ((usbvision
= kzalloc(sizeof(struct usb_usbvision
), GFP_KERNEL
)) == NULL
) {
1673 usbvision
->dev
= dev
;
1675 init_MUTEX(&usbvision
->lock
); /* to 1 == available */
1677 // prepare control urb for control messages during interrupts
1678 usbvision
->ctrlUrb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
1679 if (usbvision
->ctrlUrb
== NULL
) {
1682 init_waitqueue_head(&usbvision
->ctrlUrb_wq
);
1683 init_MUTEX(&usbvision
->ctrlUrbLock
); /* to 1 == available */
1685 usbvision_init_powerOffTimer(usbvision
);
1690 if (usbvision
&& usbvision
->ctrlUrb
) {
1691 usb_free_urb(usbvision
->ctrlUrb
);
1700 * usbvision_release()
1702 * This code does final release of struct usb_usbvision. This happens
1703 * after the device is disconnected -and- all clients closed their files.
1706 static void usbvision_release(struct usb_usbvision
*usbvision
)
1708 PDEBUG(DBG_PROBE
, "");
1710 down(&usbvision
->lock
);
1712 usbvision_reset_powerOffTimer(usbvision
);
1714 usbvision
->initialized
= 0;
1716 up(&usbvision
->lock
);
1718 usbvision_remove_sysfs(usbvision
->vdev
);
1719 usbvision_unregister_video(usbvision
);
1721 if (usbvision
->ctrlUrb
) {
1722 usb_free_urb(usbvision
->ctrlUrb
);
1727 PDEBUG(DBG_PROBE
, "success");
1731 /******************************** usb interface *****************************************/
1733 static void usbvision_configure_video(struct usb_usbvision
*usbvision
)
1737 if (usbvision
== NULL
)
1740 model
= usbvision
->DevModel
;
1741 usbvision
->palette
= usbvision_v4l2_format
[2]; // V4L2_PIX_FMT_RGB24;
1743 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2_override
) {
1744 usbvision
->Vin_Reg2_Preset
= usbvision_device_data
[usbvision
->DevModel
].Vin_Reg2
;
1746 usbvision
->Vin_Reg2_Preset
= 0;
1749 for (i
= 0; i
< TVNORMS
; i
++)
1750 if (usbvision_device_data
[model
].VideoNorm
== tvnorms
[i
].mode
)
1754 usbvision
->tvnorm
= &tvnorms
[i
]; /* set default norm */
1756 usbvision
->video_inputs
= usbvision_device_data
[model
].VideoChannels
;
1757 usbvision
->ctl_input
= 0;
1759 /* This should be here to make i2c clients to be able to register */
1760 usbvision_audio_off(usbvision
); //first switch off audio
1761 if (!PowerOnAtOpen
) {
1762 usbvision_power_on(usbvision
); //and then power up the noisy tuner
1763 usbvision_i2c_register(usbvision
);
1770 * This procedure queries device descriptor and accepts the interface
1771 * if it looks like USBVISION video device
1774 static int __devinit
usbvision_probe(struct usb_interface
*intf
,
1775 const struct usb_device_id
*devid
)
1777 struct usb_device
*dev
= usb_get_dev(interface_to_usbdev(intf
));
1778 struct usb_interface
*uif
;
1779 __u8 ifnum
= intf
->altsetting
->desc
.bInterfaceNumber
;
1780 const struct usb_host_interface
*interface
;
1781 struct usb_usbvision
*usbvision
= NULL
;
1782 const struct usb_endpoint_descriptor
*endpoint
;
1785 PDEBUG(DBG_PROBE
, "VID=%#04x, PID=%#04x, ifnum=%u",
1786 dev
->descriptor
.idVendor
,
1787 dev
->descriptor
.idProduct
, ifnum
);
1789 model
= devid
->driver_info
;
1790 if ( (model
<0) || (model
>=usbvision_device_data_size
) ) {
1791 PDEBUG(DBG_PROBE
, "model out of bounds %d",model
);
1794 printk(KERN_INFO
"%s: %s found\n", __FUNCTION__
,
1795 usbvision_device_data
[model
].ModelString
);
1797 if (usbvision_device_data
[model
].Interface
>= 0) {
1798 interface
= &dev
->actconfig
->interface
[usbvision_device_data
[model
].Interface
]->altsetting
[0];
1801 interface
= &dev
->actconfig
->interface
[ifnum
]->altsetting
[0];
1803 endpoint
= &interface
->endpoint
[1].desc
;
1804 if ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) != USB_ENDPOINT_XFER_ISOC
) {
1805 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__
, ifnum
);
1806 err("%s: Endpoint attributes %d", __FUNCTION__
, endpoint
->bmAttributes
);
1809 if ((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) {
1810 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__
, ifnum
);
1814 if ((usbvision
= usbvision_alloc(dev
)) == NULL
) {
1815 err("%s: couldn't allocate USBVision struct", __FUNCTION__
);
1819 if (dev
->descriptor
.bNumConfigurations
> 1) {
1820 usbvision
->bridgeType
= BRIDGE_NT1004
;
1822 else if (model
== DAZZLE_DVC_90_REV_1_SECAM
) {
1823 usbvision
->bridgeType
= BRIDGE_NT1005
;
1826 usbvision
->bridgeType
= BRIDGE_NT1003
;
1828 PDEBUG(DBG_PROBE
, "bridgeType %d", usbvision
->bridgeType
);
1830 down(&usbvision
->lock
);
1832 /* compute alternate max packet sizes */
1833 uif
= dev
->actconfig
->interface
[0];
1835 usbvision
->num_alt
=uif
->num_altsetting
;
1836 PDEBUG(DBG_PROBE
, "Alternate settings: %i",usbvision
->num_alt
);
1837 usbvision
->alt_max_pkt_size
= kmalloc(32*
1838 usbvision
->num_alt
,GFP_KERNEL
);
1839 if (usbvision
->alt_max_pkt_size
== NULL
) {
1840 err("usbvision: out of memory!\n");
1844 for (i
= 0; i
< usbvision
->num_alt
; i
++) {
1845 u16 tmp
= le16_to_cpu(uif
->altsetting
[i
].endpoint
[1].desc
.
1847 usbvision
->alt_max_pkt_size
[i
] =
1848 (tmp
& 0x07ff) * (((tmp
& 0x1800) >> 11) + 1);
1849 PDEBUG(DBG_PROBE
, "Alternate setting %i, max size= %i",i
,
1850 usbvision
->alt_max_pkt_size
[i
]);
1854 usbvision
->nr
= usbvision_nr
++;
1856 usbvision
->have_tuner
= usbvision_device_data
[model
].Tuner
;
1857 if (usbvision
->have_tuner
) {
1858 usbvision
->tuner_type
= usbvision_device_data
[model
].TunerType
;
1861 usbvision
->tuner_addr
= ADDR_UNSET
;
1863 usbvision
->DevModel
= model
;
1864 usbvision
->remove_pending
= 0;
1865 usbvision
->iface
= ifnum
;
1866 usbvision
->ifaceAlt
= 0;
1867 usbvision
->video_endp
= endpoint
->bEndpointAddress
;
1868 usbvision
->isocPacketSize
= 0;
1869 usbvision
->usb_bandwidth
= 0;
1870 usbvision
->user
= 0;
1871 usbvision
->streaming
= Stream_Off
;
1872 usbvision_register_video(usbvision
);
1873 usbvision_configure_video(usbvision
);
1874 up(&usbvision
->lock
);
1877 usb_set_intfdata (intf
, usbvision
);
1878 usbvision_create_sysfs(usbvision
->vdev
);
1880 PDEBUG(DBG_PROBE
, "success");
1886 * usbvision_disconnect()
1888 * This procedure stops all driver activity, deallocates interface-private
1889 * structure (pointed by 'ptr') and after that driver should be removable
1890 * with no ill consequences.
1893 static void __devexit
usbvision_disconnect(struct usb_interface
*intf
)
1895 struct usb_usbvision
*usbvision
= usb_get_intfdata(intf
);
1897 PDEBUG(DBG_PROBE
, "");
1899 if (usbvision
== NULL
) {
1900 err("%s: usb_get_intfdata() failed", __FUNCTION__
);
1903 usb_set_intfdata (intf
, NULL
);
1905 down(&usbvision
->lock
);
1907 // At this time we ask to cancel outstanding URBs
1908 usbvision_stop_isoc(usbvision
);
1910 if (usbvision
->power
) {
1911 usbvision_i2c_unregister(usbvision
);
1912 usbvision_power_off(usbvision
);
1914 usbvision
->remove_pending
= 1; // Now all ISO data will be ignored
1916 usb_put_dev(usbvision
->dev
);
1917 usbvision
->dev
= NULL
; // USB device is no more
1919 up(&usbvision
->lock
);
1921 if (usbvision
->user
) {
1922 printk(KERN_INFO
"%s: In use, disconnect pending\n", __FUNCTION__
);
1923 wake_up_interruptible(&usbvision
->wait_frame
);
1924 wake_up_interruptible(&usbvision
->wait_stream
);
1927 usbvision_release(usbvision
);
1930 PDEBUG(DBG_PROBE
, "success");
1934 static struct usb_driver usbvision_driver
= {
1935 .name
= "usbvision",
1936 .id_table
= usbvision_table
,
1937 .probe
= usbvision_probe
,
1938 .disconnect
= usbvision_disconnect
1944 * This code is run to initialize the driver.
1947 static int __init
usbvision_init(void)
1951 PDEBUG(DBG_PROBE
, "");
1953 PDEBUG(DBG_IOCTL
, "IOCTL debugging is enabled [video]");
1954 PDEBUG(DBG_IO
, "IO debugging is enabled [video]");
1955 PDEBUG(DBG_PROBE
, "PROBE debugging is enabled [video]");
1956 PDEBUG(DBG_MMAP
, "MMAP debugging is enabled [video]");
1958 /* disable planar mode support unless compression enabled */
1959 if (isocMode
!= ISOC_MODE_COMPRESS
) {
1960 // FIXME : not the right way to set supported flag
1961 usbvision_v4l2_format
[6].supported
= 0; // V4L2_PIX_FMT_YVU420
1962 usbvision_v4l2_format
[7].supported
= 0; // V4L2_PIX_FMT_YUV422P
1965 errCode
= usb_register(&usbvision_driver
);
1968 printk(KERN_INFO DRIVER_DESC
" : " USBVISION_VERSION_STRING
"\n");
1969 PDEBUG(DBG_PROBE
, "success");
1974 static void __exit
usbvision_exit(void)
1976 PDEBUG(DBG_PROBE
, "");
1978 usb_deregister(&usbvision_driver
);
1979 PDEBUG(DBG_PROBE
, "success");
1982 module_init(usbvision_init
);
1983 module_exit(usbvision_exit
);
1986 * Overrides for Emacs so that we follow Linus's tabbing style.
1987 * ---------------------------------------------------------------------------