osdep_service_linux.h 11 KB

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