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