net: wireless: rockchip_wlan: add rtl8723ds support
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8723ds / include / osdep_service_linux.h
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2013 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_LINUX_SERVICE_H_
21 #define __OSDEP_LINUX_SERVICE_H_
22
23 #include <linux/version.h>
24 #include <linux/spinlock.h>
25 #include <linux/compiler.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/module.h>
31 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 5))
32         #include <linux/kref.h>
33 #endif
34 /* #include <linux/smp_lock.h> */
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/circ_buf.h>
38 #include <asm/uaccess.h>
39 #include <asm/byteorder.h>
40 #include <asm/atomic.h>
41 #include <asm/io.h>
42 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
43         #include <asm/semaphore.h>
44 #else
45         #include <linux/semaphore.h>
46 #endif
47 #include <linux/sem.h>
48 #include <linux/sched.h>
49 #include <linux/etherdevice.h>
50 #include <linux/wireless.h>
51 #include <net/iw_handler.h>
52 #include <linux/if_arp.h>
53 #include <linux/rtnetlink.h>
54 #include <linux/delay.h>
55 #include <linux/interrupt.h>    /* for struct tasklet_struct */
56 #include <linux/ip.h>
57 #include <linux/kthread.h>
58 #include <linux/list.h>
59 #include <linux/vmalloc.h>
60
61 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2, 5, 41))
62         #include <linux/tqueue.h>
63 #endif
64
65 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0))
66         #include <uapi/linux/limits.h>
67 #else
68         #include <linux/limits.h>
69 #endif
70
71 #ifdef RTK_DMP_PLATFORM
72         #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 12))
73                 #include <linux/pageremap.h>
74         #endif
75         #include <asm/io.h>
76 #endif
77
78 #ifdef CONFIG_NET_RADIO
79         #define CONFIG_WIRELESS_EXT
80 #endif
81
82 /* Monitor mode */
83 #include <net/ieee80211_radiotap.h>
84
85 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24))
86         #include <linux/ieee80211.h>
87 #endif
88
89 #ifdef CONFIG_IOCTL_CFG80211
90         /*      #include <linux/ieee80211.h> */
91         #include <net/cfg80211.h>
92 #endif /* CONFIG_IOCTL_CFG80211 */
93
94 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
95         #include <linux/in.h>
96         #include <linux/udp.h>
97 #endif
98
99 #ifdef CONFIG_HAS_EARLYSUSPEND
100         #include <linux/earlysuspend.h>
101 #endif /* CONFIG_HAS_EARLYSUSPEND */
102
103 #ifdef CONFIG_EFUSE_CONFIG_FILE
104         #include <linux/fs.h>
105 #endif
106
107 #ifdef CONFIG_USB_HCI
108         #include <linux/usb.h>
109         #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21))
110                 #include <linux/usb_ch9.h>
111         #else
112                 #include <linux/usb/ch9.h>
113         #endif
114 #endif
115
116 #ifdef CONFIG_BT_COEXIST_SOCKET_TRX
117         #include <net/sock.h>
118         #include <net/tcp.h>
119         #include <linux/udp.h>
120         #include <linux/in.h>
121         #include <linux/netlink.h>
122 #endif /* CONFIG_BT_COEXIST_SOCKET_TRX */
123
124 #ifdef CONFIG_USB_HCI
125         typedef struct urb   *PURB;
126         #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22))
127                 #ifdef CONFIG_USB_SUSPEND
128                         #define CONFIG_AUTOSUSPEND      1
129                 #endif
130         #endif
131 #endif
132
133 #if defined(CONFIG_RTW_GRO) && (!defined(CONFIG_RTW_NAPI))
134
135         #error "Enable NAPI before enable GRO\n"
136
137 #elif (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29) && defined(CONFIG_RTW_NAPI))
138
139         #error "Linux Kernel version too old (should newer than 2.6.29)\n"
140
141 #endif
142
143
144 typedef struct  semaphore _sema;
145 typedef spinlock_t      _lock;
146 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
147         typedef struct mutex            _mutex;
148 #else
149         typedef struct semaphore        _mutex;
150 #endif
151 typedef struct timer_list _timer;
152
153 struct  __queue {
154         struct  list_head       queue;
155         _lock   lock;
156 };
157
158 typedef struct sk_buff  _pkt;
159 typedef unsigned char   _buffer;
160
161 typedef struct  __queue _queue;
162 typedef struct  list_head       _list;
163 typedef int     _OS_STATUS;
164 /* typedef u32  _irqL; */
165 typedef unsigned long _irqL;
166 typedef struct  net_device *_nic_hdl;
167
168 typedef void            *_thread_hdl_;
169 typedef int             thread_return;
170 typedef void    *thread_context;
171
172 #define thread_exit() complete_and_exit(NULL, 0)
173
174 typedef void timer_hdl_return;
175 typedef void *timer_hdl_context;
176
177 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
178         typedef struct work_struct _workitem;
179 #else
180         typedef struct tq_struct _workitem;
181 #endif
182
183 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24))
184         #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
185 #endif
186
187 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22))
188 /* Porting from linux kernel, for compatible with old kernel. */
189 static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
190 {
191         return skb->tail;
192 }
193
194 static inline void skb_reset_tail_pointer(struct sk_buff *skb)
195 {
196         skb->tail = skb->data;
197 }
198
199 static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
200 {
201         skb->tail = skb->data + offset;
202 }
203
204 static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
205 {
206         return skb->end;
207 }
208 #endif
209
210 __inline static _list *get_next(_list   *list)
211 {
212         return list->next;
213 }
214
215 __inline static _list   *get_list_head(_queue   *queue)
216 {
217         return &(queue->queue);
218 }
219
220
221 #define LIST_CONTAINOR(ptr, type, member) \
222         ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
223
224
225 __inline static void _enter_critical(_lock *plock, _irqL *pirqL)
226 {
227         spin_lock_irqsave(plock, *pirqL);
228 }
229
230 __inline static void _exit_critical(_lock *plock, _irqL *pirqL)
231 {
232         spin_unlock_irqrestore(plock, *pirqL);
233 }
234
235 __inline static void _enter_critical_ex(_lock *plock, _irqL *pirqL)
236 {
237         spin_lock_irqsave(plock, *pirqL);
238 }
239
240 __inline static void _exit_critical_ex(_lock *plock, _irqL *pirqL)
241 {
242         spin_unlock_irqrestore(plock, *pirqL);
243 }
244
245 __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
246 {
247         spin_lock_bh(plock);
248 }
249
250 __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
251 {
252         spin_unlock_bh(plock);
253 }
254
255 __inline static int _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
256 {
257         int ret = 0;
258 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
259         /* mutex_lock(pmutex); */
260         ret = mutex_lock_interruptible(pmutex);
261 #else
262         ret = down_interruptible(pmutex);
263 #endif
264         return ret;
265 }
266
267
268 __inline static void _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
269 {
270 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37))
271         mutex_unlock(pmutex);
272 #else
273         up(pmutex);
274 #endif
275 }
276
277 __inline static void rtw_list_delete(_list *plist)
278 {
279         list_del_init(plist);
280 }
281
282 #define RTW_TIMER_HDL_ARGS void *FunctionContext
283
284 __inline static void _init_timer(_timer *ptimer, _nic_hdl nic_hdl, void *pfunc, void *cntx)
285 {
286         /* setup_timer(ptimer, pfunc,(u32)cntx);         */
287         ptimer->function = pfunc;
288         ptimer->data = (unsigned long)cntx;
289         init_timer(ptimer);
290 }
291
292 __inline static void _set_timer(_timer *ptimer, u32 delay_time)
293 {
294         mod_timer(ptimer , (jiffies + (delay_time * HZ / 1000)));
295 }
296
297 __inline static void _cancel_timer(_timer *ptimer, u8 *bcancelled)
298 {
299         del_timer_sync(ptimer);
300         *bcancelled = 1;
301 }
302
303
304 static inline void _init_workitem(_workitem *pwork, void *pfunc, void *cntx)
305 {
306 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
307         INIT_WORK(pwork, pfunc);
308 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
309         INIT_WORK(pwork, pfunc, pwork);
310 #else
311         INIT_TQUEUE(pwork, pfunc, pwork);
312 #endif
313 }
314
315 __inline static void _set_workitem(_workitem *pwork)
316 {
317 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
318         schedule_work(pwork);
319 #else
320         schedule_task(pwork);
321 #endif
322 }
323
324 __inline static void _cancel_workitem_sync(_workitem *pwork)
325 {
326 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22))
327         cancel_work_sync(pwork);
328 #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 41))
329         flush_scheduled_work();
330 #else
331         flush_scheduled_tasks();
332 #endif
333 }
334 /*
335  * Global Mutex: can only be used at PASSIVE level.
336  *   */
337
338 #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter)                              \
339         {                                                               \
340                 while (atomic_inc_return((atomic_t *)&(_MutexCounter)) != 1) { \
341                         atomic_dec((atomic_t *)&(_MutexCounter));        \
342                         msleep(10);                          \
343                 }                                                           \
344         }
345
346 #define RELEASE_GLOBAL_MUTEX(_MutexCounter)                              \
347         {                                                               \
348                 atomic_dec((atomic_t *)&(_MutexCounter));        \
349         }
350
351 static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
352 {
353 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
354         return (netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
355                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
356                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
357                 netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3)));
358 #else
359         return netif_queue_stopped(pnetdev);
360 #endif
361 }
362
363 static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
364 {
365 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
366         netif_tx_wake_all_queues(pnetdev);
367 #else
368         netif_wake_queue(pnetdev);
369 #endif
370 }
371
372 static inline void rtw_netif_start_queue(struct net_device *pnetdev)
373 {
374 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
375         netif_tx_start_all_queues(pnetdev);
376 #else
377         netif_start_queue(pnetdev);
378 #endif
379 }
380
381 static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
382 {
383 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
384         netif_tx_stop_all_queues(pnetdev);
385 #else
386         netif_stop_queue(pnetdev);
387 #endif
388 }
389 static inline void rtw_netif_carrier_on(struct net_device *pnetdev)
390 {
391         netif_device_attach(pnetdev);
392         netif_carrier_on(pnetdev);
393 }
394 static inline int rtw_merge_string(char *dst, int dst_len, const char *src1, const char *src2)
395 {
396         int     len = 0;
397         len += snprintf(dst + len, dst_len - len, "%s", src1);
398         len += snprintf(dst + len, dst_len - len, "%s", src2);
399
400         return len;
401 }
402
403 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27))
404         #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)), (sig), 1)
405 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
406         #define rtw_signal_process(pid, sig) kill_proc((pid), (sig), 1)
407 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)) */
408
409
410 /* Suspend lock prevent system from going suspend */
411 #ifdef CONFIG_WAKELOCK
412         #include <linux/wakelock.h>
413 #elif defined(CONFIG_ANDROID_POWER)
414         #include <linux/android_power.h>
415 #endif
416
417 /* limitation of path length */
418 #define PATH_LENGTH_MAX PATH_MAX
419
420 /* Atomic integer operations */
421 #define ATOMIC_T atomic_t
422
423 #define rtw_netdev_priv(netdev) (((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv)
424
425 #define NDEV_FMT "%s"
426 #define NDEV_ARG(ndev) ndev->name
427 #define ADPT_FMT "%s"
428 #define ADPT_ARG(adapter) (adapter->pnetdev ? adapter->pnetdev->name : NULL)
429 #define FUNC_NDEV_FMT "%s(%s)"
430 #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
431 #define FUNC_ADPT_FMT "%s(%s)"
432 #define FUNC_ADPT_ARG(adapter) __func__, (adapter->pnetdev ? adapter->pnetdev->name : NULL)
433
434 struct rtw_netdev_priv_indicator {
435         void *priv;
436         u32 sizeof_priv;
437 };
438 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv);
439 extern struct net_device *rtw_alloc_etherdev(int sizeof_priv);
440
441 #define STRUCT_PACKED __attribute__ ((packed))
442
443
444 #endif