linux/drivers/staging/cx25821/cx25821-video5.c
<<
>>
Prefs
   1/*
   2 *  Driver for the Conexant CX25821 PCIe bridge
   3 *
   4 *  Copyright (C) 2009 Conexant Systems Inc.
   5 *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
   6 *  Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
   7 *
   8 *  This program is free software; you can redistribute it and/or modify
   9 *  it under the terms of the GNU General Public License as published by
  10 *  the Free Software Foundation; either version 2 of the License, or
  11 *  (at your option) any later version.
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *
  17 *  GNU General Public License for more details.
  18 *
  19 *  You should have received a copy of the GNU General Public License
  20 *  along with this program; if not, write to the Free Software
  21 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22 */
  23
  24#include "cx25821-video.h"
  25
  26static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  27{
  28        struct cx25821_buffer *buf =
  29            container_of(vb, struct cx25821_buffer, vb);
  30        struct cx25821_buffer *prev;
  31        struct cx25821_fh *fh = vq->priv_data;
  32        struct cx25821_dev *dev = fh->dev;
  33        struct cx25821_dmaqueue *q = &dev->vidq[SRAM_CH05];
  34
  35        /* add jump to stopper */
  36        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  37        buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  38        buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
  39
  40        dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
  41
  42        if (!list_empty(&q->queued)) {
  43                list_add_tail(&buf->vb.queue, &q->queued);
  44                buf->vb.state = VIDEOBUF_QUEUED;
  45                dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
  46                        buf->vb.i);
  47
  48        } else if (list_empty(&q->active)) {
  49                list_add_tail(&buf->vb.queue, &q->active);
  50                cx25821_start_video_dma(dev, q, buf,
  51                                        &dev->sram_channels[SRAM_CH05]);
  52                buf->vb.state = VIDEOBUF_ACTIVE;
  53                buf->count = q->count++;
  54                mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
  55                dprintk(2,
  56                        "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
  57                        buf, buf->vb.i, buf->count, q->count);
  58        } else {
  59                prev =
  60                    list_entry(q->active.prev, struct cx25821_buffer, vb.queue);
  61                if (prev->vb.width == buf->vb.width
  62                    && prev->vb.height == buf->vb.height
  63                    && prev->fmt == buf->fmt) {
  64                        list_add_tail(&buf->vb.queue, &q->active);
  65                        buf->vb.state = VIDEOBUF_ACTIVE;
  66                        buf->count = q->count++;
  67                        prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  68
  69                        /* 64 bit bits 63-32 */
  70                        prev->risc.jmp[2] = cpu_to_le32(0);
  71                        dprintk(2,
  72                                "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
  73                                buf, buf->vb.i, buf->count);
  74
  75                } else {
  76                        list_add_tail(&buf->vb.queue, &q->queued);
  77                        buf->vb.state = VIDEOBUF_QUEUED;
  78                        dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
  79                                buf->vb.i);
  80                }
  81        }
  82
  83        if (list_empty(&q->active)) {
  84                dprintk(2, "active queue empty!\n");
  85        }
  86}
  87
  88static struct videobuf_queue_ops cx25821_video_qops = {
  89        .buf_setup = buffer_setup,
  90        .buf_prepare = buffer_prepare,
  91        .buf_queue = buffer_queue,
  92        .buf_release = buffer_release,
  93};
  94
  95static int video_open(struct file *file)
  96{
  97        int minor = video_devdata(file)->minor;
  98        struct cx25821_dev *h, *dev = NULL;
  99        struct cx25821_fh *fh;
 100        struct list_head *list;
 101        enum v4l2_buf_type type = 0;
 102        u32 pix_format;
 103
 104        lock_kernel();
 105        list_for_each(list, &cx25821_devlist) {
 106                h = list_entry(list, struct cx25821_dev, devlist);
 107
 108                if (h->video_dev[SRAM_CH05]
 109                    && h->video_dev[SRAM_CH05]->minor == minor) {
 110                        dev = h;
 111                        type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 112                }
 113        }
 114
 115        if (NULL == dev) {
 116                unlock_kernel();
 117                return -ENODEV;
 118        }
 119
 120        printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
 121
 122        /* allocate + initialize per filehandle data */
 123        fh = kzalloc(sizeof(*fh), GFP_KERNEL);
 124        if (NULL == fh) {
 125                unlock_kernel();
 126                return -ENOMEM;
 127        }
 128        file->private_data = fh;
 129        fh->dev = dev;
 130        fh->type = type;
 131        fh->width = 720;
 132
 133        if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
 134                fh->height = 576;
 135        else
 136                fh->height = 480;
 137
 138        dev->channel_opened = SRAM_CH05;
 139        pix_format =
 140            (dev->pixel_formats[dev->channel_opened] ==
 141             PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
 142        fh->fmt = format_by_fourcc(pix_format);
 143
 144        v4l2_prio_open(&dev->prio, &fh->prio);
 145
 146        videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
 147                               &dev->pci->dev, &dev->slock,
 148                               V4L2_BUF_TYPE_VIDEO_CAPTURE,
 149                               V4L2_FIELD_INTERLACED,
 150                               sizeof(struct cx25821_buffer), fh);
 151
 152        dprintk(1, "post videobuf_queue_init()\n");
 153        unlock_kernel();
 154
 155        return 0;
 156}
 157
 158static ssize_t video_read(struct file *file, char __user * data, size_t count,
 159                          loff_t * ppos)
 160{
 161        struct cx25821_fh *fh = file->private_data;
 162
 163        switch (fh->type) {
 164        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 165                if (res_locked(fh->dev, RESOURCE_VIDEO5))
 166                        return -EBUSY;
 167
 168                return videobuf_read_one(&fh->vidq, data, count, ppos,
 169                                         file->f_flags & O_NONBLOCK);
 170
 171        default:
 172                BUG();
 173                return 0;
 174        }
 175}
 176
 177static unsigned int video_poll(struct file *file,
 178                               struct poll_table_struct *wait)
 179{
 180        struct cx25821_fh *fh = file->private_data;
 181        struct cx25821_buffer *buf;
 182
 183        if (res_check(fh, RESOURCE_VIDEO5)) {
 184                /* streaming capture */
 185                if (list_empty(&fh->vidq.stream))
 186                        return POLLERR;
 187                buf = list_entry(fh->vidq.stream.next,
 188                                 struct cx25821_buffer, vb.stream);
 189        } else {
 190                /* read() capture */
 191                buf = (struct cx25821_buffer *)fh->vidq.read_buf;
 192                if (NULL == buf)
 193                        return POLLERR;
 194        }
 195
 196        poll_wait(file, &buf->vb.done, wait);
 197        if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
 198                if (buf->vb.state == VIDEOBUF_DONE) {
 199                        struct cx25821_dev *dev = fh->dev;
 200
 201                        if (dev && dev->use_cif_resolution[SRAM_CH05]) {
 202                                u8 cam_id = *((char *)buf->vb.baddr + 3);
 203                                memcpy((char *)buf->vb.baddr,
 204                                       (char *)buf->vb.baddr + (fh->width * 2),
 205                                       (fh->width * 2));
 206                                *((char *)buf->vb.baddr + 3) = cam_id;
 207                        }
 208                }
 209
 210                return POLLIN | POLLRDNORM;
 211        }
 212
 213        return 0;
 214}
 215
 216static int video_release(struct file *file)
 217{
 218        struct cx25821_fh *fh = file->private_data;
 219        struct cx25821_dev *dev = fh->dev;
 220
 221        //stop the risc engine and fifo
 222        cx_write(channel5->dma_ctl, 0); /* FIFO and RISC disable */
 223
 224        /* stop video capture */
 225        if (res_check(fh, RESOURCE_VIDEO5)) {
 226                videobuf_queue_cancel(&fh->vidq);
 227                res_free(dev, fh, RESOURCE_VIDEO5);
 228        }
 229
 230        if (fh->vidq.read_buf) {
 231                buffer_release(&fh->vidq, fh->vidq.read_buf);
 232                kfree(fh->vidq.read_buf);
 233        }
 234
 235        videobuf_mmap_free(&fh->vidq);
 236
 237        v4l2_prio_close(&dev->prio, &fh->prio);
 238        file->private_data = NULL;
 239        kfree(fh);
 240
 241        return 0;
 242}
 243
 244static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 245{
 246        struct cx25821_fh *fh = priv;
 247        struct cx25821_dev *dev = fh->dev;
 248
 249        if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
 250                return -EINVAL;
 251        }
 252
 253        if (unlikely(i != fh->type)) {
 254                return -EINVAL;
 255        }
 256
 257        if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO5)))) {
 258                return -EBUSY;
 259        }
 260
 261        return videobuf_streamon(get_queue(fh));
 262}
 263
 264static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
 265{
 266        struct cx25821_fh *fh = priv;
 267        struct cx25821_dev *dev = fh->dev;
 268        int err, res;
 269
 270        if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 271                return -EINVAL;
 272        if (i != fh->type)
 273                return -EINVAL;
 274
 275        res = get_resource(fh, RESOURCE_VIDEO5);
 276        err = videobuf_streamoff(get_queue(fh));
 277        if (err < 0)
 278                return err;
 279        res_free(dev, fh, res);
 280        return 0;
 281}
 282
 283static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 284                                struct v4l2_format *f)
 285{
 286        struct cx25821_fh *fh = priv;
 287        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
 288        int err;
 289        int pix_format = 0;
 290
 291        if (fh) {
 292                err = v4l2_prio_check(&dev->prio, &fh->prio);
 293                if (0 != err)
 294                        return err;
 295        }
 296
 297        dprintk(2, "%s()\n", __func__);
 298        err = vidioc_try_fmt_vid_cap(file, priv, f);
 299
 300        if (0 != err)
 301                return err;
 302
 303        fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
 304        fh->vidq.field = f->fmt.pix.field;
 305
 306        // check if width and height is valid based on set standard
 307        if (is_valid_width(f->fmt.pix.width, dev->tvnorm)) {
 308                fh->width = f->fmt.pix.width;
 309        }
 310
 311        if (is_valid_height(f->fmt.pix.height, dev->tvnorm)) {
 312                fh->height = f->fmt.pix.height;
 313        }
 314
 315        if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
 316                pix_format = PIXEL_FRMT_411;
 317        else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
 318                pix_format = PIXEL_FRMT_422;
 319        else
 320                return -EINVAL;
 321
 322        cx25821_set_pixel_format(dev, SRAM_CH05, pix_format);
 323
 324        // check if cif resolution
 325        if (fh->width == 320 || fh->width == 352) {
 326                dev->use_cif_resolution[SRAM_CH05] = 1;
 327        } else {
 328                dev->use_cif_resolution[SRAM_CH05] = 0;
 329        }
 330        dev->cif_width[SRAM_CH05] = fh->width;
 331        medusa_set_resolution(dev, fh->width, SRAM_CH05);
 332
 333        dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
 334                fh->height, fh->vidq.field);
 335        cx25821_call_all(dev, video, s_fmt, f);
 336
 337        return 0;
 338}
 339
 340static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
 341{
 342        int ret_val = 0;
 343        struct cx25821_fh *fh = priv;
 344        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
 345
 346        ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
 347
 348        p->sequence = dev->vidq[SRAM_CH05].count;
 349
 350        return ret_val;
 351}
 352static int vidioc_log_status(struct file *file, void *priv)
 353{
 354        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
 355        char name[32 + 2];
 356
 357        struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH05];
 358        u32 tmp = 0;
 359
 360        snprintf(name, sizeof(name), "%s/2", dev->name);
 361        printk(KERN_INFO "%s/2: ============  START LOG STATUS  ============\n",
 362               dev->name);
 363        cx25821_call_all(dev, core, log_status);
 364
 365        tmp = cx_read(sram_ch->dma_ctl);
 366        printk(KERN_INFO "Video input 5 is %s\n",
 367               (tmp & 0x11) ? "streaming" : "stopped");
 368        printk(KERN_INFO "%s/2: =============  END LOG STATUS  =============\n",
 369               dev->name);
 370        return 0;
 371}
 372
 373static int vidioc_s_ctrl(struct file *file, void *priv,
 374                         struct v4l2_control *ctl)
 375{
 376        struct cx25821_fh *fh = priv;
 377        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
 378        int err;
 379
 380        if (fh) {
 381                err = v4l2_prio_check(&dev->prio, &fh->prio);
 382                if (0 != err)
 383                        return err;
 384        }
 385
 386        return cx25821_set_control(dev, ctl, SRAM_CH05);
 387}
 388
 389// exported stuff
 390static const struct v4l2_file_operations video_fops = {
 391        .owner = THIS_MODULE,
 392        .open = video_open,
 393        .release = video_release,
 394        .read = video_read,
 395        .poll = video_poll,
 396        .mmap = video_mmap,
 397        .ioctl = video_ioctl2,
 398};
 399
 400static const struct v4l2_ioctl_ops video_ioctl_ops = {
 401        .vidioc_querycap = vidioc_querycap,
 402        .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
 403        .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
 404        .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
 405        .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
 406        .vidioc_reqbufs = vidioc_reqbufs,
 407        .vidioc_querybuf = vidioc_querybuf,
 408        .vidioc_qbuf = vidioc_qbuf,
 409        .vidioc_dqbuf = vidioc_dqbuf,
 410#ifdef TUNER_FLAG
 411        .vidioc_s_std = vidioc_s_std,
 412        .vidioc_querystd = vidioc_querystd,
 413#endif
 414        .vidioc_cropcap = vidioc_cropcap,
 415        .vidioc_s_crop = vidioc_s_crop,
 416        .vidioc_g_crop = vidioc_g_crop,
 417        .vidioc_enum_input = vidioc_enum_input,
 418        .vidioc_g_input = vidioc_g_input,
 419        .vidioc_s_input = vidioc_s_input,
 420        .vidioc_g_ctrl = vidioc_g_ctrl,
 421        .vidioc_s_ctrl = vidioc_s_ctrl,
 422        .vidioc_queryctrl = vidioc_queryctrl,
 423        .vidioc_streamon = vidioc_streamon,
 424        .vidioc_streamoff = vidioc_streamoff,
 425        .vidioc_log_status = vidioc_log_status,
 426        .vidioc_g_priority = vidioc_g_priority,
 427        .vidioc_s_priority = vidioc_s_priority,
 428#ifdef CONFIG_VIDEO_V4L1_COMPAT
 429        .vidiocgmbuf = vidiocgmbuf,
 430#endif
 431#ifdef TUNER_FLAG
 432        .vidioc_g_tuner = vidioc_g_tuner,
 433        .vidioc_s_tuner = vidioc_s_tuner,
 434        .vidioc_g_frequency = vidioc_g_frequency,
 435        .vidioc_s_frequency = vidioc_s_frequency,
 436#endif
 437#ifdef CONFIG_VIDEO_ADV_DEBUG
 438        .vidioc_g_register = vidioc_g_register,
 439        .vidioc_s_register = vidioc_s_register,
 440#endif
 441};
 442
 443struct video_device cx25821_video_template5 = {
 444        .name = "cx25821-video",
 445        .fops = &video_fops,
 446        .minor = -1,
 447        .ioctl_ops = &video_ioctl_ops,
 448        .tvnorms = CX25821_NORMS,
 449        .current_norm = V4L2_STD_NTSC_M,
 450};
 451