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 |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 51 | |
| 52 | #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST |
Mengdong Lin | 1a8e7fa | 2015-08-10 22:48:30 +0800 | [diff] [blame] | 53 | #define SOC_TPLG_PASS_END SOC_TPLG_PASS_PINS |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 54 | |
| 55 | struct soc_tplg { |
| 56 | const struct firmware *fw; |
| 57 | |
| 58 | /* runtime FW parsing */ |
| 59 | const u8 *pos; /* read postion */ |
| 60 | const u8 *hdr_pos; /* header position */ |
| 61 | unsigned int pass; /* pass number */ |
| 62 | |
| 63 | /* component caller */ |
| 64 | struct device *dev; |
| 65 | struct snd_soc_component *comp; |
| 66 | u32 index; /* current block index */ |
| 67 | u32 req_index; /* required index, only loaded/free matching blocks */ |
| 68 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 69 | /* vendor specific kcontrol operations */ |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 70 | const struct snd_soc_tplg_kcontrol_ops *io_ops; |
| 71 | int io_ops_count; |
| 72 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 73 | /* vendor specific bytes ext handlers, for TLV bytes controls */ |
| 74 | const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops; |
| 75 | int bytes_ext_ops_count; |
| 76 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 77 | /* optional fw loading callbacks to component drivers */ |
| 78 | struct snd_soc_tplg_ops *ops; |
| 79 | }; |
| 80 | |
| 81 | static int soc_tplg_process_headers(struct soc_tplg *tplg); |
| 82 | static void soc_tplg_complete(struct soc_tplg *tplg); |
| 83 | struct snd_soc_dapm_widget * |
| 84 | snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm, |
| 85 | const struct snd_soc_dapm_widget *widget); |
| 86 | struct snd_soc_dapm_widget * |
| 87 | snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm, |
| 88 | const struct snd_soc_dapm_widget *widget); |
| 89 | |
| 90 | /* check we dont overflow the data for this control chunk */ |
| 91 | static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size, |
| 92 | unsigned int count, size_t bytes, const char *elem_type) |
| 93 | { |
| 94 | const u8 *end = tplg->pos + elem_size * count; |
| 95 | |
| 96 | if (end > tplg->fw->data + tplg->fw->size) { |
| 97 | dev_err(tplg->dev, "ASoC: %s overflow end of data\n", |
| 98 | elem_type); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | /* check there is enough room in chunk for control. |
| 103 | extra bytes at the end of control are for vendor data here */ |
| 104 | if (elem_size * count > bytes) { |
| 105 | dev_err(tplg->dev, |
| 106 | "ASoC: %s count %d of size %zu is bigger than chunk %zu\n", |
| 107 | elem_type, count, elem_size, bytes); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static inline int soc_tplg_is_eof(struct soc_tplg *tplg) |
| 115 | { |
| 116 | const u8 *end = tplg->hdr_pos; |
| 117 | |
| 118 | if (end >= tplg->fw->data + tplg->fw->size) |
| 119 | return 1; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg) |
| 124 | { |
| 125 | return (unsigned long)(tplg->hdr_pos - tplg->fw->data); |
| 126 | } |
| 127 | |
| 128 | static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg) |
| 129 | { |
| 130 | return (unsigned long)(tplg->pos - tplg->fw->data); |
| 131 | } |
| 132 | |
| 133 | /* mapping of Kcontrol types and associated operations. */ |
| 134 | static const struct snd_soc_tplg_kcontrol_ops io_ops[] = { |
| 135 | {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw, |
| 136 | snd_soc_put_volsw, snd_soc_info_volsw}, |
| 137 | {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx, |
| 138 | snd_soc_put_volsw_sx, NULL}, |
| 139 | {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double, |
| 140 | snd_soc_put_enum_double, snd_soc_info_enum_double}, |
| 141 | {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double, |
| 142 | snd_soc_put_enum_double, NULL}, |
| 143 | {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get, |
| 144 | snd_soc_bytes_put, snd_soc_bytes_info}, |
| 145 | {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range, |
| 146 | snd_soc_put_volsw_range, snd_soc_info_volsw_range}, |
| 147 | {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx, |
| 148 | snd_soc_put_xr_sx, snd_soc_info_xr_sx}, |
| 149 | {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe, |
| 150 | snd_soc_put_strobe, NULL}, |
| 151 | {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw, |
Jeeja KP | 2c57d478 | 2015-07-14 13:10:47 +0530 | [diff] [blame] | 152 | snd_soc_dapm_put_volsw, snd_soc_info_volsw}, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 153 | {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double, |
| 154 | snd_soc_dapm_put_enum_double, snd_soc_info_enum_double}, |
| 155 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double, |
| 156 | snd_soc_dapm_put_enum_double, NULL}, |
| 157 | {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double, |
| 158 | snd_soc_dapm_put_enum_double, NULL}, |
| 159 | {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch, |
| 160 | snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch}, |
| 161 | }; |
| 162 | |
| 163 | struct soc_tplg_map { |
| 164 | int uid; |
| 165 | int kid; |
| 166 | }; |
| 167 | |
| 168 | /* mapping of widget types from UAPI IDs to kernel IDs */ |
| 169 | static const struct soc_tplg_map dapm_map[] = { |
| 170 | {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input}, |
| 171 | {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output}, |
| 172 | {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux}, |
| 173 | {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer}, |
| 174 | {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga}, |
| 175 | {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv}, |
| 176 | {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc}, |
| 177 | {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac}, |
| 178 | {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch}, |
| 179 | {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre}, |
| 180 | {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post}, |
| 181 | {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in}, |
| 182 | {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out}, |
| 183 | {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in}, |
| 184 | {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out}, |
| 185 | {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link}, |
| 186 | }; |
| 187 | |
| 188 | static int tplc_chan_get_reg(struct soc_tplg *tplg, |
| 189 | struct snd_soc_tplg_channel *chan, int map) |
| 190 | { |
| 191 | int i; |
| 192 | |
| 193 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
| 194 | if (chan[i].id == map) |
| 195 | return chan[i].reg; |
| 196 | } |
| 197 | |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | static int tplc_chan_get_shift(struct soc_tplg *tplg, |
| 202 | struct snd_soc_tplg_channel *chan, int map) |
| 203 | { |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) { |
| 207 | if (chan[i].id == map) |
| 208 | return chan[i].shift; |
| 209 | } |
| 210 | |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | static int get_widget_id(int tplg_type) |
| 215 | { |
| 216 | int i; |
| 217 | |
| 218 | for (i = 0; i < ARRAY_SIZE(dapm_map); i++) { |
| 219 | if (tplg_type == dapm_map[i].uid) |
| 220 | return dapm_map[i].kid; |
| 221 | } |
| 222 | |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | static enum snd_soc_dobj_type get_dobj_mixer_type( |
| 227 | struct snd_soc_tplg_ctl_hdr *control_hdr) |
| 228 | { |
| 229 | if (control_hdr == NULL) |
| 230 | return SND_SOC_DOBJ_NONE; |
| 231 | |
| 232 | switch (control_hdr->ops.info) { |
| 233 | case SND_SOC_TPLG_CTL_VOLSW: |
| 234 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 235 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 236 | case SND_SOC_TPLG_CTL_RANGE: |
| 237 | case SND_SOC_TPLG_CTL_STROBE: |
| 238 | return SND_SOC_DOBJ_MIXER; |
| 239 | case SND_SOC_TPLG_CTL_ENUM: |
| 240 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 241 | return SND_SOC_DOBJ_ENUM; |
| 242 | case SND_SOC_TPLG_CTL_BYTES: |
| 243 | return SND_SOC_DOBJ_BYTES; |
| 244 | default: |
| 245 | return SND_SOC_DOBJ_NONE; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | static enum snd_soc_dobj_type get_dobj_type(struct snd_soc_tplg_hdr *hdr, |
| 250 | struct snd_soc_tplg_ctl_hdr *control_hdr) |
| 251 | { |
| 252 | switch (hdr->type) { |
| 253 | case SND_SOC_TPLG_TYPE_MIXER: |
| 254 | return get_dobj_mixer_type(control_hdr); |
| 255 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: |
| 256 | case SND_SOC_TPLG_TYPE_MANIFEST: |
| 257 | return SND_SOC_DOBJ_NONE; |
| 258 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: |
| 259 | return SND_SOC_DOBJ_WIDGET; |
| 260 | case SND_SOC_TPLG_TYPE_DAI_LINK: |
| 261 | return SND_SOC_DOBJ_DAI_LINK; |
| 262 | case SND_SOC_TPLG_TYPE_PCM: |
| 263 | return SND_SOC_DOBJ_PCM; |
| 264 | case SND_SOC_TPLG_TYPE_CODEC_LINK: |
| 265 | return SND_SOC_DOBJ_CODEC_LINK; |
| 266 | default: |
| 267 | return SND_SOC_DOBJ_NONE; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static inline void soc_bind_err(struct soc_tplg *tplg, |
| 272 | struct snd_soc_tplg_ctl_hdr *hdr, int index) |
| 273 | { |
| 274 | dev_err(tplg->dev, |
| 275 | "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n", |
| 276 | hdr->ops.get, hdr->ops.put, hdr->ops.info, index, |
| 277 | soc_tplg_get_offset(tplg)); |
| 278 | } |
| 279 | |
| 280 | static inline void soc_control_err(struct soc_tplg *tplg, |
| 281 | struct snd_soc_tplg_ctl_hdr *hdr, const char *name) |
| 282 | { |
| 283 | dev_err(tplg->dev, |
| 284 | "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n", |
| 285 | name, hdr->ops.get, hdr->ops.put, hdr->ops.info, |
| 286 | soc_tplg_get_offset(tplg)); |
| 287 | } |
| 288 | |
| 289 | /* pass vendor data to component driver for processing */ |
| 290 | static int soc_tplg_vendor_load_(struct soc_tplg *tplg, |
| 291 | struct snd_soc_tplg_hdr *hdr) |
| 292 | { |
| 293 | int ret = 0; |
| 294 | |
| 295 | if (tplg->comp && tplg->ops && tplg->ops->vendor_load) |
| 296 | ret = tplg->ops->vendor_load(tplg->comp, hdr); |
| 297 | else { |
| 298 | dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n", |
| 299 | hdr->vendor_type); |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | if (ret < 0) |
| 304 | dev_err(tplg->dev, |
| 305 | "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n", |
| 306 | soc_tplg_get_hdr_offset(tplg), |
| 307 | soc_tplg_get_hdr_offset(tplg), |
| 308 | hdr->type, hdr->vendor_type); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* pass vendor data to component driver for processing */ |
| 313 | static int soc_tplg_vendor_load(struct soc_tplg *tplg, |
| 314 | struct snd_soc_tplg_hdr *hdr) |
| 315 | { |
| 316 | if (tplg->pass != SOC_TPLG_PASS_VENDOR) |
| 317 | return 0; |
| 318 | |
| 319 | return soc_tplg_vendor_load_(tplg, hdr); |
| 320 | } |
| 321 | |
| 322 | /* optionally pass new dynamic widget to component driver. This is mainly for |
| 323 | * external widgets where we can assign private data/ops */ |
| 324 | static int soc_tplg_widget_load(struct soc_tplg *tplg, |
| 325 | struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w) |
| 326 | { |
| 327 | if (tplg->comp && tplg->ops && tplg->ops->widget_load) |
| 328 | return tplg->ops->widget_load(tplg->comp, w, tplg_w); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 333 | /* pass DAI configurations to component driver for extra intialization */ |
| 334 | static int soc_tplg_dai_load(struct soc_tplg *tplg, |
| 335 | struct snd_soc_dai_driver *dai_drv) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 336 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 337 | if (tplg->comp && tplg->ops && tplg->ops->dai_load) |
| 338 | return tplg->ops->dai_load(tplg->comp, dai_drv); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /* tell the component driver that all firmware has been loaded in this request */ |
| 344 | static void soc_tplg_complete(struct soc_tplg *tplg) |
| 345 | { |
| 346 | if (tplg->comp && tplg->ops && tplg->ops->complete) |
| 347 | tplg->ops->complete(tplg->comp); |
| 348 | } |
| 349 | |
| 350 | /* add a dynamic kcontrol */ |
| 351 | static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev, |
| 352 | const struct snd_kcontrol_new *control_new, const char *prefix, |
| 353 | void *data, struct snd_kcontrol **kcontrol) |
| 354 | { |
| 355 | int err; |
| 356 | |
| 357 | *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix); |
| 358 | if (*kcontrol == NULL) { |
| 359 | dev_err(dev, "ASoC: Failed to create new kcontrol %s\n", |
| 360 | control_new->name); |
| 361 | return -ENOMEM; |
| 362 | } |
| 363 | |
| 364 | err = snd_ctl_add(card, *kcontrol); |
| 365 | if (err < 0) { |
| 366 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
| 367 | control_new->name, err); |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* add a dynamic kcontrol for component driver */ |
| 375 | static int soc_tplg_add_kcontrol(struct soc_tplg *tplg, |
| 376 | struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol) |
| 377 | { |
| 378 | struct snd_soc_component *comp = tplg->comp; |
| 379 | |
| 380 | return soc_tplg_add_dcontrol(comp->card->snd_card, |
| 381 | comp->dev, k, NULL, comp, kcontrol); |
| 382 | } |
| 383 | |
| 384 | /* remove a mixer kcontrol */ |
| 385 | static void remove_mixer(struct snd_soc_component *comp, |
| 386 | struct snd_soc_dobj *dobj, int pass) |
| 387 | { |
| 388 | struct snd_card *card = comp->card->snd_card; |
| 389 | struct soc_mixer_control *sm = |
| 390 | container_of(dobj, struct soc_mixer_control, dobj); |
| 391 | const unsigned int *p = NULL; |
| 392 | |
| 393 | if (pass != SOC_TPLG_PASS_MIXER) |
| 394 | return; |
| 395 | |
| 396 | if (dobj->ops && dobj->ops->control_unload) |
| 397 | dobj->ops->control_unload(comp, dobj); |
| 398 | |
| 399 | if (sm->dobj.control.kcontrol->tlv.p) |
| 400 | p = sm->dobj.control.kcontrol->tlv.p; |
| 401 | snd_ctl_remove(card, sm->dobj.control.kcontrol); |
| 402 | list_del(&sm->dobj.list); |
| 403 | kfree(sm); |
| 404 | kfree(p); |
| 405 | } |
| 406 | |
| 407 | /* remove an enum kcontrol */ |
| 408 | static void remove_enum(struct snd_soc_component *comp, |
| 409 | struct snd_soc_dobj *dobj, int pass) |
| 410 | { |
| 411 | struct snd_card *card = comp->card->snd_card; |
| 412 | struct soc_enum *se = container_of(dobj, struct soc_enum, dobj); |
| 413 | int i; |
| 414 | |
| 415 | if (pass != SOC_TPLG_PASS_MIXER) |
| 416 | return; |
| 417 | |
| 418 | if (dobj->ops && dobj->ops->control_unload) |
| 419 | dobj->ops->control_unload(comp, dobj); |
| 420 | |
| 421 | snd_ctl_remove(card, se->dobj.control.kcontrol); |
| 422 | list_del(&se->dobj.list); |
| 423 | |
| 424 | kfree(se->dobj.control.dvalues); |
| 425 | for (i = 0; i < se->items; i++) |
| 426 | kfree(se->dobj.control.dtexts[i]); |
| 427 | kfree(se); |
| 428 | } |
| 429 | |
| 430 | /* remove a byte kcontrol */ |
| 431 | static void remove_bytes(struct snd_soc_component *comp, |
| 432 | struct snd_soc_dobj *dobj, int pass) |
| 433 | { |
| 434 | struct snd_card *card = comp->card->snd_card; |
| 435 | struct soc_bytes_ext *sb = |
| 436 | container_of(dobj, struct soc_bytes_ext, dobj); |
| 437 | |
| 438 | if (pass != SOC_TPLG_PASS_MIXER) |
| 439 | return; |
| 440 | |
| 441 | if (dobj->ops && dobj->ops->control_unload) |
| 442 | dobj->ops->control_unload(comp, dobj); |
| 443 | |
| 444 | snd_ctl_remove(card, sb->dobj.control.kcontrol); |
| 445 | list_del(&sb->dobj.list); |
| 446 | kfree(sb); |
| 447 | } |
| 448 | |
| 449 | /* remove a widget and it's kcontrols - routes must be removed first */ |
| 450 | static void remove_widget(struct snd_soc_component *comp, |
| 451 | struct snd_soc_dobj *dobj, int pass) |
| 452 | { |
| 453 | struct snd_card *card = comp->card->snd_card; |
| 454 | struct snd_soc_dapm_widget *w = |
| 455 | container_of(dobj, struct snd_soc_dapm_widget, dobj); |
| 456 | int i; |
| 457 | |
| 458 | if (pass != SOC_TPLG_PASS_WIDGET) |
| 459 | return; |
| 460 | |
| 461 | if (dobj->ops && dobj->ops->widget_unload) |
| 462 | dobj->ops->widget_unload(comp, dobj); |
| 463 | |
| 464 | /* |
| 465 | * Dynamic Widgets either have 1 enum kcontrol or 1..N mixers. |
| 466 | * The enum may either have an array of values or strings. |
| 467 | */ |
| 468 | if (dobj->widget.kcontrol_enum) { |
| 469 | /* enumerated widget mixer */ |
| 470 | struct soc_enum *se = |
| 471 | (struct soc_enum *)w->kcontrols[0]->private_value; |
| 472 | |
| 473 | snd_ctl_remove(card, w->kcontrols[0]); |
| 474 | |
| 475 | kfree(se->dobj.control.dvalues); |
| 476 | for (i = 0; i < se->items; i++) |
| 477 | kfree(se->dobj.control.dtexts[i]); |
| 478 | |
| 479 | kfree(se); |
| 480 | kfree(w->kcontrol_news); |
| 481 | } else { |
| 482 | /* non enumerated widget mixer */ |
| 483 | for (i = 0; i < w->num_kcontrols; i++) { |
| 484 | struct snd_kcontrol *kcontrol = w->kcontrols[i]; |
| 485 | struct soc_mixer_control *sm = |
| 486 | (struct soc_mixer_control *) kcontrol->private_value; |
| 487 | |
| 488 | kfree(w->kcontrols[i]->tlv.p); |
| 489 | |
| 490 | snd_ctl_remove(card, w->kcontrols[i]); |
| 491 | kfree(sm); |
| 492 | } |
| 493 | kfree(w->kcontrol_news); |
| 494 | } |
| 495 | /* widget w is freed by soc-dapm.c */ |
| 496 | } |
| 497 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 498 | /* remove DAI configurations */ |
| 499 | static void remove_dai(struct snd_soc_component *comp, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 500 | struct snd_soc_dobj *dobj, int pass) |
| 501 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 502 | struct snd_soc_dai_driver *dai_drv = |
| 503 | container_of(dobj, struct snd_soc_dai_driver, dobj); |
| 504 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 505 | if (pass != SOC_TPLG_PASS_PCM_DAI) |
| 506 | return; |
| 507 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 508 | if (dobj->ops && dobj->ops->dai_unload) |
| 509 | dobj->ops->dai_unload(comp, dobj); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 510 | |
| 511 | list_del(&dobj->list); |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 512 | kfree(dai_drv); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* bind a kcontrol to it's IO handlers */ |
| 516 | static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr, |
| 517 | struct snd_kcontrol_new *k, |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 518 | const struct soc_tplg *tplg) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 519 | { |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 520 | const struct snd_soc_tplg_kcontrol_ops *ops; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 521 | const struct snd_soc_tplg_bytes_ext_ops *ext_ops; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 522 | int num_ops, i; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 523 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 524 | if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES |
| 525 | && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER |
| 526 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE |
| 527 | && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { |
| 528 | struct soc_bytes_ext *sbe; |
| 529 | struct snd_soc_tplg_bytes_control *be; |
| 530 | |
| 531 | sbe = (struct soc_bytes_ext *)k->private_value; |
| 532 | be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr); |
| 533 | |
| 534 | /* TLV bytes controls need standard kcontrol info handler, |
| 535 | * TLV callback and extended put/get handlers. |
| 536 | */ |
Omair M Abdullah | f4be978 | 2015-11-09 23:20:01 +0530 | [diff] [blame] | 537 | k->info = snd_soc_bytes_info_ext; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 538 | k->tlv.c = snd_soc_bytes_tlv_callback; |
| 539 | |
| 540 | ext_ops = tplg->bytes_ext_ops; |
| 541 | num_ops = tplg->bytes_ext_ops_count; |
| 542 | for (i = 0; i < num_ops; i++) { |
| 543 | if (!sbe->put && ext_ops[i].id == be->ext_ops.put) |
| 544 | sbe->put = ext_ops[i].put; |
| 545 | if (!sbe->get && ext_ops[i].id == be->ext_ops.get) |
| 546 | sbe->get = ext_ops[i].get; |
| 547 | } |
| 548 | |
| 549 | if (sbe->put && sbe->get) |
| 550 | return 0; |
| 551 | else |
| 552 | return -EINVAL; |
| 553 | } |
| 554 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 555 | /* try and map vendor specific kcontrol handlers first */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 556 | ops = tplg->io_ops; |
| 557 | num_ops = tplg->io_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 558 | for (i = 0; i < num_ops; i++) { |
| 559 | |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 560 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 561 | k->put = ops[i].put; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 562 | if (k->get == NULL && ops[i].id == hdr->ops.get) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 563 | k->get = ops[i].get; |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 564 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
| 565 | k->info = ops[i].info; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 568 | /* vendor specific handlers found ? */ |
| 569 | if (k->put && k->get && k->info) |
| 570 | return 0; |
| 571 | |
| 572 | /* none found so try standard kcontrol handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 573 | ops = io_ops; |
| 574 | num_ops = ARRAY_SIZE(io_ops); |
Mengdong Lin | 88a17d8 | 2015-08-18 18:11:51 +0800 | [diff] [blame] | 575 | for (i = 0; i < num_ops; i++) { |
| 576 | |
| 577 | if (k->put == NULL && ops[i].id == hdr->ops.put) |
| 578 | k->put = ops[i].put; |
| 579 | if (k->get == NULL && ops[i].id == hdr->ops.get) |
| 580 | k->get = ops[i].get; |
| 581 | if (k->info == NULL && ops[i].id == hdr->ops.info) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 582 | k->info = ops[i].info; |
| 583 | } |
| 584 | |
| 585 | /* standard handlers found ? */ |
| 586 | if (k->put && k->get && k->info) |
| 587 | return 0; |
| 588 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 589 | /* nothing to bind */ |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | /* bind a widgets to it's evnt handlers */ |
| 594 | int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w, |
| 595 | const struct snd_soc_tplg_widget_events *events, |
| 596 | int num_events, u16 event_type) |
| 597 | { |
| 598 | int i; |
| 599 | |
| 600 | w->event = NULL; |
| 601 | |
| 602 | for (i = 0; i < num_events; i++) { |
| 603 | if (event_type == events[i].type) { |
| 604 | |
| 605 | /* found - so assign event */ |
| 606 | w->event = events[i].event_handler; |
| 607 | return 0; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /* not found */ |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event); |
| 615 | |
| 616 | /* optionally pass new dynamic kcontrol to component driver. */ |
| 617 | static int soc_tplg_init_kcontrol(struct soc_tplg *tplg, |
| 618 | struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr) |
| 619 | { |
| 620 | if (tplg->comp && tplg->ops && tplg->ops->control_load) |
| 621 | return tplg->ops->control_load(tplg->comp, k, hdr); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 626 | |
| 627 | static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg, |
| 628 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 629 | { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 630 | unsigned int item_len = 2 * sizeof(unsigned int); |
| 631 | unsigned int *p; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 632 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 633 | p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL); |
| 634 | if (!p) |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 635 | return -ENOMEM; |
| 636 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 637 | p[0] = SNDRV_CTL_TLVT_DB_SCALE; |
| 638 | p[1] = item_len; |
| 639 | p[2] = scale->min; |
| 640 | p[3] = (scale->step & TLV_DB_SCALE_MASK) |
| 641 | | (scale->mute ? TLV_DB_SCALE_MUTE : 0); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 642 | |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 643 | kc->tlv.p = (void *)p; |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int soc_tplg_create_tlv(struct soc_tplg *tplg, |
| 648 | struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc) |
| 649 | { |
| 650 | struct snd_soc_tplg_ctl_tlv *tplg_tlv; |
| 651 | |
| 652 | if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) |
| 653 | return 0; |
| 654 | |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 655 | if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) { |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 656 | tplg_tlv = &tc->tlv; |
| 657 | switch (tplg_tlv->type) { |
| 658 | case SNDRV_CTL_TLVT_DB_SCALE: |
| 659 | return soc_tplg_create_tlv_db_scale(tplg, kc, |
| 660 | &tplg_tlv->scale); |
| 661 | |
| 662 | /* TODO: add support for other TLV types */ |
| 663 | default: |
| 664 | dev_dbg(tplg->dev, "Unsupported TLV type %d\n", |
| 665 | tplg_tlv->type); |
| 666 | return -EINVAL; |
| 667 | } |
| 668 | } |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static inline void soc_tplg_free_tlv(struct soc_tplg *tplg, |
| 674 | struct snd_kcontrol_new *kc) |
| 675 | { |
| 676 | kfree(kc->tlv.p); |
| 677 | } |
| 678 | |
| 679 | static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count, |
| 680 | size_t size) |
| 681 | { |
| 682 | struct snd_soc_tplg_bytes_control *be; |
| 683 | struct soc_bytes_ext *sbe; |
| 684 | struct snd_kcontrol_new kc; |
| 685 | int i, err; |
| 686 | |
| 687 | if (soc_tplg_check_elem_count(tplg, |
| 688 | sizeof(struct snd_soc_tplg_bytes_control), count, |
| 689 | size, "mixer bytes")) { |
| 690 | dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n", |
| 691 | count); |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | for (i = 0; i < count; i++) { |
| 696 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 697 | |
| 698 | /* validate kcontrol */ |
| 699 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 700 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 704 | if (sbe == NULL) |
| 705 | return -ENOMEM; |
| 706 | |
| 707 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
| 708 | be->priv.size); |
| 709 | |
| 710 | dev_dbg(tplg->dev, |
| 711 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 712 | be->hdr.name, be->hdr.access); |
| 713 | |
| 714 | memset(&kc, 0, sizeof(kc)); |
| 715 | kc.name = be->hdr.name; |
| 716 | kc.private_value = (long)sbe; |
| 717 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 718 | kc.access = be->hdr.access; |
| 719 | |
| 720 | sbe->max = be->max; |
| 721 | sbe->dobj.type = SND_SOC_DOBJ_BYTES; |
| 722 | sbe->dobj.ops = tplg->ops; |
| 723 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 724 | |
| 725 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 726 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 727 | if (err) { |
| 728 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 729 | kfree(sbe); |
| 730 | continue; |
| 731 | } |
| 732 | |
| 733 | /* pass control to driver for optional further init */ |
| 734 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 735 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 736 | if (err < 0) { |
| 737 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 738 | be->hdr.name); |
| 739 | kfree(sbe); |
| 740 | continue; |
| 741 | } |
| 742 | |
| 743 | /* register control here */ |
| 744 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 745 | &sbe->dobj.control.kcontrol); |
| 746 | if (err < 0) { |
| 747 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 748 | be->hdr.name); |
| 749 | kfree(sbe); |
| 750 | continue; |
| 751 | } |
| 752 | |
| 753 | list_add(&sbe->dobj.list, &tplg->comp->dobj_list); |
| 754 | } |
| 755 | return 0; |
| 756 | |
| 757 | } |
| 758 | |
| 759 | static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count, |
| 760 | size_t size) |
| 761 | { |
| 762 | struct snd_soc_tplg_mixer_control *mc; |
| 763 | struct soc_mixer_control *sm; |
| 764 | struct snd_kcontrol_new kc; |
| 765 | int i, err; |
| 766 | |
| 767 | if (soc_tplg_check_elem_count(tplg, |
| 768 | sizeof(struct snd_soc_tplg_mixer_control), |
| 769 | count, size, "mixers")) { |
| 770 | |
| 771 | dev_err(tplg->dev, "ASoC: invalid count %d for controls\n", |
| 772 | count); |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | for (i = 0; i < count; i++) { |
| 777 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 778 | |
| 779 | /* validate kcontrol */ |
| 780 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 781 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 782 | return -EINVAL; |
| 783 | |
| 784 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 785 | if (sm == NULL) |
| 786 | return -ENOMEM; |
| 787 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
| 788 | mc->priv.size); |
| 789 | |
| 790 | dev_dbg(tplg->dev, |
| 791 | "ASoC: adding mixer kcontrol %s with access 0x%x\n", |
| 792 | mc->hdr.name, mc->hdr.access); |
| 793 | |
| 794 | memset(&kc, 0, sizeof(kc)); |
| 795 | kc.name = mc->hdr.name; |
| 796 | kc.private_value = (long)sm; |
| 797 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 798 | kc.access = mc->hdr.access; |
| 799 | |
| 800 | /* we only support FL/FR channel mapping atm */ |
| 801 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 802 | SNDRV_CHMAP_FL); |
| 803 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 804 | SNDRV_CHMAP_FR); |
| 805 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 806 | SNDRV_CHMAP_FL); |
| 807 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 808 | SNDRV_CHMAP_FR); |
| 809 | |
| 810 | sm->max = mc->max; |
| 811 | sm->min = mc->min; |
| 812 | sm->invert = mc->invert; |
| 813 | sm->platform_max = mc->platform_max; |
| 814 | sm->dobj.index = tplg->index; |
| 815 | sm->dobj.ops = tplg->ops; |
| 816 | sm->dobj.type = SND_SOC_DOBJ_MIXER; |
| 817 | INIT_LIST_HEAD(&sm->dobj.list); |
| 818 | |
| 819 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 820 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 821 | if (err) { |
| 822 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 823 | kfree(sm); |
| 824 | continue; |
| 825 | } |
| 826 | |
| 827 | /* pass control to driver for optional further init */ |
| 828 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 829 | (struct snd_soc_tplg_ctl_hdr *) mc); |
| 830 | if (err < 0) { |
| 831 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 832 | mc->hdr.name); |
| 833 | kfree(sm); |
| 834 | continue; |
| 835 | } |
| 836 | |
| 837 | /* create any TLV data */ |
Mengdong Lin | 28a87ee | 2015-08-05 14:41:13 +0100 | [diff] [blame] | 838 | soc_tplg_create_tlv(tplg, &kc, &mc->hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 839 | |
| 840 | /* register control here */ |
| 841 | err = soc_tplg_add_kcontrol(tplg, &kc, |
| 842 | &sm->dobj.control.kcontrol); |
| 843 | if (err < 0) { |
| 844 | dev_err(tplg->dev, "ASoC: failed to add %s\n", |
| 845 | mc->hdr.name); |
| 846 | soc_tplg_free_tlv(tplg, &kc); |
| 847 | kfree(sm); |
| 848 | continue; |
| 849 | } |
| 850 | |
| 851 | list_add(&sm->dobj.list, &tplg->comp->dobj_list); |
| 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | static int soc_tplg_denum_create_texts(struct soc_enum *se, |
| 858 | struct snd_soc_tplg_enum_control *ec) |
| 859 | { |
| 860 | int i, ret; |
| 861 | |
| 862 | se->dobj.control.dtexts = |
| 863 | kzalloc(sizeof(char *) * ec->items, GFP_KERNEL); |
| 864 | if (se->dobj.control.dtexts == NULL) |
| 865 | return -ENOMEM; |
| 866 | |
| 867 | for (i = 0; i < ec->items; i++) { |
| 868 | |
| 869 | if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 870 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) { |
| 871 | ret = -EINVAL; |
| 872 | goto err; |
| 873 | } |
| 874 | |
| 875 | se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL); |
| 876 | if (!se->dobj.control.dtexts[i]) { |
| 877 | ret = -ENOMEM; |
| 878 | goto err; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return 0; |
| 883 | |
| 884 | err: |
| 885 | for (--i; i >= 0; i--) |
| 886 | kfree(se->dobj.control.dtexts[i]); |
| 887 | kfree(se->dobj.control.dtexts); |
| 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | static int soc_tplg_denum_create_values(struct soc_enum *se, |
| 892 | struct snd_soc_tplg_enum_control *ec) |
| 893 | { |
| 894 | if (ec->items > sizeof(*ec->values)) |
| 895 | return -EINVAL; |
| 896 | |
Andrzej Hajda | 376c0af | 2015-08-07 09:59:37 +0200 | [diff] [blame] | 897 | se->dobj.control.dvalues = kmemdup(ec->values, |
| 898 | ec->items * sizeof(u32), |
| 899 | GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 900 | if (!se->dobj.control.dvalues) |
| 901 | return -ENOMEM; |
| 902 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count, |
| 907 | size_t size) |
| 908 | { |
| 909 | struct snd_soc_tplg_enum_control *ec; |
| 910 | struct soc_enum *se; |
| 911 | struct snd_kcontrol_new kc; |
| 912 | int i, ret, err; |
| 913 | |
| 914 | if (soc_tplg_check_elem_count(tplg, |
| 915 | sizeof(struct snd_soc_tplg_enum_control), |
| 916 | count, size, "enums")) { |
| 917 | |
| 918 | dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n", |
| 919 | count); |
| 920 | return -EINVAL; |
| 921 | } |
| 922 | |
| 923 | for (i = 0; i < count; i++) { |
| 924 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
| 925 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
| 926 | ec->priv.size); |
| 927 | |
| 928 | /* validate kcontrol */ |
| 929 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 930 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 931 | return -EINVAL; |
| 932 | |
| 933 | se = kzalloc((sizeof(*se)), GFP_KERNEL); |
| 934 | if (se == NULL) |
| 935 | return -ENOMEM; |
| 936 | |
| 937 | dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n", |
| 938 | ec->hdr.name, ec->items); |
| 939 | |
| 940 | memset(&kc, 0, sizeof(kc)); |
| 941 | kc.name = ec->hdr.name; |
| 942 | kc.private_value = (long)se; |
| 943 | kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 944 | kc.access = ec->hdr.access; |
| 945 | |
| 946 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 947 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, |
| 948 | SNDRV_CHMAP_FL); |
| 949 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, |
| 950 | SNDRV_CHMAP_FL); |
| 951 | |
| 952 | se->items = ec->items; |
| 953 | se->mask = ec->mask; |
| 954 | se->dobj.index = tplg->index; |
| 955 | se->dobj.type = SND_SOC_DOBJ_ENUM; |
| 956 | se->dobj.ops = tplg->ops; |
| 957 | INIT_LIST_HEAD(&se->dobj.list); |
| 958 | |
| 959 | switch (ec->hdr.ops.info) { |
| 960 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 961 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 962 | err = soc_tplg_denum_create_values(se, ec); |
| 963 | if (err < 0) { |
| 964 | dev_err(tplg->dev, |
| 965 | "ASoC: could not create values for %s\n", |
| 966 | ec->hdr.name); |
| 967 | kfree(se); |
| 968 | continue; |
| 969 | } |
| 970 | /* fall through and create texts */ |
| 971 | case SND_SOC_TPLG_CTL_ENUM: |
| 972 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 973 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 974 | err = soc_tplg_denum_create_texts(se, ec); |
| 975 | if (err < 0) { |
| 976 | dev_err(tplg->dev, |
| 977 | "ASoC: could not create texts for %s\n", |
| 978 | ec->hdr.name); |
| 979 | kfree(se); |
| 980 | continue; |
| 981 | } |
| 982 | break; |
| 983 | default: |
| 984 | dev_err(tplg->dev, |
| 985 | "ASoC: invalid enum control type %d for %s\n", |
| 986 | ec->hdr.ops.info, ec->hdr.name); |
| 987 | kfree(se); |
| 988 | continue; |
| 989 | } |
| 990 | |
| 991 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 992 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 993 | if (err) { |
| 994 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 995 | kfree(se); |
| 996 | continue; |
| 997 | } |
| 998 | |
| 999 | /* pass control to driver for optional further init */ |
| 1000 | err = soc_tplg_init_kcontrol(tplg, &kc, |
| 1001 | (struct snd_soc_tplg_ctl_hdr *) ec); |
| 1002 | if (err < 0) { |
| 1003 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1004 | ec->hdr.name); |
| 1005 | kfree(se); |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | /* register control here */ |
| 1010 | ret = soc_tplg_add_kcontrol(tplg, |
| 1011 | &kc, &se->dobj.control.kcontrol); |
| 1012 | if (ret < 0) { |
| 1013 | dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n", |
| 1014 | ec->hdr.name); |
| 1015 | kfree(se); |
| 1016 | continue; |
| 1017 | } |
| 1018 | |
| 1019 | list_add(&se->dobj.list, &tplg->comp->dobj_list); |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg, |
| 1026 | struct snd_soc_tplg_hdr *hdr) |
| 1027 | { |
| 1028 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1029 | int i; |
| 1030 | |
| 1031 | if (tplg->pass != SOC_TPLG_PASS_MIXER) { |
| 1032 | tplg->pos += hdr->size + hdr->payload_size; |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count, |
| 1037 | soc_tplg_get_offset(tplg)); |
| 1038 | |
| 1039 | for (i = 0; i < hdr->count; i++) { |
| 1040 | |
| 1041 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1042 | |
| 1043 | switch (control_hdr->ops.info) { |
| 1044 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1045 | case SND_SOC_TPLG_CTL_STROBE: |
| 1046 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1047 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1048 | case SND_SOC_TPLG_CTL_RANGE: |
| 1049 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
| 1050 | case SND_SOC_TPLG_DAPM_CTL_PIN: |
| 1051 | soc_tplg_dmixer_create(tplg, 1, hdr->payload_size); |
| 1052 | break; |
| 1053 | case SND_SOC_TPLG_CTL_ENUM: |
| 1054 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1055 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1056 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1057 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1058 | soc_tplg_denum_create(tplg, 1, hdr->payload_size); |
| 1059 | break; |
| 1060 | case SND_SOC_TPLG_CTL_BYTES: |
| 1061 | soc_tplg_dbytes_create(tplg, 1, hdr->payload_size); |
| 1062 | break; |
| 1063 | default: |
| 1064 | soc_bind_err(tplg, control_hdr, i); |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg, |
| 1073 | struct snd_soc_tplg_hdr *hdr) |
| 1074 | { |
| 1075 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
| 1076 | struct snd_soc_dapm_route route; |
| 1077 | struct snd_soc_tplg_dapm_graph_elem *elem; |
| 1078 | int count = hdr->count, i; |
| 1079 | |
| 1080 | if (tplg->pass != SOC_TPLG_PASS_GRAPH) { |
| 1081 | tplg->pos += hdr->size + hdr->payload_size; |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | if (soc_tplg_check_elem_count(tplg, |
| 1086 | sizeof(struct snd_soc_tplg_dapm_graph_elem), |
| 1087 | count, hdr->payload_size, "graph")) { |
| 1088 | |
| 1089 | dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n", |
| 1090 | count); |
| 1091 | return -EINVAL; |
| 1092 | } |
| 1093 | |
| 1094 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes\n", count); |
| 1095 | |
| 1096 | for (i = 0; i < count; i++) { |
| 1097 | elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos; |
| 1098 | tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem); |
| 1099 | |
| 1100 | /* validate routes */ |
| 1101 | if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1102 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1103 | return -EINVAL; |
| 1104 | if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1105 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1106 | return -EINVAL; |
| 1107 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1108 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1109 | return -EINVAL; |
| 1110 | |
| 1111 | route.source = elem->source; |
| 1112 | route.sink = elem->sink; |
| 1113 | route.connected = NULL; /* set to NULL atm for tplg users */ |
| 1114 | if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0) |
| 1115 | route.control = NULL; |
| 1116 | else |
| 1117 | route.control = elem->control; |
| 1118 | |
| 1119 | /* add route, but keep going if some fail */ |
| 1120 | snd_soc_dapm_add_routes(dapm, &route, 1); |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create( |
| 1127 | struct soc_tplg *tplg, int num_kcontrols) |
| 1128 | { |
| 1129 | struct snd_kcontrol_new *kc; |
| 1130 | struct soc_mixer_control *sm; |
| 1131 | struct snd_soc_tplg_mixer_control *mc; |
| 1132 | int i, err; |
| 1133 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1134 | kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1135 | if (kc == NULL) |
| 1136 | return NULL; |
| 1137 | |
| 1138 | for (i = 0; i < num_kcontrols; i++) { |
| 1139 | mc = (struct snd_soc_tplg_mixer_control *)tplg->pos; |
| 1140 | sm = kzalloc(sizeof(*sm), GFP_KERNEL); |
| 1141 | if (sm == NULL) |
| 1142 | goto err; |
| 1143 | |
| 1144 | tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) + |
| 1145 | mc->priv.size); |
| 1146 | |
| 1147 | /* validate kcontrol */ |
| 1148 | if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1149 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1150 | goto err_str; |
| 1151 | |
| 1152 | dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n", |
| 1153 | mc->hdr.name, i); |
| 1154 | |
| 1155 | kc[i].name = mc->hdr.name; |
| 1156 | kc[i].private_value = (long)sm; |
| 1157 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1158 | kc[i].access = mc->hdr.access; |
| 1159 | |
| 1160 | /* we only support FL/FR channel mapping atm */ |
| 1161 | sm->reg = tplc_chan_get_reg(tplg, mc->channel, |
| 1162 | SNDRV_CHMAP_FL); |
| 1163 | sm->rreg = tplc_chan_get_reg(tplg, mc->channel, |
| 1164 | SNDRV_CHMAP_FR); |
| 1165 | sm->shift = tplc_chan_get_shift(tplg, mc->channel, |
| 1166 | SNDRV_CHMAP_FL); |
| 1167 | sm->rshift = tplc_chan_get_shift(tplg, mc->channel, |
| 1168 | SNDRV_CHMAP_FR); |
| 1169 | |
| 1170 | sm->max = mc->max; |
| 1171 | sm->min = mc->min; |
| 1172 | sm->invert = mc->invert; |
| 1173 | sm->platform_max = mc->platform_max; |
| 1174 | sm->dobj.index = tplg->index; |
| 1175 | INIT_LIST_HEAD(&sm->dobj.list); |
| 1176 | |
| 1177 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1178 | err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1179 | if (err) { |
| 1180 | soc_control_err(tplg, &mc->hdr, mc->hdr.name); |
| 1181 | kfree(sm); |
| 1182 | continue; |
| 1183 | } |
| 1184 | |
| 1185 | /* pass control to driver for optional further init */ |
| 1186 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1187 | (struct snd_soc_tplg_ctl_hdr *)mc); |
| 1188 | if (err < 0) { |
| 1189 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1190 | mc->hdr.name); |
| 1191 | kfree(sm); |
| 1192 | continue; |
| 1193 | } |
| 1194 | } |
| 1195 | return kc; |
| 1196 | |
| 1197 | err_str: |
| 1198 | kfree(sm); |
| 1199 | err: |
| 1200 | for (--i; i >= 0; i--) |
| 1201 | kfree((void *)kc[i].private_value); |
| 1202 | kfree(kc); |
| 1203 | return NULL; |
| 1204 | } |
| 1205 | |
| 1206 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create( |
| 1207 | struct soc_tplg *tplg) |
| 1208 | { |
| 1209 | struct snd_kcontrol_new *kc; |
| 1210 | struct snd_soc_tplg_enum_control *ec; |
| 1211 | struct soc_enum *se; |
| 1212 | int i, err; |
| 1213 | |
| 1214 | ec = (struct snd_soc_tplg_enum_control *)tplg->pos; |
| 1215 | tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) + |
| 1216 | ec->priv.size); |
| 1217 | |
| 1218 | /* validate kcontrol */ |
| 1219 | if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1220 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1221 | return NULL; |
| 1222 | |
| 1223 | kc = kzalloc(sizeof(*kc), GFP_KERNEL); |
| 1224 | if (kc == NULL) |
| 1225 | return NULL; |
| 1226 | |
| 1227 | se = kzalloc(sizeof(*se), GFP_KERNEL); |
| 1228 | if (se == NULL) |
| 1229 | goto err; |
| 1230 | |
| 1231 | dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n", |
| 1232 | ec->hdr.name); |
| 1233 | |
| 1234 | kc->name = ec->hdr.name; |
| 1235 | kc->private_value = (long)se; |
| 1236 | kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1237 | kc->access = ec->hdr.access; |
| 1238 | |
| 1239 | /* we only support FL/FR channel mapping atm */ |
| 1240 | se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1241 | se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL); |
| 1242 | se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR); |
| 1243 | |
| 1244 | se->items = ec->items; |
| 1245 | se->mask = ec->mask; |
| 1246 | se->dobj.index = tplg->index; |
| 1247 | |
| 1248 | switch (ec->hdr.ops.info) { |
| 1249 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1250 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1251 | err = soc_tplg_denum_create_values(se, ec); |
| 1252 | if (err < 0) { |
| 1253 | dev_err(tplg->dev, "ASoC: could not create values for %s\n", |
| 1254 | ec->hdr.name); |
| 1255 | goto err_se; |
| 1256 | } |
| 1257 | /* fall through to create texts */ |
| 1258 | case SND_SOC_TPLG_CTL_ENUM: |
| 1259 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1260 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1261 | err = soc_tplg_denum_create_texts(se, ec); |
| 1262 | if (err < 0) { |
| 1263 | dev_err(tplg->dev, "ASoC: could not create texts for %s\n", |
| 1264 | ec->hdr.name); |
| 1265 | goto err_se; |
| 1266 | } |
| 1267 | break; |
| 1268 | default: |
| 1269 | dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n", |
| 1270 | ec->hdr.ops.info, ec->hdr.name); |
| 1271 | goto err_se; |
| 1272 | } |
| 1273 | |
| 1274 | /* map io handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1275 | err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1276 | if (err) { |
| 1277 | soc_control_err(tplg, &ec->hdr, ec->hdr.name); |
| 1278 | goto err_se; |
| 1279 | } |
| 1280 | |
| 1281 | /* pass control to driver for optional further init */ |
| 1282 | err = soc_tplg_init_kcontrol(tplg, kc, |
| 1283 | (struct snd_soc_tplg_ctl_hdr *)ec); |
| 1284 | if (err < 0) { |
| 1285 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1286 | ec->hdr.name); |
| 1287 | goto err_se; |
| 1288 | } |
| 1289 | |
| 1290 | return kc; |
| 1291 | |
| 1292 | err_se: |
| 1293 | /* free values and texts */ |
| 1294 | kfree(se->dobj.control.dvalues); |
| 1295 | for (i = 0; i < ec->items; i++) |
| 1296 | kfree(se->dobj.control.dtexts[i]); |
| 1297 | |
| 1298 | kfree(se); |
| 1299 | err: |
| 1300 | kfree(kc); |
| 1301 | |
| 1302 | return NULL; |
| 1303 | } |
| 1304 | |
| 1305 | static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create( |
| 1306 | struct soc_tplg *tplg, int count) |
| 1307 | { |
| 1308 | struct snd_soc_tplg_bytes_control *be; |
| 1309 | struct soc_bytes_ext *sbe; |
| 1310 | struct snd_kcontrol_new *kc; |
| 1311 | int i, err; |
| 1312 | |
Axel Lin | 4ca7deb | 2015-08-05 22:34:22 +0800 | [diff] [blame] | 1313 | kc = kcalloc(count, sizeof(*kc), GFP_KERNEL); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1314 | if (!kc) |
| 1315 | return NULL; |
| 1316 | |
| 1317 | for (i = 0; i < count; i++) { |
| 1318 | be = (struct snd_soc_tplg_bytes_control *)tplg->pos; |
| 1319 | |
| 1320 | /* validate kcontrol */ |
| 1321 | if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1322 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1323 | goto err; |
| 1324 | |
| 1325 | sbe = kzalloc(sizeof(*sbe), GFP_KERNEL); |
| 1326 | if (sbe == NULL) |
| 1327 | goto err; |
| 1328 | |
| 1329 | tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) + |
| 1330 | be->priv.size); |
| 1331 | |
| 1332 | dev_dbg(tplg->dev, |
| 1333 | "ASoC: adding bytes kcontrol %s with access 0x%x\n", |
| 1334 | be->hdr.name, be->hdr.access); |
| 1335 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1336 | kc[i].name = be->hdr.name; |
| 1337 | kc[i].private_value = (long)sbe; |
| 1338 | kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
| 1339 | kc[i].access = be->hdr.access; |
| 1340 | |
| 1341 | sbe->max = be->max; |
| 1342 | INIT_LIST_HEAD(&sbe->dobj.list); |
| 1343 | |
| 1344 | /* map standard io handlers and check for external handlers */ |
Mengdong Lin | 2b5cdb9 | 2015-08-18 18:12:01 +0800 | [diff] [blame] | 1345 | err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1346 | if (err) { |
| 1347 | soc_control_err(tplg, &be->hdr, be->hdr.name); |
| 1348 | kfree(sbe); |
| 1349 | continue; |
| 1350 | } |
| 1351 | |
| 1352 | /* pass control to driver for optional further init */ |
| 1353 | err = soc_tplg_init_kcontrol(tplg, &kc[i], |
| 1354 | (struct snd_soc_tplg_ctl_hdr *)be); |
| 1355 | if (err < 0) { |
| 1356 | dev_err(tplg->dev, "ASoC: failed to init %s\n", |
| 1357 | be->hdr.name); |
| 1358 | kfree(sbe); |
| 1359 | continue; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | return kc; |
| 1364 | |
| 1365 | err: |
| 1366 | for (--i; i >= 0; i--) |
| 1367 | kfree((void *)kc[i].private_value); |
| 1368 | |
| 1369 | kfree(kc); |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | |
| 1373 | static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg, |
| 1374 | struct snd_soc_tplg_dapm_widget *w) |
| 1375 | { |
| 1376 | struct snd_soc_dapm_context *dapm = &tplg->comp->dapm; |
| 1377 | struct snd_soc_dapm_widget template, *widget; |
| 1378 | struct snd_soc_tplg_ctl_hdr *control_hdr; |
| 1379 | struct snd_soc_card *card = tplg->comp->card; |
| 1380 | int ret = 0; |
| 1381 | |
| 1382 | if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1383 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1384 | return -EINVAL; |
| 1385 | if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == |
| 1386 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n", |
| 1390 | w->name, w->id); |
| 1391 | |
| 1392 | memset(&template, 0, sizeof(template)); |
| 1393 | |
| 1394 | /* map user to kernel widget ID */ |
| 1395 | template.id = get_widget_id(w->id); |
| 1396 | if (template.id < 0) |
| 1397 | return template.id; |
| 1398 | |
| 1399 | template.name = kstrdup(w->name, GFP_KERNEL); |
| 1400 | if (!template.name) |
| 1401 | return -ENOMEM; |
| 1402 | template.sname = kstrdup(w->sname, GFP_KERNEL); |
| 1403 | if (!template.sname) { |
| 1404 | ret = -ENOMEM; |
| 1405 | goto err; |
| 1406 | } |
| 1407 | template.reg = w->reg; |
| 1408 | template.shift = w->shift; |
| 1409 | template.mask = w->mask; |
Subhransu S. Prusty | 6dc6db7 | 2015-06-29 17:36:44 +0100 | [diff] [blame] | 1410 | template.subseq = w->subseq; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1411 | template.on_val = w->invert ? 0 : 1; |
| 1412 | template.off_val = w->invert ? 1 : 0; |
| 1413 | template.ignore_suspend = w->ignore_suspend; |
| 1414 | template.event_flags = w->event_flags; |
| 1415 | template.dobj.index = tplg->index; |
| 1416 | |
| 1417 | tplg->pos += |
| 1418 | (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size); |
| 1419 | if (w->num_kcontrols == 0) { |
| 1420 | template.num_kcontrols = 0; |
| 1421 | goto widget; |
| 1422 | } |
| 1423 | |
| 1424 | control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos; |
| 1425 | dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n", |
| 1426 | w->name, w->num_kcontrols, control_hdr->type); |
| 1427 | |
| 1428 | switch (control_hdr->ops.info) { |
| 1429 | case SND_SOC_TPLG_CTL_VOLSW: |
| 1430 | case SND_SOC_TPLG_CTL_STROBE: |
| 1431 | case SND_SOC_TPLG_CTL_VOLSW_SX: |
| 1432 | case SND_SOC_TPLG_CTL_VOLSW_XR_SX: |
| 1433 | case SND_SOC_TPLG_CTL_RANGE: |
| 1434 | case SND_SOC_TPLG_DAPM_CTL_VOLSW: |
| 1435 | template.num_kcontrols = w->num_kcontrols; |
| 1436 | template.kcontrol_news = |
| 1437 | soc_tplg_dapm_widget_dmixer_create(tplg, |
| 1438 | template.num_kcontrols); |
| 1439 | if (!template.kcontrol_news) { |
| 1440 | ret = -ENOMEM; |
| 1441 | goto hdr_err; |
| 1442 | } |
| 1443 | break; |
| 1444 | case SND_SOC_TPLG_CTL_ENUM: |
| 1445 | case SND_SOC_TPLG_CTL_ENUM_VALUE: |
| 1446 | case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE: |
| 1447 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT: |
| 1448 | case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE: |
| 1449 | template.dobj.widget.kcontrol_enum = 1; |
| 1450 | template.num_kcontrols = 1; |
| 1451 | template.kcontrol_news = |
| 1452 | soc_tplg_dapm_widget_denum_create(tplg); |
| 1453 | if (!template.kcontrol_news) { |
| 1454 | ret = -ENOMEM; |
| 1455 | goto hdr_err; |
| 1456 | } |
| 1457 | break; |
| 1458 | case SND_SOC_TPLG_CTL_BYTES: |
| 1459 | template.num_kcontrols = w->num_kcontrols; |
| 1460 | template.kcontrol_news = |
| 1461 | soc_tplg_dapm_widget_dbytes_create(tplg, |
| 1462 | template.num_kcontrols); |
| 1463 | if (!template.kcontrol_news) { |
| 1464 | ret = -ENOMEM; |
| 1465 | goto hdr_err; |
| 1466 | } |
| 1467 | break; |
| 1468 | default: |
| 1469 | dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n", |
| 1470 | control_hdr->ops.get, control_hdr->ops.put, |
| 1471 | control_hdr->ops.info); |
| 1472 | ret = -EINVAL; |
| 1473 | goto hdr_err; |
| 1474 | } |
| 1475 | |
| 1476 | widget: |
| 1477 | ret = soc_tplg_widget_load(tplg, &template, w); |
| 1478 | if (ret < 0) |
| 1479 | goto hdr_err; |
| 1480 | |
| 1481 | /* card dapm mutex is held by the core if we are loading topology |
| 1482 | * data during sound card init. */ |
| 1483 | if (card->instantiated) |
| 1484 | widget = snd_soc_dapm_new_control(dapm, &template); |
| 1485 | else |
| 1486 | widget = snd_soc_dapm_new_control_unlocked(dapm, &template); |
| 1487 | if (widget == NULL) { |
| 1488 | dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n", |
| 1489 | w->name); |
| 1490 | goto hdr_err; |
| 1491 | } |
| 1492 | |
| 1493 | widget->dobj.type = SND_SOC_DOBJ_WIDGET; |
| 1494 | widget->dobj.ops = tplg->ops; |
| 1495 | widget->dobj.index = tplg->index; |
| 1496 | list_add(&widget->dobj.list, &tplg->comp->dobj_list); |
| 1497 | return 0; |
| 1498 | |
| 1499 | hdr_err: |
| 1500 | kfree(template.sname); |
| 1501 | err: |
| 1502 | kfree(template.name); |
| 1503 | return ret; |
| 1504 | } |
| 1505 | |
| 1506 | static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg, |
| 1507 | struct snd_soc_tplg_hdr *hdr) |
| 1508 | { |
| 1509 | struct snd_soc_tplg_dapm_widget *widget; |
| 1510 | int ret, count = hdr->count, i; |
| 1511 | |
| 1512 | if (tplg->pass != SOC_TPLG_PASS_WIDGET) |
| 1513 | return 0; |
| 1514 | |
| 1515 | dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count); |
| 1516 | |
| 1517 | for (i = 0; i < count; i++) { |
| 1518 | widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos; |
| 1519 | ret = soc_tplg_dapm_widget_create(tplg, widget); |
| 1520 | if (ret < 0) |
| 1521 | dev_err(tplg->dev, "ASoC: failed to load widget %s\n", |
| 1522 | widget->name); |
| 1523 | } |
| 1524 | |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int soc_tplg_dapm_complete(struct soc_tplg *tplg) |
| 1529 | { |
| 1530 | struct snd_soc_card *card = tplg->comp->card; |
| 1531 | int ret; |
| 1532 | |
| 1533 | /* Card might not have been registered at this point. |
| 1534 | * If so, just return success. |
| 1535 | */ |
| 1536 | if (!card || !card->instantiated) { |
| 1537 | dev_warn(tplg->dev, "ASoC: Parent card not yet available," |
| 1538 | "Do not add new widgets now\n"); |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | ret = snd_soc_dapm_new_widgets(card); |
| 1543 | if (ret < 0) |
| 1544 | dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n", |
| 1545 | ret); |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1550 | static int soc_tplg_dai_create(struct soc_tplg *tplg, |
| 1551 | struct snd_soc_tplg_pcm *pcm) |
| 1552 | { |
| 1553 | struct snd_soc_dai_driver *dai_drv; |
| 1554 | struct snd_soc_pcm_stream *stream; |
| 1555 | struct snd_soc_tplg_stream_caps *caps; |
| 1556 | int ret; |
| 1557 | |
| 1558 | dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL); |
| 1559 | if (dai_drv == NULL) |
| 1560 | return -ENOMEM; |
| 1561 | |
| 1562 | dai_drv->name = pcm->dai_name; |
| 1563 | dai_drv->id = pcm->dai_id; |
| 1564 | |
| 1565 | if (pcm->playback) { |
| 1566 | stream = &dai_drv->playback; |
| 1567 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK]; |
| 1568 | |
| 1569 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); |
| 1570 | stream->channels_min = caps->channels_min; |
| 1571 | stream->channels_max = caps->channels_max; |
| 1572 | stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min, |
| 1573 | caps->rate_max); |
| 1574 | stream->formats = caps->formats; |
| 1575 | } |
| 1576 | |
| 1577 | if (pcm->capture) { |
| 1578 | stream = &dai_drv->capture; |
| 1579 | caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE]; |
| 1580 | |
| 1581 | stream->stream_name = kstrdup(caps->name, GFP_KERNEL); |
| 1582 | stream->channels_min = caps->channels_min; |
| 1583 | stream->channels_max = caps->channels_max; |
| 1584 | stream->rates = snd_pcm_rate_range_to_bits(caps->rate_min, |
| 1585 | caps->rate_max); |
| 1586 | stream->formats = caps->formats; |
| 1587 | } |
| 1588 | |
| 1589 | /* pass control to component driver for optional further init */ |
| 1590 | ret = soc_tplg_dai_load(tplg, dai_drv); |
| 1591 | if (ret < 0) { |
| 1592 | dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n"); |
| 1593 | kfree(dai_drv); |
| 1594 | return ret; |
| 1595 | } |
| 1596 | |
| 1597 | dai_drv->dobj.index = tplg->index; |
| 1598 | dai_drv->dobj.ops = tplg->ops; |
| 1599 | dai_drv->dobj.type = SND_SOC_DOBJ_PCM; |
| 1600 | list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list); |
| 1601 | |
| 1602 | /* register the DAI to the component */ |
| 1603 | return snd_soc_register_dai(tplg->comp, dai_drv); |
| 1604 | } |
| 1605 | |
| 1606 | static int soc_tplg_pcm_create(struct soc_tplg *tplg, |
| 1607 | struct snd_soc_tplg_pcm *pcm) |
| 1608 | { |
| 1609 | return soc_tplg_dai_create(tplg, pcm); |
| 1610 | } |
| 1611 | |
| 1612 | static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1613 | struct snd_soc_tplg_hdr *hdr) |
| 1614 | { |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1615 | struct snd_soc_tplg_pcm *pcm; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1616 | int count = hdr->count; |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1617 | int i; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1618 | |
| 1619 | if (tplg->pass != SOC_TPLG_PASS_PCM_DAI) |
| 1620 | return 0; |
| 1621 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1622 | pcm = (struct snd_soc_tplg_pcm *)tplg->pos; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1623 | |
| 1624 | if (soc_tplg_check_elem_count(tplg, |
Vedang Patel | 5b2688a | 2015-09-30 17:28:47 +0800 | [diff] [blame] | 1625 | sizeof(struct snd_soc_tplg_pcm), count, |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1626 | hdr->payload_size, "PCM DAI")) { |
| 1627 | dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n", |
| 1628 | count); |
| 1629 | return -EINVAL; |
| 1630 | } |
| 1631 | |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1632 | /* create the FE DAIs and DAI links */ |
| 1633 | for (i = 0; i < count; i++) { |
| 1634 | soc_tplg_pcm_create(tplg, pcm); |
| 1635 | pcm++; |
| 1636 | } |
| 1637 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1638 | dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count); |
Vedang Patel | 5b2688a | 2015-09-30 17:28:47 +0800 | [diff] [blame] | 1639 | tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1640 | |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1641 | return 0; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | static int soc_tplg_manifest_load(struct soc_tplg *tplg, |
| 1645 | struct snd_soc_tplg_hdr *hdr) |
| 1646 | { |
| 1647 | struct snd_soc_tplg_manifest *manifest; |
| 1648 | |
| 1649 | if (tplg->pass != SOC_TPLG_PASS_MANIFEST) |
| 1650 | return 0; |
| 1651 | |
| 1652 | manifest = (struct snd_soc_tplg_manifest *)tplg->pos; |
| 1653 | tplg->pos += sizeof(struct snd_soc_tplg_manifest); |
| 1654 | |
| 1655 | if (tplg->comp && tplg->ops && tplg->ops->manifest) |
| 1656 | return tplg->ops->manifest(tplg->comp, manifest); |
| 1657 | |
| 1658 | dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n"); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /* validate header magic, size and type */ |
| 1663 | static int soc_valid_header(struct soc_tplg *tplg, |
| 1664 | struct snd_soc_tplg_hdr *hdr) |
| 1665 | { |
| 1666 | if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size) |
| 1667 | return 0; |
| 1668 | |
| 1669 | /* big endian firmware objects not supported atm */ |
| 1670 | if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) { |
| 1671 | dev_err(tplg->dev, |
| 1672 | "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n", |
| 1673 | tplg->pass, hdr->magic, |
| 1674 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 1675 | return -EINVAL; |
| 1676 | } |
| 1677 | |
| 1678 | if (hdr->magic != SND_SOC_TPLG_MAGIC) { |
| 1679 | dev_err(tplg->dev, |
| 1680 | "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n", |
| 1681 | tplg->pass, hdr->magic, |
| 1682 | soc_tplg_get_hdr_offset(tplg), tplg->fw->size); |
| 1683 | return -EINVAL; |
| 1684 | } |
| 1685 | |
| 1686 | if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) { |
| 1687 | dev_err(tplg->dev, |
| 1688 | "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n", |
| 1689 | tplg->pass, hdr->abi, |
| 1690 | SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg), |
| 1691 | tplg->fw->size); |
| 1692 | return -EINVAL; |
| 1693 | } |
| 1694 | |
| 1695 | if (hdr->payload_size == 0) { |
| 1696 | dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n", |
| 1697 | soc_tplg_get_hdr_offset(tplg)); |
| 1698 | return -EINVAL; |
| 1699 | } |
| 1700 | |
| 1701 | if (tplg->pass == hdr->type) |
| 1702 | dev_dbg(tplg->dev, |
| 1703 | "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n", |
| 1704 | hdr->payload_size, hdr->type, hdr->version, |
| 1705 | hdr->vendor_type, tplg->pass); |
| 1706 | |
| 1707 | return 1; |
| 1708 | } |
| 1709 | |
| 1710 | /* check header type and call appropriate handler */ |
| 1711 | static int soc_tplg_load_header(struct soc_tplg *tplg, |
| 1712 | struct snd_soc_tplg_hdr *hdr) |
| 1713 | { |
| 1714 | tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr); |
| 1715 | |
| 1716 | /* check for matching ID */ |
| 1717 | if (hdr->index != tplg->req_index && |
| 1718 | hdr->index != SND_SOC_TPLG_INDEX_ALL) |
| 1719 | return 0; |
| 1720 | |
| 1721 | tplg->index = hdr->index; |
| 1722 | |
| 1723 | switch (hdr->type) { |
| 1724 | case SND_SOC_TPLG_TYPE_MIXER: |
| 1725 | case SND_SOC_TPLG_TYPE_ENUM: |
| 1726 | case SND_SOC_TPLG_TYPE_BYTES: |
| 1727 | return soc_tplg_kcontrol_elems_load(tplg, hdr); |
| 1728 | case SND_SOC_TPLG_TYPE_DAPM_GRAPH: |
| 1729 | return soc_tplg_dapm_graph_elems_load(tplg, hdr); |
| 1730 | case SND_SOC_TPLG_TYPE_DAPM_WIDGET: |
| 1731 | return soc_tplg_dapm_widget_elems_load(tplg, hdr); |
| 1732 | case SND_SOC_TPLG_TYPE_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1733 | return soc_tplg_pcm_elems_load(tplg, hdr); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1734 | case SND_SOC_TPLG_TYPE_MANIFEST: |
| 1735 | return soc_tplg_manifest_load(tplg, hdr); |
| 1736 | default: |
| 1737 | /* bespoke vendor data object */ |
| 1738 | return soc_tplg_vendor_load(tplg, hdr); |
| 1739 | } |
| 1740 | |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | /* process the topology file headers */ |
| 1745 | static int soc_tplg_process_headers(struct soc_tplg *tplg) |
| 1746 | { |
| 1747 | struct snd_soc_tplg_hdr *hdr; |
| 1748 | int ret; |
| 1749 | |
| 1750 | tplg->pass = SOC_TPLG_PASS_START; |
| 1751 | |
| 1752 | /* process the header types from start to end */ |
| 1753 | while (tplg->pass <= SOC_TPLG_PASS_END) { |
| 1754 | |
| 1755 | tplg->hdr_pos = tplg->fw->data; |
| 1756 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 1757 | |
| 1758 | while (!soc_tplg_is_eof(tplg)) { |
| 1759 | |
| 1760 | /* make sure header is valid before loading */ |
| 1761 | ret = soc_valid_header(tplg, hdr); |
| 1762 | if (ret < 0) |
| 1763 | return ret; |
| 1764 | else if (ret == 0) |
| 1765 | break; |
| 1766 | |
| 1767 | /* load the header object */ |
| 1768 | ret = soc_tplg_load_header(tplg, hdr); |
| 1769 | if (ret < 0) |
| 1770 | return ret; |
| 1771 | |
| 1772 | /* goto next header */ |
| 1773 | tplg->hdr_pos += hdr->payload_size + |
| 1774 | sizeof(struct snd_soc_tplg_hdr); |
| 1775 | hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos; |
| 1776 | } |
| 1777 | |
| 1778 | /* next data type pass */ |
| 1779 | tplg->pass++; |
| 1780 | } |
| 1781 | |
| 1782 | /* signal DAPM we are complete */ |
| 1783 | ret = soc_tplg_dapm_complete(tplg); |
| 1784 | if (ret < 0) |
| 1785 | dev_err(tplg->dev, |
| 1786 | "ASoC: failed to initialise DAPM from Firmware\n"); |
| 1787 | |
| 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | static int soc_tplg_load(struct soc_tplg *tplg) |
| 1792 | { |
| 1793 | int ret; |
| 1794 | |
| 1795 | ret = soc_tplg_process_headers(tplg); |
| 1796 | if (ret == 0) |
| 1797 | soc_tplg_complete(tplg); |
| 1798 | |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | /* load audio component topology from "firmware" file */ |
| 1803 | int snd_soc_tplg_component_load(struct snd_soc_component *comp, |
| 1804 | struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id) |
| 1805 | { |
| 1806 | struct soc_tplg tplg; |
| 1807 | |
| 1808 | /* setup parsing context */ |
| 1809 | memset(&tplg, 0, sizeof(tplg)); |
| 1810 | tplg.fw = fw; |
| 1811 | tplg.dev = comp->dev; |
| 1812 | tplg.comp = comp; |
| 1813 | tplg.ops = ops; |
| 1814 | tplg.req_index = id; |
| 1815 | tplg.io_ops = ops->io_ops; |
| 1816 | tplg.io_ops_count = ops->io_ops_count; |
Mengdong Lin | 1a3232d | 2015-08-18 18:12:20 +0800 | [diff] [blame] | 1817 | tplg.bytes_ext_ops = ops->bytes_ext_ops; |
| 1818 | tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1819 | |
| 1820 | return soc_tplg_load(&tplg); |
| 1821 | } |
| 1822 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load); |
| 1823 | |
| 1824 | /* remove this dynamic widget */ |
| 1825 | void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w) |
| 1826 | { |
| 1827 | /* make sure we are a widget */ |
| 1828 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET) |
| 1829 | return; |
| 1830 | |
| 1831 | remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET); |
| 1832 | } |
| 1833 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove); |
| 1834 | |
| 1835 | /* remove all dynamic widgets from this DAPM context */ |
| 1836 | void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm, |
| 1837 | u32 index) |
| 1838 | { |
| 1839 | struct snd_soc_dapm_widget *w, *next_w; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1840 | |
| 1841 | list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) { |
| 1842 | |
| 1843 | /* make sure we are a widget with correct context */ |
| 1844 | if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm) |
| 1845 | continue; |
| 1846 | |
| 1847 | /* match ID */ |
| 1848 | if (w->dobj.index != index && |
| 1849 | w->dobj.index != SND_SOC_TPLG_INDEX_ALL) |
| 1850 | continue; |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1851 | /* check and free and dynamic widget kcontrols */ |
| 1852 | snd_soc_tplg_widget_remove(w); |
Lars-Peter Clausen | b97e269 | 2015-07-21 18:11:07 +0200 | [diff] [blame] | 1853 | snd_soc_dapm_free_widget(w); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1854 | } |
Jyri Sarha | fd589a1 | 2015-11-10 18:12:42 +0200 | [diff] [blame] | 1855 | snd_soc_dapm_reset_cache(dapm); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1856 | } |
| 1857 | EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all); |
| 1858 | |
| 1859 | /* remove dynamic controls from the component driver */ |
| 1860 | int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index) |
| 1861 | { |
| 1862 | struct snd_soc_dobj *dobj, *next_dobj; |
| 1863 | int pass = SOC_TPLG_PASS_END; |
| 1864 | |
| 1865 | /* process the header types from end to start */ |
| 1866 | while (pass >= SOC_TPLG_PASS_START) { |
| 1867 | |
| 1868 | /* remove mixer controls */ |
| 1869 | list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list, |
| 1870 | list) { |
| 1871 | |
| 1872 | /* match index */ |
| 1873 | if (dobj->index != index && |
| 1874 | dobj->index != SND_SOC_TPLG_INDEX_ALL) |
| 1875 | continue; |
| 1876 | |
| 1877 | switch (dobj->type) { |
| 1878 | case SND_SOC_DOBJ_MIXER: |
| 1879 | remove_mixer(comp, dobj, pass); |
| 1880 | break; |
| 1881 | case SND_SOC_DOBJ_ENUM: |
| 1882 | remove_enum(comp, dobj, pass); |
| 1883 | break; |
| 1884 | case SND_SOC_DOBJ_BYTES: |
| 1885 | remove_bytes(comp, dobj, pass); |
| 1886 | break; |
| 1887 | case SND_SOC_DOBJ_WIDGET: |
| 1888 | remove_widget(comp, dobj, pass); |
| 1889 | break; |
| 1890 | case SND_SOC_DOBJ_PCM: |
Mengdong Lin | 64527e8 | 2016-01-15 16:13:28 +0800 | [diff] [blame^] | 1891 | remove_dai(comp, dobj, pass); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 1892 | break; |
| 1893 | default: |
| 1894 | dev_err(comp->dev, "ASoC: invalid component type %d for removal\n", |
| 1895 | dobj->type); |
| 1896 | break; |
| 1897 | } |
| 1898 | } |
| 1899 | pass--; |
| 1900 | } |
| 1901 | |
| 1902 | /* let caller know if FW can be freed when no objects are left */ |
| 1903 | return !list_empty(&comp->dobj_list); |
| 1904 | } |
| 1905 | EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove); |