linux/drivers/media/video/hdpvr/hdpvr-video.c
<<
>>
Prefs
   1/*
   2 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
   3 *
   4 * Copyright (C) 2008      Janne Grunau (j@jannau.net)
   5 *
   6 *      This program is free software; you can redistribute it and/or
   7 *      modify it under the terms of the GNU General Public License as
   8 *      published by the Free Software Foundation, version 2.
   9 *
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/errno.h>
  14#include <linux/init.h>
  15#include <linux/slab.h>
  16#include <linux/module.h>
  17#include <linux/uaccess.h>
  18#include <linux/usb.h>
  19#include <linux/mutex.h>
  20#include <linux/version.h>
  21#include <linux/workqueue.h>
  22
  23#include <linux/videodev2.h>
  24#include <media/v4l2-dev.h>
  25#include <media/v4l2-common.h>
  26#include <media/v4l2-ioctl.h>
  27#include "hdpvr.h"
  28
  29#define BULK_URB_TIMEOUT   90 /* 0.09 seconds */
  30
  31#define print_buffer_status() { \
  32                v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,       \
  33                         "%s:%d buffer stat: %d free, %d proc\n",       \
  34                         __func__, __LINE__,                            \
  35                         list_size(&dev->free_buff_list),               \
  36                         list_size(&dev->rec_buff_list)); }
  37
  38struct hdpvr_fh {
  39        struct hdpvr_device     *dev;
  40};
  41
  42static uint list_size(struct list_head *list)
  43{
  44        struct list_head *tmp;
  45        uint count = 0;
  46
  47        list_for_each(tmp, list) {
  48                count++;
  49        }
  50
  51        return count;
  52}
  53
  54/*=========================================================================*/
  55/* urb callback */
  56static void hdpvr_read_bulk_callback(struct urb *urb)
  57{
  58        struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
  59        struct hdpvr_device *dev = buf->dev;
  60
  61        /* marking buffer as received and wake waiting */
  62        buf->status = BUFSTAT_READY;
  63        wake_up_interruptible(&dev->wait_data);
  64}
  65
  66/*=========================================================================*/
  67/* bufffer bits */
  68
  69/* function expects dev->io_mutex to be hold by caller */
  70int hdpvr_cancel_queue(struct hdpvr_device *dev)
  71{
  72        struct hdpvr_buffer *buf;
  73
  74        list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
  75                usb_kill_urb(buf->urb);
  76                buf->status = BUFSTAT_AVAILABLE;
  77        }
  78
  79        list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
  80
  81        return 0;
  82}
  83
  84static int hdpvr_free_queue(struct list_head *q)
  85{
  86        struct list_head *tmp;
  87        struct list_head *p;
  88        struct hdpvr_buffer *buf;
  89        struct urb *urb;
  90
  91        for (p = q->next; p != q;) {
  92                buf = list_entry(p, struct hdpvr_buffer, buff_list);
  93
  94                urb = buf->urb;
  95                usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  96                                  urb->transfer_buffer, urb->transfer_dma);
  97                usb_free_urb(urb);
  98                tmp = p->next;
  99                list_del(p);
 100                kfree(buf);
 101                p = tmp;
 102        }
 103
 104        return 0;
 105}
 106
 107/* function expects dev->io_mutex to be hold by caller */
 108int hdpvr_free_buffers(struct hdpvr_device *dev)
 109{
 110        hdpvr_cancel_queue(dev);
 111
 112        hdpvr_free_queue(&dev->free_buff_list);
 113        hdpvr_free_queue(&dev->rec_buff_list);
 114
 115        return 0;
 116}
 117
 118/* function expects dev->io_mutex to be hold by caller */
 119int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
 120{
 121        uint i;
 122        int retval = -ENOMEM;
 123        u8 *mem;
 124        struct hdpvr_buffer *buf;
 125        struct urb *urb;
 126
 127        v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
 128                 "allocating %u buffers\n", count);
 129
 130        for (i = 0; i < count; i++) {
 131
 132                buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
 133                if (!buf) {
 134                        v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
 135                        goto exit;
 136                }
 137                buf->dev = dev;
 138
 139                urb = usb_alloc_urb(0, GFP_KERNEL);
 140                if (!urb) {
 141                        v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
 142                        goto exit_urb;
 143                }
 144                buf->urb = urb;
 145
 146                mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
 147                                         &urb->transfer_dma);
 148                if (!mem) {
 149                        v4l2_err(&dev->v4l2_dev,
 150                                 "cannot allocate usb transfer buffer\n");
 151                        goto exit_urb_buffer;
 152                }
 153
 154                usb_fill_bulk_urb(buf->urb, dev->udev,
 155                                  usb_rcvbulkpipe(dev->udev,
 156                                                  dev->bulk_in_endpointAddr),
 157                                  mem, dev->bulk_in_size,
 158                                  hdpvr_read_bulk_callback, buf);
 159
 160                buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 161                buf->status = BUFSTAT_AVAILABLE;
 162                list_add_tail(&buf->buff_list, &dev->free_buff_list);
 163        }
 164        return 0;
 165exit_urb_buffer:
 166        usb_free_urb(urb);
 167exit_urb:
 168        kfree(buf);
 169exit:
 170        hdpvr_free_buffers(dev);
 171        return retval;
 172}
 173
 174static int hdpvr_submit_buffers(struct hdpvr_device *dev)
 175{
 176        struct hdpvr_buffer *buf;
 177        struct urb *urb;
 178        int ret = 0, err_count = 0;
 179
 180        mutex_lock(&dev->io_mutex);
 181
 182        while (dev->status == STATUS_STREAMING &&
 183               !list_empty(&dev->free_buff_list)) {
 184
 185                buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
 186                                 buff_list);
 187                if (buf->status != BUFSTAT_AVAILABLE) {
 188                        v4l2_err(&dev->v4l2_dev,
 189                                 "buffer not marked as available\n");
 190                        ret = -EFAULT;
 191                        goto err;
 192                }
 193
 194                urb = buf->urb;
 195                urb->status = 0;
 196                urb->actual_length = 0;
 197                ret = usb_submit_urb(urb, GFP_KERNEL);
 198                if (ret) {
 199                        v4l2_err(&dev->v4l2_dev,
 200                                 "usb_submit_urb in %s returned %d\n",
 201                                 __func__, ret);
 202                        if (++err_count > 2)
 203                                break;
 204                        continue;
 205                }
 206                buf->status = BUFSTAT_INPROGRESS;
 207                list_move_tail(&buf->buff_list, &dev->rec_buff_list);
 208        }
 209err:
 210        print_buffer_status();
 211        mutex_unlock(&dev->io_mutex);
 212        return ret;
 213}
 214
 215static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
 216{
 217        struct hdpvr_buffer *buf;
 218
 219        mutex_lock(&dev->io_mutex);
 220
 221        if (list_empty(&dev->rec_buff_list)) {
 222                mutex_unlock(&dev->io_mutex);
 223                return NULL;
 224        }
 225
 226        buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
 227                         buff_list);
 228        mutex_unlock(&dev->io_mutex);
 229
 230        return buf;
 231}
 232
 233static void hdpvr_transmit_buffers(struct work_struct *work)
 234{
 235        struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
 236                                                worker);
 237
 238        while (dev->status == STATUS_STREAMING) {
 239
 240                if (hdpvr_submit_buffers(dev)) {
 241                        v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
 242                        goto error;
 243                }
 244                if (wait_event_interruptible(dev->wait_buffer,
 245                                !list_empty(&dev->free_buff_list) ||
 246                                             dev->status != STATUS_STREAMING))
 247                        goto error;
 248        }
 249
 250        v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
 251                 "transmit worker exited\n");
 252        return;
 253error:
 254        v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
 255                 "transmit buffers errored\n");
 256        dev->status = STATUS_ERROR;
 257}
 258
 259/* function expects dev->io_mutex to be hold by caller */
 260static int hdpvr_start_streaming(struct hdpvr_device *dev)
 261{
 262        int ret;
 263        struct hdpvr_video_info *vidinf;
 264
 265        if (dev->status == STATUS_STREAMING)
 266                return 0;
 267        else if (dev->status != STATUS_IDLE)
 268                return -EAGAIN;
 269
 270        vidinf = get_video_info(dev);
 271
 272        if (vidinf) {
 273                v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 274                         "video signal: %dx%d@%dhz\n", vidinf->width,
 275                         vidinf->height, vidinf->fps);
 276                kfree(vidinf);
 277
 278                /* start streaming 2 request */
 279                ret = usb_control_msg(dev->udev,
 280                                      usb_sndctrlpipe(dev->udev, 0),
 281                                      0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
 282                v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 283                         "encoder start control request returned %d\n", ret);
 284
 285                hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
 286
 287                INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
 288                queue_work(dev->workqueue, &dev->worker);
 289
 290                v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 291                         "streaming started\n");
 292                dev->status = STATUS_STREAMING;
 293
 294                return 0;
 295        }
 296        msleep(250);
 297        v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
 298                 "no video signal at input %d\n", dev->options.video_input);
 299        return -EAGAIN;
 300}
 301
 302
 303/* function expects dev->io_mutex to be hold by caller */
 304static int hdpvr_stop_streaming(struct hdpvr_device *dev)
 305{
 306        int actual_length;
 307        uint c = 0;
 308        u8 *buf;
 309
 310        if (dev->status == STATUS_IDLE)
 311                return 0;
 312        else if (dev->status != STATUS_STREAMING)
 313                return -EAGAIN;
 314
 315        buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
 316        if (!buf)
 317                v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
 318                         "for emptying the internal device buffer. "
 319                         "Next capture start will be slow\n");
 320
 321        dev->status = STATUS_SHUTTING_DOWN;
 322        hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
 323        mutex_unlock(&dev->io_mutex);
 324
 325        wake_up_interruptible(&dev->wait_buffer);
 326        msleep(50);
 327
 328        flush_workqueue(dev->workqueue);
 329
 330        mutex_lock(&dev->io_mutex);
 331        /* kill the still outstanding urbs */
 332        hdpvr_cancel_queue(dev);
 333
 334        /* emptying the device buffer beforeshutting it down */
 335        while (buf && ++c < 500 &&
 336               !usb_bulk_msg(dev->udev,
 337                             usb_rcvbulkpipe(dev->udev,
 338                                             dev->bulk_in_endpointAddr),
 339                             buf, dev->bulk_in_size, &actual_length,
 340                             BULK_URB_TIMEOUT)) {
 341                v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 342                         "%2d: got %d bytes\n", c, actual_length);
 343        }
 344        kfree(buf);
 345        v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 346                 "used %d urbs to empty device buffers\n", c-1);
 347        msleep(10);
 348
 349        dev->status = STATUS_IDLE;
 350
 351        return 0;
 352}
 353
 354
 355/*=======================================================================*/
 356/*
 357 * video 4 linux 2 file operations
 358 */
 359
 360static int hdpvr_open(struct file *file)
 361{
 362        struct hdpvr_device *dev;
 363        struct hdpvr_fh *fh;
 364        int retval = -ENOMEM;
 365
 366        dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
 367        if (!dev) {
 368                pr_err("open failing with with ENODEV\n");
 369                retval = -ENODEV;
 370                goto err;
 371        }
 372
 373        fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
 374        if (!fh) {
 375                v4l2_err(&dev->v4l2_dev, "Out of memory\n");
 376                goto err;
 377        }
 378        /* lock the device to allow correctly handling errors
 379         * in resumption */
 380        mutex_lock(&dev->io_mutex);
 381        dev->open_count++;
 382        mutex_unlock(&dev->io_mutex);
 383
 384        fh->dev = dev;
 385
 386        /* save our object in the file's private structure */
 387        file->private_data = fh;
 388
 389        retval = 0;
 390err:
 391        return retval;
 392}
 393
 394static int hdpvr_release(struct file *file)
 395{
 396        struct hdpvr_fh         *fh  = file->private_data;
 397        struct hdpvr_device     *dev = fh->dev;
 398
 399        if (!dev)
 400                return -ENODEV;
 401
 402        mutex_lock(&dev->io_mutex);
 403        if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
 404                hdpvr_stop_streaming(dev);
 405
 406        mutex_unlock(&dev->io_mutex);
 407
 408        return 0;
 409}
 410
 411/*
 412 * hdpvr_v4l2_read()
 413 * will allocate buffers when called for the first time
 414 */
 415static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
 416                          loff_t *pos)
 417{
 418        struct hdpvr_fh *fh = file->private_data;
 419        struct hdpvr_device *dev = fh->dev;
 420        struct hdpvr_buffer *buf = NULL;
 421        struct urb *urb;
 422        unsigned int ret = 0;
 423        int rem, cnt;
 424
 425        if (*pos)
 426                return -ESPIPE;
 427
 428        if (!dev)
 429                return -ENODEV;
 430
 431        mutex_lock(&dev->io_mutex);
 432        if (dev->status == STATUS_IDLE) {
 433                if (hdpvr_start_streaming(dev)) {
 434                        v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
 435                                 "start_streaming failed\n");
 436                        ret = -EIO;
 437                        msleep(200);
 438                        dev->status = STATUS_IDLE;
 439                        mutex_unlock(&dev->io_mutex);
 440                        goto err;
 441                }
 442                print_buffer_status();
 443        }
 444        mutex_unlock(&dev->io_mutex);
 445
 446        /* wait for the first buffer */
 447        if (!(file->f_flags & O_NONBLOCK)) {
 448                if (wait_event_interruptible(dev->wait_data,
 449                                             hdpvr_get_next_buffer(dev)))
 450                        return -ERESTARTSYS;
 451        }
 452
 453        buf = hdpvr_get_next_buffer(dev);
 454
 455        while (count > 0 && buf) {
 456
 457                if (buf->status != BUFSTAT_READY &&
 458                    dev->status != STATUS_DISCONNECTED) {
 459                        /* return nonblocking */
 460                        if (file->f_flags & O_NONBLOCK) {
 461                                if (!ret)
 462                                        ret = -EAGAIN;
 463                                goto err;
 464                        }
 465
 466                        if (wait_event_interruptible(dev->wait_data,
 467                                              buf->status == BUFSTAT_READY)) {
 468                                ret = -ERESTARTSYS;
 469                                goto err;
 470                        }
 471                }
 472
 473                if (buf->status != BUFSTAT_READY)
 474                        break;
 475
 476                /* set remaining bytes to copy */
 477                urb = buf->urb;
 478                rem = urb->actual_length - buf->pos;
 479                cnt = rem > count ? count : rem;
 480
 481                if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
 482                                 cnt)) {
 483                        v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
 484                        if (!ret)
 485                                ret = -EFAULT;
 486                        goto err;
 487                }
 488
 489                buf->pos += cnt;
 490                count -= cnt;
 491                buffer += cnt;
 492                ret += cnt;
 493
 494                /* finished, take next buffer */
 495                if (buf->pos == urb->actual_length) {
 496                        mutex_lock(&dev->io_mutex);
 497                        buf->pos = 0;
 498                        buf->status = BUFSTAT_AVAILABLE;
 499
 500                        list_move_tail(&buf->buff_list, &dev->free_buff_list);
 501
 502                        print_buffer_status();
 503
 504                        mutex_unlock(&dev->io_mutex);
 505
 506                        wake_up_interruptible(&dev->wait_buffer);
 507
 508                        buf = hdpvr_get_next_buffer(dev);
 509                }
 510        }
 511err:
 512        if (!ret && !buf)
 513                ret = -EAGAIN;
 514        return ret;
 515}
 516
 517static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
 518{
 519        struct hdpvr_buffer *buf = NULL;
 520        struct hdpvr_fh *fh = filp->private_data;
 521        struct hdpvr_device *dev = fh->dev;
 522        unsigned int mask = 0;
 523
 524        mutex_lock(&dev->io_mutex);
 525
 526        if (!video_is_registered(dev->video_dev)) {
 527                mutex_unlock(&dev->io_mutex);
 528                return -EIO;
 529        }
 530
 531        if (dev->status == STATUS_IDLE) {
 532                if (hdpvr_start_streaming(dev)) {
 533                        v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
 534                                 "start_streaming failed\n");
 535                        dev->status = STATUS_IDLE;
 536                }
 537
 538                print_buffer_status();
 539        }
 540        mutex_unlock(&dev->io_mutex);
 541
 542        buf = hdpvr_get_next_buffer(dev);
 543        /* only wait if no data is available */
 544        if (!buf || buf->status != BUFSTAT_READY) {
 545                poll_wait(filp, &dev->wait_data, wait);
 546                buf = hdpvr_get_next_buffer(dev);
 547        }
 548        if (buf && buf->status == BUFSTAT_READY)
 549                mask |= POLLIN | POLLRDNORM;
 550
 551        return mask;
 552}
 553
 554
 555static const struct v4l2_file_operations hdpvr_fops = {
 556        .owner          = THIS_MODULE,
 557        .open           = hdpvr_open,
 558        .release        = hdpvr_release,
 559        .read           = hdpvr_read,
 560        .poll           = hdpvr_poll,
 561        .unlocked_ioctl = video_ioctl2,
 562};
 563
 564/*=======================================================================*/
 565/*
 566 * V4L2 ioctl handling
 567 */
 568
 569static int vidioc_querycap(struct file *file, void  *priv,
 570                           struct v4l2_capability *cap)
 571{
 572        struct hdpvr_device *dev = video_drvdata(file);
 573
 574        strcpy(cap->driver, "hdpvr");
 575        strcpy(cap->card, "Hauppauge HD PVR");
 576        usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
 577        cap->version = HDPVR_VERSION;
 578        cap->capabilities =     V4L2_CAP_VIDEO_CAPTURE |
 579                                V4L2_CAP_AUDIO         |
 580                                V4L2_CAP_READWRITE;
 581        return 0;
 582}
 583
 584static int vidioc_s_std(struct file *file, void *private_data,
 585                        v4l2_std_id *std)
 586{
 587        struct hdpvr_fh *fh = file->private_data;
 588        struct hdpvr_device *dev = fh->dev;
 589        u8 std_type = 1;
 590
 591        if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
 592                std_type = 0;
 593
 594        return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
 595}
 596
 597static const char *iname[] = {
 598        [HDPVR_COMPONENT] = "Component",
 599        [HDPVR_SVIDEO]    = "S-Video",
 600        [HDPVR_COMPOSITE] = "Composite",
 601};
 602
 603static int vidioc_enum_input(struct file *file, void *priv,
 604                                struct v4l2_input *i)
 605{
 606        struct hdpvr_fh *fh = file->private_data;
 607        struct hdpvr_device *dev = fh->dev;
 608        unsigned int n;
 609
 610        n = i->index;
 611        if (n >= HDPVR_VIDEO_INPUTS)
 612                return -EINVAL;
 613
 614        i->type = V4L2_INPUT_TYPE_CAMERA;
 615
 616        strncpy(i->name, iname[n], sizeof(i->name) - 1);
 617        i->name[sizeof(i->name) - 1] = '\0';
 618
 619        i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
 620
 621        i->std = dev->video_dev->tvnorms;
 622
 623        return 0;
 624}
 625
 626static int vidioc_s_input(struct file *file, void *private_data,
 627                          unsigned int index)
 628{
 629        struct hdpvr_fh *fh = file->private_data;
 630        struct hdpvr_device *dev = fh->dev;
 631        int retval;
 632
 633        if (index >= HDPVR_VIDEO_INPUTS)
 634                return -EINVAL;
 635
 636        if (dev->status != STATUS_IDLE)
 637                return -EAGAIN;
 638
 639        retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
 640        if (!retval)
 641                dev->options.video_input = index;
 642
 643        return retval;
 644}
 645
 646static int vidioc_g_input(struct file *file, void *private_data,
 647                          unsigned int *index)
 648{
 649        struct hdpvr_fh *fh = file->private_data;
 650        struct hdpvr_device *dev = fh->dev;
 651
 652        *index = dev->options.video_input;
 653        return 0;
 654}
 655
 656
 657static const char *audio_iname[] = {
 658        [HDPVR_RCA_FRONT] = "RCA front",
 659        [HDPVR_RCA_BACK]  = "RCA back",
 660        [HDPVR_SPDIF]     = "SPDIF",
 661};
 662
 663static int vidioc_enumaudio(struct file *file, void *priv,
 664                                struct v4l2_audio *audio)
 665{
 666        unsigned int n;
 667
 668        n = audio->index;
 669        if (n >= HDPVR_AUDIO_INPUTS)
 670                return -EINVAL;
 671
 672        audio->capability = V4L2_AUDCAP_STEREO;
 673
 674        strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
 675        audio->name[sizeof(audio->name) - 1] = '\0';
 676
 677        return 0;
 678}
 679
 680static int vidioc_s_audio(struct file *file, void *private_data,
 681                          struct v4l2_audio *audio)
 682{
 683        struct hdpvr_fh *fh = file->private_data;
 684        struct hdpvr_device *dev = fh->dev;
 685        int retval;
 686
 687        if (audio->index >= HDPVR_AUDIO_INPUTS)
 688                return -EINVAL;
 689
 690        if (dev->status != STATUS_IDLE)
 691                return -EAGAIN;
 692
 693        retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
 694        if (!retval)
 695                dev->options.audio_input = audio->index;
 696
 697        return retval;
 698}
 699
 700static int vidioc_g_audio(struct file *file, void *private_data,
 701                          struct v4l2_audio *audio)
 702{
 703        struct hdpvr_fh *fh = file->private_data;
 704        struct hdpvr_device *dev = fh->dev;
 705
 706        audio->index = dev->options.audio_input;
 707        audio->capability = V4L2_AUDCAP_STEREO;
 708        strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
 709        audio->name[sizeof(audio->name) - 1] = '\0';
 710        return 0;
 711}
 712
 713static const s32 supported_v4l2_ctrls[] = {
 714        V4L2_CID_BRIGHTNESS,
 715        V4L2_CID_CONTRAST,
 716        V4L2_CID_SATURATION,
 717        V4L2_CID_HUE,
 718        V4L2_CID_SHARPNESS,
 719        V4L2_CID_MPEG_AUDIO_ENCODING,
 720        V4L2_CID_MPEG_VIDEO_ENCODING,
 721        V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
 722        V4L2_CID_MPEG_VIDEO_BITRATE,
 723        V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
 724};
 725
 726static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
 727                          int ac3)
 728{
 729        int err;
 730
 731        switch (qc->id) {
 732        case V4L2_CID_BRIGHTNESS:
 733                return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
 734        case V4L2_CID_CONTRAST:
 735                return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
 736        case V4L2_CID_SATURATION:
 737                return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
 738        case V4L2_CID_HUE:
 739                return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
 740        case V4L2_CID_SHARPNESS:
 741                return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
 742        case V4L2_CID_MPEG_AUDIO_ENCODING:
 743                return v4l2_ctrl_query_fill(
 744                        qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
 745                        ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
 746                        : V4L2_MPEG_AUDIO_ENCODING_AAC,
 747                        1, V4L2_MPEG_AUDIO_ENCODING_AAC);
 748        case V4L2_CID_MPEG_VIDEO_ENCODING:
 749                return v4l2_ctrl_query_fill(
 750                        qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
 751                        V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
 752                        V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
 753
 754/*      case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
 755/*              return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
 756        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 757                return v4l2_ctrl_query_fill(
 758                        qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
 759                        V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
 760                        V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
 761
 762        case V4L2_CID_MPEG_VIDEO_BITRATE:
 763                return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
 764                                            6500000);
 765        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
 766                err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
 767                                           9000000);
 768                if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
 769                        qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
 770                return err;
 771        default:
 772                return -EINVAL;
 773        }
 774}
 775
 776static int vidioc_queryctrl(struct file *file, void *private_data,
 777                            struct v4l2_queryctrl *qc)
 778{
 779        struct hdpvr_fh *fh = file->private_data;
 780        struct hdpvr_device *dev = fh->dev;
 781        int i, next;
 782        u32 id = qc->id;
 783
 784        memset(qc, 0, sizeof(*qc));
 785
 786        next = !!(id &  V4L2_CTRL_FLAG_NEXT_CTRL);
 787        qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
 788
 789        for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
 790                if (next) {
 791                        if (qc->id < supported_v4l2_ctrls[i])
 792                                qc->id = supported_v4l2_ctrls[i];
 793                        else
 794                                continue;
 795                }
 796
 797                if (qc->id == supported_v4l2_ctrls[i])
 798                        return fill_queryctrl(&dev->options, qc,
 799                                              dev->flags & HDPVR_FLAG_AC3_CAP);
 800
 801                if (qc->id < supported_v4l2_ctrls[i])
 802                        break;
 803        }
 804
 805        return -EINVAL;
 806}
 807
 808static int vidioc_g_ctrl(struct file *file, void *private_data,
 809                         struct v4l2_control *ctrl)
 810{
 811        struct hdpvr_fh *fh = file->private_data;
 812        struct hdpvr_device *dev = fh->dev;
 813
 814        switch (ctrl->id) {
 815        case V4L2_CID_BRIGHTNESS:
 816                ctrl->value = dev->options.brightness;
 817                break;
 818        case V4L2_CID_CONTRAST:
 819                ctrl->value = dev->options.contrast;
 820                break;
 821        case V4L2_CID_SATURATION:
 822                ctrl->value = dev->options.saturation;
 823                break;
 824        case V4L2_CID_HUE:
 825                ctrl->value = dev->options.hue;
 826                break;
 827        case V4L2_CID_SHARPNESS:
 828                ctrl->value = dev->options.sharpness;
 829                break;
 830        default:
 831                return -EINVAL;
 832        }
 833        return 0;
 834}
 835
 836static int vidioc_s_ctrl(struct file *file, void *private_data,
 837                         struct v4l2_control *ctrl)
 838{
 839        struct hdpvr_fh *fh = file->private_data;
 840        struct hdpvr_device *dev = fh->dev;
 841        int retval;
 842
 843        switch (ctrl->id) {
 844        case V4L2_CID_BRIGHTNESS:
 845                retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
 846                if (!retval)
 847                        dev->options.brightness = ctrl->value;
 848                break;
 849        case V4L2_CID_CONTRAST:
 850                retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
 851                if (!retval)
 852                        dev->options.contrast = ctrl->value;
 853                break;
 854        case V4L2_CID_SATURATION:
 855                retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
 856                if (!retval)
 857                        dev->options.saturation = ctrl->value;
 858                break;
 859        case V4L2_CID_HUE:
 860                retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
 861                if (!retval)
 862                        dev->options.hue = ctrl->value;
 863                break;
 864        case V4L2_CID_SHARPNESS:
 865                retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
 866                if (!retval)
 867                        dev->options.sharpness = ctrl->value;
 868                break;
 869        default:
 870                return -EINVAL;
 871        }
 872
 873        return retval;
 874}
 875
 876
 877static int hdpvr_get_ctrl(struct hdpvr_options *opt,
 878                          struct v4l2_ext_control *ctrl)
 879{
 880        switch (ctrl->id) {
 881        case V4L2_CID_MPEG_AUDIO_ENCODING:
 882                ctrl->value = opt->audio_codec;
 883                break;
 884        case V4L2_CID_MPEG_VIDEO_ENCODING:
 885                ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
 886                break;
 887/*      case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
 888/*              ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
 889/*              break; */
 890        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 891                ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
 892                        ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
 893                        : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
 894                break;
 895        case V4L2_CID_MPEG_VIDEO_BITRATE:
 896                ctrl->value = opt->bitrate * 100000;
 897                break;
 898        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
 899                ctrl->value = opt->peak_bitrate * 100000;
 900                break;
 901        case V4L2_CID_MPEG_STREAM_TYPE:
 902                ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
 903                break;
 904        default:
 905                return -EINVAL;
 906        }
 907        return 0;
 908}
 909
 910static int vidioc_g_ext_ctrls(struct file *file, void *priv,
 911                              struct v4l2_ext_controls *ctrls)
 912{
 913        struct hdpvr_fh *fh = file->private_data;
 914        struct hdpvr_device *dev = fh->dev;
 915        int i, err = 0;
 916
 917        if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
 918                for (i = 0; i < ctrls->count; i++) {
 919                        struct v4l2_ext_control *ctrl = ctrls->controls + i;
 920
 921                        err = hdpvr_get_ctrl(&dev->options, ctrl);
 922                        if (err) {
 923                                ctrls->error_idx = i;
 924                                break;
 925                        }
 926                }
 927                return err;
 928
 929        }
 930
 931        return -EINVAL;
 932}
 933
 934
 935static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
 936{
 937        int ret = -EINVAL;
 938
 939        switch (ctrl->id) {
 940        case V4L2_CID_MPEG_AUDIO_ENCODING:
 941                if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
 942                    (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
 943                        ret = 0;
 944                break;
 945        case V4L2_CID_MPEG_VIDEO_ENCODING:
 946                if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
 947                        ret = 0;
 948                break;
 949/*      case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
 950/*              if (ctrl->value == 0 || ctrl->value == 128) */
 951/*                      ret = 0; */
 952/*              break; */
 953        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
 954                if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
 955                    ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
 956                        ret = 0;
 957                break;
 958        case V4L2_CID_MPEG_VIDEO_BITRATE:
 959        {
 960                uint bitrate = ctrl->value / 100000;
 961                if (bitrate >= 10 && bitrate <= 135)
 962                        ret = 0;
 963                break;
 964        }
 965        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
 966        {
 967                uint peak_bitrate = ctrl->value / 100000;
 968                if (peak_bitrate >= 10 && peak_bitrate <= 202)
 969                        ret = 0;
 970                break;
 971        }
 972        case V4L2_CID_MPEG_STREAM_TYPE:
 973                if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
 974                        ret = 0;
 975                break;
 976        default:
 977                return -EINVAL;
 978        }
 979        return 0;
 980}
 981
 982static int vidioc_try_ext_ctrls(struct file *file, void *priv,
 983                                struct v4l2_ext_controls *ctrls)
 984{
 985        struct hdpvr_fh *fh = file->private_data;
 986        struct hdpvr_device *dev = fh->dev;
 987        int i, err = 0;
 988
 989        if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
 990                for (i = 0; i < ctrls->count; i++) {
 991                        struct v4l2_ext_control *ctrl = ctrls->controls + i;
 992
 993                        err = hdpvr_try_ctrl(ctrl,
 994                                             dev->flags & HDPVR_FLAG_AC3_CAP);
 995                        if (err) {
 996                                ctrls->error_idx = i;
 997                                break;
 998                        }
 999                }
1000                return err;
1001        }
1002
1003        return -EINVAL;
1004}
1005
1006
1007static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1008                          struct v4l2_ext_control *ctrl)
1009{
1010        struct hdpvr_options *opt = &dev->options;
1011        int ret = 0;
1012
1013        switch (ctrl->id) {
1014        case V4L2_CID_MPEG_AUDIO_ENCODING:
1015                if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1016                        opt->audio_codec = ctrl->value;
1017                        ret = hdpvr_set_audio(dev, opt->audio_input,
1018                                              opt->audio_codec);
1019                }
1020                break;
1021        case V4L2_CID_MPEG_VIDEO_ENCODING:
1022                break;
1023/*      case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1024/*              if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1025/*                      opt->gop_mode |= 0x2; */
1026/*                      hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1027/*                                        opt->gop_mode); */
1028/*              } */
1029/*              if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1030/*                      opt->gop_mode &= ~0x2; */
1031/*                      hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1032/*                                        opt->gop_mode); */
1033/*              } */
1034/*              break; */
1035        case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1036                if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1037                    opt->bitrate_mode != HDPVR_CONSTANT) {
1038                        opt->bitrate_mode = HDPVR_CONSTANT;
1039                        hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1040                                          opt->bitrate_mode);
1041                }
1042                if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1043                    opt->bitrate_mode == HDPVR_CONSTANT) {
1044                        opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1045                        hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1046                                          opt->bitrate_mode);
1047                }
1048                break;
1049        case V4L2_CID_MPEG_VIDEO_BITRATE: {
1050                uint bitrate = ctrl->value / 100000;
1051
1052                opt->bitrate = bitrate;
1053                if (bitrate >= opt->peak_bitrate)
1054                        opt->peak_bitrate = bitrate+1;
1055
1056                hdpvr_set_bitrate(dev);
1057                break;
1058        }
1059        case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1060                uint peak_bitrate = ctrl->value / 100000;
1061
1062                if (opt->bitrate_mode == HDPVR_CONSTANT)
1063                        break;
1064
1065                if (opt->bitrate < peak_bitrate) {
1066                        opt->peak_bitrate = peak_bitrate;
1067                        hdpvr_set_bitrate(dev);
1068                } else
1069                        ret = -EINVAL;
1070                break;
1071        }
1072        case V4L2_CID_MPEG_STREAM_TYPE:
1073                break;
1074        default:
1075                return -EINVAL;
1076        }
1077        return ret;
1078}
1079
1080static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1081                              struct v4l2_ext_controls *ctrls)
1082{
1083        struct hdpvr_fh *fh = file->private_data;
1084        struct hdpvr_device *dev = fh->dev;
1085        int i, err = 0;
1086
1087        if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1088                for (i = 0; i < ctrls->count; i++) {
1089                        struct v4l2_ext_control *ctrl = ctrls->controls + i;
1090
1091                        err = hdpvr_try_ctrl(ctrl,
1092                                             dev->flags & HDPVR_FLAG_AC3_CAP);
1093                        if (err) {
1094                                ctrls->error_idx = i;
1095                                break;
1096                        }
1097                        err = hdpvr_set_ctrl(dev, ctrl);
1098                        if (err) {
1099                                ctrls->error_idx = i;
1100                                break;
1101                        }
1102                }
1103                return err;
1104
1105        }
1106
1107        return -EINVAL;
1108}
1109
1110static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1111                                    struct v4l2_fmtdesc *f)
1112{
1113
1114        if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1115                return -EINVAL;
1116
1117        f->flags = V4L2_FMT_FLAG_COMPRESSED;
1118        strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1119        f->pixelformat = V4L2_PIX_FMT_MPEG;
1120
1121        return 0;
1122}
1123
1124static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1125                                struct v4l2_format *f)
1126{
1127        struct hdpvr_fh *fh = file->private_data;
1128        struct hdpvr_device *dev = fh->dev;
1129        struct hdpvr_video_info *vid_info;
1130
1131        if (!dev)
1132                return -ENODEV;
1133
1134        vid_info = get_video_info(dev);
1135        if (!vid_info)
1136                return -EFAULT;
1137
1138        f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1139        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
1140        f->fmt.pix.width        = vid_info->width;
1141        f->fmt.pix.height       = vid_info->height;
1142        f->fmt.pix.sizeimage    = dev->bulk_in_size;
1143        f->fmt.pix.colorspace   = 0;
1144        f->fmt.pix.bytesperline = 0;
1145        f->fmt.pix.field        = V4L2_FIELD_ANY;
1146
1147        kfree(vid_info);
1148        return 0;
1149}
1150
1151static int vidioc_encoder_cmd(struct file *filp, void *priv,
1152                               struct v4l2_encoder_cmd *a)
1153{
1154        struct hdpvr_fh *fh = filp->private_data;
1155        struct hdpvr_device *dev = fh->dev;
1156        int res;
1157
1158        mutex_lock(&dev->io_mutex);
1159
1160        memset(&a->raw, 0, sizeof(a->raw));
1161        switch (a->cmd) {
1162        case V4L2_ENC_CMD_START:
1163                a->flags = 0;
1164                res = hdpvr_start_streaming(dev);
1165                break;
1166        case V4L2_ENC_CMD_STOP:
1167                res = hdpvr_stop_streaming(dev);
1168                break;
1169        default:
1170                v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1171                         "Unsupported encoder cmd %d\n", a->cmd);
1172                res = -EINVAL;
1173        }
1174        mutex_unlock(&dev->io_mutex);
1175        return res;
1176}
1177
1178static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1179                                        struct v4l2_encoder_cmd *a)
1180{
1181        switch (a->cmd) {
1182        case V4L2_ENC_CMD_START:
1183        case V4L2_ENC_CMD_STOP:
1184                return 0;
1185        default:
1186                return -EINVAL;
1187        }
1188}
1189
1190static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1191        .vidioc_querycap        = vidioc_querycap,
1192        .vidioc_s_std           = vidioc_s_std,
1193        .vidioc_enum_input      = vidioc_enum_input,
1194        .vidioc_g_input         = vidioc_g_input,
1195        .vidioc_s_input         = vidioc_s_input,
1196        .vidioc_enumaudio       = vidioc_enumaudio,
1197        .vidioc_g_audio         = vidioc_g_audio,
1198        .vidioc_s_audio         = vidioc_s_audio,
1199        .vidioc_queryctrl       = vidioc_queryctrl,
1200        .vidioc_g_ctrl          = vidioc_g_ctrl,
1201        .vidioc_s_ctrl          = vidioc_s_ctrl,
1202        .vidioc_g_ext_ctrls     = vidioc_g_ext_ctrls,
1203        .vidioc_s_ext_ctrls     = vidioc_s_ext_ctrls,
1204        .vidioc_try_ext_ctrls   = vidioc_try_ext_ctrls,
1205        .vidioc_enum_fmt_vid_cap        = vidioc_enum_fmt_vid_cap,
1206        .vidioc_g_fmt_vid_cap           = vidioc_g_fmt_vid_cap,
1207        .vidioc_encoder_cmd     = vidioc_encoder_cmd,
1208        .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1209};
1210
1211static void hdpvr_device_release(struct video_device *vdev)
1212{
1213        struct hdpvr_device *dev = video_get_drvdata(vdev);
1214
1215        hdpvr_delete(dev);
1216        mutex_lock(&dev->io_mutex);
1217        destroy_workqueue(dev->workqueue);
1218        mutex_unlock(&dev->io_mutex);
1219
1220        v4l2_device_unregister(&dev->v4l2_dev);
1221
1222        /* deregister I2C adapter */
1223#if defined(CONFIG_I2C) || (CONFIG_I2C_MODULE)
1224        mutex_lock(&dev->i2c_mutex);
1225        i2c_del_adapter(&dev->i2c_adapter);
1226        mutex_unlock(&dev->i2c_mutex);
1227#endif /* CONFIG_I2C */
1228
1229        kfree(dev->usbc_buf);
1230        kfree(dev);
1231}
1232
1233static const struct video_device hdpvr_video_template = {
1234/*      .type                   = VFL_TYPE_GRABBER, */
1235/*      .type2                  = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1236        .fops                   = &hdpvr_fops,
1237        .release                = hdpvr_device_release,
1238        .ioctl_ops              = &hdpvr_ioctl_ops,
1239        .tvnorms                =
1240                V4L2_STD_NTSC  | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1241                V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1242                V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1243                V4L2_STD_PAL_60,
1244        .current_norm           = V4L2_STD_NTSC | V4L2_STD_PAL_M |
1245                V4L2_STD_PAL_60,
1246};
1247
1248int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1249                            int devnum)
1250{
1251        /* setup and register video device */
1252        dev->video_dev = video_device_alloc();
1253        if (!dev->video_dev) {
1254                v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
1255                goto error;
1256        }
1257
1258        *(dev->video_dev) = hdpvr_video_template;
1259        strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1260        dev->video_dev->parent = parent;
1261        video_set_drvdata(dev->video_dev, dev);
1262
1263        if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1264                v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1265                goto error;
1266        }
1267
1268        return 0;
1269error:
1270        return -ENOMEM;
1271}
1272