OSDN Git Service

Remove msleep() wrapper
[android-x86/external-modules-rtl8723au.git] / include / osdep_service.h
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #ifndef __OSDEP_SERVICE_H_
21 #define __OSDEP_SERVICE_H_
22
23 #include <drv_conf.h>
24 #include <basic_types.h>
25
26 #define _FAIL           0
27 #define _SUCCESS        1
28 #define RTW_RX_HANDLED 2
29 //#define RTW_STATUS_TIMEDOUT -110
30
31 #undef _TRUE
32 #define _TRUE           1
33
34 #undef _FALSE
35 #define _FALSE          0
36
37 #include <linux/version.h>
38 #include <linux/spinlock.h>
39 #include <linux/compiler.h>
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/module.h>
45 #include <linux/kref.h>
46 #include <linux/netdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/circ_buf.h>
49 #include <asm/uaccess.h>
50 #include <asm/byteorder.h>
51 #include <asm/atomic.h>
52 #include <asm/io.h>
53 #include <linux/semaphore.h>
54 #include <linux/sem.h>
55 #include <linux/sched.h>
56 #include <linux/etherdevice.h>
57 #include <linux/wireless.h>
58 #include <net/iw_handler.h>
59 #include <linux/if_arp.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/delay.h>
62 #include <linux/proc_fs.h>      // Necessary because we use the proc fs
63 #include <linux/interrupt.h>    // for struct tasklet_struct
64 #include <linux/ip.h>
65 #include <linux/kthread.h>
66
67
68 #ifdef CONFIG_IOCTL_CFG80211
69 //      #include <linux/ieee80211.h>
70 #include <net/ieee80211_radiotap.h>
71 #include <net/cfg80211.h>
72 #endif //CONFIG_IOCTL_CFG80211
73
74 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
75 #include <linux/in.h>
76 #include <linux/udp.h>
77 #endif
78
79 #include <linux/usb.h>
80 #include <linux/usb/ch9.h>
81
82 #ifdef CONFIG_USB_SUSPEND
83 #define CONFIG_AUTOSUSPEND      1
84 #endif
85
86 typedef struct timer_list _timer;
87
88 struct  __queue {
89         struct  list_head       queue;
90         spinlock_t              lock;
91 };
92
93 typedef struct sk_buff  _pkt;
94 typedef unsigned char   _buffer;
95
96 typedef struct  __queue _queue;
97 typedef struct  net_device * _nic_hdl;
98
99 typedef void*           _thread_hdl_;
100 typedef int             thread_return;
101 typedef void*   thread_context;
102
103 #define thread_exit() complete_and_exit(NULL, 0)
104
105 typedef void timer_hdl_return;
106 typedef void* timer_hdl_context;
107
108 __inline static struct list_head        *get_list_head(_queue   *queue)
109 {
110         return (&(queue->queue));
111 }
112
113
114 __inline static void _init_timer(_timer *ptimer,_nic_hdl nic_hdl,void *pfunc,void* cntx)
115 {
116         //setup_timer(ptimer, pfunc,(u32)cntx);
117         ptimer->function = pfunc;
118         ptimer->data = (unsigned long)cntx;
119         init_timer(ptimer);
120 }
121
122 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
123 {
124         mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
125 }
126
127 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
128 {
129         del_timer_sync(ptimer);
130         *bcancelled=  _TRUE;//TRUE ==1; FALSE==0
131 }
132
133 #define RTW_TIMER_HDL_ARGS void *FunctionContext
134
135 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
136 #define RTW_DECLARE_TIMER_HDL(name) void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
137
138
139 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
140 {
141 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
142         return (netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
143                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
144                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
145                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3)) );
146 #else
147         return netif_queue_stopped(pnetdev);
148 #endif
149 }
150
151 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
152 {
153 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
154         netif_tx_wake_all_queues(pnetdev);
155 #else
156         netif_wake_queue(pnetdev);
157 #endif
158 }
159
160 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
161 {
162 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
163         netif_tx_start_all_queues(pnetdev);
164 #else
165         netif_start_queue(pnetdev);
166 #endif
167 }
168
169 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
170 {
171 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
172         netif_tx_stop_all_queues(pnetdev);
173 #else
174         netif_stop_queue(pnetdev);
175 #endif
176 }
177
178 #ifndef BIT
179 #define BIT(x)  ( 1 << (x))
180 #endif
181
182 #define BIT0    0x00000001
183 #define BIT1    0x00000002
184 #define BIT2    0x00000004
185 #define BIT3    0x00000008
186 #define BIT4    0x00000010
187 #define BIT5    0x00000020
188 #define BIT6    0x00000040
189 #define BIT7    0x00000080
190 #define BIT8    0x00000100
191 #define BIT9    0x00000200
192 #define BIT10   0x00000400
193 #define BIT11   0x00000800
194 #define BIT12   0x00001000
195 #define BIT13   0x00002000
196 #define BIT14   0x00004000
197 #define BIT15   0x00008000
198 #define BIT16   0x00010000
199 #define BIT17   0x00020000
200 #define BIT18   0x00040000
201 #define BIT19   0x00080000
202 #define BIT20   0x00100000
203 #define BIT21   0x00200000
204 #define BIT22   0x00400000
205 #define BIT23   0x00800000
206 #define BIT24   0x01000000
207 #define BIT25   0x02000000
208 #define BIT26   0x04000000
209 #define BIT27   0x08000000
210 #define BIT28   0x10000000
211 #define BIT29   0x20000000
212 #define BIT30   0x40000000
213 #define BIT31   0x80000000
214 #define BIT32   0x0100000000
215 #define BIT33   0x0200000000
216 #define BIT34   0x0400000000
217 #define BIT35   0x0800000000
218 #define BIT36   0x1000000000
219
220 extern int RTW_STATUS_CODE(int error_code);
221
222 #define CONFIG_USE_VMALLOC
223
224 extern u8*      _rtw_vmalloc(u32 sz);
225 extern u8*      _rtw_zvmalloc(u32 sz);
226 extern void     _rtw_vmfree(u8 *pbuf, u32 sz);
227 extern u8*      _rtw_zmalloc(u32 sz);
228 extern u8*      _rtw_malloc(u32 sz);
229 extern void     _rtw_mfree(u8 *pbuf, u32 sz);
230 #ifdef CONFIG_USE_VMALLOC
231 #define rtw_vmalloc(sz)                 _rtw_vmalloc((sz))
232 #define rtw_zvmalloc(sz)                        _rtw_zvmalloc((sz))
233 #define rtw_vmfree(pbuf, sz)            _rtw_vmfree((pbuf), (sz))
234 #else //CONFIG_USE_VMALLOC
235 #define rtw_vmalloc(sz)                 _rtw_malloc((sz))
236 #define rtw_zvmalloc(sz)                        _rtw_zmalloc((sz))
237 #define rtw_vmfree(pbuf, sz)            _rtw_mfree((pbuf), (sz))
238 #endif //CONFIG_USE_VMALLOC
239 #define rtw_malloc(sz)                  _rtw_malloc((sz))
240 #define rtw_zmalloc(sz)                 _rtw_zmalloc((sz))
241 #define rtw_mfree(pbuf, sz)             _rtw_mfree((pbuf), (sz))
242
243 extern void     _rtw_init_queue(_queue  *pqueue);
244 extern u32      _rtw_queue_empty(_queue *pqueue);
245 extern u32      rtw_end_of_queue_search(struct list_head *queue, struct list_head *pelement);
246
247 extern u32      rtw_get_current_time(void);
248 extern u32      rtw_systime_to_ms(u32 systime);
249 extern u32      rtw_ms_to_systime(u32 ms);
250 extern s32      rtw_get_passing_time_ms(u32 start);
251 extern s32      rtw_get_time_interval_ms(u32 start, u32 end);
252
253 extern void     rtw_sleep_schedulable(int ms);
254
255 extern void     rtw_usleep_os(int us);
256
257 #ifdef DBG_DELAY_OS
258 #define rtw_mdelay_os(ms) _rtw_mdelay_os((ms), __FUNCTION__, __LINE__)
259 #define rtw_udelay_os(ms) _rtw_udelay_os((ms), __FUNCTION__, __LINE__)
260 extern void _rtw_mdelay_os(int ms, const char *func, const int line);
261 extern void _rtw_udelay_os(int us, const char *func, const int line);
262 #else
263 extern void     rtw_mdelay_os(int ms);
264 extern void     rtw_udelay_os(int us);
265 #endif
266
267 extern void rtw_yield_os(void);
268
269
270 __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
271 {
272         return del_timer_sync(ptimer);
273 }
274
275 static __inline void thread_enter(char *name)
276 {
277 #ifdef daemonize
278         daemonize("%s", name);
279 #endif
280         allow_signal(SIGTERM);
281 }
282
283 __inline static void flush_signals_thread(void)
284 {
285         if (signal_pending (current))
286                 flush_signals(current);
287 }
288
289 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
290 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ?  0: 1)) << 2)
291
292 __inline static u32 _RND4(u32 sz)
293 {
294
295         u32     val;
296
297         val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
298
299         return val;
300
301 }
302
303 __inline static u32 _RND8(u32 sz)
304 {
305
306         u32     val;
307
308         val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
309
310         return val;
311
312 }
313
314 __inline static u32 _RND128(u32 sz)
315 {
316
317         u32     val;
318
319         val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
320
321         return val;
322
323 }
324
325 __inline static u32 _RND256(u32 sz)
326 {
327
328         u32     val;
329
330         val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
331
332         return val;
333
334 }
335
336 __inline static u32 _RND512(u32 sz)
337 {
338
339         u32     val;
340
341         val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
342
343         return val;
344
345 }
346
347 __inline static u32 bitshift(u32 bitmask)
348 {
349         u32 i;
350
351         for (i = 0; i <= 31; i++)
352                 if (((bitmask>>i) &  0x1) == 1) break;
353
354         return i;
355 }
356
357 #ifndef MAC_FMT
358 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
359 #endif
360 #ifndef MAC_ARG
361 #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
362 #endif
363
364 #define STRUCT_PACKED __attribute__ ((packed))
365
366 // limitation of path length
367 #define PATH_LENGTH_MAX PATH_MAX
368
369 // Suspend lock prevent system from going suspend
370 #ifdef CONFIG_WAKELOCK
371 #include <linux/wakelock.h>
372 #elif defined(CONFIG_ANDROID_POWER)
373 #include <linux/android_power.h>
374 #endif
375
376 extern void rtw_suspend_lock_init(void);
377 extern void rtw_suspend_lock_uninit(void);
378 extern void rtw_lock_suspend(void);
379 extern void rtw_unlock_suspend(void);
380 #ifdef CONFIG_WOWLAN
381 extern void rtw_lock_suspend_timeout(long timeout);
382 #endif //CONFIG_WOWLAN
383
384 //File operation APIs, just for linux now
385 extern int rtw_is_file_readable(char *path);
386 extern int rtw_retrive_from_file(char *path, u8* buf, u32 sz);
387 extern int rtw_store_to_file(char *path, u8* buf, u32 sz);
388
389
390 #if 1 //#ifdef MEM_ALLOC_REFINE_ADAPTOR
391 struct rtw_netdev_priv_indicator {
392         void *priv;
393         u32 sizeof_priv;
394 };
395 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv);
396 extern struct net_device * rtw_alloc_etherdev(int sizeof_priv);
397
398 #define rtw_netdev_priv(netdev) ( ((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv )
399
400 extern void rtw_free_netdev(struct net_device * netdev);
401
402 #else //MEM_ALLOC_REFINE_ADAPTOR
403
404 #define rtw_alloc_etherdev(sizeof_priv) alloc_etherdev((sizeof_priv))
405
406 #define rtw_netdev_priv(netdev) netdev_priv((netdev))
407 #define rtw_free_netdev(netdev) free_netdev((netdev))
408 #endif
409
410 #define NDEV_FMT "%s"
411 #define NDEV_ARG(ndev) ndev->name
412 #define ADPT_FMT "%s"
413 #define ADPT_ARG(adapter) adapter->pnetdev->name
414 #define FUNC_NDEV_FMT "%s(%s)"
415 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
416 #define FUNC_ADPT_FMT "%s(%s)"
417 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
418
419 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
420
421 extern u64 rtw_modular64(u64 x, u64 y);
422 extern u64 rtw_division64(u64 x, u64 y);
423
424
425 /* Macros for handling unaligned memory accesses */
426
427 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
428 #define RTW_PUT_BE16(a, val)                    \
429         do {                                    \
430                 (a)[0] = ((u16) (val)) >> 8;    \
431                 (a)[1] = ((u16) (val)) & 0xff;  \
432         } while (0)
433
434 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
435 #define RTW_PUT_LE16(a, val)                    \
436         do {                                    \
437                 (a)[1] = ((u16) (val)) >> 8;    \
438                 (a)[0] = ((u16) (val)) & 0xff;  \
439         } while (0)
440
441 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
442                          ((u32) (a)[2]))
443 #define RTW_PUT_BE24(a, val)                                    \
444         do {                                                    \
445                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
446                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
447                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
448         } while (0)
449
450 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
451                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
452 #define RTW_PUT_BE32(a, val)                                    \
453         do {                                                    \
454                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
455                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
456                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
457                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
458         } while (0)
459
460 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
461                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
462 #define RTW_PUT_LE32(a, val)                                    \
463         do {                                                    \
464                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
465                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
466                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
467                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
468         } while (0)
469
470 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
471                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
472                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
473                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
474 #define RTW_PUT_BE64(a, val)                            \
475         do {                                            \
476                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
477                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
478                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
479                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
480                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
481                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
482                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
483                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
484         } while (0)
485
486 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
487                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
488                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
489                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
490
491 void rtw_buf_free(u8 **buf, u32 *buf_len);
492 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
493
494 struct rtw_cbuf {
495         u32 write;
496         u32 read;
497         u32 size;
498         void *bufs[0];
499 };
500
501 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
502 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
503 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
504 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
505 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
506 void rtw_cbuf_free(struct rtw_cbuf *cbuf);
507
508 #ifdef CONFIG_XMIT_ACK
509 #ifdef CONFIG_XMIT_ACK_POLLING
510 s32 c2h_evt_hdl(_adapter *adapter, struct c2h_evt_hdr *c2h_evt, c2h_id_filter filter);
511 #endif
512 #endif
513
514 #endif