1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
4 #define _OSDEP_SERVICE_C_
6 #include <osdep_service.h>
8 #include <recv_osdep.h>
9 #include <linux/vmalloc.h>
10 #include <rtw_ioctl_set.h>
13 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
14 * @return: one of RTW_STATUS_CODE
16 inline int RTW_STATUS_CODE(int error_code)
25 int num = 0, flag = 0;
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)
40 inline u8 *_rtw_vmalloc(u32 sz)
47 inline u8 *_rtw_zvmalloc(u32 sz)
50 pbuf = _rtw_vmalloc(sz);
56 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
61 u8 *_rtw_malloc(u32 sz)
65 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
69 u8 *_rtw_zmalloc(u32 sz)
71 u8 *pbuf = _rtw_malloc(sz);
78 void *rtw_malloc2d(int h, int w, int size)
82 void **a = (void **)rtw_zmalloc(h*sizeof(void *) + h*w*size);
84 pr_info("%s: alloc memory fail!\n", __func__);
88 for (j = 0; j < h; j++)
89 a[j] = ((char *)(a+h)) + j*w*size;
95 For the following list_xxx operations,
96 caller must guarantee the atomic context.
97 Otherwise, there will be racing condition.
100 Caller must check if the list is empty before calling rtw_list_delete
103 u32 _rtw_down_sema(struct semaphore *sema)
105 if (down_interruptible(sema))
111 void _rtw_mutex_init(struct mutex *pmutex)
116 void _rtw_mutex_free(struct mutex *pmutex)
118 mutex_destroy(pmutex);
121 void _rtw_spinlock_free(spinlock_t *plock)
125 void _rtw_init_queue(struct __queue *pqueue)
127 INIT_LIST_HEAD(&(pqueue->queue));
128 spin_lock_init(&(pqueue->lock));
131 inline u32 rtw_systime_to_ms(u32 systime)
133 return systime * 1000 / HZ;
136 inline u32 rtw_ms_to_systime(u32 ms)
138 return ms * HZ / 1000;
141 /* the input parameter start use the same unit as jiffies */
142 inline s32 rtw_get_passing_time_ms(u32 start)
144 return rtw_systime_to_ms(jiffies-start);
147 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
149 return rtw_systime_to_ms(end-start);
152 void rtw_sleep_schedulable(int ms)
156 delta = (ms * HZ)/1000;/* ms) */
159 set_current_state(TASK_INTERRUPTIBLE);
160 if (schedule_timeout(delta) != 0)
164 void rtw_msleep_os(int ms)
166 msleep((unsigned int)ms);
169 void rtw_usleep_os(int us)
174 msleep((us/1000) + 1);
177 void rtw_mdelay_os(int ms)
179 mdelay((unsigned long)ms);
182 void rtw_udelay_os(int us)
184 udelay((unsigned long)us);
187 void rtw_yield_os(void)
192 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
194 inline void rtw_suspend_lock_init(void)
198 inline void rtw_suspend_lock_uninit(void)
202 inline void rtw_lock_suspend(void)
206 inline void rtw_unlock_suspend(void)
210 inline void ATOMIC_SET(ATOMIC_T *v, int i)
215 inline int ATOMIC_READ(ATOMIC_T *v)
217 return atomic_read(v);
220 inline void ATOMIC_ADD(ATOMIC_T *v, int i)
225 inline void ATOMIC_SUB(ATOMIC_T *v, int i)
230 inline void ATOMIC_INC(ATOMIC_T *v)
235 inline void ATOMIC_DEC(ATOMIC_T *v)
240 inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
242 return atomic_add_return(i, v);
245 inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
247 return atomic_sub_return(i, v);
250 inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
252 return atomic_inc_return(v);
255 inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
257 return atomic_dec_return(v);
260 static const struct device_type wlan_type = {
264 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
267 struct net_device *pnetdev;
268 struct rtw_netdev_priv_indicator *pnpi;
270 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
274 pnetdev->dev.type = &wlan_type;
275 pnpi = netdev_priv(pnetdev);
276 pnpi->priv = old_priv;
277 pnpi->sizeof_priv = sizeof_priv;
283 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
285 struct net_device *pnetdev;
286 struct rtw_netdev_priv_indicator *pnpi;
288 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
292 pnpi = netdev_priv(pnetdev);
294 pnpi->priv = rtw_zvmalloc(sizeof_priv);
296 free_netdev(pnetdev);
301 pnpi->sizeof_priv = sizeof_priv;
306 void rtw_free_netdev(struct net_device *netdev)
308 struct rtw_netdev_priv_indicator *pnpi;
313 pnpi = netdev_priv(netdev);
318 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
325 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
327 struct net_device *pnetdev;
328 struct net_device *cur_pnetdev;
329 struct rereg_nd_name_data *rereg_priv;
335 cur_pnetdev = padapter->pnetdev;
336 rereg_priv = &padapter->rereg_nd_name_priv;
338 /* free the old_pnetdev */
339 if (rereg_priv->old_pnetdev) {
340 free_netdev(rereg_priv->old_pnetdev);
341 rereg_priv->old_pnetdev = NULL;
344 if (!rtnl_is_locked())
345 unregister_netdev(cur_pnetdev);
347 unregister_netdevice(cur_pnetdev);
349 rtw_proc_remove_one(cur_pnetdev);
351 rereg_priv->old_pnetdev = cur_pnetdev;
353 pnetdev = rtw_init_netdev(padapter);
359 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
361 rtw_init_netdev_name(pnetdev, ifname);
363 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
365 if (!rtnl_is_locked())
366 ret = register_netdev(pnetdev);
368 ret = register_netdevice(pnetdev);
370 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
371 ("register_netdev() failed\n"));
374 rtw_proc_init_one(pnetdev);
380 u64 rtw_modular64(u64 x, u64 y)
385 u64 rtw_division64(u64 x, u64 y)
391 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
393 u32 ori_len = 0, dup_len = 0;
397 if (!buf || !buf_len)
400 if (!src || !src_len)
404 dup = rtw_malloc(src_len);
407 memcpy(dup, src, dup_len);
414 /* replace buf with dup */
424 * rtw_cbuf_full - test if cbuf is full
425 * @cbuf: pointer of struct rtw_cbuf
427 * Returns: true if cbuf is full
429 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
431 return (cbuf->write == cbuf->read-1) ? true : false;
435 * rtw_cbuf_empty - test if cbuf is empty
436 * @cbuf: pointer of struct rtw_cbuf
438 * Returns: true if cbuf is empty
440 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
442 return (cbuf->write == cbuf->read) ? true : false;
446 * rtw_cbuf_push - push a pointer into cbuf
447 * @cbuf: pointer of struct rtw_cbuf
448 * @buf: pointer to push in
450 * Lock free operation, be careful of the use scheme
451 * Returns: true push success
453 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
455 if (rtw_cbuf_full(cbuf))
459 DBG_88E("%s on %u\n", __func__, cbuf->write);
460 cbuf->bufs[cbuf->write] = buf;
461 cbuf->write = (cbuf->write+1)%cbuf->size;
467 * rtw_cbuf_pop - pop a pointer from cbuf
468 * @cbuf: pointer of struct rtw_cbuf
470 * Lock free operation, be careful of the use scheme
471 * Returns: pointer popped out
473 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
476 if (rtw_cbuf_empty(cbuf))
480 DBG_88E("%s on %u\n", __func__, cbuf->read);
481 buf = cbuf->bufs[cbuf->read];
482 cbuf->read = (cbuf->read+1)%cbuf->size;
488 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
489 * @size: size of pointer
491 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
493 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
495 struct rtw_cbuf *cbuf;
497 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
498 sizeof(void *)*size);