Commit | Line | Data |
---|---|---|
705ececd | 1 | /* |
c078a4aa | 2 | * Line 6 Linux USB driver |
705ececd | 3 | * |
1027f476 | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
705ececd MG |
5 | * |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2. | |
9 | * | |
10 | */ | |
11 | ||
705ececd MG |
12 | #include <linux/slab.h> |
13 | ||
14 | #include "midibuf.h" | |
15 | ||
b702ed25 | 16 | static int midibuf_message_length(unsigned char code) |
705ececd | 17 | { |
d1d1a9d3 DT |
18 | int message_length; |
19 | ||
ce9b490c | 20 | if (code < 0x80) |
d1d1a9d3 | 21 | message_length = -1; |
ce9b490c | 22 | else if (code < 0xf0) { |
705ececd | 23 | static const int length[] = { 3, 3, 3, 3, 2, 2, 3 }; |
d1d1a9d3 DT |
24 | |
25 | message_length = length[(code >> 4) - 8]; | |
ce9b490c | 26 | } else { |
705ececd | 27 | /* |
e1a164d7 | 28 | Note that according to the MIDI specification 0xf2 is |
c6fffce9 | 29 | the "Song Position Pointer", but this is used by Line 6 |
e1a164d7 MG |
30 | to send sysex messages to the host. |
31 | */ | |
ce9b490c | 32 | static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1, |
e1a164d7 MG |
33 | 1, 1, 1, -1, 1, 1 |
34 | }; | |
d1d1a9d3 | 35 | message_length = length[code & 0x0f]; |
705ececd | 36 | } |
d1d1a9d3 DT |
37 | |
38 | return message_length; | |
705ececd MG |
39 | } |
40 | ||
269edc8e | 41 | static int midibuf_is_empty(struct midi_buffer *this) |
1027f476 MG |
42 | { |
43 | return (this->pos_read == this->pos_write) && !this->full; | |
44 | } | |
45 | ||
269edc8e | 46 | static int midibuf_is_full(struct midi_buffer *this) |
1027f476 MG |
47 | { |
48 | return this->full; | |
49 | } | |
50 | ||
269edc8e | 51 | void line6_midibuf_reset(struct midi_buffer *this) |
705ececd MG |
52 | { |
53 | this->pos_read = this->pos_write = this->full = 0; | |
54 | this->command_prev = -1; | |
55 | } | |
56 | ||
269edc8e | 57 | int line6_midibuf_init(struct midi_buffer *this, int size, int split) |
705ececd | 58 | { |
ce9b490c | 59 | this->buf = kmalloc(size, GFP_KERNEL); |
705ececd | 60 | |
536165d8 | 61 | if (this->buf == NULL) |
705ececd MG |
62 | return -ENOMEM; |
63 | ||
64 | this->size = size; | |
65 | this->split = split; | |
1027f476 | 66 | line6_midibuf_reset(this); |
705ececd MG |
67 | return 0; |
68 | } | |
705ececd | 69 | |
269edc8e | 70 | int line6_midibuf_bytes_free(struct midi_buffer *this) |
705ececd MG |
71 | { |
72 | return | |
e1a164d7 MG |
73 | midibuf_is_full(this) ? |
74 | 0 : | |
75 | (this->pos_read - this->pos_write + this->size - 1) % this->size + | |
76 | 1; | |
705ececd MG |
77 | } |
78 | ||
269edc8e | 79 | int line6_midibuf_bytes_used(struct midi_buffer *this) |
705ececd MG |
80 | { |
81 | return | |
e1a164d7 MG |
82 | midibuf_is_empty(this) ? |
83 | 0 : | |
84 | (this->pos_write - this->pos_read + this->size - 1) % this->size + | |
85 | 1; | |
705ececd MG |
86 | } |
87 | ||
269edc8e | 88 | int line6_midibuf_write(struct midi_buffer *this, unsigned char *data, |
e1a164d7 | 89 | int length) |
705ececd MG |
90 | { |
91 | int bytes_free; | |
92 | int length1, length2; | |
93 | int skip_active_sense = 0; | |
94 | ||
ce9b490c | 95 | if (midibuf_is_full(this) || (length <= 0)) |
705ececd MG |
96 | return 0; |
97 | ||
98 | /* skip trailing active sense */ | |
ce9b490c | 99 | if (data[length - 1] == 0xfe) { |
705ececd MG |
100 | --length; |
101 | skip_active_sense = 1; | |
102 | } | |
103 | ||
1027f476 | 104 | bytes_free = line6_midibuf_bytes_free(this); |
705ececd | 105 | |
ce9b490c | 106 | if (length > bytes_free) |
705ececd MG |
107 | length = bytes_free; |
108 | ||
ce9b490c | 109 | if (length > 0) { |
705ececd MG |
110 | length1 = this->size - this->pos_write; |
111 | ||
ce9b490c | 112 | if (length < length1) { |
705ececd MG |
113 | /* no buffer wraparound */ |
114 | memcpy(this->buf + this->pos_write, data, length); | |
115 | this->pos_write += length; | |
ce9b490c | 116 | } else { |
705ececd MG |
117 | /* buffer wraparound */ |
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 | ||
ce9b490c | 124 | if (this->pos_write == this->pos_read) |
705ececd MG |
125 | this->full = 1; |
126 | } | |
127 | ||
128 | return length + skip_active_sense; | |
129 | } | |
130 | ||
269edc8e SH |
131 | int line6_midibuf_read(struct midi_buffer *this, unsigned char *data, |
132 | int length) | |
705ececd MG |
133 | { |
134 | int bytes_used; | |
135 | int length1, length2; | |
136 | int command; | |
137 | int midi_length; | |
138 | int repeat = 0; | |
139 | int i; | |
140 | ||
ce9b490c GKH |
141 | /* we need to be able to store at least a 3 byte MIDI message */ |
142 | if (length < 3) | |
143 | return -EINVAL; | |
705ececd | 144 | |
ce9b490c | 145 | if (midibuf_is_empty(this)) |
705ececd MG |
146 | return 0; |
147 | ||
1027f476 | 148 | bytes_used = line6_midibuf_bytes_used(this); |
705ececd | 149 | |
ce9b490c | 150 | if (length > bytes_used) |
705ececd MG |
151 | length = bytes_used; |
152 | ||
153 | length1 = this->size - this->pos_read; | |
154 | ||
155 | /* check MIDI command length */ | |
156 | command = this->buf[this->pos_read]; | |
157 | ||
ce9b490c | 158 | if (command & 0x80) { |
705ececd MG |
159 | midi_length = midibuf_message_length(command); |
160 | this->command_prev = command; | |
ce9b490c GKH |
161 | } else { |
162 | if (this->command_prev > 0) { | |
e1a164d7 MG |
163 | int midi_length_prev = |
164 | midibuf_message_length(this->command_prev); | |
705ececd | 165 | |
ce9b490c | 166 | if (midi_length_prev > 0) { |
705ececd MG |
167 | midi_length = midi_length_prev - 1; |
168 | repeat = 1; | |
ce9b490c | 169 | } else |
705ececd | 170 | midi_length = -1; |
ce9b490c | 171 | } else |
705ececd MG |
172 | midi_length = -1; |
173 | } | |
174 | ||
ce9b490c | 175 | if (midi_length < 0) { |
705ececd | 176 | /* search for end of message */ |
ce9b490c | 177 | if (length < length1) { |
705ececd | 178 | /* no buffer wraparound */ |
ce9b490c GKH |
179 | for (i = 1; i < length; ++i) |
180 | if (this->buf[this->pos_read + i] & 0x80) | |
705ececd MG |
181 | break; |
182 | ||
183 | midi_length = i; | |
ce9b490c | 184 | } else { |
705ececd MG |
185 | /* buffer wraparound */ |
186 | length2 = length - length1; | |
187 | ||
ce9b490c GKH |
188 | for (i = 1; i < length1; ++i) |
189 | if (this->buf[this->pos_read + i] & 0x80) | |
705ececd MG |
190 | break; |
191 | ||
ce9b490c | 192 | if (i < length1) |
705ececd MG |
193 | midi_length = i; |
194 | else { | |
ce9b490c GKH |
195 | for (i = 0; i < length2; ++i) |
196 | if (this->buf[i] & 0x80) | |
705ececd MG |
197 | break; |
198 | ||
199 | midi_length = length1 + i; | |
200 | } | |
201 | } | |
202 | ||
ce9b490c | 203 | if (midi_length == length) |
e1a164d7 | 204 | midi_length = -1; /* end of message not found */ |
705ececd MG |
205 | } |
206 | ||
ce9b490c GKH |
207 | if (midi_length < 0) { |
208 | if (!this->split) | |
e1a164d7 | 209 | return 0; /* command is not yet complete */ |
ce9b490c GKH |
210 | } else { |
211 | if (length < midi_length) | |
e1a164d7 | 212 | return 0; /* command is not yet complete */ |
705ececd MG |
213 | |
214 | length = midi_length; | |
215 | } | |
216 | ||
ce9b490c | 217 | if (length < length1) { |
705ececd MG |
218 | /* no buffer wraparound */ |
219 | memcpy(data + repeat, this->buf + this->pos_read, length); | |
220 | this->pos_read += length; | |
ce9b490c | 221 | } else { |
705ececd MG |
222 | /* buffer wraparound */ |
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 | ||
ce9b490c | 229 | if (repeat) |
705ececd MG |
230 | data[0] = this->command_prev; |
231 | ||
232 | this->full = 0; | |
233 | return length + repeat; | |
234 | } | |
235 | ||
269edc8e | 236 | int line6_midibuf_ignore(struct midi_buffer *this, int length) |
705ececd | 237 | { |
1027f476 | 238 | int bytes_used = line6_midibuf_bytes_used(this); |
705ececd | 239 | |
ce9b490c | 240 | if (length > bytes_used) |
705ececd MG |
241 | length = bytes_used; |
242 | ||
243 | this->pos_read = (this->pos_read + length) % this->size; | |
244 | this->full = 0; | |
245 | return length; | |
246 | } | |
247 | ||
269edc8e | 248 | void line6_midibuf_destroy(struct midi_buffer *this) |
705ececd | 249 | { |
536165d8 GKH |
250 | kfree(this->buf); |
251 | this->buf = NULL; | |
705ececd | 252 | } |