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