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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* Copyright (C) 2012-2017 Samsung Electronics, Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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/err.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "lib/circ_buf.h"
struct circ_buf_desc *circ_buf_create(unsigned long size)
{
struct circ_buf *circ_buf;
struct circ_buf_desc *circ_buf_desc;
circ_buf_desc = circ_buf_desc_alloc();
if (!circ_buf_desc)
return NULL;
circ_buf = circ_buf_alloc(size);
if (!circ_buf) {
circ_buf_desc_free(circ_buf_desc);
return NULL;
}
circ_buf_connect(circ_buf_desc, circ_buf, size);
return circ_buf_desc;
}
void circ_buf_destroy(struct circ_buf_desc *circ_buf_desc)
{
circ_buf_free(circ_buf_desc->circ_buf);
circ_buf_desc_free(circ_buf_desc);
}
struct circ_buf_desc *circ_buf_desc_alloc(void)
{
struct circ_buf_desc *circ_buf_desc;
circ_buf_desc = kmalloc(sizeof(struct circ_buf_desc), GFP_KERNEL);
if (!circ_buf_desc)
return NULL;
circ_buf_desc->circ_buf = NULL;
circ_buf_desc->write_count = 0;
circ_buf_desc->read_count = 0;
circ_buf_desc->size = 0;
return circ_buf_desc;
}
void circ_buf_desc_free(struct circ_buf_desc *circ_buf_desc)
{
kfree(circ_buf_desc);
}
struct circ_buf *circ_buf_alloc(unsigned long size)
{
struct circ_buf *circ_buf;
circ_buf = kmalloc(size + CIRC_BUF_META_SIZE, GFP_KERNEL);
if (!circ_buf)
return NULL;
circ_buf_init(circ_buf);
return circ_buf;
}
struct circ_buf *circ_buf_set(void *buf)
{
struct circ_buf *circ_buf = buf;
circ_buf_init(circ_buf);
return circ_buf;
}
void circ_buf_free(struct circ_buf *circ_buf)
{
kfree(circ_buf);
}
void circ_buf_init(struct circ_buf *circ_buf)
{
circ_buf->write_count = 0;
circ_buf->read_count = 0;
}
void circ_buf_connect(struct circ_buf_desc *circ_buf_desc,
struct circ_buf *circ_buf, unsigned long size)
{
circ_buf_desc->circ_buf = circ_buf;
circ_buf_desc->read_count = circ_buf->read_count;
circ_buf_desc->write_count = circ_buf->write_count;
circ_buf_desc->size = size;
}
ssize_t circ_buf_write(struct circ_buf_desc *circ_buf_desc, const char *buf,
size_t length, enum circ_buf_user_mode mode)
{
ssize_t ret;
ret = circ_buf_write_local(circ_buf_desc, buf, length, mode);
if (!IS_ERR_VALUE(ret))
circ_buf_flush_write(circ_buf_desc);
return ret;
}
static int circ_buf_write_contig(struct circ_buf_desc *circ_buf_desc,
const char *buf, size_t length, enum circ_buf_user_mode mode)
{
int ret = 0;
struct circ_buf *circ_buf = circ_buf_desc->circ_buf;
switch (mode) {
case CIRC_BUF_MODE_KERNEL:
memcpy(circ_buf->buffer + circ_buf_desc->write_count, buf, length);
break;
case CIRC_BUF_MODE_USER:
if (copy_from_user(circ_buf->buffer + circ_buf_desc->write_count,
buf, length))
ret = -EFAULT;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static unsigned int circ_buf_is_count_valid(struct circ_buf_desc *circ_buf_desc,
unsigned int count)
{
return count <= circ_buf_desc->size;
}
ssize_t circ_buf_write_local(struct circ_buf_desc *circ_buf_desc, const char *buf,
size_t length, enum circ_buf_user_mode mode)
{
size_t avail, cur = 0;
unsigned int size = circ_buf_desc->size;
unsigned int end = circ_buf_desc->size + 1;
unsigned int new_count;
ssize_t ret;
size_t cur_length = length;
size_t bytes_used;
unsigned int write_count_orig = circ_buf_desc->write_count;
unsigned int write_count_shared = circ_buf_desc->circ_buf->write_count;
unsigned int read_count_shared = circ_buf_desc->circ_buf->read_count;
if (mode == CIRC_BUF_MODE_DROP)
return -EINVAL;
if (!circ_buf_is_count_valid(circ_buf_desc, write_count_shared)
|| !circ_buf_is_count_valid(circ_buf_desc, read_count_shared))
return -EIO;
circ_buf_desc->read_count = read_count_shared;
if (circ_buf_desc->write_count >= circ_buf_desc->read_count) {
avail = size - (circ_buf_desc->write_count - circ_buf_desc->read_count);
bytes_used = write_count_shared - circ_buf_desc->read_count;
} else {
avail = circ_buf_desc->read_count - circ_buf_desc->write_count - 1;
bytes_used = end - circ_buf_desc->read_count + write_count_shared;
}
if (avail + bytes_used < cur_length) {
ret = -EMSGSIZE;
goto fail;
}
if (avail < cur_length) {
ret = -EAGAIN;
goto fail;
}
if (circ_buf_desc->write_count >= circ_buf_desc->read_count) {
avail = end - circ_buf_desc->write_count - (circ_buf_desc->read_count ? 0 : 1);
cur = min(cur_length, avail);
ret = circ_buf_write_contig(circ_buf_desc, buf, cur, mode);
if (ret)
goto fail;
new_count = circ_buf_desc->write_count + cur;
circ_buf_desc->write_count = (new_count == end ? 0 : new_count);
buf += cur;
cur_length -= cur;
}
ret = circ_buf_write_contig(circ_buf_desc, buf, cur_length, mode);
if (ret)
goto fail;
circ_buf_desc->write_count += cur_length;
return length;
fail:
circ_buf_desc->write_count = write_count_orig;
return ret;
}
ssize_t circ_buf_read(struct circ_buf_desc *circ_buf_desc, char *buf,
size_t length, enum circ_buf_user_mode mode)
{
ssize_t ret;
ret = circ_buf_read_local(circ_buf_desc, buf, length, mode);
if (!IS_ERR_VALUE(ret))
circ_buf_flush_read(circ_buf_desc);
return ret;
}
ssize_t circ_buf_read_local(struct circ_buf_desc *circ_buf_desc, char *buf,
size_t length, enum circ_buf_user_mode mode)
{
return __circ_buf_read_local(circ_buf_desc, buf, length, 0, mode);
}
static int circ_buf_read_contig(struct circ_buf_desc *circ_buf_desc,
char *buf, size_t length, enum circ_buf_user_mode mode)
{
int ret = 0;
struct circ_buf *circ_buf = circ_buf_desc->circ_buf;
switch(mode) {
case CIRC_BUF_MODE_KERNEL:
memcpy(buf, circ_buf->buffer + circ_buf_desc->read_count, length);
break;
case CIRC_BUF_MODE_USER:
if (copy_to_user(buf, circ_buf->buffer + circ_buf_desc->read_count, length))
ret = -EFAULT;
break;
case CIRC_BUF_MODE_DROP:
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
ssize_t __circ_buf_read_local(struct circ_buf_desc *circ_buf_desc, char *buf,
size_t length, size_t offset, enum circ_buf_user_mode mode)
{
size_t avail, cur = 0;
unsigned int end = circ_buf_desc->size + 1;
unsigned int new_count;
ssize_t ret;
size_t cur_length = length;
size_t bytes_free;
unsigned int read_count_orig = circ_buf_desc->read_count;
unsigned int write_count_shared = circ_buf_desc->circ_buf->write_count;
unsigned int read_count_shared = circ_buf_desc->circ_buf->read_count;
if (!circ_buf_is_count_valid(circ_buf_desc, write_count_shared)
|| !circ_buf_is_count_valid(circ_buf_desc, read_count_shared))
return -EIO;
circ_buf_desc->write_count = write_count_shared;
circ_buf_desc->read_count += offset;
if (circ_buf_desc->write_count >= circ_buf_desc->read_count) {
avail = circ_buf_desc->write_count - circ_buf_desc->read_count;
bytes_free = circ_buf_desc->size - circ_buf_desc->write_count
+ read_count_shared;
} else {
avail = end - (circ_buf_desc->read_count - circ_buf_desc->write_count);
bytes_free = read_count_shared - circ_buf_desc->write_count - 1;
}
if (avail + bytes_free < cur_length) {
ret = -EMSGSIZE;
goto fail;
}
if (avail < cur_length) {
ret = -EAGAIN;
goto fail;
}
smp_rmb();
if (circ_buf_desc->write_count < circ_buf_desc->read_count) {
avail = end - circ_buf_desc->read_count;
cur = min(cur_length, avail);
ret = circ_buf_read_contig(circ_buf_desc, buf, cur, mode);
if (ret)
goto fail;
new_count = circ_buf_desc->read_count + cur;
circ_buf_desc->read_count = (new_count == end ? 0 : new_count);
buf += cur;
cur_length -= cur;
}
ret = circ_buf_read_contig(circ_buf_desc, buf, cur_length, mode);
if (ret)
goto fail;
circ_buf_desc->read_count += cur_length;
return length;
fail:
circ_buf_desc->read_count = read_count_orig;
return ret;
}
void circ_buf_flush_write(struct circ_buf_desc *circ_buf_desc)
{
smp_wmb();
circ_buf_desc->circ_buf->write_count = circ_buf_desc->write_count;
}
void circ_buf_flush_read(struct circ_buf_desc *circ_buf_desc)
{
circ_buf_desc->circ_buf->read_count = circ_buf_desc->read_count;
}
void circ_buf_rollback_write(struct circ_buf_desc *circ_buf_desc)
{
circ_buf_desc->write_count = circ_buf_desc->circ_buf->write_count;
}
void circ_buf_rollback_read(struct circ_buf_desc *circ_buf_desc)
{
circ_buf_desc->read_count = circ_buf_desc->circ_buf->read_count;
}
unsigned int circ_buf_is_empty(struct circ_buf_desc *circ_buf_desc)
{
return circ_buf_bytes_used(circ_buf_desc) == 0;
}
unsigned int circ_buf_is_full(struct circ_buf_desc *circ_buf_desc)
{
return circ_buf_bytes_used(circ_buf_desc) == circ_buf_desc->size;
}
size_t circ_buf_bytes_free(struct circ_buf_desc *circ_buf_desc)
{
return circ_buf_desc->size - circ_buf_bytes_used(circ_buf_desc);
}
size_t circ_buf_bytes_used(struct circ_buf_desc *circ_buf_desc)
{
unsigned int write_count = circ_buf_desc->circ_buf->write_count;
unsigned int read_count = circ_buf_desc->circ_buf->read_count;
if (write_count >= read_count)
return write_count - read_count;
return circ_buf_desc->size + 1 - read_count + write_count;
}
|