1 /******************************************************************************
2 *
3 * Copyright(c) 2007 - 2010 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 * Modifications for inclusion into the Linux staging tree are
19 * Copyright(c) 2010 Larry Finger. All rights reserved.
20 *
21 * Contact information:
22 * WLAN FAE <wlanfae@realtek.com>
23 * Larry Finger <Larry.Finger@lwfinger.net>
24 *
25 ******************************************************************************/
26 #ifndef __OSDEP_SERVICE_H_
27 #define __OSDEP_SERVICE_H_
28
29 #define _SUCCESS 1
30 #define _FAIL 0
31
32 #include <linux/version.h>
33 #include <linux/spinlock.h>
34
35 #include <linux/interrupt.h>
36 #include <linux/semaphore.h>
37 #include <linux/sched.h>
38 #include <linux/sem.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <net/iw_handler.h>
42 #include <linux/proc_fs.h> /* Necessary because we use the proc fs */
43
44 #include "basic_types.h"
45
46 struct __queue {
47 struct list_head queue;
48 spinlock_t lock;
49 };
50
51 #define _pkt struct sk_buff
52 #define _buffer unsigned char
53 #define thread_exit() complete_and_exit(NULL, 0)
54 #define _workitem struct work_struct
55
56 #define _init_queue(pqueue) \
57 do { \
58 _init_listhead(&((pqueue)->queue)); \
59 spin_lock_init(&((pqueue)->lock)); \
60 } while (0)
61
get_next(struct list_head * list)62 static inline struct list_head *get_next(struct list_head *list)
63 {
64 return list->next;
65 }
66
get_list_head(struct __queue * queue)67 static inline struct list_head *get_list_head(struct __queue *queue)
68 {
69 return &(queue->queue);
70 }
71
72 #define LIST_CONTAINOR(ptr, type, member) \
73 ((type *)((char *)(ptr)-(SIZE_T)(&((type *)0)->member)))
74
_enter_hwio_critical(struct semaphore * prwlock,unsigned long * pirqL)75 static inline void _enter_hwio_critical(struct semaphore *prwlock,
76 unsigned long *pirqL)
77 {
78 down(prwlock);
79 }
80
_exit_hwio_critical(struct semaphore * prwlock,unsigned long * pirqL)81 static inline void _exit_hwio_critical(struct semaphore *prwlock,
82 unsigned long *pirqL)
83 {
84 up(prwlock);
85 }
86
list_delete(struct list_head * plist)87 static inline void list_delete(struct list_head *plist)
88 {
89 list_del_init(plist);
90 }
91
_init_timer(struct timer_list * ptimer,struct net_device * padapter,void * pfunc,void * cntx)92 static inline void _init_timer(struct timer_list *ptimer,
93 struct net_device *padapter,
94 void *pfunc, void *cntx)
95 {
96 ptimer->function = pfunc;
97 ptimer->data = (addr_t)cntx;
98 init_timer(ptimer);
99 }
100
_set_timer(struct timer_list * ptimer,u32 delay_time)101 static inline void _set_timer(struct timer_list *ptimer, u32 delay_time)
102 {
103 mod_timer(ptimer, (jiffies+(delay_time*HZ/1000)));
104 }
105
_cancel_timer(struct timer_list * ptimer,u8 * bcancelled)106 static inline void _cancel_timer(struct timer_list *ptimer, u8 *bcancelled)
107 {
108 del_timer(ptimer);
109 *bcancelled = true; /*true ==1; false==0*/
110 }
111
_init_workitem(_workitem * pwork,void * pfunc,void * cntx)112 static inline void _init_workitem(_workitem *pwork, void *pfunc, void *cntx)
113 {
114 INIT_WORK(pwork, pfunc);
115 }
116
_set_workitem(_workitem * pwork)117 static inline void _set_workitem(_workitem *pwork)
118 {
119 schedule_work(pwork);
120 }
121
122 #include "rtl871x_byteorder.h"
123
124 #ifndef BIT
125 #define BIT(x) (1 << (x))
126 #endif
127
128 /*
129 For the following list_xxx operations,
130 caller must guarantee the atomic context.
131 Otherwise, there will be racing condition.
132 */
is_list_empty(struct list_head * phead)133 static inline u32 is_list_empty(struct list_head *phead)
134 {
135 if (list_empty(phead))
136 return true;
137 else
138 return false;
139 }
140
list_insert_tail(struct list_head * plist,struct list_head * phead)141 static inline void list_insert_tail(struct list_head *plist,
142 struct list_head *phead)
143 {
144 list_add_tail(plist, phead);
145 }
146
_down_sema(struct semaphore * sema)147 static inline u32 _down_sema(struct semaphore *sema)
148 {
149 if (down_interruptible(sema))
150 return _FAIL;
151 else
152 return _SUCCESS;
153 }
154
_rtl_rwlock_init(struct semaphore * prwlock)155 static inline void _rtl_rwlock_init(struct semaphore *prwlock)
156 {
157 sema_init(prwlock, 1);
158 }
159
_init_listhead(struct list_head * list)160 static inline void _init_listhead(struct list_head *list)
161 {
162 INIT_LIST_HEAD(list);
163 }
164
_queue_empty(struct __queue * pqueue)165 static inline u32 _queue_empty(struct __queue *pqueue)
166 {
167 return is_list_empty(&(pqueue->queue));
168 }
169
end_of_queue_search(struct list_head * head,struct list_head * plist)170 static inline u32 end_of_queue_search(struct list_head *head, struct list_head *plist)
171 {
172 if (head == plist)
173 return true;
174 else
175 return false;
176 }
177
sleep_schedulable(int ms)178 static inline void sleep_schedulable(int ms)
179 {
180 u32 delta;
181
182 delta = (ms * HZ) / 1000;/*(ms)*/
183 if (delta == 0)
184 delta = 1;/* 1 ms */
185 set_current_state(TASK_INTERRUPTIBLE);
186 if (schedule_timeout(delta) != 0)
187 return ;
188 }
189
_malloc(u32 sz)190 static inline u8 *_malloc(u32 sz)
191 {
192 return kmalloc(sz, GFP_ATOMIC);
193 }
194
_cancel_timer_ex(struct timer_list * ptimer)195 static inline unsigned char _cancel_timer_ex(struct timer_list *ptimer)
196 {
197 return del_timer(ptimer);
198 }
199
thread_enter(void * context)200 static inline void thread_enter(void *context)
201 {
202 allow_signal(SIGTERM);
203 }
204
flush_signals_thread(void)205 static inline void flush_signals_thread(void)
206 {
207 if (signal_pending(current))
208 flush_signals(current);
209 }
210
_RND8(u32 sz)211 static inline u32 _RND8(u32 sz)
212 {
213 return ((sz >> 3) + ((sz & 7) ? 1 : 0)) << 3;
214 }
215
_RND128(u32 sz)216 static inline u32 _RND128(u32 sz)
217 {
218 return ((sz >> 7) + ((sz & 127) ? 1 : 0)) << 7;
219 }
220
_RND256(u32 sz)221 static inline u32 _RND256(u32 sz)
222 {
223 return ((sz >> 8) + ((sz & 255) ? 1 : 0)) << 8;
224 }
225
_RND512(u32 sz)226 static inline u32 _RND512(u32 sz)
227 {
228 return ((sz >> 9) + ((sz & 511) ? 1 : 0)) << 9;
229 }
230
231 #endif
232
233