Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / usbvision / usbvision-video.c
blobaa3258bbb4afe49f81b52bcd17d20d0420908e42
1 /*
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
25 * implemented.
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
35 * TODO:
36 * - use submit_urb for all setup packets
37 * - Fix memory settings for nt1004. It is 4 times as big as the
38 * nt1003 memory.
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>
52 #include <linux/mm.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>
60 #include <asm/io.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>
73 #ifdef CONFIG_KMOD
74 #include <linux/kmod.h>
75 #endif
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)
97 #else
98 #define PDEBUG(level, fmt, args...) do {} while(0)
99 #endif
101 #define DBG_IOCTL 1<<0
102 #define DBG_IO 1<<1
103 #define DBG_PROBE 1<<2
104 #define DBG_MMAP 1<<3
106 //String operations
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[] = {
127 .name = "PAL",
128 .id = V4L2_STD_PAL,
129 }, {
130 .name = "NTSC",
131 .id = V4L2_STD_NTSC,
132 }, {
133 .name = "SECAM",
134 .id = V4L2_STD_SECAM,
135 }, {
136 .name = "PAL-M",
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);
163 #else // Old Style
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 ...)
172 #endif
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)");
182 // Misc stuff
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;
226 ctrl.value = 0;
227 if(usbvision->user)
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;
239 ctrl.value = 0;
240 if(usbvision->user)
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;
252 ctrl.value = 0;
253 if(usbvision->user)
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;
265 ctrl.value = 0;
266 if(usbvision->user)
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)
298 int res;
299 if (!vdev)
300 return;
301 do {
302 res=class_device_create_file(&vdev->class_dev,
303 &class_device_attr_version);
304 if (res<0)
305 break;
306 res=class_device_create_file(&vdev->class_dev,
307 &class_device_attr_model);
308 if (res<0)
309 break;
310 res=class_device_create_file(&vdev->class_dev,
311 &class_device_attr_hue);
312 if (res<0)
313 break;
314 res=class_device_create_file(&vdev->class_dev,
315 &class_device_attr_contrast);
316 if (res<0)
317 break;
318 res=class_device_create_file(&vdev->class_dev,
319 &class_device_attr_brightness);
320 if (res<0)
321 break;
322 res=class_device_create_file(&vdev->class_dev,
323 &class_device_attr_saturation);
324 if (res<0)
325 break;
326 res=class_device_create_file(&vdev->class_dev,
327 &class_device_attr_streaming);
328 if (res<0)
329 break;
330 res=class_device_create_file(&vdev->class_dev,
331 &class_device_attr_compression);
332 if (res<0)
333 break;
334 res=class_device_create_file(&vdev->class_dev,
335 &class_device_attr_bridge);
336 if (res>=0)
337 return;
338 } while (0);
340 err("%s error: %d\n", __FUNCTION__, res);
343 static void usbvision_remove_sysfs(struct video_device *vdev)
345 if (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);
369 * usbvision_open()
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);
380 int errCode = 0;
382 PDEBUG(DBG_IO, "open");
385 usbvision_reset_powerOffTimer(usbvision);
387 if (usbvision->user)
388 errCode = -EBUSY;
389 else {
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);
396 if (errCode) {
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 */
404 if (!errCode) {
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) {
413 int setup_ok = 0;
414 setup_ok = usbvision_setup(usbvision,isocMode);
415 if (setup_ok)
416 usbvision->initialized = 1;
417 else
418 errCode = -EBUSY;
421 if (!errCode) {
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);
426 usbvision->user++;
428 else {
429 if (PowerOnAtOpen) {
430 usbvision_i2c_unregister(usbvision);
431 usbvision_power_off(usbvision);
432 usbvision->initialized = 0;
435 up(&usbvision->lock);
438 if (errCode) {
441 /* prepare queues */
442 usbvision_empty_framequeues(usbvision);
444 PDEBUG(DBG_IO, "success");
445 return errCode;
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);
473 usbvision->user--;
475 if (PowerOnAtOpen) {
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");
491 return 0;
496 * usbvision_ioctl()
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))
508 return -EFAULT;
510 switch (cmd) {
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;
518 int errCode;
520 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
521 return -EINVAL;
522 if (!capable(CAP_SYS_ADMIN))
523 return -EPERM;
524 /* NT100x has a 8-bit register space */
525 if (cmd == VIDIOC_DBG_G_REGISTER)
526 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
527 else
528 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
529 if (errCode < 0) {
530 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__,
531 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S', errCode);
532 return 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);
540 return 0;
542 #endif
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,
550 sizeof(vc->card));
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 |
555 V4L2_CAP_AUDIO |
556 V4L2_CAP_READWRITE |
557 V4L2_CAP_STREAMING |
558 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
559 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
560 return 0;
562 case VIDIOC_ENUMINPUT:
564 struct v4l2_input *vi = arg;
565 int chan;
567 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
568 return -EINVAL;
569 if (usbvision->have_tuner) {
570 chan = vi->index;
572 else {
573 chan = vi->index + 1; //skip Television string
575 switch(chan) {
576 case 0:
577 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
578 strcpy(vi->name, "White Video Input");
580 else {
581 strcpy(vi->name, "Television");
582 vi->type = V4L2_INPUT_TYPE_TUNER;
583 vi->audioset = 1;
584 vi->tuner = chan;
585 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
587 break;
588 case 1:
589 vi->type = V4L2_INPUT_TYPE_CAMERA;
590 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
591 strcpy(vi->name, "Green Video Input");
593 else {
594 strcpy(vi->name, "Composite Video Input");
596 vi->std = V4L2_STD_PAL;
597 break;
598 case 2:
599 vi->type = V4L2_INPUT_TYPE_CAMERA;
600 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
601 strcpy(vi->name, "Yellow Video Input");
603 else {
604 strcpy(vi->name, "S-Video Input");
606 vi->std = V4L2_STD_PAL;
607 break;
608 case 3:
609 vi->type = V4L2_INPUT_TYPE_CAMERA;
610 strcpy(vi->name, "Red Video Input");
611 vi->std = V4L2_STD_PAL;
612 break;
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);
616 return 0;
618 case VIDIOC_ENUMSTD:
620 struct v4l2_standard *e = arg;
621 unsigned int i;
622 int ret;
624 i = e->index;
625 if (i >= TVNORMS)
626 return -EINVAL;
627 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
628 tvnorms[e->index].name);
629 e->index = i;
630 if (ret < 0)
631 return ret;
632 return 0;
634 case VIDIOC_G_INPUT:
636 int *input = arg;
637 *input = usbvision->ctl_input;
638 return 0;
640 case VIDIOC_S_INPUT:
642 int *input = arg;
643 if ((*input >= usbvision->video_inputs) || (*input < 0) )
644 return -EINVAL;
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);
652 return 0;
654 case VIDIOC_G_STD:
656 v4l2_std_id *id = arg;
658 *id = usbvision->tvnorm->id;
660 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
661 return 0;
663 case VIDIOC_S_STD:
665 v4l2_std_id *id = arg;
666 unsigned int i;
668 for (i = 0; i < TVNORMS; i++)
669 if (*id == tvnorms[i].id)
670 break;
671 if (i == TVNORMS)
672 for (i = 0; i < TVNORMS; i++)
673 if (*id & tvnorms[i].id)
674 break;
675 if (i == TVNORMS)
676 return -EINVAL;
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);
687 return 0;
689 case VIDIOC_G_TUNER:
691 struct v4l2_tuner *vt = arg;
693 if (!usbvision->have_tuner || vt->index) // Only tuner 0
694 return -EINVAL;
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);
700 return 0;
702 case VIDIOC_S_TUNER:
704 struct v4l2_tuner *vt = arg;
706 // Only no or one tuner for now
707 if (!usbvision->have_tuner || vt->index)
708 return -EINVAL;
709 /* let clients handle this */
710 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
712 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
713 return 0;
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);
723 return 0;
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)
731 return -EINVAL;
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);
736 return 0;
738 case VIDIOC_G_AUDIO:
740 struct v4l2_audio *v = arg;
741 memset(v,0, sizeof(v));
742 strcpy(v->name, "TV");
743 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
744 return 0;
746 case VIDIOC_S_AUDIO:
748 struct v4l2_audio *v = arg;
749 if(v->index) {
750 return -EINVAL;
752 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
753 return 0;
755 case VIDIOC_QUERYCTRL:
757 struct v4l2_queryctrl *ctrl = arg;
758 int id=ctrl->id;
760 memset(ctrl,0,sizeof(*ctrl));
761 ctrl->id=id;
763 call_i2c_clients(usbvision, cmd, arg);
765 if (ctrl->type)
766 return 0;
767 else
768 return -EINVAL;
770 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
772 case VIDIOC_G_CTRL:
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);
777 return 0;
779 case VIDIOC_S_CTRL:
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);
785 return 0;
787 case VIDIOC_REQBUFS:
789 struct v4l2_requestbuffers *vr = arg;
790 int ret;
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))
797 return -EINVAL;
799 if(usbvision->streaming == Stream_On) {
800 if ((ret = usbvision_stream_interrupt(usbvision)))
801 return ret;
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);
811 return 0;
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) {
821 return -EINVAL;
823 if(vb->index>=usbvision->num_frames) {
824 return -EINVAL;
826 // Updating the corresponding frame state
827 vb->flags = 0;
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;
844 return 0;
846 case VIDIOC_QBUF:
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) {
854 return -EINVAL;
856 if(vb->index>=usbvision->num_frames) {
857 return -EINVAL;
860 frame = &usbvision->frame[vb->index];
862 if (frame->grabstate != FrameState_Unused) {
863 return -EAGAIN;
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);
881 return 0;
883 case VIDIOC_DQBUF:
885 struct v4l2_buffer *vb = arg;
886 int ret;
887 struct usbvision_frame *f;
888 unsigned long lock_flags;
890 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
891 return -EINVAL;
893 if (list_empty(&(usbvision->outqueue))) {
894 if (usbvision->streaming == Stream_Idle)
895 return -EINVAL;
896 ret = wait_event_interruptible
897 (usbvision->wait_frame,
898 !list_empty(&(usbvision->outqueue)));
899 if (ret)
900 return ret;
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;
919 return 0;
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");
931 return 0;
933 case VIDIOC_STREAMOFF:
935 int *type = arg;
936 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
938 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
939 return -EINVAL;
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");
949 return 0;
951 case VIDIOC_ENUM_FMT:
953 struct v4l2_fmtdesc *vfd = arg;
955 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
956 return -EINVAL;
958 vfd->flags = 0;
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));
963 return 0;
965 case VIDIOC_G_FMT:
967 struct v4l2_format *vf = arg;
969 switch (vf->type) {
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);
981 return 0;
983 default:
984 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
985 return -EINVAL;
987 return 0;
989 case VIDIOC_TRY_FMT:
990 case VIDIOC_S_FMT:
992 struct v4l2_format *vf = arg;
993 int formatIdx,ret;
995 switch(vf->type) {
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];
1002 break;
1005 /* robustness */
1006 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1007 return -EINVAL;
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);
1018 return 0;
1021 /* stop io in case it is already in progress */
1022 if(usbvision->streaming == Stream_On) {
1023 if ((ret = usbvision_stream_interrupt(usbvision)))
1024 return ret;
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);
1038 return 0;
1040 default:
1041 return -EINVAL;
1044 default:
1045 return -ENOIOCTLCMD;
1047 return 0;
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;
1065 int ret,i;
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))
1071 return -EFAULT;
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
1077 VIDIOC_REQBUF */
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))) {
1109 if(noblock)
1110 return -EAGAIN;
1112 ret = wait_event_interruptible
1113 (usbvision->wait_frame,
1114 !list_empty(&(usbvision->outqueue)));
1115 if (ret)
1116 return ret;
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;
1128 return 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)) {
1139 return -EFAULT;
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;
1152 /* } */
1154 return count;
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;
1161 void *pos;
1162 u32 i;
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);
1173 return -EFAULT;
1176 if (!(vma->vm_flags & VM_WRITE) ||
1177 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1178 up(&usbvision->lock);
1179 return -EINVAL;
1182 for (i = 0; i < usbvision->num_frames; i++) {
1183 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1184 break;
1186 if (i == usbvision->num_frames) {
1187 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1188 up(&usbvision->lock);
1189 return -EINVAL;
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;
1197 while (size > 0) {
1199 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1200 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1201 up(&usbvision->lock);
1202 return -EAGAIN;
1204 start += PAGE_SIZE;
1205 pos += PAGE_SIZE;
1206 size -= PAGE_SIZE;
1209 up(&usbvision->lock);
1210 return 0;
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;
1223 int errCode = 0;
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__);
1231 errCode = -EBUSY;
1233 else {
1234 if(PowerOnAtOpen) {
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);
1244 if (errCode < 0) {
1245 usbvision->last_error = errCode;
1246 return -EBUSY;
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);
1255 usbvision->user++;
1258 if (errCode) {
1259 if (PowerOnAtOpen) {
1260 usbvision_i2c_unregister(usbvision);
1261 usbvision_power_off(usbvision);
1262 usbvision->initialized = 0;
1265 up(&usbvision->lock);
1266 return errCode;
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);
1274 int errCode = 0;
1276 PDEBUG(DBG_IO, "");
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);
1286 usbvision->radio=0;
1287 usbvision->user--;
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");
1304 return errCode;
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))
1314 return -EIO;
1316 switch (cmd) {
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,
1324 sizeof(vc->card));
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");
1330 return 0;
1332 case VIDIOC_QUERYCTRL:
1334 struct v4l2_queryctrl *ctrl = arg;
1335 int id=ctrl->id;
1337 memset(ctrl,0,sizeof(*ctrl));
1338 ctrl->id=id;
1340 call_i2c_clients(usbvision, cmd, arg);
1341 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1343 if (ctrl->type)
1344 return 0;
1345 else
1346 return -EINVAL;
1349 case VIDIOC_G_CTRL:
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);
1355 return 0;
1357 case VIDIOC_S_CTRL:
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);
1363 return 0;
1365 case VIDIOC_G_TUNER:
1367 struct v4l2_tuner *t = arg;
1369 if (t->index > 0)
1370 return -EINVAL;
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);
1379 return 0;
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)
1387 return -EINVAL;
1388 /* let clients handle this */
1389 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1391 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1392 return 0;
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");
1401 return 0;
1403 case VIDIOC_S_AUDIO:
1404 case VIDIOC_S_INPUT:
1405 case VIDIOC_S_STD:
1406 return 0;
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);
1419 return 0;
1421 case VIDIOC_S_FREQUENCY:
1423 struct v4l2_frequency *f = arg;
1425 if (f->tuner != 0)
1426 return -EINVAL;
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);
1431 return 0;
1433 default:
1435 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1436 return -ENOIOCTLCMD;
1439 return 0;
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)
1456 /* TODO */
1457 return -EINVAL;
1461 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1463 /* TODO */
1464 return -EINVAL;
1467 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1468 unsigned int cmd, void *arg)
1470 /* TODO */
1471 return -EINVAL;
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
1485 // Video template
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,
1502 .minor = -1,
1506 // Radio template
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",
1523 .minor = -1,
1527 // vbi template
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",
1544 .minor = -1,
1548 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1549 struct video_device *vdev_template,
1550 char *name)
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__);
1557 return NULL;
1560 vdev = video_device_alloc();
1561 if (NULL == vdev) {
1562 return NULL;
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);
1569 return vdev;
1572 // unregister video4linux devices
1573 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1575 // vbi Device:
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);
1581 else {
1582 video_device_release(usbvision->vbi);
1584 usbvision->vbi = NULL;
1587 // Radio Device:
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);
1593 else {
1594 video_device_release(usbvision->rdev);
1596 usbvision->rdev = NULL;
1599 // Video Device:
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);
1605 else {
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)
1615 // Video Device:
1616 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1617 if (usbvision->vdev == NULL) {
1618 goto err_exit;
1620 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1621 goto err_exit;
1623 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision->nr,usbvision->vdev->minor & 0x1f);
1625 // Radio Device:
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) {
1630 goto err_exit;
1632 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1633 goto err_exit;
1635 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision->nr, usbvision->rdev->minor & 0x1f);
1637 // vbi Device:
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) {
1641 goto err_exit;
1643 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1644 goto err_exit;
1646 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision->nr,usbvision->vbi->minor & 0x1f);
1648 // all done
1649 return 0;
1651 err_exit:
1652 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1653 usbvision_unregister_video(usbvision);
1654 return -1;
1658 * usbvision_alloc()
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) {
1670 goto err_exit;
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) {
1680 goto err_exit;
1682 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1683 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1685 usbvision_init_powerOffTimer(usbvision);
1687 return usbvision;
1689 err_exit:
1690 if (usbvision && usbvision->ctrlUrb) {
1691 usb_free_urb(usbvision->ctrlUrb);
1693 if (usbvision) {
1694 kfree(usbvision);
1696 return NULL;
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);
1725 kfree(usbvision);
1727 PDEBUG(DBG_PROBE, "success");
1731 /******************************** usb interface *****************************************/
1733 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1735 int model,i;
1737 if (usbvision == NULL)
1738 return;
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;
1745 } else {
1746 usbvision->Vin_Reg2_Preset = 0;
1749 for (i = 0; i < TVNORMS; i++)
1750 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1751 break;
1752 if (i == TVNORMS)
1753 i = 0;
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);
1768 * usbvision_probe()
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;
1783 int model,i;
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);
1792 return -ENODEV;
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];
1800 else {
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);
1807 return -ENODEV;
1809 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1810 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1811 return -ENODEV;
1814 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1815 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1816 return -ENOMEM;
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;
1825 else {
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");
1841 return -ENOMEM;
1844 for (i = 0; i < usbvision->num_alt ; i++) {
1845 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1846 wMaxPacketSize);
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");
1881 return 0;
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__);
1901 return;
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);
1926 else {
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
1942 * usbvision_init()
1944 * This code is run to initialize the driver.
1947 static int __init usbvision_init(void)
1949 int errCode;
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);
1967 if (errCode == 0) {
1968 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1969 PDEBUG(DBG_PROBE, "success");
1971 return errCode;
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 * ---------------------------------------------------------------------------
1988 * Local variables:
1989 * c-basic-offset: 8
1990 * End: