1 /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __RING_BUFFER_H
30 #define __RING_BUFFER_H
31 
32 /* Ring buffer status codes */
33 enum rb_status {
34     RB_SUCCESS = 0,
35     RB_FAILURE = 1,
36     RB_FULL = 2,
37     RB_RETRY = 3,
38 };
39 
40 struct rb_stats {
41     u32 total_bytes_written;
42     u32 total_bytes_read;
43     u32 cur_valid_bytes;
44     unsigned int max_num_bufs;
45     size_t each_buf_size;
46 };
47 
48 typedef void (*threshold_call_back) (void *cb_ctx);
49 
50 /* intiitalizes the ring buffer and returns the context to it */
51 void * ring_buffer_init(size_t size_of_buf, int num_bufs);
52 
53 /* Frees up the mem allocated for this ring buffer operation */
54 void ring_buffer_deinit(void *ctx);
55 
56 /* Writes writes length of bytes from buf to ring buffer */
57 enum rb_status rb_write(void *ctx, u8 *buf, size_t length, int overwrite,
58                         size_t record_length);
59 
60 /* Tries to read max_length of bytes from ring buffer to buf
61  * and returns actual length of bytes read from ring buffer
62  */
63 size_t rb_read(void *ctx, u8 *buf, size_t max_length);
64 
65 /* A buffer with possible maximum of bytes that can be read
66  * from a single buffer of ring buffer
67  * Ring buffer module looses the ownership of the buffer returned by this api,
68  * which means the caller has to make sure to free the buffer returned.
69  */
70 u8 *rb_get_read_buf(void *ctx, size_t *length);
71 
72 /* calls callback whenever ring_buffer reaches percent percentage of it'ss
73  * full size
74  */
75 void rb_config_threshold(void *ctx,
76                          unsigned int num_min_bytes,
77                          threshold_call_back callback,
78                          void *cb_ctx);
79 
80 /* Get the current status of ring buffer */
81 void rb_get_stats(void *ctx, struct rb_stats *rbs);
82 
83 #endif /* __RING_BUFFER_H */
84