blob: 55df0da5f9196d640f47d89a1e375a3cc3cb1097 [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Chris Rorvickc6fffce2015-01-20 02:20:49 -06002 * Line 6 Linux USB driver - 0.9.1beta
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f472010-08-12 01:35:30 +02004 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08005 *
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
Markus Grabner705ecec2009-02-27 19:43:04 -080012#include <linux/slab.h>
13
14#include "midibuf.h"
15
Greg Kroah-Hartmanb702ed252009-02-27 20:45:03 -080016static int midibuf_message_length(unsigned char code)
Markus Grabner705ecec2009-02-27 19:43:04 -080017{
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020018 int message_length;
19
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080020 if (code < 0x80)
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020021 message_length = -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080022 else if (code < 0xf0) {
Markus Grabner705ecec2009-02-27 19:43:04 -080023 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020024
25 message_length = length[(code >> 4) - 8];
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080026 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -080027 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +020028 Note that according to the MIDI specification 0xf2 is
Chris Rorvickc6fffce2015-01-20 02:20:49 -060029 the "Song Position Pointer", but this is used by Line 6
Markus Grabnere1a164d2010-08-23 01:08:25 +020030 to send sysex messages to the host.
31 */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080032 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
Markus Grabnere1a164d2010-08-23 01:08:25 +020033 1, 1, 1, -1, 1, 1
34 };
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020035 message_length = length[code & 0x0f];
Markus Grabner705ecec2009-02-27 19:43:04 -080036 }
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020037
38 return message_length;
Markus Grabner705ecec2009-02-27 19:43:04 -080039}
40
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010041static int midibuf_is_empty(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080042{
43 return (this->pos_read == this->pos_write) && !this->full;
44}
45
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010046static int midibuf_is_full(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080047{
48 return this->full;
49}
50
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010051void line6_midibuf_reset(struct midi_buffer *this)
Markus Grabner1027f472010-08-12 01:35:30 +020052{
53 this->pos_read = this->pos_write = this->full = 0;
54 this->command_prev = -1;
55}
56
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010057int line6_midibuf_init(struct midi_buffer *this, int size, int split)
Markus Grabner1027f472010-08-12 01:35:30 +020058{
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
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010070void line6_midibuf_status(struct midi_buffer *this)
Markus Grabner1027f472010-08-12 01:35:30 +020071{
Stefan Hajnoczib3a24fc2012-11-11 13:24:40 +010072 pr_debug("midibuf size=%d split=%d pos_read=%d pos_write=%d full=%d command_prev=%02x\n",
73 this->size, this->split, this->pos_read, this->pos_write,
74 this->full, this->command_prev);
Markus Grabner1027f472010-08-12 01:35:30 +020075}
76
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010077int line6_midibuf_bytes_free(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080078{
79 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020080 midibuf_is_full(this) ?
81 0 :
82 (this->pos_read - this->pos_write + this->size - 1) % this->size +
83 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080084}
85
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010086int line6_midibuf_bytes_used(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080087{
88 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020089 midibuf_is_empty(this) ?
90 0 :
91 (this->pos_write - this->pos_read + this->size - 1) % this->size +
92 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080093}
94
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010095int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
Markus Grabnere1a164d2010-08-23 01:08:25 +020096 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -080097{
98 int bytes_free;
99 int length1, length2;
100 int skip_active_sense = 0;
101
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800102 if (midibuf_is_full(this) || (length <= 0))
Markus Grabner705ecec2009-02-27 19:43:04 -0800103 return 0;
104
105 /* skip trailing active sense */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800106 if (data[length - 1] == 0xfe) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800107 --length;
108 skip_active_sense = 1;
109 }
110
Markus Grabner1027f472010-08-12 01:35:30 +0200111 bytes_free = line6_midibuf_bytes_free(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800112
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800113 if (length > bytes_free)
Markus Grabner705ecec2009-02-27 19:43:04 -0800114 length = bytes_free;
115
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800116 if (length > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800117 length1 = this->size - this->pos_write;
118
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800119 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800120 /* no buffer wraparound */
121 memcpy(this->buf + this->pos_write, data, length);
122 this->pos_write += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800123 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800124 /* buffer wraparound */
125 length2 = length - length1;
126 memcpy(this->buf + this->pos_write, data, length1);
127 memcpy(this->buf, data + length1, length2);
128 this->pos_write = length2;
129 }
130
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800131 if (this->pos_write == this->pos_read)
Markus Grabner705ecec2009-02-27 19:43:04 -0800132 this->full = 1;
133 }
134
135 return length + skip_active_sense;
136}
137
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100138int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
139 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800140{
141 int bytes_used;
142 int length1, length2;
143 int command;
144 int midi_length;
145 int repeat = 0;
146 int i;
147
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800148 /* we need to be able to store at least a 3 byte MIDI message */
149 if (length < 3)
150 return -EINVAL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800151
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800152 if (midibuf_is_empty(this))
Markus Grabner705ecec2009-02-27 19:43:04 -0800153 return 0;
154
Markus Grabner1027f472010-08-12 01:35:30 +0200155 bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800156
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800157 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800158 length = bytes_used;
159
160 length1 = this->size - this->pos_read;
161
162 /* check MIDI command length */
163 command = this->buf[this->pos_read];
164
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800165 if (command & 0x80) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800166 midi_length = midibuf_message_length(command);
167 this->command_prev = command;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800168 } else {
169 if (this->command_prev > 0) {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200170 int midi_length_prev =
171 midibuf_message_length(this->command_prev);
Markus Grabner705ecec2009-02-27 19:43:04 -0800172
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800173 if (midi_length_prev > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800174 midi_length = midi_length_prev - 1;
175 repeat = 1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800176 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800177 midi_length = -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800178 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800179 midi_length = -1;
180 }
181
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800182 if (midi_length < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800183 /* search for end of message */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800184 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800185 /* no buffer wraparound */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800186 for (i = 1; i < length; ++i)
187 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800188 break;
189
190 midi_length = i;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800191 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800192 /* buffer wraparound */
193 length2 = length - length1;
194
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800195 for (i = 1; i < length1; ++i)
196 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800197 break;
198
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800199 if (i < length1)
Markus Grabner705ecec2009-02-27 19:43:04 -0800200 midi_length = i;
201 else {
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800202 for (i = 0; i < length2; ++i)
203 if (this->buf[i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800204 break;
205
206 midi_length = length1 + i;
207 }
208 }
209
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800210 if (midi_length == length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200211 midi_length = -1; /* end of message not found */
Markus Grabner705ecec2009-02-27 19:43:04 -0800212 }
213
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800214 if (midi_length < 0) {
215 if (!this->split)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200216 return 0; /* command is not yet complete */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800217 } else {
218 if (length < midi_length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200219 return 0; /* command is not yet complete */
Markus Grabner705ecec2009-02-27 19:43:04 -0800220
221 length = midi_length;
222 }
223
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800224 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800225 /* no buffer wraparound */
226 memcpy(data + repeat, this->buf + this->pos_read, length);
227 this->pos_read += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800228 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800229 /* buffer wraparound */
230 length2 = length - length1;
231 memcpy(data + repeat, this->buf + this->pos_read, length1);
232 memcpy(data + repeat + length1, this->buf, length2);
233 this->pos_read = length2;
234 }
235
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800236 if (repeat)
Markus Grabner705ecec2009-02-27 19:43:04 -0800237 data[0] = this->command_prev;
238
239 this->full = 0;
240 return length + repeat;
241}
242
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100243int line6_midibuf_ignore(struct midi_buffer *this, int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800244{
Markus Grabner1027f472010-08-12 01:35:30 +0200245 int bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800246
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800247 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800248 length = bytes_used;
249
250 this->pos_read = (this->pos_read + length) % this->size;
251 this->full = 0;
252 return length;
253}
254
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100255int line6_midibuf_skip_message(struct midi_buffer *this, unsigned short mask)
Markus Grabner705ecec2009-02-27 19:43:04 -0800256{
257 int cmd = this->command_prev;
258
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800259 if ((cmd >= 0x80) && (cmd < 0xf0))
260 if ((mask & (1 << (cmd & 0x0f))) == 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800261 return 1;
262
263 return 0;
264}
265
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100266void line6_midibuf_destroy(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -0800267{
Greg Kroah-Hartman536165d2009-02-27 20:49:46 -0800268 kfree(this->buf);
269 this->buf = NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800270}