6ff836f481daca9f935bb2903bdbcff05158378f
[linux-2.6-microblaze.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  ******************************************************************************/
15 #define _OSDEP_SERVICE_C_
16
17 #include <osdep_service.h>
18 #include <osdep_intf.h>
19 #include <drv_types.h>
20 #include <recv_osdep.h>
21 #include <linux/vmalloc.h>
22 #include <rtw_ioctl_set.h>
23
24 /*
25  * Translate the OS dependent @param error_code to OS independent
26  * RTW_STATUS_CODE
27  * @return: one of RTW_STATUS_CODE
28  */
29 inline int RTW_STATUS_CODE(int error_code)
30 {
31         if (error_code >= 0)
32                 return _SUCCESS;
33         return _FAIL;
34 }
35
36 u8 *_rtw_malloc(u32 sz)
37 {
38         return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
39 }
40
41 void *rtw_malloc2d(int h, int w, int size)
42 {
43         int j;
44
45         void **a = kzalloc(h * sizeof(void *) + h * w * size, GFP_KERNEL);
46         if (!a)
47                 goto out;
48
49         for (j = 0; j < h; j++)
50                 a[j] = ((char *)(a + h)) + j * w * size;
51 out:
52         return a;
53 }
54
55 void _rtw_init_queue(struct __queue *pqueue)
56 {
57         INIT_LIST_HEAD(&pqueue->queue);
58         spin_lock_init(&pqueue->lock);
59 }
60
61 struct net_device *rtw_alloc_etherdev_with_old_priv(void *old_priv)
62 {
63         struct net_device *pnetdev;
64         struct rtw_netdev_priv_indicator *pnpi;
65
66         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
67         if (!pnetdev)
68                 goto RETURN;
69
70         pnpi = netdev_priv(pnetdev);
71         pnpi->priv = old_priv;
72
73 RETURN:
74         return pnetdev;
75 }
76
77 void rtw_free_netdev(struct net_device *netdev)
78 {
79         struct rtw_netdev_priv_indicator *pnpi;
80
81         if (!netdev)
82                 goto RETURN;
83
84         pnpi = netdev_priv(netdev);
85
86         if (!pnpi->priv)
87                 goto RETURN;
88
89         vfree(pnpi->priv);
90         free_netdev(netdev);
91
92 RETURN:
93         return;
94 }
95
96 u64 rtw_modular64(u64 x, u64 y)
97 {
98         return do_div(x, y);
99 }
100
101 void rtw_buf_free(u8 **buf, u32 *buf_len)
102 {
103         *buf_len = 0;
104         kfree(*buf);
105         *buf = NULL;
106 }
107
108 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
109 {
110         u32 dup_len = 0;
111         u8 *ori = NULL;
112         u8 *dup = NULL;
113
114         if (!buf || !buf_len)
115                 return;
116
117         if (!src || !src_len)
118                 goto keep_ori;
119
120         /* duplicate src */
121         dup = rtw_malloc(src_len);
122         if (dup) {
123                 dup_len = src_len;
124                 memcpy(dup, src, dup_len);
125         }
126
127 keep_ori:
128         ori = *buf;
129
130         /* replace buf with dup */
131         *buf_len = 0;
132         *buf = dup;
133         *buf_len = dup_len;
134
135         /* free ori */
136         kfree(ori);
137 }