1 /****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * Filename: utils.h 22 * 23 * Description: Utility functions declaration 24 * 25 ******************************************************************************/ 26 27 #ifndef UTILS_H 28 #define UTILS_H 29 30 /****************************************************************************** 31 ** Constants & Macros 32 ******************************************************************************/ 33 34 #define STREAM_TO_UINT16(u16, p) {u16 = ((uint16_t)(*(p)) + (((uint16_t)(*((p) + 1))) << 8)); (p) += 2;} 35 #define UINT16_TO_STREAM(p, u16) {*(p)++ = (uint8_t)(u16); *(p)++ = (uint8_t)((u16) >> 8);} 36 #define UINT32_TO_STREAM(p, u32) {*(p)++ = (uint8_t)(u32); *(p)++ = (uint8_t)((u32) >> 8); *(p)++ = (uint8_t)((u32) >> 16); *(p)++ = (uint8_t)((u32) >> 24);} 37 38 /****************************************************************************** 39 ** Type definitions 40 ******************************************************************************/ 41 42 typedef struct 43 { 44 void *p_first; 45 void *p_last; 46 uint16_t count; 47 } BUFFER_Q; 48 49 /****************************************************************************** 50 ** Extern variables and functions 51 ******************************************************************************/ 52 53 /****************************************************************************** 54 ** Functions 55 ******************************************************************************/ 56 57 /******************************************************************************* 58 ** 59 ** Function utils_init 60 ** 61 ** Description Utils initialization 62 ** 63 ** Returns None 64 ** 65 *******************************************************************************/ 66 void utils_init (); 67 68 /******************************************************************************* 69 ** 70 ** Function utils_cleanup 71 ** 72 ** Description Utils cleanup 73 ** 74 ** Returns None 75 ** 76 *******************************************************************************/ 77 void utils_cleanup (); 78 79 /******************************************************************************* 80 ** 81 ** Function utils_queue_init 82 ** 83 ** Description Initialize the given buffer queue 84 ** 85 ** Returns None 86 ** 87 *******************************************************************************/ 88 void utils_queue_init (BUFFER_Q *p_q); 89 90 /******************************************************************************* 91 ** 92 ** Function utils_enqueue 93 ** 94 ** Description Enqueue a buffer at the tail of the given queue 95 ** 96 ** Returns None 97 ** 98 *******************************************************************************/ 99 void utils_enqueue (BUFFER_Q *p_q, void *p_buf); 100 101 /******************************************************************************* 102 ** 103 ** Function utils_dequeue 104 ** 105 ** Description Dequeues a buffer from the head of the given queue 106 ** 107 ** Returns NULL if queue is empty, else buffer 108 ** 109 *******************************************************************************/ 110 void *utils_dequeue (BUFFER_Q *p_q); 111 112 /******************************************************************************* 113 ** 114 ** Function utils_dequeue_unlocked 115 ** 116 ** Description Dequeues a buffer from the head of the given queue without lock 117 ** 118 ** Returns NULL if queue is empty, else buffer 119 ** 120 *******************************************************************************/ 121 void *utils_dequeue_unlocked (BUFFER_Q *p_q); 122 123 /******************************************************************************* 124 ** 125 ** Function utils_getnext 126 ** 127 ** Description Return a pointer to the next buffer linked to the given buffer 128 ** 129 ** Returns NULL if the given buffer does not point to any next buffer, 130 ** else next buffer address 131 ** 132 *******************************************************************************/ 133 void *utils_getnext (void *p_buf); 134 135 /******************************************************************************* 136 ** 137 ** Function utils_remove_from_queue 138 ** 139 ** Description Dequeue the given buffer from the middle of the given queue 140 ** 141 ** Returns NULL if the given queue is empty, else the given buffer 142 ** 143 *******************************************************************************/ 144 void *utils_remove_from_queue (BUFFER_Q *p_q, void *p_buf); 145 146 /******************************************************************************* 147 ** 148 ** Function utils_remove_from_queue_unlocked 149 ** 150 ** Description Dequeue the given buffer from the middle of the given queue without lock 151 ** 152 ** Returns NULL if the given queue is empty, else the given buffer 153 ** 154 *******************************************************************************/ 155 void *utils_remove_from_queue_unlocked (BUFFER_Q *p_q, void *p_buf); 156 157 158 /******************************************************************************* 159 ** 160 ** Function utils_delay 161 ** 162 ** Description sleep unconditionally for timeout milliseconds 163 ** 164 ** Returns None 165 ** 166 *******************************************************************************/ 167 void utils_delay (uint32_t timeout); 168 169 /******************************************************************************* 170 ** 171 ** Function utils_lock 172 ** 173 ** Description application calls this function before entering critical 174 ** section 175 ** 176 ** Returns None 177 ** 178 *******************************************************************************/ 179 void utils_lock (void); 180 181 /******************************************************************************* 182 ** 183 ** Function utils_unlock 184 ** 185 ** Description application calls this function when leaving critical 186 ** section 187 ** 188 ** Returns None 189 ** 190 *******************************************************************************/ 191 void utils_unlock (void); 192 193 #endif /* UTILS_H */ 194 195