Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume
Subsystems (3)
cover Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume
A number of issues in system suspend and system resume have been reported on the CS35L41 HDA driver. These patches fix some of the issues, as well as improve the suspend/resume code in the driver.
The main issues resolved are:
- CS35L41 boot issues after system resume (on SPI systems using S4 hibernation)
- a deadlock during system suspend due to a race condition with the filesystem
Also included is a patch to prevent issues with IRQs during suspend/resume.
In addition, there are 3 patches which prepare the driver for these improvements and fixes:
- ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
- ALSA: hda: cs35l41: Unset irq if not used or configured
- ALSA: hda/cs35l41: Remove support for alternative firmware types
Stefan Binding (7):
ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
ASoC: cs35l41: Prevent software reset when using SPI
ALSA: hda: cs35l41: Unset irq if not used or configured
ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
ALSA: hda/cs35l41: Remove support for alternative firmware types
ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and
tunings
ALSA: hda/cs35l41: rework how firmware requests are queued and flushed
to prevent races
sound/hda/codecs/side-codecs/cs35l41_hda.c | 362 +++++++++++++-------- sound/hda/codecs/side-codecs/cs35l41_hda.h | 7 +- sound/soc/codecs/cs35l41-lib.c | 51 ++- 3 files changed, 275 insertions(+), 145 deletions(-)
Signature · 1 line
2.43.0
PATCH 4/7 ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
Currently, the CS35L41 driver does most of its system suspend and resume functions inside single suspend and resume callbacks.
Ensure IRQs are correctly disabled and enabled at the right times during the System Suspend and Resume process, to ensure we don't get spurious interrupts which cause issues.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 91 ++++++++++++++++++---- 1 file changed, 78 insertions(+), 13 deletions(-)
sound/hda/codecs/side-codecs/cs35l41_hda.c +78 -13
@@ -942,10 +942,8 @@ static int cs35l41_system_suspend(struct device *dev)dev_dbg (cs35l41->dev,"System Suspend\n" ); -if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) { -dev_err_once (cs35l41->dev,"System Suspend not supported\n" ); +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH )return 0 ;/* don't block the whole system suspend */ - } scoped_guard(mutex, &cs35l41->fw_mutex) {if (cs35l41->playback_started)
@@ -963,15 +961,75 @@ static int cs35l41_system_suspend(struct device *dev)if (ret)dev_err (dev,"System Suspend Failed, not ready for Reset: %d\n" , ret); + +/* +* The interrupt line is normally shared, but after we start suspending +* we can't check if our device is the source of an interrupt, and can't +* clear it. Prevent this race by temporarily disabling the parent irq +* until we reach _noirq. +*/ +if (cs35l41->irq) +disable_irq (cs35l41->irq); + +dev_dbg (cs35l41->dev,"System Suspended\n" ); + +return ret; +} + +static int cs35l41_system_suspend_late (struct device *dev) +{ +struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev); + +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) +return 0 ;/* don't block the whole system suspend */ + +/* +* RESET is usually shared by all amps so it must not be asserted until +* all driver instances have done their suspend() stage. +*/ if (cs35l41->reset_gpio) {dev_info (cs35l41->dev,"Asserting Reset\n" );gpiod_set_value_cansleep (cs35l41->reset_gpio,0 );usleep_range (2000 ,2100 ); } -dev_dbg (cs35l41->dev,"System Suspended\n" ); +return 0 ; +} -return ret; +static int cs35l41_system_suspend_noirq (struct device *dev) +{ +struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev); + +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) +return 0 ;/* don't block the whole system suspend */ + +/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */ +if (cs35l41->irq) +enable_irq (cs35l41->irq); + +return 0 ; +} + +static int cs35l41_system_resume_noirq (struct device *dev) +{ +struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev); + +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) { +dev_err_once (cs35l41->dev,"System Resume not supported\n" ); +return 0 ;/* don't block the whole system resume */ + } + +/* +* WAKE interrupts unmask if the CS35L41 resets, which can cause +* spurious interrupts, and the interrupt line is normally shared. +* We can't check if our device is the source of an interrupt, and can't +* clear it, until it has fully resumed. Prevent this race by temporarily +* disabling the parent irq until we complete resume(). +*/ +if (cs35l41->irq) +disable_irq (cs35l41->irq); + +return 0 ; }static int cs35l41_system_resume (struct device *dev)
@@ -981,28 +1039,32 @@ static int cs35l41_system_resume(struct device *dev)dev_dbg (cs35l41->dev,"System Resume\n" ); -if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) { -dev_err_once (cs35l41->dev,"System Resume not supported\n" ); +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH )return 0 ;/* don't block the whole system resume */ - }if (cs35l41->reset_gpio) { +dev_info (cs35l41->dev,"Deasserting Reset\n" );gpiod_set_value_cansleep (cs35l41->reset_gpio,0 );usleep_range (2000 ,2100 );gpiod_set_value_cansleep (cs35l41->reset_gpio,1 ); +usleep_range (2000 ,2100 ); } -usleep_range (2000 ,2100 ); -regcache_cache_only (cs35l41->regmap, false); ret =cs35l41_reset_and_boot (cs35l41->dev, cs35l41->regmap); -if (ret) -return ret;regcache_cache_only (cs35l41->regmap, true); +if (ret) { +dev_err (cs35l41->dev,"System Resume Failed: Boot Failed: %d\n" , ret); +if (cs35l41->irq) +enable_irq (cs35l41->irq); +return ret; + } ret =pm_runtime_force_resume (dev); +if (cs35l41->irq) +enable_irq (cs35l41->irq);if (ret) {dev_err (dev,"System Resume Failed: Unable to runtime resume: %d\n" , ret);return ret;
@@ -1015,7 +1077,7 @@ static int cs35l41_system_resume(struct device *dev)schedule_work (&cs35l41->fw_load_work); } -return ret; +return 0 ; }static int cs35l41_runtime_idle (struct device *dev)
@@ -2083,6 +2145,9 @@ const struct dev_pm_ops cs35l41_hda_pm_ops = { cs35l41_runtime_idle) .prepare = cs35l41_system_suspend_prep, SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume) + LATE_SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend_late, NULL) +NOIRQ_SYSTEM_SLEEP_PM_OPS (cs35l41_system_suspend_noirq, + cs35l41_system_resume_noirq) };EXPORT_SYMBOL_NS_GPL (cs35l41_hda_pm_ops,"SND_HDA_SCODEC_CS35L41" );
Signature · 1 line
2.43.0
PATCH 2/7 ASoC: cs35l41: Prevent software reset when using SPI
In reply to Stefan BindingIf there is any SPI Bus activity during a software reset, even if the chip select is not set for that Amp, the amp may not boot.
This affects both CS35L41 ASoC and HDA drivers. For HDA this also affects system resume.
Whilst the driver needs the amp to be reset when coming out of system suspend, the driver will always toggle the reset GPIO off and on to ensure this occurs. If a reset GPIO is not defined for the amplifier, the amplifier will not probe - unless the amp is using a shared reset. In the case of a shared reset, the GPIO will control resets for both amps anyway.
--- sound/soc/codecs/cs35l41-lib.c | 51 ++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-)
sound/soc/codecs/cs35l41-lib.c +36 -15
@@ -13,6 +13,7 @@#include <linux/regulator/consumer.h> #include <linux/slab.h> #include <linux/firmware/cirrus/wmfw.h> +#include <linux/spi/spi.h> #include <sound/cs35l41.h>
@@ -1588,6 +1589,18 @@ int cs35l41_exit_hibernate(struct device *dev, struct regmap *regmap) }EXPORT_SYMBOL_GPL (cs35l41_exit_hibernate); +static bool cs35l41_is_spi_regmap (struct regmap *regmap) +{ +struct device *dev =regmap_get_device (regmap); + +if (!dev) { +dev_err (dev,"Failed to get device from regmap\n" ); +return false; + } + +return (IS_ENABLED (CONFIG_SPI ) && dev->bus == &spi_bus_type); +} +int cs35l41_wait_boot_done (struct device *dev,struct regmap *regmap) {ktime_t timeout;
@@ -1627,27 +1640,35 @@ int cs35l41_reset_and_boot(struct device *dev, struct regmap *regmap) {int ret; - ret =regmap_write (regmap,CS35L41_SFT_RESET ,CS35L41_SOFTWARE_RESET ); -if (ret) { -dev_warn (dev,"Failed to write software reset: %d\n" , ret); - +if (cs35l41_is_spi_regmap (regmap)) {/* -* If the write failed, it is likely because the chip is still booting after a -* reset. Wait for boot, then retry the software reset. If the initial write failed -* for another reason, then either waiting for boot, or the second write will fail, -* So we can return the error then. +* If there is any SPI Bus activity during a software reset, even if the +* chip select is not set for that Amp, the amp may not boot. */ - ret =cs35l41_wait_boot_done (dev, regmap); -if (ret) -return ret; - +dev_dbg (dev,"Preventing Software Reset on SPI\n" ); + }else { ret =regmap_write (regmap,CS35L41_SFT_RESET ,CS35L41_SOFTWARE_RESET );if (ret) { -dev_err (dev,"Failed to write software reset on retry: %d\n" , ret); -return ret; +dev_warn (dev,"Failed to write software reset: %d\n" , ret); + +/* +* If the write failed, it is likely because the chip is still booting after +* a reset. Wait for boot, then retry the software reset. If the initial +* write failed for another reason, then either waiting for boot, or the +* second write will fail, so we can return the error then. +*/ + ret =cs35l41_wait_boot_done (dev, regmap); +if (ret) +return ret; + + ret =regmap_write (regmap,CS35L41_SFT_RESET ,CS35L41_SOFTWARE_RESET ); +if (ret) { +dev_err (dev,"Failed to write software reset on retry: %d\n" , ret); +return ret; + } } +usleep_range (2000 ,2100 ); } -usleep_range (2000 ,2100 );return cs35l41_wait_boot_done (dev, regmap); }
Signature · 1 line
2.43.0
PATCH 3/7 ALSA: hda: cs35l41: Unset irq if not used or configured
In reply to Stefan BindingIf the irq is not allocated, or does not exist in ACPI, clear the saved irq value. This prevents the driver from being able to access an unconfigured or unused irq later.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 5 +++++ 1 file changed, 5 insertions(+)
sound/hda/codecs/side-codecs/cs35l41_hda.c +5 -0
@@ -1633,6 +1633,8 @@ static void cs35l41_configure_interrupt(struct cs35l41_hda *cs35l41, int irq_polIRQF_ONESHOT |IRQF_SHARED | irq_pol, 0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);if (ret) { +/* unset irq if unable to be used */ + cs35l41->irq =0 ;dev_dbg (cs35l41->dev,"Unable to add IRQ Chip: %d." , ret); goto err; }
@@ -1712,6 +1714,9 @@ static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)if (using_irq)cs35l41_configure_interrupt (cs35l41, irq_pol); +else +/* Unset irq if not used */ + cs35l41->irq =0 ;return cs35l41_hda_channel_map (cs35l41); }
Signature · 1 line
2.43.0
PATCH 6/7 ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings
In reply to Stefan BindingWhen the CS35L41 HDA driver loads firmware, it typically loops through possible filenames, requesting each and stopping when it finds a valid file.
Since firmware is sometimes reloaded - such as during system resume, it loops through the possible filenames each time. However, since the filename will never change, we can store the filename when we successfully load the firmware for the first time, and reuse it later.
This ensures that the only firmware files requested by the driver in system resume are the same as the ones cached during suspend, since only successfully requested firmware files are cached. This prevents unnecessary file access during resume - if a cached firmware is requested, the cached file is used, eliminating any potential blocking calls to the filesystem.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 47 +++++++++++++++++----- sound/hda/codecs/side-codecs/cs35l41_hda.h | 3 ++ 2 files changed, 40 insertions(+), 10 deletions(-)
sound/hda/codecs/side-codecs/cs35l41_hda.c +37 -10
@@ -142,6 +142,7 @@ static int cs35l41_request_tuning_param_file(struct cs35l41_hda *cs35l41, char *dev_dbg (cs35l41->dev,"Failed to request '%s'\n" , *filename);kfree (*filename); *filename =NULL ; + cs35l41->skip_tuning_params = true; }return ret;
@@ -308,6 +309,7 @@ static int cs35l41_fallback_firmware_file(struct cs35l41_hda *cs35l41,if (ret) {release_firmware (*wmfw_firmware);kfree (*wmfw_filename); + *wmfw_filename =NULL ; goto err; }return 0 ;
@@ -325,6 +327,25 @@ static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41, {int ret; +if (*wmfw_filename) { + ret =firmware_request_nowarn (wmfw_firmware, *wmfw_filename, cs35l41->dev); +if (ret !=0 ) { +dev_err (cs35l41->dev,"Failed to request known file '%s'\n" , + *wmfw_filename); +return ret; + } +if (*coeff_filename) { + ret =firmware_request_nowarn (coeff_firmware, *coeff_filename, + cs35l41->dev); +if (ret !=0 ) { +dev_err (cs35l41->dev,"Failed to request known file '%s'\n" , + *coeff_filename); +return ret; + } + } +return 0 ; + } +if (cs35l41->speaker_id >-1 ) { ret =cs35l41_request_firmware_files_spkid (cs35l41, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename);
@@ -374,6 +395,7 @@ static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41, coeff_err:release_firmware (*wmfw_firmware);kfree (*wmfw_filename); + *wmfw_filename =NULL ; fallback:return cs35l41_fallback_firmware_file (cs35l41, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename);
@@ -514,6 +536,9 @@ static int cs35l41_load_tuning_params(struct cs35l41_hda *cs35l41, char *tuning_ char *tuning_param_filename __free(kfree) = NULL;int ret; +if (cs35l41->skip_tuning_params) +return 0 ; + ret =cs35l41_request_tuning_param_file (cs35l41, tuning_filename, &tuning_param_file, &tuning_param_filename, cs35l41->acpi_subsystem_id);if (ret) {
@@ -538,8 +563,6 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41) const struct firmware *coeff_firmware __free(firmware) = NULL; const struct firmware *wmfw_firmware __free(firmware) = NULL;struct cs_dsp *dsp = &cs35l41->cs_dsp; - char *coeff_filename __free(kfree) = NULL; - char *wmfw_filename __free(kfree) = NULL;int ret;if (!cs35l41->halo_initialized) {
@@ -554,23 +577,23 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)cs35l41_set_default_tuning_params (cs35l41); - ret =cs35l41_request_firmware_files (cs35l41, &wmfw_firmware, &wmfw_filename, - &coeff_firmware, &coeff_filename); + ret =cs35l41_request_firmware_files (cs35l41, &wmfw_firmware, &cs35l41->wmfw_filename, + &coeff_firmware, &cs35l41->coeff_filename);if (ret <0 )return ret; -dev_dbg (cs35l41->dev,"Loading WMFW Firmware: %s\n" , wmfw_filename); -if (coeff_filename) { -dev_dbg (cs35l41->dev,"Loading Coefficient File: %s\n" , coeff_filename); - ret =cs35l41_load_tuning_params (cs35l41, coeff_filename); +dev_dbg (cs35l41->dev,"Loading WMFW Firmware: %s\n" , cs35l41->wmfw_filename); +if (cs35l41->coeff_filename) { +dev_dbg (cs35l41->dev,"Loading Coefficient File: %s\n" , cs35l41->coeff_filename); + ret =cs35l41_load_tuning_params (cs35l41, cs35l41->coeff_filename);if (ret)dev_warn (cs35l41->dev,"Unable to load Tuning Parameters: %d\n" , ret); }else {dev_warn (cs35l41->dev,"No Coefficient File available.\n" ); } - ret =cs_dsp_power_up (dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename, -CS35L41_HDA_FW_ID ); + ret =cs_dsp_power_up (dsp, wmfw_firmware, cs35l41->wmfw_filename, coeff_firmware, + cs35l41->coeff_filename,CS35L41_HDA_FW_ID );if (ret) {cs35l41_set_default_tuning_params (cs35l41);return ret;
@@ -600,6 +623,10 @@ static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)cs35l41_shutdown_dsp (cs35l41);cs_dsp_remove (dsp); cs35l41->halo_initialized = false; +kfree (cs35l41->wmfw_filename); +kfree (cs35l41->coeff_filename); + cs35l41->wmfw_filename =NULL ; + cs35l41->coeff_filename =NULL ; }/* Protection release cycle to get the speaker out of Safe-Mode */
sound/hda/codecs/side-codecs/cs35l41_hda.h +3 -0
@@ -93,7 +93,10 @@ struct cs35l41_hda {unsigned int tuning_gain;struct cirrus_amp_cal_data cal_data;bool cal_data_valid; +bool skip_tuning_params; +char *coeff_filename; +char *wmfw_filename; };enum halo_state {
Signature · 1 line
2.43.0
PATCH 5/7 ALSA: hda/cs35l41: Remove support for alternative firmware types
In reply to Stefan BindingOriginally the driver has support for loading alternative firmware types. However, whilst it could load alternative types, only speaker protection was ever actually supported in the driver.
Remove this support to ensure that the firmware type is fixed. This is preparing the driver to make proper use of firmware caching in later commits.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 86 ++-------------------- sound/hda/codecs/side-codecs/cs35l41_hda.h | 2 - 2 files changed, 8 insertions(+), 80 deletions(-)
sound/hda/codecs/side-codecs/cs35l41_hda.c +8 -78
@@ -37,6 +37,7 @@#define CS35L41_DSM_GET_MUTE 5#define CS35L41_NOTIFY_EVENT 0x91#define CS35L41_TUNING_SIG 0x109A4A35 +#define CS35L41_HDA_FW_ID "spk-prot"enum cs35l41_tuning_param_types {TUNING_PARAM_GAIN ,
@@ -73,21 +74,6 @@ static const struct cirrus_amp_cal_controls cs35l41_calibration_controls = { .checksum = CAL_CHECKSUM_DSP_CTL_NAME, }; -enum cs35l41_hda_fw_id { -CS35L41_HDA_FW_SPK_PROT , -CS35L41_HDA_FW_SPK_CALI , -CS35L41_HDA_FW_SPK_DIAG , -CS35L41_HDA_FW_MISC , -CS35L41_HDA_NUM_FW -}; - -static const char *const cs35l41_hda_fw_ids[CS35L41_HDA_NUM_FW ] = { - [CS35L41_HDA_FW_SPK_PROT ] ="spk-prot" , - [CS35L41_HDA_FW_SPK_CALI ] ="spk-cali" , - [CS35L41_HDA_FW_SPK_DIAG ] ="spk-diag" , - [CS35L41_HDA_FW_MISC ] ="misc" , -}; -static bool firmware_autostart =1 ;module_param (firmware_autostart, bool,0444 ); MODULE_PARM_DESC(firmware_autostart, "Allow automatic firmware download on boot"
@@ -173,24 +159,19 @@ static int cs35l41_request_firmware_file(struct cs35l41_hda *cs35l41, *firmware =NULL ;if (spkid >-1 && ssid && amp_name) *filename =kasprintf (GFP_KERNEL ,"cirrus/%s-%s-%s-%s-spkid%d-%s.%s" ,CS35L41_PART , - dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type], - ssid, spkid, amp_name, filetype); + dsp_name,CS35L41_HDA_FW_ID , ssid, spkid, amp_name, filetype);else if (spkid >-1 && ssid) *filename =kasprintf (GFP_KERNEL ,"cirrus/%s-%s-%s-%s-spkid%d.%s" ,CS35L41_PART , - dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type], - ssid, spkid, filetype); + dsp_name,CS35L41_HDA_FW_ID , ssid, spkid, filetype);else if (ssid && amp_name) *filename =kasprintf (GFP_KERNEL ,"cirrus/%s-%s-%s-%s-%s.%s" ,CS35L41_PART , - dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type], - ssid, amp_name, filetype); + dsp_name,CS35L41_HDA_FW_ID , ssid, amp_name, filetype);else if (ssid) *filename =kasprintf (GFP_KERNEL ,"cirrus/%s-%s-%s-%s.%s" ,CS35L41_PART , - dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type], - ssid, filetype); + dsp_name,CS35L41_HDA_FW_ID , ssid, filetype);else *filename =kasprintf (GFP_KERNEL ,"cirrus/%s-%s-%s.%s" ,CS35L41_PART , - dsp_name, cs35l41_hda_fw_ids[cs35l41->firmware_type], - filetype); + dsp_name,CS35L41_HDA_FW_ID , filetype);if (*filename ==NULL )return -ENOMEM ;
@@ -589,7 +570,7 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41) } ret =cs_dsp_power_up (dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename, - cs35l41_hda_fw_ids[cs35l41->firmware_type]); +CS35L41_HDA_FW_ID );if (ret) {cs35l41_set_default_tuning_params (cs35l41);return ret;
@@ -1237,7 +1218,7 @@ static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41) }dev_info (cs35l41->dev,"Firmware Loaded - Type: %s, Gain: %d\n" , - cs35l41_hda_fw_ids[cs35l41->firmware_type], cs35l41->tuning_gain); +CS35L41_HDA_FW_ID , cs35l41->tuning_gain);return 0 ;
@@ -1319,38 +1300,6 @@ static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,return 1 ; } -static int cs35l41_fw_type_ctl_get (struct snd_kcontrol *kcontrol, -struct snd_ctl_elem_value *ucontrol) -{ -struct cs35l41_hda *cs35l41 =snd_kcontrol_chip (kcontrol); - - ucontrol->value.enumerated.item[0 ] = cs35l41->firmware_type; - -return 0 ; -} - -static int cs35l41_fw_type_ctl_put (struct snd_kcontrol *kcontrol, -struct snd_ctl_elem_value *ucontrol) -{ -struct cs35l41_hda *cs35l41 =snd_kcontrol_chip (kcontrol); - -if (ucontrol->value.enumerated.item[0 ] <CS35L41_HDA_NUM_FW ) { -if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0 ]) { - cs35l41->firmware_type = ucontrol->value.enumerated.item[0 ]; -return 1 ; - }else { -return 0 ; - } - } - -return -EINVAL ; -} - -static int cs35l41_fw_type_ctl_info (struct snd_kcontrol *kcontrol,struct snd_ctl_elem_info *uinfo) -{ -return snd_ctl_enum_info (uinfo,1 ,ARRAY_SIZE (cs35l41_hda_fw_ids), cs35l41_hda_fw_ids); -} -static void cs35l41_remove_controls (struct cs35l41_hda *cs35l41) {if (!cs35l41->codec)
@@ -1361,9 +1310,6 @@ static void cs35l41_remove_controls(struct cs35l41_hda *cs35l41)snd_ctl_remove (cs35l41->codec->card, cs35l41->fw_load_ctl); cs35l41->fw_load_ctl =NULL ; - -snd_ctl_remove (cs35l41->codec->card, cs35l41->fw_type_ctl); - cs35l41->fw_type_ctl =NULL ; } static int cs35l41_add_control(struct cs35l41_hda *cs35l41,
@@ -1390,16 +1336,8 @@ static int cs35l41_add_control(struct cs35l41_hda *cs35l41,static int cs35l41_create_controls (struct cs35l41_hda *cs35l41) { -char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN ];char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN ];char mute_override_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN ]; -struct snd_kcontrol_new fw_type_ctl = { - .name = fw_type_ctl_name, - .iface =SNDRV_CTL_ELEM_IFACE_CARD , - .info = cs35l41_fw_type_ctl_info, - .get = cs35l41_fw_type_ctl_get, - .put = cs35l41_fw_type_ctl_put, - };struct snd_kcontrol_new fw_load_ctl = { .name = fw_load_ctl_name, .iface =SNDRV_CTL_ELEM_IFACE_CARD ,
@@ -1416,17 +1354,11 @@ static int cs35l41_create_controls(struct cs35l41_hda *cs35l41) };int ret; -scnprintf (fw_type_ctl_name,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ,"%s DSP1 Firmware Type" , - cs35l41->amp_name);scnprintf (fw_load_ctl_name,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ,"%s DSP1 Firmware Load" , cs35l41->amp_name);scnprintf (mute_override_ctl_name,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ,"%s Forced Mute Status" , cs35l41->amp_name); - ret =cs35l41_add_control (cs35l41, &fw_type_ctl, &cs35l41->fw_type_ctl); -if (ret) - goto err; - ret =cs35l41_add_control (cs35l41, &fw_load_ctl, &cs35l41->fw_load_ctl);if (ret) goto err;
@@ -1526,8 +1458,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masstrscpy (comp->name,dev_name (dev),sizeof (comp->name)); - cs35l41->firmware_type =CS35L41_HDA_FW_SPK_PROT ; -if (firmware_autostart) {dev_dbg (cs35l41->dev,"Firmware Autostart.\n" ); cs35l41->request_fw_load = true;
sound/hda/codecs/side-codecs/cs35l41_hda.h +0 -2
@@ -73,11 +73,9 @@ struct cs35l41_hda {unsigned volatile long irq_errors;const char *amp_name;const char *acpi_subsystem_id; -int firmware_type;int speaker_id;struct mutex fw_mutex;struct work_struct fw_load_work; -struct snd_kcontrol *fw_type_ctl;struct snd_kcontrol *fw_load_ctl;struct snd_kcontrol *mute_override_ctl;
Signature · 1 line
2.43.0
PATCH 1/7 ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls
In reply to Stefan BindingThese calls are there to prevent sleep when adding or removing device links in the bind/unbind calls. However, since these calls occur in probe and remove calls, where sleep is blocked, this is unnecessary.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 6 ------ 1 file changed, 6 deletions(-)
sound/hda/codecs/side-codecs/cs35l41_hda.c +0 -6
@@ -1443,7 +1443,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masstruct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);struct hda_component_parent *parent = master_data;struct hda_component *comp; -unsigned int sleep_flags;int ret =0 ; comp =hda_component_from_index (parent, cs35l41->index);
@@ -1492,10 +1491,8 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masmutex_unlock (&cs35l41->fw_mutex); - sleep_flags =lock_system_sleep ();if (!device_link_add (&cs35l41->codec->core.dev, cs35l41->dev,DL_FLAG_STATELESS ))dev_warn (dev,"Unable to create device link\n" ); -unlock_system_sleep (sleep_flags); dev_info(cs35l41->dev, "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
@@ -1512,16 +1509,13 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);struct hda_component_parent *parent = master_data;struct hda_component *comp; -unsigned int sleep_flags; comp =hda_component_from_index (parent, cs35l41->index);if (!comp)return ;if (comp->dev == dev) { - sleep_flags =lock_system_sleep ();device_link_remove (&cs35l41->codec->core.dev, cs35l41->dev); -unlock_system_sleep (sleep_flags);memset (comp,0 ,sizeof (*comp));cs35l41_remove_controls (cs35l41);
Signature · 1 line
2.43.0
PATCH 7/7 ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races
In reply to Stefan BindingCurrently, in a small number of scenarios, the firmware on CS35L41 can be reloaded:
- Using an ALSA control
- System Resume
- After Playback (after System Resume - if reload has not occurred)
In all these cases, the reload is done inside a work item and scheduled on the default workqueue.
This should instead be queued on the system_freezable_wq, which ensures that the work item is handled correctly during system suspend, where it is not allowed to start whilst the workqueue is frozen.
There are 3 main scenarios where we need to ensure firmware load work is flushed:
- Before System Suspend
- Before first playback after System Resume
- Error Paths
Register the driver for PM Notifications so that it can correctly flush before suspending. Ensure any firmware work is either cancelled (if pending) or flushed in the PM_*_PREPARE notification callbacks. This ensures that the work item completes before any workqueues are frozen. If the firmware load work item is still running when the workqueue is frozen, then it would ordinarily be flushed, but may hang on waiting for filesystem access in a call to request_firmware(), if filesystem access requires work in a freezable workqueue itself.
Additionally, ensure that we flush the work before we start playback after system resume. This ensures firmware is loaded before we restart any playback after system suspend.
We also need to re-queue the firmware load in the PM_POST_* notifiers, if the suspend was aborted before we finished suspending, but after the PM_*_PREPARE notifier cancelled a pending firmware load.
--- sound/hda/codecs/side-codecs/cs35l41_hda.c | 129 +++++++++++++++++---- sound/hda/codecs/side-codecs/cs35l41_hda.h | 2 + 2 files changed, 109 insertions(+), 22 deletions(-)
sound/hda/codecs/side-codecs/cs35l41_hda.c +107 -22
@@ -12,6 +12,7 @@#include <sound/hda_codec.h> #include <sound/soc.h> #include <linux/pm_runtime.h> +#include <linux/suspend.h> #include <linux/spi/spi.h> #include <linux/vmalloc.h> #include "hda_local.h"
@@ -603,6 +604,38 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)return 0 ; } +static void cs35l41_fw_load_request_unlocked (struct cs35l41_hda *cs35l41) +{ +/* firmware request is pending */ +if (cs35l41->fw_request_ongoing) +return ; + +/* no need to request firmware load if DSP running state matches request */ +if (cs35l41->cs_dsp.running == cs35l41->request_fw_load) +return ; + +/* block firmware load request if suspending */ +if (cs35l41->suspending) +return ; + + cs35l41->fw_request_ongoing =queue_work (system_freezable_wq, &cs35l41->fw_load_work); +} + +static void cs35l41_fw_load_request (struct cs35l41_hda *cs35l41) +{ +guard (mutex)(&cs35l41->fw_mutex); + +cs35l41_fw_load_request_unlocked (cs35l41); +} + +static void cs35l41_fw_load_cancel_sync (struct cs35l41_hda *cs35l41) +{ +cancel_work_sync (&cs35l41->fw_load_work); + +guard (mutex)(&cs35l41->fw_mutex); + cs35l41->fw_request_ongoing = false; +} +static void cs35l41_shutdown_dsp (struct cs35l41_hda *cs35l41) {struct cs_dsp *dsp = &cs35l41->cs_dsp;
@@ -617,7 +650,7 @@ static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41) {struct cs_dsp *dsp = &cs35l41->cs_dsp; -cancel_work_sync (&cs35l41->fw_load_work); +cs35l41_fw_load_cancel_sync (cs35l41);guard (mutex)(&cs35l41->fw_mutex);cs35l41_shutdown_dsp (cs35l41);
@@ -811,6 +844,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)pm_runtime_get_sync (dev);break ;case HDA_GEN_PCM_ACT_PREPARE : +flush_work (&cs35l41->fw_load_work); scoped_guard(mutex, &cs35l41->fw_mutex) {cs35l41_hda_play_start (dev); }
@@ -821,14 +855,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action) }break ;case HDA_GEN_PCM_ACT_CLOSE : - scoped_guard(mutex, &cs35l41->fw_mutex) { -if (!cs35l41->cs_dsp.running && cs35l41->request_fw_load && - !cs35l41->fw_request_ongoing) { -dev_info (dev,"Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n" ); - cs35l41->fw_request_ongoing = true; -schedule_work (&cs35l41->fw_load_work); - } - } +cs35l41_fw_load_request (cs35l41);/* * Playback must be finished for all amps before we start runtime suspend.
@@ -925,6 +952,45 @@ static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)return 0 ; } +static void cs35l41_set_suspending (struct cs35l41_hda *cs35l41,bool suspending) +{ +guard (mutex)(&cs35l41->fw_mutex); + + cs35l41->suspending = suspending; +} + +static int cs35l41_hda_pm_notifier (struct notifier_block *nb,unsigned long action,void *data) +{ + struct cs35l41_hda *cs35l41 = container_of(nb, struct cs35l41_hda, pm_notifier); + +dev_dbg (cs35l41->dev,"PM notifier action: %lu\n" , action); + +switch (action) { +case PM_HIBERNATION_PREPARE : +case PM_SUSPEND_PREPARE : +case PM_RESTORE_PREPARE : +cs35l41_set_suspending (cs35l41, true); +cs35l41_fw_load_cancel_sync (cs35l41); +break ; +case PM_POST_HIBERNATION : +case PM_POST_SUSPEND : +case PM_POST_RESTORE : +/* +* If suspending is still set, we are in the recovery path - reload firmware +* after failed suspend +*/ +if (cs35l41->suspending) { +cs35l41_set_suspending (cs35l41, false); +cs35l41_fw_load_request (cs35l41); + } +break ; +default : +break ; + } + +return NOTIFY_DONE ; +} +static int cs35l41_system_suspend_prep (struct device *dev) {struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);
@@ -980,6 +1046,7 @@ static int cs35l41_system_suspend(struct device *dev)disable_irq (cs35l41->irq);dev_dbg (cs35l41->dev,"System Suspended\n" ); +cs35l41_set_suspending (cs35l41, false);return ret; }
@@ -1078,12 +1145,7 @@ static int cs35l41_system_resume(struct device *dev)return ret; } -guard (mutex)(&cs35l41->fw_mutex); - -if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) { - cs35l41->fw_request_ongoing = true; -schedule_work (&cs35l41->fw_load_work); - } +cs35l41_fw_load_request (cs35l41);return 0 ; }
@@ -1320,9 +1382,11 @@ static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,return -EBUSY ; } - cs35l41->fw_request_ongoing = true; +guard (mutex)(&cs35l41->fw_mutex); + cs35l41->request_fw_load = ucontrol->value.integer.value[0 ]; -schedule_work (&cs35l41->fw_load_work); + +cs35l41_fw_load_request_unlocked (cs35l41);return 1 ; }
@@ -1464,7 +1528,7 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masstruct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);struct hda_component_parent *parent = master_data;struct hda_component *comp; -int ret =0 ; +int ret; comp =hda_component_from_index (parent, cs35l41->index);if (!comp)
@@ -1494,8 +1558,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masdev_dbg (cs35l41->dev,"Firmware Autostart is disabled.\n" ); } - ret =cs35l41_create_controls (cs35l41); - comp->playback_hook = cs35l41_hda_playback_hook; comp->pre_playback_hook = cs35l41_hda_pre_playback_hook; comp->post_playback_hook = cs35l41_hda_post_playback_hook;
@@ -1510,9 +1572,18 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masmutex_unlock (&cs35l41->fw_mutex); +/* Create the necessary controls, do not create them under fw_mutex */ + ret =cs35l41_create_controls (cs35l41); +if (ret) { +dev_err (cs35l41->dev,"Failed to create controls: %d\n" , ret); + goto err; + } +if (!device_link_add (&cs35l41->codec->core.dev, cs35l41->dev,DL_FLAG_STATELESS ))dev_warn (dev,"Unable to create device link\n" ); +register_pm_notifier (&cs35l41->pm_notifier); + dev_info(cs35l41->dev, "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n", cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
@@ -1520,6 +1591,15 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas channel_name[cs35l41->hw_cfg.spk_pos], cs35l41->cs_dsp.running, cs35l41->speaker_id); +return 0 ; +err: +memset (comp,0 ,sizeof (*comp)); + scoped_guard(mutex, &cs35l41->fw_mutex) { + cs35l41->request_fw_load = false; + } +cs35l41_fw_load_cancel_sync (cs35l41); + cs35l41->codec =NULL ; +return ret; }
@@ -1538,7 +1618,11 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *memset (comp,0 ,sizeof (*comp));cs35l41_remove_controls (cs35l41); -cancel_work_sync (&cs35l41->fw_load_work); + scoped_guard(mutex, &cs35l41->fw_mutex) { + cs35l41->request_fw_load = false; + } +unregister_pm_notifier (&cs35l41->pm_notifier); +cs35l41_fw_load_cancel_sync (cs35l41); cs35l41->codec =NULL ; } }
@@ -1970,6 +2054,7 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i cs35l41->irq = irq; cs35l41->regmap = regmap; cs35l41->control_bus = control_bus; + cs35l41->pm_notifier.notifier_call = cs35l41_hda_pm_notifier;dev_set_drvdata (dev, cs35l41); ret =cs35l41_hda_read_acpi (cs35l41, device_name, id);
@@ -2076,7 +2161,7 @@ void cs35l41_hda_remove(struct device *dev)struct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);component_del (cs35l41->dev, &cs35l41_hda_comp_ops); -cancel_work_sync (&cs35l41->fw_load_work); +cs35l41_fw_load_cancel_sync (cs35l41);pm_runtime_get_sync (cs35l41->dev);pm_runtime_dont_use_autosuspend (cs35l41->dev);
sound/hda/codecs/side-codecs/cs35l41_hda.h +2 -0
@@ -78,6 +78,8 @@ struct cs35l41_hda {struct work_struct fw_load_work;struct snd_kcontrol *fw_load_ctl;struct snd_kcontrol *mute_override_ctl; +struct notifier_block pm_notifier; +bool suspending;struct regmap_irq_chip_data *irq_data;bool firmware_running;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI
In reply to Stefan BindingThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ASoC: cs35l41: missing Fixes trailer for cs35l41_reset_and_boot() SPI fix --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ASoC: cs35l41: missing Fixes trailer for cs35l41_reset_and_boot() SPI fix
--
commit c3f7f4bdf676f39aef6330251414c7d8f0d98d22
Author: Stefan Binding <[email protected]>
Quoted 15 lines
ASoC: cs35l41: Prevent software reset when using SPI
If there is any SPI Bus activity during a software reset, even if the chip select is not set for that Amp, the amp may not boot.
This affects both CS35L41 ASoC and HDA drivers. For HDA this also affects system resume.
Whilst the driver needs the amp to be reset when coming out of system suspend, the driver will always toggle the reset GPIO off and on to ensure this occurs. If a reset GPIO is not defined for the amplifier, the amplifier will not probe - unless the amp is using a shared reset. In the case of a shared reset, the GPIO will control resets for both amps anyway.
Quoted 3 lines
Signed-off-by Stefan Binding <[email protected]>
[Severity: Low]
This fixes a boot issue introduced with cs35l41_reset_and_boot(). Should this include:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
In reply to Stefan BindingThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ALSA: hda/cs35l41: shared reset race in cs35l41_system_resume() - [Medium] ALSA: hda/cs35l41…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ALSA: hda/cs35l41: shared reset race in cs35l41_system_resume()
- [Medium] ALSA: hda/cs35l41: disable_irq() ordered after pm_runtime_force_suspend()
--
commit f2bfd6268c2ad165c328e195154a7eb4b196ae14
Author: Stefan Binding <[email protected]>
ALSA: hda/cs35l41: Split System Suspend/Resume callbacks
This commit splits CS35L41 HDA system suspend and resume into suspend_late, suspend_noirq, and resume_noirq callbacks to assert the shared reset GPIO after all instances have suspended and temporarily disable the shared IRQ line across suspend and resume transitions.
Quoted 4 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +0
-0
[ ... ]
sound/hda/codecs/side-codecs/cs35l41_hda.c static int cs35l41_system_suspend(struct device *dev) · line 972
Quoted 13 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +9
-0@@ -963,15 +961,75 @@ static int cs35l41_system_suspend(struct device *dev)if (ret)dev_err (dev,"System Suspend Failed, not ready for Reset: %d\n" , ret); + +/* +* The interrupt line is normally shared, but after we start suspending +* we can't check if our device is the source of an interrupt, and can't +* clear it. Prevent this race by temporarily disabling the parent irq +* until we reach _noirq. +*/ +if (cs35l41->irq) +disable_irq (cs35l41->irq);
[Severity: Medium]
Should disable_irq(cs35l41->irq) run before pm_runtime_force_suspend(dev) rather than after cs35l41_ready_for_reset()?
pm_runtime_force_suspend() disables runtime PM and runs cs35l41_runtime_suspend(), which puts the chip into hibernate or safe-reset and sets regcache_cache_only(cs35l41->regmap, true). If an interrupt fires on the shared line before disable_irq() is reached, regmap_irq_thread() runs while runtime PM is disabled and regmap is cache-only, causing pm_runtime_get_sync() or volatile IRQ status reads to fail without clearing the interrupt.
[ ... ]
Quoted 13 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +2
-3@@ -981,28 +1039,32 @@ static int cs35l41_system_resume(struct device *dev)dev_dbg (cs35l41->dev,"System Resume\n" ); -if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH ) { -dev_err_once (cs35l41->dev,"System Resume not supported\n" ); +if (cs35l41->hw_cfg.bst_type ==CS35L41_EXT_BOOST_NO_VSPK_SWITCH )return 0 ;/* don't block the whole system resume */ - }if (cs35l41->reset_gpio) { +dev_info (cs35l41->dev,"Deasserting Reset\n" );gpiod_set_value_cansleep (cs35l41->reset_gpio,0 );
sound/hda/codecs/side-codecs/cs35l41_hda.c static int cs35l41_system_resume(struct device *dev) · line 1055
Quoted 10 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +1
-2usleep_range (2000 ,2100 );gpiod_set_value_cansleep (cs35l41->reset_gpio,1 ); +usleep_range (2000 ,2100 ); } -usleep_range (2000 ,2100 ); -regcache_cache_only (cs35l41->regmap, false); ret =cs35l41_reset_and_boot (cs35l41->dev, cs35l41->regmap);
[Severity: High]
Can pulsing and releasing the shared reset_gpio in cs35l41_system_resume() race with sibling amplifier instances that have cs35l41->reset_gpio == NULL?
In cs35l41_hda_probe(), only the first amplifier acquires reset_gpio; sibling instances sharing the physical reset line get -EBUSY and set reset_gpio = NULL. With async resume enabled on I2C/SPI devices and the post-reset sleep moved inside the if (cs35l41->reset_gpio) block, the amplifiers can interleave during dpm_resume():
CPU0 (amp0, owns reset_gpio) CPU1 (amp1, reset_gpio == NULL)cs35l41_system_resume() cs35l41_system_resume()
gpiod_set_value_cansleep(..., 0)
usleep_range(2000, 2100)
regcache_cache_only(..., false)
cs35l41_reset_and_boot()
<-- fails or is wiped by reset gpiod_set_value_cansleep(..., 1)
Moving the shared reset pulse and deassertion into a .resume_early callback paired with cs35l41_system_suspend_late() (as in cs35l56_hda_system_resume_early()) would ensure reset is released before any instance starts cs35l41_system_resume().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races
In reply to Stefan BindingThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] ALSA: hda/cs35l41: DSP left running on cs35l41_hda_bind() error and unbind - [Medium] ALSA…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] ALSA: hda/cs35l41: DSP left running on cs35l41_hda_bind() error and unbind
- [Medium] ALSA: hda/cs35l41: missing DSP shutdown in cs35l41_hda_bind() and unbind
--
commit 9729b30d404ee5bf8ccaef4a8856612e54fee87c
Author: Stefan Binding <[email protected]>
ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races
This commit queues CS35L41 firmware load work on system_freezable_wq, adds a PM notifier to cancel or re-queue firmware loads across system suspend, and moves ALSA control creation in cs35l41_hda_bind() outside fw_mutex.
Quoted 4 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +0
-0
[ ... ]
Quoted 42 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +11
-3@@ -1464,7 +1528,7 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masstruct cs35l41_hda *cs35l41 =dev_get_drvdata (dev);struct hda_component_parent *parent = master_data;struct hda_component *comp; -int ret =0 ; +int ret; comp =hda_component_from_index (parent, cs35l41->index);if (!comp)@@ -1494,8 +1558,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masdev_dbg (cs35l41->dev,"Firmware Autostart is disabled.\n" ); } - ret =cs35l41_create_controls (cs35l41); - comp->playback_hook = cs35l41_hda_playback_hook; comp->pre_playback_hook = cs35l41_hda_pre_playback_hook; comp->post_playback_hook = cs35l41_hda_post_playback_hook;@@ -1510,9 +1572,18 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *masmutex_unlock (&cs35l41->fw_mutex); +/* Create the necessary controls, do not create them under fw_mutex */ + ret =cs35l41_create_controls (cs35l41); +if (ret) { +dev_err (cs35l41->dev,"Failed to create controls: %d\n" , ret); + goto err; + } +if (!device_link_add (&cs35l41->codec->core.dev, cs35l41->dev,DL_FLAG_STATELESS ))dev_warn (dev,"Unable to create device link\n" ); +register_pm_notifier (&cs35l41->pm_notifier); + dev_info(cs35l41->dev, "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n", cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,@@ -1520,6 +1591,15 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas channel_name[cs35l41->hw_cfg.spk_pos], cs35l41->cs_dsp.running, cs35l41->speaker_id); +return 0 ;
sound/hda/codecs/side-codecs/cs35l41_hda.c static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas · line 1604
Quoted 10 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +8
-0+err: +memset (comp,0 ,sizeof (*comp)); + scoped_guard(mutex, &cs35l41->fw_mutex) { + cs35l41->request_fw_load = false; + } +cs35l41_fw_load_cancel_sync (cs35l41); + cs35l41->codec =NULL ; +return ret; }
[Severity: Medium]
Does the err path leave the DSP running when cs35l41_create_controls() fails?
Earlier in cs35l41_hda_bind(), cs35l41_smart_amp() loads and starts the DSP synchronously rather than via fw_load_work. Cancelling fw_load_work and clearing request_fw_load without calling cs35l41_shutdown_dsp() leaves cs_dsp.running and cs_dsp.booted set to true while unbound, so a later bind calls cs_dsp_power_up() on an already running DSP and leaks cs_dsp.alg_regions.
sound/hda/codecs/side-codecs/cs35l41_hda.c static void cs35l41_hda_unbind(struct device *dev, struct device *master, void * · line 1628
Quoted 13 lines
sound/hda/codecs/side-codecs/cs35l41_hda.c +5
-1@@ -1538,7 +1618,11 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *memset (comp,0 ,sizeof (*comp));cs35l41_remove_controls (cs35l41); -cancel_work_sync (&cs35l41->fw_load_work); + scoped_guard(mutex, &cs35l41->fw_mutex) { + cs35l41->request_fw_load = false; + } +unregister_pm_notifier (&cs35l41->pm_notifier); +cs35l41_fw_load_cancel_sync (cs35l41); cs35l41->codec =NULL ; } }
[Severity: Medium]
Should cs35l41_hda_unbind() also shut down the DSP if cs_dsp.running is true?
Resetting request_fw_load to false without calling cs35l41_shutdown_dsp() leaves cs_dsp.running true across an unbind/rebind cycle, either causing cs35l41_hda_bind() to run cs_dsp_power_up() on an active DSP when firmware_autostart is enabled, or leaving request_fw_load out of sync with cs_dsp.running so the next HDA_GEN_PCM_ACT_CLOSE unexpectedly unloads the firmware.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7