usbvision: fix crash on detecting device with invalid configuration
[linux/fpc-iii.git] / drivers / media / video / usbvision / usbvision-video.c
blob902140e8db3e738e0067cfdf2a42b968157649ee
1 /*
2 * USB USBVISION Video device driver 0.9.10
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/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/highmem.h>
54 #include <linux/vmalloc.h>
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/spinlock.h>
58 #include <linux/io.h>
59 #include <linux/videodev2.h>
60 #include <linux/i2c.h>
62 #include <media/saa7115.h>
63 #include <media/v4l2-common.h>
64 #include <media/v4l2-ioctl.h>
65 #include <media/tuner.h>
67 #include <linux/workqueue.h>
69 #include "usbvision.h"
70 #include "usbvision-cards.h"
72 #define DRIVER_AUTHOR \
73 "Joerg Heckenbach <joerg@heckenbach-aw.de>, " \
74 "Dwaine Garden <DwaineGarden@rogers.com>"
75 #define DRIVER_NAME "usbvision"
76 #define DRIVER_ALIAS "USBVision"
77 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
78 #define DRIVER_LICENSE "GPL"
79 #define USBVISION_VERSION_STRING "0.9.11"
81 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
84 #ifdef USBVISION_DEBUG
85 #define PDEBUG(level, fmt, args...) { \
86 if (video_debug & (level)) \
87 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
88 __func__, __LINE__ , ## args); \
90 #else
91 #define PDEBUG(level, fmt, args...) do {} while (0)
92 #endif
94 #define DBG_IO (1 << 1)
95 #define DBG_PROBE (1 << 2)
96 #define DBG_MMAP (1 << 3)
98 /* String operations */
99 #define rmspace(str) while (*str == ' ') str++;
100 #define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
103 /* sequential number of usbvision device */
104 static int usbvision_nr;
106 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
107 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
108 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
109 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
110 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
111 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
112 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
113 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
114 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
117 /* Function prototypes */
118 static void usbvision_release(struct usb_usbvision *usbvision);
120 /* Default initialization of device driver parameters */
121 /* Set the default format for ISOC endpoint */
122 static int isoc_mode = ISOC_MODE_COMPRESS;
123 /* Set the default Debug Mode of the device driver */
124 static int video_debug;
125 /* Set the default device to power on at startup */
126 static int power_on_at_open = 1;
127 /* Sequential Number of Video Device */
128 static int video_nr = -1;
129 /* Sequential Number of Radio Device */
130 static int radio_nr = -1;
132 /* Grab parameters for the device driver */
134 /* Showing parameters under SYSFS */
135 module_param(isoc_mode, int, 0444);
136 module_param(video_debug, int, 0444);
137 module_param(power_on_at_open, int, 0444);
138 module_param(video_nr, int, 0444);
139 module_param(radio_nr, int, 0444);
141 MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
142 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
143 MODULE_PARM_DESC(power_on_at_open, " Set the default device to power on when device is opened. Default: 1 (On)");
144 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
145 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
148 /* Misc stuff */
149 MODULE_AUTHOR(DRIVER_AUTHOR);
150 MODULE_DESCRIPTION(DRIVER_DESC);
151 MODULE_LICENSE(DRIVER_LICENSE);
152 MODULE_VERSION(USBVISION_VERSION_STRING);
153 MODULE_ALIAS(DRIVER_ALIAS);
156 /*****************************************************************************/
157 /* SYSFS Code - Copied from the stv680.c usb module. */
158 /* Device information is located at /sys/class/video4linux/video0 */
159 /* Device parameters information is located at /sys/module/usbvision */
160 /* Device USB Information is located at */
161 /* /sys/bus/usb/drivers/USBVision Video Grabber */
162 /*****************************************************************************/
164 #define YES_NO(x) ((x) ? "Yes" : "No")
166 static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
168 struct video_device *vdev =
169 container_of(cd, struct video_device, dev);
170 return video_get_drvdata(vdev);
173 static ssize_t show_version(struct device *cd,
174 struct device_attribute *attr, char *buf)
176 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
178 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
180 static ssize_t show_model(struct device *cd,
181 struct device_attribute *attr, char *buf)
183 struct video_device *vdev =
184 container_of(cd, struct video_device, dev);
185 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
186 return sprintf(buf, "%s\n",
187 usbvision_device_data[usbvision->dev_model].model_string);
189 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
191 static ssize_t show_hue(struct device *cd,
192 struct device_attribute *attr, char *buf)
194 struct video_device *vdev =
195 container_of(cd, struct video_device, dev);
196 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
197 struct v4l2_control ctrl;
198 ctrl.id = V4L2_CID_HUE;
199 ctrl.value = 0;
200 if (usbvision->user)
201 call_all(usbvision, core, g_ctrl, &ctrl);
202 return sprintf(buf, "%d\n", ctrl.value);
204 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
206 static ssize_t show_contrast(struct device *cd,
207 struct device_attribute *attr, char *buf)
209 struct video_device *vdev =
210 container_of(cd, struct video_device, dev);
211 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
212 struct v4l2_control ctrl;
213 ctrl.id = V4L2_CID_CONTRAST;
214 ctrl.value = 0;
215 if (usbvision->user)
216 call_all(usbvision, core, g_ctrl, &ctrl);
217 return sprintf(buf, "%d\n", ctrl.value);
219 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
221 static ssize_t show_brightness(struct device *cd,
222 struct device_attribute *attr, char *buf)
224 struct video_device *vdev =
225 container_of(cd, struct video_device, dev);
226 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
227 struct v4l2_control ctrl;
228 ctrl.id = V4L2_CID_BRIGHTNESS;
229 ctrl.value = 0;
230 if (usbvision->user)
231 call_all(usbvision, core, g_ctrl, &ctrl);
232 return sprintf(buf, "%d\n", ctrl.value);
234 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
236 static ssize_t show_saturation(struct device *cd,
237 struct device_attribute *attr, char *buf)
239 struct video_device *vdev =
240 container_of(cd, struct video_device, dev);
241 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
242 struct v4l2_control ctrl;
243 ctrl.id = V4L2_CID_SATURATION;
244 ctrl.value = 0;
245 if (usbvision->user)
246 call_all(usbvision, core, g_ctrl, &ctrl);
247 return sprintf(buf, "%d\n", ctrl.value);
249 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
251 static ssize_t show_streaming(struct device *cd,
252 struct device_attribute *attr, char *buf)
254 struct video_device *vdev =
255 container_of(cd, struct video_device, dev);
256 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
257 return sprintf(buf, "%s\n",
258 YES_NO(usbvision->streaming == stream_on ? 1 : 0));
260 static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
262 static ssize_t show_compression(struct device *cd,
263 struct device_attribute *attr, char *buf)
265 struct video_device *vdev =
266 container_of(cd, struct video_device, dev);
267 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
268 return sprintf(buf, "%s\n",
269 YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
271 static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
273 static ssize_t show_device_bridge(struct device *cd,
274 struct device_attribute *attr, char *buf)
276 struct video_device *vdev =
277 container_of(cd, struct video_device, dev);
278 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
279 return sprintf(buf, "%d\n", usbvision->bridge_type);
281 static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
283 static void usbvision_create_sysfs(struct video_device *vdev)
285 int res;
287 if (!vdev)
288 return;
289 do {
290 res = device_create_file(&vdev->dev, &dev_attr_version);
291 if (res < 0)
292 break;
293 res = device_create_file(&vdev->dev, &dev_attr_model);
294 if (res < 0)
295 break;
296 res = device_create_file(&vdev->dev, &dev_attr_hue);
297 if (res < 0)
298 break;
299 res = device_create_file(&vdev->dev, &dev_attr_contrast);
300 if (res < 0)
301 break;
302 res = device_create_file(&vdev->dev, &dev_attr_brightness);
303 if (res < 0)
304 break;
305 res = device_create_file(&vdev->dev, &dev_attr_saturation);
306 if (res < 0)
307 break;
308 res = device_create_file(&vdev->dev, &dev_attr_streaming);
309 if (res < 0)
310 break;
311 res = device_create_file(&vdev->dev, &dev_attr_compression);
312 if (res < 0)
313 break;
314 res = device_create_file(&vdev->dev, &dev_attr_bridge);
315 if (res >= 0)
316 return;
317 } while (0);
319 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
322 static void usbvision_remove_sysfs(struct video_device *vdev)
324 if (vdev) {
325 device_remove_file(&vdev->dev, &dev_attr_version);
326 device_remove_file(&vdev->dev, &dev_attr_model);
327 device_remove_file(&vdev->dev, &dev_attr_hue);
328 device_remove_file(&vdev->dev, &dev_attr_contrast);
329 device_remove_file(&vdev->dev, &dev_attr_brightness);
330 device_remove_file(&vdev->dev, &dev_attr_saturation);
331 device_remove_file(&vdev->dev, &dev_attr_streaming);
332 device_remove_file(&vdev->dev, &dev_attr_compression);
333 device_remove_file(&vdev->dev, &dev_attr_bridge);
338 * usbvision_open()
340 * This is part of Video 4 Linux API. The driver can be opened by one
341 * client only (checks internal counter 'usbvision->user'). The procedure
342 * then allocates buffers needed for video processing.
345 static int usbvision_v4l2_open(struct file *file)
347 struct usb_usbvision *usbvision = video_drvdata(file);
348 int err_code = 0;
350 PDEBUG(DBG_IO, "open");
352 usbvision_reset_power_off_timer(usbvision);
354 if (usbvision->user)
355 err_code = -EBUSY;
356 else {
357 /* Allocate memory for the scratch ring buffer */
358 err_code = usbvision_scratch_alloc(usbvision);
359 if (isoc_mode == ISOC_MODE_COMPRESS) {
360 /* Allocate intermediate decompression buffers
361 only if needed */
362 err_code = usbvision_decompress_alloc(usbvision);
364 if (err_code) {
365 /* Deallocate all buffers if trouble */
366 usbvision_scratch_free(usbvision);
367 usbvision_decompress_free(usbvision);
371 /* If so far no errors then we shall start the camera */
372 if (!err_code) {
373 if (usbvision->power == 0) {
374 usbvision_power_on(usbvision);
375 usbvision_i2c_register(usbvision);
378 /* Send init sequence only once, it's large! */
379 if (!usbvision->initialized) {
380 int setup_ok = 0;
381 setup_ok = usbvision_setup(usbvision, isoc_mode);
382 if (setup_ok)
383 usbvision->initialized = 1;
384 else
385 err_code = -EBUSY;
388 if (!err_code) {
389 usbvision_begin_streaming(usbvision);
390 err_code = usbvision_init_isoc(usbvision);
391 /* device must be initialized before isoc transfer */
392 usbvision_muxsel(usbvision, 0);
393 usbvision->user++;
394 } else {
395 if (power_on_at_open) {
396 usbvision_i2c_unregister(usbvision);
397 usbvision_power_off(usbvision);
398 usbvision->initialized = 0;
403 /* prepare queues */
404 usbvision_empty_framequeues(usbvision);
406 PDEBUG(DBG_IO, "success");
407 return err_code;
411 * usbvision_v4l2_close()
413 * This is part of Video 4 Linux API. The procedure
414 * stops streaming and deallocates all buffers that were earlier
415 * allocated in usbvision_v4l2_open().
418 static int usbvision_v4l2_close(struct file *file)
420 struct usb_usbvision *usbvision = video_drvdata(file);
422 PDEBUG(DBG_IO, "close");
424 usbvision_audio_off(usbvision);
425 usbvision_restart_isoc(usbvision);
426 usbvision_stop_isoc(usbvision);
428 usbvision_decompress_free(usbvision);
429 usbvision_frames_free(usbvision);
430 usbvision_empty_framequeues(usbvision);
431 usbvision_scratch_free(usbvision);
433 usbvision->user--;
435 if (power_on_at_open) {
436 /* power off in a little while
437 to avoid off/on every close/open short sequences */
438 usbvision_set_power_off_timer(usbvision);
439 usbvision->initialized = 0;
442 if (usbvision->remove_pending) {
443 printk(KERN_INFO "%s: Final disconnect\n", __func__);
444 usbvision_release(usbvision);
447 PDEBUG(DBG_IO, "success");
448 return 0;
453 * usbvision_ioctl()
455 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
458 #ifdef CONFIG_VIDEO_ADV_DEBUG
459 static int vidioc_g_register(struct file *file, void *priv,
460 struct v4l2_dbg_register *reg)
462 struct usb_usbvision *usbvision = video_drvdata(file);
463 int err_code;
465 if (!v4l2_chip_match_host(&reg->match))
466 return -EINVAL;
467 /* NT100x has a 8-bit register space */
468 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
469 if (err_code < 0) {
470 dev_err(&usbvision->vdev->dev,
471 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
472 __func__, err_code);
473 return err_code;
475 reg->val = err_code;
476 reg->size = 1;
477 return 0;
480 static int vidioc_s_register(struct file *file, void *priv,
481 struct v4l2_dbg_register *reg)
483 struct usb_usbvision *usbvision = video_drvdata(file);
484 int err_code;
486 if (!v4l2_chip_match_host(&reg->match))
487 return -EINVAL;
488 /* NT100x has a 8-bit register space */
489 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
490 if (err_code < 0) {
491 dev_err(&usbvision->vdev->dev,
492 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
493 __func__, err_code);
494 return err_code;
496 return 0;
498 #endif
500 static int vidioc_querycap(struct file *file, void *priv,
501 struct v4l2_capability *vc)
503 struct usb_usbvision *usbvision = video_drvdata(file);
505 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
506 strlcpy(vc->card,
507 usbvision_device_data[usbvision->dev_model].model_string,
508 sizeof(vc->card));
509 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
510 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
511 V4L2_CAP_AUDIO |
512 V4L2_CAP_READWRITE |
513 V4L2_CAP_STREAMING |
514 (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
515 return 0;
518 static int vidioc_enum_input(struct file *file, void *priv,
519 struct v4l2_input *vi)
521 struct usb_usbvision *usbvision = video_drvdata(file);
522 int chan;
524 if (vi->index >= usbvision->video_inputs)
525 return -EINVAL;
526 if (usbvision->have_tuner)
527 chan = vi->index;
528 else
529 chan = vi->index + 1; /* skip Television string*/
531 /* Determine the requested input characteristics
532 specific for each usbvision card model */
533 switch (chan) {
534 case 0:
535 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
536 strcpy(vi->name, "White Video Input");
537 } else {
538 strcpy(vi->name, "Television");
539 vi->type = V4L2_INPUT_TYPE_TUNER;
540 vi->audioset = 1;
541 vi->tuner = chan;
542 vi->std = USBVISION_NORMS;
544 break;
545 case 1:
546 vi->type = V4L2_INPUT_TYPE_CAMERA;
547 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
548 strcpy(vi->name, "Green Video Input");
549 else
550 strcpy(vi->name, "Composite Video Input");
551 vi->std = V4L2_STD_PAL;
552 break;
553 case 2:
554 vi->type = V4L2_INPUT_TYPE_CAMERA;
555 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
556 strcpy(vi->name, "Yellow Video Input");
557 else
558 strcpy(vi->name, "S-Video Input");
559 vi->std = V4L2_STD_PAL;
560 break;
561 case 3:
562 vi->type = V4L2_INPUT_TYPE_CAMERA;
563 strcpy(vi->name, "Red Video Input");
564 vi->std = V4L2_STD_PAL;
565 break;
567 return 0;
570 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
572 struct usb_usbvision *usbvision = video_drvdata(file);
574 *input = usbvision->ctl_input;
575 return 0;
578 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
580 struct usb_usbvision *usbvision = video_drvdata(file);
582 if (input >= usbvision->video_inputs)
583 return -EINVAL;
585 usbvision_muxsel(usbvision, input);
586 usbvision_set_input(usbvision);
587 usbvision_set_output(usbvision,
588 usbvision->curwidth,
589 usbvision->curheight);
590 return 0;
593 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
595 struct usb_usbvision *usbvision = video_drvdata(file);
597 usbvision->tvnorm_id = *id;
599 call_all(usbvision, core, s_std, usbvision->tvnorm_id);
600 /* propagate the change to the decoder */
601 usbvision_muxsel(usbvision, usbvision->ctl_input);
603 return 0;
606 static int vidioc_g_tuner(struct file *file, void *priv,
607 struct v4l2_tuner *vt)
609 struct usb_usbvision *usbvision = video_drvdata(file);
611 if (!usbvision->have_tuner || vt->index) /* Only tuner 0 */
612 return -EINVAL;
613 if (usbvision->radio) {
614 strcpy(vt->name, "Radio");
615 vt->type = V4L2_TUNER_RADIO;
616 } else {
617 strcpy(vt->name, "Television");
619 /* Let clients fill in the remainder of this struct */
620 call_all(usbvision, tuner, g_tuner, vt);
622 return 0;
625 static int vidioc_s_tuner(struct file *file, void *priv,
626 struct v4l2_tuner *vt)
628 struct usb_usbvision *usbvision = video_drvdata(file);
630 /* Only no or one tuner for now */
631 if (!usbvision->have_tuner || vt->index)
632 return -EINVAL;
633 /* let clients handle this */
634 call_all(usbvision, tuner, s_tuner, vt);
636 return 0;
639 static int vidioc_g_frequency(struct file *file, void *priv,
640 struct v4l2_frequency *freq)
642 struct usb_usbvision *usbvision = video_drvdata(file);
644 freq->tuner = 0; /* Only one tuner */
645 if (usbvision->radio)
646 freq->type = V4L2_TUNER_RADIO;
647 else
648 freq->type = V4L2_TUNER_ANALOG_TV;
649 freq->frequency = usbvision->freq;
651 return 0;
654 static int vidioc_s_frequency(struct file *file, void *priv,
655 struct v4l2_frequency *freq)
657 struct usb_usbvision *usbvision = video_drvdata(file);
659 /* Only no or one tuner for now */
660 if (!usbvision->have_tuner || freq->tuner)
661 return -EINVAL;
663 usbvision->freq = freq->frequency;
664 call_all(usbvision, tuner, s_frequency, freq);
666 return 0;
669 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
671 struct usb_usbvision *usbvision = video_drvdata(file);
673 if (usbvision->radio)
674 strcpy(a->name, "Radio");
675 else
676 strcpy(a->name, "TV");
678 return 0;
681 static int vidioc_s_audio(struct file *file, void *fh,
682 struct v4l2_audio *a)
684 if (a->index)
685 return -EINVAL;
686 return 0;
689 static int vidioc_queryctrl(struct file *file, void *priv,
690 struct v4l2_queryctrl *ctrl)
692 struct usb_usbvision *usbvision = video_drvdata(file);
694 call_all(usbvision, core, queryctrl, ctrl);
696 if (!ctrl->type)
697 return -EINVAL;
699 return 0;
702 static int vidioc_g_ctrl(struct file *file, void *priv,
703 struct v4l2_control *ctrl)
705 struct usb_usbvision *usbvision = video_drvdata(file);
707 call_all(usbvision, core, g_ctrl, ctrl);
708 return 0;
711 static int vidioc_s_ctrl(struct file *file, void *priv,
712 struct v4l2_control *ctrl)
714 struct usb_usbvision *usbvision = video_drvdata(file);
716 call_all(usbvision, core, s_ctrl, ctrl);
717 return 0;
720 static int vidioc_reqbufs(struct file *file,
721 void *priv, struct v4l2_requestbuffers *vr)
723 struct usb_usbvision *usbvision = video_drvdata(file);
724 int ret;
726 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
728 /* Check input validity:
729 the user must do a VIDEO CAPTURE and MMAP method. */
730 if (vr->memory != V4L2_MEMORY_MMAP)
731 return -EINVAL;
733 if (usbvision->streaming == stream_on) {
734 ret = usbvision_stream_interrupt(usbvision);
735 if (ret)
736 return ret;
739 usbvision_frames_free(usbvision);
740 usbvision_empty_framequeues(usbvision);
741 vr->count = usbvision_frames_alloc(usbvision, vr->count);
743 usbvision->cur_frame = NULL;
745 return 0;
748 static int vidioc_querybuf(struct file *file,
749 void *priv, struct v4l2_buffer *vb)
751 struct usb_usbvision *usbvision = video_drvdata(file);
752 struct usbvision_frame *frame;
754 /* FIXME : must control
755 that buffers are mapped (VIDIOC_REQBUFS has been called) */
756 if (vb->index >= usbvision->num_frames)
757 return -EINVAL;
758 /* Updating the corresponding frame state */
759 vb->flags = 0;
760 frame = &usbvision->frame[vb->index];
761 if (frame->grabstate >= frame_state_ready)
762 vb->flags |= V4L2_BUF_FLAG_QUEUED;
763 if (frame->grabstate >= frame_state_done)
764 vb->flags |= V4L2_BUF_FLAG_DONE;
765 if (frame->grabstate == frame_state_unused)
766 vb->flags |= V4L2_BUF_FLAG_MAPPED;
767 vb->memory = V4L2_MEMORY_MMAP;
769 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
771 vb->memory = V4L2_MEMORY_MMAP;
772 vb->field = V4L2_FIELD_NONE;
773 vb->length = usbvision->curwidth *
774 usbvision->curheight *
775 usbvision->palette.bytes_per_pixel;
776 vb->timestamp = usbvision->frame[vb->index].timestamp;
777 vb->sequence = usbvision->frame[vb->index].sequence;
778 return 0;
781 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
783 struct usb_usbvision *usbvision = video_drvdata(file);
784 struct usbvision_frame *frame;
785 unsigned long lock_flags;
787 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
788 if (vb->index >= usbvision->num_frames)
789 return -EINVAL;
791 frame = &usbvision->frame[vb->index];
793 if (frame->grabstate != frame_state_unused)
794 return -EAGAIN;
796 /* Mark it as ready and enqueue frame */
797 frame->grabstate = frame_state_ready;
798 frame->scanstate = scan_state_scanning;
799 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
801 vb->flags &= ~V4L2_BUF_FLAG_DONE;
803 /* set v4l2_format index */
804 frame->v4l2_format = usbvision->palette;
806 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
807 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
808 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
810 return 0;
813 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
815 struct usb_usbvision *usbvision = video_drvdata(file);
816 int ret;
817 struct usbvision_frame *f;
818 unsigned long lock_flags;
820 if (list_empty(&(usbvision->outqueue))) {
821 if (usbvision->streaming == stream_idle)
822 return -EINVAL;
823 ret = wait_event_interruptible
824 (usbvision->wait_frame,
825 !list_empty(&(usbvision->outqueue)));
826 if (ret)
827 return ret;
830 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
831 f = list_entry(usbvision->outqueue.next,
832 struct usbvision_frame, frame);
833 list_del(usbvision->outqueue.next);
834 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
836 f->grabstate = frame_state_unused;
838 vb->memory = V4L2_MEMORY_MMAP;
839 vb->flags = V4L2_BUF_FLAG_MAPPED |
840 V4L2_BUF_FLAG_QUEUED |
841 V4L2_BUF_FLAG_DONE;
842 vb->index = f->index;
843 vb->sequence = f->sequence;
844 vb->timestamp = f->timestamp;
845 vb->field = V4L2_FIELD_NONE;
846 vb->bytesused = f->scanlength;
848 return 0;
851 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
853 struct usb_usbvision *usbvision = video_drvdata(file);
855 usbvision->streaming = stream_on;
856 call_all(usbvision, video, s_stream, 1);
858 return 0;
861 static int vidioc_streamoff(struct file *file,
862 void *priv, enum v4l2_buf_type type)
864 struct usb_usbvision *usbvision = video_drvdata(file);
866 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
867 return -EINVAL;
869 if (usbvision->streaming == stream_on) {
870 usbvision_stream_interrupt(usbvision);
871 /* Stop all video streamings */
872 call_all(usbvision, video, s_stream, 0);
874 usbvision_empty_framequeues(usbvision);
876 return 0;
879 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
880 struct v4l2_fmtdesc *vfd)
882 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
883 return -EINVAL;
884 strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
885 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
886 return 0;
889 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
890 struct v4l2_format *vf)
892 struct usb_usbvision *usbvision = video_drvdata(file);
893 vf->fmt.pix.width = usbvision->curwidth;
894 vf->fmt.pix.height = usbvision->curheight;
895 vf->fmt.pix.pixelformat = usbvision->palette.format;
896 vf->fmt.pix.bytesperline =
897 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
898 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
899 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
900 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
902 return 0;
905 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
906 struct v4l2_format *vf)
908 struct usb_usbvision *usbvision = video_drvdata(file);
909 int format_idx;
911 /* Find requested format in available ones */
912 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
913 if (vf->fmt.pix.pixelformat ==
914 usbvision_v4l2_format[format_idx].format) {
915 usbvision->palette = usbvision_v4l2_format[format_idx];
916 break;
919 /* robustness */
920 if (format_idx == USBVISION_SUPPORTED_PALETTES)
921 return -EINVAL;
922 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
923 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
925 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
926 usbvision->palette.bytes_per_pixel;
927 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
929 return 0;
932 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
933 struct v4l2_format *vf)
935 struct usb_usbvision *usbvision = video_drvdata(file);
936 int ret;
938 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
939 if (ret)
940 return ret;
942 /* stop io in case it is already in progress */
943 if (usbvision->streaming == stream_on) {
944 ret = usbvision_stream_interrupt(usbvision);
945 if (ret)
946 return ret;
948 usbvision_frames_free(usbvision);
949 usbvision_empty_framequeues(usbvision);
951 usbvision->cur_frame = NULL;
953 /* by now we are committed to the new data... */
954 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
956 return 0;
959 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
960 size_t count, loff_t *ppos)
962 struct usb_usbvision *usbvision = video_drvdata(file);
963 int noblock = file->f_flags & O_NONBLOCK;
964 unsigned long lock_flags;
965 int ret, i;
966 struct usbvision_frame *frame;
968 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
969 (unsigned long)count, noblock);
971 if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
972 return -EFAULT;
974 /* This entry point is compatible with the mmap routines
975 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
976 to get frames or call read on the device. */
977 if (!usbvision->num_frames) {
978 /* First, allocate some frames to work with
979 if this has not been done with VIDIOC_REQBUF */
980 usbvision_frames_free(usbvision);
981 usbvision_empty_framequeues(usbvision);
982 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
985 if (usbvision->streaming != stream_on) {
986 /* no stream is running, make it running ! */
987 usbvision->streaming = stream_on;
988 call_all(usbvision, video, s_stream, 1);
991 /* Then, enqueue as many frames as possible
992 (like a user of VIDIOC_QBUF would do) */
993 for (i = 0; i < usbvision->num_frames; i++) {
994 frame = &usbvision->frame[i];
995 if (frame->grabstate == frame_state_unused) {
996 /* Mark it as ready and enqueue frame */
997 frame->grabstate = frame_state_ready;
998 frame->scanstate = scan_state_scanning;
999 /* Accumulated in usbvision_parse_data() */
1000 frame->scanlength = 0;
1002 /* set v4l2_format index */
1003 frame->v4l2_format = usbvision->palette;
1005 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1006 list_add_tail(&frame->frame, &usbvision->inqueue);
1007 spin_unlock_irqrestore(&usbvision->queue_lock,
1008 lock_flags);
1012 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
1013 if (list_empty(&(usbvision->outqueue))) {
1014 if (noblock)
1015 return -EAGAIN;
1017 ret = wait_event_interruptible
1018 (usbvision->wait_frame,
1019 !list_empty(&(usbvision->outqueue)));
1020 if (ret)
1021 return ret;
1024 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1025 frame = list_entry(usbvision->outqueue.next,
1026 struct usbvision_frame, frame);
1027 list_del(usbvision->outqueue.next);
1028 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1030 /* An error returns an empty frame */
1031 if (frame->grabstate == frame_state_error) {
1032 frame->bytes_read = 0;
1033 return 0;
1036 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
1037 __func__,
1038 frame->index, frame->bytes_read, frame->scanlength);
1040 /* copy bytes to user space; we allow for partials reads */
1041 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
1042 count = frame->scanlength - frame->bytes_read;
1044 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
1045 return -EFAULT;
1047 frame->bytes_read += count;
1048 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
1049 __func__,
1050 (unsigned long)count, frame->bytes_read);
1052 /* For now, forget the frame if it has not been read in one shot. */
1053 /* if (frame->bytes_read >= frame->scanlength) {*/ /* All data has been read */
1054 frame->bytes_read = 0;
1056 /* Mark it as available to be used again. */
1057 frame->grabstate = frame_state_unused;
1058 /* } */
1060 return count;
1063 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1065 unsigned long size = vma->vm_end - vma->vm_start,
1066 start = vma->vm_start;
1067 void *pos;
1068 u32 i;
1069 struct usb_usbvision *usbvision = video_drvdata(file);
1071 PDEBUG(DBG_MMAP, "mmap");
1073 if (!USBVISION_IS_OPERATIONAL(usbvision))
1074 return -EFAULT;
1076 if (!(vma->vm_flags & VM_WRITE) ||
1077 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1078 return -EINVAL;
1081 for (i = 0; i < usbvision->num_frames; i++) {
1082 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1083 vma->vm_pgoff)
1084 break;
1086 if (i == usbvision->num_frames) {
1087 PDEBUG(DBG_MMAP,
1088 "mmap: user supplied mapping address is out of range");
1089 return -EINVAL;
1092 /* VM_IO is eventually going to replace PageReserved altogether */
1093 vma->vm_flags |= VM_IO;
1094 vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
1096 pos = usbvision->frame[i].data;
1097 while (size > 0) {
1098 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1099 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1100 return -EAGAIN;
1102 start += PAGE_SIZE;
1103 pos += PAGE_SIZE;
1104 size -= PAGE_SIZE;
1107 return 0;
1112 * Here comes the stuff for radio on usbvision based devices
1115 static int usbvision_radio_open(struct file *file)
1117 struct usb_usbvision *usbvision = video_drvdata(file);
1118 int err_code = 0;
1120 PDEBUG(DBG_IO, "%s:", __func__);
1122 if (usbvision->user) {
1123 dev_err(&usbvision->rdev->dev,
1124 "%s: Someone tried to open an already opened USBVision Radio!\n",
1125 __func__);
1126 err_code = -EBUSY;
1127 } else {
1128 if (power_on_at_open) {
1129 usbvision_reset_power_off_timer(usbvision);
1130 if (usbvision->power == 0) {
1131 usbvision_power_on(usbvision);
1132 usbvision_i2c_register(usbvision);
1136 /* Alternate interface 1 is is the biggest frame size */
1137 err_code = usbvision_set_alternate(usbvision);
1138 if (err_code < 0) {
1139 usbvision->last_error = err_code;
1140 err_code = -EBUSY;
1141 goto out;
1144 /* If so far no errors then we shall start the radio */
1145 usbvision->radio = 1;
1146 call_all(usbvision, tuner, s_radio);
1147 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1148 usbvision->user++;
1151 if (err_code) {
1152 if (power_on_at_open) {
1153 usbvision_i2c_unregister(usbvision);
1154 usbvision_power_off(usbvision);
1155 usbvision->initialized = 0;
1158 out:
1159 return err_code;
1163 static int usbvision_radio_close(struct file *file)
1165 struct usb_usbvision *usbvision = video_drvdata(file);
1166 int err_code = 0;
1168 PDEBUG(DBG_IO, "");
1170 /* Set packet size to 0 */
1171 usbvision->iface_alt = 0;
1172 err_code = usb_set_interface(usbvision->dev, usbvision->iface,
1173 usbvision->iface_alt);
1175 usbvision_audio_off(usbvision);
1176 usbvision->radio = 0;
1177 usbvision->user--;
1179 if (power_on_at_open) {
1180 usbvision_set_power_off_timer(usbvision);
1181 usbvision->initialized = 0;
1184 if (usbvision->remove_pending) {
1185 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1186 usbvision_release(usbvision);
1189 PDEBUG(DBG_IO, "success");
1190 return err_code;
1193 /* Video registration stuff */
1195 /* Video template */
1196 static const struct v4l2_file_operations usbvision_fops = {
1197 .owner = THIS_MODULE,
1198 .open = usbvision_v4l2_open,
1199 .release = usbvision_v4l2_close,
1200 .read = usbvision_v4l2_read,
1201 .mmap = usbvision_v4l2_mmap,
1202 .unlocked_ioctl = video_ioctl2,
1203 /* .poll = video_poll, */
1206 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1207 .vidioc_querycap = vidioc_querycap,
1208 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1209 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1210 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1211 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1212 .vidioc_reqbufs = vidioc_reqbufs,
1213 .vidioc_querybuf = vidioc_querybuf,
1214 .vidioc_qbuf = vidioc_qbuf,
1215 .vidioc_dqbuf = vidioc_dqbuf,
1216 .vidioc_s_std = vidioc_s_std,
1217 .vidioc_enum_input = vidioc_enum_input,
1218 .vidioc_g_input = vidioc_g_input,
1219 .vidioc_s_input = vidioc_s_input,
1220 .vidioc_queryctrl = vidioc_queryctrl,
1221 .vidioc_g_audio = vidioc_g_audio,
1222 .vidioc_s_audio = vidioc_s_audio,
1223 .vidioc_g_ctrl = vidioc_g_ctrl,
1224 .vidioc_s_ctrl = vidioc_s_ctrl,
1225 .vidioc_streamon = vidioc_streamon,
1226 .vidioc_streamoff = vidioc_streamoff,
1227 .vidioc_g_tuner = vidioc_g_tuner,
1228 .vidioc_s_tuner = vidioc_s_tuner,
1229 .vidioc_g_frequency = vidioc_g_frequency,
1230 .vidioc_s_frequency = vidioc_s_frequency,
1231 #ifdef CONFIG_VIDEO_ADV_DEBUG
1232 .vidioc_g_register = vidioc_g_register,
1233 .vidioc_s_register = vidioc_s_register,
1234 #endif
1237 static struct video_device usbvision_video_template = {
1238 .fops = &usbvision_fops,
1239 .ioctl_ops = &usbvision_ioctl_ops,
1240 .name = "usbvision-video",
1241 .release = video_device_release,
1242 .tvnorms = USBVISION_NORMS,
1243 .current_norm = V4L2_STD_PAL
1247 /* Radio template */
1248 static const struct v4l2_file_operations usbvision_radio_fops = {
1249 .owner = THIS_MODULE,
1250 .open = usbvision_radio_open,
1251 .release = usbvision_radio_close,
1252 .unlocked_ioctl = video_ioctl2,
1255 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1256 .vidioc_querycap = vidioc_querycap,
1257 .vidioc_enum_input = vidioc_enum_input,
1258 .vidioc_g_input = vidioc_g_input,
1259 .vidioc_s_input = vidioc_s_input,
1260 .vidioc_queryctrl = vidioc_queryctrl,
1261 .vidioc_g_audio = vidioc_g_audio,
1262 .vidioc_s_audio = vidioc_s_audio,
1263 .vidioc_g_ctrl = vidioc_g_ctrl,
1264 .vidioc_s_ctrl = vidioc_s_ctrl,
1265 .vidioc_g_tuner = vidioc_g_tuner,
1266 .vidioc_s_tuner = vidioc_s_tuner,
1267 .vidioc_g_frequency = vidioc_g_frequency,
1268 .vidioc_s_frequency = vidioc_s_frequency,
1271 static struct video_device usbvision_radio_template = {
1272 .fops = &usbvision_radio_fops,
1273 .name = "usbvision-radio",
1274 .release = video_device_release,
1275 .ioctl_ops = &usbvision_radio_ioctl_ops,
1277 .tvnorms = USBVISION_NORMS,
1278 .current_norm = V4L2_STD_PAL
1282 static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
1283 struct video_device *vdev_template,
1284 char *name)
1286 struct usb_device *usb_dev = usbvision->dev;
1287 struct video_device *vdev;
1289 if (usb_dev == NULL) {
1290 dev_err(&usbvision->dev->dev,
1291 "%s: usbvision->dev is not set\n", __func__);
1292 return NULL;
1295 vdev = video_device_alloc();
1296 if (NULL == vdev)
1297 return NULL;
1298 *vdev = *vdev_template;
1299 vdev->lock = &usbvision->v4l2_lock;
1300 vdev->v4l2_dev = &usbvision->v4l2_dev;
1301 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1302 video_set_drvdata(vdev, usbvision);
1303 return vdev;
1306 /* unregister video4linux devices */
1307 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1309 /* Radio Device: */
1310 if (usbvision->rdev) {
1311 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1312 video_device_node_name(usbvision->rdev));
1313 if (video_is_registered(usbvision->rdev))
1314 video_unregister_device(usbvision->rdev);
1315 else
1316 video_device_release(usbvision->rdev);
1317 usbvision->rdev = NULL;
1320 /* Video Device: */
1321 if (usbvision->vdev) {
1322 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1323 video_device_node_name(usbvision->vdev));
1324 if (video_is_registered(usbvision->vdev))
1325 video_unregister_device(usbvision->vdev);
1326 else
1327 video_device_release(usbvision->vdev);
1328 usbvision->vdev = NULL;
1332 /* register video4linux devices */
1333 static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
1335 /* Video Device: */
1336 usbvision->vdev = usbvision_vdev_init(usbvision,
1337 &usbvision_video_template,
1338 "USBVision Video");
1339 if (usbvision->vdev == NULL)
1340 goto err_exit;
1341 if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1342 goto err_exit;
1343 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1344 usbvision->nr, video_device_node_name(usbvision->vdev));
1346 /* Radio Device: */
1347 if (usbvision_device_data[usbvision->dev_model].radio) {
1348 /* usbvision has radio */
1349 usbvision->rdev = usbvision_vdev_init(usbvision,
1350 &usbvision_radio_template,
1351 "USBVision Radio");
1352 if (usbvision->rdev == NULL)
1353 goto err_exit;
1354 if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1355 goto err_exit;
1356 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1357 usbvision->nr, video_device_node_name(usbvision->rdev));
1359 /* all done */
1360 return 0;
1362 err_exit:
1363 dev_err(&usbvision->dev->dev,
1364 "USBVision[%d]: video_register_device() failed\n",
1365 usbvision->nr);
1366 usbvision_unregister_video(usbvision);
1367 return -1;
1371 * usbvision_alloc()
1373 * This code allocates the struct usb_usbvision.
1374 * It is filled with default values.
1376 * Returns NULL on error, a pointer to usb_usbvision else.
1379 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1380 struct usb_interface *intf)
1382 struct usb_usbvision *usbvision;
1384 usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL);
1385 if (usbvision == NULL)
1386 return NULL;
1388 usbvision->dev = dev;
1389 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1390 goto err_free;
1392 mutex_init(&usbvision->v4l2_lock);
1394 /* prepare control urb for control messages during interrupts */
1395 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1396 if (usbvision->ctrl_urb == NULL)
1397 goto err_unreg;
1398 init_waitqueue_head(&usbvision->ctrl_urb_wq);
1400 usbvision_init_power_off_timer(usbvision);
1402 return usbvision;
1404 err_unreg:
1405 v4l2_device_unregister(&usbvision->v4l2_dev);
1406 err_free:
1407 kfree(usbvision);
1408 return NULL;
1412 * usbvision_release()
1414 * This code does final release of struct usb_usbvision. This happens
1415 * after the device is disconnected -and- all clients closed their files.
1418 static void usbvision_release(struct usb_usbvision *usbvision)
1420 PDEBUG(DBG_PROBE, "");
1422 usbvision_reset_power_off_timer(usbvision);
1424 usbvision->initialized = 0;
1426 usbvision_remove_sysfs(usbvision->vdev);
1427 usbvision_unregister_video(usbvision);
1428 kfree(usbvision->alt_max_pkt_size);
1430 usb_free_urb(usbvision->ctrl_urb);
1432 v4l2_device_unregister(&usbvision->v4l2_dev);
1433 kfree(usbvision);
1435 PDEBUG(DBG_PROBE, "success");
1439 /*********************** usb interface **********************************/
1441 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1443 int model;
1445 if (usbvision == NULL)
1446 return;
1448 model = usbvision->dev_model;
1449 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1451 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1452 usbvision->vin_reg2_preset =
1453 usbvision_device_data[usbvision->dev_model].vin_reg2;
1454 } else {
1455 usbvision->vin_reg2_preset = 0;
1458 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1460 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1461 usbvision->ctl_input = 0;
1463 /* This should be here to make i2c clients to be able to register */
1464 /* first switch off audio */
1465 if (usbvision_device_data[model].audio_channels > 0)
1466 usbvision_audio_off(usbvision);
1467 if (!power_on_at_open) {
1468 /* and then power up the noisy tuner */
1469 usbvision_power_on(usbvision);
1470 usbvision_i2c_register(usbvision);
1475 * usbvision_probe()
1477 * This procedure queries device descriptor and accepts the interface
1478 * if it looks like USBVISION video device
1481 static int __devinit usbvision_probe(struct usb_interface *intf,
1482 const struct usb_device_id *devid)
1484 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1485 struct usb_interface *uif;
1486 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1487 const struct usb_host_interface *interface;
1488 struct usb_usbvision *usbvision = NULL;
1489 const struct usb_endpoint_descriptor *endpoint;
1490 int model, i, ret;
1492 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1493 dev->descriptor.idVendor,
1494 dev->descriptor.idProduct, ifnum);
1496 model = devid->driver_info;
1497 if (model < 0 || model >= usbvision_device_data_size) {
1498 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1499 ret = -ENODEV;
1500 goto err_usb;
1502 printk(KERN_INFO "%s: %s found\n", __func__,
1503 usbvision_device_data[model].model_string);
1506 * this is a security check.
1507 * an exploit using an incorrect bInterfaceNumber is known
1509 if (ifnum >= USB_MAXINTERFACES || !dev->actconfig->interface[ifnum])
1510 return -ENODEV;
1512 if (usbvision_device_data[model].interface >= 0)
1513 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1514 else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1515 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1516 else {
1517 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1518 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1519 ret = -ENODEV;
1520 goto err_usb;
1523 if (interface->desc.bNumEndpoints < 2) {
1524 dev_err(&intf->dev, "interface %d has %d endpoints, but must"
1525 " have minimum 2\n", ifnum, interface->desc.bNumEndpoints);
1526 ret = -ENODEV;
1527 goto err_usb;
1529 endpoint = &interface->endpoint[1].desc;
1531 if (!usb_endpoint_xfer_isoc(endpoint)) {
1532 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1533 __func__, ifnum);
1534 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1535 __func__, endpoint->bmAttributes);
1536 ret = -ENODEV;
1537 goto err_usb;
1539 if (usb_endpoint_dir_out(endpoint)) {
1540 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1541 __func__, ifnum);
1542 ret = -ENODEV;
1543 goto err_usb;
1546 usbvision = usbvision_alloc(dev, intf);
1547 if (usbvision == NULL) {
1548 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1549 ret = -ENOMEM;
1550 goto err_usb;
1553 if (dev->descriptor.bNumConfigurations > 1)
1554 usbvision->bridge_type = BRIDGE_NT1004;
1555 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1556 usbvision->bridge_type = BRIDGE_NT1005;
1557 else
1558 usbvision->bridge_type = BRIDGE_NT1003;
1559 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1561 /* compute alternate max packet sizes */
1562 uif = dev->actconfig->interface[0];
1564 usbvision->num_alt = uif->num_altsetting;
1565 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1566 usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
1567 if (usbvision->alt_max_pkt_size == NULL) {
1568 dev_err(&intf->dev, "usbvision: out of memory!\n");
1569 ret = -ENOMEM;
1570 goto err_pkt;
1573 for (i = 0; i < usbvision->num_alt; i++) {
1574 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1575 wMaxPacketSize);
1576 usbvision->alt_max_pkt_size[i] =
1577 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1578 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1579 usbvision->alt_max_pkt_size[i]);
1583 usbvision->nr = usbvision_nr++;
1585 usbvision->have_tuner = usbvision_device_data[model].tuner;
1586 if (usbvision->have_tuner)
1587 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1589 usbvision->dev_model = model;
1590 usbvision->remove_pending = 0;
1591 usbvision->iface = ifnum;
1592 usbvision->iface_alt = 0;
1593 usbvision->video_endp = endpoint->bEndpointAddress;
1594 usbvision->isoc_packet_size = 0;
1595 usbvision->usb_bandwidth = 0;
1596 usbvision->user = 0;
1597 usbvision->streaming = stream_off;
1598 usbvision_configure_video(usbvision);
1599 usbvision_register_video(usbvision);
1601 usbvision_create_sysfs(usbvision->vdev);
1603 PDEBUG(DBG_PROBE, "success");
1604 return 0;
1606 err_pkt:
1607 usbvision_release(usbvision);
1608 err_usb:
1609 usb_put_dev(dev);
1610 return ret;
1615 * usbvision_disconnect()
1617 * This procedure stops all driver activity, deallocates interface-private
1618 * structure (pointed by 'ptr') and after that driver should be removable
1619 * with no ill consequences.
1622 static void __devexit usbvision_disconnect(struct usb_interface *intf)
1624 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1626 PDEBUG(DBG_PROBE, "");
1628 if (usbvision == NULL) {
1629 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1630 return;
1633 mutex_lock(&usbvision->v4l2_lock);
1635 /* At this time we ask to cancel outstanding URBs */
1636 usbvision_stop_isoc(usbvision);
1638 v4l2_device_disconnect(&usbvision->v4l2_dev);
1640 if (usbvision->power) {
1641 usbvision_i2c_unregister(usbvision);
1642 usbvision_power_off(usbvision);
1644 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1646 usb_put_dev(usbvision->dev);
1647 usbvision->dev = NULL; /* USB device is no more */
1649 mutex_unlock(&usbvision->v4l2_lock);
1651 if (usbvision->user) {
1652 printk(KERN_INFO "%s: In use, disconnect pending\n",
1653 __func__);
1654 wake_up_interruptible(&usbvision->wait_frame);
1655 wake_up_interruptible(&usbvision->wait_stream);
1656 } else {
1657 usbvision_release(usbvision);
1660 PDEBUG(DBG_PROBE, "success");
1663 static struct usb_driver usbvision_driver = {
1664 .name = "usbvision",
1665 .id_table = usbvision_table,
1666 .probe = usbvision_probe,
1667 .disconnect = __devexit_p(usbvision_disconnect),
1671 * usbvision_init()
1673 * This code is run to initialize the driver.
1676 static int __init usbvision_init(void)
1678 int err_code;
1680 PDEBUG(DBG_PROBE, "");
1682 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1683 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1684 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1686 /* disable planar mode support unless compression enabled */
1687 if (isoc_mode != ISOC_MODE_COMPRESS) {
1688 /* FIXME : not the right way to set supported flag */
1689 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1690 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1693 err_code = usb_register(&usbvision_driver);
1695 if (err_code == 0) {
1696 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1697 PDEBUG(DBG_PROBE, "success");
1699 return err_code;
1702 static void __exit usbvision_exit(void)
1704 PDEBUG(DBG_PROBE, "");
1706 usb_deregister(&usbvision_driver);
1707 PDEBUG(DBG_PROBE, "success");
1710 module_init(usbvision_init);
1711 module_exit(usbvision_exit);
1714 * Overrides for Emacs so that we follow Linus's tabbing style.
1715 * ---------------------------------------------------------------------------
1716 * Local variables:
1717 * c-basic-offset: 8
1718 * End: