soc-topology.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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. }
  988. return kc;
  989. err_str:
  990. kfree(sm);
  991. err:
  992. for (--i; i >= 0; i--)
  993. kfree((void *)kc[i].private_value);
  994. kfree(kc);
  995. return NULL;
  996. }
  997. static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
  998. struct soc_tplg *tplg)
  999. {
  1000. struct snd_kcontrol_new *kc;
  1001. struct snd_soc_tplg_enum_control *ec;
  1002. struct soc_enum *se;
  1003. int i, err;
  1004. ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
  1005. tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
  1006. ec->priv.size);
  1007. /* validate kcontrol */
  1008. if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1009. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1010. return NULL;
  1011. kc = kzalloc(sizeof(*kc), GFP_KERNEL);
  1012. if (kc == NULL)
  1013. return NULL;
  1014. se = kzalloc(sizeof(*se), GFP_KERNEL);
  1015. if (se == NULL)
  1016. goto err;
  1017. dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
  1018. ec->hdr.name);
  1019. kc->name = ec->hdr.name;
  1020. kc->private_value = (long)se;
  1021. kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1022. kc->access = ec->hdr.access;
  1023. /* we only support FL/FR channel mapping atm */
  1024. se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
  1025. se->shift_l = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FL);
  1026. se->shift_r = tplc_chan_get_shift(tplg, ec->channel, SNDRV_CHMAP_FR);
  1027. se->items = ec->items;
  1028. se->mask = ec->mask;
  1029. se->dobj.index = tplg->index;
  1030. switch (ec->hdr.ops.info) {
  1031. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  1032. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  1033. err = soc_tplg_denum_create_values(se, ec);
  1034. if (err < 0) {
  1035. dev_err(tplg->dev, "ASoC: could not create values for %s\n",
  1036. ec->hdr.name);
  1037. goto err_se;
  1038. }
  1039. /* fall through to create texts */
  1040. case SND_SOC_TPLG_CTL_ENUM:
  1041. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  1042. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  1043. err = soc_tplg_denum_create_texts(se, ec);
  1044. if (err < 0) {
  1045. dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
  1046. ec->hdr.name);
  1047. goto err_se;
  1048. }
  1049. break;
  1050. default:
  1051. dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
  1052. ec->hdr.ops.info, ec->hdr.name);
  1053. goto err_se;
  1054. }
  1055. /* map io handlers */
  1056. err = soc_tplg_kcontrol_bind_io(&ec->hdr, kc, tplg);
  1057. if (err) {
  1058. soc_control_err(tplg, &ec->hdr, ec->hdr.name);
  1059. goto err_se;
  1060. }
  1061. /* pass control to driver for optional further init */
  1062. err = soc_tplg_init_kcontrol(tplg, kc,
  1063. (struct snd_soc_tplg_ctl_hdr *)ec);
  1064. if (err < 0) {
  1065. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  1066. ec->hdr.name);
  1067. goto err_se;
  1068. }
  1069. return kc;
  1070. err_se:
  1071. /* free values and texts */
  1072. kfree(se->dobj.control.dvalues);
  1073. for (i = 0; i < ec->items; i++)
  1074. kfree(se->dobj.control.dtexts[i]);
  1075. kfree(se);
  1076. err:
  1077. kfree(kc);
  1078. return NULL;
  1079. }
  1080. static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
  1081. struct soc_tplg *tplg, int count)
  1082. {
  1083. struct snd_soc_tplg_bytes_control *be;
  1084. struct soc_bytes_ext *sbe;
  1085. struct snd_kcontrol_new *kc;
  1086. int i, err;
  1087. kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
  1088. if (!kc)
  1089. return NULL;
  1090. for (i = 0; i < count; i++) {
  1091. be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
  1092. /* validate kcontrol */
  1093. if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1094. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1095. goto err;
  1096. sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
  1097. if (sbe == NULL)
  1098. goto err;
  1099. tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
  1100. be->priv.size);
  1101. dev_dbg(tplg->dev,
  1102. "ASoC: adding bytes kcontrol %s with access 0x%x\n",
  1103. be->hdr.name, be->hdr.access);
  1104. kc[i].name = be->hdr.name;
  1105. kc[i].private_value = (long)sbe;
  1106. kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1107. kc[i].access = be->hdr.access;
  1108. sbe->max = be->max;
  1109. INIT_LIST_HEAD(&sbe->dobj.list);
  1110. /* map standard io handlers and check for external handlers */
  1111. err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
  1112. if (err) {
  1113. soc_control_err(tplg, &be->hdr, be->hdr.name);
  1114. kfree(sbe);
  1115. continue;
  1116. }
  1117. /* pass control to driver for optional further init */
  1118. err = soc_tplg_init_kcontrol(tplg, &kc[i],
  1119. (struct snd_soc_tplg_ctl_hdr *)be);
  1120. if (err < 0) {
  1121. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  1122. be->hdr.name);
  1123. kfree(sbe);
  1124. continue;
  1125. }
  1126. }
  1127. return kc;
  1128. err:
  1129. for (--i; i >= 0; i--)
  1130. kfree((void *)kc[i].private_value);
  1131. kfree(kc);
  1132. return NULL;
  1133. }
  1134. static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
  1135. struct snd_soc_tplg_dapm_widget *w)
  1136. {
  1137. struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
  1138. struct snd_soc_dapm_widget template, *widget;
  1139. struct snd_soc_tplg_ctl_hdr *control_hdr;
  1140. struct snd_soc_card *card = tplg->comp->card;
  1141. int ret = 0;
  1142. if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1143. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1144. return -EINVAL;
  1145. if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1146. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1147. return -EINVAL;
  1148. dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
  1149. w->name, w->id);
  1150. memset(&template, 0, sizeof(template));
  1151. /* map user to kernel widget ID */
  1152. template.id = get_widget_id(w->id);
  1153. if (template.id < 0)
  1154. return template.id;
  1155. template.name = kstrdup(w->name, GFP_KERNEL);
  1156. if (!template.name)
  1157. return -ENOMEM;
  1158. template.sname = kstrdup(w->sname, GFP_KERNEL);
  1159. if (!template.sname) {
  1160. ret = -ENOMEM;
  1161. goto err;
  1162. }
  1163. template.reg = w->reg;
  1164. template.shift = w->shift;
  1165. template.mask = w->mask;
  1166. template.subseq = w->subseq;
  1167. template.on_val = w->invert ? 0 : 1;
  1168. template.off_val = w->invert ? 1 : 0;
  1169. template.ignore_suspend = w->ignore_suspend;
  1170. template.event_flags = w->event_flags;
  1171. template.dobj.index = tplg->index;
  1172. tplg->pos +=
  1173. (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
  1174. if (w->num_kcontrols == 0) {
  1175. template.num_kcontrols = 0;
  1176. goto widget;
  1177. }
  1178. control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
  1179. dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
  1180. w->name, w->num_kcontrols, control_hdr->type);
  1181. switch (control_hdr->ops.info) {
  1182. case SND_SOC_TPLG_CTL_VOLSW:
  1183. case SND_SOC_TPLG_CTL_STROBE:
  1184. case SND_SOC_TPLG_CTL_VOLSW_SX:
  1185. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  1186. case SND_SOC_TPLG_CTL_RANGE:
  1187. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  1188. template.num_kcontrols = w->num_kcontrols;
  1189. template.kcontrol_news =
  1190. soc_tplg_dapm_widget_dmixer_create(tplg,
  1191. template.num_kcontrols);
  1192. if (!template.kcontrol_news) {
  1193. ret = -ENOMEM;
  1194. goto hdr_err;
  1195. }
  1196. break;
  1197. case SND_SOC_TPLG_CTL_ENUM:
  1198. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  1199. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  1200. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  1201. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  1202. template.dobj.widget.kcontrol_enum = 1;
  1203. template.num_kcontrols = 1;
  1204. template.kcontrol_news =
  1205. soc_tplg_dapm_widget_denum_create(tplg);
  1206. if (!template.kcontrol_news) {
  1207. ret = -ENOMEM;
  1208. goto hdr_err;
  1209. }
  1210. break;
  1211. case SND_SOC_TPLG_CTL_BYTES:
  1212. template.num_kcontrols = w->num_kcontrols;
  1213. template.kcontrol_news =
  1214. soc_tplg_dapm_widget_dbytes_create(tplg,
  1215. template.num_kcontrols);
  1216. if (!template.kcontrol_news) {
  1217. ret = -ENOMEM;
  1218. goto hdr_err;
  1219. }
  1220. break;
  1221. default:
  1222. dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
  1223. control_hdr->ops.get, control_hdr->ops.put,
  1224. control_hdr->ops.info);
  1225. ret = -EINVAL;
  1226. goto hdr_err;
  1227. }
  1228. widget:
  1229. ret = soc_tplg_widget_load(tplg, &template, w);
  1230. if (ret < 0)
  1231. goto hdr_err;
  1232. /* card dapm mutex is held by the core if we are loading topology
  1233. * data during sound card init. */
  1234. if (card->instantiated)
  1235. widget = snd_soc_dapm_new_control(dapm, &template);
  1236. else
  1237. widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
  1238. if (IS_ERR(widget)) {
  1239. ret = PTR_ERR(widget);
  1240. /* Do not nag about probe deferrals */
  1241. if (ret != -EPROBE_DEFER)
  1242. dev_err(tplg->dev,
  1243. "ASoC: failed to create widget %s controls (%d)\n",
  1244. w->name, ret);
  1245. goto hdr_err;
  1246. }
  1247. if (widget == NULL) {
  1248. dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
  1249. w->name);
  1250. ret = -ENOMEM;
  1251. goto hdr_err;
  1252. }
  1253. widget->dobj.type = SND_SOC_DOBJ_WIDGET;
  1254. widget->dobj.ops = tplg->ops;
  1255. widget->dobj.index = tplg->index;
  1256. kfree(template.sname);
  1257. kfree(template.name);
  1258. list_add(&widget->dobj.list, &tplg->comp->dobj_list);
  1259. return 0;
  1260. hdr_err:
  1261. kfree(template.sname);
  1262. err:
  1263. kfree(template.name);
  1264. return ret;
  1265. }
  1266. static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
  1267. struct snd_soc_tplg_hdr *hdr)
  1268. {
  1269. struct snd_soc_tplg_dapm_widget *widget;
  1270. int ret, count = hdr->count, i;
  1271. if (tplg->pass != SOC_TPLG_PASS_WIDGET)
  1272. return 0;
  1273. dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
  1274. for (i = 0; i < count; i++) {
  1275. widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
  1276. if (widget->size != sizeof(*widget)) {
  1277. dev_err(tplg->dev, "ASoC: invalid widget size\n");
  1278. return -EINVAL;
  1279. }
  1280. ret = soc_tplg_dapm_widget_create(tplg, widget);
  1281. if (ret < 0) {
  1282. dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
  1283. widget->name);
  1284. return ret;
  1285. }
  1286. }
  1287. return 0;
  1288. }
  1289. static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
  1290. {
  1291. struct snd_soc_card *card = tplg->comp->card;
  1292. int ret;
  1293. /* Card might not have been registered at this point.
  1294. * If so, just return success.
  1295. */
  1296. if (!card || !card->instantiated) {
  1297. dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
  1298. "Do not add new widgets now\n");
  1299. return 0;
  1300. }
  1301. ret = snd_soc_dapm_new_widgets(card);
  1302. if (ret < 0)
  1303. dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
  1304. ret);
  1305. return 0;
  1306. }
  1307. static void set_stream_info(struct snd_soc_pcm_stream *stream,
  1308. struct snd_soc_tplg_stream_caps *caps)
  1309. {
  1310. stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
  1311. stream->channels_min = caps->channels_min;
  1312. stream->channels_max = caps->channels_max;
  1313. stream->rates = caps->rates;
  1314. stream->rate_min = caps->rate_min;
  1315. stream->rate_max = caps->rate_max;
  1316. stream->formats = caps->formats;
  1317. stream->sig_bits = caps->sig_bits;
  1318. }
  1319. static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
  1320. unsigned int flag_mask, unsigned int flags)
  1321. {
  1322. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
  1323. dai_drv->symmetric_rates =
  1324. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
  1325. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
  1326. dai_drv->symmetric_channels =
  1327. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
  1328. 1 : 0;
  1329. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
  1330. dai_drv->symmetric_samplebits =
  1331. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
  1332. 1 : 0;
  1333. }
  1334. static int soc_tplg_dai_create(struct soc_tplg *tplg,
  1335. struct snd_soc_tplg_pcm *pcm)
  1336. {
  1337. struct snd_soc_dai_driver *dai_drv;
  1338. struct snd_soc_pcm_stream *stream;
  1339. struct snd_soc_tplg_stream_caps *caps;
  1340. int ret;
  1341. dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  1342. if (dai_drv == NULL)
  1343. return -ENOMEM;
  1344. dai_drv->name = pcm->dai_name;
  1345. dai_drv->id = pcm->dai_id;
  1346. if (pcm->playback) {
  1347. stream = &dai_drv->playback;
  1348. caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1349. set_stream_info(stream, caps);
  1350. }
  1351. if (pcm->capture) {
  1352. stream = &dai_drv->capture;
  1353. caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1354. set_stream_info(stream, caps);
  1355. }
  1356. /* pass control to component driver for optional further init */
  1357. ret = soc_tplg_dai_load(tplg, dai_drv);
  1358. if (ret < 0) {
  1359. dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
  1360. kfree(dai_drv);
  1361. return ret;
  1362. }
  1363. dai_drv->dobj.index = tplg->index;
  1364. dai_drv->dobj.ops = tplg->ops;
  1365. dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
  1366. list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
  1367. /* register the DAI to the component */
  1368. return snd_soc_register_dai(tplg->comp, dai_drv);
  1369. }
  1370. /* create the FE DAI link */
  1371. static int soc_tplg_link_create(struct soc_tplg *tplg,
  1372. struct snd_soc_tplg_pcm *pcm)
  1373. {
  1374. struct snd_soc_dai_link *link;
  1375. int ret;
  1376. link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  1377. if (link == NULL)
  1378. return -ENOMEM;
  1379. link->name = pcm->pcm_name;
  1380. link->stream_name = pcm->pcm_name;
  1381. link->id = pcm->pcm_id;
  1382. link->cpu_dai_name = pcm->dai_name;
  1383. link->codec_name = "snd-soc-dummy";
  1384. link->codec_dai_name = "snd-soc-dummy-dai";
  1385. /* enable DPCM */
  1386. link->dynamic = 1;
  1387. link->dpcm_playback = pcm->playback;
  1388. link->dpcm_capture = pcm->capture;
  1389. /* pass control to component driver for optional further init */
  1390. ret = soc_tplg_dai_link_load(tplg, link);
  1391. if (ret < 0) {
  1392. dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
  1393. kfree(link);
  1394. return ret;
  1395. }
  1396. link->dobj.index = tplg->index;
  1397. link->dobj.ops = tplg->ops;
  1398. link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
  1399. list_add(&link->dobj.list, &tplg->comp->dobj_list);
  1400. snd_soc_add_dai_link(tplg->comp->card, link);
  1401. return 0;
  1402. }
  1403. /* create a FE DAI and DAI link from the PCM object */
  1404. static int soc_tplg_pcm_create(struct soc_tplg *tplg,
  1405. struct snd_soc_tplg_pcm *pcm)
  1406. {
  1407. int ret;
  1408. ret = soc_tplg_dai_create(tplg, pcm);
  1409. if (ret < 0)
  1410. return ret;
  1411. return soc_tplg_link_create(tplg, pcm);
  1412. }
  1413. static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
  1414. struct snd_soc_tplg_hdr *hdr)
  1415. {
  1416. struct snd_soc_tplg_pcm *pcm;
  1417. int count = hdr->count;
  1418. int i;
  1419. if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
  1420. return 0;
  1421. if (soc_tplg_check_elem_count(tplg,
  1422. sizeof(struct snd_soc_tplg_pcm), count,
  1423. hdr->payload_size, "PCM DAI")) {
  1424. dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
  1425. count);
  1426. return -EINVAL;
  1427. }
  1428. /* create the FE DAIs and DAI links */
  1429. pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
  1430. for (i = 0; i < count; i++) {
  1431. if (pcm->size != sizeof(*pcm)) {
  1432. dev_err(tplg->dev, "ASoC: invalid pcm size\n");
  1433. return -EINVAL;
  1434. }
  1435. soc_tplg_pcm_create(tplg, pcm);
  1436. pcm++;
  1437. }
  1438. dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
  1439. tplg->pos += sizeof(struct snd_soc_tplg_pcm) * count;
  1440. return 0;
  1441. }
  1442. /* *
  1443. * soc_tplg_be_dai_config - Find and configure an existing BE DAI.
  1444. * @tplg: topology context
  1445. * @be: topology BE DAI configs.
  1446. *
  1447. * The BE dai should already be registered by the platform driver. The
  1448. * platform driver should specify the BE DAI name and ID for matching.
  1449. */
  1450. static int soc_tplg_be_dai_config(struct soc_tplg *tplg,
  1451. struct snd_soc_tplg_be_dai *be)
  1452. {
  1453. struct snd_soc_dai_link_component dai_component = {0};
  1454. struct snd_soc_dai *dai;
  1455. struct snd_soc_dai_driver *dai_drv;
  1456. struct snd_soc_pcm_stream *stream;
  1457. struct snd_soc_tplg_stream_caps *caps;
  1458. int ret;
  1459. dai_component.dai_name = be->dai_name;
  1460. dai = snd_soc_find_dai(&dai_component);
  1461. if (!dai) {
  1462. dev_err(tplg->dev, "ASoC: BE DAI %s not registered\n",
  1463. be->dai_name);
  1464. return -EINVAL;
  1465. }
  1466. if (be->dai_id != dai->id) {
  1467. dev_err(tplg->dev, "ASoC: BE DAI %s id mismatch\n",
  1468. be->dai_name);
  1469. return -EINVAL;
  1470. }
  1471. dai_drv = dai->driver;
  1472. if (!dai_drv)
  1473. return -EINVAL;
  1474. if (be->playback) {
  1475. stream = &dai_drv->playback;
  1476. caps = &be->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1477. set_stream_info(stream, caps);
  1478. }
  1479. if (be->capture) {
  1480. stream = &dai_drv->capture;
  1481. caps = &be->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1482. set_stream_info(stream, caps);
  1483. }
  1484. if (be->flag_mask)
  1485. set_dai_flags(dai_drv, be->flag_mask, be->flags);
  1486. /* pass control to component driver for optional further init */
  1487. ret = soc_tplg_dai_load(tplg, dai_drv);
  1488. if (ret < 0) {
  1489. dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
  1490. return ret;
  1491. }
  1492. return 0;
  1493. }
  1494. static int soc_tplg_be_dai_elems_load(struct soc_tplg *tplg,
  1495. struct snd_soc_tplg_hdr *hdr)
  1496. {
  1497. struct snd_soc_tplg_be_dai *be;
  1498. int count = hdr->count;
  1499. int i;
  1500. if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
  1501. return 0;
  1502. /* config the existing BE DAIs */
  1503. for (i = 0; i < count; i++) {
  1504. be = (struct snd_soc_tplg_be_dai *)tplg->pos;
  1505. if (be->size != sizeof(*be)) {
  1506. dev_err(tplg->dev, "ASoC: invalid BE DAI size\n");
  1507. return -EINVAL;
  1508. }
  1509. soc_tplg_be_dai_config(tplg, be);
  1510. tplg->pos += (sizeof(*be) + be->priv.size);
  1511. }
  1512. dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
  1513. return 0;
  1514. }
  1515. static int soc_tplg_manifest_load(struct soc_tplg *tplg,
  1516. struct snd_soc_tplg_hdr *hdr)
  1517. {
  1518. struct snd_soc_tplg_manifest *manifest;
  1519. if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
  1520. return 0;
  1521. manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
  1522. if (manifest->size != sizeof(*manifest)) {
  1523. dev_err(tplg->dev, "ASoC: invalid manifest size\n");
  1524. return -EINVAL;
  1525. }
  1526. tplg->pos += sizeof(struct snd_soc_tplg_manifest);
  1527. if (tplg->comp && tplg->ops && tplg->ops->manifest)
  1528. return tplg->ops->manifest(tplg->comp, manifest);
  1529. dev_err(tplg->dev, "ASoC: Firmware manifest not supported\n");
  1530. return 0;
  1531. }
  1532. /* validate header magic, size and type */
  1533. static int soc_valid_header(struct soc_tplg *tplg,
  1534. struct snd_soc_tplg_hdr *hdr)
  1535. {
  1536. if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
  1537. return 0;
  1538. if (hdr->size != sizeof(*hdr)) {
  1539. dev_err(tplg->dev,
  1540. "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
  1541. hdr->type, soc_tplg_get_hdr_offset(tplg),
  1542. tplg->fw->size);
  1543. return -EINVAL;
  1544. }
  1545. /* big endian firmware objects not supported atm */
  1546. if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
  1547. dev_err(tplg->dev,
  1548. "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
  1549. tplg->pass, hdr->magic,
  1550. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1551. return -EINVAL;
  1552. }
  1553. if (hdr->magic != SND_SOC_TPLG_MAGIC) {
  1554. dev_err(tplg->dev,
  1555. "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
  1556. tplg->pass, hdr->magic,
  1557. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1558. return -EINVAL;
  1559. }
  1560. if (hdr->abi != SND_SOC_TPLG_ABI_VERSION) {
  1561. dev_err(tplg->dev,
  1562. "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
  1563. tplg->pass, hdr->abi,
  1564. SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
  1565. tplg->fw->size);
  1566. return -EINVAL;
  1567. }
  1568. if (hdr->payload_size == 0) {
  1569. dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
  1570. soc_tplg_get_hdr_offset(tplg));
  1571. return -EINVAL;
  1572. }
  1573. if (tplg->pass == hdr->type)
  1574. dev_dbg(tplg->dev,
  1575. "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
  1576. hdr->payload_size, hdr->type, hdr->version,
  1577. hdr->vendor_type, tplg->pass);
  1578. return 1;
  1579. }
  1580. /* check header type and call appropriate handler */
  1581. static int soc_tplg_load_header(struct soc_tplg *tplg,
  1582. struct snd_soc_tplg_hdr *hdr)
  1583. {
  1584. tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
  1585. /* check for matching ID */
  1586. if (hdr->index != tplg->req_index &&
  1587. hdr->index != SND_SOC_TPLG_INDEX_ALL)
  1588. return 0;
  1589. tplg->index = hdr->index;
  1590. switch (hdr->type) {
  1591. case SND_SOC_TPLG_TYPE_MIXER:
  1592. case SND_SOC_TPLG_TYPE_ENUM:
  1593. case SND_SOC_TPLG_TYPE_BYTES:
  1594. return soc_tplg_kcontrol_elems_load(tplg, hdr);
  1595. case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
  1596. return soc_tplg_dapm_graph_elems_load(tplg, hdr);
  1597. case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
  1598. return soc_tplg_dapm_widget_elems_load(tplg, hdr);
  1599. case SND_SOC_TPLG_TYPE_PCM:
  1600. return soc_tplg_pcm_elems_load(tplg, hdr);
  1601. case SND_SOC_TPLG_TYPE_BE_DAI:
  1602. return soc_tplg_be_dai_elems_load(tplg, hdr);
  1603. case SND_SOC_TPLG_TYPE_MANIFEST:
  1604. return soc_tplg_manifest_load(tplg, hdr);
  1605. default:
  1606. /* bespoke vendor data object */
  1607. return soc_tplg_vendor_load(tplg, hdr);
  1608. }
  1609. return 0;
  1610. }
  1611. /* process the topology file headers */
  1612. static int soc_tplg_process_headers(struct soc_tplg *tplg)
  1613. {
  1614. struct snd_soc_tplg_hdr *hdr;
  1615. int ret;
  1616. tplg->pass = SOC_TPLG_PASS_START;
  1617. /* process the header types from start to end */
  1618. while (tplg->pass <= SOC_TPLG_PASS_END) {
  1619. tplg->hdr_pos = tplg->fw->data;
  1620. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  1621. while (!soc_tplg_is_eof(tplg)) {
  1622. /* make sure header is valid before loading */
  1623. ret = soc_valid_header(tplg, hdr);
  1624. if (ret < 0)
  1625. return ret;
  1626. else if (ret == 0)
  1627. break;
  1628. /* load the header object */
  1629. ret = soc_tplg_load_header(tplg, hdr);
  1630. if (ret < 0)
  1631. return ret;
  1632. /* goto next header */
  1633. tplg->hdr_pos += hdr->payload_size +
  1634. sizeof(struct snd_soc_tplg_hdr);
  1635. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  1636. }
  1637. /* next data type pass */
  1638. tplg->pass++;
  1639. }
  1640. /* signal DAPM we are complete */
  1641. ret = soc_tplg_dapm_complete(tplg);
  1642. if (ret < 0)
  1643. dev_err(tplg->dev,
  1644. "ASoC: failed to initialise DAPM from Firmware\n");
  1645. return ret;
  1646. }
  1647. static int soc_tplg_load(struct soc_tplg *tplg)
  1648. {
  1649. int ret;
  1650. ret = soc_tplg_process_headers(tplg);
  1651. if (ret == 0)
  1652. soc_tplg_complete(tplg);
  1653. return ret;
  1654. }
  1655. /* load audio component topology from "firmware" file */
  1656. int snd_soc_tplg_component_load(struct snd_soc_component *comp,
  1657. struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
  1658. {
  1659. struct soc_tplg tplg;
  1660. /* setup parsing context */
  1661. memset(&tplg, 0, sizeof(tplg));
  1662. tplg.fw = fw;
  1663. tplg.dev = comp->dev;
  1664. tplg.comp = comp;
  1665. tplg.ops = ops;
  1666. tplg.req_index = id;
  1667. tplg.io_ops = ops->io_ops;
  1668. tplg.io_ops_count = ops->io_ops_count;
  1669. tplg.bytes_ext_ops = ops->bytes_ext_ops;
  1670. tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
  1671. return soc_tplg_load(&tplg);
  1672. }
  1673. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
  1674. /* remove this dynamic widget */
  1675. void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
  1676. {
  1677. /* make sure we are a widget */
  1678. if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
  1679. return;
  1680. remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
  1681. }
  1682. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
  1683. /* remove all dynamic widgets from this DAPM context */
  1684. void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
  1685. u32 index)
  1686. {
  1687. struct snd_soc_dapm_widget *w, *next_w;
  1688. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1689. /* make sure we are a widget with correct context */
  1690. if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
  1691. continue;
  1692. /* match ID */
  1693. if (w->dobj.index != index &&
  1694. w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
  1695. continue;
  1696. /* check and free and dynamic widget kcontrols */
  1697. snd_soc_tplg_widget_remove(w);
  1698. snd_soc_dapm_free_widget(w);
  1699. }
  1700. snd_soc_dapm_reset_cache(dapm);
  1701. }
  1702. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
  1703. /* remove dynamic controls from the component driver */
  1704. int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
  1705. {
  1706. struct snd_soc_dobj *dobj, *next_dobj;
  1707. int pass = SOC_TPLG_PASS_END;
  1708. /* process the header types from end to start */
  1709. while (pass >= SOC_TPLG_PASS_START) {
  1710. /* remove mixer controls */
  1711. list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
  1712. list) {
  1713. /* match index */
  1714. if (dobj->index != index &&
  1715. dobj->index != SND_SOC_TPLG_INDEX_ALL)
  1716. continue;
  1717. switch (dobj->type) {
  1718. case SND_SOC_DOBJ_MIXER:
  1719. remove_mixer(comp, dobj, pass);
  1720. break;
  1721. case SND_SOC_DOBJ_ENUM:
  1722. remove_enum(comp, dobj, pass);
  1723. break;
  1724. case SND_SOC_DOBJ_BYTES:
  1725. remove_bytes(comp, dobj, pass);
  1726. break;
  1727. case SND_SOC_DOBJ_WIDGET:
  1728. remove_widget(comp, dobj, pass);
  1729. break;
  1730. case SND_SOC_DOBJ_PCM:
  1731. remove_dai(comp, dobj, pass);
  1732. break;
  1733. case SND_SOC_DOBJ_DAI_LINK:
  1734. remove_link(comp, dobj, pass);
  1735. break;
  1736. default:
  1737. dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
  1738. dobj->type);
  1739. break;
  1740. }
  1741. }
  1742. pass--;
  1743. }
  1744. /* let caller know if FW can be freed when no objects are left */
  1745. return !list_empty(&comp->dobj_list);
  1746. }
  1747. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);