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