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_zmalloc(u32 sz);
192 u8 *_rtw_malloc(u32 sz);
193 void _rtw_mfree(u8 *pbuf, u32 sz);
194 #define rtw_malloc(sz) _rtw_malloc((sz))
195 #define rtw_zmalloc(sz) _rtw_zmalloc((sz))
196 #define rtw_mfree(pbuf, sz) _rtw_mfree((pbuf), (sz))
198 void *rtw_malloc2d(int h, int w, int size);
199 void rtw_mfree2d(void *pbuf, int h, int w, int size);
201 void _rtw_memcpy(void *dec, void *sour, u32 sz);
202 void _rtw_memset(void *pbuf, int c, u32 sz);
204 void _rtw_init_listhead(struct list_head *list);
205 u32 rtw_is_list_empty(struct list_head *phead);
206 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead);
207 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead);
208 void rtw_list_delete(struct list_head *plist);
210 u32 _rtw_down_sema(struct semaphore *sema);
212 void _rtw_init_queue(struct __queue *pqueue);
213 u32 _rtw_queue_empty(struct __queue *pqueue);
214 u32 rtw_end_of_queue_search(struct list_head *queue,
215 struct list_head *pelement);
217 u32 rtw_systime_to_ms(u32 systime);
218 u32 rtw_ms_to_systime(u32 ms);
219 s32 rtw_get_passing_time_ms(u32 start);
220 s32 rtw_get_time_interval_ms(u32 start, u32 end);
222 void rtw_sleep_schedulable(int ms);
226 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
228 return del_timer_sync(ptimer);
231 static inline void thread_enter(char *name)
233 allow_signal(SIGTERM);
236 static inline void flush_signals_thread(void)
238 if (signal_pending(current))
239 flush_signals(current);
242 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
243 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ? 0 : 1)) << 2)
245 static inline u32 _RND4(u32 sz)
249 val = ((sz >> 2) + ((sz & 3) ? 1 : 0)) << 2;
253 static inline u32 _RND8(u32 sz)
257 val = ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
261 static inline u32 _RND128(u32 sz)
265 val = ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
269 static inline u32 _RND256(u32 sz)
273 val = ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
277 static inline u32 _RND512(u32 sz)
281 val = ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
285 static inline u32 bitshift(u32 bitmask)
289 for (i = 0; i <= 31; i++)
290 if (((bitmask>>i) & 0x1) == 1)
295 /* limitation of path length */
296 #define PATH_LENGTH_MAX PATH_MAX
298 struct rtw_netdev_priv_indicator {
302 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
304 struct net_device *rtw_alloc_etherdev(int sizeof_priv);
306 #define rtw_netdev_priv(netdev) \
307 (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
308 void rtw_free_netdev(struct net_device *netdev);
310 #define NDEV_FMT "%s"
311 #define NDEV_ARG(ndev) ndev->name
312 #define ADPT_FMT "%s"
313 #define ADPT_ARG(adapter) adapter->pnetdev->name
314 #define FUNC_NDEV_FMT "%s(%s)"
315 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
316 #define FUNC_ADPT_FMT "%s(%s)"
317 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
319 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)), (sig), 1)
321 u64 rtw_modular64(u64 x, u64 y);
322 u64 rtw_division64(u64 x, u64 y);
324 /* Macros for handling unaligned memory accesses */
326 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
327 #define RTW_PUT_BE16(a, val) \
329 (a)[0] = ((u16) (val)) >> 8; \
330 (a)[1] = ((u16) (val)) & 0xff; \
333 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
334 #define RTW_PUT_LE16(a, val) \
336 (a)[1] = ((u16) (val)) >> 8; \
337 (a)[0] = ((u16) (val)) & 0xff; \
340 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
342 #define RTW_PUT_BE24(a, val) \
344 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
345 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
346 (a)[2] = (u8) (((u32) (val)) & 0xff); \
349 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
350 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
351 #define RTW_PUT_BE32(a, val) \
353 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
354 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
355 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
356 (a)[3] = (u8) (((u32) (val)) & 0xff); \
359 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
360 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
361 #define RTW_PUT_LE32(a, val) \
363 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
364 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
365 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
366 (a)[0] = (u8) (((u32) (val)) & 0xff); \
369 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
370 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
371 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
372 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
373 #define RTW_PUT_BE64(a, val) \
375 (a)[0] = (u8) (((u64) (val)) >> 56); \
376 (a)[1] = (u8) (((u64) (val)) >> 48); \
377 (a)[2] = (u8) (((u64) (val)) >> 40); \
378 (a)[3] = (u8) (((u64) (val)) >> 32); \
379 (a)[4] = (u8) (((u64) (val)) >> 24); \
380 (a)[5] = (u8) (((u64) (val)) >> 16); \
381 (a)[6] = (u8) (((u64) (val)) >> 8); \
382 (a)[7] = (u8) (((u64) (val)) & 0xff); \
385 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
386 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
387 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
388 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
390 void rtw_buf_free(u8 **buf, u32 *buf_len);
391 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
400 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
401 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
402 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
403 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
404 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
405 int wifirate2_ratetbl_inx(unsigned char rate);