]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
Merge tag 'vfio-v4.7-rc1' of git://github.com/awilliam/linux-vfio
[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  ******************************************************************************/
15
16
17 #define _OSDEP_SERVICE_C_
18
19 #include <osdep_service.h>
20 #include <osdep_intf.h>
21 #include <drv_types.h>
22 #include <recv_osdep.h>
23 #include <linux/vmalloc.h>
24 #include <rtw_ioctl_set.h>
25
26 /*
27 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
28 * @return: one of RTW_STATUS_CODE
29 */
30 inline int RTW_STATUS_CODE(int error_code)
31 {
32         if (error_code >= 0)
33                 return _SUCCESS;
34         return _FAIL;
35 }
36
37 u8 *_rtw_malloc(u32 sz)
38 {
39         return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
40 }
41
42 void *rtw_malloc2d(int h, int w, int size)
43 {
44         int j;
45
46         void **a = kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
47         if (!a) {
48                 pr_info("%s: alloc memory fail!\n", __func__);
49                 return NULL;
50         }
51
52         for (j = 0; j < h; j++)
53                 a[j] = ((char *)(a+h)) + j*w*size;
54
55         return a;
56 }
57
58 u32 _rtw_down_sema(struct semaphore *sema)
59 {
60         if (down_interruptible(sema))
61                 return _FAIL;
62         return _SUCCESS;
63 }
64
65 void    _rtw_init_queue(struct __queue *pqueue)
66 {
67         INIT_LIST_HEAD(&(pqueue->queue));
68         spin_lock_init(&(pqueue->lock));
69 }
70
71 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
72                                                     void *old_priv)
73 {
74         struct net_device *pnetdev;
75         struct rtw_netdev_priv_indicator *pnpi;
76
77         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
78         if (!pnetdev)
79                 goto RETURN;
80
81         pnpi = netdev_priv(pnetdev);
82         pnpi->priv = old_priv;
83         pnpi->sizeof_priv = sizeof_priv;
84
85 RETURN:
86         return pnetdev;
87 }
88
89 void rtw_free_netdev(struct net_device *netdev)
90 {
91         struct rtw_netdev_priv_indicator *pnpi;
92
93         if (!netdev)
94                 goto RETURN;
95
96         pnpi = netdev_priv(netdev);
97
98         if (!pnpi->priv)
99                 goto RETURN;
100
101         vfree(pnpi->priv);
102         free_netdev(netdev);
103
104 RETURN:
105         return;
106 }
107
108 u64 rtw_modular64(u64 x, u64 y)
109 {
110         return do_div(x, y);
111 }
112
113 void rtw_buf_free(u8 **buf, u32 *buf_len)
114 {
115         *buf_len = 0;
116         kfree(*buf);
117         *buf = NULL;
118 }
119
120 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
121 {
122         u32 dup_len = 0;
123         u8 *ori = NULL;
124         u8 *dup = NULL;
125
126         if (!buf || !buf_len)
127                 return;
128
129         if (!src || !src_len)
130                 goto keep_ori;
131
132         /* duplicate src */
133         dup = rtw_malloc(src_len);
134         if (dup) {
135                 dup_len = src_len;
136                 memcpy(dup, src, dup_len);
137         }
138
139 keep_ori:
140         ori = *buf;
141
142         /* replace buf with dup */
143         *buf_len = 0;
144         *buf = dup;
145         *buf_len = dup_len;
146
147         /* free ori */
148         kfree(ori);
149 }