soc-topology.c 47 KB

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