GNU Linux-libre 4.14.303-gnu1
[releases.git] / sound / usb / line6 / midibuf.c
1 /*
2  * Line 6 Linux USB driver
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
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
12 #include <linux/slab.h>
13
14 #include "midibuf.h"
15
16
17 static int midibuf_message_length(unsigned char code)
18 {
19         int message_length;
20
21         if (code < 0x80)
22                 message_length = -1;
23         else if (code < 0xf0) {
24                 static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
25
26                 message_length = length[(code >> 4) - 8];
27         } else {
28                 static const int length[] = { -1, 2, 2, 2, -1, -1, 1, 1, 1, -1,
29                         1, 1, 1, -1, 1, 1
30                 };
31                 message_length = length[code & 0x0f];
32         }
33
34         return message_length;
35 }
36
37 static int midibuf_is_empty(struct midi_buffer *this)
38 {
39         return (this->pos_read == this->pos_write) && !this->full;
40 }
41
42 static int midibuf_is_full(struct midi_buffer *this)
43 {
44         return this->full;
45 }
46
47 void line6_midibuf_reset(struct midi_buffer *this)
48 {
49         this->pos_read = this->pos_write = this->full = 0;
50         this->command_prev = -1;
51 }
52
53 int line6_midibuf_init(struct midi_buffer *this, int size, int split)
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
66 int line6_midibuf_bytes_free(struct midi_buffer *this)
67 {
68         return
69             midibuf_is_full(this) ?
70             0 :
71             (this->pos_read - this->pos_write + this->size - 1) % this->size +
72             1;
73 }
74
75 int line6_midibuf_bytes_used(struct midi_buffer *this)
76 {
77         return
78             midibuf_is_empty(this) ?
79             0 :
80             (this->pos_write - this->pos_read + this->size - 1) % this->size +
81             1;
82 }
83
84 int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
85                         int length)
86 {
87         int bytes_free;
88         int length1, length2;
89         int skip_active_sense = 0;
90
91         if (midibuf_is_full(this) || (length <= 0))
92                 return 0;
93
94         /* skip trailing active sense */
95         if (data[length - 1] == 0xfe) {
96                 --length;
97                 skip_active_sense = 1;
98         }
99
100         bytes_free = line6_midibuf_bytes_free(this);
101
102         if (length > bytes_free)
103                 length = bytes_free;
104
105         if (length > 0) {
106                 length1 = this->size - this->pos_write;
107
108                 if (length < length1) {
109                         /* no buffer wraparound */
110                         memcpy(this->buf + this->pos_write, data, length);
111                         this->pos_write += length;
112                 } else {
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
120                 if (this->pos_write == this->pos_read)
121                         this->full = 1;
122         }
123
124         return length + skip_active_sense;
125 }
126
127 int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
128                        int length, int read_type)
129 {
130         int bytes_used;
131         int length1, length2;
132         int command;
133         int midi_length;
134         int repeat = 0;
135         int i;
136
137         /* we need to be able to store at least a 3 byte MIDI message */
138         if (length < 3)
139                 return -EINVAL;
140
141         if (midibuf_is_empty(this))
142                 return 0;
143
144         bytes_used = line6_midibuf_bytes_used(this);
145
146         if (length > bytes_used)
147                 length = bytes_used;
148
149         length1 = this->size - this->pos_read;
150
151         command = this->buf[this->pos_read];
152         /*
153            PODxt always has status byte lower nibble set to 0010,
154            when it means to send 0000, so we correct if here so
155            that control/program changes come on channel 1 and
156            sysex message status byte is correct
157          */
158         if (read_type == LINE6_MIDIBUF_READ_RX) {
159                 if (command == 0xb2 || command == 0xc2 || command == 0xf2) {
160                         unsigned char fixed = command & 0xf0;
161                         this->buf[this->pos_read] = fixed;
162                         command = fixed;
163                 }
164         }
165
166         /* check MIDI command length */
167         if (command & 0x80) {
168                 midi_length = midibuf_message_length(command);
169                 this->command_prev = command;
170         } else {
171                 if (this->command_prev > 0) {
172                         int midi_length_prev =
173                             midibuf_message_length(this->command_prev);
174
175                         if (midi_length_prev > 1) {
176                                 midi_length = midi_length_prev - 1;
177                                 repeat = 1;
178                         } else
179                                 midi_length = -1;
180                 } else
181                         midi_length = -1;
182         }
183
184         if (midi_length < 0) {
185                 /* search for end of message */
186                 if (length < length1) {
187                         /* no buffer wraparound */
188                         for (i = 1; i < length; ++i)
189                                 if (this->buf[this->pos_read + i] & 0x80)
190                                         break;
191
192                         midi_length = i;
193                 } else {
194                         /* buffer wraparound */
195                         length2 = length - length1;
196
197                         for (i = 1; i < length1; ++i)
198                                 if (this->buf[this->pos_read + i] & 0x80)
199                                         break;
200
201                         if (i < length1)
202                                 midi_length = i;
203                         else {
204                                 for (i = 0; i < length2; ++i)
205                                         if (this->buf[i] & 0x80)
206                                                 break;
207
208                                 midi_length = length1 + i;
209                         }
210                 }
211
212                 if (midi_length == length)
213                         midi_length = -1;       /* end of message not found */
214         }
215
216         if (midi_length < 0) {
217                 if (!this->split)
218                         return 0;       /* command is not yet complete */
219         } else {
220                 if (length < midi_length)
221                         return 0;       /* command is not yet complete */
222
223                 length = midi_length;
224         }
225
226         if (length < length1) {
227                 /* no buffer wraparound */
228                 memcpy(data + repeat, this->buf + this->pos_read, length);
229                 this->pos_read += length;
230         } else {
231                 /* buffer wraparound */
232                 length2 = length - length1;
233                 memcpy(data + repeat, this->buf + this->pos_read, length1);
234                 memcpy(data + repeat + length1, this->buf, length2);
235                 this->pos_read = length2;
236         }
237
238         if (repeat)
239                 data[0] = this->command_prev;
240
241         this->full = 0;
242         return length + repeat;
243 }
244
245 int line6_midibuf_ignore(struct midi_buffer *this, int length)
246 {
247         int bytes_used = line6_midibuf_bytes_used(this);
248
249         if (length > bytes_used)
250                 length = bytes_used;
251
252         this->pos_read = (this->pos_read + length) % this->size;
253         this->full = 0;
254         return length;
255 }
256
257 void line6_midibuf_destroy(struct midi_buffer *this)
258 {
259         kfree(this->buf);
260         this->buf = NULL;
261 }