osdep_service_linux.h 12 KB

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