blob: 6a70463f82c4efcbd533c192844e072cae497cc2 [file] [log] [blame]
Thomas Gleixnera10e7632019-05-31 01:09:32 -07001// SPDX-License-Identifier: GPL-2.0-only
Markus Grabner705ecec2009-02-27 19:43:04 -08002/*
Chris Rorvickc078a4a2015-01-20 02:20:50 -06003 * Line 6 Linux USB driver
Markus Grabner705ecec2009-02-27 19:43:04 -08004 *
Markus Grabner1027f472010-08-12 01:35:30 +02005 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
Markus Grabner705ecec2009-02-27 19:43:04 -08006 */
7
Markus Grabner705ecec2009-02-27 19:43:04 -08008#include <linux/slab.h>
9
10#include "midibuf.h"
11
Greg Kroah-Hartmanb702ed252009-02-27 20:45:03 -080012static int midibuf_message_length(unsigned char code)
Markus Grabner705ecec2009-02-27 19:43:04 -080013{
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020014 int message_length;
15
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080016 if (code < 0x80)
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020017 message_length = -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080018 else if (code < 0xf0) {
Markus Grabner705ecec2009-02-27 19:43:04 -080019 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020020
21 message_length = length[(code >> 4) - 8];
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080022 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -080023 /*
Markus Grabnere1a164d2010-08-23 01:08:25 +020024 Note that according to the MIDI specification 0xf2 is
Chris Rorvickc6fffce2015-01-20 02:20:49 -060025 the "Song Position Pointer", but this is used by Line 6
Markus Grabnere1a164d2010-08-23 01:08:25 +020026 to send sysex messages to the host.
27 */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080028 static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
Markus Grabnere1a164d2010-08-23 01:08:25 +020029 1, 1, 1, -1, 1, 1
30 };
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020031 message_length = length[code & 0x0f];
Markus Grabner705ecec2009-02-27 19:43:04 -080032 }
Domagoj Trsand1d1a9d2014-09-20 14:40:38 +020033
34 return message_length;
Markus Grabner705ecec2009-02-27 19:43:04 -080035}
36
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010037static int midibuf_is_empty(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080038{
39 return (this->pos_read == this->pos_write) && !this->full;
40}
41
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010042static int midibuf_is_full(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080043{
44 return this->full;
45}
46
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010047void line6_midibuf_reset(struct midi_buffer *this)
Markus Grabner1027f472010-08-12 01:35:30 +020048{
49 this->pos_read = this->pos_write = this->full = 0;
50 this->command_prev = -1;
51}
52
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010053int line6_midibuf_init(struct midi_buffer *this, int size, int split)
Markus Grabner1027f472010-08-12 01:35:30 +020054{
55 this->buf = kmalloc(size, GFP_KERNEL);
56
57 if (this->buf == NULL)
58 return -ENOMEM;
59
60 this->size = size;
61 this->split = split;
62 line6_midibuf_reset(this);
63 return 0;
64}
65
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010066int line6_midibuf_bytes_free(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080067{
68 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020069 midibuf_is_full(this) ?
70 0 :
71 (this->pos_read - this->pos_write + this->size - 1) % this->size +
72 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080073}
74
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010075int line6_midibuf_bytes_used(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -080076{
77 return
Markus Grabnere1a164d2010-08-23 01:08:25 +020078 midibuf_is_empty(this) ?
79 0 :
80 (this->pos_write - this->pos_read + this->size - 1) % this->size +
81 1;
Markus Grabner705ecec2009-02-27 19:43:04 -080082}
83
Stefan Hajnoczi269edc82013-01-11 23:08:09 +010084int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
Markus Grabnere1a164d2010-08-23 01:08:25 +020085 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -080086{
87 int bytes_free;
88 int length1, length2;
89 int skip_active_sense = 0;
90
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080091 if (midibuf_is_full(this) || (length <= 0))
Markus Grabner705ecec2009-02-27 19:43:04 -080092 return 0;
93
94 /* skip trailing active sense */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -080095 if (data[length - 1] == 0xfe) {
Markus Grabner705ecec2009-02-27 19:43:04 -080096 --length;
97 skip_active_sense = 1;
98 }
99
Markus Grabner1027f472010-08-12 01:35:30 +0200100 bytes_free = line6_midibuf_bytes_free(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800101
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800102 if (length > bytes_free)
Markus Grabner705ecec2009-02-27 19:43:04 -0800103 length = bytes_free;
104
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800105 if (length > 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800106 length1 = this->size - this->pos_write;
107
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800108 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800109 /* no buffer wraparound */
110 memcpy(this->buf + this->pos_write, data, length);
111 this->pos_write += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800112 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800113 /* buffer wraparound */
114 length2 = length - length1;
115 memcpy(this->buf + this->pos_write, data, length1);
116 memcpy(this->buf, data + length1, length2);
117 this->pos_write = length2;
118 }
119
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800120 if (this->pos_write == this->pos_read)
Markus Grabner705ecec2009-02-27 19:43:04 -0800121 this->full = 1;
122 }
123
124 return length + skip_active_sense;
125}
126
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100127int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
128 int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800129{
130 int bytes_used;
131 int length1, length2;
132 int command;
133 int midi_length;
134 int repeat = 0;
135 int i;
136
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800137 /* we need to be able to store at least a 3 byte MIDI message */
138 if (length < 3)
139 return -EINVAL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800140
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800141 if (midibuf_is_empty(this))
Markus Grabner705ecec2009-02-27 19:43:04 -0800142 return 0;
143
Markus Grabner1027f472010-08-12 01:35:30 +0200144 bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800145
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800146 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800147 length = bytes_used;
148
149 length1 = this->size - this->pos_read;
150
151 /* check MIDI command length */
152 command = this->buf[this->pos_read];
153
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800154 if (command & 0x80) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800155 midi_length = midibuf_message_length(command);
156 this->command_prev = command;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800157 } else {
158 if (this->command_prev > 0) {
Markus Grabnere1a164d2010-08-23 01:08:25 +0200159 int midi_length_prev =
160 midibuf_message_length(this->command_prev);
Markus Grabner705ecec2009-02-27 19:43:04 -0800161
Takashi Iwaid6834692020-03-09 10:59:22 +0100162 if (midi_length_prev > 1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800163 midi_length = midi_length_prev - 1;
164 repeat = 1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800165 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800166 midi_length = -1;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800167 } else
Markus Grabner705ecec2009-02-27 19:43:04 -0800168 midi_length = -1;
169 }
170
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800171 if (midi_length < 0) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800172 /* search for end of message */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800173 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800174 /* no buffer wraparound */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800175 for (i = 1; i < length; ++i)
176 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800177 break;
178
179 midi_length = i;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800180 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800181 /* buffer wraparound */
182 length2 = length - length1;
183
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800184 for (i = 1; i < length1; ++i)
185 if (this->buf[this->pos_read + i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800186 break;
187
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800188 if (i < length1)
Markus Grabner705ecec2009-02-27 19:43:04 -0800189 midi_length = i;
190 else {
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800191 for (i = 0; i < length2; ++i)
192 if (this->buf[i] & 0x80)
Markus Grabner705ecec2009-02-27 19:43:04 -0800193 break;
194
195 midi_length = length1 + i;
196 }
197 }
198
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800199 if (midi_length == length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200200 midi_length = -1; /* end of message not found */
Markus Grabner705ecec2009-02-27 19:43:04 -0800201 }
202
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800203 if (midi_length < 0) {
204 if (!this->split)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200205 return 0; /* command is not yet complete */
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800206 } else {
207 if (length < midi_length)
Markus Grabnere1a164d2010-08-23 01:08:25 +0200208 return 0; /* command is not yet complete */
Markus Grabner705ecec2009-02-27 19:43:04 -0800209
210 length = midi_length;
211 }
212
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800213 if (length < length1) {
Markus Grabner705ecec2009-02-27 19:43:04 -0800214 /* no buffer wraparound */
215 memcpy(data + repeat, this->buf + this->pos_read, length);
216 this->pos_read += length;
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800217 } else {
Markus Grabner705ecec2009-02-27 19:43:04 -0800218 /* buffer wraparound */
219 length2 = length - length1;
220 memcpy(data + repeat, this->buf + this->pos_read, length1);
221 memcpy(data + repeat + length1, this->buf, length2);
222 this->pos_read = length2;
223 }
224
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800225 if (repeat)
Markus Grabner705ecec2009-02-27 19:43:04 -0800226 data[0] = this->command_prev;
227
228 this->full = 0;
229 return length + repeat;
230}
231
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100232int line6_midibuf_ignore(struct midi_buffer *this, int length)
Markus Grabner705ecec2009-02-27 19:43:04 -0800233{
Markus Grabner1027f472010-08-12 01:35:30 +0200234 int bytes_used = line6_midibuf_bytes_used(this);
Markus Grabner705ecec2009-02-27 19:43:04 -0800235
Greg Kroah-Hartmance9b4902009-02-27 22:43:11 -0800236 if (length > bytes_used)
Markus Grabner705ecec2009-02-27 19:43:04 -0800237 length = bytes_used;
238
239 this->pos_read = (this->pos_read + length) % this->size;
240 this->full = 0;
241 return length;
242}
243
Stefan Hajnoczi269edc82013-01-11 23:08:09 +0100244void line6_midibuf_destroy(struct midi_buffer *this)
Markus Grabner705ecec2009-02-27 19:43:04 -0800245{
Greg Kroah-Hartman536165d2009-02-27 20:49:46 -0800246 kfree(this->buf);
247 this->buf = NULL;
Markus Grabner705ecec2009-02-27 19:43:04 -0800248}