1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
22 #define _OSDEP_SERVICE_C_
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>
31 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
32 * @return: one of RTW_STATUS_CODE
34 inline int RTW_STATUS_CODE(int error_code)
43 int num = 0, flag = 0;
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)
58 inline u8 *_rtw_vmalloc(u32 sz)
65 inline u8 *_rtw_zvmalloc(u32 sz)
68 pbuf = _rtw_vmalloc(sz);
74 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
79 u8 *_rtw_malloc(u32 sz)
83 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
87 u8 *_rtw_zmalloc(u32 sz)
89 u8 *pbuf = _rtw_malloc(sz);
96 void *rtw_malloc2d(int h, int w, int size)
100 void **a = (void **)rtw_zmalloc(h*sizeof(void *) + h*w*size);
102 pr_info("%s: alloc memory fail!\n", __func__);
106 for (j = 0; j < h; j++)
107 a[j] = ((char *)(a+h)) + j*w*size;
112 void rtw_mfree2d(void *pbuf, int h, int w, int size)
117 int _rtw_memcmp(void *dst, void *src, u32 sz)
119 /* under Linux/GNU/GLibc, the return value of memcmp for two same
121 if (!(memcmp(dst, src, sz)))
127 void _rtw_memset(void *pbuf, int c, u32 sz)
132 void _rtw_init_listhead(struct list_head *list)
134 INIT_LIST_HEAD(list);
138 For the following list_xxx operations,
139 caller must guarantee the atomic context.
140 Otherwise, there will be racing condition.
142 u32 rtw_is_list_empty(struct list_head *phead)
144 if (list_empty(phead))
150 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead)
152 list_add(plist, phead);
155 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead)
157 list_add_tail(plist, phead);
161 Caller must check if the list is empty before calling rtw_list_delete
164 void _rtw_init_sema(struct semaphore *sema, int init_val)
166 sema_init(sema, init_val);
169 void _rtw_free_sema(struct semaphore *sema)
173 void _rtw_up_sema(struct semaphore *sema)
178 u32 _rtw_down_sema(struct semaphore *sema)
180 if (down_interruptible(sema))
186 void _rtw_mutex_init(struct mutex *pmutex)
191 void _rtw_mutex_free(struct mutex *pmutex)
193 mutex_destroy(pmutex);
196 void _rtw_spinlock_init(spinlock_t *plock)
198 spin_lock_init(plock);
201 void _rtw_spinlock_free(spinlock_t *plock)
205 void _rtw_init_queue(struct __queue *pqueue)
207 _rtw_init_listhead(&(pqueue->queue));
208 _rtw_spinlock_init(&(pqueue->lock));
211 u32 _rtw_queue_empty(struct __queue *pqueue)
213 return rtw_is_list_empty(&(pqueue->queue));
216 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
224 u32 rtw_get_current_time(void)
229 inline u32 rtw_systime_to_ms(u32 systime)
231 return systime * 1000 / HZ;
234 inline u32 rtw_ms_to_systime(u32 ms)
236 return ms * HZ / 1000;
239 /* the input parameter start use the same unit as returned by
240 * rtw_get_current_time */
241 inline s32 rtw_get_passing_time_ms(u32 start)
243 return rtw_systime_to_ms(jiffies-start);
246 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
248 return rtw_systime_to_ms(end-start);
251 void rtw_sleep_schedulable(int ms)
255 delta = (ms * HZ)/1000;/* ms) */
258 set_current_state(TASK_INTERRUPTIBLE);
259 if (schedule_timeout(delta) != 0)
263 void rtw_msleep_os(int ms)
265 msleep((unsigned int)ms);
268 void rtw_usleep_os(int us)
273 msleep((us/1000) + 1);
276 void rtw_mdelay_os(int ms)
278 mdelay((unsigned long)ms);
281 void rtw_udelay_os(int us)
283 udelay((unsigned long)us);
286 void rtw_yield_os(void)
291 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
293 inline void rtw_suspend_lock_init(void)
297 inline void rtw_suspend_lock_uninit(void)
301 inline void rtw_lock_suspend(void)
305 inline void rtw_unlock_suspend(void)
309 inline void ATOMIC_SET(ATOMIC_T *v, int i)
314 inline int ATOMIC_READ(ATOMIC_T *v)
316 return atomic_read(v);
319 inline void ATOMIC_ADD(ATOMIC_T *v, int i)
324 inline void ATOMIC_SUB(ATOMIC_T *v, int i)
329 inline void ATOMIC_INC(ATOMIC_T *v)
334 inline void ATOMIC_DEC(ATOMIC_T *v)
339 inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
341 return atomic_add_return(i, v);
344 inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
346 return atomic_sub_return(i, v);
349 inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
351 return atomic_inc_return(v);
354 inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
356 return atomic_dec_return(v);
359 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
362 struct net_device *pnetdev;
363 struct rtw_netdev_priv_indicator *pnpi;
365 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
369 pnpi = netdev_priv(pnetdev);
370 pnpi->priv = old_priv;
371 pnpi->sizeof_priv = sizeof_priv;
377 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
379 struct net_device *pnetdev;
380 struct rtw_netdev_priv_indicator *pnpi;
382 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
386 pnpi = netdev_priv(pnetdev);
388 pnpi->priv = rtw_zvmalloc(sizeof_priv);
390 free_netdev(pnetdev);
395 pnpi->sizeof_priv = sizeof_priv;
400 void rtw_free_netdev(struct net_device *netdev)
402 struct rtw_netdev_priv_indicator *pnpi;
407 pnpi = netdev_priv(netdev);
412 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
419 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
421 struct net_device *pnetdev;
422 struct net_device *cur_pnetdev;
423 struct rereg_nd_name_data *rereg_priv;
429 cur_pnetdev = padapter->pnetdev;
430 rereg_priv = &padapter->rereg_nd_name_priv;
432 /* free the old_pnetdev */
433 if (rereg_priv->old_pnetdev) {
434 free_netdev(rereg_priv->old_pnetdev);
435 rereg_priv->old_pnetdev = NULL;
438 if (!rtnl_is_locked())
439 unregister_netdev(cur_pnetdev);
441 unregister_netdevice(cur_pnetdev);
443 rtw_proc_remove_one(cur_pnetdev);
445 rereg_priv->old_pnetdev = cur_pnetdev;
447 pnetdev = rtw_init_netdev(padapter);
453 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
455 rtw_init_netdev_name(pnetdev, ifname);
457 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
459 if (!rtnl_is_locked())
460 ret = register_netdev(pnetdev);
462 ret = register_netdevice(pnetdev);
464 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
465 ("register_netdev() failed\n"));
468 rtw_proc_init_one(pnetdev);
474 u64 rtw_modular64(u64 x, u64 y)
479 u64 rtw_division64(u64 x, u64 y)
485 void rtw_buf_free(u8 **buf, u32 *buf_len)
492 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
494 u32 ori_len = 0, dup_len = 0;
498 if (!buf || !buf_len)
501 if (!src || !src_len)
505 dup = rtw_malloc(src_len);
508 memcpy(dup, src, dup_len);
515 /* replace buf with dup */
526 * rtw_cbuf_full - test if cbuf is full
527 * @cbuf: pointer of struct rtw_cbuf
529 * Returns: true if cbuf is full
531 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
533 return (cbuf->write == cbuf->read-1) ? true : false;
537 * rtw_cbuf_empty - test if cbuf is empty
538 * @cbuf: pointer of struct rtw_cbuf
540 * Returns: true if cbuf is empty
542 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
544 return (cbuf->write == cbuf->read) ? true : false;
548 * rtw_cbuf_push - push a pointer into cbuf
549 * @cbuf: pointer of struct rtw_cbuf
550 * @buf: pointer to push in
552 * Lock free operation, be careful of the use scheme
553 * Returns: true push success
555 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
557 if (rtw_cbuf_full(cbuf))
561 DBG_88E("%s on %u\n", __func__, cbuf->write);
562 cbuf->bufs[cbuf->write] = buf;
563 cbuf->write = (cbuf->write+1)%cbuf->size;
569 * rtw_cbuf_pop - pop a pointer from cbuf
570 * @cbuf: pointer of struct rtw_cbuf
572 * Lock free operation, be careful of the use scheme
573 * Returns: pointer popped out
575 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
578 if (rtw_cbuf_empty(cbuf))
582 DBG_88E("%s on %u\n", __func__, cbuf->read);
583 buf = cbuf->bufs[cbuf->read];
584 cbuf->read = (cbuf->read+1)%cbuf->size;
590 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
591 * @size: size of pointer
593 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
595 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
597 struct rtw_cbuf *cbuf;
599 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
600 sizeof(void *)*size);