2 * Line 6 Linux USB driver
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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.
12 #include <linux/slab.h>
16 static int midibuf_message_length(unsigned char code)
22 else if (code < 0xf0) {
23 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
25 message_length = length[(code >> 4) - 8];
28 Note that according to the MIDI specification 0xf2 is
29 the "Song Position Pointer", but this is used by Line 6
30 to send sysex messages to the host.
32 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
35 message_length = length[code & 0x0f];
38 return message_length;
41 static int midibuf_is_empty(struct midi_buffer *this)
43 return (this->pos_read == this->pos_write) && !this->full;
46 static int midibuf_is_full(struct midi_buffer *this)
51 void line6_midibuf_reset(struct midi_buffer *this)
53 this->pos_read = this->pos_write = this->full = 0;
54 this->command_prev = -1;
57 int line6_midibuf_init(struct midi_buffer *this, int size, int split)
59 this->buf = kmalloc(size, GFP_KERNEL);
61 if (this->buf == NULL)
66 line6_midibuf_reset(this);
70 int line6_midibuf_bytes_free(struct midi_buffer *this)
73 midibuf_is_full(this) ?
75 (this->pos_read - this->pos_write + this->size - 1) % this->size +
79 int line6_midibuf_bytes_used(struct midi_buffer *this)
82 midibuf_is_empty(this) ?
84 (this->pos_write - this->pos_read + this->size - 1) % this->size +
88 int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
93 int skip_active_sense = 0;
95 if (midibuf_is_full(this) || (length <= 0))
98 /* skip trailing active sense */
99 if (data[length - 1] == 0xfe) {
101 skip_active_sense = 1;
104 bytes_free = line6_midibuf_bytes_free(this);
106 if (length > bytes_free)
110 length1 = this->size - this->pos_write;
112 if (length < length1) {
113 /* no buffer wraparound */
114 memcpy(this->buf + this->pos_write, data, length);
115 this->pos_write += length;
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;
124 if (this->pos_write == this->pos_read)
128 return length + skip_active_sense;
131 int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
135 int length1, length2;
141 /* we need to be able to store at least a 3 byte MIDI message */
145 if (midibuf_is_empty(this))
148 bytes_used = line6_midibuf_bytes_used(this);
150 if (length > bytes_used)
153 length1 = this->size - this->pos_read;
155 /* check MIDI command length */
156 command = this->buf[this->pos_read];
158 if (command & 0x80) {
159 midi_length = midibuf_message_length(command);
160 this->command_prev = command;
162 if (this->command_prev > 0) {
163 int midi_length_prev =
164 midibuf_message_length(this->command_prev);
166 if (midi_length_prev > 0) {
167 midi_length = midi_length_prev - 1;
175 if (midi_length < 0) {
176 /* search for end of message */
177 if (length < length1) {
178 /* no buffer wraparound */
179 for (i = 1; i < length; ++i)
180 if (this->buf[this->pos_read + i] & 0x80)
185 /* buffer wraparound */
186 length2 = length - length1;
188 for (i = 1; i < length1; ++i)
189 if (this->buf[this->pos_read + i] & 0x80)
195 for (i = 0; i < length2; ++i)
196 if (this->buf[i] & 0x80)
199 midi_length = length1 + i;
203 if (midi_length == length)
204 midi_length = -1; /* end of message not found */
207 if (midi_length < 0) {
209 return 0; /* command is not yet complete */
211 if (length < midi_length)
212 return 0; /* command is not yet complete */
214 length = midi_length;
217 if (length < length1) {
218 /* no buffer wraparound */
219 memcpy(data + repeat, this->buf + this->pos_read, length);
220 this->pos_read += length;
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;
230 data[0] = this->command_prev;
233 return length + repeat;
236 int line6_midibuf_ignore(struct midi_buffer *this, int length)
238 int bytes_used = line6_midibuf_bytes_used(this);
240 if (length > bytes_used)
243 this->pos_read = (this->pos_read + length) % this->size;
248 void line6_midibuf_destroy(struct midi_buffer *this)