osdep_service_linux.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. typedef struct timer_list _timer;
  111. struct __queue {
  112. struct list_head queue;
  113. _lock lock;
  114. };
  115. typedef struct sk_buff _pkt;
  116. typedef unsigned char _buffer;
  117. typedef struct __queue _queue;
  118. typedef struct list_head _list;
  119. typedef int _OS_STATUS;
  120. //typedef u32 _irqL;
  121. typedef unsigned long _irqL;
  122. typedef struct net_device * _nic_hdl;
  123. typedef void* _thread_hdl_;
  124. typedef int thread_return;
  125. typedef void* thread_context;
  126. #define thread_exit() complete_and_exit(NULL, 0)
  127. typedef void timer_hdl_return;
  128. typedef void* timer_hdl_context;
  129. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41))
  130. typedef struct work_struct _workitem;
  131. #else
  132. typedef struct tq_struct _workitem;
  133. #endif
  134. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
  135. #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
  136. #endif
  137. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22))
  138. // Porting from linux kernel, for compatible with old kernel.
  139. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  140. {
  141. return skb->tail;
  142. }
  143. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  144. {
  145. skb->tail = skb->data;
  146. }
  147. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  148. {
  149. skb->tail = skb->data + offset;
  150. }
  151. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  152. {
  153. return skb->end;
  154. }
  155. #endif
  156. __inline static _list *get_next(_list *list)
  157. {
  158. return list->next;
  159. }
  160. __inline static _list *get_list_head(_queue *queue)
  161. {
  162. return (&(queue->queue));
  163. }
  164. #define LIST_CONTAINOR(ptr, type, member) \
  165. ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
  166. __inline static void _enter_critical(_lock *plock, _irqL *pirqL)
  167. {
  168. spin_lock_irqsave(plock, *pirqL);
  169. }
  170. __inline static void _exit_critical(_lock *plock, _irqL *pirqL)
  171. {
  172. spin_unlock_irqrestore(plock, *pirqL);
  173. }
  174. __inline static void _enter_critical_ex(_lock *plock, _irqL *pirqL)
  175. {
  176. spin_lock_irqsave(plock, *pirqL);
  177. }
  178. __inline static void _exit_critical_ex(_lock *plock, _irqL *pirqL)
  179. {
  180. spin_unlock_irqrestore(plock, *pirqL);
  181. }
  182. __inline static void _enter_critical_bh(_lock *plock, _irqL *pirqL)
  183. {
  184. spin_lock_bh(plock);
  185. }
  186. __inline static void _exit_critical_bh(_lock *plock, _irqL *pirqL)
  187. {
  188. spin_unlock_bh(plock);
  189. }
  190. __inline static int _enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
  191. {
  192. int ret = 0;
  193. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
  194. //mutex_lock(pmutex);
  195. ret = mutex_lock_interruptible(pmutex);
  196. #else
  197. ret = down_interruptible(pmutex);
  198. #endif
  199. return ret;
  200. }
  201. __inline static void _exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
  202. {
  203. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
  204. mutex_unlock(pmutex);
  205. #else
  206. up(pmutex);
  207. #endif
  208. }
  209. __inline static void rtw_list_delete(_list *plist)
  210. {
  211. list_del_init(plist);
  212. }
  213. #define RTW_TIMER_HDL_ARGS void *FunctionContext
  214. __inline static void _init_timer(_timer *ptimer,_nic_hdl nic_hdl,void *pfunc,void* cntx)
  215. {
  216. //setup_timer(ptimer, pfunc,(u32)cntx);
  217. ptimer->function = pfunc;
  218. ptimer->data = (unsigned long)cntx;
  219. init_timer(ptimer);
  220. }
  221. __inline static void _set_timer(_timer *ptimer,u32 delay_time)
  222. {
  223. mod_timer(ptimer , (jiffies+(delay_time*HZ/1000)));
  224. }
  225. __inline static void _cancel_timer(_timer *ptimer,u8 *bcancelled)
  226. {
  227. del_timer_sync(ptimer);
  228. *bcancelled= _TRUE;//TRUE ==1; FALSE==0
  229. }
  230. __inline static void _init_workitem(_workitem *pwork, void *pfunc, PVOID cntx)
  231. {
  232. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
  233. INIT_WORK(pwork, pfunc);
  234. #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41))
  235. INIT_WORK(pwork, pfunc,pwork);
  236. #else
  237. INIT_TQUEUE(pwork, pfunc,pwork);
  238. #endif
  239. }
  240. __inline static void _set_workitem(_workitem *pwork)
  241. {
  242. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41))
  243. schedule_work(pwork);
  244. #else
  245. schedule_task(pwork);
  246. #endif
  247. }
  248. __inline static void _cancel_workitem_sync(_workitem *pwork)
  249. {
  250. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,22))
  251. cancel_work_sync(pwork);
  252. #elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,41))
  253. flush_scheduled_work();
  254. #else
  255. flush_scheduled_tasks();
  256. #endif
  257. }
  258. //
  259. // Global Mutex: can only be used at PASSIVE level.
  260. //
  261. #define ACQUIRE_GLOBAL_MUTEX(_MutexCounter) \
  262. { \
  263. while (atomic_inc_return((atomic_t *)&(_MutexCounter)) != 1)\
  264. { \
  265. atomic_dec((atomic_t *)&(_MutexCounter)); \
  266. msleep(10); \
  267. } \
  268. }
  269. #define RELEASE_GLOBAL_MUTEX(_MutexCounter) \
  270. { \
  271. atomic_dec((atomic_t *)&(_MutexCounter)); \
  272. }
  273. static inline int rtw_netif_queue_stopped(struct net_device *pnetdev)
  274. {
  275. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  276. return (netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 0)) &&
  277. netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 1)) &&
  278. netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 2)) &&
  279. netif_tx_queue_stopped(netdev_get_tx_queue(pnetdev, 3)) );
  280. #else
  281. return netif_queue_stopped(pnetdev);
  282. #endif
  283. }
  284. static inline void rtw_netif_wake_queue(struct net_device *pnetdev)
  285. {
  286. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  287. netif_tx_wake_all_queues(pnetdev);
  288. #else
  289. netif_wake_queue(pnetdev);
  290. #endif
  291. }
  292. static inline void rtw_netif_start_queue(struct net_device *pnetdev)
  293. {
  294. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  295. netif_tx_start_all_queues(pnetdev);
  296. #else
  297. netif_start_queue(pnetdev);
  298. #endif
  299. }
  300. static inline void rtw_netif_stop_queue(struct net_device *pnetdev)
  301. {
  302. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  303. netif_tx_stop_all_queues(pnetdev);
  304. #else
  305. netif_stop_queue(pnetdev);
  306. #endif
  307. }
  308. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
  309. #define rtw_signal_process(pid, sig) kill_pid(find_vpid((pid)),(sig), 1)
  310. #else //(LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
  311. #define rtw_signal_process(pid, sig) kill_proc((pid), (sig), 1)
  312. #endif //(LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27))
  313. // Suspend lock prevent system from going suspend
  314. #ifdef CONFIG_WAKELOCK
  315. #include <linux/wakelock.h>
  316. #elif defined(CONFIG_ANDROID_POWER)
  317. #include <linux/android_power.h>
  318. #endif
  319. // limitation of path length
  320. #define PATH_LENGTH_MAX PATH_MAX
  321. //Atomic integer operations
  322. #define ATOMIC_T atomic_t
  323. #define rtw_netdev_priv(netdev) ( ((struct rtw_netdev_priv_indicator *)netdev_priv(netdev))->priv )
  324. #define NDEV_FMT "%s"
  325. #define NDEV_ARG(ndev) ndev->name
  326. #define ADPT_FMT "%s"
  327. #define ADPT_ARG(adapter) adapter->pnetdev->name
  328. #define FUNC_NDEV_FMT "%s(%s)"
  329. #define FUNC_NDEV_ARG(ndev) __func__, ndev->name
  330. #define FUNC_ADPT_FMT "%s(%s)"
  331. #define FUNC_ADPT_ARG(adapter) __func__, adapter->pnetdev->name
  332. struct rtw_netdev_priv_indicator {
  333. void *priv;
  334. u32 sizeof_priv;
  335. };
  336. struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv);
  337. extern struct net_device * rtw_alloc_etherdev(int sizeof_priv);
  338. #define STRUCT_PACKED __attribute__ ((packed))
  339. #endif