linux/drivers/media/usb/usbvision/usbvision-video.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * USB USBVISION Video device driver 0.9.10
   4 *
   5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
   6 *
   7 * This module is part of usbvision driver project.
   8 *
   9 * Let's call the version 0.... until compression decoding is completely
  10 * implemented.
  11 *
  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
  18 *
  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
  29 */
  30
  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>
  44
  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>
  50
  51#include <linux/workqueue.h>
  52
  53#include "usbvision.h"
  54#include "usbvision-cards.h"
  55
  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"
  63
  64#define ENABLE_HEXDUMP  0       /* Enable if you need it */
  65
  66
  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); \
  72        }
  73#else
  74        #define PDEBUG(level, fmt, args...) do {} while (0)
  75#endif
  76
  77#define DBG_IO          (1 << 1)
  78#define DBG_PROBE       (1 << 2)
  79#define DBG_MMAP        (1 << 3)
  80
  81/* String operations */
  82#define rmspace(str)    while (*str == ' ') str++;
  83#define goto2next(str)  while (*str != ' ') str++; while (*str == ' ') str++;
  84
  85
  86/* sequential number of usbvision device */
  87static int usbvision_nr;
  88
  89static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
  90        { 1, 1,  8, V4L2_PIX_FMT_GREY },
  91        { 1, 2, 16, V4L2_PIX_FMT_RGB565 },
  92        { 1, 3, 24, V4L2_PIX_FMT_RGB24 },
  93        { 1, 4, 32, V4L2_PIX_FMT_RGB32 },
  94        { 1, 2, 16, V4L2_PIX_FMT_RGB555 },
  95        { 1, 2, 16, V4L2_PIX_FMT_YUYV },
  96        { 1, 2, 12, V4L2_PIX_FMT_YVU420 }, /* 1.5 ! */
  97        { 1, 2, 16, V4L2_PIX_FMT_YUV422P }
  98};
  99
 100/* Function prototypes */
 101static void usbvision_release(struct usb_usbvision *usbvision);
 102
 103/* Default initialization of device driver parameters */
 104/* Set the default format for ISOC endpoint */
 105static int isoc_mode = ISOC_MODE_COMPRESS;
 106/* Set the default Debug Mode of the device driver */
 107static int video_debug;
 108/* Sequential Number of Video Device */
 109static int video_nr = -1;
 110/* Sequential Number of Radio Device */
 111static int radio_nr = -1;
 112
 113/* Grab parameters for the device driver */
 114
 115/* Showing parameters under SYSFS */
 116module_param(isoc_mode, int, 0444);
 117module_param(video_debug, int, 0444);
 118module_param(video_nr, int, 0444);
 119module_param(radio_nr, int, 0444);
 120
 121MODULE_PARM_DESC(isoc_mode, " Set the default format for ISOC endpoint.  Default: 0x60 (Compression On)");
 122MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver.  Default: 0 (Off)");
 123MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX).  Default: -1 (autodetect)");
 124MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX).  Default: -1 (autodetect)");
 125
 126
 127/* Misc stuff */
 128MODULE_AUTHOR(DRIVER_AUTHOR);
 129MODULE_DESCRIPTION(DRIVER_DESC);
 130MODULE_LICENSE("GPL");
 131MODULE_VERSION(USBVISION_VERSION_STRING);
 132MODULE_ALIAS(DRIVER_ALIAS);
 133
 134
 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/*****************************************************************************/
 142
 143#define YES_NO(x) ((x) ? "Yes" : "No")
 144
 145static inline struct usb_usbvision *cd_to_usbvision(struct device *cd)
 146{
 147        struct video_device *vdev = to_video_device(cd);
 148        return video_get_drvdata(vdev);
 149}
 150
 151static ssize_t show_version(struct device *cd,
 152                            struct device_attribute *attr, char *buf)
 153{
 154        return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
 155}
 156static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
 157
 158static ssize_t show_model(struct device *cd,
 159                          struct device_attribute *attr, char *buf)
 160{
 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);
 165}
 166static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
 167
 168static ssize_t show_hue(struct device *cd,
 169                        struct device_attribute *attr, char *buf)
 170{
 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));
 175
 176        return sprintf(buf, "%d\n", val);
 177}
 178static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
 179
 180static ssize_t show_contrast(struct device *cd,
 181                             struct device_attribute *attr, char *buf)
 182{
 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));
 187
 188        return sprintf(buf, "%d\n", val);
 189}
 190static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
 191
 192static ssize_t show_brightness(struct device *cd,
 193                               struct device_attribute *attr, char *buf)
 194{
 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));
 199
 200        return sprintf(buf, "%d\n", val);
 201}
 202static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
 203
 204static ssize_t show_saturation(struct device *cd,
 205                               struct device_attribute *attr, char *buf)
 206{
 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));
 211
 212        return sprintf(buf, "%d\n", val);
 213}
 214static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
 215
 216static ssize_t show_streaming(struct device *cd,
 217                              struct device_attribute *attr, char *buf)
 218{
 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));
 223}
 224static DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
 225
 226static ssize_t show_compression(struct device *cd,
 227                                struct device_attribute *attr, char *buf)
 228{
 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));
 233}
 234static DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
 235
 236static ssize_t show_device_bridge(struct device *cd,
 237                                  struct device_attribute *attr, char *buf)
 238{
 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);
 242}
 243static DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
 244
 245static void usbvision_create_sysfs(struct video_device *vdev)
 246{
 247        int res;
 248
 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);
 280
 281        dev_err(&vdev->dev, "%s error: %d\n", __func__, res);
 282}
 283
 284static void usbvision_remove_sysfs(struct video_device *vdev)
 285{
 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);
 296        }
 297}
 298
 299/*
 300 * usbvision_open()
 301 *
 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.
 305 *
 306 */
 307static int usbvision_v4l2_open(struct file *file)
 308{
 309        struct usb_usbvision *usbvision = video_drvdata(file);
 310        int err_code = 0;
 311
 312        PDEBUG(DBG_IO, "open");
 313
 314        if (mutex_lock_interruptible(&usbvision->v4l2_lock))
 315                return -ERESTARTSYS;
 316
 317        if (usbvision->remove_pending) {
 318                err_code = -ENODEV;
 319                goto unlock;
 320        }
 321        if (usbvision->user) {
 322                err_code = -EBUSY;
 323        } else {
 324                err_code = v4l2_fh_open(file);
 325                if (err_code)
 326                        goto unlock;
 327
 328                /* Allocate memory for the scratch ring buffer */
 329                err_code = usbvision_scratch_alloc(usbvision);
 330                if (isoc_mode == ISOC_MODE_COMPRESS) {
 331                        /* Allocate intermediate decompression buffers
 332                           only if needed */
 333                        err_code = usbvision_decompress_alloc(usbvision);
 334                }
 335                if (err_code) {
 336                        /* Deallocate all buffers if trouble */
 337                        usbvision_scratch_free(usbvision);
 338                        usbvision_decompress_free(usbvision);
 339                }
 340        }
 341
 342        /* If so far no errors then we shall start the camera */
 343        if (!err_code) {
 344                /* Send init sequence only once, it's large! */
 345                if (!usbvision->initialized) {
 346                        int setup_ok = 0;
 347                        setup_ok = usbvision_setup(usbvision, isoc_mode);
 348                        if (setup_ok)
 349                                usbvision->initialized = 1;
 350                        else
 351                                err_code = -EBUSY;
 352                }
 353
 354                if (!err_code) {
 355                        usbvision_begin_streaming(usbvision);
 356                        err_code = usbvision_init_isoc(usbvision);
 357                        /* device must be initialized before isoc transfer */
 358                        usbvision_muxsel(usbvision, 0);
 359
 360                        /* prepare queues */
 361                        usbvision_empty_framequeues(usbvision);
 362                        usbvision->user++;
 363                }
 364        }
 365
 366unlock:
 367        mutex_unlock(&usbvision->v4l2_lock);
 368
 369        PDEBUG(DBG_IO, "success");
 370        return err_code;
 371}
 372
 373/*
 374 * usbvision_v4l2_close()
 375 *
 376 * This is part of Video 4 Linux API. The procedure
 377 * stops streaming and deallocates all buffers that were earlier
 378 * allocated in usbvision_v4l2_open().
 379 *
 380 */
 381static int usbvision_v4l2_close(struct file *file)
 382{
 383        struct usb_usbvision *usbvision = video_drvdata(file);
 384        int r;
 385
 386        PDEBUG(DBG_IO, "close");
 387
 388        mutex_lock(&usbvision->v4l2_lock);
 389        usbvision_audio_off(usbvision);
 390        usbvision_restart_isoc(usbvision);
 391        usbvision_stop_isoc(usbvision);
 392
 393        usbvision_decompress_free(usbvision);
 394        usbvision_frames_free(usbvision);
 395        usbvision_empty_framequeues(usbvision);
 396        usbvision_scratch_free(usbvision);
 397
 398        usbvision->user--;
 399        r = usbvision->remove_pending;
 400        mutex_unlock(&usbvision->v4l2_lock);
 401
 402        if (r) {
 403                printk(KERN_INFO "%s: Final disconnect\n", __func__);
 404                usbvision_release(usbvision);
 405                return 0;
 406        }
 407
 408        PDEBUG(DBG_IO, "success");
 409        return v4l2_fh_release(file);
 410}
 411
 412
 413/*
 414 * usbvision_ioctl()
 415 *
 416 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
 417 *
 418 */
 419#ifdef CONFIG_VIDEO_ADV_DEBUG
 420static int vidioc_g_register(struct file *file, void *priv,
 421                                struct v4l2_dbg_register *reg)
 422{
 423        struct usb_usbvision *usbvision = video_drvdata(file);
 424        int err_code;
 425
 426        /* NT100x has a 8-bit register space */
 427        err_code = usbvision_read_reg(usbvision, reg->reg&0xff);
 428        if (err_code < 0) {
 429                dev_err(&usbvision->vdev.dev,
 430                        "%s: VIDIOC_DBG_G_REGISTER failed: error %d\n",
 431                                __func__, err_code);
 432                return err_code;
 433        }
 434        reg->val = err_code;
 435        reg->size = 1;
 436        return 0;
 437}
 438
 439static int vidioc_s_register(struct file *file, void *priv,
 440                                const struct v4l2_dbg_register *reg)
 441{
 442        struct usb_usbvision *usbvision = video_drvdata(file);
 443        int err_code;
 444
 445        /* NT100x has a 8-bit register space */
 446        err_code = usbvision_write_reg(usbvision, reg->reg & 0xff, reg->val);
 447        if (err_code < 0) {
 448                dev_err(&usbvision->vdev.dev,
 449                        "%s: VIDIOC_DBG_S_REGISTER failed: error %d\n",
 450                                __func__, err_code);
 451                return err_code;
 452        }
 453        return 0;
 454}
 455#endif
 456
 457static int vidioc_querycap(struct file *file, void  *priv,
 458                                        struct v4l2_capability *vc)
 459{
 460        struct usb_usbvision *usbvision = video_drvdata(file);
 461
 462        if (!usbvision->dev)
 463                return -ENODEV;
 464
 465        strscpy(vc->driver, "USBVision", sizeof(vc->driver));
 466        strscpy(vc->card,
 467                usbvision_device_data[usbvision->dev_model].model_string,
 468                sizeof(vc->card));
 469        usb_make_path(usbvision->dev, vc->bus_info, sizeof(vc->bus_info));
 470        vc->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
 471                           V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
 472        if (usbvision_device_data[usbvision->dev_model].radio)
 473                vc->capabilities |= V4L2_CAP_RADIO;
 474        if (usbvision->have_tuner)
 475                vc->capabilities |= V4L2_CAP_TUNER;
 476        return 0;
 477}
 478
 479static int vidioc_enum_input(struct file *file, void *priv,
 480                                struct v4l2_input *vi)
 481{
 482        struct usb_usbvision *usbvision = video_drvdata(file);
 483        int chan;
 484
 485        if (vi->index >= usbvision->video_inputs)
 486                return -EINVAL;
 487        if (usbvision->have_tuner)
 488                chan = vi->index;
 489        else
 490                chan = vi->index + 1; /* skip Television string*/
 491
 492        /* Determine the requested input characteristics
 493           specific for each usbvision card model */
 494        switch (chan) {
 495        case 0:
 496                if (usbvision_device_data[usbvision->dev_model].video_channels == 4) {
 497                        strscpy(vi->name, "White Video Input", sizeof(vi->name));
 498                } else {
 499                        strscpy(vi->name, "Television", sizeof(vi->name));
 500                        vi->type = V4L2_INPUT_TYPE_TUNER;
 501                        vi->tuner = chan;
 502                        vi->std = USBVISION_NORMS;
 503                }
 504                break;
 505        case 1:
 506                vi->type = V4L2_INPUT_TYPE_CAMERA;
 507                if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
 508                        strscpy(vi->name, "Green Video Input", sizeof(vi->name));
 509                else
 510                        strscpy(vi->name, "Composite Video Input",
 511                                sizeof(vi->name));
 512                vi->std = USBVISION_NORMS;
 513                break;
 514        case 2:
 515                vi->type = V4L2_INPUT_TYPE_CAMERA;
 516                if (usbvision_device_data[usbvision->dev_model].video_channels == 4)
 517                        strscpy(vi->name, "Yellow Video Input", sizeof(vi->name));
 518                else
 519                        strscpy(vi->name, "S-Video Input", sizeof(vi->name));
 520                vi->std = USBVISION_NORMS;
 521                break;
 522        case 3:
 523                vi->type = V4L2_INPUT_TYPE_CAMERA;
 524                strscpy(vi->name, "Red Video Input", sizeof(vi->name));
 525                vi->std = USBVISION_NORMS;
 526                break;
 527        }
 528        return 0;
 529}
 530
 531static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
 532{
 533        struct usb_usbvision *usbvision = video_drvdata(file);
 534
 535        *input = usbvision->ctl_input;
 536        return 0;
 537}
 538
 539static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
 540{
 541        struct usb_usbvision *usbvision = video_drvdata(file);
 542
 543        if (input >= usbvision->video_inputs)
 544                return -EINVAL;
 545
 546        usbvision_muxsel(usbvision, input);
 547        usbvision_set_input(usbvision);
 548        usbvision_set_output(usbvision,
 549                             usbvision->curwidth,
 550                             usbvision->curheight);
 551        return 0;
 552}
 553
 554static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
 555{
 556        struct usb_usbvision *usbvision = video_drvdata(file);
 557
 558        usbvision->tvnorm_id = id;
 559
 560        call_all(usbvision, video, s_std, usbvision->tvnorm_id);
 561        /* propagate the change to the decoder */
 562        usbvision_muxsel(usbvision, usbvision->ctl_input);
 563
 564        return 0;
 565}
 566
 567static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 568{
 569        struct usb_usbvision *usbvision = video_drvdata(file);
 570
 571        *id = usbvision->tvnorm_id;
 572        return 0;
 573}
 574
 575static int vidioc_g_tuner(struct file *file, void *priv,
 576                                struct v4l2_tuner *vt)
 577{
 578        struct usb_usbvision *usbvision = video_drvdata(file);
 579
 580        if (vt->index)  /* Only tuner 0 */
 581                return -EINVAL;
 582        if (vt->type == V4L2_TUNER_RADIO)
 583                strscpy(vt->name, "Radio", sizeof(vt->name));
 584        else
 585                strscpy(vt->name, "Television", sizeof(vt->name));
 586
 587        /* Let clients fill in the remainder of this struct */
 588        call_all(usbvision, tuner, g_tuner, vt);
 589
 590        return 0;
 591}
 592
 593static int vidioc_s_tuner(struct file *file, void *priv,
 594                                const struct v4l2_tuner *vt)
 595{
 596        struct usb_usbvision *usbvision = video_drvdata(file);
 597
 598        /* Only one tuner for now */
 599        if (vt->index)
 600                return -EINVAL;
 601        /* let clients handle this */
 602        call_all(usbvision, tuner, s_tuner, vt);
 603
 604        return 0;
 605}
 606
 607static int vidioc_g_frequency(struct file *file, void *priv,
 608                                struct v4l2_frequency *freq)
 609{
 610        struct usb_usbvision *usbvision = video_drvdata(file);
 611
 612        /* Only one tuner */
 613        if (freq->tuner)
 614                return -EINVAL;
 615        if (freq->type == V4L2_TUNER_RADIO)
 616                freq->frequency = usbvision->radio_freq;
 617        else
 618                freq->frequency = usbvision->tv_freq;
 619
 620        return 0;
 621}
 622
 623static int vidioc_s_frequency(struct file *file, void *priv,
 624                                const struct v4l2_frequency *freq)
 625{
 626        struct usb_usbvision *usbvision = video_drvdata(file);
 627        struct v4l2_frequency new_freq = *freq;
 628
 629        /* Only one tuner for now */
 630        if (freq->tuner)
 631                return -EINVAL;
 632
 633        call_all(usbvision, tuner, s_frequency, freq);
 634        call_all(usbvision, tuner, g_frequency, &new_freq);
 635        if (freq->type == V4L2_TUNER_RADIO)
 636                usbvision->radio_freq = new_freq.frequency;
 637        else
 638                usbvision->tv_freq = new_freq.frequency;
 639
 640        return 0;
 641}
 642
 643static int vidioc_reqbufs(struct file *file,
 644                           void *priv, struct v4l2_requestbuffers *vr)
 645{
 646        struct usb_usbvision *usbvision = video_drvdata(file);
 647        int ret;
 648
 649        RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
 650
 651        /* Check input validity:
 652           the user must do a VIDEO CAPTURE and MMAP method. */
 653        if (vr->memory != V4L2_MEMORY_MMAP)
 654                return -EINVAL;
 655
 656        if (usbvision->streaming == stream_on) {
 657                ret = usbvision_stream_interrupt(usbvision);
 658                if (ret)
 659                        return ret;
 660        }
 661
 662        usbvision_frames_free(usbvision);
 663        usbvision_empty_framequeues(usbvision);
 664        vr->count = usbvision_frames_alloc(usbvision, vr->count);
 665
 666        usbvision->cur_frame = NULL;
 667
 668        return 0;
 669}
 670
 671static int vidioc_querybuf(struct file *file,
 672                            void *priv, struct v4l2_buffer *vb)
 673{
 674        struct usb_usbvision *usbvision = video_drvdata(file);
 675        struct usbvision_frame *frame;
 676
 677        /* FIXME : must control
 678           that buffers are mapped (VIDIOC_REQBUFS has been called) */
 679        if (vb->index >= usbvision->num_frames)
 680                return -EINVAL;
 681        /* Updating the corresponding frame state */
 682        vb->flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 683        frame = &usbvision->frame[vb->index];
 684        if (frame->grabstate >= frame_state_ready)
 685                vb->flags |= V4L2_BUF_FLAG_QUEUED;
 686        if (frame->grabstate >= frame_state_done)
 687                vb->flags |= V4L2_BUF_FLAG_DONE;
 688        if (frame->grabstate == frame_state_unused)
 689                vb->flags |= V4L2_BUF_FLAG_MAPPED;
 690        vb->memory = V4L2_MEMORY_MMAP;
 691
 692        vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
 693
 694        vb->memory = V4L2_MEMORY_MMAP;
 695        vb->field = V4L2_FIELD_NONE;
 696        vb->length = usbvision->curwidth *
 697                usbvision->curheight *
 698                usbvision->palette.bytes_per_pixel;
 699        vb->timestamp = ns_to_timeval(usbvision->frame[vb->index].ts);
 700        vb->sequence = usbvision->frame[vb->index].sequence;
 701        return 0;
 702}
 703
 704static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 705{
 706        struct usb_usbvision *usbvision = video_drvdata(file);
 707        struct usbvision_frame *frame;
 708        unsigned long lock_flags;
 709
 710        /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
 711        if (vb->index >= usbvision->num_frames)
 712                return -EINVAL;
 713
 714        frame = &usbvision->frame[vb->index];
 715
 716        if (frame->grabstate != frame_state_unused)
 717                return -EAGAIN;
 718
 719        /* Mark it as ready and enqueue frame */
 720        frame->grabstate = frame_state_ready;
 721        frame->scanstate = scan_state_scanning;
 722        frame->scanlength = 0;  /* Accumulated in usbvision_parse_data() */
 723
 724        vb->flags &= ~V4L2_BUF_FLAG_DONE;
 725
 726        /* set v4l2_format index */
 727        frame->v4l2_format = usbvision->palette;
 728
 729        spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 730        list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
 731        spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
 732
 733        return 0;
 734}
 735
 736static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 737{
 738        struct usb_usbvision *usbvision = video_drvdata(file);
 739        int ret;
 740        struct usbvision_frame *f;
 741        unsigned long lock_flags;
 742
 743        if (list_empty(&(usbvision->outqueue))) {
 744                if (usbvision->streaming == stream_idle)
 745                        return -EINVAL;
 746                ret = wait_event_interruptible
 747                        (usbvision->wait_frame,
 748                         !list_empty(&(usbvision->outqueue)));
 749                if (ret)
 750                        return ret;
 751        }
 752
 753        spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 754        f = list_entry(usbvision->outqueue.next,
 755                       struct usbvision_frame, frame);
 756        list_del(usbvision->outqueue.next);
 757        spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
 758
 759        f->grabstate = frame_state_unused;
 760
 761        vb->memory = V4L2_MEMORY_MMAP;
 762        vb->flags = V4L2_BUF_FLAG_MAPPED |
 763                V4L2_BUF_FLAG_QUEUED |
 764                V4L2_BUF_FLAG_DONE |
 765                V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 766        vb->index = f->index;
 767        vb->sequence = f->sequence;
 768        vb->timestamp = ns_to_timeval(f->ts);
 769        vb->field = V4L2_FIELD_NONE;
 770        vb->bytesused = f->scanlength;
 771
 772        return 0;
 773}
 774
 775static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 776{
 777        struct usb_usbvision *usbvision = video_drvdata(file);
 778
 779        usbvision->streaming = stream_on;
 780        call_all(usbvision, video, s_stream, 1);
 781
 782        return 0;
 783}
 784
 785static int vidioc_streamoff(struct file *file,
 786                            void *priv, enum v4l2_buf_type type)
 787{
 788        struct usb_usbvision *usbvision = video_drvdata(file);
 789
 790        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 791                return -EINVAL;
 792
 793        if (usbvision->streaming == stream_on) {
 794                usbvision_stream_interrupt(usbvision);
 795                /* Stop all video streamings */
 796                call_all(usbvision, video, s_stream, 0);
 797        }
 798        usbvision_empty_framequeues(usbvision);
 799
 800        return 0;
 801}
 802
 803static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 804                                        struct v4l2_fmtdesc *vfd)
 805{
 806        if (vfd->index >= USBVISION_SUPPORTED_PALETTES - 1)
 807                return -EINVAL;
 808        vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
 809        return 0;
 810}
 811
 812static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 813                                        struct v4l2_format *vf)
 814{
 815        struct usb_usbvision *usbvision = video_drvdata(file);
 816        vf->fmt.pix.width = usbvision->curwidth;
 817        vf->fmt.pix.height = usbvision->curheight;
 818        vf->fmt.pix.pixelformat = usbvision->palette.format;
 819        vf->fmt.pix.bytesperline =
 820                usbvision->curwidth * usbvision->palette.bytes_per_pixel;
 821        vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline * usbvision->curheight;
 822        vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 823        vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
 824
 825        return 0;
 826}
 827
 828static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 829                               struct v4l2_format *vf)
 830{
 831        struct usb_usbvision *usbvision = video_drvdata(file);
 832        int format_idx;
 833
 834        /* Find requested format in available ones */
 835        for (format_idx = 0; format_idx < USBVISION_SUPPORTED_PALETTES; format_idx++) {
 836                if (vf->fmt.pix.pixelformat ==
 837                   usbvision_v4l2_format[format_idx].format) {
 838                        usbvision->palette = usbvision_v4l2_format[format_idx];
 839                        break;
 840                }
 841        }
 842        /* robustness */
 843        if (format_idx == USBVISION_SUPPORTED_PALETTES)
 844                return -EINVAL;
 845        RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
 846        RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
 847
 848        vf->fmt.pix.bytesperline = vf->fmt.pix.width*
 849                usbvision->palette.bytes_per_pixel;
 850        vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
 851        vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 852        vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
 853
 854        return 0;
 855}
 856
 857static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 858                               struct v4l2_format *vf)
 859{
 860        struct usb_usbvision *usbvision = video_drvdata(file);
 861        int ret;
 862
 863        ret = vidioc_try_fmt_vid_cap(file, priv, vf);
 864        if (ret)
 865                return ret;
 866
 867        /* stop io in case it is already in progress */
 868        if (usbvision->streaming == stream_on) {
 869                ret = usbvision_stream_interrupt(usbvision);
 870                if (ret)
 871                        return ret;
 872        }
 873        usbvision_frames_free(usbvision);
 874        usbvision_empty_framequeues(usbvision);
 875
 876        usbvision->cur_frame = NULL;
 877
 878        /* by now we are committed to the new data... */
 879        usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
 880
 881        return 0;
 882}
 883
 884static ssize_t usbvision_read(struct file *file, char __user *buf,
 885                      size_t count, loff_t *ppos)
 886{
 887        struct usb_usbvision *usbvision = video_drvdata(file);
 888        int noblock = file->f_flags & O_NONBLOCK;
 889        unsigned long lock_flags;
 890        int ret, i;
 891        struct usbvision_frame *frame;
 892
 893        PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
 894               (unsigned long)count, noblock);
 895
 896        if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
 897                return -EFAULT;
 898
 899        /* This entry point is compatible with the mmap routines
 900           so that a user can do either VIDIOC_QBUF/VIDIOC_DQBUF
 901           to get frames or call read on the device. */
 902        if (!usbvision->num_frames) {
 903                /* First, allocate some frames to work with
 904                   if this has not been done with VIDIOC_REQBUF */
 905                usbvision_frames_free(usbvision);
 906                usbvision_empty_framequeues(usbvision);
 907                usbvision_frames_alloc(usbvision, USBVISION_NUMFRAMES);
 908        }
 909
 910        if (usbvision->streaming != stream_on) {
 911                /* no stream is running, make it running ! */
 912                usbvision->streaming = stream_on;
 913                call_all(usbvision, video, s_stream, 1);
 914        }
 915
 916        /* Then, enqueue as many frames as possible
 917           (like a user of VIDIOC_QBUF would do) */
 918        for (i = 0; i < usbvision->num_frames; i++) {
 919                frame = &usbvision->frame[i];
 920                if (frame->grabstate == frame_state_unused) {
 921                        /* Mark it as ready and enqueue frame */
 922                        frame->grabstate = frame_state_ready;
 923                        frame->scanstate = scan_state_scanning;
 924                        /* Accumulated in usbvision_parse_data() */
 925                        frame->scanlength = 0;
 926
 927                        /* set v4l2_format index */
 928                        frame->v4l2_format = usbvision->palette;
 929
 930                        spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 931                        list_add_tail(&frame->frame, &usbvision->inqueue);
 932                        spin_unlock_irqrestore(&usbvision->queue_lock,
 933                                               lock_flags);
 934                }
 935        }
 936
 937        /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
 938        if (list_empty(&(usbvision->outqueue))) {
 939                if (noblock)
 940                        return -EAGAIN;
 941
 942                ret = wait_event_interruptible
 943                        (usbvision->wait_frame,
 944                         !list_empty(&(usbvision->outqueue)));
 945                if (ret)
 946                        return ret;
 947        }
 948
 949        spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
 950        frame = list_entry(usbvision->outqueue.next,
 951                           struct usbvision_frame, frame);
 952        list_del(usbvision->outqueue.next);
 953        spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
 954
 955        /* An error returns an empty frame */
 956        if (frame->grabstate == frame_state_error) {
 957                frame->bytes_read = 0;
 958                return 0;
 959        }
 960
 961        PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
 962               __func__,
 963               frame->index, frame->bytes_read, frame->scanlength);
 964
 965        /* copy bytes to user space; we allow for partials reads */
 966        if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
 967                count = frame->scanlength - frame->bytes_read;
 968
 969        if (copy_to_user(buf, frame->data + frame->bytes_read, count))
 970                return -EFAULT;
 971
 972        frame->bytes_read += count;
 973        PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld",
 974               __func__,
 975               (unsigned long)count, frame->bytes_read);
 976
 977        /*
 978         * FIXME:
 979         * For now, forget the frame if it has not been read in one shot.
 980         */
 981        frame->bytes_read = 0;
 982
 983        /* Mark it as available to be used again. */
 984        frame->grabstate = frame_state_unused;
 985
 986        return count;
 987}
 988
 989static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
 990                      size_t count, loff_t *ppos)
 991{
 992        struct usb_usbvision *usbvision = video_drvdata(file);
 993        int res;
 994
 995        if (mutex_lock_interruptible(&usbvision->v4l2_lock))
 996                return -ERESTARTSYS;
 997        res = usbvision_read(file, buf, count, ppos);
 998        mutex_unlock(&usbvision->v4l2_lock);
 999        return res;
1000}
1001
1002static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1003{
1004        unsigned long size = vma->vm_end - vma->vm_start,
1005                start = vma->vm_start;
1006        void *pos;
1007        u32 i;
1008        struct usb_usbvision *usbvision = video_drvdata(file);
1009
1010        PDEBUG(DBG_MMAP, "mmap");
1011
1012        if (!USBVISION_IS_OPERATIONAL(usbvision))
1013                return -EFAULT;
1014
1015        if (!(vma->vm_flags & VM_WRITE) ||
1016            size != PAGE_ALIGN(usbvision->max_frame_size)) {
1017                return -EINVAL;
1018        }
1019
1020        for (i = 0; i < usbvision->num_frames; i++) {
1021                if (((PAGE_ALIGN(usbvision->max_frame_size)*i) >> PAGE_SHIFT) ==
1022                    vma->vm_pgoff)
1023                        break;
1024        }
1025        if (i == usbvision->num_frames) {
1026                PDEBUG(DBG_MMAP,
1027                       "mmap: user supplied mapping address is out of range");
1028                return -EINVAL;
1029        }
1030
1031        /* VM_IO is eventually going to replace PageReserved altogether */
1032        vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1033
1034        pos = usbvision->frame[i].data;
1035        while (size > 0) {
1036                if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
1037                        PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
1038                        return -EAGAIN;
1039                }
1040                start += PAGE_SIZE;
1041                pos += PAGE_SIZE;
1042                size -= PAGE_SIZE;
1043        }
1044
1045        return 0;
1046}
1047
1048static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1049{
1050        struct usb_usbvision *usbvision = video_drvdata(file);
1051        int res;
1052
1053        if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1054                return -ERESTARTSYS;
1055        res = usbvision_mmap(file, vma);
1056        mutex_unlock(&usbvision->v4l2_lock);
1057        return res;
1058}
1059
1060/*
1061 * Here comes the stuff for radio on usbvision based devices
1062 *
1063 */
1064static int usbvision_radio_open(struct file *file)
1065{
1066        struct usb_usbvision *usbvision = video_drvdata(file);
1067        int err_code = 0;
1068
1069        PDEBUG(DBG_IO, "%s:", __func__);
1070
1071        if (mutex_lock_interruptible(&usbvision->v4l2_lock))
1072                return -ERESTARTSYS;
1073
1074        if (usbvision->remove_pending) {
1075                err_code = -ENODEV;
1076                goto out;
1077        }
1078        err_code = v4l2_fh_open(file);
1079        if (err_code)
1080                goto out;
1081        if (usbvision->user) {
1082                dev_err(&usbvision->rdev.dev,
1083                        "%s: Someone tried to open an already opened USBVision Radio!\n",
1084                                __func__);
1085                err_code = -EBUSY;
1086        } else {
1087                /* Alternate interface 1 is is the biggest frame size */
1088                err_code = usbvision_set_alternate(usbvision);
1089                if (err_code < 0) {
1090                        usbvision->last_error = err_code;
1091                        err_code = -EBUSY;
1092                        goto out;
1093                }
1094
1095                /* If so far no errors then we shall start the radio */
1096                usbvision->radio = 1;
1097                call_all(usbvision, tuner, s_radio);
1098                usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
1099                usbvision->user++;
1100        }
1101out:
1102        mutex_unlock(&usbvision->v4l2_lock);
1103        return err_code;
1104}
1105
1106
1107static int usbvision_radio_close(struct file *file)
1108{
1109        struct usb_usbvision *usbvision = video_drvdata(file);
1110        int r;
1111
1112        PDEBUG(DBG_IO, "");
1113
1114        mutex_lock(&usbvision->v4l2_lock);
1115        /* Set packet size to 0 */
1116        usbvision->iface_alt = 0;
1117        if (usbvision->dev)
1118                usb_set_interface(usbvision->dev, usbvision->iface,
1119                                  usbvision->iface_alt);
1120
1121        usbvision_audio_off(usbvision);
1122        usbvision->radio = 0;
1123        usbvision->user--;
1124        r = usbvision->remove_pending;
1125        mutex_unlock(&usbvision->v4l2_lock);
1126
1127        if (r) {
1128                printk(KERN_INFO "%s: Final disconnect\n", __func__);
1129                v4l2_fh_release(file);
1130                usbvision_release(usbvision);
1131                return 0;
1132        }
1133
1134        PDEBUG(DBG_IO, "success");
1135        return v4l2_fh_release(file);
1136}
1137
1138/* Video registration stuff */
1139
1140/* Video template */
1141static const struct v4l2_file_operations usbvision_fops = {
1142        .owner             = THIS_MODULE,
1143        .open           = usbvision_v4l2_open,
1144        .release        = usbvision_v4l2_close,
1145        .read           = usbvision_v4l2_read,
1146        .mmap           = usbvision_v4l2_mmap,
1147        .unlocked_ioctl = video_ioctl2,
1148};
1149
1150static const struct v4l2_ioctl_ops usbvision_ioctl_ops = {
1151        .vidioc_querycap      = vidioc_querycap,
1152        .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1153        .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1154        .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1155        .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1156        .vidioc_reqbufs       = vidioc_reqbufs,
1157        .vidioc_querybuf      = vidioc_querybuf,
1158        .vidioc_qbuf          = vidioc_qbuf,
1159        .vidioc_dqbuf         = vidioc_dqbuf,
1160        .vidioc_s_std         = vidioc_s_std,
1161        .vidioc_g_std         = vidioc_g_std,
1162        .vidioc_enum_input    = vidioc_enum_input,
1163        .vidioc_g_input       = vidioc_g_input,
1164        .vidioc_s_input       = vidioc_s_input,
1165        .vidioc_streamon      = vidioc_streamon,
1166        .vidioc_streamoff     = vidioc_streamoff,
1167        .vidioc_g_tuner       = vidioc_g_tuner,
1168        .vidioc_s_tuner       = vidioc_s_tuner,
1169        .vidioc_g_frequency   = vidioc_g_frequency,
1170        .vidioc_s_frequency   = vidioc_s_frequency,
1171        .vidioc_log_status    = v4l2_ctrl_log_status,
1172        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1173        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1174#ifdef CONFIG_VIDEO_ADV_DEBUG
1175        .vidioc_g_register    = vidioc_g_register,
1176        .vidioc_s_register    = vidioc_s_register,
1177#endif
1178};
1179
1180static struct video_device usbvision_video_template = {
1181        .fops           = &usbvision_fops,
1182        .ioctl_ops      = &usbvision_ioctl_ops,
1183        .name           = "usbvision-video",
1184        .release        = video_device_release_empty,
1185        .tvnorms        = USBVISION_NORMS,
1186};
1187
1188
1189/* Radio template */
1190static const struct v4l2_file_operations usbvision_radio_fops = {
1191        .owner             = THIS_MODULE,
1192        .open           = usbvision_radio_open,
1193        .release        = usbvision_radio_close,
1194        .poll           = v4l2_ctrl_poll,
1195        .unlocked_ioctl = video_ioctl2,
1196};
1197
1198static const struct v4l2_ioctl_ops usbvision_radio_ioctl_ops = {
1199        .vidioc_querycap      = vidioc_querycap,
1200        .vidioc_g_tuner       = vidioc_g_tuner,
1201        .vidioc_s_tuner       = vidioc_s_tuner,
1202        .vidioc_g_frequency   = vidioc_g_frequency,
1203        .vidioc_s_frequency   = vidioc_s_frequency,
1204        .vidioc_log_status    = v4l2_ctrl_log_status,
1205        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1206        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1207};
1208
1209static struct video_device usbvision_radio_template = {
1210        .fops           = &usbvision_radio_fops,
1211        .name           = "usbvision-radio",
1212        .release        = video_device_release_empty,
1213        .ioctl_ops      = &usbvision_radio_ioctl_ops,
1214};
1215
1216
1217static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1218                                struct video_device *vdev,
1219                                const struct video_device *vdev_template,
1220                                const char *name)
1221{
1222        struct usb_device *usb_dev = usbvision->dev;
1223
1224        if (!usb_dev) {
1225                dev_err(&usbvision->dev->dev,
1226                        "%s: usbvision->dev is not set\n", __func__);
1227                return;
1228        }
1229
1230        *vdev = *vdev_template;
1231        vdev->lock = &usbvision->v4l2_lock;
1232        vdev->v4l2_dev = &usbvision->v4l2_dev;
1233        snprintf(vdev->name, sizeof(vdev->name), "%s", name);
1234        video_set_drvdata(vdev, usbvision);
1235}
1236
1237/* unregister video4linux devices */
1238static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1239{
1240        /* Radio Device: */
1241        if (video_is_registered(&usbvision->rdev)) {
1242                PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1243                       video_device_node_name(&usbvision->rdev));
1244                video_unregister_device(&usbvision->rdev);
1245        }
1246
1247        /* Video Device: */
1248        if (video_is_registered(&usbvision->vdev)) {
1249                PDEBUG(DBG_PROBE, "unregister %s [v4l2]",
1250                       video_device_node_name(&usbvision->vdev));
1251                video_unregister_device(&usbvision->vdev);
1252        }
1253}
1254
1255/* register video4linux devices */
1256static int usbvision_register_video(struct usb_usbvision *usbvision)
1257{
1258        int res = -ENOMEM;
1259
1260        /* Video Device: */
1261        usbvision_vdev_init(usbvision, &usbvision->vdev,
1262                              &usbvision_video_template, "USBVision Video");
1263        if (!usbvision->have_tuner) {
1264                v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1265                v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1266                v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_G_FREQUENCY);
1267                v4l2_disable_ioctl(&usbvision->vdev, VIDIOC_S_TUNER);
1268        }
1269        usbvision->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
1270                                      V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1271        if (usbvision->have_tuner)
1272                usbvision->vdev.device_caps |= V4L2_CAP_TUNER;
1273
1274        if (video_register_device(&usbvision->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
1275                goto err_exit;
1276        printk(KERN_INFO "USBVision[%d]: registered USBVision Video device %s [v4l2]\n",
1277               usbvision->nr, video_device_node_name(&usbvision->vdev));
1278
1279        /* Radio Device: */
1280        if (usbvision_device_data[usbvision->dev_model].radio) {
1281                /* usbvision has radio */
1282                usbvision_vdev_init(usbvision, &usbvision->rdev,
1283                              &usbvision_radio_template, "USBVision Radio");
1284                usbvision->rdev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1285                if (video_register_device(&usbvision->rdev, VFL_TYPE_RADIO, radio_nr) < 0)
1286                        goto err_exit;
1287                printk(KERN_INFO "USBVision[%d]: registered USBVision Radio device %s [v4l2]\n",
1288                       usbvision->nr, video_device_node_name(&usbvision->rdev));
1289        }
1290        /* all done */
1291        return 0;
1292
1293 err_exit:
1294        dev_err(&usbvision->dev->dev,
1295                "USBVision[%d]: video_register_device() failed\n",
1296                        usbvision->nr);
1297        usbvision_unregister_video(usbvision);
1298        return res;
1299}
1300
1301/*
1302 * usbvision_alloc()
1303 *
1304 * This code allocates the struct usb_usbvision.
1305 * It is filled with default values.
1306 *
1307 * Returns NULL on error, a pointer to usb_usbvision else.
1308 *
1309 */
1310static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1311                                             struct usb_interface *intf)
1312{
1313        struct usb_usbvision *usbvision;
1314
1315        usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1316        if (!usbvision)
1317                return NULL;
1318
1319        usbvision->dev = dev;
1320        if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1321                goto err_free;
1322
1323        if (v4l2_ctrl_handler_init(&usbvision->hdl, 4))
1324                goto err_unreg;
1325        usbvision->v4l2_dev.ctrl_handler = &usbvision->hdl;
1326        mutex_init(&usbvision->v4l2_lock);
1327
1328        /* prepare control urb for control messages during interrupts */
1329        usbvision->ctrl_urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
1330        if (!usbvision->ctrl_urb)
1331                goto err_unreg;
1332
1333        return usbvision;
1334
1335err_unreg:
1336        v4l2_ctrl_handler_free(&usbvision->hdl);
1337        v4l2_device_unregister(&usbvision->v4l2_dev);
1338err_free:
1339        kfree(usbvision);
1340        return NULL;
1341}
1342
1343/*
1344 * usbvision_release()
1345 *
1346 * This code does final release of struct usb_usbvision. This happens
1347 * after the device is disconnected -and- all clients closed their files.
1348 *
1349 */
1350static void usbvision_release(struct usb_usbvision *usbvision)
1351{
1352        PDEBUG(DBG_PROBE, "");
1353
1354        usbvision->initialized = 0;
1355
1356        usbvision_remove_sysfs(&usbvision->vdev);
1357        usbvision_unregister_video(usbvision);
1358        kfree(usbvision->alt_max_pkt_size);
1359
1360        usb_free_urb(usbvision->ctrl_urb);
1361
1362        v4l2_ctrl_handler_free(&usbvision->hdl);
1363        v4l2_device_unregister(&usbvision->v4l2_dev);
1364        kfree(usbvision);
1365
1366        PDEBUG(DBG_PROBE, "success");
1367}
1368
1369
1370/*********************** usb interface **********************************/
1371
1372static void usbvision_configure_video(struct usb_usbvision *usbvision)
1373{
1374        int model;
1375
1376        if (!usbvision)
1377                return;
1378
1379        model = usbvision->dev_model;
1380        usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1381
1382        if (usbvision_device_data[usbvision->dev_model].vin_reg2_override) {
1383                usbvision->vin_reg2_preset =
1384                        usbvision_device_data[usbvision->dev_model].vin_reg2;
1385        } else {
1386                usbvision->vin_reg2_preset = 0;
1387        }
1388
1389        usbvision->tvnorm_id = usbvision_device_data[model].video_norm;
1390        usbvision->video_inputs = usbvision_device_data[model].video_channels;
1391        usbvision->ctl_input = 0;
1392        usbvision->radio_freq = 87.5 * 16000;
1393        usbvision->tv_freq = 400 * 16;
1394
1395        /* This should be here to make i2c clients to be able to register */
1396        /* first switch off audio */
1397        if (usbvision_device_data[model].audio_channels > 0)
1398                usbvision_audio_off(usbvision);
1399        /* and then power up the tuner */
1400        usbvision_power_on(usbvision);
1401        usbvision_i2c_register(usbvision);
1402}
1403
1404/*
1405 * usbvision_probe()
1406 *
1407 * This procedure queries device descriptor and accepts the interface
1408 * if it looks like USBVISION video device
1409 *
1410 */
1411static int usbvision_probe(struct usb_interface *intf,
1412                           const struct usb_device_id *devid)
1413{
1414        struct usb_device *dev = usb_get_dev(interface_to_usbdev(intf));
1415        struct usb_interface *uif;
1416        __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
1417        const struct usb_host_interface *interface;
1418        struct usb_usbvision *usbvision = NULL;
1419        const struct usb_endpoint_descriptor *endpoint;
1420        int model, i, ret;
1421
1422        PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
1423                                le16_to_cpu(dev->descriptor.idVendor),
1424                                le16_to_cpu(dev->descriptor.idProduct), ifnum);
1425
1426        model = devid->driver_info;
1427        if (model < 0 || model >= usbvision_device_data_size) {
1428                PDEBUG(DBG_PROBE, "model out of bounds %d", model);
1429                ret = -ENODEV;
1430                goto err_usb;
1431        }
1432        printk(KERN_INFO "%s: %s found\n", __func__,
1433                                usbvision_device_data[model].model_string);
1434
1435        if (usbvision_device_data[model].interface >= 0)
1436                interface = &dev->actconfig->interface[usbvision_device_data[model].interface]->altsetting[0];
1437        else if (ifnum < dev->actconfig->desc.bNumInterfaces)
1438                interface = &dev->actconfig->interface[ifnum]->altsetting[0];
1439        else {
1440                dev_err(&intf->dev, "interface %d is invalid, max is %d\n",
1441                    ifnum, dev->actconfig->desc.bNumInterfaces - 1);
1442                ret = -ENODEV;
1443                goto err_usb;
1444        }
1445
1446        if (interface->desc.bNumEndpoints < 2) {
1447                dev_err(&intf->dev, "interface %d has %d endpoints, but must have minimum 2\n",
1448                        ifnum, interface->desc.bNumEndpoints);
1449                ret = -ENODEV;
1450                goto err_usb;
1451        }
1452        endpoint = &interface->endpoint[1].desc;
1453
1454        if (!usb_endpoint_xfer_isoc(endpoint)) {
1455                dev_err(&intf->dev, "%s: interface %d. has non-ISO endpoint!\n",
1456                    __func__, ifnum);
1457                dev_err(&intf->dev, "%s: Endpoint attributes %d",
1458                    __func__, endpoint->bmAttributes);
1459                ret = -ENODEV;
1460                goto err_usb;
1461        }
1462        if (usb_endpoint_dir_out(endpoint)) {
1463                dev_err(&intf->dev, "%s: interface %d. has ISO OUT endpoint!\n",
1464                    __func__, ifnum);
1465                ret = -ENODEV;
1466                goto err_usb;
1467        }
1468
1469        usbvision = usbvision_alloc(dev, intf);
1470        if (!usbvision) {
1471                dev_err(&intf->dev, "%s: couldn't allocate USBVision struct\n", __func__);
1472                ret = -ENOMEM;
1473                goto err_usb;
1474        }
1475
1476        if (dev->descriptor.bNumConfigurations > 1)
1477                usbvision->bridge_type = BRIDGE_NT1004;
1478        else if (model == DAZZLE_DVC_90_REV_1_SECAM)
1479                usbvision->bridge_type = BRIDGE_NT1005;
1480        else
1481                usbvision->bridge_type = BRIDGE_NT1003;
1482        PDEBUG(DBG_PROBE, "bridge_type %d", usbvision->bridge_type);
1483
1484        /* compute alternate max packet sizes */
1485        uif = dev->actconfig->interface[0];
1486
1487        usbvision->num_alt = uif->num_altsetting;
1488        PDEBUG(DBG_PROBE, "Alternate settings: %i", usbvision->num_alt);
1489        usbvision->alt_max_pkt_size = kmalloc_array(32, usbvision->num_alt,
1490                                                    GFP_KERNEL);
1491        if (!usbvision->alt_max_pkt_size) {
1492                ret = -ENOMEM;
1493                goto err_pkt;
1494        }
1495
1496        for (i = 0; i < usbvision->num_alt; i++) {
1497                u16 tmp;
1498
1499                if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1500                        ret = -ENODEV;
1501                        goto err_pkt;
1502                }
1503
1504                tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
1505                                      wMaxPacketSize);
1506                usbvision->alt_max_pkt_size[i] =
1507                        (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
1508                PDEBUG(DBG_PROBE, "Alternate setting %i, max size= %i", i,
1509                       usbvision->alt_max_pkt_size[i]);
1510        }
1511
1512
1513        usbvision->nr = usbvision_nr++;
1514
1515        spin_lock_init(&usbvision->queue_lock);
1516        init_waitqueue_head(&usbvision->wait_frame);
1517        init_waitqueue_head(&usbvision->wait_stream);
1518
1519        usbvision->have_tuner = usbvision_device_data[model].tuner;
1520        if (usbvision->have_tuner)
1521                usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1522
1523        usbvision->dev_model = model;
1524        usbvision->remove_pending = 0;
1525        usbvision->iface = ifnum;
1526        usbvision->iface_alt = 0;
1527        usbvision->video_endp = endpoint->bEndpointAddress;
1528        usbvision->isoc_packet_size = 0;
1529        usbvision->usb_bandwidth = 0;
1530        usbvision->user = 0;
1531        usbvision->streaming = stream_off;
1532        usbvision_configure_video(usbvision);
1533        usbvision_register_video(usbvision);
1534
1535        usbvision_create_sysfs(&usbvision->vdev);
1536
1537        PDEBUG(DBG_PROBE, "success");
1538        return 0;
1539
1540err_pkt:
1541        usbvision_release(usbvision);
1542err_usb:
1543        usb_put_dev(dev);
1544        return ret;
1545}
1546
1547
1548/*
1549 * usbvision_disconnect()
1550 *
1551 * This procedure stops all driver activity, deallocates interface-private
1552 * structure (pointed by 'ptr') and after that driver should be removable
1553 * with no ill consequences.
1554 *
1555 */
1556static void usbvision_disconnect(struct usb_interface *intf)
1557{
1558        struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1559        int u;
1560
1561        PDEBUG(DBG_PROBE, "");
1562
1563        if (!usbvision) {
1564                pr_err("%s: usb_get_intfdata() failed\n", __func__);
1565                return;
1566        }
1567
1568        mutex_lock(&usbvision->v4l2_lock);
1569
1570        /* At this time we ask to cancel outstanding URBs */
1571        usbvision_stop_isoc(usbvision);
1572
1573        v4l2_device_disconnect(&usbvision->v4l2_dev);
1574        usbvision_i2c_unregister(usbvision);
1575        usbvision->remove_pending = 1;  /* Now all ISO data will be ignored */
1576        u = usbvision->user;
1577
1578        usb_put_dev(usbvision->dev);
1579        usbvision->dev = NULL;  /* USB device is no more */
1580
1581        mutex_unlock(&usbvision->v4l2_lock);
1582
1583        if (u) {
1584                printk(KERN_INFO "%s: In use, disconnect pending\n",
1585                       __func__);
1586                wake_up_interruptible(&usbvision->wait_frame);
1587                wake_up_interruptible(&usbvision->wait_stream);
1588        } else {
1589                usbvision_release(usbvision);
1590        }
1591
1592        PDEBUG(DBG_PROBE, "success");
1593}
1594
1595static struct usb_driver usbvision_driver = {
1596        .name           = "usbvision",
1597        .id_table       = usbvision_table,
1598        .probe          = usbvision_probe,
1599        .disconnect     = usbvision_disconnect,
1600};
1601
1602/*
1603 * usbvision_init()
1604 *
1605 * This code is run to initialize the driver.
1606 *
1607 */
1608static int __init usbvision_init(void)
1609{
1610        int err_code;
1611
1612        PDEBUG(DBG_PROBE, "");
1613
1614        PDEBUG(DBG_IO,  "IO      debugging is enabled [video]");
1615        PDEBUG(DBG_PROBE, "PROBE   debugging is enabled [video]");
1616        PDEBUG(DBG_MMAP, "MMAP    debugging is enabled [video]");
1617
1618        /* disable planar mode support unless compression enabled */
1619        if (isoc_mode != ISOC_MODE_COMPRESS) {
1620                /* FIXME : not the right way to set supported flag */
1621                usbvision_v4l2_format[6].supported = 0; /* V4L2_PIX_FMT_YVU420 */
1622                usbvision_v4l2_format[7].supported = 0; /* V4L2_PIX_FMT_YUV422P */
1623        }
1624
1625        err_code = usb_register(&usbvision_driver);
1626
1627        if (err_code == 0) {
1628                printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1629                PDEBUG(DBG_PROBE, "success");
1630        }
1631        return err_code;
1632}
1633
1634static void __exit usbvision_exit(void)
1635{
1636        PDEBUG(DBG_PROBE, "");
1637
1638        usb_deregister(&usbvision_driver);
1639        PDEBUG(DBG_PROBE, "success");
1640}
1641
1642module_init(usbvision_init);
1643module_exit(usbvision_exit);
1644