linux/drivers/staging/media/solo6x10/solo6x10-v4l2.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-contig.h>
  34
  35#include "solo6x10.h"
  36#include "solo6x10-tw28.h"
  37
  38/* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
  39#define SOLO_HW_BPL             2048
  40#define solo_vlines(__solo)     (__solo->video_vsize * 2)
  41#define solo_image_size(__solo) (solo_bytesperline(__solo) * \
  42                                 solo_vlines(__solo))
  43#define solo_bytesperline(__solo) (__solo->video_hsize * 2)
  44
  45#define MIN_VID_BUFFERS         2
  46
  47static inline void erase_on(struct solo_dev *solo_dev)
  48{
  49        solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
  50        solo_dev->erasing = 1;
  51        solo_dev->frame_blank = 0;
  52}
  53
  54static inline int erase_off(struct solo_dev *solo_dev)
  55{
  56        if (!solo_dev->erasing)
  57                return 0;
  58
  59        /* First time around, assert erase off */
  60        if (!solo_dev->frame_blank)
  61                solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
  62        /* Keep the erasing flag on for 8 frames minimum */
  63        if (solo_dev->frame_blank++ >= 8)
  64                solo_dev->erasing = 0;
  65
  66        return 1;
  67}
  68
  69void solo_video_in_isr(struct solo_dev *solo_dev)
  70{
  71        wake_up_interruptible_all(&solo_dev->disp_thread_wait);
  72}
  73
  74static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
  75                           int sx, int sy, int ex, int ey, int scale)
  76{
  77        if (ch >= solo_dev->nr_chans)
  78                return;
  79
  80        /* Here, we just keep window/channel the same */
  81        solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
  82                       SOLO_VI_WIN_CHANNEL(ch) |
  83                       SOLO_VI_WIN_SX(sx) |
  84                       SOLO_VI_WIN_EX(ex) |
  85                       SOLO_VI_WIN_SCALE(scale));
  86
  87        solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
  88                       SOLO_VI_WIN_SY(sy) |
  89                       SOLO_VI_WIN_EY(ey));
  90}
  91
  92static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
  93{
  94        u8 ch = idx * 4;
  95
  96        if (ch >= solo_dev->nr_chans)
  97                return -EINVAL;
  98
  99        if (!on) {
 100                u8 i;
 101                for (i = ch; i < ch + 4; i++)
 102                        solo_win_setup(solo_dev, i, solo_dev->video_hsize,
 103                                       solo_vlines(solo_dev),
 104                                       solo_dev->video_hsize,
 105                                       solo_vlines(solo_dev), 0);
 106                return 0;
 107        }
 108
 109        /* Row 1 */
 110        solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
 111                       solo_vlines(solo_dev) / 2, 3);
 112        solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
 113                       solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
 114        /* Row 2 */
 115        solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
 116                       solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
 117        solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
 118                       solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
 119                       solo_vlines(solo_dev), 3);
 120
 121        return 0;
 122}
 123
 124static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
 125{
 126        int sy, ysize, hsize, i;
 127
 128        if (!on) {
 129                for (i = 0; i < 16; i++)
 130                        solo_win_setup(solo_dev, i, solo_dev->video_hsize,
 131                                       solo_vlines(solo_dev),
 132                                       solo_dev->video_hsize,
 133                                       solo_vlines(solo_dev), 0);
 134                return 0;
 135        }
 136
 137        ysize = solo_vlines(solo_dev) / 4;
 138        hsize = solo_dev->video_hsize / 4;
 139
 140        for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
 141                solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
 142                               sy + ysize, 5);
 143                solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
 144                               hsize * 2, sy + ysize, 5);
 145                solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
 146                               hsize * 3, sy + ysize, 5);
 147                solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
 148                               solo_dev->video_hsize, sy + ysize, 5);
 149        }
 150
 151        return 0;
 152}
 153
 154static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
 155{
 156        u8 ext_ch;
 157
 158        if (ch < solo_dev->nr_chans) {
 159                solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
 160                               on ? 0 : solo_vlines(solo_dev),
 161                               solo_dev->video_hsize, solo_vlines(solo_dev),
 162                               on ? 1 : 0);
 163                return 0;
 164        }
 165
 166        if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
 167                return -EINVAL;
 168
 169        ext_ch = ch - solo_dev->nr_chans;
 170
 171        /* 4up's first */
 172        if (ext_ch < 4)
 173                return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
 174
 175        /* Remaining case is 16up for 16-port */
 176        return solo_v4l2_ch_ext_16up(solo_dev, on);
 177}
 178
 179static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
 180{
 181        if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
 182                return -EINVAL;
 183
 184        erase_on(solo_dev);
 185
 186        solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
 187        solo_v4l2_ch(solo_dev, ch, 1);
 188
 189        solo_dev->cur_disp_ch = ch;
 190
 191        return 0;
 192}
 193
 194static void solo_fillbuf(struct solo_dev *solo_dev,
 195                         struct vb2_buffer *vb)
 196{
 197        dma_addr_t vbuf;
 198        unsigned int fdma_addr;
 199        int error = -1;
 200        int i;
 201
 202        vbuf = vb2_dma_contig_plane_dma_addr(vb, 0);
 203        if (!vbuf)
 204                goto finish_buf;
 205
 206        if (erase_off(solo_dev)) {
 207                void *p = vb2_plane_vaddr(vb, 0);
 208                int image_size = solo_image_size(solo_dev);
 209                for (i = 0; i < image_size; i += 2) {
 210                        ((u8 *)p)[i] = 0x80;
 211                        ((u8 *)p)[i + 1] = 0x00;
 212                }
 213                error = 0;
 214        } else {
 215                fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
 216                                (SOLO_HW_BPL * solo_vlines(solo_dev)));
 217
 218                error = solo_p2m_dma_t(solo_dev, 0, vbuf, fdma_addr,
 219                                       solo_bytesperline(solo_dev),
 220                                       solo_vlines(solo_dev), SOLO_HW_BPL);
 221        }
 222
 223finish_buf:
 224        if (!error) {
 225                vb2_set_plane_payload(vb, 0,
 226                        solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
 227                vb->v4l2_buf.sequence = solo_dev->sequence++;
 228                v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
 229        }
 230
 231        vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
 232}
 233
 234static void solo_thread_try(struct solo_dev *solo_dev)
 235{
 236        struct solo_vb2_buf *vb;
 237
 238        /* Only "break" from this loop if slock is held, otherwise
 239         * just return. */
 240        for (;;) {
 241                unsigned int cur_write;
 242
 243                cur_write = SOLO_VI_STATUS0_PAGE(
 244                        solo_reg_read(solo_dev, SOLO_VI_STATUS0));
 245                if (cur_write == solo_dev->old_write)
 246                        return;
 247
 248                spin_lock(&solo_dev->slock);
 249
 250                if (list_empty(&solo_dev->vidq_active))
 251                        break;
 252
 253                vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
 254                                      list);
 255
 256                solo_dev->old_write = cur_write;
 257                list_del(&vb->list);
 258
 259                spin_unlock(&solo_dev->slock);
 260
 261                solo_fillbuf(solo_dev, &vb->vb);
 262        }
 263
 264        assert_spin_locked(&solo_dev->slock);
 265        spin_unlock(&solo_dev->slock);
 266}
 267
 268static int solo_thread(void *data)
 269{
 270        struct solo_dev *solo_dev = data;
 271        DECLARE_WAITQUEUE(wait, current);
 272
 273        set_freezable();
 274        add_wait_queue(&solo_dev->disp_thread_wait, &wait);
 275
 276        for (;;) {
 277                long timeout = schedule_timeout_interruptible(HZ);
 278                if (timeout == -ERESTARTSYS || kthread_should_stop())
 279                        break;
 280                solo_thread_try(solo_dev);
 281                try_to_freeze();
 282        }
 283
 284        remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
 285
 286        return 0;
 287}
 288
 289static int solo_start_thread(struct solo_dev *solo_dev)
 290{
 291        int ret = 0;
 292
 293        solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
 294
 295        if (IS_ERR(solo_dev->kthread)) {
 296                ret = PTR_ERR(solo_dev->kthread);
 297                solo_dev->kthread = NULL;
 298                return ret;
 299        }
 300        solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
 301
 302        return ret;
 303}
 304
 305static void solo_stop_thread(struct solo_dev *solo_dev)
 306{
 307        if (!solo_dev->kthread)
 308                return;
 309
 310        solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
 311        kthread_stop(solo_dev->kthread);
 312        solo_dev->kthread = NULL;
 313}
 314
 315static int solo_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
 316                           unsigned int *num_buffers, unsigned int *num_planes,
 317                           unsigned int sizes[], void *alloc_ctxs[])
 318{
 319        struct solo_dev *solo_dev = vb2_get_drv_priv(q);
 320
 321        sizes[0] = solo_image_size(solo_dev);
 322        alloc_ctxs[0] = solo_dev->alloc_ctx;
 323        *num_planes = 1;
 324
 325        if (*num_buffers < MIN_VID_BUFFERS)
 326                *num_buffers = MIN_VID_BUFFERS;
 327
 328        return 0;
 329}
 330
 331static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
 332{
 333        struct solo_dev *solo_dev = vb2_get_drv_priv(q);
 334
 335        solo_dev->sequence = 0;
 336        return solo_start_thread(solo_dev);
 337}
 338
 339static int solo_stop_streaming(struct vb2_queue *q)
 340{
 341        struct solo_dev *solo_dev = vb2_get_drv_priv(q);
 342
 343        solo_stop_thread(solo_dev);
 344        INIT_LIST_HEAD(&solo_dev->vidq_active);
 345        return 0;
 346}
 347
 348static void solo_buf_queue(struct vb2_buffer *vb)
 349{
 350        struct vb2_queue *vq = vb->vb2_queue;
 351        struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
 352        struct solo_vb2_buf *solo_vb =
 353                container_of(vb, struct solo_vb2_buf, vb);
 354
 355        spin_lock(&solo_dev->slock);
 356        list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
 357        spin_unlock(&solo_dev->slock);
 358        wake_up_interruptible(&solo_dev->disp_thread_wait);
 359}
 360
 361static const struct vb2_ops solo_video_qops = {
 362        .queue_setup    = solo_queue_setup,
 363        .buf_queue      = solo_buf_queue,
 364        .start_streaming = solo_start_streaming,
 365        .stop_streaming = solo_stop_streaming,
 366        .wait_prepare   = vb2_ops_wait_prepare,
 367        .wait_finish    = vb2_ops_wait_finish,
 368};
 369
 370static int solo_querycap(struct file *file, void  *priv,
 371                         struct v4l2_capability *cap)
 372{
 373        struct solo_dev *solo_dev = video_drvdata(file);
 374
 375        strcpy(cap->driver, SOLO6X10_NAME);
 376        strcpy(cap->card, "Softlogic 6x10");
 377        snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
 378                 pci_name(solo_dev->pdev));
 379        cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
 380                        V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
 381        cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
 382        return 0;
 383}
 384
 385static int solo_enum_ext_input(struct solo_dev *solo_dev,
 386                               struct v4l2_input *input)
 387{
 388        static const char * const dispnames_1[] = { "4UP" };
 389        static const char * const dispnames_2[] = { "4UP-1", "4UP-2" };
 390        static const char * const dispnames_5[] = {
 391                "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP"
 392        };
 393        const char * const *dispnames;
 394
 395        if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext))
 396                return -EINVAL;
 397
 398        if (solo_dev->nr_ext == 5)
 399                dispnames = dispnames_5;
 400        else if (solo_dev->nr_ext == 2)
 401                dispnames = dispnames_2;
 402        else
 403                dispnames = dispnames_1;
 404
 405        snprintf(input->name, sizeof(input->name), "Multi %s",
 406                 dispnames[input->index - solo_dev->nr_chans]);
 407
 408        return 0;
 409}
 410
 411static int solo_enum_input(struct file *file, void *priv,
 412                           struct v4l2_input *input)
 413{
 414        struct solo_dev *solo_dev = video_drvdata(file);
 415
 416        if (input->index >= solo_dev->nr_chans) {
 417                int ret = solo_enum_ext_input(solo_dev, input);
 418                if (ret < 0)
 419                        return ret;
 420        } else {
 421                snprintf(input->name, sizeof(input->name), "Camera %d",
 422                         input->index + 1);
 423
 424                /* We can only check this for normal inputs */
 425                if (!tw28_get_video_status(solo_dev, input->index))
 426                        input->status = V4L2_IN_ST_NO_SIGNAL;
 427        }
 428
 429        input->type = V4L2_INPUT_TYPE_CAMERA;
 430        input->std = solo_dev->vfd->tvnorms;
 431        return 0;
 432}
 433
 434static int solo_set_input(struct file *file, void *priv, unsigned int index)
 435{
 436        struct solo_dev *solo_dev = video_drvdata(file);
 437        int ret = solo_v4l2_set_ch(solo_dev, index);
 438
 439        if (!ret) {
 440                while (erase_off(solo_dev))
 441                        /* Do nothing */;
 442        }
 443
 444        return ret;
 445}
 446
 447static int solo_get_input(struct file *file, void *priv, unsigned int *index)
 448{
 449        struct solo_dev *solo_dev = video_drvdata(file);
 450
 451        *index = solo_dev->cur_disp_ch;
 452
 453        return 0;
 454}
 455
 456static int solo_enum_fmt_cap(struct file *file, void *priv,
 457                             struct v4l2_fmtdesc *f)
 458{
 459        if (f->index)
 460                return -EINVAL;
 461
 462        f->pixelformat = V4L2_PIX_FMT_UYVY;
 463        strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
 464
 465        return 0;
 466}
 467
 468static int solo_try_fmt_cap(struct file *file, void *priv,
 469                            struct v4l2_format *f)
 470{
 471        struct solo_dev *solo_dev = video_drvdata(file);
 472        struct v4l2_pix_format *pix = &f->fmt.pix;
 473        int image_size = solo_image_size(solo_dev);
 474
 475        if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
 476                return -EINVAL;
 477
 478        pix->width = solo_dev->video_hsize;
 479        pix->height = solo_vlines(solo_dev);
 480        pix->sizeimage = image_size;
 481        pix->field = V4L2_FIELD_INTERLACED;
 482        pix->pixelformat = V4L2_PIX_FMT_UYVY;
 483        pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
 484        pix->priv = 0;
 485        return 0;
 486}
 487
 488static int solo_set_fmt_cap(struct file *file, void *priv,
 489                            struct v4l2_format *f)
 490{
 491        struct solo_dev *solo_dev = video_drvdata(file);
 492
 493        if (vb2_is_busy(&solo_dev->vidq))
 494                return -EBUSY;
 495
 496        /* For right now, if it doesn't match our running config,
 497         * then fail */
 498        return solo_try_fmt_cap(file, priv, f);
 499}
 500
 501static int solo_get_fmt_cap(struct file *file, void *priv,
 502                            struct v4l2_format *f)
 503{
 504        struct solo_dev *solo_dev = video_drvdata(file);
 505        struct v4l2_pix_format *pix = &f->fmt.pix;
 506
 507        pix->width = solo_dev->video_hsize;
 508        pix->height = solo_vlines(solo_dev);
 509        pix->pixelformat = V4L2_PIX_FMT_UYVY;
 510        pix->field = V4L2_FIELD_INTERLACED;
 511        pix->sizeimage = solo_image_size(solo_dev);
 512        pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
 513        pix->bytesperline = solo_bytesperline(solo_dev);
 514        pix->priv = 0;
 515
 516        return 0;
 517}
 518
 519static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
 520{
 521        struct solo_dev *solo_dev = video_drvdata(file);
 522
 523        if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
 524                *i = V4L2_STD_NTSC_M;
 525        else
 526                *i = V4L2_STD_PAL;
 527        return 0;
 528}
 529
 530int solo_set_video_type(struct solo_dev *solo_dev, bool type)
 531{
 532        int i;
 533
 534        /* Make sure all video nodes are idle */
 535        if (vb2_is_busy(&solo_dev->vidq))
 536                return -EBUSY;
 537        for (i = 0; i < solo_dev->nr_chans; i++)
 538                if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
 539                        return -EBUSY;
 540        solo_dev->video_type = type;
 541        /* Reconfigure for the new standard */
 542        solo_disp_init(solo_dev);
 543        solo_enc_init(solo_dev);
 544        solo_tw28_init(solo_dev);
 545        for (i = 0; i < solo_dev->nr_chans; i++)
 546                solo_update_mode(solo_dev->v4l2_enc[i]);
 547        return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
 548}
 549
 550static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
 551{
 552        struct solo_dev *solo_dev = video_drvdata(file);
 553
 554        return solo_set_video_type(solo_dev, std & V4L2_STD_PAL);
 555}
 556
 557static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
 558{
 559        struct solo_dev *solo_dev =
 560                container_of(ctrl->handler, struct solo_dev, disp_hdl);
 561
 562        switch (ctrl->id) {
 563        case V4L2_CID_MOTION_TRACE:
 564                if (ctrl->val) {
 565                        solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
 566                                        SOLO_VI_MOTION_Y_ADD |
 567                                        SOLO_VI_MOTION_Y_VALUE(0x20) |
 568                                        SOLO_VI_MOTION_CB_VALUE(0x10) |
 569                                        SOLO_VI_MOTION_CR_VALUE(0x10));
 570                        solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
 571                                        SOLO_VI_MOTION_CR_ADD |
 572                                        SOLO_VI_MOTION_Y_VALUE(0x10) |
 573                                        SOLO_VI_MOTION_CB_VALUE(0x80) |
 574                                        SOLO_VI_MOTION_CR_VALUE(0x10));
 575                } else {
 576                        solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
 577                        solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
 578                }
 579                return 0;
 580        default:
 581                break;
 582        }
 583        return -EINVAL;
 584}
 585
 586static const struct v4l2_file_operations solo_v4l2_fops = {
 587        .owner                  = THIS_MODULE,
 588        .open                   = v4l2_fh_open,
 589        .release                = vb2_fop_release,
 590        .read                   = vb2_fop_read,
 591        .poll                   = vb2_fop_poll,
 592        .mmap                   = vb2_fop_mmap,
 593        .unlocked_ioctl         = video_ioctl2,
 594};
 595
 596static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
 597        .vidioc_querycap                = solo_querycap,
 598        .vidioc_s_std                   = solo_s_std,
 599        .vidioc_g_std                   = solo_g_std,
 600        /* Input callbacks */
 601        .vidioc_enum_input              = solo_enum_input,
 602        .vidioc_s_input                 = solo_set_input,
 603        .vidioc_g_input                 = solo_get_input,
 604        /* Video capture format callbacks */
 605        .vidioc_enum_fmt_vid_cap        = solo_enum_fmt_cap,
 606        .vidioc_try_fmt_vid_cap         = solo_try_fmt_cap,
 607        .vidioc_s_fmt_vid_cap           = solo_set_fmt_cap,
 608        .vidioc_g_fmt_vid_cap           = solo_get_fmt_cap,
 609        /* Streaming I/O */
 610        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
 611        .vidioc_querybuf                = vb2_ioctl_querybuf,
 612        .vidioc_qbuf                    = vb2_ioctl_qbuf,
 613        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
 614        .vidioc_streamon                = vb2_ioctl_streamon,
 615        .vidioc_streamoff               = vb2_ioctl_streamoff,
 616        /* Logging and events */
 617        .vidioc_log_status              = v4l2_ctrl_log_status,
 618        .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
 619        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 620};
 621
 622static struct video_device solo_v4l2_template = {
 623        .name                   = SOLO6X10_NAME,
 624        .fops                   = &solo_v4l2_fops,
 625        .ioctl_ops              = &solo_v4l2_ioctl_ops,
 626        .minor                  = -1,
 627        .release                = video_device_release,
 628        .tvnorms                = V4L2_STD_NTSC_M | V4L2_STD_PAL,
 629};
 630
 631static const struct v4l2_ctrl_ops solo_ctrl_ops = {
 632        .s_ctrl = solo_s_ctrl,
 633};
 634
 635static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
 636        .ops = &solo_ctrl_ops,
 637        .id = V4L2_CID_MOTION_TRACE,
 638        .name = "Motion Detection Trace",
 639        .type = V4L2_CTRL_TYPE_BOOLEAN,
 640        .max = 1,
 641        .step = 1,
 642};
 643
 644int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
 645{
 646        int ret;
 647        int i;
 648
 649        init_waitqueue_head(&solo_dev->disp_thread_wait);
 650        spin_lock_init(&solo_dev->slock);
 651        mutex_init(&solo_dev->lock);
 652        INIT_LIST_HEAD(&solo_dev->vidq_active);
 653
 654        solo_dev->vfd = video_device_alloc();
 655        if (!solo_dev->vfd)
 656                return -ENOMEM;
 657
 658        *solo_dev->vfd = solo_v4l2_template;
 659        solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
 660        solo_dev->vfd->queue = &solo_dev->vidq;
 661        solo_dev->vfd->lock = &solo_dev->lock;
 662        v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
 663        v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
 664        if (solo_dev->disp_hdl.error) {
 665                ret = solo_dev->disp_hdl.error;
 666                goto fail;
 667        }
 668        solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
 669        set_bit(V4L2_FL_USE_FH_PRIO, &solo_dev->vfd->flags);
 670
 671        video_set_drvdata(solo_dev->vfd, solo_dev);
 672
 673        solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 674        solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
 675        solo_dev->vidq.ops = &solo_video_qops;
 676        solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
 677        solo_dev->vidq.drv_priv = solo_dev;
 678        solo_dev->vidq.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 679        solo_dev->vidq.gfp_flags = __GFP_DMA32;
 680        solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
 681        solo_dev->vidq.lock = &solo_dev->lock;
 682        ret = vb2_queue_init(&solo_dev->vidq);
 683        if (ret < 0)
 684                goto fail;
 685
 686        solo_dev->alloc_ctx = vb2_dma_contig_init_ctx(&solo_dev->pdev->dev);
 687        if (IS_ERR(solo_dev->alloc_ctx)) {
 688                dev_err(&solo_dev->pdev->dev, "Can't allocate buffer context");
 689                return PTR_ERR(solo_dev->alloc_ctx);
 690        }
 691
 692        /* Cycle all the channels and clear */
 693        for (i = 0; i < solo_dev->nr_chans; i++) {
 694                solo_v4l2_set_ch(solo_dev, i);
 695                while (erase_off(solo_dev))
 696                        /* Do nothing */;
 697        }
 698
 699        /* Set the default display channel */
 700        solo_v4l2_set_ch(solo_dev, 0);
 701        while (erase_off(solo_dev))
 702                /* Do nothing */;
 703
 704        ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
 705        if (ret < 0)
 706                goto fail;
 707
 708        snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
 709                 SOLO6X10_NAME, solo_dev->vfd->num);
 710
 711        dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with "
 712                 "%d inputs (%d extended)\n", solo_dev->vfd->num,
 713                 solo_dev->nr_chans, solo_dev->nr_ext);
 714
 715        return 0;
 716
 717fail:
 718        video_device_release(solo_dev->vfd);
 719        vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
 720        v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
 721        solo_dev->vfd = NULL;
 722        return ret;
 723}
 724
 725void solo_v4l2_exit(struct solo_dev *solo_dev)
 726{
 727        if (solo_dev->vfd == NULL)
 728                return;
 729
 730        video_unregister_device(solo_dev->vfd);
 731        vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
 732        v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
 733        solo_dev->vfd = NULL;
 734}
 735