These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / media / usb / stk1160 / stk1160-v4l.c
index 749ad56..0bd34f1 100644 (file)
@@ -42,6 +42,17 @@ static bool keep_buffers;
 module_param(keep_buffers, bool, 0644);
 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming");
 
+enum stk1160_decimate_mode {
+       STK1160_DECIMATE_MORE_THAN_HALF,
+       STK1160_DECIMATE_LESS_THAN_HALF,
+};
+
+struct stk1160_decimate_ctrl {
+       bool col_en, row_en;
+       enum stk1160_decimate_mode col_mode, row_mode;
+       unsigned int col_n, row_n;
+};
+
 /* supported video standards */
 static struct stk1160_fmt format[] = {
        {
@@ -51,6 +62,19 @@ static struct stk1160_fmt format[] = {
        }
 };
 
+/*
+ * Helper to find the next divisor that results in modulo being zero.
+ * This is required to guarantee valid decimation unit counts.
+ */
+static unsigned int
+div_round_integer(unsigned int x, unsigned int y)
+{
+       for (;; y++) {
+               if (x % y == 0)
+                       return x / y;
+       }
+}
+
 static void stk1160_set_std(struct stk1160 *dev)
 {
        int i;
@@ -106,6 +130,41 @@ static void stk1160_set_std(struct stk1160 *dev)
 
 }
 
+static void stk1160_set_fmt(struct stk1160 *dev,
+                           struct stk1160_decimate_ctrl *ctrl)
+{
+       u32 val = 0;
+
+       if (ctrl) {
+               /*
+                * Since the format is UYVY, the device must skip or send
+                * a number of rows/columns multiple of four. This way, the
+                * colour format is preserved. The STK1160_DEC_UNIT_SIZE bit
+                * does exactly this.
+                */
+               val |= STK1160_DEC_UNIT_SIZE;
+               val |= ctrl->col_en ? STK1160_H_DEC_EN : 0;
+               val |= ctrl->row_en ? STK1160_V_DEC_EN : 0;
+               val |= ctrl->col_mode ==
+                       STK1160_DECIMATE_MORE_THAN_HALF ?
+                       STK1160_H_DEC_MODE : 0;
+               val |= ctrl->row_mode ==
+                       STK1160_DECIMATE_MORE_THAN_HALF ?
+                       STK1160_V_DEC_MODE : 0;
+
+               /* Horizontal count units */
+               stk1160_write_reg(dev, STK1160_DMCTRL_H_UNITS, ctrl->col_n);
+               /* Vertical count units */
+               stk1160_write_reg(dev, STK1160_DMCTRL_V_UNITS, ctrl->row_n);
+
+               stk1160_dbg("decimate 0x%x, column units %d, row units %d\n",
+                           val, ctrl->col_n, ctrl->row_n);
+       }
+
+       /* Decimation control */
+       stk1160_write_reg(dev, STK1160_DMCTRL, val);
+}
+
 /*
  * Set a new alternate setting.
  * Returns true is dev->max_pkt_size has changed, false otherwise.
@@ -136,7 +195,7 @@ static bool stk1160_set_alternate(struct stk1160 *dev)
                        dev->alt = i;
        }
 
-       stk1160_info("setting alternate %d\n", dev->alt);
+       stk1160_dbg("setting alternate %d\n", dev->alt);
 
        if (dev->alt != prev_alt) {
                stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n",
@@ -194,6 +253,8 @@ static int stk1160_start_streaming(struct stk1160 *dev)
        /* Start saa711x */
        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
 
+       dev->sequence = 0;
+
        /* Start stk1160 */
        stk1160_write_reg(dev, STK1160_DCTRL, 0xb3);
        stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00);
@@ -224,7 +285,7 @@ static void stk1160_stop_hw(struct stk1160 *dev)
 
        /* set alternate 0 */
        dev->alt = 0;
-       stk1160_info("setting alternate %d\n", dev->alt);
+       stk1160_dbg("setting alternate %d\n", dev->alt);
        usb_set_interface(dev->udev, 0, 0);
 
        /* Stop stk1160 */
@@ -321,41 +382,134 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
        return 0;
 }
 
-static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
-                       struct v4l2_format *f)
+static int stk1160_try_fmt(struct stk1160 *dev, struct v4l2_format *f,
+                           struct stk1160_decimate_ctrl *ctrl)
 {
-       struct stk1160 *dev = video_drvdata(file);
+       unsigned int width, height;
+       unsigned int base_width, base_height;
+       unsigned int col_n, row_n;
+       enum stk1160_decimate_mode col_mode, row_mode;
+       bool col_en, row_en;
+
+       base_width = 720;
+       base_height = (dev->norm & V4L2_STD_525_60) ? 480 : 576;
+
+       /* Minimum width and height is 5% the frame size */
+       width = clamp_t(unsigned int, f->fmt.pix.width,
+                       base_width / 20, base_width);
+       height = clamp_t(unsigned int, f->fmt.pix.height,
+                       base_height / 20, base_height);
+
+       /* Let's set default no decimation values */
+       col_n = 0;
+       row_n = 0;
+       col_en = false;
+       row_en = false;
+       f->fmt.pix.width = base_width;
+       f->fmt.pix.height = base_height;
+       row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
+       col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
+
+       if (width < base_width && width > base_width / 2) {
+               /*
+                * The device will send count units for each
+                * unit skipped. This means count unit is:
+                *
+                * n = width / (frame width - width)
+                *
+                * And the width is:
+                *
+                * width = (n / n + 1) * frame width
+                */
+               col_n = div_round_integer(width, base_width - width);
+               if (col_n > 0 && col_n <= 255) {
+                       col_en = true;
+                       col_mode = STK1160_DECIMATE_LESS_THAN_HALF;
+                       f->fmt.pix.width = (base_width * col_n) / (col_n + 1);
+               }
 
-       /*
-        * User can't choose size at his own will,
-        * so we just return him the current size chosen
-        * at standard selection.
-        * TODO: Implement frame scaling?
-        */
+       } else if (width <= base_width / 2) {
+
+               /*
+                * The device will skip count units for each
+                * unit sent. This means count is:
+                *
+                * n = (frame width / width) - 1
+                *
+                * And the width is:
+                *
+                * width = frame width / (n + 1)
+                */
+               col_n = div_round_integer(base_width, width) - 1;
+               if (col_n > 0 && col_n <= 255) {
+                       col_en = true;
+                       col_mode = STK1160_DECIMATE_MORE_THAN_HALF;
+                       f->fmt.pix.width = base_width / (col_n + 1);
+               }
+       }
+
+       if (height < base_height && height > base_height / 2) {
+               row_n = div_round_integer(height, base_height - height);
+               if (row_n > 0 && row_n <= 255) {
+                       row_en = true;
+                       row_mode = STK1160_DECIMATE_LESS_THAN_HALF;
+                       f->fmt.pix.height = (base_height * row_n) / (row_n + 1);
+               }
+
+       } else if (height <= base_height / 2) {
+               row_n = div_round_integer(base_height, height) - 1;
+               if (row_n > 0 && row_n <= 255) {
+                       row_en = true;
+                       row_mode = STK1160_DECIMATE_MORE_THAN_HALF;
+                       f->fmt.pix.height = base_height / (row_n + 1);
+               }
+       }
 
        f->fmt.pix.pixelformat = dev->fmt->fourcc;
-       f->fmt.pix.width = dev->width;
-       f->fmt.pix.height = dev->height;
        f->fmt.pix.field = V4L2_FIELD_INTERLACED;
-       f->fmt.pix.bytesperline = dev->width * 2;
-       f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline;
+       f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
+       f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
        f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
 
+       if (ctrl) {
+               ctrl->col_en = col_en;
+               ctrl->col_n = col_n;
+               ctrl->col_mode = col_mode;
+               ctrl->row_en = row_en;
+               ctrl->row_n = row_n;
+               ctrl->row_mode = row_mode;
+       }
+
+       stk1160_dbg("width %d, height %d\n",
+                   f->fmt.pix.width, f->fmt.pix.height);
        return 0;
 }
 
+static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
+                                 struct v4l2_format *f)
+{
+       struct stk1160 *dev = video_drvdata(file);
+
+       return stk1160_try_fmt(dev, f, NULL);
+}
+
 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
                                        struct v4l2_format *f)
 {
        struct stk1160 *dev = video_drvdata(file);
        struct vb2_queue *q = &dev->vb_vidq;
+       struct stk1160_decimate_ctrl ctrl;
+       int rc;
 
        if (vb2_is_busy(q))
                return -EBUSY;
 
-       vidioc_try_fmt_vid_cap(file, priv, f);
-
-       /* We don't support any format changes */
+       rc = stk1160_try_fmt(dev, f, &ctrl);
+       if (rc < 0)
+               return rc;
+       dev->width = f->fmt.pix.width;
+       dev->height = f->fmt.pix.height;
+       stk1160_set_fmt(dev, &ctrl);
 
        return 0;
 }
@@ -391,22 +545,15 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
                return -ENODEV;
 
        /* We need to set this now, before we call stk1160_set_std */
+       dev->width = 720;
+       dev->height = (norm & V4L2_STD_525_60) ? 480 : 576;
        dev->norm = norm;
 
-       /* This is taken from saa7115 video decoder */
-       if (dev->norm & V4L2_STD_525_60) {
-               dev->width = 720;
-               dev->height = 480;
-       } else if (dev->norm & V4L2_STD_625_50) {
-               dev->width = 720;
-               dev->height = 576;
-       } else {
-               stk1160_err("invalid standard\n");
-               return -EINVAL;
-       }
-
        stk1160_set_std(dev);
 
+       /* Calling with NULL disables frame decimation */
+       stk1160_set_fmt(dev, NULL);
+
        v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std,
                        dev->norm);
 
@@ -500,6 +647,7 @@ static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
        .vidioc_dqbuf         = vb2_ioctl_dqbuf,
        .vidioc_streamon      = vb2_ioctl_streamon,
        .vidioc_streamoff     = vb2_ioctl_streamoff,
+       .vidioc_expbuf        = vb2_ioctl_expbuf,
 
        .vidioc_log_status  = v4l2_ctrl_log_status,
        .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
@@ -516,7 +664,7 @@ static const struct v4l2_ioctl_ops stk1160_ioctl_ops = {
 /*
  * Videobuf2 operations
  */
-static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
+static int queue_setup(struct vb2_queue *vq, const void *parg,
                                unsigned int *nbuffers, unsigned int *nplanes,
                                unsigned int sizes[], void *alloc_ctxs[])
 {
@@ -537,8 +685,8 @@ static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt,
 
        sizes[0] = size;
 
-       stk1160_info("%s: buffer count %d, each %ld bytes\n",
-                       __func__, *nbuffers, size);
+       stk1160_dbg("%s: buffer count %d, each %ld bytes\n",
+                   __func__, *nbuffers, size);
 
        return 0;
 }
@@ -547,8 +695,9 @@ static void buffer_queue(struct vb2_buffer *vb)
 {
        unsigned long flags;
        struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue);
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
        struct stk1160_buffer *buf =
-               container_of(vb, struct stk1160_buffer, vb);
+               container_of(vbuf, struct stk1160_buffer, vb);
 
        spin_lock_irqsave(&dev->buf_lock, flags);
        if (!dev->udev) {
@@ -556,7 +705,7 @@ static void buffer_queue(struct vb2_buffer *vb)
                 * If the device is disconnected return the buffer to userspace
                 * directly. The next QBUF call will fail with -ENODEV.
                 */
-               vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
+               vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
        } else {
 
                buf->mem = vb2_plane_vaddr(vb, 0);
@@ -569,7 +718,7 @@ static void buffer_queue(struct vb2_buffer *vb)
                 * the buffer to userspace directly.
                 */
                if (buf->length < dev->width * dev->height * 2)
-                       vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
+                       vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
                else
                        list_add_tail(&buf->list, &dev->avail_bufs);
 
@@ -621,9 +770,9 @@ void stk1160_clear_queue(struct stk1160 *dev)
                buf = list_first_entry(&dev->avail_bufs,
                        struct stk1160_buffer, list);
                list_del(&buf->list);
-               vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
-               stk1160_info("buffer [%p/%d] aborted\n",
-                               buf, buf->vb.v4l2_buf.index);
+               vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+               stk1160_dbg("buffer [%p/%d] aborted\n",
+                           buf, buf->vb.vb2_buf.index);
        }
 
        /* It's important to release the current buffer */
@@ -631,9 +780,9 @@ void stk1160_clear_queue(struct stk1160 *dev)
                buf = dev->isoc_ctl.buf;
                dev->isoc_ctl.buf = NULL;
 
-               vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
-               stk1160_info("buffer [%p/%d] aborted\n",
-                               buf, buf->vb.v4l2_buf.index);
+               vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+               stk1160_dbg("buffer [%p/%d] aborted\n",
+                           buf, buf->vb.vb2_buf.index);
        }
        spin_unlock_irqrestore(&dev->buf_lock, flags);
 }
@@ -645,7 +794,7 @@ int stk1160_vb2_setup(struct stk1160 *dev)
 
        q = &dev->vb_vidq;
        q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-       q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
+       q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
        q->drv_priv = dev;
        q->buf_struct_size = sizeof(struct stk1160_buffer);
        q->ops = &stk1160_video_qops;