staging: rtl8188eu:Remove rtw_zmalloc(), wrapper for kzalloc()
[deliverable/linux.git] / drivers / staging / rtl8188eu / os_dep / osdep_service.c
CommitLineData
a2c60d42
LF
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>
0a95a7f4 25#include <osdep_intf.h>
a2c60d42
LF
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*/
35inline int RTW_STATUS_CODE(int error_code)
36{
37 if (error_code >= 0)
38 return _SUCCESS;
39 return _FAIL;
40}
41
a2c60d42
LF
42u8 *_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
a2c60d42
LF
50void *rtw_malloc2d(int h, int w, int size)
51{
52 int j;
53
fadbe0cd 54 void **a = (void **)kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
a2c60d42
LF
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
a2c60d42
LF
66u32 _rtw_down_sema(struct semaphore *sema)
67{
68 if (down_interruptible(sema))
69 return _FAIL;
70 else
71 return _SUCCESS;
72}
73
a2c60d42
LF
74void _rtw_init_queue(struct __queue *pqueue)
75{
aa3f5ccb 76 INIT_LIST_HEAD(&(pqueue->queue));
f214e521 77 spin_lock_init(&(pqueue->lock));
a2c60d42
LF
78}
79
80u32 _rtw_queue_empty(struct __queue *pqueue)
81{
9c4b0e70 82 return list_empty(&(pqueue->queue));
a2c60d42
LF
83}
84
a2c60d42
LF
85inline u32 rtw_systime_to_ms(u32 systime)
86{
87 return systime * 1000 / HZ;
88}
89
90inline u32 rtw_ms_to_systime(u32 ms)
91{
92 return ms * HZ / 1000;
93}
94
c01fb496 95/* the input parameter start must be in jiffies */
a2c60d42
LF
96inline s32 rtw_get_passing_time_ms(u32 start)
97{
98 return rtw_systime_to_ms(jiffies-start);
99}
100
101inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
102{
103 return rtw_systime_to_ms(end-start);
104}
105
a2c60d42
LF
106#define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
107
a2c60d42
LF
108struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
109 void *old_priv)
110{
111 struct net_device *pnetdev;
112 struct rtw_netdev_priv_indicator *pnpi;
113
114 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
115 if (!pnetdev)
116 goto RETURN;
117
118 pnpi = netdev_priv(pnetdev);
119 pnpi->priv = old_priv;
120 pnpi->sizeof_priv = sizeof_priv;
121
122RETURN:
123 return pnetdev;
124}
125
126struct net_device *rtw_alloc_etherdev(int sizeof_priv)
127{
128 struct net_device *pnetdev;
129 struct rtw_netdev_priv_indicator *pnpi;
130
131 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
132 if (!pnetdev)
133 goto RETURN;
134
135 pnpi = netdev_priv(pnetdev);
136
2397c6e0 137 pnpi->priv = vzalloc(sizeof_priv);
a2c60d42
LF
138 if (!pnpi->priv) {
139 free_netdev(pnetdev);
140 pnetdev = NULL;
141 goto RETURN;
142 }
143
144 pnpi->sizeof_priv = sizeof_priv;
145RETURN:
146 return pnetdev;
147}
148
149void rtw_free_netdev(struct net_device *netdev)
150{
151 struct rtw_netdev_priv_indicator *pnpi;
152
153 if (!netdev)
154 goto RETURN;
155
156 pnpi = netdev_priv(netdev);
157
158 if (!pnpi->priv)
159 goto RETURN;
160
03bd6aea 161 vfree(pnpi->priv);
a2c60d42
LF
162 free_netdev(netdev);
163
164RETURN:
165 return;
166}
167
168int rtw_change_ifname(struct adapter *padapter, const char *ifname)
169{
170 struct net_device *pnetdev;
efbff73e 171 struct net_device *cur_pnetdev;
a2c60d42
LF
172 struct rereg_nd_name_data *rereg_priv;
173 int ret;
174
175 if (!padapter)
176 goto error;
177
efbff73e 178 cur_pnetdev = padapter->pnetdev;
a2c60d42
LF
179 rereg_priv = &padapter->rereg_nd_name_priv;
180
181 /* free the old_pnetdev */
182 if (rereg_priv->old_pnetdev) {
183 free_netdev(rereg_priv->old_pnetdev);
184 rereg_priv->old_pnetdev = NULL;
185 }
186
187 if (!rtnl_is_locked())
188 unregister_netdev(cur_pnetdev);
189 else
190 unregister_netdevice(cur_pnetdev);
191
192 rtw_proc_remove_one(cur_pnetdev);
193
194 rereg_priv->old_pnetdev = cur_pnetdev;
195
196 pnetdev = rtw_init_netdev(padapter);
197 if (!pnetdev) {
198 ret = -1;
199 goto error;
200 }
201
202 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
203
204 rtw_init_netdev_name(pnetdev, ifname);
205
206 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
207
208 if (!rtnl_is_locked())
209 ret = register_netdev(pnetdev);
210 else
211 ret = register_netdevice(pnetdev);
212 if (ret != 0) {
213 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
214 ("register_netdev() failed\n"));
215 goto error;
216 }
217 rtw_proc_init_one(pnetdev);
218 return 0;
219error:
220 return -1;
221}
222
223u64 rtw_modular64(u64 x, u64 y)
224{
225 return do_div(x, y);
226}
227
228u64 rtw_division64(u64 x, u64 y)
229{
230 do_div(x, y);
231 return x;
232}
233
234void rtw_buf_free(u8 **buf, u32 *buf_len)
235{
236 *buf_len = 0;
237 kfree(*buf);
238 *buf = NULL;
239}
240
241void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
242{
243 u32 ori_len = 0, dup_len = 0;
244 u8 *ori = NULL;
245 u8 *dup = NULL;
246
247 if (!buf || !buf_len)
248 return;
249
250 if (!src || !src_len)
251 goto keep_ori;
252
253 /* duplicate src */
254 dup = rtw_malloc(src_len);
255 if (dup) {
256 dup_len = src_len;
257 memcpy(dup, src, dup_len);
258 }
259
260keep_ori:
261 ori = *buf;
262 ori_len = *buf_len;
263
264 /* replace buf with dup */
265 *buf_len = 0;
266 *buf = dup;
267 *buf_len = dup_len;
268
269 /* free ori */
270 kfree(ori);
271}
272
273
274/**
275 * rtw_cbuf_full - test if cbuf is full
276 * @cbuf: pointer of struct rtw_cbuf
277 *
278 * Returns: true if cbuf is full
279 */
280inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
281{
282 return (cbuf->write == cbuf->read-1) ? true : false;
283}
284
285/**
286 * rtw_cbuf_empty - test if cbuf is empty
287 * @cbuf: pointer of struct rtw_cbuf
288 *
289 * Returns: true if cbuf is empty
290 */
291inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
292{
293 return (cbuf->write == cbuf->read) ? true : false;
294}
295
296/**
297 * rtw_cbuf_push - push a pointer into cbuf
298 * @cbuf: pointer of struct rtw_cbuf
299 * @buf: pointer to push in
300 *
301 * Lock free operation, be careful of the use scheme
302 * Returns: true push success
303 */
304bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
305{
306 if (rtw_cbuf_full(cbuf))
307 return _FAIL;
308
309 if (0)
310 DBG_88E("%s on %u\n", __func__, cbuf->write);
311 cbuf->bufs[cbuf->write] = buf;
312 cbuf->write = (cbuf->write+1)%cbuf->size;
313
314 return _SUCCESS;
315}
316
317/**
318 * rtw_cbuf_pop - pop a pointer from cbuf
319 * @cbuf: pointer of struct rtw_cbuf
320 *
321 * Lock free operation, be careful of the use scheme
322 * Returns: pointer popped out
323 */
324void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
325{
326 void *buf;
327 if (rtw_cbuf_empty(cbuf))
328 return NULL;
329
330 if (0)
331 DBG_88E("%s on %u\n", __func__, cbuf->read);
332 buf = cbuf->bufs[cbuf->read];
333 cbuf->read = (cbuf->read+1)%cbuf->size;
334
335 return buf;
336}
337
338/**
7efc02ca 339 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
a2c60d42
LF
340 * @size: size of pointer
341 *
342 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
343 */
344struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
345{
346 struct rtw_cbuf *cbuf;
347
348 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
349 sizeof(void *)*size);
350
351 if (cbuf) {
352 cbuf->write = 0;
353 cbuf->read = 0;
354 cbuf->size = size;
355 }
356 return cbuf;
357}
This page took 0.16865 seconds and 5 git commands to generate.