linux/drivers/media/pci/saa7134/saa7134-empress.c
<<
>>
Prefs
   1/*
   2 *
   3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
   4 *
   5 *  This program is free software; you can redistribute it and/or modify
   6 *  it under the terms of the GNU General Public License as published by
   7 *  the Free Software Foundation; either version 2 of the License, or
   8 *  (at your option) any later version.
   9 *
  10 *  This program is distributed in the hope that it will be useful,
  11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 *  GNU General Public License for more details.
  14 */
  15
  16#include "saa7134.h"
  17#include "saa7134-reg.h"
  18
  19#include <linux/init.h>
  20#include <linux/list.h>
  21#include <linux/module.h>
  22#include <linux/kernel.h>
  23#include <linux/delay.h>
  24
  25#include <media/v4l2-common.h>
  26#include <media/v4l2-event.h>
  27
  28/* ------------------------------------------------------------------ */
  29
  30MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  31MODULE_LICENSE("GPL");
  32
  33static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  34
  35module_param_array(empress_nr, int, NULL, 0444);
  36MODULE_PARM_DESC(empress_nr,"ts device number");
  37
  38/* ------------------------------------------------------------------ */
  39
  40static int start_streaming(struct vb2_queue *vq, unsigned int count)
  41{
  42        struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  43        struct saa7134_dev *dev = dmaq->dev;
  44        u32 leading_null_bytes = 0;
  45        int err;
  46
  47        err = saa7134_ts_start_streaming(vq, count);
  48        if (err)
  49                return err;
  50
  51        /* If more cards start to need this, then this
  52           should probably be added to the card definitions. */
  53        switch (dev->board) {
  54        case SAA7134_BOARD_BEHOLD_M6:
  55        case SAA7134_BOARD_BEHOLD_M63:
  56        case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  57                leading_null_bytes = 1;
  58                break;
  59        }
  60        saa_call_all(dev, core, init, leading_null_bytes);
  61        /* Unmute audio */
  62        saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  63                        saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
  64        dev->empress_started = 1;
  65        return 0;
  66}
  67
  68static void stop_streaming(struct vb2_queue *vq)
  69{
  70        struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  71        struct saa7134_dev *dev = dmaq->dev;
  72
  73        saa7134_ts_stop_streaming(vq);
  74        saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
  75        msleep(20);
  76        saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
  77        msleep(100);
  78        /* Mute audio */
  79        saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  80                        saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
  81        dev->empress_started = 0;
  82}
  83
  84static const struct vb2_ops saa7134_empress_qops = {
  85        .queue_setup    = saa7134_ts_queue_setup,
  86        .buf_init       = saa7134_ts_buffer_init,
  87        .buf_prepare    = saa7134_ts_buffer_prepare,
  88        .buf_queue      = saa7134_vb2_buffer_queue,
  89        .wait_prepare   = vb2_ops_wait_prepare,
  90        .wait_finish    = vb2_ops_wait_finish,
  91        .start_streaming = start_streaming,
  92        .stop_streaming = stop_streaming,
  93};
  94
  95/* ------------------------------------------------------------------ */
  96
  97static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
  98                                        struct v4l2_fmtdesc *f)
  99{
 100        if (f->index != 0)
 101                return -EINVAL;
 102
 103        strlcpy(f->description, "MPEG TS", sizeof(f->description));
 104        f->pixelformat = V4L2_PIX_FMT_MPEG;
 105        f->flags = V4L2_FMT_FLAG_COMPRESSED;
 106        return 0;
 107}
 108
 109static int empress_g_fmt_vid_cap(struct file *file, void *priv,
 110                                struct v4l2_format *f)
 111{
 112        struct saa7134_dev *dev = video_drvdata(file);
 113        struct v4l2_subdev_format fmt = {
 114                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 115        };
 116        struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
 117
 118        saa_call_all(dev, pad, get_fmt, NULL, &fmt);
 119
 120        v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt);
 121        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
 122        f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
 123        f->fmt.pix.bytesperline = 0;
 124
 125        return 0;
 126}
 127
 128static int empress_s_fmt_vid_cap(struct file *file, void *priv,
 129                                struct v4l2_format *f)
 130{
 131        struct saa7134_dev *dev = video_drvdata(file);
 132        struct v4l2_subdev_format format = {
 133                .which = V4L2_SUBDEV_FORMAT_ACTIVE,
 134        };
 135
 136        v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
 137        saa_call_all(dev, pad, set_fmt, NULL, &format);
 138        v4l2_fill_pix_format(&f->fmt.pix, &format.format);
 139
 140        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
 141        f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
 142        f->fmt.pix.bytesperline = 0;
 143
 144        return 0;
 145}
 146
 147static int empress_try_fmt_vid_cap(struct file *file, void *priv,
 148                                struct v4l2_format *f)
 149{
 150        struct saa7134_dev *dev = video_drvdata(file);
 151        struct v4l2_subdev_pad_config pad_cfg;
 152        struct v4l2_subdev_format format = {
 153                .which = V4L2_SUBDEV_FORMAT_TRY,
 154        };
 155
 156        v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
 157        saa_call_all(dev, pad, set_fmt, &pad_cfg, &format);
 158        v4l2_fill_pix_format(&f->fmt.pix, &format.format);
 159
 160        f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
 161        f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
 162        f->fmt.pix.bytesperline = 0;
 163
 164        return 0;
 165}
 166
 167static const struct v4l2_file_operations ts_fops =
 168{
 169        .owner    = THIS_MODULE,
 170        .open     = v4l2_fh_open,
 171        .release  = vb2_fop_release,
 172        .read     = vb2_fop_read,
 173        .poll     = vb2_fop_poll,
 174        .mmap     = vb2_fop_mmap,
 175        .unlocked_ioctl = video_ioctl2,
 176};
 177
 178static const struct v4l2_ioctl_ops ts_ioctl_ops = {
 179        .vidioc_querycap                = saa7134_querycap,
 180        .vidioc_enum_fmt_vid_cap        = empress_enum_fmt_vid_cap,
 181        .vidioc_try_fmt_vid_cap         = empress_try_fmt_vid_cap,
 182        .vidioc_s_fmt_vid_cap           = empress_s_fmt_vid_cap,
 183        .vidioc_g_fmt_vid_cap           = empress_g_fmt_vid_cap,
 184        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
 185        .vidioc_querybuf                = vb2_ioctl_querybuf,
 186        .vidioc_qbuf                    = vb2_ioctl_qbuf,
 187        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
 188        .vidioc_expbuf                  = vb2_ioctl_expbuf,
 189        .vidioc_streamon                = vb2_ioctl_streamon,
 190        .vidioc_streamoff               = vb2_ioctl_streamoff,
 191        .vidioc_g_frequency             = saa7134_g_frequency,
 192        .vidioc_s_frequency             = saa7134_s_frequency,
 193        .vidioc_g_tuner                 = saa7134_g_tuner,
 194        .vidioc_s_tuner                 = saa7134_s_tuner,
 195        .vidioc_enum_input              = saa7134_enum_input,
 196        .vidioc_g_input                 = saa7134_g_input,
 197        .vidioc_s_input                 = saa7134_s_input,
 198        .vidioc_s_std                   = saa7134_s_std,
 199        .vidioc_g_std                   = saa7134_g_std,
 200        .vidioc_querystd                = saa7134_querystd,
 201        .vidioc_log_status              = v4l2_ctrl_log_status,
 202        .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
 203        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 204};
 205
 206/* ----------------------------------------------------------- */
 207
 208static const struct video_device saa7134_empress_template = {
 209        .name          = "saa7134-empress",
 210        .fops          = &ts_fops,
 211        .ioctl_ops     = &ts_ioctl_ops,
 212
 213        .tvnorms                        = SAA7134_NORMS,
 214};
 215
 216static void empress_signal_update(struct work_struct *work)
 217{
 218        struct saa7134_dev* dev =
 219                container_of(work, struct saa7134_dev, empress_workqueue);
 220
 221        if (dev->nosignal) {
 222                pr_debug("no video signal\n");
 223        } else {
 224                pr_debug("video signal acquired\n");
 225        }
 226}
 227
 228static void empress_signal_change(struct saa7134_dev *dev)
 229{
 230        schedule_work(&dev->empress_workqueue);
 231}
 232
 233static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
 234{
 235        switch (ctrl->id) {
 236        case V4L2_CID_BRIGHTNESS:
 237        case V4L2_CID_HUE:
 238        case V4L2_CID_CONTRAST:
 239        case V4L2_CID_SATURATION:
 240        case V4L2_CID_AUDIO_MUTE:
 241        case V4L2_CID_AUDIO_VOLUME:
 242        case V4L2_CID_PRIVATE_INVERT:
 243        case V4L2_CID_PRIVATE_AUTOMUTE:
 244                return true;
 245        default:
 246                return false;
 247        }
 248}
 249
 250static int empress_init(struct saa7134_dev *dev)
 251{
 252        struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
 253        struct vb2_queue *q;
 254        int err;
 255
 256        pr_debug("%s: %s\n", dev->name, __func__);
 257        dev->empress_dev = video_device_alloc();
 258        if (NULL == dev->empress_dev)
 259                return -ENOMEM;
 260        *(dev->empress_dev) = saa7134_empress_template;
 261        dev->empress_dev->v4l2_dev  = &dev->v4l2_dev;
 262        dev->empress_dev->release = video_device_release;
 263        dev->empress_dev->lock = &dev->lock;
 264        snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
 265                 "%s empress (%s)", dev->name,
 266                 saa7134_boards[dev->board].name);
 267        v4l2_ctrl_handler_init(hdl, 21);
 268        v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
 269        if (dev->empress_sd)
 270                v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
 271        if (hdl->error) {
 272                video_device_release(dev->empress_dev);
 273                return hdl->error;
 274        }
 275        dev->empress_dev->ctrl_handler = hdl;
 276
 277        INIT_WORK(&dev->empress_workqueue, empress_signal_update);
 278
 279        q = &dev->empress_vbq;
 280        q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 281        /*
 282         * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
 283         * transfers that do not start at the beginning of a page. A USERPTR
 284         * can start anywhere in a page, so USERPTR support is a no-go.
 285         */
 286        q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
 287        q->drv_priv = &dev->ts_q;
 288        q->ops = &saa7134_empress_qops;
 289        q->gfp_flags = GFP_DMA32;
 290        q->mem_ops = &vb2_dma_sg_memops;
 291        q->buf_struct_size = sizeof(struct saa7134_buf);
 292        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 293        q->lock = &dev->lock;
 294        q->dev = &dev->pci->dev;
 295        err = vb2_queue_init(q);
 296        if (err)
 297                return err;
 298        dev->empress_dev->queue = q;
 299
 300        video_set_drvdata(dev->empress_dev, dev);
 301        err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
 302                                    empress_nr[dev->nr]);
 303        if (err < 0) {
 304                pr_info("%s: can't register video device\n",
 305                       dev->name);
 306                video_device_release(dev->empress_dev);
 307                dev->empress_dev = NULL;
 308                return err;
 309        }
 310        pr_info("%s: registered device %s [mpeg]\n",
 311               dev->name, video_device_node_name(dev->empress_dev));
 312
 313        empress_signal_update(&dev->empress_workqueue);
 314        return 0;
 315}
 316
 317static int empress_fini(struct saa7134_dev *dev)
 318{
 319        pr_debug("%s: %s\n", dev->name, __func__);
 320
 321        if (NULL == dev->empress_dev)
 322                return 0;
 323        flush_work(&dev->empress_workqueue);
 324        video_unregister_device(dev->empress_dev);
 325        vb2_queue_release(&dev->empress_vbq);
 326        v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
 327        dev->empress_dev = NULL;
 328        return 0;
 329}
 330
 331static struct saa7134_mpeg_ops empress_ops = {
 332        .type          = SAA7134_MPEG_EMPRESS,
 333        .init          = empress_init,
 334        .fini          = empress_fini,
 335        .signal_change = empress_signal_change,
 336};
 337
 338static int __init empress_register(void)
 339{
 340        return saa7134_ts_register(&empress_ops);
 341}
 342
 343static void __exit empress_unregister(void)
 344{
 345        saa7134_ts_unregister(&empress_ops);
 346}
 347
 348module_init(empress_register);
 349module_exit(empress_unregister);
 350