]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
96f8eb88b06d2ba43bbabc3547988572c8ad2fa8
[karo-tx-linux.git] / drivers / staging / rtl8188eu / os_dep / osdep_service.c
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 */
35 inline int RTW_STATUS_CODE(int error_code)
36 {
37         if (error_code >= 0)
38                 return _SUCCESS;
39         return _FAIL;
40 }
41
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
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
66 u32 _rtw_down_sema(struct semaphore *sema)
67 {
68         if (down_interruptible(sema))
69                 return _FAIL;
70         else
71                 return _SUCCESS;
72 }
73
74 void    _rtw_init_queue(struct __queue *pqueue)
75 {
76         INIT_LIST_HEAD(&(pqueue->queue));
77         spin_lock_init(&(pqueue->lock));
78 }
79
80 inline u32 rtw_ms_to_systime(u32 ms)
81 {
82         return ms * HZ / 1000;
83 }
84
85 /*  the input parameter start must be in jiffies */
86 inline s32 rtw_get_passing_time_ms(u32 start)
87 {
88         return jiffies_to_msecs(jiffies-start);
89 }
90
91 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
92                                                     void *old_priv)
93 {
94         struct net_device *pnetdev;
95         struct rtw_netdev_priv_indicator *pnpi;
96
97         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
98         if (!pnetdev)
99                 goto RETURN;
100
101         pnpi = netdev_priv(pnetdev);
102         pnpi->priv = old_priv;
103         pnpi->sizeof_priv = sizeof_priv;
104
105 RETURN:
106         return pnetdev;
107 }
108
109 void rtw_free_netdev(struct net_device *netdev)
110 {
111         struct rtw_netdev_priv_indicator *pnpi;
112
113         if (!netdev)
114                 goto RETURN;
115
116         pnpi = netdev_priv(netdev);
117
118         if (!pnpi->priv)
119                 goto RETURN;
120
121         vfree(pnpi->priv);
122         free_netdev(netdev);
123
124 RETURN:
125         return;
126 }
127
128 u64 rtw_modular64(u64 x, u64 y)
129 {
130         return do_div(x, y);
131 }
132
133 void rtw_buf_free(u8 **buf, u32 *buf_len)
134 {
135         *buf_len = 0;
136         kfree(*buf);
137         *buf = NULL;
138 }
139
140 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
141 {
142         u32 ori_len = 0, dup_len = 0;
143         u8 *ori = NULL;
144         u8 *dup = NULL;
145
146         if (!buf || !buf_len)
147                 return;
148
149         if (!src || !src_len)
150                 goto keep_ori;
151
152         /* duplicate src */
153         dup = rtw_malloc(src_len);
154         if (dup) {
155                 dup_len = src_len;
156                 memcpy(dup, src, dup_len);
157         }
158
159 keep_ori:
160         ori = *buf;
161         ori_len = *buf_len;
162
163         /* replace buf with dup */
164         *buf_len = 0;
165         *buf = dup;
166         *buf_len = dup_len;
167
168         /* free ori */
169         kfree(ori);
170 }
171
172
173 /**
174  * rtw_cbuf_full - test if cbuf is full
175  * @cbuf: pointer of struct rtw_cbuf
176  *
177  * Returns: true if cbuf is full
178  */
179 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
180 {
181         return (cbuf->write == cbuf->read-1) ? true : false;
182 }
183
184 /**
185  * rtw_cbuf_empty - test if cbuf is empty
186  * @cbuf: pointer of struct rtw_cbuf
187  *
188  * Returns: true if cbuf is empty
189  */
190 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
191 {
192         return (cbuf->write == cbuf->read) ? true : false;
193 }
194
195 /**
196  * rtw_cbuf_push - push a pointer into cbuf
197  * @cbuf: pointer of struct rtw_cbuf
198  * @buf: pointer to push in
199  *
200  * Lock free operation, be careful of the use scheme
201  * Returns: true push success
202  */
203 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
204 {
205         if (rtw_cbuf_full(cbuf))
206                 return _FAIL;
207
208         if (0)
209                 DBG_88E("%s on %u\n", __func__, cbuf->write);
210         cbuf->bufs[cbuf->write] = buf;
211         cbuf->write = (cbuf->write+1)%cbuf->size;
212
213         return _SUCCESS;
214 }
215
216 /**
217  * rtw_cbuf_pop - pop a pointer from cbuf
218  * @cbuf: pointer of struct rtw_cbuf
219  *
220  * Lock free operation, be careful of the use scheme
221  * Returns: pointer popped out
222  */
223 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
224 {
225         void *buf;
226         if (rtw_cbuf_empty(cbuf))
227                 return NULL;
228
229         if (0)
230                 DBG_88E("%s on %u\n", __func__, cbuf->read);
231         buf = cbuf->bufs[cbuf->read];
232         cbuf->read = (cbuf->read+1)%cbuf->size;
233
234         return buf;
235 }
236
237 /**
238  * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
239  * @size: size of pointer
240  *
241  * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
242  */
243 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
244 {
245         struct rtw_cbuf *cbuf;
246
247         cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
248                sizeof(void *)*size);
249
250         if (cbuf) {
251                 cbuf->write = 0;
252                 cbuf->read = 0;
253                 cbuf->size = size;
254         }
255         return cbuf;
256 }