Send error message when DQBUF is failed
[platform/adaptation/camera-hal-v4l2.git] / src / hal_camera_v4l2.c
1 /*
2  * tizen_camera_v4l2.c
3  *
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28 #include <fcntl.h>
29 #include <inttypes.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <glob.h>
34 #include <dlog.h>
35 #include <sched.h>
36 #include "hal_camera_v4l2_private.h"
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif /* LOG_TAG */
41 #define LOG_TAG "CAMERA_HAL"
42
43 #define TEST_JPEG_PATH          "/home/owner/media/Images/test.jpg"
44 #define DEVICE_NODE_PATH_MAX    16
45 #define DEVICE_NODE_PATH_PREFIX "/dev/video"
46 #define FOURCC_FORMAT           "%c%c%c%c"
47 #define FOURCC_CONVERT(fourcc)  \
48         fourcc & 0xff,\
49         (fourcc >> 8) & 0xff,\
50         (fourcc >> 16) & 0xff,\
51         (fourcc >> 24) & 0xff
52
53
54 static camera_device_info_list_s *g_device_info_list;
55 static guint32 g_device_caps;
56 static GMutex g_device_info_lock;
57
58
59 static void __camera_hal_v4l2_destructor(void) __attribute__((destructor));
60 static void __camera_send_message(hal_camera_handle *handle, camera_message_type_e type, int value);
61
62
63 static void __camera_hal_v4l2_destructor(void)
64 {
65         LOGD("release device info list %p", g_device_info_list);
66
67         g_free(g_device_info_list);
68         g_device_info_list = NULL;
69
70         return;
71 }
72
73
74 static void __camera_send_message(hal_camera_handle *handle, camera_message_type_e type, int value)
75 {
76         camera_message_s *message = NULL;
77
78         if (!handle) {
79                 LOGE("NULL handle");
80                 return;
81         }
82
83         message = g_new0(camera_message_s, 1);
84
85         message->type = type;
86
87         switch (type) {
88         case CAMERA_MESSAGE_TYPE_FOCUS_CHANGED:
89                 message->focus_state = (camera_focus_state_e)value;
90                 break;
91         case CAMERA_MESSAGE_TYPE_CAPTURED:
92                 break;
93         case CAMERA_MESSAGE_TYPE_HDR_PROGRESS:
94                 message->hdr_progress = (uint32_t)value;
95                 break;
96         case CAMERA_MESSAGE_TYPE_ERROR:
97                 message->error_code = (camera_error_e)value;
98                 break;
99         default:
100                 LOGE("unknown type[%d]", type);
101                 g_free(message);
102                 return;
103         }
104
105         g_mutex_lock(&handle->msg_cb_lock);
106
107         LOGD("type[%d], value[0x%x]", type, value);
108
109         g_queue_push_tail(handle->msg_list, message);
110         g_cond_signal(&handle->msg_cb_cond);
111
112         g_mutex_unlock(&handle->msg_cb_lock);
113 }
114
115
116 static int __camera_v4l2_wait_frame(int device_fd, int wait_time)
117 {
118         int ret = CAMERA_ERROR_NONE;
119         fd_set fds;
120         struct timeval timeout;
121
122         if (device_fd < 0) {
123                 LOGE("invalid fd %d", device_fd);
124                 return CAMERA_ERROR_INVALID_PARAMETER;
125         }
126
127         FD_ZERO(&fds);
128         FD_SET(device_fd, &fds);
129
130         memset(&timeout, 0x0, sizeof(struct timeval));
131
132         timeout.tv_sec = wait_time;
133         timeout.tv_usec = 0;
134
135         /*LOGD("select : %d sec", wait_time);*/
136
137         ret = select(device_fd + 1, &fds, NULL, NULL, &timeout);
138         if (ret == -1) {
139                 if (EINTR == errno) {
140                         LOGD("select error : EINTR");
141                         return CAMERA_ERROR_NONE;
142                 }
143                 LOGE("select failed. errno %d", errno);
144                 return CAMERA_ERROR_INTERNAL;
145         }
146
147         if (ret == 0) {
148                 LOGE("select timeout.");
149                 return CAMERA_ERROR_INTERNAL;
150         }
151
152         /*LOGD("select done");*/
153
154         return CAMERA_ERROR_NONE;
155 }
156
157
158 static int __camera_v4l2_g_ctrl(int device_fd, int cid, int *value)
159 {
160         int ret = 0;
161         struct v4l2_control ctrl;
162
163         if (!value) {
164                 LOGE("NULL param");
165                 return CAMERA_ERROR_INVALID_PARAMETER;
166         }
167
168         memset(&ctrl, 0x0, sizeof(struct v4l2_control));
169
170         ctrl.id = cid;
171
172         ret = v4l2_ioctl(device_fd, VIDIOC_G_CTRL, &ctrl);
173
174         *value = ctrl.value;
175
176         LOGD("G_CTRL id 0x%x, value %d, ret %d", cid, *value, ret);
177
178         return ret;
179 }
180
181
182 static int __camera_v4l2_s_ctrl(int device_fd, int cid, int value)
183 {
184         int ret = 0;
185         struct v4l2_control ctrl;
186
187         memset(&ctrl, 0x0, sizeof(struct v4l2_control));
188
189         ctrl.id = cid;
190         ctrl.value = value;
191
192         ret = v4l2_ioctl(device_fd, VIDIOC_S_CTRL, &ctrl);
193
194         LOGD("S_CTRL id 0x%x, value %d, ret %d", cid, value, ret);
195
196         return ret;
197 }
198
199
200 static int __camera_v4l2_stream(int device_fd, int type, gboolean onoff)
201 {
202         if (device_fd < 0) {
203                 LOGE("invalid fd %d", device_fd);
204                 return CAMERA_ERROR_INVALID_PARAMETER;
205         }
206
207         if (v4l2_ioctl(device_fd, onoff ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type) < 0) {
208                 LOGE("stream %d failed. [t:%d] errno %d", onoff, type, errno);
209                 return CAMERA_ERROR_INTERNAL;
210         }
211
212         LOGD("stream %d done [t:%d]", onoff, type);
213
214         return CAMERA_ERROR_NONE;
215 }
216
217
218 static int __camera_v4l2_reqbufs(int device_fd, int type, int memory, uint32_t count, uint32_t *result_count)
219 {
220         struct v4l2_requestbuffers v4l2_reqbuf;
221
222         if (device_fd < 0) {
223                 LOGE("invalid fd %d", device_fd);
224                 return CAMERA_ERROR_INVALID_PARAMETER;
225         }
226
227         if (!result_count) {
228                 LOGE("NULL parameter");
229                 return CAMERA_ERROR_INVALID_PARAMETER;
230         }
231
232         memset(&v4l2_reqbuf, 0x0, sizeof(struct v4l2_requestbuffers));
233
234         v4l2_reqbuf.type = type;
235         v4l2_reqbuf.memory = memory;
236         v4l2_reqbuf.count = count;
237
238         if (v4l2_ioctl(device_fd, VIDIOC_REQBUFS, &v4l2_reqbuf) < 0) {
239                 LOGE("REQBUFS[count %d] failed. errno %d", count, errno);
240                 return CAMERA_ERROR_INTERNAL;
241         }
242
243         if (v4l2_reqbuf.count != count)
244                 LOGW("different count [req:%d, result:%d]", count, v4l2_reqbuf.count);
245
246         *result_count = v4l2_reqbuf.count;
247
248         return CAMERA_ERROR_NONE;
249 }
250
251
252 static int __camera_v4l2_qbuf(int device_fd, int type, int memory, int index)
253 {
254         struct v4l2_buffer v4l2_buf;
255         struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];
256
257         if (device_fd < 0) {
258                 LOGE("invalid fd %d", device_fd);
259                 return CAMERA_ERROR_INVALID_PARAMETER;
260         }
261
262         memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
263         memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
264
265         v4l2_buf.index = index;
266         v4l2_buf.type = type;
267         v4l2_buf.memory = memory;
268         v4l2_buf.m.planes = v4l2_planes;
269         v4l2_buf.length = 460800;
270         v4l2_buf.bytesused = 460800;
271
272         if (v4l2_ioctl(device_fd, VIDIOC_QBUF, &v4l2_buf) < 0) {
273                 LOGE("qbuf failed.  [i: %d, t: %d, m: %d] errno %d",
274                         index, type, memory, errno);
275                 return CAMERA_ERROR_INTERNAL;
276         }
277
278         /*LOGD("QBUF done [i: %d, t: %d, m: %d]", index, type, memory);*/
279
280         return CAMERA_ERROR_NONE;
281 }
282
283
284 static int __camera_v4l2_dqbuf(int device_fd, int type, int memory, int *index)
285 {
286         int ret = CAMERA_ERROR_NONE;
287         struct v4l2_buffer v4l2_buf;
288         struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];
289
290         if (device_fd < 0) {
291                 LOGE("invalid fd %d", device_fd);
292                 return CAMERA_ERROR_INVALID_PARAMETER;
293         }
294
295         if (!index) {
296                 LOGE("NULL parameter");
297                 return CAMERA_ERROR_INVALID_PARAMETER;
298         }
299
300         memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
301         memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
302
303         v4l2_buf.type = type;
304         v4l2_buf.memory = memory;
305         v4l2_buf.m.planes = v4l2_planes;
306
307         ret = v4l2_ioctl(device_fd, VIDIOC_DQBUF, &v4l2_buf);
308         if (ret < 0) {
309                 LOGE("dqbuf failed. [t: %d, m: %d] errno %d",
310                         type, memory, errno);
311                 return CAMERA_ERROR_DEVICE_READ;
312         }
313
314         *index = v4l2_buf.index;
315
316         /*LOGD("dqbuf index %d", *index);*/
317
318         return CAMERA_ERROR_NONE;
319 }
320
321
322 static int __camera_get_format(guint32 fourcc, int *pixel_format)
323 {
324         if (!pixel_format) {
325                 LOGE("NULL parameter");
326                 return CAMERA_ERROR_INVALID_PARAMETER;
327         }
328
329         switch (fourcc) {
330         case V4L2_PIX_FMT_NV12:
331         case V4L2_PIX_FMT_NV12M:
332         case V4L2_PIX_FMT_NV12MT:
333                 *pixel_format = CAMERA_PIXEL_FORMAT_NV12;
334                 break;
335         case V4L2_PIX_FMT_NV21:
336         case V4L2_PIX_FMT_NV21M:
337                 *pixel_format = CAMERA_PIXEL_FORMAT_NV21;
338                 break;
339         case V4L2_PIX_FMT_YUV420:
340                 *pixel_format = CAMERA_PIXEL_FORMAT_I420;
341                 break;
342         case V4L2_PIX_FMT_YVU420:
343                 *pixel_format = CAMERA_PIXEL_FORMAT_YV12;
344                 break;
345         case V4L2_PIX_FMT_YUYV:
346                 *pixel_format = CAMERA_PIXEL_FORMAT_YUYV;
347                 break;
348         case V4L2_PIX_FMT_UYVY:
349                 *pixel_format = CAMERA_PIXEL_FORMAT_UYVY;
350                 break;
351         case V4L2_PIX_FMT_JPEG:
352                 *pixel_format = CAMERA_PIXEL_FORMAT_JPEG;
353                 break;
354         case V4L2_PIX_FMT_H264:
355                 *pixel_format = CAMERA_PIXEL_FORMAT_H264;
356                 break;
357         case V4L2_PIX_FMT_MJPEG:
358                 *pixel_format = CAMERA_PIXEL_FORMAT_MJPEG;
359                 break;
360         default:
361                 LOGE("unknown fourcc "FOURCC_FORMAT, FOURCC_CONVERT(fourcc));
362                 return CAMERA_ERROR_INTERNAL;
363         }
364
365         LOGD("fourcc "FOURCC_FORMAT" -> %d",
366                 FOURCC_CONVERT(fourcc), *pixel_format);
367
368         return CAMERA_ERROR_NONE;
369 }
370
371
372 static int __camera_get_fourcc_plane_num(int pixel_format, guint32 *fourcc, guint32 *plane_num)
373 {
374         if (!fourcc || !plane_num) {
375                 LOGE("NULL parameter %p %p", fourcc, plane_num);
376                 return CAMERA_ERROR_INVALID_PARAMETER;
377         }
378
379         switch (pixel_format) {
380         case CAMERA_PIXEL_FORMAT_NV12:
381                 *fourcc = V4L2_PIX_FMT_NV12;
382                 *plane_num = 2;
383                 break;
384         case CAMERA_PIXEL_FORMAT_NV21:
385                 *fourcc = V4L2_PIX_FMT_NV21;
386                 *plane_num = 2;
387                 break;
388         case CAMERA_PIXEL_FORMAT_I420:
389                 *fourcc = V4L2_PIX_FMT_YUV420;
390                 *plane_num = 3;
391                 break;
392         case CAMERA_PIXEL_FORMAT_YV12:
393                 *fourcc = V4L2_PIX_FMT_YVU420;
394                 *plane_num = 3;
395                 break;
396         case CAMERA_PIXEL_FORMAT_YUYV:
397                 *fourcc = V4L2_PIX_FMT_YUYV;
398                 *plane_num = 1;
399                 break;
400         case CAMERA_PIXEL_FORMAT_UYVY:
401                 *fourcc = V4L2_PIX_FMT_UYVY;
402                 *plane_num = 1;
403                 break;
404         case CAMERA_PIXEL_FORMAT_JPEG:
405                 *fourcc = V4L2_PIX_FMT_JPEG;
406                 *plane_num = 1;
407                 break;
408         case CAMERA_PIXEL_FORMAT_H264:
409                 *fourcc = V4L2_PIX_FMT_H264;
410                 *plane_num = 1;
411                 break;
412         case CAMERA_PIXEL_FORMAT_MJPEG:
413                 *fourcc = V4L2_PIX_FMT_MJPEG;
414                 *plane_num = 1;
415                 break;
416         default:
417                 LOGE("unknown format %d", pixel_format);
418                 return CAMERA_ERROR_INTERNAL;
419         }
420
421         LOGD("format %d -> fourcc "FOURCC_FORMAT,
422                 pixel_format, FOURCC_CONVERT(*fourcc));
423
424         return CAMERA_ERROR_NONE;
425 }
426
427
428 static void __camera_get_fps_list(int device_fd, guint32 pixel_format, int width, int height, camera_fps_list_s *fps_list)
429 {
430         uint32_t fps_count = 0;
431         struct v4l2_frmivalenum ival;
432
433         if (device_fd < 0 || !fps_list) {
434                 LOGE("invalid param %d %p", device_fd, fps_list);
435                 return;
436         }
437
438         ival.index = 0;
439         ival.pixel_format = pixel_format;
440         ival.width = width;
441         ival.height = height;
442
443         while (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0) {
444                 if (ival.type != V4L2_FRMIVAL_TYPE_DISCRETE) {
445                         LOGE("NOT DISCRETE type[%u] for [%dx%d]", ival.type, width, height);
446                         return;
447                 }
448
449                 if (ival.index++ >= FPS_COUNT_MAX) {
450                         LOGW("\t\t\t\tFramerate[i:%u][%u/%u] is available, but list is full[max:%d]",
451                                 ival.index - 1, ival.discrete.denominator, ival.discrete.numerator, FPS_COUNT_MAX);
452                         continue;
453                 }
454
455                 LOGI("\t\t\t\tFramerate[%u/%u]", ival.discrete.denominator, ival.discrete.numerator);
456                 fps_list->fps[fps_count++] = ival.discrete.denominator;
457         }
458
459
460         fps_list->count = fps_count;
461 }
462
463
464 static int __camera_get_device_info(int device_index, int device_fd, camera_device_info_s *device_info, char *node_path)
465 {
466         int format_index = 0;
467         int format_count = 0;
468         int resolution_index = 0;
469         int resolution_count = 0;
470         int camera_format = 0;
471         struct v4l2_fmtdesc v4l2_format;
472         struct v4l2_frmsizeenum v4l2_frame;
473
474         if (device_fd < 0 || !device_info || !node_path) {
475                 LOGE("invalid param %d %p %p", device_fd, device_info, node_path);
476                 return CAMERA_ERROR_INVALID_PARAMETER;
477         }
478
479         LOGD("Get Supported format, resolution and fps");
480
481         for (format_index = 0, format_count = 0 ; ; format_index++) {
482                 memset(&v4l2_format, 0x0, sizeof(struct v4l2_fmtdesc));
483
484                 v4l2_format.index = format_index;
485                 v4l2_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
486
487                 if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FMT, &v4l2_format) < 0) {
488                         LOGW("\tformat : end of enumeration");
489                         break;
490                 }
491
492                 LOGD("\tTry [%d] format "FOURCC_FORMAT" (emulated:%d)",
493                         format_count, FOURCC_CONVERT(v4l2_format.pixelformat),
494                         ((v4l2_format.flags & V4L2_FMT_FLAG_EMULATED) ? 1 : 0));
495
496                 if (__camera_get_format(v4l2_format.pixelformat, &camera_format) != CAMERA_ERROR_NONE)
497                         continue;
498
499                 if (format_count + 1 >= CAMERA_PIXEL_FORMAT_MAX) {
500                         LOGW("format list is full[max:%u], skip format[i:%u][%d]",
501                                 CAMERA_PIXEL_FORMAT_MAX, v4l2_format.index, camera_format);
502                         continue;
503                 }
504
505                 device_info->format_list.formats[format_count++] = camera_format;
506
507                 for (resolution_index = 0, resolution_count = 0 ; ; resolution_index++) {
508                         memset(&v4l2_frame, 0x0, sizeof(struct v4l2_frmsizeenum));
509
510                         v4l2_frame.index = resolution_index;
511                         v4l2_frame.pixel_format = v4l2_format.pixelformat;
512
513                         if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMESIZES, &v4l2_frame) < 0) {
514                                 LOGW("\t\tframesize : end of enumeration");
515                                 break;
516                         }
517
518                         if (resolution_count + 1 >= RESOLUTION_COUNT_MAX) {
519                                 LOGW("resolution list is full, skip resolution[%ux%u]", v4l2_frame.discrete.width, v4l2_frame.discrete.height);
520                                 continue;
521                         }
522
523                         switch (v4l2_frame.type) {
524                         case V4L2_FRMSIZE_TYPE_DISCRETE:
525                                 device_info->preview_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
526                                 device_info->preview_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
527                                 device_info->capture_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
528                                 device_info->capture_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
529                                 device_info->video_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
530                                 device_info->video_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
531
532                                 LOGD("\t\tsize[%d] %ux%u", resolution_count,
533                                         v4l2_frame.discrete.width,
534                                         v4l2_frame.discrete.height);
535
536                                 __camera_get_fps_list(device_fd,
537                                         v4l2_frame.pixel_format,
538                                         v4l2_frame.discrete.width,
539                                         v4l2_frame.discrete.height,
540                                         &device_info->preview_fps_list[resolution_count]);
541
542                                 memcpy(&device_info->video_fps_list[resolution_count], &device_info->preview_fps_list[resolution_count], sizeof(camera_fps_list_s));
543
544                                 resolution_count++;
545                                 break;
546                         case V4L2_FRMSIZE_TYPE_CONTINUOUS:
547                                 LOGW("\t\tsize[%d] %ux%u - %ux%u", resolution_count,
548                                         v4l2_frame.stepwise.min_width,
549                                         v4l2_frame.stepwise.min_height,
550                                         v4l2_frame.stepwise.max_width,
551                                         v4l2_frame.stepwise.max_height);
552                                 break;
553                         case V4L2_FRMSIZE_TYPE_STEPWISE:
554                                 LOGW("\t\tsize[%d] %ux%u - %ux%u (step %ux%u)", resolution_count,
555                                         v4l2_frame.stepwise.min_width,
556                                         v4l2_frame.stepwise.min_height,
557                                         v4l2_frame.stepwise.max_width,
558                                         v4l2_frame.stepwise.max_height,
559                                         v4l2_frame.stepwise.step_width,
560                                         v4l2_frame.stepwise.step_height);
561                                 break;
562                         default:
563                                 LOGE("\t\tunknown frame type %d", v4l2_frame.type);
564                                 break;
565                         }
566                 }
567
568                 device_info->preview_list.count = resolution_count;
569                 device_info->capture_list.count = resolution_count;
570                 device_info->video_list.count = resolution_count;
571
572                 LOGD("\t\tresolution count [%d]", resolution_count);
573         }
574
575         device_info->index = device_index;
576         device_info->format_list.count = format_count;
577         device_info->facing_direction = CAMERA_FACING_DIRECTION_EXTERNAL;
578         snprintf(device_info->name, sizeof(device_info->name), "V4L2_CAMERA");
579         snprintf(device_info->node_path, sizeof(device_info->node_path), "%s", node_path);
580
581         LOGD("\tformat count [%d]", format_count);
582
583         return CAMERA_ERROR_NONE;
584 }
585
586
587 static int __camera_get_device_info_list(void)
588 {
589         int i = 0;
590         int ret = 0;
591         int device_count = 0;
592         int device_fd = CAMERA_HAL_INITIAL_FD;
593 #ifdef HAVE_LIBV4L2
594         int libv4l2_fd = CAMERA_HAL_INITIAL_FD;
595 #endif /* HAVE_LIBV4L2 */
596         glob_t glob_buf;
597         struct v4l2_capability v4l2_cap;
598         camera_device_info_list_s *device_info_list = NULL;
599
600         g_mutex_lock(&g_device_info_lock);
601
602         if (g_device_info_list) {
603                 LOGD("device info list is already existed");
604                 ret = CAMERA_ERROR_NONE;
605                 goto _GET_DEVICE_INFO_LIST_DONE;
606         }
607
608         device_info_list = g_new0(camera_device_info_list_s, 1);
609         if (!device_info_list) {
610                 LOGE("failed to alloc device info structure");
611                 ret = CAMERA_ERROR_OUT_OF_MEMORY;
612                 goto _GET_DEVICE_INFO_LIST_DONE;
613         }
614
615         memset(&glob_buf, 0x0, sizeof(glob_t));
616
617         ret = glob(DEVICE_NODE_PATH_PREFIX"*", 0, 0, &glob_buf);
618         if (ret != 0) {
619                 switch (ret) {
620                 case GLOB_NOSPACE:
621                         LOGE("out of memory");
622                         ret = CAMERA_ERROR_OUT_OF_MEMORY;
623                         goto _GET_DEVICE_INFO_LIST_DONE;
624                 case GLOB_ABORTED:
625                         LOGE("read error");
626                         ret = CAMERA_ERROR_INTERNAL;
627                         goto _GET_DEVICE_INFO_LIST_DONE;
628                 case GLOB_NOMATCH:
629                         LOGE("match not found");
630                         ret = CAMERA_ERROR_INTERNAL;
631                         goto _GET_DEVICE_INFO_LIST_DONE;
632                 default:
633                         LOGE("unknown error : %d", ret);
634                         ret = CAMERA_ERROR_INTERNAL;
635                         goto _GET_DEVICE_INFO_LIST_DONE;
636                 }
637         }
638
639         LOGD("device node count : %zu", glob_buf.gl_pathc);
640
641         for (i = 0 ; i < glob_buf.gl_pathc ; i++) {
642                 LOGD("[%d] check device [%s]", i, glob_buf.gl_pathv[i]);
643
644                 device_fd = open(glob_buf.gl_pathv[i], O_RDWR);
645                 if (device_fd < 0) {
646                         LOGE("open failed [%s] errno %d", glob_buf.gl_pathv[i], errno);
647                         continue;
648                 }
649
650 #ifdef HAVE_LIBV4L2
651                 libv4l2_fd = v4l2_fd_open(device_fd, V4L2_ENABLE_ENUM_FMT_EMULATION);
652
653                 LOGI("device_fd[%d], libv4l2_fd[%d]", device_fd, libv4l2_fd);
654
655                 if (libv4l2_fd != CAMERA_HAL_INITIAL_FD)
656                         device_fd = libv4l2_fd;
657 #endif /* HAVE_LIBV4L2 */
658
659                 memset(&v4l2_cap, 0x0, sizeof(struct v4l2_capability));
660
661                 if (v4l2_ioctl(device_fd, VIDIOC_QUERYCAP, &v4l2_cap) < 0) {
662                         LOGE("querycap failed. errno %d", errno);
663                         v4l2_close(device_fd);
664                         continue;
665                 }
666
667                 if (v4l2_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
668                         g_device_caps = v4l2_cap.device_caps;
669                 else
670                         g_device_caps = v4l2_cap.capabilities;
671
672                 if (!(g_device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
673                         (g_device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
674                         LOGW("[%s] is not a capture device 0x%x", glob_buf.gl_pathv[i], g_device_caps);
675                         v4l2_close(device_fd);
676                         continue;
677                 }
678
679                 ret = __camera_get_device_info(device_count, device_fd,
680                         &device_info_list->device_info[device_count], glob_buf.gl_pathv[i]);
681
682                 v4l2_close(device_fd);
683
684                 if (ret == CAMERA_ERROR_NONE)
685                         device_count++;
686         }
687
688         device_info_list->count = device_count;
689         g_device_info_list = device_info_list;
690
691         LOGD("new g_device_info_list %p - device count %d",
692                 g_device_info_list, device_count);
693
694 _GET_DEVICE_INFO_LIST_DONE:
695         g_mutex_unlock(&g_device_info_lock);
696         LOGD("ret 0x%x", ret);
697
698         if (ret != CAMERA_ERROR_NONE)
699                 g_free(device_info_list);
700
701         return ret;
702 }
703
704
705 static int __camera_stop_stream(hal_camera_handle *handle, uint32_t buffer_count)
706 {
707         int i = 0;
708         int ret = CAMERA_ERROR_NONE;
709
710         if (!handle) {
711                 LOGE("NULL handle");
712                 return CAMERA_ERROR_INVALID_PARAMETER;
713         }
714
715         LOGD("buffer count[%d]", buffer_count);
716
717         /* stream off */
718         ret = __camera_v4l2_stream(handle->device_fd, handle->buffer_type, FALSE);
719
720         LOGD("stream off : 0x%x", ret);
721
722         /* munmap */
723         for (i = 0 ; i < buffer_count ; i++) {
724                 if (handle->camera_buffers[i].planes[0].data != NULL) {
725                         LOGW("munmap %p", handle->camera_buffers[i].planes[0].data);
726
727                         v4l2_munmap(handle->camera_buffers[i].planes[0].data, handle->camera_buffers[i].planes[0].size);
728
729                         handle->camera_buffers[i].planes[0].data = 0;
730                         handle->camera_buffers[i].planes[0].size = 0;
731                 } else {
732                         LOGW("NULL data [index %d]", i);
733                 }
734         }
735
736         /* reqbufs 0 */
737         ret = __camera_v4l2_reqbufs(handle->device_fd,
738                 handle->buffer_type, V4L2_MEMORY_MMAP, 0, &buffer_count);
739
740         LOGD("reqbufs 0 : 0x%x", ret);
741
742         return ret;
743 }
744
745
746 static int __camera_start_stream(hal_camera_handle *handle, camera_pixel_format_e pixel_format,
747         camera_resolution_s *resolution, uint32_t fps, uint32_t request_buffer_count)
748 {
749         int i = 0;
750         int ret = CAMERA_ERROR_NONE;
751         camera_buffer_s *buffer = NULL;
752         struct v4l2_format v4l2_fmt;
753         struct v4l2_streamparm v4l2_parm;
754         struct v4l2_buffer v4l2_buf;
755         struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];;
756         guint32 fourcc = 0;
757         guint32 plane_num = 0;
758
759         if (!handle || !resolution) {
760                 LOGE("NULL param %p %p", handle, resolution);
761                 return CAMERA_ERROR_INTERNAL;
762         }
763
764         /* S_FMT */
765         ret = __camera_get_fourcc_plane_num(pixel_format, &fourcc, &plane_num);
766         if (ret != CAMERA_ERROR_NONE) {
767                 LOGE("get fourcc failed [format %d]", pixel_format);
768                 return ret;
769         }
770
771         memset(&v4l2_fmt, 0x0, sizeof(struct v4l2_format));
772
773         v4l2_fmt.type = handle->buffer_type;
774         if (V4L2_TYPE_IS_MULTIPLANAR(handle->buffer_type)) {
775                 v4l2_fmt.fmt.pix_mp.width = resolution->width;
776                 v4l2_fmt.fmt.pix_mp.height = resolution->height;
777                 v4l2_fmt.fmt.pix_mp.pixelformat = fourcc;
778                 v4l2_fmt.fmt.pix_mp.num_planes = plane_num;
779         } else {
780                 v4l2_fmt.fmt.pix.width = resolution->width;
781                 v4l2_fmt.fmt.pix.height = resolution->height;
782                 v4l2_fmt.fmt.pix.pixelformat = fourcc;
783                 v4l2_fmt.fmt.pix.bytesperline = resolution->width;
784         }
785
786         if (v4l2_ioctl(handle->device_fd, VIDIOC_S_FMT, &v4l2_fmt) < 0) {
787                 LOGE("S_FMT failed. errno %d", errno);
788                 return CAMERA_ERROR_INTERNAL;
789         }
790
791         if (V4L2_TYPE_IS_MULTIPLANAR(handle->buffer_type)) {
792                 for (i = 0 ; i < v4l2_fmt.fmt.pix_mp.num_planes ; i++) {
793                         LOGD("plane[%d] stride %u, sizeimage %u", i,
794                                 v4l2_fmt.fmt.pix_mp.plane_fmt[i].bytesperline,
795                                 v4l2_fmt.fmt.pix_mp.plane_fmt[i].sizeimage);
796                 }
797         } else {
798                 LOGD("stride %d, sizeimage %d",
799                         v4l2_fmt.fmt.pix.bytesperline,
800                         v4l2_fmt.fmt.pix.sizeimage);
801         }
802
803         /* G_PARM */
804         memset(&v4l2_parm, 0x0, sizeof(struct v4l2_streamparm));
805
806         v4l2_parm.type = handle->buffer_type;
807
808         if (v4l2_ioctl(handle->device_fd, VIDIOC_G_PARM, &v4l2_parm) < 0) {
809                 LOGE("G_PARM failed. errno %d", errno);
810                 return CAMERA_ERROR_INTERNAL;
811         }
812
813         /* S_PARM to set fps */
814         v4l2_parm.parm.capture.timeperframe.numerator = 1;
815         v4l2_parm.parm.capture.timeperframe.denominator = fps;
816
817         if (v4l2_ioctl(handle->device_fd, VIDIOC_S_PARM, &v4l2_parm) < 0) {
818                 LOGE("S_PARM failed. errno %d", errno);
819                 return CAMERA_ERROR_INTERNAL;
820         }
821
822         /* request buffer */
823         ret = __camera_v4l2_reqbufs(handle->device_fd,
824                 handle->buffer_type, V4L2_MEMORY_MMAP, request_buffer_count, &handle->buffer_count);
825         if (ret != CAMERA_ERROR_NONE) {
826                 return ret;
827         }
828
829         LOGD("buffer count : request %d -> result %d",
830                 request_buffer_count, handle->buffer_count);
831
832         /* query buffer, mmap and qbuf */
833         for (i = 0 ; i < handle->buffer_count ; i++) {
834                 memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
835                 memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
836
837                 v4l2_buf.type = handle->buffer_type;
838                 v4l2_buf.memory = V4L2_MEMORY_MMAP;
839                 v4l2_buf.index = i;
840                 v4l2_buf.m.planes = v4l2_planes;
841                 v4l2_buf.length = plane_num;
842
843                 if (v4l2_ioctl(handle->device_fd, VIDIOC_QUERYBUF, &v4l2_buf) < 0) {
844                         LOGE("[%d] query buf failed. errno %d", i, errno);
845                         goto _START_STREAM_FAILED;
846                 }
847
848                 buffer = &handle->camera_buffers[i];
849
850                 buffer->index = i;
851                 buffer->format = pixel_format;
852                 buffer->resolution.width = resolution->width;
853                 buffer->resolution.height = resolution->height;
854                 buffer->total_size = v4l2_buf.length;
855                 buffer->num_planes = plane_num;
856                 buffer->planes[0].size = v4l2_buf.length;
857                 buffer->planes[0].data = v4l2_mmap(0,
858                         v4l2_buf.length,
859                         PROT_READ | PROT_WRITE,
860                         MAP_SHARED,
861                         handle->device_fd,
862                         v4l2_buf.m.offset);
863
864                 if (buffer->planes[0].data == MAP_FAILED) {
865                         LOGE("[%d] mmap failed (errno %d)", i, errno);
866                         goto _START_STREAM_FAILED;
867                 }
868
869                 if (__camera_v4l2_qbuf(handle->device_fd, handle->buffer_type, V4L2_MEMORY_MMAP, i) != CAMERA_ERROR_NONE) {
870                         LOGE("[%d] qbuf failed (errno %d)", i, errno);
871                         goto _START_STREAM_FAILED;
872                 }
873         }
874
875         /* stream on */
876         ret = __camera_v4l2_stream(handle->device_fd, handle->buffer_type, TRUE);
877         if (ret != CAMERA_ERROR_NONE) {
878                 LOGE("stream on failed");
879                 goto _START_STREAM_FAILED;
880         }
881
882         return CAMERA_ERROR_NONE;
883
884 _START_STREAM_FAILED:
885         __camera_stop_stream(handle, handle->buffer_count);
886         return ret;
887 }
888
889
890 static void __camera_do_capture(hal_camera_handle *handle)
891 {
892         int ret = CAMERA_ERROR_NONE;
893         int buffer_index = 0;
894         gint64 current_time = 0;
895         gint64 previous_time = 0;
896         gint64 interval_us = 0;
897
898         if (!handle) {
899                 LOGE("NULL handle");
900                 return;
901         }
902
903         LOGD("start");
904
905         if (handle->capture_count > 1)
906                 interval_us = (gint64)handle->capture_interval_ms * 1000;
907
908         /* restart stream for capture */
909         if (handle->capture_restart_stream) {
910                 ret = __camera_stop_stream(handle, handle->buffer_count);
911                 if (ret != CAMERA_ERROR_NONE) {
912                         LOGE("stop stream failed for capture[0x%x]", ret);
913                         goto _CAPTURE_DONE;
914                 }
915
916                 ret = __camera_start_stream(handle,
917                         handle->preview_format.capture_format,
918                         &handle->preview_format.capture_resolution,
919                         handle->preview_format.stream_fps,
920                         BUFFER_MAX);
921                 if (ret != CAMERA_ERROR_NONE) {
922                         LOGE("start stream failed for capture[0x%x]", ret);
923                         goto _CAPTURE_DONE;
924                 }
925         }
926
927         do {
928                 /* get capture buffer */
929                 ret = __camera_v4l2_wait_frame(handle->device_fd, 5);
930                 if (ret != CAMERA_ERROR_NONE) {
931                         LOGE("frame wait failed for capture[0x%x]", ret);
932                         goto _CAPTURE_DONE;
933                 }
934
935                 ret = __camera_v4l2_dqbuf(handle->device_fd,
936                         handle->buffer_type, V4L2_MEMORY_MMAP, &buffer_index);
937                 if (ret != CAMERA_ERROR_NONE) {
938                         LOGE("dqbuf failed for capture[0x%x]", ret);
939                         goto _CAPTURE_DONE;
940                 }
941
942                 if (handle->captured_count > 0) {
943                         g_mutex_lock(&handle->buffer_lock);
944                         if (handle->state != CAMERA_STATE_CAPTURING) {
945                                 LOGW("stop continuous capture");
946                                 handle->captured_count = handle->capture_count;
947                                 g_mutex_unlock(&handle->buffer_lock);
948                                 goto _TRY_NEXT;
949                         }
950                         g_mutex_unlock(&handle->buffer_lock);
951                 }
952
953                 if (handle->capture_count > 1) {
954                         current_time = g_get_monotonic_time();
955
956                         LOGI("time[prev:%"PRId64", cur:%"PRId64"] interval[%"PRId64" us]",
957                                 previous_time, current_time, interval_us);
958
959                         if (current_time < previous_time + interval_us)
960                                 goto _TRY_NEXT;
961                 }
962
963                 g_mutex_lock(&handle->buffer_lock);
964                 handle->captured_count++;
965                 g_mutex_unlock(&handle->buffer_lock);
966
967                 LOGD("capture cb[%p], buffer index[%d],count[%d]",
968                         handle->capture_cb, buffer_index, handle->captured_count);
969
970                 if (handle->capture_cb) {
971                         handle->capture_cb(&handle->camera_buffers[buffer_index],
972                                 NULL, NULL, handle->capture_cb_data);
973                 } else {
974                         LOGW("capture callback is NULL");
975                         /* Need to post error? */
976                 }
977
978                 previous_time = current_time;
979
980 _TRY_NEXT:
981                 ret = __camera_v4l2_qbuf(handle->device_fd,
982                         handle->buffer_type, V4L2_MEMORY_MMAP, buffer_index);
983                 if (ret != CAMERA_ERROR_NONE)
984                         LOGE("qbuf failed for capture[0x%x]", ret);
985         } while (handle->captured_count < handle->capture_count);
986
987         g_mutex_lock(&handle->buffer_lock);
988
989         if (handle->state == CAMERA_STATE_CAPTURING) {
990                 LOGD("wait for capture stop signal");
991                 g_cond_wait(&handle->buffer_cond, &handle->buffer_lock);
992                 LOGD("signal received");
993         } else {
994                 LOGD("The state is already changed.");
995         }
996
997         g_mutex_unlock(&handle->buffer_lock);
998
999 _CAPTURE_DONE:
1000         /* restart stream for preview */
1001         if (handle->capture_restart_stream) {
1002                 ret = __camera_stop_stream(handle, handle->buffer_count);
1003                 if (ret != CAMERA_ERROR_NONE)
1004                         LOGE("stop stream failed for preview[0x%x]", ret);
1005
1006                 ret = __camera_start_stream(handle,
1007                         handle->preview_format.stream_format,
1008                         &handle->preview_format.stream_resolution,
1009                         handle->preview_format.stream_fps,
1010                         BUFFER_MAX);
1011                 if (ret != CAMERA_ERROR_NONE)
1012                         LOGE("start stream failed for preview[0x%x]", ret);
1013         }
1014
1015         LOGD("done");
1016 }
1017
1018
1019 static void *__camera_buffer_handler_func(gpointer data)
1020 {
1021         int error = CAMERA_ERROR_NONE;
1022         int index = 0;
1023         hal_camera_handle *handle = (hal_camera_handle *)data;
1024
1025         if (!handle) {
1026                 LOGE("NULL handle for buffer handler");
1027                 return NULL;
1028         }
1029
1030         LOGD("enter");
1031
1032         /* run buffer thread */
1033         g_mutex_lock(&handle->buffer_lock);
1034
1035         while (handle->buffer_thread_run) {
1036                 g_mutex_unlock(&handle->buffer_lock);
1037
1038                 if (__camera_v4l2_wait_frame(handle->device_fd, 5) != CAMERA_ERROR_NONE) {
1039                         LOGE("frame wait failed");
1040                         g_mutex_lock(&handle->buffer_lock);
1041                         break;
1042                 }
1043
1044                 g_mutex_lock(&handle->buffer_lock);
1045
1046                 if (handle->buffer_thread_run == FALSE) {
1047                         LOGW("stop buffer handler thread");
1048                         break;
1049                 }
1050
1051                 error = __camera_v4l2_dqbuf(handle->device_fd, handle->buffer_type, V4L2_MEMORY_MMAP, &index);
1052                 if (error != CAMERA_ERROR_NONE) {
1053                         LOGE("dqbuf failed[0x%x]", error);
1054                         break;
1055                 }
1056
1057                 handle->buffer_dequeued_count++;
1058
1059                 /*LOGD("dequeued buffer count %d", handle->buffer_dequeued_count);*/
1060
1061                 g_mutex_unlock(&handle->buffer_lock);
1062
1063                 if (handle->preview_cb) {
1064                         handle->preview_cb(&handle->camera_buffers[index], NULL, handle->preview_cb_data);
1065                 } else {
1066                         LOGW("preview callback is NULL");
1067                         camera_v4l2_release_preview_buffer((void *)handle, index);
1068                 }
1069
1070                 g_mutex_lock(&handle->extra_preview_lock);
1071
1072                 if (handle->extra_preview_cb) {
1073                         handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 0, handle->extra_preview_cb_data);
1074                         handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 1, handle->extra_preview_cb_data);
1075                         handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 2, handle->extra_preview_cb_data);
1076                         handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 3, handle->extra_preview_cb_data);
1077                 }
1078
1079                 g_mutex_unlock(&handle->extra_preview_lock);
1080
1081                 /* check capture request flag */
1082                 if (handle->capture_request) {
1083                         __camera_do_capture(handle);
1084                         handle->capture_request = FALSE;
1085                 }
1086
1087                 sched_yield();
1088
1089                 g_mutex_lock(&handle->buffer_lock);
1090         }
1091
1092         g_mutex_unlock(&handle->buffer_lock);
1093
1094         if (error != CAMERA_ERROR_NONE)
1095                 __camera_send_message(handle, CAMERA_MESSAGE_TYPE_ERROR, error);
1096
1097         LOGD("leave");
1098
1099         return NULL;
1100 }
1101
1102
1103 static void __camera_message_release_func(gpointer data)
1104 {
1105         camera_message_s *message = (camera_message_s *)data;
1106
1107         if (!message) {
1108                 LOGW("NULL message");
1109                 return;
1110         }
1111
1112         LOGD("release message %p, type %d", message, message->type);
1113
1114         g_free(message);
1115
1116         return;
1117 }
1118
1119
1120 static void *_camera_message_handler_func(gpointer data)
1121 {
1122         int i = 0;
1123         camera_message_s *message = NULL;
1124         hal_camera_handle *handle = (hal_camera_handle *)data;
1125
1126         if (!handle) {
1127                 LOGE("NULL handle for capture thread");
1128                 return NULL;
1129         }
1130
1131         LOGD("enter - message thread");
1132
1133         g_mutex_lock(&handle->msg_cb_lock);
1134
1135         while (handle->msg_cb_run) {
1136                 if (g_queue_is_empty(handle->msg_list)) {
1137                         LOGD("wait for message");
1138                         g_cond_wait(&handle->msg_cb_cond, &handle->msg_cb_lock);
1139                         LOGD("message signal received");
1140                 }
1141
1142                 if (!handle->msg_cb_run) {
1143                         LOGW("break message thread");
1144                         break;
1145                 }
1146
1147                 message = g_queue_pop_head(handle->msg_list);
1148                 if (!message) {
1149                         LOGW("NULL message");
1150                         continue;
1151                 }
1152
1153                 g_mutex_unlock(&handle->msg_cb_lock);
1154
1155                 for (i = 0 ; i < MESSAGE_CALLBACK_MAX ; i++) {
1156                         if (handle->msg_cb[i]) {
1157                                 LOGD("call message callback[%d] type[%d]", i, message->type);
1158                                 handle->msg_cb[i](message, handle->msg_cb_data[i]);
1159                         }
1160                 }
1161
1162                 g_free(message);
1163                 message = NULL;
1164
1165                 g_mutex_lock(&handle->msg_cb_lock);
1166         }
1167
1168         g_mutex_unlock(&handle->msg_cb_lock);
1169
1170         LOGD("leave - message thread");
1171
1172         return NULL;
1173 }
1174
1175
1176 static void __camera_release_handle(hal_camera_handle *handle)
1177 {
1178         if (!handle) {
1179                 LOGW("NULL handle");
1180                 return;
1181         }
1182
1183         if (handle->msg_thread) {
1184                 g_mutex_lock(&handle->msg_cb_lock);
1185                 handle->msg_cb_run = FALSE;
1186                 g_cond_signal(&handle->msg_cb_cond);
1187                 g_mutex_unlock(&handle->msg_cb_lock);
1188                 g_thread_join(handle->msg_thread);
1189                 g_queue_free_full(handle->msg_list, (GDestroyNotify)__camera_message_release_func);
1190                 handle->msg_list = NULL;
1191         }
1192
1193         g_mutex_clear(&handle->lock);
1194         g_mutex_clear(&handle->buffer_lock);
1195         g_mutex_clear(&handle->msg_cb_lock);
1196         g_mutex_clear(&handle->extra_preview_lock);
1197         g_cond_clear(&handle->buffer_cond);
1198         g_cond_clear(&handle->msg_cb_cond);
1199
1200         if (handle->bufmgr) {
1201                 tbm_bufmgr_deinit(handle->bufmgr);
1202                 handle->bufmgr = NULL;
1203         }
1204
1205         LOGD("camera HAL handle %p destroy", handle);
1206
1207         g_free(handle);
1208
1209         return;
1210 }
1211
1212
1213 int camera_v4l2_init(void **camera_handle)
1214 {
1215         int ret = CAMERA_ERROR_NONE;
1216         hal_camera_handle *new_handle = NULL;
1217         tbm_bufmgr bufmgr = NULL;
1218
1219         LOGD("enter");
1220
1221         if (!camera_handle) {
1222                 LOGE("NULL pointer for handle");
1223                 return CAMERA_ERROR_INVALID_PARAMETER;
1224         }
1225
1226         bufmgr = tbm_bufmgr_init(-1);
1227         if (bufmgr == NULL) {
1228                 LOGE("get tbm bufmgr failed");
1229                 return CAMERA_ERROR_INTERNAL;
1230         }
1231
1232         new_handle = g_new0(hal_camera_handle, 1);
1233         if (!new_handle) {
1234                 LOGE("failed to alloc camera hal handle");
1235                 tbm_bufmgr_deinit(bufmgr);
1236                 return CAMERA_ERROR_OUT_OF_MEMORY;
1237         }
1238
1239         new_handle->bufmgr = bufmgr;
1240
1241         g_mutex_init(&new_handle->lock);
1242         g_mutex_init(&new_handle->buffer_lock);
1243         g_mutex_init(&new_handle->msg_cb_lock);
1244         g_mutex_init(&new_handle->extra_preview_lock);
1245         g_cond_init(&new_handle->buffer_cond);
1246         g_cond_init(&new_handle->msg_cb_cond);
1247
1248         /* message thread */
1249         new_handle->msg_list = g_queue_new();
1250         new_handle->msg_cb_run = TRUE;
1251         new_handle->msg_thread = g_thread_try_new("camera_hal_msg_thread",
1252                 _camera_message_handler_func, (gpointer)new_handle, NULL);
1253         if (!new_handle->msg_thread) {
1254                 LOGE("failed to create message thread");
1255                 ret = CAMERA_ERROR_INTERNAL;
1256                 goto _INIT_ERROR;
1257         }
1258
1259         new_handle->device_index = CAMERA_HAL_INITIAL_INDEX;
1260         new_handle->device_fd = CAMERA_HAL_INITIAL_FD;
1261         new_handle->state = CAMERA_STATE_INITIALIZED;
1262
1263         ret = __camera_get_device_info_list();
1264         if (ret != CAMERA_ERROR_NONE) {
1265                 LOGE("get device info failed");
1266                 goto _INIT_ERROR;
1267         }
1268
1269 #ifdef HAVE_LIBV4L2
1270         LOGI("libv4l2 ENABLED");
1271 #else /* HAVE_LIBV4L2 */
1272         LOGI("libv4l2 DISABLED");
1273 #endif /* HAVE_LIBV4L2 */
1274
1275         *camera_handle = new_handle;
1276
1277         LOGD("camera HAL handle %p", new_handle);
1278
1279         return CAMERA_ERROR_NONE;
1280
1281 _INIT_ERROR:
1282         __camera_release_handle(new_handle);
1283
1284         return ret;
1285 }
1286
1287
1288 int camera_v4l2_deinit(void *camera_handle)
1289 {
1290         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1291
1292         if (!handle) {
1293                 LOGE("NULL handle");
1294                 return CAMERA_ERROR_INVALID_PARAMETER;
1295         }
1296
1297         g_mutex_lock(&handle->lock);
1298
1299         if (handle->state != CAMERA_STATE_INITIALIZED) {
1300                 LOGE("invalid state %d, can not destroy handle", handle->state);
1301                 g_mutex_unlock(&handle->lock);
1302                 return CAMERA_ERROR_INVALID_STATE;
1303         }
1304
1305         g_mutex_unlock(&handle->lock);
1306
1307         __camera_release_handle(handle);
1308
1309         return CAMERA_ERROR_NONE;
1310 }
1311
1312
1313 int camera_v4l2_get_device_info_list(camera_device_info_list_s *device_info_list)
1314 {
1315         int ret = 0;
1316
1317         if (!device_info_list) {
1318                 LOGE("NULL pointer for device_info_list");
1319                 return CAMERA_ERROR_INVALID_PARAMETER;
1320         }
1321
1322         ret = __camera_get_device_info_list();
1323         if (ret != CAMERA_ERROR_NONE) {
1324                 LOGE("get device info failed");
1325                 return ret;
1326         }
1327
1328         memcpy(device_info_list, g_device_info_list, sizeof(camera_device_info_list_s));
1329
1330         return CAMERA_ERROR_NONE;
1331 }
1332
1333
1334 int camera_v4l2_open_device(void *camera_handle, int device_index)
1335 {
1336         int ret = CAMERA_ERROR_NONE;
1337         int device_fd = CAMERA_HAL_INITIAL_FD;
1338 #ifdef HAVE_LIBV4L2
1339         int libv4l2_fd = CAMERA_HAL_INITIAL_FD;
1340 #endif /* HAVE_LIBV4L2 */
1341         char *node_path = NULL;
1342         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1343
1344         if (!handle) {
1345                 LOGE("NULL handle");
1346                 return CAMERA_ERROR_INVALID_PARAMETER;
1347         }
1348
1349         g_mutex_lock(&handle->lock);
1350
1351         if (handle->state != CAMERA_STATE_INITIALIZED) {
1352                 LOGE("invalid state %d", handle->state);
1353                 ret = CAMERA_ERROR_INVALID_STATE;
1354                 goto _OPEN_DEVICE_DONE;
1355         }
1356
1357         if (!g_device_info_list) {
1358                 LOGE("NO DEVICE INFO");
1359                 ret = CAMERA_ERROR_INTERNAL;
1360                 goto _OPEN_DEVICE_DONE;
1361         }
1362
1363         if (device_index >= g_device_info_list->count) {
1364                 LOGE("invalid index %d [info:%d]", device_index, g_device_info_list->count);
1365                 ret = CAMERA_ERROR_INVALID_PARAMETER;
1366                 goto _OPEN_DEVICE_DONE;
1367         }
1368
1369         node_path = g_device_info_list->device_info[device_index].node_path;
1370
1371         device_fd = open(node_path, O_RDWR);
1372         if (device_fd < 0) {
1373                 switch (errno) {
1374                         case EACCES:
1375                         case EPERM:
1376                                 ret = CAMERA_ERROR_PERMISSION_DENIED;
1377                                 break;
1378                         case ENOENT:
1379                                 ret = CAMERA_ERROR_DEVICE_NOT_FOUND;
1380                                 break;
1381                         case EBUSY:
1382                                 ret = CAMERA_ERROR_DEVICE_BUSY;
1383                                 break;
1384                         default:
1385                                 ret = CAMERA_ERROR_DEVICE_OPEN;
1386                                 break;
1387                 }
1388
1389                 LOGE("open [%s] failed 0x%x [errno %d]",
1390                         node_path, ret, errno);
1391
1392                 goto _OPEN_DEVICE_DONE;
1393         }
1394
1395         if (g_device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
1396                 handle->buffer_type = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1397         else
1398                 handle->buffer_type = V4L2_CAP_VIDEO_CAPTURE;
1399
1400 #ifdef HAVE_LIBV4L2
1401         libv4l2_fd = v4l2_fd_open(device_fd, V4L2_ENABLE_ENUM_FMT_EMULATION);
1402
1403         LOGI("device_fd[%d], libv4l2_fd[%d]", device_fd, libv4l2_fd);
1404
1405         if (libv4l2_fd != CAMERA_HAL_INITIAL_FD)
1406                 device_fd = libv4l2_fd;
1407 #endif /* HAVE_LIBV4L2 */
1408
1409         handle->state = CAMERA_STATE_OPENED;
1410         handle->device_index = device_index;
1411         handle->device_fd = device_fd;
1412
1413         LOGD("[%d] device[%s] opened [fd %d, type %d]",
1414                 device_index, node_path, device_fd, handle->buffer_type);
1415
1416 _OPEN_DEVICE_DONE:
1417         g_mutex_unlock(&handle->lock);
1418
1419         return ret;
1420 }
1421
1422
1423 int camera_v4l2_open_device_ext(void *camera_handle, const char *device_name)
1424 {
1425         LOGE("NOT SUPPORTED");
1426         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1427 }
1428
1429
1430 int camera_v4l2_close_device(void *camera_handle)
1431 {
1432         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1433
1434         if (!handle) {
1435                 LOGE("NULL handle");
1436                 return CAMERA_ERROR_INVALID_PARAMETER;
1437         }
1438
1439         g_mutex_lock(&handle->lock);
1440
1441         if (handle->state != CAMERA_STATE_OPENED) {
1442                 LOGE("invalid state %d", handle->state);
1443                 g_mutex_unlock(&handle->lock);
1444                 return CAMERA_ERROR_INVALID_STATE;
1445         }
1446
1447         if (handle->device_fd >= 0) {
1448                 LOGD("close fd %d", handle->device_fd);
1449
1450                 v4l2_close(handle->device_fd);
1451                 handle->device_fd = CAMERA_HAL_INITIAL_FD;
1452         } else {
1453                 LOGW("invalid fd %d", handle->device_fd);
1454         }
1455
1456         handle->state = CAMERA_STATE_INITIALIZED;
1457
1458         LOGD("device [%d] closed", handle->device_index);
1459
1460         g_mutex_unlock(&handle->lock);
1461
1462         return CAMERA_ERROR_NONE;
1463 }
1464
1465
1466 int camera_v4l2_add_message_callback(void *camera_handle, hal_camera_message_cb callback, void *user_data, uint32_t *cb_id)
1467 {
1468         uint32_t i = 0;
1469         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1470
1471         if (!handle) {
1472                 LOGE("NULL handle");
1473                 return CAMERA_ERROR_INVALID_PARAMETER;
1474         }
1475
1476         if (!callback || !cb_id) {
1477                 LOGE("NULL pointer for callback %p or cb_id %p", callback, cb_id);
1478                 return CAMERA_ERROR_INVALID_PARAMETER;
1479         }
1480
1481         g_mutex_lock(&handle->lock);
1482
1483         for (i = 0 ; i < MESSAGE_CALLBACK_MAX ; i++) {
1484                 if (handle->msg_cb[i] == NULL) {
1485                         handle->msg_cb[i] = callback;
1486                         handle->msg_cb_data[i] = user_data;
1487                         *cb_id = i;
1488                         LOGD("message cb [%p] added, user data %p - id %u", callback, user_data, i);
1489                         g_mutex_unlock(&handle->lock);
1490                         return CAMERA_ERROR_NONE;
1491                 }
1492         }
1493
1494         g_mutex_unlock(&handle->lock);
1495
1496         LOGE("no available message cb slot");
1497
1498         return CAMERA_ERROR_INTERNAL;
1499 }
1500
1501
1502 int camera_v4l2_remove_message_callback(void *camera_handle, uint32_t cb_id)
1503 {
1504         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1505
1506         if (!handle) {
1507                 LOGE("NULL handle");
1508                 return CAMERA_ERROR_INVALID_PARAMETER;
1509         }
1510
1511         if (cb_id >= MESSAGE_CALLBACK_MAX) {
1512                 LOGE("invalid cb_id %u", cb_id);
1513                 return CAMERA_ERROR_INVALID_PARAMETER;
1514         }
1515
1516         g_mutex_lock(&handle->lock);
1517
1518         if (handle->msg_cb[cb_id]) {
1519                 LOGD("remove message callback %p, user data %p - cb_id %u",
1520                         handle->msg_cb[cb_id], handle->msg_cb_data[cb_id], cb_id);
1521
1522                 handle->msg_cb[cb_id] = NULL;
1523                 handle->msg_cb_data[cb_id] = NULL;
1524         } else {
1525                 LOGE("already removed message cb");
1526                 g_mutex_unlock(&handle->lock);
1527                 return CAMERA_ERROR_INTERNAL;
1528         }
1529
1530         g_mutex_unlock(&handle->lock);
1531
1532         return CAMERA_ERROR_NONE;
1533 }
1534
1535
1536 int camera_v4l2_set_preview_stream_format(void *camera_handle, camera_format_s *format)
1537 {
1538         int i = 0;
1539         int j = 0;
1540         int ret = CAMERA_ERROR_NONE;
1541         gboolean capability_check = FALSE;
1542         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1543         camera_device_info_s *device_info = NULL;
1544
1545         if (!handle || !format) {
1546                 LOGE("NULL param %p %p", handle, format);
1547                 return CAMERA_ERROR_INVALID_PARAMETER;
1548         }
1549
1550         if (!g_device_info_list) {
1551                 LOGE("no device info list");
1552                 return CAMERA_ERROR_INTERNAL;
1553         }
1554
1555         g_mutex_lock(&handle->lock);
1556
1557         if (handle->state != CAMERA_STATE_OPENED &&
1558                 handle->state != CAMERA_STATE_PREVIEWING) {
1559                 LOGE("invalid state %d", handle->state);
1560                 g_mutex_unlock(&handle->lock);
1561                 return CAMERA_ERROR_INVALID_STATE;
1562         }
1563
1564         /* check capability */
1565         device_info = &g_device_info_list->device_info[handle->device_index];
1566
1567         /* format */
1568         for (i = 0 ; i < device_info->format_list.count ; i++) {
1569                 if (format->stream_format == device_info->format_list.formats[i]) {
1570                         LOGD("format matched %d, check resolution.", format->stream_format);
1571
1572                         /* resolution */
1573                         for (j = 0 ; j < device_info->preview_list.count ; j++) {
1574                                 if (format->stream_resolution.width == device_info->preview_list.resolutions[j].width &&
1575                                         format->stream_resolution.height == device_info->preview_list.resolutions[j].height) {
1576                                         LOGD("resolution matched %dx%d",
1577                                                 format->stream_resolution.width,
1578                                                 format->stream_resolution.height);
1579                                         capability_check = TRUE;
1580                                         break;
1581                                 }
1582                         }
1583
1584                         break;
1585                 }
1586         }
1587
1588         if (!capability_check) {
1589                 LOGE("capability failed - %d, %dx%d",
1590                         format->stream_format,
1591                         format->stream_resolution.width,
1592                         format->stream_resolution.height);
1593                 g_mutex_unlock(&handle->lock);
1594                 return CAMERA_ERROR_INVALID_PARAMETER;
1595         }
1596
1597         /* compare with current settings */
1598         if (handle->state == CAMERA_STATE_PREVIEWING) {
1599                 if (handle->preview_format.stream_format == format->stream_format &&
1600                         handle->preview_format.stream_resolution.width == format->stream_resolution.width &&
1601                         handle->preview_format.stream_resolution.height == format->stream_resolution.height &&
1602                         handle->preview_format.stream_fps == format->stream_fps &&
1603                         handle->preview_format.stream_rotation == format->stream_rotation) {
1604                         LOGD("no need to restart preview stream");
1605                         goto _SET_PREVIEW_STREAM_FORMAT_DONE;
1606                 }
1607
1608                 LOGD("Preview setting is changed. Restart preview now.");
1609
1610                 /* stop preview stream to change it */
1611                 ret = __camera_stop_stream(handle, handle->buffer_count);
1612                 if (ret != CAMERA_ERROR_NONE) {
1613                         LOGE("failed to stop stream");
1614                         g_mutex_unlock(&handle->lock);
1615                         return ret;
1616                 }
1617
1618                 /* restart preview stream to change it */
1619                 ret = __camera_start_stream(handle,
1620                         format->stream_format,
1621                         &format->stream_resolution,
1622                         format->stream_fps,
1623                         BUFFER_MAX);
1624                 if (ret != CAMERA_ERROR_NONE) {
1625                         LOGE("failed to start stream");
1626                         g_mutex_unlock(&handle->lock);
1627                         return ret;
1628                 }
1629         }
1630
1631 _SET_PREVIEW_STREAM_FORMAT_DONE:
1632         /* set capture restart flag */
1633         if (format->stream_format == format->capture_format &&
1634                 format->stream_resolution.width == format->capture_resolution.width &&
1635                 format->stream_resolution.height == format->capture_resolution.height)
1636                 handle->capture_restart_stream = FALSE;
1637         else
1638                 handle->capture_restart_stream = TRUE;
1639
1640         memcpy(&handle->preview_format, format, sizeof(camera_format_s));
1641
1642         LOGD("set format PREVIEW[%d:%dx%d,fps:%d], CAPTURE[%d:%dx%d,restart:%d]",
1643                 format->stream_format,
1644                 format->stream_resolution.width,
1645                 format->stream_resolution.height,
1646                 format->stream_fps,
1647                 format->capture_format,
1648                 format->capture_resolution.width,
1649                 format->capture_resolution.height,
1650                 handle->capture_restart_stream);
1651
1652         g_mutex_unlock(&handle->lock);
1653
1654         return CAMERA_ERROR_NONE;
1655 }
1656
1657
1658 int camera_v4l2_get_preview_stream_format(void *camera_handle, camera_format_s *format)
1659 {
1660         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1661
1662         if (!handle || !format) {
1663                 LOGE("NULL param %p %p", handle, format);
1664                 return CAMERA_ERROR_INVALID_PARAMETER;
1665         }
1666
1667         g_mutex_lock(&handle->lock);
1668
1669         memcpy(format, &handle->preview_format, sizeof(camera_format_s));
1670
1671         LOGD("get stream format %d, %dx%d", format->stream_format,
1672                 format->stream_resolution.width, format->stream_resolution.height);
1673
1674         g_mutex_unlock(&handle->lock);
1675
1676         return CAMERA_ERROR_NONE;
1677 }
1678
1679
1680 int camera_v4l2_set_user_buffer_fd(void *camera_handle, int *fds, int number)
1681 {
1682         LOGE("NOT SUPPORTED");
1683         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1684 }
1685
1686
1687 int camera_v4l2_start_preview(void *camera_handle, hal_camera_preview_frame_cb callback, void *user_data)
1688 {
1689         int ret = 0;
1690         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1691
1692         if (!handle || !callback) {
1693                 LOGE("NULL param %p %p", handle, callback);
1694                 return CAMERA_ERROR_INVALID_PARAMETER;
1695         }
1696
1697         g_mutex_lock(&handle->lock);
1698
1699         if (handle->state != CAMERA_STATE_OPENED) {
1700                 LOGE("invalid state %d", handle->state);
1701                 g_mutex_unlock(&handle->lock);
1702                 return CAMERA_ERROR_INVALID_STATE;
1703         }
1704
1705         ret = __camera_start_stream(handle,
1706                 handle->preview_format.stream_format,
1707                 &handle->preview_format.stream_resolution,
1708                 handle->preview_format.stream_fps,
1709                 BUFFER_MAX);
1710         if (ret != CAMERA_ERROR_NONE) {
1711                 LOGE("__camera_start_stream failed[0x%x]", ret);
1712                 g_mutex_unlock(&handle->lock);
1713                 return ret;
1714         }
1715
1716         g_mutex_lock(&handle->buffer_lock);
1717
1718         handle->buffer_thread_run = TRUE;
1719
1720         handle->buffer_thread = g_thread_try_new("camera_hal_buffer_thread",
1721                 __camera_buffer_handler_func, (gpointer)handle, NULL);
1722         if (!handle->buffer_thread) {
1723                 LOGE("failed to create buffer handler thread");
1724                 g_mutex_unlock(&handle->buffer_lock);
1725
1726                 __camera_stop_stream(handle, handle->buffer_count);
1727
1728                 g_mutex_unlock(&handle->lock);
1729
1730                 return ret;
1731         }
1732
1733         handle->preview_cb = callback;
1734         handle->preview_cb_data = user_data;
1735
1736         g_mutex_unlock(&handle->buffer_lock);
1737
1738         handle->state = CAMERA_STATE_PREVIEWING;
1739
1740         LOGD("start preview done");
1741
1742         g_mutex_unlock(&handle->lock);
1743
1744         return CAMERA_ERROR_NONE;
1745 }
1746
1747
1748 int camera_v4l2_release_preview_buffer(void *camera_handle, int buffer_index)
1749 {
1750         int ret = CAMERA_ERROR_NONE;
1751         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1752
1753         if (!handle) {
1754                 LOGE("NULL handle");
1755                 return CAMERA_ERROR_INVALID_PARAMETER;
1756         }
1757
1758         if (buffer_index >= handle->buffer_count) {
1759                 LOGE("invalid buffer index %d", buffer_index);
1760                 return CAMERA_ERROR_INVALID_PARAMETER;
1761         }
1762
1763         ret = __camera_v4l2_qbuf(handle->device_fd,
1764                 handle->buffer_type, V4L2_MEMORY_MMAP, buffer_index);
1765
1766         g_mutex_lock(&handle->buffer_lock);
1767
1768         if (ret == CAMERA_ERROR_NONE) {
1769                 if (handle->buffer_dequeued_count > 0)
1770                         handle->buffer_dequeued_count--;
1771                 else
1772                         LOGW("invalid dequeued buffer count[%u]", handle->buffer_dequeued_count);
1773
1774                 /*LOGD("qbud done : index %d, dequeued buffer count %d",
1775                         buffer_index, handle->buffer_dequeued_count);*/
1776         } else {
1777                 LOGE("qbuf failed [index %d]", buffer_index);
1778         }
1779
1780         g_cond_signal(&handle->buffer_cond);
1781
1782         g_mutex_unlock(&handle->buffer_lock);
1783
1784         return ret;
1785 }
1786
1787
1788 int camera_v4l2_stop_preview(void *camera_handle)
1789 {
1790         int ret = CAMERA_ERROR_NONE;
1791         gint64 end_time;
1792         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1793
1794         if (!handle) {
1795                 LOGE("NULL handle");
1796                 return CAMERA_ERROR_INVALID_PARAMETER;
1797         }
1798
1799         LOGD("start");
1800
1801         g_mutex_lock(&handle->lock);
1802
1803         if (handle->state != CAMERA_STATE_PREVIEWING) {
1804                 LOGE("invalid state %d", handle->state);
1805                 g_mutex_unlock(&handle->lock);
1806                 return CAMERA_ERROR_INVALID_STATE;
1807         }
1808
1809         g_mutex_lock(&handle->buffer_lock);
1810
1811         handle->buffer_thread_run = FALSE;
1812
1813         while (handle->buffer_dequeued_count > 0) {
1814                 LOGD("wait for dequeued buffer [%d]", handle->buffer_dequeued_count);
1815                 end_time = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
1816                 if (!g_cond_wait_until(&handle->buffer_cond, &handle->buffer_lock, end_time)) {
1817                         LOGE("buffer wait failed");
1818                         break;
1819                 } else {
1820                         LOGD("signal received. check again...");
1821                 }
1822         }
1823
1824         g_mutex_unlock(&handle->buffer_lock);
1825
1826         ret = __camera_stop_stream(handle, handle->buffer_count);
1827
1828         /* wait for preview thread exit */
1829         g_thread_join(handle->buffer_thread);
1830         handle->buffer_thread = NULL;
1831
1832         handle->state = CAMERA_STATE_OPENED;
1833
1834         LOGD("stop preview done [0x%x]", ret);
1835
1836         g_mutex_unlock(&handle->lock);
1837
1838         return CAMERA_ERROR_NONE;
1839 }
1840
1841
1842 int camera_v4l2_start_auto_focus(void *camera_handle)
1843 {
1844         if (!camera_handle) {
1845                 LOGE("NULL handle");
1846                 return CAMERA_ERROR_INVALID_PARAMETER;
1847         }
1848
1849         LOGE("NOT SUPPORTED");
1850
1851         /* auto focus is not supported */
1852         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1853 }
1854
1855
1856 int camera_v4l2_stop_auto_focus(void *camera_handle)
1857 {
1858         if (!camera_handle) {
1859                 LOGE("NULL handle");
1860                 return CAMERA_ERROR_INVALID_PARAMETER;
1861         }
1862
1863         LOGE("NOT SUPPORTED");
1864
1865         /* auto focus is not supported */
1866         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1867 }
1868
1869
1870 int camera_v4l2_start_capture(void *camera_handle, hal_camera_capture_cb callback, void *user_data)
1871 {
1872         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1873
1874         if (!handle || !callback) {
1875                 LOGE("NULL param %p %p", camera_handle, callback);
1876                 return CAMERA_ERROR_INVALID_PARAMETER;
1877         }
1878
1879         g_mutex_lock(&handle->lock);
1880
1881         if (handle->state != CAMERA_STATE_PREVIEWING) {
1882                 LOGE("invalid state %d", handle->state);
1883                 g_mutex_unlock(&handle->lock);
1884                 return CAMERA_ERROR_INVALID_STATE;
1885         }
1886
1887         /* set callback and user data */
1888         handle->capture_cb = callback;
1889         handle->capture_cb_data = user_data;
1890
1891         /* reset captured count */
1892         handle->captured_count = 0;
1893
1894         LOGD("start capture - count %u", handle->capture_count);
1895
1896         /* set capture request flag */
1897         handle->capture_request = TRUE;
1898
1899         handle->state = CAMERA_STATE_CAPTURING;
1900
1901         g_mutex_unlock(&handle->lock);
1902
1903         return CAMERA_ERROR_NONE;
1904 }
1905
1906
1907 int camera_v4l2_stop_capture(void *camera_handle)
1908 {
1909         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1910
1911         if (!handle) {
1912                 LOGE("NULL handle");
1913                 return CAMERA_ERROR_INVALID_PARAMETER;
1914         }
1915
1916         g_mutex_lock(&handle->lock);
1917
1918         if (handle->state != CAMERA_STATE_CAPTURING) {
1919                 LOGE("invalid state %d", handle->state);
1920                 g_mutex_unlock(&handle->lock);
1921                 return CAMERA_ERROR_INVALID_STATE;
1922         }
1923
1924         g_mutex_lock(&handle->buffer_lock);
1925
1926         if (handle->captured_count == 0) {
1927                 LOGE("No captured image yet.");
1928                 g_mutex_unlock(&handle->buffer_lock);
1929                 g_mutex_unlock(&handle->lock);
1930                 return CAMERA_ERROR_INTERNAL;
1931         }
1932
1933         LOGD("send signal to start preview after capture");
1934
1935         g_cond_signal(&handle->buffer_cond);
1936         g_mutex_unlock(&handle->buffer_lock);
1937
1938         handle->state = CAMERA_STATE_PREVIEWING;
1939
1940         g_mutex_unlock(&handle->lock);
1941
1942         return CAMERA_ERROR_NONE;
1943 }
1944
1945
1946 int camera_v4l2_set_video_stream_format(void *camera_handle, camera_format_s *format)
1947 {
1948         if (!camera_handle || !format) {
1949                 LOGE("NULL param %p %p", camera_handle, format);
1950                 return CAMERA_ERROR_INVALID_PARAMETER;
1951         }
1952
1953         LOGE("NOT SUPPORTED");
1954
1955         /* single stream device can not support video stream */
1956         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1957 }
1958
1959
1960 int camera_v4l2_get_video_stream_format(void *camera_handle, camera_format_s *format)
1961 {
1962         if (!camera_handle || !format) {
1963                 LOGE("NULL param %p %p", camera_handle, format);
1964                 return CAMERA_ERROR_INVALID_PARAMETER;
1965         }
1966
1967         LOGE("NOT SUPPORTED");
1968
1969         /* single stream device can not support video stream */
1970         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1971 }
1972
1973
1974 int camera_v4l2_start_record(void *camera_handle, hal_camera_video_frame_cb callback, void *user_data)
1975 {
1976         if (!camera_handle || !callback) {
1977                 LOGE("NULL param %p %p", camera_handle, callback);
1978                 return CAMERA_ERROR_INVALID_PARAMETER;
1979         }
1980
1981         LOGE("NOT SUPPORTED");
1982
1983         /* single stream device can not support video stream */
1984         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1985 }
1986
1987
1988 int camera_v4l2_release_video_buffer(void *camera_handle, int buffer_index)
1989 {
1990         if (!camera_handle) {
1991                 LOGE("NULL handle");
1992                 return CAMERA_ERROR_INVALID_PARAMETER;
1993         }
1994
1995         LOGE("NOT SUPPORTED");
1996
1997         /* single stream device can not support video stream */
1998         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1999 }
2000
2001
2002 int camera_v4l2_stop_record(void *camera_handle)
2003 {
2004         if (!camera_handle) {
2005                 LOGE("NULL handle");
2006                 return CAMERA_ERROR_INVALID_PARAMETER;
2007         }
2008
2009         LOGE("NOT SUPPORTED");
2010
2011         /* single stream device can not support video stream */
2012         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2013 }
2014
2015
2016 int camera_v4l2_set_extra_preview_frame_cb(void *camera_handle, hal_camera_extra_preview_frame_cb callback, void *user_data)
2017 {
2018         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2019
2020         if (!handle) {
2021                 LOGE("NULL handle");
2022                 return CAMERA_ERROR_INVALID_PARAMETER;
2023         }
2024
2025         g_mutex_lock(&handle->extra_preview_lock);
2026
2027         handle->extra_preview_cb = callback;
2028         handle->extra_preview_cb_data = user_data;
2029
2030         g_mutex_unlock(&handle->extra_preview_lock);
2031
2032         LOGI("done");
2033
2034         return CAMERA_ERROR_NONE;
2035 }
2036
2037
2038 int camera_v4l2_unset_extra_preview_frame_cb(void *camera_handle)
2039 {
2040         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2041
2042         if (!handle) {
2043                 LOGE("NULL handle");
2044                 return CAMERA_ERROR_INVALID_PARAMETER;
2045         }
2046
2047         g_mutex_lock(&handle->extra_preview_lock);
2048
2049         handle->extra_preview_cb = NULL;
2050         handle->extra_preview_cb_data = NULL;
2051
2052         g_mutex_unlock(&handle->extra_preview_lock);
2053
2054         LOGI("done");
2055
2056         return CAMERA_ERROR_NONE;
2057 }
2058
2059
2060 int camera_v4l2_release_extra_preview_buffer(void *camera_handle, int stream_id, int buffer_index)
2061 {
2062         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2063
2064         if (!handle) {
2065                 LOGE("NULL handle");
2066                 return CAMERA_ERROR_INVALID_PARAMETER;
2067         }
2068
2069         LOGI("done - stream_id[%d], index[%d]", stream_id, buffer_index);
2070
2071         return CAMERA_ERROR_NONE;
2072 }
2073
2074
2075 int camera_v4l2_set_extra_preview_stream_format(void *camera_handle, int stream_id, camera_format_s *format)
2076 {
2077         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2078
2079         if (!handle) {
2080                 LOGE("NULL handle");
2081                 return CAMERA_ERROR_INVALID_PARAMETER;
2082         }
2083
2084         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2085                 LOGE("invalid stream_id[%d]", stream_id);
2086                 return CAMERA_ERROR_INVALID_PARAMETER;
2087         }
2088
2089         LOGI("stream_id[%d], [%d,%dx%d,%d]",
2090                 stream_id, format->stream_format,
2091                 format->stream_resolution.width, format->stream_resolution.height,
2092                 format->stream_fps);
2093
2094         memcpy(&handle->extra_preview_format[stream_id], format, sizeof(camera_format_s));
2095
2096         return CAMERA_ERROR_NONE;
2097 }
2098
2099
2100 int camera_v4l2_get_extra_preview_stream_format(void *camera_handle, int stream_id, camera_format_s *format)
2101 {
2102         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2103
2104         if (!handle || !format) {
2105                 LOGE("NULL param[%p,%p]", handle, format);
2106                 return CAMERA_ERROR_INVALID_PARAMETER;
2107         }
2108
2109         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2110                 LOGE("invalid stream_id[%d]", stream_id);
2111                 return CAMERA_ERROR_INVALID_PARAMETER;
2112         }
2113
2114         memcpy(format, &handle->extra_preview_format[stream_id], sizeof(camera_format_s));
2115
2116         LOGI("stream_id[%d], [%d,%dx%d,%d]",
2117                 stream_id, format->stream_format,
2118                 format->stream_resolution.width, format->stream_resolution.height,
2119                 format->stream_fps);
2120
2121         return CAMERA_ERROR_NONE;
2122 }
2123
2124
2125 int camera_v4l2_set_extra_preview_bitrate(void *camera_handle, int stream_id, int bitrate)
2126 {
2127         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2128
2129         if (!handle) {
2130                 LOGE("NULL handle");
2131                 return CAMERA_ERROR_INVALID_PARAMETER;
2132         }
2133
2134         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2135                 LOGE("invalid stream_id[%d]", stream_id);
2136                 return CAMERA_ERROR_INVALID_PARAMETER;
2137         }
2138
2139         LOGI("set bitrate[%d] for stream_id[%d]", bitrate, stream_id);
2140
2141         handle->extra_preview_format[stream_id].stream_bitrate = (uint32_t)bitrate;
2142
2143         return CAMERA_ERROR_NONE;
2144 }
2145
2146
2147 int camera_v4l2_get_extra_preview_bitrate(void *camera_handle, int stream_id, int *bitrate)
2148 {
2149         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2150
2151         if (!handle || !bitrate) {
2152                 LOGE("NULL param[%p,%p]", handle, bitrate);
2153                 return CAMERA_ERROR_INVALID_PARAMETER;
2154         }
2155
2156         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2157                 LOGE("invalid stream_id[%d]", stream_id);
2158                 return CAMERA_ERROR_INVALID_PARAMETER;
2159         }
2160
2161         *bitrate = (int)handle->extra_preview_format[stream_id].stream_bitrate;
2162
2163         LOGI("get bitrate[%d] for stream_id[%d]", *bitrate, stream_id);
2164
2165         return CAMERA_ERROR_NONE;
2166 }
2167
2168
2169 int camera_v4l2_set_extra_preview_gop_interval(void *camera_handle, int stream_id, int interval)
2170 {
2171         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2172
2173         if (!handle) {
2174                 LOGE("NULL handle");
2175                 return CAMERA_ERROR_INVALID_PARAMETER;
2176         }
2177
2178         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2179                 LOGE("invalid stream_id[%d]", stream_id);
2180                 return CAMERA_ERROR_INVALID_PARAMETER;
2181         }
2182
2183         LOGI("set GOP interval[%d] for stream_id[%d]", interval, stream_id);
2184
2185         handle->extra_preview_gop_interval[stream_id] = interval;
2186
2187         return CAMERA_ERROR_NONE;
2188 }
2189
2190
2191 int camera_v4l2_get_extra_preview_gop_interval(void *camera_handle, int stream_id, int *interval)
2192 {
2193         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2194
2195         if (!handle || !interval) {
2196                 LOGE("NULL param[%p,%p]", handle, interval);
2197                 return CAMERA_ERROR_INVALID_PARAMETER;
2198         }
2199
2200         if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2201                 LOGE("invalid stream_id[%d]", stream_id);
2202                 return CAMERA_ERROR_INVALID_PARAMETER;
2203         }
2204
2205         *interval = (int)handle->extra_preview_gop_interval[stream_id];
2206
2207         LOGI("get GOP interval[%d] for stream_id[%d]", *interval, stream_id);
2208
2209         return CAMERA_ERROR_NONE;
2210 }
2211
2212
2213 static int __set_command(hal_camera_handle *handle, int64_t command, void *value)
2214 {
2215         int cid = 0;
2216         int ctrl_ret = 0;
2217         int set_value = 0;
2218
2219         if (!handle || !value) {
2220                 LOGE("NULL param %p %p", handle, value);
2221                 return CAMERA_ERROR_INVALID_PARAMETER;
2222         }
2223
2224         set_value = *(int *)value;
2225
2226         if (handle->state < CAMERA_STATE_OPENED) {
2227                 LOGE("invalid state %d", handle->state);
2228                 return CAMERA_ERROR_INVALID_STATE;
2229         }
2230
2231         LOGD("set command %"PRIx64" - state %d", command, handle->state);
2232
2233         switch (command) {
2234         case CAMERA_COMMAND_BRIGHTNESS:
2235                 cid = V4L2_CID_BRIGHTNESS;
2236                 break;
2237         case CAMERA_COMMAND_CONTRAST:
2238                 cid = V4L2_CID_CONTRAST;
2239                 break;
2240         case CAMERA_COMMAND_SATURATION:
2241                 cid = V4L2_CID_SATURATION;
2242                 break;
2243         case CAMERA_COMMAND_SHARPNESS:
2244                 cid = V4L2_CID_SHARPNESS;
2245                 break;
2246         case CAMERA_COMMAND_PTZ_TYPE:
2247                 if (set_value != CAMERA_PTZ_TYPE_ELECTRONIC) {
2248                         LOGE("not supported PTZ type %d", set_value);
2249                         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2250                 }
2251                 return CAMERA_ERROR_NONE;
2252         case CAMERA_COMMAND_PAN:
2253                 cid = V4L2_CID_PAN_ABSOLUTE;
2254                 break;
2255         case CAMERA_COMMAND_TILT:
2256                 cid = V4L2_CID_TILT_ABSOLUTE;
2257                 break;
2258         case CAMERA_COMMAND_FLIP:
2259                 if (set_value != CAMERA_FLIP_NONE) {
2260                         LOGE("NOT_SUPPORTED flip %d", set_value);
2261                         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2262                 }
2263                 return CAMERA_ERROR_NONE;
2264         case CAMERA_COMMAND_CAPTURE_COUNT:
2265                 handle->capture_count = set_value;
2266                 LOGI("capture count %u", handle->capture_count);
2267                 return CAMERA_ERROR_NONE;
2268         case CAMERA_COMMAND_CAPTURE_INTERVAL:
2269                 handle->capture_interval_ms = set_value;
2270                 LOGI("capture interval %u ms", handle->capture_interval_ms);
2271                 return CAMERA_ERROR_NONE;
2272         case CAMERA_COMMAND_FOCUS_MODE:
2273                 LOGI("set focus mode [old:%d -> new:%d]", handle->focus_mode, set_value);
2274                 handle->focus_mode = set_value;
2275                 return CAMERA_ERROR_NONE;
2276         case CAMERA_COMMAND_FOCUS_RANGE:
2277                 LOGI("set focus range [old:%d -> new:%d]", handle->focus_range, set_value);
2278                 handle->focus_range = set_value;
2279                 return CAMERA_ERROR_NONE;
2280         case CAMERA_COMMAND_FOCUS_LEVEL:
2281                 LOGI("set focus level [old:%d -> new:%d]", handle->focus_level, set_value);
2282                 handle->focus_level = set_value;
2283                 return CAMERA_ERROR_NONE;
2284         default:
2285                 LOGE("NOT_SUPPORTED command %"PRIx64, command);
2286                 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2287         }
2288
2289         ctrl_ret = __camera_v4l2_s_ctrl(handle->device_fd, cid, set_value);
2290         if (ctrl_ret < 0) {
2291                 switch (errno) {
2292                 case EACCES:
2293                 case EPERM:
2294                         LOGE("Permission denied %d", errno);
2295                         return CAMERA_ERROR_PERMISSION_DENIED;
2296                 case EINVAL:
2297                         LOGE("Invalid argument");
2298                         return CAMERA_ERROR_INVALID_PARAMETER;
2299                 case EBUSY:
2300                         LOGE("Device busy");
2301                         return CAMERA_ERROR_DEVICE_BUSY;
2302                 case ENOTSUP:
2303                         LOGE("Not supported");
2304                         return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2305                 default:
2306                         LOGE("Unknown errro %d", errno);
2307                         return CAMERA_ERROR_INTERNAL;
2308                 }
2309         }
2310
2311         return CAMERA_ERROR_NONE;
2312 }
2313
2314
2315 int camera_v4l2_set_command(void *camera_handle, int64_t command, void *value)
2316 {
2317         int ret = CAMERA_ERROR_NONE;
2318         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2319
2320         g_mutex_lock(&handle->lock);
2321
2322         ret = __set_command(handle, command, value);
2323
2324         g_mutex_unlock(&handle->lock);
2325
2326         return ret;
2327 }
2328
2329
2330 int camera_v4l2_get_command(void *camera_handle, int64_t command, void **value)
2331 {
2332         int ret = CAMERA_ERROR_NONE;
2333         int cid = 0;
2334         int ctrl_ret = 0;
2335         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2336
2337         if (!handle || !value) {
2338                 LOGE("NULL param %p %p", handle, value);
2339                 return CAMERA_ERROR_INVALID_PARAMETER;
2340         }
2341
2342         g_mutex_lock(&handle->lock);
2343
2344         LOGD("get command %"PRIx64" - state %d", command, handle->state);
2345
2346         switch (command) {
2347         case CAMERA_COMMAND_BRIGHTNESS:
2348                 cid = V4L2_CID_BRIGHTNESS;
2349                 break;
2350         case CAMERA_COMMAND_CONTRAST:
2351                 cid = V4L2_CID_CONTRAST;
2352                 break;
2353         case CAMERA_COMMAND_SATURATION:
2354                 cid = V4L2_CID_SATURATION;
2355                 break;
2356         case CAMERA_COMMAND_SHARPNESS:
2357                 cid = V4L2_CID_SHARPNESS;
2358                 break;
2359         case CAMERA_COMMAND_FOCUS_MODE:
2360                 **(int **)value = handle->focus_mode;
2361                 LOGI("get focus mode %d", **(int **)value);
2362                 goto _GET_COMMAND_DONE;
2363         case CAMERA_COMMAND_FOCUS_RANGE:
2364                 **(int **)value = handle->focus_range;
2365                 LOGI("get focus range %d", **(int **)value);
2366                 goto _GET_COMMAND_DONE;
2367         case CAMERA_COMMAND_FOCUS_LEVEL:
2368                 **(int **)value = handle->focus_level;
2369                 LOGI("get focus level %d", **(int **)value);
2370                 goto _GET_COMMAND_DONE;
2371         default:
2372                 LOGE("NOT_SUPPORTED %"PRIx64, command);
2373                 g_mutex_unlock(&handle->lock);
2374                 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2375         }
2376
2377         ctrl_ret = __camera_v4l2_g_ctrl(handle->device_fd, cid, (int *)*value);
2378         if (ctrl_ret < 0) {
2379                 switch (errno) {
2380                 case EACCES:
2381                 case EPERM:
2382                         LOGE("Permission denied %d", errno);
2383                         ret = CAMERA_ERROR_PERMISSION_DENIED;
2384                         break;
2385                 case EINVAL:
2386                         LOGE("Invalid argument");
2387                         ret = CAMERA_ERROR_INVALID_PARAMETER;
2388                         break;
2389                 case EBUSY:
2390                         LOGE("Device busy");
2391                         ret = CAMERA_ERROR_DEVICE_BUSY;
2392                         break;
2393                 case ENOTSUP:
2394                         LOGE("Not supported");
2395                         ret = CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2396                         break;
2397                 default:
2398                         LOGE("Unknown errro %d", errno);
2399                         ret = CAMERA_ERROR_INTERNAL;
2400                         break;
2401                 }
2402         }
2403
2404 _GET_COMMAND_DONE:
2405         g_mutex_unlock(&handle->lock);
2406
2407         return ret;
2408 }
2409
2410
2411 static void __dump_batch_command(camera_batch_command_control_s *batch_command)
2412 {
2413         if (!batch_command) {
2414                 LOGE("NULL batch command");
2415                 return;
2416         }
2417
2418         LOGI("[WHITE_BALANCE] %d", batch_command->white_balance);
2419         LOGI("[ISO] %d", batch_command->iso);
2420         LOGI("[CONTRAST] %d", batch_command->contrast);
2421         LOGI("[HUE] %d", batch_command->hue);
2422         LOGI("[SATURATION] %d", batch_command->saturation);
2423         LOGI("[SHARPNESS] %d", batch_command->sharpness);
2424         LOGI("[BRIGHTNESS] %d", batch_command->brightness);
2425         LOGI("[EFFECT] %d", batch_command->effect);
2426         LOGI("[SCENE_MODE] %d", batch_command->scene_mode);
2427         LOGI("[EXPOSURE_MODE] %d", batch_command->exposure_mode);
2428         LOGI("[EXPOSURE] %d", batch_command->exposure);
2429         LOGI("[ROTATION] %d", batch_command->rotation);
2430         LOGI("[FLIP] %d", batch_command->flip);
2431         LOGI("[FOCUS_MODE] %d", batch_command->focus_mode);
2432         LOGI("[FOCUS_RANGE] %d", batch_command->focus_range);
2433         LOGI("[FOCUS_AREA] %d,%d,%dx%d", batch_command->focus_area.x, batch_command->focus_area.y,
2434                 batch_command->focus_area.width, batch_command->focus_area.height);
2435         LOGI("[FOCUS_LEVEL] %d", batch_command->focus_level);
2436         LOGI("[SHOT_MODE] %d", batch_command->shot_mode);
2437         LOGI("[ANTI_SHAKE] %d", batch_command->anti_shake);
2438         LOGI("[DIGITAL_ZOOM] %d", batch_command->digital_zoom);
2439         LOGI("[OPTICAL_ZOOM] %d", batch_command->optical_zoom);
2440         LOGI("[RECORDING_HINT] %d", batch_command->recording_hint);
2441         LOGI("[WDR] %d", batch_command->wdr);
2442         LOGI("[SHUTTER_SPEED] %d/%d", batch_command->shutter_speed.numerator, batch_command->shutter_speed.denominator);
2443         LOGI("[FLASH_MODE] %d", batch_command->flash_mode);
2444         LOGI("[FLASH_BRIGHTNESS] %d", batch_command->flash_brightness);
2445         LOGI("[FACE_DETECTION] %d", batch_command->face_detection);
2446         LOGI("[PTZ_TYPE] %d", batch_command->ptz_type);
2447         LOGI("[PAN] %d", batch_command->pan);
2448         LOGI("[TILT] %d", batch_command->tilt);
2449         LOGI("[BITRATE] %d", batch_command->bitrate);
2450         LOGI("[GOP_INTERVAL] %d", batch_command->gop_interval);
2451         LOGI("[CAPTURE_COUNT] %d", batch_command->capture_count);
2452         LOGI("[CAPTURE_INTERVAL] %d", batch_command->capture_interval);
2453 }
2454
2455
2456 int camera_v4l2_set_batch_command(void *camera_handle, camera_batch_command_control_s *batch_command, int64_t *error_command)
2457 {
2458         int ret = CAMERA_ERROR_NONE;
2459         int i = 0;
2460         int support_count = 0;
2461         hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2462         set_batch_table_s set_table[] = {
2463                 {CAMERA_COMMAND_BRIGHTNESS, batch_command ? (void *)&batch_command->brightness : NULL},
2464                 {CAMERA_COMMAND_CONTRAST, batch_command ? (void *)&batch_command->contrast : NULL},
2465                 {CAMERA_COMMAND_SATURATION, batch_command ? (void *)&batch_command->saturation : NULL},
2466                 {CAMERA_COMMAND_SHARPNESS, batch_command ? (void *)&batch_command->sharpness : NULL},
2467                 {CAMERA_COMMAND_PTZ_TYPE, batch_command ? (void *)&batch_command->ptz_type : NULL},
2468                 {CAMERA_COMMAND_PAN, batch_command ? (void *)&batch_command->pan : NULL},
2469                 {CAMERA_COMMAND_TILT, batch_command ? (void *)&batch_command->tilt : NULL},
2470                 {CAMERA_COMMAND_FLIP, batch_command ? (void *)&batch_command->flip : NULL},
2471                 {CAMERA_COMMAND_CAPTURE_COUNT, batch_command ? (void *)&batch_command->capture_count : NULL},
2472                 {CAMERA_COMMAND_CAPTURE_INTERVAL, batch_command ? (void *)&batch_command->capture_interval : NULL},
2473                 {CAMERA_COMMAND_FOCUS_MODE, batch_command ? (void *)&batch_command->focus_mode : NULL},
2474                 {CAMERA_COMMAND_FOCUS_RANGE, batch_command ? (void *)&batch_command->focus_range : NULL},
2475                 {CAMERA_COMMAND_FOCUS_LEVEL, batch_command ? (void *)&batch_command->focus_level : NULL}
2476         };
2477
2478         if (!handle || !batch_command) {
2479                 LOGE("NULL param %p %p", handle, batch_command);
2480                 return CAMERA_ERROR_INVALID_PARAMETER;
2481         }
2482
2483         g_mutex_lock(&handle->lock);
2484
2485         support_count = sizeof(set_table) / sizeof(set_batch_table_s);
2486
2487         LOGD("set batch command - support count %d", support_count);
2488
2489         __dump_batch_command(batch_command);
2490
2491         for (i = 0 ; i < support_count ; i++) {
2492                 if (!(batch_command->command_set_flag & set_table[i].command))
2493                         continue;
2494
2495                 ret = __set_command(handle, set_table[i].command, set_table[i].value);
2496                 if (ret != CAMERA_ERROR_NONE) {
2497                         LOGE("failed command %"PRIx64", ret 0x%x", set_table[i].command, ret);
2498                         break;
2499                 }
2500         }
2501
2502         g_mutex_unlock(&handle->lock);
2503
2504         return ret;
2505 }
2506
2507
2508 static int camera_v4l2_backend_init(void **data)
2509 {
2510         hal_backend_camera_funcs *funcs;
2511
2512         funcs = calloc(1, sizeof(hal_backend_camera_funcs));
2513         if (!funcs)
2514                 return CAMERA_ERROR_OUT_OF_MEMORY;
2515
2516         funcs->init = camera_v4l2_init;
2517         funcs->deinit = camera_v4l2_deinit;
2518         funcs->get_device_info_list = camera_v4l2_get_device_info_list;
2519         funcs->open_device = camera_v4l2_open_device;
2520         funcs->open_device_ext = camera_v4l2_open_device_ext;
2521         funcs->close_device = camera_v4l2_close_device;
2522         funcs->add_message_callback = camera_v4l2_add_message_callback;
2523         funcs->remove_message_callback = camera_v4l2_remove_message_callback;
2524         funcs->set_preview_stream_format = camera_v4l2_set_preview_stream_format;
2525         funcs->get_preview_stream_format = camera_v4l2_get_preview_stream_format;
2526         funcs->set_user_buffer_fd = camera_v4l2_set_user_buffer_fd;
2527         funcs->start_preview = camera_v4l2_start_preview;
2528         funcs->release_preview_buffer = camera_v4l2_release_preview_buffer;
2529         funcs->stop_preview = camera_v4l2_stop_preview;
2530         funcs->start_auto_focus = camera_v4l2_start_auto_focus;
2531         funcs->stop_auto_focus = camera_v4l2_stop_auto_focus;
2532         funcs->start_capture = camera_v4l2_start_capture;
2533         funcs->stop_capture = camera_v4l2_stop_capture;
2534         funcs->set_video_stream_format = camera_v4l2_set_video_stream_format;
2535         funcs->get_video_stream_format = camera_v4l2_get_video_stream_format;
2536         funcs->start_record = camera_v4l2_start_record;
2537         funcs->release_video_buffer = camera_v4l2_release_video_buffer;
2538         funcs->stop_record = camera_v4l2_stop_record;
2539         funcs->set_command = camera_v4l2_set_command;
2540         funcs->get_command = camera_v4l2_get_command;
2541         funcs->set_batch_command = camera_v4l2_set_batch_command;
2542         funcs->set_extra_preview_frame_cb = camera_v4l2_set_extra_preview_frame_cb;
2543         funcs->unset_extra_preview_frame_cb = camera_v4l2_unset_extra_preview_frame_cb;
2544         funcs->release_extra_preview_buffer = camera_v4l2_release_extra_preview_buffer;
2545         funcs->set_extra_preview_stream_format = camera_v4l2_set_extra_preview_stream_format;
2546         funcs->get_extra_preview_stream_format = camera_v4l2_get_extra_preview_stream_format;
2547         funcs->set_extra_preview_bitrate = camera_v4l2_set_extra_preview_bitrate;
2548         funcs->get_extra_preview_bitrate = camera_v4l2_get_extra_preview_bitrate;
2549         funcs->set_extra_preview_gop_interval = camera_v4l2_set_extra_preview_gop_interval;
2550         funcs->get_extra_preview_gop_interval = camera_v4l2_get_extra_preview_gop_interval;
2551
2552         *data = (void *)funcs;
2553
2554         return 0;
2555 }
2556
2557
2558 static int camera_v4l2_backend_exit(void *data)
2559 {
2560         if (!data)
2561                 return 0;
2562
2563         free(data);
2564
2565         return 0;
2566 }
2567
2568
2569 hal_backend hal_backend_camera_data = {
2570         .name = "camera-v4l2",
2571         .vendor = "TIZEN",
2572         .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
2573         .init = camera_v4l2_backend_init,
2574         .exit = camera_v4l2_backend_exit,
2575 };