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