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 * Let's call the version 0.... until compression decoding is completely
23 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
24 * It was based on USB CPiA driver written by Peter Pregler,
25 * Scott J. Bertin and Johannes Erdfelt
26 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
27 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
28 * Updates to driver completed by Dwaine P. Garden
32 * - use submit_urb for all setup packets
33 * - Fix memory settings for nt1004. It is 4 times as big as the
35 * - Add audio on endpoint 3 for nt1004 chip.
36 * Seems impossible, needs a codec interface. Which one?
37 * - Clean up the driver.
38 * - optimization for performance.
39 * - Add Videotext capability (VBI). Working on it.....
40 * - Check audio for other devices
44 #include <linux/kernel.h>
45 #include <linux/list.h>
46 #include <linux/timer.h>
47 #include <linux/slab.h>
49 #include <linux/highmem.h>
50 #include <linux/vmalloc.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/spinlock.h>
55 #include <linux/videodev2.h>
56 #include <linux/i2c.h>
58 #include <media/i2c/saa7115.h>
59 #include <media/v4l2-common.h>
60 #include <media/v4l2-ioctl.h>
61 #include <media/v4l2-event.h>
62 #include <media/tuner.h>
64 #include <linux/workqueue.h>
66 #include "usbvision.h"
67 #include "usbvision-cards.h"
69 #define DRIVER_AUTHOR \
70 "Joerg Heckenbach <joerg@heckenbach-aw.de>, " \
71 "Dwaine Garden <DwaineGarden@rogers.com>"
72 #define DRIVER_NAME "usbvision"
73 #define DRIVER_ALIAS "USBVision"
74 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
75 #define DRIVER_LICENSE "GPL"
76 #define USBVISION_VERSION_STRING "0.9.11"
78 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
81 #ifdef USBVISION_DEBUG
82 #define PDEBUG(level, fmt, args...) { \
83 if (video_debug & (level)) \
84 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
85 __func__, __LINE__ , ## args); \
88 #define PDEBUG(level, fmt, args...) do {} while (0)
91 #define DBG_IO (1 << 1)
92 #define DBG_PROBE (1 << 2)
93 #define DBG_MMAP (1 << 3)
95 /* String operations */
96 #define rmspace(str) while (*str == ' ') str++;
97 #define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
100 /* sequential number of usbvision device */
101 static int usbvision_nr;
103 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
104 { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
105 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
106 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
107 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
108 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
109 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
110 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
111 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
114 /* Function prototypes */
115 static void usbvision_release(struct usb_usbvision *usbvision);
117 /* Default initialization of device driver parameters */
118 /* Set the default format for ISOC endpoint */
119 static int isoc_mode = ISOC_MODE_COMPRESS;
120 /* Set the default Debug Mode of the device driver */
121 static int video_debug;
122 /* Sequential Number of Video Device */
123 static int video_nr = -1;
124 /* Sequential Number of Radio Device */
125 static int radio_nr = -1;
127 /* Grab parameters for the device driver */
129 /* Showing parameters under SYSFS */
130 module_param(isoc_mode, int, 0444);
131 module_param(video_debug, int, 0444);
132 module_param(video_nr, int, 0444);
133 module_param(radio_nr, int, 0444);
135 MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
136 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
137 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
138 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
142 MODULE_AUTHOR(DRIVER_AUTHOR);
143 MODULE_DESCRIPTION(DRIVER_DESC);
144 MODULE_LICENSE(DRIVER_LICENSE);
145 MODULE_VERSION(USBVISION_VERSION_STRING);
146 MODULE_ALIAS(DRIVER_ALIAS);
149 /*****************************************************************************/
150 /* SYSFS Code - Copied from the stv680.c usb module. */
151 /* Device information is located at /sys/class/video4linux/video0 */
152 /* Device parameters information is located at /sys/module/usbvision */
153 /* Device USB Information is located at */
154 /* /sys/bus/usb/drivers/USBVision Video Grabber */
155 /*****************************************************************************/
157 #define YES_NO(x) ((x) ? "Yes" : "No")
159 static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
161 struct video_device *vdev = to_video_device(cd);
162 return video_get_drvdata(vdev);
165 static ssize_t show_version(struct device *cd,
166 struct device_attribute *attr, char *buf)
168 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
170 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
172 static ssize_t show_model(struct device *cd,
173 struct device_attribute *attr, char *buf)
175 struct video_device *vdev = to_video_device(cd);
176 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
177 return sprintf(buf, "%s\n",
178 usbvision_device_data[usbvision->dev_model].model_string);
180 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
182 static ssize_t show_hue(struct device *cd,
183 struct device_attribute *attr, char *buf)
185 struct video_device *vdev = to_video_device(cd);
186 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
187 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
190 return sprintf(buf, "%d\n", val);
192 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
194 static ssize_t show_contrast(struct device *cd,
195 struct device_attribute *attr, char *buf)
197 struct video_device *vdev = to_video_device(cd);
198 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
199 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
202 return sprintf(buf, "%d\n", val);
204 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
206 static ssize_t show_brightness(struct device *cd,
207 struct device_attribute *attr, char *buf)
209 struct video_device *vdev = to_video_device(cd);
210 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
211 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
212 V4L2_CID_BRIGHTNESS));
214 return sprintf(buf, "%d\n", val);
216 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
218 static ssize_t show_saturation(struct device *cd,
219 struct device_attribute *attr, char *buf)
221 struct video_device *vdev = to_video_device(cd);
222 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
223 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
224 V4L2_CID_SATURATION));
226 return sprintf(buf, "%d\n", val);
228 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
230 static ssize_t show_streaming(struct device *cd,
231 struct device_attribute *attr, char *buf)
233 struct video_device *vdev = to_video_device(cd);
234 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
235 return sprintf(buf, "%s\n",
236 YES_NO(usbvision->streaming == stream_on ? 1 : 0));
238 static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
240 static ssize_t show_compression(struct device *cd,
241 struct device_attribute *attr, char *buf)
243 struct video_device *vdev = to_video_device(cd);
244 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
245 return sprintf(buf, "%s\n",
246 YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
248 static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
250 static ssize_t show_device_bridge(struct device *cd,
251 struct device_attribute *attr, char *buf)
253 struct video_device *vdev = to_video_device(cd);
254 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
255 return sprintf(buf, "%d\n", usbvision->bridge_type);
257 static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
259 static void usbvision_create_sysfs(struct video_device *vdev)
266 res = device_create_file(&vdev->dev, &dev_attr_version);
269 res = device_create_file(&vdev->dev, &dev_attr_model);
272 res = device_create_file(&vdev->dev, &dev_attr_hue);
275 res = device_create_file(&vdev->dev, &dev_attr_contrast);
278 res = device_create_file(&vdev->dev, &dev_attr_brightness);
281 res = device_create_file(&vdev->dev, &dev_attr_saturation);
284 res = device_create_file(&vdev->dev, &dev_attr_streaming);
287 res = device_create_file(&vdev->dev, &dev_attr_compression);
290 res = device_create_file(&vdev->dev, &dev_attr_bridge);
295 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
298 static void usbvision_remove_sysfs(struct video_device *vdev)
301 device_remove_file(&vdev->dev, &dev_attr_version);
302 device_remove_file(&vdev->dev, &dev_attr_model);
303 device_remove_file(&vdev->dev, &dev_attr_hue);
304 device_remove_file(&vdev->dev, &dev_attr_contrast);
305 device_remove_file(&vdev->dev, &dev_attr_brightness);
306 device_remove_file(&vdev->dev, &dev_attr_saturation);
307 device_remove_file(&vdev->dev, &dev_attr_streaming);
308 device_remove_file(&vdev->dev, &dev_attr_compression);
309 device_remove_file(&vdev->dev, &dev_attr_bridge);
316 * This is part of Video 4 Linux API. The driver can be opened by one
317 * client only (checks internal counter 'usbvision->user'). The procedure
318 * then allocates buffers needed for video processing.
321 static int usbvision_v4l2_open(struct file *file)
323 struct usb_usbvision *usbvision = video_drvdata(file);
326 PDEBUG(DBG_IO, "open");
328 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
331 if (usbvision->remove_pending) {
335 if (usbvision->user) {
338 err_code = v4l2_fh_open(file);
342 /* Allocate memory for the scratch ring buffer */
343 err_code = usbvision_scratch_alloc(usbvision);
344 if (isoc_mode == ISOC_MODE_COMPRESS) {
345 /* Allocate intermediate decompression buffers
347 err_code = usbvision_decompress_alloc(usbvision);
350 /* Deallocate all buffers if trouble */
351 usbvision_scratch_free(usbvision);
352 usbvision_decompress_free(usbvision);
356 /* If so far no errors then we shall start the camera */
358 /* Send init sequence only once, it's large! */
359 if (!usbvision->initialized) {
361 setup_ok = usbvision_setup(usbvision, isoc_mode);
363 usbvision->initialized = 1;
369 usbvision_begin_streaming(usbvision);
370 err_code = usbvision_init_isoc(usbvision);
371 /* device must be initialized before isoc transfer */
372 usbvision_muxsel(usbvision, 0);
375 usbvision_empty_framequeues(usbvision);
381 mutex_unlock(&usbvision->v4l2_lock);
383 PDEBUG(DBG_IO, "success");
388 * usbvision_v4l2_close()
390 * This is part of Video 4 Linux API. The procedure
391 * stops streaming and deallocates all buffers that were earlier
392 * allocated in usbvision_v4l2_open().
395 static int usbvision_v4l2_close(struct file *file)
397 struct usb_usbvision *usbvision = video_drvdata(file);
400 PDEBUG(DBG_IO, "close");
402 mutex_lock(&usbvision->v4l2_lock);
403 usbvision_audio_off(usbvision);
404 usbvision_restart_isoc(usbvision);
405 usbvision_stop_isoc(usbvision);
407 usbvision_decompress_free(usbvision);
408 usbvision_frames_free(usbvision);
409 usbvision_empty_framequeues(usbvision);
410 usbvision_scratch_free(usbvision);
413 r = usbvision->remove_pending;
414 mutex_unlock(&usbvision->v4l2_lock);
417 printk(KERN_INFO "%s: Final disconnect\n", __func__);
418 usbvision_release(usbvision);
422 PDEBUG(DBG_IO, "success");
423 return v4l2_fh_release(file);
430 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
433 #ifdef CONFIG_VIDEO_ADV_DEBUG
434 static int vidioc_g_register(struct file *file, void *priv,
435 struct v4l2_dbg_register *reg)
437 struct usb_usbvision *usbvision = video_drvdata(file);
440 /* NT100x has a 8-bit register space */
441 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
443 dev_err(&usbvision->vdev.dev,
444 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
453 static int vidioc_s_register(struct file *file, void *priv,
454 const struct v4l2_dbg_register *reg)
456 struct usb_usbvision *usbvision = video_drvdata(file);
459 /* NT100x has a 8-bit register space */
460 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
462 dev_err(&usbvision->vdev.dev,
463 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
471 static int vidioc_querycap(struct file *file, void *priv,
472 struct v4l2_capability *vc)
474 struct usb_usbvision *usbvision = video_drvdata(file);
475 struct video_device *vdev = video_devdata(file);
477 strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
479 usbvision_device_data[usbvision->dev_model].model_string,
481 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
482 vc->device_caps = usbvision->have_tuner ? V4L2_CAP_TUNER : 0;
483 if (vdev->vfl_type == VFL_TYPE_GRABBER)
484 vc->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
485 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
487 vc->device_caps |= V4L2_CAP_RADIO;
489 vc->capabilities = vc->device_caps | V4L2_CAP_VIDEO_CAPTURE |
490 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
491 if (usbvision_device_data[usbvision->dev_model].radio)
492 vc->capabilities |= V4L2_CAP_RADIO;
496 static int vidioc_enum_input(struct file *file, void *priv,
497 struct v4l2_input *vi)
499 struct usb_usbvision *usbvision = video_drvdata(file);
502 if (vi->index >= usbvision->video_inputs)
504 if (usbvision->have_tuner)
507 chan = vi->index + 1; /* skip Television string*/
509 /* Determine the requested input characteristics
510 specific for each usbvision card model */
513 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
514 strcpy(vi->name, "White Video Input");
516 strcpy(vi->name, "Television");
517 vi->type = V4L2_INPUT_TYPE_TUNER;
519 vi->std = USBVISION_NORMS;
523 vi->type = V4L2_INPUT_TYPE_CAMERA;
524 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
525 strcpy(vi->name, "Green Video Input");
527 strcpy(vi->name, "Composite Video Input");
528 vi->std = USBVISION_NORMS;
531 vi->type = V4L2_INPUT_TYPE_CAMERA;
532 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
533 strcpy(vi->name, "Yellow Video Input");
535 strcpy(vi->name, "S-Video Input");
536 vi->std = USBVISION_NORMS;
539 vi->type = V4L2_INPUT_TYPE_CAMERA;
540 strcpy(vi->name, "Red Video Input");
541 vi->std = USBVISION_NORMS;
547 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
549 struct usb_usbvision *usbvision = video_drvdata(file);
551 *input = usbvision->ctl_input;
555 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
557 struct usb_usbvision *usbvision = video_drvdata(file);
559 if (input >= usbvision->video_inputs)
562 usbvision_muxsel(usbvision, input);
563 usbvision_set_input(usbvision);
564 usbvision_set_output(usbvision,
566 usbvision->curheight);
570 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
572 struct usb_usbvision *usbvision = video_drvdata(file);
574 usbvision->tvnorm_id = id;
576 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
577 /* propagate the change to the decoder */
578 usbvision_muxsel(usbvision, usbvision->ctl_input);
583 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
585 struct usb_usbvision *usbvision = video_drvdata(file);
587 *id = usbvision->tvnorm_id;
591 static int vidioc_g_tuner(struct file *file, void *priv,
592 struct v4l2_tuner *vt)
594 struct usb_usbvision *usbvision = video_drvdata(file);
596 if (vt->index) /* Only tuner 0 */
598 if (vt->type == V4L2_TUNER_RADIO)
599 strcpy(vt->name, "Radio");
601 strcpy(vt->name, "Television");
603 /* Let clients fill in the remainder of this struct */
604 call_all(usbvision, tuner, g_tuner, vt);
609 static int vidioc_s_tuner(struct file *file, void *priv,
610 const struct v4l2_tuner *vt)
612 struct usb_usbvision *usbvision = video_drvdata(file);
614 /* Only one tuner for now */
617 /* let clients handle this */
618 call_all(usbvision, tuner, s_tuner, vt);
623 static int vidioc_g_frequency(struct file *file, void *priv,
624 struct v4l2_frequency *freq)
626 struct usb_usbvision *usbvision = video_drvdata(file);
631 if (freq->type == V4L2_TUNER_RADIO)
632 freq->frequency = usbvision->radio_freq;
634 freq->frequency = usbvision->tv_freq;
639 static int vidioc_s_frequency(struct file *file, void *priv,
640 const struct v4l2_frequency *freq)
642 struct usb_usbvision *usbvision = video_drvdata(file);
643 struct v4l2_frequency new_freq = *freq;
645 /* Only one tuner for now */
649 call_all(usbvision, tuner, s_frequency, freq);
650 call_all(usbvision, tuner, g_frequency, &new_freq);
651 if (freq->type == V4L2_TUNER_RADIO)
652 usbvision->radio_freq = new_freq.frequency;
654 usbvision->tv_freq = new_freq.frequency;
659 static int vidioc_reqbufs(struct file *file,
660 void *priv, struct v4l2_requestbuffers *vr)
662 struct usb_usbvision *usbvision = video_drvdata(file);
665 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
667 /* Check input validity:
668 the user must do a VIDEO CAPTURE and MMAP method. */
669 if (vr->memory != V4L2_MEMORY_MMAP)
672 if (usbvision->streaming == stream_on) {
673 ret = usbvision_stream_interrupt(usbvision);
678 usbvision_frames_free(usbvision);
679 usbvision_empty_framequeues(usbvision);
680 vr->count = usbvision_frames_alloc(usbvision, vr->count);
682 usbvision->cur_frame = NULL;
687 static int vidioc_querybuf(struct file *file,
688 void *priv, struct v4l2_buffer *vb)
690 struct usb_usbvision *usbvision = video_drvdata(file);
691 struct usbvision_frame *frame;
693 /* FIXME : must control
694 that buffers are mapped (VIDIOC_REQBUFS has been called) */
695 if (vb->index >= usbvision->num_frames)
697 /* Updating the corresponding frame state */
698 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
699 frame = &usbvision->frame[vb->index];
700 if (frame->grabstate >= frame_state_ready)
701 vb->flags |= V4L2_BUF_FLAG_QUEUED;
702 if (frame->grabstate >= frame_state_done)
703 vb->flags |= V4L2_BUF_FLAG_DONE;
704 if (frame->grabstate == frame_state_unused)
705 vb->flags |= V4L2_BUF_FLAG_MAPPED;
706 vb->memory = V4L2_MEMORY_MMAP;
708 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
710 vb->memory = V4L2_MEMORY_MMAP;
711 vb->field = V4L2_FIELD_NONE;
712 vb->length = usbvision->curwidth *
713 usbvision->curheight *
714 usbvision->palette.bytes_per_pixel;
715 vb->timestamp = usbvision->frame[vb->index].timestamp;
716 vb->sequence = usbvision->frame[vb->index].sequence;
720 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
722 struct usb_usbvision *usbvision = video_drvdata(file);
723 struct usbvision_frame *frame;
724 unsigned long lock_flags;
726 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
727 if (vb->index >= usbvision->num_frames)
730 frame = &usbvision->frame[vb->index];
732 if (frame->grabstate != frame_state_unused)
735 /* Mark it as ready and enqueue frame */
736 frame->grabstate = frame_state_ready;
737 frame->scanstate = scan_state_scanning;
738 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
740 vb->flags &= ~V4L2_BUF_FLAG_DONE;
742 /* set v4l2_format index */
743 frame->v4l2_format = usbvision->palette;
745 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
746 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
747 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
752 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
754 struct usb_usbvision *usbvision = video_drvdata(file);
756 struct usbvision_frame *f;
757 unsigned long lock_flags;
759 if (list_empty(&(usbvision->outqueue))) {
760 if (usbvision->streaming == stream_idle)
762 ret = wait_event_interruptible
763 (usbvision->wait_frame,
764 !list_empty(&(usbvision->outqueue)));
769 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
770 f = list_entry(usbvision->outqueue.next,
771 struct usbvision_frame, frame);
772 list_del(usbvision->outqueue.next);
773 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
775 f->grabstate = frame_state_unused;
777 vb->memory = V4L2_MEMORY_MMAP;
778 vb->flags = V4L2_BUF_FLAG_MAPPED |
779 V4L2_BUF_FLAG_QUEUED |
781 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
782 vb->index = f->index;
783 vb->sequence = f->sequence;
784 vb->timestamp = f->timestamp;
785 vb->field = V4L2_FIELD_NONE;
786 vb->bytesused = f->scanlength;
791 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
793 struct usb_usbvision *usbvision = video_drvdata(file);
795 usbvision->streaming = stream_on;
796 call_all(usbvision, video, s_stream, 1);
801 static int vidioc_streamoff(struct file *file,
802 void *priv, enum v4l2_buf_type type)
804 struct usb_usbvision *usbvision = video_drvdata(file);
806 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
809 if (usbvision->streaming == stream_on) {
810 usbvision_stream_interrupt(usbvision);
811 /* Stop all video streamings */
812 call_all(usbvision, video, s_stream, 0);
814 usbvision_empty_framequeues(usbvision);
819 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
820 struct v4l2_fmtdesc *vfd)
822 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
824 strcpy(vfd->description, usbvision_v4l2_format[vfd->index].desc);
825 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
829 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
830 struct v4l2_format *vf)
832 struct usb_usbvision *usbvision = video_drvdata(file);
833 vf->fmt.pix.width = usbvision->curwidth;
834 vf->fmt.pix.height = usbvision->curheight;
835 vf->fmt.pix.pixelformat = usbvision->palette.format;
836 vf->fmt.pix.bytesperline =
837 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
838 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
839 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
840 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
845 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
846 struct v4l2_format *vf)
848 struct usb_usbvision *usbvision = video_drvdata(file);
851 /* Find requested format in available ones */
852 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
853 if (vf->fmt.pix.pixelformat ==
854 usbvision_v4l2_format[format_idx].format) {
855 usbvision->palette = usbvision_v4l2_format[format_idx];
860 if (format_idx == USBVISION_SUPPORTED_PALETTES)
862 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
863 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
865 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
866 usbvision->palette.bytes_per_pixel;
867 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
868 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
869 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
874 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_format *vf)
877 struct usb_usbvision *usbvision = video_drvdata(file);
880 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
884 /* stop io in case it is already in progress */
885 if (usbvision->streaming == stream_on) {
886 ret = usbvision_stream_interrupt(usbvision);
890 usbvision_frames_free(usbvision);
891 usbvision_empty_framequeues(usbvision);
893 usbvision->cur_frame = NULL;
895 /* by now we are committed to the new data... */
896 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
901 static ssize_t usbvision_read(struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
904 struct usb_usbvision *usbvision = video_drvdata(file);
905 int noblock = file->f_flags & O_NONBLOCK;
906 unsigned long lock_flags;
908 struct usbvision_frame *frame;
910 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
911 (unsigned long)count, noblock);
913 if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
916 /* This entry point is compatible with the mmap routines
917 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
918 to get frames or call read on the device. */
919 if (!usbvision->num_frames) {
920 /* First, allocate some frames to work with
921 if this has not been done with VIDIOC_REQBUF */
922 usbvision_frames_free(usbvision);
923 usbvision_empty_framequeues(usbvision);
924 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
927 if (usbvision->streaming != stream_on) {
928 /* no stream is running, make it running ! */
929 usbvision->streaming = stream_on;
930 call_all(usbvision, video, s_stream, 1);
933 /* Then, enqueue as many frames as possible
934 (like a user of VIDIOC_QBUF would do) */
935 for (i = 0; i < usbvision->num_frames; i++) {
936 frame = &usbvision->frame[i];
937 if (frame->grabstate == frame_state_unused) {
938 /* Mark it as ready and enqueue frame */
939 frame->grabstate = frame_state_ready;
940 frame->scanstate = scan_state_scanning;
941 /* Accumulated in usbvision_parse_data() */
942 frame->scanlength = 0;
944 /* set v4l2_format index */
945 frame->v4l2_format = usbvision->palette;
947 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
948 list_add_tail(&frame->frame, &usbvision->inqueue);
949 spin_unlock_irqrestore(&usbvision->queue_lock,
954 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
955 if (list_empty(&(usbvision->outqueue))) {
959 ret = wait_event_interruptible
960 (usbvision->wait_frame,
961 !list_empty(&(usbvision->outqueue)));
966 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
967 frame = list_entry(usbvision->outqueue.next,
968 struct usbvision_frame, frame);
969 list_del(usbvision->outqueue.next);
970 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
972 /* An error returns an empty frame */
973 if (frame->grabstate == frame_state_error) {
974 frame->bytes_read = 0;
978 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
980 frame->index, frame->bytes_read, frame->scanlength);
982 /* copy bytes to user space; we allow for partials reads */
983 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
984 count = frame->scanlength - frame->bytes_read;
986 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
989 frame->bytes_read += count;
990 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
992 (unsigned long)count, frame->bytes_read);
997 * For now, forget the frame if it has not been read in one shot.
999 frame->bytes_read = 0;
1001 /* Mark it as available to be used again. */
1002 frame->grabstate = frame_state_unused;
1004 if (frame->bytes_read >= frame->scanlength) {
1005 /* All data has been read */
1006 frame->bytes_read = 0;
1008 /* Mark it as available to be used again. */
1009 frame->grabstate = frame_state_unused;
1016 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
1017 size_t count, loff_t *ppos)
1019 struct usb_usbvision *usbvision = video_drvdata(file);
1022 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1023 return -ERESTARTSYS;
1024 res = usbvision_read(file, buf, count, ppos);
1025 mutex_unlock(&usbvision->v4l2_lock);
1029 static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1031 unsigned long size = vma->vm_end - vma->vm_start,
1032 start = vma->vm_start;
1035 struct usb_usbvision *usbvision = video_drvdata(file);
1037 PDEBUG(DBG_MMAP, "mmap");
1039 if (!USBVISION_IS_OPERATIONAL(usbvision))
1042 if (!(vma->vm_flags & VM_WRITE) ||
1043 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1047 for (i = 0; i < usbvision->num_frames; i++) {
1048 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1052 if (i == usbvision->num_frames) {
1054 "mmap: user supplied mapping address is out of range");
1058 /* VM_IO is eventually going to replace PageReserved altogether */
1059 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1061 pos = usbvision->frame[i].data;
1063 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1064 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1075 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1077 struct usb_usbvision *usbvision = video_drvdata(file);
1080 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1081 return -ERESTARTSYS;
1082 res = usbvision_mmap(file, vma);
1083 mutex_unlock(&usbvision->v4l2_lock);
1088 * Here comes the stuff for radio on usbvision based devices
1091 static int usbvision_radio_open(struct file *file)
1093 struct usb_usbvision *usbvision = video_drvdata(file);
1096 PDEBUG(DBG_IO, "%s:", __func__);
1098 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1099 return -ERESTARTSYS;
1101 if (usbvision->remove_pending) {
1105 err_code = v4l2_fh_open(file);
1108 if (usbvision->user) {
1109 dev_err(&usbvision->rdev.dev,
1110 "%s: Someone tried to open an already opened USBVision Radio!\n",
1114 /* Alternate interface 1 is is the biggest frame size */
1115 err_code = usbvision_set_alternate(usbvision);
1117 usbvision->last_error = err_code;
1122 /* If so far no errors then we shall start the radio */
1123 usbvision->radio = 1;
1124 call_all(usbvision, tuner, s_radio);
1125 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1129 mutex_unlock(&usbvision->v4l2_lock);
1134 static int usbvision_radio_close(struct file *file)
1136 struct usb_usbvision *usbvision = video_drvdata(file);
1141 mutex_lock(&usbvision->v4l2_lock);
1142 /* Set packet size to 0 */
1143 usbvision->iface_alt = 0;
1144 usb_set_interface(usbvision->dev, usbvision->iface,
1145 usbvision->iface_alt);
1147 usbvision_audio_off(usbvision);
1148 usbvision->radio = 0;
1150 r = usbvision->remove_pending;
1151 mutex_unlock(&usbvision->v4l2_lock);
1154 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1155 v4l2_fh_release(file);
1156 usbvision_release(usbvision);
1160 PDEBUG(DBG_IO, "success");
1161 return v4l2_fh_release(file);
1164 /* Video registration stuff */
1166 /* Video template */
1167 static const struct v4l2_file_operations usbvision_fops = {
1168 .owner = THIS_MODULE,
1169 .open = usbvision_v4l2_open,
1170 .release = usbvision_v4l2_close,
1171 .read = usbvision_v4l2_read,
1172 .mmap = usbvision_v4l2_mmap,
1173 .unlocked_ioctl = video_ioctl2,
1176 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1177 .vidioc_querycap = vidioc_querycap,
1178 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1179 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1180 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1181 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1182 .vidioc_reqbufs = vidioc_reqbufs,
1183 .vidioc_querybuf = vidioc_querybuf,
1184 .vidioc_qbuf = vidioc_qbuf,
1185 .vidioc_dqbuf = vidioc_dqbuf,
1186 .vidioc_s_std = vidioc_s_std,
1187 .vidioc_g_std = vidioc_g_std,
1188 .vidioc_enum_input = vidioc_enum_input,
1189 .vidioc_g_input = vidioc_g_input,
1190 .vidioc_s_input = vidioc_s_input,
1191 .vidioc_streamon = vidioc_streamon,
1192 .vidioc_streamoff = vidioc_streamoff,
1193 .vidioc_g_tuner = vidioc_g_tuner,
1194 .vidioc_s_tuner = vidioc_s_tuner,
1195 .vidioc_g_frequency = vidioc_g_frequency,
1196 .vidioc_s_frequency = vidioc_s_frequency,
1197 .vidioc_log_status = v4l2_ctrl_log_status,
1198 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1199 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1200 #ifdef CONFIG_VIDEO_ADV_DEBUG
1201 .vidioc_g_register = vidioc_g_register,
1202 .vidioc_s_register = vidioc_s_register,
1206 static struct video_device usbvision_video_template = {
1207 .fops = &usbvision_fops,
1208 .ioctl_ops = &usbvision_ioctl_ops,
1209 .name = "usbvision-video",
1210 .release = video_device_release_empty,
1211 .tvnorms = USBVISION_NORMS,
1215 /* Radio template */
1216 static const struct v4l2_file_operations usbvision_radio_fops = {
1217 .owner = THIS_MODULE,
1218 .open = usbvision_radio_open,
1219 .release = usbvision_radio_close,
1220 .poll = v4l2_ctrl_poll,
1221 .unlocked_ioctl = video_ioctl2,
1224 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1225 .vidioc_querycap = vidioc_querycap,
1226 .vidioc_g_tuner = vidioc_g_tuner,
1227 .vidioc_s_tuner = vidioc_s_tuner,
1228 .vidioc_g_frequency = vidioc_g_frequency,
1229 .vidioc_s_frequency = vidioc_s_frequency,
1230 .vidioc_log_status = v4l2_ctrl_log_status,
1231 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1232 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1235 static struct video_device usbvision_radio_template = {
1236 .fops = &usbvision_radio_fops,
1237 .name = "usbvision-radio",
1238 .release = video_device_release_empty,
1239 .ioctl_ops = &usbvision_radio_ioctl_ops,
1243 static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1244 struct video_device *vdev,
1245 const struct video_device *vdev_template,
1248 struct usb_device *usb_dev = usbvision->dev;
1251 dev_err(&usbvision->dev->dev,
1252 "%s: usbvision->dev is not set\n", __func__);
1256 *vdev = *vdev_template;
1257 vdev->lock = &usbvision->v4l2_lock;
1258 vdev->v4l2_dev = &usbvision->v4l2_dev;
1259 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1260 video_set_drvdata(vdev, usbvision);
1263 /* unregister video4linux devices */
1264 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1267 if (video_is_registered(&usbvision->rdev)) {
1268 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1269 video_device_node_name(&usbvision->rdev));
1270 video_unregister_device(&usbvision->rdev);
1274 if (video_is_registered(&usbvision->vdev)) {
1275 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1276 video_device_node_name(&usbvision->vdev));
1277 video_unregister_device(&usbvision->vdev);
1281 /* register video4linux devices */
1282 static int usbvision_register_video(struct usb_usbvision *usbvision)
1287 usbvision_vdev_init(usbvision, &usbvision->vdev,
1288 &usbvision_video_template, "USBVision Video");
1289 if (!usbvision->have_tuner) {
1290 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1291 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1292 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1293 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1295 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1297 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1298 usbvision->nr, video_device_node_name(&usbvision->vdev));
1301 if (usbvision_device_data[usbvision->dev_model].radio) {
1302 /* usbvision has radio */
1303 usbvision_vdev_init(usbvision, &usbvision->rdev,
1304 &usbvision_radio_template, "USBVision Radio");
1305 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1307 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1308 usbvision->nr, video_device_node_name(&usbvision->rdev));
1314 dev_err(&usbvision->dev->dev,
1315 "USBVision[%d]: video_register_device() failed\n",
1317 usbvision_unregister_video(usbvision);
1324 * This code allocates the struct usb_usbvision.
1325 * It is filled with default values.
1327 * Returns NULL on error, a pointer to usb_usbvision else.
1330 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1331 struct usb_interface *intf)
1333 struct usb_usbvision *usbvision;
1335 usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1339 usbvision->dev = dev;
1340 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1343 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1345 usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
1346 mutex_init(&usbvision->v4l2_lock);
1348 /* prepare control urb for control messages during interrupts */
1349 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1350 if (!usbvision->ctrl_urb)
1356 v4l2_ctrl_handler_free(&usbvision->hdl);
1357 v4l2_device_unregister(&usbvision->v4l2_dev);
1364 * usbvision_release()
1366 * This code does final release of struct usb_usbvision. This happens
1367 * after the device is disconnected -and- all clients closed their files.
1370 static void usbvision_release(struct usb_usbvision *usbvision)
1372 PDEBUG(DBG_PROBE, "");
1374 usbvision->initialized = 0;
1376 usbvision_remove_sysfs(&usbvision->vdev);
1377 usbvision_unregister_video(usbvision);
1378 kfree(usbvision->alt_max_pkt_size);
1380 usb_free_urb(usbvision->ctrl_urb);
1382 v4l2_ctrl_handler_free(&usbvision->hdl);
1383 v4l2_device_unregister(&usbvision->v4l2_dev);
1386 PDEBUG(DBG_PROBE, "success");
1390 /*********************** usb interface **********************************/
1392 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1399 model = usbvision->dev_model;
1400 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1402 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1403 usbvision->vin_reg2_preset =
1404 usbvision_device_data[usbvision->dev_model].vin_reg2;
1406 usbvision->vin_reg2_preset = 0;
1409 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1410 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1411 usbvision->ctl_input = 0;
1412 usbvision->radio_freq = 87.5 * 16000;
1413 usbvision->tv_freq = 400 * 16;
1415 /* This should be here to make i2c clients to be able to register */
1416 /* first switch off audio */
1417 if (usbvision_device_data[model].audio_channels > 0)
1418 usbvision_audio_off(usbvision);
1419 /* and then power up the tuner */
1420 usbvision_power_on(usbvision);
1421 usbvision_i2c_register(usbvision);
1427 * This procedure queries device descriptor and accepts the interface
1428 * if it looks like USBVISION video device
1431 static int usbvision_probe(struct usb_interface *intf,
1432 const struct usb_device_id *devid)
1434 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1435 struct usb_interface *uif;
1436 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1437 const struct usb_host_interface *interface;
1438 struct usb_usbvision *usbvision = NULL;
1439 const struct usb_endpoint_descriptor *endpoint;
1442 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1443 le16_to_cpu(dev->descriptor.idVendor),
1444 le16_to_cpu(dev->descriptor.idProduct), ifnum);
1446 model = devid->driver_info;
1447 if (model < 0 || model >= usbvision_device_data_size) {
1448 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1452 printk(KERN_INFO "%s: %s found\n", __func__,
1453 usbvision_device_data[model].model_string);
1455 if (usbvision_device_data[model].interface >= 0)
1456 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1457 else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1458 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1460 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1461 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1466 if (interface->desc.bNumEndpoints < 2) {
1467 dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
1468 ifnum, interface->desc.bNumEndpoints);
1472 endpoint = &interface->endpoint[1].desc;
1474 if (!usb_endpoint_xfer_isoc(endpoint)) {
1475 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1477 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1478 __func__, endpoint->bmAttributes);
1482 if (usb_endpoint_dir_out(endpoint)) {
1483 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1489 usbvision = usbvision_alloc(dev, intf);
1491 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1496 if (dev->descriptor.bNumConfigurations > 1)
1497 usbvision->bridge_type = BRIDGE_NT1004;
1498 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1499 usbvision->bridge_type = BRIDGE_NT1005;
1501 usbvision->bridge_type = BRIDGE_NT1003;
1502 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1504 /* compute alternate max packet sizes */
1505 uif = dev->actconfig->interface[0];
1507 usbvision->num_alt = uif->num_altsetting;
1508 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1509 usbvision->alt_max_pkt_size = kmalloc(32 * usbvision->num_alt, GFP_KERNEL);
1510 if (!usbvision->alt_max_pkt_size) {
1515 for (i = 0; i < usbvision->num_alt; i++) {
1518 if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1523 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1525 usbvision->alt_max_pkt_size[i] =
1526 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1527 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1528 usbvision->alt_max_pkt_size[i]);
1532 usbvision->nr = usbvision_nr++;
1534 spin_lock_init(&usbvision->queue_lock);
1535 init_waitqueue_head(&usbvision->wait_frame);
1536 init_waitqueue_head(&usbvision->wait_stream);
1538 usbvision->have_tuner = usbvision_device_data[model].tuner;
1539 if (usbvision->have_tuner)
1540 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1542 usbvision->dev_model = model;
1543 usbvision->remove_pending = 0;
1544 usbvision->iface = ifnum;
1545 usbvision->iface_alt = 0;
1546 usbvision->video_endp = endpoint->bEndpointAddress;
1547 usbvision->isoc_packet_size = 0;
1548 usbvision->usb_bandwidth = 0;
1549 usbvision->user = 0;
1550 usbvision->streaming = stream_off;
1551 usbvision_configure_video(usbvision);
1552 usbvision_register_video(usbvision);
1554 usbvision_create_sysfs(&usbvision->vdev);
1556 PDEBUG(DBG_PROBE, "success");
1560 usbvision_release(usbvision);
1568 * usbvision_disconnect()
1570 * This procedure stops all driver activity, deallocates interface-private
1571 * structure (pointed by 'ptr') and after that driver should be removable
1572 * with no ill consequences.
1575 static void usbvision_disconnect(struct usb_interface *intf)
1577 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1580 PDEBUG(DBG_PROBE, "");
1583 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1587 mutex_lock(&usbvision->v4l2_lock);
1589 /* At this time we ask to cancel outstanding URBs */
1590 usbvision_stop_isoc(usbvision);
1592 v4l2_device_disconnect(&usbvision->v4l2_dev);
1593 usbvision_i2c_unregister(usbvision);
1594 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1595 u = usbvision->user;
1597 usb_put_dev(usbvision->dev);
1598 usbvision->dev = NULL; /* USB device is no more */
1600 mutex_unlock(&usbvision->v4l2_lock);
1603 printk(KERN_INFO "%s: In use, disconnect pending\n",
1605 wake_up_interruptible(&usbvision->wait_frame);
1606 wake_up_interruptible(&usbvision->wait_stream);
1608 usbvision_release(usbvision);
1611 PDEBUG(DBG_PROBE, "success");
1614 static struct usb_driver usbvision_driver = {
1615 .name = "usbvision",
1616 .id_table = usbvision_table,
1617 .probe = usbvision_probe,
1618 .disconnect = usbvision_disconnect,
1624 * This code is run to initialize the driver.
1627 static int __init usbvision_init(void)
1631 PDEBUG(DBG_PROBE, "");
1633 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1634 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1635 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1637 /* disable planar mode support unless compression enabled */
1638 if (isoc_mode != ISOC_MODE_COMPRESS) {
1639 /* FIXME : not the right way to set supported flag */
1640 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1641 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1644 err_code = usb_register(&usbvision_driver);
1646 if (err_code == 0) {
1647 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1648 PDEBUG(DBG_PROBE, "success");
1653 static void __exit usbvision_exit(void)
1655 PDEBUG(DBG_PROBE, "");
1657 usb_deregister(&usbvision_driver);
1658 PDEBUG(DBG_PROBE, "success");
1661 module_init(usbvision_init);
1662 module_exit(usbvision_exit);