]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
d87b54711c0d52d3eebfd563b798c2733d99c572
[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 = kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
55         if (!a) {
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 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
81                                                     void *old_priv)
82 {
83         struct net_device *pnetdev;
84         struct rtw_netdev_priv_indicator *pnpi;
85
86         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
87         if (!pnetdev)
88                 goto RETURN;
89
90         pnpi = netdev_priv(pnetdev);
91         pnpi->priv = old_priv;
92         pnpi->sizeof_priv = sizeof_priv;
93
94 RETURN:
95         return pnetdev;
96 }
97
98 void rtw_free_netdev(struct net_device *netdev)
99 {
100         struct rtw_netdev_priv_indicator *pnpi;
101
102         if (!netdev)
103                 goto RETURN;
104
105         pnpi = netdev_priv(netdev);
106
107         if (!pnpi->priv)
108                 goto RETURN;
109
110         vfree(pnpi->priv);
111         free_netdev(netdev);
112
113 RETURN:
114         return;
115 }
116
117 u64 rtw_modular64(u64 x, u64 y)
118 {
119         return do_div(x, y);
120 }
121
122 void rtw_buf_free(u8 **buf, u32 *buf_len)
123 {
124         *buf_len = 0;
125         kfree(*buf);
126         *buf = NULL;
127 }
128
129 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
130 {
131         u32 dup_len = 0;
132         u8 *ori = NULL;
133         u8 *dup = NULL;
134
135         if (!buf || !buf_len)
136                 return;
137
138         if (!src || !src_len)
139                 goto keep_ori;
140
141         /* duplicate src */
142         dup = rtw_malloc(src_len);
143         if (dup) {
144                 dup_len = src_len;
145                 memcpy(dup, src, dup_len);
146         }
147
148 keep_ori:
149         ori = *buf;
150
151         /* replace buf with dup */
152         *buf_len = 0;
153         *buf = dup;
154         *buf_len = dup_len;
155
156         /* free ori */
157         kfree(ori);
158 }