From 7504f92483407233d9093e164d8f001db5c374e5 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Fri, 8 May 2020 22:05:29 +0100
Subject: [PATCH] staging: vc04_services: isp: Rework lens shading to
 take a dmabuf

This removes the need for the client to use vcsm at all.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 .../bcm2835-isp/bcm2835-v4l2-isp.c            | 36 +++++++++++++++++--
 include/uapi/linux/bcm2835-isp.h              |  4 +--
 2 files changed, 35 insertions(+), 5 deletions(-)

--- a/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
+++ b/drivers/staging/vc04_services/bcm2835-isp/bcm2835-v4l2-isp.c
@@ -21,6 +21,8 @@
 #include "vchiq-mmal/mmal-parameters.h"
 #include "vchiq-mmal/mmal-vchiq.h"
 
+#include "vc-sm-cma/vc_sm_knl.h"
+
 #include "bcm2835_isp_ctrls.h"
 #include "bcm2835_isp_fmts.h"
 
@@ -722,10 +724,38 @@ static int bcm2835_isp_s_ctrl(struct v4l
 				    sizeof(struct bcm2835_isp_custom_ccm));
 		break;
 	case V4L2_CID_USER_BCM2835_ISP_LENS_SHADING:
-		ret = set_isp_param(node, MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
-				    ctrl->p_new.p_u8,
-				    sizeof(struct bcm2835_isp_lens_shading));
+	{
+		struct bcm2835_isp_lens_shading ls;
+		struct dma_buf *dmabuf;
+		void *vcsm_handle;
+
+		memcpy(&ls, ctrl->p_new.p_u8,
+		       sizeof(struct bcm2835_isp_lens_shading));
+
+		dmabuf = dma_buf_get(ls.dmabuf);
+		if (!dmabuf)
+			return -EINVAL;
+
+		ret = vc_sm_cma_import_dmabuf(dmabuf,
+					      &vcsm_handle);
+		if (ret) {
+			dma_buf_put(dmabuf);
+			return -EINVAL;
+		}
+
+		ls.dmabuf = vc_sm_cma_int_handle(vcsm_handle);
+		if (ls.dmabuf)
+			ret = set_isp_param(node,
+					    MMAL_PARAMETER_LENS_SHADING_OVERRIDE,
+					    &ls,
+					    sizeof(struct bcm2835_isp_lens_shading));
+		else
+			ret = -EINVAL;
+
+		vc_sm_cma_free(vcsm_handle);
+		dma_buf_put(dmabuf);
 		break;
+	}
 	case V4L2_CID_USER_BCM2835_ISP_BLACK_LEVEL:
 		ret = set_isp_param(node, MMAL_PARAMETER_BLACK_LEVEL,
 				    ctrl->p_new.p_u8,
--- a/include/uapi/linux/bcm2835-isp.h
+++ b/include/uapi/linux/bcm2835-isp.h
@@ -108,7 +108,7 @@ enum bcm2835_isp_gain_format {
  * @grid_stride:	Row to row distance (in grid cells) between grid cells
  *			in the same horizontal location.
  * @grid_height:	Height of lens shading tables in grid cells.
- * @mem_handle_table:	Memory handle to the tables.
+ * @dmabuf:		dmabuf file handle containing the table.
  * @ref_transform:	Reference transform - unsupported, please pass zero.
  * @corner_sampled:	Whether the gains are sampled at the corner points
  *			of the grid cells or in the cell centres.
@@ -120,7 +120,7 @@ struct bcm2835_isp_lens_shading {
 	__u32 grid_width;
 	__u32 grid_stride;
 	__u32 grid_height;
-	__u32 mem_handle_table;
+	__s32 dmabuf;
 	__u32 ref_transform;
 	__u32 corner_sampled;
 	__u32 gain_format;
