Initial release
[adaptation/ap_samsung/gst-plugins-s5pc2xx.git] / camerasrc / src / camerasrc-internal.c
1 /*
2  * camerasrc
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at your option)
11  * any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23
24 #include <math.h>
25 #include "camerasrc-internal.h"
26
27 /*#define USE_IOCTL_DEBUG*/
28 #if defined (USE_IOCTL_DEBUG)
29 static char* get_request_name(int request, char* res_str) {
30         switch (request) {
31         case VIDIOC_QBUF:
32                 sprintf(res_str, "[VIDIOC_QBUF]");
33                 break;
34         case VIDIOC_DQBUF:
35                 sprintf(res_str, "[VIDIOC_DQBUF]");
36                 break;
37         case VIDIOC_S_INPUT:
38                 sprintf(res_str, "[VIDIOC_S_INPUT]");
39                 break;
40         case VIDIOC_G_INPUT:
41                 sprintf(res_str, "[VIDIOC_G_INPUT]");
42                 break;
43         case VIDIOC_S_PARM:
44                 sprintf(res_str, "[VIDIOC_S_PARM]");
45                 break;
46         case VIDIOC_G_PARM:
47                 sprintf(res_str, "[VIDIOC_G_PARM]");
48                 break;
49         case VIDIOC_S_FMT:
50                 sprintf(res_str, "[VIDIOC_S_FMT]");
51                 break;
52         case VIDIOC_G_FMT:
53                 sprintf(res_str, "[VIDIOC_G_FMT]");
54                 break;
55         case VIDIOC_REQBUFS:
56                 sprintf(res_str, "[VIDIOC_REQBUFS]");
57                 break;
58         case VIDIOC_QUERYBUF:
59                 sprintf(res_str, "[VIDIOC_QUERYBUF]");
60                 break;
61         case VIDIOC_STREAMON:
62                 sprintf(res_str, "[VIDIOC_STREAMON]");
63                 break;
64         case VIDIOC_STREAMOFF:
65                 sprintf(res_str, "[VIDIOC_STREAMOFF]");
66                 break;
67         case VIDIOC_S_CTRL:
68                 sprintf(res_str, "[VIDIOC_S_CTRL] ");
69                 break;
70         case VIDIOC_G_CTRL:
71                 sprintf(res_str, "[VIDIOC_G_CTRL]");
72                 break;
73         case VIDIOC_ENUMINPUT:
74                 sprintf(res_str, "[VIDIOC_ENUMINPUT]");
75                 break;
76         case VIDIOC_S_JPEGCOMP:
77                 sprintf(res_str, "[VIDIOC_S_JPEGCOMP]");
78                 break;
79         case VIDIOC_G_JPEGCOMP:
80                 sprintf(res_str, "[VIDIOC_G_JPEGCOMP]");
81                 break;
82         /* Extension */
83         case VIDIOC_S_STROBE:
84                 sprintf(res_str, "[VIDIOC_S_STROBE]");
85                 break;
86         case VIDIOC_G_STROBE:
87                 sprintf(res_str, "[VIDIOC_G_STROBE]");
88                 break;
89         case VIDIOC_S_RECOGNITION:
90                 sprintf(res_str, "[VIDIOC_S_RECOGNITION]");
91                 break;
92         case VIDIOC_G_RECOGNITION:
93                 sprintf(res_str, "[VIDIOC_G_RECOGNITION]");
94                 break;
95         case VIDIOC_G_EXIF:
96                 sprintf(res_str, "[VIDIOC_G_EXIF]");
97                 break;
98         default:
99                 sprintf(res_str, "[UNKNOWN IOCTL(%x)]", request);
100                 break;
101         }
102
103         return 0;
104 }
105
106 #define PRINT_IOCTL_INFO(request, arg) {\
107         char res_str[255];\
108         get_request_name(request, res_str);\
109         camsrc_info("[request : %s, argument address : %x]", res_str, arg);\
110 }
111 #else
112
113 #define PRINT_IOCTL_INFO(request, arg)
114
115 #endif
116
117
118 #define LOCK(x) {\
119         if(0 != pthread_mutex_lock(&(x->mutex))) {\
120                 camsrc_error("Mutex lock error");\
121                 camsrc_assert(0);\
122         }\
123 }
124
125 #define UNLOCK(x) {\
126         if(0 != pthread_mutex_unlock(&(x->mutex))) {\
127                 camsrc_error("Mutex unlock error");\
128                 camsrc_assert(0);\
129         }\
130 }
131
132 #define AF_MUT_LOCK(p) {\
133         if(0 != pthread_mutex_lock(&(p->af_mutex))) {\
134                 camsrc_error("AF Mutex locking error");\
135                 camsrc_assert(0);\
136         }\
137 }
138
139 #define AF_MUT_UNLOCK(p) {\
140         if(0 != pthread_mutex_unlock(&(p->af_mutex))) {\
141                 camsrc_error("AF Mutex unlocking error");\
142                 camsrc_assert(0);\
143         }\
144 }
145
146 #define SET_CTRL_VAL(cid, in_value) {\
147         int err = CAMERASRC_ERR_UNKNOWN;\
148         struct v4l2_control control;\
149         control.id = cid;\
150         control.value = in_value;\
151         camsrc_info("[VIDIOC_S_CTRL] >> [%x] request with value %d", cid, in_value); \
152         err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control);\
153         if(err != CAMERASRC_SUCCESS) {\
154                 return err;\
155         }\
156 }
157
158 #define GET_CTRL_VAL(cid, ret_value) {\
159         int err = CAMERASRC_ERR_UNKNOWN;\
160         struct v4l2_control control;\
161         control.id = cid;\
162         err = _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &control);\
163         if(err != CAMERASRC_SUCCESS) {\
164                 return err;\
165         }\
166         ret_value = control.value;\
167         camsrc_info("[VIDIOC_G_CTRL] << [%x] request with value %d", cid, ret_value); \
168 }
169
170 #define SET_CTRL_VAL_ERR(cid, in_value, err) {\
171         struct v4l2_control control;\
172         control.id = cid;\
173         control.value = in_value;\
174         camsrc_info("[VIDIOC_S_CTRL] >> [%x] request with value %d", cid, in_value); \
175         _camerasrc_ioctl_with_err(handle, VIDIOC_S_CTRL, &control, &err);\
176 }
177
178 #define GET_CTRL_VAL_ERR(cid, ret_value, err) {\
179         struct v4l2_control control;\
180         control.id = cid;\
181         _camerasrc_ioctl_with_err(handle, VIDIOC_G_CTRL, &control, &err);\
182         ret_value = control.value;\
183         camsrc_info("[VIDIOC_G_CTRL] << [%x] request with value %d", cid, ret_value); \
184 }
185
186 /* AF function */
187 static int _camerasrc_start_autofocusing(camerasrc_handle_t *handle);
188 static int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle);
189 static int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle);
190 static int _camerasrc_release_autofocusing(camerasrc_handle_t *handle);
191
192
193 int static af_retry_cnt = 0;
194
195 static int _camerasrc_ioctl(camerasrc_handle_t *handle, int request, void *arg)
196 {
197         int fd = handle->dev_fd;
198         int err;
199         int nAgain = 10;
200         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
201
202         LOCK(handle);
203
204         if (handle->check_esd == 1) {
205                 camsrc_warning("ESD shock occured. All device control will success");
206                 UNLOCK(handle);
207                 return CAMERASRC_SUCCESS;
208         }
209
210         if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) {
211                 camsrc_warning("Device emergency closed. All device control will success");
212                 UNLOCK(handle);
213                 return CAMERASRC_SUCCESS;
214         }
215
216         PRINT_IOCTL_INFO(request, arg);
217
218 again:
219         do {
220                 err = ioctl (fd, request, arg);
221         } while (-1 == err && EINTR == errno);
222
223         if (err != 0) {
224                 handle->errnum = errno;
225                 err = errno;
226                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
227                 camsrc_error("ioctl[%x] err : %s", request, err_msg);
228                 if (err == EEXIST) {
229                         camsrc_info("EEXIST occured, but can go.");
230                         err = 0;
231                 } else if (err == ENOENT) {
232                         camsrc_info("ENOENT occured, but can go.");
233                         err = 0;
234 #if defined (ENABLE_Q_ERROR)
235 #warning "ENABLE_Q_ERROR enabled"
236                 } else if (request == VIDIOC_DQBUF) {
237                         goto DQ_ERROR;
238                 } else if (request == VIDIOC_QBUF) {
239                         goto ENQ_ERROR;
240 #endif
241                 } else if (err == EINVAL) {
242                         camsrc_error("EINVAL occured, Shutdown");
243                         UNLOCK(handle);
244                         return CAMERASRC_ERR_INVALID_PARAMETER;
245                 } else if (err == EBUSY) {
246                         camsrc_error("EBUSY occured, Shutdown");
247                         UNLOCK(handle);
248                         return CAMERASRC_ERR_PRIVILEGE;
249                 } else if (err == EAGAIN && nAgain--) {
250                         goto again;
251                 } else {
252                         /* Why does this return SUCCESS? */
253                         camsrc_error("Unhandled exception occured on IOCTL");
254                 }
255         }
256
257         UNLOCK(handle);
258
259         return CAMERASRC_SUCCESS;
260
261 #if defined (ENABLE_Q_ERROR)
262 DQ_ERROR:
263         camsrc_error("DQ Frame error occured");
264         printf("DQ Frame error occured");
265         UNLOCK(handle);
266         return CAMERASRC_ERR_INTERNAL;
267
268 ENQ_ERROR:
269         camsrc_error("Q Frame error occured");
270         printf("Q Frame error occured");
271         UNLOCK(handle);
272         return CAMERASRC_ERR_INTERNAL;
273 #endif
274 }
275
276
277 static int _camerasrc_ioctl_with_err(camerasrc_handle_t *handle, int request, void *arg, int *error)
278 {
279         int fd = handle->dev_fd;
280         int err;
281         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
282
283         LOCK(handle);
284
285         *error = 0;
286
287         if (handle->check_esd == 1) {
288                 camsrc_warning("ESD shock occured. All device control will success");
289                 UNLOCK(handle);
290                 return CAMERASRC_SUCCESS;
291         }
292
293         if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) {
294                 camsrc_warning("Device emergency closed. All device control will success");
295                 UNLOCK(handle);
296                 return CAMERASRC_SUCCESS;
297         }
298
299         PRINT_IOCTL_INFO(request, arg);
300
301         do {
302                 err = ioctl (fd, request, arg);
303         } while (-1 == err && EINTR == errno);
304
305         if (err != 0) {
306                 handle->errnum = errno;
307                 *error = errno;
308                 strerror_r(*error, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
309                 camsrc_error("ioctl[%x] err : %s", request, err_msg);
310                 UNLOCK(handle);
311                 return CAMERASRC_ERR_IO_CONTROL;
312         }
313
314         UNLOCK(handle);
315
316         return CAMERASRC_SUCCESS;
317 }
318
319
320 static int _camerasrc_ioctl_once(camerasrc_handle_t *handle, int request, void *arg)
321 {
322         int fd = handle->dev_fd;
323         int err = -1;
324         int nAgain = 10;
325         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
326
327         LOCK(handle);
328
329         if (handle->check_esd == 1) {
330                 camsrc_warning("ESD shock occured. All device control will success");
331                 UNLOCK(handle);
332                 return CAMERASRC_SUCCESS;
333         }
334
335         if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) {
336                 camsrc_warning("Device emergency closed. All device control will success");
337                 UNLOCK(handle);
338                 return CAMERASRC_SUCCESS;
339         }
340
341         PRINT_IOCTL_INFO(request, arg);
342
343 again:
344         err =  ioctl (fd, request, arg);
345
346         if (err != 0) {
347                 handle->errnum = errno;
348                 err = errno;
349                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
350                 camsrc_error("ioctl[%x] err : %s", request, err_msg);
351                 if (err == EEXIST) {
352                         camsrc_info("EEXIST occured, but can go.");
353                         err = 0;
354                 } else if (err == ENOENT) {
355                         camsrc_info("ENOENT occured, but can go.");
356                         err = 0;
357 #if defined (ENABLE_Q_ERROR)
358 #warning "ENABLE_Q_ERROR enabled"
359                 } else if (request == VIDIOC_DQBUF) {
360                         goto DQ_ERROR;
361                 } else if (request == VIDIOC_QBUF) {
362                         goto ENQ_ERROR;
363 #endif
364                 } else if (err == EINVAL) {
365                         camsrc_error("EINVAL occured, Shutdown");
366                         UNLOCK(handle);
367                         return CAMERASRC_ERR_INVALID_PARAMETER;
368                 } else if (err == EAGAIN && nAgain--) {
369                         goto again;
370                 } else {
371                         camsrc_error("Unhandled exception occured on IOCTL");
372                 }
373         }
374
375         UNLOCK(handle);
376
377         return CAMERASRC_SUCCESS;
378
379 #if defined (ENABLE_Q_ERROR)
380 DQ_ERROR:
381         camsrc_error("DQ Frame error occured");
382         printf("DQ Frame error occured");
383         UNLOCK(handle);
384         return CAMERASRC_ERR_INTERNAL;
385
386 ENQ_ERROR:
387         camsrc_error("Q Frame error occured");
388         printf("Q Frame error occured");
389         UNLOCK(handle);
390         return CAMERASRC_ERR_INTERNAL;
391 #endif
392 }
393
394
395 static int _camerasrc_skip_frame(camerasrc_handle_t *handle, long int timeout, int skip_frame)
396 {
397         camerasrc_handle_t *p = NULL;
398         int err = CAMERASRC_ERR_UNKNOWN;
399         fd_set fds;
400         struct timeval tv;
401         int r;
402         int skip_frame_num = skip_frame;
403
404         camsrc_error("enter");
405
406         p = handle;
407
408         while (skip_frame_num--) {
409                 camsrc_error("SKIP FRAME #%d", skip_frame-skip_frame_num+1);
410
411                 struct v4l2_buffer buf;
412
413                 FD_ZERO (&fds);
414                 FD_SET (p->dev_fd, &fds);
415
416                 camsrc_error("************Still capture wait frame start");
417
418                 /* Timeout. */
419                 tv.tv_sec = (long int)timeout/1000;
420                 tv.tv_usec = timeout - tv.tv_sec * 1000;
421
422                 r = select (p->dev_fd + 1, &fds, NULL, NULL, &tv);
423
424                 if (-1 == r) {
425                         if (EINTR == errno) {
426                                 return CAMERASRC_SUCCESS;
427                         }
428                         camsrc_error("select() failed.");
429                         return CAMERASRC_ERR_INTERNAL;
430                 }
431
432                 if (0 == r) {
433                         camsrc_error("select() timeout.");
434                         return CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT;
435                 }
436
437                 /**@note from samsung camera sample. *****************************************/
438                 /*SKIP FRAME START*/
439                 CLEAR(buf);
440                 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
441                 buf.memory = V4L2_MEMORY_MMAP;
442                 buf.index = p->buffer_idx;
443
444                 if (-1 == _camerasrc_ioctl (p, VIDIOC_DQBUF, &buf)) {
445                         switch (errno) {
446                         case EAGAIN:
447                                 camsrc_info("VIDIOC_DQBUF [EAGAIN]");
448                                 return CAMERASRC_SUCCESS;
449                         case EIO:
450                                 /* Could ignore EIO, see spec. */
451                                 /* fall through */
452                                 camsrc_info("VIDIOC_DQBUF [EIO]");
453                         default:
454                                 camsrc_info("VIDIOC_DQBUF [%d]", errno);
455                                 return CAMERASRC_ERR_IO_CONTROL;
456                         }
457                 }
458
459                 if (-1 == _camerasrc_ioctl (p, VIDIOC_QBUF, &buf)) {
460                         return CAMERASRC_ERR_IO_CONTROL;
461                 }
462         }
463
464         camsrc_error("leave");
465         err = CAMERASRC_SUCCESS;
466
467         return err;
468 }
469
470 static int _camerasrc_init_autofocusing_mode(camerasrc_handle_t *handle)
471 {
472         int ctrl_id = V4L2_CID_FOCUS_AUTO_MODE;
473         int mode;
474
475         camsrc_info("enter");
476
477         if (handle->af_status == CAMERASRC_AUTO_FOCUS_STATUS_ONGOING) {
478                 camsrc_info("Dev BUSY. Init failed.");
479                 return CAMERASRC_ERR_INVALID_STATE;
480         }
481
482         switch (handle->cur_af_mode) {
483         case CAMERASRC_AF_MODE_AUTO:
484                 mode = V4L2_FOCUS_AUTO_NORMAL;
485                 camsrc_info("ON AUTOFOCUSING...BY AUTO");
486                 break;
487         case CAMERASRC_AF_MODE_MANUAL:
488                 ctrl_id = V4L2_CID_FOCUS_MODE;
489                 mode = V4L2_FOCUS_MODE_MANUAL;
490                 camsrc_info("ON AUTOFOCUSING...BY MANUAL");
491                 break;
492         case CAMERASRC_AF_MODE_CONTINUOUS:
493                 mode = V4L2_FOCUS_AUTO_CONTINUOUS;
494                 camsrc_info("ON AUTOFOCUSING...BY CONTINUOUS");
495                 if (handle->format.pix_format != CAMERASRC_PIX_NV12 &&
496                     handle->format.pix_format != CAMERASRC_PIX_SN12) {
497                         camsrc_info("DO NOT CONTINUOUS AF when NV12 or SN12 format");
498                         return CAMERASRC_SUCCESS;
499                 }
500                 break;
501         case CAMERASRC_AF_MODE_TOUCH_AUTO:
502                 mode = V4L2_FOCUS_AUTO_RECTANGLE;
503                 camsrc_info("ON AUTOFOCUSING...BY TOUCH AUTO");
504                 break;
505         case CAMERASRC_AF_MODE_PAN:
506                 camsrc_warning("PAN MODE focusing is not supported.");
507         default:
508                 camsrc_warning("Unsupported AF mode[%d]", handle->cur_af_mode );
509                 return CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
510         }
511
512         /* MACRO is prior than other setting. FIXME */
513         if (handle->cur_af_range == CAMERASRC_AF_RANGE_MACRO) {
514                 mode = V4L2_FOCUS_AUTO_MACRO;
515                 camsrc_info("ON AUTOFOCUSING...BY MACRO");
516         }
517
518         SET_CTRL_VAL(ctrl_id, mode);
519
520         /* Start AF if continuous mode and preview is running */
521         if (mode == V4L2_FOCUS_AUTO_CONTINUOUS &&
522             handle->cur_state >= CAMERASRC_STATE_PREVIEW) {
523                 _camerasrc_start_autofocusing(handle);
524         }
525
526         return CAMERASRC_SUCCESS;
527 }
528
529 static void __CAMERASRC_FUJITSU_GET_AF_CTRL_VAL(camerasrc_handle_t* handle, int* status)
530 {
531         int err = 0;
532
533         if (af_retry_cnt >= (int)CAMERASRC_AF_TOTALTIME/CAMERASRC_AF_INTERVAL) {
534                 *status = CAMERASRC_SENSOR_AF_STATUS_FAILED;
535                 af_retry_cnt = 0;
536                 return;
537         } else {
538                 af_retry_cnt++;
539                 camsrc_info("retry count = %d", af_retry_cnt);
540         }
541
542         SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 1, err);
543         switch (err) {
544         case 0:
545                 camsrc_info("Succeeded");
546                 *status = CAMERASRC_SENSOR_AF_STATUS_FOCUSED;
547                 af_retry_cnt = 0;
548                 break;
549         case EBUSY:
550                 camsrc_info("Busy");
551                 *status = CAMERASRC_SENSOR_AF_STATUS_ONGOING;
552                 break;
553         default:
554                 camsrc_info("Failed");
555                 *status = CAMERASRC_SENSOR_AF_STATUS_FAILED;
556                 af_retry_cnt = 0;
557                 break;
558         }
559
560         return;
561 }
562
563 static void* _camerasrc_run_autofocusing(camerasrc_handle_t *handle)
564 {
565         int err = 0;
566         int sensor_status = 0;
567
568         camsrc_info("enter");
569
570         while(1) {
571                 AF_MUT_LOCK(handle);
572                 switch (handle->af_cmd) {
573                 case CAMERASRC_AUTO_FOCUS_CMD_START:
574                 {
575                         camsrc_info("AF CMD:START");
576                         __CAMERASRC_FUJITSU_GET_AF_CTRL_VAL(handle, &sensor_status);
577
578                         switch (sensor_status) {
579                         case CAMERASRC_SENSOR_AF_STATUS_ONGOING:
580                                 camsrc_info("FOCUSING. IN PROGRESS...");
581                                 handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_ONGOING;
582                                 break;
583                         case CAMERASRC_SENSOR_AF_STATUS_FOCUSED: /* focus operation success. Go to Null. */
584                                 camsrc_info("FOCUSED. stop autofocusing...");
585
586                                 handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED;
587                                 handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL;
588
589                                 CAMERASRC_SET_STATE(handle, CAMERASRC_STATE_PREVIEW);
590
591                                 if (handle->af_cb != NULL) {
592                                         if (handle->af_usr_data != NULL) {
593                                                 handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED, handle->af_usr_data);
594                                         } else {
595                                                 handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED, NULL);
596                                         }
597                                 }
598
599                                 AF_MUT_UNLOCK(handle);
600                                 continue;
601                         case CAMERASRC_SENSOR_AF_STATUS_FAILED: /* focus operation failed. Set stop and go to NULL. */
602                                 camsrc_info("FOCUSING FAILED");
603
604                                 if (handle->cur_af_mode != CAMERASRC_AF_MODE_CONTINUOUS ) {
605                                         SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err);
606                                         camsrc_info("Stopping AF done. err = %d", err);
607                                 }
608
609                                 handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED;
610                                 handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL;
611
612                                 CAMERASRC_SET_STATE(handle, CAMERASRC_STATE_PREVIEW);
613
614                                 if (handle->af_cb != NULL) {
615                                         if(handle->af_usr_data != NULL) {
616                                                 handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FAILED, handle->af_usr_data);
617                                         } else {
618                                                 handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FAILED, NULL);
619                                         }
620                                 }
621
622                                 AF_MUT_UNLOCK(handle);
623                                 continue;
624                         default:
625                                 camsrc_error("Unrecognizable status");
626                                 break;
627                         }
628                         break;
629                 }
630                 case CAMERASRC_AUTO_FOCUS_CMD_STOP:
631                 {
632                         camsrc_info("AF CMD:STOP");
633                         SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err);
634                         handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED;
635                         handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL;
636                         camsrc_info("Stopping AF done. err = %d", err);
637                         AF_MUT_UNLOCK(handle);
638                         continue;
639                 }
640                 case CAMERASRC_AUTO_FOCUS_CMD_KILL:
641                 {
642                         camsrc_info("AF CMD:KILL");
643
644                         SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err);
645                         camsrc_info("Stopping AF done. err = %d", err);
646
647                         handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED;
648                         handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL;
649
650                         AF_MUT_UNLOCK(handle);
651                         goto OUT_OF_LOOP;
652                 }
653                 case CAMERASRC_AUTO_FOCUS_CMD_NULL:
654                 default:
655                 {
656                         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
657                         camsrc_info("AF CMD:NULL....");
658                         err = pthread_cond_wait(&handle->af_wait_cond, &handle->af_mutex);
659                         if (err) {
660                                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
661                                 camsrc_error("AF CMD pthread_cond_wait - err:(%s)", err_msg);
662                         }
663
664                         af_retry_cnt = 0;
665
666                         /* Start AF delay for AE */
667                         if (handle->af_cmd == CAMERASRC_AUTO_FOCUS_CMD_START) {
668                                 struct timeval cur_time;
669                                 int sec = 0;
670                                 int usec = 0;
671                                 int diff = 0;           /* msec */
672                                 int sleep_time = 0;     /* usec */
673
674                                 SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err);
675
676                                 camsrc_info("Check set AF area Time(Delay:%d)", CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA);
677
678                                 if (handle->set_af_area_time.tv_sec != 0) {
679                                         gettimeofday(&cur_time, NULL);
680                                         sec = cur_time.tv_sec - handle->set_af_area_time.tv_sec;
681                                         usec = cur_time.tv_usec - handle->set_af_area_time.tv_usec;
682
683                                         camsrc_info("diff sec:%d, usec:%d", sec, usec);
684                                         diff = (sec * 1000) + (usec / 1000);
685
686                                         /* Skip sleep if diff is less than zero or greater than minimum exposure time */
687                                         if (diff < 0 || diff > CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA) {
688                                                 camsrc_info("no need to sleep(diff: %d ms)", diff);
689                                         } else {
690                                                 sleep_time = (CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA - diff) * 1000;
691                                                 camsrc_info("usleep(%d)", sleep_time);
692                                                 usleep(sleep_time);
693                                         }
694                                 }
695                         }
696
697                         AF_MUT_UNLOCK(handle);
698                         continue;
699                 }
700                 }
701
702                 AF_MUT_UNLOCK(handle);
703                 usleep(CAMERASRC_AF_INTERVAL);
704         }
705
706 OUT_OF_LOOP:
707         camsrc_info("AF thread is finished.");
708         return NULL;
709 }
710
711 static int _camerasrc_start_autofocusing(camerasrc_handle_t *handle)
712 {
713         int err;
714         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
715         camsrc_info("enter");
716
717         AF_MUT_LOCK(handle);
718
719         handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_START;
720         err = pthread_cond_signal(&handle->af_wait_cond);
721         if (err) {
722                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
723                 camsrc_info("AF wait cond err(%s)", err_msg);
724         }
725
726         AF_MUT_UNLOCK(handle);
727
728         return CAMERASRC_SUCCESS;
729 }
730
731 static int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle)
732 {
733         int err;
734         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
735
736         camsrc_info("enter");
737
738         AF_MUT_LOCK(handle);
739
740         handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_STOP;
741         err = pthread_cond_signal(&handle->af_wait_cond);
742         if (err) {
743                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
744                 camsrc_info("AF wait cond err(%s)", err_msg);
745         }
746
747         AF_MUT_UNLOCK(handle);
748
749         return CAMERASRC_SUCCESS;
750 }
751
752 static int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle)
753 {
754         int err;
755         char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',};
756         camsrc_info("enter");
757
758         AF_MUT_LOCK(handle);
759
760         handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_KILL;
761         err = pthread_cond_signal(&handle->af_wait_cond);
762         if (err) {
763                 strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN);
764                 camsrc_info("AF wait cond err(%s)", err_msg);
765         }
766
767         AF_MUT_UNLOCK(handle);
768
769         return CAMERASRC_SUCCESS;
770 }
771
772 static int _camerasrc_release_autofocusing(camerasrc_handle_t *handle)
773 {
774         int err;
775         camsrc_info("enter");
776
777         SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err);
778
779         return CAMERASRC_SUCCESS;
780 }
781
782 static int _camerasrc_copy_frame(camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail)
783 {
784         if (handle == NULL || src_buffer == NULL || dst_buffer == NULL) {
785                 camsrc_error("handle[%p], src_buffer[%p], dst_buffer[%p]",
786                                           handle, src_buffer, dst_buffer);
787                 return CAMERASRC_ERR_NULL_POINTER;
788         }
789
790         if (src_buffer->start == NULL || dst_buffer->start == NULL) {
791                 camsrc_error("src_buffer->start[%p], dst_buffer->start[%p]",
792                                           src_buffer->start, dst_buffer->start);
793                 return CAMERASRC_ERR_NULL_POINTER;
794         }
795
796         if (handle->format.colorspace == CAMERASRC_COL_RAW) {
797                 int cpy_len;
798
799                 if (handle->format.pix_format == CAMERASRC_PIX_YUV422P ||
800                     handle->format.pix_format == CAMERASRC_PIX_YUY2 ||
801                     handle->format.pix_format == CAMERASRC_PIX_UYVY) {
802                         cpy_len = (YUV422_SIZE(handle)>=src_buffer->length)?src_buffer->length:YUV422_SIZE(handle);
803                 } else if (handle->format.pix_format == CAMERASRC_PIX_YUV420P ||
804                            handle->format.pix_format == CAMERASRC_PIX_YUV420 ||
805                            handle->format.pix_format == CAMERASRC_PIX_NV12) {
806                         cpy_len = (YUV420_SIZE(handle)>=src_buffer->length)?src_buffer->length:YUV420_SIZE(handle);
807                 } else {
808                         camsrc_error("UNSUPPORTED format [%x]", handle->format.pix_format );
809                         return CAMERASRC_ERR_INVALID_FORMAT;
810                 }
811
812 #if defined (USE_FRAME_COPY_BOUNDARY_CHECK)
813                 camsrc_info("Boundary check %p ~ %p ... [length = %d] ", dst_buffer->start, dst_buffer->start+cpy_len, cpy_len);
814                 memset(dst_buffer->start, 0, cpy_len);
815                 camsrc_info("Boundary check OK");
816 #endif
817
818                 camsrc_info("RAW frame dequeing...");
819                 dst_buffer->length = cpy_len;
820
821                 if (handle->format.pix_format == CAMERASRC_PIX_SN12) {
822                         camsrc_error("Can't copy the frame with SN12 option");
823                         return CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
824                 } else if (handle->format.pix_format == CAMERASRC_PIX_ST12) {
825                         camsrc_error("Can't copy the frame with ST12 option");
826                         return CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
827                 }
828
829                 camsrc_error("format[%d] copy occured. copy len = %d", handle->format.pix_format, cpy_len);
830                 memcpy(dst_buffer->start, src_buffer->start, cpy_len );
831         } else if (handle->format.colorspace == CAMERASRC_COL_JPEG) {
832 #if defined (USE_FRAME_COPY_BOUNDARY_CHECK)
833                 camsrc_info("Boundary check %p ~ %p ...", dst_buffer->start, dst_buffer->start+src_buffer->length);
834                 memset(dst_buffer->start, 0, src_buffer->length);
835                 camsrc_info("Boundary check OK");
836 #endif
837
838                 if (!isThumbnail) {
839                         if (src_buffer->length <= 0) {
840                                 camsrc_error("length[%d] is too small", src_buffer->length );
841                                 return CAMERASRC_ERR_INVALID_VALUE;
842                         }
843
844                         dst_buffer->length = src_buffer->length;
845                         camsrc_info("JPEG main frame copy... img length = %d", dst_buffer->length);
846                         memcpy(dst_buffer->start, src_buffer->start, dst_buffer->length);
847                         camsrc_info("JPEG main frame copy done.");
848                 } else {
849                         if (handle->format.pix_format == CAMERASRC_PIX_RGGB8) {
850                                 /* JPEG + JPEG */
851                                 if (src_buffer->length <= 0) {
852                                         camsrc_error("length[%d] is too small", src_buffer->length );
853                                         return CAMERASRC_ERR_INVALID_VALUE;
854                                 }
855
856                                 dst_buffer->length = src_buffer->length;
857                                 camsrc_info("JPEG thm frame(JPEG) copy... img length = %d", dst_buffer->length);
858                                 memcpy(dst_buffer->start, src_buffer->start, dst_buffer->length);
859                                 camsrc_info("JPEG thm frame(JPEG) copy done.");
860                         } else if (handle->format.pix_format == CAMERASRC_PIX_RGGB10) {
861                                 /* JPEG + YUV */
862                                 dst_buffer->length = CAMERASRC_YUV_THMBNL_SIZE;
863                                 camsrc_info("JPEG thm frame(YUV) copy... img length = %d", CAMERASRC_YUV_THMBNL_SIZE);
864                                 memcpy(dst_buffer->start, src_buffer->start, CAMERASRC_YUV_THMBNL_SIZE);
865                                 camsrc_info("JPEG thm frame(YUV) copy done.");
866                         } else {
867                                 camsrc_info("Unknown format [%d]", handle->format.pix_format );
868                                 return CAMERASRC_ERR_INVALID_FORMAT;
869                         }
870                 }
871         }
872
873         return CAMERASRC_SUCCESS;
874 }
875
876
877 static int _camerasrc_set_thumbnail_size(camerasrc_handle_t *handle)
878 {
879         camsrc_info("enter");
880
881         return CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
882 }
883
884 static int _camerasrc_start_facedetection (camerasrc_handle_t *handle)
885 {
886         camsrc_info("enter");
887         camsrc_info("FACE DETECTION START!");
888         SET_CTRL_VAL(V4L2_CID_FACE_DETECTION, CAM_FACE_DETECTION_ON);
889         camsrc_info("leave");
890         return CAMERASRC_SUCCESS;
891 }
892
893
894 static int _camerasrc_stop_facedetection (camerasrc_handle_t *handle)
895 {
896         camsrc_info("enter");
897         camsrc_info("FACE DETECTION STOP!");
898         SET_CTRL_VAL(V4L2_CID_FACE_DETECTION, CAM_FACE_DETECTION_OFF);
899         camsrc_info("leave");
900         return CAMERASRC_SUCCESS;
901 }
902
903
904 static int _camerasrc_check_emp_shock(camerasrc_handle_t *handle, int *check_val)
905 {
906         camsrc_info("enter");
907         int ret_value = 0;
908
909         GET_CTRL_VAL(V4L2_CID_ESD_INT, ret_value);
910         *check_val = ret_value;
911
912         camsrc_info("leave");
913         return CAMERASRC_SUCCESS;
914 }
915
916
917 static int _camerasrc_get_frame_data(camerasrc_handle_t *handle, camerasrc_frame_data_t *data)
918 {
919         /*camsrc_info("enter");*/
920         int err = CAMERASRC_ERR_UNKNOWN;
921         struct v4l2_control control;
922
923         /* Get Y physical address */
924         /*camsrc_info("[VIDIOC_G_CTRL] << [V4L2_CID_PADDR_Y] request with value");*/
925         control.id = V4L2_CID_PADDR_Y;
926         control.value = data->index;
927         err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control);
928         if (err != CAMERASRC_SUCCESS) {
929                 return err;
930         }
931
932         data->phyAddrY = control.value;
933
934         /* Get CbCr physical address */
935         /*camsrc_info("[VIDIOC_G_CTRL] << [V4L2_CID_PADDR_CBCR] request with value");*/
936         control.id = V4L2_CID_PADDR_CBCR;
937         control.value = data->index;
938         err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control);
939         if (err != CAMERASRC_SUCCESS) {
940                 return err;
941         }
942
943         data->phyAddrCbCr = control.value;
944
945         /* Distance between Y ptr and CbCr ptr of virtual address is same with that of Physical one. */
946         data->virAddrY = (unsigned int)handle->buffer[data->index].start;
947         data->virAddrCbCr = data->virAddrY + (data->phyAddrCbCr - data->phyAddrY);
948
949         /*camsrc_info("virAddrY(%p), virAddrCbCr(%p)", data->virAddrY, data->virAddrCbCr);*/
950
951         return CAMERASRC_SUCCESS;
952 }
953
954
955 static void _dump_exif_info(camerasrc_exif_t *exif_struct)
956 {
957         camsrc_info("== Dynamic value ==");
958         camsrc_info("unsigned int exposure_time_numerator = %d", exif_struct->exposure_time_numerator);
959         camsrc_info("unsigned int exposure_time_denominator = %d", exif_struct->exposure_time_denominator);
960         camsrc_info("int shutter_speed_numerator = %d", exif_struct->shutter_speed_numerator);
961         camsrc_info("int shutter_speed_denominator = %d", exif_struct->shutter_speed_denominator);
962         camsrc_info("int brigtness_numerator = %d", exif_struct->brigtness_numerator);
963         camsrc_info("int brightness_denominator = %d", exif_struct->brightness_denominator);
964         camsrc_info("unsigned short int iso = %d", exif_struct->iso);
965         camsrc_info("unsigned short int flash = %d", exif_struct->flash);
966         camsrc_info("int metering_mode = %d", exif_struct->metering_mode);
967         camsrc_info("int exif_image_width = %d", exif_struct->exif_image_width);
968         camsrc_info("int exif_image_height = %d", exif_struct->exif_image_height);
969         camsrc_info("int exposure_bias_in_APEX = %d", exif_struct->exposure_bias_in_APEX);
970         camsrc_info("int software_used = %d", exif_struct->software_used);
971         camsrc_info("int focal_len_numerator = %d", exif_struct->focal_len_numerator);
972         camsrc_info("int focal_len_denominator = %d", exif_struct->focal_len_denominator);
973         camsrc_info("int aperture_f_num_numerator = %d", exif_struct->aperture_f_num_numerator);
974         camsrc_info("int aperture_f_num_denominator = %d", exif_struct->aperture_f_num_denominator);
975         camsrc_info("int aperture_in_APEX = %d", exif_struct->aperture_in_APEX);
976         camsrc_info("int max_lens_aperture_in_APEX = %d", exif_struct->max_lens_aperture_in_APEX);
977
978         camsrc_info("== Fixed value ==");
979         camsrc_info("int component_configuration = %x", exif_struct->component_configuration);
980         camsrc_info("int colorspace = %d", exif_struct->colorspace);
981
982         return;
983 }
984
985
986 static int _camerasrc_get_exif_info (camerasrc_handle_t *handle, camerasrc_exif_t *exif_struct)
987 {
988         int photometry_mode = V4L2_PHOTOMETRY_MULTISEG;
989
990         if (exif_struct == NULL) {
991                 return CAMERASRC_ERR_INVALID_PARAMETER;
992         }
993
994         /** Dynamic value **/
995
996         /* exposure time */
997         GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_EXPTIME, exif_struct->exposure_time_numerator);
998         exif_struct->exposure_time_denominator = 1;
999
1000         /* shutter speed */
1001         GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_TV, exif_struct->shutter_speed_numerator);
1002         exif_struct->shutter_speed_denominator = 1;
1003
1004         /* brightness */
1005         GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_BV, exif_struct->brigtness_numerator);
1006         exif_struct->brightness_denominator = 1;
1007
1008         /* iso */
1009         GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_ISO, exif_struct->iso);
1010         ISO_APPROXIMATE_VALUE(exif_struct->iso, exif_struct->iso);
1011
1012         /* flash */
1013         GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_FLASH, exif_struct->flash);
1014
1015         /* image size */
1016         exif_struct->exif_image_width = handle->format.img_size.dim.width;
1017         exif_struct->exif_image_height = handle->format.img_size.dim.height;
1018
1019         /*FIXME focal length - Set fixed value at gst_camerasrc_control_get_exif_info function */
1020         exif_struct->focal_len_numerator = 1;
1021         exif_struct->focal_len_denominator = 1;
1022
1023         /*FIXME f number - Set fixed value at gst_camerasrc_control_get_exif_info function */
1024         exif_struct->aperture_f_num_numerator = 1;
1025         exif_struct->aperture_f_num_denominator = 1;
1026
1027         /* FIXME APEX */
1028         exif_struct->aperture_in_APEX \
1029                 = CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(exif_struct->aperture_f_num_numerator, exif_struct->aperture_f_num_denominator);
1030         exif_struct->max_lens_aperture_in_APEX = 1;
1031         /*= CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(exif_info.max_aperture_f_num_numerator, exif_info.max_aperture_f_num_denominator);*/
1032         exif_struct->exposure_bias_in_APEX = exif_struct->aperture_in_APEX \
1033                 + CAMERASRC_EXIF_SHUTTERSPEED_VALUE_IN_APEX(exif_struct->exposure_time_numerator, exif_struct->exposure_time_denominator);
1034
1035         /* Get the value using CID */
1036         /* Not implemented yet
1037            GET_CTRL_VAL(V4L2_CID_FW_VERSION, exif_struct->software_used);
1038          */
1039         GET_CTRL_VAL(V4L2_CID_PHOTOMETRY, photometry_mode);
1040         PHOTOMETRY_MODE_TO_METERING_MODE(photometry_mode, exif_struct->metering_mode);
1041
1042         /** Fixed value **/
1043         exif_struct->component_configuration = _CAMERASRC_EXIF_COMP_CONF;
1044         exif_struct->colorspace = _CAMERASRC_EXIF_COLORSPACE;
1045
1046         _dump_exif_info(exif_struct);
1047
1048         return CAMERASRC_SUCCESS;
1049 }
1050
1051 static int fd_on = 0;
1052
1053 static int _camerasrc_set_cmd(camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value)
1054 {
1055         int err = CAMERASRC_ERR_UNKNOWN;
1056         struct v4l2_jpegcompression comp_arg;
1057
1058         switch (cmd) {
1059         case _CAMERASRC_CMD_STROBE_MODE:
1060         {
1061                 camerasrc_strobe_mode_t *mode = (camerasrc_strobe_mode_t*)value;
1062
1063                 camsrc_info("[_CAMERASRC_CMD_STROBE_MODE] cmd set value %d", *mode );
1064
1065                 SET_CTRL_VAL(V4L2_CID_CAMERA_FLASH_MODE, *mode);
1066         }
1067                 break;
1068         case _CAMERASRC_CMD_FACEDETECTION:
1069         {
1070                 int err = CAMERASRC_ERR_UNKNOWN;
1071                 struct v4l2_recognition recog;
1072
1073                 camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] cmd set");
1074                 if (((int)value) == _CAMERASRC_FACEDETECTION_START) {
1075                         camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] start");
1076                         recog.mode = V4L2_RECOGNITION_MODE_ON;
1077                         recog.pattern = V4L2_RECOG_PATTERN_FACE;
1078                         recog.obj_num = 10;
1079                         /*recog.detect_idx = 0;*/
1080                         recog.action= V4L2_RECOGNITION_ACTION_NONE;
1081                         fd_on = 1;
1082                 } else if (((int)value) == _CAMERASRC_FACEDETECTION_STOP) {
1083                         camsrc_error("[_CAMERASRC_CMD_FACEDETECTION] stop");
1084                         recog.mode = V4L2_RECOGNITION_MODE_OFF;
1085                         recog.pattern = V4L2_RECOG_PATTERN_FACE;
1086                         recog.obj_num = 10;
1087                         /*recog.detect_idx = 0;*/
1088                         recog.action= V4L2_RECOGNITION_ACTION_NONE;
1089                         fd_on = 0;
1090                 } else {
1091                         camsrc_error("[_CAMERASRC_CMD_FACEDETECTION] not support cmd");
1092                 }
1093
1094                 err = _camerasrc_ioctl(handle, VIDIOC_S_RECOGNITION, &recog);
1095                 if (err != CAMERASRC_SUCCESS) {
1096                         goto ERROR;
1097                 }
1098         }
1099                 break;
1100         case _CAMERASRC_CMD_SHUTTER_SPEED:
1101                 /* WRITEME */
1102                 camsrc_info("[_CAMERASRC_CMD_SHUTTER_SPEED] cmd set");
1103                 break;
1104         case _CAMERASRC_CMD_JPEG_LENGTH:
1105                 /* WRITEME */
1106                 camsrc_info("[_CAMERASRC_CMD_JPEG_LENGTH] cmd set");
1107                 break;
1108         case _CAMERASRC_CMD_JPEG_THMBNL_LENGTH:
1109                 camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_LENGTH] cmd set");
1110                 err = _camerasrc_set_thumbnail_size(handle);
1111                 if (err != CAMERASRC_SUCCESS) {
1112                         goto ERROR;
1113                 }
1114                 break;
1115         case _CAMERASRC_CMD_EXPOSURE_VALUE:
1116                 camsrc_info("[_CAMERASRC_CMD_EXPOSURE_VALUE] cmd set");
1117                 /* WRITEME */
1118                 break;
1119         case _CAMERASRC_CMD_ESD_CHECK:
1120                 camsrc_info("[_CAMERASRC_CMD_ESD_CHECK] cmd set");
1121                 /* WRITEME */
1122                 break;
1123         case _CAMERASRC_CMD_CTRL:
1124                 camsrc_info("[_CAMERASRC_CMD_CTRL] cmd set");
1125                 SET_CTRL_VAL(_CAMERASRC_GET_CID(((_camerasrc_ctrl_t *) value)->cid, handle->cur_dev_id), ((_camerasrc_ctrl_t *) value)->value);
1126                 break;
1127         case _CAMERASRC_CMD_SUPPORT_EMBED_EXIF:
1128                 /* WRITEME */
1129                 camsrc_info("[_CAMERASRC_CMD_SUPPORT_EMBED_EXIF] cmd set");
1130                 break;
1131         case _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING:
1132                 camsrc_warning("[_CAMERASRC_CMD_SUPPORT_JPEG_ENCODING] cmd set isn't supported");
1133                 break;
1134         case _CAMERASRC_CMD_AF_CONTROL:
1135                 /* WRITEME */
1136                 camsrc_info("[_CAMERASRC_CMD_AF_CONTROL] cmd set(value=%d)", value);
1137
1138                 /* FIXME : Please fix whole AF implementation!!! */
1139                 switch ((int)value) {
1140                 case _CAMERASRC_AF_START:
1141                         _camerasrc_start_autofocusing(handle);
1142                         break;
1143                 case _CAMERASRC_AF_STOP:
1144                         _camerasrc_stop_autofocusing(handle);
1145                         break;
1146                 case _CAMERASRC_AF_DESTROY:
1147                         _camerasrc_destroy_autofocusing(handle);
1148                         break;
1149                 case _CAMERASRC_AF_RELEASE:
1150                 case _CAMERASRC_AF_INIT:
1151                 default:
1152                         _camerasrc_init_autofocusing_mode(handle);
1153                         break;
1154                 }
1155                 break;
1156         case _CAMERASRC_CMD_AF_AREA:
1157         {
1158                 camerasrc_rect_t *rect = (camerasrc_rect_t *)value;
1159
1160                 camsrc_info("[_CAMERASRC_CMD_AF_AREA] cmd set (%d,%d,%dx%d)",
1161                                        rect->x, rect->y, rect->width, rect->height);
1162
1163                 SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_LEFT, rect->x);
1164                 SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_TOP, rect->y);
1165                 SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_WIDTH, 0);/*rect->width); Not supported */
1166                 SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_HEIGHT, 0);/*rect->height); Not supported */
1167
1168                 if (gettimeofday(&handle->set_af_area_time, NULL) == 0) {
1169                         camsrc_info("[_CAMERASRC_CMD_AF_AREA] Get Time Success" );
1170                 } else {
1171                         handle->set_af_area_time.tv_sec = 0;
1172                         handle->set_af_area_time.tv_usec = 0;
1173                         camsrc_warning("[_CAMERASRC_CMD_AF_AREA] Get Time Failed" );
1174                 }
1175                 break;
1176         }
1177         case _CAMERASRC_CMD_FRAME_DATA:
1178                 /* WRITEME */
1179                 camsrc_info("[_CAMERASRC_CMD_FRAME_DATA] cmd set");
1180                 break;
1181         case _CAMERASRC_CMD_EXIF_INFO:
1182                 /* WRITEME */
1183                 camsrc_info("[_CAMERASRC_CMD_EXIF_INFO] cmd set");
1184                 break;
1185         case _CAMERASRC_CMD_CHECK_ESD:
1186                 /* WRITEME */
1187                 camsrc_info("[_CAMERASRC_CMD_CHECK_ESD] cmd set");
1188                 break;
1189         case _CAMERASRC_CMD_JPEG_COMPRESS_RATIO:
1190                 /* WRITEME */
1191                 camsrc_info("[_CAMERASRC_CMD_JPEG_COMPRESS_RATIO] cmd set, val = %d", (int)(*((unsigned int*) value)));
1192                 if (handle->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) {
1193                         comp_arg.quality = (int)(*((unsigned int*) value));
1194                         err = _camerasrc_ioctl(handle, VIDIOC_S_JPEGCOMP, &comp_arg);
1195                         if (err != CAMERASRC_SUCCESS) {
1196                                 goto ERROR;
1197                         }
1198                 } else if (handle->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) {
1199                         err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
1200                         goto ERROR;
1201                 }
1202                 break;
1203         case _CAMERASRC_CMD_ROTATION:
1204         {
1205                 int *rotate = (int *)value;
1206                 camsrc_info("[_CAMERASRC_CMD_ROTATION] cmd set : %d", *rotate);
1207                 SET_CTRL_VAL(V4L2_CID_ROTATION, (int)*rotate);
1208         }
1209                 break;
1210         case _CAMERASRC_CMD_SENSOR_MODE:
1211         {
1212                 camerasrc_sensor_mode_t *mode = (camerasrc_sensor_mode_t *)value;
1213                 camsrc_info("[_CAMERASRC_CMD_SENSOR_MODE] cmd set : %d", *mode);
1214                 SET_CTRL_VAL(V4L2_CID_CAMERA_SENSOR_MODE, (int)*mode);
1215         }
1216                 break;
1217         case _CAMERASRC_CMD_VFLIP:
1218         {
1219                 int *vflip = (int *)value;
1220                 camsrc_info("[_CAMERASRC_CMD_VFLIP] cmd set : %d", *vflip);
1221                 SET_CTRL_VAL(V4L2_CID_VFLIP, (int)*vflip);
1222         }
1223                 break;
1224         case _CAMERASRC_CMD_HFLIP:
1225         {
1226                 int *hflip = (int *)value;
1227                 camsrc_info("[_CAMERASRC_CMD_HFLIP] cmd set : %d", *hflip);
1228                 SET_CTRL_VAL(V4L2_CID_HFLIP, (int)*hflip);
1229         }
1230                 break;
1231         default:
1232                 camsrc_error("[_CAMERASRC_CMD_UNKNOWN] cmd set");
1233                 err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
1234                 goto ERROR;
1235         }
1236
1237         return CAMERASRC_SUCCESS;
1238
1239 ERROR:
1240         camsrc_error("cmd execution error occured");
1241
1242         return err;
1243 }
1244
1245 static int _camerasrc_get_cmd(camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value)
1246 {
1247         int err = CAMERASRC_ERR_UNKNOWN;
1248         struct v4l2_jpegcompression comp_arg;
1249
1250         if (!value) {
1251                 camsrc_error("value is NULL");
1252                 return CAMERASRC_ERR_NULL_POINTER;
1253         }
1254
1255         switch (cmd) {
1256         case _CAMERASRC_CMD_SUPPORT_EMBED_EXIF:
1257                 camsrc_info("[_CAMERASRC_CMD_SUPPORT_EMBED_EXIF] cmd get");
1258                 *((int*)value) = _CAMERASRC_CMD_SUPPORT_EMBED_EXIF_DEF;
1259                 break;
1260         case _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING:
1261                 camsrc_info("[_CAMERASRC_CMD_SUPPORT_JPEG_ENCODING] cmd get(cur_dev_id=%d)", handle->cur_dev_id);
1262                 if (handle->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) {
1263                         *((int*)value) = 1;
1264                 } else {
1265                         *((int*)value) = 0;
1266                 }
1267                 break;
1268         case _CAMERASRC_CMD_STROBE_MODE:
1269         {
1270                 camerasrc_strobe_mode_t mode;
1271
1272                 GET_CTRL_VAL(V4L2_CID_CAMERA_FLASH_MODE, mode);
1273                 *((camerasrc_strobe_mode_t*)value) = mode;
1274
1275                 camsrc_info("[_CAMERASRC_CMD_STROBE_MODE] cmd get - %d", mode);
1276         }
1277                 break;
1278         case _CAMERASRC_CMD_FACEDETECTION:
1279                 camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] cmd get - %d", fd_on);
1280                 *(int *)value = fd_on;
1281                 break;
1282         case _CAMERASRC_CMD_SHUTTER_SPEED:
1283                 /* WRITEME */
1284                 camsrc_info("[_CAMERASRC_CMD_SHUTTER_SPEED] cmd get");
1285                 break;
1286         case _CAMERASRC_CMD_JPEG_LENGTH:
1287         {
1288                 int err = CAMERASRC_ERR_UNKNOWN;
1289                 struct v4l2_control ctrl;
1290
1291                 ctrl.id = V4L2_CID_CAM_JPEG_MAIN_SIZE;
1292                 err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl );
1293                 if (err != CAMERASRC_SUCCESS) {
1294                         *((int*)value) = 0;
1295                         goto ERROR;
1296                 }
1297
1298                 *((int*)value) = ctrl.value;
1299                 camsrc_info("[_CAMERASRC_CMD_JPEG_LENGTH] cmd get");
1300                 break;
1301         }
1302         case _CAMERASRC_CMD_JPEG_THMBNL_LENGTH:
1303         {
1304                 int err = CAMERASRC_ERR_UNKNOWN;
1305                 struct v4l2_control ctrl;
1306
1307                 ctrl.id = V4L2_CID_CAM_JPEG_THUMB_SIZE;
1308                 err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl );
1309                 if (err != CAMERASRC_SUCCESS) {
1310                         *((int*)value) = 0;
1311                         goto ERROR;
1312                 }
1313
1314                 *((int*)value) = ctrl.value;
1315                 camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_LENGTH] cmd get : %d", *((int*)value));
1316                 break;
1317         }
1318         case _CAMERASRC_CMD_JPEG_THMBNL_OFFSET:
1319         {
1320                 int err = CAMERASRC_ERR_UNKNOWN;
1321                 struct v4l2_control ctrl;
1322                 ctrl.id = V4L2_CID_CAM_JPEG_THUMB_OFFSET;
1323
1324                 err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl );
1325                 if(err != CAMERASRC_SUCCESS)
1326                 {
1327                         *((int*)value) = 0;
1328                         goto ERROR;
1329                 }
1330
1331                 *((int*)value) = ctrl.value;
1332                 camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_OFFSET] cmd get : %x", *((int*)value));
1333                 break;
1334         }
1335         case _CAMERASRC_CMD_JPEG_SCRNL_LENGTH:
1336         {
1337                 int err = CAMERASRC_ERR_UNKNOWN;
1338                 struct v4l2_control ctrl;
1339                 ctrl.id = V4L2_CID_CAM_JPEG_POSTVIEW_SIZE;
1340
1341                 err = _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &ctrl);
1342                 if (err != CAMERASRC_SUCCESS) {
1343                         *((int*)value) = 0;
1344                         goto ERROR;
1345                 }
1346
1347                 *((int*)value) = ctrl.value;
1348                 camsrc_info("[_CAMERASRC_CMD_JPEG_SCRNL_LENGTH] cmd get : %x", *((int*)value));
1349                 break;
1350         }
1351         case _CAMERASRC_CMD_JPEG_SCRNL_OFFSET:
1352         {
1353                 int err = CAMERASRC_ERR_UNKNOWN;
1354                 struct v4l2_control ctrl;
1355                 ctrl.id = V4L2_CID_CAM_JPEG_POSTVIEW_OFFSET;
1356
1357                 err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl );
1358                 if (err != CAMERASRC_SUCCESS) {
1359                         *((int*)value) = 0;
1360                         goto ERROR;
1361                 }
1362
1363                 *((int*)value) = ctrl.value;
1364                 camsrc_info("[_CAMERASRC_CMD_JPEG_SCRNL_OFFSET] cmd get : %x", *((int*)value));
1365                 break;
1366         }
1367         case _CAMERASRC_CMD_EXPOSURE_VALUE:
1368                 /* WRITEME */
1369                 camsrc_info("[_CAMERASRC_CMD_EXPOSURE_VALUE] cmd get");
1370                 break;
1371         case _CAMERASRC_CMD_ESD_CHECK:
1372                 /* WRITEME */
1373                 camsrc_info("[_CAMERASRC_CMD_ESD_CHECK] cmd get");
1374                 break;
1375         case _CAMERASRC_CMD_CTRL:
1376                 camsrc_info("[_CAMERASRC_CMD_CTRL] cmd get");
1377                 GET_CTRL_VAL(_CAMERASRC_GET_CID(((_camerasrc_ctrl_t *) value)->cid, handle->cur_dev_id), ((_camerasrc_ctrl_t *) value)->value);
1378                 break;
1379         case _CAMERASRC_CMD_AF_CONTROL:
1380                 /* WRITEME */
1381                 camsrc_info("[_CAMERASRC_CMD_AF_CONTROL] cmd get");
1382                 break;
1383         case _CAMERASRC_CMD_AF_AREA:
1384         {
1385                 camerasrc_rect_t* rect = (camerasrc_rect_t*)value;
1386
1387                 GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_LEFT, rect->x);
1388                 GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_TOP, rect->y);
1389                 GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_WIDTH, rect->width);
1390                 GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_HEIGHT, rect->height);
1391
1392                 camsrc_info("[_CAMERASRC_CMD_AF_AREA] cmd get (%d,%d,%dx%d)",
1393                                        rect->x, rect->y, rect->width, rect->height);
1394                 break;
1395         }
1396         case _CAMERASRC_CMD_FRAME_DATA:
1397                 /* WRITEME */
1398                 /*camsrc_info("[_CAMERASRC_CMD_FRAME_DATA] cmd get");*/
1399                 err = _camerasrc_get_frame_data(handle, (camerasrc_frame_data_t*)value);
1400                 if (err != CAMERASRC_SUCCESS) {
1401                         goto ERROR;
1402                 }
1403                 break;
1404         case _CAMERASRC_CMD_EXIF_INFO:
1405                 camsrc_info("[_CAMERASRC_CMD_EXIF_INFO] cmd get");
1406                 err = _camerasrc_get_exif_info (handle, (camerasrc_exif_t*)value);
1407                 if (err != CAMERASRC_SUCCESS) {
1408                         goto ERROR;
1409                 }
1410                 break;
1411         case _CAMERASRC_CMD_CHECK_ESD:
1412                 camsrc_info("[_CAMERASRC_CMD_CHECK_ESD] cmd get");
1413                 GET_CTRL_VAL(V4L2_CID_ESD_INT, *((int *) value));
1414                 break;
1415         case _CAMERASRC_CMD_JPEG_COMPRESS_RATIO:
1416                 camsrc_info("[_CAMERASRC_CMD_JPEG_COMPRESS_RATIO] cmd get");
1417                 err = _camerasrc_ioctl(handle, VIDIOC_G_JPEGCOMP, &comp_arg);
1418                 if (err != CAMERASRC_SUCCESS) {
1419                         goto ERROR;
1420                 }
1421                 *((unsigned int*)value) = (int)comp_arg.quality;
1422                 break;
1423         case _CAMERASRC_CMD_ROTATION:
1424                 camsrc_info("[_CAMERASRC_CMD_ROTATION] cmd get");
1425                 GET_CTRL_VAL(V4L2_CID_ROTATION, *(int*)value);
1426                 break;
1427         case _CAMERASRC_CMD_SENSOR_MODE:
1428                 camsrc_info("[_CAMERASRC_CMD_SENSOR_MODE] cmd get");
1429                 GET_CTRL_VAL(V4L2_CID_CAMERA_SENSOR_MODE, *(int*)value);
1430                 break;
1431         case _CAMERASRC_CMD_VFLIP:
1432                 camsrc_info("[_CAMERASRC_CMD_VFLIP] cmd get");
1433                 GET_CTRL_VAL(V4L2_CID_VFLIP, *(int*)value);
1434                 break;
1435         case _CAMERASRC_CMD_HFLIP:
1436                 camsrc_info("[_CAMERASRC_CMD_HFLIP] cmd get");
1437                 GET_CTRL_VAL(V4L2_CID_HFLIP, *(int*)value);
1438                 break;
1439         default:
1440                 camsrc_error("[_CAMERASRC_CMD_UNKNOWN] cmd get");
1441                 err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT;
1442                 goto ERROR;
1443         }
1444
1445         return CAMERASRC_SUCCESS;
1446
1447 ERROR:
1448         camsrc_error("cmd execution error occured");
1449
1450         return err;
1451 }
1452
1453 static const CAMERASRC_DEV_DEPENDENT_MISC_FUNC dev_misc_functions = {
1454         ._ioctl            = _camerasrc_ioctl,
1455         ._ioctl_once       = _camerasrc_ioctl_once,
1456         ._run_autofocusing = _camerasrc_run_autofocusing,
1457         ._skip_frame       = _camerasrc_skip_frame,
1458         ._copy_frame       = _camerasrc_copy_frame,
1459         ._set_cmd          = _camerasrc_set_cmd,
1460         ._get_cmd          = _camerasrc_get_cmd,
1461 };
1462
1463 const CAMERASRC_DEV_DEPENDENT_MISC_FUNC *dev_misc_func = &dev_misc_functions;