1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
20 #ifndef __OSDEP_SERVICE_H_
21 #define __OSDEP_SERVICE_H_
24 #include <basic_types.h>
28 #define RTW_RX_HANDLED 2
29 //#define RTW_STATUS_TIMEDOUT -110
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>
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
65 #include <linux/kthread.h>
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
74 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
76 #include <linux/udp.h>
79 #include <linux/usb.h>
80 #include <linux/usb/ch9.h>
82 #ifdef CONFIG_USB_SUSPEND
83 #define CONFIG_AUTOSUSPEND 1
86 typedef struct timer_list _timer;
89 struct list_head queue;
93 typedef struct sk_buff _pkt;
94 typedef unsigned char _buffer;
96 typedef struct __queue _queue;
97 typedef struct net_device * _nic_hdl;
99 typedef void* _thread_hdl_;
100 typedef int thread_return;
101 typedef void* thread_context;
103 #define thread_exit() complete_and_exit(NULL, 0)
105 typedef void timer_hdl_return;
106 typedef void* timer_hdl_context;
108 __inline static struct list_head *get_list_head(_queue *queue)
110 return (&(queue->queue));
114 __inline static void _init_timer(_timer *ptimer,_nic_hdl nic_hdl,void *pfunc,void* cntx)
116 //setup_timer(ptimer, pfunc,(u32)cntx);
117 ptimer->function = pfunc;
118 ptimer->data = (unsigned long)cntx;
122 __inline static void _set_timer(_timer *ptimer,u32 delay_time)
124 mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
127 __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
129 del_timer_sync(ptimer);
130 *bcancelled= _TRUE;//TRUE ==1; FALSE==0
133 #define RTW_TIMER_HDL_ARGS void *FunctionContext
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)
139 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
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)) );
147 return netif_queue_stopped(pnetdev);
151 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
153 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
154 netif_tx_wake_all_queues(pnetdev);
156 netif_wake_queue(pnetdev);
160 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
162 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
163 netif_tx_start_all_queues(pnetdev);
165 netif_start_queue(pnetdev);
169 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
171 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
172 netif_tx_stop_all_queues(pnetdev);
174 netif_stop_queue(pnetdev);
179 #define BIT(x) ( 1 << (x))
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
220 extern int RTW_STATUS_CODE(int error_code);
222 #define CONFIG_USE_VMALLOC
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))
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);
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);
253 extern void rtw_sleep_schedulable(int ms);
255 extern void rtw_usleep_os(int us);
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);
263 extern void rtw_mdelay_os(int ms);
264 extern void rtw_udelay_os(int us);
267 extern void rtw_yield_os(void);
270 __inline static unsigned char _cancel_timer_ex(_timer *ptimer)
272 return del_timer_sync(ptimer);
275 static __inline void thread_enter(char *name)
278 daemonize("%s", name);
280 allow_signal(SIGTERM);
283 __inline static void flush_signals_thread(void)
285 if (signal_pending (current))
286 flush_signals(current);
289 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
290 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0: 1)) << 2)
292 __inline static u32 _RND4(u32 sz)
297 val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
303 __inline static u32 _RND8(u32 sz)
308 val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
314 __inline static u32 _RND128(u32 sz)
319 val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
325 __inline static u32 _RND256(u32 sz)
330 val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
336 __inline static u32 _RND512(u32 sz)
341 val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
347 __inline static u32 bitshift(u32 bitmask)
351 for (i = 0; i <= 31; i++)
352 if (((bitmask>>i) & 0x1) == 1) break;
358 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
361 #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
364 #define STRUCT_PACKED __attribute__ ((packed))
366 // limitation of path length
367 #define PATH_LENGTH_MAX PATH_MAX
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>
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);
381 extern void rtw_lock_suspend_timeout(long timeout);
382 #endif //CONFIG_WOWLAN
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);
390 #if 1 //#ifdef MEM_ALLOC_REFINE_ADAPTOR
391 struct rtw_netdev_priv_indicator {
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);
398 #define rtw_netdev_priv(netdev) ( ((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv )
400 extern void rtw_free_netdev(struct net_device * netdev);
402 #else //MEM_ALLOC_REFINE_ADAPTOR
404 #define rtw_alloc_etherdev(sizeof_priv) alloc_etherdev((sizeof_priv))
406 #define rtw_netdev_priv(netdev) netdev_priv((netdev))
407 #define rtw_free_netdev(netdev) free_netdev((netdev))
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
419 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
421 extern u64 rtw_modular64(u64 x, u64 y);
422 extern u64 rtw_division64(u64 x, u64 y);
425 /* Macros for handling unaligned memory accesses */
427 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
428 #define RTW_PUT_BE16(a, val) \
430 (a)[0] = ((u16) (val)) >> 8; \
431 (a)[1] = ((u16) (val)) & 0xff; \
434 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
435 #define RTW_PUT_LE16(a, val) \
437 (a)[1] = ((u16) (val)) >> 8; \
438 (a)[0] = ((u16) (val)) & 0xff; \
441 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
443 #define RTW_PUT_BE24(a, val) \
445 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
446 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
447 (a)[2] = (u8) (((u32) (val)) & 0xff); \
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) \
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); \
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) \
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); \
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) \
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); \
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]))
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);
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);
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);