ALSA: rawmidi: A lightweight function to discard pending bytes
authorTakashi Iwai <tiwai@suse.de>
Thu, 13 Sep 2018 06:20:43 +0000 (08:20 +0200)
committerTakashi Iwai <tiwai@suse.de>
Thu, 4 Oct 2018 18:13:17 +0000 (20:13 +0200)
For discarding the pending bytes on rawmidi, we process with a loop of
snd_rawmidi_transmit() which is just a waste of CPU power.
Implement a lightweight API function to discard the pending bytes and
the proceed the ring buffer instantly, and use it instead of open
codes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
include/sound/rawmidi.h
sound/core/rawmidi.c
sound/core/seq/seq_virmidi.c
sound/usb/midi.c

index 6665cb29e1a238af24b7dcb4df543c147cbad742..3b5a061132b6760c4cda087d67731b1231335900 100644 (file)
@@ -171,6 +171,7 @@ int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
                              unsigned char *buffer, int count);
 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream,
                               int count);
+int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);
 
 /* main midi functions */
 
index 08d5662039e38123e349bc5352c2b674280048f3..ee601d7f092694aecd7e853845b4e3f73cd0f261 100644 (file)
@@ -1236,6 +1236,28 @@ int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
 }
 EXPORT_SYMBOL(snd_rawmidi_transmit);
 
+/**
+ * snd_rawmidi_proceed - Discard the all pending bytes and proceed
+ * @substream: rawmidi substream
+ *
+ * Return: the number of discarded bytes
+ */
+int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
+{
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
+       unsigned long flags;
+       int count = 0;
+
+       spin_lock_irqsave(&runtime->lock, flags);
+       if (runtime->avail < runtime->buffer_size) {
+               count = runtime->buffer_size - runtime->avail;
+               __snd_rawmidi_transmit_ack(substream, count);
+       }
+       spin_unlock_irqrestore(&runtime->lock, flags);
+       return count;
+}
+EXPORT_SYMBOL(snd_rawmidi_proceed);
+
 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
                                      const unsigned char __user *userbuf,
                                      const unsigned char *kernelbuf,
index cb988efd1ed0d666494f76f23797fbde6a7e833e..e5a40795914a66acd9ca1d6bb1c2aedaafb4321d 100644 (file)
@@ -149,9 +149,7 @@ static void snd_vmidi_output_work(struct work_struct *work)
        /* discard the outputs in dispatch mode unless subscribed */
        if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
            !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
-               char buf[32];
-               while (snd_rawmidi_transmit(substream, buf, sizeof(buf)) > 0)
-                       ; /* ignored */
+               snd_rawmidi_proceed(substream);
                return;
        }
 
index dcfc546d81b924f455172e32557db3140880d5e1..b737f0ec77d090bf4abc5f87e388941912b3deb6 100644 (file)
@@ -1175,8 +1175,7 @@ static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
                if (port->ep->umidi->disconnected) {
                        /* gobble up remaining bytes to prevent wait in
                         * snd_rawmidi_drain_output */
-                       while (!snd_rawmidi_transmit_empty(substream))
-                               snd_rawmidi_transmit_ack(substream, 1);
+                       snd_rawmidi_proceed(substream);
                        return;
                }
                tasklet_schedule(&port->ep->tasklet);