blob: a9277a220be4910a98ebfe2ac5eeb781cb9b4fe1 [file] [log] [blame]
Iliyan Malchevc3229892011-08-08 11:24:41 -07001/*
2 * Copyright (C) Texas Instruments - http://www.ti.com/
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18* @file CameraHal.cpp
19*
20* This file maps the Camera Hardware Interface to V4L2.
21*
22*/
23
Tyler Luu9a412952011-10-05 23:32:09 -050024#include <utils/threads.h>
25
Iliyan Malchevc3229892011-08-08 11:24:41 -070026#include "CameraHal.h"
27#include "CameraProperties.h"
28#include "TICameraParameters.h"
29
30
Jason Simmonsf7a4d112012-10-17 15:06:16 -070031#ifdef CAMERAHAL_DEBUG_VERBOSE
32# define CAMHAL_LOG_MODULE_FUNCTION_NAME LOG_FUNCTION_NAME
33#else
34# define CAMHAL_LOG_MODULE_FUNCTION_NAME
35#endif
36
37
38namespace Ti {
39namespace Camera {
40
41static CameraProperties gCameraProperties;
42static CameraHal* gCameraHals[MAX_CAMERAS_SUPPORTED];
Tyler Luu9a412952011-10-05 23:32:09 -050043static unsigned int gCamerasOpen = 0;
44static android::Mutex gCameraHalDeviceLock;
Iliyan Malchevc3229892011-08-08 11:24:41 -070045
46static int camera_device_open(const hw_module_t* module, const char* name,
47 hw_device_t** device);
48static int camera_device_close(hw_device_t* device);
49static int camera_get_number_of_cameras(void);
50static int camera_get_camera_info(int camera_id, struct camera_info *info);
51
52static struct hw_module_methods_t camera_module_methods = {
53 open: camera_device_open
54};
55
Jason Simmonsf7a4d112012-10-17 15:06:16 -070056} // namespace Camera
57} // namespace Ti
58
59
Iliyan Malchevc3229892011-08-08 11:24:41 -070060camera_module_t HAL_MODULE_INFO_SYM = {
61 common: {
62 tag: HARDWARE_MODULE_TAG,
63 version_major: 1,
64 version_minor: 0,
65 id: CAMERA_HARDWARE_MODULE_ID,
66 name: "TI OMAP CameraHal Module",
67 author: "TI",
Jason Simmonsf7a4d112012-10-17 15:06:16 -070068 methods: &Ti::Camera::camera_module_methods,
Iliyan Malchevc3229892011-08-08 11:24:41 -070069 dso: NULL, /* remove compilation warnings */
70 reserved: {0}, /* remove compilation warnings */
71 },
Jason Simmonsf7a4d112012-10-17 15:06:16 -070072 get_number_of_cameras: Ti::Camera::camera_get_number_of_cameras,
73 get_camera_info: Ti::Camera::camera_get_camera_info,
Iliyan Malchevc3229892011-08-08 11:24:41 -070074};
75
Jason Simmonsf7a4d112012-10-17 15:06:16 -070076
77namespace Ti {
78namespace Camera {
79
Iliyan Malchevc3229892011-08-08 11:24:41 -070080typedef struct ti_camera_device {
81 camera_device_t base;
82 /* TI specific "private" data can go here (base.priv) */
83 int cameraid;
84} ti_camera_device_t;
85
86
87/*******************************************************************
88 * implementation of camera_device_ops functions
89 *******************************************************************/
90
91int camera_set_preview_window(struct camera_device * device,
92 struct preview_stream_ops *window)
93{
Jason Simmonsf7a4d112012-10-17 15:06:16 -070094 CAMHAL_LOG_MODULE_FUNCTION_NAME;
95
Iliyan Malchevc3229892011-08-08 11:24:41 -070096 int rv = -EINVAL;
97 ti_camera_device_t* ti_dev = NULL;
98
Iliyan Malchevc3229892011-08-08 11:24:41 -070099 if(!device)
100 return rv;
101
102 ti_dev = (ti_camera_device_t*) device;
103
104 rv = gCameraHals[ti_dev->cameraid]->setPreviewWindow(window);
105
106 return rv;
107}
108
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700109#ifdef OMAP_ENHANCEMENT_CPCAM
110int camera_set_extended_preview_ops(struct camera_device * device,
111 preview_stream_extended_ops_t * extendedOps)
112{
113 CAMHAL_LOG_MODULE_FUNCTION_NAME;
114
115 if (!device) {
116 return BAD_VALUE;
117 }
118
119 ti_camera_device_t * const tiDevice = reinterpret_cast<ti_camera_device_t*>(device);
120 gCameraHals[tiDevice->cameraid]->setExtendedPreviewStreamOps(extendedOps);
121
122 return OK;
123}
124
125int camera_set_buffer_source(struct camera_device * device,
126 struct preview_stream_ops *tapin,
127 struct preview_stream_ops *tapout)
128{
129 CAMHAL_LOG_MODULE_FUNCTION_NAME;
130
131 int rv = -EINVAL;
132 ti_camera_device_t* ti_dev = NULL;
133
134 if(!device)
135 return rv;
136
137 ti_dev = (ti_camera_device_t*) device;
138
139 rv = gCameraHals[ti_dev->cameraid]->setBufferSource(tapin, tapout);
140
141 return rv;
142}
143
144int camera_release_buffer_source(struct camera_device * device,
145 struct preview_stream_ops *tapin,
146 struct preview_stream_ops *tapout)
147{
148 CAMHAL_LOG_MODULE_FUNCTION_NAME;
149
150 int rv = -EINVAL;
151 ti_camera_device_t* ti_dev = NULL;
152
153 if(!device)
154 return rv;
155
156 ti_dev = (ti_camera_device_t*) device;
157
158 rv = gCameraHals[ti_dev->cameraid]->releaseBufferSource(tapin, tapout);
159
160 return rv;
161}
162#endif
163
Iliyan Malchevc3229892011-08-08 11:24:41 -0700164void camera_set_callbacks(struct camera_device * device,
165 camera_notify_callback notify_cb,
166 camera_data_callback data_cb,
167 camera_data_timestamp_callback data_cb_timestamp,
168 camera_request_memory get_memory,
169 void *user)
170{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700171 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700172
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700173 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700174
175 if(!device)
176 return;
177
178 ti_dev = (ti_camera_device_t*) device;
179
180 gCameraHals[ti_dev->cameraid]->setCallbacks(notify_cb, data_cb, data_cb_timestamp, get_memory, user);
181}
182
183void camera_enable_msg_type(struct camera_device * device, int32_t msg_type)
184{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700185 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700186
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700187 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700188
189 if(!device)
190 return;
191
192 ti_dev = (ti_camera_device_t*) device;
193
194 gCameraHals[ti_dev->cameraid]->enableMsgType(msg_type);
195}
196
197void camera_disable_msg_type(struct camera_device * device, int32_t msg_type)
198{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700199 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700200
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700201 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700202
203 if(!device)
204 return;
205
206 ti_dev = (ti_camera_device_t*) device;
207
208 gCameraHals[ti_dev->cameraid]->disableMsgType(msg_type);
209}
210
211int camera_msg_type_enabled(struct camera_device * device, int32_t msg_type)
212{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700213 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700214
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700215 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700216
217 if(!device)
218 return 0;
219
220 ti_dev = (ti_camera_device_t*) device;
221
222 return gCameraHals[ti_dev->cameraid]->msgTypeEnabled(msg_type);
223}
224
225int camera_start_preview(struct camera_device * device)
226{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700227 CAMHAL_LOG_MODULE_FUNCTION_NAME;
228
Iliyan Malchevc3229892011-08-08 11:24:41 -0700229 int rv = -EINVAL;
230 ti_camera_device_t* ti_dev = NULL;
231
Iliyan Malchevc3229892011-08-08 11:24:41 -0700232 if(!device)
233 return rv;
234
235 ti_dev = (ti_camera_device_t*) device;
236
237 rv = gCameraHals[ti_dev->cameraid]->startPreview();
238
239 return rv;
240}
241
242void camera_stop_preview(struct camera_device * device)
243{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700244 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700245
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700246 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700247
248 if(!device)
249 return;
250
251 ti_dev = (ti_camera_device_t*) device;
252
253 gCameraHals[ti_dev->cameraid]->stopPreview();
254}
255
256int camera_preview_enabled(struct camera_device * device)
257{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700258 CAMHAL_LOG_MODULE_FUNCTION_NAME;
259
Iliyan Malchevc3229892011-08-08 11:24:41 -0700260 int rv = -EINVAL;
261 ti_camera_device_t* ti_dev = NULL;
262
Iliyan Malchevc3229892011-08-08 11:24:41 -0700263 if(!device)
264 return rv;
265
266 ti_dev = (ti_camera_device_t*) device;
267
268 rv = gCameraHals[ti_dev->cameraid]->previewEnabled();
269 return rv;
270}
271
272int camera_store_meta_data_in_buffers(struct camera_device * device, int enable)
273{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700274 CAMHAL_LOG_MODULE_FUNCTION_NAME;
275
Iliyan Malchevc3229892011-08-08 11:24:41 -0700276 int rv = -EINVAL;
277 ti_camera_device_t* ti_dev = NULL;
278
Iliyan Malchevc3229892011-08-08 11:24:41 -0700279 if(!device)
280 return rv;
281
282 ti_dev = (ti_camera_device_t*) device;
283
284 // TODO: meta data buffer not current supported
285 rv = gCameraHals[ti_dev->cameraid]->storeMetaDataInBuffers(enable);
286 return rv;
287 //return enable ? android::INVALID_OPERATION: android::OK;
288}
289
290int camera_start_recording(struct camera_device * device)
291{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700292 CAMHAL_LOG_MODULE_FUNCTION_NAME;
293
Iliyan Malchevc3229892011-08-08 11:24:41 -0700294 int rv = -EINVAL;
295 ti_camera_device_t* ti_dev = NULL;
296
Iliyan Malchevc3229892011-08-08 11:24:41 -0700297 if(!device)
298 return rv;
299
300 ti_dev = (ti_camera_device_t*) device;
301
302 rv = gCameraHals[ti_dev->cameraid]->startRecording();
303 return rv;
304}
305
306void camera_stop_recording(struct camera_device * device)
307{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700308 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700309
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700310 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700311
312 if(!device)
313 return;
314
315 ti_dev = (ti_camera_device_t*) device;
316
317 gCameraHals[ti_dev->cameraid]->stopRecording();
318}
319
320int camera_recording_enabled(struct camera_device * device)
321{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700322 CAMHAL_LOG_MODULE_FUNCTION_NAME;
323
Iliyan Malchevc3229892011-08-08 11:24:41 -0700324 int rv = -EINVAL;
325 ti_camera_device_t* ti_dev = NULL;
326
Iliyan Malchevc3229892011-08-08 11:24:41 -0700327 if(!device)
328 return rv;
329
330 ti_dev = (ti_camera_device_t*) device;
331
332 rv = gCameraHals[ti_dev->cameraid]->recordingEnabled();
333 return rv;
334}
335
336void camera_release_recording_frame(struct camera_device * device,
337 const void *opaque)
338{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700339 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700340
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700341 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700342
343 if(!device)
344 return;
345
346 ti_dev = (ti_camera_device_t*) device;
347
348 gCameraHals[ti_dev->cameraid]->releaseRecordingFrame(opaque);
349}
350
351int camera_auto_focus(struct camera_device * device)
352{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700353 CAMHAL_LOG_MODULE_FUNCTION_NAME;
354
Iliyan Malchevc3229892011-08-08 11:24:41 -0700355 int rv = -EINVAL;
356 ti_camera_device_t* ti_dev = NULL;
357
Iliyan Malchevc3229892011-08-08 11:24:41 -0700358 if(!device)
359 return rv;
360
361 ti_dev = (ti_camera_device_t*) device;
362
363 rv = gCameraHals[ti_dev->cameraid]->autoFocus();
364 return rv;
365}
366
367int camera_cancel_auto_focus(struct camera_device * device)
368{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700369 CAMHAL_LOG_MODULE_FUNCTION_NAME;
370
Iliyan Malchevc3229892011-08-08 11:24:41 -0700371 int rv = -EINVAL;
372 ti_camera_device_t* ti_dev = NULL;
373
Iliyan Malchevc3229892011-08-08 11:24:41 -0700374 if(!device)
375 return rv;
376
377 ti_dev = (ti_camera_device_t*) device;
378
379 rv = gCameraHals[ti_dev->cameraid]->cancelAutoFocus();
380 return rv;
381}
382
383int camera_take_picture(struct camera_device * device)
384{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700385 CAMHAL_LOG_MODULE_FUNCTION_NAME;
386
Iliyan Malchevc3229892011-08-08 11:24:41 -0700387 int rv = -EINVAL;
388 ti_camera_device_t* ti_dev = NULL;
389
Iliyan Malchevc3229892011-08-08 11:24:41 -0700390 if(!device)
391 return rv;
392
393 ti_dev = (ti_camera_device_t*) device;
394
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700395 rv = gCameraHals[ti_dev->cameraid]->takePicture(0);
Iliyan Malchevc3229892011-08-08 11:24:41 -0700396 return rv;
397}
398
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700399#ifdef OMAP_ENHANCEMENT_CPCAM
400int camera_take_picture_with_parameters(struct camera_device * device, const char *params)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700401{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700402 CAMHAL_LOG_MODULE_FUNCTION_NAME;
403
Iliyan Malchevc3229892011-08-08 11:24:41 -0700404 int rv = -EINVAL;
405 ti_camera_device_t* ti_dev = NULL;
406
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700407 if(!device)
408 return rv;
409
410 ti_dev = (ti_camera_device_t*) device;
411
412 rv = gCameraHals[ti_dev->cameraid]->takePicture(params);
413 return rv;
414}
415#endif
416
417int camera_cancel_picture(struct camera_device * device)
418{
419 CAMHAL_LOG_MODULE_FUNCTION_NAME;
420
421 int rv = -EINVAL;
422 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700423
424 if(!device)
425 return rv;
426
427 ti_dev = (ti_camera_device_t*) device;
428
429 rv = gCameraHals[ti_dev->cameraid]->cancelPicture();
430 return rv;
431}
432
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700433#ifdef OMAP_ENHANCEMENT_CPCAM
434int camera_reprocess(struct camera_device * device, const char *params)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700435{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700436 CAMHAL_LOG_MODULE_FUNCTION_NAME;
437
Iliyan Malchevc3229892011-08-08 11:24:41 -0700438 int rv = -EINVAL;
439 ti_camera_device_t* ti_dev = NULL;
440
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700441 if(!device)
442 return rv;
443
444 ti_dev = (ti_camera_device_t*) device;
445
446 rv = gCameraHals[ti_dev->cameraid]->reprocess(params);
447 return rv;
448}
449
450int camera_cancel_reprocess(struct camera_device * device)
451{
452 CAMHAL_LOG_MODULE_FUNCTION_NAME;
453
454 int rv = -EINVAL;
455 ti_camera_device_t* ti_dev = NULL;
456
457 if(!device)
458 return rv;
459
460 ti_dev = (ti_camera_device_t*) device;
461
462 rv = gCameraHals[ti_dev->cameraid]->cancel_reprocess();
463 return rv;
464}
465#endif
466
467int camera_set_parameters(struct camera_device * device, const char *params)
468{
469 CAMHAL_LOG_MODULE_FUNCTION_NAME;
470
471 int rv = -EINVAL;
472 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700473
474 if(!device)
475 return rv;
476
477 ti_dev = (ti_camera_device_t*) device;
478
479 rv = gCameraHals[ti_dev->cameraid]->setParameters(params);
480 return rv;
481}
482
483char* camera_get_parameters(struct camera_device * device)
484{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700485 CAMHAL_LOG_MODULE_FUNCTION_NAME;
486
Iliyan Malchevc3229892011-08-08 11:24:41 -0700487 char* param = NULL;
488 ti_camera_device_t* ti_dev = NULL;
489
Iliyan Malchevc3229892011-08-08 11:24:41 -0700490 if(!device)
491 return NULL;
492
493 ti_dev = (ti_camera_device_t*) device;
494
495 param = gCameraHals[ti_dev->cameraid]->getParameters();
496
497 return param;
498}
499
500static void camera_put_parameters(struct camera_device *device, char *parms)
501{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700502 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700503
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700504 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700505
506 if(!device)
507 return;
508
509 ti_dev = (ti_camera_device_t*) device;
510
511 gCameraHals[ti_dev->cameraid]->putParameters(parms);
512}
513
514int camera_send_command(struct camera_device * device,
515 int32_t cmd, int32_t arg1, int32_t arg2)
516{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700517 CAMHAL_LOG_MODULE_FUNCTION_NAME;
518
Iliyan Malchevc3229892011-08-08 11:24:41 -0700519 int rv = -EINVAL;
520 ti_camera_device_t* ti_dev = NULL;
521
Iliyan Malchevc3229892011-08-08 11:24:41 -0700522 if(!device)
523 return rv;
524
525 ti_dev = (ti_camera_device_t*) device;
526
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700527#ifdef OMAP_ENHANCEMENT
528 if ( cmd == CAMERA_CMD_SETUP_EXTENDED_OPERATIONS ) {
529 camera_device_extended_ops_t * const ops = static_cast<camera_device_extended_ops_t*>(
530 camera_cmd_send_command_args_to_pointer(arg1, arg2));
531
532#ifdef OMAP_ENHANCEMENT_CPCAM
533 ops->set_extended_preview_ops = camera_set_extended_preview_ops;
534 ops->set_buffer_source = camera_set_buffer_source;
535 ops->release_buffer_source = camera_release_buffer_source;
536 ops->take_picture_with_parameters = camera_take_picture_with_parameters;
537 ops->reprocess = camera_reprocess;
538 ops->cancel_reprocess = camera_cancel_reprocess;
539#endif
540
541 return OK;
542 }
543#endif
544
Iliyan Malchevc3229892011-08-08 11:24:41 -0700545 rv = gCameraHals[ti_dev->cameraid]->sendCommand(cmd, arg1, arg2);
546 return rv;
547}
548
549void camera_release(struct camera_device * device)
550{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700551 CAMHAL_LOG_MODULE_FUNCTION_NAME;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700552
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700553 ti_camera_device_t* ti_dev = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700554
555 if(!device)
556 return;
557
558 ti_dev = (ti_camera_device_t*) device;
559
560 gCameraHals[ti_dev->cameraid]->release();
561}
562
563int camera_dump(struct camera_device * device, int fd)
564{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700565 CAMHAL_LOG_MODULE_FUNCTION_NAME;
566
Iliyan Malchevc3229892011-08-08 11:24:41 -0700567 int rv = -EINVAL;
568 ti_camera_device_t* ti_dev = NULL;
569
570 if(!device)
571 return rv;
572
573 ti_dev = (ti_camera_device_t*) device;
574
575 rv = gCameraHals[ti_dev->cameraid]->dump(fd);
576 return rv;
577}
578
579extern "C" void heaptracker_free_leaked_memory(void);
580
581int camera_device_close(hw_device_t* device)
582{
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700583 CAMHAL_LOG_MODULE_FUNCTION_NAME;
584
Iliyan Malchevc3229892011-08-08 11:24:41 -0700585 int ret = 0;
586 ti_camera_device_t* ti_dev = NULL;
587
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700588 android::AutoMutex lock(gCameraHalDeviceLock);
Tyler Luu9a412952011-10-05 23:32:09 -0500589
Iliyan Malchevc3229892011-08-08 11:24:41 -0700590 if (!device) {
591 ret = -EINVAL;
592 goto done;
593 }
594
595 ti_dev = (ti_camera_device_t*) device;
596
Tyler Luubf3ea792011-10-20 16:58:58 -0500597 if (ti_dev) {
598 if (gCameraHals[ti_dev->cameraid]) {
599 delete gCameraHals[ti_dev->cameraid];
600 gCameraHals[ti_dev->cameraid] = NULL;
601 gCamerasOpen--;
602 }
Iliyan Malchevc3229892011-08-08 11:24:41 -0700603
Tyler Luubf3ea792011-10-20 16:58:58 -0500604 if (ti_dev->base.ops) {
605 free(ti_dev->base.ops);
606 }
607 free(ti_dev);
Iliyan Malchevc3229892011-08-08 11:24:41 -0700608 }
Iliyan Malchevc3229892011-08-08 11:24:41 -0700609done:
610#ifdef HEAPTRACKER
611 heaptracker_free_leaked_memory();
612#endif
613 return ret;
614}
615
616/*******************************************************************
617 * implementation of camera_module functions
618 *******************************************************************/
619
620/* open device handle to one of the cameras
621 *
622 * assume camera service will keep singleton of each camera
623 * so this function will always only be called once per camera instance
624 */
625
626int camera_device_open(const hw_module_t* module, const char* name,
627 hw_device_t** device)
628{
Sundar Raman73bfc812011-08-29 16:30:34 -0700629 int rv = 0;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700630 int num_cameras = 0;
631 int cameraid;
632 ti_camera_device_t* camera_device = NULL;
633 camera_device_ops_t* camera_ops = NULL;
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700634 CameraHal* camera = NULL;
635 CameraProperties::Properties* properties = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700636
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700637 android::AutoMutex lock(gCameraHalDeviceLock);
Tyler Luu9a412952011-10-05 23:32:09 -0500638
Iliyan Malcheve4639442011-11-17 11:39:33 -0800639 CAMHAL_LOGI("camera_device open");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700640
641 if (name != NULL) {
642 cameraid = atoi(name);
643 num_cameras = gCameraProperties.camerasSupported();
644
645 if(cameraid > num_cameras)
646 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700647 CAMHAL_LOGE("camera service provided cameraid out of bounds, "
Iliyan Malchevc3229892011-08-08 11:24:41 -0700648 "cameraid = %d, num supported = %d",
649 cameraid, num_cameras);
Tyler Luu9a412952011-10-05 23:32:09 -0500650 rv = -EINVAL;
651 goto fail;
652 }
653
654 if(gCamerasOpen >= MAX_SIMUL_CAMERAS_SUPPORTED)
655 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700656 CAMHAL_LOGE("maximum number of cameras already open");
Tyler Luu9a412952011-10-05 23:32:09 -0500657 rv = -ENOMEM;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700658 goto fail;
659 }
660
661 camera_device = (ti_camera_device_t*)malloc(sizeof(*camera_device));
662 if(!camera_device)
663 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700664 CAMHAL_LOGE("camera_device allocation fail");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700665 rv = -ENOMEM;
666 goto fail;
667 }
668
669 camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
670 if(!camera_ops)
671 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700672 CAMHAL_LOGE("camera_ops allocation fail");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700673 rv = -ENOMEM;
674 goto fail;
675 }
676
677 memset(camera_device, 0, sizeof(*camera_device));
678 memset(camera_ops, 0, sizeof(*camera_ops));
679
680 camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
681 camera_device->base.common.version = 0;
682 camera_device->base.common.module = (hw_module_t *)(module);
683 camera_device->base.common.close = camera_device_close;
684 camera_device->base.ops = camera_ops;
685
686 camera_ops->set_preview_window = camera_set_preview_window;
687 camera_ops->set_callbacks = camera_set_callbacks;
688 camera_ops->enable_msg_type = camera_enable_msg_type;
689 camera_ops->disable_msg_type = camera_disable_msg_type;
690 camera_ops->msg_type_enabled = camera_msg_type_enabled;
691 camera_ops->start_preview = camera_start_preview;
692 camera_ops->stop_preview = camera_stop_preview;
693 camera_ops->preview_enabled = camera_preview_enabled;
694 camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;
695 camera_ops->start_recording = camera_start_recording;
696 camera_ops->stop_recording = camera_stop_recording;
697 camera_ops->recording_enabled = camera_recording_enabled;
698 camera_ops->release_recording_frame = camera_release_recording_frame;
699 camera_ops->auto_focus = camera_auto_focus;
700 camera_ops->cancel_auto_focus = camera_cancel_auto_focus;
701 camera_ops->take_picture = camera_take_picture;
702 camera_ops->cancel_picture = camera_cancel_picture;
703 camera_ops->set_parameters = camera_set_parameters;
704 camera_ops->get_parameters = camera_get_parameters;
705 camera_ops->put_parameters = camera_put_parameters;
706 camera_ops->send_command = camera_send_command;
707 camera_ops->release = camera_release;
708 camera_ops->dump = camera_dump;
709
710 *device = &camera_device->base.common;
711
712 // -------- TI specific stuff --------
713
714 camera_device->cameraid = cameraid;
715
716 if(gCameraProperties.getProperties(cameraid, &properties) < 0)
717 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700718 CAMHAL_LOGE("Couldn't get camera properties");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700719 rv = -ENOMEM;
720 goto fail;
721 }
722
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700723 camera = new CameraHal(cameraid);
Iliyan Malchevc3229892011-08-08 11:24:41 -0700724
725 if(!camera)
726 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700727 CAMHAL_LOGE("Couldn't create instance of CameraHal class");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700728 rv = -ENOMEM;
729 goto fail;
730 }
731
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700732 if(properties && (camera->initialize(properties) != NO_ERROR))
Iliyan Malchevc3229892011-08-08 11:24:41 -0700733 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700734 CAMHAL_LOGE("Couldn't initialize camera instance");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700735 rv = -ENODEV;
736 goto fail;
737 }
738
739 gCameraHals[cameraid] = camera;
Tyler Luu9a412952011-10-05 23:32:09 -0500740 gCamerasOpen++;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700741 }
742
743 return rv;
744
745fail:
Tyler Luubf3ea792011-10-20 16:58:58 -0500746 if(camera_device) {
Iliyan Malchevc3229892011-08-08 11:24:41 -0700747 free(camera_device);
Tyler Luubf3ea792011-10-20 16:58:58 -0500748 camera_device = NULL;
749 }
750 if(camera_ops) {
Iliyan Malchevc3229892011-08-08 11:24:41 -0700751 free(camera_ops);
Tyler Luubf3ea792011-10-20 16:58:58 -0500752 camera_ops = NULL;
753 }
754 if(camera) {
Iliyan Malchevc3229892011-08-08 11:24:41 -0700755 delete camera;
Tyler Luubf3ea792011-10-20 16:58:58 -0500756 camera = NULL;
757 }
758 *device = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700759 return rv;
760}
761
762int camera_get_number_of_cameras(void)
763{
764 int num_cameras = MAX_CAMERAS_SUPPORTED;
765
Iliyan Malchevc3229892011-08-08 11:24:41 -0700766 // this going to be the first call from camera service
767 // initialize camera properties here...
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700768 if(gCameraProperties.initialize() != NO_ERROR)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700769 {
770 CAMHAL_LOGEA("Unable to create or initialize CameraProperties");
771 return NULL;
772 }
773
774 num_cameras = gCameraProperties.camerasSupported();
Iliyan Malchevc3229892011-08-08 11:24:41 -0700775
776 return num_cameras;
777}
778
779int camera_get_camera_info(int camera_id, struct camera_info *info)
780{
781 int rv = 0;
782 int face_value = CAMERA_FACING_BACK;
783 int orientation = 0;
784 const char *valstr = NULL;
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700785 CameraProperties::Properties* properties = NULL;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700786
787 // this going to be the first call from camera service
788 // initialize camera properties here...
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700789 if(gCameraProperties.initialize() != NO_ERROR)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700790 {
791 CAMHAL_LOGEA("Unable to create or initialize CameraProperties");
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700792 rv = -EINVAL;
793 goto end;
Iliyan Malchevc3229892011-08-08 11:24:41 -0700794 }
795
796 //Get camera properties for camera index
797 if(gCameraProperties.getProperties(camera_id, &properties) < 0)
798 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700799 CAMHAL_LOGE("Couldn't get camera properties");
Iliyan Malchevc3229892011-08-08 11:24:41 -0700800 rv = -EINVAL;
801 goto end;
802 }
803
804 if(properties)
805 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700806 valstr = properties->get(CameraProperties::FACING_INDEX);
Iliyan Malchevc3229892011-08-08 11:24:41 -0700807 if(valstr != NULL)
808 {
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700809 if (strcmp(valstr, TICameraParameters::FACING_FRONT) == 0)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700810 {
811 face_value = CAMERA_FACING_FRONT;
812 }
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700813 else if (strcmp(valstr, TICameraParameters::FACING_BACK) == 0)
Iliyan Malchevc3229892011-08-08 11:24:41 -0700814 {
815 face_value = CAMERA_FACING_BACK;
816 }
817 }
818
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700819 valstr = properties->get(CameraProperties::ORIENTATION_INDEX);
Iliyan Malchevc3229892011-08-08 11:24:41 -0700820 if(valstr != NULL)
821 {
822 orientation = atoi(valstr);
823 }
824 }
825 else
826 {
827 CAMHAL_LOGEB("getProperties() returned a NULL property set for Camera id %d", camera_id);
828 }
829
830 info->facing = face_value;
831 info->orientation = orientation;
832
833end:
834 return rv;
835}
836
837
Jason Simmonsf7a4d112012-10-17 15:06:16 -0700838} // namespace Camera
839} // namespace Ti