1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * USB USBVISION Video device driver 0.9.10
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
7 * This module is part of usbvision driver project.
9 * Let's call the version 0.... until compression decoding is completely
12 * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
13 * It was based on USB CPiA driver written by Peter Pregler,
14 * Scott J. Bertin and Johannes Erdfelt
15 * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
16 * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
17 * Updates to driver completed by Dwaine P. Garden
20 * - use submit_urb for all setup packets
21 * - Fix memory settings for nt1004. It is 4 times as big as the
23 * - Add audio on endpoint 3 for nt1004 chip.
24 * Seems impossible, needs a codec interface. Which one?
25 * - Clean up the driver.
26 * - optimization for performance.
27 * - Add Videotext capability (VBI). Working on it.....
28 * - Check audio for other devices
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/timer.h>
34 #include <linux/slab.h>
36 #include <linux/highmem.h>
37 #include <linux/vmalloc.h>
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/spinlock.h>
42 #include <linux/videodev2.h>
43 #include <linux/i2c.h>
45 #include <media/i2c/saa7115.h>
46 #include <media/v4l2-common.h>
47 #include <media/v4l2-ioctl.h>
48 #include <media/v4l2-event.h>
49 #include <media/tuner.h>
51 #include <linux/workqueue.h>
53 #include "usbvision.h"
54 #include "usbvision-cards.h"
56 #define DRIVER_AUTHOR \
57 "Joerg Heckenbach <joerg@heckenbach-aw.de>, " \
58 "Dwaine Garden <DwaineGarden@rogers.com>"
59 #define DRIVER_NAME "usbvision"
60 #define DRIVER_ALIAS "USBVision"
61 #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
62 #define USBVISION_VERSION_STRING "0.9.11"
64 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
67 #ifdef USBVISION_DEBUG
68 #define PDEBUG(level, fmt, args...) { \
69 if (video_debug & (level)) \
70 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
71 __func__, __LINE__ , ## args); \
74 #define PDEBUG(level, fmt, args...) do {} while (0)
77 #define DBG_IO (1 << 1)
78 #define DBG_PROBE (1 << 2)
79 #define DBG_MMAP (1 << 3)
81 /* String operations */
82 #define rmspace(str) while (*str == ' ') str++;
83 #define goto2next(str) while (*str != ' ') str++; while (*str == ' ') str++;
86 /* sequential number of usbvision device */
87 static int usbvision_nr;
89 static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
90 { 1, 1, 8, V4L2_PIX_FMT_GREY },
91 { 1, 2, 16, V4L2_PIX_FMT_RGB565 },
92 { 1, 3, 24, V4L2_PIX_FMT_RGB24 },
93 { 1, 4, 32, V4L2_PIX_FMT_RGB32 },
94 { 1, 2, 16, V4L2_PIX_FMT_RGB555 },
95 { 1, 2, 16, V4L2_PIX_FMT_YUYV },
96 { 1, 2, 12, V4L2_PIX_FMT_YVU420 }, /* 1.5 ! */
97 { 1, 2, 16, V4L2_PIX_FMT_YUV422P }
100 /* Function prototypes */
101 static void usbvision_release(struct usb_usbvision *usbvision);
103 /* Default initialization of device driver parameters */
104 /* Set the default format for ISOC endpoint */
105 static int isoc_mode = ISOC_MODE_COMPRESS;
106 /* Set the default Debug Mode of the device driver */
107 static int video_debug;
108 /* Sequential Number of Video Device */
109 static int video_nr = -1;
110 /* Sequential Number of Radio Device */
111 static int radio_nr = -1;
113 /* Grab parameters for the device driver */
115 /* Showing parameters under SYSFS */
116 module_param(isoc_mode, int, 0444);
117 module_param(video_debug, int, 0444);
118 module_param(video_nr, int, 0444);
119 module_param(radio_nr, int, 0444);
121 MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
122 MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
123 MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
124 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
128 MODULE_AUTHOR(DRIVER_AUTHOR);
129 MODULE_DESCRIPTION(DRIVER_DESC);
130 MODULE_LICENSE("GPL");
131 MODULE_VERSION(USBVISION_VERSION_STRING);
132 MODULE_ALIAS(DRIVER_ALIAS);
135 /*****************************************************************************/
136 /* SYSFS Code - Copied from the stv680.c usb module. */
137 /* Device information is located at /sys/class/video4linux/video0 */
138 /* Device parameters information is located at /sys/module/usbvision */
139 /* Device USB Information is located at */
140 /* /sys/bus/usb/drivers/USBVision Video Grabber */
141 /*****************************************************************************/
143 #define YES_NO(x) ((x) ? "Yes" : "No")
145 static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
147 struct video_device *vdev = to_video_device(cd);
148 return video_get_drvdata(vdev);
151 static ssize_t show_version(struct device *cd,
152 struct device_attribute *attr, char *buf)
154 return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
156 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
158 static ssize_t show_model(struct device *cd,
159 struct device_attribute *attr, char *buf)
161 struct video_device *vdev = to_video_device(cd);
162 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
163 return sprintf(buf, "%s\n",
164 usbvision_device_data[usbvision->dev_model].model_string);
166 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
168 static ssize_t show_hue(struct device *cd,
169 struct device_attribute *attr, char *buf)
171 struct video_device *vdev = to_video_device(cd);
172 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
173 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
176 return sprintf(buf, "%d\n", val);
178 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
180 static ssize_t show_contrast(struct device *cd,
181 struct device_attribute *attr, char *buf)
183 struct video_device *vdev = to_video_device(cd);
184 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
185 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
188 return sprintf(buf, "%d\n", val);
190 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
192 static ssize_t show_brightness(struct device *cd,
193 struct device_attribute *attr, char *buf)
195 struct video_device *vdev = to_video_device(cd);
196 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
197 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
198 V4L2_CID_BRIGHTNESS));
200 return sprintf(buf, "%d\n", val);
202 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
204 static ssize_t show_saturation(struct device *cd,
205 struct device_attribute *attr, char *buf)
207 struct video_device *vdev = to_video_device(cd);
208 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
209 s32 val = v4l2_ctrl_g_ctrl(v4l2_ctrl_find(&usbvision->hdl,
210 V4L2_CID_SATURATION));
212 return sprintf(buf, "%d\n", val);
214 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
216 static ssize_t show_streaming(struct device *cd,
217 struct device_attribute *attr, char *buf)
219 struct video_device *vdev = to_video_device(cd);
220 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
221 return sprintf(buf, "%s\n",
222 YES_NO(usbvision->streaming == stream_on ? 1 : 0));
224 static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
226 static ssize_t show_compression(struct device *cd,
227 struct device_attribute *attr, char *buf)
229 struct video_device *vdev = to_video_device(cd);
230 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
231 return sprintf(buf, "%s\n",
232 YES_NO(usbvision->isoc_mode == ISOC_MODE_COMPRESS));
234 static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
236 static ssize_t show_device_bridge(struct device *cd,
237 struct device_attribute *attr, char *buf)
239 struct video_device *vdev = to_video_device(cd);
240 struct usb_usbvision *usbvision = video_get_drvdata(vdev);
241 return sprintf(buf, "%d\n", usbvision->bridge_type);
243 static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
245 static void usbvision_create_sysfs(struct video_device *vdev)
252 res = device_create_file(&vdev->dev, &dev_attr_version);
255 res = device_create_file(&vdev->dev, &dev_attr_model);
258 res = device_create_file(&vdev->dev, &dev_attr_hue);
261 res = device_create_file(&vdev->dev, &dev_attr_contrast);
264 res = device_create_file(&vdev->dev, &dev_attr_brightness);
267 res = device_create_file(&vdev->dev, &dev_attr_saturation);
270 res = device_create_file(&vdev->dev, &dev_attr_streaming);
273 res = device_create_file(&vdev->dev, &dev_attr_compression);
276 res = device_create_file(&vdev->dev, &dev_attr_bridge);
281 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
284 static void usbvision_remove_sysfs(struct video_device *vdev)
287 device_remove_file(&vdev->dev, &dev_attr_version);
288 device_remove_file(&vdev->dev, &dev_attr_model);
289 device_remove_file(&vdev->dev, &dev_attr_hue);
290 device_remove_file(&vdev->dev, &dev_attr_contrast);
291 device_remove_file(&vdev->dev, &dev_attr_brightness);
292 device_remove_file(&vdev->dev, &dev_attr_saturation);
293 device_remove_file(&vdev->dev, &dev_attr_streaming);
294 device_remove_file(&vdev->dev, &dev_attr_compression);
295 device_remove_file(&vdev->dev, &dev_attr_bridge);
302 * This is part of Video 4 Linux API. The driver can be opened by one
303 * client only (checks internal counter 'usbvision->user'). The procedure
304 * then allocates buffers needed for video processing.
307 static int usbvision_v4l2_open(struct file *file)
309 struct usb_usbvision *usbvision = video_drvdata(file);
312 PDEBUG(DBG_IO, "open");
314 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
317 if (usbvision->remove_pending) {
321 if (usbvision->user) {
324 err_code = v4l2_fh_open(file);
328 /* Allocate memory for the scratch ring buffer */
329 err_code = usbvision_scratch_alloc(usbvision);
330 if (isoc_mode == ISOC_MODE_COMPRESS) {
331 /* Allocate intermediate decompression buffers
333 err_code = usbvision_decompress_alloc(usbvision);
336 /* Deallocate all buffers if trouble */
337 usbvision_scratch_free(usbvision);
338 usbvision_decompress_free(usbvision);
342 /* If so far no errors then we shall start the camera */
344 /* Send init sequence only once, it's large! */
345 if (!usbvision->initialized) {
347 setup_ok = usbvision_setup(usbvision, isoc_mode);
349 usbvision->initialized = 1;
355 usbvision_begin_streaming(usbvision);
356 err_code = usbvision_init_isoc(usbvision);
357 /* device must be initialized before isoc transfer */
358 usbvision_muxsel(usbvision, 0);
361 usbvision_empty_framequeues(usbvision);
367 mutex_unlock(&usbvision->v4l2_lock);
369 PDEBUG(DBG_IO, "success");
374 * usbvision_v4l2_close()
376 * This is part of Video 4 Linux API. The procedure
377 * stops streaming and deallocates all buffers that were earlier
378 * allocated in usbvision_v4l2_open().
381 static int usbvision_v4l2_close(struct file *file)
383 struct usb_usbvision *usbvision = video_drvdata(file);
386 PDEBUG(DBG_IO, "close");
388 mutex_lock(&usbvision->v4l2_lock);
389 usbvision_audio_off(usbvision);
390 usbvision_restart_isoc(usbvision);
391 usbvision_stop_isoc(usbvision);
393 usbvision_decompress_free(usbvision);
394 usbvision_frames_free(usbvision);
395 usbvision_empty_framequeues(usbvision);
396 usbvision_scratch_free(usbvision);
399 r = usbvision->remove_pending;
400 mutex_unlock(&usbvision->v4l2_lock);
403 printk(KERN_INFO "%s: Final disconnect\n", __func__);
404 usbvision_release(usbvision);
408 PDEBUG(DBG_IO, "success");
409 return v4l2_fh_release(file);
416 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
419 #ifdef CONFIG_VIDEO_ADV_DEBUG
420 static int vidioc_g_register(struct file *file, void *priv,
421 struct v4l2_dbg_register *reg)
423 struct usb_usbvision *usbvision = video_drvdata(file);
426 /* NT100x has a 8-bit register space */
427 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
429 dev_err(&usbvision->vdev.dev,
430 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
439 static int vidioc_s_register(struct file *file, void *priv,
440 const struct v4l2_dbg_register *reg)
442 struct usb_usbvision *usbvision = video_drvdata(file);
445 /* NT100x has a 8-bit register space */
446 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
448 dev_err(&usbvision->vdev.dev,
449 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
457 static int vidioc_querycap(struct file *file, void *priv,
458 struct v4l2_capability *vc)
460 struct usb_usbvision *usbvision = video_drvdata(file);
465 strscpy(vc->driver, "USBVision", sizeof(vc->driver));
467 usbvision_device_data[usbvision->dev_model].model_string,
469 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
470 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
471 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
472 if (usbvision_device_data[usbvision->dev_model].radio)
473 vc->capabilities |= V4L2_CAP_RADIO;
474 if (usbvision->have_tuner)
475 vc->capabilities |= V4L2_CAP_TUNER;
479 static int vidioc_enum_input(struct file *file, void *priv,
480 struct v4l2_input *vi)
482 struct usb_usbvision *usbvision = video_drvdata(file);
485 if (vi->index >= usbvision->video_inputs)
487 if (usbvision->have_tuner)
490 chan = vi->index + 1; /* skip Television string*/
492 /* Determine the requested input characteristics
493 specific for each usbvision card model */
496 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
497 strscpy(vi->name, "White Video Input", sizeof(vi->name));
499 strscpy(vi->name, "Television", sizeof(vi->name));
500 vi->type = V4L2_INPUT_TYPE_TUNER;
502 vi->std = USBVISION_NORMS;
506 vi->type = V4L2_INPUT_TYPE_CAMERA;
507 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
508 strscpy(vi->name, "Green Video Input", sizeof(vi->name));
510 strscpy(vi->name, "Composite Video Input",
512 vi->std = USBVISION_NORMS;
515 vi->type = V4L2_INPUT_TYPE_CAMERA;
516 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
517 strscpy(vi->name, "Yellow Video Input", sizeof(vi->name));
519 strscpy(vi->name, "S-Video Input", sizeof(vi->name));
520 vi->std = USBVISION_NORMS;
523 vi->type = V4L2_INPUT_TYPE_CAMERA;
524 strscpy(vi->name, "Red Video Input", sizeof(vi->name));
525 vi->std = USBVISION_NORMS;
531 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
533 struct usb_usbvision *usbvision = video_drvdata(file);
535 *input = usbvision->ctl_input;
539 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
541 struct usb_usbvision *usbvision = video_drvdata(file);
543 if (input >= usbvision->video_inputs)
546 usbvision_muxsel(usbvision, input);
547 usbvision_set_input(usbvision);
548 usbvision_set_output(usbvision,
550 usbvision->curheight);
554 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
556 struct usb_usbvision *usbvision = video_drvdata(file);
558 usbvision->tvnorm_id = id;
560 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
561 /* propagate the change to the decoder */
562 usbvision_muxsel(usbvision, usbvision->ctl_input);
567 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
569 struct usb_usbvision *usbvision = video_drvdata(file);
571 *id = usbvision->tvnorm_id;
575 static int vidioc_g_tuner(struct file *file, void *priv,
576 struct v4l2_tuner *vt)
578 struct usb_usbvision *usbvision = video_drvdata(file);
580 if (vt->index) /* Only tuner 0 */
582 if (vt->type == V4L2_TUNER_RADIO)
583 strscpy(vt->name, "Radio", sizeof(vt->name));
585 strscpy(vt->name, "Television", sizeof(vt->name));
587 /* Let clients fill in the remainder of this struct */
588 call_all(usbvision, tuner, g_tuner, vt);
593 static int vidioc_s_tuner(struct file *file, void *priv,
594 const struct v4l2_tuner *vt)
596 struct usb_usbvision *usbvision = video_drvdata(file);
598 /* Only one tuner for now */
601 /* let clients handle this */
602 call_all(usbvision, tuner, s_tuner, vt);
607 static int vidioc_g_frequency(struct file *file, void *priv,
608 struct v4l2_frequency *freq)
610 struct usb_usbvision *usbvision = video_drvdata(file);
615 if (freq->type == V4L2_TUNER_RADIO)
616 freq->frequency = usbvision->radio_freq;
618 freq->frequency = usbvision->tv_freq;
623 static int vidioc_s_frequency(struct file *file, void *priv,
624 const struct v4l2_frequency *freq)
626 struct usb_usbvision *usbvision = video_drvdata(file);
627 struct v4l2_frequency new_freq = *freq;
629 /* Only one tuner for now */
633 call_all(usbvision, tuner, s_frequency, freq);
634 call_all(usbvision, tuner, g_frequency, &new_freq);
635 if (freq->type == V4L2_TUNER_RADIO)
636 usbvision->radio_freq = new_freq.frequency;
638 usbvision->tv_freq = new_freq.frequency;
643 static int vidioc_reqbufs(struct file *file,
644 void *priv, struct v4l2_requestbuffers *vr)
646 struct usb_usbvision *usbvision = video_drvdata(file);
649 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
651 /* Check input validity:
652 the user must do a VIDEO CAPTURE and MMAP method. */
653 if (vr->memory != V4L2_MEMORY_MMAP)
656 if (usbvision->streaming == stream_on) {
657 ret = usbvision_stream_interrupt(usbvision);
662 usbvision_frames_free(usbvision);
663 usbvision_empty_framequeues(usbvision);
664 vr->count = usbvision_frames_alloc(usbvision, vr->count);
666 usbvision->cur_frame = NULL;
671 static int vidioc_querybuf(struct file *file,
672 void *priv, struct v4l2_buffer *vb)
674 struct usb_usbvision *usbvision = video_drvdata(file);
675 struct usbvision_frame *frame;
677 /* FIXME : must control
678 that buffers are mapped (VIDIOC_REQBUFS has been called) */
679 if (vb->index >= usbvision->num_frames)
681 /* Updating the corresponding frame state */
682 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
683 frame = &usbvision->frame[vb->index];
684 if (frame->grabstate >= frame_state_ready)
685 vb->flags |= V4L2_BUF_FLAG_QUEUED;
686 if (frame->grabstate >= frame_state_done)
687 vb->flags |= V4L2_BUF_FLAG_DONE;
688 if (frame->grabstate == frame_state_unused)
689 vb->flags |= V4L2_BUF_FLAG_MAPPED;
690 vb->memory = V4L2_MEMORY_MMAP;
692 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
694 vb->memory = V4L2_MEMORY_MMAP;
695 vb->field = V4L2_FIELD_NONE;
696 vb->length = usbvision->curwidth *
697 usbvision->curheight *
698 usbvision->palette.bytes_per_pixel;
699 v4l2_buffer_set_timestamp(vb, usbvision->frame[vb->index].ts);
700 vb->sequence = usbvision->frame[vb->index].sequence;
704 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
706 struct usb_usbvision *usbvision = video_drvdata(file);
707 struct usbvision_frame *frame;
708 unsigned long lock_flags;
710 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
711 if (vb->index >= usbvision->num_frames)
714 frame = &usbvision->frame[vb->index];
716 if (frame->grabstate != frame_state_unused)
719 /* Mark it as ready and enqueue frame */
720 frame->grabstate = frame_state_ready;
721 frame->scanstate = scan_state_scanning;
722 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
724 vb->flags &= ~V4L2_BUF_FLAG_DONE;
726 /* set v4l2_format index */
727 frame->v4l2_format = usbvision->palette;
729 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
730 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
731 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
736 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
738 struct usb_usbvision *usbvision = video_drvdata(file);
740 struct usbvision_frame *f;
741 unsigned long lock_flags;
743 if (list_empty(&(usbvision->outqueue))) {
744 if (usbvision->streaming == stream_idle)
746 ret = wait_event_interruptible
747 (usbvision->wait_frame,
748 !list_empty(&(usbvision->outqueue)));
753 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
754 f = list_entry(usbvision->outqueue.next,
755 struct usbvision_frame, frame);
756 list_del(usbvision->outqueue.next);
757 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
759 f->grabstate = frame_state_unused;
761 vb->memory = V4L2_MEMORY_MMAP;
762 vb->flags = V4L2_BUF_FLAG_MAPPED |
763 V4L2_BUF_FLAG_QUEUED |
765 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
766 vb->index = f->index;
767 vb->sequence = f->sequence;
768 v4l2_buffer_set_timestamp(vb, f->ts);
769 vb->field = V4L2_FIELD_NONE;
770 vb->bytesused = f->scanlength;
775 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
777 struct usb_usbvision *usbvision = video_drvdata(file);
779 usbvision->streaming = stream_on;
780 call_all(usbvision, video, s_stream, 1);
785 static int vidioc_streamoff(struct file *file,
786 void *priv, enum v4l2_buf_type type)
788 struct usb_usbvision *usbvision = video_drvdata(file);
790 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
793 if (usbvision->streaming == stream_on) {
794 usbvision_stream_interrupt(usbvision);
795 /* Stop all video streamings */
796 call_all(usbvision, video, s_stream, 0);
798 usbvision_empty_framequeues(usbvision);
803 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
804 struct v4l2_fmtdesc *vfd)
806 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
808 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
812 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
813 struct v4l2_format *vf)
815 struct usb_usbvision *usbvision = video_drvdata(file);
816 vf->fmt.pix.width = usbvision->curwidth;
817 vf->fmt.pix.height = usbvision->curheight;
818 vf->fmt.pix.pixelformat = usbvision->palette.format;
819 vf->fmt.pix.bytesperline =
820 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
821 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
822 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
823 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
828 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
829 struct v4l2_format *vf)
831 struct usb_usbvision *usbvision = video_drvdata(file);
834 /* Find requested format in available ones */
835 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
836 if (vf->fmt.pix.pixelformat ==
837 usbvision_v4l2_format[format_idx].format) {
838 usbvision->palette = usbvision_v4l2_format[format_idx];
843 if (format_idx == USBVISION_SUPPORTED_PALETTES)
845 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
846 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
848 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
849 usbvision->palette.bytes_per_pixel;
850 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
851 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
852 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
857 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
858 struct v4l2_format *vf)
860 struct usb_usbvision *usbvision = video_drvdata(file);
863 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
867 /* stop io in case it is already in progress */
868 if (usbvision->streaming == stream_on) {
869 ret = usbvision_stream_interrupt(usbvision);
873 usbvision_frames_free(usbvision);
874 usbvision_empty_framequeues(usbvision);
876 usbvision->cur_frame = NULL;
878 /* by now we are committed to the new data... */
879 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
884 static ssize_t usbvision_read(struct file *file, char __user *buf,
885 size_t count, loff_t *ppos)
887 struct usb_usbvision *usbvision = video_drvdata(file);
888 int noblock = file->f_flags & O_NONBLOCK;
889 unsigned long lock_flags;
891 struct usbvision_frame *frame;
893 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
894 (unsigned long)count, noblock);
896 if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
899 /* This entry point is compatible with the mmap routines
900 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
901 to get frames or call read on the device. */
902 if (!usbvision->num_frames) {
903 /* First, allocate some frames to work with
904 if this has not been done with VIDIOC_REQBUF */
905 usbvision_frames_free(usbvision);
906 usbvision_empty_framequeues(usbvision);
907 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
910 if (usbvision->streaming != stream_on) {
911 /* no stream is running, make it running ! */
912 usbvision->streaming = stream_on;
913 call_all(usbvision, video, s_stream, 1);
916 /* Then, enqueue as many frames as possible
917 (like a user of VIDIOC_QBUF would do) */
918 for (i = 0; i < usbvision->num_frames; i++) {
919 frame = &usbvision->frame[i];
920 if (frame->grabstate == frame_state_unused) {
921 /* Mark it as ready and enqueue frame */
922 frame->grabstate = frame_state_ready;
923 frame->scanstate = scan_state_scanning;
924 /* Accumulated in usbvision_parse_data() */
925 frame->scanlength = 0;
927 /* set v4l2_format index */
928 frame->v4l2_format = usbvision->palette;
930 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
931 list_add_tail(&frame->frame, &usbvision->inqueue);
932 spin_unlock_irqrestore(&usbvision->queue_lock,
937 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
938 if (list_empty(&(usbvision->outqueue))) {
942 ret = wait_event_interruptible
943 (usbvision->wait_frame,
944 !list_empty(&(usbvision->outqueue)));
949 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
950 frame = list_entry(usbvision->outqueue.next,
951 struct usbvision_frame, frame);
952 list_del(usbvision->outqueue.next);
953 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
955 /* An error returns an empty frame */
956 if (frame->grabstate == frame_state_error) {
957 frame->bytes_read = 0;
961 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
963 frame->index, frame->bytes_read, frame->scanlength);
965 /* copy bytes to user space; we allow for partials reads */
966 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
967 count = frame->scanlength - frame->bytes_read;
969 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
972 frame->bytes_read += count;
973 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
975 (unsigned long)count, frame->bytes_read);
979 * For now, forget the frame if it has not been read in one shot.
981 frame->bytes_read = 0;
983 /* Mark it as available to be used again. */
984 frame->grabstate = frame_state_unused;
989 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
990 size_t count, loff_t *ppos)
992 struct usb_usbvision *usbvision = video_drvdata(file);
995 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
997 res = usbvision_read(file, buf, count, ppos);
998 mutex_unlock(&usbvision->v4l2_lock);
1002 static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1004 unsigned long size = vma->vm_end - vma->vm_start,
1005 start = vma->vm_start;
1008 struct usb_usbvision *usbvision = video_drvdata(file);
1010 PDEBUG(DBG_MMAP, "mmap");
1012 if (!USBVISION_IS_OPERATIONAL(usbvision))
1015 if (!(vma->vm_flags & VM_WRITE) ||
1016 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1020 for (i = 0; i < usbvision->num_frames; i++) {
1021 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1025 if (i == usbvision->num_frames) {
1027 "mmap: user supplied mapping address is out of range");
1031 /* VM_IO is eventually going to replace PageReserved altogether */
1032 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1034 pos = usbvision->frame[i].data;
1036 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1037 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1048 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1050 struct usb_usbvision *usbvision = video_drvdata(file);
1053 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1054 return -ERESTARTSYS;
1055 res = usbvision_mmap(file, vma);
1056 mutex_unlock(&usbvision->v4l2_lock);
1061 * Here comes the stuff for radio on usbvision based devices
1064 static int usbvision_radio_open(struct file *file)
1066 struct usb_usbvision *usbvision = video_drvdata(file);
1069 PDEBUG(DBG_IO, "%s:", __func__);
1071 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1072 return -ERESTARTSYS;
1074 if (usbvision->remove_pending) {
1078 err_code = v4l2_fh_open(file);
1081 if (usbvision->user) {
1082 dev_err(&usbvision->rdev.dev,
1083 "%s: Someone tried to open an already opened USBVision Radio!\n",
1087 /* Alternate interface 1 is is the biggest frame size */
1088 err_code = usbvision_set_alternate(usbvision);
1090 usbvision->last_error = err_code;
1095 /* If so far no errors then we shall start the radio */
1096 usbvision->radio = 1;
1097 call_all(usbvision, tuner, s_radio);
1098 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1102 mutex_unlock(&usbvision->v4l2_lock);
1107 static int usbvision_radio_close(struct file *file)
1109 struct usb_usbvision *usbvision = video_drvdata(file);
1114 mutex_lock(&usbvision->v4l2_lock);
1115 /* Set packet size to 0 */
1116 usbvision->iface_alt = 0;
1118 usb_set_interface(usbvision->dev, usbvision->iface,
1119 usbvision->iface_alt);
1121 usbvision_audio_off(usbvision);
1122 usbvision->radio = 0;
1124 r = usbvision->remove_pending;
1125 mutex_unlock(&usbvision->v4l2_lock);
1128 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1129 v4l2_fh_release(file);
1130 usbvision_release(usbvision);
1134 PDEBUG(DBG_IO, "success");
1135 return v4l2_fh_release(file);
1138 /* Video registration stuff */
1140 /* Video template */
1141 static const struct v4l2_file_operations usbvision_fops = {
1142 .owner = THIS_MODULE,
1143 .open = usbvision_v4l2_open,
1144 .release = usbvision_v4l2_close,
1145 .read = usbvision_v4l2_read,
1146 .mmap = usbvision_v4l2_mmap,
1147 .unlocked_ioctl = video_ioctl2,
1150 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1151 .vidioc_querycap = vidioc_querycap,
1152 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1153 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1154 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1155 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1156 .vidioc_reqbufs = vidioc_reqbufs,
1157 .vidioc_querybuf = vidioc_querybuf,
1158 .vidioc_qbuf = vidioc_qbuf,
1159 .vidioc_dqbuf = vidioc_dqbuf,
1160 .vidioc_s_std = vidioc_s_std,
1161 .vidioc_g_std = vidioc_g_std,
1162 .vidioc_enum_input = vidioc_enum_input,
1163 .vidioc_g_input = vidioc_g_input,
1164 .vidioc_s_input = vidioc_s_input,
1165 .vidioc_streamon = vidioc_streamon,
1166 .vidioc_streamoff = vidioc_streamoff,
1167 .vidioc_g_tuner = vidioc_g_tuner,
1168 .vidioc_s_tuner = vidioc_s_tuner,
1169 .vidioc_g_frequency = vidioc_g_frequency,
1170 .vidioc_s_frequency = vidioc_s_frequency,
1171 .vidioc_log_status = v4l2_ctrl_log_status,
1172 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1173 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1174 #ifdef CONFIG_VIDEO_ADV_DEBUG
1175 .vidioc_g_register = vidioc_g_register,
1176 .vidioc_s_register = vidioc_s_register,
1180 static struct video_device usbvision_video_template = {
1181 .fops = &usbvision_fops,
1182 .ioctl_ops = &usbvision_ioctl_ops,
1183 .name = "usbvision-video",
1184 .release = video_device_release_empty,
1185 .tvnorms = USBVISION_NORMS,
1189 /* Radio template */
1190 static const struct v4l2_file_operations usbvision_radio_fops = {
1191 .owner = THIS_MODULE,
1192 .open = usbvision_radio_open,
1193 .release = usbvision_radio_close,
1194 .poll = v4l2_ctrl_poll,
1195 .unlocked_ioctl = video_ioctl2,
1198 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1199 .vidioc_querycap = vidioc_querycap,
1200 .vidioc_g_tuner = vidioc_g_tuner,
1201 .vidioc_s_tuner = vidioc_s_tuner,
1202 .vidioc_g_frequency = vidioc_g_frequency,
1203 .vidioc_s_frequency = vidioc_s_frequency,
1204 .vidioc_log_status = v4l2_ctrl_log_status,
1205 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1206 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1209 static struct video_device usbvision_radio_template = {
1210 .fops = &usbvision_radio_fops,
1211 .name = "usbvision-radio",
1212 .release = video_device_release_empty,
1213 .ioctl_ops = &usbvision_radio_ioctl_ops,
1217 static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1218 struct video_device *vdev,
1219 const struct video_device *vdev_template,
1222 struct usb_device *usb_dev = usbvision->dev;
1225 dev_err(&usbvision->dev->dev,
1226 "%s: usbvision->dev is not set\n", __func__);
1230 *vdev = *vdev_template;
1231 vdev->lock = &usbvision->v4l2_lock;
1232 vdev->v4l2_dev = &usbvision->v4l2_dev;
1233 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1234 video_set_drvdata(vdev, usbvision);
1237 /* unregister video4linux devices */
1238 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1241 if (video_is_registered(&usbvision->rdev)) {
1242 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1243 video_device_node_name(&usbvision->rdev));
1244 video_unregister_device(&usbvision->rdev);
1248 if (video_is_registered(&usbvision->vdev)) {
1249 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1250 video_device_node_name(&usbvision->vdev));
1251 video_unregister_device(&usbvision->vdev);
1255 /* register video4linux devices */
1256 static int usbvision_register_video(struct usb_usbvision *usbvision)
1261 usbvision_vdev_init(usbvision, &usbvision->vdev,
1262 &usbvision_video_template, "USBVision Video");
1263 if (!usbvision->have_tuner) {
1264 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1265 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1266 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1267 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1269 usbvision->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1270 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1271 if (usbvision->have_tuner)
1272 usbvision->vdev.device_caps |= V4L2_CAP_TUNER;
1274 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1276 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1277 usbvision->nr, video_device_node_name(&usbvision->vdev));
1280 if (usbvision_device_data[usbvision->dev_model].radio) {
1281 /* usbvision has radio */
1282 usbvision_vdev_init(usbvision, &usbvision->rdev,
1283 &usbvision_radio_template, "USBVision Radio");
1284 usbvision->rdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1285 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1287 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1288 usbvision->nr, video_device_node_name(&usbvision->rdev));
1294 dev_err(&usbvision->dev->dev,
1295 "USBVision[%d]: video_register_device() failed\n",
1297 usbvision_unregister_video(usbvision);
1304 * This code allocates the struct usb_usbvision.
1305 * It is filled with default values.
1307 * Returns NULL on error, a pointer to usb_usbvision else.
1310 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1311 struct usb_interface *intf)
1313 struct usb_usbvision *usbvision;
1315 usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1319 usbvision->dev = dev;
1320 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1323 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1325 usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
1326 mutex_init(&usbvision->v4l2_lock);
1328 /* prepare control urb for control messages during interrupts */
1329 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1330 if (!usbvision->ctrl_urb)
1336 v4l2_ctrl_handler_free(&usbvision->hdl);
1337 v4l2_device_unregister(&usbvision->v4l2_dev);
1344 * usbvision_release()
1346 * This code does final release of struct usb_usbvision. This happens
1347 * after the device is disconnected -and- all clients closed their files.
1350 static void usbvision_release(struct usb_usbvision *usbvision)
1352 PDEBUG(DBG_PROBE, "");
1354 usbvision->initialized = 0;
1356 usbvision_remove_sysfs(&usbvision->vdev);
1357 usbvision_unregister_video(usbvision);
1358 kfree(usbvision->alt_max_pkt_size);
1360 usb_free_urb(usbvision->ctrl_urb);
1362 v4l2_ctrl_handler_free(&usbvision->hdl);
1363 v4l2_device_unregister(&usbvision->v4l2_dev);
1366 PDEBUG(DBG_PROBE, "success");
1370 /*********************** usb interface **********************************/
1372 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1379 model = usbvision->dev_model;
1380 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1382 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1383 usbvision->vin_reg2_preset =
1384 usbvision_device_data[usbvision->dev_model].vin_reg2;
1386 usbvision->vin_reg2_preset = 0;
1389 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1390 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1391 usbvision->ctl_input = 0;
1392 usbvision->radio_freq = 87.5 * 16000;
1393 usbvision->tv_freq = 400 * 16;
1395 /* This should be here to make i2c clients to be able to register */
1396 /* first switch off audio */
1397 if (usbvision_device_data[model].audio_channels > 0)
1398 usbvision_audio_off(usbvision);
1399 /* and then power up the tuner */
1400 usbvision_power_on(usbvision);
1401 usbvision_i2c_register(usbvision);
1407 * This procedure queries device descriptor and accepts the interface
1408 * if it looks like USBVISION video device
1411 static int usbvision_probe(struct usb_interface *intf,
1412 const struct usb_device_id *devid)
1414 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1415 struct usb_interface *uif;
1416 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1417 const struct usb_host_interface *interface;
1418 struct usb_usbvision *usbvision = NULL;
1419 const struct usb_endpoint_descriptor *endpoint;
1422 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1423 le16_to_cpu(dev->descriptor.idVendor),
1424 le16_to_cpu(dev->descriptor.idProduct), ifnum);
1426 model = devid->driver_info;
1427 if (model < 0 || model >= usbvision_device_data_size) {
1428 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1432 printk(KERN_INFO "%s: %s found\n", __func__,
1433 usbvision_device_data[model].model_string);
1435 if (usbvision_device_data[model].interface >= 0)
1436 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1437 else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1438 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1440 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1441 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1446 if (interface->desc.bNumEndpoints < 2) {
1447 dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
1448 ifnum, interface->desc.bNumEndpoints);
1452 endpoint = &interface->endpoint[1].desc;
1454 if (!usb_endpoint_xfer_isoc(endpoint)) {
1455 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1457 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1458 __func__, endpoint->bmAttributes);
1462 if (usb_endpoint_dir_out(endpoint)) {
1463 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1469 usbvision = usbvision_alloc(dev, intf);
1471 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1476 if (dev->descriptor.bNumConfigurations > 1)
1477 usbvision->bridge_type = BRIDGE_NT1004;
1478 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1479 usbvision->bridge_type = BRIDGE_NT1005;
1481 usbvision->bridge_type = BRIDGE_NT1003;
1482 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1484 /* compute alternate max packet sizes */
1485 uif = dev->actconfig->interface[0];
1487 usbvision->num_alt = uif->num_altsetting;
1488 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1489 usbvision->alt_max_pkt_size = kmalloc_array(32, usbvision->num_alt,
1491 if (!usbvision->alt_max_pkt_size) {
1496 for (i = 0; i < usbvision->num_alt; i++) {
1499 if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1504 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1506 usbvision->alt_max_pkt_size[i] =
1507 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1508 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1509 usbvision->alt_max_pkt_size[i]);
1513 usbvision->nr = usbvision_nr++;
1515 spin_lock_init(&usbvision->queue_lock);
1516 init_waitqueue_head(&usbvision->wait_frame);
1517 init_waitqueue_head(&usbvision->wait_stream);
1519 usbvision->have_tuner = usbvision_device_data[model].tuner;
1520 if (usbvision->have_tuner)
1521 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1523 usbvision->dev_model = model;
1524 usbvision->remove_pending = 0;
1525 usbvision->iface = ifnum;
1526 usbvision->iface_alt = 0;
1527 usbvision->video_endp = endpoint->bEndpointAddress;
1528 usbvision->isoc_packet_size = 0;
1529 usbvision->usb_bandwidth = 0;
1530 usbvision->user = 0;
1531 usbvision->streaming = stream_off;
1532 usbvision_configure_video(usbvision);
1533 usbvision_register_video(usbvision);
1535 usbvision_create_sysfs(&usbvision->vdev);
1537 PDEBUG(DBG_PROBE, "success");
1541 usbvision_release(usbvision);
1549 * usbvision_disconnect()
1551 * This procedure stops all driver activity, deallocates interface-private
1552 * structure (pointed by 'ptr') and after that driver should be removable
1553 * with no ill consequences.
1556 static void usbvision_disconnect(struct usb_interface *intf)
1558 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1561 PDEBUG(DBG_PROBE, "");
1564 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1568 mutex_lock(&usbvision->v4l2_lock);
1570 /* At this time we ask to cancel outstanding URBs */
1571 usbvision_stop_isoc(usbvision);
1573 v4l2_device_disconnect(&usbvision->v4l2_dev);
1574 usbvision_i2c_unregister(usbvision);
1575 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1576 u = usbvision->user;
1578 usb_put_dev(usbvision->dev);
1579 usbvision->dev = NULL; /* USB device is no more */
1581 mutex_unlock(&usbvision->v4l2_lock);
1584 printk(KERN_INFO "%s: In use, disconnect pending\n",
1586 wake_up_interruptible(&usbvision->wait_frame);
1587 wake_up_interruptible(&usbvision->wait_stream);
1589 usbvision_release(usbvision);
1592 PDEBUG(DBG_PROBE, "success");
1595 static struct usb_driver usbvision_driver = {
1596 .name = "usbvision",
1597 .id_table = usbvision_table,
1598 .probe = usbvision_probe,
1599 .disconnect = usbvision_disconnect,
1605 * This code is run to initialize the driver.
1608 static int __init usbvision_init(void)
1612 PDEBUG(DBG_PROBE, "");
1614 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1615 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1616 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1618 /* disable planar mode support unless compression enabled */
1619 if (isoc_mode != ISOC_MODE_COMPRESS) {
1620 /* FIXME : not the right way to set supported flag */
1621 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1622 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1625 err_code = usb_register(&usbvision_driver);
1627 if (err_code == 0) {
1628 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1629 PDEBUG(DBG_PROBE, "success");
1634 static void __exit usbvision_exit(void)
1636 PDEBUG(DBG_PROBE, "");
1638 usb_deregister(&usbvision_driver);
1639 PDEBUG(DBG_PROBE, "success");
1642 module_init(usbvision_init);
1643 module_exit(usbvision_exit);