]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
Merge commit 'v3.14' into next
[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 <drv_types.h>
26 #include <recv_osdep.h>
27 #include <linux/vmalloc.h>
28 #include <rtw_ioctl_set.h>
29
30 /*
31 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
32 * @return: one of RTW_STATUS_CODE
33 */
34 inline int RTW_STATUS_CODE(int error_code)
35 {
36         if (error_code >= 0)
37                 return _SUCCESS;
38         return _FAIL;
39 }
40
41 u32 rtw_atoi(u8 *s)
42 {
43         int num = 0, flag = 0;
44         int i;
45         for (i = 0; i <= strlen(s); i++) {
46                 if (s[i] >= '0' && s[i] <= '9')
47                         num = num * 10 + s[i] - '0';
48                 else if (s[0] == '-' && i == 0)
49                         flag = 1;
50                 else
51                         break;
52         }
53         if (flag == 1)
54                 num = num * -1;
55         return num;
56 }
57
58 inline u8 *_rtw_vmalloc(u32 sz)
59 {
60         u8      *pbuf;
61         pbuf = vmalloc(sz);
62         return pbuf;
63 }
64
65 inline u8 *_rtw_zvmalloc(u32 sz)
66 {
67         u8      *pbuf;
68         pbuf = _rtw_vmalloc(sz);
69         if (pbuf != NULL)
70                 memset(pbuf, 0, sz);
71         return pbuf;
72 }
73
74 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
75 {
76         vfree(pbuf);
77 }
78
79 u8 *_rtw_malloc(u32 sz)
80 {
81         u8      *pbuf = NULL;
82
83         pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
84         return pbuf;
85 }
86
87 u8 *_rtw_zmalloc(u32 sz)
88 {
89         u8      *pbuf = _rtw_malloc(sz);
90
91         if (pbuf != NULL)
92                 memset(pbuf, 0, sz);
93         return pbuf;
94 }
95
96 void *rtw_malloc2d(int h, int w, int size)
97 {
98         int j;
99
100         void **a = (void **)rtw_zmalloc(h*sizeof(void *) + h*w*size);
101         if (a == NULL) {
102                 pr_info("%s: alloc memory fail!\n", __func__);
103                 return NULL;
104         }
105
106         for (j = 0; j < h; j++)
107                 a[j] = ((char *)(a+h)) + j*w*size;
108
109         return a;
110 }
111
112 void rtw_mfree2d(void *pbuf, int h, int w, int size)
113 {
114         kfree(pbuf);
115 }
116
117 int _rtw_memcmp(void *dst, void *src, u32 sz)
118 {
119 /* under Linux/GNU/GLibc, the return value of memcmp for two same
120  * mem. chunk is 0 */
121         if (!(memcmp(dst, src, sz)))
122                 return true;
123         else
124                 return false;
125 }
126
127 void _rtw_memset(void *pbuf, int c, u32 sz)
128 {
129         memset(pbuf, c, sz);
130 }
131
132 void _rtw_init_listhead(struct list_head *list)
133 {
134         INIT_LIST_HEAD(list);
135 }
136
137 /*
138 For the following list_xxx operations,
139 caller must guarantee the atomic context.
140 Otherwise, there will be racing condition.
141 */
142 u32     rtw_is_list_empty(struct list_head *phead)
143 {
144         if (list_empty(phead))
145                 return true;
146         else
147                 return false;
148 }
149
150 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead)
151 {
152         list_add(plist, phead);
153 }
154
155 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead)
156 {
157         list_add_tail(plist, phead);
158 }
159
160 /*
161 Caller must check if the list is empty before calling rtw_list_delete
162 */
163
164 u32 _rtw_down_sema(struct semaphore *sema)
165 {
166         if (down_interruptible(sema))
167                 return _FAIL;
168         else
169                 return _SUCCESS;
170 }
171
172 void    _rtw_init_queue(struct __queue *pqueue)
173 {
174         _rtw_init_listhead(&(pqueue->queue));
175         spin_lock_init(&(pqueue->lock));
176 }
177
178 u32       _rtw_queue_empty(struct __queue *pqueue)
179 {
180         return rtw_is_list_empty(&(pqueue->queue));
181 }
182
183 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
184 {
185         if (head == plist)
186                 return true;
187         else
188                 return false;
189 }
190
191 inline u32 rtw_systime_to_ms(u32 systime)
192 {
193         return systime * 1000 / HZ;
194 }
195
196 inline u32 rtw_ms_to_systime(u32 ms)
197 {
198         return ms * HZ / 1000;
199 }
200
201 /*  the input parameter start must be in jiffies */
202 inline s32 rtw_get_passing_time_ms(u32 start)
203 {
204         return rtw_systime_to_ms(jiffies-start);
205 }
206
207 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
208 {
209         return rtw_systime_to_ms(end-start);
210 }
211
212 void rtw_sleep_schedulable(int ms)
213 {
214         u32 delta;
215
216         delta = (ms * HZ)/1000;/* ms) */
217         if (delta == 0)
218                 delta = 1;/*  1 ms */
219         set_current_state(TASK_INTERRUPTIBLE);
220         if (schedule_timeout(delta) != 0)
221                 return;
222 }
223
224 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
225
226 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
227                                                     void *old_priv)
228 {
229         struct net_device *pnetdev;
230         struct rtw_netdev_priv_indicator *pnpi;
231
232         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
233         if (!pnetdev)
234                 goto RETURN;
235
236         pnpi = netdev_priv(pnetdev);
237         pnpi->priv = old_priv;
238         pnpi->sizeof_priv = sizeof_priv;
239
240 RETURN:
241         return pnetdev;
242 }
243
244 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
245 {
246         struct net_device *pnetdev;
247         struct rtw_netdev_priv_indicator *pnpi;
248
249         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
250         if (!pnetdev)
251                 goto RETURN;
252
253         pnpi = netdev_priv(pnetdev);
254
255         pnpi->priv = rtw_zvmalloc(sizeof_priv);
256         if (!pnpi->priv) {
257                 free_netdev(pnetdev);
258                 pnetdev = NULL;
259                 goto RETURN;
260         }
261
262         pnpi->sizeof_priv = sizeof_priv;
263 RETURN:
264         return pnetdev;
265 }
266
267 void rtw_free_netdev(struct net_device *netdev)
268 {
269         struct rtw_netdev_priv_indicator *pnpi;
270
271         if (!netdev)
272                 goto RETURN;
273
274         pnpi = netdev_priv(netdev);
275
276         if (!pnpi->priv)
277                 goto RETURN;
278
279         rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
280         free_netdev(netdev);
281
282 RETURN:
283         return;
284 }
285
286 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
287 {
288         struct net_device *pnetdev;
289         struct net_device *cur_pnetdev;
290         struct rereg_nd_name_data *rereg_priv;
291         int ret;
292
293         if (!padapter)
294                 goto error;
295
296         cur_pnetdev = padapter->pnetdev;
297         rereg_priv = &padapter->rereg_nd_name_priv;
298
299         /* free the old_pnetdev */
300         if (rereg_priv->old_pnetdev) {
301                 free_netdev(rereg_priv->old_pnetdev);
302                 rereg_priv->old_pnetdev = NULL;
303         }
304
305         if (!rtnl_is_locked())
306                 unregister_netdev(cur_pnetdev);
307         else
308                 unregister_netdevice(cur_pnetdev);
309
310         rtw_proc_remove_one(cur_pnetdev);
311
312         rereg_priv->old_pnetdev = cur_pnetdev;
313
314         pnetdev = rtw_init_netdev(padapter);
315         if (!pnetdev)  {
316                 ret = -1;
317                 goto error;
318         }
319
320         SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
321
322         rtw_init_netdev_name(pnetdev, ifname);
323
324         memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
325
326         if (!rtnl_is_locked())
327                 ret = register_netdev(pnetdev);
328         else
329                 ret = register_netdevice(pnetdev);
330         if (ret != 0) {
331                 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
332                          ("register_netdev() failed\n"));
333                 goto error;
334         }
335         rtw_proc_init_one(pnetdev);
336         return 0;
337 error:
338         return -1;
339 }
340
341 u64 rtw_modular64(u64 x, u64 y)
342 {
343         return do_div(x, y);
344 }
345
346 u64 rtw_division64(u64 x, u64 y)
347 {
348         do_div(x, y);
349         return x;
350 }
351
352 void rtw_buf_free(u8 **buf, u32 *buf_len)
353 {
354         *buf_len = 0;
355         kfree(*buf);
356         *buf = NULL;
357 }
358
359 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
360 {
361         u32 ori_len = 0, dup_len = 0;
362         u8 *ori = NULL;
363         u8 *dup = NULL;
364
365         if (!buf || !buf_len)
366                 return;
367
368         if (!src || !src_len)
369                 goto keep_ori;
370
371         /* duplicate src */
372         dup = rtw_malloc(src_len);
373         if (dup) {
374                 dup_len = src_len;
375                 memcpy(dup, src, dup_len);
376         }
377
378 keep_ori:
379         ori = *buf;
380         ori_len = *buf_len;
381
382         /* replace buf with dup */
383         *buf_len = 0;
384         *buf = dup;
385         *buf_len = dup_len;
386
387         /* free ori */
388         kfree(ori);
389 }
390
391
392 /**
393  * rtw_cbuf_full - test if cbuf is full
394  * @cbuf: pointer of struct rtw_cbuf
395  *
396  * Returns: true if cbuf is full
397  */
398 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
399 {
400         return (cbuf->write == cbuf->read-1) ? true : false;
401 }
402
403 /**
404  * rtw_cbuf_empty - test if cbuf is empty
405  * @cbuf: pointer of struct rtw_cbuf
406  *
407  * Returns: true if cbuf is empty
408  */
409 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
410 {
411         return (cbuf->write == cbuf->read) ? true : false;
412 }
413
414 /**
415  * rtw_cbuf_push - push a pointer into cbuf
416  * @cbuf: pointer of struct rtw_cbuf
417  * @buf: pointer to push in
418  *
419  * Lock free operation, be careful of the use scheme
420  * Returns: true push success
421  */
422 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
423 {
424         if (rtw_cbuf_full(cbuf))
425                 return _FAIL;
426
427         if (0)
428                 DBG_88E("%s on %u\n", __func__, cbuf->write);
429         cbuf->bufs[cbuf->write] = buf;
430         cbuf->write = (cbuf->write+1)%cbuf->size;
431
432         return _SUCCESS;
433 }
434
435 /**
436  * rtw_cbuf_pop - pop a pointer from cbuf
437  * @cbuf: pointer of struct rtw_cbuf
438  *
439  * Lock free operation, be careful of the use scheme
440  * Returns: pointer popped out
441  */
442 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
443 {
444         void *buf;
445         if (rtw_cbuf_empty(cbuf))
446                 return NULL;
447
448         if (0)
449                 DBG_88E("%s on %u\n", __func__, cbuf->read);
450         buf = cbuf->bufs[cbuf->read];
451         cbuf->read = (cbuf->read+1)%cbuf->size;
452
453         return buf;
454 }
455
456 /**
457  * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
458  * @size: size of pointer
459  *
460  * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
461  */
462 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
463 {
464         struct rtw_cbuf *cbuf;
465
466         cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
467                sizeof(void *)*size);
468
469         if (cbuf) {
470                 cbuf->write = 0;
471                 cbuf->read = 0;
472                 cbuf->size = size;
473         }
474         return cbuf;
475 }