temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / misc / ts27010mux / ts27010_ringbuf.h
1 /*
2  * simple ring buffer
3  *
4  * supports a concurrent reader and writer without locking
5  */
6
7
8 struct ts27010_ringbuf {
9         int len;
10         int head;
11         int tail;
12         u8 buf[];
13 };
14
15
16 static inline struct ts27010_ringbuf *ts27010_ringbuf_alloc(int len)
17 {
18         struct ts27010_ringbuf *rbuf;
19
20         rbuf = kzalloc(sizeof(*rbuf) + len, GFP_KERNEL);
21         if (rbuf == NULL)
22                 return NULL;
23
24         rbuf->len = len;
25         rbuf->head = 0;
26         rbuf->tail = 0;
27
28         return rbuf;
29 }
30
31 static inline void ts27010_ringbuf_free(struct ts27010_ringbuf *rbuf)
32 {
33         kfree(rbuf);
34 }
35
36 static inline int ts27010_ringbuf_level(struct ts27010_ringbuf *rbuf)
37 {
38         int level = rbuf->head - rbuf->tail;
39
40         if (level < 0)
41                 level = rbuf->len + level;
42
43         return level;
44 }
45
46 static inline int ts27010_ringbuf_room(struct ts27010_ringbuf *rbuf)
47 {
48         return rbuf->len - ts27010_ringbuf_level(rbuf) - 1;
49 }
50
51 static inline u8 ts27010_ringbuf_peek(struct ts27010_ringbuf *rbuf, int i)
52 {
53         return rbuf->buf[(rbuf->tail + i) % rbuf->len];
54 }
55
56 static inline int ts27010_ringbuf_consume(struct ts27010_ringbuf *rbuf,
57                                           int count)
58 {
59         count = min(count, ts27010_ringbuf_level(rbuf));
60
61         rbuf->tail = (rbuf->tail + count) % rbuf->len;
62
63         return count;
64 }
65
66 static inline int ts27010_ringbuf_push(struct ts27010_ringbuf *rbuf, u8 datum)
67 {
68         if (ts27010_ringbuf_room(rbuf) == 0)
69                 return 0;
70
71         rbuf->buf[rbuf->head] = datum;
72         rbuf->head = (rbuf->head + 1) % rbuf->len;
73
74         return 1;
75 }
76
77 static inline int ts27010_ringbuf_write(struct ts27010_ringbuf *rbuf,
78                                         const u8 *data, int len)
79 {
80         int count = 0;
81         int i;
82
83         for (i = 0; i < len; i++)
84                 count += ts27010_ringbuf_push(rbuf, data[i]);
85
86         return count;
87 }
88
89