1 /******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4 *
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.
8 *
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
12 * more details.
13 *
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
17 *
18 *
19 ******************************************************************************/
20
21
22 #define _OSDEP_SERVICE_C_
23
24 #include <osdep_service.h>
25 #include <osdep_intf.h>
26 #include <drv_types.h>
27 #include <recv_osdep.h>
28 #include <linux/vmalloc.h>
29 #include <rtw_ioctl_set.h>
30
31 /*
32 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
33 * @return: one of RTW_STATUS_CODE
34 */
RTW_STATUS_CODE(int error_code)35 inline int RTW_STATUS_CODE(int error_code)
36 {
37 if (error_code >= 0)
38 return _SUCCESS;
39 return _FAIL;
40 }
41
_rtw_malloc(u32 sz)42 u8 *_rtw_malloc(u32 sz)
43 {
44 u8 *pbuf = NULL;
45
46 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
47 return pbuf;
48 }
49
rtw_malloc2d(int h,int w,int size)50 void *rtw_malloc2d(int h, int w, int size)
51 {
52 int j;
53
54 void **a = (void **)kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
55 if (a == NULL) {
56 pr_info("%s: alloc memory fail!\n", __func__);
57 return NULL;
58 }
59
60 for (j = 0; j < h; j++)
61 a[j] = ((char *)(a+h)) + j*w*size;
62
63 return a;
64 }
65
_rtw_down_sema(struct semaphore * sema)66 u32 _rtw_down_sema(struct semaphore *sema)
67 {
68 if (down_interruptible(sema))
69 return _FAIL;
70 else
71 return _SUCCESS;
72 }
73
_rtw_init_queue(struct __queue * pqueue)74 void _rtw_init_queue(struct __queue *pqueue)
75 {
76 INIT_LIST_HEAD(&(pqueue->queue));
77 spin_lock_init(&(pqueue->lock));
78 }
79
80 /* the input parameter start must be in jiffies */
rtw_get_passing_time_ms(u32 start)81 inline s32 rtw_get_passing_time_ms(u32 start)
82 {
83 return jiffies_to_msecs(jiffies-start);
84 }
85
rtw_alloc_etherdev_with_old_priv(int sizeof_priv,void * old_priv)86 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
87 void *old_priv)
88 {
89 struct net_device *pnetdev;
90 struct rtw_netdev_priv_indicator *pnpi;
91
92 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
93 if (!pnetdev)
94 goto RETURN;
95
96 pnpi = netdev_priv(pnetdev);
97 pnpi->priv = old_priv;
98 pnpi->sizeof_priv = sizeof_priv;
99
100 RETURN:
101 return pnetdev;
102 }
103
rtw_free_netdev(struct net_device * netdev)104 void rtw_free_netdev(struct net_device *netdev)
105 {
106 struct rtw_netdev_priv_indicator *pnpi;
107
108 if (!netdev)
109 goto RETURN;
110
111 pnpi = netdev_priv(netdev);
112
113 if (!pnpi->priv)
114 goto RETURN;
115
116 vfree(pnpi->priv);
117 free_netdev(netdev);
118
119 RETURN:
120 return;
121 }
122
rtw_modular64(u64 x,u64 y)123 u64 rtw_modular64(u64 x, u64 y)
124 {
125 return do_div(x, y);
126 }
127
rtw_buf_free(u8 ** buf,u32 * buf_len)128 void rtw_buf_free(u8 **buf, u32 *buf_len)
129 {
130 *buf_len = 0;
131 kfree(*buf);
132 *buf = NULL;
133 }
134
rtw_buf_update(u8 ** buf,u32 * buf_len,u8 * src,u32 src_len)135 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
136 {
137 u32 ori_len = 0, dup_len = 0;
138 u8 *ori = NULL;
139 u8 *dup = NULL;
140
141 if (!buf || !buf_len)
142 return;
143
144 if (!src || !src_len)
145 goto keep_ori;
146
147 /* duplicate src */
148 dup = rtw_malloc(src_len);
149 if (dup) {
150 dup_len = src_len;
151 memcpy(dup, src, dup_len);
152 }
153
154 keep_ori:
155 ori = *buf;
156 ori_len = *buf_len;
157
158 /* replace buf with dup */
159 *buf_len = 0;
160 *buf = dup;
161 *buf_len = dup_len;
162
163 /* free ori */
164 kfree(ori);
165 }
166