1/* Copyright (c) 2011, Code Aurora Forum. 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 Code Aurora Forum, Inc. 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 LOG_NDEBUG 0
32#define LOG_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 "QualcommCameraHardware.h"
43//#include <camera/CameraHardwareInterface.h>
44
45extern "C" {
46#include <sys/time.h>
47}
48
49/* HAL function implementation goes here*/
50
51/**
52 * The functions need to be provided by the camera HAL.
53 *
54 * If getNumberOfCameras() returns N, the valid cameraId for getCameraInfo()
55 * and openCameraHardware() is 0 to N-1.
56 */
57
58static hw_module_methods_t camera_module_methods = {
59    open: camera_device_open,
60};
61
62static hw_module_t camera_common  = {
63  tag: HARDWARE_MODULE_TAG,
64  version_major: 0,
65  version_minor: 01,
66  id: CAMERA_HARDWARE_MODULE_ID,
67  name: "Qcamera",
68  author:"Qcom",
69  methods: &camera_module_methods,
70  dso: NULL,
71  //reserved[0]:  0,
72};
73camera_module_t HAL_MODULE_INFO_SYM = {
74  common: camera_common,
75  get_number_of_cameras: get_number_of_cameras,
76  get_camera_info: get_camera_info,
77};
78
79camera_device_ops_t camera_ops = {
80  set_preview_window:         android::set_preview_window,
81  set_callbacks:              android::set_CallBacks,
82  enable_msg_type:            android::enable_msg_type,
83  disable_msg_type:           android::disable_msg_type,
84  msg_type_enabled:           android::msg_type_enabled,
85
86  start_preview:              android::start_preview,
87  stop_preview:               android::stop_preview,
88  preview_enabled:            android::preview_enabled,
89  store_meta_data_in_buffers: android::store_meta_data_in_buffers,
90
91  start_recording:            android::start_recording,
92  stop_recording:             android::stop_recording,
93  recording_enabled:          android::recording_enabled,
94  release_recording_frame:    android::release_recording_frame,
95
96  auto_focus:                 android::auto_focus,
97  cancel_auto_focus:          android::cancel_auto_focus,
98
99  take_picture:               android::take_picture,
100  cancel_picture:             android::cancel_picture,
101
102  set_parameters:             android::set_parameters,
103  get_parameters:             android::get_parameters,
104  put_parameters:             android::put_parameters,
105  send_command:               android::send_command,
106
107  release:                    android::release,
108  dump:                       android::dump,
109};
110
111namespace android {
112
113typedef struct {
114  camera_device hw_dev;
115  //sp<CameraHardwareInterface> hardware;
116  QCameraHardwareInterface *hardware;
117  int camera_released;
118  int cameraId;
119  //CameraParameters parameters;
120} camera_hardware_t;
121
122typedef struct {
123  camera_memory_t mem;
124  int32_t msgType;
125  sp<IMemory> dataPtr;
126  void* user;
127  unsigned int index;
128} q_cam_memory_t;
129
130QCameraHardwareInterface *util_get_Hal_obj( struct camera_device * device)
131{
132    QCameraHardwareInterface *hardware = NULL;
133    if(device && device->priv){
134        camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
135        hardware = camHal->hardware;
136    }
137    return hardware;
138}
139
140#if 0 //mzhu
141CameraParameters* util_get_HAL_parameter( struct camera_device * device)
142{
143    CameraParameters *param = NULL;
144    if(device && device->priv){
145        camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
146        param = &(camHal->parameters);
147    }
148    return param;
149}
150#endif //mzhu
151
152extern "C" int get_number_of_cameras()
153{
154    /* try to query every time we get the call!*/
155
156    LOGE("Q%s: E", __func__);
157    return android::HAL_getNumberOfCameras( );
158}
159
160extern "C" int get_camera_info(int camera_id, struct camera_info *info)
161{
162    int rc = -1;
163    LOGE("Q%s: E", __func__);
164    if(info) {
165        struct CameraInfo camInfo;
166        memset(&camInfo, -1, sizeof (struct CameraInfo));
167        android::HAL_getCameraInfo(camera_id, &camInfo);
168        if (camInfo.facing >= 0) {
169            rc = 0;
170            info->facing = camInfo.facing;
171            info->orientation = camInfo.orientation;
172        }
173    }
174    LOGV("Q%s: X", __func__);
175    return rc;
176}
177
178
179/* HAL should return NULL if it fails to open camera hardware. */
180extern "C" int  camera_device_open(
181  const struct hw_module_t* module, const char* id,
182          struct hw_device_t** hw_device)
183{
184    int rc = -1;
185	int mode = 0; // TODO: need to add 3d/2d mode, etc
186    camera_device *device = NULL;
187    if(module && id && hw_device) {
188        int cameraId = atoi(id);
189
190        if (!strcmp(module->name, camera_common.name)) {
191            camera_hardware_t *camHal =
192                (camera_hardware_t *) malloc(sizeof (camera_hardware_t));
193            if(!camHal) {
194                *hw_device = NULL;
195				    LOGE("%s:  end in no mem", __func__);
196				    return rc;
197		    }
198            /* we have the camera_hardware obj malloced */
199            memset(camHal, 0, sizeof (camera_hardware_t));
200            camHal->hardware = new QCameraHardwareInterface(cameraId, mode); //HAL_openCameraHardware(cameraId);
201            if (camHal->hardware && camHal->hardware->isCameraReady()) {
202				camHal->cameraId = cameraId;
203		        device = &camHal->hw_dev;
204                device->common.close = close_camera_device;
205                device->ops = &camera_ops;
206                device->priv = (void *)camHal;
207                rc =  0;
208            } else {
209                if (camHal->hardware) {
210                    delete camHal->hardware;
211                    camHal->hardware = NULL;
212                }
213                free(camHal);
214                device = NULL;
215            }
216        }
217    }
218	/* pass actual hw_device ptr to framework. This amkes that we actally be use memberof() macro */
219    *hw_device = (hw_device_t*)&device->common;
220    LOGE("%s:  end rc %d", __func__, rc);
221    return rc;
222}
223
224extern "C"  int close_camera_device( hw_device_t *hw_dev)
225{
226    LOGE("Q%s: device =%p E", __func__, hw_dev);
227    int rc =  -1;
228    camera_device_t *device = (camera_device_t *)hw_dev;
229
230    if(device) {
231        camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
232        if(camHal ) {
233            QCameraHardwareInterface *hardware = util_get_Hal_obj( device);
234            if(!camHal->camera_released) {
235                if(hardware != NULL) {
236                    hardware->release( );
237                }
238            }
239            if(hardware != NULL)
240                delete hardware;
241            free(camHal);
242        }
243        rc = 0;
244    }
245    return rc;
246}
247
248
249int set_preview_window(struct camera_device * device,
250        struct preview_stream_ops *window)
251{
252    int rc = -1;
253    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
254
255    if(hardware != NULL) {
256        rc = hardware->setPreviewWindow(window);
257    }
258    return rc;
259}
260
261void set_CallBacks(struct camera_device * device,
262        camera_notify_callback notify_cb,
263        camera_data_callback data_cb,
264        camera_data_timestamp_callback data_cb_timestamp,
265        camera_request_memory get_memory,
266        void *user)
267{
268    LOGE("Q%s: E", __func__);
269    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
270    if(hardware != NULL){
271        hardware->setCallbacks(notify_cb,data_cb, data_cb_timestamp, get_memory, user);
272    }
273}
274
275void enable_msg_type(struct camera_device * device, int32_t msg_type)
276{
277    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
278    if(hardware != NULL){
279        hardware->enableMsgType(msg_type);
280    }
281}
282
283void disable_msg_type(struct camera_device * device, int32_t msg_type)
284{
285    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
286    LOGE("Q%s: E", __func__);
287    if(hardware != NULL){
288        hardware->disableMsgType(msg_type);
289    }
290}
291
292int msg_type_enabled(struct camera_device * device, int32_t msg_type)
293{
294    LOGE("Q%s: E", __func__);
295    int rc = -1;
296    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
297    if(hardware != NULL){
298        rc = hardware->msgTypeEnabled(msg_type);
299    }
300    return rc;
301}
302
303int start_preview(struct camera_device * device)
304{
305    LOGE("Q%s: E", __func__);
306    int rc = -1;
307    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
308    if(hardware != NULL){
309        rc = hardware->startPreview( );
310    }
311    LOGE("Q%s: X", __func__);
312    return rc;
313}
314
315void stop_preview(struct camera_device * device)
316{
317    LOGE("Q%s: E", __func__);
318    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
319    if(hardware != NULL){
320        hardware->stopPreview( );
321    }
322}
323
324int preview_enabled(struct camera_device * device)
325{
326    LOGE("Q%s: E", __func__);
327    int rc = -1;
328    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
329    if(hardware != NULL){
330        rc = hardware->previewEnabled( );
331    }
332    return rc;
333}
334
335int store_meta_data_in_buffers(struct camera_device * device, int enable)
336{
337    LOGE("Q%s: E", __func__);
338    int rc = -1;
339    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
340    if(hardware != NULL){
341      rc = hardware->storeMetaDataInBuffers(enable);
342    }
343    return rc;
344}
345
346int start_recording(struct camera_device * device)
347{
348    LOGE("Q%s: E", __func__);
349    int rc = -1;
350    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
351    if(hardware != NULL){
352        rc = hardware->startRecording( );
353    }
354    return rc;
355}
356
357void stop_recording(struct camera_device * device)
358{
359    LOGE("Q%s: E", __func__);
360    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
361    if(hardware != NULL){
362        hardware->stopRecording( );
363    }
364}
365
366int recording_enabled(struct camera_device * device)
367{
368    LOGE("Q%s: E", __func__);
369    int rc = -1;
370    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
371    if(hardware != NULL){
372        rc = hardware->recordingEnabled( );
373    }
374    return rc;
375}
376
377void release_recording_frame(struct camera_device * device,
378                const void *opaque)
379{
380    LOGV("Q%s: E", __func__);
381    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
382    if(hardware != NULL){
383        hardware->releaseRecordingFrame(opaque);
384    }
385}
386
387int auto_focus(struct camera_device * device)
388{
389    LOGE("Q%s: E", __func__);
390    int rc = -1;
391    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
392    if(hardware != NULL){
393        rc = hardware->autoFocus( );
394    }
395    return rc;
396}
397
398int cancel_auto_focus(struct camera_device * device)
399{
400    LOGE("Q%s: E", __func__);
401    int rc = -1;
402    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
403    if(hardware != NULL){
404        rc = hardware->cancelAutoFocus( );
405    }
406    return rc;
407}
408
409int take_picture(struct camera_device * device)
410{
411    LOGE("Q%s: E", __func__);
412    int rc = -1;
413    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
414    if(hardware != NULL){
415        rc = hardware->takePicture( );
416    }
417    return rc;
418}
419
420int cancel_picture(struct camera_device * device)
421
422{
423    LOGE("Q%s: E", __func__);
424    int rc = -1;
425    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
426    if(hardware != NULL){
427        rc = hardware->cancelPicture( );
428    }
429    return rc;
430}
431
432int set_parameters(struct camera_device * device, const char *parms)
433
434{
435    LOGE("Q%s: E", __func__);
436    int rc = -1;
437    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
438    if(hardware != NULL && parms){
439        //CameraParameters param;// = util_get_HAL_parameter(device);
440        //String8 str = String8(parms);
441
442        //param.unflatten(str);
443        rc = hardware->setParameters(parms);
444        //rc = 0;
445  }
446  return rc;
447}
448
449char* get_parameters(struct camera_device * device)
450{
451    LOGE("Q%s: E", __func__);
452    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
453    if(hardware != NULL){
454		char *parms = NULL;
455        hardware->getParameters(&parms);
456		return parms;
457    }
458    return NULL;
459}
460
461void put_parameters(struct camera_device * device, char *parm)
462
463{
464    LOGE("Q%s: E", __func__);
465    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
466    if(hardware != NULL){
467      hardware->putParameters(parm);
468    }
469}
470
471int send_command(struct camera_device * device,
472            int32_t cmd, int32_t arg1, int32_t arg2)
473{
474    LOGE("Q%s: E", __func__);
475    int rc = -1;
476    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
477    if(hardware != NULL){
478        rc = hardware->sendCommand( cmd, arg1, arg2);
479    }
480    return rc;
481}
482
483void release(struct camera_device * device)
484{
485    LOGE("Q%s: E", __func__);
486    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
487    if(hardware != NULL){
488        camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
489        hardware->release( );
490        camHal->camera_released = true;
491    }
492}
493
494int dump(struct camera_device * device, int fd)
495{
496    LOGE("Q%s: E", __func__);
497    int rc = -1;
498    QCameraHardwareInterface *hardware = util_get_Hal_obj(device);
499    if(hardware != NULL){
500        rc = hardware->dump( fd );
501      //rc = 0;
502    }
503    return rc;
504}
505
506}; // namespace android
507