Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 1 | /* |
Chris Rorvick | c6fffce | 2015-01-20 02:20:49 -0600 | [diff] [blame^] | 2 | * Line 6 Linux USB driver - 0.9.1beta |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 3 | * |
Markus Grabner | 1027f47 | 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 | 1027f47 | 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 | 1027f47 | 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 | void line6_midibuf_status(struct midi_buffer *this) |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 71 | { |
Stefan Hajnoczi | b3a24fc | 2012-11-11 13:24:40 +0100 | [diff] [blame] | 72 | 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 Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 77 | int line6_midibuf_bytes_free(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 78 | { |
| 79 | return |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 80 | midibuf_is_full(this) ? |
| 81 | 0 : |
| 82 | (this->pos_read - this->pos_write + this->size - 1) % this->size + |
| 83 | 1; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 86 | int line6_midibuf_bytes_used(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 87 | { |
| 88 | return |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 89 | midibuf_is_empty(this) ? |
| 90 | 0 : |
| 91 | (this->pos_write - this->pos_read + this->size - 1) % this->size + |
| 92 | 1; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 95 | int line6_midibuf_write(struct midi_buffer *this, unsigned char *data, |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 96 | int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 97 | { |
| 98 | int bytes_free; |
| 99 | int length1, length2; |
| 100 | int skip_active_sense = 0; |
| 101 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 102 | if (midibuf_is_full(this) || (length <= 0)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 103 | return 0; |
| 104 | |
| 105 | /* skip trailing active sense */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 106 | if (data[length - 1] == 0xfe) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 107 | --length; |
| 108 | skip_active_sense = 1; |
| 109 | } |
| 110 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 111 | bytes_free = line6_midibuf_bytes_free(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 112 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 113 | if (length > bytes_free) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 114 | length = bytes_free; |
| 115 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 116 | if (length > 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 117 | length1 = this->size - this->pos_write; |
| 118 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 119 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 120 | /* no buffer wraparound */ |
| 121 | memcpy(this->buf + this->pos_write, data, length); |
| 122 | this->pos_write += length; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 123 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 124 | /* 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 131 | if (this->pos_write == this->pos_read) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 132 | this->full = 1; |
| 133 | } |
| 134 | |
| 135 | return length + skip_active_sense; |
| 136 | } |
| 137 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 138 | int line6_midibuf_read(struct midi_buffer *this, unsigned char *data, |
| 139 | int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 140 | { |
| 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 148 | /* we need to be able to store at least a 3 byte MIDI message */ |
| 149 | if (length < 3) |
| 150 | return -EINVAL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 151 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 152 | if (midibuf_is_empty(this)) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 153 | return 0; |
| 154 | |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 155 | bytes_used = line6_midibuf_bytes_used(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 156 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 157 | if (length > bytes_used) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 158 | 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 165 | if (command & 0x80) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 166 | midi_length = midibuf_message_length(command); |
| 167 | this->command_prev = command; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 168 | } else { |
| 169 | if (this->command_prev > 0) { |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 170 | int midi_length_prev = |
| 171 | midibuf_message_length(this->command_prev); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 172 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 173 | if (midi_length_prev > 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 174 | midi_length = midi_length_prev - 1; |
| 175 | repeat = 1; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 176 | } else |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 177 | midi_length = -1; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 178 | } else |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 179 | midi_length = -1; |
| 180 | } |
| 181 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 182 | if (midi_length < 0) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 183 | /* search for end of message */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 184 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 185 | /* no buffer wraparound */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 186 | for (i = 1; i < length; ++i) |
| 187 | if (this->buf[this->pos_read + i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 188 | break; |
| 189 | |
| 190 | midi_length = i; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 191 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 192 | /* buffer wraparound */ |
| 193 | length2 = length - length1; |
| 194 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 195 | for (i = 1; i < length1; ++i) |
| 196 | if (this->buf[this->pos_read + i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 197 | break; |
| 198 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 199 | if (i < length1) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 200 | midi_length = i; |
| 201 | else { |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 202 | for (i = 0; i < length2; ++i) |
| 203 | if (this->buf[i] & 0x80) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 204 | break; |
| 205 | |
| 206 | midi_length = length1 + i; |
| 207 | } |
| 208 | } |
| 209 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 210 | if (midi_length == length) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 211 | midi_length = -1; /* end of message not found */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 214 | if (midi_length < 0) { |
| 215 | if (!this->split) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 216 | return 0; /* command is not yet complete */ |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 217 | } else { |
| 218 | if (length < midi_length) |
Markus Grabner | e1a164d | 2010-08-23 01:08:25 +0200 | [diff] [blame] | 219 | return 0; /* command is not yet complete */ |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 220 | |
| 221 | length = midi_length; |
| 222 | } |
| 223 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 224 | if (length < length1) { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 225 | /* no buffer wraparound */ |
| 226 | memcpy(data + repeat, this->buf + this->pos_read, length); |
| 227 | this->pos_read += length; |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 228 | } else { |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 229 | /* 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-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 236 | if (repeat) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 237 | data[0] = this->command_prev; |
| 238 | |
| 239 | this->full = 0; |
| 240 | return length + repeat; |
| 241 | } |
| 242 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 243 | int line6_midibuf_ignore(struct midi_buffer *this, int length) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 244 | { |
Markus Grabner | 1027f47 | 2010-08-12 01:35:30 +0200 | [diff] [blame] | 245 | int bytes_used = line6_midibuf_bytes_used(this); |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 246 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 247 | if (length > bytes_used) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 248 | length = bytes_used; |
| 249 | |
| 250 | this->pos_read = (this->pos_read + length) % this->size; |
| 251 | this->full = 0; |
| 252 | return length; |
| 253 | } |
| 254 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 255 | int line6_midibuf_skip_message(struct midi_buffer *this, unsigned short mask) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 256 | { |
| 257 | int cmd = this->command_prev; |
| 258 | |
Greg Kroah-Hartman | ce9b490 | 2009-02-27 22:43:11 -0800 | [diff] [blame] | 259 | if ((cmd >= 0x80) && (cmd < 0xf0)) |
| 260 | if ((mask & (1 << (cmd & 0x0f))) == 0) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 261 | return 1; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Stefan Hajnoczi | 269edc8 | 2013-01-11 23:08:09 +0100 | [diff] [blame] | 266 | void line6_midibuf_destroy(struct midi_buffer *this) |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 267 | { |
Greg Kroah-Hartman | 536165d | 2009-02-27 20:49:46 -0800 | [diff] [blame] | 268 | kfree(this->buf); |
| 269 | this->buf = NULL; |
Markus Grabner | 705ecec | 2009-02-27 19:43:04 -0800 | [diff] [blame] | 270 | } |