1
2
3
4
5
6
7
8
9
10
11
12#include <linux/slab.h>
13
14#include "midibuf.h"
15
16static int midibuf_message_length(unsigned char code)
17{
18 int message_length;
19
20 if (code < 0x80)
21 message_length = -1;
22 else if (code < 0xf0) {
23 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
24
25 message_length = length[(code >> 4) - 8];
26 } else {
27
28
29
30
31
32 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
33 1, 1, 1, -1, 1, 1
34 };
35 message_length = length[code & 0x0f];
36 }
37
38 return message_length;
39}
40
41static int midibuf_is_empty(struct midi_buffer *this)
42{
43 return (this->pos_read == this->pos_write) && !this->full;
44}
45
46static int midibuf_is_full(struct midi_buffer *this)
47{
48 return this->full;
49}
50
51void line6_midibuf_reset(struct midi_buffer *this)
52{
53 this->pos_read = this->pos_write = this->full = 0;
54 this->command_prev = -1;
55}
56
57int line6_midibuf_init(struct midi_buffer *this, int size, int split)
58{
59 this->buf = kmalloc(size, GFP_KERNEL);
60
61 if (this->buf == NULL)
62 return -ENOMEM;
63
64 this->size = size;
65 this->split = split;
66 line6_midibuf_reset(this);
67 return 0;
68}
69
70int line6_midibuf_bytes_free(struct midi_buffer *this)
71{
72 return
73 midibuf_is_full(this) ?
74 0 :
75 (this->pos_read - this->pos_write + this->size - 1) % this->size +
76 1;
77}
78
79int line6_midibuf_bytes_used(struct midi_buffer *this)
80{
81 return
82 midibuf_is_empty(this) ?
83 0 :
84 (this->pos_write - this->pos_read + this->size - 1) % this->size +
85 1;
86}
87
88int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
89 int length)
90{
91 int bytes_free;
92 int length1, length2;
93 int skip_active_sense = 0;
94
95 if (midibuf_is_full(this) || (length <= 0))
96 return 0;
97
98
99 if (data[length - 1] == 0xfe) {
100 --length;
101 skip_active_sense = 1;
102 }
103
104 bytes_free = line6_midibuf_bytes_free(this);
105
106 if (length > bytes_free)
107 length = bytes_free;
108
109 if (length > 0) {
110 length1 = this->size - this->pos_write;
111
112 if (length < length1) {
113
114 memcpy(this->buf + this->pos_write, data, length);
115 this->pos_write += length;
116 } else {
117
118 length2 = length - length1;
119 memcpy(this->buf + this->pos_write, data, length1);
120 memcpy(this->buf, data + length1, length2);
121 this->pos_write = length2;
122 }
123
124 if (this->pos_write == this->pos_read)
125 this->full = 1;
126 }
127
128 return length + skip_active_sense;
129}
130
131int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
132 int length)
133{
134 int bytes_used;
135 int length1, length2;
136 int command;
137 int midi_length;
138 int repeat = 0;
139 int i;
140
141
142 if (length < 3)
143 return -EINVAL;
144
145 if (midibuf_is_empty(this))
146 return 0;
147
148 bytes_used = line6_midibuf_bytes_used(this);
149
150 if (length > bytes_used)
151 length = bytes_used;
152
153 length1 = this->size - this->pos_read;
154
155
156 command = this->buf[this->pos_read];
157
158 if (command & 0x80) {
159 midi_length = midibuf_message_length(command);
160 this->command_prev = command;
161 } else {
162 if (this->command_prev > 0) {
163 int midi_length_prev =
164 midibuf_message_length(this->command_prev);
165
166 if (midi_length_prev > 0) {
167 midi_length = midi_length_prev - 1;
168 repeat = 1;
169 } else
170 midi_length = -1;
171 } else
172 midi_length = -1;
173 }
174
175 if (midi_length < 0) {
176
177 if (length < length1) {
178
179 for (i = 1; i < length; ++i)
180 if (this->buf[this->pos_read + i] & 0x80)
181 break;
182
183 midi_length = i;
184 } else {
185
186 length2 = length - length1;
187
188 for (i = 1; i < length1; ++i)
189 if (this->buf[this->pos_read + i] & 0x80)
190 break;
191
192 if (i < length1)
193 midi_length = i;
194 else {
195 for (i = 0; i < length2; ++i)
196 if (this->buf[i] & 0x80)
197 break;
198
199 midi_length = length1 + i;
200 }
201 }
202
203 if (midi_length == length)
204 midi_length = -1;
205 }
206
207 if (midi_length < 0) {
208 if (!this->split)
209 return 0;
210 } else {
211 if (length < midi_length)
212 return 0;
213
214 length = midi_length;
215 }
216
217 if (length < length1) {
218
219 memcpy(data + repeat, this->buf + this->pos_read, length);
220 this->pos_read += length;
221 } else {
222
223 length2 = length - length1;
224 memcpy(data + repeat, this->buf + this->pos_read, length1);
225 memcpy(data + repeat + length1, this->buf, length2);
226 this->pos_read = length2;
227 }
228
229 if (repeat)
230 data[0] = this->command_prev;
231
232 this->full = 0;
233 return length + repeat;
234}
235
236int line6_midibuf_ignore(struct midi_buffer *this, int length)
237{
238 int bytes_used = line6_midibuf_bytes_used(this);
239
240 if (length > bytes_used)
241 length = bytes_used;
242
243 this->pos_read = (this->pos_read + length) % this->size;
244 this->full = 0;
245 return length;
246}
247
248void line6_midibuf_destroy(struct midi_buffer *this)
249{
250 kfree(this->buf);
251 this->buf = NULL;
252}
253