diff options
author | Takashi Iwai <tiwai@suse.de> | 2018-07-17 17:07:29 -0400 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2018-07-17 17:07:29 -0400 |
commit | f5beb598b0c4dd023833ae1a7c188ecd987b7125 (patch) | |
tree | 06bada3fae446ba30e941ef6756eae3bf86d395e | |
parent | 7fdc9b08071b6a3fc85bf90b79e13f6e973a7e5e (diff) |
ALSA: rawmidi: Minor code refactoring
Unify a few open codes with helper functions to improve the
readability. Minor behavior changes (rather fixes) are:
- runtime->drain clearance is done within lock
- active_sensing is updated before resizing buffer in
SNDRV_RAWMIDI_IOCTL_PARAMS ioctl.
Other than that, simply code cleanups.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r-- | sound/core/rawmidi.c | 77 |
1 files changed, 33 insertions, 44 deletions
diff --git a/sound/core/rawmidi.c b/sound/core/rawmidi.c index 6b24c2d2dae6..cc944a3637a2 100644 --- a/sound/core/rawmidi.c +++ b/sound/core/rawmidi.c | |||
@@ -164,17 +164,28 @@ static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, i | |||
164 | cancel_work_sync(&substream->runtime->event_work); | 164 | cancel_work_sync(&substream->runtime->event_work); |
165 | } | 165 | } |
166 | 166 | ||
167 | int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream) | 167 | static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime, |
168 | bool is_input) | ||
169 | { | ||
170 | runtime->drain = 0; | ||
171 | runtime->appl_ptr = runtime->hw_ptr = 0; | ||
172 | runtime->avail = is_input ? 0 : runtime->buffer_size; | ||
173 | } | ||
174 | |||
175 | static void reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime, | ||
176 | bool is_input) | ||
168 | { | 177 | { |
169 | unsigned long flags; | 178 | unsigned long flags; |
170 | struct snd_rawmidi_runtime *runtime = substream->runtime; | ||
171 | 179 | ||
172 | snd_rawmidi_output_trigger(substream, 0); | ||
173 | runtime->drain = 0; | ||
174 | spin_lock_irqsave(&runtime->lock, flags); | 180 | spin_lock_irqsave(&runtime->lock, flags); |
175 | runtime->appl_ptr = runtime->hw_ptr = 0; | 181 | __reset_runtime_ptrs(runtime, is_input); |
176 | runtime->avail = runtime->buffer_size; | ||
177 | spin_unlock_irqrestore(&runtime->lock, flags); | 182 | spin_unlock_irqrestore(&runtime->lock, flags); |
183 | } | ||
184 | |||
185 | int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream) | ||
186 | { | ||
187 | snd_rawmidi_output_trigger(substream, 0); | ||
188 | reset_runtime_ptrs(substream->runtime, false); | ||
178 | return 0; | 189 | return 0; |
179 | } | 190 | } |
180 | EXPORT_SYMBOL(snd_rawmidi_drop_output); | 191 | EXPORT_SYMBOL(snd_rawmidi_drop_output); |
@@ -213,15 +224,8 @@ EXPORT_SYMBOL(snd_rawmidi_drain_output); | |||
213 | 224 | ||
214 | int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream) | 225 | int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream) |
215 | { | 226 | { |
216 | unsigned long flags; | ||
217 | struct snd_rawmidi_runtime *runtime = substream->runtime; | ||
218 | |||
219 | snd_rawmidi_input_trigger(substream, 0); | 227 | snd_rawmidi_input_trigger(substream, 0); |
220 | runtime->drain = 0; | 228 | reset_runtime_ptrs(substream->runtime, true); |
221 | spin_lock_irqsave(&runtime->lock, flags); | ||
222 | runtime->appl_ptr = runtime->hw_ptr = 0; | ||
223 | runtime->avail = 0; | ||
224 | spin_unlock_irqrestore(&runtime->lock, flags); | ||
225 | return 0; | 229 | return 0; |
226 | } | 230 | } |
227 | EXPORT_SYMBOL(snd_rawmidi_drain_input); | 231 | EXPORT_SYMBOL(snd_rawmidi_drain_input); |
@@ -639,15 +643,12 @@ static int snd_rawmidi_info_select_user(struct snd_card *card, | |||
639 | return 0; | 643 | return 0; |
640 | } | 644 | } |
641 | 645 | ||
642 | int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream, | 646 | static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime, |
643 | struct snd_rawmidi_params *params) | 647 | struct snd_rawmidi_params *params, |
648 | bool is_input) | ||
644 | { | 649 | { |
645 | char *newbuf, *oldbuf; | 650 | char *newbuf, *oldbuf; |
646 | struct snd_rawmidi_runtime *runtime = substream->runtime; | ||
647 | 651 | ||
648 | if (substream->append && substream->use_count > 1) | ||
649 | return -EBUSY; | ||
650 | snd_rawmidi_drain_output(substream); | ||
651 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) | 652 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) |
652 | return -EINVAL; | 653 | return -EINVAL; |
653 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) | 654 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) |
@@ -660,42 +661,30 @@ int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream, | |||
660 | oldbuf = runtime->buffer; | 661 | oldbuf = runtime->buffer; |
661 | runtime->buffer = newbuf; | 662 | runtime->buffer = newbuf; |
662 | runtime->buffer_size = params->buffer_size; | 663 | runtime->buffer_size = params->buffer_size; |
663 | runtime->avail = runtime->buffer_size; | 664 | __reset_runtime_ptrs(runtime, is_input); |
664 | runtime->appl_ptr = runtime->hw_ptr = 0; | ||
665 | spin_unlock_irq(&runtime->lock); | 665 | spin_unlock_irq(&runtime->lock); |
666 | kfree(oldbuf); | 666 | kfree(oldbuf); |
667 | } | 667 | } |
668 | runtime->avail_min = params->avail_min; | 668 | runtime->avail_min = params->avail_min; |
669 | substream->active_sensing = !params->no_active_sensing; | ||
670 | return 0; | 669 | return 0; |
671 | } | 670 | } |
671 | |||
672 | int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream, | ||
673 | struct snd_rawmidi_params *params) | ||
674 | { | ||
675 | if (substream->append && substream->use_count > 1) | ||
676 | return -EBUSY; | ||
677 | snd_rawmidi_drain_output(substream); | ||
678 | substream->active_sensing = !params->no_active_sensing; | ||
679 | return resize_runtime_buffer(substream->runtime, params, false); | ||
680 | } | ||
672 | EXPORT_SYMBOL(snd_rawmidi_output_params); | 681 | EXPORT_SYMBOL(snd_rawmidi_output_params); |
673 | 682 | ||
674 | int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, | 683 | int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, |
675 | struct snd_rawmidi_params *params) | 684 | struct snd_rawmidi_params *params) |
676 | { | 685 | { |
677 | char *newbuf, *oldbuf; | ||
678 | struct snd_rawmidi_runtime *runtime = substream->runtime; | ||
679 | |||
680 | snd_rawmidi_drain_input(substream); | 686 | snd_rawmidi_drain_input(substream); |
681 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) | 687 | return resize_runtime_buffer(substream->runtime, params, true); |
682 | return -EINVAL; | ||
683 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) | ||
684 | return -EINVAL; | ||
685 | if (params->buffer_size != runtime->buffer_size) { | ||
686 | newbuf = kmalloc(params->buffer_size, GFP_KERNEL); | ||
687 | if (!newbuf) | ||
688 | return -ENOMEM; | ||
689 | spin_lock_irq(&runtime->lock); | ||
690 | oldbuf = runtime->buffer; | ||
691 | runtime->buffer = newbuf; | ||
692 | runtime->buffer_size = params->buffer_size; | ||
693 | runtime->appl_ptr = runtime->hw_ptr = 0; | ||
694 | spin_unlock_irq(&runtime->lock); | ||
695 | kfree(oldbuf); | ||
696 | } | ||
697 | runtime->avail_min = params->avail_min; | ||
698 | return 0; | ||
699 | } | 688 | } |
700 | EXPORT_SYMBOL(snd_rawmidi_input_params); | 689 | EXPORT_SYMBOL(snd_rawmidi_input_params); |
701 | 690 | ||