Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Hauppauge HD PVR USB driver - video 4 linux 2 interface
0004  *
0005  * Copyright (C) 2008      Janne Grunau (j@jannau.net)
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/errno.h>
0010 #include <linux/init.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/uaccess.h>
0014 #include <linux/usb.h>
0015 #include <linux/mutex.h>
0016 #include <linux/workqueue.h>
0017 
0018 #include <linux/videodev2.h>
0019 #include <linux/v4l2-dv-timings.h>
0020 #include <media/v4l2-dev.h>
0021 #include <media/v4l2-common.h>
0022 #include <media/v4l2-dv-timings.h>
0023 #include <media/v4l2-ioctl.h>
0024 #include <media/v4l2-event.h>
0025 #include "hdpvr.h"
0026 
0027 #define BULK_URB_TIMEOUT   90 /* 0.09 seconds */
0028 
0029 #define print_buffer_status() { \
0030         v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,   \
0031              "%s:%d buffer stat: %d free, %d proc\n",   \
0032              __func__, __LINE__,                \
0033              list_size(&dev->free_buff_list),       \
0034              list_size(&dev->rec_buff_list)); }
0035 
0036 static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
0037     V4L2_DV_BT_CEA_720X480I59_94,
0038     V4L2_DV_BT_CEA_720X576I50,
0039     V4L2_DV_BT_CEA_720X480P59_94,
0040     V4L2_DV_BT_CEA_720X576P50,
0041     V4L2_DV_BT_CEA_1280X720P50,
0042     V4L2_DV_BT_CEA_1280X720P60,
0043     V4L2_DV_BT_CEA_1920X1080I50,
0044     V4L2_DV_BT_CEA_1920X1080I60,
0045 };
0046 
0047 /* Use 480i59 as the default timings */
0048 #define HDPVR_DEF_DV_TIMINGS_IDX (0)
0049 
0050 struct hdpvr_fh {
0051     struct v4l2_fh fh;
0052     bool legacy_mode;
0053 };
0054 
0055 static uint list_size(struct list_head *list)
0056 {
0057     struct list_head *tmp;
0058     uint count = 0;
0059 
0060     list_for_each(tmp, list) {
0061         count++;
0062     }
0063 
0064     return count;
0065 }
0066 
0067 /*=========================================================================*/
0068 /* urb callback */
0069 static void hdpvr_read_bulk_callback(struct urb *urb)
0070 {
0071     struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
0072     struct hdpvr_device *dev = buf->dev;
0073 
0074     /* marking buffer as received and wake waiting */
0075     buf->status = BUFSTAT_READY;
0076     wake_up_interruptible(&dev->wait_data);
0077 }
0078 
0079 /*=========================================================================*/
0080 /* buffer bits */
0081 
0082 /* function expects dev->io_mutex to be hold by caller */
0083 int hdpvr_cancel_queue(struct hdpvr_device *dev)
0084 {
0085     struct hdpvr_buffer *buf;
0086 
0087     list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
0088         usb_kill_urb(buf->urb);
0089         buf->status = BUFSTAT_AVAILABLE;
0090     }
0091 
0092     list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
0093 
0094     return 0;
0095 }
0096 
0097 static int hdpvr_free_queue(struct list_head *q)
0098 {
0099     struct list_head *tmp;
0100     struct list_head *p;
0101     struct hdpvr_buffer *buf;
0102     struct urb *urb;
0103 
0104     for (p = q->next; p != q;) {
0105         buf = list_entry(p, struct hdpvr_buffer, buff_list);
0106 
0107         urb = buf->urb;
0108         usb_free_coherent(urb->dev, urb->transfer_buffer_length,
0109                   urb->transfer_buffer, urb->transfer_dma);
0110         usb_free_urb(urb);
0111         tmp = p->next;
0112         list_del(p);
0113         kfree(buf);
0114         p = tmp;
0115     }
0116 
0117     return 0;
0118 }
0119 
0120 /* function expects dev->io_mutex to be hold by caller */
0121 int hdpvr_free_buffers(struct hdpvr_device *dev)
0122 {
0123     hdpvr_cancel_queue(dev);
0124 
0125     hdpvr_free_queue(&dev->free_buff_list);
0126     hdpvr_free_queue(&dev->rec_buff_list);
0127 
0128     return 0;
0129 }
0130 
0131 /* function expects dev->io_mutex to be hold by caller */
0132 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
0133 {
0134     uint i;
0135     int retval = -ENOMEM;
0136     u8 *mem;
0137     struct hdpvr_buffer *buf;
0138     struct urb *urb;
0139 
0140     v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
0141          "allocating %u buffers\n", count);
0142 
0143     for (i = 0; i < count; i++) {
0144 
0145         buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
0146         if (!buf) {
0147             v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
0148             goto exit;
0149         }
0150         buf->dev = dev;
0151 
0152         urb = usb_alloc_urb(0, GFP_KERNEL);
0153         if (!urb)
0154             goto exit_urb;
0155         buf->urb = urb;
0156 
0157         mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
0158                      &urb->transfer_dma);
0159         if (!mem) {
0160             v4l2_err(&dev->v4l2_dev,
0161                  "cannot allocate usb transfer buffer\n");
0162             goto exit_urb_buffer;
0163         }
0164 
0165         usb_fill_bulk_urb(buf->urb, dev->udev,
0166                   usb_rcvbulkpipe(dev->udev,
0167                           dev->bulk_in_endpointAddr),
0168                   mem, dev->bulk_in_size,
0169                   hdpvr_read_bulk_callback, buf);
0170 
0171         buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0172         buf->status = BUFSTAT_AVAILABLE;
0173         list_add_tail(&buf->buff_list, &dev->free_buff_list);
0174     }
0175     return 0;
0176 exit_urb_buffer:
0177     usb_free_urb(urb);
0178 exit_urb:
0179     kfree(buf);
0180 exit:
0181     hdpvr_free_buffers(dev);
0182     return retval;
0183 }
0184 
0185 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
0186 {
0187     struct hdpvr_buffer *buf;
0188     struct urb *urb;
0189     int ret = 0, err_count = 0;
0190 
0191     mutex_lock(&dev->io_mutex);
0192 
0193     while (dev->status == STATUS_STREAMING &&
0194            !list_empty(&dev->free_buff_list)) {
0195 
0196         buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
0197                  buff_list);
0198         if (buf->status != BUFSTAT_AVAILABLE) {
0199             v4l2_err(&dev->v4l2_dev,
0200                  "buffer not marked as available\n");
0201             ret = -EFAULT;
0202             goto err;
0203         }
0204 
0205         urb = buf->urb;
0206         urb->status = 0;
0207         urb->actual_length = 0;
0208         ret = usb_submit_urb(urb, GFP_KERNEL);
0209         if (ret) {
0210             v4l2_err(&dev->v4l2_dev,
0211                  "usb_submit_urb in %s returned %d\n",
0212                  __func__, ret);
0213             if (++err_count > 2)
0214                 break;
0215             continue;
0216         }
0217         buf->status = BUFSTAT_INPROGRESS;
0218         list_move_tail(&buf->buff_list, &dev->rec_buff_list);
0219     }
0220 err:
0221     print_buffer_status();
0222     mutex_unlock(&dev->io_mutex);
0223     return ret;
0224 }
0225 
0226 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
0227 {
0228     struct hdpvr_buffer *buf;
0229 
0230     mutex_lock(&dev->io_mutex);
0231 
0232     if (list_empty(&dev->rec_buff_list)) {
0233         mutex_unlock(&dev->io_mutex);
0234         return NULL;
0235     }
0236 
0237     buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
0238              buff_list);
0239     mutex_unlock(&dev->io_mutex);
0240 
0241     return buf;
0242 }
0243 
0244 static void hdpvr_transmit_buffers(struct work_struct *work)
0245 {
0246     struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
0247                         worker);
0248 
0249     while (dev->status == STATUS_STREAMING) {
0250 
0251         if (hdpvr_submit_buffers(dev)) {
0252             v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
0253             goto error;
0254         }
0255         if (wait_event_interruptible(dev->wait_buffer,
0256                 !list_empty(&dev->free_buff_list) ||
0257                          dev->status != STATUS_STREAMING))
0258             goto error;
0259     }
0260 
0261     v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
0262          "transmit worker exited\n");
0263     return;
0264 error:
0265     v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
0266          "transmit buffers errored\n");
0267     dev->status = STATUS_ERROR;
0268 }
0269 
0270 /* function expects dev->io_mutex to be hold by caller */
0271 static int hdpvr_start_streaming(struct hdpvr_device *dev)
0272 {
0273     int ret;
0274     struct hdpvr_video_info vidinf;
0275 
0276     if (dev->status == STATUS_STREAMING)
0277         return 0;
0278     if (dev->status != STATUS_IDLE)
0279         return -EAGAIN;
0280 
0281     ret = get_video_info(dev, &vidinf);
0282     if (ret < 0)
0283         return ret;
0284 
0285     if (!vidinf.valid) {
0286         msleep(250);
0287         v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
0288                 "no video signal at input %d\n", dev->options.video_input);
0289         return -EAGAIN;
0290     }
0291 
0292     v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0293             "video signal: %dx%d@%dhz\n", vidinf.width,
0294             vidinf.height, vidinf.fps);
0295 
0296     /* start streaming 2 request */
0297     ret = usb_control_msg(dev->udev,
0298             usb_sndctrlpipe(dev->udev, 0),
0299             0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
0300     v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0301             "encoder start control request returned %d\n", ret);
0302     if (ret < 0)
0303         return ret;
0304 
0305     ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
0306     if (ret)
0307         return ret;
0308 
0309     dev->status = STATUS_STREAMING;
0310 
0311     schedule_work(&dev->worker);
0312 
0313     v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0314             "streaming started\n");
0315 
0316     return 0;
0317 }
0318 
0319 
0320 /* function expects dev->io_mutex to be hold by caller */
0321 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
0322 {
0323     int actual_length;
0324     uint c = 0;
0325     u8 *buf;
0326 
0327     if (dev->status == STATUS_IDLE)
0328         return 0;
0329     else if (dev->status != STATUS_STREAMING)
0330         return -EAGAIN;
0331 
0332     buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
0333     if (!buf)
0334         v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer for emptying the internal device buffer. Next capture start will be slow\n");
0335 
0336     dev->status = STATUS_SHUTTING_DOWN;
0337     hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
0338     mutex_unlock(&dev->io_mutex);
0339 
0340     wake_up_interruptible(&dev->wait_buffer);
0341     msleep(50);
0342 
0343     flush_work(&dev->worker);
0344 
0345     mutex_lock(&dev->io_mutex);
0346     /* kill the still outstanding urbs */
0347     hdpvr_cancel_queue(dev);
0348 
0349     /* emptying the device buffer beforeshutting it down */
0350     while (buf && ++c < 500 &&
0351            !usb_bulk_msg(dev->udev,
0352                  usb_rcvbulkpipe(dev->udev,
0353                          dev->bulk_in_endpointAddr),
0354                  buf, dev->bulk_in_size, &actual_length,
0355                  BULK_URB_TIMEOUT)) {
0356         v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0357              "%2d: got %d bytes\n", c, actual_length);
0358     }
0359     kfree(buf);
0360     v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0361          "used %d urbs to empty device buffers\n", c-1);
0362     msleep(10);
0363 
0364     dev->status = STATUS_IDLE;
0365 
0366     return 0;
0367 }
0368 
0369 
0370 /*=======================================================================*/
0371 /*
0372  * video 4 linux 2 file operations
0373  */
0374 
0375 static int hdpvr_open(struct file *file)
0376 {
0377     struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
0378 
0379     if (fh == NULL)
0380         return -ENOMEM;
0381     fh->legacy_mode = true;
0382     v4l2_fh_init(&fh->fh, video_devdata(file));
0383     v4l2_fh_add(&fh->fh);
0384     file->private_data = fh;
0385     return 0;
0386 }
0387 
0388 static int hdpvr_release(struct file *file)
0389 {
0390     struct hdpvr_device *dev = video_drvdata(file);
0391 
0392     mutex_lock(&dev->io_mutex);
0393     if (file->private_data == dev->owner) {
0394         hdpvr_stop_streaming(dev);
0395         dev->owner = NULL;
0396     }
0397     mutex_unlock(&dev->io_mutex);
0398 
0399     return v4l2_fh_release(file);
0400 }
0401 
0402 /*
0403  * hdpvr_v4l2_read()
0404  * will allocate buffers when called for the first time
0405  */
0406 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
0407               loff_t *pos)
0408 {
0409     struct hdpvr_device *dev = video_drvdata(file);
0410     struct hdpvr_buffer *buf = NULL;
0411     struct urb *urb;
0412     int ret = 0;
0413     int rem, cnt;
0414 
0415     if (*pos)
0416         return -ESPIPE;
0417 
0418     mutex_lock(&dev->io_mutex);
0419     if (dev->status == STATUS_IDLE) {
0420         if (hdpvr_start_streaming(dev)) {
0421             v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
0422                  "start_streaming failed\n");
0423             ret = -EIO;
0424             msleep(200);
0425             dev->status = STATUS_IDLE;
0426             mutex_unlock(&dev->io_mutex);
0427             goto err;
0428         }
0429         dev->owner = file->private_data;
0430         print_buffer_status();
0431     }
0432     mutex_unlock(&dev->io_mutex);
0433 
0434     /* wait for the first buffer */
0435     if (!(file->f_flags & O_NONBLOCK)) {
0436         if (wait_event_interruptible(dev->wait_data,
0437                          !list_empty_careful(&dev->rec_buff_list)))
0438             return -ERESTARTSYS;
0439     }
0440 
0441     buf = hdpvr_get_next_buffer(dev);
0442 
0443     while (count > 0 && buf) {
0444 
0445         if (buf->status != BUFSTAT_READY &&
0446             dev->status != STATUS_DISCONNECTED) {
0447             int err;
0448             /* return nonblocking */
0449             if (file->f_flags & O_NONBLOCK) {
0450                 if (!ret)
0451                     ret = -EAGAIN;
0452                 goto err;
0453             }
0454 
0455             err = wait_event_interruptible_timeout(dev->wait_data,
0456                 buf->status == BUFSTAT_READY,
0457                 msecs_to_jiffies(1000));
0458             if (err < 0) {
0459                 ret = err;
0460                 goto err;
0461             }
0462             if (!err) {
0463                 v4l2_info(&dev->v4l2_dev,
0464                       "timeout: restart streaming\n");
0465                 mutex_lock(&dev->io_mutex);
0466                 hdpvr_stop_streaming(dev);
0467                 mutex_unlock(&dev->io_mutex);
0468                 /*
0469                  * The FW needs about 4 seconds after streaming
0470                  * stopped before it is ready to restart
0471                  * streaming.
0472                  */
0473                 msleep(4000);
0474                 err = hdpvr_start_streaming(dev);
0475                 if (err) {
0476                     ret = err;
0477                     goto err;
0478                 }
0479             }
0480         }
0481 
0482         if (buf->status != BUFSTAT_READY)
0483             break;
0484 
0485         /* set remaining bytes to copy */
0486         urb = buf->urb;
0487         rem = urb->actual_length - buf->pos;
0488         cnt = rem > count ? count : rem;
0489 
0490         if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
0491                  cnt)) {
0492             v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
0493             if (!ret)
0494                 ret = -EFAULT;
0495             goto err;
0496         }
0497 
0498         buf->pos += cnt;
0499         count -= cnt;
0500         buffer += cnt;
0501         ret += cnt;
0502 
0503         /* finished, take next buffer */
0504         if (buf->pos == urb->actual_length) {
0505             mutex_lock(&dev->io_mutex);
0506             buf->pos = 0;
0507             buf->status = BUFSTAT_AVAILABLE;
0508 
0509             list_move_tail(&buf->buff_list, &dev->free_buff_list);
0510 
0511             print_buffer_status();
0512 
0513             mutex_unlock(&dev->io_mutex);
0514 
0515             wake_up_interruptible(&dev->wait_buffer);
0516 
0517             buf = hdpvr_get_next_buffer(dev);
0518         }
0519     }
0520 err:
0521     if (!ret && !buf)
0522         ret = -EAGAIN;
0523     return ret;
0524 }
0525 
0526 static __poll_t hdpvr_poll(struct file *filp, poll_table *wait)
0527 {
0528     __poll_t req_events = poll_requested_events(wait);
0529     struct hdpvr_buffer *buf = NULL;
0530     struct hdpvr_device *dev = video_drvdata(filp);
0531     __poll_t mask = v4l2_ctrl_poll(filp, wait);
0532 
0533     if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
0534         return mask;
0535 
0536     mutex_lock(&dev->io_mutex);
0537 
0538     if (dev->status == STATUS_IDLE) {
0539         if (hdpvr_start_streaming(dev)) {
0540             v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
0541                  "start_streaming failed\n");
0542             dev->status = STATUS_IDLE;
0543         } else {
0544             dev->owner = filp->private_data;
0545         }
0546 
0547         print_buffer_status();
0548     }
0549     mutex_unlock(&dev->io_mutex);
0550 
0551     buf = hdpvr_get_next_buffer(dev);
0552     /* only wait if no data is available */
0553     if (!buf || buf->status != BUFSTAT_READY) {
0554         poll_wait(filp, &dev->wait_data, wait);
0555         buf = hdpvr_get_next_buffer(dev);
0556     }
0557     if (buf && buf->status == BUFSTAT_READY)
0558         mask |= EPOLLIN | EPOLLRDNORM;
0559 
0560     return mask;
0561 }
0562 
0563 
0564 static const struct v4l2_file_operations hdpvr_fops = {
0565     .owner      = THIS_MODULE,
0566     .open       = hdpvr_open,
0567     .release    = hdpvr_release,
0568     .read       = hdpvr_read,
0569     .poll       = hdpvr_poll,
0570     .unlocked_ioctl = video_ioctl2,
0571 };
0572 
0573 /*=======================================================================*/
0574 /*
0575  * V4L2 ioctl handling
0576  */
0577 
0578 static int vidioc_querycap(struct file *file, void  *priv,
0579                struct v4l2_capability *cap)
0580 {
0581     struct hdpvr_device *dev = video_drvdata(file);
0582 
0583     strscpy(cap->driver, "hdpvr", sizeof(cap->driver));
0584     strscpy(cap->card, "Hauppauge HD PVR", sizeof(cap->card));
0585     usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
0586     return 0;
0587 }
0588 
0589 static int vidioc_s_std(struct file *file, void *_fh,
0590             v4l2_std_id std)
0591 {
0592     struct hdpvr_device *dev = video_drvdata(file);
0593     struct hdpvr_fh *fh = _fh;
0594     u8 std_type = 1;
0595 
0596     if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
0597         return -ENODATA;
0598     if (dev->status != STATUS_IDLE)
0599         return -EBUSY;
0600     if (std & V4L2_STD_525_60)
0601         std_type = 0;
0602     dev->cur_std = std;
0603     dev->width = 720;
0604     dev->height = std_type ? 576 : 480;
0605 
0606     return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
0607 }
0608 
0609 static int vidioc_g_std(struct file *file, void *_fh,
0610             v4l2_std_id *std)
0611 {
0612     struct hdpvr_device *dev = video_drvdata(file);
0613     struct hdpvr_fh *fh = _fh;
0614 
0615     if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
0616         return -ENODATA;
0617     *std = dev->cur_std;
0618     return 0;
0619 }
0620 
0621 static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
0622 {
0623     struct hdpvr_device *dev = video_drvdata(file);
0624     struct hdpvr_video_info vid_info;
0625     struct hdpvr_fh *fh = _fh;
0626     int ret;
0627 
0628     *a = V4L2_STD_UNKNOWN;
0629     if (dev->options.video_input == HDPVR_COMPONENT)
0630         return fh->legacy_mode ? 0 : -ENODATA;
0631     ret = get_video_info(dev, &vid_info);
0632     if (vid_info.valid && vid_info.width == 720 &&
0633         (vid_info.height == 480 || vid_info.height == 576)) {
0634         *a = (vid_info.height == 480) ?
0635             V4L2_STD_525_60 : V4L2_STD_625_50;
0636     }
0637     return ret;
0638 }
0639 
0640 static int vidioc_s_dv_timings(struct file *file, void *_fh,
0641                     struct v4l2_dv_timings *timings)
0642 {
0643     struct hdpvr_device *dev = video_drvdata(file);
0644     struct hdpvr_fh *fh = _fh;
0645     int i;
0646 
0647     fh->legacy_mode = false;
0648     if (dev->options.video_input)
0649         return -ENODATA;
0650     if (dev->status != STATUS_IDLE)
0651         return -EBUSY;
0652     for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
0653         if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
0654             break;
0655     if (i == ARRAY_SIZE(hdpvr_dv_timings))
0656         return -EINVAL;
0657     dev->cur_dv_timings = hdpvr_dv_timings[i];
0658     dev->width = hdpvr_dv_timings[i].bt.width;
0659     dev->height = hdpvr_dv_timings[i].bt.height;
0660     return 0;
0661 }
0662 
0663 static int vidioc_g_dv_timings(struct file *file, void *_fh,
0664                     struct v4l2_dv_timings *timings)
0665 {
0666     struct hdpvr_device *dev = video_drvdata(file);
0667     struct hdpvr_fh *fh = _fh;
0668 
0669     fh->legacy_mode = false;
0670     if (dev->options.video_input)
0671         return -ENODATA;
0672     *timings = dev->cur_dv_timings;
0673     return 0;
0674 }
0675 
0676 static int vidioc_query_dv_timings(struct file *file, void *_fh,
0677                     struct v4l2_dv_timings *timings)
0678 {
0679     struct hdpvr_device *dev = video_drvdata(file);
0680     struct hdpvr_fh *fh = _fh;
0681     struct hdpvr_video_info vid_info;
0682     bool interlaced;
0683     int ret = 0;
0684     int i;
0685 
0686     fh->legacy_mode = false;
0687     if (dev->options.video_input)
0688         return -ENODATA;
0689     ret = get_video_info(dev, &vid_info);
0690     if (ret)
0691         return ret;
0692     if (!vid_info.valid)
0693         return -ENOLCK;
0694     interlaced = vid_info.fps <= 30;
0695     for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
0696         const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
0697         unsigned hsize;
0698         unsigned vsize;
0699         unsigned fps;
0700 
0701         hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
0702         vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
0703         fps = (unsigned)bt->pixelclock / (hsize * vsize);
0704         if (bt->width != vid_info.width ||
0705             bt->height != vid_info.height ||
0706             bt->interlaced != interlaced ||
0707             (fps != vid_info.fps && fps + 1 != vid_info.fps))
0708             continue;
0709         *timings = hdpvr_dv_timings[i];
0710         break;
0711     }
0712     if (i == ARRAY_SIZE(hdpvr_dv_timings))
0713         ret = -ERANGE;
0714 
0715     return ret;
0716 }
0717 
0718 static int vidioc_enum_dv_timings(struct file *file, void *_fh,
0719                     struct v4l2_enum_dv_timings *timings)
0720 {
0721     struct hdpvr_device *dev = video_drvdata(file);
0722     struct hdpvr_fh *fh = _fh;
0723 
0724     fh->legacy_mode = false;
0725     memset(timings->reserved, 0, sizeof(timings->reserved));
0726     if (dev->options.video_input)
0727         return -ENODATA;
0728     if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
0729         return -EINVAL;
0730     timings->timings = hdpvr_dv_timings[timings->index];
0731     return 0;
0732 }
0733 
0734 static int vidioc_dv_timings_cap(struct file *file, void *_fh,
0735                     struct v4l2_dv_timings_cap *cap)
0736 {
0737     struct hdpvr_device *dev = video_drvdata(file);
0738     struct hdpvr_fh *fh = _fh;
0739 
0740     fh->legacy_mode = false;
0741     if (dev->options.video_input)
0742         return -ENODATA;
0743     cap->type = V4L2_DV_BT_656_1120;
0744     cap->bt.min_width = 720;
0745     cap->bt.max_width = 1920;
0746     cap->bt.min_height = 480;
0747     cap->bt.max_height = 1080;
0748     cap->bt.min_pixelclock = 27000000;
0749     cap->bt.max_pixelclock = 74250000;
0750     cap->bt.standards = V4L2_DV_BT_STD_CEA861;
0751     cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
0752     return 0;
0753 }
0754 
0755 static const char *iname[] = {
0756     [HDPVR_COMPONENT] = "Component",
0757     [HDPVR_SVIDEO]    = "S-Video",
0758     [HDPVR_COMPOSITE] = "Composite",
0759 };
0760 
0761 static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
0762 {
0763     unsigned int n;
0764 
0765     n = i->index;
0766     if (n >= HDPVR_VIDEO_INPUTS)
0767         return -EINVAL;
0768 
0769     i->type = V4L2_INPUT_TYPE_CAMERA;
0770 
0771     strscpy(i->name, iname[n], sizeof(i->name));
0772 
0773     i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
0774 
0775     i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
0776     i->std = n ? V4L2_STD_ALL : 0;
0777 
0778     return 0;
0779 }
0780 
0781 static int vidioc_s_input(struct file *file, void *_fh,
0782               unsigned int index)
0783 {
0784     struct hdpvr_device *dev = video_drvdata(file);
0785     int retval;
0786 
0787     if (index >= HDPVR_VIDEO_INPUTS)
0788         return -EINVAL;
0789 
0790     if (dev->status != STATUS_IDLE)
0791         return -EBUSY;
0792 
0793     retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
0794     if (!retval) {
0795         dev->options.video_input = index;
0796         /*
0797          * Unfortunately gstreamer calls ENUMSTD and bails out if it
0798          * won't find any formats, even though component input is
0799          * selected. This means that we have to leave tvnorms at
0800          * V4L2_STD_ALL. We cannot use the 'legacy' trick since
0801          * tvnorms is set at the device node level and not at the
0802          * filehandle level.
0803          *
0804          * Comment this out for now, but if the legacy mode can be
0805          * removed in the future, then this code should be enabled
0806          * again.
0807         dev->video_dev.tvnorms =
0808             (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
0809          */
0810     }
0811 
0812     return retval;
0813 }
0814 
0815 static int vidioc_g_input(struct file *file, void *private_data,
0816               unsigned int *index)
0817 {
0818     struct hdpvr_device *dev = video_drvdata(file);
0819 
0820     *index = dev->options.video_input;
0821     return 0;
0822 }
0823 
0824 
0825 static const char *audio_iname[] = {
0826     [HDPVR_RCA_FRONT] = "RCA front",
0827     [HDPVR_RCA_BACK]  = "RCA back",
0828     [HDPVR_SPDIF]     = "SPDIF",
0829 };
0830 
0831 static int vidioc_enumaudio(struct file *file, void *priv,
0832                 struct v4l2_audio *audio)
0833 {
0834     unsigned int n;
0835 
0836     n = audio->index;
0837     if (n >= HDPVR_AUDIO_INPUTS)
0838         return -EINVAL;
0839 
0840     audio->capability = V4L2_AUDCAP_STEREO;
0841 
0842     strscpy(audio->name, audio_iname[n], sizeof(audio->name));
0843 
0844     return 0;
0845 }
0846 
0847 static int vidioc_s_audio(struct file *file, void *private_data,
0848               const struct v4l2_audio *audio)
0849 {
0850     struct hdpvr_device *dev = video_drvdata(file);
0851     int retval;
0852 
0853     if (audio->index >= HDPVR_AUDIO_INPUTS)
0854         return -EINVAL;
0855 
0856     if (dev->status != STATUS_IDLE)
0857         return -EBUSY;
0858 
0859     retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
0860     if (!retval)
0861         dev->options.audio_input = audio->index;
0862 
0863     return retval;
0864 }
0865 
0866 static int vidioc_g_audio(struct file *file, void *private_data,
0867               struct v4l2_audio *audio)
0868 {
0869     struct hdpvr_device *dev = video_drvdata(file);
0870 
0871     audio->index = dev->options.audio_input;
0872     audio->capability = V4L2_AUDCAP_STEREO;
0873     strscpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
0874     return 0;
0875 }
0876 
0877 static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
0878 {
0879     struct hdpvr_device *dev =
0880         container_of(ctrl->handler, struct hdpvr_device, hdl);
0881 
0882     switch (ctrl->id) {
0883     case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
0884         if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
0885             dev->video_bitrate->val >= dev->video_bitrate_peak->val)
0886             dev->video_bitrate_peak->val =
0887                     dev->video_bitrate->val + 100000;
0888         break;
0889     }
0890     return 0;
0891 }
0892 
0893 static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
0894 {
0895     struct hdpvr_device *dev =
0896         container_of(ctrl->handler, struct hdpvr_device, hdl);
0897     struct hdpvr_options *opt = &dev->options;
0898     int ret = -EINVAL;
0899 
0900     switch (ctrl->id) {
0901     case V4L2_CID_BRIGHTNESS:
0902         ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
0903         if (ret)
0904             break;
0905         dev->options.brightness = ctrl->val;
0906         return 0;
0907     case V4L2_CID_CONTRAST:
0908         ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
0909         if (ret)
0910             break;
0911         dev->options.contrast = ctrl->val;
0912         return 0;
0913     case V4L2_CID_SATURATION:
0914         ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
0915         if (ret)
0916             break;
0917         dev->options.saturation = ctrl->val;
0918         return 0;
0919     case V4L2_CID_HUE:
0920         ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
0921         if (ret)
0922             break;
0923         dev->options.hue = ctrl->val;
0924         return 0;
0925     case V4L2_CID_SHARPNESS:
0926         ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
0927         if (ret)
0928             break;
0929         dev->options.sharpness = ctrl->val;
0930         return 0;
0931     case V4L2_CID_MPEG_AUDIO_ENCODING:
0932         if (dev->flags & HDPVR_FLAG_AC3_CAP) {
0933             opt->audio_codec = ctrl->val;
0934             return hdpvr_set_audio(dev, opt->audio_input + 1,
0935                           opt->audio_codec);
0936         }
0937         return 0;
0938     case V4L2_CID_MPEG_VIDEO_ENCODING:
0939         return 0;
0940 /*  case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
0941 /*      if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
0942 /*          opt->gop_mode |= 0x2; */
0943 /*          hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
0944 /*                    opt->gop_mode); */
0945 /*      } */
0946 /*      if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
0947 /*          opt->gop_mode &= ~0x2; */
0948 /*          hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
0949 /*                    opt->gop_mode); */
0950 /*      } */
0951 /*      break; */
0952     case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
0953         uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
0954         uint bitrate = dev->video_bitrate->val / 100000;
0955 
0956         if (ctrl->is_new) {
0957             if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
0958                 opt->bitrate_mode = HDPVR_CONSTANT;
0959             else
0960                 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
0961             hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
0962                       opt->bitrate_mode);
0963             v4l2_ctrl_activate(dev->video_bitrate_peak,
0964                 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
0965         }
0966 
0967         if (dev->video_bitrate_peak->is_new ||
0968             dev->video_bitrate->is_new) {
0969             opt->bitrate = bitrate;
0970             opt->peak_bitrate = peak_bitrate;
0971             hdpvr_set_bitrate(dev);
0972         }
0973         return 0;
0974     }
0975     case V4L2_CID_MPEG_STREAM_TYPE:
0976         return 0;
0977     default:
0978         break;
0979     }
0980     return ret;
0981 }
0982 
0983 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
0984                     struct v4l2_fmtdesc *f)
0985 {
0986     if (f->index != 0)
0987         return -EINVAL;
0988 
0989     f->pixelformat = V4L2_PIX_FMT_MPEG;
0990 
0991     return 0;
0992 }
0993 
0994 static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
0995                 struct v4l2_format *f)
0996 {
0997     struct hdpvr_device *dev = video_drvdata(file);
0998     struct hdpvr_fh *fh = _fh;
0999     int ret;
1000 
1001     /*
1002      * The original driver would always returns the current detected
1003      * resolution as the format (and EFAULT if it couldn't be detected).
1004      * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
1005      * better way of doing this, but to stay compatible with existing
1006      * applications we assume legacy mode every time an application opens
1007      * the device. Only if one of the new DV_TIMINGS ioctls is called
1008      * will the filehandle go into 'normal' mode where g_fmt returns the
1009      * last set format.
1010      */
1011     if (fh->legacy_mode) {
1012         struct hdpvr_video_info vid_info;
1013 
1014         ret = get_video_info(dev, &vid_info);
1015         if (ret < 0)
1016             return ret;
1017         if (!vid_info.valid)
1018             return -EFAULT;
1019         f->fmt.pix.width = vid_info.width;
1020         f->fmt.pix.height = vid_info.height;
1021     } else {
1022         f->fmt.pix.width = dev->width;
1023         f->fmt.pix.height = dev->height;
1024     }
1025     f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
1026     f->fmt.pix.sizeimage    = dev->bulk_in_size;
1027     f->fmt.pix.bytesperline = 0;
1028     if (f->fmt.pix.width == 720) {
1029         /* SDTV formats */
1030         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1031         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1032     } else {
1033         /* HDTV formats */
1034         f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
1035         f->fmt.pix.field = V4L2_FIELD_NONE;
1036     }
1037     return 0;
1038 }
1039 
1040 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1041                    struct v4l2_encoder_cmd *a)
1042 {
1043     struct hdpvr_device *dev = video_drvdata(filp);
1044     int res = 0;
1045 
1046     mutex_lock(&dev->io_mutex);
1047     a->flags = 0;
1048 
1049     switch (a->cmd) {
1050     case V4L2_ENC_CMD_START:
1051         if (dev->owner && filp->private_data != dev->owner) {
1052             res = -EBUSY;
1053             break;
1054         }
1055         if (dev->status == STATUS_STREAMING)
1056             break;
1057         res = hdpvr_start_streaming(dev);
1058         if (!res)
1059             dev->owner = filp->private_data;
1060         else
1061             dev->status = STATUS_IDLE;
1062         break;
1063     case V4L2_ENC_CMD_STOP:
1064         if (dev->owner && filp->private_data != dev->owner) {
1065             res = -EBUSY;
1066             break;
1067         }
1068         if (dev->status == STATUS_IDLE)
1069             break;
1070         res = hdpvr_stop_streaming(dev);
1071         if (!res)
1072             dev->owner = NULL;
1073         break;
1074     default:
1075         v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1076              "Unsupported encoder cmd %d\n", a->cmd);
1077         res = -EINVAL;
1078         break;
1079     }
1080 
1081     mutex_unlock(&dev->io_mutex);
1082     return res;
1083 }
1084 
1085 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1086                     struct v4l2_encoder_cmd *a)
1087 {
1088     a->flags = 0;
1089     switch (a->cmd) {
1090     case V4L2_ENC_CMD_START:
1091     case V4L2_ENC_CMD_STOP:
1092         return 0;
1093     default:
1094         return -EINVAL;
1095     }
1096 }
1097 
1098 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1099     .vidioc_querycap    = vidioc_querycap,
1100     .vidioc_s_std       = vidioc_s_std,
1101     .vidioc_g_std       = vidioc_g_std,
1102     .vidioc_querystd    = vidioc_querystd,
1103     .vidioc_s_dv_timings    = vidioc_s_dv_timings,
1104     .vidioc_g_dv_timings    = vidioc_g_dv_timings,
1105     .vidioc_query_dv_timings= vidioc_query_dv_timings,
1106     .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
1107     .vidioc_dv_timings_cap  = vidioc_dv_timings_cap,
1108     .vidioc_enum_input  = vidioc_enum_input,
1109     .vidioc_g_input     = vidioc_g_input,
1110     .vidioc_s_input     = vidioc_s_input,
1111     .vidioc_enumaudio   = vidioc_enumaudio,
1112     .vidioc_g_audio     = vidioc_g_audio,
1113     .vidioc_s_audio     = vidioc_s_audio,
1114     .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
1115     .vidioc_g_fmt_vid_cap   = vidioc_g_fmt_vid_cap,
1116     .vidioc_s_fmt_vid_cap   = vidioc_g_fmt_vid_cap,
1117     .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1118     .vidioc_encoder_cmd = vidioc_encoder_cmd,
1119     .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1120     .vidioc_log_status  = v4l2_ctrl_log_status,
1121     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1122     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1123 };
1124 
1125 static void hdpvr_device_release(struct video_device *vdev)
1126 {
1127     struct hdpvr_device *dev = video_get_drvdata(vdev);
1128 
1129     hdpvr_delete(dev);
1130     flush_work(&dev->worker);
1131 
1132     v4l2_device_unregister(&dev->v4l2_dev);
1133     v4l2_ctrl_handler_free(&dev->hdl);
1134 
1135     /* deregister I2C adapter */
1136 #if IS_ENABLED(CONFIG_I2C)
1137     mutex_lock(&dev->i2c_mutex);
1138     i2c_del_adapter(&dev->i2c_adapter);
1139     mutex_unlock(&dev->i2c_mutex);
1140 #endif /* CONFIG_I2C */
1141 
1142     kfree(dev->usbc_buf);
1143     kfree(dev);
1144 }
1145 
1146 static const struct video_device hdpvr_video_template = {
1147     .fops           = &hdpvr_fops,
1148     .release        = hdpvr_device_release,
1149     .ioctl_ops      = &hdpvr_ioctl_ops,
1150     .tvnorms        = V4L2_STD_ALL,
1151     .device_caps        = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
1152                   V4L2_CAP_READWRITE,
1153 };
1154 
1155 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
1156     .try_ctrl = hdpvr_try_ctrl,
1157     .s_ctrl = hdpvr_s_ctrl,
1158 };
1159 
1160 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1161                 int devnum)
1162 {
1163     struct v4l2_ctrl_handler *hdl = &dev->hdl;
1164     bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
1165     int res;
1166 
1167     // initialize dev->worker
1168     INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
1169 
1170     dev->cur_std = V4L2_STD_525_60;
1171     dev->width = 720;
1172     dev->height = 480;
1173     dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
1174     v4l2_ctrl_handler_init(hdl, 11);
1175     if (dev->fw_ver > 0x15) {
1176         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1177             V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
1178         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1179             V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
1180         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1181             V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
1182         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1183             V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
1184         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1185             V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1186     } else {
1187         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1188             V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
1189         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1190             V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
1191         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1192             V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
1193         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1194             V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
1195         v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1196             V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1197     }
1198 
1199     v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1200         V4L2_CID_MPEG_STREAM_TYPE,
1201         V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1202         0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1203     v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1204         V4L2_CID_MPEG_AUDIO_ENCODING,
1205         ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
1206         0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
1207     v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1208         V4L2_CID_MPEG_VIDEO_ENCODING,
1209         V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1210         V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1211 
1212     dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1213         V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1214         V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1215         V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1216 
1217     dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1218         V4L2_CID_MPEG_VIDEO_BITRATE,
1219         1000000, 13500000, 100000, 6500000);
1220     dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1221         V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1222         1100000, 20200000, 100000, 9000000);
1223     dev->v4l2_dev.ctrl_handler = hdl;
1224     if (hdl->error) {
1225         res = hdl->error;
1226         v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1227         goto error;
1228     }
1229     v4l2_ctrl_cluster(3, &dev->video_mode);
1230     res = v4l2_ctrl_handler_setup(hdl);
1231     if (res < 0) {
1232         v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1233         goto error;
1234     }
1235 
1236     /* setup and register video device */
1237     dev->video_dev = hdpvr_video_template;
1238     strscpy(dev->video_dev.name, "Hauppauge HD PVR",
1239         sizeof(dev->video_dev.name));
1240     dev->video_dev.v4l2_dev = &dev->v4l2_dev;
1241     video_set_drvdata(&dev->video_dev, dev);
1242 
1243     res = video_register_device(&dev->video_dev, VFL_TYPE_VIDEO, devnum);
1244     if (res < 0) {
1245         v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1246         goto error;
1247     }
1248 
1249     return 0;
1250 error:
1251     v4l2_ctrl_handler_free(hdl);
1252     return res;
1253 }