ASoC: rt5682: do not block workqueue if card is unbound
authorKai Vehmanen <kai.vehmanen@linux.intel.com>
Mon, 7 Feb 2022 15:30:00 +0000 (17:30 +0200)
committerMark Brown <broonie@kernel.org>
Tue, 8 Feb 2022 13:37:46 +0000 (13:37 +0000)
The current rt5682_jack_detect_handler() assumes the component
and card will always show up and implements an infinite usleep
loop waiting for them to show up.

This does not hold true if a codec interrupt (or other
event) occurs when the card is unbound. The codec driver's
remove  or shutdown functions cannot cancel the workqueue due
to the wait loop. As a result, code can either end up blocking
the workqueue, or hit a kernel oops when the card is freed.

Fix the issue by rescheduling the jack detect handler in
case the card is not ready. In case card never shows up,
the shutdown/remove/suspend calls can now cancel the detect
task.

Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Reviewed-by: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Reviewed-by: Péter Ujfalusi <peter.ujfalusi@linux.intel.com>
Reviewed-by: Shuming Fan <shumingf@realtek.com>
Link: https://lore.kernel.org/r/20220207153000.3452802-3-kai.vehmanen@linux.intel.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/codecs/rt5682.c

index 0a0ec4a021e111785b34875b8fbadbbaf2608343..be68d573a4906406ac3ebebef450ede8a21eef63 100644 (file)
@@ -1092,11 +1092,13 @@ void rt5682_jack_detect_handler(struct work_struct *work)
        struct snd_soc_dapm_context *dapm;
        int val, btn_type;
 
-       while (!rt5682->component)
-               usleep_range(10000, 15000);
-
-       while (!rt5682->component->card->instantiated)
-               usleep_range(10000, 15000);
+       if (!rt5682->component || !rt5682->component->card ||
+           !rt5682->component->card->instantiated) {
+               /* card not yet ready, try later */
+               mod_delayed_work(system_power_efficient_wq,
+                                &rt5682->jack_detect_work, msecs_to_jiffies(15));
+               return;
+       }
 
        dapm = snd_soc_component_get_dapm(rt5682->component);