]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
93c76d7e81810b941fdbc7abe6e1790991089122
[karo-tx-linux.git] / drivers / staging / rtl8188eu / os_dep / osdep_service.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 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
21
22 #define _OSDEP_SERVICE_C_
23
24 #include <osdep_service.h>
25 #include <osdep_intf.h>
26 #include <drv_types.h>
27 #include <recv_osdep.h>
28 #include <linux/vmalloc.h>
29 #include <rtw_ioctl_set.h>
30
31 /*
32 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
33 * @return: one of RTW_STATUS_CODE
34 */
35 inline int RTW_STATUS_CODE(int error_code)
36 {
37         if (error_code >= 0)
38                 return _SUCCESS;
39         return _FAIL;
40 }
41
42 u8 *_rtw_malloc(u32 sz)
43 {
44         u8      *pbuf = NULL;
45
46         pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
47         return pbuf;
48 }
49
50 void *rtw_malloc2d(int h, int w, int size)
51 {
52         int j;
53
54         void **a = (void **)kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
55         if (a == NULL) {
56                 pr_info("%s: alloc memory fail!\n", __func__);
57                 return NULL;
58         }
59
60         for (j = 0; j < h; j++)
61                 a[j] = ((char *)(a+h)) + j*w*size;
62
63         return a;
64 }
65
66 u32 _rtw_down_sema(struct semaphore *sema)
67 {
68         if (down_interruptible(sema))
69                 return _FAIL;
70         else
71                 return _SUCCESS;
72 }
73
74 void    _rtw_init_queue(struct __queue *pqueue)
75 {
76         INIT_LIST_HEAD(&(pqueue->queue));
77         spin_lock_init(&(pqueue->lock));
78 }
79
80 inline u32 rtw_systime_to_ms(u32 systime)
81 {
82         return systime * 1000 / HZ;
83 }
84
85 inline u32 rtw_ms_to_systime(u32 ms)
86 {
87         return ms * HZ / 1000;
88 }
89
90 /*  the input parameter start must be in jiffies */
91 inline s32 rtw_get_passing_time_ms(u32 start)
92 {
93         return rtw_systime_to_ms(jiffies-start);
94 }
95
96 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
97                                                     void *old_priv)
98 {
99         struct net_device *pnetdev;
100         struct rtw_netdev_priv_indicator *pnpi;
101
102         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
103         if (!pnetdev)
104                 goto RETURN;
105
106         pnpi = netdev_priv(pnetdev);
107         pnpi->priv = old_priv;
108         pnpi->sizeof_priv = sizeof_priv;
109
110 RETURN:
111         return pnetdev;
112 }
113
114 void rtw_free_netdev(struct net_device *netdev)
115 {
116         struct rtw_netdev_priv_indicator *pnpi;
117
118         if (!netdev)
119                 goto RETURN;
120
121         pnpi = netdev_priv(netdev);
122
123         if (!pnpi->priv)
124                 goto RETURN;
125
126         vfree(pnpi->priv);
127         free_netdev(netdev);
128
129 RETURN:
130         return;
131 }
132
133 u64 rtw_modular64(u64 x, u64 y)
134 {
135         return do_div(x, y);
136 }
137
138 void rtw_buf_free(u8 **buf, u32 *buf_len)
139 {
140         *buf_len = 0;
141         kfree(*buf);
142         *buf = NULL;
143 }
144
145 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
146 {
147         u32 ori_len = 0, dup_len = 0;
148         u8 *ori = NULL;
149         u8 *dup = NULL;
150
151         if (!buf || !buf_len)
152                 return;
153
154         if (!src || !src_len)
155                 goto keep_ori;
156
157         /* duplicate src */
158         dup = rtw_malloc(src_len);
159         if (dup) {
160                 dup_len = src_len;
161                 memcpy(dup, src, dup_len);
162         }
163
164 keep_ori:
165         ori = *buf;
166         ori_len = *buf_len;
167
168         /* replace buf with dup */
169         *buf_len = 0;
170         *buf = dup;
171         *buf_len = dup_len;
172
173         /* free ori */
174         kfree(ori);
175 }
176
177
178 /**
179  * rtw_cbuf_full - test if cbuf is full
180  * @cbuf: pointer of struct rtw_cbuf
181  *
182  * Returns: true if cbuf is full
183  */
184 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
185 {
186         return (cbuf->write == cbuf->read-1) ? true : false;
187 }
188
189 /**
190  * rtw_cbuf_empty - test if cbuf is empty
191  * @cbuf: pointer of struct rtw_cbuf
192  *
193  * Returns: true if cbuf is empty
194  */
195 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
196 {
197         return (cbuf->write == cbuf->read) ? true : false;
198 }
199
200 /**
201  * rtw_cbuf_push - push a pointer into cbuf
202  * @cbuf: pointer of struct rtw_cbuf
203  * @buf: pointer to push in
204  *
205  * Lock free operation, be careful of the use scheme
206  * Returns: true push success
207  */
208 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
209 {
210         if (rtw_cbuf_full(cbuf))
211                 return _FAIL;
212
213         if (0)
214                 DBG_88E("%s on %u\n", __func__, cbuf->write);
215         cbuf->bufs[cbuf->write] = buf;
216         cbuf->write = (cbuf->write+1)%cbuf->size;
217
218         return _SUCCESS;
219 }
220
221 /**
222  * rtw_cbuf_pop - pop a pointer from cbuf
223  * @cbuf: pointer of struct rtw_cbuf
224  *
225  * Lock free operation, be careful of the use scheme
226  * Returns: pointer popped out
227  */
228 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
229 {
230         void *buf;
231         if (rtw_cbuf_empty(cbuf))
232                 return NULL;
233
234         if (0)
235                 DBG_88E("%s on %u\n", __func__, cbuf->read);
236         buf = cbuf->bufs[cbuf->read];
237         cbuf->read = (cbuf->read+1)%cbuf->size;
238
239         return buf;
240 }
241
242 /**
243  * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
244  * @size: size of pointer
245  *
246  * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
247  */
248 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
249 {
250         struct rtw_cbuf *cbuf;
251
252         cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
253                sizeof(void *)*size);
254
255         if (cbuf) {
256                 cbuf->write = 0;
257                 cbuf->read = 0;
258                 cbuf->size = size;
259         }
260         return cbuf;
261 }