Skip to content

Commit 0c79030

Browse files
gtrainaviciuspopcornmix
authored andcommitted
Pisound Micro: Fix for MIDI output under full load.
This fixes MIDI output of Pisound Micro after running for a while under full load and increases timing stability. Signed-off-by: Giedrius Trainavičius <[email protected]>
1 parent a5c11f9 commit 0c79030

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

sound/drivers/upisnd/upisnd_comm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ int upisnd_comm_init(struct upisnd_instance *instance,
327327

328328
int upisnd_comm_send_midi(struct upisnd_instance *instance, const void *data, unsigned int n)
329329
{
330-
if (n >= UPISND_MAX_PACKET_LENGTH)
330+
if (n == 0 || n >= UPISND_MAX_PACKET_LENGTH)
331331
return -EINVAL;
332332

333333
u8 buffer[UPISND_MAX_PACKET_LENGTH];

sound/drivers/upisnd/upisnd_midi.c

+30-19
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static const struct snd_rawmidi_global_ops upisnd_midi_ops = {
129129

130130
static void upisnd_midi_in_handler(struct work_struct *work)
131131
{
132-
int i, n, err;
132+
int n, err;
133133

134134
printd("In handler");
135135
struct upisnd_instance *instance = container_of(work,
@@ -153,8 +153,7 @@ static void upisnd_midi_in_handler(struct work_struct *work)
153153
instance->midi.rx_cnt += err;
154154
}
155155

156-
for (i = 0; i < err; ++i)
157-
kfifo_skip(&instance->midi.midi_in_fifo);
156+
kfifo_skip_count(&instance->midi.midi_in_fifo, err);
158157

159158
if (!kfifo_is_empty(&instance->midi.midi_in_fifo) &&
160159
!work_pending(&instance->midi.midi_in_handler)) {
@@ -193,25 +192,37 @@ static void upisnd_midi_out_handler(struct work_struct *work)
193192
instance->midi.output_buffer_used_in_millibytes) / 1000;
194193

195194
u8 buffer[UPISND_MAX_PACKET_LENGTH - 1];
195+
int n, batch, err;
196196

197197
printd("Available: %u", output_buffer_available);
198-
int n = snd_rawmidi_transmit_peek(instance->midi.midi_output,
199-
buffer,
200-
min(output_buffer_available, sizeof(buffer)));
201-
202-
if (n > 0) {
203-
printd("Peeked: %d", n);
204-
snd_rawmidi_transmit_ack(instance->midi.midi_output, n);
205-
n = upisnd_comm_send_midi(instance, buffer, (unsigned int)n);
206-
if (n < 0)
207-
printe("Error occurred when sending MIDI data over I2C! (%d)", n);
208-
} else {
209-
printe("snd_rawmidi_transmit_peek returned error %d!", n);
210-
goto cleanup;
211-
}
212198

213-
instance->midi.tx_cnt += n;
214-
instance->midi.output_buffer_used_in_millibytes += n * 1000;
199+
for (batch = 0; batch < 3; ++batch) {
200+
if (output_buffer_available == 0)
201+
break;
202+
203+
n = snd_rawmidi_transmit_peek(instance->midi.midi_output,
204+
buffer,
205+
min(output_buffer_available, sizeof(buffer)));
206+
207+
if (n > 0) {
208+
printd("Peeked: %d (batch %d)", n, batch);
209+
err = upisnd_comm_send_midi(instance, buffer, (unsigned int)n);
210+
if (err < 0) {
211+
printe("Error occurred when sending MIDI data over I2C! (%d)", n);
212+
goto cleanup;
213+
}
214+
snd_rawmidi_transmit_ack(instance->midi.midi_output, n);
215+
216+
instance->midi.tx_cnt += n;
217+
instance->midi.output_buffer_used_in_millibytes += n * 1000;
218+
output_buffer_available -= n;
219+
} else if (n < 0) {
220+
printe("snd_rawmidi_transmit_peek returned error %d!", n);
221+
goto cleanup;
222+
} else {
223+
break;
224+
}
225+
}
215226

216227
printd("Checking if empty %p", instance->midi.midi_output);
217228
if (!snd_rawmidi_transmit_empty(instance->midi.midi_output)) {

sound/drivers/upisnd/upisnd_protocol.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ static inline u8 upisnd_cmd_decode_length(u8 b)
273273

274274
static inline u8 upisnd_cmd_encode(enum upisnd_cmd_type_e type, u8 size)
275275
{
276-
return (size < UPISND_MAX_PACKET_LENGTH && size > 0) ? (type | (size - 1)) : 0xff;
276+
return (size <= UPISND_MAX_PACKET_LENGTH && size > 0) ? (type | (size - 1))
277+
: UPISND_CMD_INVALID;
277278
}
278279

279280
static inline u8 upisnd_msg_id_encode(upisnd_msg_id_t msg_id, bool response)

0 commit comments

Comments
 (0)