mm/slab.c: proper prototypes
[wrt350n-kernel.git] / drivers / media / video / usbvision / usbvision-video.c
blob216704170a4c2075f7c53922644d57e62774007b
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/smp_lock.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/moduleparam.h>
72 #include <linux/workqueue.h>
74 #ifdef CONFIG_KMOD
75 #include <linux/kmod.h>
76 #endif
78 #include "usbvision.h"
79 #include "usbvision-cards.h"
81 #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
82 #define DRIVER_NAME "usbvision"
83 #define DRIVER_ALIAS "USBVision"
84 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
85 #define DRIVER_LICENSE "GPL"
86 #define USBVISION_DRIVER_VERSION_MAJOR 0
87 #define USBVISION_DRIVER_VERSION_MINOR 9
88 #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
89 #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
90 #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
92 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
95 #ifdef USBVISION_DEBUG
96 #define PDEBUG(level, fmt, args...) \
97 if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
98 #else
99 #define PDEBUG(level, fmt, args...) do {} while(0)
100 #endif
102 #define DBG_IOCTL 1<<0
103 #define DBG_IO 1<<1
104 #define DBG_PROBE 1<<2
105 #define DBG_MMAP 1<<3
107 //String operations
108 #define rmspace(str) while(*str==' ') str++;
109 #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
112 static int usbvision_nr = 0; // sequential number of usbvision device
114 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
115 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
116 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
117 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
118 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
119 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
120 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
121 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
122 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
125 /* supported tv norms */
126 static struct usbvision_tvnorm tvnorms[] = {
128 .name = "PAL",
129 .id = V4L2_STD_PAL,
130 }, {
131 .name = "NTSC",
132 .id = V4L2_STD_NTSC,
133 }, {
134 .name = "SECAM",
135 .id = V4L2_STD_SECAM,
136 }, {
137 .name = "PAL-M",
138 .id = V4L2_STD_PAL_M,
142 #define TVNORMS ARRAY_SIZE(tvnorms)
144 // Function prototypes
145 static void usbvision_release(struct usb_usbvision *usbvision);
147 // Default initalization of device driver parameters
148 static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
149 static int video_debug = 0; // Set the default Debug Mode of the device driver
150 static int PowerOnAtOpen = 1; // Set the default device to power on at startup
151 static int video_nr = -1; // Sequential Number of Video Device
152 static int radio_nr = -1; // Sequential Number of Radio Device
153 static int vbi_nr = -1; // Sequential Number of VBI Device
155 // Grab parameters for the device driver
157 #if defined(module_param) // Showing parameters under SYSFS
158 module_param(isocMode, int, 0444);
159 module_param(video_debug, int, 0444);
160 module_param(PowerOnAtOpen, int, 0444);
161 module_param(video_nr, int, 0444);
162 module_param(radio_nr, int, 0444);
163 module_param(vbi_nr, int, 0444);
164 #else // Old Style
165 MODULE_PARAM(isocMode, "i");
166 MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
167 MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
168 MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
169 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.
170 MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
171 MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
172 MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
173 #endif
175 MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
176 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
177 MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
178 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
179 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
180 MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
183 // Misc stuff
184 MODULE_AUTHOR(DRIVER_AUTHOR);
185 MODULE_DESCRIPTION(DRIVER_DESC);
186 MODULE_LICENSE(DRIVER_LICENSE);
187 MODULE_VERSION(USBVISION_VERSION_STRING);
188 MODULE_ALIAS(DRIVER_ALIAS);
191 /****************************************************************************************/
192 /* SYSFS Code - Copied from the stv680.c usb module. */
193 /* Device information is located at /sys/class/video4linux/video0 */
194 /* Device parameters information is located at /sys/module/usbvision */
195 /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
196 /****************************************************************************************/
199 #define YES_NO(x) ((x) ? "Yes" : "No")
201 static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
203 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
204 return video_get_drvdata(vdev);
207 static ssize_t show_version(struct class_device *cd, char *buf)
209 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
211 static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
213 static ssize_t show_model(struct class_device *cd, char *buf)
215 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
216 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
217 return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
219 static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
221 static ssize_t show_hue(struct class_device *cd, char *buf)
223 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
224 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
225 struct v4l2_control ctrl;
226 ctrl.id = V4L2_CID_HUE;
227 ctrl.value = 0;
228 if(usbvision->user)
229 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
230 return sprintf(buf, "%d\n", ctrl.value);
232 static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
234 static ssize_t show_contrast(struct class_device *cd, char *buf)
236 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
237 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
238 struct v4l2_control ctrl;
239 ctrl.id = V4L2_CID_CONTRAST;
240 ctrl.value = 0;
241 if(usbvision->user)
242 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
243 return sprintf(buf, "%d\n", ctrl.value);
245 static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
247 static ssize_t show_brightness(struct class_device *cd, char *buf)
249 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
250 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
251 struct v4l2_control ctrl;
252 ctrl.id = V4L2_CID_BRIGHTNESS;
253 ctrl.value = 0;
254 if(usbvision->user)
255 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
256 return sprintf(buf, "%d\n", ctrl.value);
258 static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
260 static ssize_t show_saturation(struct class_device *cd, char *buf)
262 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
263 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
264 struct v4l2_control ctrl;
265 ctrl.id = V4L2_CID_SATURATION;
266 ctrl.value = 0;
267 if(usbvision->user)
268 call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
269 return sprintf(buf, "%d\n", ctrl.value);
271 static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
273 static ssize_t show_streaming(struct class_device *cd, char *buf)
275 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
276 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
277 return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
279 static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
281 static ssize_t show_compression(struct class_device *cd, char *buf)
283 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
284 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
285 return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
287 static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
289 static ssize_t show_device_bridge(struct class_device *cd, char *buf)
291 struct video_device *vdev = container_of(cd, struct video_device, class_dev);
292 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
293 return sprintf(buf, "%d\n", usbvision->bridgeType);
295 static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
297 static void usbvision_create_sysfs(struct video_device *vdev)
299 int res;
300 if (!vdev)
301 return;
302 do {
303 res=class_device_create_file(&vdev->class_dev,
304 &class_device_attr_version);
305 if (res<0)
306 break;
307 res=class_device_create_file(&vdev->class_dev,
308 &class_device_attr_model);
309 if (res<0)
310 break;
311 res=class_device_create_file(&vdev->class_dev,
312 &class_device_attr_hue);
313 if (res<0)
314 break;
315 res=class_device_create_file(&vdev->class_dev,
316 &class_device_attr_contrast);
317 if (res<0)
318 break;
319 res=class_device_create_file(&vdev->class_dev,
320 &class_device_attr_brightness);
321 if (res<0)
322 break;
323 res=class_device_create_file(&vdev->class_dev,
324 &class_device_attr_saturation);
325 if (res<0)
326 break;
327 res=class_device_create_file(&vdev->class_dev,
328 &class_device_attr_streaming);
329 if (res<0)
330 break;
331 res=class_device_create_file(&vdev->class_dev,
332 &class_device_attr_compression);
333 if (res<0)
334 break;
335 res=class_device_create_file(&vdev->class_dev,
336 &class_device_attr_bridge);
337 if (res>=0)
338 return;
339 } while (0);
341 err("%s error: %d\n", __FUNCTION__, res);
344 static void usbvision_remove_sysfs(struct video_device *vdev)
346 if (vdev) {
347 class_device_remove_file(&vdev->class_dev,
348 &class_device_attr_version);
349 class_device_remove_file(&vdev->class_dev,
350 &class_device_attr_model);
351 class_device_remove_file(&vdev->class_dev,
352 &class_device_attr_hue);
353 class_device_remove_file(&vdev->class_dev,
354 &class_device_attr_contrast);
355 class_device_remove_file(&vdev->class_dev,
356 &class_device_attr_brightness);
357 class_device_remove_file(&vdev->class_dev,
358 &class_device_attr_saturation);
359 class_device_remove_file(&vdev->class_dev,
360 &class_device_attr_streaming);
361 class_device_remove_file(&vdev->class_dev,
362 &class_device_attr_compression);
363 class_device_remove_file(&vdev->class_dev,
364 &class_device_attr_bridge);
370 * usbvision_open()
372 * This is part of Video 4 Linux API. The driver can be opened by one
373 * client only (checks internal counter 'usbvision->user'). The procedure
374 * then allocates buffers needed for video processing.
377 static int usbvision_v4l2_open(struct inode *inode, struct file *file)
379 struct video_device *dev = video_devdata(file);
380 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
381 int errCode = 0;
383 PDEBUG(DBG_IO, "open");
386 usbvision_reset_powerOffTimer(usbvision);
388 if (usbvision->user)
389 errCode = -EBUSY;
390 else {
391 /* Allocate memory for the scratch ring buffer */
392 errCode = usbvision_scratch_alloc(usbvision);
393 if (isocMode==ISOC_MODE_COMPRESS) {
394 /* Allocate intermediate decompression buffers only if needed */
395 errCode = usbvision_decompress_alloc(usbvision);
397 if (errCode) {
398 /* Deallocate all buffers if trouble */
399 usbvision_scratch_free(usbvision);
400 usbvision_decompress_free(usbvision);
404 /* If so far no errors then we shall start the camera */
405 if (!errCode) {
406 down(&usbvision->lock);
407 if (usbvision->power == 0) {
408 usbvision_power_on(usbvision);
409 usbvision_i2c_register(usbvision);
412 /* Send init sequence only once, it's large! */
413 if (!usbvision->initialized) {
414 int setup_ok = 0;
415 setup_ok = usbvision_setup(usbvision,isocMode);
416 if (setup_ok)
417 usbvision->initialized = 1;
418 else
419 errCode = -EBUSY;
422 if (!errCode) {
423 usbvision_begin_streaming(usbvision);
424 errCode = usbvision_init_isoc(usbvision);
425 /* device needs to be initialized before isoc transfer */
426 usbvision_muxsel(usbvision,0);
427 usbvision->user++;
429 else {
430 if (PowerOnAtOpen) {
431 usbvision_i2c_unregister(usbvision);
432 usbvision_power_off(usbvision);
433 usbvision->initialized = 0;
436 up(&usbvision->lock);
439 if (errCode) {
442 /* prepare queues */
443 usbvision_empty_framequeues(usbvision);
445 PDEBUG(DBG_IO, "success");
446 return errCode;
450 * usbvision_v4l2_close()
452 * This is part of Video 4 Linux API. The procedure
453 * stops streaming and deallocates all buffers that were earlier
454 * allocated in usbvision_v4l2_open().
457 static int usbvision_v4l2_close(struct inode *inode, struct file *file)
459 struct video_device *dev = video_devdata(file);
460 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
462 PDEBUG(DBG_IO, "close");
463 down(&usbvision->lock);
465 usbvision_audio_off(usbvision);
466 usbvision_restart_isoc(usbvision);
467 usbvision_stop_isoc(usbvision);
469 usbvision_decompress_free(usbvision);
470 usbvision_frames_free(usbvision);
471 usbvision_empty_framequeues(usbvision);
472 usbvision_scratch_free(usbvision);
474 usbvision->user--;
476 if (PowerOnAtOpen) {
477 /* power off in a little while to avoid off/on every close/open short sequences */
478 usbvision_set_powerOffTimer(usbvision);
479 usbvision->initialized = 0;
482 up(&usbvision->lock);
484 if (usbvision->remove_pending) {
485 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
486 usbvision_release(usbvision);
489 PDEBUG(DBG_IO, "success");
492 return 0;
497 * usbvision_ioctl()
499 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
502 static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
503 unsigned int cmd, void *arg)
505 struct video_device *dev = video_devdata(file);
506 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
508 if (!USBVISION_IS_OPERATIONAL(usbvision))
509 return -EFAULT;
511 switch (cmd) {
513 #ifdef CONFIG_VIDEO_ADV_DEBUG
514 /* ioctls to allow direct acces to the NT100x registers */
515 case VIDIOC_DBG_G_REGISTER:
516 case VIDIOC_DBG_S_REGISTER:
518 struct v4l2_register *reg = arg;
519 int errCode;
521 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
522 return -EINVAL;
523 if (!capable(CAP_SYS_ADMIN))
524 return -EPERM;
525 /* NT100x has a 8-bit register space */
526 if (cmd == VIDIOC_DBG_G_REGISTER)
527 errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
528 else
529 errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
530 if (errCode < 0) {
531 err("%s: VIDIOC_DBG_%c_REGISTER failed: error %d", __FUNCTION__,
532 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S', errCode);
533 return errCode;
535 if (cmd == VIDIOC_DBG_S_REGISTER)
536 reg->val = (u8)errCode;
538 PDEBUG(DBG_IOCTL, "VIDIOC_DBG_%c_REGISTER reg=0x%02X, value=0x%02X",
539 cmd == VIDIOC_DBG_G_REGISTER ? 'G' : 'S',
540 (unsigned int)reg->reg, (unsigned int)reg->val);
541 return 0;
543 #endif
544 case VIDIOC_QUERYCAP:
546 struct v4l2_capability *vc=arg;
548 memset(vc, 0, sizeof(*vc));
549 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
550 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
551 sizeof(vc->card));
552 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
553 sizeof(vc->bus_info));
554 vc->version = USBVISION_DRIVER_VERSION;
555 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
556 V4L2_CAP_AUDIO |
557 V4L2_CAP_READWRITE |
558 V4L2_CAP_STREAMING |
559 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
560 PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
561 return 0;
563 case VIDIOC_ENUMINPUT:
565 struct v4l2_input *vi = arg;
566 int chan;
568 if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
569 return -EINVAL;
570 if (usbvision->have_tuner) {
571 chan = vi->index;
573 else {
574 chan = vi->index + 1; //skip Television string
576 switch(chan) {
577 case 0:
578 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
579 strcpy(vi->name, "White Video Input");
581 else {
582 strcpy(vi->name, "Television");
583 vi->type = V4L2_INPUT_TYPE_TUNER;
584 vi->audioset = 1;
585 vi->tuner = chan;
586 vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
588 break;
589 case 1:
590 vi->type = V4L2_INPUT_TYPE_CAMERA;
591 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
592 strcpy(vi->name, "Green Video Input");
594 else {
595 strcpy(vi->name, "Composite Video Input");
597 vi->std = V4L2_STD_PAL;
598 break;
599 case 2:
600 vi->type = V4L2_INPUT_TYPE_CAMERA;
601 if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
602 strcpy(vi->name, "Yellow Video Input");
604 else {
605 strcpy(vi->name, "S-Video Input");
607 vi->std = V4L2_STD_PAL;
608 break;
609 case 3:
610 vi->type = V4L2_INPUT_TYPE_CAMERA;
611 strcpy(vi->name, "Red Video Input");
612 vi->std = V4L2_STD_PAL;
613 break;
615 PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
616 vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
617 return 0;
619 case VIDIOC_ENUMSTD:
621 struct v4l2_standard *e = arg;
622 unsigned int i;
623 int ret;
625 i = e->index;
626 if (i >= TVNORMS)
627 return -EINVAL;
628 ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
629 tvnorms[e->index].name);
630 e->index = i;
631 if (ret < 0)
632 return ret;
633 return 0;
635 case VIDIOC_G_INPUT:
637 int *input = arg;
638 *input = usbvision->ctl_input;
639 return 0;
641 case VIDIOC_S_INPUT:
643 int *input = arg;
644 if ((*input >= usbvision->video_inputs) || (*input < 0) )
645 return -EINVAL;
646 usbvision->ctl_input = *input;
648 down(&usbvision->lock);
649 usbvision_muxsel(usbvision, usbvision->ctl_input);
650 usbvision_set_input(usbvision);
651 usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
652 up(&usbvision->lock);
653 return 0;
655 case VIDIOC_G_STD:
657 v4l2_std_id *id = arg;
659 *id = usbvision->tvnorm->id;
661 PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
662 return 0;
664 case VIDIOC_S_STD:
666 v4l2_std_id *id = arg;
667 unsigned int i;
669 for (i = 0; i < TVNORMS; i++)
670 if (*id == tvnorms[i].id)
671 break;
672 if (i == TVNORMS)
673 for (i = 0; i < TVNORMS; i++)
674 if (*id & tvnorms[i].id)
675 break;
676 if (i == TVNORMS)
677 return -EINVAL;
679 down(&usbvision->lock);
680 usbvision->tvnorm = &tvnorms[i];
682 call_i2c_clients(usbvision, VIDIOC_S_STD,
683 &usbvision->tvnorm->id);
685 up(&usbvision->lock);
687 PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
688 return 0;
690 case VIDIOC_G_TUNER:
692 struct v4l2_tuner *vt = arg;
694 if (!usbvision->have_tuner || vt->index) // Only tuner 0
695 return -EINVAL;
696 strcpy(vt->name, "Television");
697 /* Let clients fill in the remainder of this struct */
698 call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
700 PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
701 return 0;
703 case VIDIOC_S_TUNER:
705 struct v4l2_tuner *vt = arg;
707 // Only no or one tuner for now
708 if (!usbvision->have_tuner || vt->index)
709 return -EINVAL;
710 /* let clients handle this */
711 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
713 PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
714 return 0;
716 case VIDIOC_G_FREQUENCY:
718 struct v4l2_frequency *freq = arg;
720 freq->tuner = 0; // Only one tuner
721 freq->type = V4L2_TUNER_ANALOG_TV;
722 freq->frequency = usbvision->freq;
723 PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
724 return 0;
726 case VIDIOC_S_FREQUENCY:
728 struct v4l2_frequency *freq = arg;
730 // Only no or one tuner for now
731 if (!usbvision->have_tuner || freq->tuner)
732 return -EINVAL;
734 usbvision->freq = freq->frequency;
735 call_i2c_clients(usbvision, cmd, freq);
736 PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
737 return 0;
739 case VIDIOC_G_AUDIO:
741 struct v4l2_audio *v = arg;
742 memset(v,0, sizeof(v));
743 strcpy(v->name, "TV");
744 PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
745 return 0;
747 case VIDIOC_S_AUDIO:
749 struct v4l2_audio *v = arg;
750 if(v->index) {
751 return -EINVAL;
753 PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
754 return 0;
756 case VIDIOC_QUERYCTRL:
758 struct v4l2_queryctrl *ctrl = arg;
759 int id=ctrl->id;
761 memset(ctrl,0,sizeof(*ctrl));
762 ctrl->id=id;
764 call_i2c_clients(usbvision, cmd, arg);
766 if (ctrl->type)
767 return 0;
768 else
769 return -EINVAL;
771 PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
773 case VIDIOC_G_CTRL:
775 struct v4l2_control *ctrl = arg;
776 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
777 PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
778 return 0;
780 case VIDIOC_S_CTRL:
782 struct v4l2_control *ctrl = arg;
784 PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
785 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
786 return 0;
788 case VIDIOC_REQBUFS:
790 struct v4l2_requestbuffers *vr = arg;
791 int ret;
793 RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
795 // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
796 if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
797 (vr->memory != V4L2_MEMORY_MMAP))
798 return -EINVAL;
800 if(usbvision->streaming == Stream_On) {
801 if ((ret = usbvision_stream_interrupt(usbvision)))
802 return ret;
805 usbvision_frames_free(usbvision);
806 usbvision_empty_framequeues(usbvision);
807 vr->count = usbvision_frames_alloc(usbvision,vr->count);
809 usbvision->curFrame = NULL;
811 PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
812 return 0;
814 case VIDIOC_QUERYBUF:
816 struct v4l2_buffer *vb = arg;
817 struct usbvision_frame *frame;
819 // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
821 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
822 return -EINVAL;
824 if(vb->index>=usbvision->num_frames) {
825 return -EINVAL;
827 // Updating the corresponding frame state
828 vb->flags = 0;
829 frame = &usbvision->frame[vb->index];
830 if(frame->grabstate >= FrameState_Ready)
831 vb->flags |= V4L2_BUF_FLAG_QUEUED;
832 if(frame->grabstate >= FrameState_Done)
833 vb->flags |= V4L2_BUF_FLAG_DONE;
834 if(frame->grabstate == FrameState_Unused)
835 vb->flags |= V4L2_BUF_FLAG_MAPPED;
836 vb->memory = V4L2_MEMORY_MMAP;
838 vb->m.offset = vb->index*PAGE_ALIGN(usbvision->max_frame_size);
840 vb->memory = V4L2_MEMORY_MMAP;
841 vb->field = V4L2_FIELD_NONE;
842 vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
843 vb->timestamp = usbvision->frame[vb->index].timestamp;
844 vb->sequence = usbvision->frame[vb->index].sequence;
845 return 0;
847 case VIDIOC_QBUF:
849 struct v4l2_buffer *vb = arg;
850 struct usbvision_frame *frame;
851 unsigned long lock_flags;
853 // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
854 if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
855 return -EINVAL;
857 if(vb->index>=usbvision->num_frames) {
858 return -EINVAL;
861 frame = &usbvision->frame[vb->index];
863 if (frame->grabstate != FrameState_Unused) {
864 return -EAGAIN;
867 /* Mark it as ready and enqueue frame */
868 frame->grabstate = FrameState_Ready;
869 frame->scanstate = ScanState_Scanning;
870 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
872 vb->flags &= ~V4L2_BUF_FLAG_DONE;
874 /* set v4l2_format index */
875 frame->v4l2_format = usbvision->palette;
877 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
878 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
879 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
881 PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
882 return 0;
884 case VIDIOC_DQBUF:
886 struct v4l2_buffer *vb = arg;
887 int ret;
888 struct usbvision_frame *f;
889 unsigned long lock_flags;
891 if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
892 return -EINVAL;
894 if (list_empty(&(usbvision->outqueue))) {
895 if (usbvision->streaming == Stream_Idle)
896 return -EINVAL;
897 ret = wait_event_interruptible
898 (usbvision->wait_frame,
899 !list_empty(&(usbvision->outqueue)));
900 if (ret)
901 return ret;
904 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
905 f = list_entry(usbvision->outqueue.next,
906 struct usbvision_frame, frame);
907 list_del(usbvision->outqueue.next);
908 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
910 f->grabstate = FrameState_Unused;
912 vb->memory = V4L2_MEMORY_MMAP;
913 vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
914 vb->index = f->index;
915 vb->sequence = f->sequence;
916 vb->timestamp = f->timestamp;
917 vb->field = V4L2_FIELD_NONE;
918 vb->bytesused = f->scanlength;
920 return 0;
922 case VIDIOC_STREAMON:
924 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
926 usbvision->streaming = Stream_On;
928 call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
930 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
932 return 0;
934 case VIDIOC_STREAMOFF:
936 int *type = arg;
937 int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
939 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
940 return -EINVAL;
942 if(usbvision->streaming == Stream_On) {
943 usbvision_stream_interrupt(usbvision);
944 // Stop all video streamings
945 call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
947 usbvision_empty_framequeues(usbvision);
949 PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
950 return 0;
952 case VIDIOC_ENUM_FMT:
954 struct v4l2_fmtdesc *vfd = arg;
956 if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
957 return -EINVAL;
959 vfd->flags = 0;
960 vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
961 strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
962 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
963 memset(vfd->reserved, 0, sizeof(vfd->reserved));
964 return 0;
966 case VIDIOC_G_FMT:
968 struct v4l2_format *vf = arg;
970 switch (vf->type) {
971 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
973 vf->fmt.pix.width = usbvision->curwidth;
974 vf->fmt.pix.height = usbvision->curheight;
975 vf->fmt.pix.pixelformat = usbvision->palette.format;
976 vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
977 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
978 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
979 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
980 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
981 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
982 return 0;
984 default:
985 PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
986 return -EINVAL;
988 return 0;
990 case VIDIOC_TRY_FMT:
991 case VIDIOC_S_FMT:
993 struct v4l2_format *vf = arg;
994 int formatIdx,ret;
996 switch(vf->type) {
997 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
999 /* Find requested format in available ones */
1000 for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
1001 if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
1002 usbvision->palette = usbvision_v4l2_format[formatIdx];
1003 break;
1006 /* robustness */
1007 if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
1008 return -EINVAL;
1010 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
1011 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
1013 vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
1014 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
1016 if(cmd == VIDIOC_TRY_FMT) {
1017 PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
1018 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1019 return 0;
1022 /* stop io in case it is already in progress */
1023 if(usbvision->streaming == Stream_On) {
1024 if ((ret = usbvision_stream_interrupt(usbvision)))
1025 return ret;
1027 usbvision_frames_free(usbvision);
1028 usbvision_empty_framequeues(usbvision);
1030 usbvision->curFrame = NULL;
1032 // by now we are committed to the new data...
1033 down(&usbvision->lock);
1034 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
1035 up(&usbvision->lock);
1037 PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
1038 vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
1039 return 0;
1041 default:
1042 return -EINVAL;
1045 default:
1046 return -ENOIOCTLCMD;
1048 return 0;
1051 static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
1052 unsigned int cmd, unsigned long arg)
1054 return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
1058 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1059 size_t count, loff_t *ppos)
1061 struct video_device *dev = video_devdata(file);
1062 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1063 int noblock = file->f_flags & O_NONBLOCK;
1064 unsigned long lock_flags;
1066 int ret,i;
1067 struct usbvision_frame *frame;
1069 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
1071 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
1072 return -EFAULT;
1074 /* This entry point is compatible with the mmap routines so that a user can do either
1075 VIDIOC_QBUF/VIDIOC_DQBUF to get frames or call read on the device. */
1076 if(!usbvision->num_frames) {
1077 /* First, allocate some frames to work with if this has not been done with
1078 VIDIOC_REQBUF */
1079 usbvision_frames_free(usbvision);
1080 usbvision_empty_framequeues(usbvision);
1081 usbvision_frames_alloc(usbvision,USBVISION_NUMFRAMES);
1084 if(usbvision->streaming != Stream_On) {
1085 /* no stream is running, make it running ! */
1086 usbvision->streaming = Stream_On;
1087 call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
1090 /* Then, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
1091 for(i=0;i<usbvision->num_frames;i++) {
1092 frame = &usbvision->frame[i];
1093 if(frame->grabstate == FrameState_Unused) {
1094 /* Mark it as ready and enqueue frame */
1095 frame->grabstate = FrameState_Ready;
1096 frame->scanstate = ScanState_Scanning;
1097 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
1099 /* set v4l2_format index */
1100 frame->v4l2_format = usbvision->palette;
1102 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1103 list_add_tail(&frame->frame, &usbvision->inqueue);
1104 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1108 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1109 if (list_empty(&(usbvision->outqueue))) {
1110 if(noblock)
1111 return -EAGAIN;
1113 ret = wait_event_interruptible
1114 (usbvision->wait_frame,
1115 !list_empty(&(usbvision->outqueue)));
1116 if (ret)
1117 return ret;
1120 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1121 frame = list_entry(usbvision->outqueue.next,
1122 struct usbvision_frame, frame);
1123 list_del(usbvision->outqueue.next);
1124 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1126 /* An error returns an empty frame */
1127 if (frame->grabstate == FrameState_Error) {
1128 frame->bytes_read = 0;
1129 return 0;
1132 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
1133 frame->index, frame->bytes_read, frame->scanlength);
1135 /* copy bytes to user space; we allow for partials reads */
1136 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1137 count = frame->scanlength - frame->bytes_read;
1139 if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
1140 return -EFAULT;
1143 frame->bytes_read += count;
1144 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
1145 (unsigned long)count, frame->bytes_read);
1147 // For now, forget the frame if it has not been read in one shot.
1148 /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
1149 frame->bytes_read = 0;
1151 /* Mark it as available to be used again. */
1152 frame->grabstate = FrameState_Unused;
1153 /* } */
1155 return count;
1158 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1160 unsigned long size = vma->vm_end - vma->vm_start,
1161 start = vma->vm_start;
1162 void *pos;
1163 u32 i;
1165 struct video_device *dev = video_devdata(file);
1166 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1168 PDEBUG(DBG_MMAP, "mmap");
1170 down(&usbvision->lock);
1172 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1173 up(&usbvision->lock);
1174 return -EFAULT;
1177 if (!(vma->vm_flags & VM_WRITE) ||
1178 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1179 up(&usbvision->lock);
1180 return -EINVAL;
1183 for (i = 0; i < usbvision->num_frames; i++) {
1184 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) == vma->vm_pgoff)
1185 break;
1187 if (i == usbvision->num_frames) {
1188 PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
1189 up(&usbvision->lock);
1190 return -EINVAL;
1193 /* VM_IO is eventually going to replace PageReserved altogether */
1194 vma->vm_flags |= VM_IO;
1195 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1197 pos = usbvision->frame[i].data;
1198 while (size > 0) {
1200 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1201 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1202 up(&usbvision->lock);
1203 return -EAGAIN;
1205 start += PAGE_SIZE;
1206 pos += PAGE_SIZE;
1207 size -= PAGE_SIZE;
1210 up(&usbvision->lock);
1211 return 0;
1216 * Here comes the stuff for radio on usbvision based devices
1219 static int usbvision_radio_open(struct inode *inode, struct file *file)
1221 struct video_device *dev = video_devdata(file);
1222 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1223 struct v4l2_frequency freq;
1224 int errCode = 0;
1226 PDEBUG(DBG_IO, "%s:", __FUNCTION__);
1228 down(&usbvision->lock);
1230 if (usbvision->user) {
1231 err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
1232 errCode = -EBUSY;
1234 else {
1235 if(PowerOnAtOpen) {
1236 usbvision_reset_powerOffTimer(usbvision);
1237 if (usbvision->power == 0) {
1238 usbvision_power_on(usbvision);
1239 usbvision_i2c_register(usbvision);
1243 /* Alternate interface 1 is is the biggest frame size */
1244 errCode = usbvision_set_alternate(usbvision);
1245 if (errCode < 0) {
1246 usbvision->last_error = errCode;
1247 return -EBUSY;
1250 // If so far no errors then we shall start the radio
1251 usbvision->radio = 1;
1252 call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
1253 freq.frequency = 1517; //SWR3 @ 94.8MHz
1254 call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
1255 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1256 usbvision->user++;
1259 if (errCode) {
1260 if (PowerOnAtOpen) {
1261 usbvision_i2c_unregister(usbvision);
1262 usbvision_power_off(usbvision);
1263 usbvision->initialized = 0;
1266 up(&usbvision->lock);
1267 return errCode;
1271 static int usbvision_radio_close(struct inode *inode, struct file *file)
1273 struct video_device *dev = video_devdata(file);
1274 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1275 int errCode = 0;
1277 PDEBUG(DBG_IO, "");
1279 down(&usbvision->lock);
1281 /* Set packet size to 0 */
1282 usbvision->ifaceAlt=0;
1283 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
1284 usbvision->ifaceAlt);
1286 usbvision_audio_off(usbvision);
1287 usbvision->radio=0;
1288 usbvision->user--;
1290 if (PowerOnAtOpen) {
1291 usbvision_set_powerOffTimer(usbvision);
1292 usbvision->initialized = 0;
1295 up(&usbvision->lock);
1297 if (usbvision->remove_pending) {
1298 printk(KERN_INFO "%s: Final disconnect\n", __FUNCTION__);
1299 usbvision_release(usbvision);
1303 PDEBUG(DBG_IO, "success");
1305 return errCode;
1308 static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
1309 unsigned int cmd, void *arg)
1311 struct video_device *dev = video_devdata(file);
1312 struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
1314 if (!USBVISION_IS_OPERATIONAL(usbvision))
1315 return -EIO;
1317 switch (cmd) {
1318 case VIDIOC_QUERYCAP:
1320 struct v4l2_capability *vc=arg;
1322 memset(vc, 0, sizeof(*vc));
1323 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
1324 strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
1325 sizeof(vc->card));
1326 strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
1327 sizeof(vc->bus_info));
1328 vc->version = USBVISION_DRIVER_VERSION;
1329 vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
1330 PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
1331 return 0;
1333 case VIDIOC_QUERYCTRL:
1335 struct v4l2_queryctrl *ctrl = arg;
1336 int id=ctrl->id;
1338 memset(ctrl,0,sizeof(*ctrl));
1339 ctrl->id=id;
1341 call_i2c_clients(usbvision, cmd, arg);
1342 PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
1344 if (ctrl->type)
1345 return 0;
1346 else
1347 return -EINVAL;
1350 case VIDIOC_G_CTRL:
1352 struct v4l2_control *ctrl = arg;
1354 call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
1355 PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1356 return 0;
1358 case VIDIOC_S_CTRL:
1360 struct v4l2_control *ctrl = arg;
1362 call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
1363 PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
1364 return 0;
1366 case VIDIOC_G_TUNER:
1368 struct v4l2_tuner *t = arg;
1370 if (t->index > 0)
1371 return -EINVAL;
1373 memset(t,0,sizeof(*t));
1374 strcpy(t->name, "Radio");
1375 t->type = V4L2_TUNER_RADIO;
1377 /* Let clients fill in the remainder of this struct */
1378 call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
1379 PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
1380 return 0;
1382 case VIDIOC_S_TUNER:
1384 struct v4l2_tuner *vt = arg;
1386 // Only no or one tuner for now
1387 if (!usbvision->have_tuner || vt->index)
1388 return -EINVAL;
1389 /* let clients handle this */
1390 call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
1392 PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
1393 return 0;
1395 case VIDIOC_G_AUDIO:
1397 struct v4l2_audio *a = arg;
1399 memset(a,0,sizeof(*a));
1400 strcpy(a->name,"Radio");
1401 PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
1402 return 0;
1404 case VIDIOC_S_AUDIO:
1405 case VIDIOC_S_INPUT:
1406 case VIDIOC_S_STD:
1407 return 0;
1409 case VIDIOC_G_FREQUENCY:
1411 struct v4l2_frequency *f = arg;
1413 memset(f,0,sizeof(*f));
1415 f->type = V4L2_TUNER_RADIO;
1416 f->frequency = usbvision->freq;
1417 call_i2c_clients(usbvision, cmd, f);
1418 PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1420 return 0;
1422 case VIDIOC_S_FREQUENCY:
1424 struct v4l2_frequency *f = arg;
1426 if (f->tuner != 0)
1427 return -EINVAL;
1428 usbvision->freq = f->frequency;
1429 call_i2c_clients(usbvision, cmd, f);
1430 PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
1432 return 0;
1434 default:
1436 PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
1437 return -ENOIOCTLCMD;
1440 return 0;
1444 static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
1445 unsigned int cmd, unsigned long arg)
1447 return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
1452 * Here comes the stuff for vbi on usbvision based devices
1455 static int usbvision_vbi_open(struct inode *inode, struct file *file)
1457 /* TODO */
1458 return -EINVAL;
1462 static int usbvision_vbi_close(struct inode *inode, struct file *file)
1464 /* TODO */
1465 return -EINVAL;
1468 static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
1469 unsigned int cmd, void *arg)
1471 /* TODO */
1472 return -EINVAL;
1475 static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
1476 unsigned int cmd, unsigned long arg)
1478 return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
1483 // Video registration stuff
1486 // Video template
1487 static const struct file_operations usbvision_fops = {
1488 .owner = THIS_MODULE,
1489 .open = usbvision_v4l2_open,
1490 .release = usbvision_v4l2_close,
1491 .read = usbvision_v4l2_read,
1492 .mmap = usbvision_v4l2_mmap,
1493 .ioctl = usbvision_v4l2_ioctl,
1494 .llseek = no_llseek,
1496 static struct video_device usbvision_video_template = {
1497 .owner = THIS_MODULE,
1498 .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
1499 .hardware = VID_HARDWARE_USBVISION,
1500 .fops = &usbvision_fops,
1501 .name = "usbvision-video",
1502 .release = video_device_release,
1503 .minor = -1,
1507 // Radio template
1508 static const struct file_operations usbvision_radio_fops = {
1509 .owner = THIS_MODULE,
1510 .open = usbvision_radio_open,
1511 .release = usbvision_radio_close,
1512 .ioctl = usbvision_radio_ioctl,
1513 .llseek = no_llseek,
1516 static struct video_device usbvision_radio_template=
1518 .owner = THIS_MODULE,
1519 .type = VID_TYPE_TUNER,
1520 .hardware = VID_HARDWARE_USBVISION,
1521 .fops = &usbvision_radio_fops,
1522 .release = video_device_release,
1523 .name = "usbvision-radio",
1524 .minor = -1,
1528 // vbi template
1529 static const struct file_operations usbvision_vbi_fops = {
1530 .owner = THIS_MODULE,
1531 .open = usbvision_vbi_open,
1532 .release = usbvision_vbi_close,
1533 .ioctl = usbvision_vbi_ioctl,
1534 .llseek = no_llseek,
1537 static struct video_device usbvision_vbi_template=
1539 .owner = THIS_MODULE,
1540 .type = VID_TYPE_TUNER,
1541 .hardware = VID_HARDWARE_USBVISION,
1542 .fops = &usbvision_vbi_fops,
1543 .release = video_device_release,
1544 .name = "usbvision-vbi",
1545 .minor = -1,
1549 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1550 struct video_device *vdev_template,
1551 char *name)
1553 struct usb_device *usb_dev = usbvision->dev;
1554 struct video_device *vdev;
1556 if (usb_dev == NULL) {
1557 err("%s: usbvision->dev is not set", __FUNCTION__);
1558 return NULL;
1561 vdev = video_device_alloc();
1562 if (NULL == vdev) {
1563 return NULL;
1565 *vdev = *vdev_template;
1566 // vdev->minor = -1;
1567 vdev->dev = &usb_dev->dev;
1568 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1569 video_set_drvdata(vdev, usbvision);
1570 return vdev;
1573 // unregister video4linux devices
1574 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1576 // vbi Device:
1577 if (usbvision->vbi) {
1578 PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
1579 if (usbvision->vbi->minor != -1) {
1580 video_unregister_device(usbvision->vbi);
1582 else {
1583 video_device_release(usbvision->vbi);
1585 usbvision->vbi = NULL;
1588 // Radio Device:
1589 if (usbvision->rdev) {
1590 PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
1591 if (usbvision->rdev->minor != -1) {
1592 video_unregister_device(usbvision->rdev);
1594 else {
1595 video_device_release(usbvision->rdev);
1597 usbvision->rdev = NULL;
1600 // Video Device:
1601 if (usbvision->vdev) {
1602 PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
1603 if (usbvision->vdev->minor != -1) {
1604 video_unregister_device(usbvision->vdev);
1606 else {
1607 video_device_release(usbvision->vdev);
1609 usbvision->vdev = NULL;
1613 // register video4linux devices
1614 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1616 // Video Device:
1617 usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
1618 if (usbvision->vdev == NULL) {
1619 goto err_exit;
1621 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
1622 goto err_exit;
1624 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]\n", usbvision->nr,usbvision->vdev->minor & 0x1f);
1626 // Radio Device:
1627 if (usbvision_device_data[usbvision->DevModel].Radio) {
1628 // usbvision has radio
1629 usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
1630 if (usbvision->rdev == NULL) {
1631 goto err_exit;
1633 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
1634 goto err_exit;
1636 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]\n", usbvision->nr, usbvision->rdev->minor & 0x1f);
1638 // vbi Device:
1639 if (usbvision_device_data[usbvision->DevModel].vbi) {
1640 usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
1641 if (usbvision->vdev == NULL) {
1642 goto err_exit;
1644 if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
1645 goto err_exit;
1647 printk(KERN_INFO "USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)\n", usbvision->nr,usbvision->vbi->minor & 0x1f);
1649 // all done
1650 return 0;
1652 err_exit:
1653 err("USBVision[%d]: video_register_device() failed", usbvision->nr);
1654 usbvision_unregister_video(usbvision);
1655 return -1;
1659 * usbvision_alloc()
1661 * This code allocates the struct usb_usbvision. It is filled with default values.
1663 * Returns NULL on error, a pointer to usb_usbvision else.
1666 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
1668 struct usb_usbvision *usbvision;
1670 if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
1671 goto err_exit;
1674 usbvision->dev = dev;
1676 init_MUTEX(&usbvision->lock); /* to 1 == available */
1678 // prepare control urb for control messages during interrupts
1679 usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1680 if (usbvision->ctrlUrb == NULL) {
1681 goto err_exit;
1683 init_waitqueue_head(&usbvision->ctrlUrb_wq);
1684 init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
1686 usbvision_init_powerOffTimer(usbvision);
1688 return usbvision;
1690 err_exit:
1691 if (usbvision && usbvision->ctrlUrb) {
1692 usb_free_urb(usbvision->ctrlUrb);
1694 if (usbvision) {
1695 kfree(usbvision);
1697 return NULL;
1701 * usbvision_release()
1703 * This code does final release of struct usb_usbvision. This happens
1704 * after the device is disconnected -and- all clients closed their files.
1707 static void usbvision_release(struct usb_usbvision *usbvision)
1709 PDEBUG(DBG_PROBE, "");
1711 down(&usbvision->lock);
1713 usbvision_reset_powerOffTimer(usbvision);
1715 usbvision->initialized = 0;
1717 up(&usbvision->lock);
1719 usbvision_remove_sysfs(usbvision->vdev);
1720 usbvision_unregister_video(usbvision);
1722 if (usbvision->ctrlUrb) {
1723 usb_free_urb(usbvision->ctrlUrb);
1726 kfree(usbvision);
1728 PDEBUG(DBG_PROBE, "success");
1732 /******************************** usb interface *****************************************/
1734 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1736 int model,i;
1738 if (usbvision == NULL)
1739 return;
1741 model = usbvision->DevModel;
1742 usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
1744 if (usbvision_device_data[usbvision->DevModel].Vin_Reg2_override) {
1745 usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2;
1746 } else {
1747 usbvision->Vin_Reg2_Preset = 0;
1750 for (i = 0; i < TVNORMS; i++)
1751 if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
1752 break;
1753 if (i == TVNORMS)
1754 i = 0;
1755 usbvision->tvnorm = &tvnorms[i]; /* set default norm */
1757 usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
1758 usbvision->ctl_input = 0;
1760 /* This should be here to make i2c clients to be able to register */
1761 usbvision_audio_off(usbvision); //first switch off audio
1762 if (!PowerOnAtOpen) {
1763 usbvision_power_on(usbvision); //and then power up the noisy tuner
1764 usbvision_i2c_register(usbvision);
1769 * usbvision_probe()
1771 * This procedure queries device descriptor and accepts the interface
1772 * if it looks like USBVISION video device
1775 static int __devinit usbvision_probe(struct usb_interface *intf,
1776 const struct usb_device_id *devid)
1778 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1779 struct usb_interface *uif;
1780 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1781 const struct usb_host_interface *interface;
1782 struct usb_usbvision *usbvision = NULL;
1783 const struct usb_endpoint_descriptor *endpoint;
1784 int model,i;
1786 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1787 dev->descriptor.idVendor,
1788 dev->descriptor.idProduct, ifnum);
1790 model = devid->driver_info;
1791 if ( (model<0) || (model>=usbvision_device_data_size) ) {
1792 PDEBUG(DBG_PROBE, "model out of bounds %d",model);
1793 return -ENODEV;
1795 printk(KERN_INFO "%s: %s found\n", __FUNCTION__,
1796 usbvision_device_data[model].ModelString);
1798 if (usbvision_device_data[model].Interface >= 0) {
1799 interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
1801 else {
1802 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1804 endpoint = &interface->endpoint[1].desc;
1805 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
1806 err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
1807 err("%s: Endpoint attributes %d", __FUNCTION__, endpoint->bmAttributes);
1808 return -ENODEV;
1810 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1811 err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
1812 return -ENODEV;
1815 if ((usbvision = usbvision_alloc(dev)) == NULL) {
1816 err("%s: couldn't allocate USBVision struct", __FUNCTION__);
1817 return -ENOMEM;
1820 if (dev->descriptor.bNumConfigurations > 1) {
1821 usbvision->bridgeType = BRIDGE_NT1004;
1823 else if (model == DAZZLE_DVC_90_REV_1_SECAM) {
1824 usbvision->bridgeType = BRIDGE_NT1005;
1826 else {
1827 usbvision->bridgeType = BRIDGE_NT1003;
1829 PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
1831 down(&usbvision->lock);
1833 /* compute alternate max packet sizes */
1834 uif = dev->actconfig->interface[0];
1836 usbvision->num_alt=uif->num_altsetting;
1837 PDEBUG(DBG_PROBE, "Alternate settings: %i",usbvision->num_alt);
1838 usbvision->alt_max_pkt_size = kmalloc(32*
1839 usbvision->num_alt,GFP_KERNEL);
1840 if (usbvision->alt_max_pkt_size == NULL) {
1841 err("usbvision: out of memory!\n");
1842 return -ENOMEM;
1845 for (i = 0; i < usbvision->num_alt ; i++) {
1846 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1847 wMaxPacketSize);
1848 usbvision->alt_max_pkt_size[i] =
1849 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1850 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i",i,
1851 usbvision->alt_max_pkt_size[i]);
1855 usbvision->nr = usbvision_nr++;
1857 usbvision->have_tuner = usbvision_device_data[model].Tuner;
1858 if (usbvision->have_tuner) {
1859 usbvision->tuner_type = usbvision_device_data[model].TunerType;
1862 usbvision->tuner_addr = ADDR_UNSET;
1864 usbvision->DevModel = model;
1865 usbvision->remove_pending = 0;
1866 usbvision->iface = ifnum;
1867 usbvision->ifaceAlt = 0;
1868 usbvision->video_endp = endpoint->bEndpointAddress;
1869 usbvision->isocPacketSize = 0;
1870 usbvision->usb_bandwidth = 0;
1871 usbvision->user = 0;
1872 usbvision->streaming = Stream_Off;
1873 usbvision_register_video(usbvision);
1874 usbvision_configure_video(usbvision);
1875 up(&usbvision->lock);
1878 usb_set_intfdata (intf, usbvision);
1879 usbvision_create_sysfs(usbvision->vdev);
1881 PDEBUG(DBG_PROBE, "success");
1882 return 0;
1887 * usbvision_disconnect()
1889 * This procedure stops all driver activity, deallocates interface-private
1890 * structure (pointed by 'ptr') and after that driver should be removable
1891 * with no ill consequences.
1894 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1896 struct usb_usbvision *usbvision = usb_get_intfdata(intf);
1898 PDEBUG(DBG_PROBE, "");
1900 if (usbvision == NULL) {
1901 err("%s: usb_get_intfdata() failed", __FUNCTION__);
1902 return;
1904 usb_set_intfdata (intf, NULL);
1906 down(&usbvision->lock);
1908 // At this time we ask to cancel outstanding URBs
1909 usbvision_stop_isoc(usbvision);
1911 if (usbvision->power) {
1912 usbvision_i2c_unregister(usbvision);
1913 usbvision_power_off(usbvision);
1915 usbvision->remove_pending = 1; // Now all ISO data will be ignored
1917 usb_put_dev(usbvision->dev);
1918 usbvision->dev = NULL; // USB device is no more
1920 up(&usbvision->lock);
1922 if (usbvision->user) {
1923 printk(KERN_INFO "%s: In use, disconnect pending\n", __FUNCTION__);
1924 wake_up_interruptible(&usbvision->wait_frame);
1925 wake_up_interruptible(&usbvision->wait_stream);
1927 else {
1928 usbvision_release(usbvision);
1931 PDEBUG(DBG_PROBE, "success");
1935 static struct usb_driver usbvision_driver = {
1936 .name = "usbvision",
1937 .id_table = usbvision_table,
1938 .probe = usbvision_probe,
1939 .disconnect = usbvision_disconnect
1943 * usbvision_init()
1945 * This code is run to initialize the driver.
1948 static int __init usbvision_init(void)
1950 int errCode;
1952 PDEBUG(DBG_PROBE, "");
1954 PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
1955 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1956 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1957 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1959 /* disable planar mode support unless compression enabled */
1960 if (isocMode != ISOC_MODE_COMPRESS ) {
1961 // FIXME : not the right way to set supported flag
1962 usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
1963 usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
1966 errCode = usb_register(&usbvision_driver);
1968 if (errCode == 0) {
1969 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1970 PDEBUG(DBG_PROBE, "success");
1972 return errCode;
1975 static void __exit usbvision_exit(void)
1977 PDEBUG(DBG_PROBE, "");
1979 usb_deregister(&usbvision_driver);
1980 PDEBUG(DBG_PROBE, "success");
1983 module_init(usbvision_init);
1984 module_exit(usbvision_exit);
1987 * Overrides for Emacs so that we follow Linus's tabbing style.
1988 * ---------------------------------------------------------------------------
1989 * Local variables:
1990 * c-basic-offset: 8
1991 * End: