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