1 //------------------------------------------------------------------------------
2 // This file contains the definitions of the basic atheros data types.
3 // It is used to map the data types in atheros files to a platform specific
4 // type.
5 // Copyright (c) 2004-2010 Atheros Communications Inc.
6 // All rights reserved.
7 //
8 //
9 //
10 // Permission to use, copy, modify, and/or distribute this software for any
11 // purpose with or without fee is hereby granted, provided that the above
12 // copyright notice and this permission notice appear in all copies.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 //
22 //
23 //
24 // Author(s): ="Atheros"
25 //------------------------------------------------------------------------------
26 
27 #ifndef _OSAPI_LINUX_H_
28 #define _OSAPI_LINUX_H_
29 
30 #ifdef __KERNEL__
31 
32 #include <linux/version.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/string.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/jiffies.h>
39 #include <linux/timer.h>
40 #include <linux/delay.h>
41 #include <linux/wait.h>
42 #include <linux/semaphore.h>
43 #include <linux/cache.h>
44 
45 #ifdef __GNUC__
46 #define __ATTRIB_PACK           __attribute__ ((packed))
47 #define __ATTRIB_PRINTF         __attribute__ ((format (printf, 1, 2)))
48 #define __ATTRIB_NORETURN       __attribute__ ((noreturn))
49 #ifndef INLINE
50 #define INLINE                  __inline__
51 #endif
52 #else /* Not GCC */
53 #define __ATTRIB_PACK
54 #define __ATTRIB_PRINTF
55 #define __ATTRIB_NORETURN
56 #ifndef INLINE
57 #define INLINE                  __inline
58 #endif
59 #endif /* End __GNUC__ */
60 
61 #define PREPACK
62 #define POSTPACK                __ATTRIB_PACK
63 
64 /*
65  * Endianes macros
66  */
67 #define A_BE2CPU8(x)       ntohb(x)
68 #define A_BE2CPU16(x)      ntohs(x)
69 #define A_BE2CPU32(x)      ntohl(x)
70 
71 #define A_LE2CPU8(x)       (x)
72 #define A_LE2CPU16(x)      (x)
73 #define A_LE2CPU32(x)      (x)
74 
75 #define A_CPU2BE8(x)       htonb(x)
76 #define A_CPU2BE16(x)      htons(x)
77 #define A_CPU2BE32(x)      htonl(x)
78 
79 #define A_MEMZERO(addr, len)            memset(addr, 0, len)
80 #define A_MALLOC(size)                  kmalloc((size), GFP_KERNEL)
81 #define A_MALLOC_NOWAIT(size)           kmalloc((size), GFP_ATOMIC)
82 #define A_FREE(addr)                    kfree(addr)
83 
84 #if defined(ANDROID_ENV) && defined(CONFIG_ANDROID_LOGGER)
85 extern unsigned int enablelogcat;
86 extern int android_logger_lv(void* module, int mask);
87 enum logidx { LOG_MAIN_IDX = 0 };
88 extern int logger_write(const enum logidx idx,
89                 const unsigned char prio,
90                 const char __kernel * const tag,
91                 const char __kernel * const fmt,
92                 ...);
93 #define A_ANDROID_PRINTF(mask, module, tags, args...) do {  \
94     if (enablelogcat) \
95         logger_write(LOG_MAIN_IDX, android_logger_lv(module, mask), tags, args); \
96     else \
97         printk(KERN_ALERT args); \
98 } while (0)
99 #ifdef DEBUG
100 #define A_LOGGER_MODULE_NAME(x) #x
101 #define A_LOGGER(mask, mod, args...) \
102     A_ANDROID_PRINTF(mask, &GET_ATH_MODULE_DEBUG_VAR_NAME(mod), "ar6k_" A_LOGGER_MODULE_NAME(mod), args);
103 #endif
104 #define A_PRINTF(args...) A_ANDROID_PRINTF(ATH_DEBUG_INFO, NULL, "ar6k_driver", args)
105 #else
106 #define A_LOGGER(mask, mod, args...)    printk(KERN_ALERT args)
107 #define A_PRINTF(args...)               printk(KERN_ALERT args)
108 #endif /* ANDROID */
109 #define A_PRINTF_LOG(args...)           printk(args)
110 #define A_SPRINTF(buf, args...)			sprintf (buf, args)
111 
112 /* Mutual Exclusion */
113 typedef spinlock_t                      A_MUTEX_T;
114 #define A_MUTEX_INIT(mutex)             spin_lock_init(mutex)
115 #define A_MUTEX_LOCK(mutex)             spin_lock_bh(mutex)
116 #define A_MUTEX_UNLOCK(mutex)           spin_unlock_bh(mutex)
117 #define A_IS_MUTEX_VALID(mutex)         true  /* okay to return true, since A_MUTEX_DELETE does nothing */
118 #define A_MUTEX_DELETE(mutex)           /* spin locks are not kernel resources so nothing to free.. */
119 
120 /* Get current time in ms adding a constant offset (in ms) */
121 #define A_GET_MS(offset)    \
122 	(((jiffies / HZ) * 1000) + (offset))
123 
124 /*
125  * Timer Functions
126  */
127 #define A_MDELAY(msecs)                 mdelay(msecs)
128 typedef struct timer_list               A_TIMER;
129 
130 #define A_INIT_TIMER(pTimer, pFunction, pArg) do {              \
131     init_timer(pTimer);                                         \
132     (pTimer)->function = (pFunction);                           \
133     (pTimer)->data   = (unsigned long)(pArg);                   \
134 } while (0)
135 
136 /*
137  * Start a Timer that elapses after 'periodMSec' milli-seconds
138  * Support is provided for a one-shot timer. The 'repeatFlag' is
139  * ignored.
140  */
141 #define A_TIMEOUT_MS(pTimer, periodMSec, repeatFlag) do {                   \
142     if (repeatFlag) {                                                       \
143         printk("\n" __FILE__ ":%d: Timer Repeat requested\n",__LINE__);     \
144         panic("Timer Repeat");                                              \
145     }                                                                       \
146     mod_timer((pTimer), jiffies + HZ * (periodMSec) / 1000);                \
147 } while (0)
148 
149 /*
150  * Cancel the Timer.
151  */
152 #define A_UNTIMEOUT(pTimer) do {                                \
153     del_timer((pTimer));                                        \
154 } while (0)
155 
156 #define A_DELETE_TIMER(pTimer) do {                             \
157 } while (0)
158 
159 /*
160  * Wait Queue related functions
161  */
162 typedef wait_queue_head_t               A_WAITQUEUE_HEAD;
163 #define A_INIT_WAITQUEUE_HEAD(head)     init_waitqueue_head(head)
164 #ifndef wait_event_interruptible_timeout
165 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
166 do {                                                                    \
167         wait_queue_t __wait;                                            \
168         init_waitqueue_entry(&__wait, current);                         \
169                                                                         \
170         add_wait_queue(&wq, &__wait);                                   \
171         for (;;) {                                                      \
172                 set_current_state(TASK_INTERRUPTIBLE);                  \
173                 if (condition)                                          \
174                         break;                                          \
175                 if (!signal_pending(current)) {                         \
176                         ret = schedule_timeout(ret);                    \
177                         if (!ret)                                       \
178                                 break;                                  \
179                         continue;                                       \
180                 }                                                       \
181                 ret = -ERESTARTSYS;                                     \
182                 break;                                                  \
183         }                                                               \
184         current->state = TASK_RUNNING;                                  \
185         remove_wait_queue(&wq, &__wait);                                \
186 } while (0)
187 
188 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
189 ({                                                                      \
190         long __ret = timeout;                                           \
191         if (!(condition))                                               \
192                 __wait_event_interruptible_timeout(wq, condition, __ret); \
193         __ret;                                                          \
194 })
195 #endif /* wait_event_interruptible_timeout */
196 
197 #define A_WAIT_EVENT_INTERRUPTIBLE_TIMEOUT(head, condition, timeout) do { \
198     wait_event_interruptible_timeout(head, condition, timeout); \
199 } while (0)
200 
201 #define A_WAKE_UP(head)                 wake_up(head)
202 
203 #ifdef DEBUG
204 extern unsigned int panic_on_assert;
205 #define A_ASSERT(expr)  \
206     if (!(expr)) {   \
207         printk(KERN_ALERT"Debug Assert Caught, File %s, Line: %d, Test:%s \n",__FILE__, __LINE__,#expr); \
208         if (panic_on_assert) panic(#expr);                                                               \
209     }
210 #else
211 #define A_ASSERT(expr)
212 #endif /* DEBUG */
213 
214 #ifdef ANDROID_ENV
215 struct firmware;
216 int android_request_firmware(const struct firmware **firmware_p, const char *filename,
217                      struct device *device);
218 void android_release_firmware(const struct firmware *firmware);
219 #define A_REQUEST_FIRMWARE(_ppf, _pfile, _dev) android_request_firmware(_ppf, _pfile, _dev)
220 #define A_RELEASE_FIRMWARE(_pf) android_release_firmware(_pf)
221 #else
222 #define A_REQUEST_FIRMWARE(_ppf, _pfile, _dev) request_firmware(_ppf, _pfile, _dev)
223 #define A_RELEASE_FIRMWARE(_pf) release_firmware(_pf)
224 #endif
225 
226 /*
227  * Initialization of the network buffer subsystem
228  */
229 #define A_NETBUF_INIT()
230 
231 /*
232  * Network buffer queue support
233  */
234 typedef struct sk_buff_head A_NETBUF_QUEUE_T;
235 
236 #define A_NETBUF_QUEUE_INIT(q)  \
237     a_netbuf_queue_init(q)
238 
239 #define A_NETBUF_ENQUEUE(q, pkt) \
240     a_netbuf_enqueue((q), (pkt))
241 #define A_NETBUF_PREQUEUE(q, pkt) \
242     a_netbuf_prequeue((q), (pkt))
243 #define A_NETBUF_DEQUEUE(q) \
244     (a_netbuf_dequeue(q))
245 #define A_NETBUF_QUEUE_SIZE(q)  \
246     a_netbuf_queue_size(q)
247 #define A_NETBUF_QUEUE_EMPTY(q) \
248     (a_netbuf_queue_empty(q) ? true : false)
249 
250 /*
251  * Network buffer support
252  */
253 #define A_NETBUF_ALLOC(size) \
254     a_netbuf_alloc(size)
255 #define A_NETBUF_ALLOC_RAW(size) \
256     a_netbuf_alloc_raw(size)
257 #define A_NETBUF_FREE(bufPtr) \
258     a_netbuf_free(bufPtr)
259 #define A_NETBUF_DATA(bufPtr) \
260     a_netbuf_to_data(bufPtr)
261 #define A_NETBUF_LEN(bufPtr) \
262     a_netbuf_to_len(bufPtr)
263 #define A_NETBUF_PUSH(bufPtr, len) \
264     a_netbuf_push(bufPtr, len)
265 #define A_NETBUF_PUT(bufPtr, len) \
266     a_netbuf_put(bufPtr, len)
267 #define A_NETBUF_TRIM(bufPtr,len) \
268     a_netbuf_trim(bufPtr, len)
269 #define A_NETBUF_PULL(bufPtr, len) \
270     a_netbuf_pull(bufPtr, len)
271 #define A_NETBUF_HEADROOM(bufPtr)\
272     a_netbuf_headroom(bufPtr)
273 #define A_NETBUF_SETLEN(bufPtr,len) \
274     a_netbuf_setlen(bufPtr, len)
275 
276 /* Add data to end of a buffer  */
277 #define A_NETBUF_PUT_DATA(bufPtr, srcPtr,  len) \
278     a_netbuf_put_data(bufPtr, srcPtr, len)
279 
280 /* Add data to start of the  buffer */
281 #define A_NETBUF_PUSH_DATA(bufPtr, srcPtr,  len) \
282     a_netbuf_push_data(bufPtr, srcPtr, len)
283 
284 /* Remove data at start of the buffer */
285 #define A_NETBUF_PULL_DATA(bufPtr, dstPtr, len) \
286     a_netbuf_pull_data(bufPtr, dstPtr, len)
287 
288 /* Remove data from the end of the buffer */
289 #define A_NETBUF_TRIM_DATA(bufPtr, dstPtr, len) \
290     a_netbuf_trim_data(bufPtr, dstPtr, len)
291 
292 /* View data as "size" contiguous bytes of type "t" */
293 #define A_NETBUF_VIEW_DATA(bufPtr, t, size) \
294     (t )( ((struct skbuf *)(bufPtr))->data)
295 
296 /* return the beginning of the headroom for the buffer */
297 #define A_NETBUF_HEAD(bufPtr) \
298         ((((struct sk_buff *)(bufPtr))->head))
299 
300 /*
301  * OS specific network buffer access routines
302  */
303 void *a_netbuf_alloc(int size);
304 void *a_netbuf_alloc_raw(int size);
305 void a_netbuf_free(void *bufPtr);
306 void *a_netbuf_to_data(void *bufPtr);
307 u32 a_netbuf_to_len(void *bufPtr);
308 int a_netbuf_push(void *bufPtr, s32 len);
309 int a_netbuf_push_data(void *bufPtr, char *srcPtr, s32 len);
310 int a_netbuf_put(void *bufPtr, s32 len);
311 int a_netbuf_put_data(void *bufPtr, char *srcPtr, s32 len);
312 int a_netbuf_pull(void *bufPtr, s32 len);
313 int a_netbuf_pull_data(void *bufPtr, char *dstPtr, s32 len);
314 int a_netbuf_trim(void *bufPtr, s32 len);
315 int a_netbuf_trim_data(void *bufPtr, char *dstPtr, s32 len);
316 int a_netbuf_setlen(void *bufPtr, s32 len);
317 s32 a_netbuf_headroom(void *bufPtr);
318 void a_netbuf_enqueue(A_NETBUF_QUEUE_T *q, void *pkt);
319 void a_netbuf_prequeue(A_NETBUF_QUEUE_T *q, void *pkt);
320 void *a_netbuf_dequeue(A_NETBUF_QUEUE_T *q);
321 int a_netbuf_queue_size(A_NETBUF_QUEUE_T *q);
322 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q);
323 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q);
324 void a_netbuf_queue_init(A_NETBUF_QUEUE_T *q);
325 
326 /*
327  * Kernel v.s User space functions
328  */
329 u32 a_copy_to_user(void *to, const void *from, u32 n);
330 u32 a_copy_from_user(void *to, const void *from, u32 n);
331 
332 /* In linux, WLAN Rx and Tx run in different contexts, so no need to check
333  * for any commands/data queued for WLAN */
334 #define A_CHECK_DRV_TX()
335 
336 #define A_GET_CACHE_LINE_BYTES()    L1_CACHE_BYTES
337 
338 #define A_CACHE_LINE_PAD            128
339 
A_ALIGN_TO_CACHE_LINE(void * ptr)340 static inline void *A_ALIGN_TO_CACHE_LINE(void *ptr) {
341     return (void *)L1_CACHE_ALIGN((unsigned long)ptr);
342 }
343 
344 #else /* __KERNEL__ */
345 
346 #ifdef __GNUC__
347 #define __ATTRIB_PACK           __attribute__ ((packed))
348 #define __ATTRIB_PRINTF         __attribute__ ((format (printf, 1, 2)))
349 #define __ATTRIB_NORETURN       __attribute__ ((noreturn))
350 #ifndef INLINE
351 #define INLINE                  __inline__
352 #endif
353 #else /* Not GCC */
354 #define __ATTRIB_PACK
355 #define __ATTRIB_PRINTF
356 #define __ATTRIB_NORETURN
357 #ifndef INLINE
358 #define INLINE                  __inline
359 #endif
360 #endif /* End __GNUC__ */
361 
362 #define PREPACK
363 #define POSTPACK                __ATTRIB_PACK
364 
365 #define A_MEMZERO(addr, len)            memset((addr), 0, (len))
366 #define A_MALLOC(size)                  malloc(size)
367 #define A_FREE(addr)                    free(addr)
368 
369 #ifdef ANDROID
370 #ifndef err
371 #include <errno.h>
372 #define err(_s, args...) do { \
373     fprintf(stderr, "%s: line %d ", __FILE__, __LINE__); \
374     fprintf(stderr, args); fprintf(stderr, ": %d\n", errno); \
375     exit(_s); } while (0)
376 #endif
377 #else
378 #include <err.h>
379 #endif
380 
381 #endif /* __KERNEL__ */
382 
383 #endif /* _OSAPI_LINUX_H_ */
384