12 #include <linux/slab.h>
16 static int midibuf_message_length(
unsigned char code)
20 else if (code < 0xf0) {
21 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
22 return length[(code >> 4) - 8];
29 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
32 return length[code & 0x0f];
36 static int midibuf_is_empty(
struct MidiBuffer *
this)
38 return (this->pos_read == this->pos_write) && !this->
full;
41 static int midibuf_is_full(
struct MidiBuffer *
this)
48 this->pos_read = this->pos_write = this->
full = 0;
49 this->command_prev = -1;
67 pr_debug(
"midibuf size=%d split=%d pos_read=%d pos_write=%d "
68 "full=%d command_prev=%02x\n", this->
size, this->
split,
69 this->pos_read, this->pos_write, this->
full, this->command_prev);
75 midibuf_is_full(
this) ?
77 (this->pos_read - this->pos_write + this->
size - 1) % this->
size +
84 midibuf_is_empty(
this) ?
86 (this->pos_write - this->pos_read + this->
size - 1) % this->
size +
95 int skip_active_sense = 0;
97 if (midibuf_is_full(
this) || (length <= 0))
101 if (data[length - 1] == 0xfe) {
103 skip_active_sense = 1;
108 if (length > bytes_free)
112 length1 = this->
size - this->pos_write;
114 if (length < length1) {
116 memcpy(this->
buf + this->pos_write, data, length);
117 this->pos_write +=
length;
120 length2 = length - length1;
121 memcpy(this->
buf + this->pos_write, data, length1);
122 memcpy(this->
buf, data + length1, length2);
123 this->pos_write = length2;
126 if (this->pos_write == this->pos_read)
130 return length + skip_active_sense;
136 int length1, length2;
146 if (midibuf_is_empty(
this))
151 if (length > bytes_used)
154 length1 = this->
size - this->pos_read;
157 command = this->
buf[this->pos_read];
159 if (command & 0x80) {
160 midi_length = midibuf_message_length(command);
163 if (this->command_prev > 0) {
164 int midi_length_prev =
165 midibuf_message_length(this->command_prev);
167 if (midi_length_prev > 0) {
168 midi_length = midi_length_prev - 1;
176 if (midi_length < 0) {
178 if (length < length1) {
181 if (this->
buf[this->pos_read + i] & 0x80)
187 length2 = length - length1;
189 for (i = 1; i < length1; ++
i)
190 if (this->
buf[this->pos_read + i] & 0x80)
196 for (i = 0; i < length2; ++
i)
197 if (this->
buf[i] & 0x80)
200 midi_length = length1 +
i;
204 if (midi_length == length)
208 if (midi_length < 0) {
212 if (length < midi_length)
215 length = midi_length;
218 if (length < length1) {
220 memcpy(data + repeat, this->
buf + this->pos_read, length);
224 length2 = length - length1;
225 memcpy(data + repeat, this->
buf + this->pos_read, length1);
226 memcpy(data + repeat + length1, this->
buf, length2);
227 this->pos_read = length2;
231 data[0] = this->command_prev;
234 return length + repeat;
241 if (length > bytes_used)
244 this->pos_read = (this->pos_read +
length) % this->
size;
251 int cmd = this->command_prev;
253 if ((cmd >= 0x80) && (cmd < 0xf0))
254 if ((mask & (1 << (cmd & 0x0f))) == 0)