1 /* 2 ** Copyright (c) 2011 The Linux Foundation. All rights reserved. 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 /*#error uncomment this for compiler test!*/ 18 19 //#define ALOG_NDEBUG 0 20 #define ALOG_NIDEBUG 0 21 #define LOG_TAG "QualcommCamera" 22 #include <utils/Log.h> 23 #include <utils/threads.h> 24 #include <fcntl.h> 25 #include <sys/mman.h> 26 27 /* include QCamera Hardware Interface Header*/ 28 #include "QualcommCamera.h" 29 #include "QualcommCameraHardware.h" 30 //#include <camera/CameraHardwareInterface.h> 31 32 extern "C" { 33 #include <sys/time.h> 34 } 35 36 /* HAL function implementation goes here*/ 37 38 /** 39 * The functions need to be provided by the camera HAL. 40 * 41 * If getNumberOfCameras() returns N, the valid cameraId for getCameraInfo() 42 * and openCameraHardware() is 0 to N-1. 43 */ 44 45 static hw_module_methods_t camera_module_methods = { 46 open: camera_device_open, 47 }; 48 49 50 static hw_module_t camera_common = { 51 tag: HARDWARE_MODULE_TAG, 52 version_major: 0, 53 version_minor: 01, 54 id: CAMERA_HARDWARE_MODULE_ID, 55 name: "Qcamera", 56 author:"Qcom", 57 methods: &camera_module_methods, 58 dso: NULL, 59 //reserved[0]: 0, 60 }; 61 62 camera_module_t HAL_MODULE_INFO_SYM = { 63 common: camera_common, 64 get_number_of_cameras: get_number_of_cameras, 65 get_camera_info: get_camera_info, 66 }; 67 68 camera_device_ops_t camera_ops = { 69 set_preview_window: android::set_preview_window, 70 set_callbacks: android::set_callbacks, 71 enable_msg_type: android::enable_msg_type, 72 disable_msg_type: android::disable_msg_type, 73 msg_type_enabled: android::msg_type_enabled, 74 75 start_preview: android::start_preview, 76 stop_preview: android::stop_preview, 77 preview_enabled: android::preview_enabled, 78 store_meta_data_in_buffers: android::store_meta_data_in_buffers, 79 80 start_recording: android::start_recording, 81 stop_recording: android::stop_recording, 82 recording_enabled: android::recording_enabled, 83 release_recording_frame: android::release_recording_frame, 84 85 auto_focus: android::auto_focus, 86 cancel_auto_focus: android::cancel_auto_focus, 87 88 take_picture: android::take_picture, 89 cancel_picture: android::cancel_picture, 90 91 set_parameters: android::set_parameters, 92 get_parameters: android::get_parameters, 93 put_parameters: android::put_parameters, 94 send_command: android::send_command, 95 96 release: android::release, 97 dump: android::dump, 98 }; 99 100 namespace android { 101 102 typedef struct { 103 QualcommCameraHardware *hardware; 104 int camera_released; 105 QCameraParameters parameters; 106 #if 1 107 camera_notify_callback notify_cb; 108 camera_data_callback data_cb; 109 camera_data_timestamp_callback data_cb_timestamp; 110 camera_request_memory get_memory; 111 void *user_data; 112 #endif 113 } camera_hardware_t; 114 115 typedef struct { 116 camera_memory_t mem; 117 int32_t msgType; 118 sp<IMemory> dataPtr; 119 void* user; 120 unsigned int index; 121 } q_cam_memory_t; 122 123 124 static void camera_release_memory(struct camera_memory *mem) 125 { 126 } 127 128 void cam_notify_callback(int32_t msgType, 129 int32_t ext1, 130 int32_t ext2, 131 void* user) 132 { 133 ALOGV("Q%s: E", __func__); 134 camera_device * device = (camera_device *)user; 135 if(device) { 136 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 137 if(camHal) { 138 camera_notify_callback notify_cb = camHal->notify_cb; 139 void *user_data = camHal->user_data; 140 if(notify_cb) { 141 notify_cb(msgType, ext1, ext2, user_data); 142 } 143 } 144 } 145 } 146 147 camera_memory_t* get_mem(int fd,size_t buf_size, 148 unsigned int num_bufs, 149 void *user) 150 { 151 ALOGV("Q%s: E", __func__); 152 camera_device * device = (camera_device *)user; 153 if(device) { 154 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 155 if(camHal) { 156 camera_request_memory getmem_cb = camHal->get_memory; 157 void *user_data = camHal->user_data; 158 if(getmem_cb) { 159 return getmem_cb(fd, buf_size, num_bufs, user_data); 160 } 161 } 162 } 163 return NULL; 164 } 165 #if 0 166 void native_send_data_callback(int32_t msgType, 167 camera_memory_t * framebuffer, 168 void* user) 169 { 170 ALOGE("Q%s: E", __func__); 171 static unsigned int counter = 0; 172 #if 0 173 camera_device * device = (camera_device *)user; 174 if(device) { 175 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 176 if(camHal) { 177 camera_data_callback data_cb = camHal->data_cb; 178 void *user_data = camHal->user_data; 179 if(data_cb) { 180 q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t)); 181 if (qmem) { 182 qmem->dataPtr = dataPtr; 183 qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset()); 184 qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID(); 185 qmem->mem.size = dataPtr->size( ); 186 qmem->mem.release = camera_release_memory; 187 qmem->msgType = msgType; 188 qmem->index = counter; 189 #endif 190 data_cb(msgType, framebuffer, counter, NULL, user); 191 counter++; 192 #if 0 193 } else { 194 ALOGE("%s: out of memory", __func__); 195 } 196 #endif 197 // } 198 // } 199 // } 200 } 201 #endif 202 203 static void cam_data_callback(int32_t msgType, 204 const sp<IMemory>& dataPtr, 205 void* user) 206 { 207 ALOGV("Q%s: E", __func__); 208 static unsigned int counter = 0; 209 camera_device * device = (camera_device *)user; 210 if(device) { 211 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 212 if(camHal) { 213 camera_data_callback data_cb = camHal->data_cb; 214 void *user_data = camHal->user_data; 215 if(data_cb) { 216 q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t)); 217 if (qmem) { 218 qmem->dataPtr = dataPtr; 219 qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset()); 220 qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID(); 221 qmem->mem.size = dataPtr->size( ); 222 qmem->mem.release = camera_release_memory; 223 qmem->msgType = msgType; 224 qmem->index = counter; 225 counter++; 226 data_cb(msgType, (camera_memory_t *)qmem, counter, NULL, user_data); 227 } else { 228 ALOGE("%s: out of memory", __func__); 229 } 230 } 231 } 232 } 233 } 234 235 static void cam_data_callback_timestamp(nsecs_t timestamp, 236 int32_t msgType, 237 const sp<IMemory>& dataPtr, 238 void* user) 239 { 240 ALOGV("Q%s: E", __func__); 241 242 static unsigned int counter = 0; 243 camera_device * device = (camera_device *)user; 244 if(device) { 245 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 246 if(camHal) { 247 camera_data_timestamp_callback data_cb_timestamp = camHal->data_cb_timestamp; 248 void *user_data = camHal->user_data; 249 if(data_cb_timestamp) { 250 q_cam_memory_t *qmem = (q_cam_memory_t *)malloc(sizeof(q_cam_memory_t)); 251 if (qmem) { 252 qmem->dataPtr = dataPtr; 253 qmem->mem.data = (void *)((int)dataPtr->pointer() + dataPtr->offset()); 254 qmem->mem.handle = NULL; //(void *)dataPtr->getHeapID(); 255 qmem->mem.size = dataPtr->size( ); 256 qmem->mem.release = camera_release_memory; 257 qmem->msgType = msgType; 258 qmem->index = counter; 259 counter++; 260 data_cb_timestamp(timestamp, msgType, (camera_memory_t *)qmem, counter, user_data); 261 } else { 262 ALOGE("%s: out of memory", __func__); 263 } 264 } 265 } 266 } 267 } 268 269 QualcommCameraHardware * util_get_Hal_obj( struct camera_device * device) 270 { 271 QualcommCameraHardware* hardware = NULL; 272 if(device && device->priv){ 273 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 274 hardware = camHal->hardware; 275 } 276 return hardware; 277 } 278 void close_Hal_obj( struct camera_device * device) 279 { 280 ALOGV("%s: E", __func__); 281 QualcommCameraHardware* hardware = NULL; 282 if(device && device->priv){ 283 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 284 ALOGI("%s: clear hw", __func__); 285 hardware = camHal->hardware; 286 delete hardware; 287 } 288 ALOGV("%s: X", __func__); 289 } 290 291 292 QCameraParameters* util_get_HAL_parameter( struct camera_device * device) 293 { 294 QCameraParameters *param = NULL; 295 if(device && device->priv){ 296 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 297 param = &(camHal->parameters); 298 } 299 return param; 300 } 301 302 303 extern "C" int get_number_of_cameras() 304 { 305 /* try to query every time we get the call!*/ 306 307 ALOGV("Q%s: E", __func__); 308 return android::HAL_getNumberOfCameras( ); 309 } 310 311 extern "C" int get_camera_info(int camera_id, struct camera_info *info) 312 { 313 int rc = -1; 314 ALOGV("Q%s: E", __func__); 315 if(info) { 316 struct CameraInfo camInfo; 317 memset(&camInfo, -1, sizeof (struct CameraInfo)); 318 HAL_getCameraInfo(camera_id, &camInfo); 319 if (camInfo.facing >= 0) { 320 rc = 0; 321 info->facing = camInfo.facing; 322 info->orientation = camInfo.orientation; 323 } 324 } 325 ALOGV("Q%s: X", __func__); 326 return rc; 327 } 328 329 330 /* HAL should return NULL if it fails to open camera hardware. */ 331 extern "C" int camera_device_open( 332 const struct hw_module_t* module, const char* id, 333 struct hw_device_t** hw_device) 334 { 335 ALOGV("Q%s: E", __func__); 336 int rc = -1; 337 camera_device *device = NULL; 338 if(module && id && hw_device) { 339 int cameraId = atoi(id); 340 341 if (!strcmp(module->name, camera_common.name)) { 342 device = 343 (camera_device *)malloc(sizeof (struct camera_device)); 344 if(device) { 345 camera_hardware_t *camHal = 346 (camera_hardware_t *) malloc(sizeof (camera_hardware_t)); 347 if(camHal) { 348 memset(camHal, 0, sizeof (camera_hardware_t)); 349 camHal->hardware = HAL_openCameraHardware(cameraId); 350 if (camHal->hardware != NULL) { 351 /*To Do: populate camHal*/ 352 device->common.close = close_camera_device; 353 device->ops = &camera_ops; 354 device->priv = (void *)camHal; 355 rc = 0; 356 } else { 357 free(camHal); 358 free (device); 359 device = NULL; 360 } 361 } else { 362 free (device); 363 device = NULL; 364 } 365 } 366 } 367 } 368 *hw_device = (hw_device_t*)device; 369 return rc; 370 } 371 372 extern "C" int close_camera_device( hw_device_t *hw_dev) 373 { 374 ALOGV("Q%s: device =%p E", __func__, hw_dev); 375 int rc = -1; 376 camera_device_t *device = (camera_device_t *)hw_dev; 377 if(device) { 378 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 379 if(camHal ) { 380 //if(!camHal->camera_released) { 381 QualcommCameraHardware* hardware = util_get_Hal_obj( device); 382 if(hardware != NULL) { 383 if(camHal->camera_released != true) 384 hardware->release( ); 385 //hardware.clear( ); 386 387 } 388 //} 389 close_Hal_obj(device); 390 free(device->priv); 391 device->priv = NULL; 392 } 393 free(device); 394 rc = 0; 395 } 396 return rc; 397 } 398 399 400 int set_preview_window(struct camera_device * device, 401 struct preview_stream_ops *window) 402 { 403 ALOGV("Q%s: E window = %p", __func__, window); 404 int rc = -1; 405 QualcommCameraHardware *hardware = util_get_Hal_obj(device); 406 if(hardware != NULL) { 407 rc = hardware->set_PreviewWindow((void *)window); 408 } 409 return rc; 410 } 411 412 void set_callbacks(struct camera_device * device, 413 camera_notify_callback notify_cb, 414 camera_data_callback data_cb, 415 camera_data_timestamp_callback data_cb_timestamp, 416 camera_request_memory get_memory, 417 void *user) 418 { 419 ALOGV("Q%s: E", __func__); 420 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 421 if(hardware != NULL){ 422 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 423 if(camHal) { 424 camera_notify_callback cam_nt_cb; 425 camera_data_callback cam_dt_cb; 426 camera_data_timestamp_callback cam_dt_timestamp_cb; 427 428 camHal->notify_cb = notify_cb; 429 camHal->data_cb = data_cb; 430 camHal->data_cb_timestamp = data_cb_timestamp; 431 camHal->user_data = user; 432 camHal->get_memory = get_memory; 433 #if 0 434 if(notify_cb) { 435 cam_nt_cb = cam_notify_callback; 436 } else { 437 cam_nt_cb = NULL; 438 } 439 440 if(data_cb) { 441 cam_dt_cb = cam_data_callback; 442 } else { 443 cam_dt_cb = NULL; 444 } 445 446 if(data_cb_timestamp) { 447 cam_dt_timestamp_cb = cam_data_callback_timestamp; 448 } else { 449 cam_dt_timestamp_cb = NULL; 450 } 451 #endif 452 ALOGV("cam_nt_cb =%p,cam_dt_cb=%p,cam_dt_timestamp_cb=%p", cam_nt_cb, cam_dt_cb, cam_dt_timestamp_cb); 453 hardware->setCallbacks(notify_cb,data_cb,data_cb_timestamp,get_memory, user); 454 } 455 } 456 } 457 458 void enable_msg_type(struct camera_device * device, int32_t msg_type) 459 { 460 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 461 if(hardware != NULL){ 462 hardware->enableMsgType(msg_type); 463 } 464 } 465 466 void disable_msg_type(struct camera_device * device, int32_t msg_type) 467 { 468 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 469 ALOGV("Q%s: E", __func__); 470 if(hardware != NULL){ 471 hardware->disableMsgType(msg_type); 472 } 473 } 474 475 int msg_type_enabled(struct camera_device * device, int32_t msg_type) 476 { 477 ALOGV("Q%s: E", __func__); 478 int rc = -1; 479 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 480 if(hardware != NULL){ 481 rc = hardware->msgTypeEnabled(msg_type); 482 } 483 return rc; 484 } 485 486 int start_preview(struct camera_device * device) 487 { 488 ALOGV("Q%s: E", __func__); 489 int rc = -1; 490 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 491 if(hardware != NULL){ 492 rc = hardware->startPreview( ); 493 } 494 ALOGV("Q%s: X", __func__); 495 return rc; 496 } 497 498 void stop_preview(struct camera_device * device) 499 { 500 ALOGV("Q%s: E", __func__); 501 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 502 if(hardware != NULL){ 503 hardware->stopPreview( ); 504 } 505 } 506 507 int preview_enabled(struct camera_device * device) 508 { 509 ALOGV("Q%s: E", __func__); 510 int rc = -1; 511 QualcommCameraHardware* hardware = util_get_Hal_obj(device); 512 if(hardware != NULL){ 513 rc = hardware->previewEnabled( ); 514 } 515 return rc; 516 } 517 518 int store_meta_data_in_buffers(struct camera_device * device, int enable) 519 { 520 ALOGV("Q%s: E", __func__); 521 int rc = -1; 522 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 523 if(hardware != NULL){ 524 rc = hardware->storeMetaDataInBuffers( enable); 525 } 526 return rc; 527 } 528 529 int start_recording(struct camera_device * device) 530 { 531 ALOGV("Q%s: E", __func__); 532 int rc = -1; 533 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 534 if(hardware != NULL){ 535 rc = hardware->startRecording( ); 536 } 537 return rc; 538 } 539 540 void stop_recording(struct camera_device * device) 541 { 542 ALOGV("Q%s: E", __func__); 543 QualcommCameraHardware* hardware = util_get_Hal_obj(device); 544 if(hardware != NULL){ 545 hardware->stopRecording( ); 546 } 547 } 548 549 int recording_enabled(struct camera_device * device) 550 { 551 ALOGV("Q%s: E", __func__); 552 int rc = -1; 553 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 554 if(hardware != NULL){ 555 rc = hardware->recordingEnabled( ); 556 } 557 return rc; 558 } 559 560 void release_recording_frame(struct camera_device * device, 561 const void *opaque) 562 { 563 ALOGV("Q%s: E", __func__); 564 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 565 if(hardware != NULL){ 566 hardware->releaseRecordingFrame( opaque); 567 } 568 } 569 570 int auto_focus(struct camera_device * device) 571 { 572 ALOGV("Q%s: E", __func__); 573 int rc = -1; 574 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 575 if(hardware != NULL){ 576 rc = hardware->autoFocus( ); 577 } 578 return rc; 579 } 580 581 int cancel_auto_focus(struct camera_device * device) 582 { 583 ALOGV("Q%s: E", __func__); 584 int rc = -1; 585 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 586 if(hardware != NULL){ 587 rc = hardware->cancelAutoFocus( ); 588 } 589 return rc; 590 } 591 592 int take_picture(struct camera_device * device) 593 { 594 ALOGV("Q%s: E", __func__); 595 int rc = -1; 596 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 597 if(hardware != NULL){ 598 rc = hardware->takePicture( ); 599 } 600 return rc; 601 } 602 603 int cancel_picture(struct camera_device * device) 604 605 { 606 ALOGV("Q%s: E", __func__); 607 int rc = -1; 608 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 609 if(hardware != NULL){ 610 rc = hardware->cancelPicture( ); 611 } 612 return rc; 613 } 614 615 QCameraParameters g_param; 616 String8 g_str; 617 int set_parameters(struct camera_device * device, const char *parms) 618 619 { 620 ALOGV("Q%s: E", __func__); 621 int rc = -1; 622 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 623 if(hardware != NULL && parms){ 624 // = util_get_HAL_parameter(device); 625 g_str = String8(parms); 626 627 g_param.unflatten(g_str); 628 rc = hardware->setParameters( g_param ); 629 } 630 return rc; 631 } 632 633 char* get_parameters(struct camera_device * device) 634 { 635 ALOGV("Q%s: E", __func__); 636 char* rc = NULL; 637 638 QCameraParameters param; 639 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 640 if(hardware != NULL){ 641 g_param = hardware->getParameters( ); 642 g_str = g_param.flatten( ); 643 rc = (char *)g_str.string( ); 644 if (!rc) { 645 ALOGE("get_parameters: NULL string"); 646 } else { 647 //ALOGE("get_parameters: %s", rc); 648 } 649 } 650 ALOGV("get_parameters X"); 651 return rc; 652 } 653 654 void put_parameters(struct camera_device * device, char *parm) 655 656 { 657 ALOGV("Q%s: E", __func__); 658 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 659 if(hardware != NULL){ 660 if(hardware != NULL){ 661 //rc = hardware->putParameters(parm ); 662 } 663 } 664 ALOGV("put_parameters X"); 665 } 666 667 int send_command(struct camera_device * device, 668 int32_t cmd, int32_t arg1, int32_t arg2) 669 { 670 ALOGV("Q%s: E", __func__); 671 int rc = -1; 672 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 673 if(hardware != NULL){ 674 rc = hardware->sendCommand( cmd, arg1, arg2); 675 } 676 return rc; 677 } 678 679 void release(struct camera_device * device) 680 { 681 ALOGV("Q%s: E", __func__); 682 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 683 if(hardware != NULL){ 684 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 685 hardware->release( ); 686 camHal->camera_released = true; 687 } 688 } 689 690 int dump(struct camera_device * device, int fd) 691 { 692 ALOGV("Q%s: E", __func__); 693 int rc = -1; 694 QualcommCameraHardware * hardware = util_get_Hal_obj(device); 695 if(hardware != NULL){ 696 //rc = hardware->dump( fd ); 697 rc = 0; 698 } 699 return rc; 700 } 701 702 }; // namespace android 703