sound: rawmidi: fix double init when opening MIDI device with O_APPEND
authorClemens Ladisch <clemens@ladisch.de>
Wed, 21 Oct 2009 07:09:38 +0000 (09:09 +0200)
committerTakashi Iwai <tiwai@suse.de>
Tue, 10 Nov 2009 15:20:43 +0000 (16:20 +0100)
Commit 9a1b64caac82aa02cb74587ffc798e6f42c6170a in 2.6.30 moved the
substream initialization code to where it would be executed every time
the substream is opened.

This had the consequence that any further opening would drop and leak
the data in the existing buffer, and that the device driver's open
callback would be called multiple times, unexpectedly.

Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
Cc: <stable@kernel.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/core/rawmidi.c

index c0adc14c91f0ad31b2f42084c794d7f1a70957a5..3071e6f5801e24ef6c12a062bde725d80df1b002 100644 (file)
@@ -266,17 +266,19 @@ static int open_substream(struct snd_rawmidi *rmidi,
 {
        int err;
 
-       err = snd_rawmidi_runtime_create(substream);
-       if (err < 0)
-               return err;
-       err = substream->ops->open(substream);
-       if (err < 0)
-               return err;
-       substream->opened = 1;
-       if (substream->use_count++ == 0)
+       if (substream->use_count == 0) {
+               err = snd_rawmidi_runtime_create(substream);
+               if (err < 0)
+                       return err;
+               err = substream->ops->open(substream);
+               if (err < 0)
+                       return err;
+               substream->opened = 1;
                substream->active_sensing = 0;
-       if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
-               substream->append = 1;
+               if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
+                       substream->append = 1;
+       }
+       substream->use_count++;
        rmidi->streams[substream->stream].substream_opened++;
        return 0;
 }