forked from initdc/eswin_6600u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslave_log_buf.c
executable file
·149 lines (127 loc) · 4.06 KB
/
slave_log_buf.c
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
#include "slave_log_buf.h"
spinlock_t lock;
uint32_t ring_buffer_init(struct ring_buffer* ring_buf, uint32_t size)
{
if(ring_buf== NULL)
return false;
if (!is_power_of_2(size))
{
printk("size must be power of 2.\n");
return false;
}
memset(ring_buf, 0, sizeof(struct ring_buffer));
spin_lock_init(&lock);
ring_buf->buffer = kmalloc(size,GFP_KERNEL);
ring_buf->size = size;
ring_buf->write_point = 0;
ring_buf->read_point = 0;
ring_buf->f_lock = &lock;
ring_buf->init = true;
ring_buf->cover = false;
ring_buf->show = false;
return true;
}
void ring_buffer_deinit(struct ring_buffer *ring_buf)
{
memset(ring_buf, 0, sizeof(struct ring_buffer));
if(ring_buf->buffer != NULL)
{
kfree(ring_buf->buffer);
ring_buf->buffer = NULL;
}
}
uint32_t __ring_buffer_len(const struct ring_buffer *ring_buf)
{
if(ring_buf->cover == false)
{
return ring_buf->write_point;
}
if(ring_buf->show == true)
{
if(ring_buf->write_point < ring_buf->read_point)
return (ring_buf->write_point + ring_buf->size - ring_buf->read_point);
else
return (ring_buf->write_point - ring_buf->read_point);
}
return ring_buf->size;
}
uint32_t __ring_buffer_get(struct ring_buffer *ring_buf, void * buffer, uint32_t size)
{
if((ring_buf== NULL) || (buffer== NULL))
return 0;
uint32_t copy_len = 0;
uint32_t read_len = 0;
if(ring_buf->write_point < ring_buf->read_point)
read_len = (ring_buf->write_point + ring_buf->size - ring_buf->read_point);
else
read_len = (ring_buf->write_point - ring_buf->read_point);
size = min(size, read_len);
/* first get the data from fifo->read_point until the end of the buffer */
copy_len = min(size, ring_buf->size - ring_buf->read_point);
memcpy(buffer, ring_buf->buffer + ring_buf->read_point, copy_len);
/* then get the rest (if any) from the beginning of the buffer */
if(size - copy_len > 0)
{
memcpy(buffer + copy_len, ring_buf->buffer, size - copy_len);
}
ring_buf->read_point += size;
ring_buf->read_point = (ring_buf->read_point & (ring_buf->size - 1));
return size;
}
//Ïò»º³åÇøÖдæ·ÅÊý¾Ý
uint32_t __ring_buffer_put(struct ring_buffer *ring_buf, void *buffer, uint32_t size)
{
if((ring_buf == NULL) || (buffer == NULL))
{
return 0;
}
uint32_t copy_len = 0;
/* first put the data starting from fifo->write_point to buffer end */
copy_len = min(size, ring_buf->size - ring_buf->write_point);
memcpy(ring_buf->buffer + ring_buf->write_point, buffer, copy_len);
/* then put the rest (if any) at the beginning of the buffer */
if(size - copy_len > 0)
{
memcpy(ring_buf->buffer, buffer + copy_len, size - copy_len);
ring_buf->cover = true;
}
ring_buf->write_point += size;
ring_buf->write_point = (ring_buf->write_point & (ring_buf->size - 1));
return size;
}
uint32_t ring_buffer_len(const struct ring_buffer *ring_buf)
{
uint32_t len = 0;
spin_lock_irq(ring_buf->f_lock);
len = __ring_buffer_len(ring_buf);
spin_unlock_irq(ring_buf->f_lock);
return len;
}
uint32_t ring_buffer_get(struct ring_buffer *ring_buf, void *buffer, uint32_t size)
{
uint32_t ret;
spin_lock_irq(ring_buf->f_lock);
ret = __ring_buffer_get(ring_buf, buffer, size);
spin_unlock_irq(ring_buf->f_lock);
return ret;
}
uint32_t ring_buffer_put(struct ring_buffer *ring_buf, void *buffer, uint32_t size)
{
uint32_t ret;
spin_lock_irq(ring_buf->f_lock);
ret = __ring_buffer_put(ring_buf, buffer, size);
spin_unlock_irq(ring_buf->f_lock);
return ret;
}
uint32_t ring_buffer_scrolling_display(struct ring_buffer *ring_buf, char show)
{
uint32_t ret = true;
spin_lock_irq(ring_buf->f_lock);
ring_buf->show = show;
if((ring_buf->cover == true)&&(show == true))
{
ring_buf->read_point = (ring_buf->write_point & (ring_buf->size - 1)) + 1;
}
spin_unlock_irq(ring_buf->f_lock);
return ret;
}