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 u32 _rtw_down_sema(struct semaphore *sema)
166 if (down_interruptible(sema))
172 void _rtw_init_queue(struct __queue *pqueue)
174 _rtw_init_listhead(&(pqueue->queue));
175 spin_lock_init(&(pqueue->lock));
178 u32 _rtw_queue_empty(struct __queue *pqueue)
180 return rtw_is_list_empty(&(pqueue->queue));
183 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
191 inline u32 rtw_systime_to_ms(u32 systime)
193 return systime * 1000 / HZ;
196 inline u32 rtw_ms_to_systime(u32 ms)
198 return ms * HZ / 1000;
201 /* the input parameter start must be in jiffies */
202 inline s32 rtw_get_passing_time_ms(u32 start)
204 return rtw_systime_to_ms(jiffies-start);
207 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
209 return rtw_systime_to_ms(end-start);
212 void rtw_sleep_schedulable(int ms)
216 delta = (ms * HZ)/1000;/* ms) */
219 set_current_state(TASK_INTERRUPTIBLE);
220 if (schedule_timeout(delta) != 0)
224 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
226 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
229 struct net_device *pnetdev;
230 struct rtw_netdev_priv_indicator *pnpi;
232 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
236 pnpi = netdev_priv(pnetdev);
237 pnpi->priv = old_priv;
238 pnpi->sizeof_priv = sizeof_priv;
244 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
246 struct net_device *pnetdev;
247 struct rtw_netdev_priv_indicator *pnpi;
249 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
253 pnpi = netdev_priv(pnetdev);
255 pnpi->priv = rtw_zvmalloc(sizeof_priv);
257 free_netdev(pnetdev);
262 pnpi->sizeof_priv = sizeof_priv;
267 void rtw_free_netdev(struct net_device *netdev)
269 struct rtw_netdev_priv_indicator *pnpi;
274 pnpi = netdev_priv(netdev);
279 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
286 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
288 struct net_device *pnetdev;
289 struct net_device *cur_pnetdev;
290 struct rereg_nd_name_data *rereg_priv;
296 cur_pnetdev = padapter->pnetdev;
297 rereg_priv = &padapter->rereg_nd_name_priv;
299 /* free the old_pnetdev */
300 if (rereg_priv->old_pnetdev) {
301 free_netdev(rereg_priv->old_pnetdev);
302 rereg_priv->old_pnetdev = NULL;
305 if (!rtnl_is_locked())
306 unregister_netdev(cur_pnetdev);
308 unregister_netdevice(cur_pnetdev);
310 rtw_proc_remove_one(cur_pnetdev);
312 rereg_priv->old_pnetdev = cur_pnetdev;
314 pnetdev = rtw_init_netdev(padapter);
320 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
322 rtw_init_netdev_name(pnetdev, ifname);
324 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
326 if (!rtnl_is_locked())
327 ret = register_netdev(pnetdev);
329 ret = register_netdevice(pnetdev);
331 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
332 ("register_netdev() failed\n"));
335 rtw_proc_init_one(pnetdev);
341 u64 rtw_modular64(u64 x, u64 y)
346 u64 rtw_division64(u64 x, u64 y)
352 void rtw_buf_free(u8 **buf, u32 *buf_len)
359 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
361 u32 ori_len = 0, dup_len = 0;
365 if (!buf || !buf_len)
368 if (!src || !src_len)
372 dup = rtw_malloc(src_len);
375 memcpy(dup, src, dup_len);
382 /* replace buf with dup */
393 * rtw_cbuf_full - test if cbuf is full
394 * @cbuf: pointer of struct rtw_cbuf
396 * Returns: true if cbuf is full
398 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
400 return (cbuf->write == cbuf->read-1) ? true : false;
404 * rtw_cbuf_empty - test if cbuf is empty
405 * @cbuf: pointer of struct rtw_cbuf
407 * Returns: true if cbuf is empty
409 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
411 return (cbuf->write == cbuf->read) ? true : false;
415 * rtw_cbuf_push - push a pointer into cbuf
416 * @cbuf: pointer of struct rtw_cbuf
417 * @buf: pointer to push in
419 * Lock free operation, be careful of the use scheme
420 * Returns: true push success
422 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
424 if (rtw_cbuf_full(cbuf))
428 DBG_88E("%s on %u\n", __func__, cbuf->write);
429 cbuf->bufs[cbuf->write] = buf;
430 cbuf->write = (cbuf->write+1)%cbuf->size;
436 * rtw_cbuf_pop - pop a pointer from cbuf
437 * @cbuf: pointer of struct rtw_cbuf
439 * Lock free operation, be careful of the use scheme
440 * Returns: pointer popped out
442 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
445 if (rtw_cbuf_empty(cbuf))
449 DBG_88E("%s on %u\n", __func__, cbuf->read);
450 buf = cbuf->bufs[cbuf->read];
451 cbuf->read = (cbuf->read+1)%cbuf->size;
457 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
458 * @size: size of pointer
460 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
462 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
464 struct rtw_cbuf *cbuf;
466 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
467 sizeof(void *)*size);