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    , "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" }
  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->user) {
 318                err_code = -EBUSY;
 319        } else {
 320                err_code = v4l2_fh_open(file);
 321                if (err_code)
 322                        goto unlock;
 323
 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);
 330                }
 331                if (err_code) {
 332                        /* Deallocate all buffers if trouble */
 333                        usbvision_scratch_free(usbvision);
 334                        usbvision_decompress_free(usbvision);
 335                }
 336        }
 337
 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;
 348                }
 349
 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);
 355
 356                        /* prepare queues */
 357                        usbvision_empty_framequeues(usbvision);
 358                        usbvision->user++;
 359                }
 360        }
 361
 362unlock:
 363        mutex_unlock(&usbvision->v4l2_lock);
 364
 365        PDEBUG(DBG_IO, "success");
 366        return err_code;
 367}
 368
 369/*
 370 * usbvision_v4l2_close()
 371 *
 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().
 375 *
 376 */
 377static int usbvision_v4l2_close(struct file *file)
 378{
 379        struct usb_usbvision *usbvision = video_drvdata(file);
 380
 381        PDEBUG(DBG_IO, "close");
 382
 383        mutex_lock(&usbvision->v4l2_lock);
 384        usbvision_audio_off(usbvision);
 385        usbvision_restart_isoc(usbvision);
 386        usbvision_stop_isoc(usbvision);
 387
 388        usbvision_decompress_free(usbvision);
 389        usbvision_frames_free(usbvision);
 390        usbvision_empty_framequeues(usbvision);
 391        usbvision_scratch_free(usbvision);
 392
 393        usbvision->user--;
 394        mutex_unlock(&usbvision->v4l2_lock);
 395
 396        if (usbvision->remove_pending) {
 397                printk(KERN_INFO "%s: Final disconnect\n", __func__);
 398                usbvision_release(usbvision);
 399                return 0;
 400        }
 401
 402        PDEBUG(DBG_IO, "success");
 403        return v4l2_fh_release(file);
 404}
 405
 406
 407/*
 408 * usbvision_ioctl()
 409 *
 410 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
 411 *
 412 */
 413#ifdef CONFIG_VIDEO_ADV_DEBUG
 414static int vidioc_g_register(struct file *file, void *priv,
 415                                struct v4l2_dbg_register *reg)
 416{
 417        struct usb_usbvision *usbvision = video_drvdata(file);
 418        int err_code;
 419
 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;
 427        }
 428        reg->val = err_code;
 429        reg->size = 1;
 430        return 0;
 431}
 432
 433static int vidioc_s_register(struct file *file, void *priv,
 434                                const struct v4l2_dbg_register *reg)
 435{
 436        struct usb_usbvision *usbvision = video_drvdata(file);
 437        int err_code;
 438
 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;
 446        }
 447        return 0;
 448}
 449#endif
 450
 451static int vidioc_querycap(struct file *file, void  *priv,
 452                                        struct v4l2_capability *vc)
 453{
 454        struct usb_usbvision *usbvision = video_drvdata(file);
 455
 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;
 468}
 469
 470static int vidioc_enum_input(struct file *file, void *priv,
 471                                struct v4l2_input *vi)
 472{
 473        struct usb_usbvision *usbvision = video_drvdata(file);
 474        int chan;
 475
 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*/
 482
 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;
 494                }
 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;
 518        }
 519        return 0;
 520}
 521
 522static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
 523{
 524        struct usb_usbvision *usbvision = video_drvdata(file);
 525
 526        *input = usbvision->ctl_input;
 527        return 0;
 528}
 529
 530static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
 531{
 532        struct usb_usbvision *usbvision = video_drvdata(file);
 533
 534        if (input >= usbvision->video_inputs)
 535                return -EINVAL;
 536
 537        usbvision_muxsel(usbvision, input);
 538        usbvision_set_input(usbvision);
 539        usbvision_set_output(usbvision,
 540                             usbvision->curwidth,
 541                             usbvision->curheight);
 542        return 0;
 543}
 544
 545static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
 546{
 547        struct usb_usbvision *usbvision = video_drvdata(file);
 548
 549        usbvision->tvnorm_id = id;
 550
 551        call_all(usbvision, video, s_std, usbvision->tvnorm_id);
 552        /* propagate the change to the decoder */
 553        usbvision_muxsel(usbvision, usbvision->ctl_input);
 554
 555        return 0;
 556}
 557
 558static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
 559{
 560        struct usb_usbvision *usbvision = video_drvdata(file);
 561
 562        *id = usbvision->tvnorm_id;
 563        return 0;
 564}
 565
 566static int vidioc_g_tuner(struct file *file, void *priv,
 567                                struct v4l2_tuner *vt)
 568{
 569        struct usb_usbvision *usbvision = video_drvdata(file);
 570
 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));
 577
 578        /* Let clients fill in the remainder of this struct */
 579        call_all(usbvision, tuner, g_tuner, vt);
 580
 581        return 0;
 582}
 583
 584static int vidioc_s_tuner(struct file *file, void *priv,
 585                                const struct v4l2_tuner *vt)
 586{
 587        struct usb_usbvision *usbvision = video_drvdata(file);
 588
 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);
 594
 595        return 0;
 596}
 597
 598static int vidioc_g_frequency(struct file *file, void *priv,
 599                                struct v4l2_frequency *freq)
 600{
 601        struct usb_usbvision *usbvision = video_drvdata(file);
 602
 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;
 610
 611        return 0;
 612}
 613
 614static int vidioc_s_frequency(struct file *file, void *priv,
 615                                const struct v4l2_frequency *freq)
 616{
 617        struct usb_usbvision *usbvision = video_drvdata(file);
 618        struct v4l2_frequency new_freq = *freq;
 619
 620        /* Only one tuner for now */
 621        if (freq->tuner)
 622                return -EINVAL;
 623
 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;
 630
 631        return 0;
 632}
 633
 634static int vidioc_reqbufs(struct file *file,
 635                           void *priv, struct v4l2_requestbuffers *vr)
 636{
 637        struct usb_usbvision *usbvision = video_drvdata(file);
 638        int ret;
 639
 640        RESTRICT_TO_RANGE(vr->count, 1, USBVISION_NUMFRAMES);
 641
 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;
 646
 647        if (usbvision->streaming == stream_on) {
 648                ret = usbvision_stream_interrupt(usbvision);
 649                if (ret)
 650                        return ret;
 651        }
 652
 653        usbvision_frames_free(usbvision);
 654        usbvision_empty_framequeues(usbvision);
 655        vr->count = usbvision_frames_alloc(usbvision, vr->count);
 656
 657        usbvision->cur_frame = NULL;
 658
 659        return 0;
 660}
 661
 662static int vidioc_querybuf(struct file *file,
 663                            void *priv, struct v4l2_buffer *vb)
 664{
 665        struct usb_usbvision *usbvision = video_drvdata(file);
 666        struct usbvision_frame *frame;
 667
 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;
 682
 683        vb->m.offset = vb->index * PAGE_ALIGN(usbvision->max_frame_size);
 684
 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;
 693}
 694
 695static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 696{
 697        struct usb_usbvision *usbvision = video_drvdata(file);
 698        struct usbvision_frame *frame;
 699        unsigned long lock_flags;
 700
 701        /* FIXME : works only on VIDEO_CAPTURE MODE, MMAP. */
 702        if (vb->index >= usbvision->num_frames)
 703                return -EINVAL;
 704
 705        frame = &usbvision->frame[vb->index];
 706
 707        if (frame->grabstate != frame_state_unused)
 708                return -EAGAIN;
 709
 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() */
 714
 715        vb->flags &= ~V4L2_BUF_FLAG_DONE;
 716
 717        /* set v4l2_format index */
 718        frame->v4l2_format = usbvision->palette;
 719
 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);
 723
 724        return 0;
 725}
 726
 727static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *vb)
 728{
 729        struct usb_usbvision *usbvision = video_drvdata(file);
 730        int ret;
 731        struct usbvision_frame *f;
 732        unsigned long lock_flags;
 733
 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;
 742        }
 743
 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);
 749
 750        f->grabstate = frame_state_unused;
 751
 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;
 762
 763        return 0;
 764}
 765
 766static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 767{
 768        struct usb_usbvision *usbvision = video_drvdata(file);
 769
 770        usbvision->streaming = stream_on;
 771        call_all(usbvision, video, s_stream, 1);
 772
 773        return 0;
 774}
 775
 776static int vidioc_streamoff(struct file *file,
 777                            void *priv, enum v4l2_buf_type type)
 778{
 779        struct usb_usbvision *usbvision = video_drvdata(file);
 780
 781        if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 782                return -EINVAL;
 783
 784        if (usbvision->streaming == stream_on) {
 785                usbvision_stream_interrupt(usbvision);
 786                /* Stop all video streamings */
 787                call_all(usbvision, video, s_stream, 0);
 788        }
 789        usbvision_empty_framequeues(usbvision);
 790
 791        return 0;
 792}
 793
 794static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 795                                        struct v4l2_fmtdesc *vfd)
 796{
 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;
 803}
 804
 805static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 806                                        struct v4l2_format *vf)
 807{
 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 */
 817
 818        return 0;
 819}
 820
 821static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 822                               struct v4l2_format *vf)
 823{
 824        struct usb_usbvision *usbvision = video_drvdata(file);
 825        int format_idx;
 826
 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;
 833                }
 834        }
 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);
 840
 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 */
 846
 847        return 0;
 848}
 849
 850static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 851                               struct v4l2_format *vf)
 852{
 853        struct usb_usbvision *usbvision = video_drvdata(file);
 854        int ret;
 855
 856        ret = vidioc_try_fmt_vid_cap(file, priv, vf);
 857        if (ret)
 858                return ret;
 859
 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;
 865        }
 866        usbvision_frames_free(usbvision);
 867        usbvision_empty_framequeues(usbvision);
 868
 869        usbvision->cur_frame = NULL;
 870
 871        /* by now we are committed to the new data... */
 872        usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
 873
 874        return 0;
 875}
 876
 877static ssize_t usbvision_read(struct file *file, char __user *buf,
 878                      size_t count, loff_t *ppos)
 879{
 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;
 885
 886        PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __func__,
 887               (unsigned long)count, noblock);
 888
 889        if (!USBVISION_IS_OPERATIONAL(usbvision) || !buf)
 890                return -EFAULT;
 891
 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);
 901        }
 902
 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);
 907        }
 908
 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;
 919
 920                        /* set v4l2_format index */
 921                        frame->v4l2_format = usbvision->palette;
 922
 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);
 927                }
 928        }
 929
 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;
 934
 935                ret = wait_event_interruptible
 936                        (usbvision->wait_frame,
 937                         !list_empty(&(usbvision->outqueue)));
 938                if (ret)
 939                        return ret;
 940        }
 941
 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);
 947
 948        /* An error returns an empty frame */
 949        if (frame->grabstate == frame_state_error) {
 950                frame->bytes_read = 0;
 951                return 0;
 952        }
 953
 954        PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld",
 955               __func__,
 956               frame->index, frame->bytes_read, frame->scanlength);
 957
 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;
 961
 962        if (copy_to_user(buf, frame->data + frame->bytes_read, count))
 963                return -EFAULT;
 964
 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);
 969
 970#if 1
 971        /*
 972         * FIXME:
 973         * For now, forget the frame if it has not been read in one shot.
 974         */
 975        frame->bytes_read = 0;
 976
 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;
 983
 984                /* Mark it as available to be used again. */
 985                frame->grabstate = frame_state_unused;
 986        }
 987#endif
 988
 989        return count;
 990}
 991
 992static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
 993                      size_t count, loff_t *ppos)
 994{
 995        struct usb_usbvision *usbvision = video_drvdata(file);
 996        int res;
 997
 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;
1003}
1004
1005static int usbvision_mmap(struct file *file, struct vm_area_struct *vma)
1006{
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);
1012
1013        PDEBUG(DBG_MMAP, "mmap");
1014
1015        if (!USBVISION_IS_OPERATIONAL(usbvision))
1016                return -EFAULT;
1017
1018        if (!(vma->vm_flags & VM_WRITE) ||
1019            size != PAGE_ALIGN(usbvision->max_frame_size)) {
1020                return -EINVAL;
1021        }
1022
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;
1027        }
1028        if (i == usbvision->num_frames) {
1029                PDEBUG(DBG_MMAP,
1030                       "mmap: user supplied mapping address is out of range");
1031                return -EINVAL;
1032        }
1033
1034        /* VM_IO is eventually going to replace PageReserved altogether */
1035        vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
1036
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;
1042                }
1043                start += PAGE_SIZE;
1044                pos += PAGE_SIZE;
1045                size -= PAGE_SIZE;
1046        }
1047
1048        return 0;
1049}
1050
1051static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
1052{
1053        struct usb_usbvision *usbvision = video_drvdata(file);
1054        int res;
1055
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;
1061}
1062
1063/*
1064 * Here comes the stuff for radio on usbvision based devices
1065 *
1066 */
1067static int usbvision_radio_open(struct file *file)
1068{
1069        struct usb_usbvision *usbvision = video_drvdata(file);
1070        int err_code = 0;
1071
1072        PDEBUG(DBG_IO, "%s:", __func__);
1073
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;
1091                }
1092
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++;
1098        }
1099out:
1100        mutex_unlock(&usbvision->v4l2_lock);
1101        return err_code;
1102}
1103
1104
1105static int usbvision_radio_close(struct file *file)
1106{
1107        struct usb_usbvision *usbvision = video_drvdata(file);
1108
1109        PDEBUG(DBG_IO, "");
1110
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);
1116
1117        usbvision_audio_off(usbvision);
1118        usbvision->radio = 0;
1119        usbvision->user--;
1120        mutex_unlock(&usbvision->v4l2_lock);
1121
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;
1127        }
1128
1129        PDEBUG(DBG_IO, "success");
1130        return v4l2_fh_release(file);
1131}
1132
1133/* Video registration stuff */
1134
1135/* Video template */
1136static 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,
1143};
1144
1145static 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
1173};
1174
1175static 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,
1181};
1182
1183
1184/* Radio template */
1185static 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,
1191};
1192
1193static 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,
1202};
1203
1204static 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,
1209};
1210
1211
1212static void usbvision_vdev_init(struct usb_usbvision *usbvision,
1213                                struct video_device *vdev,
1214                                const struct video_device *vdev_template,
1215                                const char *name)
1216{
1217        struct usb_device *usb_dev = usbvision->dev;
1218
1219        if (!usb_dev) {
1220                dev_err(&usbvision->dev->dev,
1221                        "%s: usbvision->dev is not set\n", __func__);
1222                return;
1223        }
1224
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);
1230}
1231
1232/* unregister video4linux devices */
1233static void usbvision_unregister_video(struct usb_usbvision *usbvision)
1234{
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);
1240        }
1241
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);
1247        }
1248}
1249
1250/* register video4linux devices */
1251static int usbvision_register_video(struct usb_usbvision *usbvision)
1252{
1253        int res = -ENOMEM;
1254
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);
1263        }
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;
1268
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));
1273
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));
1284        }
1285        /* all done */
1286        return 0;
1287
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;
1294}
1295
1296/*
1297 * usbvision_alloc()
1298 *
1299 * This code allocates the struct usb_usbvision.
1300 * It is filled with default values.
1301 *
1302 * Returns NULL on error, a pointer to usb_usbvision else.
1303 *
1304 */
1305static struct usb_usbvision *usbvision_alloc(struct usb_device *dev,
1306                                             struct usb_interface *intf)
1307{
1308        struct usb_usbvision *usbvision;
1309
1310        usbvision = kzalloc(sizeof(*usbvision), GFP_KERNEL);
1311        if (!usbvision)
1312                return NULL;
1313
1314        usbvision->dev = dev;
1315        if (v4l2_device_register(&intf->dev, &usbvision->v4l2_dev))
1316                goto err_free;
1317
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);
1322
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;
1327
1328        return usbvision;
1329
1330err_unreg:
1331        v4l2_ctrl_handler_free(&usbvision->hdl);
1332        v4l2_device_unregister(&usbvision->v4l2_dev);
1333err_free:
1334        kfree(usbvision);
1335        return NULL;
1336}
1337
1338/*
1339 * usbvision_release()
1340 *
1341 * This code does final release of struct usb_usbvision. This happens
1342 * after the device is disconnected -and- all clients closed their files.
1343 *
1344 */
1345static void usbvision_release(struct usb_usbvision *usbvision)
1346{
1347        PDEBUG(DBG_PROBE, "");
1348
1349        usbvision->initialized = 0;
1350
1351        usbvision_remove_sysfs(&usbvision->vdev);
1352        usbvision_unregister_video(usbvision);
1353        kfree(usbvision->alt_max_pkt_size);
1354
1355        usb_free_urb(usbvision->ctrl_urb);
1356
1357        v4l2_ctrl_handler_free(&usbvision->hdl);
1358        v4l2_device_unregister(&usbvision->v4l2_dev);
1359        kfree(usbvision);
1360
1361        PDEBUG(DBG_PROBE, "success");
1362}
1363
1364
1365/*********************** usb interface **********************************/
1366
1367static void usbvision_configure_video(struct usb_usbvision *usbvision)
1368{
1369        int model;
1370
1371        if (!usbvision)
1372                return;
1373
1374        model = usbvision->dev_model;
1375        usbvision->palette = usbvision_v4l2_format[2]; /* V4L2_PIX_FMT_RGB24; */
1376
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;
1382        }
1383
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;
1389
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);
1397}
1398
1399/*
1400 * usbvision_probe()
1401 *
1402 * This procedure queries device descriptor and accepts the interface
1403 * if it looks like USBVISION video device
1404 *
1405 */
1406static int usbvision_probe(struct usb_interface *intf,
1407                           const struct usb_device_id *devid)
1408{
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;
1416
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);
1420
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;
1426        }
1427        printk(KERN_INFO "%s: %s found\n", __func__,
1428                                usbvision_device_data[model].model_string);
1429
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;
1439        }
1440
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;
1446        }
1447        endpoint = &interface->endpoint[1].desc;
1448
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;
1456        }
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;
1462        }
1463
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;
1469        }
1470
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);
1478
1479        /* compute alternate max packet sizes */
1480        uif = dev->actconfig->interface[0];
1481
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;
1489        }
1490
1491        for (i = 0; i < usbvision->num_alt; i++) {
1492                u16 tmp;
1493
1494                if (uif->altsetting[i].desc.bNumEndpoints < 2) {
1495                        ret = -ENODEV;
1496                        goto err_pkt;
1497                }
1498
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]);
1505        }
1506
1507
1508        usbvision->nr = usbvision_nr++;
1509
1510        spin_lock_init(&usbvision->queue_lock);
1511        init_waitqueue_head(&usbvision->wait_frame);
1512        init_waitqueue_head(&usbvision->wait_stream);
1513
1514        usbvision->have_tuner = usbvision_device_data[model].tuner;
1515        if (usbvision->have_tuner)
1516                usbvision->tuner_type = usbvision_device_data[model].tuner_type;
1517
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);
1529
1530        usbvision_create_sysfs(&usbvision->vdev);
1531
1532        PDEBUG(DBG_PROBE, "success");
1533        return 0;
1534
1535err_pkt:
1536        usbvision_release(usbvision);
1537err_usb:
1538        usb_put_dev(dev);
1539        return ret;
1540}
1541
1542
1543/*
1544 * usbvision_disconnect()
1545 *
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.
1549 *
1550 */
1551static void usbvision_disconnect(struct usb_interface *intf)
1552{
1553        struct usb_usbvision *usbvision = to_usbvision(usb_get_intfdata(intf));
1554
1555        PDEBUG(DBG_PROBE, "");
1556
1557        if (!usbvision) {
1558                pr_err("%s: usb_get_intfdata() failed\n", __func__);
1559                return;
1560        }
1561
1562        mutex_lock(&usbvision->v4l2_lock);
1563
1564        /* At this time we ask to cancel outstanding URBs */
1565        usbvision_stop_isoc(usbvision);
1566
1567        v4l2_device_disconnect(&usbvision->v4l2_dev);
1568        usbvision_i2c_unregister(usbvision);
1569        usbvision->remove_pending = 1;  /* Now all ISO data will be ignored */
1570
1571        usb_put_dev(usbvision->dev);
1572        usbvision->dev = NULL;  /* USB device is no more */
1573
1574        mutex_unlock(&usbvision->v4l2_lock);
1575
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);
1583        }
1584
1585        PDEBUG(DBG_PROBE, "success");
1586}
1587
1588static struct usb_driver usbvision_driver = {
1589        .name           = "usbvision",
1590        .id_table       = usbvision_table,
1591        .probe          = usbvision_probe,
1592        .disconnect     = usbvision_disconnect,
1593};
1594
1595/*
1596 * usbvision_init()
1597 *
1598 * This code is run to initialize the driver.
1599 *
1600 */
1601static int __init usbvision_init(void)
1602{
1603        int err_code;
1604
1605        PDEBUG(DBG_PROBE, "");
1606
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]");
1610
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 */
1616        }
1617
1618        err_code = usb_register(&usbvision_driver);
1619
1620        if (err_code == 0) {
1621                printk(KERN_INFO DRIVER_DESC " : " USBVISION_VERSION_STRING "\n");
1622                PDEBUG(DBG_PROBE, "success");
1623        }
1624        return err_code;
1625}
1626
1627static void __exit usbvision_exit(void)
1628{
1629        PDEBUG(DBG_PROBE, "");
1630
1631        usb_deregister(&usbvision_driver);
1632        PDEBUG(DBG_PROBE, "success");
1633}
1634
1635module_init(usbvision_init);
1636module_exit(usbvision_exit);
1637