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