1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* Copyright (c) 2012,2014, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/wcnss_wlan.h>
#include <linux/spinlock.h>
static DEFINE_SPINLOCK(alloc_lock);
struct wcnss_prealloc {
int occupied;
unsigned int size;
void *ptr;
};
/* pre-alloced mem for WLAN driver */
static struct wcnss_prealloc wcnss_allocs[] = {
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 8 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 12 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 16 * 1024, NULL},
{0, 24 * 1024, NULL},
{0, 24 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 32 * 1024, NULL},
{0, 64 * 1024, NULL},
{0, 64 * 1024, NULL},
{0, 76 * 1024, NULL},
};
int wcnss_prealloc_init(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
wcnss_allocs[i].occupied = 0;
wcnss_allocs[i].ptr = kmalloc(wcnss_allocs[i].size, GFP_KERNEL);
if (wcnss_allocs[i].ptr == NULL)
return -ENOMEM;
}
return 0;
}
void wcnss_prealloc_deinit(void)
{
int i = 0;
for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
kfree(wcnss_allocs[i].ptr);
wcnss_allocs[i].ptr = NULL;
}
}
void *wcnss_prealloc_get(unsigned int size)
{
int i = 0;
unsigned long flags;
spin_lock_irqsave(&alloc_lock, flags);
for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
if (wcnss_allocs[i].occupied)
continue;
if (wcnss_allocs[i].size > size) {
/* we found the slot */
wcnss_allocs[i].occupied = 1;
spin_unlock_irqrestore(&alloc_lock, flags);
return wcnss_allocs[i].ptr;
}
}
spin_unlock_irqrestore(&alloc_lock, flags);
pr_err("wcnss: %s: prealloc not available for size: %d\n",
__func__, size);
return NULL;
}
EXPORT_SYMBOL(wcnss_prealloc_get);
int wcnss_prealloc_put(void *ptr)
{
int i = 0;
unsigned long flags;
spin_lock_irqsave(&alloc_lock, flags);
for (i = 0; i < ARRAY_SIZE(wcnss_allocs); i++) {
if (wcnss_allocs[i].ptr == ptr) {
wcnss_allocs[i].occupied = 0;
spin_unlock_irqrestore(&alloc_lock, flags);
return 1;
}
}
spin_unlock_irqrestore(&alloc_lock, flags);
return 0;
}
EXPORT_SYMBOL(wcnss_prealloc_put);
static int __init wcnss_pre_alloc_init(void)
{
return wcnss_prealloc_init();
}
static void __exit wcnss_pre_alloc_exit(void)
{
wcnss_prealloc_deinit();
}
module_init(wcnss_pre_alloc_init);
module_exit(wcnss_pre_alloc_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION(DEVICE "WCNSS Prealloc Driver");
|