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 , "GREY" },
91 { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
92 { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
93 { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
94 { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
95 { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
96 { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, /* 1.5 ! */
97 { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "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->user) {
320 err_code = v4l2_fh_open(file);
324 /* Allocate memory for the scratch ring buffer */
325 err_code = usbvision_scratch_alloc(usbvision);
326 if (isoc_mode == ISOC_MODE_COMPRESS) {
327 /* Allocate intermediate decompression buffers
329 err_code = usbvision_decompress_alloc(usbvision);
332 /* Deallocate all buffers if trouble */
333 usbvision_scratch_free(usbvision);
334 usbvision_decompress_free(usbvision);
338 /* If so far no errors then we shall start the camera */
340 /* Send init sequence only once, it's large! */
341 if (!usbvision->initialized) {
343 setup_ok = usbvision_setup(usbvision, isoc_mode);
345 usbvision->initialized = 1;
351 usbvision_begin_streaming(usbvision);
352 err_code = usbvision_init_isoc(usbvision);
353 /* device must be initialized before isoc transfer */
354 usbvision_muxsel(usbvision, 0);
357 usbvision_empty_framequeues(usbvision);
363 mutex_unlock(&usbvision->v4l2_lock);
365 PDEBUG(DBG_IO, "success");
370 * usbvision_v4l2_close()
372 * This is part of Video 4 Linux API. The procedure
373 * stops streaming and deallocates all buffers that were earlier
374 * allocated in usbvision_v4l2_open().
377 static int usbvision_v4l2_close(struct file *file)
379 struct usb_usbvision *usbvision = video_drvdata(file);
381 PDEBUG(DBG_IO, "close");
383 mutex_lock(&usbvision->v4l2_lock);
384 usbvision_audio_off(usbvision);
385 usbvision_restart_isoc(usbvision);
386 usbvision_stop_isoc(usbvision);
388 usbvision_decompress_free(usbvision);
389 usbvision_frames_free(usbvision);
390 usbvision_empty_framequeues(usbvision);
391 usbvision_scratch_free(usbvision);
394 mutex_unlock(&usbvision->v4l2_lock);
396 if (usbvision->remove_pending) {
397 printk(KERN_INFO "%s: Final disconnect\n", __func__);
398 usbvision_release(usbvision);
402 PDEBUG(DBG_IO, "success");
403 return v4l2_fh_release(file);
410 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
413 #ifdef CONFIG_VIDEO_ADV_DEBUG
414 static int vidioc_g_register(struct file *file, void *priv,
415 struct v4l2_dbg_register *reg)
417 struct usb_usbvision *usbvision = video_drvdata(file);
420 /* NT100x has a 8-bit register space */
421 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
423 dev_err(&usbvision->vdev.dev,
424 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
433 static int vidioc_s_register(struct file *file, void *priv,
434 const struct v4l2_dbg_register *reg)
436 struct usb_usbvision *usbvision = video_drvdata(file);
439 /* NT100x has a 8-bit register space */
440 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
442 dev_err(&usbvision->vdev.dev,
443 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
451 static int vidioc_querycap(struct file *file, void *priv,
452 struct v4l2_capability *vc)
454 struct usb_usbvision *usbvision = video_drvdata(file);
455 struct video_device *vdev = video_devdata(file);
457 strscpy(vc->driver, "USBVision", sizeof(vc->driver));
459 usbvision_device_data[usbvision->dev_model].model_string,
461 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
462 vc->device_caps = usbvision->have_tuner ? V4L2_CAP_TUNER : 0;
463 if (vdev->vfl_type == VFL_TYPE_GRABBER)
464 vc->device_caps |= V4L2_CAP_VIDEO_CAPTURE |
465 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
467 vc->device_caps |= V4L2_CAP_RADIO;
469 vc->capabilities = vc->device_caps | V4L2_CAP_VIDEO_CAPTURE |
470 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
471 if (usbvision_device_data[usbvision->dev_model].radio)
472 vc->capabilities |= V4L2_CAP_RADIO;
476 static int vidioc_enum_input(struct file *file, void *priv,
477 struct v4l2_input *vi)
479 struct usb_usbvision *usbvision = video_drvdata(file);
482 if (vi->index >= usbvision->video_inputs)
484 if (usbvision->have_tuner)
487 chan = vi->index + 1; /* skip Television string*/
489 /* Determine the requested input characteristics
490 specific for each usbvision card model */
493 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
494 strscpy(vi->name, "White Video Input", sizeof(vi->name));
496 strscpy(vi->name, "Television", sizeof(vi->name));
497 vi->type = V4L2_INPUT_TYPE_TUNER;
499 vi->std = USBVISION_NORMS;
503 vi->type = V4L2_INPUT_TYPE_CAMERA;
504 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
505 strscpy(vi->name, "Green Video Input", sizeof(vi->name));
507 strscpy(vi->name, "Composite Video Input",
509 vi->std = USBVISION_NORMS;
512 vi->type = V4L2_INPUT_TYPE_CAMERA;
513 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
514 strscpy(vi->name, "Yellow Video Input", sizeof(vi->name));
516 strscpy(vi->name, "S-Video Input", sizeof(vi->name));
517 vi->std = USBVISION_NORMS;
520 vi->type = V4L2_INPUT_TYPE_CAMERA;
521 strscpy(vi->name, "Red Video Input", sizeof(vi->name));
522 vi->std = USBVISION_NORMS;
528 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
530 struct usb_usbvision *usbvision = video_drvdata(file);
532 *input = usbvision->ctl_input;
536 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
538 struct usb_usbvision *usbvision = video_drvdata(file);
540 if (input >= usbvision->video_inputs)
543 usbvision_muxsel(usbvision, input);
544 usbvision_set_input(usbvision);
545 usbvision_set_output(usbvision,
547 usbvision->curheight);
551 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
553 struct usb_usbvision *usbvision = video_drvdata(file);
555 usbvision->tvnorm_id = id;
557 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
558 /* propagate the change to the decoder */
559 usbvision_muxsel(usbvision, usbvision->ctl_input);
564 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
566 struct usb_usbvision *usbvision = video_drvdata(file);
568 *id = usbvision->tvnorm_id;
572 static int vidioc_g_tuner(struct file *file, void *priv,
573 struct v4l2_tuner *vt)
575 struct usb_usbvision *usbvision = video_drvdata(file);
577 if (vt->index) /* Only tuner 0 */
579 if (vt->type == V4L2_TUNER_RADIO)
580 strscpy(vt->name, "Radio", sizeof(vt->name));
582 strscpy(vt->name, "Television", sizeof(vt->name));
584 /* Let clients fill in the remainder of this struct */
585 call_all(usbvision, tuner, g_tuner, vt);
590 static int vidioc_s_tuner(struct file *file, void *priv,
591 const struct v4l2_tuner *vt)
593 struct usb_usbvision *usbvision = video_drvdata(file);
595 /* Only one tuner for now */
598 /* let clients handle this */
599 call_all(usbvision, tuner, s_tuner, vt);
604 static int vidioc_g_frequency(struct file *file, void *priv,
605 struct v4l2_frequency *freq)
607 struct usb_usbvision *usbvision = video_drvdata(file);
612 if (freq->type == V4L2_TUNER_RADIO)
613 freq->frequency = usbvision->radio_freq;
615 freq->frequency = usbvision->tv_freq;
620 static int vidioc_s_frequency(struct file *file, void *priv,
621 const struct v4l2_frequency *freq)
623 struct usb_usbvision *usbvision = video_drvdata(file);
624 struct v4l2_frequency new_freq = *freq;
626 /* Only one tuner for now */
630 call_all(usbvision, tuner, s_frequency, freq);
631 call_all(usbvision, tuner, g_frequency, &new_freq);
632 if (freq->type == V4L2_TUNER_RADIO)
633 usbvision->radio_freq = new_freq.frequency;
635 usbvision->tv_freq = new_freq.frequency;
640 static int vidioc_reqbufs(struct file *file,
641 void *priv, struct v4l2_requestbuffers *vr)
643 struct usb_usbvision *usbvision = video_drvdata(file);
646 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
648 /* Check input validity:
649 the user must do a VIDEO CAPTURE and MMAP method. */
650 if (vr->memory != V4L2_MEMORY_MMAP)
653 if (usbvision->streaming == stream_on) {
654 ret = usbvision_stream_interrupt(usbvision);
659 usbvision_frames_free(usbvision);
660 usbvision_empty_framequeues(usbvision);
661 vr->count = usbvision_frames_alloc(usbvision, vr->count);
663 usbvision->cur_frame = NULL;
668 static int vidioc_querybuf(struct file *file,
669 void *priv, struct v4l2_buffer *vb)
671 struct usb_usbvision *usbvision = video_drvdata(file);
672 struct usbvision_frame *frame;
674 /* FIXME : must control
675 that buffers are mapped (VIDIOC_REQBUFS has been called) */
676 if (vb->index >= usbvision->num_frames)
678 /* Updating the corresponding frame state */
679 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
680 frame = &usbvision->frame[vb->index];
681 if (frame->grabstate >= frame_state_ready)
682 vb->flags |= V4L2_BUF_FLAG_QUEUED;
683 if (frame->grabstate >= frame_state_done)
684 vb->flags |= V4L2_BUF_FLAG_DONE;
685 if (frame->grabstate == frame_state_unused)
686 vb->flags |= V4L2_BUF_FLAG_MAPPED;
687 vb->memory = V4L2_MEMORY_MMAP;
689 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
691 vb->memory = V4L2_MEMORY_MMAP;
692 vb->field = V4L2_FIELD_NONE;
693 vb->length = usbvision->curwidth *
694 usbvision->curheight *
695 usbvision->palette.bytes_per_pixel;
696 vb->timestamp = ns_to_timeval(usbvision->frame[vb->index].ts);
697 vb->sequence = usbvision->frame[vb->index].sequence;
701 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
703 struct usb_usbvision *usbvision = video_drvdata(file);
704 struct usbvision_frame *frame;
705 unsigned long lock_flags;
707 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
708 if (vb->index >= usbvision->num_frames)
711 frame = &usbvision->frame[vb->index];
713 if (frame->grabstate != frame_state_unused)
716 /* Mark it as ready and enqueue frame */
717 frame->grabstate = frame_state_ready;
718 frame->scanstate = scan_state_scanning;
719 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
721 vb->flags &= ~V4L2_BUF_FLAG_DONE;
723 /* set v4l2_format index */
724 frame->v4l2_format = usbvision->palette;
726 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
727 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
728 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
733 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
735 struct usb_usbvision *usbvision = video_drvdata(file);
737 struct usbvision_frame *f;
738 unsigned long lock_flags;
740 if (list_empty(&(usbvision->outqueue))) {
741 if (usbvision->streaming == stream_idle)
743 ret = wait_event_interruptible
744 (usbvision->wait_frame,
745 !list_empty(&(usbvision->outqueue)));
750 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
751 f = list_entry(usbvision->outqueue.next,
752 struct usbvision_frame, frame);
753 list_del(usbvision->outqueue.next);
754 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
756 f->grabstate = frame_state_unused;
758 vb->memory = V4L2_MEMORY_MMAP;
759 vb->flags = V4L2_BUF_FLAG_MAPPED |
760 V4L2_BUF_FLAG_QUEUED |
762 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
763 vb->index = f->index;
764 vb->sequence = f->sequence;
765 vb->timestamp = ns_to_timeval(f->ts);
766 vb->field = V4L2_FIELD_NONE;
767 vb->bytesused = f->scanlength;
772 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
774 struct usb_usbvision *usbvision = video_drvdata(file);
776 usbvision->streaming = stream_on;
777 call_all(usbvision, video, s_stream, 1);
782 static int vidioc_streamoff(struct file *file,
783 void *priv, enum v4l2_buf_type type)
785 struct usb_usbvision *usbvision = video_drvdata(file);
787 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
790 if (usbvision->streaming == stream_on) {
791 usbvision_stream_interrupt(usbvision);
792 /* Stop all video streamings */
793 call_all(usbvision, video, s_stream, 0);
795 usbvision_empty_framequeues(usbvision);
800 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
801 struct v4l2_fmtdesc *vfd)
803 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
805 strscpy(vfd->description, usbvision_v4l2_format[vfd->index].desc,
806 sizeof(vfd->description));
807 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
811 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
812 struct v4l2_format *vf)
814 struct usb_usbvision *usbvision = video_drvdata(file);
815 vf->fmt.pix.width = usbvision->curwidth;
816 vf->fmt.pix.height = usbvision->curheight;
817 vf->fmt.pix.pixelformat = usbvision->palette.format;
818 vf->fmt.pix.bytesperline =
819 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
820 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
821 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
822 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
827 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
828 struct v4l2_format *vf)
830 struct usb_usbvision *usbvision = video_drvdata(file);
833 /* Find requested format in available ones */
834 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
835 if (vf->fmt.pix.pixelformat ==
836 usbvision_v4l2_format[format_idx].format) {
837 usbvision->palette = usbvision_v4l2_format[format_idx];
842 if (format_idx == USBVISION_SUPPORTED_PALETTES)
844 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
845 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
847 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
848 usbvision->palette.bytes_per_pixel;
849 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
850 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
851 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
856 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
857 struct v4l2_format *vf)
859 struct usb_usbvision *usbvision = video_drvdata(file);
862 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
866 /* stop io in case it is already in progress */
867 if (usbvision->streaming == stream_on) {
868 ret = usbvision_stream_interrupt(usbvision);
872 usbvision_frames_free(usbvision);
873 usbvision_empty_framequeues(usbvision);
875 usbvision->cur_frame = NULL;
877 /* by now we are committed to the new data... */
878 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
883 static ssize_t usbvision_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
886 struct usb_usbvision *usbvision = video_drvdata(file);
887 int noblock = file->f_flags & O_NONBLOCK;
888 unsigned long lock_flags;
890 struct usbvision_frame *frame;
892 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
893 (unsigned long)count, noblock);
895 if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
898 /* This entry point is compatible with the mmap routines
899 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
900 to get frames or call read on the device. */
901 if (!usbvision->num_frames) {
902 /* First, allocate some frames to work with
903 if this has not been done with VIDIOC_REQBUF */
904 usbvision_frames_free(usbvision);
905 usbvision_empty_framequeues(usbvision);
906 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
909 if (usbvision->streaming != stream_on) {
910 /* no stream is running, make it running ! */
911 usbvision->streaming = stream_on;
912 call_all(usbvision, video, s_stream, 1);
915 /* Then, enqueue as many frames as possible
916 (like a user of VIDIOC_QBUF would do) */
917 for (i = 0; i < usbvision->num_frames; i++) {
918 frame = &usbvision->frame[i];
919 if (frame->grabstate == frame_state_unused) {
920 /* Mark it as ready and enqueue frame */
921 frame->grabstate = frame_state_ready;
922 frame->scanstate = scan_state_scanning;
923 /* Accumulated in usbvision_parse_data() */
924 frame->scanlength = 0;
926 /* set v4l2_format index */
927 frame->v4l2_format = usbvision->palette;
929 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
930 list_add_tail(&frame->frame, &usbvision->inqueue);
931 spin_unlock_irqrestore(&usbvision->queue_lock,
936 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
937 if (list_empty(&(usbvision->outqueue))) {
941 ret = wait_event_interruptible
942 (usbvision->wait_frame,
943 !list_empty(&(usbvision->outqueue)));
948 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
949 frame = list_entry(usbvision->outqueue.next,
950 struct usbvision_frame, frame);
951 list_del(usbvision->outqueue.next);
952 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
954 /* An error returns an empty frame */
955 if (frame->grabstate == frame_state_error) {
956 frame->bytes_read = 0;
960 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
962 frame->index, frame->bytes_read, frame->scanlength);
964 /* copy bytes to user space; we allow for partials reads */
965 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
966 count = frame->scanlength - frame->bytes_read;
968 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
971 frame->bytes_read += count;
972 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
974 (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;
986 if (frame->bytes_read >= frame->scanlength) {
987 /* All data has been read */
988 frame->bytes_read = 0;
990 /* Mark it as available to be used again. */
991 frame->grabstate = frame_state_unused;
998 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1001 struct usb_usbvision *usbvision = video_drvdata(file);
1004 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1005 return -ERESTARTSYS;
1006 res = usbvision_read(file, buf, count, ppos);
1007 mutex_unlock(&usbvision->v4l2_lock);
1011 static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1013 unsigned long size = vma->vm_end - vma->vm_start,
1014 start = vma->vm_start;
1017 struct usb_usbvision *usbvision = video_drvdata(file);
1019 PDEBUG(DBG_MMAP, "mmap");
1021 if (!USBVISION_IS_OPERATIONAL(usbvision))
1024 if (!(vma->vm_flags & VM_WRITE) ||
1025 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1029 for (i = 0; i < usbvision->num_frames; i++) {
1030 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1034 if (i == usbvision->num_frames) {
1036 "mmap: user supplied mapping address is out of range");
1040 /* VM_IO is eventually going to replace PageReserved altogether */
1041 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1043 pos = usbvision->frame[i].data;
1045 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1046 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1057 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1059 struct usb_usbvision *usbvision = video_drvdata(file);
1062 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1063 return -ERESTARTSYS;
1064 res = usbvision_mmap(file, vma);
1065 mutex_unlock(&usbvision->v4l2_lock);
1070 * Here comes the stuff for radio on usbvision based devices
1073 static int usbvision_radio_open(struct file *file)
1075 struct usb_usbvision *usbvision = video_drvdata(file);
1078 PDEBUG(DBG_IO, "%s:", __func__);
1080 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1081 return -ERESTARTSYS;
1082 err_code = v4l2_fh_open(file);
1085 if (usbvision->user) {
1086 dev_err(&usbvision->rdev.dev,
1087 "%s: Someone tried to open an already opened USBVision Radio!\n",
1091 /* Alternate interface 1 is is the biggest frame size */
1092 err_code = usbvision_set_alternate(usbvision);
1094 usbvision->last_error = err_code;
1099 /* If so far no errors then we shall start the radio */
1100 usbvision->radio = 1;
1101 call_all(usbvision, tuner, s_radio);
1102 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1106 mutex_unlock(&usbvision->v4l2_lock);
1111 static int usbvision_radio_close(struct file *file)
1113 struct usb_usbvision *usbvision = video_drvdata(file);
1117 mutex_lock(&usbvision->v4l2_lock);
1118 /* Set packet size to 0 */
1119 usbvision->iface_alt = 0;
1120 usb_set_interface(usbvision->dev, usbvision->iface,
1121 usbvision->iface_alt);
1123 usbvision_audio_off(usbvision);
1124 usbvision->radio = 0;
1126 mutex_unlock(&usbvision->v4l2_lock);
1128 if (usbvision->remove_pending) {
1129 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1130 v4l2_fh_release(file);
1131 usbvision_release(usbvision);
1135 PDEBUG(DBG_IO, "success");
1136 return v4l2_fh_release(file);
1139 /* Video registration stuff */
1141 /* Video template */
1142 static const struct v4l2_file_operations usbvision_fops = {
1143 .owner = THIS_MODULE,
1144 .open = usbvision_v4l2_open,
1145 .release = usbvision_v4l2_close,
1146 .read = usbvision_v4l2_read,
1147 .mmap = usbvision_v4l2_mmap,
1148 .unlocked_ioctl = video_ioctl2,
1151 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1152 .vidioc_querycap = vidioc_querycap,
1153 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1154 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1155 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1156 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1157 .vidioc_reqbufs = vidioc_reqbufs,
1158 .vidioc_querybuf = vidioc_querybuf,
1159 .vidioc_qbuf = vidioc_qbuf,
1160 .vidioc_dqbuf = vidioc_dqbuf,
1161 .vidioc_s_std = vidioc_s_std,
1162 .vidioc_g_std = vidioc_g_std,
1163 .vidioc_enum_input = vidioc_enum_input,
1164 .vidioc_g_input = vidioc_g_input,
1165 .vidioc_s_input = vidioc_s_input,
1166 .vidioc_streamon = vidioc_streamon,
1167 .vidioc_streamoff = vidioc_streamoff,
1168 .vidioc_g_tuner = vidioc_g_tuner,
1169 .vidioc_s_tuner = vidioc_s_tuner,
1170 .vidioc_g_frequency = vidioc_g_frequency,
1171 .vidioc_s_frequency = vidioc_s_frequency,
1172 .vidioc_log_status = v4l2_ctrl_log_status,
1173 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1174 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1175 #ifdef CONFIG_VIDEO_ADV_DEBUG
1176 .vidioc_g_register = vidioc_g_register,
1177 .vidioc_s_register = vidioc_s_register,
1181 static struct video_device usbvision_video_template = {
1182 .fops = &usbvision_fops,
1183 .ioctl_ops = &usbvision_ioctl_ops,
1184 .name = "usbvision-video",
1185 .release = video_device_release_empty,
1186 .tvnorms = USBVISION_NORMS,
1190 /* Radio template */
1191 static const struct v4l2_file_operations usbvision_radio_fops = {
1192 .owner = THIS_MODULE,
1193 .open = usbvision_radio_open,
1194 .release = usbvision_radio_close,
1195 .poll = v4l2_ctrl_poll,
1196 .unlocked_ioctl = video_ioctl2,
1199 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1200 .vidioc_querycap = vidioc_querycap,
1201 .vidioc_g_tuner = vidioc_g_tuner,
1202 .vidioc_s_tuner = vidioc_s_tuner,
1203 .vidioc_g_frequency = vidioc_g_frequency,
1204 .vidioc_s_frequency = vidioc_s_frequency,
1205 .vidioc_log_status = v4l2_ctrl_log_status,
1206 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1207 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1210 static struct video_device usbvision_radio_template = {
1211 .fops = &usbvision_radio_fops,
1212 .name = "usbvision-radio",
1213 .release = video_device_release_empty,
1214 .ioctl_ops = &usbvision_radio_ioctl_ops,
1218 static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1219 struct video_device *vdev,
1220 const struct video_device *vdev_template,
1223 struct usb_device *usb_dev = usbvision->dev;
1226 dev_err(&usbvision->dev->dev,
1227 "%s: usbvision->dev is not set\n", __func__);
1231 *vdev = *vdev_template;
1232 vdev->lock = &usbvision->v4l2_lock;
1233 vdev->v4l2_dev = &usbvision->v4l2_dev;
1234 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1235 video_set_drvdata(vdev, usbvision);
1238 /* unregister video4linux devices */
1239 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1242 if (video_is_registered(&usbvision->rdev)) {
1243 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1244 video_device_node_name(&usbvision->rdev));
1245 video_unregister_device(&usbvision->rdev);
1249 if (video_is_registered(&usbvision->vdev)) {
1250 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1251 video_device_node_name(&usbvision->vdev));
1252 video_unregister_device(&usbvision->vdev);
1256 /* register video4linux devices */
1257 static int usbvision_register_video(struct usb_usbvision *usbvision)
1262 usbvision_vdev_init(usbvision, &usbvision->vdev,
1263 &usbvision_video_template, "USBVision Video");
1264 if (!usbvision->have_tuner) {
1265 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1266 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1267 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1268 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1270 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1272 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1273 usbvision->nr, video_device_node_name(&usbvision->vdev));
1276 if (usbvision_device_data[usbvision->dev_model].radio) {
1277 /* usbvision has radio */
1278 usbvision_vdev_init(usbvision, &usbvision->rdev,
1279 &usbvision_radio_template, "USBVision Radio");
1280 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1282 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1283 usbvision->nr, video_device_node_name(&usbvision->rdev));
1289 dev_err(&usbvision->dev->dev,
1290 "USBVision[%d]: video_register_device() failed\n",
1292 usbvision_unregister_video(usbvision);
1299 * This code allocates the struct usb_usbvision.
1300 * It is filled with default values.
1302 * Returns NULL on error, a pointer to usb_usbvision else.
1305 static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1306 struct usb_interface *intf)
1308 struct usb_usbvision *usbvision;
1310 usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1314 usbvision->dev = dev;
1315 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1318 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1320 usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
1321 mutex_init(&usbvision->v4l2_lock);
1323 /* prepare control urb for control messages during interrupts */
1324 usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1325 if (!usbvision->ctrl_urb)
1331 v4l2_ctrl_handler_free(&usbvision->hdl);
1332 v4l2_device_unregister(&usbvision->v4l2_dev);
1339 * usbvision_release()
1341 * This code does final release of struct usb_usbvision. This happens
1342 * after the device is disconnected -and- all clients closed their files.
1345 static void usbvision_release(struct usb_usbvision *usbvision)
1347 PDEBUG(DBG_PROBE, "");
1349 usbvision->initialized = 0;
1351 usbvision_remove_sysfs(&usbvision->vdev);
1352 usbvision_unregister_video(usbvision);
1353 kfree(usbvision->alt_max_pkt_size);
1355 usb_free_urb(usbvision->ctrl_urb);
1357 v4l2_ctrl_handler_free(&usbvision->hdl);
1358 v4l2_device_unregister(&usbvision->v4l2_dev);
1361 PDEBUG(DBG_PROBE, "success");
1365 /*********************** usb interface **********************************/
1367 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1374 model = usbvision->dev_model;
1375 usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1377 if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1378 usbvision->vin_reg2_preset =
1379 usbvision_device_data[usbvision->dev_model].vin_reg2;
1381 usbvision->vin_reg2_preset = 0;
1384 usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1385 usbvision->video_inputs = usbvision_device_data[model].video_channels;
1386 usbvision->ctl_input = 0;
1387 usbvision->radio_freq = 87.5 * 16000;
1388 usbvision->tv_freq = 400 * 16;
1390 /* This should be here to make i2c clients to be able to register */
1391 /* first switch off audio */
1392 if (usbvision_device_data[model].audio_channels > 0)
1393 usbvision_audio_off(usbvision);
1394 /* and then power up the tuner */
1395 usbvision_power_on(usbvision);
1396 usbvision_i2c_register(usbvision);
1402 * This procedure queries device descriptor and accepts the interface
1403 * if it looks like USBVISION video device
1406 static int usbvision_probe(struct usb_interface *intf,
1407 const struct usb_device_id *devid)
1409 struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1410 struct usb_interface *uif;
1411 __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1412 const struct usb_host_interface *interface;
1413 struct usb_usbvision *usbvision = NULL;
1414 const struct usb_endpoint_descriptor *endpoint;
1417 PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1418 le16_to_cpu(dev->descriptor.idVendor),
1419 le16_to_cpu(dev->descriptor.idProduct), ifnum);
1421 model = devid->driver_info;
1422 if (model < 0 || model >= usbvision_device_data_size) {
1423 PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1427 printk(KERN_INFO "%s: %s found\n", __func__,
1428 usbvision_device_data[model].model_string);
1430 if (usbvision_device_data[model].interface >= 0)
1431 interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1432 else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1433 interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1435 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1436 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1441 if (interface->desc.bNumEndpoints < 2) {
1442 dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
1443 ifnum, interface->desc.bNumEndpoints);
1447 endpoint = &interface->endpoint[1].desc;
1449 if (!usb_endpoint_xfer_isoc(endpoint)) {
1450 dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1452 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1453 __func__, endpoint->bmAttributes);
1457 if (usb_endpoint_dir_out(endpoint)) {
1458 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1464 usbvision = usbvision_alloc(dev, intf);
1466 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1471 if (dev->descriptor.bNumConfigurations > 1)
1472 usbvision->bridge_type = BRIDGE_NT1004;
1473 else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1474 usbvision->bridge_type = BRIDGE_NT1005;
1476 usbvision->bridge_type = BRIDGE_NT1003;
1477 PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1479 /* compute alternate max packet sizes */
1480 uif = dev->actconfig->interface[0];
1482 usbvision->num_alt = uif->num_altsetting;
1483 PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1484 usbvision->alt_max_pkt_size = kmalloc_array(32, usbvision->num_alt,
1486 if (!usbvision->alt_max_pkt_size) {
1491 for (i = 0; i < usbvision->num_alt; i++) {
1494 if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1499 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1501 usbvision->alt_max_pkt_size[i] =
1502 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1503 PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1504 usbvision->alt_max_pkt_size[i]);
1508 usbvision->nr = usbvision_nr++;
1510 spin_lock_init(&usbvision->queue_lock);
1511 init_waitqueue_head(&usbvision->wait_frame);
1512 init_waitqueue_head(&usbvision->wait_stream);
1514 usbvision->have_tuner = usbvision_device_data[model].tuner;
1515 if (usbvision->have_tuner)
1516 usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1518 usbvision->dev_model = model;
1519 usbvision->remove_pending = 0;
1520 usbvision->iface = ifnum;
1521 usbvision->iface_alt = 0;
1522 usbvision->video_endp = endpoint->bEndpointAddress;
1523 usbvision->isoc_packet_size = 0;
1524 usbvision->usb_bandwidth = 0;
1525 usbvision->user = 0;
1526 usbvision->streaming = stream_off;
1527 usbvision_configure_video(usbvision);
1528 usbvision_register_video(usbvision);
1530 usbvision_create_sysfs(&usbvision->vdev);
1532 PDEBUG(DBG_PROBE, "success");
1536 usbvision_release(usbvision);
1544 * usbvision_disconnect()
1546 * This procedure stops all driver activity, deallocates interface-private
1547 * structure (pointed by 'ptr') and after that driver should be removable
1548 * with no ill consequences.
1551 static void usbvision_disconnect(struct usb_interface *intf)
1553 struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1555 PDEBUG(DBG_PROBE, "");
1558 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1562 mutex_lock(&usbvision->v4l2_lock);
1564 /* At this time we ask to cancel outstanding URBs */
1565 usbvision_stop_isoc(usbvision);
1567 v4l2_device_disconnect(&usbvision->v4l2_dev);
1568 usbvision_i2c_unregister(usbvision);
1569 usbvision->remove_pending = 1; /* Now all ISO data will be ignored */
1571 usb_put_dev(usbvision->dev);
1572 usbvision->dev = NULL; /* USB device is no more */
1574 mutex_unlock(&usbvision->v4l2_lock);
1576 if (usbvision->user) {
1577 printk(KERN_INFO "%s: In use, disconnect pending\n",
1579 wake_up_interruptible(&usbvision->wait_frame);
1580 wake_up_interruptible(&usbvision->wait_stream);
1582 usbvision_release(usbvision);
1585 PDEBUG(DBG_PROBE, "success");
1588 static struct usb_driver usbvision_driver = {
1589 .name = "usbvision",
1590 .id_table = usbvision_table,
1591 .probe = usbvision_probe,
1592 .disconnect = usbvision_disconnect,
1598 * This code is run to initialize the driver.
1601 static int __init usbvision_init(void)
1605 PDEBUG(DBG_PROBE, "");
1607 PDEBUG(DBG_IO, "IO debugging is enabled [video]");
1608 PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
1609 PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
1611 /* disable planar mode support unless compression enabled */
1612 if (isoc_mode != ISOC_MODE_COMPRESS) {
1613 /* FIXME : not the right way to set supported flag */
1614 usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1615 usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1618 err_code = usb_register(&usbvision_driver);
1620 if (err_code == 0) {
1621 printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1622 PDEBUG(DBG_PROBE, "success");
1627 static void __exit usbvision_exit(void)
1629 PDEBUG(DBG_PROBE, "");
1631 usb_deregister(&usbvision_driver);
1632 PDEBUG(DBG_PROBE, "success");
1635 module_init(usbvision_init);
1636 module_exit(usbvision_exit);