blob: f0adb7baa603b3ae060cf6e0397a628af6b980e3 [file] [log] [blame]
Markus Grabner705ecec2009-02-27 19:43:04 -08001/*
Markus Grabnere1a164d2010-08-23 01:08:25 +02002 * Line6 Linux USB driver - 0.9.1beta
Markus Grabner705ecec2009-02-27 19:43:04 -08003 *
Markus Grabner1027f4762010-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{
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080018 if (code < 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -080019 return -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080020 else if (code < 0xf0) {
Markus Grabner705ecec2009-02-27 19:43:04 -080021 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
22 return length[(code >> 4) - 8];
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080023 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -080024 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +020025 Note that according to the MIDI specification 0xf2 is
26 the "Song Position Pointer", but this is used by Line6
27 to send sysex messages to the host.
28 */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080029 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
Markus Grabnere1a164d2010-08-23 01:08:25 +020030 1, 1, 1, -1, 1, 1
31 };
Markus Grabner705ecec2009-02-27 19:43:04 -080032 return length[code & 0x0f];
33 }
34}
35
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010036static int midibuf_is_empty(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080037{
38 return (this->pos_read == this->pos_write) && !this->full;
39}
40
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010041static int midibuf_is_full(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080042{
43 return this->full;
44}
45
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010046void line6_midibuf_reset(struct midi_buffer *this)
Markus Grabner1027f4762010-08-12 01:35:30 +020047{
48 this->pos_read = this->pos_write = this->full = 0;
49 this->command_prev = -1;
50}
51
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010052int line6_midibuf_init(struct midi_buffer *this, int size, int split)
Markus Grabner1027f4762010-08-12 01:35:30 +020053{
54 this->buf = kmalloc(size, GFP_KERNEL);
55
56 if (this->buf == NULL)
57 return -ENOMEM;
58
59 this->size = size;
60 this->split = split;
61 line6_midibuf_reset(this);
62 return 0;
63}
64
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010065void line6_midibuf_status(struct midi_buffer *this)
Markus Grabner1027f4762010-08-12 01:35:30 +020066{
Stefan Hajnoczib3a24fc2012-11-11 13:24:40 +010067 pr_debug("midibuf size=%d split=%d pos_read=%d pos_write=%d full=%d command_prev=%02x\n",
68 this->size, this->split, this->pos_read, this->pos_write,
69 this->full, this->command_prev);
Markus Grabner1027f4762010-08-12 01:35:30 +020070}
71
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010072int line6_midibuf_bytes_free(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080073{
74 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020075 midibuf_is_full(this) ?
76 0 :
77 (this->pos_read - this->pos_write + this->size - 1) % this->size +
78 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080079}
80
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010081int line6_midibuf_bytes_used(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080082{
83 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020084 midibuf_is_empty(this) ?
85 0 :
86 (this->pos_write - this->pos_read + this->size - 1) % this->size +
87 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080088}
89
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010090int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
Markus Grabnere1a164d2010-08-23 01:08:25 +020091 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -080092{
93 int bytes_free;
94 int length1, length2;
95 int skip_active_sense = 0;
96
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080097 if (midibuf_is_full(this) || (length <= 0))
Markus Grabner705ecec2009-02-27 19:43:04 -080098 return 0;
99
100 /* skip trailing active sense */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800101 if (data[length - 1] == 0xfe) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800102 --length;
103 skip_active_sense = 1;
104 }
105
Markus Grabner1027f4762010-08-12 01:35:30 +0200106 bytes_free = line6_midibuf_bytes_free(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800107
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800108 if (length > bytes_free)
Markus Grabner705ecec2009-02-27 19:43:04 -0800109 length = bytes_free;
110
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800111 if (length > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800112 length1 = this->size - this->pos_write;
113
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800114 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800115 /* no buffer wraparound */
116 memcpy(this->buf + this->pos_write, data, length);
117 this->pos_write += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800118 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800119 /* buffer wraparound */
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;
124 }
125
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800126 if (this->pos_write == this->pos_read)
Markus Grabner705ecec2009-02-27 19:43:04 -0800127 this->full = 1;
128 }
129
130 return length + skip_active_sense;
131}
132
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100133int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
134 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800135{
136 int bytes_used;
137 int length1, length2;
138 int command;
139 int midi_length;
140 int repeat = 0;
141 int i;
142
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800143 /* we need to be able to store at least a 3 byte MIDI message */
144 if (length < 3)
145 return -EINVAL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800146
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800147 if (midibuf_is_empty(this))
Markus Grabner705ecec2009-02-27 19:43:04 -0800148 return 0;
149
Markus Grabner1027f4762010-08-12 01:35:30 +0200150 bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800151
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800152 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800153 length = bytes_used;
154
155 length1 = this->size - this->pos_read;
156
157 /* check MIDI command length */
158 command = this->buf[this->pos_read];
159
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800160 if (command & 0x80) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800161 midi_length = midibuf_message_length(command);
162 this->command_prev = command;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800163 } else {
164 if (this->command_prev > 0) {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200165 int midi_length_prev =
166 midibuf_message_length(this->command_prev);
Markus Grabner705ecec2009-02-27 19:43:04 -0800167
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800168 if (midi_length_prev > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800169 midi_length = midi_length_prev - 1;
170 repeat = 1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800171 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800172 midi_length = -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800173 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800174 midi_length = -1;
175 }
176
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800177 if (midi_length < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800178 /* search for end of message */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800179 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800180 /* no buffer wraparound */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800181 for (i = 1; i < length; ++i)
182 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800183 break;
184
185 midi_length = i;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800186 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800187 /* buffer wraparound */
188 length2 = length - length1;
189
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800190 for (i = 1; i < length1; ++i)
191 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800192 break;
193
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800194 if (i < length1)
Markus Grabner705ecec2009-02-27 19:43:04 -0800195 midi_length = i;
196 else {
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800197 for (i = 0; i < length2; ++i)
198 if (this->buf[i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800199 break;
200
201 midi_length = length1 + i;
202 }
203 }
204
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800205 if (midi_length == length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200206 midi_length = -1; /* end of message not found */
Markus Grabner705ecec2009-02-27 19:43:04 -0800207 }
208
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800209 if (midi_length < 0) {
210 if (!this->split)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200211 return 0; /* command is not yet complete */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800212 } else {
213 if (length < midi_length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200214 return 0; /* command is not yet complete */
Markus Grabner705ecec2009-02-27 19:43:04 -0800215
216 length = midi_length;
217 }
218
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800219 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800220 /* no buffer wraparound */
221 memcpy(data + repeat, this->buf + this->pos_read, length);
222 this->pos_read += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800223 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800224 /* buffer wraparound */
225 length2 = length - length1;
226 memcpy(data + repeat, this->buf + this->pos_read, length1);
227 memcpy(data + repeat + length1, this->buf, length2);
228 this->pos_read = length2;
229 }
230
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800231 if (repeat)
Markus Grabner705ecec2009-02-27 19:43:04 -0800232 data[0] = this->command_prev;
233
234 this->full = 0;
235 return length + repeat;
236}
237
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100238int line6_midibuf_ignore(struct midi_buffer *this, int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800239{
Markus Grabner1027f4762010-08-12 01:35:30 +0200240 int bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800241
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800242 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800243 length = bytes_used;
244
245 this->pos_read = (this->pos_read + length) % this->size;
246 this->full = 0;
247 return length;
248}
249
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100250int line6_midibuf_skip_message(struct midi_buffer *this, unsigned short mask)
Markus Grabner705ecec2009-02-27 19:43:04 -0800251{
252 int cmd = this->command_prev;
253
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800254 if ((cmd >= 0x80) && (cmd < 0xf0))
255 if ((mask & (1 << (cmd & 0x0f))) == 0)
Markus Grabner705ecec2009-02-27 19:43:04 -0800256 return 1;
257
258 return 0;
259}
260
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100261void line6_midibuf_destroy(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -0800262{
Greg Kroah-Hartman536165d2009-02-27 20:49:46 -0800263 kfree(this->buf);
264 this->buf = NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800265}