1 /* Copyright (c) 2012-2016, 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 #ifndef __MM_CAMERA_H__
31 #define __MM_CAMERA_H__
32 
33 // System dependencies
34 #include <poll.h>
35 
36 // Camera dependencies
37 #include "hardware/camera_common.h"
38 #include "cam_semaphore.h"
39 #include "mm_camera_interface.h"
40 #include "mm_camera_shim.h"
41 
42 /**********************************************************************************
43 * Data structure declarations
44 ***********************************************************************************/
45 /* num of callbacks allowed for an event type */
46 #define MM_CAMERA_EVT_ENTRY_MAX 4
47 /* num of data callbacks allowed in a stream obj */
48 #define MM_CAMERA_STREAM_BUF_CB_MAX 4
49 /* num of data poll threads allowed in a channel obj */
50 #define MM_CAMERA_CHANNEL_POLL_THREAD_MAX 1
51 
52 #define MM_CAMERA_DEV_NAME_LEN 32
53 #define MM_CAMERA_DEV_OPEN_TRIES 20
54 #define MM_CAMERA_DEV_OPEN_RETRY_SLEEP 20
55 #define THREAD_NAME_SIZE 15
56 
57 /* Future frame idx, large enough to make sure capture
58 * settings can be applied and small enough to still capture an image */
59 #define MM_CAMERA_MAX_FUTURE_FRAME_WAIT 100
60 #define WAIT_TIMEOUT 10
61 
62 /*For frame sync */
63 #define MAX_OBJS_FOR_FRAME_SYNC   4
64 
65 /* num of supporting camera*/
66 #define MM_CAMERA_MAX_AUX_CAMERA 1
67 
68 #ifndef TRUE
69 #define TRUE 1
70 #endif
71 
72 #ifndef FALSE
73 #define FALSE 0
74 #endif
75 
76 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
77 
78 struct mm_channel;
79 struct mm_stream;
80 struct mm_camera_obj;
81 
82 typedef int64_t nsecs_t;
83 
84 typedef enum
85 {
86     MM_CAMERA_CMD_TYPE_DATA_CB,    /* dataB CMD */
87     MM_CAMERA_CMD_TYPE_EVT_CB,     /* evtCB CMD */
88     MM_CAMERA_CMD_TYPE_EXIT,       /* EXIT */
89     MM_CAMERA_CMD_TYPE_REQ_DATA_CB,/* request data */
90     MM_CAMERA_CMD_TYPE_SUPER_BUF_DATA_CB,    /* superbuf dataB CMD */
91     MM_CAMERA_CMD_TYPE_CONFIG_NOTIFY, /* configure notify mode */
92     MM_CAMERA_CMD_TYPE_START_ZSL, /* start zsl snapshot for channel */
93     MM_CAMERA_CMD_TYPE_STOP_ZSL, /* stop zsl snapshot for channel */
94     MM_CAMERA_CMD_TYPE_FLUSH_QUEUE, /* flush queue */
95     MM_CAMERA_CMD_TYPE_GENERAL,  /* general cmd */
96     MM_CAMERA_CMD_TYPE_MAX
97 } mm_camera_cmdcb_type_t;
98 
99 typedef struct {
100     uint32_t stream_id;
101     uint32_t frame_idx;
102     uint32_t flags;
103     mm_camera_buf_def_t *buf; /* ref to buf */
104 } mm_camera_buf_info_t;
105 
106 typedef enum {
107     MM_CAMERA_GENERIC_CMD_TYPE_AE_BRACKETING,
108     MM_CAMERA_GENERIC_CMD_TYPE_AF_BRACKETING,
109     MM_CAMERA_GENERIC_CMD_TYPE_FLASH_BRACKETING,
110     MM_CAMERA_GENERIC_CMD_TYPE_ZOOM_1X,
111     MM_CAMERA_GENERIC_CMD_TYPE_CAPTURE_SETTING,
112 } mm_camera_generic_cmd_type_t;
113 
114 typedef struct {
115     mm_camera_generic_cmd_type_t type;
116     uint32_t payload[32];
117     union {
118         cam_capture_frame_config_t frame_config;
119     };
120 } mm_camera_generic_cmd_t;
121 
122 typedef struct {
123     uint32_t frame_idx;
124     cam_stream_type_t stream_type;
125 } mm_camera_flush_cmd_t;
126 
127 typedef struct {
128     mm_camera_cmdcb_type_t cmd_type;
129     union {
130         mm_camera_buf_info_t buf;    /* frame buf if dataCB */
131         mm_camera_event_t evt;       /* evt if evtCB */
132         mm_camera_super_buf_t superbuf; /* superbuf if superbuf dataCB*/
133         mm_camera_req_buf_t req_buf; /* num of buf requested */
134         mm_camera_flush_cmd_t flush_cmd; /* frame idx boundary for flush superbuf queue*/
135         mm_camera_super_buf_notify_mode_t notify_mode; /* notification mode */
136         mm_camera_generic_cmd_t gen_cmd;
137     } u;
138 } mm_camera_cmdcb_t;
139 
140 typedef void (*mm_camera_cmd_cb_t)(mm_camera_cmdcb_t * cmd_cb, void* user_data);
141 
142 typedef struct {
143     uint8_t is_active;     /*indicates whether thread is active or not */
144     cam_queue_t cmd_queue; /* cmd queue (queuing dataCB, asyncCB, or exitCMD) */
145     pthread_t cmd_pid;           /* cmd thread ID */
146     cam_semaphore_t cmd_sem;     /* semaphore for cmd thread */
147     cam_semaphore_t sync_sem;     /* semaphore for synchronization with cmd thread */
148     mm_camera_cmd_cb_t cb;       /* cb for cmd */
149     void* user_data;             /* user_data for cb */
150     char threadName[THREAD_NAME_SIZE];
151 } mm_camera_cmd_thread_t;
152 
153 typedef enum {
154     MM_CAMERA_POLL_TYPE_EVT,
155     MM_CAMERA_POLL_TYPE_DATA,
156     MM_CAMERA_POLL_TYPE_MAX
157 } mm_camera_poll_thread_type_t;
158 
159 /* function ptr defined for poll notify CB,
160  * registered at poll thread with poll fd */
161 typedef void (*mm_camera_poll_notify_t)(void *user_data);
162 
163 typedef struct {
164     int32_t fd;
165     mm_camera_poll_notify_t notify_cb;
166     uint32_t handler;
167     void* user_data;
168 } mm_camera_poll_entry_t;
169 
170 typedef struct {
171     mm_camera_poll_thread_type_t poll_type;
172     /* array to store poll fd and cb info
173      * for MM_CAMERA_POLL_TYPE_EVT, only index 0 is valid;
174      * for MM_CAMERA_POLL_TYPE_DATA, depends on valid stream fd */
175     mm_camera_poll_entry_t poll_entries[MAX_STREAM_NUM_IN_BUNDLE];
176     int32_t pfds[2];
177     pthread_t pid;
178     int32_t state;
179     int timeoutms;
180     uint32_t cmd;
181     struct pollfd poll_fds[MAX_STREAM_NUM_IN_BUNDLE + 1];
182     uint8_t num_fds;
183     pthread_mutex_t mutex;
184     pthread_cond_t cond_v;
185     int32_t status;
186     char threadName[THREAD_NAME_SIZE];
187     //void *my_obj;
188 } mm_camera_poll_thread_t;
189 
190 /* mm_stream */
191 typedef enum {
192     MM_STREAM_STATE_NOTUSED = 0,      /* not used */
193     MM_STREAM_STATE_INITED,           /* inited  */
194     MM_STREAM_STATE_ACQUIRED,         /* acquired, fd opened  */
195     MM_STREAM_STATE_CFG,              /* fmt & dim configured */
196     MM_STREAM_STATE_BUFFED,           /* buf allocated */
197     MM_STREAM_STATE_REG,              /* buf regged, stream off */
198     MM_STREAM_STATE_ACTIVE,           /* active */
199     MM_STREAM_STATE_MAX
200 } mm_stream_state_type_t;
201 
202 typedef enum {
203     MM_STREAM_EVT_ACQUIRE,
204     MM_STREAM_EVT_RELEASE,
205     MM_STREAM_EVT_SET_FMT,
206     MM_STREAM_EVT_GET_BUF,
207     MM_STREAM_EVT_PUT_BUF,
208     MM_STREAM_EVT_REG_BUF,
209     MM_STREAM_EVT_UNREG_BUF,
210     MM_STREAM_EVT_START,
211     MM_STREAM_EVT_START_SENSOR_STREAMING,
212     MM_STREAM_EVT_STOP,
213     MM_STREAM_EVT_QBUF,
214     MM_STREAM_EVT_SET_PARM,
215     MM_STREAM_EVT_GET_PARM,
216     MM_STREAM_EVT_DO_ACTION,
217     MM_STREAM_EVT_GET_QUEUED_BUF_COUNT,
218     MM_STREAM_EVT_REG_FRAME_SYNC,
219     MM_STREAM_EVT_TRIGGER_FRAME_SYNC,
220     MM_STREAM_EVT_CANCEL_BUF,
221     MM_STREAM_EVT_MAX
222 } mm_stream_evt_type_t;
223 
224 typedef struct {
225     mm_camera_buf_notify_t cb;
226     void *user_data;
227     /* cb_count = -1: infinite
228      * cb_count > 0: register only for required times */
229     int8_t cb_count;
230     mm_camera_stream_cb_type cb_type;
231 } mm_stream_data_cb_t;
232 
233 typedef struct {
234     /* buf reference count */
235     uint8_t buf_refcnt;
236 
237     /* This flag is to indicate if after allocation,
238      * the corresponding buf needs to qbuf into kernel
239      * (e.g. for preview usecase, display needs to hold two bufs,
240      * so no need to qbuf these two bufs initially) */
241     uint8_t initial_reg_flag;
242 
243     /* indicate if buf is in kernel(1) or client(0) */
244     uint8_t in_kernel;
245     /*indicate if this buffer is mapped to daemon*/
246     int8_t map_status;
247 } mm_stream_buf_status_t;
248 
249 /*Structure definition to carry frame sync queue details*/
250 typedef struct {
251     /*Number of objects to be synced*/
252     uint8_t num_objs;
253     /*Object handle to be synced*/
254     uint32_t bundled_objs[MAX_OBJS_FOR_FRAME_SYNC];
255     /*Queue*/
256     cam_queue_t que;
257 
258     /*queue attributed*/
259     mm_camera_channel_attr_t attr;
260 
261     /*Expected frame for this queue*/
262     uint32_t expected_frame_id;
263 
264     /*Total match count*/
265     uint32_t match_cnt;
266 } mm_frame_sync_queue_t;
267 
268 /*Structure definition to carry frame sync details*/
269 typedef struct {
270     /* flag to indicate if frame sync is active*/
271     uint8_t is_active;
272 
273     /*Frame sync lock. Shared between child and parent stream*/
274     pthread_mutex_t sync_lock;
275 
276     /*Limited number of synced frame request*/
277     mm_camera_req_buf_t req_buf;
278 
279     /*Queue to hold super buffers*/
280     mm_frame_sync_queue_t superbuf_queue;
281 
282     /*Callback registered for synced frame*/
283     mm_camera_buf_notify_t super_buf_notify_cb;
284 
285     /*Client data*/
286     void *user_data;
287 } mm_frame_sync_t;
288 
289 struct mm_stream;
290 typedef struct mm_stream {
291     uint32_t my_hdl; /* local stream id */
292     uint32_t server_stream_id; /* stream id from server */
293     int32_t fd;
294     mm_stream_state_type_t state;
295 
296     /* stream info*/
297     cam_stream_info_t *stream_info;
298 
299     /* padding info */
300     cam_padding_info_t padding_info;
301 
302     /* offset */
303     cam_frame_len_offset_t frame_offset;
304 
305     pthread_mutex_t cmd_lock; /* lock to protect cmd_thread */
306     mm_camera_cmd_thread_t cmd_thread;
307 
308     /* dataCB registered on this stream obj */
309     uint8_t is_cb_active;
310     pthread_mutex_t cb_lock; /* cb lock to protect buf_cb */
311     mm_stream_data_cb_t buf_cb[MM_CAMERA_STREAM_BUF_CB_MAX];
312 
313     /* stream buffer management */
314     pthread_mutex_t buf_lock;
315     uint8_t total_buf_cnt; /*Total number of buffer including slave*/
316     uint8_t buf_num; /* num of buffers for this stream */
317     uint8_t buf_idx; /* starting buffer index */
318 
319     mm_camera_buf_def_t* buf; /* ptr to buf array */
320     mm_stream_buf_status_t buf_status[CAM_MAX_NUM_BUFS_PER_STREAM]; /* ptr to buf status array */
321 
322     uint8_t plane_buf_num; /* num of plane buffers allocated  Used only in Batch mode*/
323     mm_camera_buf_def_t *plane_buf; /*Pointer to plane buffer array Used only in Batch mode */
324     int32_t cur_buf_idx; /* Current container buffer active filling. Used only in Batch mode*/
325     uint8_t cur_bufs_staged; /*Number of plane buf freed by HAL for this usr buf*/
326 
327     /* reference to parent channel_obj */
328     struct mm_channel* ch_obj;
329 
330     uint8_t is_bundled; /* flag if stream is bundled */
331 
332     /* reference to linked channel_obj */
333     struct mm_channel* linked_obj;
334     struct mm_stream * linked_stream; /* original stream */
335     uint8_t is_linked; /* flag if stream is linked */
336 
337     mm_camera_stream_mem_vtbl_t mem_vtbl; /* mem ops tbl */
338     mm_camera_map_unmap_ops_tbl_t map_ops;
339 
340     int8_t queued_buffer_count;
341 
342     /*latest timestamp of this stream frame received & last frameID*/
343     uint32_t prev_frameID;
344     nsecs_t prev_timestamp;
345 
346     /* Need to wait for buffer mapping before stream-on*/
347     pthread_cond_t buf_cond;
348 
349     struct mm_stream *master_str_obj; /*Master stream of this stream*/
350     uint8_t num_s_cnt;
351     struct mm_stream *aux_str_obj[MM_CAMERA_MAX_AUX_CAMERA];  /*aux stream of this stream*/
352     mm_frame_sync_t frame_sync;
353     uint8_t is_res_shared;
354 } mm_stream_t;
355 
356 /* mm_channel */
357 typedef enum {
358     MM_CHANNEL_STATE_NOTUSED = 0,   /* not used */
359     MM_CHANNEL_STATE_STOPPED,       /* stopped */
360     MM_CHANNEL_STATE_ACTIVE,        /* active, at least one stream active */
361     MM_CHANNEL_STATE_PAUSED,        /* paused */
362     MM_CHANNEL_STATE_MAX
363 } mm_channel_state_type_t;
364 
365 typedef enum {
366     MM_CHANNEL_EVT_ADD_STREAM,
367     MM_CHANNEL_EVT_DEL_STREAM,
368     MM_CHANNEL_EVT_LINK_STREAM,
369     MM_CHANNEL_EVT_CONFIG_STREAM,
370     MM_CHANNEL_EVT_GET_BUNDLE_INFO,
371     MM_CHANNEL_EVT_START,
372     MM_CHANNEL_EVT_START_SENSOR_STREAMING,
373     MM_CHANNEL_EVT_STOP,
374     MM_CHANNEL_EVT_PAUSE,
375     MM_CHANNEL_EVT_RESUME,
376     MM_CHANNEL_EVT_REQUEST_SUPER_BUF,
377     MM_CHANNEL_EVT_CANCEL_REQUEST_SUPER_BUF,
378     MM_CHANNEL_EVT_FLUSH_SUPER_BUF_QUEUE,
379     MM_CHANNEL_EVT_CONFIG_NOTIFY_MODE,
380     MM_CHANNEL_EVT_START_ZSL_SNAPSHOT,
381     MM_CHANNEL_EVT_STOP_ZSL_SNAPSHOT,
382     MM_CHANNEL_EVT_MAP_STREAM_BUF,
383     MM_CHANNEL_EVT_UNMAP_STREAM_BUF,
384     MM_CHANNEL_EVT_SET_STREAM_PARM,
385     MM_CHANNEL_EVT_GET_STREAM_PARM,
386     MM_CHANNEL_EVT_DO_ACTION,
387     MM_CHANNEL_EVT_DELETE,
388     MM_CHANNEL_EVT_AF_BRACKETING,
389     MM_CHANNEL_EVT_AE_BRACKETING,
390     MM_CHANNEL_EVT_FLASH_BRACKETING,
391     MM_CHANNEL_EVT_ZOOM_1X,
392     MM_CAMERA_EVT_CAPTURE_SETTING,
393     MM_CHANNEL_EVT_GET_STREAM_QUEUED_BUF_COUNT,
394     MM_CHANNEL_EVT_MAP_STREAM_BUFS,
395     MM_CHANNEL_EVT_REG_STREAM_BUF_CB,
396     MM_CHANNEL_EVT_REG_FRAME_SYNC,
397     MM_CHANNEL_EVT_TRIGGER_FRAME_SYNC,
398 } mm_channel_evt_type_t;
399 
400 typedef struct {
401     uint32_t stream_id;
402     mm_camera_stream_config_t *config;
403 } mm_evt_paylod_config_stream_t;
404 
405 typedef struct {
406     uint32_t stream_id;
407     cam_stream_parm_buffer_t *parms;
408 } mm_evt_paylod_set_get_stream_parms_t;
409 
410 typedef struct {
411     uint32_t stream_id;
412     void *actions;
413 } mm_evt_paylod_do_stream_action_t;
414 
415 typedef struct {
416     uint32_t stream_id;
417     mm_stream_data_cb_t buf_cb;
418 } mm_evt_paylod_reg_stream_buf_cb;
419 
420 typedef struct {
421     uint8_t num_of_bufs;
422     mm_camera_buf_info_t super_buf[MAX_STREAM_NUM_IN_BUNDLE];
423     uint8_t matched;
424     uint8_t expected_frame;
425     uint32_t frame_idx;
426     /* unmatched meta idx needed in case of low priority queue */
427     uint32_t unmatched_meta_idx;
428 } mm_channel_queue_node_t;
429 
430 typedef struct {
431     cam_queue_t que;
432     uint8_t num_streams;
433     /* container for bundled stream handlers */
434     uint32_t bundled_streams[MAX_STREAM_NUM_IN_BUNDLE];
435     mm_camera_channel_attr_t attr;
436     uint32_t expected_frame_id;
437     uint32_t match_cnt;
438     uint32_t expected_frame_id_without_led;
439     uint32_t led_on_start_frame_id;
440     uint32_t led_off_start_frame_id;
441     uint32_t led_on_num_frames;
442     uint32_t once;
443     uint32_t frame_skip_count;
444     uint32_t good_frame_id;
445 } mm_channel_queue_t;
446 
447 typedef struct {
448     uint8_t is_active; /* flag to indicate if bundle is valid */
449     /* queue to store bundled super buffers */
450     uint8_t is_cb_active;
451     mm_channel_queue_t superbuf_queue;
452     mm_camera_buf_notify_t super_buf_notify_cb;
453     void *user_data;
454 } mm_channel_bundle_t;
455 
456 /* Nodes used for frame sync */
457 typedef struct {
458     /* Frame idx */
459     uint32_t frame_idx;
460     /* Frame present for corresponding channel*/
461     uint32_t frame_valid[MAX_NUM_CAMERA_PER_BUNDLE];
462     /* Frame present in all channels*/
463     uint32_t matched;
464 } mm_channel_sync_node_t;
465 
466 /* Frame sync information */
467 typedef struct {
468     /* Number of camera channels that need to be synced*/
469     uint8_t num_cam;
470     /* position of the next node to be updated */
471     uint8_t pos;
472     /* circular node array used to store frame information */
473     mm_channel_sync_node_t node[MM_CAMERA_FRAME_SYNC_NODES];
474     /* Channel corresponding to each camera */
475     struct mm_channel *ch_obj[MAX_NUM_CAMERA_PER_BUNDLE];
476     /* Cb corresponding to each camera */
477     mm_camera_buf_notify_t cb[MAX_NUM_CAMERA_PER_BUNDLE];
478 } mm_channel_frame_sync_info_t;
479 
480 /* Node information for multiple superbuf callbacks
481 *  This can be used to batch nodes before sending to upper layer */
482 typedef struct {
483     /* Number of nodes to be sent*/
484     uint8_t num_nodes;
485     /* queue node information*/
486     mm_channel_queue_node_t *node[MAX_NUM_CAMERA_PER_BUNDLE];
487     /* channel information*/
488     struct mm_channel *ch_obj[MAX_NUM_CAMERA_PER_BUNDLE];
489 } mm_channel_node_info_t;
490 
491 typedef enum {
492     MM_CHANNEL_BRACKETING_STATE_OFF,
493     MM_CHANNEL_BRACKETING_STATE_WAIT_GOOD_FRAME_IDX,
494     MM_CHANNEL_BRACKETING_STATE_ACTIVE,
495 } mm_channel_bracketing_state_t;
496 
497 struct mm_channel;
498 typedef struct mm_channel {
499     uint32_t my_hdl;
500     mm_channel_state_type_t state;
501     pthread_mutex_t ch_lock; /* channel lock */
502 
503     /* stream bundle info in the channel */
504     mm_channel_bundle_t bundle;
505 
506     /* num of pending suferbuffers */
507     uint32_t pending_cnt;
508     uint32_t pending_retro_cnt;
509     mm_camera_req_buf_type_t req_type;
510     uint32_t bWaitForPrepSnapshotDone;
511     uint32_t unLockAEC;
512     /* num of pending suferbuffers */
513     uint8_t stopZslSnapshot;
514 
515     /* cmd thread for superbuffer dataCB and async stop*/
516     mm_camera_cmd_thread_t cmd_thread;
517 
518     /* cb thread for sending data cb */
519     mm_camera_cmd_thread_t cb_thread;
520 
521     /* data poll thread
522     * currently one data poll thread per channel
523     * could extended to support one data poll thread per stream in the channel */
524     mm_camera_poll_thread_t poll_thread[MM_CAMERA_CHANNEL_POLL_THREAD_MAX];
525 
526     /* container for all streams in channel */
527     mm_stream_t streams[MAX_STREAM_NUM_IN_BUNDLE];
528 
529     /* reference to parent cam_obj */
530     struct mm_camera_obj* cam_obj;
531 
532     /* manual zsl snapshot control */
533     uint8_t manualZSLSnapshot;
534 
535     /* control for zsl led */
536     uint8_t startZSlSnapshotCalled;
537     uint8_t needLEDFlash;
538     mm_channel_bracketing_state_t bracketingState;
539     uint8_t isFlashBracketingEnabled;
540     uint8_t isZoom1xFrameRequested;
541     uint32_t burstSnapNum;
542     char threadName[THREAD_NAME_SIZE];
543 
544     /*Buffer diverted*/
545     uint8_t diverted_frame_id;
546     uint32_t sessionid;
547 
548     /*Frame capture configaration*/
549     uint8_t isConfigCapture;
550     uint8_t cur_capture_idx;
551     uint32_t capture_frame_id[MAX_CAPTURE_BATCH_NUM];
552     cam_capture_frame_config_t frameConfig;
553     uint8_t needLowLightZSL;
554 
555     /*Capture based on index*/
556     uint8_t cur_req_idx;
557     uint8_t frame_req_cnt;
558     uint32_t requested_frame_id[MAX_CAPTURE_BATCH_NUM];
559 
560     /*For channel frame sync*/
561     mm_frame_sync_t frame_sync;
562 
563     /*Multiple camera's*/
564     struct mm_channel *master_ch_obj; /*Master channel of this channel*/
565     uint8_t num_s_cnt;
566     struct mm_channel *aux_ch_obj[MM_CAMERA_MAX_AUX_CAMERA];  /*Slave channel of this channel*/
567 } mm_channel_t;
568 
569 typedef struct {
570     mm_channel_t *ch;
571     uint32_t stream_id;
572 } mm_camera_stream_link_t;
573 
574 /* struct to store information about pp cookie*/
575 typedef struct {
576     uint32_t cam_hdl;
577     uint32_t ch_hdl;
578     uint32_t stream_hdl;
579     mm_channel_queue_node_t *super_buf;
580 } mm_channel_pp_info_t;
581 
582 /* mm_camera */
583 typedef struct {
584     mm_camera_event_notify_t evt_cb;
585     void *user_data;
586 } mm_camera_evt_entry_t;
587 
588 typedef struct {
589     mm_camera_evt_entry_t evt[MM_CAMERA_EVT_ENTRY_MAX];
590     /* reg_count <=0: infinite
591      * reg_count > 0: register only for required times */
592     int reg_count;
593 } mm_camera_evt_obj_t;
594 
595 struct mm_camera_obj;
596 typedef struct mm_camera_obj {
597     uint32_t my_hdl;
598     int ref_count;
599     int32_t ctrl_fd;
600     int32_t ds_fd; /* domain socket fd */
601     pthread_mutex_t cam_lock;
602     pthread_mutex_t cb_lock; /* lock for evt cb */
603     mm_channel_t ch[MM_CAMERA_CHANNEL_MAX];
604     mm_camera_evt_obj_t evt;
605     mm_camera_poll_thread_t evt_poll_thread; /* evt poll thread */
606     mm_camera_cmd_thread_t evt_thread;       /* thread for evt CB */
607     mm_camera_vtbl_t vtbl;
608 
609     pthread_mutex_t evt_lock;
610     pthread_cond_t evt_cond;
611     mm_camera_event_t evt_rcvd;
612 
613     pthread_mutex_t msg_lock; /* lock for sending msg through socket */
614     uint32_t sessionid; /* Camera server session id */
615 
616     uint8_t my_num;          /*this camera position in multi mode*/
617     pthread_mutex_t muxer_lock;
618     struct mm_camera_obj *master_cam_obj; /*Master Camera of this camera*/
619     uint8_t num_s_cnt;
620     struct mm_camera_obj *aux_cam_obj[MM_CAMERA_MAX_AUX_CAMERA];  /*Slave Camera of this camera*/
621 } mm_camera_obj_t;
622 
623 typedef struct {
624     int8_t num_cam;
625     mm_camera_shim_ops_t cam_shim_ops;
626     int8_t num_cam_to_expose;
627     char video_dev_name[MM_CAMERA_MAX_NUM_SENSORS][MM_CAMERA_DEV_NAME_LEN];
628     mm_camera_obj_t *cam_obj[MM_CAMERA_MAX_NUM_SENSORS];
629     struct camera_info info[MM_CAMERA_MAX_NUM_SENSORS];
630     cam_sync_type_t cam_type[MM_CAMERA_MAX_NUM_SENSORS];
631     cam_sync_mode_t cam_mode[MM_CAMERA_MAX_NUM_SENSORS];
632     uint8_t is_yuv[MM_CAMERA_MAX_NUM_SENSORS]; // 1=CAM_SENSOR_YUV, 0=CAM_SENSOR_RAW
633     uint32_t cam_index[MM_CAMERA_MAX_NUM_SENSORS]; //Actual cam index are stored in bits
634 } mm_camera_ctrl_t;
635 
636 typedef enum {
637     mm_camera_async_call,
638     mm_camera_sync_call
639 } mm_camera_call_type_t;
640 
641 /*internal structure for registring frame sync*/
642 typedef struct {
643     mm_camera_obj_t *a_cam_obj;
644     uint32_t a_ch_id;
645     uint32_t a_stream_id;
646     uint8_t is_active;
647     mm_camera_channel_attr_t attr;
648     mm_camera_buf_notify_t buf_cb;
649     uint8_t is_res_shared;
650     void *userdata;
651 } mm_camera_frame_sync_t;
652 
653 /*Payload for reg frame sync event in MCI*/
654 typedef struct {
655     uint32_t stream_id;
656     mm_channel_t *a_ch_obj;
657     mm_stream_t *a_str_obj;
658     mm_camera_frame_sync_t *sync_attr;
659 } mm_evt_paylod_reg_frame_sync;
660 
661 /*Payload to handle frame sync */
662 typedef struct {
663     uint32_t stream_id;
664     mm_camera_cb_req_type type;
665 } mm_evt_paylod_trigger_frame_sync;
666 
667 
668 /**********************************************************************************
669 * external function declare
670 ***********************************************************************************/
671 /* utility functions */
672 /* set int32_t value */
673 extern int32_t mm_camera_util_s_ctrl(mm_camera_obj_t *my_obj,
674         int stream_id, int32_t fd, uint32_t id, int32_t *value);
675 
676 /* get int32_t value */
677 extern int32_t mm_camera_util_g_ctrl(mm_camera_obj_t *my_obj,
678         int stream_id, int32_t fd, uint32_t id, int32_t *value);
679 
680 /* send msg throught domain socket for fd mapping */
681 extern int32_t mm_camera_util_sendmsg(mm_camera_obj_t *my_obj,
682                                       void *msg,
683                                       size_t buf_size,
684                                       int sendfd);
685 
686 /* send msg through domain socket for bundled fd mapping */
687 extern int32_t mm_camera_util_bundled_sendmsg(mm_camera_obj_t *my_obj,
688                                               void *msg,
689                                               size_t buf_size,
690                                               int sendfds[CAM_MAX_NUM_BUFS_PER_STREAM],
691                                               int numfds);
692 
693 /* Check if hardware target is A family */
694 uint8_t mm_camera_util_chip_is_a_family(void);
695 
696 /* mm-camera */
697 extern int32_t mm_camera_open(mm_camera_obj_t *my_obj);
698 extern int32_t mm_camera_close(mm_camera_obj_t *my_obj);
699 extern int32_t mm_camera_register_event_notify(mm_camera_obj_t *my_obj,
700                                                mm_camera_event_notify_t evt_cb,
701                                                void * user_data);
702 extern int32_t mm_camera_qbuf(mm_camera_obj_t *my_obj,
703                               uint32_t ch_id,
704                               mm_camera_buf_def_t *buf);
705 extern int32_t mm_camera_cancel_buf(mm_camera_obj_t *my_obj,
706                        uint32_t ch_id,
707                        uint32_t stream_id,
708                        uint32_t buf_idx);
709 extern int32_t mm_camera_get_queued_buf_count(mm_camera_obj_t *my_obj,
710         uint32_t ch_id, uint32_t stream_id);
711 extern int32_t mm_camera_query_capability(mm_camera_obj_t *my_obj);
712 extern int32_t mm_camera_set_parms(mm_camera_obj_t *my_obj,
713                                    parm_buffer_t *parms);
714 extern int32_t mm_camera_get_parms(mm_camera_obj_t *my_obj,
715                                    parm_buffer_t *parms);
716 extern int32_t mm_camera_map_buf(mm_camera_obj_t *my_obj,
717                                  uint8_t buf_type,
718                                  int fd,
719                                  size_t size,
720                                  void *buffer);
721 extern int32_t mm_camera_map_bufs(mm_camera_obj_t *my_obj,
722                                   const cam_buf_map_type_list *buf_map_list);
723 extern int32_t mm_camera_unmap_buf(mm_camera_obj_t *my_obj,
724                                    uint8_t buf_type);
725 extern int32_t mm_camera_do_auto_focus(mm_camera_obj_t *my_obj);
726 extern int32_t mm_camera_cancel_auto_focus(mm_camera_obj_t *my_obj);
727 extern int32_t mm_camera_prepare_snapshot(mm_camera_obj_t *my_obj,
728                                           int32_t do_af_flag);
729 extern int32_t mm_camera_start_zsl_snapshot(mm_camera_obj_t *my_obj);
730 extern int32_t mm_camera_stop_zsl_snapshot(mm_camera_obj_t *my_obj);
731 extern int32_t mm_camera_flush(mm_camera_obj_t *my_obj);
732 extern int32_t mm_camera_start_zsl_snapshot_ch(mm_camera_obj_t *my_obj,
733         uint32_t ch_id);
734 extern int32_t mm_camera_stop_zsl_snapshot_ch(mm_camera_obj_t *my_obj,
735         uint32_t ch_id);
736 extern uint32_t mm_camera_add_channel(mm_camera_obj_t *my_obj,
737                                       mm_camera_channel_attr_t *attr,
738                                       mm_camera_buf_notify_t channel_cb,
739                                       void *userdata);
740 extern int32_t mm_camera_del_channel(mm_camera_obj_t *my_obj,
741                                      uint32_t ch_id);
742 extern int32_t mm_camera_get_bundle_info(mm_camera_obj_t *my_obj,
743                                          uint32_t ch_id,
744                                          cam_bundle_config_t *bundle_info);
745 extern uint32_t mm_camera_add_stream(mm_camera_obj_t *my_obj,
746                                      uint32_t ch_id);
747 extern int32_t mm_camera_del_stream(mm_camera_obj_t *my_obj,
748                                     uint32_t ch_id,
749                                     uint32_t stream_id);
750 extern uint32_t mm_camera_link_stream(mm_camera_obj_t *my_obj,
751         uint32_t ch_id,
752         uint32_t stream_id,
753         uint32_t linked_ch_id);
754 
755 extern int32_t mm_camera_reg_stream_buf_cb(mm_camera_obj_t *my_obj,
756         uint32_t ch_id, uint32_t stream_id, mm_camera_buf_notify_t buf_cb,
757         mm_camera_stream_cb_type cb_type, void *userdata);
758 extern int32_t mm_camera_config_stream(mm_camera_obj_t *my_obj,
759                                        uint32_t ch_id,
760                                        uint32_t stream_id,
761                                        mm_camera_stream_config_t *config);
762 extern int32_t mm_camera_start_channel(mm_camera_obj_t *my_obj,
763                                        uint32_t ch_id);
764 extern int32_t mm_camera_start_sensor_stream_on(mm_camera_obj_t *my_obj,
765                                        uint32_t ch_id);
766 extern int32_t mm_camera_stop_channel(mm_camera_obj_t *my_obj,
767                                       uint32_t ch_id, bool stop_immediately);
768 extern int32_t mm_camera_request_super_buf(mm_camera_obj_t *my_obj,
769         uint32_t ch_id, mm_camera_req_buf_t *buf);
770 extern int32_t mm_camera_cancel_super_buf_request(mm_camera_obj_t *my_obj,
771                                                   uint32_t ch_id);
772 extern int32_t mm_camera_flush_super_buf_queue(mm_camera_obj_t *my_obj,
773                                                uint32_t ch_id,
774                                                uint32_t frame_idx);
775 extern int32_t mm_camera_config_channel_notify(mm_camera_obj_t *my_obj,
776                                                uint32_t ch_id,
777                                                mm_camera_super_buf_notify_mode_t notify_mode);
778 extern int32_t mm_camera_set_stream_parms(mm_camera_obj_t *my_obj,
779                                           uint32_t ch_id,
780                                           uint32_t s_id,
781                                           cam_stream_parm_buffer_t *parms);
782 extern int32_t mm_camera_get_stream_parms(mm_camera_obj_t *my_obj,
783                                           uint32_t ch_id,
784                                           uint32_t s_id,
785                                           cam_stream_parm_buffer_t *parms);
786 extern int32_t mm_camera_register_event_notify_internal(mm_camera_obj_t *my_obj,
787                                                         mm_camera_event_notify_t evt_cb,
788                                                         void * user_data);
789 extern int32_t mm_camera_map_stream_buf(mm_camera_obj_t *my_obj,
790                                         uint32_t ch_id,
791                                         uint32_t stream_id,
792                                         uint8_t buf_type,
793                                         uint32_t buf_idx,
794                                         int32_t plane_idx,
795                                         int fd,
796                                         size_t size,
797                                         void *buffer);
798 extern int32_t mm_camera_map_stream_bufs(mm_camera_obj_t *my_obj,
799                                          uint32_t ch_id,
800                                          const cam_buf_map_type_list *buf_map_list);
801 extern int32_t mm_camera_unmap_stream_buf(mm_camera_obj_t *my_obj,
802                                           uint32_t ch_id,
803                                           uint32_t stream_id,
804                                           uint8_t buf_type,
805                                           uint32_t buf_idx,
806                                           int32_t plane_idx);
807 extern int32_t mm_camera_do_stream_action(mm_camera_obj_t *my_obj,
808                                           uint32_t ch_id,
809                                           uint32_t stream_id,
810                                           void *actions);
811 extern int32_t mm_camera_get_session_id(mm_camera_obj_t *my_obj,
812                                         uint32_t* sessionid);
813 extern int32_t mm_camera_set_dual_cam_cmd(mm_camera_obj_t *my_obj);
814 extern int32_t mm_camera_reg_frame_sync(mm_camera_obj_t *my_obj,
815         uint32_t ch_id, uint32_t stream_id,
816         mm_camera_frame_sync_t *sync_attr);
817 extern int32_t mm_camera_handle_frame_sync_cb(mm_camera_obj_t *my_obj,
818         uint32_t ch_id, uint32_t stream_id, mm_camera_cb_req_type req_type);
819 
820 /* mm_channel */
821 extern int32_t mm_channel_fsm_fn(mm_channel_t *my_obj,
822                                  mm_channel_evt_type_t evt,
823                                  void * in_val,
824                                  void * out_val);
825 extern int32_t mm_channel_init(mm_channel_t *my_obj,
826                                mm_camera_channel_attr_t *attr,
827                                mm_camera_buf_notify_t channel_cb,
828                                void *userdata);
829 /* qbuf is a special case that not going through state machine.
830  * This is to avoid deadlock when trying to aquire ch_lock,
831  * from the context of dataCB, but async stop is holding ch_lock */
832 extern int32_t mm_channel_qbuf(mm_channel_t *my_obj,
833                                mm_camera_buf_def_t *buf);
834 extern int32_t mm_channel_cancel_buf(mm_channel_t *my_obj,
835                         uint32_t stream_id, uint32_t buf_idx);
836 /* mm_stream */
837 extern int32_t mm_stream_fsm_fn(mm_stream_t *my_obj,
838                                 mm_stream_evt_type_t evt,
839                                 void * in_val,
840                                 void * out_val);
841 /* Function to register special callback for stream buffer*/
842 extern int32_t mm_stream_reg_buf_cb(mm_stream_t *my_obj,
843         mm_stream_data_cb_t val);
844 extern int32_t mm_stream_map_buf(mm_stream_t *my_obj,
845                                  uint8_t buf_type,
846                                  uint32_t frame_idx,
847                                  int32_t plane_idx,
848                                  int fd,
849                                  size_t size,
850                                  void *buffer);
851 extern int32_t mm_stream_map_bufs(mm_stream_t *my_obj,
852                                   const cam_buf_map_type_list *buf_map_list);
853 extern int32_t mm_stream_unmap_buf(mm_stream_t *my_obj,
854                                    uint8_t buf_type,
855                                    uint32_t frame_idx,
856                                    int32_t plane_idx);
857 
858 /* utiltity fucntion declared in mm-camera-inteface2.c
859  * and need be used by mm-camera and below*/
860 uint32_t mm_camera_util_generate_handler(uint8_t index);
861 const char *mm_camera_util_get_dev_name(uint32_t cam_handler);
862 uint8_t mm_camera_util_get_index_by_handler(uint32_t handler);
863 
864 /* poll/cmd thread functions */
865 extern int32_t mm_camera_poll_thread_launch(
866                                 mm_camera_poll_thread_t * poll_cb,
867                                 mm_camera_poll_thread_type_t poll_type);
868 extern int32_t mm_camera_poll_thread_release(mm_camera_poll_thread_t *poll_cb);
869 extern int32_t mm_camera_poll_thread_add_poll_fd(
870         mm_camera_poll_thread_t * poll_cb, uint8_t idx, uint32_t handler,
871         int32_t fd, mm_camera_poll_notify_t nofity_cb,
872         void *userdata, mm_camera_call_type_t);
873 extern int32_t mm_camera_poll_thread_del_poll_fd(
874         mm_camera_poll_thread_t * poll_cb, uint8_t idx, uint32_t handler,
875         mm_camera_call_type_t);
876 extern int32_t mm_camera_poll_thread_commit_updates(
877         mm_camera_poll_thread_t * poll_cb);
878 extern int32_t mm_camera_cmd_thread_launch(
879                                 mm_camera_cmd_thread_t * cmd_thread,
880                                 mm_camera_cmd_cb_t cb,
881                                 void* user_data);
882 extern int32_t mm_camera_cmd_thread_name(const char* name);
883 extern int32_t mm_camera_cmd_thread_release(mm_camera_cmd_thread_t * cmd_thread);
884 
885 extern int32_t mm_camera_channel_advanced_capture(mm_camera_obj_t *my_obj,
886         uint32_t ch_id, mm_camera_advanced_capture_t type,
887         uint32_t trigger, void *in_value);
888 int32_t mm_camera_enqueue_evt(mm_camera_obj_t *my_obj,
889                               mm_camera_event_t *event);
890 int32_t mm_camera_load_shim_lib();
891 cam_shim_packet_t *mm_camera_create_shim_cmd_packet(cam_shim_cmd_type type,
892         uint32_t sessionID, void *data);
893 int32_t mm_camera_destroy_shim_cmd_packet(cam_shim_packet_t *cmd);
894 int32_t mm_camera_module_event_handler(
895         uint32_t session_id, cam_event_t *event);
896 cam_status_t mm_camera_module_open_session(int sessionid,
897         int (*event_cb)(uint32_t sessionid, cam_event_t *event));
898 int32_t mm_camera_module_close_session(int session);
899 int32_t mm_camera_module_send_cmd(cam_shim_packet_t *event);
900 
901 #endif /* __MM_CAMERA_H__ */
902