soc-topology.c 49 KB

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