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 <osdep_intf.h>
26 #include <drv_types.h>
27 #include <recv_osdep.h>
28 #include <linux/vmalloc.h>
29 #include <rtw_ioctl_set.h>
32 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
33 * @return: one of RTW_STATUS_CODE
35 inline int RTW_STATUS_CODE(int error_code)
42 u8 *_rtw_malloc(u32 sz)
46 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
50 void *rtw_malloc2d(int h, int w, int size)
54 void **a = (void **)kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
56 pr_info("%s: alloc memory fail!\n", __func__);
60 for (j = 0; j < h; j++)
61 a[j] = ((char *)(a+h)) + j*w*size;
66 u32 _rtw_down_sema(struct semaphore *sema)
68 if (down_interruptible(sema))
74 void _rtw_init_queue(struct __queue *pqueue)
76 INIT_LIST_HEAD(&(pqueue->queue));
77 spin_lock_init(&(pqueue->lock));
80 inline u32 rtw_ms_to_systime(u32 ms)
82 return ms * HZ / 1000;
85 /* the input parameter start must be in jiffies */
86 inline s32 rtw_get_passing_time_ms(u32 start)
88 return jiffies_to_msecs(jiffies-start);
91 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
94 struct net_device *pnetdev;
95 struct rtw_netdev_priv_indicator *pnpi;
97 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
101 pnpi = netdev_priv(pnetdev);
102 pnpi->priv = old_priv;
103 pnpi->sizeof_priv = sizeof_priv;
109 void rtw_free_netdev(struct net_device *netdev)
111 struct rtw_netdev_priv_indicator *pnpi;
116 pnpi = netdev_priv(netdev);
128 u64 rtw_modular64(u64 x, u64 y)
133 void rtw_buf_free(u8 **buf, u32 *buf_len)
140 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
142 u32 ori_len = 0, dup_len = 0;
146 if (!buf || !buf_len)
149 if (!src || !src_len)
153 dup = rtw_malloc(src_len);
156 memcpy(dup, src, dup_len);
163 /* replace buf with dup */
174 * rtw_cbuf_full - test if cbuf is full
175 * @cbuf: pointer of struct rtw_cbuf
177 * Returns: true if cbuf is full
179 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
181 return (cbuf->write == cbuf->read-1) ? true : false;
185 * rtw_cbuf_empty - test if cbuf is empty
186 * @cbuf: pointer of struct rtw_cbuf
188 * Returns: true if cbuf is empty
190 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
192 return (cbuf->write == cbuf->read) ? true : false;
196 * rtw_cbuf_push - push a pointer into cbuf
197 * @cbuf: pointer of struct rtw_cbuf
198 * @buf: pointer to push in
200 * Lock free operation, be careful of the use scheme
201 * Returns: true push success
203 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
205 if (rtw_cbuf_full(cbuf))
209 DBG_88E("%s on %u\n", __func__, cbuf->write);
210 cbuf->bufs[cbuf->write] = buf;
211 cbuf->write = (cbuf->write+1)%cbuf->size;
217 * rtw_cbuf_pop - pop a pointer from cbuf
218 * @cbuf: pointer of struct rtw_cbuf
220 * Lock free operation, be careful of the use scheme
221 * Returns: pointer popped out
223 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
226 if (rtw_cbuf_empty(cbuf))
230 DBG_88E("%s on %u\n", __func__, cbuf->read);
231 buf = cbuf->bufs[cbuf->read];
232 cbuf->read = (cbuf->read+1)%cbuf->size;
238 * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
239 * @size: size of pointer
241 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
243 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
245 struct rtw_cbuf *cbuf;
247 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
248 sizeof(void *)*size);