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)
99 void _rtw_init_listhead(_list *list)
101 INIT_LIST_HEAD(list);
106 For the following list_xxx operations,
107 caller must guarantee the atomic context.
108 Otherwise, there will be racing condition.
110 u32 rtw_is_list_empty(_list *phead)
112 if (list_empty(phead))
118 void rtw_list_insert_head(_list *plist, _list *phead)
120 list_add(plist, phead);
123 void rtw_list_insert_tail(_list *plist, _list *phead)
125 list_add_tail(plist, phead);
131 Caller must check if the list is empty before calling rtw_list_delete
136 void _rtw_mutex_init(_mutex *pmutex)
138 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
145 void _rtw_mutex_free(_mutex *pmutex);
146 void _rtw_mutex_free(_mutex *pmutex)
148 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,37))
149 mutex_destroy(pmutex);
155 void _rtw_init_queue(_queue *pqueue)
157 _rtw_init_listhead(&(pqueue->queue));
158 spin_lock_init(&(pqueue->lock));
161 u32 _rtw_queue_empty(_queue *pqueue)
163 return (rtw_is_list_empty(&(pqueue->queue)));
167 u32 rtw_end_of_queue_search(_list *head, _list *plist)
176 u32 rtw_get_current_time(void)
181 inline u32 rtw_systime_to_ms(u32 systime)
183 return systime * 1000 / HZ;
186 inline u32 rtw_ms_to_systime(u32 ms)
188 return ms * HZ / 1000;
191 // the input parameter start use the same unit as returned by rtw_get_current_time
192 inline s32 rtw_get_passing_time_ms(u32 start)
194 return rtw_systime_to_ms(jiffies-start);
197 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
199 return rtw_systime_to_ms(end-start);
203 void rtw_sleep_schedulable(int ms)
207 delta = (ms * HZ)/1000;//(ms)
211 set_current_state(TASK_INTERRUPTIBLE);
212 if (schedule_timeout(delta) != 0) {
219 void rtw_msleep_os(int ms)
221 msleep((unsigned int)ms);
224 void rtw_usleep_os(int us)
226 // msleep((unsigned int)us);
230 msleep( (us/1000) + 1);
235 void _rtw_mdelay_os(int ms, const char *func, const int line)
239 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
245 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, ms);
247 mdelay((unsigned long)ms);
249 void _rtw_udelay_os(int us, const char *func, const int line)
254 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
261 DBG_8723A("%s:%d %s(%d)\n", func, line, __FUNCTION__, us);
263 udelay((unsigned long)us);
266 void rtw_mdelay_os(int ms)
268 mdelay((unsigned long)ms);
270 void rtw_udelay_os(int us)
272 udelay((unsigned long)us);
276 void rtw_yield_os(void)
281 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
283 #ifdef CONFIG_WAKELOCK
284 static struct wake_lock rtw_suspend_lock;
285 #elif defined(CONFIG_ANDROID_POWER)
286 static android_suspend_lock_t rtw_suspend_lock ={
287 .name = RTW_SUSPEND_LOCK_NAME
291 inline void rtw_suspend_lock_init(void)
293 #ifdef CONFIG_WAKELOCK
294 wake_lock_init(&rtw_suspend_lock, WAKE_LOCK_SUSPEND, RTW_SUSPEND_LOCK_NAME);
295 #elif defined(CONFIG_ANDROID_POWER)
296 android_init_suspend_lock(&rtw_suspend_lock);
300 inline void rtw_suspend_lock_uninit(void)
302 #ifdef CONFIG_WAKELOCK
303 wake_lock_destroy(&rtw_suspend_lock);
304 #elif defined(CONFIG_ANDROID_POWER)
305 android_uninit_suspend_lock(&rtw_suspend_lock);
309 inline void rtw_lock_suspend(void)
311 #ifdef CONFIG_WAKELOCK
312 wake_lock(&rtw_suspend_lock);
313 #elif defined(CONFIG_ANDROID_POWER)
314 android_lock_suspend(&rtw_suspend_lock);
317 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
318 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
322 inline void rtw_unlock_suspend(void)
324 #ifdef CONFIG_WAKELOCK
325 wake_unlock(&rtw_suspend_lock);
326 #elif defined(CONFIG_ANDROID_POWER)
327 android_unlock_suspend(&rtw_suspend_lock);
330 #if defined(CONFIG_WAKELOCK) || defined(CONFIG_ANDROID_POWER)
331 //DBG_8723A("####%s: suspend_lock_count:%d####\n", __FUNCTION__, rtw_suspend_lock.stat.count);
336 inline void rtw_lock_suspend_timeout(long timeout)
338 #ifdef CONFIG_WAKELOCK
339 wake_lock_timeout(&rtw_suspend_lock, timeout);
340 #elif defined(CONFIG_ANDROID_POWER)
341 android_lock_suspend_auto_expire(&rtw_suspend_lock, timeout);
344 #endif //CONFIG_WOWLAN
346 inline void ATOMIC_SET(ATOMIC_T *v, int i)
351 inline int ATOMIC_READ(ATOMIC_T *v)
353 return atomic_read(v);
356 inline void ATOMIC_ADD(ATOMIC_T *v, int i)
360 inline void ATOMIC_SUB(ATOMIC_T *v, int i)
365 inline void ATOMIC_INC(ATOMIC_T *v)
370 inline void ATOMIC_DEC(ATOMIC_T *v)
375 inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
377 return atomic_add_return(i,v);
380 inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
382 return atomic_sub_return(i,v);
385 inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
387 return atomic_inc_return(v);
390 inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
392 return atomic_dec_return(v);
397 * Open a file with the specific @param path, @param flag, @param mode
398 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
399 * @param path the path of the file to open
400 * @param flag file operation flags, please refer to linux document
401 * @param mode please refer to linux document
402 * @return Linux specific error code
404 static int openFile(struct file **fpp, char *path, int flag, int mode)
408 fp=filp_open(path, flag, mode);
420 * Close the file with the specific @param fp
421 * @param fp the pointer of struct file to close
424 static int closeFile(struct file *fp)
430 static int readFile(struct file *fp,char *buf,int len)
434 if (!fp->f_op || !fp->f_op->read)
438 rlen=fp->f_op->read(fp,buf+sum,len-sum, &fp->f_pos);
451 static int writeFile(struct file *fp,char *buf,int len)
455 if (!fp->f_op || !fp->f_op->write)
459 wlen=fp->f_op->write(fp,buf+sum,len-sum, &fp->f_pos);
473 * Test if the specifi @param path is a file and readable
474 * @param path the path of the file to test
475 * @return Linux specific error code
477 static int isFileReadable(char *path)
484 fp=filp_open(path, O_RDONLY, 0);
489 oldfs = get_fs(); set_fs(get_ds());
491 if(1!=readFile(fp, &buf, 1))
501 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
502 * @param path the path of the file to open and read
503 * @param buf the starting address of the buffer to store file content
504 * @param sz how many bytes to read at most
505 * @return the byte we've read, or Linux specific error code
507 static int retriveFromFile(char *path, u8* buf, u32 sz)
514 if( 0 == (ret=openFile(&fp,path, O_RDONLY, 0)) ){
515 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
517 oldfs = get_fs(); set_fs(get_ds());
518 ret=readFile(fp, buf, sz);
522 DBG_8723A("%s readFile, ret:%d\n",__FUNCTION__, ret);
525 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
528 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
535 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
536 * @param path the path of the file to open and write
537 * @param buf the starting address of the data to write into file
538 * @param sz how many bytes to write at most
539 * @return the byte we've written, or Linux specific error code
541 static int storeToFile(char *path, u8* buf, u32 sz)
548 if( 0 == (ret=openFile(&fp, path, O_CREAT|O_WRONLY, 0666)) ) {
549 DBG_8723A("%s openFile path:%s fp=%p\n",__FUNCTION__, path ,fp);
551 oldfs = get_fs(); set_fs(get_ds());
552 ret=writeFile(fp, buf, sz);
556 DBG_8723A("%s writeFile, ret:%d\n",__FUNCTION__, ret);
559 DBG_8723A("%s openFile path:%s Fail, ret:%d\n",__FUNCTION__, path, ret);
562 DBG_8723A("%s NULL pointer\n",__FUNCTION__);
569 * Test if the specifi @param path is a file and readable
570 * @param path the path of the file to test
571 * @return _TRUE or _FALSE
573 int rtw_is_file_readable(char *path)
575 if(isFileReadable(path) == 0)
582 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
583 * @param path the path of the file to open and read
584 * @param buf the starting address of the buffer to store file content
585 * @param sz how many bytes to read at most
586 * @return the byte we've read
588 int rtw_retrive_from_file(char *path, u8* buf, u32 sz)
590 int ret =retriveFromFile(path, buf, sz);
595 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
596 * @param path the path of the file to open and write
597 * @param buf the starting address of the data to write into file
598 * @param sz how many bytes to write at most
599 * @return the byte we've written
601 int rtw_store_to_file(char *path, u8* buf, u32 sz)
603 int ret =storeToFile(path, buf, sz);
607 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
609 struct net_device *pnetdev;
610 struct rtw_netdev_priv_indicator *pnpi;
612 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
613 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
615 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
620 pnpi = netdev_priv(pnetdev);
622 pnpi->sizeof_priv=sizeof_priv;
628 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
630 struct net_device *pnetdev;
631 struct rtw_netdev_priv_indicator *pnpi;
633 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
634 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
636 pnetdev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
641 pnpi = netdev_priv(pnetdev);
643 pnpi->priv = rtw_zvmalloc(sizeof_priv);
645 free_netdev(pnetdev);
650 pnpi->sizeof_priv=sizeof_priv;
655 void rtw_free_netdev(struct net_device * netdev)
657 struct rtw_netdev_priv_indicator *pnpi;
662 pnpi = netdev_priv(netdev);
667 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
675 * Jeff: this function should be called under ioctl (rtnl_lock is accquired) while
676 * LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
678 int rtw_change_ifname(_adapter *padapter, const char *ifname)
680 struct net_device *pnetdev;
681 struct net_device *cur_pnetdev;
682 struct rereg_nd_name_data *rereg_priv;
688 cur_pnetdev = padapter->pnetdev;
689 rereg_priv = &padapter->rereg_nd_name_priv;
691 //free the old_pnetdev
692 if(rereg_priv->old_pnetdev) {
693 free_netdev(rereg_priv->old_pnetdev);
694 rereg_priv->old_pnetdev = NULL;
697 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
698 if(!rtnl_is_locked())
699 unregister_netdev(cur_pnetdev);
702 unregister_netdevice(cur_pnetdev);
704 rtw_proc_remove_one(cur_pnetdev);
706 rereg_priv->old_pnetdev=cur_pnetdev;
708 pnetdev = rtw_init_netdev(padapter);
714 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
716 rtw_init_netdev_name(pnetdev, ifname);
718 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
720 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26))
721 if(!rtnl_is_locked())
722 ret = register_netdev(pnetdev);
725 ret = register_netdevice(pnetdev);
728 RT_TRACE(_module_hci_intfs_c_,_drv_err_,("register_netdev() failed\n"));
732 rtw_proc_init_one(pnetdev);
742 u64 rtw_modular64(u64 x, u64 y)
747 u64 rtw_division64(u64 x, u64 y)
753 void rtw_buf_free(u8 **buf, u32 *buf_len)
757 if (!buf || !buf_len)
764 _rtw_mfree(*buf, *buf_len);
769 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
771 u32 ori_len = 0, dup_len = 0;
775 if (!buf || !buf_len)
778 if (!src || !src_len)
782 dup = rtw_malloc(src_len);
785 memcpy(dup, src, dup_len);
792 /* replace buf with dup */
798 if (ori && ori_len > 0)
799 _rtw_mfree(ori, ori_len);
804 * rtw_cbuf_full - test if cbuf is full
805 * @cbuf: pointer of struct rtw_cbuf
807 * Returns: _TRUE if cbuf is full
809 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
811 return (cbuf->write == cbuf->read-1)? _TRUE : _FALSE;
815 * rtw_cbuf_empty - test if cbuf is empty
816 * @cbuf: pointer of struct rtw_cbuf
818 * Returns: _TRUE if cbuf is empty
820 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
822 return (cbuf->write == cbuf->read)? _TRUE : _FALSE;
826 * rtw_cbuf_push - push a pointer into cbuf
827 * @cbuf: pointer of struct rtw_cbuf
828 * @buf: pointer to push in
830 * Lock free operation, be careful of the use scheme
831 * Returns: _TRUE push success
833 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
835 if (rtw_cbuf_full(cbuf))
839 DBG_8723A("%s on %u\n", __func__, cbuf->write);
840 cbuf->bufs[cbuf->write] = buf;
841 cbuf->write = (cbuf->write+1)%cbuf->size;
847 * rtw_cbuf_pop - pop a pointer from cbuf
848 * @cbuf: pointer of struct rtw_cbuf
850 * Lock free operation, be careful of the use scheme
851 * Returns: pointer popped out
853 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
856 if (rtw_cbuf_empty(cbuf))
860 DBG_8723A("%s on %u\n", __func__, cbuf->read);
861 buf = cbuf->bufs[cbuf->read];
862 cbuf->read = (cbuf->read+1)%cbuf->size;
868 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
869 * @size: size of pointer
871 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
873 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
875 struct rtw_cbuf *cbuf;
877 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void*)*size);
880 cbuf->write = cbuf->read = 0;
888 * rtw_cbuf_free - free the given rtw_cbuf
889 * @cbuf: pointer of struct rtw_cbuf to free
891 void rtw_cbuf_free(struct rtw_cbuf *cbuf)
893 rtw_mfree((u8*)cbuf, sizeof(*cbuf) + sizeof(void*)*cbuf->size);