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