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