linux/drivers/staging/media/solo6x10/solo6x10-v4l2-enc.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
   3 *
   4 * Original author:
   5 * Ben Collins <bcollins@ubuntu.com>
   6 *
   7 * Additional work by:
   8 * John Brooks <john.brooks@bluecherry.net>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License as published by
  12 * the Free Software Foundation; either version 2 of the License, or
  13 * (at your option) any later version.
  14 *
  15 * This program is distributed in the hope that it will be useful,
  16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 * GNU General Public License for more details.
  19 *
  20 * You should have received a copy of the GNU General Public License
  21 * along with this program; if not, write to the Free Software
  22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23 */
  24
  25#include <linux/kernel.h>
  26#include <linux/module.h>
  27#include <linux/kthread.h>
  28#include <linux/freezer.h>
  29
  30#include <media/v4l2-ioctl.h>
  31#include <media/v4l2-common.h>
  32#include <media/v4l2-event.h>
  33#include <media/videobuf2-dma-sg.h>
  34
  35#include "solo6x10.h"
  36#include "solo6x10-tw28.h"
  37#include "solo6x10-jpeg.h"
  38
  39#define MIN_VID_BUFFERS         2
  40#define FRAME_BUF_SIZE          (196 * 1024)
  41#define MP4_QS                  16
  42#define DMA_ALIGN               4096
  43
  44/* 6010 M4V */
  45static unsigned char vop_6010_ntsc_d1[] = {
  46        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  47        0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
  48        0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  49        0x1f, 0x4c, 0x58, 0x10, 0xf0, 0x71, 0x18, 0x3f,
  50};
  51
  52static unsigned char vop_6010_ntsc_cif[] = {
  53        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  54        0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
  55        0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  56        0x1f, 0x4c, 0x2c, 0x10, 0x78, 0x51, 0x18, 0x3f,
  57};
  58
  59static unsigned char vop_6010_pal_d1[] = {
  60        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  61        0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
  62        0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  63        0x1f, 0x4c, 0x58, 0x11, 0x20, 0x71, 0x18, 0x3f,
  64};
  65
  66static unsigned char vop_6010_pal_cif[] = {
  67        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
  68        0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
  69        0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
  70        0x1f, 0x4c, 0x2c, 0x10, 0x90, 0x51, 0x18, 0x3f,
  71};
  72
  73/* 6110 h.264 */
  74static unsigned char vop_6110_ntsc_d1[] = {
  75        0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  76        0x9a, 0x74, 0x05, 0x81, 0xec, 0x80, 0x00, 0x00,
  77        0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
  78};
  79
  80static unsigned char vop_6110_ntsc_cif[] = {
  81        0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  82        0x9a, 0x74, 0x0b, 0x0f, 0xc8, 0x00, 0x00, 0x00,
  83        0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
  84};
  85
  86static unsigned char vop_6110_pal_d1[] = {
  87        0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  88        0x9a, 0x74, 0x05, 0x80, 0x93, 0x20, 0x00, 0x00,
  89        0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
  90};
  91
  92static unsigned char vop_6110_pal_cif[] = {
  93        0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  94        0x9a, 0x74, 0x0b, 0x04, 0xb2, 0x00, 0x00, 0x00,
  95        0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
  96};
  97
  98typedef __le32 vop_header[16];
  99
 100struct solo_enc_buf {
 101        enum solo_enc_types     type;
 102        const vop_header        *vh;
 103        int                     motion;
 104};
 105
 106static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
 107{
 108        struct solo_dev *solo_dev = solo_enc->solo_dev;
 109
 110        return (solo_dev->motion_mask >> solo_enc->ch) & 1;
 111}
 112
 113static int solo_motion_detected(struct solo_enc_dev *solo_enc)
 114{
 115        struct solo_dev *solo_dev = solo_enc->solo_dev;
 116        unsigned long flags;
 117        u32 ch_mask = 1 << solo_enc->ch;
 118        int ret = 0;
 119
 120        spin_lock_irqsave(&solo_enc->motion_lock, flags);
 121        if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
 122                solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
 123                ret = 1;
 124        }
 125        spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
 126
 127        return ret;
 128}
 129
 130static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
 131{
 132        struct solo_dev *solo_dev = solo_enc->solo_dev;
 133        u32 mask = 1 << solo_enc->ch;
 134        unsigned long flags;
 135
 136        spin_lock_irqsave(&solo_enc->motion_lock, flags);
 137
 138        if (on)
 139                solo_dev->motion_mask |= mask;
 140        else
 141                solo_dev->motion_mask &= ~mask;
 142
 143        solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
 144
 145        solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
 146                       SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
 147                       (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
 148
 149        spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
 150}
 151
 152void solo_update_mode(struct solo_enc_dev *solo_enc)
 153{
 154        struct solo_dev *solo_dev = solo_enc->solo_dev;
 155        int vop_len;
 156        unsigned char *vop;
 157
 158        solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
 159        solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
 160
 161        if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
 162                solo_enc->width = solo_dev->video_hsize >> 1;
 163                solo_enc->height = solo_dev->video_vsize;
 164                if (solo_dev->type == SOLO_DEV_6110) {
 165                        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
 166                                vop = vop_6110_ntsc_cif;
 167                                vop_len = sizeof(vop_6110_ntsc_cif);
 168                        } else {
 169                                vop = vop_6110_pal_cif;
 170                                vop_len = sizeof(vop_6110_pal_cif);
 171                        }
 172                } else {
 173                        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
 174                                vop = vop_6010_ntsc_cif;
 175                                vop_len = sizeof(vop_6010_ntsc_cif);
 176                        } else {
 177                                vop = vop_6010_pal_cif;
 178                                vop_len = sizeof(vop_6010_pal_cif);
 179                        }
 180                }
 181        } else {
 182                solo_enc->width = solo_dev->video_hsize;
 183                solo_enc->height = solo_dev->video_vsize << 1;
 184                solo_enc->bw_weight <<= 2;
 185                if (solo_dev->type == SOLO_DEV_6110) {
 186                        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
 187                                vop = vop_6110_ntsc_d1;
 188                                vop_len = sizeof(vop_6110_ntsc_d1);
 189                        } else {
 190                                vop = vop_6110_pal_d1;
 191                                vop_len = sizeof(vop_6110_pal_d1);
 192                        }
 193                } else {
 194                        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
 195                                vop = vop_6010_ntsc_d1;
 196                                vop_len = sizeof(vop_6010_ntsc_d1);
 197                        } else {
 198                                vop = vop_6010_pal_d1;
 199                                vop_len = sizeof(vop_6010_pal_d1);
 200                        }
 201                }
 202        }
 203
 204        memcpy(solo_enc->vop, vop, vop_len);
 205
 206        /* Some fixups for 6010/M4V */
 207        if (solo_dev->type == SOLO_DEV_6010) {
 208                u16 fps = solo_dev->fps * 1000;
 209                u16 interval = solo_enc->interval * 1000;
 210
 211                vop = solo_enc->vop;
 212
 213                /* Frame rate and interval */
 214                vop[22] = fps >> 4;
 215                vop[23] = ((fps << 4) & 0xf0) | 0x0c
 216                        | ((interval >> 13) & 0x3);
 217                vop[24] = (interval >> 5) & 0xff;
 218                vop[25] = ((interval << 3) & 0xf8) | 0x04;
 219        }
 220
 221        solo_enc->vop_len = vop_len;
 222
 223        /* Now handle the jpeg header */
 224        vop = solo_enc->jpeg_header;
 225        vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
 226        vop[SOF0_START + 6] = 0xff & solo_enc->height;
 227        vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
 228        vop[SOF0_START + 8] = 0xff & solo_enc->width;
 229
 230        memcpy(vop + DQT_START,
 231               jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
 232}
 233
 234static int solo_enc_on(struct solo_enc_dev *solo_enc)
 235{
 236        u8 ch = solo_enc->ch;
 237        struct solo_dev *solo_dev = solo_enc->solo_dev;
 238        u8 interval;
 239
 240        solo_update_mode(solo_enc);
 241
 242        /* Make sure to do a bandwidth check */
 243        if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
 244                return -EBUSY;
 245        solo_enc->sequence = 0;
 246        solo_dev->enc_bw_remain -= solo_enc->bw_weight;
 247
 248        if (solo_enc->type == SOLO_ENC_TYPE_EXT)
 249                solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
 250
 251        /* Disable all encoding for this channel */
 252        solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
 253
 254        /* Common for both std and ext encoding */
 255        solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
 256                       solo_enc->interlaced ? 1 : 0);
 257
 258        if (solo_enc->interlaced)
 259                interval = solo_enc->interval - 1;
 260        else
 261                interval = solo_enc->interval;
 262
 263        /* Standard encoding only */
 264        solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
 265        solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
 266        solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
 267
 268        /* Extended encoding only */
 269        solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
 270        solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
 271        solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
 272
 273        /* Enables the standard encoder */
 274        solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
 275
 276        return 0;
 277}
 278
 279static void solo_enc_off(struct solo_enc_dev *solo_enc)
 280{
 281        struct solo_dev *solo_dev = solo_enc->solo_dev;
 282
 283        solo_dev->enc_bw_remain += solo_enc->bw_weight;
 284
 285        solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
 286        solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
 287}
 288
 289static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
 290                              unsigned int off, unsigned int size)
 291{
 292        int ret;
 293
 294        if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
 295                return -EINVAL;
 296
 297        /* Single shot */
 298        if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
 299                return solo_p2m_dma_t(solo_dev, 0, dma,
 300                                      SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
 301                                      0, 0);
 302        }
 303
 304        /* Buffer wrap */
 305        ret = solo_p2m_dma_t(solo_dev, 0, dma,
 306                             SOLO_MP4E_EXT_ADDR(solo_dev) + off,
 307                             SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
 308
 309        if (!ret) {
 310                ret = solo_p2m_dma_t(solo_dev, 0,
 311                             dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
 312                             SOLO_MP4E_EXT_ADDR(solo_dev),
 313                             size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
 314        }
 315
 316        return ret;
 317}
 318
 319/* Build a descriptor queue out of an SG list and send it to the P2M for
 320 * processing. */
 321static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
 322                          struct sg_table *vbuf, int off, int size,
 323                          unsigned int base, unsigned int base_size)
 324{
 325        struct solo_dev *solo_dev = solo_enc->solo_dev;
 326        struct scatterlist *sg;
 327        int i;
 328        int ret;
 329
 330        if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
 331                return -EINVAL;
 332
 333        solo_enc->desc_count = 1;
 334
 335        for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
 336                struct solo_p2m_desc *desc;
 337                dma_addr_t dma;
 338                int len;
 339                int left = base_size - off;
 340
 341                desc = &solo_enc->desc_items[solo_enc->desc_count++];
 342                dma = sg_dma_address(sg);
 343                len = sg_dma_len(sg);
 344
 345                /* We assume this is smaller than the scatter size */
 346                BUG_ON(skip >= len);
 347                if (skip) {
 348                        len -= skip;
 349                        dma += skip;
 350                        size -= skip;
 351                        skip = 0;
 352                }
 353
 354                len = min(len, size);
 355
 356                if (len <= left) {
 357                        /* Single descriptor */
 358                        solo_p2m_fill_desc(desc, 0, dma, base + off,
 359                                           len, 0, 0);
 360                } else {
 361                        /* Buffer wrap */
 362                        /* XXX: Do these as separate DMA requests, to avoid
 363                           timeout errors triggered by awkwardly sized
 364                           descriptors. See
 365                           <https://github.com/bluecherrydvr/solo6x10/issues/8>
 366                         */
 367                        ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
 368                                             left, 0, 0);
 369                        if (ret)
 370                                return ret;
 371
 372                        ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
 373                                             len - left, 0, 0);
 374                        if (ret)
 375                                return ret;
 376
 377                        solo_enc->desc_count--;
 378                }
 379
 380                size -= len;
 381                if (size <= 0)
 382                        break;
 383
 384                off += len;
 385                if (off >= base_size)
 386                        off -= base_size;
 387
 388                /* Because we may use two descriptors per loop */
 389                if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
 390                        ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
 391                                                solo_enc->desc_dma,
 392                                                solo_enc->desc_count - 1);
 393                        if (ret)
 394                                return ret;
 395                        solo_enc->desc_count = 1;
 396                }
 397        }
 398
 399        if (solo_enc->desc_count <= 1)
 400                return 0;
 401
 402        return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
 403                        solo_enc->desc_dma, solo_enc->desc_count - 1);
 404}
 405
 406/* Extract values from VOP header - VE_STATUSxx */
 407static inline int vop_interlaced(const vop_header *vh)
 408{
 409        return (__le32_to_cpu((*vh)[0]) >> 30) & 1;
 410}
 411
 412static inline u8 vop_channel(const vop_header *vh)
 413{
 414        return (__le32_to_cpu((*vh)[0]) >> 24) & 0x1F;
 415}
 416
 417static inline u8 vop_type(const vop_header *vh)
 418{
 419        return (__le32_to_cpu((*vh)[0]) >> 22) & 3;
 420}
 421
 422static inline u32 vop_mpeg_size(const vop_header *vh)
 423{
 424        return __le32_to_cpu((*vh)[0]) & 0xFFFFF;
 425}
 426
 427static inline u8 vop_hsize(const vop_header *vh)
 428{
 429        return (__le32_to_cpu((*vh)[1]) >> 8) & 0xFF;
 430}
 431
 432static inline u8 vop_vsize(const vop_header *vh)
 433{
 434        return __le32_to_cpu((*vh)[1]) & 0xFF;
 435}
 436
 437static inline u32 vop_mpeg_offset(const vop_header *vh)
 438{
 439        return __le32_to_cpu((*vh)[2]);
 440}
 441
 442static inline u32 vop_jpeg_offset(const vop_header *vh)
 443{
 444        return __le32_to_cpu((*vh)[3]);
 445}
 446
 447static inline u32 vop_jpeg_size(const vop_header *vh)
 448{
 449        return __le32_to_cpu((*vh)[4]) & 0xFFFFF;
 450}
 451
 452static inline u32 vop_sec(const vop_header *vh)
 453{
 454        return __le32_to_cpu((*vh)[5]);
 455}
 456
 457static inline u32 vop_usec(const vop_header *vh)
 458{
 459        return __le32_to_cpu((*vh)[6]);
 460}
 461
 462static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
 463                          struct vb2_buffer *vb, const vop_header *vh)
 464{
 465        struct solo_dev *solo_dev = solo_enc->solo_dev;
 466        struct sg_table *vbuf = vb2_dma_sg_plane_desc(vb, 0);
 467        int frame_size;
 468        int ret;
 469
 470        vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
 471
 472        if (vb2_plane_size(vb, 0) < vop_jpeg_size(vh) + solo_enc->jpeg_len)
 473                return -EIO;
 474
 475        frame_size = ALIGN(vop_jpeg_size(vh) + solo_enc->jpeg_len, DMA_ALIGN);
 476        vb2_set_plane_payload(vb, 0, vop_jpeg_size(vh) + solo_enc->jpeg_len);
 477
 478        /* may discard all previous data in vbuf->sgl */
 479        dma_map_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
 480                        DMA_FROM_DEVICE);
 481        ret = solo_send_desc(solo_enc, solo_enc->jpeg_len, vbuf,
 482                             vop_jpeg_offset(vh) - SOLO_JPEG_EXT_ADDR(solo_dev),
 483                             frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
 484                             SOLO_JPEG_EXT_SIZE(solo_dev));
 485        dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
 486                        DMA_FROM_DEVICE);
 487
 488        /* add the header only after dma_unmap_sg() */
 489        sg_copy_from_buffer(vbuf->sgl, vbuf->nents,
 490                            solo_enc->jpeg_header, solo_enc->jpeg_len);
 491
 492        return ret;
 493}
 494
 495static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
 496                struct vb2_buffer *vb, const vop_header *vh)
 497{
 498        struct solo_dev *solo_dev = solo_enc->solo_dev;
 499        struct sg_table *vbuf = vb2_dma_sg_plane_desc(vb, 0);
 500        int frame_off, frame_size;
 501        int skip = 0;
 502        int ret;
 503
 504        if (vb2_plane_size(vb, 0) < vop_mpeg_size(vh))
 505                return -EIO;
 506
 507        /* If this is a key frame, add extra header */
 508        vb->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME |
 509                V4L2_BUF_FLAG_BFRAME);
 510        if (!vop_type(vh)) {
 511                skip = solo_enc->vop_len;
 512                vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
 513                vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh) +
 514                        solo_enc->vop_len);
 515        } else {
 516                vb->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
 517                vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh));
 518        }
 519
 520        /* Now get the actual mpeg payload */
 521        frame_off = (vop_mpeg_offset(vh) - SOLO_MP4E_EXT_ADDR(solo_dev) +
 522                sizeof(*vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
 523        frame_size = ALIGN(vop_mpeg_size(vh) + skip, DMA_ALIGN);
 524
 525        /* may discard all previous data in vbuf->sgl */
 526        dma_map_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
 527                        DMA_FROM_DEVICE);
 528        ret = solo_send_desc(solo_enc, skip, vbuf, frame_off, frame_size,
 529                        SOLO_MP4E_EXT_ADDR(solo_dev),
 530                        SOLO_MP4E_EXT_SIZE(solo_dev));
 531        dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
 532                        DMA_FROM_DEVICE);
 533
 534        /* add the header only after dma_unmap_sg() */
 535        if (!vop_type(vh))
 536                sg_copy_from_buffer(vbuf->sgl, vbuf->nents,
 537                                    solo_enc->vop, solo_enc->vop_len);
 538        return ret;
 539}
 540
 541static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
 542                            struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
 543{
 544        const vop_header *vh = enc_buf->vh;
 545        int ret;
 546
 547        /* Check for motion flags */
 548        vb->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_MOTION_ON |
 549                                V4L2_BUF_FLAG_MOTION_DETECTED);
 550        if (solo_is_motion_on(solo_enc)) {
 551                vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_ON;
 552                if (enc_buf->motion)
 553                        vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_DETECTED;
 554        }
 555
 556        switch (solo_enc->fmt) {
 557        case V4L2_PIX_FMT_MPEG4:
 558        case V4L2_PIX_FMT_H264:
 559                ret = solo_fill_mpeg(solo_enc, vb, vh);
 560                break;
 561        default: /* V4L2_PIX_FMT_MJPEG */
 562                ret = solo_fill_jpeg(solo_enc, vb, vh);
 563                break;
 564        }
 565
 566        if (!ret) {
 567                vb->v4l2_buf.sequence = solo_enc->sequence++;
 568                vb->v4l2_buf.timestamp.tv_sec = vop_sec(vh);
 569                vb->v4l2_buf.timestamp.tv_usec = vop_usec(vh);
 570        }
 571
 572        vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
 573
 574        return ret;
 575}
 576
 577static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
 578                                struct solo_enc_buf *enc_buf)
 579{
 580        struct solo_vb2_buf *vb;
 581        unsigned long flags;
 582
 583        mutex_lock(&solo_enc->lock);
 584        if (solo_enc->type != enc_buf->type)
 585                goto unlock;
 586
 587        spin_lock_irqsave(&solo_enc->av_lock, flags);
 588        if (list_empty(&solo_enc->vidq_active)) {
 589                spin_unlock_irqrestore(&solo_enc->av_lock, flags);
 590                goto unlock;
 591        }
 592        vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf,
 593                list);
 594        list_del(&vb->list);
 595        spin_unlock_irqrestore(&solo_enc->av_lock, flags);
 596
 597        solo_enc_fillbuf(solo_enc, &vb->vb, enc_buf);
 598unlock:
 599        mutex_unlock(&solo_enc->lock);
 600}
 601
 602void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
 603{
 604        wake_up_interruptible_all(&solo_dev->ring_thread_wait);
 605}
 606
 607static void solo_handle_ring(struct solo_dev *solo_dev)
 608{
 609        for (;;) {
 610                struct solo_enc_dev *solo_enc;
 611                struct solo_enc_buf enc_buf;
 612                u32 mpeg_current, off;
 613                u8 ch;
 614                u8 cur_q;
 615
 616                /* Check if the hardware has any new ones in the queue */
 617                cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
 618                if (cur_q == solo_dev->enc_idx)
 619                        break;
 620
 621                mpeg_current = solo_reg_read(solo_dev,
 622                                        SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
 623                solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
 624
 625                ch = (mpeg_current >> 24) & 0x1f;
 626                off = mpeg_current & 0x00ffffff;
 627
 628                if (ch >= SOLO_MAX_CHANNELS) {
 629                        ch -= SOLO_MAX_CHANNELS;
 630                        enc_buf.type = SOLO_ENC_TYPE_EXT;
 631                } else
 632                        enc_buf.type = SOLO_ENC_TYPE_STD;
 633
 634                solo_enc = solo_dev->v4l2_enc[ch];
 635                if (solo_enc == NULL) {
 636                        dev_err(&solo_dev->pdev->dev,
 637                                "Got spurious packet for channel %d\n", ch);
 638                        continue;
 639                }
 640
 641                /* FAIL... */
 642                if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
 643                                     sizeof(vop_header)))
 644                        continue;
 645
 646                enc_buf.vh = solo_dev->vh_buf;
 647
 648                /* Sanity check */
 649                if (vop_mpeg_offset(enc_buf.vh) !=
 650                        SOLO_MP4E_EXT_ADDR(solo_dev) + off)
 651                        continue;
 652
 653                if (solo_motion_detected(solo_enc))
 654                        enc_buf.motion = 1;
 655                else
 656                        enc_buf.motion = 0;
 657
 658                solo_enc_handle_one(solo_enc, &enc_buf);
 659        }
 660}
 661
 662static int solo_ring_thread(void *data)
 663{
 664        struct solo_dev *solo_dev = data;
 665        DECLARE_WAITQUEUE(wait, current);
 666
 667        set_freezable();
 668        add_wait_queue(&solo_dev->ring_thread_wait, &wait);
 669
 670        for (;;) {
 671                long timeout = schedule_timeout_interruptible(HZ);
 672                if (timeout == -ERESTARTSYS || kthread_should_stop())
 673                        break;
 674                solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
 675                solo_handle_ring(solo_dev);
 676                solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
 677                try_to_freeze();
 678        }
 679
 680        remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
 681
 682        return 0;
 683}
 684
 685static int solo_enc_queue_setup(struct vb2_queue *q,
 686                                const struct v4l2_format *fmt,
 687                                unsigned int *num_buffers,
 688                                unsigned int *num_planes, unsigned int sizes[],
 689                                void *alloc_ctxs[])
 690{
 691        sizes[0] = FRAME_BUF_SIZE;
 692        *num_planes = 1;
 693
 694        if (*num_buffers < MIN_VID_BUFFERS)
 695                *num_buffers = MIN_VID_BUFFERS;
 696
 697        return 0;
 698}
 699
 700static void solo_enc_buf_queue(struct vb2_buffer *vb)
 701{
 702        struct vb2_queue *vq = vb->vb2_queue;
 703        struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
 704        struct solo_vb2_buf *solo_vb =
 705                container_of(vb, struct solo_vb2_buf, vb);
 706
 707        spin_lock(&solo_enc->av_lock);
 708        list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
 709        spin_unlock(&solo_enc->av_lock);
 710}
 711
 712static int solo_ring_start(struct solo_dev *solo_dev)
 713{
 714        solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
 715                                            SOLO6X10_NAME "_ring");
 716        if (IS_ERR(solo_dev->ring_thread)) {
 717                int err = PTR_ERR(solo_dev->ring_thread);
 718                solo_dev->ring_thread = NULL;
 719                return err;
 720        }
 721
 722        solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
 723
 724        return 0;
 725}
 726
 727static void solo_ring_stop(struct solo_dev *solo_dev)
 728{
 729        if (solo_dev->ring_thread) {
 730                kthread_stop(solo_dev->ring_thread);
 731                solo_dev->ring_thread = NULL;
 732        }
 733
 734        solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
 735}
 736
 737static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
 738{
 739        struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
 740        int ret;
 741
 742        ret = solo_enc_on(solo_enc);
 743        if (ret)
 744                return ret;
 745        return solo_ring_start(solo_enc->solo_dev);
 746}
 747
 748static int solo_enc_stop_streaming(struct vb2_queue *q)
 749{
 750        struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
 751
 752        solo_enc_off(solo_enc);
 753        INIT_LIST_HEAD(&solo_enc->vidq_active);
 754        solo_ring_stop(solo_enc->solo_dev);
 755        return 0;
 756}
 757
 758static struct vb2_ops solo_enc_video_qops = {
 759        .queue_setup    = solo_enc_queue_setup,
 760        .buf_queue      = solo_enc_buf_queue,
 761        .start_streaming = solo_enc_start_streaming,
 762        .stop_streaming = solo_enc_stop_streaming,
 763        .wait_prepare   = vb2_ops_wait_prepare,
 764        .wait_finish    = vb2_ops_wait_finish,
 765};
 766
 767static int solo_enc_querycap(struct file *file, void  *priv,
 768                             struct v4l2_capability *cap)
 769{
 770        struct solo_enc_dev *solo_enc = video_drvdata(file);
 771        struct solo_dev *solo_dev = solo_enc->solo_dev;
 772
 773        strcpy(cap->driver, SOLO6X10_NAME);
 774        snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
 775                 solo_enc->ch);
 776        snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
 777                 pci_name(solo_dev->pdev));
 778        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
 779                        V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
 780        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 781        return 0;
 782}
 783
 784static int solo_enc_enum_input(struct file *file, void *priv,
 785                               struct v4l2_input *input)
 786{
 787        struct solo_enc_dev *solo_enc = video_drvdata(file);
 788        struct solo_dev *solo_dev = solo_enc->solo_dev;
 789
 790        if (input->index)
 791                return -EINVAL;
 792
 793        snprintf(input->name, sizeof(input->name), "Encoder %d",
 794                 solo_enc->ch + 1);
 795        input->type = V4L2_INPUT_TYPE_CAMERA;
 796        input->std = solo_enc->vfd->tvnorms;
 797
 798        if (!tw28_get_video_status(solo_dev, solo_enc->ch))
 799                input->status = V4L2_IN_ST_NO_SIGNAL;
 800
 801        return 0;
 802}
 803
 804static int solo_enc_set_input(struct file *file, void *priv,
 805                              unsigned int index)
 806{
 807        if (index)
 808                return -EINVAL;
 809
 810        return 0;
 811}
 812
 813static int solo_enc_get_input(struct file *file, void *priv,
 814                              unsigned int *index)
 815{
 816        *index = 0;
 817
 818        return 0;
 819}
 820
 821static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
 822                                 struct v4l2_fmtdesc *f)
 823{
 824        struct solo_enc_dev *solo_enc = video_drvdata(file);
 825        int dev_type = solo_enc->solo_dev->type;
 826
 827        switch (f->index) {
 828        case 0:
 829                switch (dev_type) {
 830                case SOLO_DEV_6010:
 831                        f->pixelformat = V4L2_PIX_FMT_MPEG4;
 832                        strcpy(f->description, "MPEG-4 part 2");
 833                        break;
 834                case SOLO_DEV_6110:
 835                        f->pixelformat = V4L2_PIX_FMT_H264;
 836                        strcpy(f->description, "H.264");
 837                        break;
 838                }
 839                break;
 840        case 1:
 841                f->pixelformat = V4L2_PIX_FMT_MJPEG;
 842                strcpy(f->description, "MJPEG");
 843                break;
 844        default:
 845                return -EINVAL;
 846        }
 847
 848        f->flags = V4L2_FMT_FLAG_COMPRESSED;
 849
 850        return 0;
 851}
 852
 853static inline int solo_valid_pixfmt(u32 pixfmt, int dev_type)
 854{
 855        return (pixfmt == V4L2_PIX_FMT_H264 && dev_type == SOLO_DEV_6110)
 856                || (pixfmt == V4L2_PIX_FMT_MPEG4 && dev_type == SOLO_DEV_6010)
 857                || pixfmt == V4L2_PIX_FMT_MJPEG ? 0 : -EINVAL;
 858}
 859
 860static int solo_enc_try_fmt_cap(struct file *file, void *priv,
 861                            struct v4l2_format *f)
 862{
 863        struct solo_enc_dev *solo_enc = video_drvdata(file);
 864        struct solo_dev *solo_dev = solo_enc->solo_dev;
 865        struct v4l2_pix_format *pix = &f->fmt.pix;
 866
 867        if (solo_valid_pixfmt(pix->pixelformat, solo_dev->type))
 868                return -EINVAL;
 869
 870        if (pix->width < solo_dev->video_hsize ||
 871            pix->height < solo_dev->video_vsize << 1) {
 872                /* Default to CIF 1/2 size */
 873                pix->width = solo_dev->video_hsize >> 1;
 874                pix->height = solo_dev->video_vsize;
 875        } else {
 876                /* Full frame */
 877                pix->width = solo_dev->video_hsize;
 878                pix->height = solo_dev->video_vsize << 1;
 879        }
 880
 881        switch (pix->field) {
 882        case V4L2_FIELD_NONE:
 883        case V4L2_FIELD_INTERLACED:
 884                break;
 885        case V4L2_FIELD_ANY:
 886        default:
 887                pix->field = V4L2_FIELD_INTERLACED;
 888                break;
 889        }
 890
 891        /* Just set these */
 892        pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
 893        pix->sizeimage = FRAME_BUF_SIZE;
 894        pix->bytesperline = 0;
 895        pix->priv = 0;
 896
 897        return 0;
 898}
 899
 900static int solo_enc_set_fmt_cap(struct file *file, void *priv,
 901                                struct v4l2_format *f)
 902{
 903        struct solo_enc_dev *solo_enc = video_drvdata(file);
 904        struct solo_dev *solo_dev = solo_enc->solo_dev;
 905        struct v4l2_pix_format *pix = &f->fmt.pix;
 906        int ret;
 907
 908        if (vb2_is_busy(&solo_enc->vidq))
 909                return -EBUSY;
 910
 911        ret = solo_enc_try_fmt_cap(file, priv, f);
 912        if (ret)
 913                return ret;
 914
 915        if (pix->width == solo_dev->video_hsize)
 916                solo_enc->mode = SOLO_ENC_MODE_D1;
 917        else
 918                solo_enc->mode = SOLO_ENC_MODE_CIF;
 919
 920        /* This does not change the encoder at all */
 921        solo_enc->fmt = pix->pixelformat;
 922
 923        /*
 924         * More information is needed about these 'extended' types. As far
 925         * as I can tell these are basically additional video streams with
 926         * different MPEG encoding attributes that can run in parallel with
 927         * the main stream. If so, then this should be implemented as a
 928         * second video node. Abusing priv like this is certainly not the
 929         * right approach.
 930        if (pix->priv)
 931                solo_enc->type = SOLO_ENC_TYPE_EXT;
 932         */
 933        solo_update_mode(solo_enc);
 934        return 0;
 935}
 936
 937static int solo_enc_get_fmt_cap(struct file *file, void *priv,
 938                                struct v4l2_format *f)
 939{
 940        struct solo_enc_dev *solo_enc = video_drvdata(file);
 941        struct v4l2_pix_format *pix = &f->fmt.pix;
 942
 943        pix->width = solo_enc->width;
 944        pix->height = solo_enc->height;
 945        pix->pixelformat = solo_enc->fmt;
 946        pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
 947                     V4L2_FIELD_NONE;
 948        pix->sizeimage = FRAME_BUF_SIZE;
 949        pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
 950        pix->priv = 0;
 951
 952        return 0;
 953}
 954
 955static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
 956{
 957        struct solo_enc_dev *solo_enc = video_drvdata(file);
 958        struct solo_dev *solo_dev = solo_enc->solo_dev;
 959
 960        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
 961                *i = V4L2_STD_NTSC_M;
 962        else
 963                *i = V4L2_STD_PAL;
 964        return 0;
 965}
 966
 967static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
 968{
 969        struct solo_enc_dev *solo_enc = video_drvdata(file);
 970
 971        return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_625_50);
 972}
 973
 974static int solo_enum_framesizes(struct file *file, void *priv,
 975                                struct v4l2_frmsizeenum *fsize)
 976{
 977        struct solo_enc_dev *solo_enc = video_drvdata(file);
 978        struct solo_dev *solo_dev = solo_enc->solo_dev;
 979
 980        if (solo_valid_pixfmt(fsize->pixel_format, solo_dev->type))
 981                return -EINVAL;
 982
 983        switch (fsize->index) {
 984        case 0:
 985                fsize->discrete.width = solo_dev->video_hsize >> 1;
 986                fsize->discrete.height = solo_dev->video_vsize;
 987                break;
 988        case 1:
 989                fsize->discrete.width = solo_dev->video_hsize;
 990                fsize->discrete.height = solo_dev->video_vsize << 1;
 991                break;
 992        default:
 993                return -EINVAL;
 994        }
 995
 996        fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
 997
 998        return 0;
 999}
1000
1001static int solo_enum_frameintervals(struct file *file, void *priv,
1002                                    struct v4l2_frmivalenum *fintv)
1003{
1004        struct solo_enc_dev *solo_enc = video_drvdata(file);
1005        struct solo_dev *solo_dev = solo_enc->solo_dev;
1006
1007        if (solo_valid_pixfmt(fintv->pixel_format, solo_dev->type))
1008                return -EINVAL;
1009        if (fintv->index)
1010                return -EINVAL;
1011        if ((fintv->width != solo_dev->video_hsize >> 1 ||
1012             fintv->height != solo_dev->video_vsize) &&
1013            (fintv->width != solo_dev->video_hsize ||
1014             fintv->height != solo_dev->video_vsize << 1))
1015                return -EINVAL;
1016
1017        fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1018
1019        fintv->stepwise.min.numerator = 1;
1020        fintv->stepwise.min.denominator = solo_dev->fps;
1021
1022        fintv->stepwise.max.numerator = 15;
1023        fintv->stepwise.max.denominator = solo_dev->fps;
1024
1025        fintv->stepwise.step.numerator = 1;
1026        fintv->stepwise.step.denominator = solo_dev->fps;
1027
1028        return 0;
1029}
1030
1031static int solo_g_parm(struct file *file, void *priv,
1032                       struct v4l2_streamparm *sp)
1033{
1034        struct solo_enc_dev *solo_enc = video_drvdata(file);
1035        struct v4l2_captureparm *cp = &sp->parm.capture;
1036
1037        cp->capability = V4L2_CAP_TIMEPERFRAME;
1038        cp->timeperframe.numerator = solo_enc->interval;
1039        cp->timeperframe.denominator = solo_enc->solo_dev->fps;
1040        cp->capturemode = 0;
1041        /* XXX: Shouldn't we be able to get/set this from videobuf? */
1042        cp->readbuffers = 2;
1043
1044        return 0;
1045}
1046
1047static inline int calc_interval(u8 fps, u32 n, u32 d)
1048{
1049        if (!n || !d)
1050                return 1;
1051        if (d == fps)
1052                return n;
1053        n *= fps;
1054        return min(15U, n / d + (n % d >= (fps >> 1)));
1055}
1056
1057static int solo_s_parm(struct file *file, void *priv,
1058                       struct v4l2_streamparm *sp)
1059{
1060        struct solo_enc_dev *solo_enc = video_drvdata(file);
1061        struct v4l2_fract *t = &sp->parm.capture.timeperframe;
1062        u8 fps = solo_enc->solo_dev->fps;
1063
1064        if (vb2_is_streaming(&solo_enc->vidq))
1065                return -EBUSY;
1066
1067        solo_enc->interval = calc_interval(fps, t->numerator, t->denominator);
1068        solo_update_mode(solo_enc);
1069        return solo_g_parm(file, priv, sp);
1070}
1071
1072static long solo_enc_default(struct file *file, void *fh,
1073                        bool valid_prio, unsigned int cmd, void *arg)
1074{
1075        struct solo_enc_dev *solo_enc = video_drvdata(file);
1076        struct solo_dev *solo_dev = solo_enc->solo_dev;
1077        struct solo_motion_thresholds *thresholds = arg;
1078
1079        switch (cmd) {
1080        case SOLO_IOC_G_MOTION_THRESHOLDS:
1081                *thresholds = solo_enc->motion_thresholds;
1082                return 0;
1083
1084        case SOLO_IOC_S_MOTION_THRESHOLDS:
1085                if (!valid_prio)
1086                        return -EBUSY;
1087                solo_enc->motion_thresholds = *thresholds;
1088                if (solo_enc->motion_enabled && !solo_enc->motion_global)
1089                        return solo_set_motion_block(solo_dev, solo_enc->ch,
1090                                                &solo_enc->motion_thresholds);
1091                return 0;
1092        default:
1093                return -ENOTTY;
1094        }
1095}
1096
1097static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
1098{
1099        struct solo_enc_dev *solo_enc =
1100                container_of(ctrl->handler, struct solo_enc_dev, hdl);
1101        struct solo_dev *solo_dev = solo_enc->solo_dev;
1102        int err;
1103
1104        switch (ctrl->id) {
1105        case V4L2_CID_BRIGHTNESS:
1106        case V4L2_CID_CONTRAST:
1107        case V4L2_CID_SATURATION:
1108        case V4L2_CID_HUE:
1109        case V4L2_CID_SHARPNESS:
1110                return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
1111                                         ctrl->val);
1112        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1113                solo_enc->gop = ctrl->val;
1114                return 0;
1115        case V4L2_CID_MOTION_THRESHOLD:
1116                solo_enc->motion_thresh = ctrl->val;
1117                if (!solo_enc->motion_global || !solo_enc->motion_enabled)
1118                        return 0;
1119                return solo_set_motion_threshold(solo_dev, solo_enc->ch,
1120                        ctrl->val);
1121        case V4L2_CID_MOTION_MODE:
1122                solo_enc->motion_global = ctrl->val == 1;
1123                solo_enc->motion_enabled = ctrl->val > 0;
1124                if (ctrl->val) {
1125                        if (solo_enc->motion_global)
1126                                solo_set_motion_threshold(solo_dev,
1127                                        solo_enc->ch, solo_enc->motion_thresh);
1128                        else
1129                                solo_set_motion_block(solo_dev, solo_enc->ch,
1130                                                &solo_enc->motion_thresholds);
1131                }
1132                solo_motion_toggle(solo_enc, ctrl->val);
1133                return 0;
1134        case V4L2_CID_OSD_TEXT:
1135                strcpy(solo_enc->osd_text, ctrl->string);
1136                err = solo_osd_print(solo_enc);
1137                return err;
1138        default:
1139                return -EINVAL;
1140        }
1141
1142        return 0;
1143}
1144
1145static const struct v4l2_file_operations solo_enc_fops = {
1146        .owner                  = THIS_MODULE,
1147        .open                   = v4l2_fh_open,
1148        .release                = vb2_fop_release,
1149        .read                   = vb2_fop_read,
1150        .poll                   = vb2_fop_poll,
1151        .mmap                   = vb2_fop_mmap,
1152        .unlocked_ioctl         = video_ioctl2,
1153};
1154
1155static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
1156        .vidioc_querycap                = solo_enc_querycap,
1157        .vidioc_s_std                   = solo_enc_s_std,
1158        .vidioc_g_std                   = solo_enc_g_std,
1159        /* Input callbacks */
1160        .vidioc_enum_input              = solo_enc_enum_input,
1161        .vidioc_s_input                 = solo_enc_set_input,
1162        .vidioc_g_input                 = solo_enc_get_input,
1163        /* Video capture format callbacks */
1164        .vidioc_enum_fmt_vid_cap        = solo_enc_enum_fmt_cap,
1165        .vidioc_try_fmt_vid_cap         = solo_enc_try_fmt_cap,
1166        .vidioc_s_fmt_vid_cap           = solo_enc_set_fmt_cap,
1167        .vidioc_g_fmt_vid_cap           = solo_enc_get_fmt_cap,
1168        /* Streaming I/O */
1169        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1170        .vidioc_querybuf                = vb2_ioctl_querybuf,
1171        .vidioc_qbuf                    = vb2_ioctl_qbuf,
1172        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1173        .vidioc_streamon                = vb2_ioctl_streamon,
1174        .vidioc_streamoff               = vb2_ioctl_streamoff,
1175        /* Frame size and interval */
1176        .vidioc_enum_framesizes         = solo_enum_framesizes,
1177        .vidioc_enum_frameintervals     = solo_enum_frameintervals,
1178        /* Video capture parameters */
1179        .vidioc_s_parm                  = solo_s_parm,
1180        .vidioc_g_parm                  = solo_g_parm,
1181        /* Logging and events */
1182        .vidioc_log_status              = v4l2_ctrl_log_status,
1183        .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
1184        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
1185        .vidioc_default                 = solo_enc_default,
1186};
1187
1188static const struct video_device solo_enc_template = {
1189        .name                   = SOLO6X10_NAME,
1190        .fops                   = &solo_enc_fops,
1191        .ioctl_ops              = &solo_enc_ioctl_ops,
1192        .minor                  = -1,
1193        .release                = video_device_release,
1194        .tvnorms                = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1195};
1196
1197static const struct v4l2_ctrl_ops solo_ctrl_ops = {
1198        .s_ctrl = solo_s_ctrl,
1199};
1200
1201static const struct v4l2_ctrl_config solo_motion_threshold_ctrl = {
1202        .ops = &solo_ctrl_ops,
1203        .id = V4L2_CID_MOTION_THRESHOLD,
1204        .name = "Motion Detection Threshold",
1205        .type = V4L2_CTRL_TYPE_INTEGER,
1206        .max = 0xffff,
1207        .def = SOLO_DEF_MOT_THRESH,
1208        .step = 1,
1209        .flags = V4L2_CTRL_FLAG_SLIDER,
1210};
1211
1212static const char * const solo_motion_mode_menu[] = {
1213        "Disabled",
1214        "Global Threshold",
1215        "Regional Threshold",
1216        NULL
1217};
1218
1219static const struct v4l2_ctrl_config solo_motion_enable_ctrl = {
1220        .ops = &solo_ctrl_ops,
1221        .id = V4L2_CID_MOTION_MODE,
1222        .name = "Motion Detection Mode",
1223        .type = V4L2_CTRL_TYPE_MENU,
1224        .qmenu = solo_motion_mode_menu,
1225        .max = 2,
1226};
1227
1228static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
1229        .ops = &solo_ctrl_ops,
1230        .id = V4L2_CID_OSD_TEXT,
1231        .name = "OSD Text",
1232        .type = V4L2_CTRL_TYPE_STRING,
1233        .max = OSD_TEXT_MAX,
1234        .step = 1,
1235};
1236
1237static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1238                                           u8 ch, unsigned nr)
1239{
1240        struct solo_enc_dev *solo_enc;
1241        struct v4l2_ctrl_handler *hdl;
1242        int ret;
1243        int x, y;
1244
1245        solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
1246        if (!solo_enc)
1247                return ERR_PTR(-ENOMEM);
1248
1249        hdl = &solo_enc->hdl;
1250        v4l2_ctrl_handler_init(hdl, 10);
1251        v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1252                        V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1253        v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1254                        V4L2_CID_CONTRAST, 0, 255, 1, 128);
1255        v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1256                        V4L2_CID_SATURATION, 0, 255, 1, 128);
1257        v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1258                        V4L2_CID_HUE, 0, 255, 1, 128);
1259        if (tw28_has_sharpness(solo_dev, ch))
1260                v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1261                        V4L2_CID_SHARPNESS, 0, 15, 1, 0);
1262        v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1263                        V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
1264        v4l2_ctrl_new_custom(hdl, &solo_motion_threshold_ctrl, NULL);
1265        v4l2_ctrl_new_custom(hdl, &solo_motion_enable_ctrl, NULL);
1266        v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
1267        if (hdl->error) {
1268                ret = hdl->error;
1269                goto hdl_free;
1270        }
1271
1272        solo_enc->solo_dev = solo_dev;
1273        solo_enc->ch = ch;
1274        mutex_init(&solo_enc->lock);
1275        spin_lock_init(&solo_enc->av_lock);
1276        INIT_LIST_HEAD(&solo_enc->vidq_active);
1277        solo_enc->fmt = (solo_dev->type == SOLO_DEV_6010) ?
1278                V4L2_PIX_FMT_MPEG4 : V4L2_PIX_FMT_H264;
1279        solo_enc->type = SOLO_ENC_TYPE_STD;
1280
1281        solo_enc->qp = SOLO_DEFAULT_QP;
1282        solo_enc->gop = solo_dev->fps;
1283        solo_enc->interval = 1;
1284        solo_enc->mode = SOLO_ENC_MODE_CIF;
1285        solo_enc->motion_global = true;
1286        solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
1287        for (y = 0; y < SOLO_MOTION_SZ; y++)
1288                for (x = 0; x < SOLO_MOTION_SZ; x++)
1289                        solo_enc->motion_thresholds.thresholds[y][x] =
1290                                                        SOLO_DEF_MOT_THRESH;
1291
1292        solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1293        solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1294        solo_enc->vidq.ops = &solo_enc_video_qops;
1295        solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
1296        solo_enc->vidq.drv_priv = solo_enc;
1297        solo_enc->vidq.gfp_flags = __GFP_DMA32;
1298        solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1299        solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1300        solo_enc->vidq.lock = &solo_enc->lock;
1301        ret = vb2_queue_init(&solo_enc->vidq);
1302        if (ret)
1303                goto hdl_free;
1304        solo_update_mode(solo_enc);
1305
1306        spin_lock_init(&solo_enc->motion_lock);
1307
1308        /* Initialize this per encoder */
1309        solo_enc->jpeg_len = sizeof(jpeg_header);
1310        memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
1311
1312        solo_enc->desc_nelts = 32;
1313        solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
1314                                      sizeof(struct solo_p2m_desc) *
1315                                      solo_enc->desc_nelts,
1316                                      &solo_enc->desc_dma);
1317        ret = -ENOMEM;
1318        if (solo_enc->desc_items == NULL)
1319                goto hdl_free;
1320
1321        solo_enc->vfd = video_device_alloc();
1322        if (!solo_enc->vfd)
1323                goto pci_free;
1324
1325        *solo_enc->vfd = solo_enc_template;
1326        solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
1327        solo_enc->vfd->ctrl_handler = hdl;
1328        solo_enc->vfd->queue = &solo_enc->vidq;
1329        solo_enc->vfd->lock = &solo_enc->lock;
1330        set_bit(V4L2_FL_USE_FH_PRIO, &solo_enc->vfd->flags);
1331        video_set_drvdata(solo_enc->vfd, solo_enc);
1332        ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
1333        if (ret < 0)
1334                goto vdev_release;
1335
1336        snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
1337                 "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
1338                 solo_enc->vfd->num);
1339
1340        return solo_enc;
1341
1342vdev_release:
1343        video_device_release(solo_enc->vfd);
1344pci_free:
1345        pci_free_consistent(solo_enc->solo_dev->pdev,
1346                        sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
1347                        solo_enc->desc_items, solo_enc->desc_dma);
1348hdl_free:
1349        v4l2_ctrl_handler_free(hdl);
1350        kfree(solo_enc);
1351        return ERR_PTR(ret);
1352}
1353
1354static void solo_enc_free(struct solo_enc_dev *solo_enc)
1355{
1356        if (solo_enc == NULL)
1357                return;
1358
1359        video_unregister_device(solo_enc->vfd);
1360        v4l2_ctrl_handler_free(&solo_enc->hdl);
1361        kfree(solo_enc);
1362}
1363
1364int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
1365{
1366        int i;
1367
1368        init_waitqueue_head(&solo_dev->ring_thread_wait);
1369
1370        solo_dev->vh_size = sizeof(vop_header);
1371        solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
1372                                                solo_dev->vh_size,
1373                                                &solo_dev->vh_dma);
1374        if (solo_dev->vh_buf == NULL)
1375                return -ENOMEM;
1376
1377        for (i = 0; i < solo_dev->nr_chans; i++) {
1378                solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
1379                if (IS_ERR(solo_dev->v4l2_enc[i]))
1380                        break;
1381        }
1382
1383        if (i != solo_dev->nr_chans) {
1384                int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
1385                while (i--)
1386                        solo_enc_free(solo_dev->v4l2_enc[i]);
1387                pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1388                                    solo_dev->vh_buf, solo_dev->vh_dma);
1389                solo_dev->vh_buf = NULL;
1390                return ret;
1391        }
1392
1393        if (solo_dev->type == SOLO_DEV_6010)
1394                solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
1395        else
1396                solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
1397
1398        dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
1399                 solo_dev->v4l2_enc[0]->vfd->num,
1400                 solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
1401
1402        return 0;
1403}
1404
1405void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
1406{
1407        int i;
1408
1409        for (i = 0; i < solo_dev->nr_chans; i++)
1410                solo_enc_free(solo_dev->v4l2_enc[i]);
1411
1412        if (solo_dev->vh_buf)
1413                pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1414                            solo_dev->vh_buf, solo_dev->vh_dma);
1415}
1416