1 /*
2 Copyright (c) 2011-2012,2015, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <pthread.h>
31 #include "mm_camera_dbg.h"
32 #include <errno.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <poll.h>
38 #include "mm_camera_sock.h"
39 #include "mm_camera_interface2.h"
40 #include "mm_camera.h"
41 
42 static int32_t mm_camera_send_native_ctrl_cmd(mm_camera_obj_t * my_obj,
43     cam_ctrl_type type, uint32_t length, void *value);
44 static int32_t mm_camera_send_native_ctrl_timeout_cmd(mm_camera_obj_t * my_obj,
45     cam_ctrl_type type, uint32_t length, void *value, int timeout);
mm_camera_ctrl_set_specialEffect(mm_camera_obj_t * my_obj,int effect)46 static int32_t mm_camera_ctrl_set_specialEffect (mm_camera_obj_t *my_obj, int effect) {
47     struct v4l2_control ctrl;
48     if (effect == CAMERA_EFFECT_MAX)
49         effect = CAMERA_EFFECT_OFF;
50     int rc = 0;
51 
52     ctrl.id = MSM_V4L2_PID_EFFECT;
53     ctrl.value = effect;
54     rc = ioctl(my_obj->ctrl_fd, VIDIOC_S_CTRL, &ctrl);
55     return rc;
56 }
57 
mm_camera_ctrl_set_antibanding(mm_camera_obj_t * my_obj,int antibanding)58 static int32_t mm_camera_ctrl_set_antibanding (mm_camera_obj_t *my_obj, int antibanding) {
59     int rc = 0;
60     struct v4l2_control ctrl;
61 
62     ctrl.id = V4L2_CID_POWER_LINE_FREQUENCY;
63     ctrl.value = antibanding;
64     rc = ioctl(my_obj->ctrl_fd, VIDIOC_S_CTRL, &ctrl);
65     return rc;
66 }
67 
mm_camera_ctrl_set_auto_focus(mm_camera_obj_t * my_obj,int value)68 static int32_t mm_camera_ctrl_set_auto_focus (mm_camera_obj_t *my_obj, int value)
69 {
70     int rc = 0;
71     struct v4l2_queryctrl queryctrl;
72 
73     memset (&queryctrl, 0, sizeof (queryctrl));
74     queryctrl.id = V4L2_CID_FOCUS_AUTO;
75 
76     if(value != 0 && value != 1) {
77         CDBG("%s:boolean required, invalid value = %d\n",__func__, value);
78         return -MM_CAMERA_E_INVALID_INPUT;
79     }
80     if (-1 == ioctl (my_obj->ctrl_fd, VIDIOC_QUERYCTRL, &queryctrl)) {
81         CDBG ("V4L2_CID_FOCUS_AUTO is not supported\n");
82     } else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
83         CDBG ("%s:V4L2_CID_FOCUS_AUTO is not supported\n", __func__);
84     } else {
85         if(0 != (rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
86                 V4L2_CID_FOCUS_AUTO, value))){
87             CDBG("%s: error, id=0x%x, value=%d, rc = %d\n",
88                      __func__, V4L2_CID_FOCUS_AUTO, value, rc);
89             rc = -1;
90         }
91     }
92     return rc;
93 }
94 
mm_camera_ctrl_set_whitebalance(mm_camera_obj_t * my_obj,int mode)95 static int32_t mm_camera_ctrl_set_whitebalance (mm_camera_obj_t *my_obj, int mode) {
96 
97     int rc = 0, value;
98     uint32_t id;
99 
100     switch(mode) {
101     case MM_CAMERA_WHITE_BALANCE_AUTO:
102         id = V4L2_CID_AUTO_WHITE_BALANCE;
103         value = 1; /* TRUE */
104         break;
105     case MM_CAMERA_WHITE_BALANCE_OFF:
106         id = V4L2_CID_AUTO_WHITE_BALANCE;
107         value = 0; /* FALSE */
108         break;
109     default:
110         id = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
111         if(mode == WHITE_BALANCE_DAYLIGHT) value = 6500;
112         else if(mode == WHITE_BALANCE_INCANDESCENT) value = 2800;
113         else if(mode == WHITE_BALANCE_FLUORESCENT ) value = 4200;
114         else if(mode == WHITE_BALANCE_CLOUDY) value = 7500;
115         else
116             value = 4200;
117     }
118     if(0 != (rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
119             id, value))){
120         CDBG("%s: error, exp_metering_action_param=%d, rc = %d\n", __func__, value, rc);
121         goto end;
122     }
123 end:
124     return rc;
125 }
126 
mm_camera_ctrl_set_toggle_afr(mm_camera_obj_t * my_obj)127 static int32_t mm_camera_ctrl_set_toggle_afr (mm_camera_obj_t *my_obj) {
128     int rc = 0;
129     int value = 0;
130     if(0 != (rc =  mm_camera_util_g_ctrl(my_obj->ctrl_fd,
131             V4L2_CID_EXPOSURE_AUTO, &value))){
132         goto end;
133     }
134     /* V4L2_CID_EXPOSURE_AUTO needs to be AUTO or SHUTTER_PRIORITY */
135     if (value != V4L2_EXPOSURE_AUTO && value != V4L2_EXPOSURE_SHUTTER_PRIORITY) {
136     CDBG("%s: V4L2_CID_EXPOSURE_AUTO needs to be AUTO/SHUTTER_PRIORITY\n",
137         __func__);
138     return -1;
139   }
140     if(0 != (rc =  mm_camera_util_g_ctrl(my_obj->ctrl_fd,
141             V4L2_CID_EXPOSURE_AUTO_PRIORITY, &value))){
142         goto end;
143     }
144     value = !value;
145     if(0 != (rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
146             V4L2_CID_EXPOSURE_AUTO_PRIORITY, value))){
147         goto end;
148     }
149 end:
150     return rc;
151 }
152 
mm_camera_util_opcode_2_ch_type(mm_camera_obj_t * my_obj,mm_camera_ops_type_t opcode)153 static mm_camera_channel_type_t mm_camera_util_opcode_2_ch_type(
154                              mm_camera_obj_t *my_obj,
155                              mm_camera_ops_type_t opcode)
156 {
157     mm_camera_channel_type_t type = MM_CAMERA_CH_MAX;
158     switch(opcode) {
159     case MM_CAMERA_OPS_PREVIEW:
160         return MM_CAMERA_CH_PREVIEW;
161     case MM_CAMERA_OPS_VIDEO:
162         return MM_CAMERA_CH_VIDEO;
163     case MM_CAMERA_OPS_SNAPSHOT:
164         return MM_CAMERA_CH_SNAPSHOT;
165     case MM_CAMERA_OPS_PREPARE_SNAPSHOT:
166         return MM_CAMERA_CH_SNAPSHOT;
167     case MM_CAMERA_OPS_RAW:
168         return MM_CAMERA_CH_RAW;
169     case MM_CAMERA_OPS_ZSL:
170         return MM_CAMERA_CH_SNAPSHOT;
171     default:
172         break;
173     }
174     return type;
175 }
176 
mm_camera_util_set_op_mode(mm_camera_obj_t * my_obj,mm_camera_op_mode_type_t * op_mode)177 static int32_t mm_camera_util_set_op_mode(mm_camera_obj_t * my_obj,
178     mm_camera_op_mode_type_t *op_mode)
179 {
180     int32_t rc = MM_CAMERA_OK;
181     uint32_t v4l2_op_mode = MSM_V4L2_CAM_OP_DEFAULT;
182 
183     if (my_obj->op_mode == *op_mode)
184         goto end;
185     if(mm_camera_poll_busy(my_obj) == TRUE) {
186         CDBG("%s: cannot change op_mode while stream on\n", __func__);
187         rc = -MM_CAMERA_E_INVALID_OPERATION;
188         goto end;
189     }
190     switch(*op_mode) {
191     case MM_CAMERA_OP_MODE_ZSL:
192         v4l2_op_mode = MSM_V4L2_CAM_OP_ZSL;
193             break;
194     case MM_CAMERA_OP_MODE_CAPTURE:
195         v4l2_op_mode = MSM_V4L2_CAM_OP_CAPTURE;
196             break;
197     case MM_CAMERA_OP_MODE_VIDEO:
198         v4l2_op_mode = MSM_V4L2_CAM_OP_VIDEO;
199             break;
200     default:
201         rc = - MM_CAMERA_E_INVALID_INPUT;
202         goto end;
203         break;
204     }
205     if(0 != (rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
206             MSM_V4L2_PID_CAM_MODE, v4l2_op_mode))){
207         CDBG("%s: input op_mode=%d, s_ctrl rc=%d\n", __func__, *op_mode, rc);
208         goto end;
209     }
210     /* if success update mode field */
211     my_obj->op_mode = *op_mode;
212 end:
213     CDBG("%s: op_mode=%d,rc=%d\n", __func__, *op_mode, rc);
214     return rc;
215 }
216 
mm_camera_set_general_parm(mm_camera_obj_t * my_obj,mm_camera_parm_t * parm)217 int32_t mm_camera_set_general_parm(mm_camera_obj_t * my_obj, mm_camera_parm_t *parm)
218 {
219     int rc = -MM_CAMERA_E_NOT_SUPPORTED;
220     int isZSL =0;
221 
222     switch(parm->parm_type)  {
223     case MM_CAMERA_PARM_EXPOSURE:
224         return mm_camera_util_s_ctrl(my_obj->ctrl_fd,
225                                                                         MSM_V4L2_PID_EXP_METERING,
226                                                                             *((int *)(parm->p_value)));
227     case MM_CAMERA_PARM_SHARPNESS:
228         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, V4L2_CID_SHARPNESS,
229                                                                             *((int *)(parm->p_value)));
230     case MM_CAMERA_PARM_CONTRAST:
231         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, V4L2_CID_CONTRAST,
232                                                                             *((int *)(parm->p_value)));
233     case MM_CAMERA_PARM_SATURATION:
234         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, V4L2_CID_SATURATION,
235                                                                             *((int *)(parm->p_value)));
236     case MM_CAMERA_PARM_BRIGHTNESS:
237         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, V4L2_CID_BRIGHTNESS,
238                                                                             *((int *)(parm->p_value)));
239     case MM_CAMERA_PARM_WHITE_BALANCE:
240         return mm_camera_ctrl_set_whitebalance (my_obj, *((int *)(parm->p_value)));
241     case MM_CAMERA_PARM_ISO:
242         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, MSM_V4L2_PID_ISO,
243                                                                             *((int *)(parm->p_value)));
244     case MM_CAMERA_PARM_ZOOM:
245         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, V4L2_CID_ZOOM_ABSOLUTE,
246                                                                             *((int *)(parm->p_value)));
247     case MM_CAMERA_PARM_LUMA_ADAPTATION:
248         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, MSM_V4L2_PID_LUMA_ADAPTATION,
249                                                                             *((int *)(parm->p_value)));
250     case MM_CAMERA_PARM_ANTIBANDING:
251         return mm_camera_ctrl_set_antibanding (my_obj, *((int *)(parm->p_value)));
252     case MM_CAMERA_PARM_CONTINUOUS_AF:
253         return mm_camera_ctrl_set_auto_focus(my_obj, *((int *)(parm->p_value)));
254     case MM_CAMERA_PARM_HJR:
255         return mm_camera_util_s_ctrl(my_obj->ctrl_fd, MSM_V4L2_PID_HJR, *((int *)(parm->p_value)));
256     case MM_CAMERA_PARM_EFFECT:
257         return mm_camera_ctrl_set_specialEffect (my_obj, *((int *)(parm->p_value)));
258     case MM_CAMERA_PARM_FPS:
259         return mm_camera_send_native_ctrl_cmd(my_obj,
260                     CAMERA_SET_PARM_FPS, sizeof(uint32_t), (void *)parm->p_value);
261     case MM_CAMERA_PARM_FPS_MODE:
262         return mm_camera_send_native_ctrl_cmd(my_obj,
263                     CAMERA_SET_FPS_MODE, sizeof(int32_t), (void *)parm->p_value);
264     case MM_CAMERA_PARM_EXPOSURE_COMPENSATION:
265         return mm_camera_send_native_ctrl_cmd(my_obj,
266                     CAMERA_SET_PARM_EXPOSURE_COMPENSATION, sizeof(int32_t), (void *)parm->p_value);
267     case MM_CAMERA_PARM_LED_MODE:
268         return mm_camera_send_native_ctrl_cmd(my_obj,
269                     CAMERA_SET_PARM_LED_MODE, sizeof(int32_t), (void *)parm->p_value);
270     case MM_CAMERA_PARM_ROLLOFF:
271         return mm_camera_send_native_ctrl_cmd(my_obj,
272                     CAMERA_SET_PARM_ROLLOFF, sizeof(int32_t), (void *)parm->p_value);
273     case MM_CAMERA_PARM_MODE:
274         my_obj->current_mode = *((camera_mode_t *)parm->p_value);
275         break;
276     case MM_CAMERA_PARM_FOCUS_RECT:
277         return mm_camera_send_native_ctrl_cmd(my_obj,
278                     CAMERA_SET_PARM_FOCUS_RECT, sizeof(int32_t), (void *)parm->p_value);
279     case MM_CAMERA_PARM_AEC_ROI:
280         return mm_camera_send_native_ctrl_cmd(my_obj,
281                     CAMERA_SET_PARM_AEC_ROI, sizeof(cam_set_aec_roi_t), (void *)parm->p_value);
282     case MM_CAMERA_PARM_AF_ROI:
283         return mm_camera_send_native_ctrl_cmd(my_obj,
284                     CAMERA_SET_PARM_AF_ROI, sizeof(roi_info_t), (void *)parm->p_value);
285     case MM_CAMERA_PARM_FOCUS_MODE:
286         return mm_camera_send_native_ctrl_cmd(my_obj,
287                     CAMERA_SET_PARM_AF_MODE, sizeof(int32_t), (void *)parm->p_value);
288 #if 0 //to be enabled later: @punits
289     case MM_CAMERA_PARM_AF_MTR_AREA:
290         return mm_camera_send_native_ctrl_cmd(my_obj,
291                     CAMERA_SET_PARM_AF_MTR_AREA, sizeof(af_mtr_area_t), (void *)parm->p_value);*/
292     case MM_CAMERA_PARM_AEC_MTR_AREA:
293         return mm_camera_send_native_ctrl_cmd(my_obj,
294                     CAMERA_SET_AEC_MTR_AREA, sizeof(aec_mtr_area_t), (void *)parm->p_value);
295 #endif
296     case MM_CAMERA_PARM_CAF_ENABLE:
297         return mm_camera_send_native_ctrl_cmd(my_obj,
298                     CAMERA_SET_PARM_CAF, sizeof(uint32_t), (void *)parm->p_value);
299     case MM_CAMERA_PARM_BESTSHOT_MODE:
300         CDBG("%s : MM_CAMERA_PARM_BESTSHOT_MODE value : %d",__func__,*((int *)(parm->p_value)));
301         return mm_camera_send_native_ctrl_cmd(my_obj,
302                     CAMERA_SET_PARM_BESTSHOT_MODE, sizeof(int32_t), (void *)parm->p_value);
303         break;
304     case MM_CAMERA_PARM_VIDEO_DIS:
305         return mm_camera_send_native_ctrl_cmd(my_obj,
306                     CAMERA_SET_VIDEO_DIS_PARAMS, sizeof(video_dis_param_ctrl_t), (void *)parm->p_value);
307     case MM_CAMERA_PARM_VIDEO_ROT:
308         return mm_camera_send_native_ctrl_cmd(my_obj,
309                     CAMERA_SET_VIDEO_ROT_PARAMS, sizeof(video_rotation_param_ctrl_t), (void *)parm->p_value);
310     case MM_CAMERA_PARM_SCE_FACTOR:
311         return mm_camera_send_native_ctrl_cmd(my_obj,
312                     CAMERA_SET_SCE_FACTOR, sizeof(int32_t), (void *)parm->p_value);
313     case MM_CAMERA_PARM_FD:
314         return mm_camera_send_native_ctrl_cmd(my_obj,
315                     CAMERA_SET_PARM_FD, sizeof(fd_set_parm_t), (void *)parm->p_value);
316     case MM_CAMERA_PARM_AEC_LOCK:
317         return mm_camera_send_native_ctrl_cmd(my_obj,
318                     CAMERA_SET_AEC_LOCK, sizeof(int32_t), (void *)parm->p_value);
319     case MM_CAMERA_PARM_AWB_LOCK:
320         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_SET_AWB_LOCK,
321                                                      sizeof(int32_t), (void *)parm->p_value);
322     case MM_CAMERA_PARM_MCE:
323         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_SET_PARM_MCE,
324                                                      sizeof(int32_t), (void *)parm->p_value);
325     case MM_CAMERA_PARM_HORIZONTAL_VIEW_ANGLE:
326         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_HORIZONTAL_VIEW_ANGLE,
327                                                      sizeof(focus_distances_info_t), (void *)parm->p_value);
328     case MM_CAMERA_PARM_VERTICAL_VIEW_ANGLE:
329         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_VERTICAL_VIEW_ANGLE,
330                                                      sizeof(focus_distances_info_t), (void *)parm->p_value);
331     case MM_CAMERA_PARM_RESET_LENS_TO_INFINITY:
332         return mm_camera_send_native_ctrl_cmd(my_obj,
333                             CAMERA_SET_PARM_RESET_LENS_TO_INFINITY,
334                             0, NULL);
335     case MM_CAMERA_PARM_SNAPSHOTDATA:
336         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_SNAPSHOTDATA,
337                                                      sizeof(snapshotData_info_t), (void *)parm->p_value);
338     case MM_CAMERA_PARM_HFR:
339         return mm_camera_send_native_ctrl_cmd(my_obj,
340                     CAMERA_SET_PARM_HFR, sizeof(int32_t), (void *)parm->p_value);
341     case MM_CAMERA_PARM_REDEYE_REDUCTION:
342         return mm_camera_send_native_ctrl_cmd(my_obj,
343                     CAMERA_SET_REDEYE_REDUCTION, sizeof(int32_t), (void *)parm->p_value);
344     case MM_CAMERA_PARM_WAVELET_DENOISE:
345         return mm_camera_send_native_ctrl_cmd(my_obj,
346                     CAMERA_SET_PARM_WAVELET_DENOISE, sizeof(denoise_param_t), (void *)parm->p_value);
347     case MM_CAMERA_PARM_3D_DISPLAY_DISTANCE:
348         return mm_camera_send_native_ctrl_cmd(my_obj,
349                     CAMERA_SET_PARM_3D_DISPLAY_DISTANCE, sizeof(float), (void *)parm->p_value);
350     case MM_CAMERA_PARM_3D_VIEW_ANGLE:
351         return mm_camera_send_native_ctrl_cmd(my_obj,
352                     CAMERA_SET_PARM_3D_VIEW_ANGLE, sizeof(uint32_t), (void *)parm->p_value);
353     case MM_CAMERA_PARM_ZOOM_RATIO:
354         break;
355     case MM_CAMERA_PARM_HISTOGRAM:
356         return mm_camera_send_native_ctrl_cmd(my_obj,
357                     CAMERA_SET_PARM_HISTOGRAM, sizeof(int8_t), (void *)parm->p_value);
358     case MM_CAMERA_PARM_JPEG_ROTATION:
359         if(my_obj->op_mode == MM_CAMERA_OP_MODE_ZSL){
360            isZSL =1;
361         }
362         mm_jpeg_encoder_setRotation(*((int *)parm->p_value),isZSL);
363         return MM_CAMERA_OK;
364 
365     case MM_CAMERA_PARM_ASD_ENABLE:
366       return mm_camera_send_native_ctrl_cmd(my_obj,
367                   CAMERA_SET_ASD_ENABLE, sizeof(uint32_t), (void *)parm->p_value);
368 
369     case MM_CAMERA_PARM_RECORDING_HINT:
370       return mm_camera_send_native_ctrl_cmd(my_obj,
371                   CAMERA_SET_RECORDING_HINT, sizeof(uint32_t), (void *)parm->p_value);
372 
373     case MM_CAMERA_PARM_PREVIEW_FORMAT:
374       return mm_camera_send_native_ctrl_cmd(my_obj,
375                   CAMERA_SET_PARM_PREVIEW_FORMAT, sizeof(uint32_t), (void *)parm->p_value);
376 
377     case MM_CAMERA_PARM_DIS_ENABLE:
378       return mm_camera_send_native_ctrl_cmd(my_obj,
379                   CAMERA_SET_DIS_ENABLE, sizeof(uint32_t), (void *)parm->p_value);
380 
381     case MM_CAMERA_PARM_FULL_LIVESHOT: {
382       my_obj->full_liveshot = *((int *)(parm->p_value));
383       return mm_camera_send_native_ctrl_cmd(my_obj,
384                   CAMERA_SET_FULL_LIVESHOT, sizeof(uint32_t), (void *)parm->p_value);
385     }
386 
387     case MM_CAMERA_PARM_LOW_POWER_MODE:
388       return mm_camera_send_native_ctrl_cmd(my_obj,
389                   CAMERA_SET_LOW_POWER_MODE, sizeof(uint32_t), (void *)parm->p_value);
390 
391     case MM_CAMERA_PARM_CAF_TYPE:
392       CDBG("SM : %s : MM_CAMERA_PARM_CAF_TYPE value : %d",__func__,*((int *)(parm->p_value)));
393       return mm_camera_send_native_ctrl_cmd(my_obj,
394                   CAMERA_SET_PARM_CAF_TYPE, sizeof(int32_t), (void *)parm->p_value);
395 
396     case MM_CAMERA_PARM_HDR:
397       return mm_camera_send_native_ctrl_cmd(my_obj,
398                         CAMERA_SET_PARM_HDR, sizeof(exp_bracketing_t), (void *)parm->p_value);
399 
400     case MM_CAMERA_PARM_CAF_LOCK_CANCEL:
401       return mm_camera_send_native_ctrl_cmd(my_obj,
402                   CAMERA_SET_CAF_LOCK_CANCEL, sizeof(uint32_t), (void *)parm->p_value);
403 
404     default:
405         CDBG("%s: default: parm %d not supported\n", __func__, parm->parm_type);
406         break;
407     }
408     return rc;
409 }
mm_camera_send_native_ctrl_cmd(mm_camera_obj_t * my_obj,cam_ctrl_type type,uint32_t length,void * value)410 static int32_t mm_camera_send_native_ctrl_cmd(mm_camera_obj_t * my_obj,
411                     cam_ctrl_type type, uint32_t length, void *value)
412 {
413     int rc = -1;
414     struct msm_ctrl_cmd ctrl_cmd;
415     memset(&ctrl_cmd, 0, sizeof(ctrl_cmd));
416     ctrl_cmd.type = type;
417     ctrl_cmd.length = (uint16_t)length;
418     ctrl_cmd.timeout_ms = 1000;
419     ctrl_cmd.value = value;
420     ctrl_cmd.status = CAM_CTRL_SUCCESS;
421     rc = mm_camera_util_private_s_ctrl(my_obj->ctrl_fd, MSM_V4L2_PID_CTRL_CMD,
422                                                                             (int)&ctrl_cmd);
423     CDBG("%s: type=%d, rc = %d, status = %d\n",
424                 __func__, type, rc, ctrl_cmd.status);
425 
426     if(rc != MM_CAMERA_OK || ((ctrl_cmd.status != CAM_CTRL_ACCEPTED) &&
427       (ctrl_cmd.status != CAM_CTRL_SUCCESS) &&
428       (ctrl_cmd.status != CAM_CTRL_INVALID_PARM)))
429         rc = -1;
430     return rc;
431 }
432 
mm_camera_send_native_ctrl_timeout_cmd(mm_camera_obj_t * my_obj,cam_ctrl_type type,uint32_t length,void * value,int timeout)433 static int32_t mm_camera_send_native_ctrl_timeout_cmd(mm_camera_obj_t * my_obj,
434   cam_ctrl_type type, uint32_t length, void *value,int timeout)
435 {
436     int rc = -1;
437     struct msm_ctrl_cmd ctrl_cmd;
438     memset(&ctrl_cmd, 0, sizeof(ctrl_cmd));
439     ctrl_cmd.type = type;
440     ctrl_cmd.length = (uint16_t)length;
441     ctrl_cmd.timeout_ms = timeout;
442     ctrl_cmd.value = value;
443     ctrl_cmd.status = CAM_CTRL_SUCCESS;
444     rc = mm_camera_util_private_s_ctrl(my_obj->ctrl_fd, MSM_V4L2_PID_CTRL_CMD,
445         (int)&ctrl_cmd);
446     CDBG("%s: type=%d, rc = %d, status = %d\n",
447         __func__, type, rc, ctrl_cmd.status);
448     if(rc != MM_CAMERA_OK || ((ctrl_cmd.status != CAM_CTRL_ACCEPTED) &&
449         (ctrl_cmd.status != CAM_CTRL_SUCCESS) &&
450         (ctrl_cmd.status != CAM_CTRL_INVALID_PARM)))
451         rc = -1;
452     return rc;
453 }
454 
mm_camera_set_parm(mm_camera_obj_t * my_obj,mm_camera_parm_t * parm)455 int32_t mm_camera_set_parm(mm_camera_obj_t * my_obj,
456     mm_camera_parm_t *parm)
457 {
458     int32_t rc = -1;
459     uint16_t len;
460     CDBG("%s type =%d", __func__, parm->parm_type);
461     switch(parm->parm_type) {
462     case MM_CAMERA_PARM_OP_MODE:
463         rc = mm_camera_util_set_op_mode(my_obj,
464                         (mm_camera_op_mode_type_t *)parm->p_value);
465         break;
466     case MM_CAMERA_PARM_DIMENSION:
467         rc = mm_camera_send_native_ctrl_cmd(my_obj,
468                     CAMERA_SET_PARM_DIMENSION, sizeof(cam_ctrl_dimension_t), parm->p_value);
469         if(rc != MM_CAMERA_OK) {
470             CDBG("%s: mm_camera_send_native_ctrl_cmd err=%d\n", __func__, rc);
471             break;
472         }
473         memcpy(&my_obj->dim, (cam_ctrl_dimension_t *)parm->p_value,
474                      sizeof(cam_ctrl_dimension_t));
475         CDBG("%s: dw=%d,dh=%d,vw=%d,vh=%d,pw=%d,ph=%d,tw=%d,th=%d,raw_w=%d,raw_h=%d\n",
476                  __func__,
477                  my_obj->dim.display_width,my_obj->dim.display_height,
478                  my_obj->dim.video_width, my_obj->dim.video_height,
479                  my_obj->dim.picture_width,my_obj->dim.picture_height,
480                  my_obj->dim.ui_thumbnail_width,my_obj->dim.ui_thumbnail_height,
481                  my_obj->dim.raw_picture_width,my_obj->dim.raw_picture_height);
482         break;
483     case MM_CAMERA_PARM_SNAPSHOT_BURST_NUM:
484         CDBG("%s: Setting snapshot burst number: %d\n", __func__, *((int *)parm->p_value));
485         my_obj->snap_burst_num_by_user = *((int *)parm->p_value);
486         rc = MM_CAMERA_OK;
487         break;
488     case MM_CAMERA_PARM_CH_IMAGE_FMT:
489         {
490             mm_camera_ch_image_fmt_parm_t *fmt;
491             fmt = (mm_camera_ch_image_fmt_parm_t *)parm->p_value;
492             rc = mm_camera_ch_fn(my_obj,    fmt->ch_type,
493                             MM_CAMERA_STATE_EVT_SET_FMT, fmt);
494         }
495         break;
496     case MM_CAMERA_PARM_CHECK_AF_RETRY:
497         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_CHECK_AF_RETRY,
498                  0, (void *)NULL);
499     case MM_CAMERA_PARM_LG_CAF_LOCK:
500         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_SET_LG_CAF_LOCK,
501                  0, (void *)NULL);
502     case MM_CAMERA_PARM_INFORM_STARTPRVIEW:
503         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_SET_INFORM_STARTPREVIEW,
504                  0, (void *)NULL);
505     default:
506         rc = mm_camera_set_general_parm(my_obj, parm);
507         break;
508     }
509     return rc;
510 }
511 
mm_camera_get_parm(mm_camera_obj_t * my_obj,mm_camera_parm_t * parm)512 int32_t mm_camera_get_parm(mm_camera_obj_t * my_obj,
513                             mm_camera_parm_t *parm)
514 {
515     int32_t rc = MM_CAMERA_OK;
516 
517     switch(parm->parm_type) {
518     case MM_CAMERA_PARM_CROP:
519       return rc = mm_camera_ch_fn(my_obj,
520                     ((mm_camera_ch_crop_t *)parm->p_value)->ch_type,
521                     MM_CAMERA_STATE_EVT_GET_CROP, parm->p_value);
522       break;
523     case MM_CAMERA_PARM_DIMENSION:
524         memcpy(parm->p_value, &my_obj->dim, sizeof(my_obj->dim));
525         CDBG("%s: dw=%d,dh=%d,vw=%d,vh=%d,pw=%d,ph=%d,tw=%d,th=%d,ovx=%x,ovy=%d,opx=%d,opy=%d, m_fmt=%d, t_ftm=%d\n",
526                  __func__,
527                  my_obj->dim.display_width,my_obj->dim.display_height,
528                  my_obj->dim.video_width,my_obj->dim.video_height,
529                  my_obj->dim.picture_width,my_obj->dim.picture_height,
530                  my_obj->dim.ui_thumbnail_width,my_obj->dim.ui_thumbnail_height,
531                  my_obj->dim.orig_video_width,my_obj->dim.orig_video_height,
532                  my_obj->dim.orig_picture_width,my_obj->dim.orig_picture_height,
533                  my_obj->dim.main_img_format, my_obj->dim.thumb_format);
534         break;
535     case MM_CAMERA_PARM_MAX_PICTURE_SIZE: {
536         mm_camera_dimension_t *dim =
537             (mm_camera_dimension_t *)parm->p_value;
538         dim->height = my_obj->properties.max_pict_height;
539         dim->width = my_obj->properties.max_pict_width;
540         CDBG("%s: Max Picture Size: %d X %d\n", __func__,
541              dim->width, dim->height);
542     }
543         break;
544     case MM_CAMERA_PARM_RAW_SNAPSHOT_FMT:
545         *((cam_format_t *)parm->p_value) = my_obj->properties.pxlcode;
546         break;
547     case MM_CAMERA_PARM_PREVIEW_FORMAT:
548         *((int *)parm->p_value) = my_obj->properties.preview_format;
549         break;
550     case MM_CAMERA_PARM_PREVIEW_SIZES_CNT:
551         *((int *)parm->p_value) = my_obj->properties.preview_sizes_cnt;
552         break;
553     case MM_CAMERA_PARM_VIDEO_SIZES_CNT:
554         *((int *)parm->p_value) = my_obj->properties.video_sizes_cnt;
555         break;
556     case MM_CAMERA_PARM_THUMB_SIZES_CNT:
557         *((int *)parm->p_value) = my_obj->properties.thumb_sizes_cnt;
558         break;
559     case MM_CAMERA_PARM_HFR_SIZES_CNT:
560         *((int *)parm->p_value) = my_obj->properties.hfr_sizes_cnt;
561         break;
562     case MM_CAMERA_PARM_HFR_FRAME_SKIP:
563         *((int *)parm->p_value) = my_obj->properties.hfr_frame_skip;
564         break;
565     case MM_CAMERA_PARM_DEFAULT_PREVIEW_WIDTH:
566         *((int *)parm->p_value) = my_obj->properties.default_preview_width;
567         break;
568     case MM_CAMERA_PARM_DEFAULT_PREVIEW_HEIGHT:
569         *((int *)parm->p_value) = my_obj->properties.default_preview_height;
570         break;
571     case MM_CAMERA_PARM_BESTSHOT_RECONFIGURE:
572         *((int *)parm->p_value) = my_obj->properties.bestshot_reconfigure;
573         break;
574     case MM_CAMERA_PARM_MAX_PREVIEW_SIZE: {
575         mm_camera_dimension_t *dim =
576             (mm_camera_dimension_t *)parm->p_value;
577         dim->height = my_obj->properties.max_preview_height;
578         dim->width = my_obj->properties.max_preview_width;
579         CDBG("%s: Max Preview Size: %d X %d\n", __func__,
580              dim->width, dim->height);
581     }
582         break;
583     case MM_CAMERA_PARM_MAX_VIDEO_SIZE: {
584         mm_camera_dimension_t *dim =
585             (mm_camera_dimension_t *)parm->p_value;
586         dim->height = my_obj->properties.max_video_height;
587         dim->width = my_obj->properties.max_video_width;
588         CDBG("%s: Max Video Size: %d X %d\n", __func__,
589              dim->width, dim->height);
590     }
591         break;
592     case MM_CAMERA_PARM_MAX_HFR_MODE:
593         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_MAX_HFR_MODE,
594                 sizeof(camera_hfr_mode_t), (void *)parm->p_value);
595     case MM_CAMERA_PARM_FOCAL_LENGTH:
596         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_FOCAL_LENGTH,
597                      sizeof(focus_distances_info_t), (void *)parm->p_value);
598     case MM_CAMERA_PARM_HORIZONTAL_VIEW_ANGLE:
599         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_HORIZONTAL_VIEW_ANGLE,
600                                                      sizeof(focus_distances_info_t), (void *)parm->p_value);
601     case MM_CAMERA_PARM_VERTICAL_VIEW_ANGLE:
602         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_VERTICAL_VIEW_ANGLE,
603                                                      sizeof(focus_distances_info_t), (void *)parm->p_value);
604     case MM_CAMERA_PARM_FOCUS_DISTANCES:
605         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_FOCUS_DISTANCES,
606                      sizeof(focus_distances_info_t), (void *)parm->p_value);
607   case MM_CAMERA_PARM_QUERY_FALSH4SNAP:
608         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_QUERY_FLASH_FOR_SNAPSHOT,
609                      sizeof(int), (void *)parm->p_value);
610   case MM_CAMERA_PARM_3D_FRAME_FORMAT:
611         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_3D_FRAME_FORMAT,
612                      sizeof(camera_3d_frame_t), (void *)parm->p_value);
613     case MM_CAMERA_PARM_MAXZOOM:
614         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_MAXZOOM,
615                      sizeof(int), (void *)parm->p_value);
616     case MM_CAMERA_PARM_ZOOM_RATIO: {
617         mm_camera_zoom_tbl_t *tbl = (mm_camera_zoom_tbl_t *)parm->p_value;
618         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_ZOOMRATIOS,
619                      sizeof(int16_t)*tbl->size, tbl->zoom_ratio_tbl);
620     }
621     case MM_CAMERA_PARM_DEF_PREVIEW_SIZES: {
622         default_sizes_tbl_t *tbl = (default_sizes_tbl_t*)parm->p_value;
623         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_DEF_PREVIEW_SIZES,
624                      sizeof(struct camera_size_type)*tbl->tbl_size, tbl->sizes_tbl);
625     }
626     case MM_CAMERA_PARM_DEF_VIDEO_SIZES: {
627         default_sizes_tbl_t *tbl = (default_sizes_tbl_t*)parm->p_value;
628         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_DEF_VIDEO_SIZES,
629                      sizeof(struct camera_size_type)*tbl->tbl_size, tbl->sizes_tbl);
630     }
631     case MM_CAMERA_PARM_DEF_THUMB_SIZES: {
632         default_sizes_tbl_t *tbl = (default_sizes_tbl_t*)parm->p_value;
633         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_DEF_THUMB_SIZES,
634                      sizeof(struct camera_size_type)*tbl->tbl_size, tbl->sizes_tbl);
635     }
636     case MM_CAMERA_PARM_DEF_HFR_SIZES:{
637         default_sizes_tbl_t *tbl = (default_sizes_tbl_t*)parm->p_value;
638         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_DEF_HFR_SIZES,
639                      sizeof(struct camera_size_type)*tbl->tbl_size, tbl->sizes_tbl);
640     }
641     case MM_CAMERA_PARM_OP_MODE:
642         *((mm_camera_op_mode_type_t *)parm->p_value) = my_obj->op_mode;
643         break;
644     case MM_CAMERA_PARM_SNAPSHOT_BURST_NUM:
645         *((int *)parm->p_value) = my_obj->snap_burst_num_by_user;
646         break;
647     case MM_CAMERA_PARM_VFE_OUTPUT_ENABLE:
648         *((int *)parm->p_value) = my_obj->properties.vfe_output_enable;
649         break;
650     case MM_CAMERA_PARM_LUX_IDX:
651         CDBG("%s: MM_CAMERA_PARM_LUX_IDX\n", __func__);
652         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_PARM_LUX_IDX,
653                      sizeof(int), (void *)parm->p_value);
654     case MM_CAMERA_PARM_MAX_NUM_FACES_DECT:
655         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_MAX_NUM_FACES_DECT,
656                      sizeof(int), (void *)parm->p_value);
657     case MM_CAMERA_PARM_FACIAL_FEATURE_INFO:
658         return mm_camera_send_native_ctrl_cmd(my_obj, CAMERA_GET_FACIAL_FEATURE_INFO,
659                      sizeof(int), (void *)parm->p_value);
660     case MM_CAMERA_PARM_HDR:
661         rc = mm_camera_send_native_ctrl_cmd(my_obj,
662                   CAMERA_GET_PARM_HDR, sizeof(exp_bracketing_t), (void *)parm->p_value);
663         //my_obj->channel_interface_mask = *((exp_bracketing_t *)(parm->p_value));
664         break;
665     case MM_CAMERA_GET_PARM_LOW_LIGHT_FOR_ZSL:
666         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_LOW_LIGHT_FOR_ZSL,
667                      sizeof(aec_info_for_flash_t), (void *)parm->p_value);
668 
669     case MM_CAMERA_PARM_GET_AF_STATUS:
670         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_AF_STATUS,
671                      sizeof(af_actuator_status_t), (void *)parm->p_value);
672 
673     case MM_CAMERA_PARM_F_NUMBER:
674         return mm_camera_send_native_ctrl_cmd(my_obj,   CAMERA_GET_PARM_F_NUMBER,
675                      sizeof(float), (void *)parm->p_value);
676     default:
677         /* needs to add more implementation */
678         rc = -1;
679         break;
680     }
681     return rc;
682 }
683 
mm_camera_request_buf(mm_camera_obj_t * my_obj,mm_camera_reg_buf_t * buf)684 int32_t mm_camera_request_buf(mm_camera_obj_t * my_obj, mm_camera_reg_buf_t *buf)
685 {
686     int32_t rc = -MM_CAMERA_E_GENERAL;
687     rc = mm_camera_ch_fn(my_obj,    buf->ch_type,
688                     MM_CAMERA_STATE_EVT_REQUEST_BUF, (void *)&buf->preview);
689     return rc;
690 }
691 
mm_camera_prepare_buf(mm_camera_obj_t * my_obj,mm_camera_reg_buf_t * buf)692 int32_t mm_camera_prepare_buf(mm_camera_obj_t * my_obj, mm_camera_reg_buf_t *buf)
693 {
694     int32_t rc = -MM_CAMERA_E_GENERAL;
695     rc = mm_camera_ch_fn(my_obj,    buf->ch_type,
696                     MM_CAMERA_STATE_EVT_REG_BUF, (void *)&buf->preview);
697     return rc;
698 }
mm_camera_unprepare_buf(mm_camera_obj_t * my_obj,mm_camera_channel_type_t ch_type)699 int32_t mm_camera_unprepare_buf(mm_camera_obj_t * my_obj, mm_camera_channel_type_t ch_type)
700 {
701     int32_t rc = -MM_CAMERA_E_GENERAL;
702     pthread_mutex_lock(&my_obj->ch[ch_type].mutex);
703     rc = mm_camera_ch_fn(my_obj, ch_type,
704                     MM_CAMERA_STATE_EVT_UNREG_BUF, NULL);
705     pthread_mutex_unlock(&my_obj->ch[ch_type].mutex);
706     return rc;
707 }
708 
mm_camera_evt_sub(mm_camera_obj_t * my_obj,mm_camera_event_type_t evt_type,int reg_count)709 static int mm_camera_evt_sub(mm_camera_obj_t * my_obj,
710                              mm_camera_event_type_t evt_type, int reg_count)
711 {
712     int rc = MM_CAMERA_OK;
713     struct v4l2_event_subscription sub;
714 
715     memset(&sub, 0, sizeof(sub));
716     sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_EVENT;
717     if(reg_count == 0) {
718         /* unsubscribe */
719         if(my_obj->evt_type_mask == (uint32_t)(1 << evt_type)) {
720             rc = ioctl(my_obj->ctrl_fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub);
721             CDBG("%s: unsubscribe event 0x%x, rc = %d", __func__, sub.type, rc);
722             sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_ERROR_EVENT;
723             rc = ioctl(my_obj->ctrl_fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub);
724             CDBG("%s: unsubscribe event 0x%x, rc = %d", __func__, sub.type, rc);
725         }
726         my_obj->evt_type_mask &= ~(1 << evt_type);
727         if(my_obj->evt_type_mask == 0) {
728             /* kill the polling thraed when unreg the last event */
729             mm_camera_poll_thread_release(my_obj, MM_CAMERA_CH_MAX);
730         }
731     } else {
732         if(!my_obj->evt_type_mask) {
733             /* this is the first reg event */
734             rc = ioctl(my_obj->ctrl_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
735             CDBG("%s: subscribe event 0x%x, rc = %d", __func__, sub.type, rc);
736             if (rc < 0)
737                 goto end;
738             sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_ERROR_EVENT;
739             rc = ioctl(my_obj->ctrl_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
740             CDBG("%s: subscribe event 0x%x, rc = %d", __func__, sub.type, rc);
741             if (rc < 0)
742                 goto end;
743         }
744         my_obj->evt_type_mask |= (1 << evt_type);
745         if(my_obj->evt_type_mask == (uint32_t)(1 << evt_type)) {
746             /* launch event polling when subscribe the first event */
747             rc = mm_camera_poll_thread_launch(my_obj, MM_CAMERA_CH_MAX);
748         }
749     }
750 end:
751     return rc;
752 }
753 
mm_camera_reg_event(mm_camera_obj_t * my_obj,mm_camera_event_notify_t evt_cb,void * user_data,mm_camera_event_type_t evt_type)754 int mm_camera_reg_event(mm_camera_obj_t * my_obj, mm_camera_event_notify_t evt_cb,
755                            void *user_data, mm_camera_event_type_t evt_type)
756 {
757     int i;
758     int rc = -1;
759     mm_camera_evt_obj_t *evt_array = &my_obj->evt[evt_type];
760     if(evt_cb) {
761         /* this is reg case */
762         for(i = 0; i < MM_CAMERA_EVT_ENTRY_MAX; i++) {
763             if(evt_array->evt[i].user_data == NULL) {
764                 evt_array->evt[i].evt_cb = evt_cb;
765                 evt_array->evt[i].user_data = user_data;
766                 evt_array->reg_count++;
767                 rc = MM_CAMERA_OK;
768                 break;
769             }
770         }
771     } else {
772         /* this is unreg case */
773         for(i = 0; i < MM_CAMERA_EVT_ENTRY_MAX; i++) {
774             if(evt_array->evt[i].user_data == user_data) {
775                 evt_array->evt[i].evt_cb = NULL;
776                 evt_array->evt[i].user_data = NULL;
777                 evt_array->reg_count--;
778                 rc = MM_CAMERA_OK;
779                 break;
780             }
781         }
782     }
783     if(rc == MM_CAMERA_OK && evt_array->reg_count <= 1) {
784         /* subscribe/unsubscribe event to kernel */
785         rc = mm_camera_evt_sub(my_obj, evt_type, evt_array->reg_count);
786     }
787     return rc;
788 }
789 
790 
mm_camera_send_af_failed_event(mm_camera_obj_t * my_obj)791 static int32_t mm_camera_send_af_failed_event(mm_camera_obj_t *my_obj)
792 {
793     int rc = 0;
794     mm_camera_event_t event;
795     event.event_type = MM_CAMERA_EVT_TYPE_CTRL;
796     event.e.ctrl.evt= MM_CAMERA_CTRL_EVT_AUTO_FOCUS_DONE;
797     event.e.ctrl.status=CAM_CTRL_FAILED;
798     CDBG_HIGH("%s: Issuing call",__func__);
799     rc = mm_camera_poll_send_ch_event(my_obj, &event);
800     return rc;
801 }
802 
mm_camera_send_ch_on_off_event(mm_camera_obj_t * my_obj,mm_camera_channel_type_t ch_type,mm_camera_ch_event_type_t evt)803 static int32_t mm_camera_send_ch_on_off_event(mm_camera_obj_t *my_obj,
804                                        mm_camera_channel_type_t ch_type,
805                                        mm_camera_ch_event_type_t evt)
806 {
807     int rc = 0;
808     mm_camera_event_t event;
809     event.event_type = MM_CAMERA_EVT_TYPE_CH;
810     event.e.ch.evt = evt;
811     event.e.ch.ch = ch_type;
812     CDBG("%s: stream on event, type=0x%x, ch=%d, evt=%d",
813          __func__, event.event_type, event.e.ch.ch, event.e.ch.evt);
814     rc = mm_camera_poll_send_ch_event(my_obj, &event);
815     return rc;
816 }
817 
mm_camera_action_start(mm_camera_obj_t * my_obj,mm_camera_ops_type_t opcode,void * parm)818 int32_t mm_camera_action_start(mm_camera_obj_t *my_obj,
819                             mm_camera_ops_type_t opcode, void *parm)
820 {
821     int32_t rc = -MM_CAMERA_E_GENERAL;
822     int send_on_off_evt = 1;
823     mm_camera_channel_type_t ch_type;
824     switch(opcode) {
825     case MM_CAMERA_OPS_FOCUS: {
826         if(!parm) return rc;
827         if(0 > mm_camera_send_native_ctrl_cmd(my_obj,
828           CAMERA_SET_PARM_AUTO_FOCUS,
829           sizeof(isp3a_af_mode_t), parm))
830           mm_camera_send_af_failed_event(my_obj);
831         return MM_CAMERA_OK;
832     }
833     case MM_CAMERA_OPS_GET_BUFFERED_FRAME: {
834         mm_camera_ops_parm_get_buffered_frame_t *tmp =
835             (mm_camera_ops_parm_get_buffered_frame_t *)parm;
836         rc = mm_camera_ch_fn(my_obj, tmp->ch_type,
837                  MM_CAMERA_STATE_EVT_DISPATCH_BUFFERED_FRAME, NULL);
838         return rc;
839     }
840     default:
841         break;
842     }
843     ch_type = mm_camera_util_opcode_2_ch_type(my_obj, opcode);
844     CDBG("%s:ch=%d,op_mode=%d,opcode=%d\n",
845         __func__,ch_type,my_obj->op_mode,opcode);
846     switch(my_obj->op_mode) {
847     case MM_CAMERA_OP_MODE_ZSL:
848     case MM_CAMERA_OP_MODE_CAPTURE:
849         switch(opcode) {
850         case MM_CAMERA_OPS_PREVIEW:
851         case MM_CAMERA_OPS_SNAPSHOT:
852         case MM_CAMERA_OPS_ZSL:
853         case MM_CAMERA_OPS_RAW:
854             rc = mm_camera_ch_fn(my_obj, ch_type,
855                     MM_CAMERA_STATE_EVT_STREAM_ON, NULL);
856             break;
857         default:
858             break;
859         }
860         break;
861     case MM_CAMERA_OP_MODE_VIDEO:
862         switch(opcode) {
863         case MM_CAMERA_OPS_PREVIEW:
864         case MM_CAMERA_OPS_VIDEO:
865         case MM_CAMERA_OPS_SNAPSHOT:
866             rc = mm_camera_ch_fn(my_obj,    ch_type,
867                     MM_CAMERA_STATE_EVT_STREAM_ON, NULL);
868             CDBG("%s: op_mode=%d, ch %d, rc=%d\n",
869                 __func__, MM_CAMERA_OP_MODE_VIDEO, ch_type ,rc);
870             break;
871         case MM_CAMERA_OPS_PREPARE_SNAPSHOT:
872             send_on_off_evt = 0;
873             rc = mm_camera_send_native_ctrl_timeout_cmd(my_obj,CAMERA_PREPARE_SNAPSHOT, 0, NULL, 2000);
874             CDBG("%s: prepare snapshot done opcode = %d, rc= %d\n", __func__, opcode, rc);
875             break;
876         default:
877             break;
878         }
879         break;
880     default:
881         break;
882     }
883     CDBG("%s: ch=%d,op_mode=%d,opcode=%d\n", __func__, ch_type,
884       my_obj->op_mode, opcode);
885     if(send_on_off_evt)
886       rc = mm_camera_send_ch_on_off_event(my_obj,ch_type,MM_CAMERA_CH_EVT_STREAMING_ON);
887     return rc;
888 }
889 
mm_camera_action_stop(mm_camera_obj_t * my_obj,mm_camera_ops_type_t opcode,void * parm)890 int32_t mm_camera_action_stop(mm_camera_obj_t *my_obj,
891     mm_camera_ops_type_t opcode, void *parm)
892 {
893     int32_t rc = -MM_CAMERA_E_GENERAL;
894     mm_camera_channel_type_t ch_type;
895 
896     if(opcode == MM_CAMERA_OPS_FOCUS) {
897       return mm_camera_send_native_ctrl_cmd(my_obj,
898                                             CAMERA_AUTO_FOCUS_CANCEL, 0, NULL);
899     }
900 
901     ch_type = mm_camera_util_opcode_2_ch_type(my_obj, opcode);
902     switch(my_obj->op_mode) {
903     case MM_CAMERA_OP_MODE_ZSL:
904     case MM_CAMERA_OP_MODE_CAPTURE:
905         switch(opcode) {
906         case MM_CAMERA_OPS_PREVIEW:
907         case MM_CAMERA_OPS_SNAPSHOT:
908         case MM_CAMERA_OPS_ZSL:
909         case MM_CAMERA_OPS_RAW:
910             rc = mm_camera_ch_fn(my_obj, ch_type,
911                             MM_CAMERA_STATE_EVT_STREAM_OFF, NULL);
912             CDBG("%s:CAPTURE mode STREAMOFF rc=%d\n",__func__, rc);
913             break;
914         default:
915             break;
916         }
917         break;
918     case MM_CAMERA_OP_MODE_VIDEO:
919         switch(opcode) {
920         case MM_CAMERA_OPS_PREVIEW:
921         case MM_CAMERA_OPS_VIDEO:
922         case MM_CAMERA_OPS_SNAPSHOT:
923             rc = mm_camera_ch_fn(my_obj , ch_type,
924                             MM_CAMERA_STATE_EVT_STREAM_OFF, NULL);
925             CDBG("%s:VIDEO mode STREAMOFF rc=%d\n",__func__, rc);
926             break;
927         default:
928             break;
929         }
930         break;
931     default:
932         break;
933     }
934     CDBG("%s:ch=%d\n",__func__, ch_type);
935     rc = mm_camera_send_ch_on_off_event(my_obj,ch_type,MM_CAMERA_CH_EVT_STREAMING_OFF);
936     return rc;
937 }
938 
mm_camera_init_ch_stream_count(mm_camera_obj_t * my_obj)939 static void mm_camera_init_ch_stream_count(mm_camera_obj_t *my_obj)
940 {
941     int i;
942 
943     for(i = 0; i < MM_CAMERA_CH_MAX; i++) {
944         if(i == MM_CAMERA_CH_SNAPSHOT) {
945             my_obj->ch_stream_count[i].stream_on_count_cfg = 2;
946             my_obj->ch_stream_count[i].stream_off_count_cfg = 2;
947         } else {
948             my_obj->ch_stream_count[i].stream_on_count_cfg = 1;
949             my_obj->ch_stream_count[i].stream_off_count_cfg = 1;
950         }
951     }
952 }
953 
mm_camera_open(mm_camera_obj_t * my_obj,mm_camera_op_mode_type_t op_mode)954 int32_t mm_camera_open(mm_camera_obj_t *my_obj,
955                     mm_camera_op_mode_type_t op_mode)
956 {
957     char dev_name[MM_CAMERA_DEV_NAME_LEN];
958     int32_t rc = MM_CAMERA_OK;
959     int8_t n_try=MM_CAMERA_DEV_OPEN_TRIES;
960     uint8_t sleep_msec=MM_CAMERA_DEV_OPEN_RETRY_SLEEP;
961     uint8_t i;
962 
963 	CDBG("%s:  begin\n", __func__);
964 
965     if(my_obj->op_mode != MM_CAMERA_OP_MODE_NOTUSED) {
966         CDBG("%s: not allowed in existing op mode %d\n",
967                  __func__, my_obj->op_mode);
968         return -MM_CAMERA_E_INVALID_OPERATION;
969     }
970     if(op_mode >= MM_CAMERA_OP_MODE_MAX) {
971         CDBG("%s: invalid input %d\n",
972                  __func__, op_mode);
973         return -MM_CAMERA_E_INVALID_INPUT;
974     }
975     snprintf(dev_name, sizeof(dev_name), "/dev/%s", mm_camera_util_get_dev_name(my_obj));
976     //rc = mm_camera_dev_open(&my_obj->ctrl_fd, dev_name);
977 	CDBG("%s: mm_camera_dev_open rc = %d\n", __func__, rc);
978 
979     do{
980         n_try--;
981         my_obj->ctrl_fd = open(dev_name,O_RDWR | O_NONBLOCK);
982 		ALOGV("%s:  ctrl_fd = %d", __func__, my_obj->ctrl_fd);
983         ALOGV("Errno:%d",errno);
984         if((my_obj->ctrl_fd > 0) || (errno != EIO) || (n_try <= 0 )) {
985 			ALOGV("%s:  opened, break out while loop", __func__);
986 
987             break;
988 		}
989         CDBG("%s:failed with I/O error retrying after %d milli-seconds",
990              __func__,sleep_msec);
991         usleep(sleep_msec*1000);
992     }while(n_try>0);
993 
994 	ALOGV("%s:  after while loop", __func__);
995     if (my_obj->ctrl_fd <= 0) {
996         CDBG("%s: cannot open control fd of '%s' Errno = %d\n",
997                  __func__, mm_camera_util_get_dev_name(my_obj),errno);
998         return -MM_CAMERA_E_GENERAL;
999     }
1000 	ALOGV("%s:  2\n", __func__);
1001 
1002     /* open domain socket*/
1003     n_try=MM_CAMERA_DEV_OPEN_TRIES;
1004     do{
1005         n_try--;
1006         my_obj->ds_fd = mm_camera_socket_create(my_obj->my_id, MM_CAMERA_SOCK_TYPE_UDP); // TODO: UDP for now, change to TCP
1007         ALOGV("%s:  ds_fd = %d", __func__, my_obj->ds_fd);
1008         ALOGV("Errno:%d",errno);
1009         if((my_obj->ds_fd > 0) || (n_try <= 0 )) {
1010             ALOGV("%s:  opened, break out while loop", __func__);
1011             break;
1012         }
1013         CDBG("%s:failed with I/O error retrying after %d milli-seconds",
1014              __func__,sleep_msec);
1015         usleep(sleep_msec*1000);
1016     }while(n_try>0);
1017 
1018     ALOGV("%s:  after while loop for domain socket open", __func__);
1019     if (my_obj->ds_fd <= 0) {
1020         CDBG_ERROR("%s: cannot open domain socket fd of '%s' Errno = %d\n",
1021                  __func__, mm_camera_util_get_dev_name(my_obj),errno);
1022         close(my_obj->ctrl_fd);
1023         my_obj->ctrl_fd = -1;
1024         return -MM_CAMERA_E_GENERAL;
1025     }
1026 
1027     /* set ctrl_fd to be the mem_mapping fd */
1028     rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
1029                         MSM_V4L2_PID_MMAP_INST, 0);
1030     if (rc != MM_CAMERA_OK) {
1031         CDBG_ERROR("error: ioctl VIDIOC_S_CTRL MSM_V4L2_PID_MMAP_INST failed: %s\n",
1032         strerror(errno));
1033         close(my_obj->ctrl_fd);
1034         close(my_obj->ds_fd);
1035         my_obj->ctrl_fd = -1;
1036         my_obj->ds_fd = -1;
1037         return -MM_CAMERA_E_GENERAL;
1038     }
1039     if(op_mode != MM_CAMERA_OP_MODE_NOTUSED)
1040         rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
1041                             MSM_V4L2_PID_CAM_MODE, op_mode);
1042     if(!rc) {
1043         my_obj->op_mode = op_mode;
1044         my_obj->current_mode = CAMERA_MODE_2D; /* set geo mode to 2D by default */
1045     }
1046 
1047     /* get camera capabilities */
1048     memset(&my_obj->properties, 0, sizeof(cam_prop_t));
1049     rc = mm_camera_send_native_ctrl_cmd(my_obj,
1050                                         CAMERA_GET_CAPABILITIES,
1051                                         sizeof(cam_prop_t),
1052                                         (void *)& my_obj->properties);
1053     if (rc != MM_CAMERA_OK) {
1054         CDBG_ERROR("%s: cannot get camera capabilities\n", __func__);
1055         close(my_obj->ctrl_fd);
1056         close(my_obj->ds_fd);
1057         my_obj->ctrl_fd = -1;
1058         my_obj->ds_fd = -1;
1059         return -MM_CAMERA_E_GENERAL;
1060     }
1061 
1062     mm_camera_poll_threads_init(my_obj);
1063     mm_camera_init_ch_stream_count(my_obj);
1064     CDBG("%s : Launch Threads in Cam Open",__func__);
1065     for(i = 0; i < MM_CAMERA_CH_MAX; i++) {
1066         mm_camera_poll_thread_launch(my_obj,(mm_camera_channel_type_t)i);
1067     }
1068     CDBG("%s: '%s', ctrl_fd=%d,op_mode=%d,rc=%d\n",
1069              __func__, dev_name, my_obj->ctrl_fd, my_obj->op_mode, rc);
1070     return rc;
1071 }
1072 
mm_camera_close(mm_camera_obj_t * my_obj)1073 int32_t mm_camera_close(mm_camera_obj_t *my_obj)
1074 {
1075     int i, rc = 0;
1076 
1077     for(i = 0; i < MM_CAMERA_CH_MAX; i++){
1078         mm_camera_ch_fn(my_obj, (mm_camera_channel_type_t)i,
1079                                 MM_CAMERA_STATE_EVT_RELEASE, NULL);
1080     }
1081 
1082     my_obj->op_mode = MM_CAMERA_OP_MODE_NOTUSED;
1083     if(my_obj->ctrl_fd > 0) {
1084         rc = close(my_obj->ctrl_fd);
1085         if(rc < 0) {
1086             /* this is a dead end. */
1087             CDBG("%s: !!!!FATAL ERROR!!!! ctrl_fd = %d, rc = %d",
1088                  __func__, my_obj->ctrl_fd, rc);
1089         }
1090         my_obj->ctrl_fd = 0;
1091     }
1092     if(my_obj->ds_fd > 0) {
1093         mm_camera_socket_close(my_obj->ds_fd);
1094         my_obj->ds_fd = 0;
1095     }
1096     return MM_CAMERA_OK;
1097 }
1098 
mm_camera_action(mm_camera_obj_t * my_obj,uint8_t start,mm_camera_ops_type_t opcode,void * parm)1099 int32_t mm_camera_action(mm_camera_obj_t *my_obj, uint8_t start,
1100                         mm_camera_ops_type_t opcode, void *parm)
1101 {
1102     int32_t rc = - MM_CAMERA_E_INVALID_OPERATION;
1103 
1104     if(start)   rc = mm_camera_action_start(my_obj, opcode, parm);
1105     else rc = mm_camera_action_stop(my_obj, opcode, parm);
1106     CDBG("%s:start_flag=%d,opcode=%d,parm=%p,rc=%d\n",__func__,start,opcode,parm, rc);
1107     return rc;
1108 }
1109 
mm_camera_ch_acquire(mm_camera_obj_t * my_obj,mm_camera_channel_type_t ch_type)1110 int32_t mm_camera_ch_acquire(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type)
1111 {
1112     return mm_camera_ch_fn(my_obj,ch_type, MM_CAMERA_STATE_EVT_ACQUIRE, 0);
1113 }
1114 
mm_camera_ch_release(mm_camera_obj_t * my_obj,mm_camera_channel_type_t ch_type)1115 void mm_camera_ch_release(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type)
1116 {
1117     mm_camera_ch_fn(my_obj,ch_type, MM_CAMERA_STATE_EVT_RELEASE, 0);
1118 }
1119 
mm_camera_sendmsg(mm_camera_obj_t * my_obj,void * msg,uint32_t buf_size,int sendfd)1120 int32_t mm_camera_sendmsg(mm_camera_obj_t *my_obj, void *msg, uint32_t buf_size, int sendfd)
1121 {
1122     return mm_camera_socket_sendmsg(my_obj->ds_fd, msg, buf_size, sendfd);
1123 }
1124