ce76a2c99bbdbace21333c37258581f3e8c7d768
[linux-2.6-microblaze.git] / drivers / staging / r8188eu / 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 <linux/version.h>
24 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
25 #include <linux/sched/signal.h>
26 #endif
27 #include <basic_types.h>
28
29 #define _FAIL           0
30 #define _SUCCESS        1
31 #define RTW_RX_HANDLED 2
32
33 #include <linux/spinlock.h>
34 #include <linux/compiler.h>
35 #include <linux/kernel.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/module.h>
40 #include <linux/kref.h>
41 #include <linux/netdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/circ_buf.h>
44 #include <linux/uaccess.h>
45 #include <asm/byteorder.h>
46 #include <asm/atomic.h>
47 #include <linux/io.h>
48 #include <linux/semaphore.h>
49 #include <linux/sem.h>
50 #include <linux/sched.h>
51 #include <linux/etherdevice.h>
52 #include <linux/wireless.h>
53 #include <net/iw_handler.h>
54 #include <linux/if_arp.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/delay.h>
57 #include <linux/proc_fs.h>      /*  Necessary because we use the proc fs */
58 #include <linux/interrupt.h>    /*  for struct tasklet_struct */
59 #include <linux/ip.h>
60 #include <linux/kthread.h>
61
62 #include <linux/usb.h>
63 #include <linux/usb/ch9.h>
64
65 struct  __queue {
66         struct  list_head       queue;
67         spinlock_t lock;
68 };
69
70 #define thread_exit() complete_and_exit(NULL, 0)
71
72 static inline struct list_head *get_list_head(struct __queue *queue)
73 {
74         return (&(queue->queue));
75 }
76
77 static inline int _enter_critical_mutex(struct mutex *pmutex, unsigned long *pirqL)
78 {
79         int ret;
80
81         ret = mutex_lock_interruptible(pmutex);
82         return ret;
83 }
84
85 static inline void _exit_critical_mutex(struct mutex *pmutex, unsigned long *pirqL)
86 {
87                 mutex_unlock(pmutex);
88 }
89
90 static inline void rtw_list_delete(struct list_head *plist)
91 {
92         list_del_init(plist);
93 }
94
95 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 15, 0)
96 static inline void _init_timer(struct timer_list *ptimer,struct  net_device *nic_hdl,void *pfunc,void* cntx)
97 {
98         ptimer->function = pfunc;
99         ptimer->data = (unsigned long)cntx;
100         init_timer(ptimer);
101 }
102 #endif
103
104 static inline void _set_timer(struct timer_list *ptimer,u32 delay_time)
105 {
106         mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
107 }
108
109 static inline void _cancel_timer(struct timer_list *ptimer,u8 *bcancelled)
110 {
111         del_timer_sync(ptimer);
112         *bcancelled=  true;/* true ==1; false==0 */
113 }
114
115 #define RTW_TIMER_HDL_ARGS void *FunctionContext
116 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
117 #define RTW_DECLARE_TIMER_HDL(name) void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
118
119 static inline void _init_workitem(struct work_struct *pwork, void *pfunc, void * cntx)
120 {
121         INIT_WORK(pwork, pfunc);
122 }
123
124 static inline void _set_workitem(struct work_struct *pwork)
125 {
126         schedule_work(pwork);
127 }
128
129 static inline void _cancel_workitem_sync(struct work_struct *pwork)
130 {
131         cancel_work_sync(pwork);
132 }
133 /*  */
134 /*  Global Mutex: can only be used at PASSIVE level. */
135 /*  */
136
137 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
138 {                                                               \
139         while (atomic_inc_return((atomic_t *)&(_MutexCounter)) != 1)\
140         {                                                           \
141                 atomic_dec((atomic_t *)&(_MutexCounter));        \
142                 msleep(10);                          \
143         }                                                           \
144 }
145
146 #define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
147 {                                                               \
148         atomic_dec((atomic_t *)&(_MutexCounter));        \
149 }
150
151 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
152 {
153         return  netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
154                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
155                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
156                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3));
157 }
158
159 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
160 {
161         netif_tx_wake_all_queues(pnetdev);
162 }
163
164 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
165 {
166         netif_tx_start_all_queues(pnetdev);
167 }
168
169 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
170 {
171         netif_tx_stop_all_queues(pnetdev);
172 }
173
174 #ifndef BIT
175         #define BIT(x)  ( 1 << (x))
176 #endif
177
178 #define BIT0    0x00000001
179 #define BIT1    0x00000002
180 #define BIT2    0x00000004
181 #define BIT3    0x00000008
182 #define BIT4    0x00000010
183 #define BIT5    0x00000020
184 #define BIT6    0x00000040
185 #define BIT7    0x00000080
186 #define BIT8    0x00000100
187 #define BIT9    0x00000200
188 #define BIT10   0x00000400
189 #define BIT11   0x00000800
190 #define BIT12   0x00001000
191 #define BIT13   0x00002000
192 #define BIT14   0x00004000
193 #define BIT15   0x00008000
194 #define BIT16   0x00010000
195 #define BIT17   0x00020000
196 #define BIT18   0x00040000
197 #define BIT19   0x00080000
198 #define BIT20   0x00100000
199 #define BIT21   0x00200000
200 #define BIT22   0x00400000
201 #define BIT23   0x00800000
202 #define BIT24   0x01000000
203 #define BIT25   0x02000000
204 #define BIT26   0x04000000
205 #define BIT27   0x08000000
206 #define BIT28   0x10000000
207 #define BIT29   0x20000000
208 #define BIT30   0x40000000
209 #define BIT31   0x80000000
210 #define BIT32   0x0100000000
211 #define BIT33   0x0200000000
212 #define BIT34   0x0400000000
213 #define BIT35   0x0800000000
214 #define BIT36   0x1000000000
215
216 extern int RTW_STATUS_CODE(int error_code);
217
218 /* flags used for rtw_update_mem_stat() */
219 enum {
220         MEM_STAT_VIR_ALLOC_SUCCESS,
221         MEM_STAT_VIR_ALLOC_FAIL,
222         MEM_STAT_VIR_FREE,
223         MEM_STAT_PHY_ALLOC_SUCCESS,
224         MEM_STAT_PHY_ALLOC_FAIL,
225         MEM_STAT_PHY_FREE,
226         MEM_STAT_TX, /* used to distinguish TX/RX, asigned from caller */
227         MEM_STAT_TX_ALLOC_SUCCESS,
228         MEM_STAT_TX_ALLOC_FAIL,
229         MEM_STAT_TX_FREE,
230         MEM_STAT_RX, /* used to distinguish TX/RX, asigned from caller */
231         MEM_STAT_RX_ALLOC_SUCCESS,
232         MEM_STAT_RX_ALLOC_FAIL,
233         MEM_STAT_RX_FREE
234 };
235
236 extern unsigned char MCS_rate_2R[16];
237 extern unsigned char MCS_rate_1R[16];
238 extern unsigned char RTW_WPA_OUI[];
239 extern unsigned char WPA_TKIP_CIPHER[4];
240 extern unsigned char RSN_TKIP_CIPHER[4];
241
242 #define rtw_update_mem_stat(flag, sz) do {} while (0)
243 u8 *_rtw_vmalloc(u32 sz);
244 u8 *_rtw_zvmalloc(u32 sz);
245 void _rtw_vmfree(u8 *pbuf, u32 sz);
246 u8 *_rtw_zmalloc(u32 sz);
247 u8 *_rtw_malloc(u32 sz);
248 void _rtw_mfree(u8 *pbuf, u32 sz);
249 #define rtw_vmalloc(sz)                 _rtw_vmalloc((sz))
250 #define rtw_zvmalloc(sz)                        _rtw_zvmalloc((sz))
251 #define rtw_vmfree(pbuf, sz)            _rtw_vmfree((pbuf), (sz))
252 #define rtw_malloc(sz)                  _rtw_malloc((sz))
253 #define rtw_zmalloc(sz)                 _rtw_zmalloc((sz))
254 #define rtw_mfree(pbuf, sz)             _rtw_mfree((pbuf), (sz))
255
256 void *rtw_malloc2d(int h, int w, int size);
257 void rtw_mfree2d(void *pbuf, int h, int w, int size);
258
259 u32  _rtw_down_sema(struct semaphore *sema);
260 void _rtw_mutex_init(struct mutex *pmutex);
261 void _rtw_mutex_free(struct mutex *pmutex);
262 void _rtw_spinlock_free(spinlock_t *plock);
263
264 void _rtw_init_queue(struct __queue *pqueue);
265
266 u32  rtw_systime_to_ms(u32 systime);
267 u32  rtw_ms_to_systime(u32 ms);
268 s32  rtw_get_passing_time_ms(u32 start);
269 s32  rtw_get_time_interval_ms(u32 start, u32 end);
270
271 void rtw_sleep_schedulable(int ms);
272
273 void rtw_msleep_os(int ms);
274 void rtw_usleep_os(int us);
275
276 u32  rtw_atoi(u8 *s);
277
278 void rtw_mdelay_os(int ms);
279 void rtw_udelay_os(int us);
280
281 void rtw_yield_os(void);
282
283 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
284 {
285         return del_timer_sync(ptimer);
286 }
287
288 static __inline void thread_enter(char *name)
289 {
290 #ifdef daemonize
291         daemonize("%s", name);
292 #endif
293         allow_signal(SIGTERM);
294 }
295
296 static inline void flush_signals_thread(void)
297 {
298         if (signal_pending (current))
299                 flush_signals(current);
300 }
301
302 static inline int res_to_status(int res)
303 {
304         return res;
305 }
306
307 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
308 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ?  0: 1)) << 2)
309
310 static inline u32 _RND4(u32 sz)
311 {
312         u32     val;
313
314         val = ((sz >> 2) + ((sz & 3) ? 1: 0)) << 2;
315         return val;
316 }
317
318 static inline u32 _RND8(u32 sz)
319 {
320         u32     val;
321
322         val = ((sz >> 3) + ((sz & 7) ? 1: 0)) << 3;
323         return val;
324 }
325
326 static inline u32 _RND128(u32 sz)
327 {
328         u32     val;
329
330         val = ((sz >> 7) + ((sz & 127) ? 1: 0)) << 7;
331         return val;
332 }
333
334 static inline u32 _RND256(u32 sz)
335 {
336         u32     val;
337
338         val = ((sz >> 8) + ((sz & 255) ? 1: 0)) << 8;
339         return val;
340 }
341
342 static inline u32 _RND512(u32 sz)
343 {
344         u32     val;
345
346         val = ((sz >> 9) + ((sz & 511) ? 1: 0)) << 9;
347         return val;
348 }
349
350 static inline u32 bitshift(u32 bitmask)
351 {
352         u32 i;
353
354         for (i = 0; i <= 31; i++)
355                 if (((bitmask>>i) &  0x1) == 1) break;
356         return i;
357 }
358
359 /*  limitation of path length */
360 #define PATH_LENGTH_MAX PATH_MAX
361
362 void rtw_suspend_lock_init(void);
363 void rtw_suspend_lock_uninit(void);
364 void rtw_lock_suspend(void);
365 void rtw_unlock_suspend(void);
366
367 /* Atomic integer operations */
368 #define ATOMIC_T atomic_t
369
370 void ATOMIC_SET(ATOMIC_T *v, int i);
371 int ATOMIC_READ(ATOMIC_T *v);
372 void ATOMIC_ADD(ATOMIC_T *v, int i);
373 void ATOMIC_SUB(ATOMIC_T *v, int i);
374 void ATOMIC_INC(ATOMIC_T *v);
375 void ATOMIC_DEC(ATOMIC_T *v);
376 int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i);
377 int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i);
378 int ATOMIC_INC_RETURN(ATOMIC_T *v);
379 int ATOMIC_DEC_RETURN(ATOMIC_T *v);
380
381 struct rtw_netdev_priv_indicator {
382         void *priv;
383         u32 sizeof_priv;
384 };
385 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
386                                                     void *old_priv);
387 struct net_device *rtw_alloc_etherdev(int sizeof_priv);
388
389 #define rtw_netdev_priv(netdev)                                 \
390         (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
391 void rtw_free_netdev(struct net_device *netdev);
392
393 #define NDEV_FMT "%s"
394 #define NDEV_ARG(ndev) ndev->name
395 #define ADPT_FMT "%s"
396 #define ADPT_ARG(adapter) adapter->pnetdev->name
397 #define FUNC_NDEV_FMT "%s(%s)"
398 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
399 #define FUNC_ADPT_FMT "%s(%s)"
400 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
401
402 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
403
404 u64 rtw_modular64(u64 x, u64 y);
405 u64 rtw_division64(u64 x, u64 y);
406
407 /* Macros for handling unaligned memory accesses */
408
409 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
410 #define RTW_PUT_BE16(a, val)                    \
411         do {                                    \
412                 (a)[0] = ((u16) (val)) >> 8;    \
413                 (a)[1] = ((u16) (val)) & 0xff;  \
414         } while (0)
415
416 #define RTW_PUT_LE16(a, val)                    \
417         do {                                    \
418                 (a)[1] = ((u16) (val)) >> 8;    \
419                 (a)[0] = ((u16) (val)) & 0xff;  \
420         } while (0)
421
422 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
423                          ((u32) (a)[2]))
424 #define RTW_PUT_BE24(a, val)                                    \
425         do {                                                    \
426                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
427                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
428                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
429         } while (0)
430
431 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
432                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
433 #define RTW_PUT_BE32(a, val)                                    \
434         do {                                                    \
435                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
436                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
437                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
438                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
439         } while (0)
440
441 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
442                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
443 #define RTW_PUT_LE32(a, val)                                    \
444         do {                                                    \
445                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
446                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
447                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
448                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
449         } while (0)
450
451 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
452                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
453                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
454                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
455 #define RTW_PUT_BE64(a, val)                            \
456         do {                                            \
457                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
458                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
459                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
460                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
461                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
462                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
463                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
464                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
465         } while (0)
466
467 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
468                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
469                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
470                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
471
472 void rtw_buf_free(u8 **buf, u32 *buf_len);
473 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
474
475 struct rtw_cbuf {
476         u32 write;
477         u32 read;
478         u32 size;
479         void *bufs[0];
480 };
481
482 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
483 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
484 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
485 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
486 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
487 int wifirate2_ratetbl_inx(unsigned char rate);
488
489 #endif