linux/drivers/media/usb/stk1160/stk1160-v4l.c
<<
>>
Prefs
   1/*
   2 * STK1160 driver
   3 *
   4 * Copyright (C) 2012 Ezequiel Garcia
   5 * <elezegarcia--a.t--gmail.com>
   6 *
   7 * Based on Easycap driver by R.M. Thomas
   8 *      Copyright (C) 2010 R.M. Thomas
   9 *      <rmthomas--a.t--sciolus.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/usb.h>
  25#include <linux/mm.h>
  26#include <linux/slab.h>
  27
  28#include <linux/videodev2.h>
  29#include <media/v4l2-device.h>
  30#include <media/v4l2-common.h>
  31#include <media/v4l2-ioctl.h>
  32#include <media/v4l2-fh.h>
  33#include <media/v4l2-event.h>
  34#include <media/videobuf2-vmalloc.h>
  35
  36#include <media/i2c/saa7115.h>
  37
  38#include "stk1160.h"
  39#include "stk1160-reg.h"
  40
  41static bool keep_buffers;
  42module_param(keep_buffers, bool, 0644);
  43MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
  44
  45enum stk1160_decimate_mode {
  46        STK1160_DECIMATE_MORE_THAN_HALF,
  47        STK1160_DECIMATE_LESS_THAN_HALF,
  48};
  49
  50struct stk1160_decimate_ctrl {
  51        bool col_en, row_en;
  52        enum stk1160_decimate_mode col_mode, row_mode;
  53        unsigned int col_n, row_n;
  54};
  55
  56/* supported video standards */
  57static struct stk1160_fmt format[] = {
  58        {
  59                .name     = "16 bpp YUY2, 4:2:2, packed",
  60                .fourcc   = V4L2_PIX_FMT_UYVY,
  61                .depth    = 16,
  62        }
  63};
  64
  65/*
  66 * Helper to find the next divisor that results in modulo being zero.
  67 * This is required to guarantee valid decimation unit counts.
  68 */
  69static unsigned int
  70div_round_integer(unsigned int x, unsigned int y)
  71{
  72        for (;; y++) {
  73                if (x % y == 0)
  74                        return x / y;
  75        }
  76}
  77
  78static void stk1160_set_std(struct stk1160 *dev)
  79{
  80        int i;
  81
  82        static struct regval std525[] = {
  83
  84                /* 720x480 */
  85
  86                /* Frame start */
  87                {STK116_CFSPO_STX_L, 0x0000},
  88                {STK116_CFSPO_STX_H, 0x0000},
  89                {STK116_CFSPO_STY_L, 0x0003},
  90                {STK116_CFSPO_STY_H, 0x0000},
  91
  92                /* Frame end */
  93                {STK116_CFEPO_ENX_L, 0x05a0},
  94                {STK116_CFEPO_ENX_H, 0x0005},
  95                {STK116_CFEPO_ENY_L, 0x00f3},
  96                {STK116_CFEPO_ENY_H, 0x0000},
  97
  98                {0xffff, 0xffff}
  99        };
 100
 101        static struct regval std625[] = {
 102
 103                /* 720x576 */
 104
 105                /* TODO: Each line of frame has some junk at the end */
 106                /* Frame start */
 107                {STK116_CFSPO,   0x0000},
 108                {STK116_CFSPO+1, 0x0000},
 109                {STK116_CFSPO+2, 0x0001},
 110                {STK116_CFSPO+3, 0x0000},
 111
 112                /* Frame end */
 113                {STK116_CFEPO,   0x05a0},
 114                {STK116_CFEPO+1, 0x0005},
 115                {STK116_CFEPO+2, 0x0121},
 116                {STK116_CFEPO+3, 0x0001},
 117
 118                {0xffff, 0xffff}
 119        };
 120
 121        if (dev->norm & V4L2_STD_525_60) {
 122                stk1160_dbg("registers to NTSC like standard\n");
 123                for (i = 0; std525[i].reg != 0xffff; i++)
 124                        stk1160_write_reg(dev, std525[i].reg, std525[i].val);
 125        } else {
 126                stk1160_dbg("registers to PAL like standard\n");
 127                for (i = 0; std625[i].reg != 0xffff; i++)
 128                        stk1160_write_reg(dev, std625[i].reg, std625[i].val);
 129        }
 130
 131}
 132
 133static void stk1160_set_fmt(struct stk1160 *dev,
 134                            struct stk1160_decimate_ctrl *ctrl)
 135{
 136        u32 val = 0;
 137
 138        if (ctrl) {
 139                /*
 140                 * Since the format is UYVY, the device must skip or send
 141                 * a number of rows/columns multiple of four. This way, the
 142                 * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit
 143                 * does exactly this.
 144                 */
 145                val |= STK1160_DEC_UNIT_SIZE;
 146                val |= ctrl->col_en ? STK1160_H_DEC_EN : 0;
 147                val |= ctrl->row_en ? STK1160_V_DEC_EN : 0;
 148                val |= ctrl->col_mode ==
 149                        STK1160_DECIMATE_MORE_THAN_HALF ?
 150                        STK1160_H_DEC_MODE : 0;
 151                val |= ctrl->row_mode ==
 152                        STK1160_DECIMATE_MORE_THAN_HALF ?
 153                        STK1160_V_DEC_MODE : 0;
 154
 155                /* Horizontal count units */
 156                stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n);
 157                /* Vertical count units */
 158                stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n);
 159
 160                stk1160_dbg("decimate 0x%x, column units %d, row units %d\n",
 161                            val, ctrl->col_n, ctrl->row_n);
 162        }
 163
 164        /* Decimation control */
 165        stk1160_write_reg(dev, STK1160_DMCTRL, val);
 166}
 167
 168/*
 169 * Set a new alternate setting.
 170 * Returns true is dev->max_pkt_size has changed, false otherwise.
 171 */
 172static bool stk1160_set_alternate(struct stk1160 *dev)
 173{
 174        int i, prev_alt = dev->alt;
 175        unsigned int min_pkt_size;
 176        bool new_pkt_size;
 177
 178        /*
 179         * If we don't set right alternate,
 180         * then we will get a green screen with junk.
 181         */
 182        min_pkt_size = STK1160_MIN_PKT_SIZE;
 183
 184        for (i = 0; i < dev->num_alt; i++) {
 185                /* stop when the selected alt setting offers enough bandwidth */
 186                if (dev->alt_max_pkt_size[i] >= min_pkt_size) {
 187                        dev->alt = i;
 188                        break;
 189                /*
 190                 * otherwise make sure that we end up with the maximum bandwidth
 191                 * because the min_pkt_size equation might be wrong...
 192                 */
 193                } else if (dev->alt_max_pkt_size[i] >
 194                           dev->alt_max_pkt_size[dev->alt])
 195                        dev->alt = i;
 196        }
 197
 198        stk1160_dbg("setting alternate %d\n", dev->alt);
 199
 200        if (dev->alt != prev_alt) {
 201                stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
 202                                min_pkt_size, dev->alt);
 203                stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n",
 204                               dev->alt, dev->alt_max_pkt_size[dev->alt]);
 205                usb_set_interface(dev->udev, 0, dev->alt);
 206        }
 207
 208        new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt];
 209        dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt];
 210
 211        return new_pkt_size;
 212}
 213
 214static int stk1160_start_streaming(struct stk1160 *dev)
 215{
 216        bool new_pkt_size;
 217        int rc = 0;
 218        int i;
 219
 220        /* Check device presence */
 221        if (!dev->udev)
 222                return -ENODEV;
 223
 224        if (mutex_lock_interruptible(&dev->v4l_lock))
 225                return -ERESTARTSYS;
 226        /*
 227         * For some reason it is mandatory to set alternate *first*
 228         * and only *then* initialize isoc urbs.
 229         * Someone please explain me why ;)
 230         */
 231        new_pkt_size = stk1160_set_alternate(dev);
 232
 233        /*
 234         * We (re)allocate isoc urbs if:
 235         * there is no allocated isoc urbs, OR
 236         * a new dev->max_pkt_size is detected
 237         */
 238        if (!dev->isoc_ctl.num_bufs || new_pkt_size) {
 239                rc = stk1160_alloc_isoc(dev);
 240                if (rc < 0)
 241                        goto out_stop_hw;
 242        }
 243
 244        /* submit urbs and enables IRQ */
 245        for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
 246                rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
 247                if (rc) {
 248                        stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
 249                        goto out_uninit;
 250                }
 251        }
 252
 253        /* Start saa711x */
 254        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
 255
 256        dev->sequence = 0;
 257
 258        /* Start stk1160 */
 259        stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
 260        stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
 261
 262        stk1160_dbg("streaming started\n");
 263
 264        mutex_unlock(&dev->v4l_lock);
 265
 266        return 0;
 267
 268out_uninit:
 269        stk1160_uninit_isoc(dev);
 270out_stop_hw:
 271        usb_set_interface(dev->udev, 0, 0);
 272        stk1160_clear_queue(dev);
 273
 274        mutex_unlock(&dev->v4l_lock);
 275
 276        return rc;
 277}
 278
 279/* Must be called with v4l_lock hold */
 280static void stk1160_stop_hw(struct stk1160 *dev)
 281{
 282        /* If the device is not physically present, there is nothing to do */
 283        if (!dev->udev)
 284                return;
 285
 286        /* set alternate 0 */
 287        dev->alt = 0;
 288        stk1160_dbg("setting alternate %d\n", dev->alt);
 289        usb_set_interface(dev->udev, 0, 0);
 290
 291        /* Stop stk1160 */
 292        stk1160_write_reg(dev, STK1160_DCTRL, 0x00);
 293        stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
 294
 295        /* Stop saa711x */
 296        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
 297}
 298
 299static int stk1160_stop_streaming(struct stk1160 *dev)
 300{
 301        if (mutex_lock_interruptible(&dev->v4l_lock))
 302                return -ERESTARTSYS;
 303
 304        /*
 305         * Once URBs are cancelled, the URB complete handler
 306         * won't be running. This is required to safely release the
 307         * current buffer (dev->isoc_ctl.buf).
 308         */
 309        stk1160_cancel_isoc(dev);
 310
 311        /*
 312         * It is possible to keep buffers around using a module parameter.
 313         * This is intended to avoid memory fragmentation.
 314         */
 315        if (!keep_buffers)
 316                stk1160_free_isoc(dev);
 317
 318        stk1160_stop_hw(dev);
 319
 320        stk1160_clear_queue(dev);
 321
 322        stk1160_dbg("streaming stopped\n");
 323
 324        mutex_unlock(&dev->v4l_lock);
 325
 326        return 0;
 327}
 328
 329static struct v4l2_file_operations stk1160_fops = {
 330        .owner = THIS_MODULE,
 331        .open = v4l2_fh_open,
 332        .release = vb2_fop_release,
 333        .read = vb2_fop_read,
 334        .poll = vb2_fop_poll,
 335        .mmap = vb2_fop_mmap,
 336        .unlocked_ioctl = video_ioctl2,
 337};
 338
 339/*
 340 * vidioc ioctls
 341 */
 342static int vidioc_querycap(struct file *file,
 343                void *priv, struct v4l2_capability *cap)
 344{
 345        struct stk1160 *dev = video_drvdata(file);
 346
 347        strcpy(cap->driver, "stk1160");
 348        strcpy(cap->card, "stk1160");
 349        usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
 350        cap->device_caps =
 351                V4L2_CAP_VIDEO_CAPTURE |
 352                V4L2_CAP_STREAMING |
 353                V4L2_CAP_READWRITE;
 354        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 355        return 0;
 356}
 357
 358static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 359                struct v4l2_fmtdesc *f)
 360{
 361        if (f->index != 0)
 362                return -EINVAL;
 363
 364        strlcpy(f->description, format[f->index].name, sizeof(f->description));
 365        f->pixelformat = format[f->index].fourcc;
 366        return 0;
 367}
 368
 369static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 370                                        struct v4l2_format *f)
 371{
 372        struct stk1160 *dev = video_drvdata(file);
 373
 374        f->fmt.pix.width = dev->width;
 375        f->fmt.pix.height = dev->height;
 376        f->fmt.pix.field = V4L2_FIELD_INTERLACED;
 377        f->fmt.pix.pixelformat = dev->fmt->fourcc;
 378        f->fmt.pix.bytesperline = dev->width * 2;
 379        f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
 380        f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 381
 382        return 0;
 383}
 384
 385static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
 386                            struct stk1160_decimate_ctrl *ctrl)
 387{
 388        unsigned int width, height;
 389        unsigned int base_width, base_height;
 390        unsigned int col_n, row_n;
 391        enum stk1160_decimate_mode col_mode, row_mode;
 392        bool col_en, row_en;
 393
 394        base_width = 720;
 395        base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
 396
 397        /* Minimum width and height is 5% the frame size */
 398        width = clamp_t(unsigned int, f->fmt.pix.width,
 399                        base_width / 20, base_width);
 400        height = clamp_t(unsigned int, f->fmt.pix.height,
 401                        base_height / 20, base_height);
 402
 403        /* Let's set default no decimation values */
 404        col_n = 0;
 405        row_n = 0;
 406        col_en = false;
 407        row_en = false;
 408        f->fmt.pix.width = base_width;
 409        f->fmt.pix.height = base_height;
 410        row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
 411        col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
 412
 413        if (width < base_width && width > base_width / 2) {
 414                /*
 415                 * The device will send count units for each
 416                 * unit skipped. This means count unit is:
 417                 *
 418                 * n = width / (frame width - width)
 419                 *
 420                 * And the width is:
 421                 *
 422                 * width = (n / n + 1) * frame width
 423                 */
 424                col_n = div_round_integer(width, base_width - width);
 425                if (col_n > 0 && col_n <= 255) {
 426                        col_en = true;
 427                        col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
 428                        f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
 429                }
 430
 431        } else if (width <= base_width / 2) {
 432
 433                /*
 434                 * The device will skip count units for each
 435                 * unit sent. This means count is:
 436                 *
 437                 * n = (frame width / width) - 1
 438                 *
 439                 * And the width is:
 440                 *
 441                 * width = frame width / (n + 1)
 442                 */
 443                col_n = div_round_integer(base_width, width) - 1;
 444                if (col_n > 0 && col_n <= 255) {
 445                        col_en = true;
 446                        col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
 447                        f->fmt.pix.width = base_width / (col_n + 1);
 448                }
 449        }
 450
 451        if (height < base_height && height > base_height / 2) {
 452                row_n = div_round_integer(height, base_height - height);
 453                if (row_n > 0 && row_n <= 255) {
 454                        row_en = true;
 455                        row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
 456                        f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
 457                }
 458
 459        } else if (height <= base_height / 2) {
 460                row_n = div_round_integer(base_height, height) - 1;
 461                if (row_n > 0 && row_n <= 255) {
 462                        row_en = true;
 463                        row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
 464                        f->fmt.pix.height = base_height / (row_n + 1);
 465                }
 466        }
 467
 468        f->fmt.pix.pixelformat = dev->fmt->fourcc;
 469        f->fmt.pix.field = V4L2_FIELD_INTERLACED;
 470        f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
 471        f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
 472        f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 473
 474        if (ctrl) {
 475                ctrl->col_en = col_en;
 476                ctrl->col_n = col_n;
 477                ctrl->col_mode = col_mode;
 478                ctrl->row_en = row_en;
 479                ctrl->row_n = row_n;
 480                ctrl->row_mode = row_mode;
 481        }
 482
 483        stk1160_dbg("width %d, height %d\n",
 484                    f->fmt.pix.width, f->fmt.pix.height);
 485        return 0;
 486}
 487
 488static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 489                                  struct v4l2_format *f)
 490{
 491        struct stk1160 *dev = video_drvdata(file);
 492
 493        return stk1160_try_fmt(dev, f, NULL);
 494}
 495
 496static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 497                                        struct v4l2_format *f)
 498{
 499        struct stk1160 *dev = video_drvdata(file);
 500        struct vb2_queue *q = &dev->vb_vidq;
 501        struct stk1160_decimate_ctrl ctrl;
 502        int rc;
 503
 504        if (vb2_is_busy(q))
 505                return -EBUSY;
 506
 507        rc = stk1160_try_fmt(dev, f, &ctrl);
 508        if (rc < 0)
 509                return rc;
 510        dev->width = f->fmt.pix.width;
 511        dev->height = f->fmt.pix.height;
 512        stk1160_set_fmt(dev, &ctrl);
 513
 514        return 0;
 515}
 516
 517static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
 518{
 519        struct stk1160 *dev = video_drvdata(file);
 520        v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
 521        return 0;
 522}
 523
 524static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
 525{
 526        struct stk1160 *dev = video_drvdata(file);
 527
 528        *norm = dev->norm;
 529        return 0;
 530}
 531
 532static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
 533{
 534        struct stk1160 *dev = video_drvdata(file);
 535        struct vb2_queue *q = &dev->vb_vidq;
 536
 537        if (dev->norm == norm)
 538                return 0;
 539
 540        if (vb2_is_busy(q))
 541                return -EBUSY;
 542
 543        /* Check device presence */
 544        if (!dev->udev)
 545                return -ENODEV;
 546
 547        /* We need to set this now, before we call stk1160_set_std */
 548        dev->width = 720;
 549        dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
 550        dev->norm = norm;
 551
 552        stk1160_set_std(dev);
 553
 554        /* Calling with NULL disables frame decimation */
 555        stk1160_set_fmt(dev, NULL);
 556
 557        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
 558                        dev->norm);
 559
 560        return 0;
 561}
 562
 563
 564static int vidioc_enum_input(struct file *file, void *priv,
 565                                struct v4l2_input *i)
 566{
 567        struct stk1160 *dev = video_drvdata(file);
 568
 569        if (i->index > STK1160_MAX_INPUT)
 570                return -EINVAL;
 571
 572        /* S-Video special handling */
 573        if (i->index == STK1160_SVIDEO_INPUT)
 574                sprintf(i->name, "S-Video");
 575        else
 576                sprintf(i->name, "Composite%d", i->index);
 577
 578        i->type = V4L2_INPUT_TYPE_CAMERA;
 579        i->std = dev->vdev.tvnorms;
 580        return 0;
 581}
 582
 583static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
 584{
 585        struct stk1160 *dev = video_drvdata(file);
 586        *i = dev->ctl_input;
 587        return 0;
 588}
 589
 590static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
 591{
 592        struct stk1160 *dev = video_drvdata(file);
 593
 594        if (i > STK1160_MAX_INPUT)
 595                return -EINVAL;
 596
 597        dev->ctl_input = i;
 598
 599        stk1160_select_input(dev);
 600
 601        return 0;
 602}
 603
 604#ifdef CONFIG_VIDEO_ADV_DEBUG
 605static int vidioc_g_register(struct file *file, void *priv,
 606                             struct v4l2_dbg_register *reg)
 607{
 608        struct stk1160 *dev = video_drvdata(file);
 609        int rc;
 610        u8 val;
 611
 612        /* Match host */
 613        rc = stk1160_read_reg(dev, reg->reg, &val);
 614        reg->val = val;
 615        reg->size = 1;
 616
 617        return rc;
 618}
 619
 620static int vidioc_s_register(struct file *file, void *priv,
 621                             const struct v4l2_dbg_register *reg)
 622{
 623        struct stk1160 *dev = video_drvdata(file);
 624
 625        /* Match host */
 626        return stk1160_write_reg(dev, reg->reg, reg->val);
 627}
 628#endif
 629
 630static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
 631        .vidioc_querycap      = vidioc_querycap,
 632        .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
 633        .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
 634        .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
 635        .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
 636        .vidioc_querystd      = vidioc_querystd,
 637        .vidioc_g_std         = vidioc_g_std,
 638        .vidioc_s_std         = vidioc_s_std,
 639        .vidioc_enum_input    = vidioc_enum_input,
 640        .vidioc_g_input       = vidioc_g_input,
 641        .vidioc_s_input       = vidioc_s_input,
 642
 643        /* vb2 takes care of these */
 644        .vidioc_reqbufs       = vb2_ioctl_reqbufs,
 645        .vidioc_querybuf      = vb2_ioctl_querybuf,
 646        .vidioc_qbuf          = vb2_ioctl_qbuf,
 647        .vidioc_dqbuf         = vb2_ioctl_dqbuf,
 648        .vidioc_streamon      = vb2_ioctl_streamon,
 649        .vidioc_streamoff     = vb2_ioctl_streamoff,
 650        .vidioc_expbuf        = vb2_ioctl_expbuf,
 651
 652        .vidioc_log_status  = v4l2_ctrl_log_status,
 653        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
 654        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 655
 656#ifdef CONFIG_VIDEO_ADV_DEBUG
 657        .vidioc_g_register = vidioc_g_register,
 658        .vidioc_s_register = vidioc_s_register,
 659#endif
 660};
 661
 662/********************************************************************/
 663
 664/*
 665 * Videobuf2 operations
 666 */
 667static int queue_setup(struct vb2_queue *vq,
 668                                unsigned int *nbuffers, unsigned int *nplanes,
 669                                unsigned int sizes[], void *alloc_ctxs[])
 670{
 671        struct stk1160 *dev = vb2_get_drv_priv(vq);
 672        unsigned long size;
 673
 674        size = dev->width * dev->height * 2;
 675
 676        /*
 677         * Here we can change the number of buffers being requested.
 678         * So, we set a minimum and a maximum like this:
 679         */
 680        *nbuffers = clamp_t(unsigned int, *nbuffers,
 681                        STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS);
 682
 683        /* This means a packed colorformat */
 684        *nplanes = 1;
 685
 686        sizes[0] = size;
 687
 688        stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
 689                    __func__, *nbuffers, size);
 690
 691        return 0;
 692}
 693
 694static void buffer_queue(struct vb2_buffer *vb)
 695{
 696        unsigned long flags;
 697        struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
 698        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 699        struct stk1160_buffer *buf =
 700                container_of(vbuf, struct stk1160_buffer, vb);
 701
 702        spin_lock_irqsave(&dev->buf_lock, flags);
 703        if (!dev->udev) {
 704                /*
 705                 * If the device is disconnected return the buffer to userspace
 706                 * directly. The next QBUF call will fail with -ENODEV.
 707                 */
 708                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 709        } else {
 710
 711                buf->mem = vb2_plane_vaddr(vb, 0);
 712                buf->length = vb2_plane_size(vb, 0);
 713                buf->bytesused = 0;
 714                buf->pos = 0;
 715
 716                /*
 717                 * If buffer length is less from expected then we return
 718                 * the buffer to userspace directly.
 719                 */
 720                if (buf->length < dev->width * dev->height * 2)
 721                        vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 722                else
 723                        list_add_tail(&buf->list, &dev->avail_bufs);
 724
 725        }
 726        spin_unlock_irqrestore(&dev->buf_lock, flags);
 727}
 728
 729static int start_streaming(struct vb2_queue *vq, unsigned int count)
 730{
 731        struct stk1160 *dev = vb2_get_drv_priv(vq);
 732        return stk1160_start_streaming(dev);
 733}
 734
 735/* abort streaming and wait for last buffer */
 736static void stop_streaming(struct vb2_queue *vq)
 737{
 738        struct stk1160 *dev = vb2_get_drv_priv(vq);
 739        stk1160_stop_streaming(dev);
 740}
 741
 742static struct vb2_ops stk1160_video_qops = {
 743        .queue_setup            = queue_setup,
 744        .buf_queue              = buffer_queue,
 745        .start_streaming        = start_streaming,
 746        .stop_streaming         = stop_streaming,
 747        .wait_prepare           = vb2_ops_wait_prepare,
 748        .wait_finish            = vb2_ops_wait_finish,
 749};
 750
 751static struct video_device v4l_template = {
 752        .name = "stk1160",
 753        .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50,
 754        .fops = &stk1160_fops,
 755        .ioctl_ops = &stk1160_ioctl_ops,
 756        .release = video_device_release_empty,
 757};
 758
 759/********************************************************************/
 760
 761/* Must be called with both v4l_lock and vb_queue_lock hold */
 762void stk1160_clear_queue(struct stk1160 *dev)
 763{
 764        struct stk1160_buffer *buf;
 765        unsigned long flags;
 766
 767        /* Release all active buffers */
 768        spin_lock_irqsave(&dev->buf_lock, flags);
 769        while (!list_empty(&dev->avail_bufs)) {
 770                buf = list_first_entry(&dev->avail_bufs,
 771                        struct stk1160_buffer, list);
 772                list_del(&buf->list);
 773                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 774                stk1160_dbg("buffer [%p/%d] aborted\n",
 775                            buf, buf->vb.vb2_buf.index);
 776        }
 777
 778        /* It's important to release the current buffer */
 779        if (dev->isoc_ctl.buf) {
 780                buf = dev->isoc_ctl.buf;
 781                dev->isoc_ctl.buf = NULL;
 782
 783                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 784                stk1160_dbg("buffer [%p/%d] aborted\n",
 785                            buf, buf->vb.vb2_buf.index);
 786        }
 787        spin_unlock_irqrestore(&dev->buf_lock, flags);
 788}
 789
 790int stk1160_vb2_setup(struct stk1160 *dev)
 791{
 792        int rc;
 793        struct vb2_queue *q;
 794
 795        q = &dev->vb_vidq;
 796        q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 797        q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
 798        q->drv_priv = dev;
 799        q->buf_struct_size = sizeof(struct stk1160_buffer);
 800        q->ops = &stk1160_video_qops;
 801        q->mem_ops = &vb2_vmalloc_memops;
 802        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 803
 804        rc = vb2_queue_init(q);
 805        if (rc < 0)
 806                return rc;
 807
 808        /* initialize video dma queue */
 809        INIT_LIST_HEAD(&dev->avail_bufs);
 810
 811        return 0;
 812}
 813
 814int stk1160_video_register(struct stk1160 *dev)
 815{
 816        int rc;
 817
 818        /* Initialize video_device with a template structure */
 819        dev->vdev = v4l_template;
 820        dev->vdev.queue = &dev->vb_vidq;
 821
 822        /*
 823         * Provide mutexes for v4l2 core and for videobuf2 queue.
 824         * It will be used to protect *only* v4l2 ioctls.
 825         */
 826        dev->vdev.lock = &dev->v4l_lock;
 827        dev->vdev.queue->lock = &dev->vb_queue_lock;
 828
 829        /* This will be used to set video_device parent */
 830        dev->vdev.v4l2_dev = &dev->v4l2_dev;
 831
 832        /* NTSC is default */
 833        dev->norm = V4L2_STD_NTSC_M;
 834        dev->width = 720;
 835        dev->height = 480;
 836
 837        /* set default format */
 838        dev->fmt = &format[0];
 839        stk1160_set_std(dev);
 840
 841        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
 842                        dev->norm);
 843
 844        video_set_drvdata(&dev->vdev, dev);
 845        rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
 846        if (rc < 0) {
 847                stk1160_err("video_register_device failed (%d)\n", rc);
 848                return rc;
 849        }
 850
 851        v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
 852                  video_device_node_name(&dev->vdev));
 853
 854        return 0;
 855}
 856