Galaxy S2 camera module
[exynos_camera.git] / exynos_camera.c
1 /*
2  * Copyright (C) 2013 Paul Kocialkowski
3  *
4  * Based on crespo libcamera and exynos4 hal libcamera:
5  * Copyright 2008, The Android Open Source Project
6  * Copyright 2010, Samsung Electronics Co. LTD
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <errno.h>
27 #include <malloc.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/mman.h>
32 #include <sys/ioctl.h>
33
34 #include <asm/types.h>
35 #include <jpeg_api.h>
36
37 #define LOG_TAG "exynos_camera"
38 #include <utils/Log.h>
39 #include <utils/Timers.h>
40
41 #include "exynos_camera.h"
42
43 /*
44  * Devices configurations
45  */
46
47 struct exynos_camera_preset exynos_camera_presets_galaxys2[] = {
48         {
49                 .name = "M5MO",
50                 .facing = CAMERA_FACING_BACK,
51                 .orientation = 90,
52                 .rotation = 0,
53                 .hflip = 0,
54                 .vflip = 0,
55                 .picture_format = V4L2_PIX_FMT_JPEG,
56                 .focal_length = 4.03f,
57                 .horizontal_view_angle = 60.5f,
58                 .vertical_view_angle = 47.1f,
59                 .metering = METERING_CENTER,
60                 .params = {
61                         .preview_size_values = "1280x720,640x480,720x480,800x480,800x450,352x288,320x240,176x144",
62                         .preview_size = "640x480",
63                         .preview_format_values = "yuv420sp,yuv420p,rgb565",
64                         .preview_format = "yuv420sp",
65                         .preview_frame_rate_values = "30,25,20,15,10,7",
66                         .preview_frame_rate = 30,
67                         .preview_fps_range_values = "(7000,30000)",
68                         .preview_fps_range = "7000,30000",
69
70                         .picture_size_values = "3264x2448,3264x1968,2048x1536,2048x1232,800x480,640x480",
71                         .picture_size = "3264x2448",
72                         .picture_format_values = "jpeg",
73                         .picture_format = "jpeg",
74                         .jpeg_thumbnail_size_values = "320x240,400x240,0x0",
75                         .jpeg_thumbnail_width = 320,
76                         .jpeg_thumbnail_height = 240,
77                         .jpeg_thumbnail_quality = 100,
78                         .jpeg_quality = 90,
79
80                         .recording_size = "720x480",
81                         .recording_size_values = "1920x1080,1280x720,720x480,640x480",
82                         .recording_format = "yuv420sp",
83
84                         .focus_mode = "auto",
85                         .focus_mode_values = "auto,infinity,macro,fixed,facedetect,continuous-video",
86                         .focus_distances = "0.15,1.20,Infinity",
87                         .focus_areas = "(0,0,0,0,0)",
88                         .max_num_focus_areas = 1,
89
90                         .zoom_supported = 1,
91                         .smooth_zoom_supported = 0,
92                         .zoom_ratios = "100,102,104,109,111,113,119,121,124,131,134,138,146,150,155,159,165,170,182,189,200,213,222,232,243,255,283,300,319,364,400",
93                         .zoom = 0,
94                         .max_zoom = 30,
95
96                         .flash_mode = "off",
97                         .flash_mode_values = "off,auto,on,torch",
98
99                         .exposure_compensation = 0,
100                         .exposure_compensation_step = 0.5,
101                         .min_exposure_compensation = -4,
102                         .max_exposure_compensation = 4,
103
104                         .whitebalance = "auto",
105                         .whitebalance_values = "auto,incandescent,fluorescent,daylight,cloudy-daylight",
106
107                         .scene_mode = "auto",
108                         .scene_mode_values = "auto,portrait,landscape,night,beach,snow,sunset,fireworks,sports,party,candlelight,dusk-dawn,fall-color,back-light,text",
109
110                         .effect = "none",
111                         .effect_values = "none,mono,negative,sepia,aqua",
112
113                         .iso = "auto",
114                         .iso_values = "auto,ISO50,ISO100,ISO200,ISO400,ISO800",
115                 },
116         },
117         {
118                 .name = "S5K5BAFX",
119                 .facing = CAMERA_FACING_FRONT,
120                 .orientation = 270,
121                 .rotation = 0,
122                 .hflip = 0,
123                 .vflip = 0,
124                 .picture_format = V4L2_PIX_FMT_YUYV,
125                 .focal_length = 2.73f,
126                 .horizontal_view_angle = 51.2f,
127                 .vertical_view_angle = 39.4f,
128                 .metering = METERING_CENTER,
129                 .params = {
130                         .preview_size_values = "640x480,352x288,320x240,176x144",
131                         .preview_size = "640x480",
132                         .preview_format_values = "yuv420sp,yuv420p,rgb565",
133                         .preview_format = "yuv420sp",
134                         .preview_frame_rate_values = "30,25,20,15,10,7",
135                         .preview_frame_rate = 30,
136                         .preview_fps_range_values = "(7000,30000)",
137                         .preview_fps_range = "7000,30000",
138
139                         .picture_size_values = "1600x1200,640x480",
140                         .picture_size = "1600x1200",
141                         .picture_format_values = "jpeg",
142                         .picture_format = "jpeg",
143                         .jpeg_thumbnail_size_values = "160x120,0x0",
144                         .jpeg_thumbnail_width = 160,
145                         .jpeg_thumbnail_height = 120,
146                         .jpeg_thumbnail_quality = 100,
147                         .jpeg_quality = 90,
148
149                         .recording_size = "640x480",
150                         .recording_size_values = "640x480",
151                         .recording_format = "yuv420sp",
152
153                         .focus_mode = "fixed",
154                         .focus_mode_values = "fixed",
155                         .focus_distances = "0.20,0.25,Infinity",
156                         .focus_areas = NULL,
157                         .max_num_focus_areas = 0,
158
159                         .zoom_supported = 0,
160
161                         .flash_mode = NULL,
162                         .flash_mode_values = NULL,
163
164                         .exposure_compensation = 0,
165                         .exposure_compensation_step = 0.5,
166                         .min_exposure_compensation = -4,
167                         .max_exposure_compensation = 4,
168
169                         .whitebalance = NULL,
170                         .whitebalance_values = NULL,
171
172                         .scene_mode = NULL,
173                         .scene_mode_values = NULL,
174
175                         .effect = NULL,
176                         .effect_values = NULL,
177
178                         .iso = "auto",
179                         .iso_values = "auto",
180                 },
181         },
182 };
183
184 struct exynos_v4l2_node exynos_v4l2_nodes_galaxys2[] = {
185         {
186                 .id = 0,
187                 .node = "/dev/video0",
188         },
189         {
190                 .id = 1,
191                 .node = "/dev/video1",
192         },
193         {
194                 .id = 2,
195                 .node = "/dev/video2",
196         },
197 };
198
199 struct exynox_camera_config exynos_camera_config_galaxys2 = {
200         .presets = (struct exynos_camera_preset *) &exynos_camera_presets_galaxys2,
201         .presets_count = 2,
202         .v4l2_nodes = (struct exynos_v4l2_node *) &exynos_v4l2_nodes_galaxys2,
203         .v4l2_nodes_count = 3,
204 };
205
206 /*
207  * Exynos Camera
208  */
209
210 struct exynox_camera_config *exynos_camera_config =
211         &exynos_camera_config_galaxys2;
212
213 int exynos_camera_init(struct exynos_camera *exynos_camera, int id)
214 {
215         char firmware_version[7] = { 0 };
216         struct exynos_v4l2_ext_control control;
217         int rc;
218
219         if (exynos_camera == NULL || id >= exynos_camera->config->presets_count)
220                 return -EINVAL;
221
222         // Init FIMC1
223         rc = exynos_v4l2_open(exynos_camera, 0);
224         if (rc < 0) {
225                 LOGE("Unable to open v4l2 device");
226                 return -1;
227         }
228
229         rc = exynos_v4l2_querycap_cap(exynos_camera, 0);
230         if (rc < 0) {
231                 LOGE("%s: querycap failed", __func__);
232                 return -1;
233         }
234
235         rc = exynos_v4l2_enum_input(exynos_camera, 0, id);
236         if (rc < 0) {
237                 LOGE("%s: enum input failed", __func__);
238                 return -1;
239         }
240
241         rc = exynos_v4l2_s_input(exynos_camera, 0, id);
242         if (rc < 0) {
243                 LOGE("%s: s input failed", __func__);
244                 return -1;
245         }
246
247         // Init FIMC2
248         rc = exynos_v4l2_open(exynos_camera, 2);
249         if (rc < 0) {
250                 LOGE("Unable to open v4l2 device");
251                 return -1;
252         }
253
254         rc = exynos_v4l2_querycap_cap(exynos_camera, 2);
255         if (rc < 0) {
256                 LOGE("%s: querycap failed", __func__);
257                 return -1;
258         }
259
260         rc = exynos_v4l2_enum_input(exynos_camera, 2, id);
261         if (rc < 0) {
262                 LOGE("%s: enum input failed", __func__);
263                 return -1;
264         }
265
266         rc = exynos_v4l2_s_input(exynos_camera, 2, id);
267         if (rc < 0) {
268                 LOGE("%s: s input failed", __func__);
269                 return -1;
270         }
271
272         // Get firmware information
273         memset(&control, 0, sizeof(control));
274         control.id = V4L2_CID_CAM_SENSOR_FW_VER;
275         control.data.string = firmware_version;
276
277         rc = exynos_v4l2_g_ext_ctrls(exynos_camera, 0, (struct v4l2_ext_control *) &control, 1);
278         if (rc < 0) {
279                 LOGE("%s: g ext ctrls failed", __func__);
280         } else {
281                 LOGD("Firmware version: %s", firmware_version);
282         }
283
284         // Params
285         rc = exynos_camera_params_init(exynos_camera, id);
286         if (rc < 0)
287                 LOGE("%s: Unable to init params", __func__);
288
289         // Gralloc
290         rc = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const struct hw_module_t **) &exynos_camera->gralloc);
291         if (rc)
292                 LOGE("%s: Unable to get gralloc module", __func__);
293
294         return 0;
295 }
296
297 void exynos_camera_deinit(struct exynos_camera *exynos_camera)
298 {
299         int i;
300         int id;
301
302         if (exynos_camera == NULL || exynos_camera->config == NULL)
303                 return;
304
305         exynos_v4l2_close(exynos_camera, 0);
306         exynos_v4l2_close(exynos_camera, 2);
307 }
308
309 // Params
310
311 int exynos_camera_params_init(struct exynos_camera *exynos_camera, int id)
312 {
313         int rc;
314
315         if (exynos_camera == NULL || id >= exynos_camera->config->presets_count)
316                 return -EINVAL;
317
318         // Camera params
319         exynos_camera->camera_rotation = exynos_camera->config->presets[id].rotation;
320         exynos_camera->camera_hflip = exynos_camera->config->presets[id].hflip;
321         exynos_camera->camera_vflip = exynos_camera->config->presets[id].vflip;
322         exynos_camera->camera_picture_format = exynos_camera->config->presets[id].picture_format;
323         exynos_camera->camera_focal_length = (int) (exynos_camera->config->presets[id].focal_length * 100);
324         exynos_camera->camera_metering = exynos_camera->config->presets[id].metering;
325
326         // Recording preview
327         exynos_param_string_set(exynos_camera, "preferred-preview-size-for-video",
328                 exynos_camera->config->presets[id].params.preview_size);
329
330         // Preview
331         exynos_param_string_set(exynos_camera, "preview-size-values",
332                 exynos_camera->config->presets[id].params.preview_size_values);
333         exynos_param_string_set(exynos_camera, "preview-size",
334                 exynos_camera->config->presets[id].params.preview_size);
335         exynos_param_string_set(exynos_camera, "preview-format-values",
336                 exynos_camera->config->presets[id].params.preview_format_values);
337         exynos_param_string_set(exynos_camera, "preview-format",
338                 exynos_camera->config->presets[id].params.preview_format);
339         exynos_param_string_set(exynos_camera, "preview-frame-rate-values",
340                 exynos_camera->config->presets[id].params.preview_frame_rate_values);
341         exynos_param_int_set(exynos_camera, "preview-frame-rate",
342                 exynos_camera->config->presets[id].params.preview_frame_rate);
343         exynos_param_string_set(exynos_camera, "preview-fps-range-values",
344                 exynos_camera->config->presets[id].params.preview_fps_range_values);
345         exynos_param_string_set(exynos_camera, "preview-fps-range",
346                 exynos_camera->config->presets[id].params.preview_fps_range);
347
348         // Picture
349         exynos_param_string_set(exynos_camera, "picture-size-values",
350                 exynos_camera->config->presets[id].params.picture_size_values);
351         exynos_param_string_set(exynos_camera, "picture-size",
352                 exynos_camera->config->presets[id].params.picture_size);
353         exynos_param_string_set(exynos_camera, "picture-format-values",
354                 exynos_camera->config->presets[id].params.picture_format_values);
355         exynos_param_string_set(exynos_camera, "picture-format",
356                 exynos_camera->config->presets[id].params.picture_format);
357         exynos_param_string_set(exynos_camera, "jpeg-thumbnail-size-values",
358                 exynos_camera->config->presets[id].params.jpeg_thumbnail_size_values);
359         exynos_param_int_set(exynos_camera, "jpeg-thumbnail-width",
360                 exynos_camera->config->presets[id].params.jpeg_thumbnail_width);
361         exynos_param_int_set(exynos_camera, "jpeg-thumbnail-height",
362                 exynos_camera->config->presets[id].params.jpeg_thumbnail_height);
363         exynos_param_int_set(exynos_camera, "jpeg-thumbnail-quality",
364                 exynos_camera->config->presets[id].params.jpeg_thumbnail_quality);
365         exynos_param_int_set(exynos_camera, "jpeg-quality",
366                 exynos_camera->config->presets[id].params.jpeg_quality);
367
368         // Recording
369         exynos_param_string_set(exynos_camera, "video-size",
370                 exynos_camera->config->presets[id].params.recording_size);
371         exynos_param_string_set(exynos_camera, "video-size-values",
372                 exynos_camera->config->presets[id].params.recording_size_values);
373         exynos_param_string_set(exynos_camera, "video-frame-format",
374                 exynos_camera->config->presets[id].params.recording_format);
375
376         // Focus
377         exynos_param_string_set(exynos_camera, "focus-mode",
378                 exynos_camera->config->presets[id].params.focus_mode);
379         exynos_param_string_set(exynos_camera, "focus-mode-values",
380                 exynos_camera->config->presets[id].params.focus_mode_values);
381         exynos_param_string_set(exynos_camera, "focus-distances",
382                 exynos_camera->config->presets[id].params.focus_distances);
383         if (exynos_camera->config->presets[id].params.max_num_focus_areas > 0) {
384                 exynos_param_string_set(exynos_camera, "focus-areas",
385                         exynos_camera->config->presets[id].params.focus_areas);
386                 exynos_param_int_set(exynos_camera, "max-num-focus-areas",
387                         exynos_camera->config->presets[id].params.max_num_focus_areas);
388         }
389
390         // Zoom
391         if (exynos_camera->config->presets[id].params.zoom_supported == 1) {
392                 exynos_param_string_set(exynos_camera, "zoom-supported", "true");
393
394                 if (exynos_camera->config->presets[id].params.smooth_zoom_supported == 1)
395                         exynos_param_string_set(exynos_camera, "smooth-zoom-supported", "true");
396
397                 if (exynos_camera->config->presets[id].params.zoom_ratios != NULL)
398                         exynos_param_string_set(exynos_camera, "zoom-ratios", exynos_camera->config->presets[id].params.zoom_ratios);
399
400                 exynos_param_int_set(exynos_camera, "zoom", exynos_camera->config->presets[id].params.zoom);
401                 exynos_param_int_set(exynos_camera, "max-zoom", exynos_camera->config->presets[id].params.max_zoom);
402
403         } else {
404                 exynos_param_string_set(exynos_camera, "zoom-supported", "false");
405         }
406
407         // Flash
408         exynos_param_string_set(exynos_camera, "flash-mode",
409                 exynos_camera->config->presets[id].params.flash_mode);
410         exynos_param_string_set(exynos_camera, "flash-mode-values",
411                 exynos_camera->config->presets[id].params.flash_mode_values);
412
413         // Exposure
414         exynos_param_int_set(exynos_camera, "exposure-compensation",
415                 exynos_camera->config->presets[id].params.exposure_compensation);
416         exynos_param_float_set(exynos_camera, "exposure-compensation-step",
417                 exynos_camera->config->presets[id].params.exposure_compensation_step);
418         exynos_param_int_set(exynos_camera, "min-exposure-compensation",
419                 exynos_camera->config->presets[id].params.min_exposure_compensation);
420         exynos_param_int_set(exynos_camera, "max-exposure-compensation",
421                 exynos_camera->config->presets[id].params.max_exposure_compensation);
422
423         // WB
424         exynos_param_string_set(exynos_camera, "whitebalance",
425                 exynos_camera->config->presets[id].params.whitebalance);
426         exynos_param_string_set(exynos_camera, "whitebalance-values",
427                 exynos_camera->config->presets[id].params.whitebalance_values);
428
429         // Scene mode
430         exynos_param_string_set(exynos_camera, "scene-mode",
431                 exynos_camera->config->presets[id].params.scene_mode);
432         exynos_param_string_set(exynos_camera, "scene-mode-values",
433                 exynos_camera->config->presets[id].params.scene_mode_values);
434
435         // Effect
436         exynos_param_string_set(exynos_camera, "effect",
437                 exynos_camera->config->presets[id].params.effect);
438         exynos_param_string_set(exynos_camera, "effect-values",
439                 exynos_camera->config->presets[id].params.effect_values);
440
441         // ISO
442         exynos_param_string_set(exynos_camera, "iso",
443                 exynos_camera->config->presets[id].params.iso);
444         exynos_param_string_set(exynos_camera, "iso-values",
445                 exynos_camera->config->presets[id].params.iso_values);
446
447         // Camera
448         exynos_param_float_set(exynos_camera, "focal-length",
449                 exynos_camera->config->presets[id].focal_length);
450         exynos_param_float_set(exynos_camera, "horizontal-view-angle",
451                 exynos_camera->config->presets[id].horizontal_view_angle);
452         exynos_param_float_set(exynos_camera, "vertical-view-angle",
453                 exynos_camera->config->presets[id].vertical_view_angle);
454
455         rc = exynos_camera_params_apply(exynos_camera);
456         if (rc < 0) {
457                 LOGE("%s: Unable to apply params", __func__);
458                 return -1;
459         }
460
461         return 0;
462 }
463
464 int exynos_camera_params_apply(struct exynos_camera *exynos_camera)
465 {
466         char *recording_hint_string;
467         char *recording_preview_size_string;
468
469         char *preview_size_string;
470         int preview_width = 0;
471         int preview_height = 0;
472         char *preview_format_string;
473         int preview_format;
474         float preview_format_bpp;
475         int preview_fps;
476
477         char *picture_size_string;
478         int picture_width = 0;
479         int picture_height = 0;
480         char *picture_format_string;
481         int picture_format;
482
483         int jpeg_thumbnail_width;
484         int jpeg_thumbnail_height;
485         int jpeg_thumbnail_quality;
486         int jpeg_quality;
487
488         char *video_size_string;
489         int recording_width = 0;
490         int recording_height = 0;
491         char *video_frame_format_string;
492         int recording_format;
493         int camera_sensor_mode;
494         int camera_sensor_output_size;
495
496         char *focus_mode_string;
497         int focus_mode;
498         char *focus_areas_string;
499         int focus_left, focus_top, focus_right, focus_bottom, focus_weigth;
500         int focus_x;
501         int focus_y;
502
503         char *zoom_supported_string;
504         int zoom, max_zoom;
505
506         char *flash_mode_string;
507         int flash_mode;
508
509         int exposure_compensation;
510         int min_exposure_compensation;
511         int max_exposure_compensation;
512
513         char *whitebalance_string;
514         int whitebalance;
515
516         char *scene_mode_string;
517         int scene_mode;
518
519         char *effect_string;
520         int effect;
521
522         char *iso_string;
523         int iso;
524
525         int force ;
526
527         int w, h;
528         char *k;
529         int rc;
530
531         if (exynos_camera == NULL)
532                 return -EINVAL;
533
534         if (!exynos_camera->preview_params_set) {
535                 LOGE("%s: Setting preview params", __func__);
536                 exynos_camera->preview_params_set = 1;
537                 force = 1;
538         } else {
539                 force = 0;
540         }
541
542         // Preview
543         preview_size_string = exynos_param_string_get(exynos_camera, "preview-size");
544         if (preview_size_string != NULL) {
545                 sscanf(preview_size_string, "%dx%d", &preview_width, &preview_height);
546
547                 if (preview_width != 0 && preview_width != exynos_camera->preview_width)
548                         exynos_camera->preview_width = preview_width;
549                 if (preview_height != 0 && preview_height != exynos_camera->preview_height)
550                         exynos_camera->preview_height = preview_height;
551         }
552
553         preview_format_string = exynos_param_string_get(exynos_camera, "preview-format");
554         if (preview_format_string != NULL) {
555                 if (strcmp(preview_format_string, "yuv420sp") == 0) {
556                         preview_format = V4L2_PIX_FMT_NV21;
557                         preview_format_bpp = 1.5f;
558                 } else if (strcmp(preview_format_string, "yuv420p") == 0) {
559                         preview_format = V4L2_PIX_FMT_YUV420;
560                         preview_format_bpp = 1.5f;
561                 } else if (strcmp(preview_format_string, "rgb565") == 0) {
562                         preview_format = V4L2_PIX_FMT_RGB565;
563                         preview_format_bpp = 2.0f;
564                 } else if (strcmp(preview_format_string, "rgb8888") == 0) {
565                         preview_format = V4L2_PIX_FMT_RGB32;
566                         preview_format_bpp = 4.0f;
567                 } else {
568                         LOGE("%s: Unsupported preview format: %s", __func__, preview_format_string);
569                         preview_format = V4L2_PIX_FMT_NV21;
570                         preview_format_bpp = 1.5f;
571                 }
572
573                 if (preview_format != exynos_camera->preview_format) {
574                         exynos_camera->preview_format = preview_format;
575                         exynos_camera->preview_format_bpp = preview_format_bpp;
576                 }
577         }
578
579         preview_fps = exynos_param_int_get(exynos_camera, "preview-frame-rate");
580         if (preview_fps > 0)
581                 exynos_camera->preview_fps = preview_fps;
582         else
583                 exynos_camera->preview_fps = 0;
584
585         // Picture
586         picture_size_string = exynos_param_string_get(exynos_camera, "picture-size");
587         if (picture_size_string != NULL) {
588                 sscanf(picture_size_string, "%dx%d", &picture_width, &picture_height);
589
590                 if (picture_width != 0 && picture_height != 0 &&
591                         picture_width != exynos_camera->picture_width &&
592                         picture_height != exynos_camera->picture_height) {
593                         exynos_camera->picture_width = picture_width;
594                         exynos_camera->picture_height = picture_height;
595                 }
596                 
597         }
598
599         picture_format_string = exynos_param_string_get(exynos_camera, "picture-format");
600         if (picture_format_string != NULL) {
601                 if (strcmp(picture_format_string, "jpeg") == 0) {
602                         picture_format = V4L2_PIX_FMT_JPEG;
603                 } else {
604                         LOGE("%s: Unsupported picture format: %s", __func__, picture_format_string);
605                         picture_format = V4L2_PIX_FMT_JPEG;
606                 }
607
608                 if (picture_format != exynos_camera->picture_format)
609                         exynos_camera->picture_format = picture_format;
610         }
611
612         jpeg_thumbnail_width = exynos_param_int_get(exynos_camera, "jpeg-thumbnail-width");
613         if (jpeg_thumbnail_width > 0)
614                 exynos_camera->jpeg_thumbnail_width = jpeg_thumbnail_width;
615
616         jpeg_thumbnail_height = exynos_param_int_get(exynos_camera, "jpeg-thumbnail-height");
617         if (jpeg_thumbnail_height > 0)
618                 exynos_camera->jpeg_thumbnail_height = jpeg_thumbnail_height;
619
620         jpeg_thumbnail_quality = exynos_param_int_get(exynos_camera, "jpeg-thumbnail-quality");
621         if (jpeg_thumbnail_quality > 0)
622                 exynos_camera->jpeg_thumbnail_quality = jpeg_thumbnail_quality;
623
624         jpeg_quality = exynos_param_int_get(exynos_camera, "jpeg-quality");
625         if (jpeg_quality <= 100 && jpeg_quality >= 0 && (jpeg_quality != exynos_camera->jpeg_quality || force)) {
626                 exynos_camera->jpeg_quality = jpeg_quality;
627                 rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAM_JPEG_QUALITY, jpeg_quality);
628                 if (rc < 0)
629                         LOGE("%s: s ctrl failed!", __func__);
630         }
631
632         // Recording
633         video_size_string = exynos_param_string_get(exynos_camera, "video-size");
634         if (video_size_string == NULL)
635                 video_size_string = exynos_param_string_get(exynos_camera, "preview-size");
636
637         if (video_size_string != NULL) {
638                 sscanf(video_size_string, "%dx%d", &recording_width, &recording_height);
639
640                 if (recording_width != 0 && recording_width != exynos_camera->recording_width)
641                         exynos_camera->recording_width = recording_width;
642                 if (recording_height != 0 && recording_height != exynos_camera->recording_height)
643                         exynos_camera->recording_height = recording_height;
644         }
645
646         video_frame_format_string = exynos_param_string_get(exynos_camera, "video-frame-format");
647         if (video_frame_format_string != NULL) {
648                 if (strcmp(video_frame_format_string, "yuv420sp") == 0) {
649                         recording_format = V4L2_PIX_FMT_NV12;
650                 } else if (strcmp(video_frame_format_string, "yuv420p") == 0) {
651                         recording_format = V4L2_PIX_FMT_YUV420;
652                 } else if (strcmp(video_frame_format_string, "rgb565") == 0) {
653                         recording_format = V4L2_PIX_FMT_RGB565;
654                 } else if (strcmp(video_frame_format_string, "rgb8888") == 0) {
655                         recording_format = V4L2_PIX_FMT_RGB32;
656                 } else {
657                         LOGE("%s: Unsupported recording format: %s", __func__, video_frame_format_string);
658                         recording_format = V4L2_PIX_FMT_NV12;
659                 }
660
661                 if (recording_format != exynos_camera->recording_format)
662                         exynos_camera->recording_format = recording_format;
663         }
664
665         recording_hint_string = exynos_param_string_get(exynos_camera, "recording-hint");
666         if (recording_hint_string != NULL && strcmp(recording_hint_string, "true") == 0) {
667                 camera_sensor_mode = SENSOR_MOVIE;
668
669                 k = exynos_param_string_get(exynos_camera, "preview-size-values");
670                 while (recording_width != 0 && recording_height != 0) {
671                         if (k == NULL)
672                                 break;
673
674                         sscanf(k, "%dx%d", &w, &h);
675
676                         // Look for same aspect ratio
677                         if ((recording_width * h) / recording_height == w) {
678                                 preview_width = w;
679                                 preview_height = h;
680                                 break;
681                         }
682
683                         k = strchr(k, ',');
684                         if (k == NULL)
685                                 break;
686
687                         k++;
688                 }
689
690                 if (preview_width != 0 && preview_width != exynos_camera->preview_width)
691                         exynos_camera->preview_width = preview_width;
692                 if (preview_height != 0 && preview_height != exynos_camera->preview_height)
693                         exynos_camera->preview_height = preview_height;
694
695                 camera_sensor_output_size = ((recording_width & 0xffff) << 16) | (recording_height & 0xffff);
696                 rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_SENSOR_OUTPUT_SIZE,
697                         camera_sensor_output_size);
698                 if (rc < 0)
699                         LOGE("%s: s ctrl failed!", __func__);
700         } else {
701                 camera_sensor_mode = SENSOR_CAMERA;
702         }
703
704         // Switching modes
705         if (camera_sensor_mode != exynos_camera->camera_sensor_mode) {
706                 exynos_camera->camera_sensor_mode = camera_sensor_mode;
707                 rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_SENSOR_MODE, camera_sensor_mode);
708                 if (rc < 0)
709                         LOGE("%s: s ctrl failed!", __func__);
710         }
711
712         // Focus
713         focus_mode_string = exynos_param_string_get(exynos_camera, "focus-mode");
714         if (focus_mode_string != NULL) {
715                 if (strcmp(focus_mode_string, "auto") == 0)
716                         focus_mode = FOCUS_MODE_AUTO;
717                 else if (strcmp(focus_mode_string, "infinity") == 0)
718                         focus_mode = FOCUS_MODE_INFINITY;
719                 else if (strcmp(focus_mode_string, "macro") == 0)
720                         focus_mode = FOCUS_MODE_MACRO;
721                 else if (strcmp(focus_mode_string, "fixed") == 0)
722                         focus_mode = FOCUS_MODE_FIXED;
723                 else if (strcmp(focus_mode_string, "facedetect") == 0)
724                         focus_mode = FOCUS_MODE_FACEDETECT;
725                 else if (strcmp(focus_mode_string, "continuous-video") == 0)
726                         focus_mode = FOCUS_MODE_CONTINOUS;
727                 else if (strcmp(focus_mode_string, "continuous-picture") == 0)
728                         focus_mode = FOCUS_MODE_CONTINOUS;
729                 else
730                         focus_mode = FOCUS_MODE_AUTO;
731
732                 if (focus_mode != exynos_camera->focus_mode || force) {
733                         exynos_camera->focus_mode = focus_mode;
734                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_FOCUS_MODE, focus_mode);
735                         if (rc < 0)
736                                 LOGE("%s: s ctrl failed!", __func__);
737                 }
738         }
739
740         focus_areas_string = exynos_param_string_get(exynos_camera, "focus-areas");
741         if (focus_areas_string != NULL) {
742                 focus_left = focus_top = focus_right = focus_bottom = focus_weigth = 0;
743
744                 rc = sscanf(focus_areas_string, "(%d,%d,%d,%d,%d)",
745                         &focus_left, &focus_top, &focus_right, &focus_bottom, &focus_weigth);
746                 if (rc != 5)
747                         LOGE("%s: sscanf failed!", __func__);
748
749                 focus_x = (((focus_left + focus_right) / 2) + 1000) * preview_width / 2000;
750                 focus_y =  (((focus_top + focus_bottom) / 2) + 1000) * preview_height / 2000;
751
752                 if (focus_x != exynos_camera->focus_x || force) {
753                         exynos_camera->focus_x = focus_x;
754
755                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_OBJECT_POSITION_X, focus_x);
756                         if (rc < 0)
757                                 LOGE("%s: s ctrl failed!", __func__);
758                 }
759
760                 if (focus_y != exynos_camera->focus_y || force) {
761                         exynos_camera->focus_y = focus_y;
762
763                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_OBJECT_POSITION_Y, focus_y);
764                         if (rc < 0)
765                                 LOGE("%s: s ctrl failed!", __func__);
766                 }
767         }
768
769         // Zoom
770         zoom_supported_string = exynos_param_string_get(exynos_camera, "zoom-supported");
771         if (zoom_supported_string != NULL && strcmp(zoom_supported_string, "true") == 0) {
772                 zoom = exynos_param_int_get(exynos_camera, "zoom");
773                 max_zoom = exynos_param_int_get(exynos_camera, "max-zoom");
774                 if (zoom <= max_zoom && zoom >= 0 && (zoom != exynos_camera->zoom || force)) {
775                         exynos_camera->zoom = zoom;
776                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_ZOOM, zoom);
777                         if (rc < 0)
778                                 LOGE("%s: s ctrl failed!", __func__);
779                 }
780
781         }
782
783         // Flash
784         flash_mode_string = exynos_param_string_get(exynos_camera, "flash-mode");
785         if (flash_mode_string != NULL) {
786                 if (strcmp(flash_mode_string, "off") == 0)
787                         flash_mode = FLASH_MODE_OFF;
788                 else if (strcmp(flash_mode_string, "auto") == 0)
789                         flash_mode = FLASH_MODE_AUTO;
790                 else if (strcmp(flash_mode_string, "on") == 0)
791                         flash_mode = FLASH_MODE_ON;
792                 else if (strcmp(flash_mode_string, "torch") == 0)
793                         flash_mode = FLASH_MODE_TORCH;
794                 else
795                         flash_mode = FLASH_MODE_AUTO;
796
797                 if (flash_mode != exynos_camera->flash_mode || force) {
798                         exynos_camera->flash_mode = flash_mode;
799                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_FLASH_MODE, flash_mode);
800                         if (rc < 0)
801                                 LOGE("%s: s ctrl failed!", __func__);
802                 }
803         }
804
805         // Exposure
806         exposure_compensation = exynos_param_int_get(exynos_camera, "exposure-compensation");
807         min_exposure_compensation = exynos_param_int_get(exynos_camera, "min-exposure-compensation");
808         max_exposure_compensation = exynos_param_int_get(exynos_camera, "max-exposure-compensation");
809
810         if (exposure_compensation <= max_exposure_compensation && exposure_compensation >= min_exposure_compensation &&
811                 (exposure_compensation != exynos_camera->exposure_compensation || force)) {
812                 exynos_camera->exposure_compensation = exposure_compensation;
813                 rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_BRIGHTNESS, exposure_compensation);
814                 if (rc < 0)
815                         LOGE("%s: s ctrl failed!", __func__);
816         }
817
818         // WB
819         whitebalance_string = exynos_param_string_get(exynos_camera, "whitebalance");
820         if (whitebalance_string != NULL) {
821                 if (strcmp(whitebalance_string, "auto") == 0)
822                         whitebalance = WHITE_BALANCE_AUTO;
823                 else if (strcmp(whitebalance_string, "incandescent") == 0)
824                         whitebalance = WHITE_BALANCE_TUNGSTEN;
825                 else if (strcmp(whitebalance_string, "fluorescent") == 0)
826                         whitebalance = WHITE_BALANCE_FLUORESCENT;
827                 else if (strcmp(whitebalance_string, "daylight") == 0)
828                         whitebalance = WHITE_BALANCE_SUNNY;
829                 else if (strcmp(whitebalance_string, "cloudy-daylight") == 0)
830                         whitebalance = WHITE_BALANCE_CLOUDY;
831                 else
832                         whitebalance = WHITE_BALANCE_AUTO;
833
834                 if (whitebalance != exynos_camera->whitebalance || force) {
835                         exynos_camera->whitebalance = whitebalance;
836                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_WHITE_BALANCE, whitebalance);
837                         if (rc < 0)
838                                 LOGE("%s: s ctrl failed!", __func__);
839                 }
840         }
841
842         // Scene mode
843         scene_mode_string = exynos_param_string_get(exynos_camera, "scene-mode");
844         if (scene_mode_string != NULL) {
845                 if (strcmp(scene_mode_string, "auto") == 0)
846                         scene_mode = SCENE_MODE_NONE;
847                 else if (strcmp(scene_mode_string, "portrait") == 0)
848                         scene_mode = SCENE_MODE_PORTRAIT;
849                 else if (strcmp(scene_mode_string, "landscape") == 0)
850                         scene_mode = SCENE_MODE_LANDSCAPE;
851                 else if (strcmp(scene_mode_string, "night") == 0)
852                         scene_mode = SCENE_MODE_NIGHTSHOT;
853                 else if (strcmp(scene_mode_string, "beach") == 0)
854                         scene_mode = SCENE_MODE_BEACH_SNOW;
855                 else if (strcmp(scene_mode_string, "snow") == 0)
856                         scene_mode = SCENE_MODE_BEACH_SNOW;
857                 else if (strcmp(scene_mode_string, "sunset") == 0)
858                         scene_mode = SCENE_MODE_SUNSET;
859                 else if (strcmp(scene_mode_string, "fireworks") == 0)
860                         scene_mode = SCENE_MODE_FIREWORKS;
861                 else if (strcmp(scene_mode_string, "sports") == 0)
862                         scene_mode = SCENE_MODE_SPORTS;
863                 else if (strcmp(scene_mode_string, "party") == 0)
864                         scene_mode = SCENE_MODE_PARTY_INDOOR;
865                 else if (strcmp(scene_mode_string, "candlelight") == 0)
866                         scene_mode = SCENE_MODE_CANDLE_LIGHT;
867                 else if (strcmp(scene_mode_string, "dusk-dawn") == 0)
868                         scene_mode = SCENE_MODE_DUSK_DAWN;
869                 else if (strcmp(scene_mode_string, "fall-color") == 0)
870                         scene_mode = SCENE_MODE_FALL_COLOR;
871                 else if (strcmp(scene_mode_string, "back-light") == 0)
872                         scene_mode = SCENE_MODE_BACK_LIGHT;
873                 else if (strcmp(scene_mode_string, "text") == 0)
874                         scene_mode = SCENE_MODE_TEXT;
875                 else
876                         scene_mode = SCENE_MODE_NONE;
877
878                 if (scene_mode != exynos_camera->scene_mode || force) {
879                         exynos_camera->scene_mode = scene_mode;
880                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_SCENE_MODE, scene_mode);
881                         if (rc < 0)
882                                 LOGE("%s: s ctrl failed!", __func__);
883                 }
884         }
885
886         // Effect
887         effect_string = exynos_param_string_get(exynos_camera, "effect");
888         if (effect_string != NULL) {
889                 if (strcmp(effect_string, "auto") == 0)
890                         effect = IMAGE_EFFECT_NONE;
891                 else if (strcmp(effect_string, "mono") == 0)
892                         effect = IMAGE_EFFECT_BNW;
893                 else if (strcmp(effect_string, "negative") == 0)
894                         effect = IMAGE_EFFECT_NEGATIVE;
895                 else if (strcmp(effect_string, "sepia") == 0)
896                         effect = IMAGE_EFFECT_SEPIA;
897                 else if (strcmp(effect_string, "aqua") == 0)
898                         effect = IMAGE_EFFECT_AQUA;
899                 else
900                         effect = IMAGE_EFFECT_NONE;
901
902                 if (effect != exynos_camera->effect || force) {
903                         exynos_camera->effect = effect;
904                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_EFFECT, effect);
905                         if (rc < 0)
906                                 LOGE("%s: s ctrl failed!", __func__);
907                 }
908         }
909
910         // ISO
911         iso_string = exynos_param_string_get(exynos_camera, "iso");
912         if (iso_string != NULL) {
913                 if (strcmp(iso_string, "auto") == 0)
914                         iso = ISO_AUTO;
915                 else if (strcmp(iso_string, "ISO50") == 0)
916                         iso = ISO_50;
917                 else if (strcmp(iso_string, "ISO100") == 0)
918                         iso = ISO_100;
919                 else if (strcmp(iso_string, "ISO200") == 0)
920                         iso = ISO_200;
921                 else if (strcmp(iso_string, "ISO400") == 0)
922                         iso = ISO_400;
923                 else if (strcmp(iso_string, "ISO800") == 0)
924                         iso = ISO_800;
925                 else
926                         iso = ISO_AUTO;
927
928                 if (iso != exynos_camera->iso || force) {
929                         exynos_camera->iso = iso;
930                         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_ISO, iso);
931                         if (rc < 0)
932                                 LOGE("%s: s ctrl failed!", __func__);
933                 }
934         }
935
936         LOGD("%s: Preview size: %dx%d, picture size: %dx%d, recording size: %dx%d",
937                 __func__, preview_width, preview_height, picture_width, picture_height,
938                 recording_width, recording_height);
939
940         return 0;
941 }
942
943 // Picture
944
945 int exynos_camera_picture(struct exynos_camera *exynos_camera)
946 {
947         camera_memory_t *data_memory = NULL;
948         camera_memory_t *exif_data_memory = NULL;
949         camera_memory_t *picture_data_memory = NULL;
950         camera_memory_t *jpeg_thumbnail_data_memory = NULL;
951
952         int camera_picture_format;
953         int picture_width;
954         int picture_height;
955         int picture_format;
956
957         int jpeg_thumbnail_width;
958         int jpeg_thumbnail_height;
959         int jpeg_thumbnail_quality;
960         int jpeg_quality;
961
962         int data_size;
963
964         int offset = 0;
965         void *picture_addr = NULL;
966         int picture_size = 0;
967         void *jpeg_thumbnail_addr = NULL;
968         int jpeg_thumbnail_size = 0;
969
970         int jpeg_fd;
971         struct jpeg_enc_param jpeg_enc_params;
972         enum jpeg_frame_format jpeg_in_format;
973         enum jpeg_stream_format jpeg_out_format;
974         enum jpeg_ret_type jpeg_result;
975         void *jpeg_in_buffer;
976         int jpeg_in_size;
977         void *jpeg_out_buffer;
978         int jpeg_out_size;
979
980         exif_attribute_t exif_attributes;
981         int exif_size = 0;
982
983         int index;
984         int rc;
985
986         if (exynos_camera == NULL)
987                 return -EINVAL;
988
989         picture_width = exynos_camera->picture_width;
990         picture_height = exynos_camera->picture_height;
991         picture_format = exynos_camera->picture_format;
992         camera_picture_format = exynos_camera->camera_picture_format;
993         jpeg_thumbnail_width = exynos_camera->jpeg_thumbnail_width;
994         jpeg_thumbnail_height = exynos_camera->jpeg_thumbnail_height;
995         jpeg_thumbnail_quality = exynos_camera->jpeg_thumbnail_quality;
996         jpeg_quality = exynos_camera->jpeg_quality;
997
998         if (camera_picture_format == 0)
999                 camera_picture_format = picture_format;
1000
1001         // V4L2
1002
1003         rc = exynos_v4l2_poll(exynos_camera, 0);
1004         if (rc < 0) {
1005                 LOGE("%s: poll failed!", __func__);
1006                 return -1;
1007         } else if (rc == 0) {
1008                 LOGE("%s: poll timeout!", __func__);
1009                 return -1;
1010         }
1011
1012         rc = exynos_v4l2_streamoff_cap(exynos_camera, 0);
1013         if (rc < 0) {
1014                 LOGE("%s: streamoff failed!", __func__);
1015                 return -1;
1016         }
1017
1018         index = exynos_v4l2_dqbuf_cap(exynos_camera, 0);
1019         if (index < 0) {
1020                 LOGE("%s: dqbuf failed!", __func__);
1021                 return -1;
1022         }
1023
1024         // This assumes that the output format is JPEG
1025
1026         if (camera_picture_format == V4L2_PIX_FMT_JPEG) {
1027                 rc = exynos_v4l2_g_ctrl(exynos_camera, 0, V4L2_CID_CAM_JPEG_MAIN_SIZE,
1028                         &picture_size);
1029                 if (rc < 0) {
1030                         LOGE("%s: g ctrl failed!", __func__);
1031                         return -1;
1032                 }
1033
1034                 rc = exynos_v4l2_g_ctrl(exynos_camera, 0, V4L2_CID_CAM_JPEG_MAIN_OFFSET,
1035                         &offset);
1036                 if (rc < 0) {
1037                         LOGE("%s: g ctrl failed!", __func__);
1038                         return -1;
1039                 }
1040
1041                 picture_addr = (void *) ((int) exynos_camera->picture_memory->data + offset);
1042
1043                 rc = exynos_v4l2_g_ctrl(exynos_camera, 0, V4L2_CID_CAM_JPEG_THUMB_SIZE,
1044                         &jpeg_thumbnail_size);
1045                 if (rc < 0) {
1046                         LOGE("%s: g ctrl failed!", __func__);
1047                         return -1;
1048                 }
1049
1050                 rc = exynos_v4l2_g_ctrl(exynos_camera, 0, V4L2_CID_CAM_JPEG_THUMB_OFFSET,
1051                         &offset);
1052                 if (rc < 0) {
1053                         LOGE("%s: g ctrl failed!", __func__);
1054                         return -1;
1055                 }
1056
1057                 jpeg_thumbnail_addr = (void *) ((int) exynos_camera->picture_memory->data + offset);
1058         }
1059
1060         // Thumbnail
1061
1062         if (camera_picture_format == V4L2_PIX_FMT_JPEG && jpeg_thumbnail_addr != NULL && jpeg_thumbnail_size >= 0) {
1063                 if (exynos_camera->callbacks.request_memory != NULL) {
1064                         jpeg_thumbnail_data_memory =
1065                                 exynos_camera->callbacks.request_memory(-1,
1066                                         jpeg_thumbnail_size, 1, 0);
1067                         if (jpeg_thumbnail_data_memory == NULL) {
1068                                 LOGE("%s: thumb memory request failed!", __func__);
1069                                 goto error;
1070                         }
1071                 } else {
1072                         LOGE("%s: No memory request function!", __func__);
1073                         goto error;
1074                 }
1075
1076                 memcpy(jpeg_thumbnail_data_memory->data, jpeg_thumbnail_addr, jpeg_thumbnail_size);
1077         } else {
1078                 jpeg_fd = api_jpeg_encode_init();
1079                 if (jpeg_fd < 0) {
1080                         LOGE("%s: Failed to init JPEG", __func__);
1081                         goto error;
1082                 }
1083
1084                 switch (camera_picture_format) {
1085                         case V4L2_PIX_FMT_RGB565:
1086                                 jpeg_in_format = RGB_565;
1087                                 jpeg_out_format = JPEG_420;
1088                                 jpeg_in_size = (picture_width * picture_height * 2);
1089                                 break;
1090                         case V4L2_PIX_FMT_NV12:
1091                         case V4L2_PIX_FMT_NV21:
1092                         case V4L2_PIX_FMT_NV12T:
1093                         case V4L2_PIX_FMT_YUV420:
1094                                 jpeg_in_format = YUV_420;
1095                                 jpeg_out_format = JPEG_420;
1096                                 jpeg_in_size = (picture_width * picture_height * 1.5);
1097                                 break;
1098                         case V4L2_PIX_FMT_YUYV:
1099                         case V4L2_PIX_FMT_UYVY:
1100                         case V4L2_PIX_FMT_YUV422P:
1101                         default:
1102                                 jpeg_in_format = YUV_422;
1103                                 jpeg_out_format = JPEG_422;
1104                                 jpeg_in_size = (picture_width * picture_height * 2);
1105                                 break;
1106                 }
1107
1108                 memset(&jpeg_enc_params, 0, sizeof(jpeg_enc_params));
1109
1110                 jpeg_enc_params.width = jpeg_thumbnail_width;
1111                 jpeg_enc_params.height = jpeg_thumbnail_height;
1112                 jpeg_enc_params.in_fmt = jpeg_in_format;
1113                 jpeg_enc_params.out_fmt = jpeg_out_format;
1114
1115                 if (jpeg_thumbnail_quality >= 90)
1116                         jpeg_enc_params.quality = QUALITY_LEVEL_1;
1117                 else if (jpeg_thumbnail_quality >= 80)
1118                         jpeg_enc_params.quality = QUALITY_LEVEL_2;
1119                 else if (jpeg_thumbnail_quality >= 70)
1120                         jpeg_enc_params.quality = QUALITY_LEVEL_3;
1121                 else
1122                         jpeg_enc_params.quality = QUALITY_LEVEL_4;
1123
1124                 api_jpeg_set_encode_param(&jpeg_enc_params);
1125
1126                 jpeg_in_buffer = api_jpeg_get_encode_in_buf(jpeg_fd, jpeg_in_size);
1127                 if (jpeg_in_buffer == NULL) {
1128                         LOGE("%s: Failed to get JPEG in buffer", __func__);
1129                         api_jpeg_encode_deinit(jpeg_fd);
1130                         goto error;
1131                 }
1132
1133                 jpeg_out_buffer = api_jpeg_get_encode_out_buf(jpeg_fd);
1134                 if (jpeg_out_buffer == NULL) {
1135                         LOGE("%s: Failed to get JPEG out buffer", __func__);
1136                         api_jpeg_encode_deinit(jpeg_fd);
1137                         goto error;
1138                 }
1139
1140                 memcpy(jpeg_in_buffer, exynos_camera->picture_memory->data, jpeg_in_size);
1141
1142                 jpeg_result = api_jpeg_encode_exe(jpeg_fd, &jpeg_enc_params);
1143                 if (jpeg_result != JPEG_ENCODE_OK) {
1144                         LOGE("%s: Failed to encode JPEG", __func__);
1145                         api_jpeg_encode_deinit(jpeg_fd);
1146                         goto error;
1147                 }
1148
1149                 jpeg_out_size = jpeg_enc_params.size;
1150                 if (jpeg_out_size <= 0) {
1151                         LOGE("%s: Failed to get JPEG out size", __func__);
1152                         api_jpeg_encode_deinit(jpeg_fd);
1153                         goto error;
1154                 }
1155
1156                 if (exynos_camera->callbacks.request_memory != NULL) {
1157                         jpeg_thumbnail_data_memory =
1158                                 exynos_camera->callbacks.request_memory(-1,
1159                                         jpeg_out_size, 1, 0);
1160                         if (jpeg_thumbnail_data_memory == NULL) {
1161                                 LOGE("%s: thumbnail memory request failed!", __func__);
1162                                 api_jpeg_encode_deinit(jpeg_fd);
1163                                 goto error;
1164                         }
1165                 } else {
1166                         LOGE("%s: No memory request function!", __func__);
1167                         api_jpeg_encode_deinit(jpeg_fd);
1168                         goto error;
1169                 }
1170
1171                 memcpy(jpeg_thumbnail_data_memory->data, jpeg_out_buffer, jpeg_out_size);
1172                 jpeg_thumbnail_size = jpeg_out_size;
1173
1174                 api_jpeg_encode_deinit(jpeg_fd);
1175         }
1176
1177         // Picture
1178
1179         if (camera_picture_format == V4L2_PIX_FMT_JPEG && picture_addr != NULL && picture_size >= 0) {
1180                 if (exynos_camera->callbacks.request_memory != NULL) {
1181                         picture_data_memory =
1182                                 exynos_camera->callbacks.request_memory(-1,
1183                                         picture_size, 1, 0);
1184                         if (picture_data_memory == NULL) {
1185                                 LOGE("%s: picture memory request failed!", __func__);
1186                                 goto error;
1187                         }
1188                 } else {
1189                         LOGE("%s: No memory request function!", __func__);
1190                         goto error;
1191                 }
1192
1193                 memcpy(picture_data_memory->data, picture_addr, picture_size);
1194         } else {
1195                 jpeg_fd = api_jpeg_encode_init();
1196                 if (jpeg_fd < 0) {
1197                         LOGE("%s: Failed to init JPEG", __func__);
1198                         goto error;
1199                 }
1200
1201                 switch (camera_picture_format) {
1202                         case V4L2_PIX_FMT_RGB565:
1203                                 jpeg_in_format = RGB_565;
1204                                 jpeg_out_format = JPEG_420;
1205                                 jpeg_in_size = (picture_width * picture_height * 2);
1206                                 break;
1207                         case V4L2_PIX_FMT_NV12:
1208                         case V4L2_PIX_FMT_NV21:
1209                         case V4L2_PIX_FMT_NV12T:
1210                         case V4L2_PIX_FMT_YUV420:
1211                                 jpeg_in_format = YUV_420;
1212                                 jpeg_out_format = JPEG_420;
1213                                 jpeg_in_size = (picture_width * picture_height * 1.5);
1214                                 break;
1215                         case V4L2_PIX_FMT_YUYV:
1216                         case V4L2_PIX_FMT_UYVY:
1217                         case V4L2_PIX_FMT_YUV422P:
1218                         default:
1219                                 jpeg_in_format = YUV_422;
1220                                 jpeg_out_format = JPEG_422;
1221                                 jpeg_in_size = (picture_width * picture_height * 2);
1222                                 break;
1223                 }
1224
1225                 memset(&jpeg_enc_params, 0, sizeof(jpeg_enc_params));
1226
1227                 jpeg_enc_params.width = picture_width;
1228                 jpeg_enc_params.height = picture_height;
1229                 jpeg_enc_params.in_fmt = jpeg_in_format;
1230                 jpeg_enc_params.out_fmt = jpeg_out_format;
1231
1232                 if (jpeg_quality >= 90)
1233                         jpeg_enc_params.quality = QUALITY_LEVEL_1;
1234                 else if (jpeg_quality >= 80)
1235                         jpeg_enc_params.quality = QUALITY_LEVEL_2;
1236                 else if (jpeg_quality >= 70)
1237                         jpeg_enc_params.quality = QUALITY_LEVEL_3;
1238                 else
1239                         jpeg_enc_params.quality = QUALITY_LEVEL_4;
1240
1241                 api_jpeg_set_encode_param(&jpeg_enc_params);
1242
1243                 jpeg_in_buffer = api_jpeg_get_encode_in_buf(jpeg_fd, jpeg_in_size);
1244                 if (jpeg_in_buffer == NULL) {
1245                         LOGE("%s: Failed to get JPEG in buffer", __func__);
1246                         api_jpeg_encode_deinit(jpeg_fd);
1247                         goto error;
1248                 }
1249
1250                 jpeg_out_buffer = api_jpeg_get_encode_out_buf(jpeg_fd);
1251                 if (jpeg_out_buffer == NULL) {
1252                         LOGE("%s: Failed to get JPEG out buffer", __func__);
1253                         api_jpeg_encode_deinit(jpeg_fd);
1254                         goto error;
1255                 }
1256
1257                 memcpy(jpeg_in_buffer, exynos_camera->picture_memory->data, jpeg_in_size);
1258
1259                 jpeg_result = api_jpeg_encode_exe(jpeg_fd, &jpeg_enc_params);
1260                 if (jpeg_result != JPEG_ENCODE_OK) {
1261                         LOGE("%s: Failed to encode JPEG", __func__);
1262                         api_jpeg_encode_deinit(jpeg_fd);
1263                         goto error;
1264                 }
1265
1266                 jpeg_out_size = jpeg_enc_params.size;
1267                 if (jpeg_out_size <= 0) {
1268                         LOGE("%s: Failed to get JPEG out size", __func__);
1269                         api_jpeg_encode_deinit(jpeg_fd);
1270                         goto error;
1271                 }
1272
1273                 if (exynos_camera->callbacks.request_memory != NULL) {
1274                         picture_data_memory =
1275                                 exynos_camera->callbacks.request_memory(-1,
1276                                         jpeg_out_size, 1, 0);
1277                         if (picture_data_memory == NULL) {
1278                                 LOGE("%s: picture memory request failed!", __func__);
1279                                 api_jpeg_encode_deinit(jpeg_fd);
1280                                 goto error;
1281                         }
1282                 } else {
1283                         LOGE("%s: No memory request function!", __func__);
1284                         api_jpeg_encode_deinit(jpeg_fd);
1285                         goto error;
1286                 }
1287
1288                 memcpy(picture_data_memory->data, jpeg_out_buffer, jpeg_out_size);
1289                 picture_size = jpeg_out_size;
1290
1291                 api_jpeg_encode_deinit(jpeg_fd);
1292         }
1293
1294         // EXIF
1295
1296         memset(&exif_attributes, 0, sizeof(exif_attributes));
1297         exynos_exif_attributes_create_static(exynos_camera, &exif_attributes);
1298         exynos_exif_attributes_create_params(exynos_camera, &exif_attributes);
1299
1300         rc = exynos_exif_create(exynos_camera, &exif_attributes,
1301                 jpeg_thumbnail_data_memory, jpeg_thumbnail_size,
1302                 &exif_data_memory, &exif_size);
1303         if (rc < 0 || exif_data_memory == NULL || exif_size <= 0) {
1304                 LOGE("%s: EXIF create failed!", __func__);
1305                 goto error;
1306         }
1307
1308         data_size = exif_size + picture_size;
1309
1310         if (exynos_camera->callbacks.request_memory != NULL) {
1311                 data_memory =
1312                         exynos_camera->callbacks.request_memory(-1,
1313                                 data_size, 1, 0);
1314                 if (data_memory == NULL) {
1315                         LOGE("%s: data memory request failed!", __func__);
1316                         goto error;
1317                 }
1318         } else {
1319                 LOGE("%s: No memory request function!", __func__);
1320                 goto error;
1321         }
1322
1323         // Copy the first two bytes of the JPEG picture
1324         memcpy(data_memory->data, picture_data_memory->data, 2);
1325
1326         // Copy the EXIF data
1327         memcpy((void *) ((int) data_memory->data + 2), exif_data_memory->data,
1328                 exif_size);
1329
1330         // Copy the JPEG picture
1331         memcpy((void *) ((int) data_memory->data + 2 + exif_size),
1332                 (void *) ((int) picture_data_memory->data + 2), picture_size - 2);
1333
1334         // Callbacks
1335
1336         if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_SHUTTER) && EXYNOS_CAMERA_CALLBACK_DEFINED(notify))
1337                 exynos_camera->callbacks.notify(CAMERA_MSG_SHUTTER, 0, 0,
1338                         exynos_camera->callbacks.user);
1339
1340         if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_RAW_IMAGE) && EXYNOS_CAMERA_CALLBACK_DEFINED(data) &&
1341                 jpeg_thumbnail_data_memory != NULL)
1342                 exynos_camera->callbacks.data(CAMERA_MSG_RAW_IMAGE,
1343                         jpeg_thumbnail_data_memory, 0, NULL, exynos_camera->callbacks.user);
1344
1345         if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_COMPRESSED_IMAGE) && EXYNOS_CAMERA_CALLBACK_DEFINED(data) &&
1346                 data_memory != NULL)
1347                 exynos_camera->callbacks.data(CAMERA_MSG_COMPRESSED_IMAGE,
1348                         data_memory, 0, NULL, exynos_camera->callbacks.user);
1349
1350         // Release memory
1351
1352         if (jpeg_thumbnail_data_memory != NULL && jpeg_thumbnail_data_memory->release != NULL)
1353                 jpeg_thumbnail_data_memory->release(jpeg_thumbnail_data_memory);
1354
1355         if (picture_data_memory != NULL && picture_data_memory->release != NULL)
1356                 picture_data_memory->release(picture_data_memory);
1357
1358         if (exif_data_memory != NULL && exif_data_memory->release != NULL)
1359                 exif_data_memory->release(exif_data_memory);
1360
1361         if (data_memory != NULL && data_memory->release != NULL)
1362                 data_memory->release(data_memory);
1363
1364         return 0;
1365
1366 error:
1367         if (jpeg_thumbnail_data_memory != NULL && jpeg_thumbnail_data_memory->release != NULL)
1368                 jpeg_thumbnail_data_memory->release(jpeg_thumbnail_data_memory);
1369
1370         if (picture_data_memory != NULL && picture_data_memory->release != NULL)
1371                 picture_data_memory->release(picture_data_memory);
1372
1373         if (exif_data_memory != NULL && exif_data_memory->release != NULL)
1374                 exif_data_memory->release(exif_data_memory);
1375
1376         if (data_memory != NULL && data_memory->release != NULL)
1377                 data_memory->release(data_memory);
1378
1379         return -1;
1380 }
1381
1382 void *exynos_camera_picture_thread(void *data)
1383 {
1384         struct exynos_camera *exynos_camera;
1385         int rc;
1386         int i;
1387
1388         if (data == NULL)
1389                 return NULL;
1390
1391         exynos_camera = (struct exynos_camera *) data;
1392
1393         LOGE("%s: Starting thread", __func__);
1394         exynos_camera->picture_thread_running = 1;
1395
1396         if (exynos_camera->picture_enabled == 1) {
1397                 pthread_mutex_lock(&exynos_camera->picture_mutex);
1398
1399                 rc = exynos_camera_picture(exynos_camera);
1400                 if (rc < 0) {
1401                         LOGE("%s: picture failed!", __func__);
1402                         exynos_camera->picture_enabled = 0;
1403                 }
1404
1405                 if (exynos_camera->picture_memory != NULL && exynos_camera->picture_memory->release != NULL) {
1406                         exynos_camera->picture_memory->release(exynos_camera->picture_memory);
1407                         exynos_camera->picture_memory = NULL;
1408                 }
1409
1410                 pthread_mutex_unlock(&exynos_camera->picture_mutex);
1411         }
1412
1413         exynos_camera->picture_thread_running = 0;
1414         exynos_camera->picture_enabled = 0;
1415
1416         LOGE("%s: Exiting thread", __func__);
1417
1418         return NULL;
1419 }
1420
1421 int exynos_camera_picture_start(struct exynos_camera *exynos_camera)
1422 {
1423         pthread_attr_t thread_attr;
1424
1425         int width, height, format, camera_format;
1426
1427         int fd;
1428         int rc;
1429
1430         if (exynos_camera == NULL)
1431                 return -EINVAL;
1432
1433         // Stop preview thread
1434         exynos_camera_preview_stop(exynos_camera);
1435
1436         width = exynos_camera->picture_width;
1437         height = exynos_camera->picture_height;
1438         format = exynos_camera->picture_format;
1439         camera_format = exynos_camera->camera_picture_format;
1440
1441         // V4L2
1442
1443         if (camera_format == 0)
1444                 camera_format = format;
1445
1446         rc = exynos_v4l2_enum_fmt_cap(exynos_camera, 0, camera_format);
1447         if (rc < 0) {
1448                 LOGE("%s: enum fmt failed!", __func__);
1449                 return -1;
1450         }
1451
1452         rc = exynos_v4l2_s_fmt_pix_cap(exynos_camera, 0, width, height, camera_format, V4L2_PIX_FMT_MODE_CAPTURE);
1453         if (rc < 0) {
1454                 LOGE("%s: s fmt failed!", __func__);
1455                 return -1;
1456         }
1457
1458         // Only use 1 buffer
1459         rc = exynos_v4l2_reqbufs_cap(exynos_camera, 0, 1);
1460         if (rc < 0) {
1461                 LOGE("%s: reqbufs failed!", __func__);
1462                 return -1;
1463         }
1464
1465         rc = exynos_v4l2_querybuf_cap(exynos_camera, 0, 0);
1466         if (rc < 0) {
1467                 LOGE("%s: querybuf failed!", __func__);
1468                 return -1;
1469         }
1470
1471         exynos_camera->picture_buffer_length = rc;
1472
1473         if (exynos_camera->callbacks.request_memory != NULL) {
1474                 fd = exynos_v4l2_find_fd(exynos_camera, 0);
1475                 if (fd < 0) {
1476                         LOGE("%s: Unable to find v4l2 fd", __func__);
1477                         return -1;
1478                 }
1479
1480                 if (exynos_camera->picture_memory != NULL && exynos_camera->picture_memory->release != NULL)
1481                         exynos_camera->picture_memory->release(exynos_camera->picture_memory);
1482
1483                 exynos_camera->picture_memory =
1484                         exynos_camera->callbacks.request_memory(fd,
1485                                 exynos_camera->picture_buffer_length, 1, 0);
1486                 if (exynos_camera->picture_memory == NULL) {
1487                         LOGE("%s: memory request failed!", __func__);
1488                         return -1;
1489                 }
1490         } else {
1491                 LOGE("%s: No memory request function!", __func__);
1492                 return -1;
1493         }
1494
1495         rc = exynos_v4l2_qbuf_cap(exynos_camera, 0, 0);
1496         if (rc < 0) {
1497                 LOGE("%s: qbuf failed!", __func__);
1498                 return -1;
1499         }
1500
1501         rc = exynos_v4l2_streamon_cap(exynos_camera, 0);
1502         if (rc < 0) {
1503                 LOGE("%s: streamon failed!", __func__);
1504                 return -1;
1505         }
1506
1507         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_CAPTURE, 0);
1508         if (rc < 0) {
1509                 LOGE("%s: s ctrl failed!", __func__);
1510                 return -1;
1511         }
1512
1513         // Thread
1514
1515         if (exynos_camera->picture_thread_running) {
1516                 LOGE("Picture thread is already running!");
1517                 return -1;
1518         }
1519
1520         pthread_mutex_init(&exynos_camera->picture_mutex, NULL);
1521
1522         pthread_attr_init(&thread_attr);
1523         pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1524
1525         exynos_camera->picture_enabled = 1;
1526
1527         rc = pthread_create(&exynos_camera->picture_thread, &thread_attr,
1528                 exynos_camera_picture_thread, (void *) exynos_camera);
1529         if (rc < 0) {
1530                 LOGE("%s: Unable to create thread", __func__);
1531                 return -1;
1532         }
1533
1534         return 0;
1535 }
1536
1537 void exynos_camera_picture_stop(struct exynos_camera *exynos_camera)
1538 {
1539         int rc;
1540         int i;
1541
1542         if (exynos_camera == NULL)
1543                 return;
1544
1545         if (!exynos_camera->picture_enabled) {
1546                 LOGE("Picture was already stopped!");
1547                 return;
1548         }
1549
1550         pthread_mutex_lock(&exynos_camera->picture_mutex);
1551
1552         // Disable picture to make the thread end
1553         exynos_camera->picture_enabled = 0;
1554
1555         pthread_mutex_unlock(&exynos_camera->picture_mutex);
1556
1557         // Wait for the thread to end
1558         for (i=0 ; i < 10 ; i++) {
1559                 if (!exynos_camera->picture_thread_running)
1560                         break;
1561
1562                 usleep(500);
1563         }
1564
1565         pthread_mutex_destroy(&exynos_camera->picture_mutex);
1566 }
1567
1568 // Auto-focus
1569
1570 void *exynos_camera_auto_focus_thread(void *data)
1571 {
1572         struct exynos_camera *exynos_camera;
1573         int auto_focus_status = -1;
1574         int auto_focus_result = 0;
1575         int rc;
1576         int i;
1577
1578         if (data == NULL)
1579                 return NULL;
1580
1581         exynos_camera = (struct exynos_camera *) data;
1582
1583         LOGE("%s: Starting thread", __func__);
1584         exynos_camera->auto_focus_thread_running = 1;
1585
1586         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_SET_AUTO_FOCUS, AUTO_FOCUS_ON);
1587         if (rc < 0) {
1588                 LOGE("%s: s ctrl failed!", __func__);
1589                 auto_focus_result = 0;
1590                 goto thread_exit;
1591         }
1592
1593         while (exynos_camera->auto_focus_enabled == 1) {
1594                 pthread_mutex_lock(&exynos_camera->auto_focus_mutex);
1595
1596                 rc = exynos_v4l2_g_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_AUTO_FOCUS_RESULT, &auto_focus_status);
1597                 if (rc < 0) {
1598                         LOGE("%s: g ctrl failed!", __func__);
1599                         auto_focus_result = 0;
1600                         pthread_mutex_unlock(&exynos_camera->auto_focus_mutex);
1601                         goto thread_exit;
1602                 }
1603
1604                 switch (auto_focus_status) {
1605                         case CAMERA_AF_STATUS_IN_PROGRESS:
1606                                 usleep(500);
1607                                 break;
1608                         case CAMERA_AF_STATUS_SUCCESS:
1609                         case CAMERA_AF_STATUS_1ST_SUCCESS:
1610                                 auto_focus_result = 1;
1611                                 pthread_mutex_unlock(&exynos_camera->auto_focus_mutex);
1612                                 goto thread_exit;
1613                         case CAMERA_AF_STATUS_FAIL:
1614                         default:
1615                                 auto_focus_result = 0;
1616                                 pthread_mutex_unlock(&exynos_camera->auto_focus_mutex);
1617                                 goto thread_exit;                               
1618                 }
1619
1620                 pthread_mutex_unlock(&exynos_camera->auto_focus_mutex);
1621         }
1622
1623 thread_exit:
1624         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CAMERA_SET_AUTO_FOCUS, AUTO_FOCUS_OFF);
1625         if (rc < 0)
1626                 LOGE("%s: s ctrl failed!", __func__);
1627
1628         if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_FOCUS) && EXYNOS_CAMERA_CALLBACK_DEFINED(notify))
1629                 exynos_camera->callbacks.notify(CAMERA_MSG_FOCUS,
1630                         (int32_t) auto_focus_result, 0, exynos_camera->callbacks.user);
1631                 
1632         exynos_camera->auto_focus_thread_running = 0;
1633         exynos_camera->auto_focus_enabled = 0;
1634
1635         LOGE("%s: Exiting thread", __func__);
1636
1637         return NULL;
1638 }
1639
1640 int exynos_camera_auto_focus_start(struct exynos_camera *exynos_camera)
1641 {
1642         pthread_attr_t thread_attr;
1643         int rc;
1644
1645         if (exynos_camera == NULL)
1646                 return -EINVAL;
1647
1648         // Thread
1649
1650         if (exynos_camera->auto_focus_thread_running) {
1651                 LOGE("Auto-focus thread is already running!");
1652                 return -1;
1653         }
1654
1655         pthread_mutex_init(&exynos_camera->auto_focus_mutex, NULL);
1656
1657         pthread_attr_init(&thread_attr);
1658         pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1659
1660         exynos_camera->auto_focus_enabled = 1;
1661
1662         rc = pthread_create(&exynos_camera->auto_focus_thread, &thread_attr,
1663                 exynos_camera_auto_focus_thread, (void *) exynos_camera);
1664         if (rc < 0) {
1665                 LOGE("%s: Unable to create thread", __func__);
1666                 return -1;
1667         }
1668
1669         return 0;
1670 }
1671
1672 void exynos_camera_auto_focus_stop(struct exynos_camera *exynos_camera)
1673 {
1674         int rc;
1675         int i;
1676
1677         if (exynos_camera == NULL)
1678                 return;
1679
1680         if (!exynos_camera->auto_focus_enabled) {
1681                 LOGE("Auto-focus was already stopped!");
1682                 return;
1683         }
1684
1685         pthread_mutex_lock(&exynos_camera->auto_focus_mutex);
1686
1687         // Disable auto-focus to make the thread end
1688         exynos_camera->auto_focus_enabled = 0;
1689
1690         pthread_mutex_unlock(&exynos_camera->auto_focus_mutex);
1691
1692         // Wait for the thread to end
1693         for (i=0 ; i < 10 ; i++) {
1694                 if (!exynos_camera->auto_focus_thread_running)
1695                         break;
1696
1697                 usleep(500);
1698         }
1699
1700         pthread_mutex_destroy(&exynos_camera->auto_focus_mutex);
1701 }
1702
1703 // Preview
1704
1705 int exynos_camera_preview(struct exynos_camera *exynos_camera)
1706 {
1707         buffer_handle_t *buffer;
1708         int stride;
1709
1710         int width, height;
1711         float format_bpp;
1712
1713         char *preview_format_string;
1714         int frame_size, offset;
1715         void *preview_data;
1716         void *window_data;
1717
1718         unsigned int recording_y_addr;
1719         unsigned int recording_cbcr_addr;
1720         nsecs_t timestamp;
1721         struct exynos_camera_addrs *addrs;
1722         struct timespec ts;
1723
1724         int index;
1725         int rc;
1726         int i;
1727
1728         if (exynos_camera == NULL || exynos_camera->preview_memory == NULL ||
1729                 exynos_camera->preview_window == NULL)
1730                 return -EINVAL;
1731
1732         timestamp = systemTime(1);
1733
1734         // V4L2
1735
1736         rc = exynos_v4l2_poll(exynos_camera, 0);
1737         if (rc < 0) {
1738                 LOGE("%s: poll failed!", __func__);
1739                 return -1;
1740         } else if (rc == 0) {
1741                 LOGE("%s: poll timeout!", __func__);
1742                 return -1;
1743         }
1744
1745         index = exynos_v4l2_dqbuf_cap(exynos_camera, 0);
1746         if (index < 0 || index >= exynos_camera->preview_buffers_count) {
1747                 LOGE("%s: dqbuf failed!", __func__);
1748                 return -1;
1749         }
1750
1751         rc = exynos_v4l2_qbuf_cap(exynos_camera, 0, index);
1752         if (rc < 0) {
1753                 LOGE("%s: qbuf failed!", __func__);
1754                 return -1;
1755         }
1756
1757         // Preview window
1758
1759         width = exynos_camera->preview_width;
1760         height = exynos_camera->preview_height;
1761         format_bpp = exynos_camera->preview_format_bpp;
1762
1763         exynos_camera->preview_window->dequeue_buffer(exynos_camera->preview_window,
1764                 &buffer, &stride);
1765         exynos_camera->gralloc->lock(exynos_camera->gralloc, *buffer, GRALLOC_USAGE_SW_WRITE_OFTEN,
1766                 0, 0, width, height, &window_data);
1767
1768         if (window_data == NULL) {
1769                 LOGE("%s: gralloc lock failed!", __func__);
1770                 return -1;
1771         }
1772
1773         frame_size = (int) ((float) width * (float) height * format_bpp);
1774         offset = index * frame_size;
1775
1776         preview_data = (void *) ((int) exynos_camera->preview_memory->data + offset);
1777         memcpy(window_data, preview_data, frame_size);
1778
1779         exynos_camera->gralloc->unlock(exynos_camera->gralloc, *buffer);
1780         exynos_camera->preview_window->enqueue_buffer(exynos_camera->preview_window,
1781                 buffer);
1782
1783         if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_PREVIEW_FRAME) && EXYNOS_CAMERA_CALLBACK_DEFINED(data)) {
1784                 exynos_camera->callbacks.data(CAMERA_MSG_PREVIEW_FRAME,
1785                         exynos_camera->preview_memory, index, NULL, exynos_camera->callbacks.user);
1786         }
1787
1788         // Recording
1789
1790         if (exynos_camera->recording_enabled && exynos_camera->recording_memory != NULL) {
1791                 pthread_mutex_lock(&exynos_camera->recording_mutex);
1792
1793                 // V4L2
1794
1795                 rc = exynos_v4l2_poll(exynos_camera, 2);
1796                 if (rc < 0) {
1797                         LOGE("%s: poll failed!", __func__);
1798                         goto error_recording;
1799                 } else if (rc == 0) {
1800                         LOGE("%s: poll timeout!", __func__);
1801                         goto error_recording;
1802                 }
1803
1804                 index = exynos_v4l2_dqbuf_cap(exynos_camera, 2);
1805                 if (index < 0) {
1806                         LOGE("%s: dqbuf failed!", __func__);
1807                         goto error_recording;
1808                 }
1809
1810                 recording_y_addr = exynos_v4l2_s_ctrl(exynos_camera, 2, V4L2_CID_PADDR_Y, index);
1811                 if (recording_y_addr == 0xffffffff) {
1812                         LOGE("%s: s ctrl failed!", __func__);
1813                         goto error_recording;
1814                 }
1815
1816                 recording_cbcr_addr = exynos_v4l2_s_ctrl(exynos_camera, 2, V4L2_CID_PADDR_CBCR, index);
1817                 if (recording_cbcr_addr == 0xffffffff) {
1818                         LOGE("%s: s ctrl failed!", __func__);
1819                         goto error_recording;
1820                 }
1821
1822                 addrs = (struct exynos_camera_addrs *) exynos_camera->recording_memory->data;
1823
1824                 addrs[index].type = 0; // kMetadataBufferTypeCameraSource
1825                 addrs[index].y = recording_y_addr;
1826                 addrs[index].cbcr = recording_cbcr_addr;
1827                 addrs[index].index = index;
1828                 addrs[index].reserved = 0;
1829
1830                 pthread_mutex_unlock(&exynos_camera->recording_mutex);
1831
1832                 if (EXYNOS_CAMERA_MSG_ENABLED(CAMERA_MSG_VIDEO_FRAME) && EXYNOS_CAMERA_CALLBACK_DEFINED(data_timestamp)) {
1833                         exynos_camera->callbacks.data_timestamp(timestamp, CAMERA_MSG_VIDEO_FRAME,
1834                                 exynos_camera->recording_memory, index, exynos_camera->callbacks.user);
1835                 } else {
1836                         rc = exynos_v4l2_qbuf_cap(exynos_camera, 2, index);
1837                         if (rc < 0) {
1838                                 LOGE("%s: qbuf failed!", __func__);
1839                                 return -1;
1840                         }
1841                 }
1842         }
1843
1844         return 0;
1845
1846 error_recording:
1847         pthread_mutex_lock(&exynos_camera->recording_mutex);
1848
1849         return -1;
1850 }
1851
1852 void *exynos_camera_preview_thread(void *data)
1853 {
1854         struct exynos_camera *exynos_camera;
1855         int rc;
1856
1857         if (data == NULL)
1858                 return NULL;
1859
1860         exynos_camera = (struct exynos_camera *) data;
1861
1862         LOGE("%s: Starting thread", __func__);
1863         exynos_camera->preview_thread_running = 1;
1864
1865         if (exynos_camera->preview_window == NULL) {
1866                 // Lock preview lock mutex
1867                 pthread_mutex_lock(&exynos_camera->preview_lock_mutex);
1868         }
1869
1870         while (exynos_camera->preview_enabled == 1) {
1871                 pthread_mutex_lock(&exynos_camera->preview_mutex);
1872
1873                 rc = exynos_camera_preview(exynos_camera);
1874                 if (rc < 0) {
1875                         LOGE("%s: preview failed!", __func__);
1876                         exynos_camera->preview_enabled = 0;
1877                 }
1878
1879                 pthread_mutex_unlock(&exynos_camera->preview_mutex);
1880         }
1881
1882         exynos_camera->preview_thread_running = 0;
1883         LOGE("%s: Exiting thread", __func__);
1884
1885         return NULL;
1886 }
1887
1888 int exynos_camera_preview_start(struct exynos_camera *exynos_camera)
1889 {
1890         struct v4l2_streamparm streamparm;
1891         int width, height, format;
1892         float format_bpp;
1893         int fps, frame_size;
1894         int fd;
1895
1896         pthread_attr_t thread_attr;
1897
1898         int rc;
1899         int i;
1900
1901         if (exynos_camera == NULL)
1902                 return -EINVAL;
1903
1904         if (exynos_camera->preview_enabled) {
1905                 LOGE("Preview was already started!");
1906                 return 0;
1907         }
1908
1909         // V4L2
1910
1911         format = exynos_camera->preview_format;
1912
1913         rc = exynos_v4l2_enum_fmt_cap(exynos_camera, 0, format);
1914         if (rc < 0) {
1915                 LOGE("%s: enum fmt failed!", __func__);
1916                 return -1;
1917         }
1918
1919         width = exynos_camera->preview_width;
1920         height = exynos_camera->preview_height;
1921         format_bpp = exynos_camera->preview_format_bpp;
1922
1923         rc = exynos_v4l2_s_fmt_pix_cap(exynos_camera, 0, width, height, format, V4L2_PIX_FMT_MODE_PREVIEW);
1924         if (rc < 0) {
1925                 LOGE("%s: s fmt failed!", __func__);
1926                 return -1;
1927         }
1928
1929         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_CACHEABLE, 1);
1930         if (rc < 0) {
1931                 LOGE("%s: s ctrl failed!", __func__);
1932                 return -1;
1933         }
1934
1935         for (i=EXYNOS_CAMERA_MAX_BUFFERS_COUNT ; i >= EXYNOS_CAMERA_MIN_BUFFERS_COUNT ; i--) {
1936                 rc = exynos_v4l2_reqbufs_cap(exynos_camera, 0, i);
1937                 if (rc >= 0)
1938                         break;
1939         }
1940
1941         if (rc < 0) {
1942                 LOGE("%s: reqbufs failed!", __func__);
1943                 return -1;
1944         }
1945
1946         exynos_camera->preview_buffers_count = rc;
1947         LOGD("Found %d preview buffers available!", exynos_camera->preview_buffers_count);
1948
1949         fps = exynos_camera->preview_fps;
1950         memset(&streamparm, 0, sizeof(streamparm));
1951         streamparm.parm.capture.timeperframe.numerator = 1;
1952         streamparm.parm.capture.timeperframe.denominator = fps;
1953
1954         rc = exynos_v4l2_s_parm_cap(exynos_camera, 0, &streamparm);
1955         if (rc < 0) {
1956                 LOGE("%s: s parm failed!", __func__);
1957                 return -1;
1958         }
1959
1960         frame_size = (int) ((float) width * (float) height * format_bpp);
1961         for (i=0 ; i < exynos_camera->preview_buffers_count ; i++) {
1962                 rc = exynos_v4l2_querybuf_cap(exynos_camera, 0, i);
1963                 if (rc < 0) {
1964                         LOGE("%s: querybuf failed!", __func__);
1965                         return -1;
1966                 }
1967
1968                 if (rc != frame_size)
1969                         LOGE("%s: Frame size mismatch: %d/%d", __func__, frame_size, rc);
1970         }
1971
1972         if (exynos_camera->callbacks.request_memory != NULL) {
1973                 fd = exynos_v4l2_find_fd(exynos_camera, 0);
1974                 if (fd < 0) {
1975                         LOGE("%s: Unable to find v4l2 fd", __func__);
1976                         return -1;
1977                 }
1978
1979                 if (exynos_camera->preview_memory != NULL && exynos_camera->preview_memory->release != NULL)
1980                         exynos_camera->preview_memory->release(exynos_camera->preview_memory);
1981
1982                 exynos_camera->preview_memory =
1983                         exynos_camera->callbacks.request_memory(fd,
1984                                 frame_size, exynos_camera->preview_buffers_count, 0);
1985                 if (exynos_camera->preview_memory == NULL) {
1986                         LOGE("%s: memory request failed!", __func__);
1987                         return -1;
1988                 }
1989         } else {
1990                 LOGE("%s: No memory request function!", __func__);
1991                 return -1;
1992         }
1993
1994         for (i=0 ; i < exynos_camera->preview_buffers_count ; i++) {
1995                 rc = exynos_v4l2_qbuf_cap(exynos_camera, 0, i);
1996                 if (rc < 0) {
1997                         LOGE("%s: qbuf failed!", __func__);
1998                         return -1;
1999                 }
2000         }
2001
2002         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_ROTATION,
2003                 exynos_camera->camera_rotation);
2004         if (rc < 0) {
2005                 LOGE("%s: s ctrl failed!", __func__);
2006                 return -1;
2007         }
2008
2009         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_HFLIP,
2010                 exynos_camera->camera_hflip);
2011         if (rc < 0) {
2012                 LOGE("%s: s ctrl failed!", __func__);
2013                 return -1;
2014         }
2015
2016         rc = exynos_v4l2_s_ctrl(exynos_camera, 0, V4L2_CID_VFLIP,
2017                 exynos_camera->camera_vflip);
2018         if (rc < 0) {
2019                 LOGE("%s: s ctrl failed!", __func__);
2020                 return -1;
2021         }
2022
2023         rc = exynos_v4l2_streamon_cap(exynos_camera, 0);
2024         if (rc < 0) {
2025                 LOGE("%s: streamon failed!", __func__);
2026                 return -1;
2027         }
2028
2029         // Thread
2030
2031         pthread_mutex_init(&exynos_camera->preview_mutex, NULL);
2032         pthread_mutex_init(&exynos_camera->preview_lock_mutex, NULL);
2033
2034         // Lock preview lock
2035         pthread_mutex_lock(&exynos_camera->preview_lock_mutex);
2036
2037         pthread_attr_init(&thread_attr);
2038         pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
2039
2040         exynos_camera->preview_enabled = 1;
2041
2042         rc = pthread_create(&exynos_camera->preview_thread, &thread_attr,
2043                 exynos_camera_preview_thread, (void *) exynos_camera);
2044         if (rc < 0) {
2045                 LOGE("%s: Unable to create thread", __func__);
2046                 return -1;
2047         }
2048
2049         return 0;
2050 }
2051
2052 void exynos_camera_preview_stop(struct exynos_camera *exynos_camera)
2053 {
2054         int rc;
2055         int i;
2056
2057         if (exynos_camera == NULL)
2058                 return;
2059
2060         if (!exynos_camera->preview_enabled) {
2061                 LOGE("Preview was already stopped!");
2062                 return;
2063         }
2064
2065         exynos_camera->preview_enabled = 0;
2066
2067         // Unlock preview lock
2068         pthread_mutex_unlock(&exynos_camera->preview_lock_mutex);
2069
2070         pthread_mutex_lock(&exynos_camera->preview_mutex);
2071
2072         // Wait for the thread to end
2073         for (i=0 ; i < 10 ; i++) {
2074                 if (!exynos_camera->preview_thread_running)
2075                         break;
2076
2077                 usleep(1000);
2078         }
2079
2080         rc = exynos_v4l2_streamoff_cap(exynos_camera, 0);
2081         if (rc < 0) {
2082                 LOGE("%s: streamoff failed!", __func__);
2083         }
2084
2085         exynos_camera->preview_params_set = 0;
2086
2087         if (exynos_camera->preview_memory != NULL && exynos_camera->preview_memory->release != NULL) {
2088                 exynos_camera->preview_memory->release(exynos_camera->preview_memory);
2089                 exynos_camera->preview_memory = NULL;
2090         }
2091
2092         exynos_camera->preview_window = NULL;
2093
2094         pthread_mutex_unlock(&exynos_camera->preview_mutex);
2095
2096         pthread_mutex_destroy(&exynos_camera->preview_lock_mutex);
2097         pthread_mutex_destroy(&exynos_camera->preview_mutex);
2098 }
2099
2100 // Recording
2101
2102 void exynos_camera_recording_frame_release(struct exynos_camera *exynos_camera, void *data)
2103 {
2104         struct exynos_camera_addrs *addrs;
2105         int rc;
2106
2107         if (exynos_camera == NULL || data == NULL)
2108                 return;
2109
2110         addrs = (struct exynos_camera_addrs *) data;
2111         if (addrs->index >= (unsigned int) exynos_camera->recording_buffers_count)
2112                 return;
2113
2114         pthread_mutex_lock(&exynos_camera->recording_mutex);
2115
2116         rc = exynos_v4l2_qbuf_cap(exynos_camera, 2, addrs->index);
2117         if (rc < 0) {
2118                 LOGE("%s: qbuf failed!", __func__);
2119                 goto error;
2120         }
2121
2122 error:
2123         pthread_mutex_unlock(&exynos_camera->recording_mutex);
2124 }
2125
2126 int exynos_camera_recording_start(struct exynos_camera *exynos_camera)
2127 {
2128         int width, height, format;
2129         int fd;
2130
2131         int rc;
2132         int i;
2133
2134         if (exynos_camera == NULL)
2135                 return -EINVAL;
2136
2137         if (exynos_camera->recording_enabled) {
2138                 LOGE("Recording was already started!");
2139                 return 0;
2140         }
2141
2142         pthread_mutex_lock(&exynos_camera->preview_mutex);
2143
2144         // V4L2
2145
2146         format = exynos_camera->recording_format;
2147
2148         rc = exynos_v4l2_enum_fmt_cap(exynos_camera, 2, format);
2149         if (rc < 0) {
2150                 LOGE("%s: enum fmt failed!", __func__);
2151                 goto error;
2152         }
2153
2154         width = exynos_camera->recording_width;
2155         height = exynos_camera->recording_height;
2156
2157         rc = exynos_v4l2_s_fmt_pix_cap(exynos_camera, 2, width, height, format, V4L2_PIX_FMT_MODE_CAPTURE);
2158         if (rc < 0) {
2159                 LOGE("%s: s fmt failed!", __func__);
2160                 goto error;
2161         }
2162
2163         for (i=EXYNOS_CAMERA_MAX_BUFFERS_COUNT ; i >= EXYNOS_CAMERA_MIN_BUFFERS_COUNT ; i--) {
2164                 rc = exynos_v4l2_reqbufs_cap(exynos_camera, 2, i);
2165                 if (rc >= 0)
2166                         break;
2167         }
2168
2169         if (rc < 0) {
2170                 LOGE("%s: reqbufs failed!", __func__);
2171                 goto error;
2172         }
2173
2174         exynos_camera->recording_buffers_count = rc;
2175         LOGD("Found %d recording buffers available!", exynos_camera->recording_buffers_count);
2176
2177         for (i=0 ; i < exynos_camera->recording_buffers_count ; i++) {
2178                 rc = exynos_v4l2_querybuf_cap(exynos_camera, 2, i);
2179                 if (rc < 0) {
2180                         LOGE("%s: querybuf failed!", __func__);
2181                         goto error;
2182                 }
2183         }
2184
2185         if (exynos_camera->callbacks.request_memory != NULL) {
2186                 if (exynos_camera->recording_memory != NULL && exynos_camera->recording_memory->release != NULL)
2187                         exynos_camera->recording_memory->release(exynos_camera->recording_memory);
2188
2189                 exynos_camera->recording_memory =
2190                         exynos_camera->callbacks.request_memory(-1, sizeof(struct exynos_camera_addrs),
2191                                 exynos_camera->recording_buffers_count, 0);
2192                 if (exynos_camera->recording_memory == NULL) {
2193                         LOGE("%s: memory request failed!", __func__);
2194                         goto error;
2195                 }
2196         } else {
2197                 LOGE("%s: No memory request function!", __func__);
2198                 goto error;
2199         }
2200
2201         for (i=0 ; i < exynos_camera->recording_buffers_count ; i++) {
2202                 rc = exynos_v4l2_qbuf_cap(exynos_camera, 2, i);
2203                 if (rc < 0) {
2204                         LOGE("%s: qbuf failed!", __func__);
2205                         goto error;
2206                 }
2207         }
2208
2209         rc = exynos_v4l2_s_ctrl(exynos_camera, 2, V4L2_CID_ROTATION,
2210                 exynos_camera->camera_rotation);
2211         if (rc < 0) {
2212                 LOGE("%s: s ctrl failed!", __func__);
2213                 goto error;
2214         }
2215
2216         rc = exynos_v4l2_s_ctrl(exynos_camera, 2, V4L2_CID_HFLIP,
2217                 exynos_camera->camera_hflip);
2218         if (rc < 0) {
2219                 LOGE("%s: s ctrl failed!", __func__);
2220                 goto error;
2221         }
2222
2223         rc = exynos_v4l2_s_ctrl(exynos_camera, 2, V4L2_CID_VFLIP,
2224                 exynos_camera->camera_vflip);
2225         if (rc < 0) {
2226                 LOGE("%s: s ctrl failed!", __func__);
2227                 goto error;
2228         }
2229
2230         rc = exynos_v4l2_streamon_cap(exynos_camera, 2);
2231         if (rc < 0) {
2232                 LOGE("%s: streamon failed!", __func__);
2233                 goto error;
2234         }
2235
2236         pthread_mutex_init(&exynos_camera->recording_mutex, NULL);
2237
2238         exynos_camera->recording_enabled = 1;
2239
2240         pthread_mutex_unlock(&exynos_camera->preview_mutex);
2241
2242         return 0;
2243 error:
2244         pthread_mutex_unlock(&exynos_camera->preview_mutex);
2245
2246         return -1;
2247 }
2248
2249 void exynos_camera_recording_stop(struct exynos_camera *exynos_camera)
2250 {
2251         int rc;
2252
2253         if (exynos_camera == NULL)
2254                 return;
2255
2256         if (!exynos_camera->recording_enabled) {
2257                 LOGE("Recording was already stopped!");
2258                 return;
2259         }
2260
2261         exynos_camera->recording_enabled = 0;
2262
2263         pthread_mutex_lock(&exynos_camera->preview_mutex);
2264
2265         rc = exynos_v4l2_streamoff_cap(exynos_camera, 2);
2266         if (rc < 0) {
2267                 LOGE("%s: streamoff failed!", __func__);
2268         }
2269
2270         if (exynos_camera->recording_memory != NULL && exynos_camera->recording_memory->release != NULL) {
2271                 exynos_camera->recording_memory->release(exynos_camera->recording_memory);
2272                 exynos_camera->recording_memory = NULL;
2273         }
2274
2275         pthread_mutex_unlock(&exynos_camera->preview_mutex);
2276
2277         pthread_mutex_destroy(&exynos_camera->recording_mutex);
2278 }
2279
2280 /*
2281  * Exynos Camera OPS
2282  */
2283
2284 int exynos_camera_set_preview_window(struct camera_device *dev,
2285         struct preview_stream_ops *w)
2286 {
2287         struct exynos_camera *exynos_camera;
2288
2289         int width, height, format, hal_format;
2290
2291         buffer_handle_t *buffer;
2292         int stride;
2293         void *addr = NULL;
2294
2295         int rc;
2296
2297         LOGD("%s(%p, %p)", __func__, dev, w);
2298
2299         if (dev == NULL || dev->priv == NULL)
2300                 return -EINVAL;
2301
2302         exynos_camera = (struct exynos_camera *) dev->priv;
2303
2304         if (w == NULL)
2305                 return 0;
2306
2307         exynos_camera->preview_window = w;
2308
2309         if (w->set_buffer_count == NULL || w->set_usage == NULL || w->set_buffers_geometry == NULL)
2310                 return -EINVAL;
2311
2312         if (exynos_camera->preview_buffers_count <= 0) {
2313                 LOGE("%s: Invalid preview buffers count", __func__);
2314                 exynos_camera->preview_buffers_count = EXYNOS_CAMERA_MAX_BUFFERS_COUNT;
2315         }
2316
2317         rc = w->set_buffer_count(w, exynos_camera->preview_buffers_count);
2318         if (rc) {
2319                 LOGE("%s: Unable to set buffer count (%d)", __func__,
2320                         exynos_camera->preview_buffers_count);
2321                 return -1;
2322         }
2323
2324         rc = w->set_usage(w, GRALLOC_USAGE_SW_WRITE_OFTEN);
2325         if (rc) {
2326                 LOGE("%s: Unable to set usage", __func__);
2327                 return -1;
2328         }
2329
2330         width = exynos_camera->preview_width;
2331         height = exynos_camera->preview_height;
2332         format = exynos_camera->preview_format;
2333
2334         switch (format) {
2335                 case V4L2_PIX_FMT_NV21:
2336                         hal_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
2337                         break;
2338                 case V4L2_PIX_FMT_YUV420:
2339                         hal_format = HAL_PIXEL_FORMAT_YV12;
2340                         break;
2341                 case V4L2_PIX_FMT_RGB565:
2342                         hal_format = HAL_PIXEL_FORMAT_RGB_565;
2343                         break;
2344                 case V4L2_PIX_FMT_RGB32:
2345                         hal_format = HAL_PIXEL_FORMAT_RGBX_8888;
2346                         break;
2347                 default:
2348                         hal_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
2349                         break;
2350         }
2351
2352         rc = w->set_buffers_geometry(w, width, height, hal_format);
2353         if (rc) {
2354                 LOGE("%s: Unable to set buffers geometry", __func__);
2355                 return -1;
2356         }
2357
2358         // Unlock preview lock
2359         pthread_mutex_unlock(&exynos_camera->preview_lock_mutex);
2360
2361         return 0;
2362 }
2363
2364 void exynos_camera_set_callbacks(struct camera_device *dev,
2365         camera_notify_callback notify_cb,
2366         camera_data_callback data_cb,
2367         camera_data_timestamp_callback data_cb_timestamp,
2368         camera_request_memory get_memory,
2369         void *user)
2370 {
2371         struct exynos_camera *exynos_camera;
2372
2373         LOGD("%s(%p, %p)", __func__, dev, user);
2374
2375         if (dev == NULL || dev->priv == NULL)
2376                 return;
2377
2378         exynos_camera = (struct exynos_camera *) dev->priv;
2379
2380         exynos_camera->callbacks.notify = notify_cb;
2381         exynos_camera->callbacks.data = data_cb;
2382         exynos_camera->callbacks.data_timestamp = data_cb_timestamp;
2383         exynos_camera->callbacks.request_memory = get_memory;
2384         exynos_camera->callbacks.user = user;
2385 }
2386
2387 void exynos_camera_enable_msg_type(struct camera_device *dev, int32_t msg_type)
2388 {
2389         struct exynos_camera *exynos_camera;
2390
2391         LOGD("%s(%p, %d)", __func__, dev, msg_type);
2392
2393         if (dev == NULL || dev->priv == NULL)
2394                 return;
2395
2396         exynos_camera = (struct exynos_camera *) dev->priv;
2397
2398         exynos_camera->messages_enabled |= msg_type;
2399 }
2400
2401 void exynos_camera_disable_msg_type(struct camera_device *dev, int32_t msg_type)
2402 {
2403         struct exynos_camera *exynos_camera;
2404
2405         LOGD("%s(%p, %d)", __func__, dev, msg_type);
2406
2407         if (dev == NULL || dev->priv == NULL)
2408                 return;
2409
2410         exynos_camera = (struct exynos_camera *) dev->priv;
2411
2412         exynos_camera->messages_enabled &= ~msg_type;
2413 }
2414
2415 int exynos_camera_msg_type_enabled(struct camera_device *dev, int32_t msg_type)
2416 {
2417         struct exynos_camera *exynos_camera;
2418
2419         LOGD("%s(%p, %d)", __func__, dev, msg_type);
2420
2421         if (dev == NULL || dev->priv == NULL)
2422                 return -EINVAL;
2423
2424         exynos_camera = (struct exynos_camera *) dev->priv;
2425
2426         return exynos_camera->messages_enabled & msg_type;
2427 }
2428
2429 int exynos_camera_start_preview(struct camera_device *dev)
2430 {
2431         struct exynos_camera *exynos_camera;
2432
2433         LOGD("%s(%p)", __func__, dev);
2434
2435         if (dev == NULL || dev->priv == NULL)
2436                 return -EINVAL;
2437
2438         exynos_camera = (struct exynos_camera *) dev->priv;
2439
2440         return exynos_camera_preview_start(exynos_camera);
2441 }
2442
2443 void exynos_camera_stop_preview(struct camera_device *dev)
2444 {
2445         struct exynos_camera *exynos_camera;
2446
2447         LOGD("%s(%p)", __func__, dev);
2448
2449         if (dev == NULL || dev->priv == NULL)
2450                 return;
2451
2452         exynos_camera = (struct exynos_camera *) dev->priv;
2453
2454         exynos_camera_preview_stop(exynos_camera);
2455 }
2456
2457 int exynos_camera_preview_enabled(struct camera_device *dev)
2458 {
2459         struct exynos_camera *exynos_camera;
2460
2461         LOGD("%s(%p)", __func__, dev);
2462
2463         if (dev == NULL || dev->priv == NULL)
2464                 return -EINVAL;
2465
2466         exynos_camera = (struct exynos_camera *) dev->priv;
2467
2468         return exynos_camera->preview_enabled;
2469 }
2470
2471 int exynos_camera_store_meta_data_in_buffers(struct camera_device *dev,
2472         int enable)
2473 {
2474         LOGD("%s(%p, %d)", __func__, dev, enable);
2475
2476         if (!enable) {
2477                 LOGE("%s: Cannot disable meta-data in buffers!", __func__);
2478                 return -1;
2479         }
2480
2481         return 0;
2482 }
2483
2484 int exynos_camera_start_recording(struct camera_device *dev)
2485 {
2486         struct exynos_camera *exynos_camera;
2487
2488         LOGD("%s(%p)", __func__, dev);
2489
2490         exynos_camera = (struct exynos_camera *) dev->priv;
2491
2492         return exynos_camera_recording_start(exynos_camera);
2493 }
2494
2495 void exynos_camera_stop_recording(struct camera_device *dev)
2496 {
2497         struct exynos_camera *exynos_camera;
2498
2499         LOGD("%s(%p)", __func__, dev);
2500
2501         exynos_camera = (struct exynos_camera *) dev->priv;
2502
2503         exynos_camera_recording_stop(exynos_camera);
2504 }
2505
2506 int exynos_camera_recording_enabled(struct camera_device *dev)
2507 {
2508         struct exynos_camera *exynos_camera;
2509
2510         LOGD("%s(%p)", __func__, dev);
2511
2512         if (dev == NULL || dev->priv == NULL)
2513                 return -EINVAL;
2514
2515         exynos_camera = (struct exynos_camera *) dev->priv;
2516
2517         return exynos_camera->recording_enabled;
2518 }
2519
2520 void exynos_camera_release_recording_frame(struct camera_device *dev,
2521         const void *opaque)
2522 {
2523         struct exynos_camera *exynos_camera;
2524
2525         LOGV("%s(%p, %p)", __func__, dev, opaque);
2526
2527         if (dev == NULL || dev->priv == NULL)
2528                 return;
2529
2530         exynos_camera = (struct exynos_camera *) dev->priv;
2531
2532         exynos_camera_recording_frame_release(exynos_camera, (void *) opaque);
2533 }
2534
2535 int exynos_camera_auto_focus(struct camera_device *dev)
2536 {
2537         struct exynos_camera *exynos_camera;
2538
2539         LOGD("%s(%p)", __func__, dev);
2540
2541         if (dev == NULL || dev->priv == NULL)
2542                 return -EINVAL;
2543
2544         exynos_camera = (struct exynos_camera *) dev->priv;
2545
2546         return exynos_camera_auto_focus_start(exynos_camera);
2547 }
2548
2549 int exynos_camera_cancel_auto_focus(struct camera_device *dev)
2550 {
2551         struct exynos_camera *exynos_camera;
2552
2553         LOGD("%s(%p)", __func__, dev);
2554
2555         if (dev == NULL || dev->priv == NULL)
2556                 return -EINVAL;
2557
2558         exynos_camera = (struct exynos_camera *) dev->priv;
2559
2560         exynos_camera_auto_focus_stop(exynos_camera);
2561
2562         return 0;
2563 }
2564
2565 int exynos_camera_take_picture(struct camera_device *dev)
2566 {
2567         struct exynos_camera *exynos_camera;
2568
2569         LOGD("%s(%p)", __func__, dev);
2570
2571         if (dev == NULL || dev->priv == NULL)
2572                 return -EINVAL;
2573
2574         exynos_camera = (struct exynos_camera *) dev->priv;
2575
2576         return exynos_camera_picture_start(exynos_camera);
2577 }
2578
2579 int exynos_camera_cancel_picture(struct camera_device *dev)
2580 {
2581         struct exynos_camera *exynos_camera;
2582
2583         LOGD("%s(%p)", __func__, dev);
2584
2585         if (dev == NULL || dev->priv == NULL)
2586                 return -EINVAL;
2587
2588         exynos_camera = (struct exynos_camera *) dev->priv;
2589
2590         exynos_camera_picture_stop(exynos_camera);
2591
2592         return 0;
2593 }
2594
2595 int exynos_camera_set_parameters(struct camera_device *dev,
2596         const char *params)
2597 {
2598         struct exynos_camera *exynos_camera;
2599         int rc;
2600
2601         LOGD("%s(%p, %s)", __func__, dev, params);
2602
2603         if (dev == NULL || dev->priv == NULL || params == NULL)
2604                 return -EINVAL;
2605
2606         exynos_camera = (struct exynos_camera *) dev->priv;
2607
2608         rc = exynos_params_string_set(exynos_camera, (char *) params);
2609         if (rc < 0) {
2610                 LOGE("%s: Unable to set params string", __func__);
2611                 return -1;
2612         }
2613
2614         rc = exynos_camera_params_apply(exynos_camera);
2615         if (rc < 0) {
2616                 LOGE("%s: Unable to apply params", __func__);
2617                 return -1;
2618         }
2619
2620         return 0;
2621 }
2622
2623 char *exynos_camera_get_parameters(struct camera_device *dev)
2624 {
2625         struct exynos_camera *exynos_camera;
2626         char *params;
2627
2628         LOGD("%s(%p)", __func__, dev);
2629
2630         if (dev == NULL || dev->priv == NULL)
2631                 return NULL;
2632
2633         exynos_camera = (struct exynos_camera *) dev->priv;
2634
2635         params = exynos_params_string_get(exynos_camera);
2636         if (params == NULL) {
2637                 LOGE("%s: Couldn't find any param", __func__);
2638                 return strdup("");
2639         }
2640
2641         return params;
2642 }
2643
2644 void exynos_camera_put_parameters(struct camera_device *dev, char *params)
2645 {
2646         LOGD("%s(%p)", __func__, dev);
2647
2648         if (params != NULL)
2649                 free(params);
2650 }
2651
2652 int exynos_camera_send_command(struct camera_device *dev,
2653         int32_t cmd, int32_t arg1, int32_t arg2)
2654 {
2655         LOGD("%s(%p, %d, %d, %d)", __func__, dev, cmd, arg1, arg2);
2656
2657         return 0;
2658 }
2659
2660 void exynos_camera_release(struct camera_device *dev)
2661 {
2662         struct exynos_camera *exynos_camera;
2663
2664         LOGD("%s(%p)", __func__, dev);
2665
2666         if (dev == NULL || dev->priv == NULL)
2667                 return;
2668
2669         exynos_camera = (struct exynos_camera *) dev->priv;
2670
2671         if (exynos_camera->preview_memory != NULL && exynos_camera->preview_memory->release != NULL) {
2672                 exynos_camera->preview_memory->release(exynos_camera->preview_memory);
2673                 exynos_camera->preview_memory = NULL;
2674         }
2675
2676         if (exynos_camera->picture_memory != NULL && exynos_camera->picture_memory->release != NULL) {
2677                 exynos_camera->picture_memory->release(exynos_camera->picture_memory);
2678                 exynos_camera->picture_memory = NULL;
2679         }
2680
2681         exynos_camera_deinit(exynos_camera);
2682 }
2683
2684 int exynos_camera_dump(struct camera_device *dev, int fd)
2685 {
2686         LOGD("%s(%p, %d)", __func__, dev, fd);
2687
2688         return 0;
2689 }
2690
2691 /*
2692  * Interface
2693  */
2694
2695 struct camera_device_ops exynos_camera_ops = {
2696         .set_preview_window = exynos_camera_set_preview_window,
2697         .set_callbacks = exynos_camera_set_callbacks,
2698         .enable_msg_type = exynos_camera_enable_msg_type,
2699         .disable_msg_type = exynos_camera_disable_msg_type,
2700         .msg_type_enabled = exynos_camera_msg_type_enabled,
2701         .start_preview = exynos_camera_start_preview,
2702         .stop_preview = exynos_camera_stop_preview,
2703         .preview_enabled = exynos_camera_preview_enabled,
2704         .store_meta_data_in_buffers = exynos_camera_store_meta_data_in_buffers,
2705         .start_recording = exynos_camera_start_recording,
2706         .stop_recording = exynos_camera_stop_recording,
2707         .recording_enabled = exynos_camera_recording_enabled,
2708         .release_recording_frame = exynos_camera_release_recording_frame,
2709         .auto_focus = exynos_camera_auto_focus,
2710         .cancel_auto_focus = exynos_camera_cancel_auto_focus,
2711         .take_picture = exynos_camera_take_picture,
2712         .cancel_picture = exynos_camera_cancel_picture,
2713         .set_parameters = exynos_camera_set_parameters,
2714         .get_parameters = exynos_camera_get_parameters,
2715         .put_parameters = exynos_camera_put_parameters,
2716         .send_command = exynos_camera_send_command,
2717         .release = exynos_camera_release,
2718         .dump = exynos_camera_dump,
2719 };
2720
2721 int exynos_camera_close(hw_device_t *device)
2722 {
2723         struct camera_device *camera_device;
2724         struct exynos_camera *exynos_camera;
2725
2726         LOGD("%s(%p)", __func__, device);
2727
2728         if (device == NULL)
2729                 return -EINVAL;
2730
2731         camera_device = (struct camera_device *) device;
2732
2733         if (camera_device->priv != NULL) {
2734                 free(camera_device->priv);
2735         }
2736
2737         free(camera_device);
2738
2739         return 0;
2740 }
2741
2742 int exynos_camera_open(const struct hw_module_t* module, const char *camera_id,
2743         struct hw_device_t** device)
2744 {
2745         struct camera_device *camera_device = NULL;
2746         struct exynos_camera *exynos_camera = NULL;
2747         int id;
2748         int rc;
2749
2750         LOGD("%s(%p, %s, %p)", __func__, module, camera_id, device);
2751
2752         if (module == NULL || camera_id == NULL || device == NULL)
2753                 return -EINVAL;
2754
2755         id = atoi(camera_id);
2756         if (id < 0)
2757                 return -EINVAL;
2758
2759         exynos_camera = calloc(1, sizeof(struct exynos_camera));
2760         exynos_camera->config = exynos_camera_config;
2761
2762         if (exynos_camera->config->presets_count > EXYNOS_CAMERA_MAX_PRESETS_COUNT ||
2763                 exynos_camera->config->v4l2_nodes_count > EXYNOS_CAMERA_MAX_V4L2_NODES_COUNT)
2764                 goto error_preset;
2765
2766         if (id >= exynos_camera->config->presets_count)
2767                 goto error_preset;
2768
2769         rc = exynos_camera_init(exynos_camera, id);
2770         if (rc < 0) {
2771                 LOGE("%s: Unable to init camera", __func__);
2772                 goto error;
2773         }
2774
2775         camera_device = calloc(1, sizeof(struct camera_device));
2776         camera_device->common.tag = HARDWARE_DEVICE_TAG;
2777         camera_device->common.version = 0;
2778         camera_device->common.module = (struct hw_module_t *) module;
2779         camera_device->common.close = exynos_camera_close;
2780
2781         camera_device->ops = &exynos_camera_ops;
2782         camera_device->priv = exynos_camera;
2783
2784         *device = (struct hw_device_t *) &(camera_device->common);
2785
2786         return 0;
2787
2788 error:
2789         exynos_camera_deinit(exynos_camera);
2790
2791 error_device:
2792         if (camera_device != NULL)
2793                 free(camera_device);
2794
2795 error_preset:
2796         if (exynos_camera != NULL)
2797                 free(exynos_camera);
2798
2799         return -1;
2800 }
2801
2802 int exynos_camera_get_number_of_cameras(void)
2803 {
2804         LOGD("%s()", __func__);
2805
2806         if (exynos_camera_config == NULL || exynos_camera_config->presets == NULL) {
2807                 LOGE("%s: Unable to find proper camera config", __func__);
2808                 return -1;
2809         }
2810
2811         return exynos_camera_config->presets_count;
2812 }
2813
2814 int exynos_camera_get_camera_info(int id, struct camera_info *info)
2815 {
2816         LOGD("%s(%d, %p)", __func__, id, info);
2817
2818         if (id < 0 || info == NULL)
2819                 return -EINVAL;
2820
2821         if (exynos_camera_config == NULL || exynos_camera_config->presets == NULL) {
2822                 LOGE("%s: Unable to find proper camera config", __func__);
2823                 return -1;
2824         }
2825
2826         if (id >= exynos_camera_config->presets_count)
2827                 return -EINVAL;
2828
2829         LOGD("Selected camera: %s", exynos_camera_config->presets[id].name);
2830
2831         info->facing = exynos_camera_config->presets[id].facing;
2832         info->orientation = exynos_camera_config->presets[id].orientation;
2833
2834         return 0;
2835 }
2836
2837 struct hw_module_methods_t exynos_camera_module_methods = {
2838         .open = exynos_camera_open,
2839 };
2840
2841 struct camera_module HAL_MODULE_INFO_SYM = {
2842         .common = {
2843                 .tag = HARDWARE_MODULE_TAG,
2844                 .version_major = 1,
2845                 .version_minor = 0,
2846                 .id = CAMERA_HARDWARE_MODULE_ID,
2847                 .name = "Exynos Camera",
2848                 .author = "Paul Kocialkowski",
2849                 .methods = &exynos_camera_module_methods,
2850         },
2851         .get_number_of_cameras = exynos_camera_get_number_of_cameras,
2852         .get_camera_info = exynos_camera_get_camera_info,
2853 };