1 /* 2 * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup Lockzhiner 18 * 19 * @file base.h 20 * 21 */ 22 #ifndef LZ_HARDWARE_BASE_H 23 #define LZ_HARDWARE_BASE_H 24 25 #define ID2BANK(id, num) ((id) / (num)) 26 #define ID2INDEX(id, num) ((id) % (num)) 27 28 #define CNTLR_INIT(NAME, id, CNTLR_S) \ 29 CNTLR_S *cntlr; \ 30 unsigned int nr_cntlr = sizeof(g_cntlrs) / sizeof(g_cntlrs[0]); \ 31 unsigned int nr_id = (NR_ ## NAME ## _CHN) * nr_cntlr; \ 32 do { \ 33 if ( (id) >= nr_id ) { \ 34 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d is invalid", __func__, (id)); \ 35 return LZ_HARDWARE_FAILURE; \ 36 } \ 37 cntlr = &g_cntlrs[id]; \ 38 if (cntlr->init) { \ 39 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d, controller has already been initialized", __func__, (id)); \ 40 return LZ_HARDWARE_FAILURE; \ 41 } \ 42 } while (0) 43 44 #define CNTLR_READY(NAME, id, CNTLR_S) \ 45 CNTLR_S *cntlr; \ 46 unsigned int nr_cntlr = sizeof(g_cntlrs) / sizeof(g_cntlrs[0]); \ 47 unsigned int nr_id = (NR_ ## NAME ## _CHN) * nr_cntlr; \ 48 do { \ 49 if ( (id) >= nr_id ) { \ 50 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d is invalid", __func__, (id)); \ 51 return LZ_HARDWARE_FAILURE; \ 52 } \ 53 cntlr = &g_cntlrs[id]; \ 54 if (!cntlr->init) { \ 55 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d, controller has NOT been initialized", __func__, (id)); \ 56 return LZ_HARDWARE_FAILURE; \ 57 } \ 58 } while (0) 59 60 #define CNTLR_INIT_M(NAME, id, finit, CNTLR_S, CHN_S) \ 61 CNTLR_S *cntlr; \ 62 CHN_S *chn; \ 63 unsigned int nr_cntlr = sizeof(g_cntlrs) / sizeof(g_cntlrs[0]); \ 64 unsigned int nr_id = (NR_ ## NAME ## _CHN) * nr_cntlr; \ 65 unsigned int bank = ID2BANK(id, (NR_ ## NAME ## _CHN)); \ 66 unsigned int index = ID2INDEX(id, (NR_ ## NAME ## _CHN)); \ 67 do { \ 68 if ( (id) >= nr_id ) { \ 69 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d is invalid", __func__, (id)); \ 70 return LZ_HARDWARE_FAILURE; \ 71 } \ 72 cntlr = &g_cntlrs[bank]; \ 73 if (!cntlr->init) { \ 74 if (finit(cntlr) != LZ_HARDWARE_SUCCESS) \ 75 return LZ_HARDWARE_FAILURE; \ 76 cntlr->init = 1; \ 77 } \ 78 chn = &cntlr->chns[index]; \ 79 if (chn->init) { \ 80 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d, channel has already been initialized", __func__, (id)); \ 81 return LZ_HARDWARE_FAILURE; \ 82 } \ 83 } while (0) 84 85 #define CNTLR_READY_M(NAME, id, CNTLR_S, CHN_S) \ 86 CNTLR_S *cntlr; \ 87 CHN_S *chn; \ 88 unsigned int nr_cntlr = sizeof(g_cntlrs) / sizeof(g_cntlrs[0]); \ 89 unsigned int nr_id = (NR_ ## NAME ## _CHN) * nr_cntlr; \ 90 unsigned int bank = ID2BANK(id, (NR_ ## NAME ## _CHN)); \ 91 unsigned int index = ID2INDEX(id, (NR_ ## NAME ## _CHN)); \ 92 do { \ 93 if ( (id) >= nr_id ) { \ 94 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d is invalid", __func__, (id)); \ 95 return LZ_HARDWARE_FAILURE; \ 96 } \ 97 cntlr = &g_cntlrs[bank]; \ 98 if (!cntlr->init) { \ 99 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d, controller has NOT been initialized", __func__, (id)); \ 100 return LZ_HARDWARE_FAILURE; \ 101 } \ 102 chn = &cntlr->chns[index]; \ 103 if (!chn->init) { \ 104 LZ_HARDWARE_LOGE(NAME ## _TAG, "%s: id %d, channel has NOT been initialized", __func__, (id)); \ 105 return LZ_HARDWARE_FAILURE; \ 106 } \ 107 } while (0) 108 109 #endif