41f437307727427f43c9146181f132451d188829
[pandora-kernel.git] / drivers / staging / ath6kl / os / linux / include / osapi_linux.h
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/types.h>
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/jiffies.h>
38 #include <linux/timer.h>
39 #include <linux/delay.h>
40 #include <linux/wait.h>
41 #include <linux/semaphore.h>
42 #include <linux/cache.h>
43
44 #ifdef __GNUC__
45 #define __ATTRIB_PACK           __attribute__ ((packed))
46 #define __ATTRIB_PRINTF         __attribute__ ((format (printf, 1, 2)))
47 #define __ATTRIB_NORETURN       __attribute__ ((noreturn))
48 #ifndef INLINE
49 #define INLINE                  __inline__
50 #endif
51 #else /* Not GCC */
52 #define __ATTRIB_PACK
53 #define __ATTRIB_PRINTF
54 #define __ATTRIB_NORETURN
55 #ifndef INLINE
56 #define INLINE                  __inline
57 #endif
58 #endif /* End __GNUC__ */
59
60 #define PREPACK
61 #define POSTPACK                __ATTRIB_PACK
62
63 /*
64  * Endianes macros
65  */
66 #define A_BE2CPU8(x)       ntohb(x)
67 #define A_BE2CPU16(x)      ntohs(x)
68 #define A_BE2CPU32(x)      ntohl(x)
69
70 #define A_LE2CPU8(x)       (x)
71 #define A_LE2CPU16(x)      (x)
72 #define A_LE2CPU32(x)      (x)
73
74 #define A_CPU2BE8(x)       htonb(x)
75 #define A_CPU2BE16(x)      htons(x)
76 #define A_CPU2BE32(x)      htonl(x)
77
78 #define A_MEMZERO(addr, len)            memset(addr, 0, len)
79 #define A_MALLOC(size)                  kmalloc((size), GFP_KERNEL)
80 #define A_MALLOC_NOWAIT(size)           kmalloc((size), GFP_ATOMIC)
81
82 #define A_LOGGER(mask, mod, args...)    printk(KERN_ALERT args)
83 #define A_PRINTF(args...)               printk(KERN_ALERT args)
84
85 #define A_PRINTF_LOG(args...)           printk(args)
86 #define A_SPRINTF(buf, args...)                 sprintf (buf, args)
87
88 /* Mutual Exclusion */
89 typedef spinlock_t                      A_MUTEX_T;
90 #define A_MUTEX_INIT(mutex)             spin_lock_init(mutex)
91 #define A_MUTEX_LOCK(mutex)             spin_lock_bh(mutex)
92 #define A_MUTEX_UNLOCK(mutex)           spin_unlock_bh(mutex)
93 #define A_IS_MUTEX_VALID(mutex)         true  /* okay to return true, since A_MUTEX_DELETE does nothing */
94 #define A_MUTEX_DELETE(mutex)           /* spin locks are not kernel resources so nothing to free.. */
95
96 /* Get current time in ms adding a constant offset (in ms) */
97 #define A_GET_MS(offset)    \
98         (((jiffies / HZ) * 1000) + (offset))
99
100 /*
101  * Timer Functions
102  */
103 #define A_MDELAY(msecs)                 mdelay(msecs)
104 typedef struct timer_list               A_TIMER;
105
106 #define A_INIT_TIMER(pTimer, pFunction, pArg) do {              \
107     init_timer(pTimer);                                         \
108     (pTimer)->function = (pFunction);                           \
109     (pTimer)->data   = (unsigned long)(pArg);                   \
110 } while (0)
111
112 /*
113  * Start a Timer that elapses after 'periodMSec' milli-seconds
114  * Support is provided for a one-shot timer. The 'repeatFlag' is
115  * ignored.
116  */
117 #define A_TIMEOUT_MS(pTimer, periodMSec, repeatFlag) do {                   \
118     if (repeatFlag) {                                                       \
119         printk("\n" __FILE__ ":%d: Timer Repeat requested\n",__LINE__);     \
120         panic("Timer Repeat");                                              \
121     }                                                                       \
122     mod_timer((pTimer), jiffies + HZ * (periodMSec) / 1000);                \
123 } while (0)
124
125 /*
126  * Cancel the Timer. 
127  */
128 #define A_UNTIMEOUT(pTimer) do {                                \
129     del_timer((pTimer));                                        \
130 } while (0)
131
132 #define A_DELETE_TIMER(pTimer) do {                             \
133 } while (0)
134
135 /*
136  * Wait Queue related functions
137  */
138 typedef wait_queue_head_t               A_WAITQUEUE_HEAD;
139 #define A_INIT_WAITQUEUE_HEAD(head)     init_waitqueue_head(head)
140 #ifndef wait_event_interruptible_timeout
141 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
142 do {                                                                    \
143         wait_queue_t __wait;                                            \
144         init_waitqueue_entry(&__wait, current);                         \
145                                                                         \
146         add_wait_queue(&wq, &__wait);                                   \
147         for (;;) {                                                      \
148                 set_current_state(TASK_INTERRUPTIBLE);                  \
149                 if (condition)                                          \
150                         break;                                          \
151                 if (!signal_pending(current)) {                         \
152                         ret = schedule_timeout(ret);                    \
153                         if (!ret)                                       \
154                                 break;                                  \
155                         continue;                                       \
156                 }                                                       \
157                 ret = -ERESTARTSYS;                                     \
158                 break;                                                  \
159         }                                                               \
160         current->state = TASK_RUNNING;                                  \
161         remove_wait_queue(&wq, &__wait);                                \
162 } while (0)
163
164 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
165 ({                                                                      \
166         long __ret = timeout;                                           \
167         if (!(condition))                                               \
168                 __wait_event_interruptible_timeout(wq, condition, __ret); \
169         __ret;                                                          \
170 })
171 #endif /* wait_event_interruptible_timeout */
172
173 #define A_WAIT_EVENT_INTERRUPTIBLE_TIMEOUT(head, condition, timeout) do { \
174     wait_event_interruptible_timeout(head, condition, timeout); \
175 } while (0)
176
177 #define A_WAKE_UP(head)                 wake_up(head)
178
179 #ifdef DEBUG
180 extern unsigned int panic_on_assert;
181 #define A_ASSERT(expr)  \
182     if (!(expr)) {   \
183         printk(KERN_ALERT"Debug Assert Caught, File %s, Line: %d, Test:%s \n",__FILE__, __LINE__,#expr); \
184         if (panic_on_assert) panic(#expr);                                                               \
185     }
186 #else
187 #define A_ASSERT(expr)
188 #endif /* DEBUG */
189
190 #define A_REQUEST_FIRMWARE(_ppf, _pfile, _dev) request_firmware(_ppf, _pfile, _dev)
191 #define A_RELEASE_FIRMWARE(_pf) release_firmware(_pf)
192
193 /*
194  * Initialization of the network buffer subsystem
195  */
196 #define A_NETBUF_INIT()
197
198 /*
199  * Network buffer queue support
200  */
201 typedef struct sk_buff_head A_NETBUF_QUEUE_T;
202
203 #define A_NETBUF_QUEUE_INIT(q)  \
204     a_netbuf_queue_init(q)
205
206 #define A_NETBUF_ENQUEUE(q, pkt) \
207     a_netbuf_enqueue((q), (pkt))
208 #define A_NETBUF_PREQUEUE(q, pkt) \
209     a_netbuf_prequeue((q), (pkt))
210 #define A_NETBUF_DEQUEUE(q) \
211     (a_netbuf_dequeue(q))
212 #define A_NETBUF_QUEUE_SIZE(q)  \
213     a_netbuf_queue_size(q)
214 #define A_NETBUF_QUEUE_EMPTY(q) \
215     (a_netbuf_queue_empty(q) ? true : false)
216
217 /*
218  * Network buffer support
219  */
220 #define A_NETBUF_ALLOC(size) \
221     a_netbuf_alloc(size)
222 #define A_NETBUF_ALLOC_RAW(size) \
223     a_netbuf_alloc_raw(size)
224 #define A_NETBUF_FREE(bufPtr) \
225     a_netbuf_free(bufPtr)
226 #define A_NETBUF_DATA(bufPtr) \
227     a_netbuf_to_data(bufPtr)
228 #define A_NETBUF_LEN(bufPtr) \
229     a_netbuf_to_len(bufPtr)
230 #define A_NETBUF_PUSH(bufPtr, len) \
231     a_netbuf_push(bufPtr, len)
232 #define A_NETBUF_PUT(bufPtr, len) \
233     a_netbuf_put(bufPtr, len)
234 #define A_NETBUF_TRIM(bufPtr,len) \
235     a_netbuf_trim(bufPtr, len)
236 #define A_NETBUF_PULL(bufPtr, len) \
237     a_netbuf_pull(bufPtr, len)
238 #define A_NETBUF_HEADROOM(bufPtr)\
239     a_netbuf_headroom(bufPtr)
240 #define A_NETBUF_SETLEN(bufPtr,len) \
241     a_netbuf_setlen(bufPtr, len)
242
243 /* Add data to end of a buffer  */
244 #define A_NETBUF_PUT_DATA(bufPtr, srcPtr,  len) \
245     a_netbuf_put_data(bufPtr, srcPtr, len) 
246
247 /* Add data to start of the  buffer */
248 #define A_NETBUF_PUSH_DATA(bufPtr, srcPtr,  len) \
249     a_netbuf_push_data(bufPtr, srcPtr, len) 
250
251 /* Remove data at start of the buffer */
252 #define A_NETBUF_PULL_DATA(bufPtr, dstPtr, len) \
253     a_netbuf_pull_data(bufPtr, dstPtr, len) 
254
255 /* Remove data from the end of the buffer */
256 #define A_NETBUF_TRIM_DATA(bufPtr, dstPtr, len) \
257     a_netbuf_trim_data(bufPtr, dstPtr, len) 
258
259 /* View data as "size" contiguous bytes of type "t" */
260 #define A_NETBUF_VIEW_DATA(bufPtr, t, size) \
261     (t )( ((struct skbuf *)(bufPtr))->data)
262
263 /* return the beginning of the headroom for the buffer */
264 #define A_NETBUF_HEAD(bufPtr) \
265         ((((struct sk_buff *)(bufPtr))->head))
266     
267 /*
268  * OS specific network buffer access routines
269  */
270 void *a_netbuf_alloc(int size);
271 void *a_netbuf_alloc_raw(int size);
272 void a_netbuf_free(void *bufPtr);
273 void *a_netbuf_to_data(void *bufPtr);
274 u32 a_netbuf_to_len(void *bufPtr);
275 int a_netbuf_push(void *bufPtr, s32 len);
276 int a_netbuf_push_data(void *bufPtr, char *srcPtr, s32 len);
277 int a_netbuf_put(void *bufPtr, s32 len);
278 int a_netbuf_put_data(void *bufPtr, char *srcPtr, s32 len);
279 int a_netbuf_pull(void *bufPtr, s32 len);
280 int a_netbuf_pull_data(void *bufPtr, char *dstPtr, s32 len);
281 int a_netbuf_trim(void *bufPtr, s32 len);
282 int a_netbuf_trim_data(void *bufPtr, char *dstPtr, s32 len);
283 int a_netbuf_setlen(void *bufPtr, s32 len);
284 s32 a_netbuf_headroom(void *bufPtr);
285 void a_netbuf_enqueue(A_NETBUF_QUEUE_T *q, void *pkt);
286 void a_netbuf_prequeue(A_NETBUF_QUEUE_T *q, void *pkt);
287 void *a_netbuf_dequeue(A_NETBUF_QUEUE_T *q);
288 int a_netbuf_queue_size(A_NETBUF_QUEUE_T *q);
289 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q);
290 int a_netbuf_queue_empty(A_NETBUF_QUEUE_T *q);
291 void a_netbuf_queue_init(A_NETBUF_QUEUE_T *q);
292
293 /*
294  * Kernel v.s User space functions
295  */
296 u32 a_copy_to_user(void *to, const void *from, u32 n);
297 u32 a_copy_from_user(void *to, const void *from, u32 n);
298
299 /* In linux, WLAN Rx and Tx run in different contexts, so no need to check
300  * for any commands/data queued for WLAN */
301 #define A_CHECK_DRV_TX()   
302              
303 #define A_GET_CACHE_LINE_BYTES()    L1_CACHE_BYTES
304
305 #define A_CACHE_LINE_PAD            128
306
307 static inline void *A_ALIGN_TO_CACHE_LINE(void *ptr) {   
308     return (void *)L1_CACHE_ALIGN((unsigned long)ptr);
309 }
310    
311 #else /* __KERNEL__ */
312
313 #ifdef __GNUC__
314 #define __ATTRIB_PACK           __attribute__ ((packed))
315 #define __ATTRIB_PRINTF         __attribute__ ((format (printf, 1, 2)))
316 #define __ATTRIB_NORETURN       __attribute__ ((noreturn))
317 #ifndef INLINE
318 #define INLINE                  __inline__
319 #endif
320 #else /* Not GCC */
321 #define __ATTRIB_PACK
322 #define __ATTRIB_PRINTF
323 #define __ATTRIB_NORETURN
324 #ifndef INLINE
325 #define INLINE                  __inline
326 #endif
327 #endif /* End __GNUC__ */
328
329 #define PREPACK
330 #define POSTPACK                __ATTRIB_PACK
331
332 #define A_MEMZERO(addr, len)            memset((addr), 0, (len))
333 #define A_MALLOC(size)                  malloc(size)
334
335 #include <err.h>
336
337 #endif /* __KERNEL__ */
338
339 #endif /* _OSAPI_LINUX_H_ */