soc-topology.c 54 KB

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