diff --git a/include/sound/sof/ipc4/header.h b/include/sound/sof/ipc4/header.h index 0f6f1de3eee90f..c571ef281d7446 100644 --- a/include/sound/sof/ipc4/header.h +++ b/include/sound/sof/ipc4/header.h @@ -681,6 +681,7 @@ struct sof_ipc4_notify_module_data { #define SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_MAGIC_VAL 0xA15A0000 #define SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_PARAMID_MASK GENMASK(15, 0) #define SOF_IPC4_NOTIFY_MODULE_EVENTID_COMPR_MAGIC_VAL 0xC0C00000 +#define SOF_IPC4_NOTIFY_MODULE_EVENTID_GESTURE_MAGIC_VAL 0x6E570000 /* * Macros for creating struct sof_ipc4_module_init_ext_init payload diff --git a/include/uapi/sound/sof/ultrasound.h b/include/uapi/sound/sof/ultrasound.h new file mode 100644 index 00000000000000..868a179074ceca --- /dev/null +++ b/include/uapi/sound/sof/ultrasound.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* + * Sound Open Firmware (SOF) Ultrasonic Gesture & Presence Detection UAPI + * + * Copyright(c) 2026 Intel Corporation. + */ + +#ifndef __INCLUDE_UAPI_SOUND_SOF_ULTRASOUND_H__ +#define __INCLUDE_UAPI_SOUND_SOF_ULTRASOUND_H__ + +#include +#include + +#define SOF_ULTRASOUND_UAPI_VERSION 1 + +/* Lower 16 bits of IPC4 module notification event_id */ +#define SOF_US_EVENT_SUBTYPE_GESTURE 1 +#define SOF_US_EVENT_SUBTYPE_PRESENCE 2 + +/** + * enum sof_ultrasound_event_type - Ultrasonic gesture / presence event codes + */ +enum sof_ultrasound_event_type { + SOF_US_EVT_NONE = 0, + SOF_US_EVT_PUSH = 1, + SOF_US_EVT_PULL = 2, + SOF_US_EVT_TAP = 3, + SOF_US_EVT_SWIPE_LEFT = 4, + SOF_US_EVT_SWIPE_RIGHT = 5, + SOF_US_EVT_PRESENCE_DETECTED = 6, + SOF_US_EVT_PRESENCE_LOST = 7, +}; + +/** + * struct sof_ultrasound_dsp_event - Wire format sent by SOF DSP inside + * struct sof_ipc4_notify_module_data.event_data[] + */ +struct sof_ultrasound_dsp_event { + __u32 version; + __u32 event_type; + __s32 doppler_mhz; + __s32 velocity_mm_s; + __u32 confidence; + __u32 snr_db_q8; + __u32 carrier_hz; + __u32 seq; + __u64 dsp_timestamp_us; +} __attribute__((packed, aligned(4))); + +/** + * struct sof_ultrasound_uapi_event - Event record returned by read() on + * /dev/sof-ultrasound + */ +struct sof_ultrasound_uapi_event { + __u32 abi_version; /* SOF_ULTRASOUND_UAPI_VERSION */ + __u32 event_type; /* enum sof_ultrasound_event_type */ + __u16 module_id; /* IPC4 originator module ID */ + __u16 instance_id; /* IPC4 originator instance ID */ + __u32 subtype; /* SOF_US_EVENT_SUBTYPE_* */ + __s32 doppler_mhz; /* signed peak Doppler shift in millihertz */ + __s32 velocity_mm_s; /* estimated radial velocity in mm/s */ + __u32 confidence; /* 0..1000 (0.0% .. 100.0%) */ + __u32 snr_db_q8; /* peak SNR above noise floor in Q24.8 dB */ + __u32 carrier_hz; /* ultrasonic carrier frequency in Hz */ + __u32 seq; /* DSP monotonic event sequence number */ + __u64 dsp_timestamp_us; /* DSP stream timestamp in microseconds */ + __u64 host_ktime_ns; /* Linux CLOCK_MONOTONIC timestamp in ns */ +}; + +/** + * struct sof_ultrasound_stats - Diagnostic counters queried via ioctl + */ +struct sof_ultrasound_stats { + __u64 total_events; + __u64 dropped_events; + __u32 active_readers; + __u32 last_event_type; +}; + +#define SOF_ULTRASOUND_IOC_MAGIC 'U' +#define SOF_ULTRASOUND_IOC_GET_STATS \ + _IOR(SOF_ULTRASOUND_IOC_MAGIC, 1, struct sof_ultrasound_stats) +#define SOF_ULTRASOUND_IOC_FLUSH _IO(SOF_ULTRASOUND_IOC_MAGIC, 2) + +#endif /* __INCLUDE_UAPI_SOUND_SOF_ULTRASOUND_H__ */ diff --git a/sound/soc/sof/Kconfig b/sound/soc/sof/Kconfig index f73a0e534851c8..0830820914aca1 100644 --- a/sound/soc/sof/Kconfig +++ b/sound/soc/sof/Kconfig @@ -69,6 +69,21 @@ config SND_SOC_SOF_CLIENT This option is not user-selectable but automagically handled by 'select' statements at a higher level. +config SND_SOC_SOF_ULTRASOUND + tristate "SOF ultrasonic gesture and presence sensing client" + depends on SND_SOC_SOF && SND_SOC_SOF_IPC4 + depends on INPUT + select SND_SOC_SOF_CLIENT + help + This option enables the SOF IPC4 ultrasonic gesture and presence + sensing client driver (snd-sof-ultrasound). It manages hostless + DSP-autonomous ultrasonic TX/RX streams with zero host audio DMA, + supports D0i3/S0ix low-power sensing, and delivers gesture and + presence events to userspace via /dev/sof-ultrasound, the Linux + input subsystem, and sysfs. + Say Y or M if you want to enable SOF ultrasonic gesture sensing. + If unsure select "N". + config SND_SOC_SOF_DEVELOPER_SUPPORT bool "SOF developer options support" depends on EXPERT && SND_SOC_SOF diff --git a/sound/soc/sof/Makefile b/sound/soc/sof/Makefile index 887abf70da3ba1..bdba5a0a11228a 100644 --- a/sound/soc/sof/Makefile +++ b/sound/soc/sof/Makefile @@ -36,6 +36,7 @@ ifneq ($(CONFIG_SND_SOC_SOF_IPC4),) snd-sof-probes-y += sof-client-probes-ipc4.o endif snd-sof-fw-gdb-objs := sof-client-fw-gdb.o +snd-sof-ultrasound-objs := sof-client-ultrasound.o snd-sof-nocodec-y := nocodec.o @@ -55,6 +56,7 @@ obj-$(CONFIG_SND_SOC_SOF_DEBUG_IPC_MSG_INJECTOR) += snd-sof-ipc-msg-injector.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_IPC_KERNEL_INJECTOR) += snd-sof-ipc-kernel-injector.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_PROBES) += snd-sof-probes.o obj-$(CONFIG_SND_SOC_SOF_DEBUG_FW_GDB) += snd-sof-fw-gdb.o +obj-$(CONFIG_SND_SOC_SOF_ULTRASOUND) += snd-sof-ultrasound.o obj-$(CONFIG_SND_SOC_SOF_INTEL_TOPLEVEL) += intel/ obj-$(CONFIG_SND_SOC_SOF_IMX_TOPLEVEL) += imx/ diff --git a/sound/soc/sof/core.c b/sound/soc/sof/core.c index 1f38fe03e49395..0543c4fa95baf9 100644 --- a/sound/soc/sof/core.c +++ b/sound/soc/sof/core.c @@ -693,11 +693,13 @@ int snd_sof_device_probe(struct device *dev, struct snd_sof_pdata *plat_data) INIT_LIST_HEAD(&sdev->ipc_client_list); INIT_LIST_HEAD(&sdev->ipc_rx_handler_list); INIT_LIST_HEAD(&sdev->fw_state_handler_list); + INIT_LIST_HEAD(&sdev->card_disconnect_handler_list); spin_lock_init(&sdev->ipc_lock); spin_lock_init(&sdev->hw_lock); mutex_init(&sdev->power_state_access); mutex_init(&sdev->ipc_client_mutex); mutex_init(&sdev->client_event_handler_mutex); + mutex_init(&sdev->card_disconnect_mutex); mutex_init(&sdev->dsp_fw_boot_mutex); /* set default timeouts if none provided */ diff --git a/sound/soc/sof/intel/hda-dai.c b/sound/soc/sof/intel/hda-dai.c index 7a8875a885ab34..36a3b4a1d36fc5 100644 --- a/sound/soc/sof/intel/hda-dai.c +++ b/sound/soc/sof/intel/hda-dai.c @@ -287,21 +287,21 @@ static bool hda_dai_has_wov_fe(struct snd_sof_dev *sdev, struct snd_pcm_substrea struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct snd_soc_dpcm *dpcm; struct snd_sof_pcm *spcm; - - if (substream->stream != SNDRV_PCM_STREAM_CAPTURE) - return false; + int dir = substream->stream; /* - * Only retain the BE for a connected WoV capture FE that is running - * or has already ignored its suspend trigger. An idle WoV FE may - * still be connected to the BE. + * Retain the BE for a connected WoV capture FE or hostless D0i3-compatible + * FE (e.g., ultrasonic sensing TX/RX) that is running or has already + * ignored its suspend trigger. */ - for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_CAPTURE, dpcm) { + for_each_dpcm_fe(rtd, dir, dpcm) { spcm = snd_sof_find_spcm_dai(sdev->component, dpcm->fe); - if (spcm && spcm->stream[SNDRV_PCM_STREAM_CAPTURE].d0i3_compatible && - spcm->stream[SNDRV_PCM_STREAM_CAPTURE].dsp_max_burst_size_in_ms <= 1 && - (dpcm->fe->dpcm[SNDRV_PCM_STREAM_CAPTURE].state == SND_SOC_DPCM_STATE_START || - spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored)) + if (spcm && + (dir == SNDRV_PCM_STREAM_CAPTURE || spcm->stream[dir].hostless) && + spcm->stream[dir].d0i3_compatible && + spcm->stream[dir].dsp_max_burst_size_in_ms <= 1 && + (dpcm->fe->dpcm[dir].state == SND_SOC_DPCM_STATE_START || + spcm->stream[dir].suspend_ignored)) return true; } diff --git a/sound/soc/sof/ipc4-pcm.c b/sound/soc/sof/ipc4-pcm.c index cbf948d8479148..b909d6bfabe153 100644 --- a/sound/soc/sof/ipc4-pcm.c +++ b/sound/soc/sof/ipc4-pcm.c @@ -992,6 +992,10 @@ void sof_ipc4_build_time_info(struct snd_sof_dev *sdev, struct snd_sof_pcm_strea } } + /* hostless streams have no host copier or host DMA timestamp */ + if (sps->hostless) + return; + /* both host and dai copier must be valid for time_info */ if (!host_copier || !dai_copier) { dev_err(sdev->dev, "host or dai copier are not found\n"); diff --git a/sound/soc/sof/ipc4.c b/sound/soc/sof/ipc4.c index bf3a122ee651e8..4a1dc937a49897 100644 --- a/sound/soc/sof/ipc4.c +++ b/sound/soc/sof/ipc4.c @@ -870,6 +870,8 @@ static void sof_ipc4_rx_msg(struct snd_sof_dev *sdev) if (handler_func) handler_func(sdev, ipc4_msg); + sof_client_ipc_rx_dispatcher(sdev, ipc4_msg); + sof_ipc4_log_header(sdev->dev, "ipc rx done ", ipc4_msg, true); if (data_size) { diff --git a/sound/soc/sof/pcm.c b/sound/soc/sof/pcm.c index 0161c0ae8bbdb4..1f6c7fd2ae19c6 100644 --- a/sound/soc/sof/pcm.c +++ b/sound/soc/sof/pcm.c @@ -160,10 +160,12 @@ static int sof_pcm_hw_params(struct snd_soc_component *component, } platform_params = &spcm->platform_params[substream->stream]; - ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, platform_params); - if (ret < 0) { - spcm_err(spcm, substream->stream, "platform hw params failed\n"); - return ret; + if (!spcm->stream[substream->stream].hostless) { + ret = snd_sof_pcm_platform_hw_params(sdev, substream, params, platform_params); + if (ret < 0) { + spcm_err(spcm, substream->stream, "platform hw params failed\n"); + return ret; + } } /* if this is a repeated hw_params without hw_free, skip setting up widgets */ @@ -174,7 +176,7 @@ static int sof_pcm_hw_params(struct snd_soc_component *component, return ret; } - if (!sdev->dspless_mode_selected) { + if (!sdev->dspless_mode_selected && !spcm->stream[substream->stream].hostless) { int host_comp_id = spcm->stream[substream->stream].comp_id; host_widget = snd_sof_find_swidget_by_comp_id(sdev, host_comp_id); @@ -190,7 +192,7 @@ static int sof_pcm_hw_params(struct snd_soc_component *component, } /* create compressed page table for audio firmware */ - if (runtime->buffer_changed) { + if (!spcm->stream[substream->stream].hostless && runtime->buffer_changed) { struct snd_dma_buffer *dmab = snd_pcm_get_dma_buf(substream); ret = snd_sof_create_page_table(component->dev, dmab, @@ -217,7 +219,8 @@ static int sof_pcm_stream_free(struct snd_sof_dev *sdev, if (spcm->prepared[substream->stream]) { /* stop DMA first if needed */ - if (pcm_ops && pcm_ops->platform_stop_during_hw_free) + if (!spcm->stream[substream->stream].hostless && + pcm_ops && pcm_ops->platform_stop_during_hw_free) snd_sof_pcm_platform_trigger(sdev, substream, SNDRV_PCM_TRIGGER_STOP); @@ -237,12 +240,14 @@ static int sof_pcm_stream_free(struct snd_sof_dev *sdev, } /* reset the DMA */ - ret = snd_sof_pcm_platform_hw_free(sdev, substream); - if (ret < 0) { - spcm_err(spcm, substream->stream, - "platform hw free failed %d\n", ret); - if (!err) - err = ret; + if (!spcm->stream[substream->stream].hostless) { + ret = snd_sof_pcm_platform_hw_free(sdev, substream); + if (ret < 0) { + spcm_err(spcm, substream->stream, + "platform hw free failed %d\n", ret); + if (!err) + err = ret; + } } /* free widget list */ @@ -445,7 +450,8 @@ static int sof_pcm_trigger(struct snd_soc_component *component, * They don't use Deep Buffer */ if (sdev->system_suspend_target == SOF_SUSPEND_S0IX && - substream->stream == SNDRV_PCM_STREAM_CAPTURE && + (substream->stream == SNDRV_PCM_STREAM_CAPTURE || + spcm->stream[substream->stream].hostless) && spcm->stream[substream->stream].d0i3_compatible && spcm->stream[substream->stream].dsp_max_burst_size_in_ms <= 1) { spcm->stream[substream->stream].suspend_ignored = true; @@ -466,7 +472,7 @@ static int sof_pcm_trigger(struct snd_soc_component *component, return -EINVAL; } - if (!ipc_first) + if (!ipc_first && !spcm->stream[substream->stream].hostless) snd_sof_pcm_platform_trigger(sdev, substream, cmd); if (pcm_ops && pcm_ops->trigger) @@ -476,14 +482,15 @@ static int sof_pcm_trigger(struct snd_soc_component *component, case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: case SNDRV_PCM_TRIGGER_START: /* invoke platform trigger to start DMA only if pcm_ops is successful */ - if (ipc_first && !ret) + if (ipc_first && !ret && !spcm->stream[substream->stream].hostless) snd_sof_pcm_platform_trigger(sdev, substream, cmd); break; case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_STOP: /* invoke platform trigger to stop DMA even if pcm_ops isn't set or if it failed */ - if (!pcm_ops || !pcm_ops->platform_stop_during_hw_free) + if (!spcm->stream[substream->stream].hostless && + (!pcm_ops || !pcm_ops->platform_stop_during_hw_free)) snd_sof_pcm_platform_trigger(sdev, substream, cmd); /* @@ -520,6 +527,10 @@ static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component, if (rtd->dai_link->no_pcm) return 0; + spcm = snd_sof_find_spcm_dai(component, rtd); + if (spcm && spcm->stream[substream->stream].hostless) + return 0; + if (pcm_ops && pcm_ops->pointer) ret = pcm_ops->pointer(component, substream, &host); @@ -530,7 +541,6 @@ static snd_pcm_uframes_t sof_pcm_pointer(struct snd_soc_component *component, if (sof_ops(sdev)->pcm_pointer) return sof_ops(sdev)->pcm_pointer(sdev, substream); - spcm = snd_sof_find_spcm_dai(component, rtd); if (!spcm) return -EINVAL; @@ -592,6 +602,12 @@ static int sof_pcm_open(struct snd_soc_component *component, spcm->stream[substream->stream].substream = substream; spcm->prepared[substream->stream] = false; + if (spcm->stream[substream->stream].hostless) { + if (spcm->stream[substream->stream].d0i3_compatible) + runtime->hw.info |= SNDRV_PCM_INFO_RESUME; + return 0; + } + ret = snd_sof_pcm_platform_open(sdev, substream); if (ret < 0) { spcm_err(spcm, substream->stream, @@ -626,14 +642,16 @@ static int sof_pcm_close(struct snd_soc_component *component, spcm_dbg(spcm, substream->stream, "Entry: close\n"); - err = snd_sof_pcm_platform_close(sdev, substream); - if (err < 0) { - spcm_err(spcm, substream->stream, - "platform pcm close failed %d\n", err); - /* - * keep going, no point in preventing the close - * from happening - */ + if (!spcm->stream[substream->stream].hostless) { + err = snd_sof_pcm_platform_close(sdev, substream); + if (err < 0) { + spcm_err(spcm, substream->stream, + "platform pcm close failed %d\n", err); + /* + * keep going, no point in preventing the close + * from happening + */ + } } spcm->stream[substream->stream].substream = NULL; @@ -768,6 +786,18 @@ int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_pa } EXPORT_SYMBOL(sof_pcm_dai_link_fixup); +static int sof_pcm_card_dev_disconnect(struct snd_device *device) +{ + struct snd_sof_dev *sdev = device->device_data; + + sof_client_card_disconnect_dispatcher(sdev); + return 0; +} + +static const struct snd_device_ops sof_pcm_card_dev_ops = { + .dev_disconnect = sof_pcm_card_dev_disconnect, +}; + static int sof_pcm_probe(struct snd_soc_component *component) { struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); @@ -796,9 +826,20 @@ static int sof_pcm_probe(struct snd_soc_component *component) } ret = snd_sof_load_topology(component, tplg_filename); - if (ret < 0) + if (ret < 0) { dev_err(component->dev, "error: failed to load DSP topology %d\n", ret); + goto pm_error; + } + + if (component->card && component->card->snd_card) { + ret = snd_device_new(component->card->snd_card, SNDRV_DEV_LOWLEVEL, + sdev, &sof_pcm_card_dev_ops); + if (ret < 0) + dev_warn(component->dev, + "failed to register card disconnect hook: %d\n", ret); + ret = 0; + } pm_error: pm_runtime_put_autosuspend(component->dev); @@ -808,14 +849,25 @@ static int sof_pcm_probe(struct snd_soc_component *component) static void sof_pcm_remove(struct snd_soc_component *component) { + struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); + /* remove topology */ snd_soc_tplg_component_remove(component); + sdev->component = NULL; } static int sof_pcm_ack(struct snd_soc_component *component, struct snd_pcm_substream *substream) { + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); + struct snd_sof_pcm *spcm; + + if (!rtd->dai_link->no_pcm) { + spcm = snd_sof_find_spcm_dai(component, rtd); + if (spcm && spcm->stream[substream->stream].hostless) + return 0; + } return snd_sof_pcm_platform_ack(sdev, substream); } diff --git a/sound/soc/sof/sof-audio.c b/sound/soc/sof/sof-audio.c index 8cbeca471542a6..05b76b254b2c29 100644 --- a/sound/soc/sof/sof-audio.c +++ b/sound/soc/sof/sof-audio.c @@ -264,9 +264,10 @@ int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsourc struct snd_sof_route *sroute; bool route_found = false; - /* ignore routes involving virtual widgets in topology */ + /* ignore routes involving virtual or hostless widgets in topology */ if (is_virtual_widget(sdev, src_widget->widget, __func__) || - is_virtual_widget(sdev, sink_widget->widget, __func__)) + is_virtual_widget(sdev, sink_widget->widget, __func__) || + src_widget->hostless || sink_widget->hostless) return 0; /* skip route if source/sink widget is not set up */ @@ -443,7 +444,7 @@ sof_unprepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widg if (is_virtual_widget(sdev, widget, __func__)) return; - if (!swidget) + if (!swidget || swidget->hostless) goto sink_unprepare; if (swidget->spipe && swidget->spipe->direction_valid && @@ -491,13 +492,13 @@ sof_prepare_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget if (is_virtual_widget(sdev, widget, __func__)) return 0; - if (!swidget) - goto sink_prepare; - widget_ops = tplg_ops ? tplg_ops->widget : NULL; if (!widget_ops) return 0; + if (!swidget || swidget->hostless) + goto sink_prepare; + if (swidget->spipe && swidget->spipe->direction_valid && !sof_widget_in_same_direction(swidget, dir)) return 0; @@ -559,7 +560,7 @@ static int sof_free_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_dap if (is_virtual_widget(sdev, widget, __func__)) return 0; - if (!swidget) + if (!swidget || swidget->hostless) goto sink_free; if (swidget->spipe && swidget->spipe->direction_valid && @@ -610,7 +611,7 @@ static int sof_set_up_widgets_in_path(struct snd_sof_dev *sdev, struct snd_soc_d if (is_virtual_widget(sdev, widget, __func__)) return 0; - if (swidget) { + if (swidget && !swidget->hostless) { int i; if (swidget->spipe && swidget->spipe->direction_valid && @@ -799,7 +800,7 @@ int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, struct snd_sof_widget *pipe_widget; struct snd_sof_pipeline *spipe; - if (!swidget || sdev->dspless_mode_selected) + if (!swidget || swidget->hostless || sdev->dspless_mode_selected) continue; spipe = swidget->spipe; @@ -894,13 +895,36 @@ bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev) } EXPORT_SYMBOL(snd_sof_dsp_only_d0i3_compatible_stream_active); +static bool sof_pcm_stream_is_suspend_ignored(struct snd_sof_pcm *spcm, int dir) +{ + struct snd_pcm_substream *substream = spcm->stream[dir].substream; + struct snd_soc_pcm_runtime *rtd; + + if (spcm->stream[dir].suspend_ignored) + return true; + + /* + * Hostless D0i3-compatible streams with ignore_suspend=1 on their FE + * DAI link bypass snd_pcm_suspend_all() during ASoC suspend; treat + * them as suspend-ignored when selecting the S0ix DSP power target. + */ + if (substream && substream->runtime && + spcm->stream[dir].hostless && spcm->stream[dir].d0i3_compatible) { + rtd = snd_soc_substream_to_rtd(substream); + if (rtd && rtd->dai_link && rtd->dai_link->ignore_suspend) + return true; + } + + return false; +} + bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev) { struct snd_sof_pcm *spcm; list_for_each_entry(spcm, &sdev->pcm_list, list) { - if (spcm->stream[SNDRV_PCM_STREAM_PLAYBACK].suspend_ignored || - spcm->stream[SNDRV_PCM_STREAM_CAPTURE].suspend_ignored) + if (sof_pcm_stream_is_suspend_ignored(spcm, SNDRV_PCM_STREAM_PLAYBACK) || + sof_pcm_stream_is_suspend_ignored(spcm, SNDRV_PCM_STREAM_CAPTURE)) return true; } diff --git a/sound/soc/sof/sof-audio.h b/sound/soc/sof/sof-audio.h index 10b41ab18b7990..8b8e6d3ebf2632 100644 --- a/sound/soc/sof/sof-audio.h +++ b/sound/soc/sof/sof-audio.h @@ -340,6 +340,7 @@ struct snd_sof_pcm_stream { struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */ bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */ bool pause_supported; /* PCM device supports PAUSE operation */ + bool hostless; /* DSP-autonomous FE stream with no host copier or host DMA */ unsigned int dsp_max_burst_size_in_ms; /* The maximum size of the host DMA burst in ms */ /* * flag to indicate that the DSP pipelines should be kept @@ -507,6 +508,8 @@ struct snd_sof_widget { struct ida output_queue_ida; struct ida input_queue_ida; + bool hostless; /* Virtual AIF anchor without DSP copier */ + void *private; /* core does not touch this */ }; diff --git a/sound/soc/sof/sof-client-ultrasound.c b/sound/soc/sof/sof-client-ultrasound.c new file mode 100644 index 00000000000000..a9a9b78e916aad --- /dev/null +++ b/sound/soc/sof/sof-client-ultrasound.c @@ -0,0 +1,1062 @@ +// SPDX-License-Identifier: GPL-2.0-only +// +// Copyright(c) 2026 Intel Corporation +// +// SOF IPC4 Ultrasonic Gesture & Presence Detection Client Driver +// +// Features: +// - Approach A (Hostless DSP-Autonomous Sensing): +// Directly opens, configures, and starts the hostless "Ultrasound TX" +// (playback -> RT722 Speaker BE) and "Ultrasound RX" (Intel DMIC BE -> sink) +// DPCM streams from kernel space with zero host DMA transfers/IRQs when +// enabled via /sys/class/misc/sof-ultrasound/enable or automatically on +// /dev/sof-ultrasound open (auto_enable=1). +// - Approach B (S0ix Low-Power Support): +// Sets ignore_suspend on the hostless FE and sensing BE links while active +// so D0i3 / S0ix suspend preserves the DSP sensing path and IPC4 wake IRQ. +// - Userspace Event APIs: +// 1. /dev/sof-ultrasound (multi-reader kfifo, read/poll/ioctl) +// 2. Linux input device ("SOF Ultrasound Gesture": EV_MSC/MSC_GESTURE and +// EV_SW/SW_FRONT_PROXIMITY) +// 3. Sysfs under /sys/class/misc/sof-ultrasound/ (enable, active, +// auto_enable, stats, last_event) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "sof-client.h" + +#define SOF_ULTRASOUND_FIFO_DEPTH 64 +#define SOF_ULTRASOUND_TX_PCM_NAME "Ultrasound TX" +#define SOF_ULTRASOUND_RX_PCM_NAME "Ultrasound RX" +#define SOF_ULTRASOUND_RATE 48000 +#define SOF_ULTRASOUND_TX_CHANNELS 2 +#define SOF_ULTRASOUND_RX_CHANNELS 4 +#define SOF_ULTRASOUND_PERIOD_FRAMES 960 +#define SOF_ULTRASOUND_PERIODS 4 +#define SOF_ULTRASOUND_MAX_SAVED_LINKS 4 + +static const char *sof_ultrasound_evt_name(u32 event_type) +{ + switch (event_type) { + case SOF_US_EVT_PUSH: + return "PUSH"; + case SOF_US_EVT_PULL: + return "PULL"; + case SOF_US_EVT_TAP: + return "TAP"; + case SOF_US_EVT_SWIPE_LEFT: + return "SWIPE_LEFT"; + case SOF_US_EVT_SWIPE_RIGHT: + return "SWIPE_RIGHT"; + case SOF_US_EVT_PRESENCE_DETECTED: + return "PRESENCE_DETECTED"; + case SOF_US_EVT_PRESENCE_LOST: + return "PRESENCE_LOST"; + default: + return "NONE"; + } +} + +struct sof_ultrasound_priv; + +struct sof_ultrasound_reader { + struct sof_ultrasound_priv *priv; + struct list_head list; + DECLARE_KFIFO(fifo, struct sof_ultrasound_uapi_event, SOF_ULTRASOUND_FIFO_DEPTH); +}; + +struct sof_ultrasound_saved_link { + struct snd_soc_dai_link *dai_link; + unsigned int orig_ignore_suspend; +}; + +struct sof_ultrasound_priv { + struct kref refcount; + struct sof_client_dev *cdev; + struct device *dev; + struct miscdevice miscdev; + struct input_dev *input; + char dev_name[32]; + + /* Event delivery state */ + spinlock_t lock; + wait_queue_head_t waitq; + struct list_head readers; + bool closing; + + u64 total_events; + u64 dropped_events; + u32 active_readers; + struct sof_ultrasound_uapi_event last_event; + + /* Approach A & B hostless stream lifecycle state */ + struct mutex ctrl_mutex; + struct file dummy_file; + struct snd_card *snd_card; + struct snd_pcm *tx_pcm; + struct snd_pcm *rx_pcm; + struct snd_pcm_substream *tx_substream; + struct snd_pcm_substream *rx_substream; + struct sof_ultrasound_saved_link saved_links[SOF_ULTRASOUND_MAX_SAVED_LINKS]; + int num_saved_links; + bool sensing_active; + bool resume_sensing; + bool sysfs_enabled; + bool auto_enable; +}; + +static const struct file_operations sof_ultrasound_dummy_pcm_fops = {}; + +static void sof_ultrasound_priv_release(struct kref *kref) +{ + struct sof_ultrasound_priv *priv = + container_of(kref, struct sof_ultrasound_priv, refcount); + + kfree(priv); +} + +static bool sof_ultrasound_pcm_matches(struct snd_pcm *pcm, const char *target) +{ + if (!pcm) + return false; + + return (strstr(pcm->id, target) || strstr(pcm->name, target)); +} + +static void sof_ultrasound_save_ignore_suspend(struct sof_ultrasound_priv *priv, + struct snd_soc_dai_link *dai_link) +{ + int i; + + if (!dai_link) + return; + + for (i = 0; i < priv->num_saved_links; i++) { + if (priv->saved_links[i].dai_link == dai_link) + return; + } + + if (priv->num_saved_links >= SOF_ULTRASOUND_MAX_SAVED_LINKS) + return; + + priv->saved_links[priv->num_saved_links].dai_link = dai_link; + priv->saved_links[priv->num_saved_links].orig_ignore_suspend = dai_link->ignore_suspend; + priv->num_saved_links++; + dai_link->ignore_suspend = 1; +} + +static void sof_ultrasound_restore_ignore_suspend(struct sof_ultrasound_priv *priv) +{ + int i; + + for (i = 0; i < priv->num_saved_links; i++) { + if (priv->saved_links[i].dai_link) + priv->saved_links[i].dai_link->ignore_suspend = + priv->saved_links[i].orig_ignore_suspend; + } + priv->num_saved_links = 0; +} + +static int sof_ultrasound_open_and_start_pcm(struct sof_ultrasound_priv *priv, + struct snd_pcm *pcm, int dir, + unsigned int channels, + struct snd_pcm_substream **out_substream) +{ + struct snd_pcm_substream *substream = NULL; + struct snd_pcm_hw_params *params; + struct snd_pcm_sw_params *sw_params; + struct snd_interval *it; + int err; + + mutex_lock(&pcm->open_mutex); + err = snd_pcm_open_substream(pcm, dir, &priv->dummy_file, &substream); + mutex_unlock(&pcm->open_mutex); + if (err < 0) { + dev_err(priv->dev, "failed to open hostless PCM %s (dir=%d): %d\n", + pcm->id, dir, err); + return err; + } + + params = kzalloc(sizeof(*params), GFP_KERNEL); + if (!params) { + err = -ENOMEM; + goto err_release; + } + + _snd_pcm_hw_params_any(params); + params->flags = SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP; + + snd_mask_none(hw_param_mask(params, SNDRV_PCM_HW_PARAM_ACCESS)); + snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_ACCESS), + SNDRV_PCM_ACCESS_RW_INTERLEAVED); + + snd_mask_none(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT)); + snd_mask_set_format(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), + SNDRV_PCM_FORMAT_S32_LE); + + it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); + it->min = channels; + it->max = channels; + it->integer = 1; + + it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); + it->min = SOF_ULTRASOUND_RATE; + it->max = SOF_ULTRASOUND_RATE; + it->integer = 1; + + it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE); + it->min = SOF_ULTRASOUND_PERIOD_FRAMES; + it->max = SOF_ULTRASOUND_PERIOD_FRAMES; + it->integer = 1; + + it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIODS); + it->min = SOF_ULTRASOUND_PERIODS; + it->max = SOF_ULTRASOUND_PERIODS; + it->integer = 1; + + err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, params); + kfree(params); + if (err < 0) { + dev_err(priv->dev, "hostless PCM %s HW_PARAMS failed: %d\n", pcm->id, err); + goto err_release; + } + + sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL); + if (!sw_params) { + err = -ENOMEM; + goto err_release; + } + sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE; + sw_params->period_step = 1; + sw_params->avail_min = substream->runtime->period_size; + sw_params->start_threshold = 1; + sw_params->stop_threshold = substream->runtime->boundary; + sw_params->boundary = substream->runtime->boundary; + + err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params); + kfree(sw_params); + if (err < 0) { + dev_err(priv->dev, "hostless PCM %s SW_PARAMS failed: %d\n", pcm->id, err); + goto err_release; + } + + err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL); + if (err < 0) { + dev_err(priv->dev, "hostless PCM %s PREPARE failed: %d\n", pcm->id, err); + goto err_release; + } + + /* + * ALSA core requires playback appl_ptr > hw_ptr on SNDRV_PCM_IOCTL_START. + * No host DMA reads this buffer because the stream is DSP-autonomous hostless. + */ + if (dir == SNDRV_PCM_STREAM_PLAYBACK) + substream->runtime->control->appl_ptr = substream->runtime->buffer_size; + + err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL); + if (err < 0) { + dev_err(priv->dev, "hostless PCM %s START failed: %d\n", pcm->id, err); + goto err_release; + } + + *out_substream = substream; + return 0; + +err_release: + mutex_lock(&pcm->open_mutex); + snd_pcm_release_substream(substream); + mutex_unlock(&pcm->open_mutex); + wake_up(&pcm->open_wait); + return err; +} + +static void sof_ultrasound_stop_and_release_pcm(struct snd_pcm *pcm, + struct snd_pcm_substream **substream_p) +{ + struct snd_pcm_substream *substream = *substream_p; + + if (!substream || !pcm) + return; + + snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL); + + mutex_lock(&pcm->open_mutex); + snd_pcm_release_substream(substream); + mutex_unlock(&pcm->open_mutex); + wake_up(&pcm->open_wait); + + *substream_p = NULL; +} + +static void sof_ultrasound_release_card_file_locked(struct sof_ultrasound_priv *priv) +{ + struct snd_card *snd_card = priv->snd_card; + + if (!snd_card) + return; + + snd_card_file_remove(snd_card, &priv->dummy_file); + if (priv->dummy_file.f_op && + priv->dummy_file.f_op != &sof_ultrasound_dummy_pcm_fops) + fops_put(priv->dummy_file.f_op); + priv->dummy_file.f_op = &sof_ultrasound_dummy_pcm_fops; + module_put(snd_card->module); + priv->snd_card = NULL; +} + +static int sof_ultrasound_start_sensing_locked(struct sof_ultrasound_priv *priv, + bool quiet_notfound) +{ + struct snd_soc_component *scomp; + struct snd_soc_card *card; + struct snd_card *snd_card; + struct snd_soc_pcm_runtime *rtd; + struct snd_soc_pcm_runtime *tx_rtd = NULL; + struct snd_soc_pcm_runtime *rx_rtd = NULL; + struct snd_soc_pcm_runtime *spk_rtd = NULL; + struct snd_soc_pcm_runtime *dmic_rtd = NULL; + int err; + + if (priv->sensing_active) + return 0; + + if (priv->closing) + return -ENODEV; + + scomp = sof_client_get_component(priv->cdev); + card = scomp ? scomp->card : NULL; + snd_card = card ? card->snd_card : NULL; + if (!snd_card) { + if (!quiet_notfound) + dev_warn(priv->dev, "sound card not ready for hostless ultrasound\n"); + return -EAGAIN; + } + + if (!try_module_get(snd_card->module)) + return -ENODEV; + + priv->dummy_file.f_op = &sof_ultrasound_dummy_pcm_fops; + err = snd_card_file_add(snd_card, &priv->dummy_file); + if (err < 0) { + module_put(snd_card->module); + return err; + } + priv->snd_card = snd_card; + + for_each_card_rtds(card, rtd) { + if (rtd->pcm && !rtd->dai_link->no_pcm) { + if (sof_ultrasound_pcm_matches(rtd->pcm, SOF_ULTRASOUND_TX_PCM_NAME)) + tx_rtd = rtd; + else if (sof_ultrasound_pcm_matches(rtd->pcm, SOF_ULTRASOUND_RX_PCM_NAME)) + rx_rtd = rtd; + } else if (rtd->dai_link && rtd->dai_link->no_pcm && rtd->dai_link->name) { + if (!strcmp(rtd->dai_link->name, "dmic01")) + dmic_rtd = rtd; + else if (strstr(rtd->dai_link->name, "SmartAmp") || + strstr(rtd->dai_link->name, "SDW1-Playback")) + spk_rtd = rtd; + } + } + + if (!tx_rtd || !rx_rtd) { + if (!quiet_notfound) + dev_warn(priv->dev, + "hostless ultrasound PCMs not found in active topology (tx=%d rx=%d)\n", + !!tx_rtd, !!rx_rtd); + err = -ENOENT; + goto err_release_card; + } + + /* Approach B: mark FE and BE links ignore_suspend while hostless sensing is active */ + sof_ultrasound_save_ignore_suspend(priv, tx_rtd->dai_link); + sof_ultrasound_save_ignore_suspend(priv, rx_rtd->dai_link); + if (dmic_rtd) + sof_ultrasound_save_ignore_suspend(priv, dmic_rtd->dai_link); + if (spk_rtd) + sof_ultrasound_save_ignore_suspend(priv, spk_rtd->dai_link); + + /* Start TX first so RT722 speaker + 20 kHz carrier are active before RX warmup finishes */ + err = sof_ultrasound_open_and_start_pcm(priv, tx_rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK, + SOF_ULTRASOUND_TX_CHANNELS, &priv->tx_substream); + if (err < 0) + goto err_restore_links; + priv->tx_pcm = tx_rtd->pcm; + + err = sof_ultrasound_open_and_start_pcm(priv, rx_rtd->pcm, SNDRV_PCM_STREAM_CAPTURE, + SOF_ULTRASOUND_RX_CHANNELS, &priv->rx_substream); + if (err < 0) + goto err_stop_tx; + priv->rx_pcm = rx_rtd->pcm; + + priv->sensing_active = true; + dev_info(priv->dev, + "hostless ultrasound sensing started (TX=%s 2ch, RX=%s 4ch, 48kHz S32_LE, zero host DMA)\n", + priv->tx_pcm->id, priv->rx_pcm->id); + return 0; + +err_stop_tx: + sof_ultrasound_stop_and_release_pcm(priv->tx_pcm, &priv->tx_substream); + priv->tx_pcm = NULL; +err_restore_links: + sof_ultrasound_restore_ignore_suspend(priv); +err_release_card: + sof_ultrasound_release_card_file_locked(priv); + return err; +} + +static void sof_ultrasound_stop_sensing_locked(struct sof_ultrasound_priv *priv) +{ + if (!priv->sensing_active) + return; + + sof_ultrasound_stop_and_release_pcm(priv->rx_pcm, &priv->rx_substream); + sof_ultrasound_stop_and_release_pcm(priv->tx_pcm, &priv->tx_substream); + priv->rx_pcm = NULL; + priv->tx_pcm = NULL; + sof_ultrasound_restore_ignore_suspend(priv); + sof_ultrasound_release_card_file_locked(priv); + priv->sensing_active = false; + + dev_info(priv->dev, "hostless ultrasound sensing stopped\n"); +} + +static void sof_ultrasound_card_disconnect(struct sof_client_dev *cdev) +{ + struct sof_ultrasound_priv *priv = cdev->data; + + if (!priv) + return; + + mutex_lock(&priv->ctrl_mutex); + priv->resume_sensing = false; + sof_ultrasound_stop_sensing_locked(priv); + mutex_unlock(&priv->ctrl_mutex); +} + +static ssize_t enable_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + bool val; + + mutex_lock(&priv->ctrl_mutex); + val = priv->sysfs_enabled; + mutex_unlock(&priv->ctrl_mutex); + + return sysfs_emit(buf, "%d\n", val ? 1 : 0); +} + +static ssize_t enable_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + bool enable; + bool has_readers; + unsigned long flags; + int ret; + + ret = kstrtobool(buf, &enable); + if (ret) + return ret; + + mutex_lock(&priv->ctrl_mutex); + if (enable) { + ret = sof_ultrasound_start_sensing_locked(priv, false); + if (ret == 0) + priv->sysfs_enabled = true; + } else { + priv->sysfs_enabled = false; + spin_lock_irqsave(&priv->lock, flags); + has_readers = priv->active_readers > 0; + spin_unlock_irqrestore(&priv->lock, flags); + + if (!has_readers || !priv->auto_enable) + sof_ultrasound_stop_sensing_locked(priv); + ret = 0; + } + mutex_unlock(&priv->ctrl_mutex); + + return ret ? ret : count; +} +static DEVICE_ATTR_RW(enable); + +static ssize_t active_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + bool val; + + mutex_lock(&priv->ctrl_mutex); + val = priv->sensing_active; + mutex_unlock(&priv->ctrl_mutex); + + return sysfs_emit(buf, "%d\n", val ? 1 : 0); +} +static DEVICE_ATTR_RO(active); + +static ssize_t auto_enable_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + bool val; + + mutex_lock(&priv->ctrl_mutex); + val = priv->auto_enable; + mutex_unlock(&priv->ctrl_mutex); + + return sysfs_emit(buf, "%d\n", val ? 1 : 0); +} + +static ssize_t auto_enable_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + bool val; + int ret; + + ret = kstrtobool(buf, &val); + if (ret) + return ret; + + mutex_lock(&priv->ctrl_mutex); + priv->auto_enable = val; + mutex_unlock(&priv->ctrl_mutex); + + return count; +} +static DEVICE_ATTR_RW(auto_enable); + +static ssize_t stats_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + u64 total, dropped; + u32 readers; + unsigned long flags; + + spin_lock_irqsave(&priv->lock, flags); + total = priv->total_events; + dropped = priv->dropped_events; + readers = priv->active_readers; + spin_unlock_irqrestore(&priv->lock, flags); + + return sysfs_emit(buf, "total_events=%llu dropped_events=%llu active_readers=%u\n", + total, dropped, readers); +} +static DEVICE_ATTR_RO(stats); + +static ssize_t last_event_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct miscdevice *misc = dev_get_drvdata(dev); + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + struct sof_ultrasound_uapi_event ev; + unsigned long flags; + + spin_lock_irqsave(&priv->lock, flags); + ev = priv->last_event; + spin_unlock_irqrestore(&priv->lock, flags); + + if (!ev.seq) + return sysfs_emit(buf, "none\n"); + + return sysfs_emit(buf, + "seq=%u type=%s(%u) doppler_mhz=%d vel_mm_s=%d snr=%u.%02udB conf=%u carrier_hz=%u mod=%u inst=%u dsp_us=%llu ktime_ns=%llu\n", + ev.seq, sof_ultrasound_evt_name(ev.event_type), ev.event_type, + ev.doppler_mhz, ev.velocity_mm_s, + ev.snr_db_q8 >> 8, ((ev.snr_db_q8 & 0xFFU) * 100U) >> 8, + ev.confidence, ev.carrier_hz, ev.module_id, ev.instance_id, + ev.dsp_timestamp_us, ev.host_ktime_ns); +} +static DEVICE_ATTR_RO(last_event); + +static struct attribute *sof_ultrasound_attrs[] = { + &dev_attr_enable.attr, + &dev_attr_active.attr, + &dev_attr_auto_enable.attr, + &dev_attr_stats.attr, + &dev_attr_last_event.attr, + NULL, +}; +ATTRIBUTE_GROUPS(sof_ultrasound); + +static void sof_ultrasound_rx_callback(struct sof_client_dev *cdev, void *msg_buf) +{ + struct sof_ultrasound_priv *priv = cdev->data; + struct sof_ipc4_msg *ipc4_msg = msg_buf; + struct sof_ipc4_notify_module_data *mod_data; + const struct sof_ultrasound_dsp_event *dsp_ev; + struct sof_ultrasound_uapi_event ev; + struct sof_ultrasound_reader *reader; + unsigned long flags; + + if (!priv || !ipc4_msg || !ipc4_msg->data_ptr) + return; + + if (ipc4_msg->data_size < sizeof(*mod_data)) + return; + + mod_data = ipc4_msg->data_ptr; + if ((mod_data->event_id & SOF_IPC4_NOTIFY_MODULE_EVENTID_SOF_MAGIC_MASK) != + SOF_IPC4_NOTIFY_MODULE_EVENTID_GESTURE_MAGIC_VAL) + return; + + if (mod_data->event_data_size < sizeof(*dsp_ev) || + ipc4_msg->data_size < sizeof(*mod_data) + sizeof(*dsp_ev)) { + dev_warn_ratelimited(priv->dev, + "ultrasound event truncated: data_size=%zu event_data_size=%u\n", + ipc4_msg->data_size, mod_data->event_data_size); + return; + } + + dsp_ev = (const struct sof_ultrasound_dsp_event *)mod_data->event_data; + + memset(&ev, 0, sizeof(ev)); + ev.abi_version = SOF_ULTRASOUND_UAPI_VERSION; + ev.event_type = dsp_ev->event_type; + ev.module_id = mod_data->module_id; + ev.instance_id = mod_data->instance_id; + ev.subtype = mod_data->event_id & 0xFFFFU; + ev.doppler_mhz = dsp_ev->doppler_mhz; + ev.velocity_mm_s = dsp_ev->velocity_mm_s; + ev.confidence = dsp_ev->confidence; + ev.snr_db_q8 = dsp_ev->snr_db_q8; + ev.carrier_hz = dsp_ev->carrier_hz; + ev.seq = dsp_ev->seq; + ev.dsp_timestamp_us = dsp_ev->dsp_timestamp_us; + ev.host_ktime_ns = ktime_get_ns(); + + dev_info(priv->dev, + "ultrasound event: %s(%u) seq=%u doppler=%d mHz vel=%d mm/s snr=%u.%02u dB conf=%u mod=%u inst=%u\n", + sof_ultrasound_evt_name(ev.event_type), ev.event_type, ev.seq, + ev.doppler_mhz, ev.velocity_mm_s, + ev.snr_db_q8 >> 8, ((ev.snr_db_q8 & 0xFFU) * 100U) >> 8, + ev.confidence, ev.module_id, ev.instance_id); + + spin_lock_irqsave(&priv->lock, flags); + priv->total_events++; + priv->last_event = ev; + list_for_each_entry(reader, &priv->readers, list) { + if (kfifo_is_full(&reader->fifo)) { + struct sof_ultrasound_uapi_event dummy; + + if (kfifo_get(&reader->fifo, &dummy)) + priv->dropped_events++; + } + kfifo_put(&reader->fifo, ev); + } + spin_unlock_irqrestore(&priv->lock, flags); + + wake_up_interruptible(&priv->waitq); + + if (priv->input) { + input_event(priv->input, EV_MSC, MSC_GESTURE, ev.event_type); + if (ev.event_type == SOF_US_EVT_PRESENCE_DETECTED) + input_report_switch(priv->input, SW_FRONT_PROXIMITY, 1); + else if (ev.event_type == SOF_US_EVT_PRESENCE_LOST) + input_report_switch(priv->input, SW_FRONT_PROXIMITY, 0); + input_sync(priv->input); + } +} + +static int sof_ultrasound_open(struct inode *inode, struct file *file) +{ + struct miscdevice *misc = file->private_data; + struct sof_ultrasound_priv *priv = + container_of(misc, struct sof_ultrasound_priv, miscdev); + struct sof_ultrasound_reader *reader; + unsigned long flags; + + reader = kzalloc_obj(*reader); + if (!reader) + return -ENOMEM; + + INIT_KFIFO(reader->fifo); + reader->priv = priv; + kref_get(&priv->refcount); + + spin_lock_irqsave(&priv->lock, flags); + if (priv->closing) { + spin_unlock_irqrestore(&priv->lock, flags); + kref_put(&priv->refcount, sof_ultrasound_priv_release); + kfree(reader); + return -ENODEV; + } + list_add_tail(&reader->list, &priv->readers); + priv->active_readers++; + spin_unlock_irqrestore(&priv->lock, flags); + + /* Auto-enable hostless DSP sensing if supported by the loaded topology */ + mutex_lock(&priv->ctrl_mutex); + if (priv->auto_enable && !priv->sensing_active && !priv->closing) + sof_ultrasound_start_sensing_locked(priv, true); + mutex_unlock(&priv->ctrl_mutex); + + file->private_data = reader; + return 0; +} + +static int sof_ultrasound_release(struct inode *inode, struct file *file) +{ + struct sof_ultrasound_reader *reader = file->private_data; + struct sof_ultrasound_priv *priv = reader->priv; + bool no_readers; + unsigned long flags; + + spin_lock_irqsave(&priv->lock, flags); + list_del(&reader->list); + if (priv->active_readers) + priv->active_readers--; + no_readers = (priv->active_readers == 0); + spin_unlock_irqrestore(&priv->lock, flags); + + if (no_readers) { + bool still_empty; + + mutex_lock(&priv->ctrl_mutex); + spin_lock_irqsave(&priv->lock, flags); + still_empty = (priv->active_readers == 0); + spin_unlock_irqrestore(&priv->lock, flags); + + if (still_empty && !priv->sysfs_enabled && priv->sensing_active) + sof_ultrasound_stop_sensing_locked(priv); + mutex_unlock(&priv->ctrl_mutex); + } + + kfree(reader); + kref_put(&priv->refcount, sof_ultrasound_priv_release); + return 0; +} + +static ssize_t sof_ultrasound_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct sof_ultrasound_reader *reader = file->private_data; + struct sof_ultrasound_priv *priv = reader->priv; + struct sof_ultrasound_uapi_event ev; + size_t copied = 0; + unsigned long flags; + int ret; + + if (count < sizeof(ev)) + return -EINVAL; + + while (copied + sizeof(ev) <= count) { + bool got = false; + bool closing; + + spin_lock_irqsave(&priv->lock, flags); + got = kfifo_get(&reader->fifo, &ev) > 0; + closing = priv->closing; + spin_unlock_irqrestore(&priv->lock, flags); + + if (got) { + if (copy_to_user(buf + copied, &ev, sizeof(ev))) + return copied ? copied : -EFAULT; + copied += sizeof(ev); + continue; + } + + if (copied > 0) + break; + + if (closing) + return 0; + + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + + ret = wait_event_interruptible(priv->waitq, + !kfifo_is_empty(&reader->fifo) || + READ_ONCE(priv->closing)); + if (ret) + return ret; + } + + return copied; +} + +static __poll_t sof_ultrasound_poll(struct file *file, poll_table *wait) +{ + struct sof_ultrasound_reader *reader = file->private_data; + struct sof_ultrasound_priv *priv = reader->priv; + __poll_t mask = 0; + unsigned long flags; + + poll_wait(file, &priv->waitq, wait); + + spin_lock_irqsave(&priv->lock, flags); + if (!kfifo_is_empty(&reader->fifo)) + mask |= EPOLLIN | EPOLLRDNORM; + if (priv->closing) + mask |= EPOLLHUP; + spin_unlock_irqrestore(&priv->lock, flags); + + return mask; +} + +static long sof_ultrasound_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct sof_ultrasound_reader *reader = file->private_data; + struct sof_ultrasound_priv *priv = reader->priv; + unsigned long flags; + + switch (cmd) { + case SOF_ULTRASOUND_IOC_GET_STATS: { + struct sof_ultrasound_stats stats; + + spin_lock_irqsave(&priv->lock, flags); + stats.total_events = priv->total_events; + stats.dropped_events = priv->dropped_events; + stats.active_readers = priv->active_readers; + stats.last_event_type = priv->last_event.event_type; + spin_unlock_irqrestore(&priv->lock, flags); + + if (copy_to_user((void __user *)arg, &stats, sizeof(stats))) + return -EFAULT; + return 0; + } + case SOF_ULTRASOUND_IOC_FLUSH: + spin_lock_irqsave(&priv->lock, flags); + kfifo_reset(&reader->fifo); + spin_unlock_irqrestore(&priv->lock, flags); + return 0; + default: + return -ENOTTY; + } +} + +static const struct file_operations sof_ultrasound_fops = { + .owner = THIS_MODULE, + .open = sof_ultrasound_open, + .release = sof_ultrasound_release, + .read = sof_ultrasound_read, + .poll = sof_ultrasound_poll, + .unlocked_ioctl = sof_ultrasound_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +#ifdef CONFIG_PM_SLEEP +static int sof_ultrasound_pm_prepare(struct device *dev) +{ + struct sof_client_dev *cdev = dev_to_sof_client_dev(dev); + struct sof_ultrasound_priv *priv = cdev->data; + + if (!priv) + return 0; + + /* + * Preserve running hostless streams and ignore_suspend only for S0ix + * (retained D0/D0i3). For S3/hibernation (DSP D3), tear down the + * kernel-owned PCMs before ASoC/SOF suspend so they can be cleanly + * rebuilt upon resume. + */ + mutex_lock(&priv->ctrl_mutex); + if (priv->sensing_active && !sof_client_is_s0ix_suspend(cdev)) { + priv->resume_sensing = true; + sof_ultrasound_stop_sensing_locked(priv); + } + mutex_unlock(&priv->ctrl_mutex); + + return 0; +} + +static void sof_ultrasound_pm_complete(struct device *dev) +{ + struct sof_client_dev *cdev = dev_to_sof_client_dev(dev); + struct sof_ultrasound_priv *priv = cdev->data; + unsigned long flags; + bool want_sensing; + + if (!priv) + return; + + mutex_lock(&priv->ctrl_mutex); + if (priv->resume_sensing) { + priv->resume_sensing = false; + spin_lock_irqsave(&priv->lock, flags); + want_sensing = !priv->closing && + (priv->sysfs_enabled || + (priv->auto_enable && priv->active_readers > 0)); + spin_unlock_irqrestore(&priv->lock, flags); + + if (want_sensing) + sof_ultrasound_start_sensing_locked(priv, false); + } + mutex_unlock(&priv->ctrl_mutex); +} + +static const struct dev_pm_ops sof_ultrasound_pm_ops = { + .prepare = sof_ultrasound_pm_prepare, + .complete = sof_ultrasound_pm_complete, +}; +#endif + +static int sof_ultrasound_probe(struct auxiliary_device *auxdev, + const struct auxiliary_device_id *id) +{ + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); + struct sof_ultrasound_priv *priv; + struct input_dev *input; + int ret; + + if (sof_client_get_ipc_type(cdev) != SOF_IPC_TYPE_4) + return -ENODEV; + + priv = kzalloc_obj(*priv); + if (!priv) + return -ENOMEM; + + kref_init(&priv->refcount); + priv->cdev = cdev; + priv->dev = &auxdev->dev; + spin_lock_init(&priv->lock); + mutex_init(&priv->ctrl_mutex); + init_waitqueue_head(&priv->waitq); + INIT_LIST_HEAD(&priv->readers); + priv->dummy_file.f_flags = O_RDWR | O_NONBLOCK; + priv->dummy_file.f_op = &sof_ultrasound_dummy_pcm_fops; + priv->auto_enable = true; + + snprintf(priv->dev_name, sizeof(priv->dev_name), "sof-ultrasound%u", auxdev->id); + priv->miscdev.minor = MISC_DYNAMIC_MINOR; + priv->miscdev.name = priv->dev_name; + priv->miscdev.fops = &sof_ultrasound_fops; + priv->miscdev.groups = sof_ultrasound_groups; + priv->miscdev.parent = &auxdev->dev; + + input = input_allocate_device(); + if (input) { + input->name = "SOF Ultrasound Gesture"; + input->phys = priv->dev_name; + input->dev.parent = &auxdev->dev; + input->id.bustype = BUS_HOST; + input_set_capability(input, EV_MSC, MSC_GESTURE); + input_set_capability(input, EV_SW, SW_FRONT_PROXIMITY); + + ret = input_register_device(input); + if (ret) { + dev_warn(&auxdev->dev, "input device registration failed: %d\n", ret); + input_free_device(input); + } else { + priv->input = input; + } + } + + cdev->data = priv; + + ret = sof_client_register_ipc_rx_handler(cdev, SOF_IPC4_NOTIFY_MODULE_NOTIFICATION, + sof_ultrasound_rx_callback); + if (ret) { + dev_err(&auxdev->dev, "failed to register IPC4 rx handler: %d\n", ret); + goto err_input; + } + + ret = sof_client_register_card_disconnect_handler(cdev, sof_ultrasound_card_disconnect); + if (ret) { + dev_err(&auxdev->dev, "failed to register card disconnect handler: %d\n", ret); + goto err_rx_handler; + } + + /* Publish char device & sysfs only after all handlers are armed */ + ret = misc_register(&priv->miscdev); + if (ret) { + dev_err(&auxdev->dev, "failed to register /dev/%s: %d\n", priv->dev_name, ret); + goto err_card_handler; + } + + dev_info(&auxdev->dev, + "SOF ultrasound hostless sensing & event interface ready at /dev/%s\n", + priv->dev_name); + return 0; + +err_card_handler: + sof_client_unregister_card_disconnect_handler(cdev); +err_rx_handler: + sof_client_unregister_ipc_rx_handler(cdev, SOF_IPC4_NOTIFY_MODULE_NOTIFICATION); +err_input: + cdev->data = NULL; + if (priv->input) + input_unregister_device(priv->input); + kref_put(&priv->refcount, sof_ultrasound_priv_release); + return ret; +} + +static void sof_ultrasound_remove(struct auxiliary_device *auxdev) +{ + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); + struct sof_ultrasound_priv *priv = cdev->data; + unsigned long flags; + + sof_client_unregister_card_disconnect_handler(cdev); + sof_client_unregister_ipc_rx_handler(cdev, SOF_IPC4_NOTIFY_MODULE_NOTIFICATION); + + spin_lock_irqsave(&priv->lock, flags); + priv->closing = true; + spin_unlock_irqrestore(&priv->lock, flags); + wake_up_interruptible_all(&priv->waitq); + + mutex_lock(&priv->ctrl_mutex); + priv->sysfs_enabled = false; + priv->resume_sensing = false; + sof_ultrasound_stop_sensing_locked(priv); + mutex_unlock(&priv->ctrl_mutex); + + misc_deregister(&priv->miscdev); + if (priv->input) + input_unregister_device(priv->input); + + cdev->data = NULL; + kref_put(&priv->refcount, sof_ultrasound_priv_release); +} + +static const struct auxiliary_device_id sof_ultrasound_client_id_table[] = { + { .name = "snd_sof.ultrasound" }, + {}, +}; +MODULE_DEVICE_TABLE(auxiliary, sof_ultrasound_client_id_table); + +static struct auxiliary_driver sof_ultrasound_client_drv = { + .name = "sof-ultrasound", + .probe = sof_ultrasound_probe, + .remove = sof_ultrasound_remove, + .id_table = sof_ultrasound_client_id_table, + .driver = { + .pm = pm_sleep_ptr(&sof_ultrasound_pm_ops), + }, +}; + +module_auxiliary_driver(sof_ultrasound_client_drv); + +MODULE_DESCRIPTION("SOF IPC4 Hostless Ultrasonic Gesture & Presence Client Driver"); +MODULE_AUTHOR("Intel Corporation"); +MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("SND_SOC_SOF_CLIENT"); diff --git a/sound/soc/sof/sof-client.c b/sound/soc/sof/sof-client.c index 6b4d87efa65319..c3943ccb83a4c6 100644 --- a/sound/soc/sof/sof-client.c +++ b/sound/soc/sof/sof-client.c @@ -45,6 +45,12 @@ struct sof_state_event_entry { struct list_head list; }; +struct sof_card_disconnect_entry { + struct sof_client_dev *cdev; + sof_client_card_disconnect_callback callback; + struct list_head list; +}; + /** * struct sof_client_dev_entry - client device entry for internal management use * @sdev: pointer to SOF core device struct @@ -186,6 +192,29 @@ static inline int sof_register_fw_gdb(struct snd_sof_dev *sdev) static inline void sof_unregister_fw_gdb(struct snd_sof_dev *sdev) {} #endif /* CONFIG_SND_SOC_SOF_DEBUG_FW_GDB */ +#if IS_ENABLED(CONFIG_SND_SOC_SOF_ULTRASOUND) +static int sof_register_ultrasound(struct snd_sof_dev *sdev) +{ + if (sdev->pdata->ipc_type != SOF_IPC_TYPE_4) + return 0; + + return sof_client_dev_register(sdev, "ultrasound", 0, NULL, 0); +} + +static void sof_unregister_ultrasound(struct snd_sof_dev *sdev) +{ + if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) + sof_client_dev_unregister(sdev, "ultrasound", 0); +} +#else +static inline int sof_register_ultrasound(struct snd_sof_dev *sdev) +{ + return 0; +} + +static inline void sof_unregister_ultrasound(struct snd_sof_dev *sdev) {} +#endif /* CONFIG_SND_SOC_SOF_ULTRASOUND */ + int sof_register_clients(struct snd_sof_dev *sdev) { int ret; @@ -218,6 +247,12 @@ int sof_register_clients(struct snd_sof_dev *sdev) goto err_fw_gdb; } + ret = sof_register_ultrasound(sdev); + if (ret) { + dev_err(sdev->dev, "Ultrasound client registration failed\n"); + goto err_ultrasound; + } + /* Platform dependent client device registration */ if (sof_ops(sdev) && sof_ops(sdev)->register_ipc_clients) @@ -226,11 +261,14 @@ int sof_register_clients(struct snd_sof_dev *sdev) if (!ret) return 0; - sof_unregister_ipc_kernel_injector(sdev); + sof_unregister_ultrasound(sdev); -err_fw_gdb: +err_ultrasound: sof_unregister_fw_gdb(sdev); +err_fw_gdb: + sof_unregister_ipc_kernel_injector(sdev); + err_kernel_injector: sof_unregister_ipc_msg_injector(sdev); @@ -245,6 +283,7 @@ void sof_unregister_clients(struct snd_sof_dev *sdev) if (sof_ops(sdev) && sof_ops(sdev)->unregister_ipc_clients) sof_ops(sdev)->unregister_ipc_clients(sdev); + sof_unregister_ultrasound(sdev); sof_unregister_ipc_kernel_injector(sdev); sof_unregister_ipc_msg_injector(sdev); sof_unregister_ipc_flood_test(sdev); @@ -491,6 +530,22 @@ struct device *sof_client_get_dma_dev(struct sof_client_dev *cdev) } EXPORT_SYMBOL_NS_GPL(sof_client_get_dma_dev, "SND_SOC_SOF_CLIENT"); +struct snd_soc_component *sof_client_get_component(struct sof_client_dev *cdev) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + + return sdev->component; +} +EXPORT_SYMBOL_NS_GPL(sof_client_get_component, "SND_SOC_SOF_CLIENT"); + +bool sof_client_is_s0ix_suspend(struct sof_client_dev *cdev) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + + return sdev->system_suspend_target == SOF_SUSPEND_S0IX; +} +EXPORT_SYMBOL_NS_GPL(sof_client_is_s0ix_suspend, "SND_SOC_SOF_CLIENT"); + const struct sof_ipc_fw_version *sof_client_get_fw_version(struct sof_client_dev *cdev) { struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); @@ -582,7 +637,7 @@ int sof_client_register_ipc_rx_handler(struct sof_client_dev *cdev, if (!(ipc_msg_type & SOF_GLB_TYPE_MASK)) return -EINVAL; } else if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) { - if (!(ipc_msg_type & SOF_IPC4_NOTIFICATION_TYPE_MASK)) + if (ipc_msg_type >= SOF_IPC4_NOTIFY_TYPE_LAST) return -EINVAL; } else { dev_warn(sdev->dev, "%s: Not supported IPC version: %d\n", @@ -676,6 +731,56 @@ void sof_client_unregister_fw_state_handler(struct sof_client_dev *cdev) } EXPORT_SYMBOL_NS_GPL(sof_client_unregister_fw_state_handler, "SND_SOC_SOF_CLIENT"); +void sof_client_card_disconnect_dispatcher(struct snd_sof_dev *sdev) +{ + struct sof_card_disconnect_entry *entry; + + guard(mutex)(&sdev->card_disconnect_mutex); + + list_for_each_entry(entry, &sdev->card_disconnect_handler_list, list) + entry->callback(entry->cdev); +} + +int sof_client_register_card_disconnect_handler(struct sof_client_dev *cdev, + sof_client_card_disconnect_callback callback) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct sof_card_disconnect_entry *entry; + + if (!callback) + return -EINVAL; + + entry = kmalloc_obj(*entry); + if (!entry) + return -ENOMEM; + + entry->cdev = cdev; + entry->callback = callback; + + guard(mutex)(&sdev->card_disconnect_mutex); + list_add(&entry->list, &sdev->card_disconnect_handler_list); + + return 0; +} +EXPORT_SYMBOL_NS_GPL(sof_client_register_card_disconnect_handler, "SND_SOC_SOF_CLIENT"); + +void sof_client_unregister_card_disconnect_handler(struct sof_client_dev *cdev) +{ + struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); + struct sof_card_disconnect_entry *entry; + + guard(mutex)(&sdev->card_disconnect_mutex); + + list_for_each_entry(entry, &sdev->card_disconnect_handler_list, list) { + if (entry->cdev == cdev) { + list_del(&entry->list); + kfree(entry); + break; + } + } +} +EXPORT_SYMBOL_NS_GPL(sof_client_unregister_card_disconnect_handler, "SND_SOC_SOF_CLIENT"); + enum sof_fw_state sof_client_get_fw_state(struct sof_client_dev *cdev) { struct snd_sof_dev *sdev = sof_client_dev_to_sof_dev(cdev); diff --git a/sound/soc/sof/sof-client.h b/sound/soc/sof/sof-client.h index e796801570c823..353d1ddcd191d3 100644 --- a/sound/soc/sof/sof-client.h +++ b/sound/soc/sof/sof-client.h @@ -10,6 +10,7 @@ struct sof_ipc_fw_version; struct sof_ipc_cmd_hdr; +struct snd_soc_component; struct snd_sof_dev; struct dentry; @@ -46,6 +47,8 @@ struct snd_sof_widget *sof_client_ipc4_find_swidget_by_id(struct sof_client_dev struct dentry *sof_client_get_debugfs_root(struct sof_client_dev *cdev); struct device *sof_client_get_dma_dev(struct sof_client_dev *cdev); +struct snd_soc_component *sof_client_get_component(struct sof_client_dev *cdev); +bool sof_client_is_s0ix_suspend(struct sof_client_dev *cdev); const struct sof_ipc_fw_version *sof_client_get_fw_version(struct sof_client_dev *cdev); size_t sof_client_get_ipc_max_payload_size(struct sof_client_dev *cdev); enum sof_ipc_type sof_client_get_ipc_type(struct sof_client_dev *cdev); @@ -73,6 +76,13 @@ typedef void (*sof_client_fw_state_callback)(struct sof_client_dev *cdev, int sof_client_register_fw_state_handler(struct sof_client_dev *cdev, sof_client_fw_state_callback callback); void sof_client_unregister_fw_state_handler(struct sof_client_dev *cdev); + +/* ALSA card disconnect notification */ +typedef void (*sof_client_card_disconnect_callback)(struct sof_client_dev *cdev); + +int sof_client_register_card_disconnect_handler(struct sof_client_dev *cdev, + sof_client_card_disconnect_callback callback); +void sof_client_unregister_card_disconnect_handler(struct sof_client_dev *cdev); enum sof_fw_state sof_client_get_fw_state(struct sof_client_dev *cdev); int sof_client_ipc_rx_message(struct sof_client_dev *cdev, void *ipc_msg, void *msg_buf); diff --git a/sound/soc/sof/sof-priv.h b/sound/soc/sof/sof-priv.h index 83e4d83cc27cbb..477be43f16cf5e 100644 --- a/sound/soc/sof/sof-priv.h +++ b/sound/soc/sof/sof-priv.h @@ -702,10 +702,18 @@ struct snd_sof_dev { * notification */ struct list_head fw_state_handler_list; + struct list_head card_disconnect_handler_list; /* to protect the ipc_rx_handler_list and dsp_state_handler_list list */ struct mutex client_event_handler_mutex; + /* + * Separate mutex for card_disconnect_handler_list: disconnect + * callbacks may perform blocking PCM/IPC/SoundWire teardown and + * must not block sof_client_ipc_rx_dispatcher() in the IRQ thread. + */ + struct mutex card_disconnect_mutex; + /* quirks to override topology values */ bool mclk_id_override; u16 mclk_id_quirk; /* same size as in IPC3 definitions */ @@ -881,6 +889,7 @@ int sof_register_clients(struct snd_sof_dev *sdev); void sof_unregister_clients(struct snd_sof_dev *sdev); void sof_client_ipc_rx_dispatcher(struct snd_sof_dev *sdev, void *msg_buf); void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev); +void sof_client_card_disconnect_dispatcher(struct snd_sof_dev *sdev); int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state); int sof_resume_clients(struct snd_sof_dev *sdev); #else /* CONFIG_SND_SOC_SOF_CLIENT */ @@ -917,6 +926,10 @@ static inline void sof_client_fw_state_dispatcher(struct snd_sof_dev *sdev) { } +static inline void sof_client_card_disconnect_dispatcher(struct snd_sof_dev *sdev) +{ +} + static inline int sof_suspend_clients(struct snd_sof_dev *sdev, pm_message_t state) { return 0; diff --git a/sound/soc/sof/topology.c b/sound/soc/sof/topology.c index 750916b57def74..1a8ea72d64ac8d 100644 --- a/sound/soc/sof/topology.c +++ b/sound/soc/sof/topology.c @@ -1206,6 +1206,7 @@ static int spcm_bind(struct snd_soc_component *scomp, struct snd_sof_pcm *spcm, } spcm->stream[dir].comp_id = host_widget->comp_id; + spcm->stream[dir].hostless = host_widget->hostless; return 0; } @@ -1550,11 +1551,19 @@ static int sof_widget_ready(struct snd_soc_component *scomp, int index, } fallthrough; + case snd_soc_dapm_aif_out: + case snd_soc_dapm_aif_in: + if (le32_to_cpu(tw->priv.size) == 0) { + swidget->hostless = true; + dev_dbg(scomp->dev, "tplg: hostless AIF widget %s (stream: %s)\n", + w->name, w->sname ? w->sname : "none"); + break; + } + ret = sof_widget_parse_tokens(scomp, swidget, tw, token_list, token_list_size); + break; case snd_soc_dapm_mixer: case snd_soc_dapm_buffer: case snd_soc_dapm_scheduler: - case snd_soc_dapm_aif_out: - case snd_soc_dapm_aif_in: case snd_soc_dapm_src: case snd_soc_dapm_asrc: case snd_soc_dapm_siggen: @@ -1727,7 +1736,7 @@ static int sof_widget_unload(struct snd_soc_component *scomp, out: /* free IPC related data */ widget_ops = tplg_ops ? tplg_ops->widget : NULL; - if (widget_ops && widget_ops[swidget->id].ipc_free) + if (!swidget->hostless && widget_ops && widget_ops[swidget->id].ipc_free) widget_ops[swidget->id].ipc_free(swidget); ida_destroy(&swidget->output_queue_ida); @@ -2137,7 +2146,8 @@ static int sof_route_load(struct snd_soc_component *scomp, int index, * to the DSP. */ if (source_swidget->id == snd_soc_dapm_out_drv || - source_swidget->id == snd_soc_dapm_output) + source_swidget->id == snd_soc_dapm_output || + source_swidget->hostless) goto err; /* sink component */ @@ -2154,7 +2164,8 @@ static int sof_route_load(struct snd_soc_component *scomp, int index, * output or out_drv to the DSP */ if (sink_swidget->id == snd_soc_dapm_out_drv || - sink_swidget->id == snd_soc_dapm_output) + sink_swidget->id == snd_soc_dapm_output || + sink_swidget->hostless) goto err; sroute->route = route; @@ -2254,7 +2265,8 @@ static int sof_complete(struct snd_soc_component *scomp) if (ret < 0) return ret; - if (widget_ops && widget_ops[swidget->id].ipc_setup) { + if (!swidget->hostless && widget_ops && + widget_ops[swidget->id].ipc_setup) { ret = widget_ops[swidget->id].ipc_setup(swidget); if (ret < 0) { dev_err(sdev->dev,