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_INTERFACE_H__
31 #define __MM_CAMERA_INTERFACE_H__
32 
33 // System dependencies
34 #include <media/msmb_camera.h>
35 #include <stdbool.h>
36 
37 // Camera dependencies
38 #include "cam_intf.h"
39 #include "cam_queue.h"
40 
41 #define MM_CAMERA_MAX_NUM_SENSORS MSM_MAX_CAMERA_SENSORS
42 #define MM_CAMERA_MAX_NUM_FRAMES CAM_MAX_NUM_BUFS_PER_STREAM
43 
44 /* num of channels allowed in a camera obj */
45 #define MM_CAMERA_CHANNEL_MAX 16
46 
47 #define PAD_TO_SIZE(size, padding) \
48         ((size + (typeof(size))(padding - 1)) & \
49         (typeof(size))(~(padding - 1)))
50 
51 #define CEIL_DIVISION(n, d) ((n+d-1)/d)
52 
53 /*Bit shift to reach next camera in camera handle*/
54 #define MM_CAMERA_HANDLE_SHIFT_MASK       16
55 #define MM_CAMERA_HANDLE_BIT_MASK         0x0000ffff
56 
57 typedef enum {
58     MM_CAMERA_TYPE_MAIN       = CAM_TYPE_MAIN,
59     MM_CAMERA_TYPE_AUX        = CAM_TYPE_AUX,
60 } mm_camera_obj_type;
61 
62 #define MM_CAMERA_DUAL_CAM (MM_CAMERA_TYPE_MAIN | MM_CAMERA_TYPE_AUX)
63 #define MM_CAMERA_MAX_CAM_CNT 2
64 
65 /** CAM_DUMP_TO_FILE:
66  *  @filename: file name
67  *  @name:filename
68  *  @index: index of the file
69  *  @extn: file extension
70  *  @p_addr: address of the buffer
71  *  @len: buffer length
72  *
73  *  dump the image to the file
74  **/
75 #define CAM_DUMP_TO_FILE(path, name, index, extn, p_addr, len) ({ \
76   size_t rc = 0; \
77   char filename[FILENAME_MAX]; \
78   if (index >= 0) \
79     snprintf(filename, FILENAME_MAX, "%s/%s%d.%s", path, name, index, extn); \
80   else \
81     snprintf(filename, FILENAME_MAX, "%s/%s.%s", path, name, extn); \
82   FILE *fp = fopen(filename, "w+"); \
83   if (fp) { \
84     rc = fwrite(p_addr, 1, len, fp); \
85     LOGE("written size %d", len); \
86     fclose(fp); \
87   } else { \
88     LOGE("open %s failed", filename); \
89   } \
90 })
91 
92 /* Declaring Buffer structure */
93 struct mm_camera_buf_def;
94 
95 /** mm_camera_plane_def_t : structure for frame plane info
96 *    @num_planes : num of planes for the frame buffer, to be
97 *               filled during mem allocation
98 *    @planes : plane info for the frame buffer, to be filled
99 *               during mem allocation
100 **/
101 typedef struct {
102     int8_t num_planes;
103     struct v4l2_plane planes[VIDEO_MAX_PLANES];
104 } mm_camera_plane_buf_def_t;
105 
106 /** mm_camera_user_buf_def_t : structure for frame plane info
107 *    @num_buffers : num of buffers in this user defined structure
108 *    @bufs_used : actual number of buffer filled
109 *    @buf_in_use : flag to notify buffer usage status.
110 *    @plane_buf : Plane buffer array pointer.
111 **/
112 typedef struct {
113     uint8_t num_buffers;
114     uint8_t bufs_used;     /*Num of Buffer filled by Kernel*/
115     uint8_t buf_in_use;  /* Container buffer is freed to fill*/
116     int32_t buf_idx[MSM_CAMERA_MAX_USER_BUFF_CNT];
117     struct mm_camera_buf_def *plane_buf;
118 } mm_camera_user_buf_def_t;
119 
120 /** mm_camera_buf_def_t: structure for stream frame buf
121 *    @stream_id : stream handler to uniquely identify a stream
122 *               object
123 *    @buf_idx : index of the buf within the stream bufs, to be
124 *               filled during mem allocation
125 *    @timespec_ts : time stamp, to be filled when DQBUF is
126 *                 called
127 *    @frame_idx : frame sequence num, to be filled when DQBUF
128 *    @plane_buf  : Frame plane definition
129 *    @fd : file descriptor of the frame buffer, to be filled
130 *        during mem allocation
131 *    @buffer : pointer to the frame buffer, to be filled during
132 *            mem allocation
133 *    @frame_len : length of the whole frame, to be filled during
134 *               mem allocation
135 *    @mem_info : user specific pointer to additional mem info
136 *    @flags:  v4l2_buffer flags, used to report error in data buffers
137 *    @cache_flags: Stores cache related read/write flags
138 **/
139 typedef struct mm_camera_buf_def {
140     uint32_t stream_id;
141     cam_stream_type_t stream_type;
142     cam_stream_buf_type buf_type;
143     uint32_t buf_idx;
144     uint8_t is_uv_subsampled;
145     struct timespec ts;
146     uint32_t frame_idx;
147     union {
148         mm_camera_plane_buf_def_t planes_buf;
149         mm_camera_user_buf_def_t user_buf;
150     };
151     int fd;
152     void *buffer;
153     size_t frame_len;
154     void *mem_info;
155     uint32_t flags;
156     uint32_t cache_flags;
157 } mm_camera_buf_def_t;
158 
159 /** mm_camera_super_buf_t: super buf structure for bundled
160 *   stream frames
161 *    @camera_handle : camera handler to uniquely identify
162 *              a camera object
163 *    @ch_id : channel handler to uniquely ideentify a channel
164 *           object
165 *    @num_bufs : number of buffers in the super buf, should not
166 *              exceeds MAX_STREAM_NUM_IN_BUNDLE
167 *    @bufs : array of buffers in the bundle
168 **/
169 typedef struct {
170     uint32_t camera_handle;
171     uint32_t ch_id;
172     uint32_t num_bufs;
173     uint8_t bUnlockAEC;
174     uint8_t bReadyForPrepareSnapshot;
175     mm_camera_buf_def_t* bufs[MAX_STREAM_NUM_IN_BUNDLE];
176 } mm_camera_super_buf_t;
177 
178 /** mm_camera_req_buf_type_t
179 * Request type for super buf from channel
180 **/
181 typedef enum {
182     MM_CAMERA_REQ_SUPER_BUF,
183     MM_CAMERA_REQ_FRAME_SYNC_BUF
184 } mm_camera_req_buf_type_t;
185 
186 /** mm_camera_req_buf_t: Attributes for super buf request
187 *
188 *    @type : type of super buf requested
189 *    @num_buf_requested : num of super bufs requested
190 *    @num_retro_buf_requested : number of retro bufs requested
191 *    @primary_only : specifies if only primary camera frame for a dual
192 *     camera is requested
193 **/
194 typedef struct {
195     mm_camera_req_buf_type_t type;
196     uint32_t num_buf_requested;
197     uint32_t num_retro_buf_requested;
198     uint8_t cam_num;    //Frame from which camera
199     uint32_t frame_idx; //Client can request frameId to pick from ZSL queue
200 } mm_camera_req_buf_t;
201 
202 typedef cam_event_t mm_camera_event_t;
203 
204 /** mm_camera_event_notify_t: function definition for event
205 *   notify handling
206 *    @camera_handle : camera handler
207 *    @evt : pointer to an event struct
208 *    @user_data: user data pointer
209 **/
210 typedef void (*mm_camera_event_notify_t)(uint32_t camera_handle,
211                                          mm_camera_event_t *evt,
212                                          void *user_data);
213 
214 /** mm_camera_buf_notify_t: function definition for frame notify
215 *   handling
216 *    @mm_camera_super_buf_t : received frame buffers
217 *    @user_data: user data pointer
218 **/
219 typedef void (*mm_camera_buf_notify_t) (mm_camera_super_buf_t *bufs,
220                                         void *user_data);
221 
222 /** map_stream_buf_op_t: function definition for operation of
223 *   mapping stream buffers via domain socket
224 *    @frame_idx : buffer index within stream buffers
225 *    @plane_idx    : plane index. If all planes share the same
226 *                   fd, plane_idx = -1; otherwise, plean_idx is
227 *                   the index to plane (0..num_of_planes)
228 *    @fd : file descriptor of the stream buffer
229 *    @size: size of the stream buffer
230 *    @buffer: Pointer to buffer to register
231 *    @userdata : user data pointer
232 **/
233 typedef int32_t (*map_stream_buf_op_t) (uint32_t frame_idx,
234                                         int32_t plane_idx,
235                                         int fd,
236                                         size_t size,
237                                         void *buffer,
238                                         cam_mapping_buf_type type,
239                                         void *userdata);
240 
241 typedef int32_t (*map_stream_bufs_op_t) (const cam_buf_map_type_list *buf_map_list,
242                                          void *userdata);
243 
244 /** unmap_stream_buf_op_t: function definition for operation of
245 *                          unmapping stream buffers via domain
246 *                          socket
247 *    @frame_idx : buffer index within stream buffers
248 *    @plane_idx : plane index. If all planes share the same
249 *                 fd, plane_idx = -1; otherwise, plean_idx is
250 *                 the index to plane (0..num_of_planes)
251 *    @userdata : user data pointer
252 **/
253 typedef int32_t (*unmap_stream_buf_op_t) (uint32_t frame_idx,
254                                           int32_t plane_idx,
255                                           cam_mapping_buf_type type,
256                                           void *userdata);
257 
258 /** mm_camera_map_unmap_ops_tbl_t: virtual table
259 *                      for mapping/unmapping stream buffers via
260 *                      domain socket
261 *    @map_ops : operation for mapping
262 *    @unmap_ops : operation for unmapping
263 *    @userdata: user data pointer
264 **/
265 typedef struct {
266     map_stream_buf_op_t map_ops;
267     map_stream_bufs_op_t bundled_map_ops;
268     unmap_stream_buf_op_t unmap_ops;
269     void *userdata;
270 } mm_camera_map_unmap_ops_tbl_t;
271 
272 /** mm_camera_stream_mem_vtbl_t: virtual table for stream
273 *                      memory allocation and deallocation
274 *    @get_bufs : function definition for allocating
275 *                stream buffers
276 *    @put_bufs : function definition for deallocating
277 *                stream buffers
278 *    @user_data: user data pointer
279 **/
280 typedef struct {
281   void *user_data;
282   int32_t (*set_config_ops) (mm_camera_map_unmap_ops_tbl_t *ops_tbl,
283           void *user_data);
284   int32_t (*get_bufs) (cam_frame_len_offset_t *offset,
285                        uint8_t *num_bufs,
286                        uint8_t **initial_reg_flag,
287                        mm_camera_buf_def_t **bufs,
288                        mm_camera_map_unmap_ops_tbl_t *ops_tbl,
289                        void *user_data);
290   int32_t (*put_bufs) (mm_camera_map_unmap_ops_tbl_t *ops_tbl,
291                        void *user_data);
292   int32_t (*invalidate_buf)(uint32_t index, void *user_data);
293   int32_t (*clean_invalidate_buf)(uint32_t index, void *user_data);
294   int32_t (*clean_buf)(uint32_t index, void *user_data);
295 } mm_camera_stream_mem_vtbl_t;
296 
297 /** mm_camera_stream_config_t: structure for stream
298 *                              configuration
299 *    @stream_info : pointer to a stream info structure
300 *    @padding_info: padding info obtained from querycapability
301 *    @mem_tbl : memory operation table for
302 *              allocating/deallocating stream buffers
303 *    @stream_cb_sync : SYNC callback handling stream frame notify
304 *    @stream_cb : ASYNC callback handling stream frame notify
305 *    @userdata : user data pointer
306 **/
307 typedef struct {
308     cam_stream_info_t *stream_info;
309     cam_padding_info_t padding_info;
310     mm_camera_stream_mem_vtbl_t mem_vtbl;
311     mm_camera_buf_notify_t stream_cb_sync;
312     mm_camera_buf_notify_t stream_cb;
313     void *userdata;
314 } mm_camera_stream_config_t;
315 
316 /** mm_camera_super_buf_notify_mode_t: enum for super uffer
317 *                                      notification mode
318 *    @MM_CAMERA_SUPER_BUF_NOTIFY_BURST :
319 *       ZSL use case: get burst of frames
320 *    @MM_CAMERA_SUPER_BUF_NOTIFY_CONTINUOUS :
321 *       get continuous frames: when the super buf is ready
322 *       dispatch it to HAL
323 **/
324 typedef enum {
325     MM_CAMERA_SUPER_BUF_NOTIFY_BURST = 0,
326     MM_CAMERA_SUPER_BUF_NOTIFY_CONTINUOUS,
327     MM_CAMERA_SUPER_BUF_NOTIFY_MAX
328 } mm_camera_super_buf_notify_mode_t;
329 
330 /** mm_camera_super_buf_priority_t: enum for super buffer
331 *                                   matching priority
332 *    @MM_CAMERA_SUPER_BUF_PRIORITY_NORMAL :
333 *       Save the frame no matter focused or not. Currently only
334 *       this type is supported.
335 *    @MM_CAMERA_SUPER_BUF_PRIORITY_FOCUS :
336 *       only queue the frame that is focused. Will enable meta
337 *       data header to carry focus info
338 *    @MM_CAMERA_SUPER_BUF_PRIORITY_EXPOSURE_BRACKETING :
339 *       after shutter, only queue matched exposure index
340 **/
341 typedef enum {
342     MM_CAMERA_SUPER_BUF_PRIORITY_NORMAL = 0,
343     MM_CAMERA_SUPER_BUF_PRIORITY_FOCUS,
344     MM_CAMERA_SUPER_BUF_PRIORITY_EXPOSURE_BRACKETING,
345     MM_CAMERA_SUPER_BUF_PRIORITY_LOW,/* Bundled metadata frame may not match*/
346     MM_CAMERA_SUPER_BUF_PRIORITY_MAX
347 } mm_camera_super_buf_priority_t;
348 
349 /** mm_camera_advanced_capture_t: enum for advanced capture type.
350 *    @MM_CAMERA_AF_BRACKETING :
351 *       to enable AF Bracketig.
352 *    @MM_CAMERA_AE_BRACKETING :
353 *       to enable AF Bracketing.
354 *    @MM_CAMERA_FLASH_BRACKETING :
355 *       to enable Flash Bracketing.
356 *    @MM_CAMERA_ZOOM_1X :
357 *       to enable zoom 1x capture request
358 **/
359 typedef enum {
360    MM_CAMERA_AF_BRACKETING = 0,
361    MM_CAMERA_AE_BRACKETING,
362    MM_CAMERA_FLASH_BRACKETING,
363    MM_CAMERA_ZOOM_1X,
364    MM_CAMERA_FRAME_CAPTURE,
365 } mm_camera_advanced_capture_t;
366 
367 /** mm_camera_stream_cb_type: enum for stream buffer callback type.
368 *    @MM_CAMERA_STREAM_CB_TYPE_ASYNC :
369 *       callback is async type. buffer process done in client thread context
370 *    @MM_CAMERA_STREAM_CB_TYPE_SYNC :
371 *       callback is sync type. buffer process done interface thread context
372 **/
373 typedef enum {
374     MM_CAMERA_STREAM_CB_TYPE_ASYNC,
375     MM_CAMERA_STREAM_CB_TYPE_SYNC,
376 } mm_camera_stream_cb_type;
377 
378 
379 /** mm_camera_channel_attr_t: structure for defining channel
380 *                             attributes
381 *    @notify_mode : notify mode: burst or continuous
382 *    @water_mark : queue depth. Only valid for burst mode
383 *    @look_back : look back how many frames from last buf.
384 *                 Only valid for burst mode
385 *    @post_frame_skip : after send first frame to HAL, how many
386 *                     frames needing to be skipped for next
387 *                     delivery. Only valid for burst mode
388 *    @max_unmatched_frames : max number of unmatched frames in
389 *                     queue
390 *    @enable_frame_sync: Enables frame sync for dual camera
391 *    @priority : save matched priority frames only
392 *    @user_expected_frame_id : Number of frames, camera interface
393 *                     will wait for getting the instant capture frame.
394 **/
395 typedef struct {
396     mm_camera_super_buf_notify_mode_t notify_mode;
397     uint8_t water_mark;
398     uint8_t look_back;
399     uint8_t post_frame_skip;
400     uint8_t max_unmatched_frames;
401     uint8_t enable_frame_sync;
402     mm_camera_super_buf_priority_t priority;
403     uint8_t user_expected_frame_id;
404 } mm_camera_channel_attr_t;
405 
406 /** mm_camera_cb_req_type: Callback request type**/
407 typedef enum {
408     MM_CAMERA_CB_REQ_TYPE_DEFAULT,
409     MM_CAMERA_CB_REQ_TYPE_SWITCH,
410     MM_CAMERA_CB_REQ_TYPE_FRAME_SYNC,
411     MM_CAMERA_CB_REQ_TYPE_ALL_CB,
412 } mm_camera_cb_req_type;
413 
414 /** mm_camera_intf_cb_req_type: structure to request different mode of stream callback
415 *    @camera_handle  : camera handle to be syced
416 *    @ch_id          : channel id to be synced
417 *    @stream_id      : stream id to be synced
418 *    @max_unmatched_frames : Frames to wait for before frame callback
419 *    @buf_cb         : callback. can be NULL. NULL uses already registered stream/channel cb
420 *    @userdata       : client objects.
421 **/
422 typedef struct {
423     uint32_t camera_handle;
424     uint32_t ch_id;
425     uint32_t stream_id;
426     mm_camera_channel_attr_t attr;
427     mm_camera_buf_notify_t buf_cb;
428     void *userdata;
429 } mm_camera_intf_frame_sync_t;
430 
431 typedef struct {
432     /** query_capability: fucntion definition for querying static
433      *                    camera capabilities
434      *    @camera_handle : camer handler
435      *  Return value: 0 -- success
436      *                -1 -- failure
437      *  Note: would assume cam_capability_t is already mapped
438      **/
439     int32_t (*query_capability) (uint32_t camera_handle);
440 
441     /** register_event_notify: fucntion definition for registering
442      *                         for event notification
443      *    @camera_handle : camer handler
444      *    @evt_cb : callback for event notify
445      *    @user_data : user data poiner
446      *  Return value: 0 -- success
447      *                -1 -- failure
448      **/
449     int32_t (*register_event_notify) (uint32_t camera_handle,
450                                       mm_camera_event_notify_t evt_cb,
451                                       void *user_data);
452 
453     /** close_camera: fucntion definition for closing a camera
454      *    @camera_handle : camer handler
455      *  Return value: 0 -- success
456      *                -1 -- failure
457      **/
458     int32_t (*close_camera) (uint32_t camera_handle);
459 
460     /** map_buf: fucntion definition for mapping a camera buffer
461      *           via domain socket
462      *    @camera_handle : camer handler
463      *    @buf_type : type of mapping buffers, can be value of
464      *                CAM_MAPPING_BUF_TYPE_CAPABILITY
465      *                CAM_MAPPING_BUF_TYPE_SETPARM_BUF
466      *                CAM_MAPPING_BUF_TYPE_GETPARM_BUF
467      *    @fd : file descriptor of the stream buffer
468      *    @size :  size of the stream buffer
469      *  Return value: 0 -- success
470      *                -1 -- failure
471      **/
472     int32_t (*map_buf) (uint32_t camera_handle,
473                         uint8_t buf_type,
474                         int fd,
475                         size_t size,
476                         void *buffer);
477 
478     /** map_bufs: function definition for mapping multiple camera buffers
479      *           via domain socket
480      *    @camera_handle : camera handler
481      *    @buf_map_list : list of buffers to map
482      *  Return value: 0 -- success
483      *                -1 -- failure
484      **/
485     int32_t (*map_bufs) (uint32_t camera_handle,
486                          const cam_buf_map_type_list *buf_map_list);
487 
488     /** unmap_buf: fucntion definition for unmapping a camera buffer
489      *           via domain socket
490      *    @camera_handle : camer handler
491      *    @buf_type : type of mapping buffers, can be value of
492      *                CAM_MAPPING_BUF_TYPE_CAPABILITY
493      *                CAM_MAPPING_BUF_TYPE_SETPARM_BUF
494      *                CAM_MAPPING_BUF_TYPE_GETPARM_BUF
495      *  Return value: 0 -- success
496      *                -1 -- failure
497      **/
498     int32_t (*unmap_buf) (uint32_t camera_handle,
499                           uint8_t buf_type);
500 
501     /** set_parms: fucntion definition for setting camera
502      *             based parameters to server
503      *    @camera_handle : camer handler
504      *    @parms : batch for parameters to be set, stored in
505      *               parm_buffer_t
506      *  Return value: 0 -- success
507      *                -1 -- failure
508      *  Note: would assume parm_buffer_t is already mapped, and
509      *       according parameter entries to be set are filled in the
510      *       buf before this call
511      **/
512     int32_t (*set_parms) (uint32_t camera_handle,
513                           parm_buffer_t *parms);
514 
515     /** get_parms: fucntion definition for querying camera
516      *             based parameters from server
517      *    @camera_handle : camer handler
518      *    @parms : batch for parameters to be queried, stored in
519      *               parm_buffer_t
520      *  Return value: 0 -- success
521      *                -1 -- failure
522      *  Note: would assume parm_buffer_t is already mapped, and
523      *       according parameter entries to be queried are filled in
524      *       the buf before this call
525      **/
526     int32_t (*get_parms) (uint32_t camera_handle,
527                           parm_buffer_t *parms);
528 
529     /** do_auto_focus: fucntion definition for performing auto focus
530      *    @camera_handle : camer handler
531      *  Return value: 0 -- success
532      *                -1 -- failure
533      *  Note: if this call success, we will always assume there will
534      *        be an auto_focus event following up.
535      **/
536     int32_t (*do_auto_focus) (uint32_t camera_handle);
537 
538     /** cancel_auto_focus: fucntion definition for cancelling
539      *                     previous auto focus request
540      *    @camera_handle : camer handler
541     *  Return value: 0 -- success
542     *                -1 -- failure
543      **/
544     int32_t (*cancel_auto_focus) (uint32_t camera_handle);
545 
546     /** prepare_snapshot: fucntion definition for preparing hardware
547      *                    for snapshot.
548      *    @camera_handle : camer handler
549      *    @do_af_flag    : flag indicating if AF needs to be done
550      *                     0 -- no AF needed
551      *                     1 -- AF needed
552      *  Return value: 0 -- success
553      *                -1 -- failure
554      **/
555     int32_t (*prepare_snapshot) (uint32_t camera_handle,
556                                  int32_t do_af_flag);
557 
558     /** start_zsl_snapshot: function definition for starting
559      *                    zsl snapshot.
560      *    @camera_handle : camer handler
561      *    @ch_id         : channel id
562      *  Return value: 0 -- success
563      *                -1 -- failure
564      **/
565     int32_t (*start_zsl_snapshot) (uint32_t camera_handle, uint32_t ch_id);
566 
567     /** stop_zsl_snapshot: function definition for stopping
568      *                    zsl snapshot.
569      *    @camera_handle : camer handler
570      *    @ch_id         : channel id
571      *  Return value: 0 -- success
572      *                -1 -- failure
573      **/
574     int32_t (*stop_zsl_snapshot) (uint32_t camera_handle, uint32_t ch_id);
575 
576     /** add_channel: fucntion definition for adding a channel
577      *    @camera_handle : camer handler
578      *    @ch_id : channel handler
579      *    @attr : pointer to channel attribute structure
580      *    @channel_cb : callbak to handle bundled super buffer
581      *    @userdata : user data pointer
582      *  Return value: channel id, zero is invalid ch_id
583      * Note: attr, channel_cb, and userdata can be NULL if no
584      *       superbufCB is needed
585      **/
586     uint32_t (*add_channel) (uint32_t camera_handle,
587                              mm_camera_channel_attr_t *attr,
588                              mm_camera_buf_notify_t channel_cb,
589                              void *userdata);
590 
591     /** delete_channel: fucntion definition for deleting a channel
592      *    @camera_handle : camer handler
593      *    @ch_id : channel handler
594      *  Return value: 0 -- success
595      *                -1 -- failure
596      **/
597     int32_t (*delete_channel) (uint32_t camera_handle,
598                                uint32_t ch_id);
599 
600     /** get_bundle_info: function definition for querying bundle
601      *  info of the channel
602      *    @camera_handle : camera handler
603      *    @ch_id         : channel handler
604      *    @bundle_info   : bundle info to be filled in
605      *  Return value: 0 -- success
606      *                -1 -- failure
607      **/
608     int32_t (*get_bundle_info) (uint32_t camera_handle,
609                                 uint32_t ch_id,
610                                 cam_bundle_config_t *bundle_info);
611 
612     /** add_stream: fucntion definition for adding a stream
613      *    @camera_handle : camer handler
614      *    @ch_id : channel handler
615      *  Return value: stream_id. zero is invalid stream_id
616      **/
617     uint32_t (*add_stream) (uint32_t camera_handle,
618                             uint32_t ch_id);
619 
620     /** delete_stream: fucntion definition for deleting a stream
621      *    @camera_handle : camer handler
622      *    @ch_id : channel handler
623      *    @stream_id : stream handler
624      *  Return value: 0 -- success
625      *                -1 -- failure
626      **/
627     int32_t (*delete_stream) (uint32_t camera_handle,
628                               uint32_t ch_id,
629                               uint32_t stream_id);
630 
631     /** link_stream: function definition for linking a stream
632      *    @camera_handle : camera handle
633      *    @ch_id : channel handle from which the stream originates
634      *    @stream_id : stream handle
635      *    @linked_ch_id: channel handle in which the stream will be linked
636      *  Return value: 0 -- success
637      *                -1 -- failure
638      **/
639     int32_t (*link_stream) (uint32_t camera_handle,
640           uint32_t ch_id,
641           uint32_t stream_id,
642           uint32_t linked_ch_id);
643 
644     /** config_stream: fucntion definition for configuring a stream
645      *    @camera_handle : camer handler
646      *    @ch_id : channel handler
647      *    @stream_id : stream handler
648      *    @confid : pointer to a stream configuration structure
649      *  Return value: 0 -- success
650      *                -1 -- failure
651      **/
652     int32_t (*config_stream) (uint32_t camera_handle,
653                               uint32_t ch_id,
654                               uint32_t stream_id,
655                               mm_camera_stream_config_t *config);
656 
657     /** map_stream_buf: fucntion definition for mapping
658      *                 stream buffer via domain socket
659      *    @camera_handle : camer handler
660      *    @ch_id : channel handler
661      *    @stream_id : stream handler
662      *    @buf_type : type of mapping buffers, can be value of
663      *             CAM_MAPPING_BUF_TYPE_STREAM_BUF
664      *             CAM_MAPPING_BUF_TYPE_STREAM_INFO
665      *             CAM_MAPPING_BUF_TYPE_OFFLINE_INPUT_BUF
666      *    @buf_idx : buffer index within the stream buffers
667      *    @plane_idx : plane index. If all planes share the same fd,
668      *               plane_idx = -1; otherwise, plean_idx is the
669      *               index to plane (0..num_of_planes)
670      *    @fd : file descriptor of the stream buffer
671      *    @size :  size of the stream buffer
672      *  Return value: 0 -- success
673      *                -1 -- failure
674      **/
675     int32_t (*map_stream_buf) (uint32_t camera_handle,
676                                uint32_t ch_id,
677                                uint32_t stream_id,
678                                uint8_t buf_type,
679                                uint32_t buf_idx,
680                                int32_t plane_idx,
681                                int fd,
682                                size_t size,
683                                void *buffer);
684 
685     /** map_stream_bufs: function definition for mapping multiple
686      *                 stream buffers via domain socket
687      *    @camera_handle : camera handler
688      *    @ch_id : channel handler
689      *    @buf_map_list : list of buffers to map
690      *  Return value: 0 -- success
691      *                -1 -- failure
692      **/
693     int32_t (*map_stream_bufs) (uint32_t camera_handle,
694                                 uint32_t ch_id,
695                                 const cam_buf_map_type_list *buf_map_list);
696 
697     /** unmap_stream_buf: fucntion definition for unmapping
698      *                 stream buffer via domain socket
699      *    @camera_handle : camer handler
700      *    @ch_id : channel handler
701      *    @stream_id : stream handler
702      *    @buf_type : type of mapping buffers, can be value of
703      *             CAM_MAPPING_BUF_TYPE_STREAM_BUF
704      *             CAM_MAPPING_BUF_TYPE_STREAM_INFO
705      *             CAM_MAPPING_BUF_TYPE_OFFLINE_INPUT_BUF
706      *    @buf_idx : buffer index within the stream buffers
707      *    @plane_idx : plane index. If all planes share the same fd,
708      *               plane_idx = -1; otherwise, plean_idx is the
709      *               index to plane (0..num_of_planes)
710      *  Return value: 0 -- success
711      *                -1 -- failure
712      **/
713     int32_t (*unmap_stream_buf) (uint32_t camera_handle,
714                                  uint32_t ch_id,
715                                  uint32_t stream_id,
716                                  uint8_t buf_type,
717                                  uint32_t buf_idx,
718                                  int32_t plane_idx);
719 
720     /** set_stream_parms: fucntion definition for setting stream
721      *                    specific parameters to server
722      *    @camera_handle : camer handler
723      *    @ch_id : channel handler
724      *    @stream_id : stream handler
725      *    @parms : batch for parameters to be set
726      *  Return value: 0 -- success
727      *                -1 -- failure
728      *  Note: would assume parm buffer is already mapped, and
729      *       according parameter entries to be set are filled in the
730      *       buf before this call
731      **/
732     int32_t (*set_stream_parms) (uint32_t camera_handle,
733                                  uint32_t ch_id,
734                                  uint32_t s_id,
735                                  cam_stream_parm_buffer_t *parms);
736 
737     /** get_stream_parms: fucntion definition for querying stream
738      *                    specific parameters from server
739      *    @camera_handle : camer handler
740      *    @ch_id : channel handler
741      *    @stream_id : stream handler
742      *    @parms : batch for parameters to be queried
743      *  Return value: 0 -- success
744      *                -1 -- failure
745      *  Note: would assume parm buffer is already mapped, and
746      *       according parameter entries to be queried are filled in
747      *       the buf before this call
748      **/
749     int32_t (*get_stream_parms) (uint32_t camera_handle,
750                                  uint32_t ch_id,
751                                  uint32_t s_id,
752                                  cam_stream_parm_buffer_t *parms);
753 
754     /** start_channel: fucntion definition for starting a channel
755      *    @camera_handle : camer handler
756      *    @ch_id : channel handler
757      *    @start_sensor_streaming: whether to start sensor streaming.
758      *                             If false, start_sensor_streaming() must be
759      *                             called to start sensor streaming.
760      *
761      *  Return value: 0 -- success
762      *                -1 -- failure
763      * This call will start all streams belongs to the channel.
764      **/
765     int32_t (*start_channel) (uint32_t camera_handle,
766                               uint32_t ch_id, bool start_sensor_streaming);
767 
768     /**
769      * start_sensor_streaming: function definition for starting sensor
770      *                         streaming.
771      *   @camera_handle : camera handler
772      *   @ch_id : channel handler
773      *
774      *  Return value: 0 -- success
775      *                -1 -- failure
776      * This call will start sensor streaming if start_channel() was called with
777      * start_sensor_streaming == FALSE.
778      */
779     int32_t (*start_sensor_streaming) (uint32_t camera_handle,
780                                        uint32_t ch_id);
781 
782     /** stop_channel: fucntion definition for stopping a channel
783      *    @camera_handle : camer handler
784      *    @ch_id : channel handler
785      *    @stop_immediately : whether to stop channel immediately. If true,
786      *                        streams will be stopped immediately without
787      *                        waiting for frame boundary. If false, streams
788      *                        will be stopped cleanly, which may wait for frame
789      *                        boundary based on stream conditions.
790      *  Return value: 0 -- success
791      *                -1 -- failure
792      * This call will stop all streams belongs to the channel
793      **/
794     int32_t (*stop_channel) (uint32_t camera_handle,
795                              uint32_t ch_id,
796                              bool stop_immediately);
797 
798     /** qbuf: fucntion definition for queuing a frame buffer back to
799      *        kernel for reuse
800      *    @camera_handle : camer handler
801      *    @ch_id : channel handler
802      *    @buf : a frame buffer to be queued back to kernel
803      *  Return value: 0 -- success
804      *                -1 -- failure
805      **/
806     int32_t (*qbuf) (uint32_t camera_handle,
807                      uint32_t ch_id,
808                      mm_camera_buf_def_t *buf);
809 
810     /** cancel_buffer: fucntion definition for recalling a frame
811      *        buffer from the kernel this is most likely when h/w
812      *        failed to use this buffer and dropped the frame we use
813      *        this API to recall the buffer and return it to the
814      *        framework
815      *    @camera_handle : camer handler
816      *    @ch_id : channel handler
817      *    @stream_id : stream handle
818      *    @buf : a frame buffer to be queued back to kernel
819      *  Return value: 0 -- success
820      *                -1 -- failure
821      **/
822     int32_t (*cancel_buffer) (uint32_t camera_handle,
823                      uint32_t ch_id,
824                      uint32_t stream_id,
825                      uint32_t buf_idx);
826 
827 
828     /** get_queued_buf_count: fucntion definition for querying queued buf count
829      *    @camera_handle : camer handler
830      *    @ch_id : channel handler
831      *    @stream_id : stream handler
832      *  Return value: queued buf count
833      **/
834     int32_t (*get_queued_buf_count) (uint32_t camera_handle,
835             uint32_t ch_id,
836             uint32_t stream_id);
837 
838     /** request_super_buf: fucntion definition for requesting frames
839      *                     from superbuf queue in burst mode
840      *    @camera_handle : camer handler
841      *    @ch_id : channel handler
842      *    @buf : provides info related to the super buf request
843      *  Return value: 0 -- success
844      *                -1 -- failure
845      **/
846     int32_t (*request_super_buf) (uint32_t camera_handle,
847                                   uint32_t ch_id,
848                                   mm_camera_req_buf_t *buf);
849 
850     /** cancel_super_buf_request: fucntion definition for canceling
851      *                     frames dispatched from superbuf queue in
852      *                     burst mode
853      *    @camera_handle : camer handler
854      *    @ch_id : channel handler
855      *  Return value: 0 -- success
856      *                -1 -- failure
857      **/
858     int32_t (*cancel_super_buf_request) (uint32_t camera_handle,
859                                          uint32_t ch_id);
860 
861     /** flush_super_buf_queue: function definition for flushing out
862      *                     all frames in the superbuf queue up to frame_idx,
863      *                     even if frames with frame_idx come in later than
864      *                     this call.
865      *    @camera_handle : camer handler
866      *    @ch_id : channel handler
867      *    @frame_idx : frame index up until which all superbufs are flushed
868      *  Return value: 0 -- success
869      *                -1 -- failure
870      **/
871     int32_t (*flush_super_buf_queue) (uint32_t camera_handle,
872                                       uint32_t ch_id, uint32_t frame_idx);
873 
874     /** configure_notify_mode: function definition for configuring the
875      *                         notification mode of channel
876      *    @camera_handle : camera handler
877      *    @ch_id : channel handler
878      *    @notify_mode : notification mode
879      *  Return value: 0 -- success
880      *                -1 -- failure
881      **/
882     int32_t (*configure_notify_mode) (uint32_t camera_handle,
883                                       uint32_t ch_id,
884                                       mm_camera_super_buf_notify_mode_t notify_mode);
885 
886    /** process_advanced_capture: function definition for start/stop advanced capture
887      *                    for snapshot.
888      *    @camera_handle : camera handle
889      *    @ch_id : channel handler
890      *    @type :  advanced capture type.
891      *    @trigger    : flag indicating if advanced capture needs to be done
892      *                     0 -- stop advanced capture
893      *                     1 -- start advanced capture
894      *    @in_value: Input value. Configaration
895      *  Return value: 0 -- success
896      *                -1 -- failure
897      **/
898     int32_t (*process_advanced_capture) (uint32_t camera_handle,
899              uint32_t ch_id, mm_camera_advanced_capture_t type,
900              int8_t start_flag, void *in_value);
901 
902    /** get_session_id: gets the backend session id from the kernel
903      *    @camera_handle : camera handle
904      *    @sessionid : session id to be retrieved
905      *     Return value: 0 -- success
906      *                -1 -- failure
907      *  Note: if this call succeeds, we will get a valid session id
908      **/
909     int32_t (*get_session_id) (uint32_t camera_handle,
910             uint32_t* sessionid);
911 
912     /** set_dual_cam_cmd: sends sync cmd
913       *    @camera_handle : camera handle
914       *     Return value: 0 -- success
915       *                -1 -- failure
916       *  Note: if this call succeeds, we will get linking established in back end
917       **/
918      int32_t (*set_dual_cam_cmd)(uint32_t camera_handle);
919     /** flush: function definition for flush
920      *  @camera_handle: camera handler
921      *  Return value: 0 -- success
922      *               -1 -- failure
923      **/
924     int32_t (*flush) (uint32_t camera_handle);
925 
926    /** register_stream_buf_cb: fucntion definition for registering special stream callbacks
927      *    @camera_handle : camer handler
928      *    @ch_id : channel handler
929      *    @stream_id : stream handler
930      *    @buf_cb : callback function pointer
931      *    @cb_type : Callback type SYNC/ASYNC
932      *    @userdata : user data pointer
933      *    Return value: 0 -- success
934      *                -       1 -- failure
935      **/
936     int32_t (*register_stream_buf_cb) (uint32_t camera_handle,
937             uint32_t ch_id, uint32_t stream_id, mm_camera_buf_notify_t buf_cb,
938             mm_camera_stream_cb_type cb_type, void *userdata);
939 
940    /** register_stream_frame_sync: fucntion definition for registering frame sync
941      *    @camera_handle : camer handler
942      *    @ch_id : channel handler
943      *    @stream_id : stream handler. Can be 0 to config only channel callback sync
944      *    @sync_attr : pointer to a stream sync configuration structure
945      *  Return value: 0 -- success
946      *                -1 -- failure
947      **/
948     int32_t (*register_frame_sync) (uint32_t camera_handle,
949             uint32_t ch_id, uint32_t stream_id,
950             mm_camera_intf_frame_sync_t *sync_attr);
951 
952    /** handle_frame_sync_cb: function to handle frame sync
953      *    @camera_handle : camer handler
954      *    @ch_id : channel handler
955      *    @stream_id : stream handler
956      *    @req_type : Frame sync request type
957      *  Return value: 0 -- success
958      *                -1 -- failure
959      **/
960     int32_t (*handle_frame_sync_cb) (uint32_t camera_handle,
961             uint32_t ch_id, uint32_t stream_id,
962             mm_camera_cb_req_type req_type);
963 } mm_camera_ops_t;
964 
965 /** mm_camera_vtbl_t: virtual table for camera operations
966 *    @camera_handle : camera handler which uniquely identifies a
967 *                   camera object
968 *    @ops : API call table
969 **/
970 typedef struct {
971     uint32_t camera_handle;
972     mm_camera_ops_t *ops;
973 } mm_camera_vtbl_t;
974 
975 /* return total number of cameras */
976 uint8_t get_num_of_cameras();
977 
978 /* return number of cameras to expose*/
979 uint8_t get_num_of_cameras_to_expose();
980 
981 /* return reference pointer of camera vtbl */
982 int32_t camera_open(uint8_t camera_idx, mm_camera_vtbl_t **camera_obj);
983 
984 /* helper functions */
985 int32_t mm_stream_calc_offset_preview(cam_stream_info_t *stream_info,
986         cam_dimension_t *dim,
987         cam_padding_info_t *padding,
988         cam_stream_buf_plane_info_t *buf_planes);
989 
990 int32_t mm_stream_calc_offset_post_view(cam_stream_info_t *stream_info,
991         cam_dimension_t *dim,
992         cam_padding_info_t *padding,
993         cam_stream_buf_plane_info_t *buf_planes);
994 
995 int32_t mm_stream_calc_offset_snapshot(cam_format_t fmt,
996         cam_dimension_t *dim,
997         cam_padding_info_t *padding,
998         cam_stream_buf_plane_info_t *buf_planes);
999 
1000 int32_t mm_stream_calc_offset_raw(cam_stream_info_t *stream_info,
1001         cam_dimension_t *dim,
1002         cam_padding_info_t *padding,
1003         cam_stream_buf_plane_info_t *buf_planes);
1004 
1005 int32_t mm_stream_calc_offset_video(cam_format_t fmt,
1006         cam_dimension_t *dim,
1007         cam_stream_buf_plane_info_t *buf_planes);
1008 
1009 int32_t mm_stream_calc_offset_metadata(cam_dimension_t *dim,
1010         cam_padding_info_t *padding,
1011         cam_stream_buf_plane_info_t *buf_planes);
1012 
1013 int32_t mm_stream_calc_offset_postproc(cam_stream_info_t *stream_info,
1014         cam_padding_info_t *padding,
1015         cam_stream_buf_plane_info_t *buf_planes);
1016 
1017 int32_t mm_stream_calc_offset_analysis(cam_format_t fmt,
1018         cam_dimension_t *dim,
1019         cam_padding_info_t *padding,
1020         cam_stream_buf_plane_info_t *buf_planes);
1021 
1022 uint32_t mm_stream_calc_lcm (int32_t num1, int32_t num2);
1023 struct camera_info *get_cam_info(uint32_t camera_id, cam_sync_type_t *pCamType);
1024 
1025 uint8_t is_yuv_sensor(uint32_t camera_id);
1026 
1027 cam_sync_type_t get_cam_type(uint32_t camera_id);
1028 
1029 /*Dual camera related utility functions*/
1030 
1031 /*Query if it is dual camera mode based on the camera index*/
1032 uint8_t is_dual_camera_by_idx(uint32_t camera_id);
1033 
1034 /*Query if it is dual camera mode based on the camera/channel/stream handles*/
1035 uint8_t is_dual_camera_by_handle(uint32_t handle);
1036 
1037 /*Get Primary camera handle for camera/channel/stream*/
1038 uint32_t get_main_camera_handle(uint32_t handle);
1039 
1040 /*Get Auxilary camera handle for camera/channel/stream*/
1041 uint32_t get_aux_camera_handle(uint32_t handle);
1042 
1043 /*Validate 2 handle if it is belong to same instance of camera/channel/stream*/
1044 uint8_t validate_handle(uint32_t src_handle, uint32_t handle);
1045 #endif /*__MM_CAMERA_INTERFACE_H__*/
1046