osdep_service_linux.h 14 KB

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