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