1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 /*#error uncomment this for compiler test!*/ 30 31 //#define ALOG_NDEBUG 0 32 #define ALOG_NIDEBUG 0 33 #define LOG_TAG "QualcommCamera" 34 #include <utils/Log.h> 35 #include <utils/threads.h> 36 #include <fcntl.h> 37 #include <sys/mman.h> 38 39 #include "QCameraHAL.h" 40 /* include QCamera Hardware Interface Header*/ 41 #include "QualcommCamera.h" 42 #include "QualcommUsbCamera.h" 43 //#include "QualcommCameraHardware.h" 44 //#include <camera/CameraHardwareInterface.h> 45 46 extern "C" { 47 #include <sys/time.h> 48 } 49 50 /* HAL function implementation goes here*/ 51 52 /** 53 * The functions need to be provided by the camera HAL. 54 * 55 * If getNumberOfCameras() returns N, the valid cameraId for getCameraInfo() 56 * and openCameraHardware() is 0 to N-1. 57 */ 58 59 static hw_module_methods_t camera_module_methods = { 60 open: camera_device_open, 61 }; 62 63 static hw_module_t camera_common = { 64 tag: HARDWARE_MODULE_TAG, 65 version_major: 0, 66 version_minor: 01, 67 id: CAMERA_HARDWARE_MODULE_ID, 68 name: "Qcamera", 69 author:"Qcom", 70 methods: &camera_module_methods, 71 dso: NULL, 72 reserved: {0}, 73 }; 74 camera_module_t HAL_MODULE_INFO_SYM = { 75 common: camera_common, 76 get_number_of_cameras: get_number_of_cameras, 77 get_camera_info: get_camera_info, 78 }; 79 80 camera_device_ops_t camera_ops = { 81 set_preview_window: android::set_preview_window, 82 set_callbacks: android::set_CallBacks, 83 enable_msg_type: android::enable_msg_type, 84 disable_msg_type: android::disable_msg_type, 85 msg_type_enabled: android::msg_type_enabled, 86 87 start_preview: android::start_preview, 88 stop_preview: android::stop_preview, 89 preview_enabled: android::preview_enabled, 90 store_meta_data_in_buffers: android::store_meta_data_in_buffers, 91 92 start_recording: android::start_recording, 93 stop_recording: android::stop_recording, 94 recording_enabled: android::recording_enabled, 95 release_recording_frame: android::release_recording_frame, 96 97 auto_focus: android::auto_focus, 98 cancel_auto_focus: android::cancel_auto_focus, 99 100 take_picture: android::take_picture, 101 cancel_picture: android::cancel_picture, 102 103 set_parameters: android::set_parameters, 104 get_parameters: android::get_parameters, 105 put_parameters: android::put_parameters, 106 send_command: android::send_command, 107 108 release: android::release, 109 dump: android::dump, 110 }; 111 112 namespace android { 113 114 typedef struct { 115 camera_device hw_dev; 116 //sp<CameraHardwareInterface> hardware; 117 QCameraHardwareInterface *hardware; 118 int camera_released; 119 int cameraId; 120 //QCameraParameters parameters; 121 } camera_hardware_t; 122 123 typedef struct { 124 camera_memory_t mem; 125 int32_t msgType; 126 sp<IMemory> dataPtr; 127 void* user; 128 unsigned int index; 129 } q_cam_memory_t; 130 131 QCameraHardwareInterface *util_get_Hal_obj( struct camera_device * device) 132 { 133 QCameraHardwareInterface *hardware = NULL; 134 if(device && device->priv){ 135 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 136 hardware = camHal->hardware; 137 } 138 return hardware; 139 } 140 141 #if 0 //mzhu 142 QCameraParameters* util_get_HAL_parameter( struct camera_device * device) 143 { 144 QCameraParameters *param = NULL; 145 if(device && device->priv){ 146 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 147 param = &(camHal->parameters); 148 } 149 return param; 150 } 151 #endif //mzhu 152 153 extern "C" int get_number_of_cameras() 154 { 155 /* try to query every time we get the call!*/ 156 157 ALOGE("Q%s: E", __func__); 158 int is_mpq = 0; 159 IS_TARGET_MPQ(is_mpq); 160 161 if(is_mpq) 162 return usbcam_get_number_of_cameras(); 163 164 /* if(!is_mpq) */ 165 return android::HAL_getNumberOfCameras( ); 166 } 167 168 extern "C" int get_camera_info(int camera_id, struct camera_info *info) 169 { 170 int rc = -1; 171 ALOGE("Q%s: E", __func__); 172 173 int is_mpq = 0; 174 IS_TARGET_MPQ(is_mpq); 175 176 if(is_mpq) 177 return usbcam_get_camera_info(camera_id, info); 178 179 /* if(!is_mpq) */ 180 if(info) { 181 struct CameraInfo camInfo; 182 memset(&camInfo, -1, sizeof (struct CameraInfo)); 183 android::HAL_getCameraInfo(camera_id, &camInfo); 184 if (camInfo.facing >= 0) { 185 rc = 0; 186 info->facing = camInfo.facing; 187 info->orientation = camInfo.orientation; 188 } 189 } 190 ALOGV("Q%s: X", __func__); 191 return rc; 192 } 193 194 195 /* HAL should return NULL if it fails to open camera hardware. */ 196 extern "C" int camera_device_open( 197 const struct hw_module_t* module, const char* id, 198 struct hw_device_t** hw_device) 199 { 200 int rc = -1; 201 int mode = 0; // TODO: need to add 3d/2d mode, etc 202 camera_device *device = NULL; 203 int is_mpq = 0; 204 IS_TARGET_MPQ(is_mpq); 205 206 if(is_mpq && module && id && hw_device) 207 return usbcam_camera_device_open(module, id, hw_device); 208 209 /* if(!is_mpq) */ 210 if(module && id && hw_device) { 211 int cameraId = atoi(id); 212 213 if (!strcmp(module->name, camera_common.name)) { 214 camera_hardware_t *camHal = 215 (camera_hardware_t *) malloc(sizeof (camera_hardware_t)); 216 if(!camHal) { 217 *hw_device = NULL; 218 ALOGE("%s: end in no mem", __func__); 219 return rc; 220 } 221 /* we have the camera_hardware obj malloced */ 222 memset(camHal, 0, sizeof (camera_hardware_t)); 223 camHal->hardware = new QCameraHardwareInterface(cameraId, mode); //HAL_openCameraHardware(cameraId); 224 if (camHal->hardware && camHal->hardware->isCameraReady()) { 225 camHal->cameraId = cameraId; 226 device = &camHal->hw_dev; 227 device->common.close = close_camera_device; 228 device->ops = &camera_ops; 229 device->priv = (void *)camHal; 230 rc = 0; 231 } else { 232 if (camHal->hardware) { 233 delete camHal->hardware; 234 camHal->hardware = NULL; 235 } 236 free(camHal); 237 device = NULL; 238 } 239 } 240 } 241 /* pass actual hw_device ptr to framework. This amkes that we actally be use memberof() macro */ 242 *hw_device = (hw_device_t*)&device->common; 243 ALOGE("%s: end rc %d", __func__, rc); 244 return rc; 245 } 246 247 extern "C" int close_camera_device( hw_device_t *hw_dev) 248 { 249 ALOGE("Q%s: device =%p E", __func__, hw_dev); 250 int rc = -1; 251 camera_device_t *device = (camera_device_t *)hw_dev; 252 253 if(device) { 254 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 255 if(camHal ) { 256 QCameraHardwareInterface *hardware = util_get_Hal_obj( device); 257 if(!camHal->camera_released) { 258 if(hardware != NULL) { 259 hardware->release( ); 260 } 261 } 262 if(hardware != NULL) 263 delete hardware; 264 free(camHal); 265 } 266 rc = 0; 267 } 268 return rc; 269 } 270 271 272 int set_preview_window(struct camera_device * device, 273 struct preview_stream_ops *window) 274 { 275 int rc = -1; 276 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 277 278 if(hardware != NULL) { 279 rc = hardware->setPreviewWindow(window); 280 } 281 return rc; 282 } 283 284 void set_CallBacks(struct camera_device * device, 285 camera_notify_callback notify_cb, 286 camera_data_callback data_cb, 287 camera_data_timestamp_callback data_cb_timestamp, 288 camera_request_memory get_memory, 289 void *user) 290 { 291 ALOGE("Q%s: E", __func__); 292 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 293 if(hardware != NULL){ 294 hardware->setCallbacks(notify_cb,data_cb, data_cb_timestamp, get_memory, user); 295 } 296 } 297 298 void enable_msg_type(struct camera_device * device, int32_t msg_type) 299 { 300 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 301 if(hardware != NULL){ 302 hardware->enableMsgType(msg_type); 303 } 304 } 305 306 void disable_msg_type(struct camera_device * device, int32_t msg_type) 307 { 308 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 309 ALOGE("Q%s: E", __func__); 310 if(hardware != NULL){ 311 hardware->disableMsgType(msg_type); 312 } 313 } 314 315 int msg_type_enabled(struct camera_device * device, int32_t msg_type) 316 { 317 ALOGE("Q%s: E", __func__); 318 int rc = -1; 319 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 320 if(hardware != NULL){ 321 rc = hardware->msgTypeEnabled(msg_type); 322 } 323 return rc; 324 } 325 326 int start_preview(struct camera_device * device) 327 { 328 ALOGE("Q%s: E", __func__); 329 int rc = -1; 330 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 331 if(hardware != NULL){ 332 rc = hardware->startPreview( ); 333 } 334 ALOGE("Q%s: X", __func__); 335 return rc; 336 } 337 338 void stop_preview(struct camera_device * device) 339 { 340 ALOGE("Q%s: E", __func__); 341 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 342 if(hardware != NULL){ 343 hardware->stopPreview( ); 344 } 345 } 346 347 int preview_enabled(struct camera_device * device) 348 { 349 ALOGE("Q%s: E", __func__); 350 int rc = -1; 351 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 352 if(hardware != NULL){ 353 rc = hardware->previewEnabled( ); 354 } 355 return rc; 356 } 357 358 int store_meta_data_in_buffers(struct camera_device * device, int enable) 359 { 360 ALOGE("Q%s: E", __func__); 361 int rc = -1; 362 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 363 if(hardware != NULL){ 364 rc = hardware->storeMetaDataInBuffers(enable); 365 } 366 return rc; 367 } 368 369 int start_recording(struct camera_device * device) 370 { 371 ALOGE("Q%s: E", __func__); 372 int rc = -1; 373 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 374 if(hardware != NULL){ 375 rc = hardware->startRecording( ); 376 } 377 return rc; 378 } 379 380 void stop_recording(struct camera_device * device) 381 { 382 ALOGE("Q%s: E", __func__); 383 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 384 if(hardware != NULL){ 385 hardware->stopRecording( ); 386 } 387 } 388 389 int recording_enabled(struct camera_device * device) 390 { 391 ALOGE("Q%s: E", __func__); 392 int rc = -1; 393 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 394 if(hardware != NULL){ 395 rc = hardware->recordingEnabled( ); 396 } 397 return rc; 398 } 399 400 void release_recording_frame(struct camera_device * device, 401 const void *opaque) 402 { 403 ALOGV("Q%s: E", __func__); 404 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 405 if(hardware != NULL){ 406 hardware->releaseRecordingFrame(opaque); 407 } 408 } 409 410 int auto_focus(struct camera_device * device) 411 { 412 ALOGE("Q%s: E", __func__); 413 int rc = -1; 414 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 415 if(hardware != NULL){ 416 rc = hardware->autoFocus( ); 417 } 418 return rc; 419 } 420 421 int cancel_auto_focus(struct camera_device * device) 422 { 423 ALOGE("Q%s: E", __func__); 424 int rc = -1; 425 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 426 if(hardware != NULL){ 427 rc = hardware->cancelAutoFocus( ); 428 } 429 return rc; 430 } 431 432 int take_picture(struct camera_device * device) 433 { 434 ALOGE("Q%s: E", __func__); 435 int rc = -1; 436 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 437 if(hardware != NULL){ 438 rc = hardware->takePicture( ); 439 } 440 return rc; 441 } 442 443 int cancel_picture(struct camera_device * device) 444 445 { 446 ALOGE("Q%s: E", __func__); 447 int rc = -1; 448 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 449 if(hardware != NULL){ 450 rc = hardware->cancelPicture( ); 451 } 452 return rc; 453 } 454 455 int set_parameters(struct camera_device * device, const char *parms) 456 457 { 458 ALOGE("Q%s: E", __func__); 459 int rc = -1; 460 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 461 if(hardware != NULL && parms){ 462 //QCameraParameters param;// = util_get_HAL_parameter(device); 463 //String8 str = String8(parms); 464 465 //param.unflatten(str); 466 rc = hardware->setParameters(parms); 467 //rc = 0; 468 } 469 return rc; 470 } 471 472 char* get_parameters(struct camera_device * device) 473 { 474 ALOGE("Q%s: E", __func__); 475 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 476 if(hardware != NULL){ 477 char *parms = NULL; 478 hardware->getParameters(&parms); 479 return parms; 480 } 481 return NULL; 482 } 483 484 void put_parameters(struct camera_device * device, char *parm) 485 486 { 487 ALOGE("Q%s: E", __func__); 488 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 489 if(hardware != NULL){ 490 hardware->putParameters(parm); 491 } 492 } 493 494 int send_command(struct camera_device * device, 495 int32_t cmd, int32_t arg1, int32_t arg2) 496 { 497 ALOGE("Q%s: E", __func__); 498 int rc = -1; 499 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 500 if(hardware != NULL){ 501 rc = hardware->sendCommand( cmd, arg1, arg2); 502 } 503 return rc; 504 } 505 506 void release(struct camera_device * device) 507 { 508 ALOGE("Q%s: E", __func__); 509 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 510 if(hardware != NULL){ 511 camera_hardware_t *camHal = (camera_hardware_t *)device->priv; 512 hardware->release( ); 513 camHal->camera_released = true; 514 } 515 } 516 517 int dump(struct camera_device * device, int fd) 518 { 519 ALOGE("Q%s: E", __func__); 520 int rc = -1; 521 QCameraHardwareInterface *hardware = util_get_Hal_obj(device); 522 if(hardware != NULL){ 523 rc = hardware->dump( fd ); 524 //rc = 0; 525 } 526 return rc; 527 } 528 529 }; // namespace android 530