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