Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / usbvision / usbvision-video.c
blobdf52f8a602155c6c32e26011f6dd93f0c6c7b097
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.
40 * Seems impossible, needs a codec interface. Which one?
41 * - Clean up the driver.
42 * - optimization for performance.
43 * - Add Videotext capability (VBI). Working on it.....
44 * - Check audio for other devices
48 #include <linux/version.h>
49 #include <linux/kernel.h>
50 #include <linux/list.h>
51 #include <linux/timer.h>
52 #include <linux/slab.h>
53 #include <linux/mm.h>
54 #include <linux/utsname.h>
55 #include <linux/highmem.h>
56 #include <linux/videodev.h>
57 #include <linux/vmalloc.h>
58 #include <linux/module.h>
59 #include <linux/init.h>
60 #include <linux/spinlock.h>
61 #include <asm/io.h>
62 #include <linux/videodev2.h>
63 #include <linux/video_decoder.h>
64 #include <linux/i2c.h>
66 #include <media/saa7115.h>
67 #include <media/v4l2-common.h>
68 #include <media/tuner.h>
69 #include <media/audiochip.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>,\
81 Dwaine Garden <DwaineGarden@rogers.com>"
82 #define DRIVER_NAME "usbvision"
83 #define DRIVER_ALIAS "USBVision"
84 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85 #define DRIVER_LICENSE "GPL"
86 #define USBVISION_DRIVER_VERSION_MAJOR 0
87 #define USBVISION_DRIVER_VERSION_MINOR 9
88 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,\
90 USBVISION_DRIVER_VERSION_MINOR,\
91 USBVISION_DRIVER_VERSION_PATCHLEVEL)
92 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR)\
93 "." __stringify(USBVISION_DRIVER_VERSION_MINOR)\
94 "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
96 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
99 #ifdef USBVISION_DEBUG
100 #define PDEBUG(level, fmt, args...) \
101 if (video_debug & (level)) \
102 info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ ,\
103 ## args)
104 #else
105 #define PDEBUG(level, fmt, args...) do {} while(0)
106 #endif
108 #define DBG_IO 1<<1
109 #define DBG_PROBE 1<<2
110 #define DBG_MMAP 1<<3
112 //String operations
113 #define rmspace(str) while(*str==' ') str++;
114 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
117 /* sequential number of usbvision device */
118 static int usbvision_nr = 0;
120 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
121 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
122 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
123 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
124 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
125 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
126 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
127 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
128 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
131 /* Function prototypes */
132 static void usbvision_release(struct usb_usbvision *usbvision);
134 /* Default initialization of device driver parameters */
135 /* Set the default format for ISOC endpoint */
136 static int isocMode = ISOC_MODE_COMPRESS;
137 /* Set the default Debug Mode of the device driver */
138 static int video_debug = 0;
139 /* Set the default device to power on at startup */
140 static int PowerOnAtOpen = 1;
141 /* Sequential Number of Video Device */
142 static int video_nr = -1;
143 /* Sequential Number of Radio Device */
144 static int radio_nr = -1;
145 /* Sequential Number of VBI Device */
146 static int vbi_nr = -1;
148 /* Grab parameters for the device driver */
150 /* Showing parameters under SYSFS */
151 module_param(isocMode, int, 0444);
152 module_param(video_debug, int, 0444);
153 module_param(PowerOnAtOpen, int, 0444);
154 module_param(video_nr, int, 0444);
155 module_param(radio_nr, int, 0444);
156 module_param(vbi_nr, int, 0444);
158 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
159 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
160 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
161 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
162 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
163 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
166 // Misc stuff
167 MODULE_AUTHOR(DRIVER_AUTHOR);
168 MODULE_DESCRIPTION(DRIVER_DESC);
169 MODULE_LICENSE(DRIVER_LICENSE);
170 MODULE_VERSION(USBVISION_VERSION_STRING);
171 MODULE_ALIAS(DRIVER_ALIAS);
174 /*****************************************************************************/
175 /* SYSFS Code - Copied from the stv680.c usb module. */
176 /* Device information is located at /sys/class/video4linux/video0 */
177 /* Device parameters information is located at /sys/module/usbvision */
178 /* Device USB Information is located at */
179 /* /sys/bus/usb/drivers/USBVision Video Grabber */
180 /*****************************************************************************/
183 #define YES_NO(x) ((x) ? "Yes" : "No")
185 static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
187 struct video_device *vdev =
188 container_of(cd, struct video_device, class_dev);
189 return video_get_drvdata(vdev);
192 static ssize_t show_version(struct device *cd,
193 struct device_attribute *attr, char *buf)
195 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
197 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
199 static ssize_t show_model(struct device *cd,
200 struct device_attribute *attr, char *buf)
202 struct video_device *vdev =
203 container_of(cd, struct video_device, class_dev);
204 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
205 return sprintf(buf, "%s\n",
206 usbvision_device_data[usbvision->DevModel].ModelString);
208 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
210 static ssize_t show_hue(struct device *cd,
211 struct device_attribute *attr, char *buf)
213 struct video_device *vdev =
214 container_of(cd, struct video_device, class_dev);
215 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
216 struct v4l2_control ctrl;
217 ctrl.id = V4L2_CID_HUE;
218 ctrl.value = 0;
219 if(usbvision->user)
220 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
221 return sprintf(buf, "%d\n", ctrl.value);
223 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
225 static ssize_t show_contrast(struct device *cd,
226 struct device_attribute *attr, char *buf)
228 struct video_device *vdev =
229 container_of(cd, struct video_device, class_dev);
230 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
231 struct v4l2_control ctrl;
232 ctrl.id = V4L2_CID_CONTRAST;
233 ctrl.value = 0;
234 if(usbvision->user)
235 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
236 return sprintf(buf, "%d\n", ctrl.value);
238 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
240 static ssize_t show_brightness(struct device *cd,
241 struct device_attribute *attr, char *buf)
243 struct video_device *vdev =
244 container_of(cd, struct video_device, class_dev);
245 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
246 struct v4l2_control ctrl;
247 ctrl.id = V4L2_CID_BRIGHTNESS;
248 ctrl.value = 0;
249 if(usbvision->user)
250 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
251 return sprintf(buf, "%d\n", ctrl.value);
253 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
255 static ssize_t show_saturation(struct device *cd,
256 struct device_attribute *attr, char *buf)
258 struct video_device *vdev =
259 container_of(cd, struct video_device, class_dev);
260 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
261 struct v4l2_control ctrl;
262 ctrl.id = V4L2_CID_SATURATION;
263 ctrl.value = 0;
264 if(usbvision->user)
265 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
266 return sprintf(buf, "%d\n", ctrl.value);
268 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
270 static ssize_t show_streaming(struct device *cd,
271 struct device_attribute *attr, char *buf)
273 struct video_device *vdev =
274 container_of(cd, struct video_device, class_dev);
275 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
276 return sprintf(buf, "%s\n",
277 YES_NO(usbvision->streaming==Stream_On?1:0));
279 static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
281 static ssize_t show_compression(struct device *cd,
282 struct device_attribute *attr, char *buf)
284 struct video_device *vdev =
285 container_of(cd, struct video_device, class_dev);
286 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
287 return sprintf(buf, "%s\n",
288 YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
290 static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
292 static ssize_t show_device_bridge(struct device *cd,
293 struct device_attribute *attr, char *buf)
295 struct video_device *vdev =
296 container_of(cd, struct video_device, class_dev);
297 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
298 return sprintf(buf, "%d\n", usbvision->bridgeType);
300 static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
302 static void usbvision_create_sysfs(struct video_device *vdev)
304 int res;
305 if (!vdev)
306 return;
307 do {
308 res = device_create_file(&vdev->class_dev,
309 &dev_attr_version);
310 if (res<0)
311 break;
312 res = device_create_file(&vdev->class_dev,
313 &dev_attr_model);
314 if (res<0)
315 break;
316 res = device_create_file(&vdev->class_dev,
317 &dev_attr_hue);
318 if (res<0)
319 break;
320 res = device_create_file(&vdev->class_dev,
321 &dev_attr_contrast);
322 if (res<0)
323 break;
324 res = device_create_file(&vdev->class_dev,
325 &dev_attr_brightness);
326 if (res<0)
327 break;
328 res = device_create_file(&vdev->class_dev,
329 &dev_attr_saturation);
330 if (res<0)
331 break;
332 res = device_create_file(&vdev->class_dev,
333 &dev_attr_streaming);
334 if (res<0)
335 break;
336 res = device_create_file(&vdev->class_dev,
337 &dev_attr_compression);
338 if (res<0)
339 break;
340 res = device_create_file(&vdev->class_dev,
341 &dev_attr_bridge);
342 if (res>=0)
343 return;
344 } while (0);
346 err("%s error: %d\n", __FUNCTION__, res);
349 static void usbvision_remove_sysfs(struct video_device *vdev)
351 if (vdev) {
352 device_remove_file(&vdev->class_dev,
353 &dev_attr_version);
354 device_remove_file(&vdev->class_dev,
355 &dev_attr_model);
356 device_remove_file(&vdev->class_dev,
357 &dev_attr_hue);
358 device_remove_file(&vdev->class_dev,
359 &dev_attr_contrast);
360 device_remove_file(&vdev->class_dev,
361 &dev_attr_brightness);
362 device_remove_file(&vdev->class_dev,
363 &dev_attr_saturation);
364 device_remove_file(&vdev->class_dev,
365 &dev_attr_streaming);
366 device_remove_file(&vdev->class_dev,
367 &dev_attr_compression);
368 device_remove_file(&vdev->class_dev,
369 &dev_attr_bridge);
375 * usbvision_open()
377 * This is part of Video 4 Linux API. The driver can be opened by one
378 * client only (checks internal counter 'usbvision->user'). The procedure
379 * then allocates buffers needed for video processing.
382 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
384 struct video_device *dev = video_devdata(file);
385 struct usb_usbvision *usbvision =
386 (struct usb_usbvision *) video_get_drvdata(dev);
387 int errCode = 0;
389 PDEBUG(DBG_IO, "open");
392 usbvision_reset_powerOffTimer(usbvision);
394 if (usbvision->user)
395 errCode = -EBUSY;
396 else {
397 /* Allocate memory for the scratch ring buffer */
398 errCode = usbvision_scratch_alloc(usbvision);
399 if (isocMode==ISOC_MODE_COMPRESS) {
400 /* Allocate intermediate decompression buffers
401 only if needed */
402 errCode = usbvision_decompress_alloc(usbvision);
404 if (errCode) {
405 /* Deallocate all buffers if trouble */
406 usbvision_scratch_free(usbvision);
407 usbvision_decompress_free(usbvision);
411 /* If so far no errors then we shall start the camera */
412 if (!errCode) {
413 mutex_lock(&usbvision->lock);
414 if (usbvision->power == 0) {
415 usbvision_power_on(usbvision);
416 usbvision_i2c_register(usbvision);
419 /* Send init sequence only once, it's large! */
420 if (!usbvision->initialized) {
421 int setup_ok = 0;
422 setup_ok = usbvision_setup(usbvision,isocMode);
423 if (setup_ok)
424 usbvision->initialized = 1;
425 else
426 errCode = -EBUSY;
429 if (!errCode) {
430 usbvision_begin_streaming(usbvision);
431 errCode = usbvision_init_isoc(usbvision);
432 /* device must be initialized before isoc transfer */
433 usbvision_muxsel(usbvision,0);
434 usbvision->user++;
435 } else {
436 if (PowerOnAtOpen) {
437 usbvision_i2c_unregister(usbvision);
438 usbvision_power_off(usbvision);
439 usbvision->initialized = 0;
442 mutex_unlock(&usbvision->lock);
445 if (errCode) {
448 /* prepare queues */
449 usbvision_empty_framequeues(usbvision);
451 PDEBUG(DBG_IO, "success");
452 return errCode;
456 * usbvision_v4l2_close()
458 * This is part of Video 4 Linux API. The procedure
459 * stops streaming and deallocates all buffers that were earlier
460 * allocated in usbvision_v4l2_open().
463 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
465 struct video_device *dev = video_devdata(file);
466 struct usb_usbvision *usbvision =
467 (struct usb_usbvision *) video_get_drvdata(dev);
469 PDEBUG(DBG_IO, "close");
470 mutex_lock(&usbvision->lock);
472 usbvision_audio_off(usbvision);
473 usbvision_restart_isoc(usbvision);
474 usbvision_stop_isoc(usbvision);
476 usbvision_decompress_free(usbvision);
477 usbvision_frames_free(usbvision);
478 usbvision_empty_framequeues(usbvision);
479 usbvision_scratch_free(usbvision);
481 usbvision->user--;
483 if (PowerOnAtOpen) {
484 /* power off in a little while
485 to avoid off/on every close/open short sequences */
486 usbvision_set_powerOffTimer(usbvision);
487 usbvision->initialized = 0;
490 mutex_unlock(&usbvision->lock);
492 if (usbvision->remove_pending) {
493 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
494 usbvision_release(usbvision);
497 PDEBUG(DBG_IO, "success");
500 return 0;
505 * usbvision_ioctl()
507 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
510 #ifdef CONFIG_VIDEO_ADV_DEBUG
511 static int vidioc_g_register (struct file *file, void *priv,
512 struct v4l2_register *reg)
514 struct video_device *dev = video_devdata(file);
515 struct usb_usbvision *usbvision =
516 (struct usb_usbvision *) video_get_drvdata(dev);
517 int errCode;
519 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
520 return -EINVAL;
521 /* NT100x has a 8-bit register space */
522 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
523 if (errCode < 0) {
524 err("%s: VIDIOC_DBG_G_REGISTER failed: error %d",
525 __FUNCTION__, errCode);
526 return errCode;
528 reg->val = errCode;
529 return 0;
532 static int vidioc_s_register (struct file *file, void *priv,
533 struct v4l2_register *reg)
535 struct video_device *dev = video_devdata(file);
536 struct usb_usbvision *usbvision =
537 (struct usb_usbvision *) video_get_drvdata(dev);
538 int errCode;
540 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
541 return -EINVAL;
542 /* NT100x has a 8-bit register space */
543 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
544 if (errCode < 0) {
545 err("%s: VIDIOC_DBG_S_REGISTER failed: error %d",
546 __FUNCTION__, errCode);
547 return errCode;
549 return 0;
551 #endif
553 static int vidioc_querycap (struct file *file, void *priv,
554 struct v4l2_capability *vc)
556 struct video_device *dev = video_devdata(file);
557 struct usb_usbvision *usbvision =
558 (struct usb_usbvision *) video_get_drvdata(dev);
560 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
561 strlcpy(vc->card,
562 usbvision_device_data[usbvision->DevModel].ModelString,
563 sizeof(vc->card));
564 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
565 sizeof(vc->bus_info));
566 vc->version = USBVISION_DRIVER_VERSION;
567 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
568 V4L2_CAP_AUDIO |
569 V4L2_CAP_READWRITE |
570 V4L2_CAP_STREAMING |
571 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
572 return 0;
575 static int vidioc_enum_input (struct file *file, void *priv,
576 struct v4l2_input *vi)
578 struct video_device *dev = video_devdata(file);
579 struct usb_usbvision *usbvision =
580 (struct usb_usbvision *) video_get_drvdata(dev);
581 int chan;
583 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
584 return -EINVAL;
585 if (usbvision->have_tuner) {
586 chan = vi->index;
587 } else {
588 chan = vi->index + 1; /*skip Television string*/
590 /* Determine the requested input characteristics
591 specific for each usbvision card model */
592 switch(chan) {
593 case 0:
594 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
595 strcpy(vi->name, "White Video Input");
596 } else {
597 strcpy(vi->name, "Television");
598 vi->type = V4L2_INPUT_TYPE_TUNER;
599 vi->audioset = 1;
600 vi->tuner = chan;
601 vi->std = USBVISION_NORMS;
603 break;
604 case 1:
605 vi->type = V4L2_INPUT_TYPE_CAMERA;
606 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
607 strcpy(vi->name, "Green Video Input");
608 } else {
609 strcpy(vi->name, "Composite Video Input");
611 vi->std = V4L2_STD_PAL;
612 break;
613 case 2:
614 vi->type = V4L2_INPUT_TYPE_CAMERA;
615 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
616 strcpy(vi->name, "Yellow Video Input");
617 } else {
618 strcpy(vi->name, "S-Video Input");
620 vi->std = V4L2_STD_PAL;
621 break;
622 case 3:
623 vi->type = V4L2_INPUT_TYPE_CAMERA;
624 strcpy(vi->name, "Red Video Input");
625 vi->std = V4L2_STD_PAL;
626 break;
628 return 0;
631 static int vidioc_g_input (struct file *file, void *priv, unsigned int *input)
633 struct video_device *dev = video_devdata(file);
634 struct usb_usbvision *usbvision =
635 (struct usb_usbvision *) video_get_drvdata(dev);
637 *input = usbvision->ctl_input;
638 return 0;
641 static int vidioc_s_input (struct file *file, void *priv, unsigned int input)
643 struct video_device *dev = video_devdata(file);
644 struct usb_usbvision *usbvision =
645 (struct usb_usbvision *) video_get_drvdata(dev);
647 if ((input >= usbvision->video_inputs) || (input < 0) )
648 return -EINVAL;
650 mutex_lock(&usbvision->lock);
651 usbvision_muxsel(usbvision, input);
652 usbvision_set_input(usbvision);
653 usbvision_set_output(usbvision,
654 usbvision->curwidth,
655 usbvision->curheight);
656 mutex_unlock(&usbvision->lock);
657 return 0;
660 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *id)
662 struct video_device *dev = video_devdata(file);
663 struct usb_usbvision *usbvision =
664 (struct usb_usbvision *) video_get_drvdata(dev);
665 usbvision->tvnormId=*id;
667 mutex_lock(&usbvision->lock);
668 call_i2c_clients(usbvision, VIDIOC_S_STD,
669 &usbvision->tvnormId);
670 mutex_unlock(&usbvision->lock);
671 /* propagate the change to the decoder */
672 usbvision_muxsel(usbvision, usbvision->ctl_input);
674 return 0;
677 static int vidioc_g_tuner (struct file *file, void *priv,
678 struct v4l2_tuner *vt)
680 struct video_device *dev = video_devdata(file);
681 struct usb_usbvision *usbvision =
682 (struct usb_usbvision *) video_get_drvdata(dev);
684 if (!usbvision->have_tuner || vt->index) // Only tuner 0
685 return -EINVAL;
686 if(usbvision->radio) {
687 strcpy(vt->name, "Radio");
688 vt->type = V4L2_TUNER_RADIO;
689 } else {
690 strcpy(vt->name, "Television");
692 /* Let clients fill in the remainder of this struct */
693 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
695 return 0;
698 static int vidioc_s_tuner (struct file *file, void *priv,
699 struct v4l2_tuner *vt)
701 struct video_device *dev = video_devdata(file);
702 struct usb_usbvision *usbvision =
703 (struct usb_usbvision *) video_get_drvdata(dev);
705 // Only no or one tuner for now
706 if (!usbvision->have_tuner || vt->index)
707 return -EINVAL;
708 /* let clients handle this */
709 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
711 return 0;
714 static int vidioc_g_frequency (struct file *file, void *priv,
715 struct v4l2_frequency *freq)
717 struct video_device *dev = video_devdata(file);
718 struct usb_usbvision *usbvision =
719 (struct usb_usbvision *) video_get_drvdata(dev);
721 freq->tuner = 0; // Only one tuner
722 if(usbvision->radio) {
723 freq->type = V4L2_TUNER_RADIO;
724 } else {
725 freq->type = V4L2_TUNER_ANALOG_TV;
727 freq->frequency = usbvision->freq;
729 return 0;
732 static int vidioc_s_frequency (struct file *file, void *priv,
733 struct v4l2_frequency *freq)
735 struct video_device *dev = video_devdata(file);
736 struct usb_usbvision *usbvision =
737 (struct usb_usbvision *) video_get_drvdata(dev);
739 // Only no or one tuner for now
740 if (!usbvision->have_tuner || freq->tuner)
741 return -EINVAL;
743 usbvision->freq = freq->frequency;
744 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, freq);
746 return 0;
749 static int vidioc_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
751 struct video_device *dev = video_devdata(file);
752 struct usb_usbvision *usbvision =
753 (struct usb_usbvision *) video_get_drvdata(dev);
755 memset(a,0,sizeof(*a));
756 if(usbvision->radio) {
757 strcpy(a->name,"Radio");
758 } else {
759 strcpy(a->name, "TV");
762 return 0;
765 static int vidioc_s_audio (struct file *file, void *fh,
766 struct v4l2_audio *a)
768 if(a->index) {
769 return -EINVAL;
772 return 0;
775 static int vidioc_queryctrl (struct file *file, void *priv,
776 struct v4l2_queryctrl *ctrl)
778 struct video_device *dev = video_devdata(file);
779 struct usb_usbvision *usbvision =
780 (struct usb_usbvision *) video_get_drvdata(dev);
781 int id=ctrl->id;
783 memset(ctrl,0,sizeof(*ctrl));
784 ctrl->id=id;
786 call_i2c_clients(usbvision, VIDIOC_QUERYCTRL, ctrl);
788 if (!ctrl->type)
789 return -EINVAL;
791 return 0;
794 static int vidioc_g_ctrl (struct file *file, void *priv,
795 struct v4l2_control *ctrl)
797 struct video_device *dev = video_devdata(file);
798 struct usb_usbvision *usbvision =
799 (struct usb_usbvision *) video_get_drvdata(dev);
800 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
802 return 0;
805 static int vidioc_s_ctrl (struct file *file, void *priv,
806 struct v4l2_control *ctrl)
808 struct video_device *dev = video_devdata(file);
809 struct usb_usbvision *usbvision =
810 (struct usb_usbvision *) video_get_drvdata(dev);
811 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
813 return 0;
816 static int vidioc_reqbufs (struct file *file,
817 void *priv, struct v4l2_requestbuffers *vr)
819 struct video_device *dev = video_devdata(file);
820 struct usb_usbvision *usbvision =
821 (struct usb_usbvision *) video_get_drvdata(dev);
822 int ret;
824 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
826 /* Check input validity:
827 the user must do a VIDEO CAPTURE and MMAP method. */
828 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
829 (vr->memory != V4L2_MEMORY_MMAP))
830 return -EINVAL;
832 if(usbvision->streaming == Stream_On) {
833 if ((ret = usbvision_stream_interrupt(usbvision)))
834 return ret;
837 usbvision_frames_free(usbvision);
838 usbvision_empty_framequeues(usbvision);
839 vr->count = usbvision_frames_alloc(usbvision,vr->count);
841 usbvision->curFrame = NULL;
843 return 0;
846 static int vidioc_querybuf (struct file *file,
847 void *priv, struct v4l2_buffer *vb)
849 struct video_device *dev = video_devdata(file);
850 struct usb_usbvision *usbvision =
851 (struct usb_usbvision *) video_get_drvdata(dev);
852 struct usbvision_frame *frame;
854 /* FIXME : must control
855 that buffers are mapped (VIDIOC_REQBUFS has been called) */
856 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
857 return -EINVAL;
859 if(vb->index>=usbvision->num_frames) {
860 return -EINVAL;
862 /* Updating the corresponding frame state */
863 vb->flags = 0;
864 frame = &usbvision->frame[vb->index];
865 if(frame->grabstate >= FrameState_Ready)
866 vb->flags |= V4L2_BUF_FLAG_QUEUED;
867 if(frame->grabstate >= FrameState_Done)
868 vb->flags |= V4L2_BUF_FLAG_DONE;
869 if(frame->grabstate == FrameState_Unused)
870 vb->flags |= V4L2_BUF_FLAG_MAPPED;
871 vb->memory = V4L2_MEMORY_MMAP;
873 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
875 vb->memory = V4L2_MEMORY_MMAP;
876 vb->field = V4L2_FIELD_NONE;
877 vb->length = usbvision->curwidth*
878 usbvision->curheight*
879 usbvision->palette.bytes_per_pixel;
880 vb->timestamp = usbvision->frame[vb->index].timestamp;
881 vb->sequence = usbvision->frame[vb->index].sequence;
882 return 0;
885 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
887 struct video_device *dev = video_devdata(file);
888 struct usb_usbvision *usbvision =
889 (struct usb_usbvision *) video_get_drvdata(dev);
890 struct usbvision_frame *frame;
891 unsigned long lock_flags;
893 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
894 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
895 return -EINVAL;
897 if(vb->index>=usbvision->num_frames) {
898 return -EINVAL;
901 frame = &usbvision->frame[vb->index];
903 if (frame->grabstate != FrameState_Unused) {
904 return -EAGAIN;
907 /* Mark it as ready and enqueue frame */
908 frame->grabstate = FrameState_Ready;
909 frame->scanstate = ScanState_Scanning;
910 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
912 vb->flags &= ~V4L2_BUF_FLAG_DONE;
914 /* set v4l2_format index */
915 frame->v4l2_format = usbvision->palette;
917 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
918 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
919 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
921 return 0;
924 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *vb)
926 struct video_device *dev = video_devdata(file);
927 struct usb_usbvision *usbvision =
928 (struct usb_usbvision *) video_get_drvdata(dev);
929 int ret;
930 struct usbvision_frame *f;
931 unsigned long lock_flags;
933 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
934 return -EINVAL;
936 if (list_empty(&(usbvision->outqueue))) {
937 if (usbvision->streaming == Stream_Idle)
938 return -EINVAL;
939 ret = wait_event_interruptible
940 (usbvision->wait_frame,
941 !list_empty(&(usbvision->outqueue)));
942 if (ret)
943 return ret;
946 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
947 f = list_entry(usbvision->outqueue.next,
948 struct usbvision_frame, frame);
949 list_del(usbvision->outqueue.next);
950 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
952 f->grabstate = FrameState_Unused;
954 vb->memory = V4L2_MEMORY_MMAP;
955 vb->flags = V4L2_BUF_FLAG_MAPPED |
956 V4L2_BUF_FLAG_QUEUED |
957 V4L2_BUF_FLAG_DONE;
958 vb->index = f->index;
959 vb->sequence = f->sequence;
960 vb->timestamp = f->timestamp;
961 vb->field = V4L2_FIELD_NONE;
962 vb->bytesused = f->scanlength;
964 return 0;
967 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
969 struct video_device *dev = video_devdata(file);
970 struct usb_usbvision *usbvision =
971 (struct usb_usbvision *) video_get_drvdata(dev);
972 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
974 usbvision->streaming = Stream_On;
975 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
977 return 0;
980 static int vidioc_streamoff(struct file *file,
981 void *priv, enum v4l2_buf_type type)
983 struct video_device *dev = video_devdata(file);
984 struct usb_usbvision *usbvision =
985 (struct usb_usbvision *) video_get_drvdata(dev);
986 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
988 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
989 return -EINVAL;
991 if(usbvision->streaming == Stream_On) {
992 usbvision_stream_interrupt(usbvision);
993 /* Stop all video streamings */
994 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
996 usbvision_empty_framequeues(usbvision);
998 return 0;
1001 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1002 struct v4l2_fmtdesc *vfd)
1004 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
1005 return -EINVAL;
1007 vfd->flags = 0;
1008 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
1010 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
1011 memset(vfd->reserved, 0, sizeof(vfd->reserved));
1012 return 0;
1015 static int vidioc_g_fmt_cap (struct file *file, void *priv,
1016 struct v4l2_format *vf)
1018 struct video_device *dev = video_devdata(file);
1019 struct usb_usbvision *usbvision =
1020 (struct usb_usbvision *) video_get_drvdata(dev);
1021 vf->fmt.pix.width = usbvision->curwidth;
1022 vf->fmt.pix.height = usbvision->curheight;
1023 vf->fmt.pix.pixelformat = usbvision->palette.format;
1024 vf->fmt.pix.bytesperline =
1025 usbvision->curwidth*usbvision->palette.bytes_per_pixel;
1026 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
1027 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1028 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
1030 return 0;
1033 static int vidioc_try_fmt_cap (struct file *file, void *priv,
1034 struct v4l2_format *vf)
1036 struct video_device *dev = video_devdata(file);
1037 struct usb_usbvision *usbvision =
1038 (struct usb_usbvision *) video_get_drvdata(dev);
1039 int formatIdx;
1041 /* Find requested format in available ones */
1042 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1043 if(vf->fmt.pix.pixelformat ==
1044 usbvision_v4l2_format[formatIdx].format) {
1045 usbvision->palette = usbvision_v4l2_format[formatIdx];
1046 break;
1049 /* robustness */
1050 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1051 return -EINVAL;
1053 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1054 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1056 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
1057 usbvision->palette.bytes_per_pixel;
1058 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1060 return 0;
1063 static int vidioc_s_fmt_cap(struct file *file, void *priv,
1064 struct v4l2_format *vf)
1066 struct video_device *dev = video_devdata(file);
1067 struct usb_usbvision *usbvision =
1068 (struct usb_usbvision *) video_get_drvdata(dev);
1069 int ret;
1071 if( 0 != (ret=vidioc_try_fmt_cap (file, priv, vf)) ) {
1072 return ret;
1075 /* stop io in case it is already in progress */
1076 if(usbvision->streaming == Stream_On) {
1077 if ((ret = usbvision_stream_interrupt(usbvision)))
1078 return ret;
1080 usbvision_frames_free(usbvision);
1081 usbvision_empty_framequeues(usbvision);
1083 usbvision->curFrame = NULL;
1085 /* by now we are committed to the new data... */
1086 mutex_lock(&usbvision->lock);
1087 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1088 mutex_unlock(&usbvision->lock);
1090 return 0;
1093 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1094 size_t count, loff_t *ppos)
1096 struct video_device *dev = video_devdata(file);
1097 struct usb_usbvision *usbvision =
1098 (struct usb_usbvision *) video_get_drvdata(dev);
1099 int noblock = file->f_flags & O_NONBLOCK;
1100 unsigned long lock_flags;
1102 int ret,i;
1103 struct usbvision_frame *frame;
1105 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__,
1106 (unsigned long)count, noblock);
1108 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1109 return -EFAULT;
1111 /* This entry point is compatible with the mmap routines
1112 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
1113 to get frames or call read on the device. */
1114 if(!usbvision->num_frames) {
1115 /* First, allocate some frames to work with
1116 if this has not been done with VIDIOC_REQBUF */
1117 usbvision_frames_free(usbvision);
1118 usbvision_empty_framequeues(usbvision);
1119 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1122 if(usbvision->streaming != Stream_On) {
1123 /* no stream is running, make it running ! */
1124 usbvision->streaming = Stream_On;
1125 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1128 /* Then, enqueue as many frames as possible
1129 (like a user of VIDIOC_QBUF would do) */
1130 for(i=0;i<usbvision->num_frames;i++) {
1131 frame = &usbvision->frame[i];
1132 if(frame->grabstate == FrameState_Unused) {
1133 /* Mark it as ready and enqueue frame */
1134 frame->grabstate = FrameState_Ready;
1135 frame->scanstate = ScanState_Scanning;
1136 /* Accumulated in usbvision_parse_data() */
1137 frame->scanlength = 0;
1139 /* set v4l2_format index */
1140 frame->v4l2_format = usbvision->palette;
1142 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1143 list_add_tail(&frame->frame, &usbvision->inqueue);
1144 spin_unlock_irqrestore(&usbvision->queue_lock,
1145 lock_flags);
1149 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1150 if (list_empty(&(usbvision->outqueue))) {
1151 if(noblock)
1152 return -EAGAIN;
1154 ret = wait_event_interruptible
1155 (usbvision->wait_frame,
1156 !list_empty(&(usbvision->outqueue)));
1157 if (ret)
1158 return ret;
1161 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1162 frame = list_entry(usbvision->outqueue.next,
1163 struct usbvision_frame, frame);
1164 list_del(usbvision->outqueue.next);
1165 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1167 /* An error returns an empty frame */
1168 if (frame->grabstate == FrameState_Error) {
1169 frame->bytes_read = 0;
1170 return 0;
1173 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1174 __FUNCTION__,
1175 frame->index, frame->bytes_read, frame->scanlength);
1177 /* copy bytes to user space; we allow for partials reads */
1178 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1179 count = frame->scanlength - frame->bytes_read;
1181 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1182 return -EFAULT;
1185 frame->bytes_read += count;
1186 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1187 __FUNCTION__,
1188 (unsigned long)count, frame->bytes_read);
1190 /* For now, forget the frame if it has not been read in one shot. */
1191 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1192 frame->bytes_read = 0;
1194 /* Mark it as available to be used again. */
1195 frame->grabstate = FrameState_Unused;
1196 /* } */
1198 return count;
1201 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1203 unsigned long size = vma->vm_end - vma->vm_start,
1204 start = vma->vm_start;
1205 void *pos;
1206 u32 i;
1208 struct video_device *dev = video_devdata(file);
1209 struct usb_usbvision *usbvision =
1210 (struct usb_usbvision *) video_get_drvdata(dev);
1212 PDEBUG(DBG_MMAP, "mmap");
1214 mutex_lock(&usbvision->lock);
1216 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1217 mutex_unlock(&usbvision->lock);
1218 return -EFAULT;
1221 if (!(vma->vm_flags & VM_WRITE) ||
1222 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1223 mutex_unlock(&usbvision->lock);
1224 return -EINVAL;
1227 for (i = 0; i < usbvision->num_frames; i++) {
1228 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1229 vma->vm_pgoff)
1230 break;
1232 if (i == usbvision->num_frames) {
1233 PDEBUG(DBG_MMAP,
1234 "mmap: user supplied mapping address is out of range");
1235 mutex_unlock(&usbvision->lock);
1236 return -EINVAL;
1239 /* VM_IO is eventually going to replace PageReserved altogether */
1240 vma->vm_flags |= VM_IO;
1241 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1243 pos = usbvision->frame[i].data;
1244 while (size > 0) {
1246 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1247 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1248 mutex_unlock(&usbvision->lock);
1249 return -EAGAIN;
1251 start += PAGE_SIZE;
1252 pos += PAGE_SIZE;
1253 size -= PAGE_SIZE;
1256 mutex_unlock(&usbvision->lock);
1257 return 0;
1262 * Here comes the stuff for radio on usbvision based devices
1265 static int usbvision_radio_open(struct inode *inode, struct file *file)
1267 struct video_device *dev = video_devdata(file);
1268 struct usb_usbvision *usbvision =
1269 (struct usb_usbvision *) video_get_drvdata(dev);
1270 int errCode = 0;
1272 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1274 mutex_lock(&usbvision->lock);
1276 if (usbvision->user) {
1277 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1278 errCode = -EBUSY;
1280 else {
1281 if(PowerOnAtOpen) {
1282 usbvision_reset_powerOffTimer(usbvision);
1283 if (usbvision->power == 0) {
1284 usbvision_power_on(usbvision);
1285 usbvision_i2c_register(usbvision);
1289 /* Alternate interface 1 is is the biggest frame size */
1290 errCode = usbvision_set_alternate(usbvision);
1291 if (errCode < 0) {
1292 usbvision->last_error = errCode;
1293 errCode = -EBUSY;
1294 goto out;
1297 // If so far no errors then we shall start the radio
1298 usbvision->radio = 1;
1299 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1300 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1301 usbvision->user++;
1304 if (errCode) {
1305 if (PowerOnAtOpen) {
1306 usbvision_i2c_unregister(usbvision);
1307 usbvision_power_off(usbvision);
1308 usbvision->initialized = 0;
1311 out:
1312 mutex_unlock(&usbvision->lock);
1313 return errCode;
1317 static int usbvision_radio_close(struct inode *inode, struct file *file)
1319 struct video_device *dev = video_devdata(file);
1320 struct usb_usbvision *usbvision =
1321 (struct usb_usbvision *) video_get_drvdata(dev);
1322 int errCode = 0;
1324 PDEBUG(DBG_IO, "");
1326 mutex_lock(&usbvision->lock);
1328 /* Set packet size to 0 */
1329 usbvision->ifaceAlt=0;
1330 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1331 usbvision->ifaceAlt);
1333 usbvision_audio_off(usbvision);
1334 usbvision->radio=0;
1335 usbvision->user--;
1337 if (PowerOnAtOpen) {
1338 usbvision_set_powerOffTimer(usbvision);
1339 usbvision->initialized = 0;
1342 mutex_unlock(&usbvision->lock);
1344 if (usbvision->remove_pending) {
1345 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
1346 usbvision_release(usbvision);
1350 PDEBUG(DBG_IO, "success");
1352 return errCode;
1356 * Here comes the stuff for vbi on usbvision based devices
1359 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1361 /* TODO */
1362 return -ENODEV;
1366 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1368 /* TODO */
1369 return -ENODEV;
1372 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1373 unsigned int cmd, void *arg)
1375 /* TODO */
1376 return -ENOIOCTLCMD;
1379 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1380 unsigned int cmd, unsigned long arg)
1382 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1387 // Video registration stuff
1390 // Video template
1391 static const struct file_operations usbvision_fops = {
1392 .owner = THIS_MODULE,
1393 .open = usbvision_v4l2_open,
1394 .release = usbvision_v4l2_close,
1395 .read = usbvision_v4l2_read,
1396 .mmap = usbvision_v4l2_mmap,
1397 .ioctl = video_ioctl2,
1398 .llseek = no_llseek,
1399 /* .poll = video_poll, */
1400 .compat_ioctl = v4l_compat_ioctl32,
1402 static struct video_device usbvision_video_template = {
1403 .owner = THIS_MODULE,
1404 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1405 .fops = &usbvision_fops,
1406 .name = "usbvision-video",
1407 .release = video_device_release,
1408 .minor = -1,
1409 .vidioc_querycap = vidioc_querycap,
1410 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1411 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1412 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1413 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1414 .vidioc_reqbufs = vidioc_reqbufs,
1415 .vidioc_querybuf = vidioc_querybuf,
1416 .vidioc_qbuf = vidioc_qbuf,
1417 .vidioc_dqbuf = vidioc_dqbuf,
1418 .vidioc_s_std = vidioc_s_std,
1419 .vidioc_enum_input = vidioc_enum_input,
1420 .vidioc_g_input = vidioc_g_input,
1421 .vidioc_s_input = vidioc_s_input,
1422 .vidioc_queryctrl = vidioc_queryctrl,
1423 .vidioc_g_audio = vidioc_g_audio,
1424 .vidioc_s_audio = vidioc_s_audio,
1425 .vidioc_g_ctrl = vidioc_g_ctrl,
1426 .vidioc_s_ctrl = vidioc_s_ctrl,
1427 .vidioc_streamon = vidioc_streamon,
1428 .vidioc_streamoff = vidioc_streamoff,
1429 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1430 /* .vidiocgmbuf = vidiocgmbuf, */
1431 #endif
1432 .vidioc_g_tuner = vidioc_g_tuner,
1433 .vidioc_s_tuner = vidioc_s_tuner,
1434 .vidioc_g_frequency = vidioc_g_frequency,
1435 .vidioc_s_frequency = vidioc_s_frequency,
1436 #ifdef CONFIG_VIDEO_ADV_DEBUG
1437 .vidioc_g_register = vidioc_g_register,
1438 .vidioc_s_register = vidioc_s_register,
1439 #endif
1440 .tvnorms = USBVISION_NORMS,
1441 .current_norm = V4L2_STD_PAL
1445 // Radio template
1446 static const struct file_operations usbvision_radio_fops = {
1447 .owner = THIS_MODULE,
1448 .open = usbvision_radio_open,
1449 .release = usbvision_radio_close,
1450 .ioctl = video_ioctl2,
1451 .llseek = no_llseek,
1452 .compat_ioctl = v4l_compat_ioctl32,
1455 static struct video_device usbvision_radio_template=
1457 .owner = THIS_MODULE,
1458 .type = VID_TYPE_TUNER,
1459 .fops = &usbvision_radio_fops,
1460 .name = "usbvision-radio",
1461 .release = video_device_release,
1462 .minor = -1,
1463 .vidioc_querycap = vidioc_querycap,
1464 .vidioc_enum_input = vidioc_enum_input,
1465 .vidioc_g_input = vidioc_g_input,
1466 .vidioc_s_input = vidioc_s_input,
1467 .vidioc_queryctrl = vidioc_queryctrl,
1468 .vidioc_g_audio = vidioc_g_audio,
1469 .vidioc_s_audio = vidioc_s_audio,
1470 .vidioc_g_ctrl = vidioc_g_ctrl,
1471 .vidioc_s_ctrl = vidioc_s_ctrl,
1472 .vidioc_g_tuner = vidioc_g_tuner,
1473 .vidioc_s_tuner = vidioc_s_tuner,
1474 .vidioc_g_frequency = vidioc_g_frequency,
1475 .vidioc_s_frequency = vidioc_s_frequency,
1477 .tvnorms = USBVISION_NORMS,
1478 .current_norm = V4L2_STD_PAL
1481 // vbi template
1482 static const struct file_operations usbvision_vbi_fops = {
1483 .owner = THIS_MODULE,
1484 .open = usbvision_vbi_open,
1485 .release = usbvision_vbi_close,
1486 .ioctl = usbvision_vbi_ioctl,
1487 .llseek = no_llseek,
1488 .compat_ioctl = v4l_compat_ioctl32,
1491 static struct video_device usbvision_vbi_template=
1493 .owner = THIS_MODULE,
1494 .type = VID_TYPE_TUNER,
1495 .fops = &usbvision_vbi_fops,
1496 .release = video_device_release,
1497 .name = "usbvision-vbi",
1498 .minor = -1,
1502 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1503 struct video_device *vdev_template,
1504 char *name)
1506 struct usb_device *usb_dev = usbvision->dev;
1507 struct video_device *vdev;
1509 if (usb_dev == NULL) {
1510 err("%s: usbvision->dev is not set", __FUNCTION__);
1511 return NULL;
1514 vdev = video_device_alloc();
1515 if (NULL == vdev) {
1516 return NULL;
1518 *vdev = *vdev_template;
1519 // vdev->minor = -1;
1520 vdev->dev = &usb_dev->dev;
1521 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1522 video_set_drvdata(vdev, usbvision);
1523 return vdev;
1526 // unregister video4linux devices
1527 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1529 // vbi Device:
1530 if (usbvision->vbi) {
1531 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]",
1532 usbvision->vbi->minor & 0x1f);
1533 if (usbvision->vbi->minor != -1) {
1534 video_unregister_device(usbvision->vbi);
1535 } else {
1536 video_device_release(usbvision->vbi);
1538 usbvision->vbi = NULL;
1541 // Radio Device:
1542 if (usbvision->rdev) {
1543 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]",
1544 usbvision->rdev->minor & 0x1f);
1545 if (usbvision->rdev->minor != -1) {
1546 video_unregister_device(usbvision->rdev);
1547 } else {
1548 video_device_release(usbvision->rdev);
1550 usbvision->rdev = NULL;
1553 // Video Device:
1554 if (usbvision->vdev) {
1555 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]",
1556 usbvision->vdev->minor & 0x1f);
1557 if (usbvision->vdev->minor != -1) {
1558 video_unregister_device(usbvision->vdev);
1559 } else {
1560 video_device_release(usbvision->vdev);
1562 usbvision->vdev = NULL;
1566 // register video4linux devices
1567 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1569 // Video Device:
1570 usbvision->vdev = usbvision_vdev_init(usbvision,
1571 &usbvision_video_template,
1572 "USBVision Video");
1573 if (usbvision->vdev == NULL) {
1574 goto err_exit;
1576 if (video_register_device(usbvision->vdev,
1577 VFL_TYPE_GRABBER,
1578 video_nr)<0) {
1579 goto err_exit;
1581 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n",
1582 usbvision->nr,usbvision->vdev->minor & 0x1f);
1584 // Radio Device:
1585 if (usbvision_device_data[usbvision->DevModel].Radio) {
1586 // usbvision has radio
1587 usbvision->rdev = usbvision_vdev_init(usbvision,
1588 &usbvision_radio_template,
1589 "USBVision Radio");
1590 if (usbvision->rdev == NULL) {
1591 goto err_exit;
1593 if (video_register_device(usbvision->rdev,
1594 VFL_TYPE_RADIO,
1595 radio_nr)<0) {
1596 goto err_exit;
1598 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n",
1599 usbvision->nr, usbvision->rdev->minor & 0x1f);
1601 // vbi Device:
1602 if (usbvision_device_data[usbvision->DevModel].vbi) {
1603 usbvision->vbi = usbvision_vdev_init(usbvision,
1604 &usbvision_vbi_template,
1605 "USBVision VBI");
1606 if (usbvision->vdev == NULL) {
1607 goto err_exit;
1609 if (video_register_device(usbvision->vbi,
1610 VFL_TYPE_VBI,
1611 vbi_nr)<0) {
1612 goto err_exit;
1614 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n",
1615 usbvision->nr,usbvision->vbi->minor & 0x1f);
1617 // all done
1618 return 0;
1620 err_exit:
1621 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1622 usbvision_unregister_video(usbvision);
1623 return -1;
1627 * usbvision_alloc()
1629 * This code allocates the struct usb_usbvision.
1630 * It is filled with default values.
1632 * Returns NULL on error, a pointer to usb_usbvision else.
1635 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1637 struct usb_usbvision *usbvision;
1639 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) ==
1640 NULL) {
1641 goto err_exit;
1644 usbvision->dev = dev;
1646 mutex_init(&usbvision->lock); /* available */
1648 // prepare control urb for control messages during interrupts
1649 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1650 if (usbvision->ctrlUrb == NULL) {
1651 goto err_exit;
1653 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1655 usbvision_init_powerOffTimer(usbvision);
1657 return usbvision;
1659 err_exit:
1660 if (usbvision && usbvision->ctrlUrb) {
1661 usb_free_urb(usbvision->ctrlUrb);
1663 if (usbvision) {
1664 kfree(usbvision);
1666 return NULL;
1670 * usbvision_release()
1672 * This code does final release of struct usb_usbvision. This happens
1673 * after the device is disconnected -and- all clients closed their files.
1676 static void usbvision_release(struct usb_usbvision *usbvision)
1678 PDEBUG(DBG_PROBE, "");
1680 mutex_lock(&usbvision->lock);
1682 usbvision_reset_powerOffTimer(usbvision);
1684 usbvision->initialized = 0;
1686 mutex_unlock(&usbvision->lock);
1688 usbvision_remove_sysfs(usbvision->vdev);
1689 usbvision_unregister_video(usbvision);
1691 if (usbvision->ctrlUrb) {
1692 usb_free_urb(usbvision->ctrlUrb);
1695 kfree(usbvision);
1697 PDEBUG(DBG_PROBE, "success");
1701 /*********************** usb interface **********************************/
1703 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1705 int model;
1707 if (usbvision == NULL)
1708 return;
1710 model = usbvision->DevModel;
1711 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1713 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
1714 usbvision->Vin_Reg2_Preset =
1715 usbvision_device_data[usbvision->DevModel].Vin_Reg2;
1716 } else {
1717 usbvision->Vin_Reg2_Preset = 0;
1720 usbvision->tvnormId = usbvision_device_data[model].VideoNorm;
1722 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1723 usbvision->ctl_input = 0;
1725 /* This should be here to make i2c clients to be able to register */
1726 /* first switch off audio */
1727 usbvision_audio_off(usbvision);
1728 if (!PowerOnAtOpen) {
1729 /* and then power up the noisy tuner */
1730 usbvision_power_on(usbvision);
1731 usbvision_i2c_register(usbvision);
1736 * usbvision_probe()
1738 * This procedure queries device descriptor and accepts the interface
1739 * if it looks like USBVISION video device
1742 static int __devinit usbvision_probe(struct usb_interface *intf,
1743 const struct usb_device_id *devid)
1745 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1746 struct usb_interface *uif;
1747 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1748 const struct usb_host_interface *interface;
1749 struct usb_usbvision *usbvision = NULL;
1750 const struct usb_endpoint_descriptor *endpoint;
1751 int model,i;
1753 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1754 dev->descriptor.idVendor,
1755 dev->descriptor.idProduct, ifnum);
1757 model = devid->driver_info;
1758 if ( (model<0) || (model>=usbvision_device_data_size) ) {
1759 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
1760 return -ENODEV;
1762 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1763 usbvision_device_data[model].ModelString);
1765 if (usbvision_device_data[model].Interface >= 0) {
1766 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1767 } else {
1768 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1770 endpoint = &interface->endpoint[1].desc;
1771 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1772 USB_ENDPOINT_XFER_ISOC) {
1773 err("%s: interface %d. has non-ISO endpoint!",
1774 __FUNCTION__, ifnum);
1775 err("%s: Endpoint attributes %d",
1776 __FUNCTION__, endpoint->bmAttributes);
1777 return -ENODEV;
1779 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ==
1780 USB_DIR_OUT) {
1781 err("%s: interface %d. has ISO OUT endpoint!",
1782 __FUNCTION__, ifnum);
1783 return -ENODEV;
1786 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1787 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1788 return -ENOMEM;
1791 if (dev->descriptor.bNumConfigurations > 1) {
1792 usbvision->bridgeType = BRIDGE_NT1004;
1793 } else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
1794 usbvision->bridgeType = BRIDGE_NT1005;
1795 } else {
1796 usbvision->bridgeType = BRIDGE_NT1003;
1798 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1800 mutex_lock(&usbvision->lock);
1802 /* compute alternate max packet sizes */
1803 uif = dev->actconfig->interface[0];
1805 usbvision->num_alt=uif->num_altsetting;
1806 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1807 usbvision->alt_max_pkt_size = kmalloc(32*
1808 usbvision->num_alt,GFP_KERNEL);
1809 if (usbvision->alt_max_pkt_size == NULL) {
1810 err("usbvision: out of memory!\n");
1811 mutex_unlock(&usbvision->lock);
1812 return -ENOMEM;
1815 for (i = 0; i < usbvision->num_alt ; i++) {
1816 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1817 wMaxPacketSize);
1818 usbvision->alt_max_pkt_size[i] =
1819 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1820 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1821 usbvision->alt_max_pkt_size[i]);
1825 usbvision->nr = usbvision_nr++;
1827 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1828 if (usbvision->have_tuner) {
1829 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1832 usbvision->tuner_addr = ADDR_UNSET;
1834 usbvision->DevModel = model;
1835 usbvision->remove_pending = 0;
1836 usbvision->iface = ifnum;
1837 usbvision->ifaceAlt = 0;
1838 usbvision->video_endp = endpoint->bEndpointAddress;
1839 usbvision->isocPacketSize = 0;
1840 usbvision->usb_bandwidth = 0;
1841 usbvision->user = 0;
1842 usbvision->streaming = Stream_Off;
1843 usbvision_register_video(usbvision);
1844 usbvision_configure_video(usbvision);
1845 mutex_unlock(&usbvision->lock);
1848 usb_set_intfdata (intf, usbvision);
1849 usbvision_create_sysfs(usbvision->vdev);
1851 PDEBUG(DBG_PROBE, "success");
1852 return 0;
1857 * usbvision_disconnect()
1859 * This procedure stops all driver activity, deallocates interface-private
1860 * structure (pointed by 'ptr') and after that driver should be removable
1861 * with no ill consequences.
1864 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1866 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1868 PDEBUG(DBG_PROBE, "");
1870 if (usbvision == NULL) {
1871 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1872 return;
1874 usb_set_intfdata (intf, NULL);
1876 mutex_lock(&usbvision->lock);
1878 // At this time we ask to cancel outstanding URBs
1879 usbvision_stop_isoc(usbvision);
1881 if (usbvision->power) {
1882 usbvision_i2c_unregister(usbvision);
1883 usbvision_power_off(usbvision);
1885 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1887 usb_put_dev(usbvision->dev);
1888 usbvision->dev = NULL; // USB device is no more
1890 mutex_unlock(&usbvision->lock);
1892 if (usbvision->user) {
1893 printk(KERN_INFO "%s: In use, disconnect pending\n",
1894 __FUNCTION__);
1895 wake_up_interruptible(&usbvision->wait_frame);
1896 wake_up_interruptible(&usbvision->wait_stream);
1897 } else {
1898 usbvision_release(usbvision);
1901 PDEBUG(DBG_PROBE, "success");
1905 static struct usb_driver usbvision_driver = {
1906 .name = "usbvision",
1907 .id_table = usbvision_table,
1908 .probe = usbvision_probe,
1909 .disconnect = usbvision_disconnect
1913 * usbvision_init()
1915 * This code is run to initialize the driver.
1918 static int __init usbvision_init(void)
1920 int errCode;
1922 PDEBUG(DBG_PROBE, "");
1924 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1925 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1926 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1928 /* disable planar mode support unless compression enabled */
1929 if (isocMode != ISOC_MODE_COMPRESS ) {
1930 // FIXME : not the right way to set supported flag
1931 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1932 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1935 errCode = usb_register(&usbvision_driver);
1937 if (errCode == 0) {
1938 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1939 PDEBUG(DBG_PROBE, "success");
1941 return errCode;
1944 static void __exit usbvision_exit(void)
1946 PDEBUG(DBG_PROBE, "");
1948 usb_deregister(&usbvision_driver);
1949 PDEBUG(DBG_PROBE, "success");
1952 module_init(usbvision_init);
1953 module_exit(usbvision_exit);
1956 * Overrides for Emacs so that we follow Linus's tabbing style.
1957 * ---------------------------------------------------------------------------
1958 * Local variables:
1959 * c-basic-offset: 8
1960 * End: