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 
43 extern "C" {
44 #include <sys/time.h>
45 }
46 
47 /**
48  * The functions need to be provided by the camera HAL.
49  *
50  * If getNumberOfCameras() returns N, the valid cameraId for getCameraInfo()
51  * and openCameraHardware() is 0 to N-1.
52  */
53 
54 static hw_module_methods_t camera_module_methods = {
55     open: camera_device_open,
56 };
57 
58 static hw_module_t camera_common  = {
59     tag: HARDWARE_MODULE_TAG,
60     module_api_version: CAMERA_MODULE_API_VERSION_2_0,
61     hal_api_version: HARDWARE_HAL_API_VERSION,
62     id: CAMERA_HARDWARE_MODULE_ID,
63     name: "Qcamera",
64     author:"Qcom",
65     methods: &camera_module_methods,
66     dso: NULL,
67     reserved:  {0},
68 };
69 
70 camera_module_t HAL_MODULE_INFO_SYM = {
71     common: camera_common,
72     get_number_of_cameras: get_number_of_cameras,
73     get_camera_info: get_camera_info,
74 };
75 
76 camera2_device_ops_t camera_ops = {
77     set_request_queue_src_ops:           android::set_request_queue_src_ops,
78     notify_request_queue_not_empty:      android::notify_request_queue_not_empty,
79     set_frame_queue_dst_ops:             android::set_frame_queue_dst_ops,
80     get_in_progress_count:               android::get_in_progress_count,
81     flush_captures_in_progress:          android::flush_captures_in_progress,
82     construct_default_request:           android::construct_default_request,
83 
84     allocate_stream:                     android::allocate_stream,
85     register_stream_buffers:             android::register_stream_buffers,
86     release_stream:                      android::release_stream,
87 
88     allocate_reprocess_stream:           android::allocate_reprocess_stream,
89     allocate_reprocess_stream_from_stream: android::allocate_reprocess_stream_from_stream,
90     release_reprocess_stream:            android::release_reprocess_stream,
91 
92     trigger_action:                      android::trigger_action,
93     set_notify_callback:                 android::set_notify_callback,
94     get_metadata_vendor_tag_ops:         android::get_metadata_vendor_tag_ops,
95     dump:                                android::dump,
96 };
97 
98 namespace android {
99 
100 typedef struct {
101   camera2_device_t hw_dev;
102   QCameraHardwareInterface *hardware;
103   int camera_released;
104   int cameraId;
105 } camera_hardware_t;
106 
util_get_Hal_obj(const camera2_device_t * device)107 QCameraHardwareInterface *util_get_Hal_obj(const camera2_device_t * device)
108 {
109     QCameraHardwareInterface *hardware = NULL;
110     if(device && device->priv){
111         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
112         hardware = camHal->hardware;
113     }
114     return hardware;
115 }
116 
get_number_of_cameras()117 extern "C" int get_number_of_cameras()
118 {
119     /* try to query every time we get the call!*/
120     ALOGE("Q%s: E", __func__);
121     return android::HAL_getNumberOfCameras();
122 }
123 
get_camera_info(int camera_id,struct camera_info * info)124 extern "C" int get_camera_info(int camera_id, struct camera_info *info)
125 {
126     int rc = -1;
127     ALOGE("Q%s: E, id = %d", __func__, camera_id);
128     if(info) {
129         rc = android::HAL_getCameraInfo(camera_id, info);
130     }
131     ALOGE("Q%s: X, id = %d", __func__, camera_id);
132     return rc;
133 }
134 
135 
136 /* HAL should return NULL if it fails to open camera hardware. */
camera_device_open(const struct hw_module_t * module,const char * id,struct hw_device_t ** hw_device)137 extern "C" int  camera_device_open(
138   const struct hw_module_t* module, const char* id,
139           struct hw_device_t** hw_device)
140 {
141     int rc = -1;
142     int mode = 0;
143     camera2_device_t *device = NULL;
144     ALOGE("Q%s: E, id = %s", __func__, id);
145     if(module && id && hw_device) {
146         int cameraId = atoi(id);
147 
148         if (!strcmp(module->name, camera_common.name)) {
149             camera_hardware_t *camHal =
150                 (camera_hardware_t *) malloc(sizeof (camera_hardware_t));
151             if(!camHal) {
152                 *hw_device = NULL;
153 	        ALOGE("%s:  end in no mem", __func__);
154 		return rc;
155 	    }
156             /* we have the camera_hardware obj malloced */
157             memset(camHal, 0, sizeof (camera_hardware_t));
158             camHal->hardware = new QCameraHardwareInterface(cameraId, mode);
159             if (camHal->hardware && camHal->hardware->isCameraReady()) {
160 		camHal->cameraId = cameraId;
161 	        device = &camHal->hw_dev;
162                 device->common.close = close_camera_device;
163                 device->common.version = CAMERA_DEVICE_API_VERSION_2_0;
164                 device->ops = &camera_ops;
165                 device->priv = (void *)camHal;
166                 rc =  0;
167             } else {
168                 if (camHal->hardware) {
169                     delete camHal->hardware;
170                     camHal->hardware = NULL;
171                 }
172                 free(camHal);
173                 device = NULL;
174             }
175         }
176     }
177     /* pass actual hw_device ptr to framework. This amkes that we actally be use memberof() macro */
178     *hw_device = (hw_device_t*)&device->common;
179     ALOGE("%s:  end rc %d", __func__, rc);
180     return rc;
181 }
182 
close_camera_device(hw_device_t * hw_dev)183 extern "C" int close_camera_device(hw_device_t *hw_dev)
184 {
185     ALOGE("Q%s: device =%p E", __func__, hw_dev);
186     int rc =  -1;
187     camera2_device_t *device = (camera2_device_t *)hw_dev;
188 
189     if(device) {
190         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
191         if(camHal ) {
192             QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
193             if(!camHal->camera_released) {
194                 if(hardware != NULL) {
195                     hardware->release( );
196                 }
197             }
198             if(hardware != NULL)
199                 delete hardware;
200             free(camHal);
201         }
202         rc = 0;
203     }
204     return rc;
205 }
206 
set_request_queue_src_ops(const struct camera2_device * device,const camera2_request_queue_src_ops_t * request_src_ops)207 int set_request_queue_src_ops(const struct camera2_device *device,
208     const camera2_request_queue_src_ops_t *request_src_ops)
209 {
210     int rc = INVALID_OPERATION;
211     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
212 
213     if(hardware != NULL) {
214         rc = hardware->set_request_queue_src_ops(request_src_ops);
215     }
216     return rc;
217 }
218 
notify_request_queue_not_empty(const struct camera2_device * device)219 int notify_request_queue_not_empty(const struct camera2_device *device)
220 {
221     int rc = INVALID_OPERATION;
222     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
223 
224     if(hardware != NULL) {
225         rc = hardware->notify_request_queue_not_empty();
226     }
227     return rc;
228 }
229 
set_frame_queue_dst_ops(const struct camera2_device * device,const camera2_frame_queue_dst_ops_t * frame_dst_ops)230 int set_frame_queue_dst_ops(const struct camera2_device *device,
231     const camera2_frame_queue_dst_ops_t *frame_dst_ops)
232 {
233     int rc = INVALID_OPERATION;
234     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
235 
236     if(hardware != NULL) {
237         rc = hardware->set_frame_queue_dst_ops(frame_dst_ops);
238     }
239     return rc;
240 }
241 
get_in_progress_count(const struct camera2_device * device)242 int get_in_progress_count(const struct camera2_device *device)
243 {
244     int rc = INVALID_OPERATION;
245     ALOGE("%s:E",__func__);
246     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
247 
248     if(hardware != NULL) {
249         rc = hardware->get_in_progress_count();
250     }
251     ALOGE("%s:X",__func__);
252     return rc;
253 }
254 
flush_captures_in_progress(const struct camera2_device *)255 int flush_captures_in_progress(const struct camera2_device *)
256 {
257     ALOGE("%s:E",__func__);
258     ALOGE("%s:X",__func__);
259     return INVALID_OPERATION;
260 }
261 
construct_default_request(const struct camera2_device * device,int request_template,camera_metadata_t ** request)262 int construct_default_request(const struct camera2_device *device,
263     int request_template,
264     camera_metadata_t **request)
265 {
266     int rc = INVALID_OPERATION;
267     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
268 
269     if(hardware != NULL) {
270         rc = hardware->construct_default_request(request_template, request);
271     }
272     return rc;
273 }
274 
allocate_stream(const struct camera2_device * device,uint32_t width,uint32_t height,int format,const camera2_stream_ops_t * stream_ops,uint32_t * stream_id,uint32_t * format_actual,uint32_t * usage,uint32_t * max_buffers)275 int allocate_stream(const struct camera2_device *device,
276         uint32_t width,
277         uint32_t height,
278         int      format,
279         const camera2_stream_ops_t *stream_ops,
280         uint32_t *stream_id,
281         uint32_t *format_actual,
282         uint32_t *usage,
283         uint32_t *max_buffers)
284 {
285     int rc = INVALID_OPERATION;
286     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
287 
288     if(hardware != NULL) {
289         rc = hardware->allocate_stream(width, height, format, stream_ops,
290             stream_id, format_actual, usage, max_buffers);
291     }
292     return rc;
293 }
294 
register_stream_buffers(const struct camera2_device * device,uint32_t stream_id,int num_buffers,buffer_handle_t * buffers)295 int register_stream_buffers(
296         const struct camera2_device *device,
297         uint32_t stream_id,
298         int num_buffers,
299         buffer_handle_t *buffers)
300 {
301     int rc = INVALID_OPERATION;
302     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
303 
304     if(hardware != NULL) {
305         rc = hardware->register_stream_buffers(stream_id, num_buffers, buffers);
306     }
307     return rc;
308 }
309 
release_stream(const struct camera2_device * device,uint32_t stream_id)310 int release_stream(
311         const struct camera2_device *device,
312         uint32_t stream_id)
313 {
314     int rc = INVALID_OPERATION;
315     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
316 
317     if(hardware != NULL) {
318         rc = hardware->release_stream(stream_id);
319     }
320     return rc;
321 }
322 
allocate_reprocess_stream(const struct camera2_device *,uint32_t width,uint32_t height,uint32_t format,const camera2_stream_in_ops_t * reprocess_stream_ops,uint32_t * stream_id,uint32_t * consumer_usage,uint32_t * max_buffers)323 int allocate_reprocess_stream(const struct camera2_device *,
324         uint32_t width,
325         uint32_t height,
326         uint32_t format,
327         const camera2_stream_in_ops_t *reprocess_stream_ops,
328         uint32_t *stream_id,
329         uint32_t *consumer_usage,
330         uint32_t *max_buffers)
331 {
332     return INVALID_OPERATION;
333 }
334 
allocate_reprocess_stream_from_stream(const struct camera2_device *,uint32_t output_stream_id,const camera2_stream_in_ops_t * reprocess_stream_ops,uint32_t * stream_id)335 int allocate_reprocess_stream_from_stream(const struct camera2_device *,
336         uint32_t output_stream_id,
337         const camera2_stream_in_ops_t *reprocess_stream_ops,
338         uint32_t *stream_id)
339 {
340     return INVALID_OPERATION;
341 }
342 
release_reprocess_stream(const struct camera2_device *,uint32_t stream_id)343 int release_reprocess_stream(
344         const struct camera2_device *,
345         uint32_t stream_id)
346 {
347     return INVALID_OPERATION;
348 }
349 
trigger_action(const struct camera2_device *,uint32_t trigger_id,int32_t ext1,int32_t ext2)350 int trigger_action(const struct camera2_device *,
351         uint32_t trigger_id,
352         int32_t ext1,
353         int32_t ext2)
354 {
355     return INVALID_OPERATION;
356 }
357 
set_notify_callback(const struct camera2_device * device,camera2_notify_callback notify_cb,void * user)358 int set_notify_callback(const struct camera2_device *device,
359         camera2_notify_callback notify_cb,
360         void *user)
361 {
362     int rc = INVALID_OPERATION;
363     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
364 
365     if(hardware != NULL) {
366         rc = hardware->set_notify_callback(notify_cb, user);
367     }
368     return rc;
369 }
370 
get_metadata_vendor_tag_ops(const struct camera2_device * device,vendor_tag_query_ops_t ** ops)371 int get_metadata_vendor_tag_ops(const struct camera2_device *device,
372         vendor_tag_query_ops_t **ops)
373 {
374     int rc = INVALID_OPERATION;
375     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
376 
377     if(hardware != NULL) {
378         rc = hardware->get_metadata_vendor_tag_ops(ops);
379     }
380     return rc;
381 }
382 
dump(const struct camera2_device *,int fd)383 int dump(const struct camera2_device *, int fd)
384 {
385     return INVALID_OPERATION;
386 }
387 
388 #if 0
389 int set_preview_window(camera2_device_t * device,
390         struct preview_stream_ops *window)
391 {
392     int rc = -1;
393     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
394 
395     if(hardware != NULL) {
396         rc = hardware->setPreviewWindow(window);
397     }
398     return rc;
399 }
400 
401 void set_CallBacks(camera2_device_t * device,
402         camera_notify_callback notify_cb,
403         camera_data_callback data_cb,
404         camera_data_timestamp_callback data_cb_timestamp,
405         camera_request_memory get_memory,
406         void *user)
407 {
408     ALOGE("Q%s: E", __func__);
409     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
410     if(hardware != NULL){
411         hardware->setCallbacks(notify_cb,data_cb, data_cb_timestamp, get_memory, user);
412     }
413 }
414 
415 void enable_msg_type(camera2_device_t * device, int32_t msg_type)
416 {
417     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
418     if(hardware != NULL){
419         hardware->enableMsgType(msg_type);
420     }
421 }
422 
423 void disable_msg_type(camera2_device_t * device, int32_t msg_type)
424 {
425     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
426     ALOGE("Q%s: E", __func__);
427     if(hardware != NULL){
428         hardware->disableMsgType(msg_type);
429     }
430 }
431 
432 int msg_type_enabled(camera2_device_t * device, int32_t msg_type)
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->msgTypeEnabled(msg_type);
439     }
440     return rc;
441 }
442 
443 int start_preview(camera2_device_t * device)
444 {
445     ALOGE("Q%s: E", __func__);
446     int rc = -1;
447     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
448     if(hardware != NULL){
449         rc = hardware->startPreview( );
450     }
451     ALOGE("Q%s: X", __func__);
452     return rc;
453 }
454 
455 void stop_preview(camera2_device_t * device)
456 {
457     ALOGE("Q%s: E", __func__);
458     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
459     if(hardware != NULL){
460         hardware->stopPreview( );
461     }
462 }
463 
464 int preview_enabled(camera2_device_t * device)
465 {
466     ALOGE("Q%s: E", __func__);
467     int rc = -1;
468     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
469     if(hardware != NULL){
470         rc = hardware->previewEnabled( );
471     }
472     return rc;
473 }
474 
475 int store_meta_data_in_buffers(camera2_device_t *device, int enable)
476 {
477     ALOGE("Q%s: E", __func__);
478     int rc = -1;
479     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
480     if(hardware != NULL){
481       rc = hardware->storeMetaDataInBuffers(enable);
482     }
483     return rc;
484 }
485 
486 int start_recording(camera2_device_t *device)
487 {
488     ALOGE("Q%s: E", __func__);
489     int rc = -1;
490     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
491     if(hardware != NULL){
492         rc = hardware->startRecording( );
493     }
494     return rc;
495 }
496 
497 void stop_recording(camera2_device_t *device)
498 {
499     ALOGE("Q%s: E", __func__);
500     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
501     if(hardware != NULL){
502         hardware->stopRecording( );
503     }
504 }
505 
506 int recording_enabled(camera2_device_t *device)
507 {
508     ALOGE("Q%s: E", __func__);
509     int rc = -1;
510     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
511     if(hardware != NULL){
512         rc = hardware->recordingEnabled( );
513     }
514     return rc;
515 }
516 
517 void release_recording_frame(camera2_device_t *device,
518                 const void *opaque)
519 {
520     ALOGV("Q%s: E", __func__);
521     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
522     if(hardware != NULL){
523         hardware->releaseRecordingFrame(opaque);
524     }
525 }
526 
527 int auto_focus(camera2_device_t *device)
528 {
529     ALOGE("Q%s: E", __func__);
530     int rc = -1;
531     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
532     if(hardware != NULL){
533         rc = hardware->autoFocus( );
534     }
535     return rc;
536 }
537 
538 int cancel_auto_focus(camera2_device_t *device)
539 {
540     ALOGE("Q%s: E", __func__);
541     int rc = -1;
542     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
543     if(hardware != NULL){
544         rc = hardware->cancelAutoFocus( );
545     }
546     return rc;
547 }
548 
549 int take_picture(camera2_device_t *device)
550 {
551     ALOGE("Q%s: E", __func__);
552     int rc = -1;
553     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
554     if(hardware != NULL){
555         rc = hardware->takePicture( );
556     }
557     return rc;
558 }
559 
560 int cancel_picture(camera2_device_t *device)
561 
562 {
563     ALOGE("Q%s: E", __func__);
564     int rc = -1;
565     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
566     if(hardware != NULL){
567         rc = hardware->cancelPicture( );
568     }
569     return rc;
570 }
571 
572 int set_parameters(camera2_device_t *device, const char *parms)
573 
574 {
575     ALOGE("Q%s: E", __func__);
576     int rc = -1;
577     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
578     if(hardware != NULL && parms){
579         //QCameraParameters param;// = util_get_HAL_parameter(device);
580         //String8 str = String8(parms);
581 
582         //param.unflatten(str);
583         rc = hardware->setParameters(parms);
584         //rc = 0;
585   }
586   return rc;
587 }
588 
589 char* get_parameters(camera2_device_t *device)
590 {
591     ALOGE("Q%s: E", __func__);
592     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
593     if(hardware != NULL){
594 		char *parms = NULL;
595         hardware->getParameters(&parms);
596 		return parms;
597     }
598     return NULL;
599 }
600 
601 void put_parameters(camera2_device_t *device, char *parm)
602 
603 {
604     ALOGE("Q%s: E", __func__);
605     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
606     if(hardware != NULL){
607       hardware->putParameters(parm);
608     }
609 }
610 
611 int send_command(camera2_device_t *device,
612             int32_t cmd, int32_t arg1, int32_t arg2)
613 {
614     ALOGE("Q%s: E", __func__);
615     int rc = -1;
616     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
617     if(hardware != NULL){
618         rc = hardware->sendCommand( cmd, arg1, arg2);
619     }
620     return rc;
621 }
622 
623 void release(camera2_device_t *device)
624 {
625     ALOGE("Q%s: E", __func__);
626     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
627     if(hardware != NULL){
628         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
629         hardware->release( );
630         camHal->camera_released = true;
631     }
632 }
633 
634 int dump(camera2_device_t *device, int fd)
635 {
636     ALOGE("Q%s: E", __func__);
637     int rc = -1;
638     QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
639     if(hardware != NULL){
640         rc = hardware->dump( fd );
641     }
642     return rc;
643 }
644 #endif
645 
646 }; // namespace android
647