GNU Linux-libre 4.14.251-gnu1
[releases.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 #define _OSDEP_SERVICE_C_
16
17 #include <osdep_service.h>
18 #include <osdep_intf.h>
19 #include <drv_types.h>
20 #include <recv_osdep.h>
21 #include <linux/vmalloc.h>
22 #include <rtw_ioctl_set.h>
23
24 u8 *_rtw_malloc(u32 sz)
25 {
26         return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
27 }
28
29 void *rtw_malloc2d(int h, int w, int size)
30 {
31         int j;
32         void **a = kzalloc(h * sizeof(void *) + h * w * size, GFP_KERNEL);
33
34         if (!a)
35                 goto out;
36
37         for (j = 0; j < h; j++)
38                 a[j] = ((char *)(a + h)) + j * w * size;
39 out:
40         return a;
41 }
42
43 void _rtw_init_queue(struct __queue *pqueue)
44 {
45         INIT_LIST_HEAD(&pqueue->queue);
46         spin_lock_init(&pqueue->lock);
47 }
48
49 struct net_device *rtw_alloc_etherdev_with_old_priv(void *old_priv)
50 {
51         struct net_device *pnetdev;
52         struct rtw_netdev_priv_indicator *pnpi;
53
54         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
55         if (!pnetdev)
56                 goto RETURN;
57
58         pnpi = netdev_priv(pnetdev);
59         pnpi->priv = old_priv;
60
61 RETURN:
62         return pnetdev;
63 }
64
65 void rtw_free_netdev(struct net_device *netdev)
66 {
67         struct rtw_netdev_priv_indicator *pnpi;
68
69         if (!netdev)
70                 goto RETURN;
71
72         pnpi = netdev_priv(netdev);
73
74         if (!pnpi->priv)
75                 goto RETURN;
76
77         vfree(pnpi->priv);
78         free_netdev(netdev);
79
80 RETURN:
81         return;
82 }
83
84 u64 rtw_modular64(u64 x, u64 y)
85 {
86         return do_div(x, y);
87 }
88
89 void rtw_buf_free(u8 **buf, u32 *buf_len)
90 {
91         *buf_len = 0;
92         kfree(*buf);
93         *buf = NULL;
94 }
95
96 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
97 {
98         u32 dup_len = 0;
99         u8 *ori = NULL;
100         u8 *dup = NULL;
101
102         if (!buf || !buf_len)
103                 return;
104
105         if (!src || !src_len)
106                 goto keep_ori;
107
108         /* duplicate src */
109         dup = rtw_malloc(src_len);
110         if (dup) {
111                 dup_len = src_len;
112                 memcpy(dup, src, dup_len);
113         }
114
115 keep_ori:
116         ori = *buf;
117
118         /* replace buf with dup */
119         *buf_len = 0;
120         *buf = dup;
121         *buf_len = dup_len;
122
123         /* free ori */
124         kfree(ori);
125 }