1 /*
2 Copyright (c) 2011-2012,2015, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef __MM_CAMERA_H__
31 #define __MM_CAMERA_H__
32 
33 #define TRUE 1
34 #define FALSE 0
35 
36 typedef enum {
37     MM_CAMERA_STREAM_STATE_NOTUSED,     /* not used */
38     MM_CAMERA_STREAM_STATE_ACQUIRED,    /* acquired, fd opened  */
39     MM_CAMERA_STREAM_STATE_CFG,             /* fmt & dim configured */
40     MM_CAMERA_STREAM_STATE_REG,             /* buf regged, stream off */
41     MM_CAMERA_STREAM_STATE_ACTIVE,      /* stream on */
42     MM_CAMERA_STREAM_STATE_MAX
43 } mm_camera_stream_state_type_t;
44 
45 typedef enum {
46     MM_CAMERA_STATE_EVT_NOTUSED,
47     MM_CAMERA_STATE_EVT_ACQUIRE,
48     MM_CAMERA_STATE_EVT_ATTR,
49     MM_CAMERA_STATE_EVT_RELEASE,
50     MM_CAMERA_STATE_EVT_REG_BUF_CB,
51     MM_CAMERA_STATE_EVT_SET_FMT,
52     MM_CAMERA_STATE_EVT_SET_DIM,
53     MM_CAMERA_STATE_EVT_REG_BUF, // request amount of buffers and enqueue all buffers to kernel
54     MM_CAMERA_STATE_EVT_UNREG_BUF,
55     MM_CAMERA_STATE_EVT_STREAM_ON,
56     MM_CAMERA_STATE_EVT_STREAM_OFF,
57     MM_CAMERA_STATE_EVT_QBUF,
58     MM_CAMERA_STATE_EVT_GET_CROP,
59     MM_CAMERA_STATE_EVT_DISPATCH_BUFFERED_FRAME,
60     MM_CAMERA_STATE_EVT_REQUEST_BUF, // request amount of buffers to kernel only
61     MM_CAMERA_STATE_EVT_MAX
62 } mm_camera_state_evt_type_t;
63 
64 typedef struct {
65     mm_camera_event_notify_t evt_cb;
66     void * user_data;
67 } mm_camera_notify_cb_t;
68 
69 typedef enum {
70     MM_CAMERA_BUF_CB_ONCE,
71     MM_CAMERA_BUF_CB_COUNT,
72     MM_CAMERA_BUF_CB_INFINITE
73 } mm_camera_buf_cb_type_t;
74 
75 typedef struct {
76     mm_camera_buf_notify_t cb;
77     mm_camera_buf_cb_type_t cb_type;
78     uint32_t cb_count;
79     void *user_data;
80 } mm_camera_buf_cb_t;
81 
82 typedef enum {
83     MM_CAMERA_STREAM_PIPE,
84     MM_CAMERA_STREAM_PREVIEW,
85     MM_CAMERA_STREAM_VIDEO,
86     MM_CAMERA_STREAM_SNAPSHOT,
87     MM_CAMERA_STREAM_THUMBNAIL,
88     MM_CAMERA_STREAM_RAW,
89     MM_CAMERA_STREAM_VIDEO_MAIN,
90     MM_CAMERA_STREAM_MAX
91 } mm_camera_stream_type_t;
92 
93 typedef struct mm_camera_frame_t mm_camera_frame_t;
94 struct mm_camera_frame_t{
95     struct msm_frame frame;
96     struct v4l2_plane planes[VIDEO_MAX_PLANES];
97     uint8_t num_planes;
98     int idx;
99     int match;
100     int valid_entry;
101     mm_camera_frame_t *next;
102 };
103 
104 typedef struct {
105     pthread_mutex_t mutex;
106     int cnt;
107 	int match_cnt;
108     mm_camera_frame_t *head;
109     mm_camera_frame_t *tail;
110 } mm_camera_frame_queue_t;
111 
112 typedef struct {
113     mm_camera_frame_queue_t readyq;
114     int32_t num_frame;
115     uint32_t frame_len;
116     int8_t reg_flag[MM_CAMERA_MAX_NUM_FRAMES];
117     uint32_t frame_offset[MM_CAMERA_MAX_NUM_FRAMES];
118     mm_camera_frame_t frame[MM_CAMERA_MAX_NUM_FRAMES];
119     int8_t ref_count[MM_CAMERA_MAX_NUM_FRAMES];
120     int32_t use_multi_fd;
121     int qbuf;
122     pthread_mutex_t mutex;
123 } mm_camera_stream_frame_t;
124 
125 typedef struct {
126     int32_t fd;
127     mm_camera_stream_state_type_t state;
128     mm_camera_stream_type_t stream_type;
129     struct v4l2_format fmt;
130     cam_format_t cam_fmt;
131     mm_camera_stream_frame_t frame;
132 } mm_camera_stream_t;
133 
134 typedef struct {
135     mm_camera_stream_t stream;
136     mm_camera_raw_streaming_type_t mode;
137 } mm_camera_ch_raw_t;
138 
139 typedef struct {
140     mm_camera_stream_t stream;
141 } mm_camera_ch_preview_t;
142 
143 typedef struct {
144     mm_camera_stream_t thumbnail;
145     mm_camera_stream_t main;
146     int    delivered_cnt;
147     int8_t pending_cnt;
148     uint32_t expected_matching_id;
149 } mm_camera_ch_snapshot_t;
150 
151 typedef struct {
152     int8_t fifo[MM_CAMERA_MAX_FRAME_NUM];
153     int8_t low;
154     int8_t high;
155     int8_t len;
156     int8_t water_mark;
157 } mm_camera_circule_fifo_t;
158 
159 typedef struct {
160     mm_camera_stream_t video;
161     mm_camera_stream_t main;
162     uint8_t has_main;
163 } mm_camera_ch_video_t;
164 
165 #define MM_CAMERA_BUF_CB_MAX 4
166 typedef struct {
167     mm_camera_channel_type_t type;
168     pthread_mutex_t mutex;
169     uint8_t acquired;
170     mm_camera_buf_cb_t buf_cb[MM_CAMERA_BUF_CB_MAX];
171     mm_camera_channel_attr_buffering_frame_t buffering_frame;
172     union {
173         mm_camera_ch_raw_t raw;
174         mm_camera_ch_preview_t preview;
175         mm_camera_ch_snapshot_t snapshot;
176         mm_camera_ch_video_t video;
177     };
178 } mm_camera_ch_t;
179 
180 #define MM_CAMERA_EVT_ENTRY_MAX 4
181 typedef struct {
182     mm_camera_event_notify_t evt_cb;
183     void *user_data;
184 } mm_camera_evt_entry_t;
185 
186 typedef struct {
187     mm_camera_evt_entry_t evt[MM_CAMERA_EVT_ENTRY_MAX];
188     int reg_count;
189 } mm_camera_evt_obj_t;
190 
191 #define MM_CAMERA_CH_STREAM_MAX 2
192 typedef enum {
193     MM_CAMERA_POLL_TYPE_EVT,
194     MM_CAMERA_POLL_TYPE_CH,
195     MM_CAMERA_POLL_TYPE_MAX
196 } mm_camera_poll_thread_type_t;
197 
198 typedef struct {
199     mm_camera_poll_thread_type_t poll_type;
200     int32_t pfds[2];
201     int poll_fd[MM_CAMERA_CH_STREAM_MAX+1];
202     int num_fds;
203     int used;
204     pthread_t pid;
205     int32_t state;
206     int timeoutms;
207     void *my_obj;
208     mm_camera_channel_type_t ch_type;
209     mm_camera_stream_t *poll_streams[MM_CAMERA_CH_STREAM_MAX];
210     uint32_t cmd;
211 } mm_camera_poll_thread_data_t;
212 
213 typedef struct {
214     pthread_mutex_t mutex;
215     pthread_cond_t cond_v;
216     int32_t status;
217     mm_camera_poll_thread_data_t data;
218 } mm_camera_poll_thread_t;
219 
220 typedef struct {
221     int stream_on_count_cfg;
222     int stream_off_count_cfg;
223     int stream_on_count;
224     int stream_off_count;
225 } mm_camera_ch_stream_count_t;
226 #define MM_CAMERA_POLL_THRAED_MAX (MM_CAMERA_CH_MAX+1)
227 
228 typedef struct {
229   struct msm_mem_map_info cookie;
230   uint32_t vaddr;
231 } mm_camera_mem_map_entry_t;
232 
233 #define MM_CAMERA_MEM_MAP_MAX 8
234 typedef struct {
235   int num;
236   mm_camera_mem_map_entry_t entry[MM_CAMERA_MEM_MAP_MAX];
237 } mm_camera_mem_map_t;
238 
239 typedef struct {
240     int8_t my_id;
241     camera_mode_t current_mode;
242     mm_camera_op_mode_type_t op_mode;
243     mm_camera_notify_cb_t *notify;
244     mm_camera_ch_t ch[MM_CAMERA_CH_MAX];
245     int ref_count;
246     uint32_t ch_streaming_mask;
247     int32_t ctrl_fd;
248     int32_t ds_fd; // domain socket fd
249     cam_ctrl_dimension_t dim;
250     cam_prop_t properties;
251     pthread_mutex_t mutex;
252     mm_camera_evt_obj_t evt[MM_CAMERA_EVT_TYPE_MAX];
253     mm_camera_ch_stream_count_t ch_stream_count[MM_CAMERA_CH_MAX];
254     uint32_t evt_type_mask;
255     mm_camera_poll_thread_t poll_threads[MM_CAMERA_POLL_THRAED_MAX];
256     mm_camera_mem_map_t hist_mem_map;
257     int full_liveshot;
258     int snap_burst_num_by_user;
259 } mm_camera_obj_t;
260 
261 #define MM_CAMERA_DEV_NAME_LEN 32
262 #define MM_CAMERA_DEV_OPEN_TRIES 2
263 #define MM_CAMERA_DEV_OPEN_RETRY_SLEEP 20
264 
265 typedef struct {
266     mm_camera_t camera[MSM_MAX_CAMERA_SENSORS];
267     int8_t num_cam;
268     char video_dev_name[MSM_MAX_CAMERA_SENSORS][MM_CAMERA_DEV_NAME_LEN];
269     mm_camera_obj_t *cam_obj[MSM_MAX_CAMERA_SENSORS];
270 } mm_camera_ctrl_t;
271 
272 typedef struct {
273     mm_camera_parm_type_t parm_type;
274      void *p_value;
275 } mm_camera_parm_t;
276 
277 extern int32_t mm_camera_stream_fsm_fn_vtbl (mm_camera_obj_t * my_obj,
278                                             mm_camera_stream_t *stream,
279                                             mm_camera_state_evt_type_t evt, void *val);
280 extern const char *mm_camera_util_get_dev_name(mm_camera_obj_t * my_obj);
281 extern int32_t mm_camera_util_s_ctrl( int32_t fd,
282                                             uint32_t id, int32_t value);
283 extern int32_t mm_camera_util_private_s_ctrl( int32_t fd,
284                                             uint32_t id, int32_t value);
285 extern int32_t mm_camera_util_g_ctrl( int32_t fd,
286                                             uint32_t id, int32_t *value);
287 extern int32_t mm_camera_ch_fn(mm_camera_obj_t * my_obj,
288                                             mm_camera_channel_type_t ch_type,
289                                             mm_camera_state_evt_type_t evt, void *val);
290 extern int32_t mm_camera_action(mm_camera_obj_t *my_obj, uint8_t start,
291                                             mm_camera_ops_type_t opcode, void *parm);
292 extern int32_t mm_camera_open(mm_camera_obj_t *my_obj,
293                                             mm_camera_op_mode_type_t op_mode);
294 extern int32_t mm_camera_close(mm_camera_obj_t *my_obj);
295 extern int32_t mm_camera_start(mm_camera_obj_t *my_obj,
296                                             mm_camera_ops_type_t opcode, void *parm);
297 extern int32_t mm_camera_stop(mm_camera_obj_t *my_obj,
298                                             mm_camera_ops_type_t opcode, void *parm);
299 extern int32_t mm_camera_get_parm(mm_camera_obj_t * my_obj,
300                                             mm_camera_parm_t *parm);
301 extern int32_t mm_camera_set_parm(mm_camera_obj_t * my_obj,
302                                             mm_camera_parm_t *parm);
303 extern int32_t mm_camera_request_buf(mm_camera_obj_t * my_obj, mm_camera_reg_buf_t *buf);
304 extern int32_t mm_camera_prepare_buf(mm_camera_obj_t * my_obj, mm_camera_reg_buf_t *buf);
305 extern int32_t mm_camera_unprepare_buf(mm_camera_obj_t * my_obj, mm_camera_channel_type_t ch_type);
306 extern int mm_camera_poll_thread_launch(mm_camera_obj_t * my_obj, int ch_type);
307 
308 int mm_camera_poll_thread_del_ch(mm_camera_obj_t * my_obj, int ch_type);
309 int mm_camera_poll_thread_add_ch(mm_camera_obj_t * my_obj, int ch_type);
310 extern int32_t mm_camera_poll_dispatch_buffered_frames(mm_camera_obj_t * my_obj, int ch_type);
311 extern int mm_camera_poll_thread_release(mm_camera_obj_t * my_obj, int ch_type);
312 extern void mm_camera_poll_threads_init(mm_camera_obj_t * my_obj);
313 extern void mm_camera_poll_threads_deinit(mm_camera_obj_t * my_obj);
314 extern int mm_camera_poll_busy(mm_camera_obj_t * my_obj);
315 extern void mm_camera_msm_data_notify(mm_camera_obj_t * my_obj, int fd,
316                                             mm_camera_stream_type_t stream_type);
317 extern void mm_camera_msm_evt_notify(mm_camera_obj_t * my_obj, int fd);
318 extern int mm_camera_read_msm_frame(mm_camera_obj_t * my_obj,
319                         mm_camera_stream_t *stream);
320 extern int32_t mm_camera_ch_acquire(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type);
321 extern void mm_camera_ch_release(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type);
322 extern int mm_camera_ch_is_active(mm_camera_obj_t * my_obj, mm_camera_channel_type_t ch_type);
323 extern void mm_camera_ch_util_get_stream_objs(mm_camera_obj_t * my_obj,
324                                                             mm_camera_channel_type_t ch_type,
325                                                             mm_camera_stream_t **stream1,
326                                                             mm_camera_stream_t **stream2);
327 extern int mm_camera_stream_qbuf(mm_camera_obj_t * my_obj,
328                                                             mm_camera_stream_t *stream,
329                                                             int idx);
330 extern int mm_camera_stream_frame_get_q_cnt(mm_camera_frame_queue_t *q);
331 extern mm_camera_frame_t *mm_camera_stream_frame_deq(mm_camera_frame_queue_t *q);
332 extern mm_camera_frame_t *mm_camera_stream_frame_deq_no_lock(mm_camera_frame_queue_t *q);
333 extern void mm_camera_stream_frame_enq(mm_camera_frame_queue_t *q, mm_camera_frame_t *node);
334 extern void mm_camera_stream_frame_enq_no_lock(mm_camera_frame_queue_t *q, mm_camera_frame_t *node);
335 extern void mm_camera_stream_frame_refill_q(mm_camera_frame_queue_t *q, mm_camera_frame_t *node, int num);
336 extern int mm_camera_stream_is_active(mm_camera_stream_t *stream);
337 extern int32_t mm_camera_stream_util_buf_done(mm_camera_obj_t * my_obj,
338                     mm_camera_stream_t *stream,
339                     mm_camera_notify_frame_t *frame);
340 //extern int mm_camera_poll_add_stream(mm_camera_obj_t * my_obj, mm_camera_stream_t *stream);
341 //extern int mm_camera_poll_del_stream(mm_camera_obj_t * my_obj, mm_camera_stream_t *stream);
342 extern int mm_camera_dev_open(int *fd, char *dev_name);
343 extern int mm_camera_reg_event(mm_camera_obj_t * my_obj, mm_camera_event_notify_t evt_cb,
344                            void *user_data, uint32_t evt_type);
345 extern int mm_camera_poll_send_ch_event(mm_camera_obj_t * my_obj, mm_camera_event_t *event);
346 extern void mm_camera_msm_proc_ch_event(mm_camera_obj_t *my_obj, mm_camera_event_t *event);
347 extern void mm_camera_dispatch_app_event(mm_camera_obj_t *my_obj, mm_camera_event_t *event);
348 extern void mm_camera_dispatch_buffered_frames(mm_camera_obj_t *my_obj, mm_camera_channel_type_t ch_type);
349 extern void mm_camera_check_pending_zsl_frames(mm_camera_obj_t *my_obj,
350                                         mm_camera_channel_type_t ch_type);
351 extern int mm_camera_ch_util_get_num_stream(mm_camera_obj_t * my_obj,mm_camera_channel_type_t ch_type);
352 extern int32_t mm_camera_sendmsg(mm_camera_obj_t *my_obj, void *msg, uint32_t buf_size, int sendfd);
353 #endif /* __MM_CAMERA_H__ */
354