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_
25 #include <osdep_service.h>
26 #include <drv_types.h>
27 #include <recv_osdep.h>
28 #include <linux/vmalloc.h>
34 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
35 * @return: one of RTW_STATUS_CODE
37 inline int RTW_STATUS_CODE(int error_code){
43 // return RTW_STATUS_TIMEDOUT;
49 inline u8* _rtw_vmalloc(u32 sz)
57 inline u8* _rtw_zvmalloc(u32 sz)
60 pbuf = _rtw_vmalloc(sz);
67 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
72 u8* _rtw_malloc(u32 sz)
77 pbuf = kmalloc(sz,in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
83 u8* _rtw_zmalloc(u32 sz)
85 u8 *pbuf = _rtw_malloc(sz);
94 void _rtw_mfree(u8 *pbuf, u32 sz)
100 void _rtw_mutex_init(_mutex *pmutex)
102 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
109 void _rtw_mutex_free(_mutex *pmutex);
110 void _rtw_mutex_free(_mutex *pmutex)
112 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
113 mutex_destroy(pmutex);
119 void _rtw_init_queue(_queue *pqueue)
121 INIT_LIST_HEAD(&(pqueue->queue));
122 spin_lock_init(&(pqueue->lock));
125 u32 _rtw_queue_empty(_queue *pqueue)
127 if (list_empty(&(pqueue->queue)))
134 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
143 u32 rtw_get_current_time(void)
148 inline u32 rtw_systime_to_ms(u32 systime)
150 return systime * 1000 / HZ;
153 inline u32 rtw_ms_to_systime(u32 ms)
155 return ms * HZ / 1000;
158 // the input parameter start use the same unit as returned by rtw_get_current_time
159 inline s32 rtw_get_passing_time_ms(u32 start)
161 return rtw_systime_to_ms(jiffies-start);
164 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
166 return rtw_systime_to_ms(end-start);
170 void rtw_sleep_schedulable(int ms)
174 delta = (ms * HZ)/1000;//(ms)
178 set_current_state(TASK_INTERRUPTIBLE);
179 if (schedule_timeout(delta) != 0) {
186 void rtw_msleep_os(int ms)
188 msleep((unsigned int)ms);
191 void rtw_usleep_os(int us)
193 // msleep((unsigned int)us);
197 msleep( (us/1000) + 1);
202 void _rtw_mdelay_os(int ms, const char *func, const int line)
206 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
212 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
214 mdelay((unsigned long)ms);
216 void _rtw_udelay_os(int us, const char *func, const int line)
221 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
228 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
230 udelay((unsigned long)us);
233 void rtw_mdelay_os(int ms)
235 mdelay((unsigned long)ms);
237 void rtw_udelay_os(int us)
239 udelay((unsigned long)us);
243 void rtw_yield_os(void)
248 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
250 #ifdef CONFIG_WAKELOCK
251 static struct wake_lock rtw_suspend_lock;
252 #elif defined(CONFIG_ANDROID_POWER)
253 static android_suspend_lock_t rtw_suspend_lock ={
254 .name = RTW_SUSPEND_LOCK_NAME
258 inline void rtw_suspend_lock_init(void)
260 #ifdef CONFIG_WAKELOCK
261 wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME);
262 #elif defined(CONFIG_ANDROID_POWER)
263 android_init_suspend_lock(&rtw_suspend_lock);
267 inline void rtw_suspend_lock_uninit(void)
269 #ifdef CONFIG_WAKELOCK
270 wake_lock_destroy(&rtw_suspend_lock);
271 #elif defined(CONFIG_ANDROID_POWER)
272 android_uninit_suspend_lock(&rtw_suspend_lock);
276 inline void rtw_lock_suspend(void)
278 #ifdef CONFIG_WAKELOCK
279 wake_lock(&rtw_suspend_lock);
280 #elif defined(CONFIG_ANDROID_POWER)
281 android_lock_suspend(&rtw_suspend_lock);
284 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
285 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
289 inline void rtw_unlock_suspend(void)
291 #ifdef CONFIG_WAKELOCK
292 wake_unlock(&rtw_suspend_lock);
293 #elif defined(CONFIG_ANDROID_POWER)
294 android_unlock_suspend(&rtw_suspend_lock);
297 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
298 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
303 inline void rtw_lock_suspend_timeout(long timeout)
305 #ifdef CONFIG_WAKELOCK
306 wake_lock_timeout(&rtw_suspend_lock, timeout);
307 #elif defined(CONFIG_ANDROID_POWER)
308 android_lock_suspend_auto_expire(&rtw_suspend_lock, timeout);
311 #endif //CONFIG_WOWLAN
314 * Open a file with the specific @param path, @param flag, @param mode
315 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
316 * @param path the path of the file to open
317 * @param flag file operation flags, please refer to linux document
318 * @param mode please refer to linux document
319 * @return Linux specific error code
321 static int openFile(struct file **fpp, char *path, int flag, int mode)
325 fp=filp_open(path, flag, mode);
337 * Close the file with the specific @param fp
338 * @param fp the pointer of struct file to close
341 static int closeFile(struct file *fp)
347 static int readFile(struct file *fp,char *buf,int len)
351 if (!fp->f_op || !fp->f_op->read)
355 rlen=fp->f_op->read(fp,buf+sum,len-sum, &fp->f_pos);
368 static int writeFile(struct file *fp,char *buf,int len)
372 if (!fp->f_op || !fp->f_op->write)
376 wlen=fp->f_op->write(fp,buf+sum,len-sum, &fp->f_pos);
390 * Test if the specifi @param path is a file and readable
391 * @param path the path of the file to test
392 * @return Linux specific error code
394 static int isFileReadable(char *path)
401 fp=filp_open(path, O_RDONLY, 0);
406 oldfs = get_fs(); set_fs(get_ds());
408 if(1!=readFile(fp, &buf, 1))
418 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
419 * @param path the path of the file to open and read
420 * @param buf the starting address of the buffer to store file content
421 * @param sz how many bytes to read at most
422 * @return the byte we've read, or Linux specific error code
424 static int retriveFromFile(char *path, u8* buf, u32 sz)
431 if( 0 == (ret=openFile(&fp,path, O_RDONLY, 0)) ){
432 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
434 oldfs = get_fs(); set_fs(get_ds());
435 ret=readFile(fp, buf, sz);
439 DBG_8723A("%s readFile, ret:%d\n",__FUNCTION__, ret);
442 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
445 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
452 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
453 * @param path the path of the file to open and write
454 * @param buf the starting address of the data to write into file
455 * @param sz how many bytes to write at most
456 * @return the byte we've written, or Linux specific error code
458 static int storeToFile(char *path, u8* buf, u32 sz)
465 if( 0 == (ret=openFile(&fp, path, O_CREAT|O_WRONLY, 0666)) ) {
466 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
468 oldfs = get_fs(); set_fs(get_ds());
469 ret=writeFile(fp, buf, sz);
473 DBG_8723A("%s writeFile, ret:%d\n",__FUNCTION__, ret);
476 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
479 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
486 * Test if the specifi @param path is a file and readable
487 * @param path the path of the file to test
488 * @return _TRUE or _FALSE
490 int rtw_is_file_readable(char *path)
492 if(isFileReadable(path) == 0)
499 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
500 * @param path the path of the file to open and read
501 * @param buf the starting address of the buffer to store file content
502 * @param sz how many bytes to read at most
503 * @return the byte we've read
505 int rtw_retrive_from_file(char *path, u8* buf, u32 sz)
507 int ret =retriveFromFile(path, buf, sz);
512 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
513 * @param path the path of the file to open and write
514 * @param buf the starting address of the data to write into file
515 * @param sz how many bytes to write at most
516 * @return the byte we've written
518 int rtw_store_to_file(char *path, u8* buf, u32 sz)
520 int ret =storeToFile(path, buf, sz);
524 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
526 struct net_device *pnetdev;
527 struct rtw_netdev_priv_indicator *pnpi;
529 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
530 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
532 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
537 pnpi = netdev_priv(pnetdev);
539 pnpi->sizeof_priv=sizeof_priv;
545 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
547 struct net_device *pnetdev;
548 struct rtw_netdev_priv_indicator *pnpi;
550 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
551 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
553 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
558 pnpi = netdev_priv(pnetdev);
560 pnpi->priv = rtw_zvmalloc(sizeof_priv);
562 free_netdev(pnetdev);
567 pnpi->sizeof_priv=sizeof_priv;
572 void rtw_free_netdev(struct net_device * netdev)
574 struct rtw_netdev_priv_indicator *pnpi;
579 pnpi = netdev_priv(netdev);
584 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
592 * Jeff: this function should be called under ioctl (rtnl_lock is accquired) while
593 * LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
595 int rtw_change_ifname(_adapter *padapter, const char *ifname)
597 struct net_device *pnetdev;
598 struct net_device *cur_pnetdev;
599 struct rereg_nd_name_data *rereg_priv;
605 cur_pnetdev = padapter->pnetdev;
606 rereg_priv = &padapter->rereg_nd_name_priv;
608 //free the old_pnetdev
609 if(rereg_priv->old_pnetdev) {
610 free_netdev(rereg_priv->old_pnetdev);
611 rereg_priv->old_pnetdev = NULL;
614 if(!rtnl_is_locked())
615 unregister_netdev(cur_pnetdev);
617 unregister_netdevice(cur_pnetdev);
619 rtw_proc_remove_one(cur_pnetdev);
621 rereg_priv->old_pnetdev=cur_pnetdev;
623 pnetdev = rtw_init_netdev(padapter);
629 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
631 rtw_init_netdev_name(pnetdev, ifname);
633 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
635 if(!rtnl_is_locked())
636 ret = register_netdev(pnetdev);
638 ret = register_netdevice(pnetdev);
641 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("register_netdev() failed\n"));
645 rtw_proc_init_one(pnetdev);
655 u64 rtw_modular64(u64 x, u64 y)
660 u64 rtw_division64(u64 x, u64 y)
666 void rtw_buf_free(u8 **buf, u32 *buf_len)
670 if (!buf || !buf_len)
677 _rtw_mfree(*buf, *buf_len);
682 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
684 u32 ori_len = 0, dup_len = 0;
688 if (!buf || !buf_len)
691 if (!src || !src_len)
695 dup = rtw_malloc(src_len);
698 memcpy(dup, src, dup_len);
705 /* replace buf with dup */
711 if (ori && ori_len > 0)
712 _rtw_mfree(ori, ori_len);
717 * rtw_cbuf_full - test if cbuf is full
718 * @cbuf: pointer of struct rtw_cbuf
720 * Returns: _TRUE if cbuf is full
722 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
724 return (cbuf->write == cbuf->read-1)? _TRUE : _FALSE;
728 * rtw_cbuf_empty - test if cbuf is empty
729 * @cbuf: pointer of struct rtw_cbuf
731 * Returns: _TRUE if cbuf is empty
733 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
735 return (cbuf->write == cbuf->read)? _TRUE : _FALSE;
739 * rtw_cbuf_push - push a pointer into cbuf
740 * @cbuf: pointer of struct rtw_cbuf
741 * @buf: pointer to push in
743 * Lock free operation, be careful of the use scheme
744 * Returns: _TRUE push success
746 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
748 if (rtw_cbuf_full(cbuf))
752 DBG_8723A("%s on %u\n", __func__, cbuf->write);
753 cbuf->bufs[cbuf->write] = buf;
754 cbuf->write = (cbuf->write+1)%cbuf->size;
760 * rtw_cbuf_pop - pop a pointer from cbuf
761 * @cbuf: pointer of struct rtw_cbuf
763 * Lock free operation, be careful of the use scheme
764 * Returns: pointer popped out
766 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
769 if (rtw_cbuf_empty(cbuf))
773 DBG_8723A("%s on %u\n", __func__, cbuf->read);
774 buf = cbuf->bufs[cbuf->read];
775 cbuf->read = (cbuf->read+1)%cbuf->size;
781 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
782 * @size: size of pointer
784 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
786 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
788 struct rtw_cbuf *cbuf;
790 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void*)*size);
793 cbuf->write = cbuf->read = 0;
801 * rtw_cbuf_free - free the given rtw_cbuf
802 * @cbuf: pointer of struct rtw_cbuf to free
804 void rtw_cbuf_free(struct rtw_cbuf *cbuf)
806 rtw_mfree((u8*)cbuf, sizeof(*cbuf) + sizeof(void*)*cbuf->size);