Thomas Gleixner | a10e763 | 2019-05-31 01:09:32 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 2 | /* |
Chris Rorvick | c078a4a | 2015-01-20 02:20:50 -0600 | [diff] [blame] | 3 | * Line 6 Linux USB driver |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 4 | * |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 5 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 8 | #include <linux/slab.h> |
| 9 | |
| 10 | #include "midibuf.h" |
| 11 | |
Greg Kroah-Hartman | b702ed25 | 2009-02-27 20:45:03 -0800 | [diff] [blame] | 12 | static int midibuf_message_length(unsigned char code) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 13 | { |
Domagoj Trsan | d1d1a9d | 2014-09-20 14:40:38 +0200 | [diff] [blame] | 14 | int message_length; |
| 15 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 16 | if (code < 0x80) |
Domagoj Trsan | d1d1a9d | 2014-09-20 14:40:38 +0200 | [diff] [blame] | 17 | message_length = -1; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 18 | else if (code < 0xf0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 19 | static const int length[] = { 3, 3, 3, 3, 2, 2, 3 }; |
Domagoj Trsan | d1d1a9d | 2014-09-20 14:40:38 +0200 | [diff] [blame] | 20 | |
| 21 | message_length = length[(code >> 4) - 8]; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 22 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 23 | /* |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 24 | Note that according to the MIDI specification 0xf2 is |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame] | 25 | the "Song Position Pointer", but this is used by Line 6 |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 26 | to send sysex messages to the host. |
| 27 | */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 28 | static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 29 | 1, 1, 1, -1, 1, 1 |
| 30 | }; |
Domagoj Trsan | d1d1a9d | 2014-09-20 14:40:38 +0200 | [diff] [blame] | 31 | message_length = length[code & 0x0f]; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 32 | } |
Domagoj Trsan | d1d1a9d | 2014-09-20 14:40:38 +0200 | [diff] [blame] | 33 | |
| 34 | return message_length; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 37 | static int midibuf_is_empty(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 38 | { |
| 39 | return (this->pos_read == this->pos_write) && !this->full; |
| 40 | } |
| 41 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 42 | static int midibuf_is_full(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 43 | { |
| 44 | return this->full; |
| 45 | } |
| 46 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 47 | void line6_midibuf_reset(struct midi_buffer *this) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 48 | { |
| 49 | this->pos_read = this->pos_write = this->full = 0; |
| 50 | this->command_prev = -1; |
| 51 | } |
| 52 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 53 | int line6_midibuf_init(struct midi_buffer *this, int size, int split) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 54 | { |
| 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 Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 66 | int line6_midibuf_bytes_free(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 67 | { |
| 68 | return |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 69 | midibuf_is_full(this) ? |
| 70 | 0 : |
| 71 | (this->pos_read - this->pos_write + this->size - 1) % this->size + |
| 72 | 1; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 75 | int line6_midibuf_bytes_used(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 76 | { |
| 77 | return |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 78 | midibuf_is_empty(this) ? |
| 79 | 0 : |
| 80 | (this->pos_write - this->pos_read + this->size - 1) % this->size + |
| 81 | 1; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 84 | int line6_midibuf_write(struct midi_buffer *this, unsigned char *data, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 85 | int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 86 | { |
| 87 | int bytes_free; |
| 88 | int length1, length2; |
| 89 | int skip_active_sense = 0; |
| 90 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 91 | if (midibuf_is_full(this) || (length <= 0)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 92 | return 0; |
| 93 | |
| 94 | /* skip trailing active sense */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 95 | if (data[length - 1] == 0xfe) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 96 | --length; |
| 97 | skip_active_sense = 1; |
| 98 | } |
| 99 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 100 | bytes_free = line6_midibuf_bytes_free(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 101 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 102 | if (length > bytes_free) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 103 | length = bytes_free; |
| 104 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 105 | if (length > 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 106 | length1 = this->size - this->pos_write; |
| 107 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 108 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 109 | /* no buffer wraparound */ |
| 110 | memcpy(this->buf + this->pos_write, data, length); |
| 111 | this->pos_write += length; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 112 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 113 | /* 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 120 | if (this->pos_write == this->pos_read) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 121 | this->full = 1; |
| 122 | } |
| 123 | |
| 124 | return length + skip_active_sense; |
| 125 | } |
| 126 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 127 | int line6_midibuf_read(struct midi_buffer *this, unsigned char *data, |
| 128 | int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 129 | { |
| 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 137 | /* we need to be able to store at least a 3 byte MIDI message */ |
| 138 | if (length < 3) |
| 139 | return -EINVAL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 140 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 141 | if (midibuf_is_empty(this)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 144 | bytes_used = line6_midibuf_bytes_used(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 145 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 146 | if (length > bytes_used) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 147 | 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 154 | if (command & 0x80) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 155 | midi_length = midibuf_message_length(command); |
| 156 | this->command_prev = command; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 157 | } else { |
| 158 | if (this->command_prev > 0) { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 159 | int midi_length_prev = |
| 160 | midibuf_message_length(this->command_prev); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 161 | |
Takashi Iwai | d683469 | 2020-03-09 10:59:22 +0100 | [diff] [blame^] | 162 | if (midi_length_prev > 1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 163 | midi_length = midi_length_prev - 1; |
| 164 | repeat = 1; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 165 | } else |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 166 | midi_length = -1; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 167 | } else |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 168 | midi_length = -1; |
| 169 | } |
| 170 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 171 | if (midi_length < 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 172 | /* search for end of message */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 173 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 174 | /* no buffer wraparound */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 175 | for (i = 1; i < length; ++i) |
| 176 | if (this->buf[this->pos_read + i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 177 | break; |
| 178 | |
| 179 | midi_length = i; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 180 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 181 | /* buffer wraparound */ |
| 182 | length2 = length - length1; |
| 183 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 184 | for (i = 1; i < length1; ++i) |
| 185 | if (this->buf[this->pos_read + i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 186 | break; |
| 187 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 188 | if (i < length1) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 189 | midi_length = i; |
| 190 | else { |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 191 | for (i = 0; i < length2; ++i) |
| 192 | if (this->buf[i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 193 | break; |
| 194 | |
| 195 | midi_length = length1 + i; |
| 196 | } |
| 197 | } |
| 198 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 199 | if (midi_length == length) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 200 | midi_length = -1; /* end of message not found */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 203 | if (midi_length < 0) { |
| 204 | if (!this->split) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 205 | return 0; /* command is not yet complete */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 206 | } else { |
| 207 | if (length < midi_length) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 208 | return 0; /* command is not yet complete */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 209 | |
| 210 | length = midi_length; |
| 211 | } |
| 212 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 213 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 214 | /* no buffer wraparound */ |
| 215 | memcpy(data + repeat, this->buf + this->pos_read, length); |
| 216 | this->pos_read += length; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 217 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 218 | /* 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 225 | if (repeat) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 226 | data[0] = this->command_prev; |
| 227 | |
| 228 | this->full = 0; |
| 229 | return length + repeat; |
| 230 | } |
| 231 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 232 | int line6_midibuf_ignore(struct midi_buffer *this, int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 233 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 234 | int bytes_used = line6_midibuf_bytes_used(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 235 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 236 | if (length > bytes_used) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 237 | length = bytes_used; |
| 238 | |
| 239 | this->pos_read = (this->pos_read + length) % this->size; |
| 240 | this->full = 0; |
| 241 | return length; |
| 242 | } |
| 243 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 244 | void line6_midibuf_destroy(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 245 | { |
Greg Kroah-Hartman | 536165d | 2009-02-27 20:49:46 -0800 | [diff] [blame] | 246 | kfree(this->buf); |
| 247 | this->buf = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 248 | } |