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 /* Open a file with the specific @param path, @param flag, @param mode
360 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
361 * @param path the path of the file to open
362 * @param flag file operation flags, please refer to linux document
363 * @param mode please refer to linux document
364 * @return Linux specific error code
366 static int openfile(struct file **fpp, char *path, int flag, int mode)
370 fp = filp_open(path, flag, mode);
380 /* Close the file with the specific @param fp
381 * @param fp the pointer of struct file to close
384 static int closefile(struct file *fp)
386 filp_close(fp, NULL);
390 static int readfile(struct file *fp, char __user *buf, int len)
392 int rlen = 0, sum = 0;
394 if (!fp->f_op || !fp->f_op->read)
398 rlen = fp->f_op->read(fp, buf+sum, len-sum, &fp->f_pos);
409 static int writefile(struct file *fp, char __user *buf, int len)
411 int wlen = 0, sum = 0;
413 if (!fp->f_op || !fp->f_op->write)
417 wlen = fp->f_op->write(fp, buf+sum, len-sum, &fp->f_pos);
428 /* Test if the specifi @param path is a file and readable
429 * @param path the path of the file to test
430 * @return Linux specific error code
432 static int isfilereadable(char *path)
439 fp = filp_open(path, O_RDONLY, 0);
443 oldfs = get_fs(); set_fs(get_ds());
445 if (1 != readfile(fp, &buf, 1))
449 filp_close(fp, NULL);
454 /* Open the file with @param path and retrive the file content into
455 * memory starting from @param buf for @param sz at most
456 * @param path the path of the file to open and read
457 * @param buf the starting address of the buffer to store file content
458 * @param sz how many bytes to read at most
459 * @return the byte we've read, or Linux specific error code
461 static int retrievefromfile(char *path, u8 __user *buf, u32 sz)
468 ret = openfile(&fp, path, O_RDONLY, 0);
470 DBG_88E("%s openfile path:%s fp =%p\n", __func__,
473 oldfs = get_fs(); set_fs(get_ds());
474 ret = readfile(fp, buf, sz);
478 DBG_88E("%s readfile, ret:%d\n", __func__, ret);
481 DBG_88E("%s openfile path:%s Fail, ret:%d\n", __func__,
485 DBG_88E("%s NULL pointer\n", __func__);
492 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
493 * @param path the path of the file to open and write
494 * @param buf the starting address of the data to write into file
495 * @param sz how many bytes to write at most
496 * @return the byte we've written, or Linux specific error code
498 static int storetofile(char *path, u8 __user *buf, u32 sz)
505 ret = openfile(&fp, path, O_CREAT|O_WRONLY, 0666);
507 DBG_88E("%s openfile path:%s fp =%p\n", __func__, path, fp);
509 oldfs = get_fs(); set_fs(get_ds());
510 ret = writefile(fp, buf, sz);
514 DBG_88E("%s writefile, ret:%d\n", __func__, ret);
517 DBG_88E("%s openfile path:%s Fail, ret:%d\n", __func__, path, ret);
520 DBG_88E("%s NULL pointer\n", __func__);
527 * Test if the specifi @param path is a file and readable
528 * @param path the path of the file to test
529 * @return true or false
531 int rtw_is_file_readable(char *path)
533 if (isfilereadable(path) == 0)
540 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
541 * @param path the path of the file to open and read
542 * @param buf the starting address of the buffer to store file content
543 * @param sz how many bytes to read at most
544 * @return the byte we've read
546 int rtw_retrive_from_file(char *path, u8 __user *buf, u32 sz)
548 int ret = retrievefromfile(path, buf, sz);
550 return ret >= 0 ? ret : 0;
554 * Open the file with @param path and wirte @param sz byte of data
555 * starting from @param buf into the file
556 * @param path the path of the file to open and write
557 * @param buf the starting address of the data to write into file
558 * @param sz how many bytes to write at most
559 * @return the byte we've written
561 int rtw_store_to_file(char *path, u8 __user *buf, u32 sz)
563 int ret = storetofile(path, buf, sz);
564 return ret >= 0 ? ret : 0;
567 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
570 struct net_device *pnetdev;
571 struct rtw_netdev_priv_indicator *pnpi;
573 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
577 pnpi = netdev_priv(pnetdev);
578 pnpi->priv = old_priv;
579 pnpi->sizeof_priv = sizeof_priv;
585 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
587 struct net_device *pnetdev;
588 struct rtw_netdev_priv_indicator *pnpi;
590 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
594 pnpi = netdev_priv(pnetdev);
596 pnpi->priv = rtw_zvmalloc(sizeof_priv);
598 free_netdev(pnetdev);
603 pnpi->sizeof_priv = sizeof_priv;
608 void rtw_free_netdev(struct net_device *netdev)
610 struct rtw_netdev_priv_indicator *pnpi;
615 pnpi = netdev_priv(netdev);
620 rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
627 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
629 struct net_device *pnetdev;
630 struct net_device *cur_pnetdev;
631 struct rereg_nd_name_data *rereg_priv;
637 cur_pnetdev = padapter->pnetdev;
638 rereg_priv = &padapter->rereg_nd_name_priv;
640 /* free the old_pnetdev */
641 if (rereg_priv->old_pnetdev) {
642 free_netdev(rereg_priv->old_pnetdev);
643 rereg_priv->old_pnetdev = NULL;
646 if (!rtnl_is_locked())
647 unregister_netdev(cur_pnetdev);
649 unregister_netdevice(cur_pnetdev);
651 rtw_proc_remove_one(cur_pnetdev);
653 rereg_priv->old_pnetdev = cur_pnetdev;
655 pnetdev = rtw_init_netdev(padapter);
661 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
663 rtw_init_netdev_name(pnetdev, ifname);
665 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
667 if (!rtnl_is_locked())
668 ret = register_netdev(pnetdev);
670 ret = register_netdevice(pnetdev);
672 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
673 ("register_netdev() failed\n"));
676 rtw_proc_init_one(pnetdev);
682 u64 rtw_modular64(u64 x, u64 y)
687 u64 rtw_division64(u64 x, u64 y)
693 void rtw_buf_free(u8 **buf, u32 *buf_len)
700 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
702 u32 ori_len = 0, dup_len = 0;
706 if (!buf || !buf_len)
709 if (!src || !src_len)
713 dup = rtw_malloc(src_len);
716 memcpy(dup, src, dup_len);
723 /* replace buf with dup */
734 * rtw_cbuf_full - test if cbuf is full
735 * @cbuf: pointer of struct rtw_cbuf
737 * Returns: true if cbuf is full
739 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
741 return (cbuf->write == cbuf->read-1) ? true : false;
745 * rtw_cbuf_empty - test if cbuf is empty
746 * @cbuf: pointer of struct rtw_cbuf
748 * Returns: true if cbuf is empty
750 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
752 return (cbuf->write == cbuf->read) ? true : false;
756 * rtw_cbuf_push - push a pointer into cbuf
757 * @cbuf: pointer of struct rtw_cbuf
758 * @buf: pointer to push in
760 * Lock free operation, be careful of the use scheme
761 * Returns: true push success
763 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
765 if (rtw_cbuf_full(cbuf))
769 DBG_88E("%s on %u\n", __func__, cbuf->write);
770 cbuf->bufs[cbuf->write] = buf;
771 cbuf->write = (cbuf->write+1)%cbuf->size;
777 * rtw_cbuf_pop - pop a pointer from cbuf
778 * @cbuf: pointer of struct rtw_cbuf
780 * Lock free operation, be careful of the use scheme
781 * Returns: pointer popped out
783 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
786 if (rtw_cbuf_empty(cbuf))
790 DBG_88E("%s on %u\n", __func__, cbuf->read);
791 buf = cbuf->bufs[cbuf->read];
792 cbuf->read = (cbuf->read+1)%cbuf->size;
798 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
799 * @size: size of pointer
801 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
803 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
805 struct rtw_cbuf *cbuf;
807 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
808 sizeof(void *)*size);