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