staging: rtl8188eu:Remove unused inline function res_to_status()
[cascardo/linux.git] / drivers / staging / rtl8188eu / 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 <basic_types.h>
24
25 #define _FAIL           0
26 #define _SUCCESS        1
27 #define RTW_RX_HANDLED  2
28
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>
42 #include <linux/io.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 */
54 #include <linux/ip.h>
55 #include <linux/kthread.h>
56
57 #include <linux/usb.h>
58 #include <linux/usb/ch9.h>
59
60 struct  __queue {
61         struct  list_head       queue;
62         spinlock_t lock;
63 };
64
65 static inline struct list_head *get_list_head(struct __queue *queue)
66 {
67         return &(queue->queue);
68 }
69
70 static inline int _enter_critical_mutex(struct mutex *pmutex,
71                                         unsigned long *pirqL)
72 {
73         int ret;
74
75         ret = mutex_lock_interruptible(pmutex);
76         return ret;
77 }
78
79 static inline void rtw_list_delete(struct list_head *plist)
80 {
81         list_del_init(plist);
82 }
83
84 static inline void _init_timer(struct timer_list *ptimer,
85                                struct  net_device *nic_hdl,
86                                void *pfunc, void *cntx)
87 {
88         ptimer->function = pfunc;
89         ptimer->data = (unsigned long)cntx;
90         init_timer(ptimer);
91 }
92
93 static inline void _set_timer(struct timer_list *ptimer, u32 delay_time)
94 {
95         mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
96 }
97
98 static inline void _cancel_timer(struct timer_list *ptimer, u8 *bcancelled)
99 {
100         del_timer_sync(ptimer);
101         *bcancelled = true;/* true ==1; false==0 */
102 }
103
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)
108
109 static inline void _cancel_workitem_sync(struct work_struct *pwork)
110 {
111         cancel_work_sync(pwork);
112 }
113
114 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
115 {
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));
120 }
121
122 #ifndef BIT
123         #define BIT(x)  (1 << (x))
124 #endif
125
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
163
164 extern int RTW_STATUS_CODE(int error_code);
165
166 /* flags used for rtw_update_mem_stat() */
167 enum {
168         MEM_STAT_VIR_ALLOC_SUCCESS,
169         MEM_STAT_VIR_ALLOC_FAIL,
170         MEM_STAT_VIR_FREE,
171         MEM_STAT_PHY_ALLOC_SUCCESS,
172         MEM_STAT_PHY_ALLOC_FAIL,
173         MEM_STAT_PHY_FREE,
174         MEM_STAT_TX, /* used to distinguish TX/RX, asigned from caller */
175         MEM_STAT_TX_ALLOC_SUCCESS,
176         MEM_STAT_TX_ALLOC_FAIL,
177         MEM_STAT_TX_FREE,
178         MEM_STAT_RX, /* used to distinguish TX/RX, asigned from caller */
179         MEM_STAT_RX_ALLOC_SUCCESS,
180         MEM_STAT_RX_ALLOC_FAIL,
181         MEM_STAT_RX_FREE
182 };
183
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];
189
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))
197
198 void *rtw_malloc2d(int h, int w, int size);
199 void rtw_mfree2d(void *pbuf, int h, int w, int size);
200
201 void _rtw_memcpy(void *dec, void *sour, u32 sz);
202 void _rtw_memset(void *pbuf, int c, u32 sz);
203
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);
209
210 u32  _rtw_down_sema(struct semaphore *sema);
211
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);
216
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);
221
222 void rtw_sleep_schedulable(int ms);
223
224 u32  rtw_atoi(u8 *s);
225
226 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
227 {
228         return del_timer_sync(ptimer);
229 }
230
231 static inline void thread_enter(char *name)
232 {
233         allow_signal(SIGTERM);
234 }
235
236 static inline void flush_signals_thread(void)
237 {
238         if (signal_pending(current))
239                 flush_signals(current);
240 }
241
242 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
243 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ?  0 : 1)) << 2)
244
245 static inline u32 _RND4(u32 sz)
246 {
247         u32     val;
248
249         val = ((sz >> 2) + ((sz & 3) ? 1 : 0)) << 2;
250         return val;
251 }
252
253 static inline u32 _RND8(u32 sz)
254 {
255         u32     val;
256
257         val = ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
258         return val;
259 }
260
261 static inline u32 _RND128(u32 sz)
262 {
263         u32     val;
264
265         val = ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
266         return val;
267 }
268
269 static inline u32 _RND256(u32 sz)
270 {
271         u32     val;
272
273         val = ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
274         return val;
275 }
276
277 static inline u32 _RND512(u32 sz)
278 {
279         u32     val;
280
281         val = ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
282         return val;
283 }
284
285 static inline u32 bitshift(u32 bitmask)
286 {
287         u32 i;
288
289         for (i = 0; i <= 31; i++)
290                 if (((bitmask>>i) &  0x1) == 1)
291                         break;
292         return i;
293 }
294
295 /*  limitation of path length */
296 #define PATH_LENGTH_MAX PATH_MAX
297
298 struct rtw_netdev_priv_indicator {
299         void *priv;
300         u32 sizeof_priv;
301 };
302 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
303                                                     void *old_priv);
304 struct net_device *rtw_alloc_etherdev(int sizeof_priv);
305
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);
309
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
318
319 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)), (sig), 1)
320
321 u64 rtw_modular64(u64 x, u64 y);
322 u64 rtw_division64(u64 x, u64 y);
323
324 /* Macros for handling unaligned memory accesses */
325
326 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
327 #define RTW_PUT_BE16(a, val)                    \
328         do {                                    \
329                 (a)[0] = ((u16) (val)) >> 8;    \
330                 (a)[1] = ((u16) (val)) & 0xff;  \
331         } while (0)
332
333 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
334 #define RTW_PUT_LE16(a, val)                    \
335         do {                                    \
336                 (a)[1] = ((u16) (val)) >> 8;    \
337                 (a)[0] = ((u16) (val)) & 0xff;  \
338         } while (0)
339
340 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
341                          ((u32) (a)[2]))
342 #define RTW_PUT_BE24(a, val)                                    \
343         do {                                                    \
344                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
345                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
346                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
347         } while (0)
348
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)                                    \
352         do {                                                    \
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);           \
357         } while (0)
358
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)                                    \
362         do {                                                    \
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);           \
367         } while (0)
368
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)                            \
374         do {                                            \
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);   \
383         } while (0)
384
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]))
389
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);
392
393 struct rtw_cbuf {
394         u32 write;
395         u32 read;
396         u32 size;
397         void *bufs[0];
398 };
399
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);
406
407 #endif