1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3
4 #define _OSDEP_SERVICE_C_
5
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/rtw_ioctl_set.h"
9
10 /*
11 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
12 * @return: one of RTW_STATUS_CODE
13 */
RTW_STATUS_CODE(int error_code)14 inline int RTW_STATUS_CODE(int error_code)
15 {
16 if (error_code >= 0)
17 return _SUCCESS;
18 return _FAIL;
19 }
20
rtw_malloc2d(int h,int w,int size)21 void *rtw_malloc2d(int h, int w, int size)
22 {
23 int j;
24
25 void **a = kzalloc(h * sizeof(void *) + h * w * size, GFP_KERNEL);
26 if (!a)
27 return NULL;
28
29 for (j = 0; j < h; j++)
30 a[j] = ((char *)(a + h)) + j * w * size;
31
32 return a;
33 }
34
35 /*
36 For the following list_xxx operations,
37 caller must guarantee the atomic context.
38 Otherwise, there will be racing condition.
39 */
40 /*
41 Caller must check if the list is empty before calling rtw_list_delete
42 */
43
44 static const struct device_type wlan_type = {
45 .name = "wlan",
46 };
47
rtw_alloc_etherdev_with_old_priv(int sizeof_priv,void * old_priv)48 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
49 void *old_priv)
50 {
51 struct net_device *pnetdev;
52 struct rtw_netdev_priv_indicator *pnpi;
53
54 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
55 if (!pnetdev)
56 return NULL;
57
58 pnetdev->dev.type = &wlan_type;
59 pnpi = netdev_priv(pnetdev);
60 pnpi->priv = old_priv;
61 pnpi->sizeof_priv = sizeof_priv;
62
63 return pnetdev;
64 }
65
rtw_alloc_etherdev(int sizeof_priv)66 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
67 {
68 struct net_device *pnetdev;
69 struct rtw_netdev_priv_indicator *pnpi;
70
71 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
72 if (!pnetdev)
73 return NULL;
74
75 pnpi = netdev_priv(pnetdev);
76
77 pnpi->priv = vzalloc(sizeof_priv);
78 if (!pnpi->priv) {
79 free_netdev(pnetdev);
80 return NULL;
81 }
82
83 pnpi->sizeof_priv = sizeof_priv;
84
85 return pnetdev;
86 }
87
rtw_free_netdev(struct net_device * netdev)88 void rtw_free_netdev(struct net_device *netdev)
89 {
90 struct rtw_netdev_priv_indicator *pnpi;
91
92 pnpi = netdev_priv(netdev);
93
94 vfree(pnpi->priv);
95 free_netdev(netdev);
96 }
97
rtw_change_ifname(struct adapter * padapter,const char * ifname)98 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
99 {
100 struct net_device *pnetdev;
101 struct net_device *cur_pnetdev;
102 struct rereg_nd_name_data *rereg_priv;
103 int ret;
104
105 if (!padapter)
106 goto error;
107
108 cur_pnetdev = padapter->pnetdev;
109 rereg_priv = &padapter->rereg_nd_name_priv;
110
111 /* free the old_pnetdev */
112 if (rereg_priv->old_pnetdev) {
113 free_netdev(rereg_priv->old_pnetdev);
114 rereg_priv->old_pnetdev = NULL;
115 }
116
117 if (!rtnl_is_locked())
118 unregister_netdev(cur_pnetdev);
119 else
120 unregister_netdevice(cur_pnetdev);
121
122 rereg_priv->old_pnetdev = cur_pnetdev;
123
124 pnetdev = rtw_init_netdev(padapter);
125 if (!pnetdev) {
126 ret = -1;
127 goto error;
128 }
129
130 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
131
132 rtw_init_netdev_name(pnetdev, ifname);
133
134 eth_hw_addr_set(pnetdev, padapter->eeprompriv.mac_addr);
135
136 if (!rtnl_is_locked())
137 ret = register_netdev(pnetdev);
138 else
139 ret = register_netdevice(pnetdev);
140 if (ret != 0)
141 goto error;
142
143 return 0;
144 error:
145 return -1;
146 }
147
rtw_buf_update(u8 ** buf,u32 * buf_len,u8 * src,u32 src_len)148 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
149 {
150 u32 dup_len = 0;
151 u8 *ori = NULL;
152 u8 *dup = NULL;
153
154 if (!buf || !buf_len)
155 return;
156
157 if (!src || !src_len)
158 goto keep_ori;
159
160 /* duplicate src */
161 dup = kmalloc(src_len, GFP_ATOMIC);
162 if (dup) {
163 dup_len = src_len;
164 memcpy(dup, src, dup_len);
165 }
166
167 keep_ori:
168 ori = *buf;
169
170 /* replace buf with dup */
171 *buf_len = 0;
172 *buf = dup;
173 *buf_len = dup_len;
174
175 /* free ori */
176 kfree(ori);
177 }
178
179 /**
180 * rtw_cbuf_empty - test if cbuf is empty
181 * @cbuf: pointer of struct rtw_cbuf
182 *
183 * Returns: true if cbuf is empty
184 */
rtw_cbuf_empty(struct rtw_cbuf * cbuf)185 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
186 {
187 return cbuf->write == cbuf->read;
188 }
189
190 /**
191 * rtw_cbuf_pop - pop a pointer from cbuf
192 * @cbuf: pointer of struct rtw_cbuf
193 *
194 * Lock free operation, be careful of the use scheme
195 * Returns: pointer popped out
196 */
rtw_cbuf_pop(struct rtw_cbuf * cbuf)197 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
198 {
199 void *buf;
200 if (rtw_cbuf_empty(cbuf))
201 return NULL;
202
203 buf = cbuf->bufs[cbuf->read];
204 cbuf->read = (cbuf->read + 1) % cbuf->size;
205
206 return buf;
207 }
208
209 /**
210 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
211 * @size: size of pointer
212 *
213 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
214 */
rtw_cbuf_alloc(u32 size)215 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
216 {
217 struct rtw_cbuf *cbuf;
218
219 cbuf = kmalloc(struct_size(cbuf, bufs, size), GFP_KERNEL);
220
221 if (cbuf) {
222 cbuf->write = 0;
223 cbuf->read = 0;
224 cbuf->size = size;
225 }
226 return cbuf;
227 }
228