osdep_service_linux.h 10 KB

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