spi: sprd: adi: Add a reset reason for watchdog mode
[linux/fpc-iii.git] / drivers / media / usb / usbvision / usbvision-video.c
blob93750af82d98a01166d175961dd4bbecbfe5569e
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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
10 * implemented.
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
19 * TODO:
20 * - use submit_urb for all setup packets
21 * - Fix memory settings for nt1004. It is 4 times as big as the
22 * nt1003 memory.
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>
35 #include <linux/mm.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>
41 #include <linux/io.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); \
73 #else
74 #define PDEBUG(level, fmt, args...) do {} while (0)
75 #endif
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)");
127 /* Misc stuff */
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,
174 V4L2_CID_HUE));
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,
186 V4L2_CID_CONTRAST));
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)
247 int res;
249 if (!vdev)
250 return;
251 do {
252 res = device_create_file(&vdev->dev, &dev_attr_version);
253 if (res < 0)
254 break;
255 res = device_create_file(&vdev->dev, &dev_attr_model);
256 if (res < 0)
257 break;
258 res = device_create_file(&vdev->dev, &dev_attr_hue);
259 if (res < 0)
260 break;
261 res = device_create_file(&vdev->dev, &dev_attr_contrast);
262 if (res < 0)
263 break;
264 res = device_create_file(&vdev->dev, &dev_attr_brightness);
265 if (res < 0)
266 break;
267 res = device_create_file(&vdev->dev, &dev_attr_saturation);
268 if (res < 0)
269 break;
270 res = device_create_file(&vdev->dev, &dev_attr_streaming);
271 if (res < 0)
272 break;
273 res = device_create_file(&vdev->dev, &dev_attr_compression);
274 if (res < 0)
275 break;
276 res = device_create_file(&vdev->dev, &dev_attr_bridge);
277 if (res >= 0)
278 return;
279 } while (0);
281 dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
284 static void usbvision_remove_sysfs(struct video_device *vdev)
286 if (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);
300 * usbvision_open()
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);
310 int err_code = 0;
312 PDEBUG(DBG_IO, "open");
314 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
315 return -ERESTARTSYS;
317 if (usbvision->user) {
318 err_code = -EBUSY;
319 } else {
320 err_code = v4l2_fh_open(file);
321 if (err_code)
322 goto unlock;
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
328 only if needed */
329 err_code = usbvision_decompress_alloc(usbvision);
331 if (err_code) {
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 */
339 if (!err_code) {
340 /* Send init sequence only once, it's large! */
341 if (!usbvision->initialized) {
342 int setup_ok = 0;
343 setup_ok = usbvision_setup(usbvision, isoc_mode);
344 if (setup_ok)
345 usbvision->initialized = 1;
346 else
347 err_code = -EBUSY;
350 if (!err_code) {
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);
356 /* prepare queues */
357 usbvision_empty_framequeues(usbvision);
358 usbvision->user++;
362 unlock:
363 mutex_unlock(&usbvision->v4l2_lock);
365 PDEBUG(DBG_IO, "success");
366 return err_code;
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);
393 usbvision->user--;
394 mutex_unlock(&usbvision->v4l2_lock);
396 if (usbvision->remove_pending) {
397 printk(KERN_INFO "%s: Final disconnect\n", __func__);
398 usbvision_release(usbvision);
399 return 0;
402 PDEBUG(DBG_IO, "success");
403 return v4l2_fh_release(file);
408 * usbvision_ioctl()
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);
418 int err_code;
420 /* NT100x has a 8-bit register space */
421 err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
422 if (err_code < 0) {
423 dev_err(&usbvision->vdev.dev,
424 "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
425 __func__, err_code);
426 return err_code;
428 reg->val = err_code;
429 reg->size = 1;
430 return 0;
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);
437 int err_code;
439 /* NT100x has a 8-bit register space */
440 err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
441 if (err_code < 0) {
442 dev_err(&usbvision->vdev.dev,
443 "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
444 __func__, err_code);
445 return err_code;
447 return 0;
449 #endif
451 static int vidioc_querycap(struct file *file, void *priv,
452 struct v4l2_capability *vc)
454 struct usb_usbvision *usbvision = video_drvdata(file);
456 strscpy(vc->driver, "USBVision", sizeof(vc->driver));
457 strscpy(vc->card,
458 usbvision_device_data[usbvision->dev_model].model_string,
459 sizeof(vc->card));
460 usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
461 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
462 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
463 if (usbvision_device_data[usbvision->dev_model].radio)
464 vc->capabilities |= V4L2_CAP_RADIO;
465 if (usbvision->have_tuner)
466 vc->capabilities |= V4L2_CAP_TUNER;
467 return 0;
470 static int vidioc_enum_input(struct file *file, void *priv,
471 struct v4l2_input *vi)
473 struct usb_usbvision *usbvision = video_drvdata(file);
474 int chan;
476 if (vi->index >= usbvision->video_inputs)
477 return -EINVAL;
478 if (usbvision->have_tuner)
479 chan = vi->index;
480 else
481 chan = vi->index + 1; /* skip Television string*/
483 /* Determine the requested input characteristics
484 specific for each usbvision card model */
485 switch (chan) {
486 case 0:
487 if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
488 strscpy(vi->name, "White Video Input", sizeof(vi->name));
489 } else {
490 strscpy(vi->name, "Television", sizeof(vi->name));
491 vi->type = V4L2_INPUT_TYPE_TUNER;
492 vi->tuner = chan;
493 vi->std = USBVISION_NORMS;
495 break;
496 case 1:
497 vi->type = V4L2_INPUT_TYPE_CAMERA;
498 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
499 strscpy(vi->name, "Green Video Input", sizeof(vi->name));
500 else
501 strscpy(vi->name, "Composite Video Input",
502 sizeof(vi->name));
503 vi->std = USBVISION_NORMS;
504 break;
505 case 2:
506 vi->type = V4L2_INPUT_TYPE_CAMERA;
507 if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
508 strscpy(vi->name, "Yellow Video Input", sizeof(vi->name));
509 else
510 strscpy(vi->name, "S-Video Input", sizeof(vi->name));
511 vi->std = USBVISION_NORMS;
512 break;
513 case 3:
514 vi->type = V4L2_INPUT_TYPE_CAMERA;
515 strscpy(vi->name, "Red Video Input", sizeof(vi->name));
516 vi->std = USBVISION_NORMS;
517 break;
519 return 0;
522 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
524 struct usb_usbvision *usbvision = video_drvdata(file);
526 *input = usbvision->ctl_input;
527 return 0;
530 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
532 struct usb_usbvision *usbvision = video_drvdata(file);
534 if (input >= usbvision->video_inputs)
535 return -EINVAL;
537 usbvision_muxsel(usbvision, input);
538 usbvision_set_input(usbvision);
539 usbvision_set_output(usbvision,
540 usbvision->curwidth,
541 usbvision->curheight);
542 return 0;
545 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
547 struct usb_usbvision *usbvision = video_drvdata(file);
549 usbvision->tvnorm_id = id;
551 call_all(usbvision, video, s_std, usbvision->tvnorm_id);
552 /* propagate the change to the decoder */
553 usbvision_muxsel(usbvision, usbvision->ctl_input);
555 return 0;
558 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
560 struct usb_usbvision *usbvision = video_drvdata(file);
562 *id = usbvision->tvnorm_id;
563 return 0;
566 static int vidioc_g_tuner(struct file *file, void *priv,
567 struct v4l2_tuner *vt)
569 struct usb_usbvision *usbvision = video_drvdata(file);
571 if (vt->index) /* Only tuner 0 */
572 return -EINVAL;
573 if (vt->type == V4L2_TUNER_RADIO)
574 strscpy(vt->name, "Radio", sizeof(vt->name));
575 else
576 strscpy(vt->name, "Television", sizeof(vt->name));
578 /* Let clients fill in the remainder of this struct */
579 call_all(usbvision, tuner, g_tuner, vt);
581 return 0;
584 static int vidioc_s_tuner(struct file *file, void *priv,
585 const struct v4l2_tuner *vt)
587 struct usb_usbvision *usbvision = video_drvdata(file);
589 /* Only one tuner for now */
590 if (vt->index)
591 return -EINVAL;
592 /* let clients handle this */
593 call_all(usbvision, tuner, s_tuner, vt);
595 return 0;
598 static int vidioc_g_frequency(struct file *file, void *priv,
599 struct v4l2_frequency *freq)
601 struct usb_usbvision *usbvision = video_drvdata(file);
603 /* Only one tuner */
604 if (freq->tuner)
605 return -EINVAL;
606 if (freq->type == V4L2_TUNER_RADIO)
607 freq->frequency = usbvision->radio_freq;
608 else
609 freq->frequency = usbvision->tv_freq;
611 return 0;
614 static int vidioc_s_frequency(struct file *file, void *priv,
615 const struct v4l2_frequency *freq)
617 struct usb_usbvision *usbvision = video_drvdata(file);
618 struct v4l2_frequency new_freq = *freq;
620 /* Only one tuner for now */
621 if (freq->tuner)
622 return -EINVAL;
624 call_all(usbvision, tuner, s_frequency, freq);
625 call_all(usbvision, tuner, g_frequency, &new_freq);
626 if (freq->type == V4L2_TUNER_RADIO)
627 usbvision->radio_freq = new_freq.frequency;
628 else
629 usbvision->tv_freq = new_freq.frequency;
631 return 0;
634 static int vidioc_reqbufs(struct file *file,
635 void *priv, struct v4l2_requestbuffers *vr)
637 struct usb_usbvision *usbvision = video_drvdata(file);
638 int ret;
640 RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
642 /* Check input validity:
643 the user must do a VIDEO CAPTURE and MMAP method. */
644 if (vr->memory != V4L2_MEMORY_MMAP)
645 return -EINVAL;
647 if (usbvision->streaming == stream_on) {
648 ret = usbvision_stream_interrupt(usbvision);
649 if (ret)
650 return ret;
653 usbvision_frames_free(usbvision);
654 usbvision_empty_framequeues(usbvision);
655 vr->count = usbvision_frames_alloc(usbvision, vr->count);
657 usbvision->cur_frame = NULL;
659 return 0;
662 static int vidioc_querybuf(struct file *file,
663 void *priv, struct v4l2_buffer *vb)
665 struct usb_usbvision *usbvision = video_drvdata(file);
666 struct usbvision_frame *frame;
668 /* FIXME : must control
669 that buffers are mapped (VIDIOC_REQBUFS has been called) */
670 if (vb->index >= usbvision->num_frames)
671 return -EINVAL;
672 /* Updating the corresponding frame state */
673 vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
674 frame = &usbvision->frame[vb->index];
675 if (frame->grabstate >= frame_state_ready)
676 vb->flags |= V4L2_BUF_FLAG_QUEUED;
677 if (frame->grabstate >= frame_state_done)
678 vb->flags |= V4L2_BUF_FLAG_DONE;
679 if (frame->grabstate == frame_state_unused)
680 vb->flags |= V4L2_BUF_FLAG_MAPPED;
681 vb->memory = V4L2_MEMORY_MMAP;
683 vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
685 vb->memory = V4L2_MEMORY_MMAP;
686 vb->field = V4L2_FIELD_NONE;
687 vb->length = usbvision->curwidth *
688 usbvision->curheight *
689 usbvision->palette.bytes_per_pixel;
690 vb->timestamp = ns_to_timeval(usbvision->frame[vb->index].ts);
691 vb->sequence = usbvision->frame[vb->index].sequence;
692 return 0;
695 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
697 struct usb_usbvision *usbvision = video_drvdata(file);
698 struct usbvision_frame *frame;
699 unsigned long lock_flags;
701 /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
702 if (vb->index >= usbvision->num_frames)
703 return -EINVAL;
705 frame = &usbvision->frame[vb->index];
707 if (frame->grabstate != frame_state_unused)
708 return -EAGAIN;
710 /* Mark it as ready and enqueue frame */
711 frame->grabstate = frame_state_ready;
712 frame->scanstate = scan_state_scanning;
713 frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
715 vb->flags &= ~V4L2_BUF_FLAG_DONE;
717 /* set v4l2_format index */
718 frame->v4l2_format = usbvision->palette;
720 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
721 list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
722 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
724 return 0;
727 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
729 struct usb_usbvision *usbvision = video_drvdata(file);
730 int ret;
731 struct usbvision_frame *f;
732 unsigned long lock_flags;
734 if (list_empty(&(usbvision->outqueue))) {
735 if (usbvision->streaming == stream_idle)
736 return -EINVAL;
737 ret = wait_event_interruptible
738 (usbvision->wait_frame,
739 !list_empty(&(usbvision->outqueue)));
740 if (ret)
741 return ret;
744 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
745 f = list_entry(usbvision->outqueue.next,
746 struct usbvision_frame, frame);
747 list_del(usbvision->outqueue.next);
748 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
750 f->grabstate = frame_state_unused;
752 vb->memory = V4L2_MEMORY_MMAP;
753 vb->flags = V4L2_BUF_FLAG_MAPPED |
754 V4L2_BUF_FLAG_QUEUED |
755 V4L2_BUF_FLAG_DONE |
756 V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
757 vb->index = f->index;
758 vb->sequence = f->sequence;
759 vb->timestamp = ns_to_timeval(f->ts);
760 vb->field = V4L2_FIELD_NONE;
761 vb->bytesused = f->scanlength;
763 return 0;
766 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
768 struct usb_usbvision *usbvision = video_drvdata(file);
770 usbvision->streaming = stream_on;
771 call_all(usbvision, video, s_stream, 1);
773 return 0;
776 static int vidioc_streamoff(struct file *file,
777 void *priv, enum v4l2_buf_type type)
779 struct usb_usbvision *usbvision = video_drvdata(file);
781 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
782 return -EINVAL;
784 if (usbvision->streaming == stream_on) {
785 usbvision_stream_interrupt(usbvision);
786 /* Stop all video streamings */
787 call_all(usbvision, video, s_stream, 0);
789 usbvision_empty_framequeues(usbvision);
791 return 0;
794 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
795 struct v4l2_fmtdesc *vfd)
797 if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
798 return -EINVAL;
799 strscpy(vfd->description, usbvision_v4l2_format[vfd->index].desc,
800 sizeof(vfd->description));
801 vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
802 return 0;
805 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
806 struct v4l2_format *vf)
808 struct usb_usbvision *usbvision = video_drvdata(file);
809 vf->fmt.pix.width = usbvision->curwidth;
810 vf->fmt.pix.height = usbvision->curheight;
811 vf->fmt.pix.pixelformat = usbvision->palette.format;
812 vf->fmt.pix.bytesperline =
813 usbvision->curwidth * usbvision->palette.bytes_per_pixel;
814 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
815 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
816 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
818 return 0;
821 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
822 struct v4l2_format *vf)
824 struct usb_usbvision *usbvision = video_drvdata(file);
825 int format_idx;
827 /* Find requested format in available ones */
828 for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
829 if (vf->fmt.pix.pixelformat ==
830 usbvision_v4l2_format[format_idx].format) {
831 usbvision->palette = usbvision_v4l2_format[format_idx];
832 break;
835 /* robustness */
836 if (format_idx == USBVISION_SUPPORTED_PALETTES)
837 return -EINVAL;
838 RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
839 RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
841 vf->fmt.pix.bytesperline = vf->fmt.pix.width*
842 usbvision->palette.bytes_per_pixel;
843 vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
844 vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
845 vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
847 return 0;
850 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
851 struct v4l2_format *vf)
853 struct usb_usbvision *usbvision = video_drvdata(file);
854 int ret;
856 ret = vidioc_try_fmt_vid_cap(file, priv, vf);
857 if (ret)
858 return ret;
860 /* stop io in case it is already in progress */
861 if (usbvision->streaming == stream_on) {
862 ret = usbvision_stream_interrupt(usbvision);
863 if (ret)
864 return ret;
866 usbvision_frames_free(usbvision);
867 usbvision_empty_framequeues(usbvision);
869 usbvision->cur_frame = NULL;
871 /* by now we are committed to the new data... */
872 usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
874 return 0;
877 static ssize_t usbvision_read(struct file *file, char __user *buf,
878 size_t count, loff_t *ppos)
880 struct usb_usbvision *usbvision = video_drvdata(file);
881 int noblock = file->f_flags & O_NONBLOCK;
882 unsigned long lock_flags;
883 int ret, i;
884 struct usbvision_frame *frame;
886 PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
887 (unsigned long)count, noblock);
889 if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
890 return -EFAULT;
892 /* This entry point is compatible with the mmap routines
893 so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
894 to get frames or call read on the device. */
895 if (!usbvision->num_frames) {
896 /* First, allocate some frames to work with
897 if this has not been done with VIDIOC_REQBUF */
898 usbvision_frames_free(usbvision);
899 usbvision_empty_framequeues(usbvision);
900 usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
903 if (usbvision->streaming != stream_on) {
904 /* no stream is running, make it running ! */
905 usbvision->streaming = stream_on;
906 call_all(usbvision, video, s_stream, 1);
909 /* Then, enqueue as many frames as possible
910 (like a user of VIDIOC_QBUF would do) */
911 for (i = 0; i < usbvision->num_frames; i++) {
912 frame = &usbvision->frame[i];
913 if (frame->grabstate == frame_state_unused) {
914 /* Mark it as ready and enqueue frame */
915 frame->grabstate = frame_state_ready;
916 frame->scanstate = scan_state_scanning;
917 /* Accumulated in usbvision_parse_data() */
918 frame->scanlength = 0;
920 /* set v4l2_format index */
921 frame->v4l2_format = usbvision->palette;
923 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
924 list_add_tail(&frame->frame, &usbvision->inqueue);
925 spin_unlock_irqrestore(&usbvision->queue_lock,
926 lock_flags);
930 /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
931 if (list_empty(&(usbvision->outqueue))) {
932 if (noblock)
933 return -EAGAIN;
935 ret = wait_event_interruptible
936 (usbvision->wait_frame,
937 !list_empty(&(usbvision->outqueue)));
938 if (ret)
939 return ret;
942 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
943 frame = list_entry(usbvision->outqueue.next,
944 struct usbvision_frame, frame);
945 list_del(usbvision->outqueue.next);
946 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
948 /* An error returns an empty frame */
949 if (frame->grabstate == frame_state_error) {
950 frame->bytes_read = 0;
951 return 0;
954 PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
955 __func__,
956 frame->index, frame->bytes_read, frame->scanlength);
958 /* copy bytes to user space; we allow for partials reads */
959 if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
960 count = frame->scanlength - frame->bytes_read;
962 if (copy_to_user(buf, frame->data + frame->bytes_read, count))
963 return -EFAULT;
965 frame->bytes_read += count;
966 PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
967 __func__,
968 (unsigned long)count, frame->bytes_read);
970 #if 1
972 * FIXME:
973 * For now, forget the frame if it has not been read in one shot.
975 frame->bytes_read = 0;
977 /* Mark it as available to be used again. */
978 frame->grabstate = frame_state_unused;
979 #else
980 if (frame->bytes_read >= frame->scanlength) {
981 /* All data has been read */
982 frame->bytes_read = 0;
984 /* Mark it as available to be used again. */
985 frame->grabstate = frame_state_unused;
987 #endif
989 return count;
992 static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
993 size_t count, loff_t *ppos)
995 struct usb_usbvision *usbvision = video_drvdata(file);
996 int res;
998 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
999 return -ERESTARTSYS;
1000 res = usbvision_read(file, buf, count, ppos);
1001 mutex_unlock(&usbvision->v4l2_lock);
1002 return res;
1005 static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1007 unsigned long size = vma->vm_end - vma->vm_start,
1008 start = vma->vm_start;
1009 void *pos;
1010 u32 i;
1011 struct usb_usbvision *usbvision = video_drvdata(file);
1013 PDEBUG(DBG_MMAP, "mmap");
1015 if (!USBVISION_IS_OPERATIONAL(usbvision))
1016 return -EFAULT;
1018 if (!(vma->vm_flags & VM_WRITE) ||
1019 size != PAGE_ALIGN(usbvision->max_frame_size)) {
1020 return -EINVAL;
1023 for (i = 0; i < usbvision->num_frames; i++) {
1024 if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1025 vma->vm_pgoff)
1026 break;
1028 if (i == usbvision->num_frames) {
1029 PDEBUG(DBG_MMAP,
1030 "mmap: user supplied mapping address is out of range");
1031 return -EINVAL;
1034 /* VM_IO is eventually going to replace PageReserved altogether */
1035 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1037 pos = usbvision->frame[i].data;
1038 while (size > 0) {
1039 if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1040 PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1041 return -EAGAIN;
1043 start += PAGE_SIZE;
1044 pos += PAGE_SIZE;
1045 size -= PAGE_SIZE;
1048 return 0;
1051 static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1053 struct usb_usbvision *usbvision = video_drvdata(file);
1054 int res;
1056 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1057 return -ERESTARTSYS;
1058 res = usbvision_mmap(file, vma);
1059 mutex_unlock(&usbvision->v4l2_lock);
1060 return res;
1064 * Here comes the stuff for radio on usbvision based devices
1067 static int usbvision_radio_open(struct file *file)
1069 struct usb_usbvision *usbvision = video_drvdata(file);
1070 int err_code = 0;
1072 PDEBUG(DBG_IO, "%s:", __func__);
1074 if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1075 return -ERESTARTSYS;
1076 err_code = v4l2_fh_open(file);
1077 if (err_code)
1078 goto out;
1079 if (usbvision->user) {
1080 dev_err(&usbvision->rdev.dev,
1081 "%s: Someone tried to open an already opened USBVision Radio!\n",
1082 __func__);
1083 err_code = -EBUSY;
1084 } else {
1085 /* Alternate interface 1 is is the biggest frame size */
1086 err_code = usbvision_set_alternate(usbvision);
1087 if (err_code < 0) {
1088 usbvision->last_error = err_code;
1089 err_code = -EBUSY;
1090 goto out;
1093 /* If so far no errors then we shall start the radio */
1094 usbvision->radio = 1;
1095 call_all(usbvision, tuner, s_radio);
1096 usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1097 usbvision->user++;
1099 out:
1100 mutex_unlock(&usbvision->v4l2_lock);
1101 return err_code;
1105 static int usbvision_radio_close(struct file *file)
1107 struct usb_usbvision *usbvision = video_drvdata(file);
1109 PDEBUG(DBG_IO, "");
1111 mutex_lock(&usbvision->v4l2_lock);
1112 /* Set packet size to 0 */
1113 usbvision->iface_alt = 0;
1114 usb_set_interface(usbvision->dev, usbvision->iface,
1115 usbvision->iface_alt);
1117 usbvision_audio_off(usbvision);
1118 usbvision->radio = 0;
1119 usbvision->user--;
1120 mutex_unlock(&usbvision->v4l2_lock);
1122 if (usbvision->remove_pending) {
1123 printk(KERN_INFO "%s: Final disconnect\n", __func__);
1124 v4l2_fh_release(file);
1125 usbvision_release(usbvision);
1126 return 0;
1129 PDEBUG(DBG_IO, "success");
1130 return v4l2_fh_release(file);
1133 /* Video registration stuff */
1135 /* Video template */
1136 static const struct v4l2_file_operations usbvision_fops = {
1137 .owner = THIS_MODULE,
1138 .open = usbvision_v4l2_open,
1139 .release = usbvision_v4l2_close,
1140 .read = usbvision_v4l2_read,
1141 .mmap = usbvision_v4l2_mmap,
1142 .unlocked_ioctl = video_ioctl2,
1145 static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1146 .vidioc_querycap = vidioc_querycap,
1147 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1148 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1149 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1150 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1151 .vidioc_reqbufs = vidioc_reqbufs,
1152 .vidioc_querybuf = vidioc_querybuf,
1153 .vidioc_qbuf = vidioc_qbuf,
1154 .vidioc_dqbuf = vidioc_dqbuf,
1155 .vidioc_s_std = vidioc_s_std,
1156 .vidioc_g_std = vidioc_g_std,
1157 .vidioc_enum_input = vidioc_enum_input,
1158 .vidioc_g_input = vidioc_g_input,
1159 .vidioc_s_input = vidioc_s_input,
1160 .vidioc_streamon = vidioc_streamon,
1161 .vidioc_streamoff = vidioc_streamoff,
1162 .vidioc_g_tuner = vidioc_g_tuner,
1163 .vidioc_s_tuner = vidioc_s_tuner,
1164 .vidioc_g_frequency = vidioc_g_frequency,
1165 .vidioc_s_frequency = vidioc_s_frequency,
1166 .vidioc_log_status = v4l2_ctrl_log_status,
1167 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1168 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1169 #ifdef CONFIG_VIDEO_ADV_DEBUG
1170 .vidioc_g_register = vidioc_g_register,
1171 .vidioc_s_register = vidioc_s_register,
1172 #endif
1175 static struct video_device usbvision_video_template = {
1176 .fops = &usbvision_fops,
1177 .ioctl_ops = &usbvision_ioctl_ops,
1178 .name = "usbvision-video",
1179 .release = video_device_release_empty,
1180 .tvnorms = USBVISION_NORMS,
1184 /* Radio template */
1185 static const struct v4l2_file_operations usbvision_radio_fops = {
1186 .owner = THIS_MODULE,
1187 .open = usbvision_radio_open,
1188 .release = usbvision_radio_close,
1189 .poll = v4l2_ctrl_poll,
1190 .unlocked_ioctl = video_ioctl2,
1193 static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1194 .vidioc_querycap = vidioc_querycap,
1195 .vidioc_g_tuner = vidioc_g_tuner,
1196 .vidioc_s_tuner = vidioc_s_tuner,
1197 .vidioc_g_frequency = vidioc_g_frequency,
1198 .vidioc_s_frequency = vidioc_s_frequency,
1199 .vidioc_log_status = v4l2_ctrl_log_status,
1200 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1201 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1204 static struct video_device usbvision_radio_template = {
1205 .fops = &usbvision_radio_fops,
1206 .name = "usbvision-radio",
1207 .release = video_device_release_empty,
1208 .ioctl_ops = &usbvision_radio_ioctl_ops,
1212 static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1213 struct video_device *vdev,
1214 const struct video_device *vdev_template,
1215 const char *name)
1217 struct usb_device *usb_dev = usbvision->dev;
1219 if (!usb_dev) {
1220 dev_err(&usbvision->dev->dev,
1221 "%s: usbvision->dev is not set\n", __func__);
1222 return;
1225 *vdev = *vdev_template;
1226 vdev->lock = &usbvision->v4l2_lock;
1227 vdev->v4l2_dev = &usbvision->v4l2_dev;
1228 snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1229 video_set_drvdata(vdev, usbvision);
1232 /* unregister video4linux devices */
1233 static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1235 /* Radio Device: */
1236 if (video_is_registered(&usbvision->rdev)) {
1237 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1238 video_device_node_name(&usbvision->rdev));
1239 video_unregister_device(&usbvision->rdev);
1242 /* Video Device: */
1243 if (video_is_registered(&usbvision->vdev)) {
1244 PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1245 video_device_node_name(&usbvision->vdev));
1246 video_unregister_device(&usbvision->vdev);
1250 /* register video4linux devices */
1251 static int usbvision_register_video(struct usb_usbvision *usbvision)
1253 int res = -ENOMEM;
1255 /* Video Device: */
1256 usbvision_vdev_init(usbvision, &usbvision->vdev,
1257 &usbvision_video_template, "USBVision Video");
1258 if (!usbvision->have_tuner) {
1259 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1260 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1261 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1262 v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1264 usbvision->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1265 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1266 if (usbvision->have_tuner)
1267 usbvision->vdev.device_caps |= V4L2_CAP_TUNER;
1269 if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1270 goto err_exit;
1271 printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1272 usbvision->nr, video_device_node_name(&usbvision->vdev));
1274 /* Radio Device: */
1275 if (usbvision_device_data[usbvision->dev_model].radio) {
1276 /* usbvision has radio */
1277 usbvision_vdev_init(usbvision, &usbvision->rdev,
1278 &usbvision_radio_template, "USBVision Radio");
1279 usbvision->rdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1280 if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1281 goto err_exit;
1282 printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1283 usbvision->nr, video_device_node_name(&usbvision->rdev));
1285 /* all done */
1286 return 0;
1288 err_exit:
1289 dev_err(&usbvision->dev->dev,
1290 "USBVision[%d]: video_register_device() failed\n",
1291 usbvision->nr);
1292 usbvision_unregister_video(usbvision);
1293 return res;
1297 * usbvision_alloc()
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);
1311 if (!usbvision)
1312 return NULL;
1314 usbvision->dev = dev;
1315 if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1316 goto err_free;
1318 if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1319 goto err_unreg;
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)
1326 goto err_unreg;
1328 return usbvision;
1330 err_unreg:
1331 v4l2_ctrl_handler_free(&usbvision->hdl);
1332 v4l2_device_unregister(&usbvision->v4l2_dev);
1333 err_free:
1334 kfree(usbvision);
1335 return NULL;
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);
1359 kfree(usbvision);
1361 PDEBUG(DBG_PROBE, "success");
1365 /*********************** usb interface **********************************/
1367 static void usbvision_configure_video(struct usb_usbvision *usbvision)
1369 int model;
1371 if (!usbvision)
1372 return;
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;
1380 } else {
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);
1400 * usbvision_probe()
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;
1415 int model, i, ret;
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);
1424 ret = -ENODEV;
1425 goto err_usb;
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];
1434 else {
1435 dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1436 ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1437 ret = -ENODEV;
1438 goto err_usb;
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);
1444 ret = -ENODEV;
1445 goto err_usb;
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",
1451 __func__, ifnum);
1452 dev_err(&intf->dev, "%s: Endpoint attributes %d",
1453 __func__, endpoint->bmAttributes);
1454 ret = -ENODEV;
1455 goto err_usb;
1457 if (usb_endpoint_dir_out(endpoint)) {
1458 dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1459 __func__, ifnum);
1460 ret = -ENODEV;
1461 goto err_usb;
1464 usbvision = usbvision_alloc(dev, intf);
1465 if (!usbvision) {
1466 dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1467 ret = -ENOMEM;
1468 goto err_usb;
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;
1475 else
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,
1485 GFP_KERNEL);
1486 if (!usbvision->alt_max_pkt_size) {
1487 ret = -ENOMEM;
1488 goto err_pkt;
1491 for (i = 0; i < usbvision->num_alt; i++) {
1492 u16 tmp;
1494 if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1495 ret = -ENODEV;
1496 goto err_pkt;
1499 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1500 wMaxPacketSize);
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");
1533 return 0;
1535 err_pkt:
1536 usbvision_release(usbvision);
1537 err_usb:
1538 usb_put_dev(dev);
1539 return ret;
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, "");
1557 if (!usbvision) {
1558 pr_err("%s: usb_get_intfdata() failed\n", __func__);
1559 return;
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",
1578 __func__);
1579 wake_up_interruptible(&usbvision->wait_frame);
1580 wake_up_interruptible(&usbvision->wait_stream);
1581 } else {
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,
1596 * usbvision_init()
1598 * This code is run to initialize the driver.
1601 static int __init usbvision_init(void)
1603 int err_code;
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");
1624 return err_code;
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);