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_
23 #include <basic_types.h>
27 #define RTW_RX_HANDLED 2
29 #include <linux/spinlock.h>
30 #include <linux/compiler.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/kref.h>
36 #include <linux/netdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/circ_buf.h>
39 #include <linux/uaccess.h>
40 #include <asm/byteorder.h>
41 #include <linux/atomic.h>
43 #include <linux/semaphore.h>
44 #include <linux/sem.h>
45 #include <linux/sched.h>
46 #include <linux/etherdevice.h>
47 #include <linux/wireless.h>
48 #include <net/iw_handler.h>
49 #include <linux/if_arp.h>
50 #include <linux/rtnetlink.h>
51 #include <linux/delay.h>
52 #include <linux/proc_fs.h> /* Necessary because we use the proc fs */
53 #include <linux/interrupt.h> /* for struct tasklet_struct */
55 #include <linux/kthread.h>
57 #include <linux/usb.h>
58 #include <linux/usb/ch9.h>
61 struct list_head queue
;
65 static inline struct list_head
*get_list_head(struct __queue
*queue
)
67 return &(queue
->queue
);
70 static inline int _enter_critical_mutex(struct mutex
*pmutex
,
75 ret
= mutex_lock_interruptible(pmutex
);
79 static inline void rtw_list_delete(struct list_head
*plist
)
84 static inline void _init_timer(struct timer_list
*ptimer
,
85 struct net_device
*nic_hdl
,
86 void *pfunc
, void *cntx
)
88 ptimer
->function
= pfunc
;
89 ptimer
->data
= (unsigned long)cntx
;
93 static inline void _set_timer(struct timer_list
*ptimer
, u32 delay_time
)
95 mod_timer(ptimer
, (jiffies
+(delay_time
*HZ
/1000)));
98 static inline void _cancel_timer(struct timer_list
*ptimer
, u8
*bcancelled
)
100 del_timer_sync(ptimer
);
101 *bcancelled
= true;/* true ==1; false==0 */
104 #define RTW_TIMER_HDL_ARGS void *FunctionContext
105 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
106 #define RTW_DECLARE_TIMER_HDL(name) \
107 void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
109 static inline void _cancel_workitem_sync(struct work_struct
*pwork
)
111 cancel_work_sync(pwork
);
114 static inline int rtw_netif_queue_stopped(struct net_device
*pnetdev
)
116 return netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev
, 0)) &&
117 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev
, 1)) &&
118 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev
, 2)) &&
119 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev
, 3));
123 #define BIT(x) (1 << (x))
126 #define BIT0 0x00000001
127 #define BIT1 0x00000002
128 #define BIT2 0x00000004
129 #define BIT3 0x00000008
130 #define BIT4 0x00000010
131 #define BIT5 0x00000020
132 #define BIT6 0x00000040
133 #define BIT7 0x00000080
134 #define BIT8 0x00000100
135 #define BIT9 0x00000200
136 #define BIT10 0x00000400
137 #define BIT11 0x00000800
138 #define BIT12 0x00001000
139 #define BIT13 0x00002000
140 #define BIT14 0x00004000
141 #define BIT15 0x00008000
142 #define BIT16 0x00010000
143 #define BIT17 0x00020000
144 #define BIT18 0x00040000
145 #define BIT19 0x00080000
146 #define BIT20 0x00100000
147 #define BIT21 0x00200000
148 #define BIT22 0x00400000
149 #define BIT23 0x00800000
150 #define BIT24 0x01000000
151 #define BIT25 0x02000000
152 #define BIT26 0x04000000
153 #define BIT27 0x08000000
154 #define BIT28 0x10000000
155 #define BIT29 0x20000000
156 #define BIT30 0x40000000
157 #define BIT31 0x80000000
158 #define BIT32 0x0100000000
159 #define BIT33 0x0200000000
160 #define BIT34 0x0400000000
161 #define BIT35 0x0800000000
162 #define BIT36 0x1000000000
164 extern int RTW_STATUS_CODE(int error_code
);
166 /* flags used for rtw_update_mem_stat() */
168 MEM_STAT_VIR_ALLOC_SUCCESS
,
169 MEM_STAT_VIR_ALLOC_FAIL
,
171 MEM_STAT_PHY_ALLOC_SUCCESS
,
172 MEM_STAT_PHY_ALLOC_FAIL
,
174 MEM_STAT_TX
, /* used to distinguish TX/RX, asigned from caller */
175 MEM_STAT_TX_ALLOC_SUCCESS
,
176 MEM_STAT_TX_ALLOC_FAIL
,
178 MEM_STAT_RX
, /* used to distinguish TX/RX, asigned from caller */
179 MEM_STAT_RX_ALLOC_SUCCESS
,
180 MEM_STAT_RX_ALLOC_FAIL
,
184 extern unsigned char MCS_rate_2R
[16];
185 extern unsigned char MCS_rate_1R
[16];
186 extern unsigned char RTW_WPA_OUI
[];
187 extern unsigned char WPA_TKIP_CIPHER
[4];
188 extern unsigned char RSN_TKIP_CIPHER
[4];
190 #define rtw_update_mem_stat(flag, sz) do {} while (0)
191 u8
*_rtw_malloc(u32 sz
);
192 void _rtw_mfree(u8
*pbuf
, u32 sz
);
193 #define rtw_malloc(sz) _rtw_malloc((sz))
194 #define rtw_mfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
196 void *rtw_malloc2d(int h
, int w
, int size
);
198 void _rtw_memcpy(void *dec
, void *sour
, u32 sz
);
200 void rtw_list_delete(struct list_head
*plist
);
202 u32
_rtw_down_sema(struct semaphore
*sema
);
204 void _rtw_init_queue(struct __queue
*pqueue
);
205 u32
_rtw_queue_empty(struct __queue
*pqueue
);
207 u32
rtw_systime_to_ms(u32 systime
);
208 u32
rtw_ms_to_systime(u32 ms
);
209 s32
rtw_get_passing_time_ms(u32 start
);
210 s32
rtw_get_time_interval_ms(u32 start
, u32 end
);
212 static inline unsigned char _cancel_timer_ex(struct timer_list
*ptimer
)
214 return del_timer_sync(ptimer
);
217 static inline void thread_enter(char *name
)
219 allow_signal(SIGTERM
);
222 static inline void flush_signals_thread(void)
224 if (signal_pending(current
))
225 flush_signals(current
);
228 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
229 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0 : 1)) << 2)
231 static inline u32
_RND4(u32 sz
)
235 val
= ((sz
>> 2) + ((sz
& 3) ? 1 : 0)) << 2;
239 static inline u32
_RND8(u32 sz
)
243 val
= ((sz
>> 3) + ((sz
& 7) ? 1 : 0)) << 3;
247 static inline u32
_RND128(u32 sz
)
251 val
= ((sz
>> 7) + ((sz
& 127) ? 1 : 0)) << 7;
255 static inline u32
_RND256(u32 sz
)
259 val
= ((sz
>> 8) + ((sz
& 255) ? 1 : 0)) << 8;
263 static inline u32
_RND512(u32 sz
)
267 val
= ((sz
>> 9) + ((sz
& 511) ? 1 : 0)) << 9;
271 static inline u32
bitshift(u32 bitmask
)
275 for (i
= 0; i
<= 31; i
++)
276 if (((bitmask
>>i
) & 0x1) == 1)
281 /* limitation of path length */
282 #define PATH_LENGTH_MAX PATH_MAX
284 struct rtw_netdev_priv_indicator
{
288 struct net_device
*rtw_alloc_etherdev_with_old_priv(int sizeof_priv
,
290 struct net_device
*rtw_alloc_etherdev(int sizeof_priv
);
292 #define rtw_netdev_priv(netdev) \
293 (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
294 void rtw_free_netdev(struct net_device
*netdev
);
296 #define NDEV_FMT "%s"
297 #define NDEV_ARG(ndev) ndev->name
298 #define ADPT_FMT "%s"
299 #define ADPT_ARG(adapter) adapter->pnetdev->name
300 #define FUNC_NDEV_FMT "%s(%s)"
301 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
302 #define FUNC_ADPT_FMT "%s(%s)"
303 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
305 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)), (sig), 1)
307 u64
rtw_modular64(u64 x
, u64 y
);
308 u64
rtw_division64(u64 x
, u64 y
);
310 /* Macros for handling unaligned memory accesses */
312 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
313 #define RTW_PUT_BE16(a, val) \
315 (a)[0] = ((u16) (val)) >> 8; \
316 (a)[1] = ((u16) (val)) & 0xff; \
319 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
320 #define RTW_PUT_LE16(a, val) \
322 (a)[1] = ((u16) (val)) >> 8; \
323 (a)[0] = ((u16) (val)) & 0xff; \
326 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
329 #define RTW_PUT_BE32(a, val) \
331 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
332 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
333 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
334 (a)[3] = (u8) (((u32) (val)) & 0xff); \
337 void rtw_buf_free(u8
**buf
, u32
*buf_len
);
338 void rtw_buf_update(u8
**buf
, u32
*buf_len
, u8
*src
, u32 src_len
);
347 bool rtw_cbuf_full(struct rtw_cbuf
*cbuf
);
348 bool rtw_cbuf_empty(struct rtw_cbuf
*cbuf
);
349 bool rtw_cbuf_push(struct rtw_cbuf
*cbuf
, void *buf
);
350 void *rtw_cbuf_pop(struct rtw_cbuf
*cbuf
);
351 struct rtw_cbuf
*rtw_cbuf_alloc(u32 size
);
352 int wifirate2_ratetbl_inx(unsigned char rate
);