1 /* Copyright (c) 2011-2014, 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 
30 #define ALOG_NIDEBUG 0
31 #define LOG_TAG "QualcommCamera"
32 #include <utils/Log.h>
33 #include <utils/threads.h>
34 #include <fcntl.h>
35 #include <sys/mman.h>
36 #include <binder/IMemory.h>
37 #include <binder/MemoryBase.h>
38 #include <binder/MemoryHeapBase.h>
39 #include <utils/RefBase.h>
40 
41 #include "QualcommCamera.h"
42 #include "QCamera2Factory.h"
43 #include "QCamera2HWI.h"
44 
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 
60 static hw_module_methods_t camera_module_methods = {
61     open: camera_device_open,
62 };
63 
64 static hw_module_t camera_common = {
65     tag: HARDWARE_MODULE_TAG,
66     module_api_version: CAMERA_MODULE_API_VERSION_1_0,
67     hal_api_version: HARDWARE_HAL_API_VERSION,
68     id: CAMERA_HARDWARE_MODULE_ID,
69     name: "QCamera Module",
70     author: "Quic on behalf of CAF",
71     methods: &camera_module_methods,
72     dso: NULL,
73     reserved:  {0},
74 };
75 
76 using namespace qcamera;
77 namespace android {
78 
79 typedef struct {
80     camera_device hw_dev;
81     QCamera2HardwareInterface *hardware;
82     int camera_released;
83     int cameraId;
84 } camera_hardware_t;
85 
86 typedef struct {
87   camera_memory_t mem;
88   int32_t msgType;
89   sp<IMemory> dataPtr;
90   void* user;
91   unsigned int index;
92 } q_cam_memory_t;
93 
util_get_Hal_obj(struct camera_device * device)94 QCamera2HardwareInterface *util_get_Hal_obj( struct camera_device * device)
95 {
96     QCamera2HardwareInterface *hardware = NULL;
97     if(device && device->priv){
98         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
99         hardware = camHal->hardware;
100     }
101     return hardware;
102 }
103 
get_number_of_cameras()104 extern "C" int get_number_of_cameras()
105 {
106     /* try to query every time we get the call!*/
107 
108     ALOGE("Q%s: E", __func__);
109     return QCamera2Factory::get_number_of_cameras();
110 }
111 
get_camera_info(int camera_id,struct camera_info * info)112 extern "C" int get_camera_info(int camera_id, struct camera_info *info)
113 {
114     int rc = -1;
115     ALOGE("Q%s: E", __func__);
116 
117     if(info) {
118         QCamera2Factory::get_camera_info(camera_id, info);
119     }
120     CDBG("Q%s: X", __func__);
121     return rc;
122 }
123 
124 
125 /* 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)126 extern "C" int  camera_device_open(
127   const struct hw_module_t* module, const char* id,
128           struct hw_device_t** hw_device)
129 {
130     int rc = -1;
131     camera_device *device = NULL;
132 
133     if(module && id && hw_device) {
134         int cameraId = atoi(id);
135 
136         if (!strcmp(module->name, camera_common.name)) {
137             camera_hardware_t *camHal =
138                 (camera_hardware_t *) malloc(sizeof (camera_hardware_t));
139             if(!camHal) {
140                 *hw_device = NULL;
141                 ALOGE("%s:  end in no mem", __func__);
142                 return rc;
143             }
144             /* we have the camera_hardware obj malloced */
145             memset(camHal, 0, sizeof (camera_hardware_t));
146             camHal->hardware = new QCamera2HardwareInterface(cameraId);
147             if (camHal->hardware) {
148                 camHal->cameraId = cameraId;
149                 device = &camHal->hw_dev;
150                 device->common.close = close_camera_device;
151                 device->ops = &QCamera2HardwareInterface::mCameraOps;
152                 device->priv = (void *)camHal;
153                 rc =  0;
154             } else {
155                 if (camHal->hardware) {
156                     delete camHal->hardware;
157                     camHal->hardware = NULL;
158                 }
159                 free(camHal);
160                 device = NULL;
161                 goto EXIT;
162             }
163         }
164     }
165     /* pass actual hw_device ptr to framework. This amkes that we actally be use memberof() macro */
166     *hw_device = (hw_device_t*)&device->common;
167 
168 EXIT:
169 
170     ALOGE("%s:  end rc %d", __func__, rc);
171     return rc;
172 }
173 
close_camera_device(hw_device_t * hw_dev)174 extern "C"  int close_camera_device( hw_device_t *hw_dev)
175 {
176     ALOGE("Q%s: device =%p E", __func__, hw_dev);
177     int rc =  -1;
178     camera_device_t *device = (camera_device_t *)hw_dev;
179 
180     if(device) {
181         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
182         if(camHal ) {
183             QCamera2HardwareInterface *hardware = util_get_Hal_obj( device);
184             if(!camHal->camera_released) {
185                 if(hardware != NULL) {
186                     hardware->release(device);
187                 }
188             }
189             if(hardware != NULL)
190                 delete hardware;
191             free(camHal);
192         }
193         rc = 0;
194     }
195     return rc;
196 }
197 
198 
set_preview_window(struct camera_device * device,struct preview_stream_ops * window)199 int set_preview_window(struct camera_device * device,
200         struct preview_stream_ops *window)
201 {
202     int rc = -1;
203     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
204 
205     if(hardware != NULL) {
206         rc = hardware->set_preview_window(device, window);
207     }
208     return rc;
209 }
210 
set_CallBacks(struct camera_device * device,camera_notify_callback notify_cb,camera_data_callback data_cb,camera_data_timestamp_callback data_cb_timestamp,camera_request_memory get_memory,void * user)211 void set_CallBacks(struct camera_device * device,
212         camera_notify_callback notify_cb,
213         camera_data_callback data_cb,
214         camera_data_timestamp_callback data_cb_timestamp,
215         camera_request_memory get_memory,
216         void *user)
217 {
218     ALOGE("Q%s: E", __func__);
219     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
220     if(hardware != NULL){
221         hardware->set_CallBacks(device, notify_cb,data_cb, data_cb_timestamp, get_memory, user);
222     }
223 }
224 
enable_msg_type(struct camera_device * device,int32_t msg_type)225 void enable_msg_type(struct camera_device * device, int32_t msg_type)
226 {
227     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
228     if(hardware != NULL){
229         hardware->enable_msg_type(device, msg_type);
230     }
231 }
232 
disable_msg_type(struct camera_device * device,int32_t msg_type)233 void disable_msg_type(struct camera_device * device, int32_t msg_type)
234 {
235     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
236     ALOGE("Q%s: E", __func__);
237     if(hardware != NULL){
238         hardware->disable_msg_type(device, msg_type);
239     }
240 }
241 
msg_type_enabled(struct camera_device * device,int32_t msg_type)242 int msg_type_enabled(struct camera_device * device, int32_t msg_type)
243 {
244     ALOGE("Q%s: E", __func__);
245     int rc = -1;
246     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
247     if(hardware != NULL){
248         rc = hardware->msg_type_enabled(device, msg_type);
249     }
250     return rc;
251 }
252 
start_preview(struct camera_device * device)253 int start_preview(struct camera_device * device)
254 {
255     ALOGE("Q%s: E", __func__);
256     int rc = -1;
257     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
258     if(hardware != NULL){
259         rc = hardware->start_preview(device);
260     }
261     ALOGE("Q%s: X", __func__);
262     return rc;
263 }
264 
stop_preview(struct camera_device * device)265 void stop_preview(struct camera_device * device)
266 {
267     ALOGE("Q%s: E", __func__);
268     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
269     if(hardware != NULL){
270         hardware->stop_preview(device);
271     }
272 }
273 
preview_enabled(struct camera_device * device)274 int preview_enabled(struct camera_device * device)
275 {
276     ALOGE("Q%s: E", __func__);
277     int rc = -1;
278     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
279     if(hardware != NULL){
280         rc = hardware->preview_enabled(device);
281     }
282     return rc;
283 }
284 
store_meta_data_in_buffers(struct camera_device * device,int enable)285 int store_meta_data_in_buffers(struct camera_device * device, int enable)
286 {
287     ALOGE("Q%s: E", __func__);
288     int rc = -1;
289     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
290     if(hardware != NULL){
291       rc = hardware->store_meta_data_in_buffers(device, enable);
292     }
293     return rc;
294 }
295 
start_recording(struct camera_device * device)296 int start_recording(struct camera_device * device)
297 {
298     ALOGE("Q%s: E", __func__);
299     int rc = -1;
300     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
301     if(hardware != NULL){
302         rc = hardware->start_recording(device);
303     }
304     return rc;
305 }
306 
stop_recording(struct camera_device * device)307 void stop_recording(struct camera_device * device)
308 {
309     ALOGE("Q%s: E", __func__);
310     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
311     if(hardware != NULL){
312         hardware->stop_recording(device);
313     }
314 }
315 
recording_enabled(struct camera_device * device)316 int recording_enabled(struct camera_device * device)
317 {
318     ALOGE("Q%s: E", __func__);
319     int rc = -1;
320     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
321     if(hardware != NULL){
322         rc = hardware->recording_enabled(device);
323     }
324     return rc;
325 }
326 
release_recording_frame(struct camera_device * device,const void * opaque)327 void release_recording_frame(struct camera_device * device,
328                 const void *opaque)
329 {
330     CDBG("Q%s: E", __func__);
331     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
332     if(hardware != NULL){
333         hardware->release_recording_frame(device, opaque);
334     }
335 }
336 
auto_focus(struct camera_device * device)337 int auto_focus(struct camera_device * device)
338 {
339     ALOGE("Q%s: E", __func__);
340     int rc = -1;
341     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
342     if(hardware != NULL){
343         rc = hardware->auto_focus(device);
344     }
345     return rc;
346 }
347 
cancel_auto_focus(struct camera_device * device)348 int cancel_auto_focus(struct camera_device * device)
349 {
350     ALOGE("Q%s: E", __func__);
351     int rc = -1;
352     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
353     if(hardware != NULL){
354         rc = hardware->cancel_auto_focus(device);
355     }
356     return rc;
357 }
358 
take_picture(struct camera_device * device)359 int take_picture(struct camera_device * device)
360 {
361     ALOGE("Q%s: E", __func__);
362     int rc = -1;
363     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
364     if(hardware != NULL){
365         rc = hardware->take_picture(device);
366     }
367     return rc;
368 }
369 
cancel_picture(struct camera_device * device)370 int cancel_picture(struct camera_device * device)
371 
372 {
373     ALOGE("Q%s: E", __func__);
374     int rc = -1;
375     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
376     if(hardware != NULL){
377         rc = hardware->cancel_picture(device);
378     }
379     return rc;
380 }
381 
set_parameters(struct camera_device * device,const char * parms)382 int set_parameters(struct camera_device * device, const char *parms)
383 
384 {
385     ALOGE("Q%s: E", __func__);
386     int rc = -1;
387     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
388     if(hardware != NULL && parms){
389         rc = hardware->set_parameters(device, parms);
390   }
391   return rc;
392 }
393 
get_parameters(struct camera_device * device)394 char* get_parameters(struct camera_device * device)
395 {
396     ALOGE("Q%s: E", __func__);
397     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
398     if(hardware != NULL){
399         char *parms = NULL;
400         parms = hardware->get_parameters(device);
401         return parms;
402     }
403     return NULL;
404 }
405 
put_parameters(struct camera_device * device,char * parm)406 void put_parameters(struct camera_device * device, char *parm)
407 
408 {
409     ALOGE("Q%s: E", __func__);
410     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
411     if(hardware != NULL){
412       hardware->put_parameters(device, parm);
413     }
414 }
415 
send_command(struct camera_device * device,int32_t cmd,int32_t arg1,int32_t arg2)416 int send_command(struct camera_device * device,
417             int32_t cmd, int32_t arg1, int32_t arg2)
418 {
419     ALOGE("Q%s: E", __func__);
420     int rc = -1;
421     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
422     if(hardware != NULL){
423         rc = hardware->send_command(device, cmd, arg1, arg2);
424     }
425     return rc;
426 }
427 
release(struct camera_device * device)428 void release(struct camera_device * device)
429 {
430     ALOGE("Q%s: E", __func__);
431     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
432     if(hardware != NULL){
433         camera_hardware_t *camHal = (camera_hardware_t *)device->priv;
434         hardware->release(device);
435         camHal->camera_released = true;
436     }
437 }
438 
dump(struct camera_device * device,int fd)439 int dump(struct camera_device * device, int fd)
440 {
441     ALOGE("Q%s: E", __func__);
442     int rc = -1;
443     QCamera2HardwareInterface *hardware = util_get_Hal_obj(device);
444     if(hardware != NULL){
445         rc = hardware->dump(device, fd);
446     }
447     return rc;
448 }
449 
450 }; // namespace android
451