1 // SPDX-License-Identifier: GPL-2.0-only
3 * Line 6 Linux USB driver
5 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
8 #include <linux/slab.h>
12 static int midibuf_message_length(unsigned char code
)
18 else if (code
< 0xf0) {
19 static const int length
[] = { 3, 3, 3, 3, 2, 2, 3 };
21 message_length
= length
[(code
>> 4) - 8];
24 Note that according to the MIDI specification 0xf2 is
25 the "Song Position Pointer", but this is used by Line 6
26 to send sysex messages to the host.
28 static const int length
[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
31 message_length
= length
[code
& 0x0f];
34 return message_length
;
37 static int midibuf_is_empty(struct midi_buffer
*this)
39 return (this->pos_read
== this->pos_write
) && !this->full
;
42 static int midibuf_is_full(struct midi_buffer
*this)
47 void line6_midibuf_reset(struct midi_buffer
*this)
49 this->pos_read
= this->pos_write
= this->full
= 0;
50 this->command_prev
= -1;
53 int line6_midibuf_init(struct midi_buffer
*this, int size
, int split
)
55 this->buf
= kmalloc(size
, GFP_KERNEL
);
57 if (this->buf
== NULL
)
62 line6_midibuf_reset(this);
66 int line6_midibuf_bytes_free(struct midi_buffer
*this)
69 midibuf_is_full(this) ?
71 (this->pos_read
- this->pos_write
+ this->size
- 1) % this->size
+
75 int line6_midibuf_bytes_used(struct midi_buffer
*this)
78 midibuf_is_empty(this) ?
80 (this->pos_write
- this->pos_read
+ this->size
- 1) % this->size
+
84 int line6_midibuf_write(struct midi_buffer
*this, unsigned char *data
,
89 int skip_active_sense
= 0;
91 if (midibuf_is_full(this) || (length
<= 0))
94 /* skip trailing active sense */
95 if (data
[length
- 1] == 0xfe) {
97 skip_active_sense
= 1;
100 bytes_free
= line6_midibuf_bytes_free(this);
102 if (length
> bytes_free
)
106 length1
= this->size
- this->pos_write
;
108 if (length
< length1
) {
109 /* no buffer wraparound */
110 memcpy(this->buf
+ this->pos_write
, data
, length
);
111 this->pos_write
+= length
;
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
;
120 if (this->pos_write
== this->pos_read
)
124 return length
+ skip_active_sense
;
127 int line6_midibuf_read(struct midi_buffer
*this, unsigned char *data
,
131 int length1
, length2
;
137 /* we need to be able to store at least a 3 byte MIDI message */
141 if (midibuf_is_empty(this))
144 bytes_used
= line6_midibuf_bytes_used(this);
146 if (length
> bytes_used
)
149 length1
= this->size
- this->pos_read
;
151 /* check MIDI command length */
152 command
= this->buf
[this->pos_read
];
154 if (command
& 0x80) {
155 midi_length
= midibuf_message_length(command
);
156 this->command_prev
= command
;
158 if (this->command_prev
> 0) {
159 int midi_length_prev
=
160 midibuf_message_length(this->command_prev
);
162 if (midi_length_prev
> 1) {
163 midi_length
= midi_length_prev
- 1;
171 if (midi_length
< 0) {
172 /* search for end of message */
173 if (length
< length1
) {
174 /* no buffer wraparound */
175 for (i
= 1; i
< length
; ++i
)
176 if (this->buf
[this->pos_read
+ i
] & 0x80)
181 /* buffer wraparound */
182 length2
= length
- length1
;
184 for (i
= 1; i
< length1
; ++i
)
185 if (this->buf
[this->pos_read
+ i
] & 0x80)
191 for (i
= 0; i
< length2
; ++i
)
192 if (this->buf
[i
] & 0x80)
195 midi_length
= length1
+ i
;
199 if (midi_length
== length
)
200 midi_length
= -1; /* end of message not found */
203 if (midi_length
< 0) {
205 return 0; /* command is not yet complete */
207 if (length
< midi_length
)
208 return 0; /* command is not yet complete */
210 length
= midi_length
;
213 if (length
< length1
) {
214 /* no buffer wraparound */
215 memcpy(data
+ repeat
, this->buf
+ this->pos_read
, length
);
216 this->pos_read
+= length
;
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
;
226 data
[0] = this->command_prev
;
229 return length
+ repeat
;
232 int line6_midibuf_ignore(struct midi_buffer
*this, int length
)
234 int bytes_used
= line6_midibuf_bytes_used(this);
236 if (length
> bytes_used
)
239 this->pos_read
= (this->pos_read
+ length
) % this->size
;
244 void line6_midibuf_destroy(struct midi_buffer
*this)