msm: camera: Changes to enable events and Auto Focus

Added event notify message support added entries for
autofocus event types

Signed-off-by: Mansoor Aftab <maftab@codeaurora.org>
diff --git a/drivers/media/video/msm/msm.c b/drivers/media/video/msm/msm.c
index 640a0a0..1babb9e 100644
--- a/drivers/media/video/msm/msm.c
+++ b/drivers/media/video/msm/msm.c
@@ -184,7 +184,7 @@
 		/* TODO: Refactor msm_ctrl_cmd::status field */
 		if (out->status == 0)
 			rc = -1;
-		else if (out->status == 1)
+		else if (out->status == 1 || out->status == 4)
 			rc = 0;
 		else
 			rc = -EINVAL;
@@ -673,21 +673,25 @@
 	struct msm_cam_v4l2_dev_inst *pcam_inst, int streamon, int rc)
 {
 	struct v4l2_event ev;
-	struct msm_camera_event_stream stm;
+	struct msm_camera_event stm;
 
-	if (!(pcam_inst->pcam->event_mask & MSM_CAMERA_EVT_TYPE_STREAM))
+	if (!(pcam_inst->pcam->event_mask &
+			(1 << MSM_CAMERA_EVT_TYPE_STREAM))) {
+		pr_err("%s:Event mask not matching", __func__);
 		return;
+	}
 
 	memset(&ev, 0, sizeof(ev));
-	stm.image_mode = pcam_inst->image_mode;
-	stm.op_mode = pcam_inst->pcam->op_mode;
+	stm.event_type = MSM_CAMERA_EVT_TYPE_STREAM;
+	stm.e.stream.image_mode = pcam_inst->image_mode;
+	stm.e.stream.op_mode = pcam_inst->pcam->op_mode;
 	if (rc == 0) {
 		if (streamon)
-			stm.status = MSM_CAMERA_STREAM_STATUS_ON;
+			stm.e.stream.status = MSM_CAMERA_STREAM_STATUS_ON;
 		else
-			stm.status = MSM_CAMERA_STREAM_STATUS_OFF;
+			stm.e.stream.status = MSM_CAMERA_STREAM_STATUS_OFF;
 	} else
-		stm.status = MSM_CAMERA_STREAM_STATUS_ERR;
+		stm.e.stream.status = MSM_CAMERA_STREAM_STATUS_ERR;
 
 	ev.type = V4L2_EVENT_PRIVATE_START + MSM_CAMERA_EVT_TYPE_STREAM;
 	ktime_get_ts(&ev.timestamp);
@@ -1019,6 +1023,9 @@
 		if (rc < 0)
 			D("%s: failed for evtType = 0x%x, rc = %d\n",
 						__func__, sub->type, rc);
+		else
+			pcam_inst->pcam->event_mask |=
+				(1 << (sub->type - V4L2_EVENT_PRIVATE_START));
 	}
 
 	D("%s: rc = %d\n", __func__, rc);
@@ -1397,16 +1404,19 @@
 		pr_err("%s NULL pointer of camera device!\n", __func__);
 		return -EINVAL;
 	}
-
-	if (!pcam_inst->vid_bufq.streaming) {
-		D("%s vid_bufq.streaming is off, inst=0x%x\n",
+	if (pcam_inst->my_index == 0) {
+		poll_wait(f, &(pcam_inst->eventHandle.events->wait), wait);
+		if (v4l2_event_pending(&pcam_inst->eventHandle))
+			rc |= POLLPRI;
+	} else {
+		if (!pcam_inst->vid_bufq.streaming) {
+			D("%s vid_bufq.streaming is off, inst=0x%x\n",
 			__func__, (u32)pcam_inst);
-		return -EINVAL;
+			return -EINVAL;
+		}
+		rc |= vb2_poll(&pcam_inst->vid_bufq, f, wait);
 	}
-
-	rc |= vb2_poll(&pcam_inst->vid_bufq, f, wait);
 	D("%s returns, rc  = 0x%x\n", __func__, rc);
-
 	return rc;
 }
 
@@ -1600,6 +1610,22 @@
 	return rc;
 }
 
+static int msm_v4l2_evt_notify(struct msm_cam_media_controller *mctl,
+	unsigned int cmd, struct msm_camera_event *ev)
+{
+	struct v4l2_event v4l2_ev;
+	struct msm_cam_v4l2_device *pcam = NULL;
+	if (!mctl) {
+		pr_err("%s: mctl is NULL\n", __func__);
+		return -EINVAL;
+	}
+	pcam = g_server_dev.pcam_active;
+	v4l2_ev.type = V4L2_EVENT_PRIVATE_START + MSM_CAMERA_EVT_TYPE_CTRL;
+	ktime_get_ts(&v4l2_ev.timestamp);
+	memcpy(v4l2_ev.u.data, ev, sizeof(struct msm_camera_event));
+	v4l2_event_queue(pcam->pvdev, &v4l2_ev);
+	return 0;
+}
 static long msm_ioctl_config(struct file *fp, unsigned int cmd,
 	unsigned long arg)
 {
@@ -1697,6 +1723,16 @@
 
 		break;
 
+	case MSM_CAM_IOCTL_V4L2_EVT_NOTIFY: {
+		struct msm_camera_event event;
+		if (copy_from_user(&event, (void __user *)arg,
+		sizeof(struct msm_camera_event))) {
+			ERR_COPY_FROM_USER();
+			return -EFAULT;
+		}
+		rc = msm_v4l2_evt_notify(config_cam->p_mctl, cmd, &event);
+		break;
+	}
 	default:{
 		/* For the rest of config command, forward to media controller*/
 		struct msm_cam_media_controller *p_mctl = config_cam->p_mctl;
diff --git a/include/media/msm_camera.h b/include/media/msm_camera.h
index 368c987..a5d4b06 100644
--- a/include/media/msm_camera.h
+++ b/include/media/msm_camera.h
@@ -135,6 +135,9 @@
 #define MSM_CAM_IOCTL_GET_CONFIG_INFO \
 	_IOR(MSM_CAM_IOCTL_MAGIC, 40, struct msm_cam_config_dev_info *)
 
+#define MSM_CAM_IOCTL_V4L2_EVT_NOTIFY \
+	_IOR(MSM_CAM_IOCTL_MAGIC, 41, struct msm_camera_event *)
+
 #define MSM_CAMERA_LED_OFF  0
 #define MSM_CAMERA_LED_LOW  1
 #define MSM_CAMERA_LED_HIGH 2
@@ -236,7 +239,7 @@
 };
 struct msm_camera_event_stats {
 	uint32_t event_id;
-	uint8_t data[60]; /* to be determined later */
+	uint8_t data[40]; /* to be determined later */
 };
 
 struct msm_camera_event {
@@ -246,14 +249,15 @@
 		struct msm_camera_event_stream stream;
 		struct msm_camera_event_ctrl ctrl;
 		struct msm_camera_event_stats stats;
-	};
+	} e;
 };
 
 /* driver event types */
 #define MSM_CAM_EVT_HISTOGRAM_NOTIFY	(V4L2_EVENT_PRIVATE_START+1)
 #define MSM_CAM_EVT_STREAMING_NOTIFY	(V4L2_EVENT_PRIVATE_START+2)
 #define MSM_CAM_EVT_ASYNC_CMD_NOTIFY	(V4L2_EVENT_PRIVATE_START+3)
-#define MSM_CAM_EVT_CNT_MAX				3
+#define MSM_CAM_EVT_AUTO_FOCUS_DONE	(V4L2_EVENT_PRIVATE_START+4)
+#define MSM_CAM_EVT_MAX			(V4L2_EVENT_PRIVATE_START+5)
 
 struct msm_event_histogram {
 	int32_t fd;