soc-topology.c 48 KB

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