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 u8 *_rtw_malloc(u32 sz)
62 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
66 u8 *_rtw_zmalloc(u32 sz)
68 u8 *pbuf = _rtw_malloc(sz);
75 void *rtw_malloc2d(int h, int w, int size)
79 void **a = (void **)rtw_zmalloc(h*sizeof(void *) + h*w*size);
81 pr_info("%s: alloc memory fail!\n", __func__);
85 for (j = 0; j < h; j++)
86 a[j] = ((char *)(a+h)) + j*w*size;
91 void rtw_mfree2d(void *pbuf, int h, int w, int size)
96 void _rtw_memset(void *pbuf, int c, u32 sz)
101 void _rtw_init_listhead(struct list_head *list)
103 INIT_LIST_HEAD(list);
107 For the following list_xxx operations,
108 caller must guarantee the atomic context.
109 Otherwise, there will be racing condition.
111 u32 rtw_is_list_empty(struct list_head *phead)
113 if (list_empty(phead))
119 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead)
121 list_add(plist, phead);
124 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead)
126 list_add_tail(plist, phead);
130 Caller must check if the list is empty before calling rtw_list_delete
133 u32 _rtw_down_sema(struct semaphore *sema)
135 if (down_interruptible(sema))
141 void _rtw_init_queue(struct __queue *pqueue)
143 _rtw_init_listhead(&(pqueue->queue));
144 spin_lock_init(&(pqueue->lock));
147 u32 _rtw_queue_empty(struct __queue *pqueue)
149 return rtw_is_list_empty(&(pqueue->queue));
152 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
160 inline u32 rtw_systime_to_ms(u32 systime)
162 return systime * 1000 / HZ;
165 inline u32 rtw_ms_to_systime(u32 ms)
167 return ms * HZ / 1000;
170 /* the input parameter start must be in jiffies */
171 inline s32 rtw_get_passing_time_ms(u32 start)
173 return rtw_systime_to_ms(jiffies-start);
176 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
178 return rtw_systime_to_ms(end-start);
181 void rtw_sleep_schedulable(int ms)
185 delta = (ms * HZ)/1000;/* ms) */
188 set_current_state(TASK_INTERRUPTIBLE);
189 if (schedule_timeout(delta) != 0)
193 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
195 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
198 struct net_device *pnetdev;
199 struct rtw_netdev_priv_indicator *pnpi;
201 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
205 pnpi = netdev_priv(pnetdev);
206 pnpi->priv = old_priv;
207 pnpi->sizeof_priv = sizeof_priv;
213 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
215 struct net_device *pnetdev;
216 struct rtw_netdev_priv_indicator *pnpi;
218 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
222 pnpi = netdev_priv(pnetdev);
224 pnpi->priv = vzalloc(sizeof_priv);
226 free_netdev(pnetdev);
231 pnpi->sizeof_priv = sizeof_priv;
236 void rtw_free_netdev(struct net_device *netdev)
238 struct rtw_netdev_priv_indicator *pnpi;
243 pnpi = netdev_priv(netdev);
255 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
257 struct net_device *pnetdev;
258 struct net_device *cur_pnetdev;
259 struct rereg_nd_name_data *rereg_priv;
265 cur_pnetdev = padapter->pnetdev;
266 rereg_priv = &padapter->rereg_nd_name_priv;
268 /* free the old_pnetdev */
269 if (rereg_priv->old_pnetdev) {
270 free_netdev(rereg_priv->old_pnetdev);
271 rereg_priv->old_pnetdev = NULL;
274 if (!rtnl_is_locked())
275 unregister_netdev(cur_pnetdev);
277 unregister_netdevice(cur_pnetdev);
279 rtw_proc_remove_one(cur_pnetdev);
281 rereg_priv->old_pnetdev = cur_pnetdev;
283 pnetdev = rtw_init_netdev(padapter);
289 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
291 rtw_init_netdev_name(pnetdev, ifname);
293 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
295 if (!rtnl_is_locked())
296 ret = register_netdev(pnetdev);
298 ret = register_netdevice(pnetdev);
300 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
301 ("register_netdev() failed\n"));
304 rtw_proc_init_one(pnetdev);
310 u64 rtw_modular64(u64 x, u64 y)
315 u64 rtw_division64(u64 x, u64 y)
321 void rtw_buf_free(u8 **buf, u32 *buf_len)
328 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
330 u32 ori_len = 0, dup_len = 0;
334 if (!buf || !buf_len)
337 if (!src || !src_len)
341 dup = rtw_malloc(src_len);
344 memcpy(dup, src, dup_len);
351 /* replace buf with dup */
362 * rtw_cbuf_full - test if cbuf is full
363 * @cbuf: pointer of struct rtw_cbuf
365 * Returns: true if cbuf is full
367 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
369 return (cbuf->write == cbuf->read-1) ? true : false;
373 * rtw_cbuf_empty - test if cbuf is empty
374 * @cbuf: pointer of struct rtw_cbuf
376 * Returns: true if cbuf is empty
378 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
380 return (cbuf->write == cbuf->read) ? true : false;
384 * rtw_cbuf_push - push a pointer into cbuf
385 * @cbuf: pointer of struct rtw_cbuf
386 * @buf: pointer to push in
388 * Lock free operation, be careful of the use scheme
389 * Returns: true push success
391 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
393 if (rtw_cbuf_full(cbuf))
397 DBG_88E("%s on %u\n", __func__, cbuf->write);
398 cbuf->bufs[cbuf->write] = buf;
399 cbuf->write = (cbuf->write+1)%cbuf->size;
405 * rtw_cbuf_pop - pop a pointer from cbuf
406 * @cbuf: pointer of struct rtw_cbuf
408 * Lock free operation, be careful of the use scheme
409 * Returns: pointer popped out
411 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
414 if (rtw_cbuf_empty(cbuf))
418 DBG_88E("%s on %u\n", __func__, cbuf->read);
419 buf = cbuf->bufs[cbuf->read];
420 cbuf->read = (cbuf->read+1)%cbuf->size;
426 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
427 * @size: size of pointer
429 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
431 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
433 struct rtw_cbuf *cbuf;
435 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
436 sizeof(void *)*size);