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