Home | History | Annotate | Download | only in mm-camera-interface
      1 /*
      2 Copyright (c) 2011-2012, 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);
     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 
     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 
     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 
     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 
    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 
    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 
    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 
    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 }
    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 
    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 
    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 
    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 
    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 
    692 int32_t mm_camera_enqueue_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_ENQUEUE_BUF, (void *)&buf->preview);
    697     return rc;
    698 }
    699 
    700 int32_t mm_camera_prepare_buf(mm_camera_obj_t * my_obj, mm_camera_reg_buf_t *buf)
    701 {
    702     int32_t rc = -MM_CAMERA_E_GENERAL;
    703     rc = mm_camera_ch_fn(my_obj,    buf->ch_type,
    704                     MM_CAMERA_STATE_EVT_REG_BUF, (void *)&buf->preview);
    705     return rc;
    706 }
    707 int32_t mm_camera_unprepare_buf(mm_camera_obj_t * my_obj, mm_camera_channel_type_t ch_type)
    708 {
    709     int32_t rc = -MM_CAMERA_E_GENERAL;
    710     pthread_mutex_lock(&my_obj->ch[ch_type].mutex);
    711     rc = mm_camera_ch_fn(my_obj, ch_type,
    712                     MM_CAMERA_STATE_EVT_UNREG_BUF, NULL);
    713     pthread_mutex_unlock(&my_obj->ch[ch_type].mutex);
    714     return rc;
    715 }
    716 
    717 static int mm_camera_evt_sub(mm_camera_obj_t * my_obj,
    718                              mm_camera_event_type_t evt_type, int reg_count)
    719 {
    720     int rc = MM_CAMERA_OK;
    721     struct v4l2_event_subscription sub;
    722 
    723     memset(&sub, 0, sizeof(sub));
    724     sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_EVENT;
    725     if(reg_count == 0) {
    726         /* unsubscribe */
    727         if(my_obj->evt_type_mask == (uint32_t)(1 << evt_type)) {
    728             rc = ioctl(my_obj->ctrl_fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub);
    729             CDBG("%s: unsubscribe event 0x%x, rc = %d", __func__, sub.type, rc);
    730             sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_ERROR_EVENT;
    731             rc = ioctl(my_obj->ctrl_fd, VIDIOC_UNSUBSCRIBE_EVENT, &sub);
    732             CDBG("%s: unsubscribe event 0x%x, rc = %d", __func__, sub.type, rc);
    733         }
    734         my_obj->evt_type_mask &= ~(1 << evt_type);
    735         if(my_obj->evt_type_mask == 0) {
    736             /* kill the polling thraed when unreg the last event */
    737             mm_camera_poll_thread_release(my_obj, MM_CAMERA_CH_MAX);
    738         }
    739     } else {
    740         if(!my_obj->evt_type_mask) {
    741             /* this is the first reg event */
    742             rc = ioctl(my_obj->ctrl_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
    743             CDBG("%s: subscribe event 0x%x, rc = %d", __func__, sub.type, rc);
    744             if (rc < 0)
    745                 goto end;
    746             sub.type = V4L2_EVENT_PRIVATE_START+MSM_CAM_APP_NOTIFY_ERROR_EVENT;
    747             rc = ioctl(my_obj->ctrl_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
    748             CDBG("%s: subscribe event 0x%x, rc = %d", __func__, sub.type, rc);
    749             if (rc < 0)
    750                 goto end;
    751         }
    752         my_obj->evt_type_mask |= (1 << evt_type);
    753         if(my_obj->evt_type_mask == (uint32_t)(1 << evt_type)) {
    754             /* launch event polling when subscribe the first event */
    755             rc = mm_camera_poll_thread_launch(my_obj, MM_CAMERA_CH_MAX);
    756         }
    757     }
    758 end:
    759     return rc;
    760 }
    761 
    762 int mm_camera_reg_event(mm_camera_obj_t * my_obj, mm_camera_event_notify_t evt_cb,
    763                            void *user_data, mm_camera_event_type_t evt_type)
    764 {
    765     int i;
    766     int rc = -1;
    767     mm_camera_evt_obj_t *evt_array = &my_obj->evt[evt_type];
    768     if(evt_cb) {
    769         /* this is reg case */
    770         for(i = 0; i < MM_CAMERA_EVT_ENTRY_MAX; i++) {
    771             if(evt_array->evt[i].user_data == NULL) {
    772                 evt_array->evt[i].evt_cb = evt_cb;
    773                 evt_array->evt[i].user_data = user_data;
    774                 evt_array->reg_count++;
    775                 rc = MM_CAMERA_OK;
    776                 break;
    777             }
    778         }
    779     } else {
    780         /* this is unreg case */
    781         for(i = 0; i < MM_CAMERA_EVT_ENTRY_MAX; i++) {
    782             if(evt_array->evt[i].user_data == user_data) {
    783                 evt_array->evt[i].evt_cb = NULL;
    784                 evt_array->evt[i].user_data = NULL;
    785                 evt_array->reg_count--;
    786                 rc = MM_CAMERA_OK;
    787                 break;
    788             }
    789         }
    790     }
    791     if(rc == MM_CAMERA_OK && evt_array->reg_count <= 1) {
    792         /* subscribe/unsubscribe event to kernel */
    793         rc = mm_camera_evt_sub(my_obj, evt_type, evt_array->reg_count);
    794     }
    795     return rc;
    796 }
    797 
    798 
    799 static int32_t mm_camera_send_af_failed_event(mm_camera_obj_t *my_obj)
    800 {
    801     int rc = 0;
    802     mm_camera_event_t event;
    803     event.event_type = MM_CAMERA_EVT_TYPE_CTRL;
    804     event.e.ctrl.evt= MM_CAMERA_CTRL_EVT_AUTO_FOCUS_DONE;
    805     event.e.ctrl.status=CAM_CTRL_FAILED;
    806     CDBG_HIGH("%s: Issuing call",__func__);
    807     rc = mm_camera_poll_send_ch_event(my_obj, &event);
    808     return rc;
    809 }
    810 
    811 static int32_t mm_camera_send_ch_on_off_event(mm_camera_obj_t *my_obj,
    812                                        mm_camera_channel_type_t ch_type,
    813                                        mm_camera_ch_event_type_t evt)
    814 {
    815     int rc = 0;
    816     mm_camera_event_t event;
    817     event.event_type = MM_CAMERA_EVT_TYPE_CH;
    818     event.e.ch.evt = evt;
    819     event.e.ch.ch = ch_type;
    820     CDBG("%s: stream on event, type=0x%x, ch=%d, evt=%d",
    821          __func__, event.event_type, event.e.ch.ch, event.e.ch.evt);
    822     rc = mm_camera_poll_send_ch_event(my_obj, &event);
    823     return rc;
    824 }
    825 
    826 int32_t mm_camera_action_start(mm_camera_obj_t *my_obj,
    827                             mm_camera_ops_type_t opcode, void *parm)
    828 {
    829     int32_t rc = -MM_CAMERA_E_GENERAL;
    830     int send_on_off_evt = 1;
    831     mm_camera_channel_type_t ch_type;
    832     switch(opcode) {
    833     case MM_CAMERA_OPS_FOCUS: {
    834         if(!parm) return rc;
    835         if(0 > mm_camera_send_native_ctrl_cmd(my_obj,
    836           CAMERA_SET_PARM_AUTO_FOCUS,
    837           sizeof(isp3a_af_mode_t), parm))
    838           mm_camera_send_af_failed_event(my_obj);
    839         return MM_CAMERA_OK;
    840     }
    841     case MM_CAMERA_OPS_GET_BUFFERED_FRAME: {
    842         mm_camera_ops_parm_get_buffered_frame_t *tmp =
    843             (mm_camera_ops_parm_get_buffered_frame_t *)parm;
    844         rc = mm_camera_ch_fn(my_obj, tmp->ch_type,
    845                  MM_CAMERA_STATE_EVT_DISPATCH_BUFFERED_FRAME, NULL);
    846         return rc;
    847     }
    848     default:
    849         break;
    850     }
    851     ch_type = mm_camera_util_opcode_2_ch_type(my_obj, opcode);
    852     CDBG("%s:ch=%d,op_mode=%d,opcode=%d\n",
    853         __func__,ch_type,my_obj->op_mode,opcode);
    854     switch(my_obj->op_mode) {
    855     case MM_CAMERA_OP_MODE_ZSL:
    856     case MM_CAMERA_OP_MODE_CAPTURE:
    857         switch(opcode) {
    858         case MM_CAMERA_OPS_PREVIEW:
    859         case MM_CAMERA_OPS_SNAPSHOT:
    860         case MM_CAMERA_OPS_ZSL:
    861         case MM_CAMERA_OPS_RAW:
    862             rc = mm_camera_ch_fn(my_obj, ch_type,
    863                     MM_CAMERA_STATE_EVT_STREAM_ON, NULL);
    864             break;
    865         default:
    866             break;
    867         }
    868         break;
    869     case MM_CAMERA_OP_MODE_VIDEO:
    870         switch(opcode) {
    871         case MM_CAMERA_OPS_PREVIEW:
    872         case MM_CAMERA_OPS_VIDEO:
    873         case MM_CAMERA_OPS_SNAPSHOT:
    874             rc = mm_camera_ch_fn(my_obj,    ch_type,
    875                     MM_CAMERA_STATE_EVT_STREAM_ON, NULL);
    876             CDBG("%s: op_mode=%d, ch %d, rc=%d\n",
    877                 __func__, MM_CAMERA_OP_MODE_VIDEO, ch_type ,rc);
    878             break;
    879         case MM_CAMERA_OPS_PREPARE_SNAPSHOT:
    880             send_on_off_evt = 0;
    881             rc = mm_camera_send_native_ctrl_timeout_cmd(my_obj,CAMERA_PREPARE_SNAPSHOT, 0, NULL, 2000);
    882             CDBG("%s: prepare snapshot done opcode = %d, rc= %d\n", __func__, opcode, rc);
    883             break;
    884         default:
    885             break;
    886         }
    887         break;
    888     default:
    889         break;
    890     }
    891     CDBG("%s: ch=%d,op_mode=%d,opcode=%d\n", __func__, ch_type,
    892       my_obj->op_mode, opcode);
    893     if(send_on_off_evt)
    894       rc = mm_camera_send_ch_on_off_event(my_obj,ch_type,MM_CAMERA_CH_EVT_STREAMING_ON);
    895     return rc;
    896 }
    897 
    898 int32_t mm_camera_action_stop(mm_camera_obj_t *my_obj,
    899     mm_camera_ops_type_t opcode, void *parm)
    900 {
    901     int32_t rc = -MM_CAMERA_E_GENERAL;
    902     mm_camera_channel_type_t ch_type;
    903 
    904     if(opcode == MM_CAMERA_OPS_FOCUS) {
    905       return mm_camera_send_native_ctrl_cmd(my_obj,
    906                                             CAMERA_AUTO_FOCUS_CANCEL, 0, NULL);
    907     }
    908 
    909     ch_type = mm_camera_util_opcode_2_ch_type(my_obj, opcode);
    910     switch(my_obj->op_mode) {
    911     case MM_CAMERA_OP_MODE_ZSL:
    912     case MM_CAMERA_OP_MODE_CAPTURE:
    913         switch(opcode) {
    914         case MM_CAMERA_OPS_PREVIEW:
    915         case MM_CAMERA_OPS_SNAPSHOT:
    916         case MM_CAMERA_OPS_ZSL:
    917         case MM_CAMERA_OPS_RAW:
    918             rc = mm_camera_ch_fn(my_obj, ch_type,
    919                             MM_CAMERA_STATE_EVT_STREAM_OFF, NULL);
    920             CDBG("%s:CAPTURE mode STREAMOFF rc=%d\n",__func__, rc);
    921             break;
    922         default:
    923             break;
    924         }
    925         break;
    926     case MM_CAMERA_OP_MODE_VIDEO:
    927         switch(opcode) {
    928         case MM_CAMERA_OPS_PREVIEW:
    929         case MM_CAMERA_OPS_VIDEO:
    930         case MM_CAMERA_OPS_SNAPSHOT:
    931             rc = mm_camera_ch_fn(my_obj , ch_type,
    932                             MM_CAMERA_STATE_EVT_STREAM_OFF, NULL);
    933             CDBG("%s:VIDEO mode STREAMOFF rc=%d\n",__func__, rc);
    934             break;
    935         default:
    936             break;
    937         }
    938         break;
    939     default:
    940         break;
    941     }
    942     CDBG("%s:ch=%d\n",__func__, ch_type);
    943     rc = mm_camera_send_ch_on_off_event(my_obj,ch_type,MM_CAMERA_CH_EVT_STREAMING_OFF);
    944     return rc;
    945 }
    946 
    947 static void mm_camera_init_ch_stream_count(mm_camera_obj_t *my_obj)
    948 {
    949     int i;
    950 
    951     for(i = 0; i < MM_CAMERA_CH_MAX; i++) {
    952         if(i == MM_CAMERA_CH_SNAPSHOT) {
    953             my_obj->ch_stream_count[i].stream_on_count_cfg = 2;
    954             my_obj->ch_stream_count[i].stream_off_count_cfg = 2;
    955         } else {
    956             my_obj->ch_stream_count[i].stream_on_count_cfg = 1;
    957             my_obj->ch_stream_count[i].stream_off_count_cfg = 1;
    958         }
    959     }
    960 }
    961 
    962 int32_t mm_camera_open(mm_camera_obj_t *my_obj,
    963                     mm_camera_op_mode_type_t op_mode)
    964 {
    965     char dev_name[MM_CAMERA_DEV_NAME_LEN];
    966     int32_t rc = MM_CAMERA_OK;
    967     int8_t n_try=MM_CAMERA_DEV_OPEN_TRIES;
    968     uint8_t sleep_msec=MM_CAMERA_DEV_OPEN_RETRY_SLEEP;
    969     uint8_t i;
    970 
    971 	CDBG("%s:  begin\n", __func__);
    972 
    973     if(my_obj->op_mode != MM_CAMERA_OP_MODE_NOTUSED) {
    974         CDBG("%s: not allowed in existing op mode %d\n",
    975                  __func__, my_obj->op_mode);
    976         return -MM_CAMERA_E_INVALID_OPERATION;
    977     }
    978     if(op_mode >= MM_CAMERA_OP_MODE_MAX) {
    979         CDBG("%s: invalid input %d\n",
    980                  __func__, op_mode);
    981         return -MM_CAMERA_E_INVALID_INPUT;
    982     }
    983     snprintf(dev_name, sizeof(dev_name), "/dev/%s", mm_camera_util_get_dev_name(my_obj));
    984     //rc = mm_camera_dev_open(&my_obj->ctrl_fd, dev_name);
    985 	CDBG("%s: mm_camera_dev_open rc = %d\n", __func__, rc);
    986 
    987     do{
    988         n_try--;
    989         my_obj->ctrl_fd = open(dev_name,O_RDWR | O_NONBLOCK);
    990 		ALOGV("%s:  ctrl_fd = %d", __func__, my_obj->ctrl_fd);
    991         ALOGV("Errno:%d",errno);
    992         if((my_obj->ctrl_fd > 0) || (errno != EIO) || (n_try <= 0 )) {
    993 			ALOGV("%s:  opened, break out while loop", __func__);
    994 
    995             break;
    996 		}
    997         CDBG("%s:failed with I/O error retrying after %d milli-seconds",
    998              __func__,sleep_msec);
    999         usleep(sleep_msec*1000);
   1000     }while(n_try>0);
   1001 
   1002 	ALOGV("%s:  after while loop", __func__);
   1003     if (my_obj->ctrl_fd <= 0) {
   1004         CDBG("%s: cannot open control fd of '%s' Errno = %d\n",
   1005                  __func__, mm_camera_util_get_dev_name(my_obj),errno);
   1006         return -MM_CAMERA_E_GENERAL;
   1007     }
   1008 	ALOGV("%s:  2\n", __func__);
   1009 
   1010     /* open domain socket*/
   1011     n_try=MM_CAMERA_DEV_OPEN_TRIES;
   1012     do{
   1013         n_try--;
   1014         my_obj->ds_fd = mm_camera_socket_create(my_obj->my_id, MM_CAMERA_SOCK_TYPE_UDP); // TODO: UDP for now, change to TCP
   1015         ALOGV("%s:  ds_fd = %d", __func__, my_obj->ds_fd);
   1016         ALOGV("Errno:%d",errno);
   1017         if((my_obj->ds_fd > 0) || (n_try <= 0 )) {
   1018             ALOGV("%s:  opened, break out while loop", __func__);
   1019             break;
   1020         }
   1021         CDBG("%s:failed with I/O error retrying after %d milli-seconds",
   1022              __func__,sleep_msec);
   1023         usleep(sleep_msec*1000);
   1024     }while(n_try>0);
   1025 
   1026     ALOGV("%s:  after while loop for domain socket open", __func__);
   1027     if (my_obj->ds_fd <= 0) {
   1028         CDBG_ERROR("%s: cannot open domain socket fd of '%s' Errno = %d\n",
   1029                  __func__, mm_camera_util_get_dev_name(my_obj),errno);
   1030         close(my_obj->ctrl_fd);
   1031         my_obj->ctrl_fd = -1;
   1032         return -MM_CAMERA_E_GENERAL;
   1033     }
   1034 
   1035     /* set ctrl_fd to be the mem_mapping fd */
   1036     rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
   1037                         MSM_V4L2_PID_MMAP_INST, 0);
   1038     if (rc != MM_CAMERA_OK) {
   1039         CDBG_ERROR("error: ioctl VIDIOC_S_CTRL MSM_V4L2_PID_MMAP_INST failed: %s\n",
   1040         strerror(errno));
   1041         close(my_obj->ctrl_fd);
   1042         close(my_obj->ds_fd);
   1043         my_obj->ctrl_fd = -1;
   1044         my_obj->ds_fd = -1;
   1045         return -MM_CAMERA_E_GENERAL;
   1046     }
   1047     if(op_mode != MM_CAMERA_OP_MODE_NOTUSED)
   1048         rc =  mm_camera_util_s_ctrl(my_obj->ctrl_fd,
   1049                             MSM_V4L2_PID_CAM_MODE, op_mode);
   1050     if(!rc) {
   1051         my_obj->op_mode = op_mode;
   1052         my_obj->current_mode = CAMERA_MODE_2D; /* set geo mode to 2D by default */
   1053     }
   1054 
   1055     /* get camera capabilities */
   1056     memset(&my_obj->properties, 0, sizeof(cam_prop_t));
   1057     rc = mm_camera_send_native_ctrl_cmd(my_obj,
   1058                                         CAMERA_GET_CAPABILITIES,
   1059                                         sizeof(cam_prop_t),
   1060                                         (void *)& my_obj->properties);
   1061     if (rc != MM_CAMERA_OK) {
   1062         CDBG_ERROR("%s: cannot get camera capabilities\n", __func__);
   1063         close(my_obj->ctrl_fd);
   1064         close(my_obj->ds_fd);
   1065         my_obj->ctrl_fd = -1;
   1066         my_obj->ds_fd = -1;
   1067         return -MM_CAMERA_E_GENERAL;
   1068     }
   1069 
   1070     mm_camera_poll_threads_init(my_obj);
   1071     mm_camera_init_ch_stream_count(my_obj);
   1072     CDBG("%s : Launch Threads in Cam Open",__func__);
   1073     for(i = 0; i < MM_CAMERA_CH_MAX; i++) {
   1074         mm_camera_poll_thread_launch(my_obj,(mm_camera_channel_type_t)i);
   1075     }
   1076     CDBG("%s: '%s', ctrl_fd=%d,op_mode=%d,rc=%d\n",
   1077              __func__, dev_name, my_obj->ctrl_fd, my_obj->op_mode, rc);
   1078     return rc;
   1079 }
   1080 
   1081 int32_t mm_camera_close(mm_camera_obj_t *my_obj)
   1082 {
   1083     int i, rc = 0;
   1084 
   1085     for(i = 0; i < MM_CAMERA_CH_MAX; i++){
   1086         mm_camera_ch_fn(my_obj, (mm_camera_channel_type_t)i,
   1087                                 MM_CAMERA_STATE_EVT_RELEASE, NULL);
   1088     }
   1089 
   1090     my_obj->op_mode = MM_CAMERA_OP_MODE_NOTUSED;
   1091     if(my_obj->ctrl_fd > 0) {
   1092         rc = close(my_obj->ctrl_fd);
   1093         if(rc < 0) {
   1094             /* this is a dead end. */
   1095             CDBG("%s: !!!!FATAL ERROR!!!! ctrl_fd = %d, rc = %d",
   1096                  __func__, my_obj->ctrl_fd, rc);
   1097         }
   1098         my_obj->ctrl_fd = 0;
   1099     }
   1100     if(my_obj->ds_fd > 0) {
   1101         mm_camera_socket_close(my_obj->ds_fd);
   1102         my_obj->ds_fd = 0;
   1103     }
   1104     return MM_CAMERA_OK;
   1105 }
   1106 
   1107 int32_t mm_camera_action(mm_camera_obj_t *my_obj, uint8_t start,
   1108                         mm_camera_ops_type_t opcode, void *parm)
   1109 {
   1110     int32_t rc = - MM_CAMERA_E_INVALID_OPERATION;
   1111 
   1112     if(start)   rc = mm_camera_action_start(my_obj, opcode, parm);
   1113     else rc = mm_camera_action_stop(my_obj, opcode, parm);
   1114     CDBG("%s:start_flag=%d,opcode=%d,parm=%p,rc=%d\n",__func__,start,opcode,parm, rc);
   1115     return rc;
   1116 }
   1117 
   1118 int32_t mm_camera_ch_acquire(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type)
   1119 {
   1120     return mm_camera_ch_fn(my_obj,ch_type, MM_CAMERA_STATE_EVT_ACQUIRE, 0);
   1121 }
   1122 
   1123 void mm_camera_ch_release(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type)
   1124 {
   1125     mm_camera_ch_fn(my_obj,ch_type, MM_CAMERA_STATE_EVT_RELEASE, 0);
   1126 }
   1127 
   1128 int32_t mm_camera_sendmsg(mm_camera_obj_t *my_obj, void *msg, uint32_t buf_size, int sendfd)
   1129 {
   1130     return mm_camera_socket_sendmsg(my_obj->ds_fd, msg, buf_size, sendfd);
   1131 }
   1132