xref: /rockchip-linux_mpp/kmpp/base/inc/kmpp_obj_helper.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2024 Rockchip Electronics Co., Ltd.
4  */
5 
6 #include "kmpp_obj_macro.h"
7 
8 #ifdef __OBJECT_HERLPER_H__
9 #errorMUST NOT include obj_helper.h within obj_helper.h10 #endif
11 
12 /* define object helper for loop include detection */
13 #define __OBJECT_HERLPER_H__
14 
15 #if !defined(KMPP_OBJ_NAME) || \
16     !defined(KMPP_OBJ_INTF_TYPE)
17 
18 #warning "When using kmpp_obj_helper.h The following macro must be defined:"
19 #warning "KMPP_OBJ_NAME                 - object name"
20 #warning "KMPP_OBJ_INTF_TYPE            - object interface type"
21 #warning "option macro:"
22 #warning "KMPP_OBJ_IMPL_TYPE            - object implement type"
23 #warning "KMPP_OBJ_EXTRA_SIZE           - object extra size in bytes"
24 #warning "KMPP_OBJ_ENTRY_TABLE          - object element value / pointer entry table"
25 #warning "KMPP_OBJ_FUNC_IOCTL           - object element ioctl cmd and function table"
26 #warning "KMPP_OBJ_FUNC_INIT            - add object init function"
27 #warning "KMPP_OBJ_FUNC_DEINIT          - add object deinit function"
28 #warning "KMPP_OBJ_FUNC_DUMP            - add object dump function"
29 #warning "KMPP_OBJ_SGLN_ID              - add object singleton id for singleton macro"
30 #warning "KMPP_OBJ_FUNC_EXPORT_DISABLE  - disable function exprot by EXPORT_SYMBOL"
31 #warning "KMPP_OBJ_ACCESS_DISABLE       - disable access function creation"
32 #warning "KMPP_OBJ_SHARE_DISABLE        - disable object sharing by /dev/kmpp_objs to userspace"
33 #warning "KMPP_OBJ_HIERARCHY_ENABLE     - enable hierarchy name creation"
34 #warning "KMPP_OBJ_MISMATCH_LOG_DISABLE - disable entry query mismatch log"
35 
36 #ifndef KMPP_OBJ_NAME
37 #error "KMPP_OBJ_NAME not defined"
38 #endif
39 #ifndef KMPP_OBJ_INTF_TYPE
40 #error "KMPP_OBJ_INTF_TYPE not defined"
41 #endif
42 
43 #else /* all input macro defined */
44 
45 #include <linux/stddef.h>
46 
47 #ifndef KMPP_OBJ_PRIV_SIZE
48 #define KMPP_OBJ_PRIV_SIZE     0
49 #endif
50 
51 #ifndef KMPP_OBJ_EXTRA_SIZE
52 #define KMPP_OBJ_EXTRA_SIZE     0
53 #endif
54 
55 #ifndef KMPP_OBJ_ENTRY_TABLE
56 #define KMPP_OBJ_ENTRY_TABLE(prefix, ENTRY, STRCT, EHOOK, SHOOK, ALIAS)
57 #endif
58 
59 #ifdef KMPP_OBJ_IMPL_TYPE
60 #ifdef KMPP_OBJ_HIERARCHY_ENABLE
61 
62 #define MPP_CFG_TYPE_ptr MPP_CFG_TYPE_OBJECT
63 #define MPP_CFG_TYPE_st  MPP_CFG_TYPE_OBJECT
64 
65 #define ENTRY_TO_TRIE(prefix, ftype, type, name, flag, ...) \
66     do { \
67             KmppEntry tbl = { \
68                 .tbl.elem_offset = ((size_t)&(((KMPP_OBJ_IMPL_TYPE *)0)->CONCAT_DOT(__VA_ARGS__))), \
69                 .tbl.elem_size = sizeof(((KMPP_OBJ_IMPL_TYPE *)0)->CONCAT_DOT(__VA_ARGS__)), \
70                 .tbl.elem_type = ELEM_TYPE_##ftype, \
71                 .tbl.flag_offset = FLAG_TYPE_TO_OFFSET(name, flag, #flag), \
72             }; \
73             MppCfgInfo info = { \
74                 .data_type = CFG_FUNC_TYPE_##ftype, \
75                 .flag_offset = tbl.tbl.flag_offset, \
76                 .data_offset = tbl.tbl.elem_offset, \
77                 .data_size = tbl.tbl.elem_size, \
78             }; \
79             MppCfgObj CONCAT_US(obj, name) = NULL; \
80             kmpp_objdef_add_entry(KMPP_OBJ_DEF(prefix), ENTRY_TO_NAME_START(name), &tbl); \
81             mpp_cfg_get_object(&CONCAT_US(obj, name), TO_STR(name), MPP_CFG_TYPE_##ftype, NULL); \
82             mpp_cfg_set_info(CONCAT_US(obj, name), &info); \
83             mpp_cfg_add(__parent, CONCAT_US(obj, name)); \
84             ENTRY_TO_NAME_END(name); \
85     } while (0);
86 #else
87 #define ENTRY_TO_TRIE(prefix, ftype, type, name, flag, ...) \
88     do { \
89         KmppEntry tbl = { \
90             .tbl.elem_offset = ((size_t)&(((KMPP_OBJ_IMPL_TYPE *)0)->CONCAT_DOT(__VA_ARGS__))), \
91             .tbl.elem_size = sizeof(((KMPP_OBJ_IMPL_TYPE *)0)->CONCAT_DOT(__VA_ARGS__)), \
92             .tbl.elem_type = ELEM_TYPE_##ftype, \
93             .tbl.flag_offset = FLAG_TYPE_TO_OFFSET(name, flag, #flag), \
94         }; \
95         kmpp_objdef_add_entry(KMPP_OBJ_DEF(prefix), ENTRY_TO_NAME_START(name), &tbl); \
96         ENTRY_TO_NAME_END(name); \
97     } while (0);
98 #endif /* KMPP_OBJ_HIERARCHY_ENABLE */
99 #else
100 #define ENTRY_TO_TRIE(prefix, ftype, type, name, flag, ...)
101 #endif /* KMPP_OBJ_IMPL_TYPE */
102 
103 #if !defined(KMPP_OBJ_ACCESS_DISABLE)
104 #define VAL_ENTRY_TBL(prefix, ftype, type, name, flag, ...) \
105     static KmppEntry *CONCAT_US(tbl, prefix, __VA_ARGS__) = NULL;
106 
107 #define VAL_HOOK_IDX(prefix, ftype, type, name, flag, ...) \
108     static rk_s32 CONCAT_US(hook, prefix, get, __VA_ARGS__) = -1; \
109     static rk_s32 CONCAT_US(hook, prefix, set, __VA_ARGS__) = -1;
110 
111 #define ENTRY_QUERY(prefix, ftype, type, name, flag, ...) \
112     do { \
113         kmpp_objdef_get_entry(KMPP_OBJ_DEF(prefix), ENTRY_TO_NAME_START(name), &CONCAT_US(tbl, prefix, __VA_ARGS__)); \
114     } while (0);
115 
116 #define HOOK_QUERY(prefix, ftype, type, name, flag, ...) \
117     do { \
118         CONCAT_US(hook, prefix, set, __VA_ARGS__) = \
119         kmpp_objdef_get_hook(KMPP_OBJ_DEF(prefix), CONCAT_STR(set, __VA_ARGS__)); \
120         CONCAT_US(hook, prefix, get, __VA_ARGS__) = \
121         kmpp_objdef_get_hook(KMPP_OBJ_DEF(prefix), CONCAT_STR(get, __VA_ARGS__)); \
122     } while (0);
123 
124 #ifdef KMPP_OBJ_IMPL_TYPE
125 #define ENTRY_TO_FUNC(prefix, ftype, type, name, flag, ...) \
126     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
127     { \
128         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
129         if (ret) return ret; \
130         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
131             ret = kmpp_obj_tbl_get_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
132         else \
133             *v = ((KMPP_OBJ_IMPL_TYPE*)kmpp_obj_to_entry(s))->CONCAT_DOT(__VA_ARGS__); \
134         return ret; \
135     } \
136     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type v) \
137     { \
138         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
139         if (ret) return ret; \
140         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
141             ret = kmpp_obj_tbl_set_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
142         else \
143             ((KMPP_OBJ_IMPL_TYPE*)kmpp_obj_to_entry(s))->CONCAT_DOT(__VA_ARGS__) = v; \
144         return ret; \
145     } \
146     rk_s32 CONCAT_US(prefix, test, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s) \
147     { \
148         if (kmpp_obj_check(s, __FUNCTION__)) return 0; \
149         return kmpp_obj_tbl_test(s, CONCAT_US(tbl, prefix, __VA_ARGS__)); \
150     }
151 
152 #define STRUCT_TO_FUNC(prefix, ftype, type, name, flag, ...) \
153     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
154     { \
155         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
156         if (ret) return ret; \
157         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
158             ret = kmpp_obj_tbl_get_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
159         else \
160             memcpy(v, &((KMPP_OBJ_IMPL_TYPE*)kmpp_obj_to_entry(s))->CONCAT_DOT(__VA_ARGS__), \
161                    sizeof(((KMPP_OBJ_IMPL_TYPE*)0)->CONCAT_DOT(__VA_ARGS__))); \
162         return ret; \
163     } \
164     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
165     { \
166         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
167         if (ret) return ret; \
168         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
169             ret = kmpp_obj_tbl_set_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
170         else \
171             memcpy(&((KMPP_OBJ_IMPL_TYPE*)kmpp_obj_to_entry(s))->CONCAT_DOT(__VA_ARGS__), v, \
172                    sizeof(((KMPP_OBJ_IMPL_TYPE*)0)->CONCAT_DOT(__VA_ARGS__))); \
173         return ret; \
174     } \
175     rk_s32 CONCAT_US(prefix, test, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s) \
176     { \
177         if (kmpp_obj_check(s, __FUNCTION__)) return 0; \
178         return kmpp_obj_tbl_test(s, CONCAT_US(tbl, prefix, __VA_ARGS__)); \
179     }
180 #else
181 #define ENTRY_TO_FUNC(prefix, ftype, type, name, flag, ...) \
182     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
183     { \
184         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
185         if (ret) return ret; \
186         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
187             ret = kmpp_obj_tbl_get_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
188         return ret; \
189     } \
190     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type v) \
191     { \
192         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
193         if (ret) return ret; \
194         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
195             ret = kmpp_obj_tbl_set_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
196         return ret; \
197     } \
198     rk_s32 CONCAT_US(prefix, test, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s) \
199     { \
200         if (kmpp_obj_check(s, __FUNCTION__)) return 0; \
201         return kmpp_obj_tbl_test(s, CONCAT_US(tbl, prefix, __VA_ARGS__)); \
202     }
203 
204 #define STRUCT_TO_FUNC(prefix, ftype, type, name, flag, ...) \
205     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
206     { \
207         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
208         if (ret) return ret; \
209         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
210             ret = kmpp_obj_tbl_get_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
211         return ret; \
212     } \
213     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
214     { \
215         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
216         if (ret) return ret; \
217         if (CONCAT_US(tbl, prefix, __VA_ARGS__)) \
218             ret = kmpp_obj_tbl_set_##ftype(s, CONCAT_US(tbl, prefix, __VA_ARGS__), v); \
219         return ret; \
220     } \
221     rk_s32 CONCAT_US(prefix, test, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s) \
222     { \
223         if (kmpp_obj_check(s, __FUNCTION__)) return 0; \
224         return kmpp_obj_tbl_test(s, CONCAT_US(tbl, prefix, __VA_ARGS__)); \
225     }
226 #endif
227 
228 #define EHOOK_TO_FUNC(prefix, ftype, type, name, flag, ...) \
229     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
230     { \
231         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
232         if (ret) return ret; \
233         if (CONCAT_US(hook, prefix, get, __VA_ARGS__) >= 0) \
234             ret = kmpp_obj_idx_run(s, CONCAT_US(hook, prefix, get, __VA_ARGS__), (void *)v, __FUNCTION__); \
235         return ret; \
236     } \
237     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type v) \
238     { \
239         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
240         if (ret) return ret; \
241         if (CONCAT_US(hook, prefix, set, __VA_ARGS__) >= 0) \
242             ret = kmpp_obj_idx_run(s, CONCAT_US(hook, prefix, set, __VA_ARGS__), (void *)&v, __FUNCTION__); \
243         return ret; \
244     }
245 
246 #define SHOOK_TO_FUNC(prefix, ftype, type, name, flag, ...) \ \
247     rk_s32 CONCAT_US(prefix, get, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
248     { \
249         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
250         if (ret) return ret; \
251         if (CONCAT_US(hook, prefix, get, __VA_ARGS__) >= 0) \
252             ret = kmpp_obj_idx_run(s, CONCAT_US(hook, prefix, get, __VA_ARGS__), (void *)v, __FUNCTION__); \
253         return ret; \
254     } \
255     rk_s32 CONCAT_US(prefix, set, __VA_ARGS__)(KMPP_OBJ_INTF_TYPE s, type *v) \
256     { \
257         rk_s32 ret = kmpp_obj_check(s, __FUNCTION__); \
258         if (ret) return ret; \
259         if (CONCAT_US(hook, prefix, set, __VA_ARGS__) >= 0) \
260             ret = kmpp_obj_idx_run(s, CONCAT_US(prefix, set, __VA_ARGS__), (void *)v, __FUNCTION__); \
261         return ret; \
262     }
263 
264 #else
265 #define VAL_ENTRY_TBL   ENTRY_NOTHING
266 #define VAL_HOOK_IDX    ENTRY_NOTHING
267 #define ENTRY_QUERY     ENTRY_NOTHING
268 #define HOOK_QUERY      ENTRY_NOTHING
269 #define ENTRY_TO_FUNC   ENTRY_NOTHING
270 #define STRUCT_TO_FUNC  ENTRY_NOTHING
271 #define EHOOK_TO_FUNC   ENTRY_NOTHING
272 #define SHOOK_TO_FUNC   ENTRY_NOTHING
273 #endif
274 
275 /* disable structure layout macro for global variable definition */
276 #undef CFG_DEF_START
277 #undef CFG_DEF_END
278 #undef STRUCT_START
279 #undef STRUCT_END
280 #undef ENTRY_TO_NAME_START
281 #undef ENTRY_TO_NAME_END
282 
283 #define CFG_DEF_START(...)
284 #define CFG_DEF_END(...)
285 #define STRUCT_START(...)
286 #define STRUCT_END(...)
287 #define ENTRY_TO_NAME_START(name, ...) TO_STR(name)
288 #define ENTRY_TO_NAME_END(...)
289 
290 /* object definition common functions */
291 static KmppObjDef KMPP_OBJ_DEF(KMPP_OBJ_NAME) = NULL;
292 static rk_u32 KMPP_OBJ_DEF_DEUBG(KMPP_OBJ_NAME) = 0;
293 
294 /* globla variable definitions */
KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME,VAL_ENTRY_TBL,VAL_ENTRY_TBL,VAL_HOOK_IDX,VAL_HOOK_IDX,ENTRY_NOTHING)295 KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME, VAL_ENTRY_TBL, VAL_ENTRY_TBL,
296                      VAL_HOOK_IDX, VAL_HOOK_IDX, ENTRY_NOTHING)
297 
298 /* enable structure layout macro for objdef registration */
299 #ifdef KMPP_OBJ_HIERARCHY_ENABLE
300 #undef CFG_DEF_START
301 #undef CFG_DEF_END
302 #undef STRUCT_START
303 #undef STRUCT_END
304 #undef ENTRY_TO_NAME_START
305 #undef ENTRY_TO_NAME_END
306 
307 #define CFG_DEF_START(...) \
308     { \
309         char str_buf[256] = {0}; \
310         rk_s32 str_pos = 0; \
311         rk_s32 str_size = sizeof(str_buf) - 1; \
312         MppCfgObj root = NULL; \
313         MppCfgObj __parent = NULL; \
314         if (once) { \
315             mpp_cfg_get_object(&root, NULL, MPP_CFG_TYPE_OBJECT, NULL); \
316             __parent = root; \
317         }
318 
319 #define CFG_DEF_END(...) \
320         { \
321             if (once) { \
322                 kmpp_objdef_add_cfg_root(KMPP_OBJ_DEF(KMPP_OBJ_NAME), root); \
323             } \
324         } \
325     }
326 
327 #define STRUCT_START(...) \
328     { \
329         rk_s32 CONCAT_US(pos, __VA_ARGS__, root) = str_pos; \
330         MppCfgObj CONCAT_US(obj, __VA_ARGS__) = NULL; \
331         MppCfgObj CONCAT_US(__parent, __VA_ARGS__) = __parent; \
332         if (once) { \
333             str_pos += snprintf(str_buf + str_pos, str_size - str_pos, \
334                                 str_pos ? ":%s" : "%s", CONCAT_STR(__VA_ARGS__)); \
335             mpp_cfg_get_object(&CONCAT_US(obj, __VA_ARGS__), CONCAT_STR(__VA_ARGS__), MPP_CFG_TYPE_OBJECT, NULL); \
336             mpp_cfg_add(CONCAT_US(__parent, __VA_ARGS__), CONCAT_US(obj, __VA_ARGS__)); \
337             __parent = CONCAT_US(obj, __VA_ARGS__); \
338         }
339 
340 #define STRUCT_END(...) \
341         str_pos = CONCAT_US(pos, __VA_ARGS__, root); \
342         str_buf[str_pos] = '\0'; \
343         if (__parent) \
344             __parent = CONCAT_US(__parent, __VA_ARGS__); \
345     }
346 
347 #define ENTRY_TO_NAME_START(name, ...) \
348     ({ \
349         snprintf(str_buf + str_pos, str_size - str_pos, str_pos ? ":%s" : "%s", TO_STR(name)); \
350         str_buf; \
351     })
352 
353 #define ENTRY_TO_NAME_END(...) \
354     str_buf[str_pos] = '\0';
355 
356 #endif
357 
358 static void CONCAT_US(KMPP_OBJ_NAME, register)(void)
359 {
360     rk_u32 once = 1;
361 
362     mpp_env_get_u32(TO_STR(CONCAT_US(KMPP_OBJ_NAME, debug)), &KMPP_OBJ_DEF_DEUBG(KMPP_OBJ_NAME), 0);
363 
364     KMPP_OBJ_DBG_LOG("register enter\n");
365 
366     kmpp_objdef_get(&KMPP_OBJ_DEF(KMPP_OBJ_NAME), KMPP_OBJ_PRIV_SIZE, TO_STR(KMPP_OBJ_INTF_TYPE));
367     if (KMPP_OBJ_DEF(KMPP_OBJ_NAME)) {
368         KMPP_OBJ_DBG_LOG(TO_STR(KMPP_OBJ_NAME) " found at kernel\n");
369     } else {
370 #ifdef KMPP_OBJ_IMPL_TYPE
371         rk_s32 impl_size = (sizeof(KMPP_OBJ_IMPL_TYPE) + KMPP_OBJ_EXTRA_SIZE + 3) & ~3;
372         rk_s32 __flag_base = impl_size << 3;
373         rk_s32 __flag_step = 0;
374         rk_s32 __flag_prev = 0;
375         rk_s32 __flag_record[ELEM_FLAG_RECORD_MAX];
376         (void) __flag_base;
377         (void) __flag_step;
378         (void) __flag_prev;
379         (void) __flag_record;
380 
381         kmpp_objdef_register(&KMPP_OBJ_DEF(KMPP_OBJ_NAME), KMPP_OBJ_PRIV_SIZE,
382                              impl_size, TO_STR(KMPP_OBJ_INTF_TYPE));
383 
384         if (!KMPP_OBJ_DEF(KMPP_OBJ_NAME)) {
385             mpp_loge_f(TO_STR(KMPP_OBJ_NAME) " init failed\n");
386             return;
387         }
388 
389         KMPP_OBJ_DBG_LOG(TO_STR(KMPP_OBJ_NAME) " registered at userspace\n");
390 
391         KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME, ENTRY_TO_TRIE, ENTRY_TO_TRIE,
392                              ENTRY_TO_TRIE, ENTRY_TO_TRIE, ENTRY_TO_TRIE)
393         kmpp_objdef_add_entry(KMPP_OBJ_DEF(KMPP_OBJ_NAME), NULL, NULL);
394         once = 0;
395 #else
396         KMPP_OBJ_DBG_LOG(TO_STR(KMPP_OBJ_NAME) " has no implementation\n");
397         return;
398 #endif
399     }
400 
401 #if defined(KMPP_OBJ_MISMATCH_LOG_DISABLE)
402     kmpp_objdef_set_prop(KMPP_OBJ_DEF(KMPP_OBJ_NAME), "disable_mismatch_log", 1);
403 #endif
404 
405     KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME, ENTRY_QUERY, ENTRY_QUERY,
406                          HOOK_QUERY, HOOK_QUERY, ENTRY_NOTHING);
407 
408 #if defined(KMPP_OBJ_FUNC_INIT)
409     kmpp_objdef_add_init(KMPP_OBJ_DEF(KMPP_OBJ_NAME), KMPP_OBJ_FUNC_INIT);
410 #endif
411 #if defined(KMPP_OBJ_FUNC_DEINIT)
412     kmpp_objdef_add_deinit(KMPP_OBJ_DEF(KMPP_OBJ_NAME), KMPP_OBJ_FUNC_DEINIT);
413 #endif
414 #if defined(KMPP_OBJ_FUNC_DUMP)
415     kmpp_objdef_add_dump(KMPP_OBJ_DEF(KMPP_OBJ_NAME), KMPP_OBJ_FUNC_DUMP);
416 #endif
417 #if !defined(KMPP_OBJ_SHARE_DISABLE) && defined(__KERNEL__)
418     kmpp_objdef_share(KMPP_OBJ_DEF(KMPP_OBJ_NAME));
419 #endif
420     KMPP_OBJ_DBG_LOG("register leave\n");
421 }
422 
CONCAT_US(KMPP_OBJ_NAME,unregister)423 static void CONCAT_US(KMPP_OBJ_NAME, unregister)(void)
424 {
425     KmppObjDef def = __sync_fetch_and_and(&KMPP_OBJ_DEF(KMPP_OBJ_NAME), NULL);
426 
427     KMPP_OBJ_DBG_LOG("unregister enter\n");
428     kmpp_objdef_put(def);
429     KMPP_OBJ_DBG_LOG("unregister leave\n");
430 }
431 
432 MPP_SINGLETON(KMPP_OBJ_SGLN_ID, KMPP_OBJ_NAME, CONCAT_US(KMPP_OBJ_NAME, register), CONCAT_US(KMPP_OBJ_NAME, unregister));
433 
CONCAT_US(KMPP_OBJ_NAME,size)434 rk_s32 CONCAT_US(KMPP_OBJ_NAME, size)(void)
435 {
436     return kmpp_objdef_get_entry_size(KMPP_OBJ_DEF(KMPP_OBJ_NAME));
437 }
438 
CONCAT_US(KMPP_OBJ_NAME,get)439 rk_s32 CONCAT_US(KMPP_OBJ_NAME, get)(KMPP_OBJ_INTF_TYPE *obj)
440 {
441     return kmpp_obj_get_f((KmppObj *)obj, KMPP_OBJ_DEF(KMPP_OBJ_NAME));
442 }
443 
CONCAT_US(KMPP_OBJ_NAME,put)444 rk_s32 CONCAT_US(KMPP_OBJ_NAME, put)(KMPP_OBJ_INTF_TYPE obj)
445 {
446     return kmpp_obj_put_f(obj);
447 }
448 
CONCAT_US(KMPP_OBJ_NAME,dump)449 rk_s32 CONCAT_US(KMPP_OBJ_NAME, dump)(KMPP_OBJ_INTF_TYPE obj, const char *caller)
450 {
451     if (!obj)
452         return rk_nok;
453 
454     return kmpp_obj_is_kobj(obj) ? kmpp_obj_kdump_f(obj, caller) : kmpp_obj_udump_f(obj, caller);
455 }
456 
CONCAT_US(KMPP_OBJ_NAME,objdef)457 KmppObjDef CONCAT_US(KMPP_OBJ_NAME, objdef)(void)
458 {
459     return KMPP_OBJ_DEF(KMPP_OBJ_NAME);
460 }
461 
462 #if !defined(KMPP_OBJ_FUNC_EXPORT_DISABLE) && defined(__KERNEL__)
463 #include <linux/export.h>
464 
465 EXPORT_SYMBOL(CONCAT_US(KMPP_OBJ_NAME, size));
466 EXPORT_SYMBOL(CONCAT_US(KMPP_OBJ_NAME, get));
467 EXPORT_SYMBOL(CONCAT_US(KMPP_OBJ_NAME, put));
468 EXPORT_SYMBOL(CONCAT_US(KMPP_OBJ_NAME, dump));
469 #endif
470 
471 /* disable structure layout macro for access function definition */
472 #undef CFG_DEF_START
473 #undef CFG_DEF_END
474 #undef STRUCT_START
475 #undef STRUCT_END
476 #undef ENTRY_TO_NAME_START
477 #undef ENTRY_TO_NAME_END
478 
479 #define CFG_DEF_START(...)
480 #define CFG_DEF_END(...)
481 #define STRUCT_START(...)
482 #define STRUCT_END(...)
483 
484 #if !defined(KMPP_OBJ_ACCESS_DISABLE)
485 /* object element access functions */
KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME,ENTRY_TO_FUNC,STRUCT_TO_FUNC,EHOOK_TO_FUNC,SHOOK_TO_FUNC,ENTRY_NOTHING)486 KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME, ENTRY_TO_FUNC, STRUCT_TO_FUNC,
487                      EHOOK_TO_FUNC, SHOOK_TO_FUNC, ENTRY_NOTHING)
488 
489 #if !defined(KMPP_OBJ_FUNC_EXPORT_DISABLE) && defined(__KERNEL__)
490 #define KMPP_OBJ_EXPORT(prefix, ftype, type, name, flag, ...) \
491     EXPORT_SYMBOL(CONCAT_US(prefix, get, __VA_ARGS__)); \
492     EXPORT_SYMBOL(CONCAT_US(prefix, set, __VA_ARGS__));
493 
494 #define KMPP_OBJ_EXPORT_NONE(prefix, ftype, type, name, flag, ...)
495 
496 KMPP_OBJ_ENTRY_TABLE(KMPP_OBJ_NAME, KMPP_OBJ_EXPORT, KMPP_OBJ_EXPORT,
497                      KMPP_OBJ_EXPORT, KMPP_OBJ_EXPORT, KMPP_OBJ_EXPORT_NONE)
498 #undef KMPP_OBJ_EXPORT
499 #endif
500 #endif
501 
502 /* restore layout definition */
503 #undef CFG_DEF_START
504 #undef CFG_DEF_END
505 #undef STRUCT_START
506 #undef STRUCT_END
507 
508 /* kmpp_obj ioctl function */
509 #ifdef KMPP_OBJ_FUNC_IOCTL
510 
511 #define IOCTL_CTX(prefix, func, ...) \
512     rk_s32 CONCAT_US(prefix, func)(KMPP_OBJ_INTF_TYPE ctx) \
513     { \
514         static rk_s32 old_cmd = GET_ARG0(-1, __VA_ARGS__); \
515         static rk_s32 cmd = -1; \
516         static rk_s32 once = 1; \
517         KmppObjDef def = KMPP_OBJ_DEF(KMPP_OBJ_NAME); \
518         if (!def) { \
519             mpp_loge_f(TO_STR(KMPP_OBJ_NAME) " can not be found for ioctl\n"); \
520             return rk_nok; \
521         } \
522         /* legacy compatible */ \
523         if (old_cmd >= 0) \
524             return kmpp_obj_ioctl(ctx, old_cmd, ctx, NULL, __FUNCTION__); \
525         if (cmd < 0) { \
526             cmd = kmpp_objdef_get_cmd(def, TO_STR(func)); \
527             if (cmd < 0) { \
528                 mpp_loge_cf(once, TO_STR(KMPP_OBJ_NAME) " ioctl cmd %s not supported\n", TO_STR(func)); \
529                 once = 0; \
530                 return rk_nok; \
531             } \
532         } \
533         return kmpp_obj_ioctl(ctx, cmd, NULL, NULL, __FUNCTION__); \
534     }
535 
536 #define IOCTL_IN_(prefix, func, in_type, ...) \
537     rk_s32 CONCAT_US(prefix, func)(KMPP_OBJ_INTF_TYPE ctx, in_type in) \
538     { \
539         KmppObjDef def = KMPP_OBJ_DEF(KMPP_OBJ_NAME); \
540         static rk_s32 cmd = -1; \
541         static rk_s32 once = 1; \
542         if (!def) { \
543             mpp_loge_f(TO_STR(KMPP_OBJ_NAME) " can not be found for ioctl\n"); \
544             return rk_nok; \
545         } \
546         if (cmd < 0) { \
547             cmd = kmpp_objdef_get_cmd(def, TO_STR(func)); \
548             if (cmd < 0) { \
549                 mpp_loge_cf(once, TO_STR(KMPP_OBJ_NAME) " ioctl cmd %s not supported\n", TO_STR(func)); \
550                 once = 0; \
551                 return rk_nok; \
552             } \
553         } \
554         return kmpp_obj_ioctl(ctx, cmd, in, NULL, __FUNCTION__); \
555     }
556 
557 #define IOCTL_OUT(prefix, func, out_type, ...) \
558     rk_s32 CONCAT_US(prefix, func)(KMPP_OBJ_INTF_TYPE ctx, out_type *out) \
559     { \
560         KmppObjDef def = KMPP_OBJ_DEF(KMPP_OBJ_NAME); \
561         static rk_s32 cmd = -1; \
562         static rk_s32 once = 1; \
563         if (!def) { \
564             mpp_loge_f(TO_STR(KMPP_OBJ_NAME) " can not be found for ioctl\n"); \
565             return rk_nok; \
566         } \
567         if (cmd < 0) { \
568             cmd = kmpp_objdef_get_cmd(def, TO_STR(func)); \
569             if (cmd < 0) { \
570                 mpp_loge_cf(once, TO_STR(KMPP_OBJ_NAME) " ioctl cmd %s not supported\n", TO_STR(func)); \
571                 once = 0; \
572                 return rk_nok; \
573             } \
574         } \
575         return kmpp_obj_ioctl(ctx, cmd, NULL, out, __FUNCTION__); \
576     }
577 
578 #define IOCTL_IO_(prefix, func, in_type, out_type, ...) \
579     rk_s32 CONCAT_US(prefix, func)(KMPP_OBJ_INTF_TYPE ctx, in_type in, out_type *out) \
580     { \
581         KmppObjDef def = KMPP_OBJ_DEF(KMPP_OBJ_NAME); \
582         static rk_s32 cmd = -1; \
583         static rk_s32 once = 1; \
584         if (!def) { \
585             mpp_loge_f(TO_STR(KMPP_OBJ_NAME) " can not be found for ioctl\n"); \
586             return rk_nok; \
587         } \
588         if (cmd < 0) { \
589             cmd = kmpp_objdef_get_cmd(def, TO_STR(func)); \
590             if (cmd < 0) { \
591                 mpp_loge_cf(once, TO_STR(KMPP_OBJ_NAME) " ioctl cmd %s not supported\n", TO_STR(func)); \
592                 once = 0; \
593                 return rk_nok; \
594             } \
595         } \
596         return kmpp_obj_ioctl(ctx, cmd, in, out, __FUNCTION__); \
597     }
598 
599 #ifdef __cplusplus
600 extern "C" {
601 #endif
602 
603     KMPP_OBJ_FUNC_IOCTL(KMPP_OBJ_NAME, IOCTL_CTX, IOCTL_IN_, IOCTL_OUT, IOCTL_IO_)
604 
605 #ifdef __cplusplus
606 }
607 #endif
608 
609 #undef IOCTL_CTX
610 #undef IOCTL_IN_
611 #undef IOCTL_OUT
612 #undef IOCTL_IO_
613 
614 #endif /* KMPP_OBJ_FUNC_IOCTL */
615 
616 #undef KMPP_OBJ_NAME
617 #undef KMPP_OBJ_INTF_TYPE
618 #undef KMPP_OBJ_IMPL_TYPE
619 #undef KMPP_OBJ_EXTRA_SIZE
620 #undef KMPP_OBJ_ENTRY_TABLE
621 #undef KMPP_OBJ_PRIV_SIZE
622 #undef KMPP_OBJ_FUNC_INIT
623 #undef KMPP_OBJ_FUNC_DEINIT
624 #undef KMPP_OBJ_FUNC_IOCTL
625 #undef KMPP_OBJ_FUNC_DUMP
626 #undef KMPP_OBJ_SGLN_ID
627 #undef KMPP_OBJ_FUNC_EXPORT_DISABLE
628 #undef KMPP_OBJ_ACCESS_DISABLE
629 #undef KMPP_OBJ_SHARE_DISABLE
630 #undef KMPP_OBJ_HIERARCHY_ENABLE
631 #undef KMPP_OBJ_MISMATCH_LOG_DISABLE
632 
633 /* undef tmp macro */
634 #undef ENTRY_TO_TRIE
635 #undef ENTRY_TO_FUNC
636 #undef STRUCT_TO_FUNC
637 #undef EHOOK_TO_FUNC
638 #undef SHOOK_TO_FUNC
639 #undef VAL_ENTRY_TBL
640 #undef VAL_HOOK_IDX
641 #undef ENTRY_QUERY
642 #undef HOOK_QUERY
643 #undef MPP_CFG_TYPE_ptr
644 #undef MPP_CFG_TYPE_st
645 
646 #undef __OBJECT_HERLPER_H__
647 
648 #endif
649