b36fc2000828199ec1ceabe1f21e72aa6deff4c2
[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/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_next(struct list_head *list)
66 {
67         return list->next;
68 }
69
70 static inline struct list_head *get_list_head(struct __queue *queue)
71 {
72         return &(queue->queue);
73 }
74
75
76 #define LIST_CONTAINOR(ptr, type, member) \
77         ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
78
79 static inline int _enter_critical_mutex(struct mutex *pmutex,
80                                         unsigned long *pirqL)
81 {
82         int ret;
83
84         ret = mutex_lock_interruptible(pmutex);
85         return ret;
86 }
87
88
89 static inline void _exit_critical_mutex(struct mutex *pmutex,
90                                         unsigned long *pirqL)
91 {
92                 mutex_unlock(pmutex);
93 }
94
95 static inline void rtw_list_delete(struct list_head *plist)
96 {
97         list_del_init(plist);
98 }
99
100 static inline void _init_timer(struct timer_list *ptimer,
101                                struct  net_device *nic_hdl,
102                                void *pfunc, void *cntx)
103 {
104         ptimer->function = pfunc;
105         ptimer->data = (unsigned long)cntx;
106         init_timer(ptimer);
107 }
108
109 static inline void _set_timer(struct timer_list *ptimer, u32 delay_time)
110 {
111         mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
112 }
113
114 static inline void _cancel_timer(struct timer_list *ptimer, u8 *bcancelled)
115 {
116         del_timer_sync(ptimer);
117         *bcancelled = true;/* true ==1; false==0 */
118 }
119
120 #define RTW_TIMER_HDL_ARGS void *FunctionContext
121 #define RTW_TIMER_HDL_NAME(name) rtw_##name##_timer_hdl
122 #define RTW_DECLARE_TIMER_HDL(name) \
123         void RTW_TIMER_HDL_NAME(name)(RTW_TIMER_HDL_ARGS)
124
125 static inline void _init_workitem(struct work_struct *pwork, void *pfunc,
126                                   void *cntx)
127 {
128         INIT_WORK(pwork, pfunc);
129 }
130
131 static inline void _set_workitem(struct work_struct *pwork)
132 {
133         schedule_work(pwork);
134 }
135
136 static inline void _cancel_workitem_sync(struct work_struct *pwork)
137 {
138         cancel_work_sync(pwork);
139 }
140
141 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
142 {
143         return  netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
144                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
145                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
146                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3));
147 }
148
149 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
150 {
151         netif_tx_wake_all_queues(pnetdev);
152 }
153
154 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
155 {
156         netif_tx_start_all_queues(pnetdev);
157 }
158
159 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
160 {
161         netif_tx_stop_all_queues(pnetdev);
162 }
163
164 #ifndef BIT
165         #define BIT(x)  (1 << (x))
166 #endif
167
168 #define BIT0    0x00000001
169 #define BIT1    0x00000002
170 #define BIT2    0x00000004
171 #define BIT3    0x00000008
172 #define BIT4    0x00000010
173 #define BIT5    0x00000020
174 #define BIT6    0x00000040
175 #define BIT7    0x00000080
176 #define BIT8    0x00000100
177 #define BIT9    0x00000200
178 #define BIT10   0x00000400
179 #define BIT11   0x00000800
180 #define BIT12   0x00001000
181 #define BIT13   0x00002000
182 #define BIT14   0x00004000
183 #define BIT15   0x00008000
184 #define BIT16   0x00010000
185 #define BIT17   0x00020000
186 #define BIT18   0x00040000
187 #define BIT19   0x00080000
188 #define BIT20   0x00100000
189 #define BIT21   0x00200000
190 #define BIT22   0x00400000
191 #define BIT23   0x00800000
192 #define BIT24   0x01000000
193 #define BIT25   0x02000000
194 #define BIT26   0x04000000
195 #define BIT27   0x08000000
196 #define BIT28   0x10000000
197 #define BIT29   0x20000000
198 #define BIT30   0x40000000
199 #define BIT31   0x80000000
200 #define BIT32   0x0100000000
201 #define BIT33   0x0200000000
202 #define BIT34   0x0400000000
203 #define BIT35   0x0800000000
204 #define BIT36   0x1000000000
205
206 extern int RTW_STATUS_CODE(int error_code);
207
208 /* flags used for rtw_update_mem_stat() */
209 enum {
210         MEM_STAT_VIR_ALLOC_SUCCESS,
211         MEM_STAT_VIR_ALLOC_FAIL,
212         MEM_STAT_VIR_FREE,
213         MEM_STAT_PHY_ALLOC_SUCCESS,
214         MEM_STAT_PHY_ALLOC_FAIL,
215         MEM_STAT_PHY_FREE,
216         MEM_STAT_TX, /* used to distinguish TX/RX, asigned from caller */
217         MEM_STAT_TX_ALLOC_SUCCESS,
218         MEM_STAT_TX_ALLOC_FAIL,
219         MEM_STAT_TX_FREE,
220         MEM_STAT_RX, /* used to distinguish TX/RX, asigned from caller */
221         MEM_STAT_RX_ALLOC_SUCCESS,
222         MEM_STAT_RX_ALLOC_FAIL,
223         MEM_STAT_RX_FREE
224 };
225
226 extern unsigned char MCS_rate_2R[16];
227 extern unsigned char MCS_rate_1R[16];
228 extern unsigned char RTW_WPA_OUI[];
229 extern unsigned char WPA_TKIP_CIPHER[4];
230 extern unsigned char RSN_TKIP_CIPHER[4];
231
232 #define rtw_update_mem_stat(flag, sz) do {} while (0)
233 u8 *_rtw_vmalloc(u32 sz);
234 u8 *_rtw_zvmalloc(u32 sz);
235 void _rtw_vmfree(u8 *pbuf, u32 sz);
236 u8 *_rtw_zmalloc(u32 sz);
237 u8 *_rtw_malloc(u32 sz);
238 void _rtw_mfree(u8 *pbuf, u32 sz);
239 #define rtw_vmalloc(sz)                 _rtw_vmalloc((sz))
240 #define rtw_zvmalloc(sz)                        _rtw_zvmalloc((sz))
241 #define rtw_vmfree(pbuf, sz)            _rtw_vmfree((pbuf), (sz))
242 #define rtw_malloc(sz)                  _rtw_malloc((sz))
243 #define rtw_zmalloc(sz)                 _rtw_zmalloc((sz))
244 #define rtw_mfree(pbuf, sz)             _rtw_mfree((pbuf), (sz))
245
246 void *rtw_malloc2d(int h, int w, int size);
247 void rtw_mfree2d(void *pbuf, int h, int w, int size);
248
249 void _rtw_memcpy(void *dec, void *sour, u32 sz);
250 int  _rtw_memcmp(void *dst, void *src, u32 sz);
251 void _rtw_memset(void *pbuf, int c, u32 sz);
252
253 void _rtw_init_listhead(struct list_head *list);
254 u32  rtw_is_list_empty(struct list_head *phead);
255 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead);
256 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead);
257 void rtw_list_delete(struct list_head *plist);
258
259 void _rtw_init_sema(struct semaphore *sema, int init_val);
260 void _rtw_free_sema(struct semaphore *sema);
261 void _rtw_up_sema(struct semaphore *sema);
262 u32  _rtw_down_sema(struct semaphore *sema);
263 void _rtw_mutex_init(struct mutex *pmutex);
264 void _rtw_mutex_free(struct mutex *pmutex);
265
266 void _rtw_init_queue(struct __queue *pqueue);
267 u32  _rtw_queue_empty(struct __queue *pqueue);
268 u32  rtw_end_of_queue_search(struct list_head *queue,
269                              struct list_head *pelement);
270
271 u32  rtw_systime_to_ms(u32 systime);
272 u32  rtw_ms_to_systime(u32 ms);
273 s32  rtw_get_passing_time_ms(u32 start);
274 s32  rtw_get_time_interval_ms(u32 start, u32 end);
275
276 void rtw_sleep_schedulable(int ms);
277
278 u32  rtw_atoi(u8 *s);
279
280 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
281 {
282         return del_timer_sync(ptimer);
283 }
284
285 static inline void thread_enter(char *name)
286 {
287         allow_signal(SIGTERM);
288 }
289
290 static inline void flush_signals_thread(void)
291 {
292         if (signal_pending(current))
293                 flush_signals(current);
294 }
295
296 static inline int res_to_status(int res)
297 {
298         return res;
299 }
300
301 #define _RND(sz, r) ((((sz)+((r)-1))/(r))*(r))
302 #define RND4(x) (((x >> 2) + (((x & 3) == 0) ?  0 : 1)) << 2)
303
304 static inline u32 _RND4(u32 sz)
305 {
306         u32     val;
307
308         val = ((sz >> 2) + ((sz & 3) ? 1 : 0)) << 2;
309         return val;
310 }
311
312 static inline u32 _RND8(u32 sz)
313 {
314         u32     val;
315
316         val = ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
317         return val;
318 }
319
320 static inline u32 _RND128(u32 sz)
321 {
322         u32     val;
323
324         val = ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
325         return val;
326 }
327
328 static inline u32 _RND256(u32 sz)
329 {
330         u32     val;
331
332         val = ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
333         return val;
334 }
335
336 static inline u32 _RND512(u32 sz)
337 {
338         u32     val;
339
340         val = ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
341         return val;
342 }
343
344 static inline u32 bitshift(u32 bitmask)
345 {
346         u32 i;
347
348         for (i = 0; i <= 31; i++)
349                 if (((bitmask>>i) &  0x1) == 1)
350                         break;
351         return i;
352 }
353
354 /*  limitation of path length */
355 #define PATH_LENGTH_MAX PATH_MAX
356
357 struct rtw_netdev_priv_indicator {
358         void *priv;
359         u32 sizeof_priv;
360 };
361 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
362                                                     void *old_priv);
363 struct net_device *rtw_alloc_etherdev(int sizeof_priv);
364
365 #define rtw_netdev_priv(netdev)                                 \
366         (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
367 void rtw_free_netdev(struct net_device *netdev);
368
369 #define NDEV_FMT "%s"
370 #define NDEV_ARG(ndev) ndev->name
371 #define ADPT_FMT "%s"
372 #define ADPT_ARG(adapter) adapter->pnetdev->name
373 #define FUNC_NDEV_FMT "%s(%s)"
374 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
375 #define FUNC_ADPT_FMT "%s(%s)"
376 #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
377
378 #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)), (sig), 1)
379
380 u64 rtw_modular64(u64 x, u64 y);
381 u64 rtw_division64(u64 x, u64 y);
382
383 /* Macros for handling unaligned memory accesses */
384
385 #define RTW_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
386 #define RTW_PUT_BE16(a, val)                    \
387         do {                                    \
388                 (a)[0] = ((u16) (val)) >> 8;    \
389                 (a)[1] = ((u16) (val)) & 0xff;  \
390         } while (0)
391
392 #define RTW_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
393 #define RTW_PUT_LE16(a, val)                    \
394         do {                                    \
395                 (a)[1] = ((u16) (val)) >> 8;    \
396                 (a)[0] = ((u16) (val)) & 0xff;  \
397         } while (0)
398
399 #define RTW_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
400                          ((u32) (a)[2]))
401 #define RTW_PUT_BE24(a, val)                                    \
402         do {                                                    \
403                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
404                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
405                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
406         } while (0)
407
408 #define RTW_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
409                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
410 #define RTW_PUT_BE32(a, val)                                    \
411         do {                                                    \
412                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
413                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
414                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
415                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
416         } while (0)
417
418 #define RTW_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
419                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
420 #define RTW_PUT_LE32(a, val)                                    \
421         do {                                                    \
422                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
423                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
424                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
425                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
426         } while (0)
427
428 #define RTW_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
429                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
430                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
431                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
432 #define RTW_PUT_BE64(a, val)                            \
433         do {                                            \
434                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
435                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
436                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
437                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
438                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
439                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
440                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
441                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
442         } while (0)
443
444 #define RTW_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
445                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
446                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
447                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
448
449 void rtw_buf_free(u8 **buf, u32 *buf_len);
450 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len);
451
452 struct rtw_cbuf {
453         u32 write;
454         u32 read;
455         u32 size;
456         void *bufs[0];
457 };
458
459 bool rtw_cbuf_full(struct rtw_cbuf *cbuf);
460 bool rtw_cbuf_empty(struct rtw_cbuf *cbuf);
461 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf);
462 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf);
463 struct rtw_cbuf *rtw_cbuf_alloc(u32 size);
464 int wifirate2_ratetbl_inx(unsigned char rate);
465
466 #endif