Kuninori Morimoto | f2b6a1b | 2018-07-02 06:24:45 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // soc-topology.c -- ALSA SoC Topology |
| 4 | // |
| 5 | // Copyright (C) 2012 Texas Instruments Inc. |
| 6 | // Copyright (C) 2015 Intel Corporation. |
| 7 | // |
| 8 | // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com> |
| 9 | // K, Mythri P <mythri.p.k@intel.com> |
| 10 | // Prusty, Subhransu S <subhransu.s.prusty@intel.com> |
| 11 | // B, Jayachandran <jayachandran.b@intel.com> |
| 12 | // Abdullah, Omair M <omair.m.abdullah@intel.com> |
| 13 | // Jin, Yao <yao.jin@intel.com> |
| 14 | // Lin, Mengdong <mengdong.lin@intel.com> |
| 15 | // |
| 16 | // Add support to read audio firmware topology alongside firmware text. The |
| 17 | // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links, |
| 18 | // equalizers, firmware, coefficients etc. |
| 19 | // |
| 20 | // This file only manages the core ALSA and ASoC components, all other bespoke |
| 21 | // firmware topology data is passed to component drivers for bespoke handling. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/export.h> |
| 25 | #include <linux/list.h> |
| 26 | #include <linux/firmware.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <sound/soc.h> |
| 29 | #include <sound/soc-dapm.h> |
| 30 | #include <sound/soc-topology.h> |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 31 | #include <sound/tlv.h> |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 32 | |
Pierre-Louis Bossart | 2114171 | 2019-04-04 14:13:58 -0500 | [diff] [blame] | 33 | #define SOC_TPLG_MAGIC_BIG_ENDIAN 0x436F5341 /* ASoC in reverse */ |
| 34 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 35 | /* |
| 36 | * We make several passes over the data (since it wont necessarily be ordered) |
| 37 | * and process objects in the following order. This guarantees the component |
| 38 | * drivers will be ready with any vendor data before the mixers and DAPM objects |
| 39 | * are loaded (that may make use of the vendor data). |
| 40 | */ |
| 41 | #define SOC_TPLG_PASS_MANIFEST 0 |
| 42 | #define SOC_TPLG_PASS_VENDOR 1 |
| 43 | #define SOC_TPLG_PASS_MIXER 2 |
| 44 | #define SOC_TPLG_PASS_WIDGET 3 |
Mengdong Lin | 1a8e7fa | 2015-08-10 22:48:30 +0800 | [diff] [blame] | 45 | #define SOC_TPLG_PASS_PCM_DAI 4 |
| 46 | #define SOC_TPLG_PASS_GRAPH 5 |
| 47 | #define SOC_TPLG_PASS_PINS 6 |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 48 | #define SOC_TPLG_PASS_BE_DAI 7 |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 49 | #define SOC_TPLG_PASS_LINK 8 |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 50 | |
| 51 | #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 52 | #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 53 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 54 | /* topology context */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 55 | struct soc_tplg { |
| 56 | const struct firmware *fw; |
| 57 | |
| 58 | /* runtime FW parsing */ |
| 59 | const u8 *pos; /* read postion */ |
| 60 | const u8 *hdr_pos; /* header position */ |
| 61 | unsigned int pass; /* pass number */ |
| 62 | |
| 63 | /* component caller */ |
| 64 | struct device *dev; |
| 65 | struct snd_soc_component *comp; |
| 66 | u32 index; /* current block index */ |
| 67 | u32 req_index; /* required index, only loaded/free matching blocks */ |
| 68 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 69 | /* vendor specific kcontrol operations */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 70 | const struct snd_soc_tplg_kcontrol_ops *io_ops; |
| 71 | int io_ops_count; |
| 72 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 73 | /* vendor specific bytes ext handlers, for TLV bytes controls */ |
| 74 | const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; |
| 75 | int bytes_ext_ops_count; |
| 76 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 77 | /* optional fw loading callbacks to component drivers */ |
| 78 | struct snd_soc_tplg_ops *ops; |
| 79 | }; |
| 80 | |
| 81 | static int soc_tplg_process_headers(struct soc_tplg *tplg); |
| 82 | static void soc_tplg_complete(struct soc_tplg *tplg); |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 83 | static void soc_tplg_denum_remove_texts(struct soc_enum *se); |
| 84 | static void soc_tplg_denum_remove_values(struct soc_enum *se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 85 | |
| 86 | /* check we dont overflow the data for this control chunk */ |
| 87 | static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, |
| 88 | unsigned int count, size_t bytes, const char *elem_type) |
| 89 | { |
| 90 | const u8 *end = tplg->pos + elem_size * count; |
| 91 | |
| 92 | if (end > tplg->fw->data + tplg->fw->size) { |
| 93 | dev_err(tplg->dev, "ASoC: %s overflow end of data\n", |
| 94 | elem_type); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | /* check there is enough room in chunk for control. |
| 99 | extra bytes at the end of control are for vendor data here */ |
| 100 | if (elem_size * count > bytes) { |
| 101 | dev_err(tplg->dev, |
| 102 | "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", |
| 103 | elem_type, count, elem_size, bytes); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static inline int soc_tplg_is_eof(struct soc_tplg *tplg) |
| 111 | { |
| 112 | const u8 *end = tplg->hdr_pos; |
| 113 | |
| 114 | if (end >= tplg->fw->data + tplg->fw->size) |
| 115 | return 1; |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) |
| 120 | { |
| 121 | return (unsigned long)(tplg->hdr_pos - tplg->fw->data); |
| 122 | } |
| 123 | |
| 124 | static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) |
| 125 | { |
| 126 | return (unsigned long)(tplg->pos - tplg->fw->data); |
| 127 | } |
| 128 | |
| 129 | /* mapping of Kcontrol types and associated operations. */ |
| 130 | static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { |
| 131 | {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, |
| 132 | snd_soc_put_volsw, snd_soc_info_volsw}, |
| 133 | {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, |
| 134 | snd_soc_put_volsw_sx, NULL}, |
| 135 | {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, |
| 136 | snd_soc_put_enum_double, snd_soc_info_enum_double}, |
| 137 | {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, |
| 138 | snd_soc_put_enum_double, NULL}, |
| 139 | {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, |
| 140 | snd_soc_bytes_put, snd_soc_bytes_info}, |
| 141 | {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, |
| 142 | snd_soc_put_volsw_range, snd_soc_info_volsw_range}, |
| 143 | {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, |
| 144 | snd_soc_put_xr_sx, snd_soc_info_xr_sx}, |
| 145 | {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, |
| 146 | snd_soc_put_strobe, NULL}, |
| 147 | {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, |
Jeeja KP | 2c57d478 | 2015-07-14 13:10:47 +0530 | [diff] [blame] | 148 | snd_soc_dapm_put_volsw, snd_soc_info_volsw}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 149 | {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, |
| 150 | snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, |
| 151 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, |
| 152 | snd_soc_dapm_put_enum_double, NULL}, |
| 153 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, |
| 154 | snd_soc_dapm_put_enum_double, NULL}, |
| 155 | {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, |
| 156 | snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, |
| 157 | }; |
| 158 | |
| 159 | struct soc_tplg_map { |
| 160 | int uid; |
| 161 | int kid; |
| 162 | }; |
| 163 | |
| 164 | /* mapping of widget types from UAPI IDs to kernel IDs */ |
| 165 | static const struct soc_tplg_map dapm_map[] = { |
| 166 | {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, |
| 167 | {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, |
| 168 | {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, |
| 169 | {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, |
| 170 | {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, |
| 171 | {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, |
| 172 | {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, |
| 173 | {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, |
| 174 | {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, |
| 175 | {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, |
| 176 | {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, |
| 177 | {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, |
| 178 | {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, |
| 179 | {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, |
| 180 | {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, |
| 181 | {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, |
Liam Girdwood | 8a70b45 | 2017-06-29 14:22:24 +0100 | [diff] [blame] | 182 | {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer}, |
| 183 | {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler}, |
| 184 | {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect}, |
| 185 | {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen}, |
| 186 | {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src}, |
| 187 | {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc}, |
| 188 | {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder}, |
| 189 | {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | static int tplc_chan_get_reg(struct soc_tplg *tplg, |
| 193 | struct snd_soc_tplg_channel *chan, int map) |
| 194 | { |
| 195 | int i; |
| 196 | |
| 197 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 198 | if (le32_to_cpu(chan[i].id) == map) |
| 199 | return le32_to_cpu(chan[i].reg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | static int tplc_chan_get_shift(struct soc_tplg *tplg, |
| 206 | struct snd_soc_tplg_channel *chan, int map) |
| 207 | { |
| 208 | int i; |
| 209 | |
| 210 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 211 | if (le32_to_cpu(chan[i].id) == map) |
| 212 | return le32_to_cpu(chan[i].shift); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | |
| 218 | static int get_widget_id(int tplg_type) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { |
| 223 | if (tplg_type == dapm_map[i].uid) |
| 224 | return dapm_map[i].kid; |
| 225 | } |
| 226 | |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 230 | static inline void soc_bind_err(struct soc_tplg *tplg, |
| 231 | struct snd_soc_tplg_ctl_hdr *hdr, int index) |
| 232 | { |
| 233 | dev_err(tplg->dev, |
| 234 | "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", |
| 235 | hdr->ops.get, hdr->ops.put, hdr->ops.info, index, |
| 236 | soc_tplg_get_offset(tplg)); |
| 237 | } |
| 238 | |
| 239 | static inline void soc_control_err(struct soc_tplg *tplg, |
| 240 | struct snd_soc_tplg_ctl_hdr *hdr, const char *name) |
| 241 | { |
| 242 | dev_err(tplg->dev, |
| 243 | "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", |
| 244 | name, hdr->ops.get, hdr->ops.put, hdr->ops.info, |
| 245 | soc_tplg_get_offset(tplg)); |
| 246 | } |
| 247 | |
| 248 | /* pass vendor data to component driver for processing */ |
Keyon Jie | c2cbd0a | 2020-05-27 10:28:01 +0800 | [diff] [blame] | 249 | static int soc_tplg_vendor_load(struct soc_tplg *tplg, |
| 250 | struct snd_soc_tplg_hdr *hdr) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 251 | { |
| 252 | int ret = 0; |
| 253 | |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 254 | if (tplg->ops && tplg->ops->vendor_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 255 | ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 256 | else { |
| 257 | dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", |
| 258 | hdr->vendor_type); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | if (ret < 0) |
| 263 | dev_err(tplg->dev, |
| 264 | "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", |
| 265 | soc_tplg_get_hdr_offset(tplg), |
| 266 | soc_tplg_get_hdr_offset(tplg), |
| 267 | hdr->type, hdr->vendor_type); |
| 268 | return ret; |
| 269 | } |
| 270 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 271 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 272 | * external widgets where we can assign private data/ops */ |
| 273 | static int soc_tplg_widget_load(struct soc_tplg *tplg, |
| 274 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 275 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 276 | if (tplg->ops && tplg->ops->widget_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 277 | return tplg->ops->widget_load(tplg->comp, tplg->index, w, |
| 278 | tplg_w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 283 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 284 | * external widgets where we can assign private data/ops */ |
| 285 | static int soc_tplg_widget_ready(struct soc_tplg *tplg, |
| 286 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 287 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 288 | if (tplg->ops && tplg->ops->widget_ready) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 289 | return tplg->ops->widget_ready(tplg->comp, tplg->index, w, |
| 290 | tplg_w); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 295 | /* pass DAI configurations to component driver for extra initialization */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 296 | static int soc_tplg_dai_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 297 | struct snd_soc_dai_driver *dai_drv, |
| 298 | struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 299 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 300 | if (tplg->ops && tplg->ops->dai_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 301 | return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv, |
| 302 | pcm, dai); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Masahiro Yamada | 183b802 | 2017-02-27 14:29:20 -0800 | [diff] [blame] | 307 | /* pass link configurations to component driver for extra initialization */ |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 308 | static int soc_tplg_dai_link_load(struct soc_tplg *tplg, |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 309 | struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg) |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 310 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 311 | if (tplg->ops && tplg->ops->link_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 312 | return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 317 | /* tell the component driver that all firmware has been loaded in this request */ |
| 318 | static void soc_tplg_complete(struct soc_tplg *tplg) |
| 319 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 320 | if (tplg->ops && tplg->ops->complete) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 321 | tplg->ops->complete(tplg->comp); |
| 322 | } |
| 323 | |
| 324 | /* add a dynamic kcontrol */ |
| 325 | static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, |
| 326 | const struct snd_kcontrol_new *control_new, const char *prefix, |
| 327 | void *data, struct snd_kcontrol **kcontrol) |
| 328 | { |
| 329 | int err; |
| 330 | |
| 331 | *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); |
| 332 | if (*kcontrol == NULL) { |
| 333 | dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", |
| 334 | control_new->name); |
| 335 | return -ENOMEM; |
| 336 | } |
| 337 | |
| 338 | err = snd_ctl_add(card, *kcontrol); |
| 339 | if (err < 0) { |
| 340 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
| 341 | control_new->name, err); |
| 342 | return err; |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /* add a dynamic kcontrol for component driver */ |
| 349 | static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, |
| 350 | struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) |
| 351 | { |
| 352 | struct snd_soc_component *comp = tplg->comp; |
| 353 | |
| 354 | return soc_tplg_add_dcontrol(comp->card->snd_card, |
이경택 | abca9e4a | 2020-04-01 18:05:24 +0900 | [diff] [blame] | 355 | comp->dev, k, comp->name_prefix, comp, kcontrol); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* remove a mixer kcontrol */ |
| 359 | static void remove_mixer(struct snd_soc_component *comp, |
| 360 | struct snd_soc_dobj *dobj, int pass) |
| 361 | { |
| 362 | struct snd_card *card = comp->card->snd_card; |
| 363 | struct soc_mixer_control *sm = |
| 364 | container_of(dobj, struct soc_mixer_control, dobj); |
| 365 | const unsigned int *p = NULL; |
| 366 | |
| 367 | if (pass != SOC_TPLG_PASS_MIXER) |
| 368 | return; |
| 369 | |
| 370 | if (dobj->ops && dobj->ops->control_unload) |
| 371 | dobj->ops->control_unload(comp, dobj); |
| 372 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 373 | if (dobj->control.kcontrol->tlv.p) |
| 374 | p = dobj->control.kcontrol->tlv.p; |
| 375 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 376 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 377 | kfree(sm); |
| 378 | kfree(p); |
| 379 | } |
| 380 | |
| 381 | /* remove an enum kcontrol */ |
| 382 | static void remove_enum(struct snd_soc_component *comp, |
| 383 | struct snd_soc_dobj *dobj, int pass) |
| 384 | { |
| 385 | struct snd_card *card = comp->card->snd_card; |
| 386 | struct soc_enum *se = container_of(dobj, struct soc_enum, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 387 | |
| 388 | if (pass != SOC_TPLG_PASS_MIXER) |
| 389 | return; |
| 390 | |
| 391 | if (dobj->ops && dobj->ops->control_unload) |
| 392 | dobj->ops->control_unload(comp, dobj); |
| 393 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 394 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 395 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 396 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 397 | soc_tplg_denum_remove_values(se); |
| 398 | soc_tplg_denum_remove_texts(se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 399 | kfree(se); |
| 400 | } |
| 401 | |
| 402 | /* remove a byte kcontrol */ |
| 403 | static void remove_bytes(struct snd_soc_component *comp, |
| 404 | struct snd_soc_dobj *dobj, int pass) |
| 405 | { |
| 406 | struct snd_card *card = comp->card->snd_card; |
| 407 | struct soc_bytes_ext *sb = |
| 408 | container_of(dobj, struct soc_bytes_ext, dobj); |
| 409 | |
| 410 | if (pass != SOC_TPLG_PASS_MIXER) |
| 411 | return; |
| 412 | |
| 413 | if (dobj->ops && dobj->ops->control_unload) |
| 414 | dobj->ops->control_unload(comp, dobj); |
| 415 | |
Amadeusz Sławiński | 33ae6ae | 2019-01-25 14:06:42 -0600 | [diff] [blame] | 416 | snd_ctl_remove(card, dobj->control.kcontrol); |
| 417 | list_del(&dobj->list); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 418 | kfree(sb); |
| 419 | } |
| 420 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 421 | /* remove a route */ |
| 422 | static void remove_route(struct snd_soc_component *comp, |
| 423 | struct snd_soc_dobj *dobj, int pass) |
| 424 | { |
| 425 | struct snd_soc_dapm_route *route = |
| 426 | container_of(dobj, struct snd_soc_dapm_route, dobj); |
| 427 | |
| 428 | if (pass != SOC_TPLG_PASS_GRAPH) |
| 429 | return; |
| 430 | |
| 431 | if (dobj->ops && dobj->ops->dapm_route_unload) |
| 432 | dobj->ops->dapm_route_unload(comp, dobj); |
| 433 | |
| 434 | list_del(&dobj->list); |
| 435 | kfree(route); |
| 436 | } |
| 437 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 438 | /* remove a widget and it's kcontrols - routes must be removed first */ |
| 439 | static void remove_widget(struct snd_soc_component *comp, |
| 440 | struct snd_soc_dobj *dobj, int pass) |
| 441 | { |
| 442 | struct snd_card *card = comp->card->snd_card; |
| 443 | struct snd_soc_dapm_widget *w = |
| 444 | container_of(dobj, struct snd_soc_dapm_widget, dobj); |
| 445 | int i; |
| 446 | |
| 447 | if (pass != SOC_TPLG_PASS_WIDGET) |
| 448 | return; |
| 449 | |
| 450 | if (dobj->ops && dobj->ops->widget_unload) |
| 451 | dobj->ops->widget_unload(comp, dobj); |
| 452 | |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 453 | if (!w->kcontrols) |
| 454 | goto free_news; |
| 455 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 456 | /* |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 457 | * Dynamic Widgets either have 1..N enum kcontrols or mixers. |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 458 | * The enum may either have an array of values or strings. |
| 459 | */ |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 460 | if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 461 | /* enumerated widget mixer */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 462 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 463 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
| 464 | struct soc_enum *se = |
| 465 | (struct soc_enum *)kcontrol->private_value; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 466 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 467 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 468 | |
Ranjani Sridharan | 54f8844 | 2019-04-04 19:48:33 -0700 | [diff] [blame] | 469 | /* free enum kcontrol's dvalues and dtexts */ |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 470 | soc_tplg_denum_remove_values(se); |
| 471 | soc_tplg_denum_remove_texts(se); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 472 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 473 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 474 | kfree(w->kcontrol_news[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 475 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 476 | } else { |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 477 | /* volume mixer or bytes controls */ |
Liam Girdwood | f53c4c2 | 2018-03-27 14:30:44 +0100 | [diff] [blame] | 478 | for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 479 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 480 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 481 | if (dobj->widget.kcontrol_type |
| 482 | == SND_SOC_TPLG_TYPE_MIXER) |
| 483 | kfree(kcontrol->tlv.p); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 484 | |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 485 | /* Private value is used as struct soc_mixer_control |
| 486 | * for volume mixers or soc_bytes_ext for bytes |
| 487 | * controls. |
| 488 | */ |
| 489 | kfree((void *)kcontrol->private_value); |
Colin Ian King | c2b3612 | 2016-12-09 14:17:47 +0000 | [diff] [blame] | 490 | snd_ctl_remove(card, kcontrol); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 491 | kfree(w->kcontrol_news[i].name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 492 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 493 | } |
Liam Girdwood | 05bdcf1 | 2018-03-14 20:42:40 +0000 | [diff] [blame] | 494 | |
| 495 | free_news: |
| 496 | kfree(w->kcontrol_news); |
| 497 | |
Amadeusz Sławiński | a46e839 | 2019-01-25 14:06:43 -0600 | [diff] [blame] | 498 | list_del(&dobj->list); |
| 499 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 500 | /* widget w is freed by soc-dapm.c */ |
| 501 | } |
| 502 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 503 | /* remove DAI configurations */ |
| 504 | static void remove_dai(struct snd_soc_component *comp, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 505 | struct snd_soc_dobj *dobj, int pass) |
| 506 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 507 | struct snd_soc_dai_driver *dai_drv = |
| 508 | container_of(dobj, struct snd_soc_dai_driver, dobj); |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 509 | struct snd_soc_dai *dai; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 510 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 511 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 512 | return; |
| 513 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 514 | if (dobj->ops && dobj->ops->dai_unload) |
| 515 | dobj->ops->dai_unload(comp, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 516 | |
Kuninori Morimoto | 43ca5da | 2019-08-20 14:05:32 +0900 | [diff] [blame] | 517 | for_each_component_dais(comp, dai) |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 518 | if (dai->driver == dai_drv) |
| 519 | dai->driver = NULL; |
| 520 | |
Bard liao | 7b6f68a | 2019-03-05 23:57:52 +0800 | [diff] [blame] | 521 | kfree(dai_drv->playback.stream_name); |
| 522 | kfree(dai_drv->capture.stream_name); |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 523 | kfree(dai_drv->name); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 524 | list_del(&dobj->list); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 525 | kfree(dai_drv); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 528 | /* remove link configurations */ |
| 529 | static void remove_link(struct snd_soc_component *comp, |
| 530 | struct snd_soc_dobj *dobj, int pass) |
| 531 | { |
| 532 | struct snd_soc_dai_link *link = |
| 533 | container_of(dobj, struct snd_soc_dai_link, dobj); |
| 534 | |
| 535 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 536 | return; |
| 537 | |
| 538 | if (dobj->ops && dobj->ops->link_unload) |
| 539 | dobj->ops->link_unload(comp, dobj); |
| 540 | |
Dragos Tarcatu | dd836dd | 2019-12-04 15:04:47 -0600 | [diff] [blame] | 541 | list_del(&dobj->list); |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 542 | snd_soc_remove_pcm_runtime(comp->card, |
| 543 | snd_soc_get_pcm_runtime(comp->card, link)); |
Dragos Tarcatu | dd836dd | 2019-12-04 15:04:47 -0600 | [diff] [blame] | 544 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 545 | kfree(link->name); |
| 546 | kfree(link->stream_name); |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 547 | kfree(link->cpus->dai_name); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 548 | kfree(link); |
| 549 | } |
| 550 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 551 | /* unload dai link */ |
| 552 | static void remove_backend_link(struct snd_soc_component *comp, |
| 553 | struct snd_soc_dobj *dobj, int pass) |
| 554 | { |
| 555 | if (pass != SOC_TPLG_PASS_LINK) |
| 556 | return; |
| 557 | |
| 558 | if (dobj->ops && dobj->ops->link_unload) |
| 559 | dobj->ops->link_unload(comp, dobj); |
| 560 | |
| 561 | /* |
| 562 | * We don't free the link here as what remove_link() do since BE |
| 563 | * links are not allocated by topology. |
| 564 | * We however need to reset the dobj type to its initial values |
| 565 | */ |
| 566 | dobj->type = SND_SOC_DOBJ_NONE; |
| 567 | list_del(&dobj->list); |
| 568 | } |
| 569 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 570 | /* bind a kcontrol to it's IO handlers */ |
| 571 | static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, |
| 572 | struct snd_kcontrol_new *k, |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 573 | const struct soc_tplg *tplg) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 574 | { |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 575 | const struct snd_soc_tplg_kcontrol_ops *ops; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 576 | const struct snd_soc_tplg_bytes_ext_ops *ext_ops; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 577 | int num_ops, i; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 578 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 579 | if (le32_to_cpu(hdr->ops.info) == SND_SOC_TPLG_CTL_BYTES |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 580 | && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER |
| 581 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
| 582 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 583 | struct soc_bytes_ext *sbe; |
| 584 | struct snd_soc_tplg_bytes_control *be; |
| 585 | |
| 586 | sbe = (struct soc_bytes_ext *)k->private_value; |
| 587 | be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); |
| 588 | |
| 589 | /* TLV bytes controls need standard kcontrol info handler, |
| 590 | * TLV callback and extended put/get handlers. |
| 591 | */ |
Omair M Abdullah | f4be978 | 2015-11-09 23:20:01 +0530 | [diff] [blame] | 592 | k->info = snd_soc_bytes_info_ext; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 593 | k->tlv.c = snd_soc_bytes_tlv_callback; |
| 594 | |
Pierre-Louis Bossart | 6788fc1 | 2020-09-17 13:39:12 +0300 | [diff] [blame] | 595 | /* |
| 596 | * When a topology-based implementation abuses the |
| 597 | * control interface and uses bytes_ext controls of |
| 598 | * more than 512 bytes, we need to disable the size |
| 599 | * checks, otherwise accesses to such controls will |
| 600 | * return an -EINVAL error and prevent the card from |
| 601 | * being configured. |
| 602 | */ |
| 603 | if (IS_ENABLED(CONFIG_SND_CTL_VALIDATION) && sbe->max > 512) |
| 604 | k->access |= SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK; |
| 605 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 606 | ext_ops = tplg->bytes_ext_ops; |
| 607 | num_ops = tplg->bytes_ext_ops_count; |
| 608 | for (i = 0; i < num_ops; i++) { |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 609 | if (!sbe->put && |
| 610 | ext_ops[i].id == le32_to_cpu(be->ext_ops.put)) |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 611 | sbe->put = ext_ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 612 | if (!sbe->get && |
| 613 | ext_ops[i].id == le32_to_cpu(be->ext_ops.get)) |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 614 | sbe->get = ext_ops[i].get; |
| 615 | } |
| 616 | |
Dharageswari R | 819b9f6 | 2020-09-08 12:28:24 +0300 | [diff] [blame] | 617 | if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) && !sbe->get) |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 618 | return -EINVAL; |
Dharageswari R | 819b9f6 | 2020-09-08 12:28:24 +0300 | [diff] [blame] | 619 | if ((k->access & SNDRV_CTL_ELEM_ACCESS_TLV_WRITE) && !sbe->put) |
| 620 | return -EINVAL; |
| 621 | return 0; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 622 | } |
| 623 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 624 | /* try and map vendor specific kcontrol handlers first */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 625 | ops = tplg->io_ops; |
| 626 | num_ops = tplg->io_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 627 | for (i = 0; i < num_ops; i++) { |
| 628 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 629 | if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 630 | k->put = ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 631 | if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 632 | k->get = ops[i].get; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 633 | if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 634 | k->info = ops[i].info; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 637 | /* vendor specific handlers found ? */ |
| 638 | if (k->put && k->get && k->info) |
| 639 | return 0; |
| 640 | |
| 641 | /* none found so try standard kcontrol handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 642 | ops = io_ops; |
| 643 | num_ops = ARRAY_SIZE(io_ops); |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 644 | for (i = 0; i < num_ops; i++) { |
| 645 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 646 | if (k->put == NULL && ops[i].id == le32_to_cpu(hdr->ops.put)) |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 647 | k->put = ops[i].put; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 648 | if (k->get == NULL && ops[i].id == le32_to_cpu(hdr->ops.get)) |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 649 | k->get = ops[i].get; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 650 | if (k->info == NULL && ops[i].id == le32_to_cpu(hdr->ops.info)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 651 | k->info = ops[i].info; |
| 652 | } |
| 653 | |
| 654 | /* standard handlers found ? */ |
| 655 | if (k->put && k->get && k->info) |
| 656 | return 0; |
| 657 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 658 | /* nothing to bind */ |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | /* bind a widgets to it's evnt handlers */ |
| 663 | int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, |
| 664 | const struct snd_soc_tplg_widget_events *events, |
| 665 | int num_events, u16 event_type) |
| 666 | { |
| 667 | int i; |
| 668 | |
| 669 | w->event = NULL; |
| 670 | |
| 671 | for (i = 0; i < num_events; i++) { |
| 672 | if (event_type == events[i].type) { |
| 673 | |
| 674 | /* found - so assign event */ |
| 675 | w->event = events[i].event_handler; |
| 676 | return 0; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /* not found */ |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); |
| 684 | |
| 685 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 686 | static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, |
| 687 | struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) |
| 688 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 689 | if (tplg->ops && tplg->ops->control_load) |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 690 | return tplg->ops->control_load(tplg->comp, tplg->index, k, |
| 691 | hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 696 | |
| 697 | static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, |
| 698 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 699 | { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 700 | unsigned int item_len = 2 * sizeof(unsigned int); |
| 701 | unsigned int *p; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 702 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 703 | p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); |
| 704 | if (!p) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 705 | return -ENOMEM; |
| 706 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 707 | p[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 708 | p[1] = item_len; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 709 | p[2] = le32_to_cpu(scale->min); |
| 710 | p[3] = (le32_to_cpu(scale->step) & TLV_DB_SCALE_MASK) |
| 711 | | (le32_to_cpu(scale->mute) ? TLV_DB_SCALE_MUTE : 0); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 712 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 713 | kc->tlv.p = (void *)p; |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | static int soc_tplg_create_tlv(struct soc_tplg *tplg, |
| 718 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) |
| 719 | { |
| 720 | struct snd_soc_tplg_ctl_tlv *tplg_tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 721 | u32 access = le32_to_cpu(tc->access); |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 722 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 723 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 724 | return 0; |
| 725 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 726 | if (!(access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 727 | tplg_tlv = &tc->tlv; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 728 | switch (le32_to_cpu(tplg_tlv->type)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 729 | case SNDRV_CTL_TLVT_DB_SCALE: |
| 730 | return soc_tplg_create_tlv_db_scale(tplg, kc, |
| 731 | &tplg_tlv->scale); |
| 732 | |
| 733 | /* TODO: add support for other TLV types */ |
| 734 | default: |
| 735 | dev_dbg(tplg->dev, "Unsupported TLV type %d\n", |
| 736 | tplg_tlv->type); |
| 737 | return -EINVAL; |
| 738 | } |
| 739 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, |
| 745 | struct snd_kcontrol_new *kc) |
| 746 | { |
| 747 | kfree(kc->tlv.p); |
| 748 | } |
| 749 | |
| 750 | static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, |
| 751 | size_t size) |
| 752 | { |
| 753 | struct snd_soc_tplg_bytes_control *be; |
| 754 | struct soc_bytes_ext *sbe; |
| 755 | struct snd_kcontrol_new kc; |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 756 | int i; |
| 757 | int err = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 758 | |
| 759 | if (soc_tplg_check_elem_count(tplg, |
| 760 | sizeof(struct snd_soc_tplg_bytes_control), count, |
| 761 | size, "mixer bytes")) { |
| 762 | dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", |
| 763 | count); |
| 764 | return -EINVAL; |
| 765 | } |
| 766 | |
| 767 | for (i = 0; i < count; i++) { |
| 768 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 769 | |
| 770 | /* validate kcontrol */ |
| 771 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 772 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 776 | if (sbe == NULL) |
| 777 | return -ENOMEM; |
| 778 | |
| 779 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 780 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 781 | |
| 782 | dev_dbg(tplg->dev, |
| 783 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 784 | be->hdr.name, be->hdr.access); |
| 785 | |
| 786 | memset(&kc, 0, sizeof(kc)); |
| 787 | kc.name = be->hdr.name; |
| 788 | kc.private_value = (long)sbe; |
| 789 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 790 | kc.access = le32_to_cpu(be->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 791 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 792 | sbe->max = le32_to_cpu(be->max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 793 | sbe->dobj.type = SND_SOC_DOBJ_BYTES; |
| 794 | sbe->dobj.ops = tplg->ops; |
| 795 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 796 | |
| 797 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 798 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 799 | if (err) { |
| 800 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 801 | kfree(sbe); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 802 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* pass control to driver for optional further init */ |
| 806 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 807 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 808 | if (err < 0) { |
| 809 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 810 | be->hdr.name); |
| 811 | kfree(sbe); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 812 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* register control here */ |
| 816 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 817 | &sbe->dobj.control.kcontrol); |
| 818 | if (err < 0) { |
| 819 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 820 | be->hdr.name); |
| 821 | kfree(sbe); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 822 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | list_add(&sbe->dobj.list, &tplg->comp->dobj_list); |
| 826 | } |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 827 | return err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 828 | |
| 829 | } |
| 830 | |
| 831 | static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, |
| 832 | size_t size) |
| 833 | { |
| 834 | struct snd_soc_tplg_mixer_control *mc; |
| 835 | struct soc_mixer_control *sm; |
| 836 | struct snd_kcontrol_new kc; |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 837 | int i; |
| 838 | int err = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 839 | |
| 840 | if (soc_tplg_check_elem_count(tplg, |
| 841 | sizeof(struct snd_soc_tplg_mixer_control), |
| 842 | count, size, "mixers")) { |
| 843 | |
| 844 | dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", |
| 845 | count); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | for (i = 0; i < count; i++) { |
| 850 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 851 | |
| 852 | /* validate kcontrol */ |
| 853 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 854 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 855 | return -EINVAL; |
| 856 | |
| 857 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 858 | if (sm == NULL) |
| 859 | return -ENOMEM; |
| 860 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 861 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 862 | |
| 863 | dev_dbg(tplg->dev, |
| 864 | "ASoC: adding mixer kcontrol %s with access 0x%x\n", |
| 865 | mc->hdr.name, mc->hdr.access); |
| 866 | |
| 867 | memset(&kc, 0, sizeof(kc)); |
| 868 | kc.name = mc->hdr.name; |
| 869 | kc.private_value = (long)sm; |
| 870 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 871 | kc.access = le32_to_cpu(mc->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 872 | |
| 873 | /* we only support FL/FR channel mapping atm */ |
| 874 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 875 | SNDRV_CHMAP_FL); |
| 876 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 877 | SNDRV_CHMAP_FR); |
| 878 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 879 | SNDRV_CHMAP_FL); |
| 880 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 881 | SNDRV_CHMAP_FR); |
| 882 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 883 | sm->max = le32_to_cpu(mc->max); |
| 884 | sm->min = le32_to_cpu(mc->min); |
| 885 | sm->invert = le32_to_cpu(mc->invert); |
| 886 | sm->platform_max = le32_to_cpu(mc->platform_max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 887 | sm->dobj.index = tplg->index; |
| 888 | sm->dobj.ops = tplg->ops; |
| 889 | sm->dobj.type = SND_SOC_DOBJ_MIXER; |
| 890 | INIT_LIST_HEAD(&sm->dobj.list); |
| 891 | |
| 892 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 893 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 894 | if (err) { |
| 895 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 896 | kfree(sm); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 897 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 900 | /* create any TLV data */ |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 901 | err = soc_tplg_create_tlv(tplg, &kc, &mc->hdr); |
| 902 | if (err < 0) { |
| 903 | dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", |
| 904 | mc->hdr.name); |
| 905 | kfree(sm); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 906 | break; |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 907 | } |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 908 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 909 | /* pass control to driver for optional further init */ |
| 910 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 911 | (struct snd_soc_tplg_ctl_hdr *) mc); |
| 912 | if (err < 0) { |
| 913 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 914 | mc->hdr.name); |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 915 | soc_tplg_free_tlv(tplg, &kc); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 916 | kfree(sm); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 917 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 920 | /* register control here */ |
| 921 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 922 | &sm->dobj.control.kcontrol); |
| 923 | if (err < 0) { |
| 924 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 925 | mc->hdr.name); |
| 926 | soc_tplg_free_tlv(tplg, &kc); |
| 927 | kfree(sm); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 928 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | list_add(&sm->dobj.list, &tplg->comp->dobj_list); |
| 932 | } |
| 933 | |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 934 | return err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int soc_tplg_denum_create_texts(struct soc_enum *se, |
| 938 | struct snd_soc_tplg_enum_control *ec) |
| 939 | { |
| 940 | int i, ret; |
| 941 | |
| 942 | se->dobj.control.dtexts = |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 943 | kcalloc(le32_to_cpu(ec->items), sizeof(char *), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 944 | if (se->dobj.control.dtexts == NULL) |
| 945 | return -ENOMEM; |
| 946 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 947 | for (i = 0; i < le32_to_cpu(ec->items); i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 948 | |
| 949 | if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 950 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 951 | ret = -EINVAL; |
| 952 | goto err; |
| 953 | } |
| 954 | |
| 955 | se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); |
| 956 | if (!se->dobj.control.dtexts[i]) { |
| 957 | ret = -ENOMEM; |
| 958 | goto err; |
| 959 | } |
| 960 | } |
| 961 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 962 | se->items = le32_to_cpu(ec->items); |
Mousumi Jana | b6e38b2 | 2017-04-11 13:06:22 +0530 | [diff] [blame] | 963 | se->texts = (const char * const *)se->dobj.control.dtexts; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 964 | return 0; |
| 965 | |
| 966 | err: |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 967 | se->items = i; |
| 968 | soc_tplg_denum_remove_texts(se); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | static inline void soc_tplg_denum_remove_texts(struct soc_enum *se) |
| 973 | { |
| 974 | int i = se->items; |
| 975 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 976 | for (--i; i >= 0; i--) |
| 977 | kfree(se->dobj.control.dtexts[i]); |
| 978 | kfree(se->dobj.control.dtexts); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static int soc_tplg_denum_create_values(struct soc_enum *se, |
| 982 | struct snd_soc_tplg_enum_control *ec) |
| 983 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 984 | int i; |
| 985 | |
| 986 | if (le32_to_cpu(ec->items) > sizeof(*ec->values)) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 987 | return -EINVAL; |
| 988 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 989 | se->dobj.control.dvalues = kzalloc(le32_to_cpu(ec->items) * |
| 990 | sizeof(u32), |
Andrzej Hajda | 376c0af | 2015-08-07 09:59:37 +0200 | [diff] [blame] | 991 | GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 992 | if (!se->dobj.control.dvalues) |
| 993 | return -ENOMEM; |
| 994 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 995 | /* convert from little-endian */ |
| 996 | for (i = 0; i < le32_to_cpu(ec->items); i++) { |
| 997 | se->dobj.control.dvalues[i] = le32_to_cpu(ec->values[i]); |
| 998 | } |
| 999 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 1003 | static inline void soc_tplg_denum_remove_values(struct soc_enum *se) |
| 1004 | { |
| 1005 | kfree(se->dobj.control.dvalues); |
| 1006 | } |
| 1007 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1008 | static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, |
| 1009 | size_t size) |
| 1010 | { |
| 1011 | struct snd_soc_tplg_enum_control *ec; |
| 1012 | struct soc_enum *se; |
| 1013 | struct snd_kcontrol_new kc; |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1014 | int i; |
| 1015 | int err = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1016 | |
| 1017 | if (soc_tplg_check_elem_count(tplg, |
| 1018 | sizeof(struct snd_soc_tplg_enum_control), |
| 1019 | count, size, "enums")) { |
| 1020 | |
| 1021 | dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", |
| 1022 | count); |
| 1023 | return -EINVAL; |
| 1024 | } |
| 1025 | |
| 1026 | for (i = 0; i < count; i++) { |
| 1027 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1028 | |
| 1029 | /* validate kcontrol */ |
| 1030 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1031 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1032 | return -EINVAL; |
| 1033 | |
| 1034 | se = kzalloc((sizeof(*se)), GFP_KERNEL); |
| 1035 | if (se == NULL) |
| 1036 | return -ENOMEM; |
| 1037 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1038 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1039 | le32_to_cpu(ec->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1040 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1041 | dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", |
| 1042 | ec->hdr.name, ec->items); |
| 1043 | |
| 1044 | memset(&kc, 0, sizeof(kc)); |
| 1045 | kc.name = ec->hdr.name; |
| 1046 | kc.private_value = (long)se; |
| 1047 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1048 | kc.access = le32_to_cpu(ec->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1049 | |
| 1050 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1051 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1052 | SNDRV_CHMAP_FL); |
| 1053 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1054 | SNDRV_CHMAP_FL); |
| 1055 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1056 | se->mask = le32_to_cpu(ec->mask); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1057 | se->dobj.index = tplg->index; |
| 1058 | se->dobj.type = SND_SOC_DOBJ_ENUM; |
| 1059 | se->dobj.ops = tplg->ops; |
| 1060 | INIT_LIST_HEAD(&se->dobj.list); |
| 1061 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1062 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1063 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1064 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1065 | err = soc_tplg_denum_create_values(se, ec); |
| 1066 | if (err < 0) { |
| 1067 | dev_err(tplg->dev, |
| 1068 | "ASoC: could not create values for %s\n", |
| 1069 | ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1070 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1071 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1072 | fallthrough; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1073 | case SND_SOC_TPLG_CTL_ENUM: |
| 1074 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1075 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1076 | err = soc_tplg_denum_create_texts(se, ec); |
| 1077 | if (err < 0) { |
| 1078 | dev_err(tplg->dev, |
| 1079 | "ASoC: could not create texts for %s\n", |
| 1080 | ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1081 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1082 | } |
| 1083 | break; |
| 1084 | default: |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1085 | err = -EINVAL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1086 | dev_err(tplg->dev, |
| 1087 | "ASoC: invalid enum control type %d for %s\n", |
| 1088 | ec->hdr.ops.info, ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1089 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1093 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1094 | if (err) { |
| 1095 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1096 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* pass control to driver for optional further init */ |
| 1100 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 1101 | (struct snd_soc_tplg_ctl_hdr *) ec); |
| 1102 | if (err < 0) { |
| 1103 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1104 | ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1105 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | /* register control here */ |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1109 | err = soc_tplg_add_kcontrol(tplg, |
| 1110 | &kc, &se->dobj.control.kcontrol); |
| 1111 | if (err < 0) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1112 | dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", |
| 1113 | ec->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1114 | goto err_denum; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | list_add(&se->dobj.list, &tplg->comp->dobj_list); |
| 1118 | } |
Pierre-Louis Bossart | 952bd93 | 2020-07-07 15:37:48 -0500 | [diff] [blame] | 1119 | return 0; |
| 1120 | |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1121 | err_denum: |
Pierre-Louis Bossart | 952bd93 | 2020-07-07 15:37:48 -0500 | [diff] [blame] | 1122 | kfree(se); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1123 | return err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, |
| 1127 | struct snd_soc_tplg_hdr *hdr) |
| 1128 | { |
| 1129 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1130 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1131 | int i; |
| 1132 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1133 | dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, |
| 1134 | soc_tplg_get_offset(tplg)); |
| 1135 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1136 | for (i = 0; i < le32_to_cpu(hdr->count); i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1137 | |
| 1138 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1139 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1140 | if (le32_to_cpu(control_hdr->size) != sizeof(*control_hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1141 | dev_err(tplg->dev, "ASoC: invalid control size\n"); |
| 1142 | return -EINVAL; |
| 1143 | } |
| 1144 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1145 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1146 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1147 | case SND_SOC_TPLG_CTL_STROBE: |
| 1148 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1149 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1150 | case SND_SOC_TPLG_CTL_RANGE: |
| 1151 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
| 1152 | case SND_SOC_TPLG_DAPM_CTL_PIN: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1153 | ret = soc_tplg_dmixer_create(tplg, 1, |
| 1154 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1155 | break; |
| 1156 | case SND_SOC_TPLG_CTL_ENUM: |
| 1157 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1158 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1159 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1160 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1161 | ret = soc_tplg_denum_create(tplg, 1, |
| 1162 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1163 | break; |
| 1164 | case SND_SOC_TPLG_CTL_BYTES: |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1165 | ret = soc_tplg_dbytes_create(tplg, 1, |
| 1166 | le32_to_cpu(hdr->payload_size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1167 | break; |
| 1168 | default: |
| 1169 | soc_bind_err(tplg, control_hdr, i); |
| 1170 | return -EINVAL; |
| 1171 | } |
Amadeusz Sławiński | 2ae548f | 2020-03-27 16:47:26 -0400 | [diff] [blame] | 1172 | if (ret < 0) { |
| 1173 | dev_err(tplg->dev, "ASoC: invalid control\n"); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1182 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 1183 | static int soc_tplg_add_route(struct soc_tplg *tplg, |
| 1184 | struct snd_soc_dapm_route *route) |
| 1185 | { |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 1186 | if (tplg->ops && tplg->ops->dapm_route_load) |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1187 | return tplg->ops->dapm_route_load(tplg->comp, tplg->index, |
| 1188 | route); |
| 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1193 | static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, |
| 1194 | struct snd_soc_tplg_hdr *hdr) |
| 1195 | { |
| 1196 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1197 | struct snd_soc_tplg_dapm_graph_elem *elem; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1198 | struct snd_soc_dapm_route **routes; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1199 | int count, i, j; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1200 | int ret = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1201 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1202 | count = le32_to_cpu(hdr->count); |
| 1203 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1204 | if (soc_tplg_check_elem_count(tplg, |
| 1205 | sizeof(struct snd_soc_tplg_dapm_graph_elem), |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1206 | count, le32_to_cpu(hdr->payload_size), "graph")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1207 | |
| 1208 | dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", |
| 1209 | count); |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
Liam Girdwood | b75a651 | 2017-06-29 14:22:26 +0100 | [diff] [blame] | 1213 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count, |
| 1214 | hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1215 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1216 | /* allocate memory for pointer to array of dapm routes */ |
| 1217 | routes = kcalloc(count, sizeof(struct snd_soc_dapm_route *), |
| 1218 | GFP_KERNEL); |
| 1219 | if (!routes) |
| 1220 | return -ENOMEM; |
| 1221 | |
| 1222 | /* |
| 1223 | * allocate memory for each dapm route in the array. |
| 1224 | * This needs to be done individually so that |
| 1225 | * each route can be freed when it is removed in remove_route(). |
| 1226 | */ |
| 1227 | for (i = 0; i < count; i++) { |
| 1228 | routes[i] = kzalloc(sizeof(*routes[i]), GFP_KERNEL); |
| 1229 | if (!routes[i]) { |
| 1230 | /* free previously allocated memory */ |
| 1231 | for (j = 0; j < i; j++) |
| 1232 | kfree(routes[j]); |
| 1233 | |
| 1234 | kfree(routes); |
| 1235 | return -ENOMEM; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1239 | for (i = 0; i < count; i++) { |
| 1240 | elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; |
| 1241 | tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); |
| 1242 | |
| 1243 | /* validate routes */ |
| 1244 | if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1245 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1246 | ret = -EINVAL; |
| 1247 | break; |
| 1248 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1249 | if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1250 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1251 | ret = -EINVAL; |
| 1252 | break; |
| 1253 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1254 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1255 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 1256 | ret = -EINVAL; |
| 1257 | break; |
| 1258 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1259 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1260 | routes[i]->source = elem->source; |
| 1261 | routes[i]->sink = elem->sink; |
| 1262 | |
| 1263 | /* set to NULL atm for tplg users */ |
| 1264 | routes[i]->connected = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1265 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1266 | routes[i]->control = NULL; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1267 | else |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1268 | routes[i]->control = elem->control; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1269 | |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1270 | /* add route dobj to dobj_list */ |
| 1271 | routes[i]->dobj.type = SND_SOC_DOBJ_GRAPH; |
| 1272 | routes[i]->dobj.ops = tplg->ops; |
| 1273 | routes[i]->dobj.index = tplg->index; |
| 1274 | list_add(&routes[i]->dobj.list, &tplg->comp->dobj_list); |
| 1275 | |
Amadeusz Sławiński | 6856e88 | 2020-03-27 16:47:27 -0400 | [diff] [blame] | 1276 | ret = soc_tplg_add_route(tplg, routes[i]); |
Pierre-Louis Bossart | 6f0307d | 2020-07-07 15:37:45 -0500 | [diff] [blame] | 1277 | if (ret < 0) { |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 1278 | dev_err(tplg->dev, "ASoC: topology: add_route failed: %d\n", ret); |
Pierre-Louis Bossart | 6f0307d | 2020-07-07 15:37:45 -0500 | [diff] [blame] | 1279 | /* |
| 1280 | * this route was added to the list, it will |
| 1281 | * be freed in remove_route() so increment the |
| 1282 | * counter to skip it in the error handling |
| 1283 | * below. |
| 1284 | */ |
| 1285 | i++; |
Amadeusz Sławiński | 6856e88 | 2020-03-27 16:47:27 -0400 | [diff] [blame] | 1286 | break; |
Pierre-Louis Bossart | 6f0307d | 2020-07-07 15:37:45 -0500 | [diff] [blame] | 1287 | } |
Liam Girdwood | 503e79b | 2018-06-14 20:53:59 +0100 | [diff] [blame] | 1288 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1289 | /* add route, but keep going if some fail */ |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1290 | snd_soc_dapm_add_routes(dapm, routes[i], 1); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
Pierre-Louis Bossart | 6f0307d | 2020-07-07 15:37:45 -0500 | [diff] [blame] | 1293 | /* |
| 1294 | * free memory allocated for all dapm routes not added to the |
| 1295 | * list in case of error |
| 1296 | */ |
| 1297 | if (ret < 0) { |
| 1298 | while (i < count) |
| 1299 | kfree(routes[i++]); |
| 1300 | } |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | * free pointer to array of dapm routes as this is no longer needed. |
| 1304 | * The memory allocated for each dapm route will be freed |
| 1305 | * when it is removed in remove_route(). |
| 1306 | */ |
| 1307 | kfree(routes); |
| 1308 | |
| 1309 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( |
| 1313 | struct soc_tplg *tplg, int num_kcontrols) |
| 1314 | { |
| 1315 | struct snd_kcontrol_new *kc; |
| 1316 | struct soc_mixer_control *sm; |
| 1317 | struct snd_soc_tplg_mixer_control *mc; |
| 1318 | int i, err; |
| 1319 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1320 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1321 | if (kc == NULL) |
| 1322 | return NULL; |
| 1323 | |
| 1324 | for (i = 0; i < num_kcontrols; i++) { |
| 1325 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1326 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1327 | /* validate kcontrol */ |
| 1328 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1329 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1330 | goto err_sm; |
| 1331 | |
| 1332 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 1333 | if (sm == NULL) |
| 1334 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1335 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1336 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1337 | le32_to_cpu(mc->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1338 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1339 | dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", |
| 1340 | mc->hdr.name, i); |
| 1341 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1342 | kc[i].private_value = (long)sm; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1343 | kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL); |
| 1344 | if (kc[i].name == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1345 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1346 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1347 | kc[i].access = le32_to_cpu(mc->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1348 | |
| 1349 | /* we only support FL/FR channel mapping atm */ |
| 1350 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 1351 | SNDRV_CHMAP_FL); |
| 1352 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 1353 | SNDRV_CHMAP_FR); |
| 1354 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 1355 | SNDRV_CHMAP_FL); |
| 1356 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 1357 | SNDRV_CHMAP_FR); |
| 1358 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1359 | sm->max = le32_to_cpu(mc->max); |
| 1360 | sm->min = le32_to_cpu(mc->min); |
| 1361 | sm->invert = le32_to_cpu(mc->invert); |
| 1362 | sm->platform_max = le32_to_cpu(mc->platform_max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1363 | sm->dobj.index = tplg->index; |
| 1364 | INIT_LIST_HEAD(&sm->dobj.list); |
| 1365 | |
| 1366 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1367 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1368 | if (err) { |
| 1369 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1370 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1371 | } |
| 1372 | |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1373 | /* create any TLV data */ |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 1374 | err = soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr); |
| 1375 | if (err < 0) { |
| 1376 | dev_err(tplg->dev, "ASoC: failed to create TLV %s\n", |
| 1377 | mc->hdr.name); |
Pierre-Louis Bossart | 129fc2b | 2020-07-07 15:37:47 -0500 | [diff] [blame] | 1378 | goto err_sm; |
Amadeusz Sławiński | 482db55 | 2020-03-27 16:47:25 -0400 | [diff] [blame] | 1379 | } |
Bard liao | 3789deb | 2019-03-13 21:49:43 +0800 | [diff] [blame] | 1380 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1381 | /* pass control to driver for optional further init */ |
| 1382 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1383 | (struct snd_soc_tplg_ctl_hdr *)mc); |
| 1384 | if (err < 0) { |
| 1385 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1386 | mc->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1387 | goto err_sm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | return kc; |
| 1391 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1392 | err_sm: |
| 1393 | for (; i >= 0; i--) { |
Pierre-Louis Bossart | 8edac48 | 2020-07-07 15:37:46 -0500 | [diff] [blame] | 1394 | soc_tplg_free_tlv(tplg, &kc[i]); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1395 | sm = (struct soc_mixer_control *)kc[i].private_value; |
| 1396 | kfree(sm); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1397 | kfree(kc[i].name); |
| 1398 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1399 | kfree(kc); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1400 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1401 | return NULL; |
| 1402 | } |
| 1403 | |
| 1404 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1405 | struct soc_tplg *tplg, int num_kcontrols) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1406 | { |
| 1407 | struct snd_kcontrol_new *kc; |
| 1408 | struct snd_soc_tplg_enum_control *ec; |
| 1409 | struct soc_enum *se; |
Amadeusz Sławiński | 3cde818 | 2019-06-17 13:36:43 +0200 | [diff] [blame] | 1410 | int i, err; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1411 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1412 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1413 | if (kc == NULL) |
| 1414 | return NULL; |
| 1415 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1416 | for (i = 0; i < num_kcontrols; i++) { |
| 1417 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
| 1418 | /* validate kcontrol */ |
| 1419 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1420 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1421 | goto err_se; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1422 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1423 | se = kzalloc(sizeof(*se), GFP_KERNEL); |
| 1424 | if (se == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1425 | goto err_se; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1426 | |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1427 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1428 | le32_to_cpu(ec->priv.size)); |
Liam Girdwood | 02b6424 | 2019-03-01 19:08:52 -0600 | [diff] [blame] | 1429 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1430 | dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1431 | ec->hdr.name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1432 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1433 | kc[i].private_value = (long)se; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1434 | kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1435 | if (kc[i].name == NULL) |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1436 | goto err_se; |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1437 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1438 | kc[i].access = le32_to_cpu(ec->hdr.access); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1439 | |
| 1440 | /* we only support FL/FR channel mapping atm */ |
| 1441 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1442 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 1443 | SNDRV_CHMAP_FL); |
| 1444 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 1445 | SNDRV_CHMAP_FR); |
| 1446 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1447 | se->items = le32_to_cpu(ec->items); |
| 1448 | se->mask = le32_to_cpu(ec->mask); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1449 | se->dobj.index = tplg->index; |
| 1450 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1451 | switch (le32_to_cpu(ec->hdr.ops.info)) { |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1452 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1453 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1454 | err = soc_tplg_denum_create_values(se, ec); |
| 1455 | if (err < 0) { |
| 1456 | dev_err(tplg->dev, "ASoC: could not create values for %s\n", |
| 1457 | ec->hdr.name); |
| 1458 | goto err_se; |
| 1459 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1460 | fallthrough; |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1461 | case SND_SOC_TPLG_CTL_ENUM: |
| 1462 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1463 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1464 | err = soc_tplg_denum_create_texts(se, ec); |
| 1465 | if (err < 0) { |
| 1466 | dev_err(tplg->dev, "ASoC: could not create texts for %s\n", |
| 1467 | ec->hdr.name); |
| 1468 | goto err_se; |
| 1469 | } |
| 1470 | break; |
| 1471 | default: |
| 1472 | dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", |
| 1473 | ec->hdr.ops.info, ec->hdr.name); |
| 1474 | goto err_se; |
| 1475 | } |
| 1476 | |
| 1477 | /* map io handlers */ |
| 1478 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg); |
| 1479 | if (err) { |
| 1480 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1481 | goto err_se; |
| 1482 | } |
| 1483 | |
| 1484 | /* pass control to driver for optional further init */ |
| 1485 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1486 | (struct snd_soc_tplg_ctl_hdr *)ec); |
| 1487 | if (err < 0) { |
| 1488 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1489 | ec->hdr.name); |
| 1490 | goto err_se; |
| 1491 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | return kc; |
| 1495 | |
| 1496 | err_se: |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1497 | for (; i >= 0; i--) { |
| 1498 | /* free values and texts */ |
| 1499 | se = (struct soc_enum *)kc[i].private_value; |
Christophe JAILLET | 6d5574e | 2017-09-14 22:44:12 +0200 | [diff] [blame] | 1500 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1501 | if (se) { |
| 1502 | soc_tplg_denum_remove_values(se); |
| 1503 | soc_tplg_denum_remove_texts(se); |
| 1504 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1505 | |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1506 | kfree(se); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1507 | kfree(kc[i].name); |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1508 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1509 | kfree(kc); |
| 1510 | |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
| 1514 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1515 | struct soc_tplg *tplg, int num_kcontrols) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1516 | { |
| 1517 | struct snd_soc_tplg_bytes_control *be; |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1518 | struct soc_bytes_ext *sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1519 | struct snd_kcontrol_new *kc; |
| 1520 | int i, err; |
| 1521 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1522 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1523 | if (!kc) |
| 1524 | return NULL; |
| 1525 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1526 | for (i = 0; i < num_kcontrols; i++) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1527 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 1528 | |
| 1529 | /* validate kcontrol */ |
| 1530 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1531 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1532 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1533 | |
| 1534 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 1535 | if (sbe == NULL) |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1536 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1537 | |
| 1538 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1539 | le32_to_cpu(be->priv.size)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1540 | |
| 1541 | dev_dbg(tplg->dev, |
| 1542 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 1543 | be->hdr.name, be->hdr.access); |
| 1544 | |
Colin Ian King | 1ad741d | 2019-06-27 14:32:08 +0100 | [diff] [blame] | 1545 | kc[i].private_value = (long)sbe; |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1546 | kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1547 | if (kc[i].name == NULL) |
| 1548 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1549 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1550 | kc[i].access = le32_to_cpu(be->hdr.access); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1551 | |
Pierre-Louis Bossart | 72bbeda | 2020-01-02 13:59:52 -0600 | [diff] [blame] | 1552 | sbe->max = le32_to_cpu(be->max); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1553 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 1554 | |
| 1555 | /* map standard io handlers and check for external handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1556 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1557 | if (err) { |
| 1558 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1559 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | /* pass control to driver for optional further init */ |
| 1563 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1564 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 1565 | if (err < 0) { |
| 1566 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1567 | be->hdr.name); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1568 | goto err_sbe; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | return kc; |
| 1573 | |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1574 | err_sbe: |
| 1575 | for (; i >= 0; i--) { |
| 1576 | sbe = (struct soc_bytes_ext *)kc[i].private_value; |
| 1577 | kfree(sbe); |
Liam Girdwood | 267e2c6 | 2018-03-27 12:04:04 +0100 | [diff] [blame] | 1578 | kfree(kc[i].name); |
| 1579 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1580 | kfree(kc); |
Amadeusz Sławiński | 9f90af3 | 2019-06-17 13:36:44 +0200 | [diff] [blame] | 1581 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1582 | return NULL; |
| 1583 | } |
| 1584 | |
| 1585 | static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, |
| 1586 | struct snd_soc_tplg_dapm_widget *w) |
| 1587 | { |
| 1588 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
| 1589 | struct snd_soc_dapm_widget template, *widget; |
| 1590 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1591 | struct snd_soc_card *card = tplg->comp->card; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1592 | unsigned int kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1593 | int ret = 0; |
| 1594 | |
| 1595 | if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1596 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1597 | return -EINVAL; |
| 1598 | if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1599 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1600 | return -EINVAL; |
| 1601 | |
| 1602 | dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", |
| 1603 | w->name, w->id); |
| 1604 | |
| 1605 | memset(&template, 0, sizeof(template)); |
| 1606 | |
| 1607 | /* map user to kernel widget ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1608 | template.id = get_widget_id(le32_to_cpu(w->id)); |
Dan Carpenter | 752c938 | 2019-09-25 14:06:24 +0300 | [diff] [blame] | 1609 | if ((int)template.id < 0) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1610 | return template.id; |
| 1611 | |
Liam Girdwood | c3421a6 | 2017-06-06 15:45:09 +0100 | [diff] [blame] | 1612 | /* strings are allocated here, but used and freed by the widget */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1613 | template.name = kstrdup(w->name, GFP_KERNEL); |
| 1614 | if (!template.name) |
| 1615 | return -ENOMEM; |
| 1616 | template.sname = kstrdup(w->sname, GFP_KERNEL); |
| 1617 | if (!template.sname) { |
| 1618 | ret = -ENOMEM; |
| 1619 | goto err; |
| 1620 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1621 | template.reg = le32_to_cpu(w->reg); |
| 1622 | template.shift = le32_to_cpu(w->shift); |
| 1623 | template.mask = le32_to_cpu(w->mask); |
| 1624 | template.subseq = le32_to_cpu(w->subseq); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1625 | template.on_val = w->invert ? 0 : 1; |
| 1626 | template.off_val = w->invert ? 1 : 0; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1627 | template.ignore_suspend = le32_to_cpu(w->ignore_suspend); |
| 1628 | template.event_flags = le16_to_cpu(w->event_flags); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1629 | template.dobj.index = tplg->index; |
| 1630 | |
| 1631 | tplg->pos += |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1632 | (sizeof(struct snd_soc_tplg_dapm_widget) + |
| 1633 | le32_to_cpu(w->priv.size)); |
| 1634 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1635 | if (w->num_kcontrols == 0) { |
Arnd Bergmann | dd5abb7 | 2016-12-09 12:51:46 +0100 | [diff] [blame] | 1636 | kcontrol_type = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1637 | template.num_kcontrols = 0; |
| 1638 | goto widget; |
| 1639 | } |
| 1640 | |
| 1641 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1642 | dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", |
| 1643 | w->name, w->num_kcontrols, control_hdr->type); |
| 1644 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1645 | switch (le32_to_cpu(control_hdr->ops.info)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1646 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1647 | case SND_SOC_TPLG_CTL_STROBE: |
| 1648 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1649 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1650 | case SND_SOC_TPLG_CTL_RANGE: |
| 1651 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1652 | kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1653 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1654 | template.kcontrol_news = |
| 1655 | soc_tplg_dapm_widget_dmixer_create(tplg, |
| 1656 | template.num_kcontrols); |
| 1657 | if (!template.kcontrol_news) { |
| 1658 | ret = -ENOMEM; |
| 1659 | goto hdr_err; |
| 1660 | } |
| 1661 | break; |
| 1662 | case SND_SOC_TPLG_CTL_ENUM: |
| 1663 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1664 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1665 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1666 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1667 | kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1668 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1669 | template.kcontrol_news = |
Mengdong Lin | 1a7dd6e | 2016-11-25 16:09:10 +0800 | [diff] [blame] | 1670 | soc_tplg_dapm_widget_denum_create(tplg, |
| 1671 | template.num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1672 | if (!template.kcontrol_news) { |
| 1673 | ret = -ENOMEM; |
| 1674 | goto hdr_err; |
| 1675 | } |
| 1676 | break; |
| 1677 | case SND_SOC_TPLG_CTL_BYTES: |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1678 | kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1679 | template.num_kcontrols = le32_to_cpu(w->num_kcontrols); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1680 | template.kcontrol_news = |
| 1681 | soc_tplg_dapm_widget_dbytes_create(tplg, |
| 1682 | template.num_kcontrols); |
| 1683 | if (!template.kcontrol_news) { |
| 1684 | ret = -ENOMEM; |
| 1685 | goto hdr_err; |
| 1686 | } |
| 1687 | break; |
| 1688 | default: |
| 1689 | dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", |
| 1690 | control_hdr->ops.get, control_hdr->ops.put, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1691 | le32_to_cpu(control_hdr->ops.info)); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1692 | ret = -EINVAL; |
| 1693 | goto hdr_err; |
| 1694 | } |
| 1695 | |
| 1696 | widget: |
| 1697 | ret = soc_tplg_widget_load(tplg, &template, w); |
| 1698 | if (ret < 0) |
| 1699 | goto hdr_err; |
| 1700 | |
| 1701 | /* card dapm mutex is held by the core if we are loading topology |
| 1702 | * data during sound card init. */ |
| 1703 | if (card->instantiated) |
| 1704 | widget = snd_soc_dapm_new_control(dapm, &template); |
| 1705 | else |
| 1706 | widget = snd_soc_dapm_new_control_unlocked(dapm, &template); |
Linus Walleij | 37e1df8 | 2017-01-13 10:23:52 +0100 | [diff] [blame] | 1707 | if (IS_ERR(widget)) { |
| 1708 | ret = PTR_ERR(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1709 | goto hdr_err; |
| 1710 | } |
| 1711 | |
| 1712 | widget->dobj.type = SND_SOC_DOBJ_WIDGET; |
Mengdong Lin | eea3dd4 | 2016-11-25 16:09:17 +0800 | [diff] [blame] | 1713 | widget->dobj.widget.kcontrol_type = kcontrol_type; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1714 | widget->dobj.ops = tplg->ops; |
| 1715 | widget->dobj.index = tplg->index; |
| 1716 | list_add(&widget->dobj.list, &tplg->comp->dobj_list); |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1717 | |
| 1718 | ret = soc_tplg_widget_ready(tplg, widget, w); |
| 1719 | if (ret < 0) |
| 1720 | goto ready_err; |
| 1721 | |
Bard liao | 7620fe9 | 2019-01-25 14:06:45 -0600 | [diff] [blame] | 1722 | kfree(template.sname); |
| 1723 | kfree(template.name); |
| 1724 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1725 | return 0; |
| 1726 | |
Liam Girdwood | ebd259d | 2017-06-09 15:43:23 +0100 | [diff] [blame] | 1727 | ready_err: |
| 1728 | snd_soc_tplg_widget_remove(widget); |
| 1729 | snd_soc_dapm_free_widget(widget); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1730 | hdr_err: |
| 1731 | kfree(template.sname); |
| 1732 | err: |
| 1733 | kfree(template.name); |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, |
| 1738 | struct snd_soc_tplg_hdr *hdr) |
| 1739 | { |
| 1740 | struct snd_soc_tplg_dapm_widget *widget; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1741 | int ret, count, i; |
| 1742 | |
| 1743 | count = le32_to_cpu(hdr->count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1744 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1745 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); |
| 1746 | |
| 1747 | for (i = 0; i < count; i++) { |
| 1748 | widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1749 | if (le32_to_cpu(widget->size) != sizeof(*widget)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 1750 | dev_err(tplg->dev, "ASoC: invalid widget size\n"); |
| 1751 | return -EINVAL; |
| 1752 | } |
| 1753 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1754 | ret = soc_tplg_dapm_widget_create(tplg, widget); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1755 | if (ret < 0) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1756 | dev_err(tplg->dev, "ASoC: failed to load widget %s\n", |
| 1757 | widget->name); |
Mengdong Lin | 7de76b6 | 2016-04-27 14:52:38 +0800 | [diff] [blame] | 1758 | return ret; |
| 1759 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | static int soc_tplg_dapm_complete(struct soc_tplg *tplg) |
| 1766 | { |
| 1767 | struct snd_soc_card *card = tplg->comp->card; |
| 1768 | int ret; |
| 1769 | |
| 1770 | /* Card might not have been registered at this point. |
| 1771 | * If so, just return success. |
| 1772 | */ |
| 1773 | if (!card || !card->instantiated) { |
| 1774 | dev_warn(tplg->dev, "ASoC: Parent card not yet available," |
Liam Girdwood | cc9d471 | 2017-06-06 15:45:08 +0100 | [diff] [blame] | 1775 | " widget card binding deferred\n"); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | ret = snd_soc_dapm_new_widgets(card); |
| 1780 | if (ret < 0) |
| 1781 | dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", |
| 1782 | ret); |
| 1783 | |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1787 | static int set_stream_info(struct snd_soc_pcm_stream *stream, |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1788 | struct snd_soc_tplg_stream_caps *caps) |
| 1789 | { |
| 1790 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1791 | if (!stream->stream_name) |
| 1792 | return -ENOMEM; |
| 1793 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1794 | stream->channels_min = le32_to_cpu(caps->channels_min); |
| 1795 | stream->channels_max = le32_to_cpu(caps->channels_max); |
| 1796 | stream->rates = le32_to_cpu(caps->rates); |
| 1797 | stream->rate_min = le32_to_cpu(caps->rate_min); |
| 1798 | stream->rate_max = le32_to_cpu(caps->rate_max); |
| 1799 | stream->formats = le64_to_cpu(caps->formats); |
| 1800 | stream->sig_bits = le32_to_cpu(caps->sig_bits); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1801 | |
| 1802 | return 0; |
Mengdong Lin | b6b6e4d | 2016-02-22 16:29:19 +0800 | [diff] [blame] | 1803 | } |
| 1804 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 1805 | static void set_dai_flags(struct snd_soc_dai_driver *dai_drv, |
| 1806 | unsigned int flag_mask, unsigned int flags) |
| 1807 | { |
| 1808 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES) |
| 1809 | dai_drv->symmetric_rates = |
| 1810 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1811 | |
| 1812 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS) |
| 1813 | dai_drv->symmetric_channels = |
| 1814 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1815 | 1 : 0; |
| 1816 | |
| 1817 | if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1818 | dai_drv->symmetric_samplebits = |
| 1819 | flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1820 | 1 : 0; |
| 1821 | } |
| 1822 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1823 | static int soc_tplg_dai_create(struct soc_tplg *tplg, |
| 1824 | struct snd_soc_tplg_pcm *pcm) |
| 1825 | { |
| 1826 | struct snd_soc_dai_driver *dai_drv; |
| 1827 | struct snd_soc_pcm_stream *stream; |
| 1828 | struct snd_soc_tplg_stream_caps *caps; |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1829 | struct snd_soc_dai *dai; |
| 1830 | struct snd_soc_dapm_context *dapm = |
| 1831 | snd_soc_component_get_dapm(tplg->comp); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1832 | int ret; |
| 1833 | |
| 1834 | dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); |
| 1835 | if (dai_drv == NULL) |
| 1836 | return -ENOMEM; |
| 1837 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1838 | if (strlen(pcm->dai_name)) { |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1839 | dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1840 | if (!dai_drv->name) { |
| 1841 | ret = -ENOMEM; |
| 1842 | goto err; |
| 1843 | } |
| 1844 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1845 | dai_drv->id = le32_to_cpu(pcm->dai_id); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1846 | |
| 1847 | if (pcm->playback) { |
| 1848 | stream = &dai_drv->playback; |
| 1849 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1850 | ret = set_stream_info(stream, caps); |
| 1851 | if (ret < 0) |
| 1852 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | if (pcm->capture) { |
| 1856 | stream = &dai_drv->capture; |
| 1857 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1858 | ret = set_stream_info(stream, caps); |
| 1859 | if (ret < 0) |
| 1860 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1861 | } |
| 1862 | |
Liam Girdwood | 5db6aab | 2018-03-27 14:30:45 +0100 | [diff] [blame] | 1863 | if (pcm->compress) |
| 1864 | dai_drv->compress_new = snd_soc_new_compress; |
| 1865 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1866 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1867 | ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1868 | if (ret < 0) { |
| 1869 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1870 | goto err; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | dai_drv->dobj.index = tplg->index; |
| 1874 | dai_drv->dobj.ops = tplg->ops; |
| 1875 | dai_drv->dobj.type = SND_SOC_DOBJ_PCM; |
| 1876 | list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); |
| 1877 | |
| 1878 | /* register the DAI to the component */ |
Pierre-Louis Bossart | 6ae4902 | 2020-06-12 15:59:38 -0500 | [diff] [blame] | 1879 | dai = devm_snd_soc_register_dai(tplg->comp->dev, tplg->comp, dai_drv, false); |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1880 | if (!dai) |
| 1881 | return -ENOMEM; |
| 1882 | |
| 1883 | /* Create the DAI widgets here */ |
| 1884 | ret = snd_soc_dapm_new_dai_widgets(dapm, dai); |
| 1885 | if (ret != 0) { |
| 1886 | dev_err(dai->dev, "Failed to create DAI widgets %d\n", ret); |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1887 | return ret; |
| 1888 | } |
| 1889 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1890 | return 0; |
| 1891 | |
| 1892 | err: |
| 1893 | kfree(dai_drv->playback.stream_name); |
| 1894 | kfree(dai_drv->capture.stream_name); |
| 1895 | kfree(dai_drv->name); |
| 1896 | kfree(dai_drv); |
| 1897 | |
Kuninori Morimoto | e443c20 | 2019-11-05 15:47:14 +0900 | [diff] [blame] | 1898 | return ret; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 1899 | } |
| 1900 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1901 | static void set_link_flags(struct snd_soc_dai_link *link, |
| 1902 | unsigned int flag_mask, unsigned int flags) |
| 1903 | { |
| 1904 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES) |
| 1905 | link->symmetric_rates = |
| 1906 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0; |
| 1907 | |
| 1908 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS) |
| 1909 | link->symmetric_channels = |
| 1910 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ? |
| 1911 | 1 : 0; |
| 1912 | |
| 1913 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS) |
| 1914 | link->symmetric_samplebits = |
| 1915 | flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ? |
| 1916 | 1 : 0; |
Mengdong Lin | 6ff67cc | 2016-11-03 01:05:32 +0800 | [diff] [blame] | 1917 | |
| 1918 | if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP) |
| 1919 | link->ignore_suspend = |
| 1920 | flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ? |
| 1921 | 1 : 0; |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1922 | } |
| 1923 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1924 | /* create the FE DAI link */ |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 1925 | static int soc_tplg_fe_link_create(struct soc_tplg *tplg, |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1926 | struct snd_soc_tplg_pcm *pcm) |
| 1927 | { |
| 1928 | struct snd_soc_dai_link *link; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1929 | struct snd_soc_dai_link_component *dlc; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1930 | int ret; |
| 1931 | |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1932 | /* link + cpu + codec + platform */ |
| 1933 | link = kzalloc(sizeof(*link) + (3 * sizeof(*dlc)), GFP_KERNEL); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1934 | if (link == NULL) |
| 1935 | return -ENOMEM; |
| 1936 | |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1937 | dlc = (struct snd_soc_dai_link_component *)(link + 1); |
| 1938 | |
| 1939 | link->cpus = &dlc[0]; |
| 1940 | link->codecs = &dlc[1]; |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1941 | link->platforms = &dlc[2]; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1942 | |
| 1943 | link->num_cpus = 1; |
| 1944 | link->num_codecs = 1; |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1945 | link->num_platforms = 1; |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1946 | |
Jaroslav Kysela | 8ce1cbd6 | 2020-01-22 20:07:52 +0100 | [diff] [blame] | 1947 | link->dobj.index = tplg->index; |
| 1948 | link->dobj.ops = tplg->ops; |
| 1949 | link->dobj.type = SND_SOC_DOBJ_DAI_LINK; |
| 1950 | |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1951 | if (strlen(pcm->pcm_name)) { |
| 1952 | link->name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
| 1953 | link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1954 | if (!link->name || !link->stream_name) { |
| 1955 | ret = -ENOMEM; |
| 1956 | goto err; |
| 1957 | } |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1958 | } |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1959 | link->id = le32_to_cpu(pcm->pcm_id); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1960 | |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1961 | if (strlen(pcm->dai_name)) { |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1962 | link->cpus->dai_name = kstrdup(pcm->dai_name, GFP_KERNEL); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 1963 | if (!link->cpus->dai_name) { |
| 1964 | ret = -ENOMEM; |
| 1965 | goto err; |
| 1966 | } |
| 1967 | } |
Mengdong Lin | 8f27c4a | 2016-11-03 01:02:59 +0800 | [diff] [blame] | 1968 | |
Kuninori Morimoto | 23b946c | 2019-06-06 13:19:14 +0900 | [diff] [blame] | 1969 | link->codecs->name = "snd-soc-dummy"; |
| 1970 | link->codecs->dai_name = "snd-soc-dummy-dai"; |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1971 | |
Pierre-Louis Bossart | 3e6de89 | 2019-06-12 11:38:45 -0500 | [diff] [blame] | 1972 | link->platforms->name = "snd-soc-dummy"; |
| 1973 | |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1974 | /* enable DPCM */ |
| 1975 | link->dynamic = 1; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1976 | link->dpcm_playback = le32_to_cpu(pcm->playback); |
| 1977 | link->dpcm_capture = le32_to_cpu(pcm->capture); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 1978 | if (pcm->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 1979 | set_link_flags(link, |
| 1980 | le32_to_cpu(pcm->flag_mask), |
| 1981 | le32_to_cpu(pcm->flags)); |
Guneshwor Singh | 67d1c21 | 2016-04-19 13:12:50 +0800 | [diff] [blame] | 1982 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1983 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 1984 | ret = soc_tplg_dai_link_load(tplg, link, NULL); |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1985 | if (ret < 0) { |
| 1986 | dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n"); |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1987 | goto err; |
| 1988 | } |
| 1989 | |
Mark Brown | 2acf6ce | 2019-12-10 13:27:14 +0000 | [diff] [blame] | 1990 | ret = snd_soc_add_pcm_runtime(tplg->comp->card, link); |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1991 | if (ret < 0) { |
| 1992 | dev_err(tplg->comp->dev, "ASoC: adding FE link failed\n"); |
| 1993 | goto err; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1996 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 1997 | |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 1998 | return 0; |
Dragos Tarcatu | 76d2703 | 2019-12-09 18:39:38 -0600 | [diff] [blame] | 1999 | err: |
| 2000 | kfree(link->name); |
| 2001 | kfree(link->stream_name); |
| 2002 | kfree(link->cpus->dai_name); |
| 2003 | kfree(link); |
| 2004 | return ret; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | /* create a FE DAI and DAI link from the PCM object */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2008 | static int soc_tplg_pcm_create(struct soc_tplg *tplg, |
| 2009 | struct snd_soc_tplg_pcm *pcm) |
| 2010 | { |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 2011 | int ret; |
| 2012 | |
| 2013 | ret = soc_tplg_dai_create(tplg, pcm); |
| 2014 | if (ret < 0) |
| 2015 | return ret; |
| 2016 | |
Mengdong Lin | ab4bc5e | 2016-11-03 01:04:42 +0800 | [diff] [blame] | 2017 | return soc_tplg_fe_link_create(tplg, pcm); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2018 | } |
| 2019 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2020 | /* copy stream caps from the old version 4 of source */ |
| 2021 | static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest, |
| 2022 | struct snd_soc_tplg_stream_caps_v4 *src) |
| 2023 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2024 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2025 | memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2026 | dest->formats = src->formats; |
| 2027 | dest->rates = src->rates; |
| 2028 | dest->rate_min = src->rate_min; |
| 2029 | dest->rate_max = src->rate_max; |
| 2030 | dest->channels_min = src->channels_min; |
| 2031 | dest->channels_max = src->channels_max; |
| 2032 | dest->periods_min = src->periods_min; |
| 2033 | dest->periods_max = src->periods_max; |
| 2034 | dest->period_size_min = src->period_size_min; |
| 2035 | dest->period_size_max = src->period_size_max; |
| 2036 | dest->buffer_size_min = src->buffer_size_min; |
| 2037 | dest->buffer_size_max = src->buffer_size_max; |
| 2038 | } |
| 2039 | |
| 2040 | /** |
| 2041 | * pcm_new_ver - Create the new version of PCM from the old version. |
| 2042 | * @tplg: topology context |
| 2043 | * @src: older version of pcm as a source |
| 2044 | * @pcm: latest version of pcm created from the source |
| 2045 | * |
| 2046 | * Support from vesion 4. User should free the returned pcm manually. |
| 2047 | */ |
| 2048 | static int pcm_new_ver(struct soc_tplg *tplg, |
| 2049 | struct snd_soc_tplg_pcm *src, |
| 2050 | struct snd_soc_tplg_pcm **pcm) |
| 2051 | { |
| 2052 | struct snd_soc_tplg_pcm *dest; |
| 2053 | struct snd_soc_tplg_pcm_v4 *src_v4; |
| 2054 | int i; |
| 2055 | |
| 2056 | *pcm = NULL; |
| 2057 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2058 | if (le32_to_cpu(src->size) != sizeof(*src_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2059 | dev_err(tplg->dev, "ASoC: invalid PCM size\n"); |
| 2060 | return -EINVAL; |
| 2061 | } |
| 2062 | |
| 2063 | dev_warn(tplg->dev, "ASoC: old version of PCM\n"); |
| 2064 | src_v4 = (struct snd_soc_tplg_pcm_v4 *)src; |
| 2065 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 2066 | if (!dest) |
| 2067 | return -ENOMEM; |
| 2068 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2069 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2070 | memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2071 | memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2072 | dest->pcm_id = src_v4->pcm_id; |
| 2073 | dest->dai_id = src_v4->dai_id; |
| 2074 | dest->playback = src_v4->playback; |
| 2075 | dest->capture = src_v4->capture; |
| 2076 | dest->compress = src_v4->compress; |
| 2077 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2078 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2079 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 2080 | sizeof(struct snd_soc_tplg_stream)); |
| 2081 | |
| 2082 | for (i = 0; i < 2; i++) |
| 2083 | stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]); |
| 2084 | |
| 2085 | *pcm = dest; |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2089 | static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2090 | struct snd_soc_tplg_hdr *hdr) |
| 2091 | { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2092 | struct snd_soc_tplg_pcm *pcm, *_pcm; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2093 | int count; |
| 2094 | int size; |
Vinod Koul | fd34045 | 2016-12-08 23:01:27 +0530 | [diff] [blame] | 2095 | int i; |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2096 | bool abi_match; |
Dragos Tarcatu | a3039ae | 2019-12-09 18:39:39 -0600 | [diff] [blame] | 2097 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2098 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2099 | count = le32_to_cpu(hdr->count); |
| 2100 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2101 | /* check the element size and count */ |
| 2102 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2103 | size = le32_to_cpu(pcm->size); |
| 2104 | if (size > sizeof(struct snd_soc_tplg_pcm) |
| 2105 | || size < sizeof(struct snd_soc_tplg_pcm_v4)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2106 | dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2107 | size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2108 | return -EINVAL; |
| 2109 | } |
| 2110 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2111 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2112 | size, count, |
| 2113 | le32_to_cpu(hdr->payload_size), |
| 2114 | "PCM DAI")) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2115 | dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", |
| 2116 | count); |
| 2117 | return -EINVAL; |
| 2118 | } |
| 2119 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2120 | for (i = 0; i < count; i++) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2121 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2122 | size = le32_to_cpu(pcm->size); |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2123 | |
| 2124 | /* check ABI version by size, create a new version of pcm |
| 2125 | * if abi not match. |
| 2126 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2127 | if (size == sizeof(*pcm)) { |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2128 | abi_match = true; |
| 2129 | _pcm = pcm; |
| 2130 | } else { |
| 2131 | abi_match = false; |
Amadeusz Sławiński | b3677fc3 | 2020-03-27 16:47:28 -0400 | [diff] [blame] | 2132 | ret = pcm_new_ver(tplg, pcm, &_pcm); |
| 2133 | if (ret < 0) |
| 2134 | return ret; |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2137 | /* create the FE DAIs and DAI links */ |
Dragos Tarcatu | a3039ae | 2019-12-09 18:39:39 -0600 | [diff] [blame] | 2138 | ret = soc_tplg_pcm_create(tplg, _pcm); |
| 2139 | if (ret < 0) { |
| 2140 | if (!abi_match) |
| 2141 | kfree(_pcm); |
| 2142 | return ret; |
| 2143 | } |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2144 | |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2145 | /* offset by version-specific struct size and |
| 2146 | * real priv data size |
| 2147 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2148 | tplg->pos += size + le32_to_cpu(_pcm->priv.size); |
Mengdong Lin | 717a8e7 | 2016-11-03 01:03:34 +0800 | [diff] [blame] | 2149 | |
Mengdong Lin | 55726dc | 2016-11-03 01:00:16 +0800 | [diff] [blame] | 2150 | if (!abi_match) |
| 2151 | kfree(_pcm); /* free the duplicated one */ |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2152 | } |
| 2153 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2154 | dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2155 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2156 | return 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2157 | } |
| 2158 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2159 | /** |
| 2160 | * set_link_hw_format - Set the HW audio format of the physical DAI link. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2161 | * @link: &snd_soc_dai_link which should be updated |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2162 | * @cfg: physical link configs. |
| 2163 | * |
| 2164 | * Topology context contains a list of supported HW formats (configs) and |
| 2165 | * a default format ID for the physical link. This function will use this |
| 2166 | * default ID to choose the HW format to set the link's DAI format for init. |
| 2167 | */ |
| 2168 | static void set_link_hw_format(struct snd_soc_dai_link *link, |
| 2169 | struct snd_soc_tplg_link_config *cfg) |
| 2170 | { |
| 2171 | struct snd_soc_tplg_hw_config *hw_config; |
| 2172 | unsigned char bclk_master, fsync_master; |
| 2173 | unsigned char invert_bclk, invert_fsync; |
| 2174 | int i; |
| 2175 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2176 | for (i = 0; i < le32_to_cpu(cfg->num_hw_configs); i++) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2177 | hw_config = &cfg->hw_config[i]; |
| 2178 | if (hw_config->id != cfg->default_hw_config_id) |
| 2179 | continue; |
| 2180 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2181 | link->dai_fmt = le32_to_cpu(hw_config->fmt) & |
| 2182 | SND_SOC_DAIFMT_FORMAT_MASK; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2183 | |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2184 | /* clock gating */ |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2185 | switch (hw_config->clock_gated) { |
| 2186 | case SND_SOC_TPLG_DAI_CLK_GATE_GATED: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2187 | link->dai_fmt |= SND_SOC_DAIFMT_GATED; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2188 | break; |
| 2189 | |
| 2190 | case SND_SOC_TPLG_DAI_CLK_GATE_CONT: |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2191 | link->dai_fmt |= SND_SOC_DAIFMT_CONT; |
Kirill Marinushkin | fbeabd0 | 2018-04-16 19:56:44 +0200 | [diff] [blame] | 2192 | break; |
| 2193 | |
| 2194 | default: |
| 2195 | /* ignore the value */ |
| 2196 | break; |
| 2197 | } |
Kirill Marinushkin | 933e1c4 | 2018-04-04 06:19:38 +0200 | [diff] [blame] | 2198 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2199 | /* clock signal polarity */ |
| 2200 | invert_bclk = hw_config->invert_bclk; |
| 2201 | invert_fsync = hw_config->invert_fsync; |
| 2202 | if (!invert_bclk && !invert_fsync) |
| 2203 | link->dai_fmt |= SND_SOC_DAIFMT_NB_NF; |
| 2204 | else if (!invert_bclk && invert_fsync) |
| 2205 | link->dai_fmt |= SND_SOC_DAIFMT_NB_IF; |
| 2206 | else if (invert_bclk && !invert_fsync) |
| 2207 | link->dai_fmt |= SND_SOC_DAIFMT_IB_NF; |
| 2208 | else |
| 2209 | link->dai_fmt |= SND_SOC_DAIFMT_IB_IF; |
| 2210 | |
| 2211 | /* clock masters */ |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2212 | bclk_master = (hw_config->bclk_master == |
| 2213 | SND_SOC_TPLG_BCLK_CM); |
| 2214 | fsync_master = (hw_config->fsync_master == |
| 2215 | SND_SOC_TPLG_FSYNC_CM); |
| 2216 | if (bclk_master && fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2217 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2218 | else if (!bclk_master && fsync_master) |
Kirill Marinushkin | a941e2f | 2018-04-04 06:19:37 +0200 | [diff] [blame] | 2219 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 2220 | else if (bclk_master && !fsync_master) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2221 | link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 2222 | else |
| 2223 | link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | /** |
| 2228 | * link_new_ver - Create a new physical link config from the old |
| 2229 | * version of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2230 | * @tplg: topology context |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2231 | * @src: old version of phyical link config as a source |
| 2232 | * @link: latest version of physical link config created from the source |
| 2233 | * |
| 2234 | * Support from vesion 4. User need free the returned link config manually. |
| 2235 | */ |
| 2236 | static int link_new_ver(struct soc_tplg *tplg, |
| 2237 | struct snd_soc_tplg_link_config *src, |
| 2238 | struct snd_soc_tplg_link_config **link) |
| 2239 | { |
| 2240 | struct snd_soc_tplg_link_config *dest; |
| 2241 | struct snd_soc_tplg_link_config_v4 *src_v4; |
| 2242 | int i; |
| 2243 | |
| 2244 | *link = NULL; |
| 2245 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2246 | if (le32_to_cpu(src->size) != |
| 2247 | sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2248 | dev_err(tplg->dev, "ASoC: invalid physical link config size\n"); |
| 2249 | return -EINVAL; |
| 2250 | } |
| 2251 | |
| 2252 | dev_warn(tplg->dev, "ASoC: old version of physical link config\n"); |
| 2253 | |
| 2254 | src_v4 = (struct snd_soc_tplg_link_config_v4 *)src; |
| 2255 | dest = kzalloc(sizeof(*dest), GFP_KERNEL); |
| 2256 | if (!dest) |
| 2257 | return -ENOMEM; |
| 2258 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2259 | dest->size = cpu_to_le32(sizeof(*dest)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2260 | dest->id = src_v4->id; |
| 2261 | dest->num_streams = src_v4->num_streams; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2262 | for (i = 0; i < le32_to_cpu(dest->num_streams); i++) |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2263 | memcpy(&dest->stream[i], &src_v4->stream[i], |
| 2264 | sizeof(struct snd_soc_tplg_stream)); |
| 2265 | |
| 2266 | *link = dest; |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
Kuninori Morimoto | d6f31e0 | 2019-12-10 09:34:14 +0900 | [diff] [blame] | 2270 | /** |
| 2271 | * snd_soc_find_dai_link - Find a DAI link |
| 2272 | * |
| 2273 | * @card: soc card |
| 2274 | * @id: DAI link ID to match |
| 2275 | * @name: DAI link name to match, optional |
| 2276 | * @stream_name: DAI link stream name to match, optional |
| 2277 | * |
| 2278 | * This function will search all existing DAI links of the soc card to |
| 2279 | * find the link of the same ID. Since DAI links may not have their |
| 2280 | * unique ID, so name and stream name should also match if being |
| 2281 | * specified. |
| 2282 | * |
| 2283 | * Return: pointer of DAI link, or NULL if not found. |
| 2284 | */ |
| 2285 | static struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, |
| 2286 | int id, const char *name, |
| 2287 | const char *stream_name) |
| 2288 | { |
| 2289 | struct snd_soc_pcm_runtime *rtd; |
| 2290 | struct snd_soc_dai_link *link; |
| 2291 | |
| 2292 | for_each_card_rtds(card, rtd) { |
| 2293 | link = rtd->dai_link; |
| 2294 | |
| 2295 | if (link->id != id) |
| 2296 | continue; |
| 2297 | |
| 2298 | if (name && (!link->name || strcmp(name, link->name))) |
| 2299 | continue; |
| 2300 | |
| 2301 | if (stream_name && (!link->stream_name |
| 2302 | || strcmp(stream_name, link->stream_name))) |
| 2303 | continue; |
| 2304 | |
| 2305 | return link; |
| 2306 | } |
| 2307 | |
| 2308 | return NULL; |
| 2309 | } |
| 2310 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2311 | /* Find and configure an existing physical DAI link */ |
| 2312 | static int soc_tplg_link_config(struct soc_tplg *tplg, |
| 2313 | struct snd_soc_tplg_link_config *cfg) |
| 2314 | { |
| 2315 | struct snd_soc_dai_link *link; |
| 2316 | const char *name, *stream_name; |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2317 | size_t len; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2318 | int ret; |
| 2319 | |
Mengdong Lin | dbab1cb | 2016-11-05 08:42:14 +0800 | [diff] [blame] | 2320 | len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2321 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2322 | return -EINVAL; |
| 2323 | else if (len) |
| 2324 | name = cfg->name; |
| 2325 | else |
| 2326 | name = NULL; |
| 2327 | |
| 2328 | len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 2329 | if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 2330 | return -EINVAL; |
| 2331 | else if (len) |
| 2332 | stream_name = cfg->stream_name; |
| 2333 | else |
| 2334 | stream_name = NULL; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2335 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2336 | link = snd_soc_find_dai_link(tplg->comp->card, le32_to_cpu(cfg->id), |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2337 | name, stream_name); |
| 2338 | if (!link) { |
| 2339 | dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n", |
| 2340 | name, cfg->id); |
| 2341 | return -EINVAL; |
| 2342 | } |
| 2343 | |
| 2344 | /* hw format */ |
| 2345 | if (cfg->num_hw_configs) |
| 2346 | set_link_hw_format(link, cfg); |
| 2347 | |
| 2348 | /* flags */ |
| 2349 | if (cfg->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2350 | set_link_flags(link, |
| 2351 | le32_to_cpu(cfg->flag_mask), |
| 2352 | le32_to_cpu(cfg->flags)); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2353 | |
| 2354 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2355 | ret = soc_tplg_dai_link_load(tplg, link, cfg); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2356 | if (ret < 0) { |
| 2357 | dev_err(tplg->dev, "ASoC: physical link loading failed\n"); |
| 2358 | return ret; |
| 2359 | } |
| 2360 | |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2361 | /* for unloading it in snd_soc_tplg_component_remove */ |
| 2362 | link->dobj.index = tplg->index; |
| 2363 | link->dobj.ops = tplg->ops; |
| 2364 | link->dobj.type = SND_SOC_DOBJ_BACKEND_LINK; |
| 2365 | list_add(&link->dobj.list, &tplg->comp->dobj_list); |
| 2366 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2367 | return 0; |
| 2368 | } |
| 2369 | |
| 2370 | |
| 2371 | /* Load physical link config elements from the topology context */ |
| 2372 | static int soc_tplg_link_elems_load(struct soc_tplg *tplg, |
| 2373 | struct snd_soc_tplg_hdr *hdr) |
| 2374 | { |
| 2375 | struct snd_soc_tplg_link_config *link, *_link; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2376 | int count; |
| 2377 | int size; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2378 | int i, ret; |
| 2379 | bool abi_match; |
| 2380 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2381 | count = le32_to_cpu(hdr->count); |
| 2382 | |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2383 | /* check the element size and count */ |
| 2384 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2385 | size = le32_to_cpu(link->size); |
| 2386 | if (size > sizeof(struct snd_soc_tplg_link_config) |
| 2387 | || size < sizeof(struct snd_soc_tplg_link_config_v4)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2388 | dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2389 | size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2390 | return -EINVAL; |
| 2391 | } |
| 2392 | |
| 2393 | if (soc_tplg_check_elem_count(tplg, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2394 | size, count, |
| 2395 | le32_to_cpu(hdr->payload_size), |
| 2396 | "physical link config")) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2397 | dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n", |
| 2398 | count); |
| 2399 | return -EINVAL; |
| 2400 | } |
| 2401 | |
| 2402 | /* config physical DAI links */ |
| 2403 | for (i = 0; i < count; i++) { |
| 2404 | link = (struct snd_soc_tplg_link_config *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2405 | size = le32_to_cpu(link->size); |
| 2406 | if (size == sizeof(*link)) { |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2407 | abi_match = true; |
| 2408 | _link = link; |
| 2409 | } else { |
| 2410 | abi_match = false; |
| 2411 | ret = link_new_ver(tplg, link, &_link); |
| 2412 | if (ret < 0) |
| 2413 | return ret; |
| 2414 | } |
| 2415 | |
| 2416 | ret = soc_tplg_link_config(tplg, _link); |
Dragos Tarcatu | 2b2d5c4 | 2020-02-07 20:53:24 +0200 | [diff] [blame] | 2417 | if (ret < 0) { |
| 2418 | if (!abi_match) |
| 2419 | kfree(_link); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2420 | return ret; |
Dragos Tarcatu | 2b2d5c4 | 2020-02-07 20:53:24 +0200 | [diff] [blame] | 2421 | } |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2422 | |
| 2423 | /* offset by version-specific struct size and |
| 2424 | * real priv data size |
| 2425 | */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2426 | tplg->pos += size + le32_to_cpu(_link->priv.size); |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2427 | |
| 2428 | if (!abi_match) |
| 2429 | kfree(_link); /* free the duplicated one */ |
| 2430 | } |
| 2431 | |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2435 | /** |
| 2436 | * soc_tplg_dai_config - Find and configure an existing physical DAI. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2437 | * @tplg: topology context |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2438 | * @d: physical DAI configs. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2439 | * |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2440 | * The physical dai should already be registered by the platform driver. |
| 2441 | * The platform driver should specify the DAI name and ID for matching. |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2442 | */ |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2443 | static int soc_tplg_dai_config(struct soc_tplg *tplg, |
| 2444 | struct snd_soc_tplg_dai *d) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2445 | { |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2446 | struct snd_soc_dai_link_component dai_component; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2447 | struct snd_soc_dai *dai; |
| 2448 | struct snd_soc_dai_driver *dai_drv; |
| 2449 | struct snd_soc_pcm_stream *stream; |
| 2450 | struct snd_soc_tplg_stream_caps *caps; |
| 2451 | int ret; |
| 2452 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2453 | memset(&dai_component, 0, sizeof(dai_component)); |
| 2454 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2455 | dai_component.dai_name = d->dai_name; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2456 | dai = snd_soc_find_dai(&dai_component); |
| 2457 | if (!dai) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2458 | dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n", |
| 2459 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2460 | return -EINVAL; |
| 2461 | } |
| 2462 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2463 | if (le32_to_cpu(d->dai_id) != dai->id) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2464 | dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n", |
| 2465 | d->dai_name); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2466 | return -EINVAL; |
| 2467 | } |
| 2468 | |
| 2469 | dai_drv = dai->driver; |
| 2470 | if (!dai_drv) |
| 2471 | return -EINVAL; |
| 2472 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2473 | if (d->playback) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2474 | stream = &dai_drv->playback; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2475 | caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2476 | ret = set_stream_info(stream, caps); |
| 2477 | if (ret < 0) |
| 2478 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2479 | } |
| 2480 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2481 | if (d->capture) { |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2482 | stream = &dai_drv->capture; |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2483 | caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2484 | ret = set_stream_info(stream, caps); |
| 2485 | if (ret < 0) |
| 2486 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2489 | if (d->flag_mask) |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2490 | set_dai_flags(dai_drv, |
| 2491 | le32_to_cpu(d->flag_mask), |
| 2492 | le32_to_cpu(d->flags)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2493 | |
| 2494 | /* pass control to component driver for optional further init */ |
Liam Girdwood | c60b613 | 2018-06-14 20:50:37 +0100 | [diff] [blame] | 2495 | ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2496 | if (ret < 0) { |
| 2497 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2498 | goto err; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | return 0; |
Amadeusz Sławiński | abc3caa | 2020-03-27 16:47:24 -0400 | [diff] [blame] | 2502 | |
| 2503 | err: |
| 2504 | kfree(dai_drv->playback.stream_name); |
| 2505 | kfree(dai_drv->capture.stream_name); |
| 2506 | return ret; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2507 | } |
| 2508 | |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2509 | /* load physical DAI elements */ |
| 2510 | static int soc_tplg_dai_elems_load(struct soc_tplg *tplg, |
| 2511 | struct snd_soc_tplg_hdr *hdr) |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2512 | { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2513 | struct snd_soc_tplg_dai *dai; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2514 | int count; |
Amadeusz Sławiński | dd8e871 | 2020-03-27 16:47:29 -0400 | [diff] [blame] | 2515 | int i, ret; |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2516 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2517 | count = le32_to_cpu(hdr->count); |
| 2518 | |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2519 | /* config the existing BE DAIs */ |
| 2520 | for (i = 0; i < count; i++) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2521 | dai = (struct snd_soc_tplg_dai *)tplg->pos; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2522 | if (le32_to_cpu(dai->size) != sizeof(*dai)) { |
Mengdong Lin | 9aa3f03 | 2016-11-03 01:05:15 +0800 | [diff] [blame] | 2523 | dev_err(tplg->dev, "ASoC: invalid physical DAI size\n"); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2524 | return -EINVAL; |
| 2525 | } |
| 2526 | |
Amadeusz Sławiński | dd8e871 | 2020-03-27 16:47:29 -0400 | [diff] [blame] | 2527 | ret = soc_tplg_dai_config(tplg, dai); |
| 2528 | if (ret < 0) { |
| 2529 | dev_err(tplg->dev, "ASoC: failed to configure DAI\n"); |
| 2530 | return ret; |
| 2531 | } |
| 2532 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2533 | tplg->pos += (sizeof(*dai) + le32_to_cpu(dai->priv.size)); |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count); |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2540 | /** |
| 2541 | * manifest_new_ver - Create a new version of manifest from the old version |
| 2542 | * of source. |
Charles Keepax | 8abab35 | 2017-01-12 11:38:15 +0000 | [diff] [blame] | 2543 | * @tplg: topology context |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2544 | * @src: old version of manifest as a source |
| 2545 | * @manifest: latest version of manifest created from the source |
| 2546 | * |
| 2547 | * Support from vesion 4. Users need free the returned manifest manually. |
| 2548 | */ |
| 2549 | static int manifest_new_ver(struct soc_tplg *tplg, |
| 2550 | struct snd_soc_tplg_manifest *src, |
| 2551 | struct snd_soc_tplg_manifest **manifest) |
| 2552 | { |
| 2553 | struct snd_soc_tplg_manifest *dest; |
| 2554 | struct snd_soc_tplg_manifest_v4 *src_v4; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2555 | int size; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2556 | |
| 2557 | *manifest = NULL; |
| 2558 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2559 | size = le32_to_cpu(src->size); |
| 2560 | if (size != sizeof(*src_v4)) { |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2561 | dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2562 | size); |
| 2563 | if (size) |
Guenter Roeck | ac9391d | 2018-05-24 12:49:21 -0700 | [diff] [blame] | 2564 | return -EINVAL; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2565 | src->size = cpu_to_le32(sizeof(*src_v4)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | dev_warn(tplg->dev, "ASoC: old version of manifest\n"); |
| 2569 | |
| 2570 | src_v4 = (struct snd_soc_tplg_manifest_v4 *)src; |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2571 | dest = kzalloc(sizeof(*dest) + le32_to_cpu(src_v4->priv.size), |
| 2572 | GFP_KERNEL); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2573 | if (!dest) |
| 2574 | return -ENOMEM; |
| 2575 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2576 | dest->size = cpu_to_le32(sizeof(*dest)); /* size of latest abi version */ |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2577 | dest->control_elems = src_v4->control_elems; |
| 2578 | dest->widget_elems = src_v4->widget_elems; |
| 2579 | dest->graph_elems = src_v4->graph_elems; |
| 2580 | dest->pcm_elems = src_v4->pcm_elems; |
| 2581 | dest->dai_link_elems = src_v4->dai_link_elems; |
| 2582 | dest->priv.size = src_v4->priv.size; |
| 2583 | if (dest->priv.size) |
| 2584 | memcpy(dest->priv.data, src_v4->priv.data, |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2585 | le32_to_cpu(src_v4->priv.size)); |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2586 | |
| 2587 | *manifest = dest; |
| 2588 | return 0; |
| 2589 | } |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2590 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2591 | static int soc_tplg_manifest_load(struct soc_tplg *tplg, |
Mengdong Lin | 0038be9 | 2016-07-26 14:32:37 +0800 | [diff] [blame] | 2592 | struct snd_soc_tplg_hdr *hdr) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2593 | { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2594 | struct snd_soc_tplg_manifest *manifest, *_manifest; |
| 2595 | bool abi_match; |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2596 | int ret = 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2597 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2598 | manifest = (struct snd_soc_tplg_manifest *)tplg->pos; |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2599 | |
| 2600 | /* check ABI version by size, create a new manifest if abi not match */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2601 | if (le32_to_cpu(manifest->size) == sizeof(*manifest)) { |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2602 | abi_match = true; |
| 2603 | _manifest = manifest; |
| 2604 | } else { |
| 2605 | abi_match = false; |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2606 | ret = manifest_new_ver(tplg, manifest, &_manifest); |
| 2607 | if (ret < 0) |
| 2608 | return ret; |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2609 | } |
| 2610 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2611 | /* pass control to component driver for optional further init */ |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 2612 | if (tplg->ops && tplg->ops->manifest) |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2613 | ret = tplg->ops->manifest(tplg->comp, tplg->index, _manifest); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2614 | |
Mengdong Lin | 583958f | 2016-10-11 14:36:42 +0800 | [diff] [blame] | 2615 | if (!abi_match) /* free the duplicated one */ |
| 2616 | kfree(_manifest); |
| 2617 | |
Dragos Tarcatu | 242c46c | 2020-02-07 20:53:25 +0200 | [diff] [blame] | 2618 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* validate header magic, size and type */ |
| 2622 | static int soc_valid_header(struct soc_tplg *tplg, |
| 2623 | struct snd_soc_tplg_hdr *hdr) |
| 2624 | { |
| 2625 | if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) |
| 2626 | return 0; |
| 2627 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2628 | if (le32_to_cpu(hdr->size) != sizeof(*hdr)) { |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2629 | dev_err(tplg->dev, |
| 2630 | "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n", |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2631 | le32_to_cpu(hdr->type), soc_tplg_get_hdr_offset(tplg), |
Mengdong Lin | 06eb49f | 2016-04-27 14:52:56 +0800 | [diff] [blame] | 2632 | tplg->fw->size); |
| 2633 | return -EINVAL; |
| 2634 | } |
| 2635 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2636 | /* big endian firmware objects not supported atm */ |
Amadeusz Sławiński | 26d8788 | 2020-04-15 12:24:35 -0400 | [diff] [blame] | 2637 | if (le32_to_cpu(hdr->magic) == SOC_TPLG_MAGIC_BIG_ENDIAN) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2638 | dev_err(tplg->dev, |
| 2639 | "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", |
| 2640 | tplg->pass, hdr->magic, |
| 2641 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2642 | return -EINVAL; |
| 2643 | } |
| 2644 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2645 | if (le32_to_cpu(hdr->magic) != SND_SOC_TPLG_MAGIC) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2646 | dev_err(tplg->dev, |
| 2647 | "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", |
| 2648 | tplg->pass, hdr->magic, |
| 2649 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 2650 | return -EINVAL; |
| 2651 | } |
| 2652 | |
Mengdong Lin | 288b8da | 2016-11-03 01:03:17 +0800 | [diff] [blame] | 2653 | /* Support ABI from version 4 */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2654 | if (le32_to_cpu(hdr->abi) > SND_SOC_TPLG_ABI_VERSION || |
| 2655 | le32_to_cpu(hdr->abi) < SND_SOC_TPLG_ABI_VERSION_MIN) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2656 | dev_err(tplg->dev, |
| 2657 | "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", |
| 2658 | tplg->pass, hdr->abi, |
| 2659 | SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), |
| 2660 | tplg->fw->size); |
| 2661 | return -EINVAL; |
| 2662 | } |
| 2663 | |
| 2664 | if (hdr->payload_size == 0) { |
| 2665 | dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", |
| 2666 | soc_tplg_get_hdr_offset(tplg)); |
| 2667 | return -EINVAL; |
| 2668 | } |
| 2669 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
| 2673 | /* check header type and call appropriate handler */ |
| 2674 | static int soc_tplg_load_header(struct soc_tplg *tplg, |
| 2675 | struct snd_soc_tplg_hdr *hdr) |
| 2676 | { |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2677 | int (*elem_load)(struct soc_tplg *tplg, |
| 2678 | struct snd_soc_tplg_hdr *hdr); |
| 2679 | unsigned int hdr_pass; |
| 2680 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2681 | tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); |
| 2682 | |
| 2683 | /* check for matching ID */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2684 | if (le32_to_cpu(hdr->index) != tplg->req_index && |
Liam Girdwood | bb97142 | 2017-06-29 14:22:25 +0100 | [diff] [blame] | 2685 | tplg->req_index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2686 | return 0; |
| 2687 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2688 | tplg->index = le32_to_cpu(hdr->index); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2689 | |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2690 | switch (le32_to_cpu(hdr->type)) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2691 | case SND_SOC_TPLG_TYPE_MIXER: |
| 2692 | case SND_SOC_TPLG_TYPE_ENUM: |
| 2693 | case SND_SOC_TPLG_TYPE_BYTES: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2694 | hdr_pass = SOC_TPLG_PASS_MIXER; |
| 2695 | elem_load = soc_tplg_kcontrol_elems_load; |
| 2696 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2697 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2698 | hdr_pass = SOC_TPLG_PASS_GRAPH; |
| 2699 | elem_load = soc_tplg_dapm_graph_elems_load; |
| 2700 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2701 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2702 | hdr_pass = SOC_TPLG_PASS_WIDGET; |
| 2703 | elem_load = soc_tplg_dapm_widget_elems_load; |
| 2704 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2705 | case SND_SOC_TPLG_TYPE_PCM: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2706 | hdr_pass = SOC_TPLG_PASS_PCM_DAI; |
| 2707 | elem_load = soc_tplg_pcm_elems_load; |
| 2708 | break; |
Mengdong Lin | 3fbf793 | 2016-11-03 01:05:01 +0800 | [diff] [blame] | 2709 | case SND_SOC_TPLG_TYPE_DAI: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2710 | hdr_pass = SOC_TPLG_PASS_BE_DAI; |
| 2711 | elem_load = soc_tplg_dai_elems_load; |
| 2712 | break; |
Mengdong Lin | 593d9e5 | 2016-11-03 01:04:27 +0800 | [diff] [blame] | 2713 | case SND_SOC_TPLG_TYPE_DAI_LINK: |
| 2714 | case SND_SOC_TPLG_TYPE_BACKEND_LINK: |
| 2715 | /* physical link configurations */ |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2716 | hdr_pass = SOC_TPLG_PASS_LINK; |
| 2717 | elem_load = soc_tplg_link_elems_load; |
| 2718 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2719 | case SND_SOC_TPLG_TYPE_MANIFEST: |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2720 | hdr_pass = SOC_TPLG_PASS_MANIFEST; |
| 2721 | elem_load = soc_tplg_manifest_load; |
| 2722 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2723 | default: |
| 2724 | /* bespoke vendor data object */ |
Keyon Jie | 82ed741 | 2020-05-27 10:28:00 +0800 | [diff] [blame] | 2725 | hdr_pass = SOC_TPLG_PASS_VENDOR; |
| 2726 | elem_load = soc_tplg_vendor_load; |
| 2727 | break; |
| 2728 | } |
| 2729 | |
| 2730 | if (tplg->pass == hdr_pass) { |
| 2731 | dev_dbg(tplg->dev, |
| 2732 | "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", |
| 2733 | hdr->payload_size, hdr->type, hdr->version, |
| 2734 | hdr->vendor_type, tplg->pass); |
| 2735 | return elem_load(tplg, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
| 2741 | /* process the topology file headers */ |
| 2742 | static int soc_tplg_process_headers(struct soc_tplg *tplg) |
| 2743 | { |
| 2744 | struct snd_soc_tplg_hdr *hdr; |
| 2745 | int ret; |
| 2746 | |
| 2747 | tplg->pass = SOC_TPLG_PASS_START; |
| 2748 | |
| 2749 | /* process the header types from start to end */ |
| 2750 | while (tplg->pass <= SOC_TPLG_PASS_END) { |
| 2751 | |
| 2752 | tplg->hdr_pos = tplg->fw->data; |
| 2753 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2754 | |
| 2755 | while (!soc_tplg_is_eof(tplg)) { |
| 2756 | |
| 2757 | /* make sure header is valid before loading */ |
| 2758 | ret = soc_valid_header(tplg, hdr); |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 2759 | if (ret < 0) { |
| 2760 | dev_err(tplg->dev, |
| 2761 | "ASoC: topology: invalid header: %d\n", ret); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2762 | return ret; |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 2763 | } else if (ret == 0) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2764 | break; |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 2765 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2766 | |
| 2767 | /* load the header object */ |
| 2768 | ret = soc_tplg_load_header(tplg, hdr); |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 2769 | if (ret < 0) { |
| 2770 | dev_err(tplg->dev, |
| 2771 | "ASoC: topology: could not load header: %d\n", ret); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2772 | return ret; |
Pierre-Louis Bossart | 8bf9475 | 2020-07-07 15:37:49 -0500 | [diff] [blame] | 2773 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* goto next header */ |
Pierre-Louis Bossart | 5aebe7c | 2019-04-04 14:13:57 -0500 | [diff] [blame] | 2776 | tplg->hdr_pos += le32_to_cpu(hdr->payload_size) + |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2777 | sizeof(struct snd_soc_tplg_hdr); |
| 2778 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 2779 | } |
| 2780 | |
| 2781 | /* next data type pass */ |
| 2782 | tplg->pass++; |
| 2783 | } |
| 2784 | |
| 2785 | /* signal DAPM we are complete */ |
| 2786 | ret = soc_tplg_dapm_complete(tplg); |
| 2787 | if (ret < 0) |
| 2788 | dev_err(tplg->dev, |
| 2789 | "ASoC: failed to initialise DAPM from Firmware\n"); |
| 2790 | |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
| 2794 | static int soc_tplg_load(struct soc_tplg *tplg) |
| 2795 | { |
| 2796 | int ret; |
| 2797 | |
| 2798 | ret = soc_tplg_process_headers(tplg); |
| 2799 | if (ret == 0) |
| 2800 | soc_tplg_complete(tplg); |
| 2801 | |
| 2802 | return ret; |
| 2803 | } |
| 2804 | |
| 2805 | /* load audio component topology from "firmware" file */ |
| 2806 | int snd_soc_tplg_component_load(struct snd_soc_component *comp, |
| 2807 | struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) |
| 2808 | { |
| 2809 | struct soc_tplg tplg; |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2810 | int ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2811 | |
Amadeusz Sławiński | c42464a | 2020-03-12 08:22:39 -0400 | [diff] [blame] | 2812 | /* component needs to exist to keep and reference data while parsing */ |
| 2813 | if (!comp) |
| 2814 | return -EINVAL; |
| 2815 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2816 | /* setup parsing context */ |
| 2817 | memset(&tplg, 0, sizeof(tplg)); |
| 2818 | tplg.fw = fw; |
| 2819 | tplg.dev = comp->dev; |
| 2820 | tplg.comp = comp; |
| 2821 | tplg.ops = ops; |
| 2822 | tplg.req_index = id; |
| 2823 | tplg.io_ops = ops->io_ops; |
| 2824 | tplg.io_ops_count = ops->io_ops_count; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 2825 | tplg.bytes_ext_ops = ops->bytes_ext_ops; |
| 2826 | tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2827 | |
Bard liao | 304017d | 2019-02-17 21:23:47 +0800 | [diff] [blame] | 2828 | ret = soc_tplg_load(&tplg); |
| 2829 | /* free the created components if fail to load topology */ |
| 2830 | if (ret) |
| 2831 | snd_soc_tplg_component_remove(comp, SND_SOC_TPLG_INDEX_ALL); |
| 2832 | |
| 2833 | return ret; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2834 | } |
| 2835 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); |
| 2836 | |
| 2837 | /* remove this dynamic widget */ |
| 2838 | void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) |
| 2839 | { |
| 2840 | /* make sure we are a widget */ |
| 2841 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET) |
| 2842 | return; |
| 2843 | |
| 2844 | remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); |
| 2845 | } |
| 2846 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); |
| 2847 | |
| 2848 | /* remove all dynamic widgets from this DAPM context */ |
| 2849 | void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, |
| 2850 | u32 index) |
| 2851 | { |
| 2852 | struct snd_soc_dapm_widget *w, *next_w; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2853 | |
Kuninori Morimoto | 1459669 | 2020-03-09 13:08:21 +0900 | [diff] [blame] | 2854 | for_each_card_widgets_safe(dapm->card, w, next_w) { |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2855 | |
| 2856 | /* make sure we are a widget with correct context */ |
| 2857 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) |
| 2858 | continue; |
| 2859 | |
| 2860 | /* match ID */ |
| 2861 | if (w->dobj.index != index && |
| 2862 | w->dobj.index != SND_SOC_TPLG_INDEX_ALL) |
| 2863 | continue; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2864 | /* check and free and dynamic widget kcontrols */ |
| 2865 | snd_soc_tplg_widget_remove(w); |
Lars-Peter Clausen | b97e269 | 2015-07-21 18:11:07 +0200 | [diff] [blame] | 2866 | snd_soc_dapm_free_widget(w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2867 | } |
Jyri Sarha | fd589a1 | 2015-11-10 18:12:42 +0200 | [diff] [blame] | 2868 | snd_soc_dapm_reset_cache(dapm); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2869 | } |
| 2870 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); |
| 2871 | |
| 2872 | /* remove dynamic controls from the component driver */ |
| 2873 | int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) |
| 2874 | { |
| 2875 | struct snd_soc_dobj *dobj, *next_dobj; |
| 2876 | int pass = SOC_TPLG_PASS_END; |
| 2877 | |
| 2878 | /* process the header types from end to start */ |
| 2879 | while (pass >= SOC_TPLG_PASS_START) { |
| 2880 | |
| 2881 | /* remove mixer controls */ |
| 2882 | list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, |
| 2883 | list) { |
| 2884 | |
| 2885 | /* match index */ |
| 2886 | if (dobj->index != index && |
Yan Wang | feb12f0 | 2018-03-26 16:48:00 +0100 | [diff] [blame] | 2887 | index != SND_SOC_TPLG_INDEX_ALL) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2888 | continue; |
| 2889 | |
| 2890 | switch (dobj->type) { |
| 2891 | case SND_SOC_DOBJ_MIXER: |
| 2892 | remove_mixer(comp, dobj, pass); |
| 2893 | break; |
| 2894 | case SND_SOC_DOBJ_ENUM: |
| 2895 | remove_enum(comp, dobj, pass); |
| 2896 | break; |
| 2897 | case SND_SOC_DOBJ_BYTES: |
| 2898 | remove_bytes(comp, dobj, pass); |
| 2899 | break; |
Ranjani Sridharan | 7df04ea | 2019-01-25 14:06:47 -0600 | [diff] [blame] | 2900 | case SND_SOC_DOBJ_GRAPH: |
| 2901 | remove_route(comp, dobj, pass); |
| 2902 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2903 | case SND_SOC_DOBJ_WIDGET: |
| 2904 | remove_widget(comp, dobj, pass); |
| 2905 | break; |
| 2906 | case SND_SOC_DOBJ_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame] | 2907 | remove_dai(comp, dobj, pass); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2908 | break; |
Mengdong Lin | acfc7d4 | 2016-01-15 16:13:37 +0800 | [diff] [blame] | 2909 | case SND_SOC_DOBJ_DAI_LINK: |
| 2910 | remove_link(comp, dobj, pass); |
| 2911 | break; |
Bard liao | adfebb5 | 2019-02-01 11:07:40 -0600 | [diff] [blame] | 2912 | case SND_SOC_DOBJ_BACKEND_LINK: |
| 2913 | /* |
| 2914 | * call link_unload ops if extra |
| 2915 | * deinitialization is needed. |
| 2916 | */ |
| 2917 | remove_backend_link(comp, dobj, pass); |
| 2918 | break; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2919 | default: |
| 2920 | dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", |
| 2921 | dobj->type); |
| 2922 | break; |
| 2923 | } |
| 2924 | } |
| 2925 | pass--; |
| 2926 | } |
| 2927 | |
| 2928 | /* let caller know if FW can be freed when no objects are left */ |
| 2929 | return !list_empty(&comp->dobj_list); |
| 2930 | } |
| 2931 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); |