soc-topology.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // soc-topology.c -- ALSA SoC Topology
  4. //
  5. // Copyright (C) 2012 Texas Instruments Inc.
  6. // Copyright (C) 2015 Intel Corporation.
  7. //
  8. // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. // K, Mythri P <mythri.p.k@intel.com>
  10. // Prusty, Subhransu S <subhransu.s.prusty@intel.com>
  11. // B, Jayachandran <jayachandran.b@intel.com>
  12. // Abdullah, Omair M <omair.m.abdullah@intel.com>
  13. // Jin, Yao <yao.jin@intel.com>
  14. // Lin, Mengdong <mengdong.lin@intel.com>
  15. //
  16. // Add support to read audio firmware topology alongside firmware text. The
  17. // topology data can contain kcontrols, DAPM graphs, widgets, DAIs, DAI links,
  18. // equalizers, firmware, coefficients etc.
  19. //
  20. // This file only manages the core ALSA and ASoC components, all other bespoke
  21. // firmware topology data is passed to component drivers for bespoke handling.
  22. #include <linux/kernel.h>
  23. #include <linux/export.h>
  24. #include <linux/list.h>
  25. #include <linux/firmware.h>
  26. #include <linux/slab.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/soc-topology.h>
  30. #include <sound/tlv.h>
  31. /*
  32. * We make several passes over the data (since it wont necessarily be ordered)
  33. * and process objects in the following order. This guarantees the component
  34. * drivers will be ready with any vendor data before the mixers and DAPM objects
  35. * are loaded (that may make use of the vendor data).
  36. */
  37. #define SOC_TPLG_PASS_MANIFEST 0
  38. #define SOC_TPLG_PASS_VENDOR 1
  39. #define SOC_TPLG_PASS_MIXER 2
  40. #define SOC_TPLG_PASS_WIDGET 3
  41. #define SOC_TPLG_PASS_PCM_DAI 4
  42. #define SOC_TPLG_PASS_GRAPH 5
  43. #define SOC_TPLG_PASS_PINS 6
  44. #define SOC_TPLG_PASS_BE_DAI 7
  45. #define SOC_TPLG_PASS_LINK 8
  46. #define SOC_TPLG_PASS_START SOC_TPLG_PASS_MANIFEST
  47. #define SOC_TPLG_PASS_END SOC_TPLG_PASS_LINK
  48. /* topology context */
  49. struct soc_tplg {
  50. const struct firmware *fw;
  51. /* runtime FW parsing */
  52. const u8 *pos; /* read postion */
  53. const u8 *hdr_pos; /* header position */
  54. unsigned int pass; /* pass number */
  55. /* component caller */
  56. struct device *dev;
  57. struct snd_soc_component *comp;
  58. u32 index; /* current block index */
  59. u32 req_index; /* required index, only loaded/free matching blocks */
  60. /* vendor specific kcontrol operations */
  61. const struct snd_soc_tplg_kcontrol_ops *io_ops;
  62. int io_ops_count;
  63. /* vendor specific bytes ext handlers, for TLV bytes controls */
  64. const struct snd_soc_tplg_bytes_ext_ops *bytes_ext_ops;
  65. int bytes_ext_ops_count;
  66. /* optional fw loading callbacks to component drivers */
  67. struct snd_soc_tplg_ops *ops;
  68. };
  69. static int soc_tplg_process_headers(struct soc_tplg *tplg);
  70. static void soc_tplg_complete(struct soc_tplg *tplg);
  71. struct snd_soc_dapm_widget *
  72. snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
  73. const struct snd_soc_dapm_widget *widget);
  74. struct snd_soc_dapm_widget *
  75. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  76. const struct snd_soc_dapm_widget *widget);
  77. /* check we dont overflow the data for this control chunk */
  78. static int soc_tplg_check_elem_count(struct soc_tplg *tplg, size_t elem_size,
  79. unsigned int count, size_t bytes, const char *elem_type)
  80. {
  81. const u8 *end = tplg->pos + elem_size * count;
  82. if (end > tplg->fw->data + tplg->fw->size) {
  83. dev_err(tplg->dev, "ASoC: %s overflow end of data\n",
  84. elem_type);
  85. return -EINVAL;
  86. }
  87. /* check there is enough room in chunk for control.
  88. extra bytes at the end of control are for vendor data here */
  89. if (elem_size * count > bytes) {
  90. dev_err(tplg->dev,
  91. "ASoC: %s count %d of size %zu is bigger than chunk %zu\n",
  92. elem_type, count, elem_size, bytes);
  93. return -EINVAL;
  94. }
  95. return 0;
  96. }
  97. static inline int soc_tplg_is_eof(struct soc_tplg *tplg)
  98. {
  99. const u8 *end = tplg->hdr_pos;
  100. if (end >= tplg->fw->data + tplg->fw->size)
  101. return 1;
  102. return 0;
  103. }
  104. static inline unsigned long soc_tplg_get_hdr_offset(struct soc_tplg *tplg)
  105. {
  106. return (unsigned long)(tplg->hdr_pos - tplg->fw->data);
  107. }
  108. static inline unsigned long soc_tplg_get_offset(struct soc_tplg *tplg)
  109. {
  110. return (unsigned long)(tplg->pos - tplg->fw->data);
  111. }
  112. /* mapping of Kcontrol types and associated operations. */
  113. static const struct snd_soc_tplg_kcontrol_ops io_ops[] = {
  114. {SND_SOC_TPLG_CTL_VOLSW, snd_soc_get_volsw,
  115. snd_soc_put_volsw, snd_soc_info_volsw},
  116. {SND_SOC_TPLG_CTL_VOLSW_SX, snd_soc_get_volsw_sx,
  117. snd_soc_put_volsw_sx, NULL},
  118. {SND_SOC_TPLG_CTL_ENUM, snd_soc_get_enum_double,
  119. snd_soc_put_enum_double, snd_soc_info_enum_double},
  120. {SND_SOC_TPLG_CTL_ENUM_VALUE, snd_soc_get_enum_double,
  121. snd_soc_put_enum_double, NULL},
  122. {SND_SOC_TPLG_CTL_BYTES, snd_soc_bytes_get,
  123. snd_soc_bytes_put, snd_soc_bytes_info},
  124. {SND_SOC_TPLG_CTL_RANGE, snd_soc_get_volsw_range,
  125. snd_soc_put_volsw_range, snd_soc_info_volsw_range},
  126. {SND_SOC_TPLG_CTL_VOLSW_XR_SX, snd_soc_get_xr_sx,
  127. snd_soc_put_xr_sx, snd_soc_info_xr_sx},
  128. {SND_SOC_TPLG_CTL_STROBE, snd_soc_get_strobe,
  129. snd_soc_put_strobe, NULL},
  130. {SND_SOC_TPLG_DAPM_CTL_VOLSW, snd_soc_dapm_get_volsw,
  131. snd_soc_dapm_put_volsw, snd_soc_info_volsw},
  132. {SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE, snd_soc_dapm_get_enum_double,
  133. snd_soc_dapm_put_enum_double, snd_soc_info_enum_double},
  134. {SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT, snd_soc_dapm_get_enum_double,
  135. snd_soc_dapm_put_enum_double, NULL},
  136. {SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE, snd_soc_dapm_get_enum_double,
  137. snd_soc_dapm_put_enum_double, NULL},
  138. {SND_SOC_TPLG_DAPM_CTL_PIN, snd_soc_dapm_get_pin_switch,
  139. snd_soc_dapm_put_pin_switch, snd_soc_dapm_info_pin_switch},
  140. };
  141. struct soc_tplg_map {
  142. int uid;
  143. int kid;
  144. };
  145. /* mapping of widget types from UAPI IDs to kernel IDs */
  146. static const struct soc_tplg_map dapm_map[] = {
  147. {SND_SOC_TPLG_DAPM_INPUT, snd_soc_dapm_input},
  148. {SND_SOC_TPLG_DAPM_OUTPUT, snd_soc_dapm_output},
  149. {SND_SOC_TPLG_DAPM_MUX, snd_soc_dapm_mux},
  150. {SND_SOC_TPLG_DAPM_MIXER, snd_soc_dapm_mixer},
  151. {SND_SOC_TPLG_DAPM_PGA, snd_soc_dapm_pga},
  152. {SND_SOC_TPLG_DAPM_OUT_DRV, snd_soc_dapm_out_drv},
  153. {SND_SOC_TPLG_DAPM_ADC, snd_soc_dapm_adc},
  154. {SND_SOC_TPLG_DAPM_DAC, snd_soc_dapm_dac},
  155. {SND_SOC_TPLG_DAPM_SWITCH, snd_soc_dapm_switch},
  156. {SND_SOC_TPLG_DAPM_PRE, snd_soc_dapm_pre},
  157. {SND_SOC_TPLG_DAPM_POST, snd_soc_dapm_post},
  158. {SND_SOC_TPLG_DAPM_AIF_IN, snd_soc_dapm_aif_in},
  159. {SND_SOC_TPLG_DAPM_AIF_OUT, snd_soc_dapm_aif_out},
  160. {SND_SOC_TPLG_DAPM_DAI_IN, snd_soc_dapm_dai_in},
  161. {SND_SOC_TPLG_DAPM_DAI_OUT, snd_soc_dapm_dai_out},
  162. {SND_SOC_TPLG_DAPM_DAI_LINK, snd_soc_dapm_dai_link},
  163. {SND_SOC_TPLG_DAPM_BUFFER, snd_soc_dapm_buffer},
  164. {SND_SOC_TPLG_DAPM_SCHEDULER, snd_soc_dapm_scheduler},
  165. {SND_SOC_TPLG_DAPM_EFFECT, snd_soc_dapm_effect},
  166. {SND_SOC_TPLG_DAPM_SIGGEN, snd_soc_dapm_siggen},
  167. {SND_SOC_TPLG_DAPM_SRC, snd_soc_dapm_src},
  168. {SND_SOC_TPLG_DAPM_ASRC, snd_soc_dapm_asrc},
  169. {SND_SOC_TPLG_DAPM_ENCODER, snd_soc_dapm_encoder},
  170. {SND_SOC_TPLG_DAPM_DECODER, snd_soc_dapm_decoder},
  171. };
  172. static int tplc_chan_get_reg(struct soc_tplg *tplg,
  173. struct snd_soc_tplg_channel *chan, int map)
  174. {
  175. int i;
  176. for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
  177. if (chan[i].id == map)
  178. return chan[i].reg;
  179. }
  180. return -EINVAL;
  181. }
  182. static int tplc_chan_get_shift(struct soc_tplg *tplg,
  183. struct snd_soc_tplg_channel *chan, int map)
  184. {
  185. int i;
  186. for (i = 0; i < SND_SOC_TPLG_MAX_CHAN; i++) {
  187. if (chan[i].id == map)
  188. return chan[i].shift;
  189. }
  190. return -EINVAL;
  191. }
  192. static int get_widget_id(int tplg_type)
  193. {
  194. int i;
  195. for (i = 0; i < ARRAY_SIZE(dapm_map); i++) {
  196. if (tplg_type == dapm_map[i].uid)
  197. return dapm_map[i].kid;
  198. }
  199. return -EINVAL;
  200. }
  201. static inline void soc_bind_err(struct soc_tplg *tplg,
  202. struct snd_soc_tplg_ctl_hdr *hdr, int index)
  203. {
  204. dev_err(tplg->dev,
  205. "ASoC: invalid control type (g,p,i) %d:%d:%d index %d at 0x%lx\n",
  206. hdr->ops.get, hdr->ops.put, hdr->ops.info, index,
  207. soc_tplg_get_offset(tplg));
  208. }
  209. static inline void soc_control_err(struct soc_tplg *tplg,
  210. struct snd_soc_tplg_ctl_hdr *hdr, const char *name)
  211. {
  212. dev_err(tplg->dev,
  213. "ASoC: no complete mixer IO handler for %s type (g,p,i) %d:%d:%d at 0x%lx\n",
  214. name, hdr->ops.get, hdr->ops.put, hdr->ops.info,
  215. soc_tplg_get_offset(tplg));
  216. }
  217. /* pass vendor data to component driver for processing */
  218. static int soc_tplg_vendor_load_(struct soc_tplg *tplg,
  219. struct snd_soc_tplg_hdr *hdr)
  220. {
  221. int ret = 0;
  222. if (tplg->comp && tplg->ops && tplg->ops->vendor_load)
  223. ret = tplg->ops->vendor_load(tplg->comp, tplg->index, hdr);
  224. else {
  225. dev_err(tplg->dev, "ASoC: no vendor load callback for ID %d\n",
  226. hdr->vendor_type);
  227. return -EINVAL;
  228. }
  229. if (ret < 0)
  230. dev_err(tplg->dev,
  231. "ASoC: vendor load failed at hdr offset %ld/0x%lx for type %d:%d\n",
  232. soc_tplg_get_hdr_offset(tplg),
  233. soc_tplg_get_hdr_offset(tplg),
  234. hdr->type, hdr->vendor_type);
  235. return ret;
  236. }
  237. /* pass vendor data to component driver for processing */
  238. static int soc_tplg_vendor_load(struct soc_tplg *tplg,
  239. struct snd_soc_tplg_hdr *hdr)
  240. {
  241. if (tplg->pass != SOC_TPLG_PASS_VENDOR)
  242. return 0;
  243. return soc_tplg_vendor_load_(tplg, hdr);
  244. }
  245. /* optionally pass new dynamic widget to component driver. This is mainly for
  246. * external widgets where we can assign private data/ops */
  247. static int soc_tplg_widget_load(struct soc_tplg *tplg,
  248. struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
  249. {
  250. if (tplg->comp && tplg->ops && tplg->ops->widget_load)
  251. return tplg->ops->widget_load(tplg->comp, tplg->index, w,
  252. tplg_w);
  253. return 0;
  254. }
  255. /* optionally pass new dynamic widget to component driver. This is mainly for
  256. * external widgets where we can assign private data/ops */
  257. static int soc_tplg_widget_ready(struct soc_tplg *tplg,
  258. struct snd_soc_dapm_widget *w, struct snd_soc_tplg_dapm_widget *tplg_w)
  259. {
  260. if (tplg->comp && tplg->ops && tplg->ops->widget_ready)
  261. return tplg->ops->widget_ready(tplg->comp, tplg->index, w,
  262. tplg_w);
  263. return 0;
  264. }
  265. /* pass DAI configurations to component driver for extra initialization */
  266. static int soc_tplg_dai_load(struct soc_tplg *tplg,
  267. struct snd_soc_dai_driver *dai_drv,
  268. struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
  269. {
  270. if (tplg->comp && tplg->ops && tplg->ops->dai_load)
  271. return tplg->ops->dai_load(tplg->comp, tplg->index, dai_drv,
  272. pcm, dai);
  273. return 0;
  274. }
  275. /* pass link configurations to component driver for extra initialization */
  276. static int soc_tplg_dai_link_load(struct soc_tplg *tplg,
  277. struct snd_soc_dai_link *link, struct snd_soc_tplg_link_config *cfg)
  278. {
  279. if (tplg->comp && tplg->ops && tplg->ops->link_load)
  280. return tplg->ops->link_load(tplg->comp, tplg->index, link, cfg);
  281. return 0;
  282. }
  283. /* tell the component driver that all firmware has been loaded in this request */
  284. static void soc_tplg_complete(struct soc_tplg *tplg)
  285. {
  286. if (tplg->comp && tplg->ops && tplg->ops->complete)
  287. tplg->ops->complete(tplg->comp);
  288. }
  289. /* add a dynamic kcontrol */
  290. static int soc_tplg_add_dcontrol(struct snd_card *card, struct device *dev,
  291. const struct snd_kcontrol_new *control_new, const char *prefix,
  292. void *data, struct snd_kcontrol **kcontrol)
  293. {
  294. int err;
  295. *kcontrol = snd_soc_cnew(control_new, data, control_new->name, prefix);
  296. if (*kcontrol == NULL) {
  297. dev_err(dev, "ASoC: Failed to create new kcontrol %s\n",
  298. control_new->name);
  299. return -ENOMEM;
  300. }
  301. err = snd_ctl_add(card, *kcontrol);
  302. if (err < 0) {
  303. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  304. control_new->name, err);
  305. return err;
  306. }
  307. return 0;
  308. }
  309. /* add a dynamic kcontrol for component driver */
  310. static int soc_tplg_add_kcontrol(struct soc_tplg *tplg,
  311. struct snd_kcontrol_new *k, struct snd_kcontrol **kcontrol)
  312. {
  313. struct snd_soc_component *comp = tplg->comp;
  314. return soc_tplg_add_dcontrol(comp->card->snd_card,
  315. comp->dev, k, NULL, comp, kcontrol);
  316. }
  317. /* remove a mixer kcontrol */
  318. static void remove_mixer(struct snd_soc_component *comp,
  319. struct snd_soc_dobj *dobj, int pass)
  320. {
  321. struct snd_card *card = comp->card->snd_card;
  322. struct soc_mixer_control *sm =
  323. container_of(dobj, struct soc_mixer_control, dobj);
  324. const unsigned int *p = NULL;
  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. if (sm->dobj.control.kcontrol->tlv.p)
  330. p = sm->dobj.control.kcontrol->tlv.p;
  331. snd_ctl_remove(card, sm->dobj.control.kcontrol);
  332. list_del(&sm->dobj.list);
  333. kfree(sm);
  334. kfree(p);
  335. }
  336. /* remove an enum kcontrol */
  337. static void remove_enum(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_enum *se = container_of(dobj, struct soc_enum, dobj);
  342. int i;
  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, se->dobj.control.kcontrol);
  348. list_del(&se->dobj.list);
  349. kfree(se->dobj.control.dvalues);
  350. for (i = 0; i < se->items; i++)
  351. kfree(se->dobj.control.dtexts[i]);
  352. kfree(se);
  353. }
  354. /* remove a byte kcontrol */
  355. static void remove_bytes(struct snd_soc_component *comp,
  356. struct snd_soc_dobj *dobj, int pass)
  357. {
  358. struct snd_card *card = comp->card->snd_card;
  359. struct soc_bytes_ext *sb =
  360. container_of(dobj, struct soc_bytes_ext, dobj);
  361. if (pass != SOC_TPLG_PASS_MIXER)
  362. return;
  363. if (dobj->ops && dobj->ops->control_unload)
  364. dobj->ops->control_unload(comp, dobj);
  365. snd_ctl_remove(card, sb->dobj.control.kcontrol);
  366. list_del(&sb->dobj.list);
  367. kfree(sb);
  368. }
  369. /* remove a widget and it's kcontrols - routes must be removed first */
  370. static void remove_widget(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 snd_soc_dapm_widget *w =
  375. container_of(dobj, struct snd_soc_dapm_widget, dobj);
  376. int i;
  377. if (pass != SOC_TPLG_PASS_WIDGET)
  378. return;
  379. if (dobj->ops && dobj->ops->widget_unload)
  380. dobj->ops->widget_unload(comp, dobj);
  381. if (!w->kcontrols)
  382. goto free_news;
  383. /*
  384. * Dynamic Widgets either have 1..N enum kcontrols or mixers.
  385. * The enum may either have an array of values or strings.
  386. */
  387. if (dobj->widget.kcontrol_type == SND_SOC_TPLG_TYPE_ENUM) {
  388. /* enumerated widget mixer */
  389. for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
  390. struct snd_kcontrol *kcontrol = w->kcontrols[i];
  391. struct soc_enum *se =
  392. (struct soc_enum *)kcontrol->private_value;
  393. int j;
  394. snd_ctl_remove(card, kcontrol);
  395. kfree(se->dobj.control.dvalues);
  396. for (j = 0; j < se->items; j++)
  397. kfree(se->dobj.control.dtexts[j]);
  398. kfree(se);
  399. kfree(w->kcontrol_news[i].name);
  400. }
  401. } else {
  402. /* volume mixer or bytes controls */
  403. for (i = 0; w->kcontrols != NULL && i < w->num_kcontrols; i++) {
  404. struct snd_kcontrol *kcontrol = w->kcontrols[i];
  405. if (dobj->widget.kcontrol_type
  406. == SND_SOC_TPLG_TYPE_MIXER)
  407. kfree(kcontrol->tlv.p);
  408. /* Private value is used as struct soc_mixer_control
  409. * for volume mixers or soc_bytes_ext for bytes
  410. * controls.
  411. */
  412. kfree((void *)kcontrol->private_value);
  413. snd_ctl_remove(card, kcontrol);
  414. kfree(w->kcontrol_news[i].name);
  415. }
  416. }
  417. free_news:
  418. kfree(w->kcontrol_news);
  419. /* widget w is freed by soc-dapm.c */
  420. }
  421. /* remove DAI configurations */
  422. static void remove_dai(struct snd_soc_component *comp,
  423. struct snd_soc_dobj *dobj, int pass)
  424. {
  425. struct snd_soc_dai_driver *dai_drv =
  426. container_of(dobj, struct snd_soc_dai_driver, dobj);
  427. if (pass != SOC_TPLG_PASS_PCM_DAI)
  428. return;
  429. if (dobj->ops && dobj->ops->dai_unload)
  430. dobj->ops->dai_unload(comp, dobj);
  431. kfree(dai_drv->name);
  432. list_del(&dobj->list);
  433. kfree(dai_drv);
  434. }
  435. /* remove link configurations */
  436. static void remove_link(struct snd_soc_component *comp,
  437. struct snd_soc_dobj *dobj, int pass)
  438. {
  439. struct snd_soc_dai_link *link =
  440. container_of(dobj, struct snd_soc_dai_link, dobj);
  441. if (pass != SOC_TPLG_PASS_PCM_DAI)
  442. return;
  443. if (dobj->ops && dobj->ops->link_unload)
  444. dobj->ops->link_unload(comp, dobj);
  445. kfree(link->name);
  446. kfree(link->stream_name);
  447. kfree(link->cpu_dai_name);
  448. list_del(&dobj->list);
  449. snd_soc_remove_dai_link(comp->card, link);
  450. kfree(link);
  451. }
  452. /* bind a kcontrol to it's IO handlers */
  453. static int soc_tplg_kcontrol_bind_io(struct snd_soc_tplg_ctl_hdr *hdr,
  454. struct snd_kcontrol_new *k,
  455. const struct soc_tplg *tplg)
  456. {
  457. const struct snd_soc_tplg_kcontrol_ops *ops;
  458. const struct snd_soc_tplg_bytes_ext_ops *ext_ops;
  459. int num_ops, i;
  460. if (hdr->ops.info == SND_SOC_TPLG_CTL_BYTES
  461. && k->iface & SNDRV_CTL_ELEM_IFACE_MIXER
  462. && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE
  463. && k->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  464. struct soc_bytes_ext *sbe;
  465. struct snd_soc_tplg_bytes_control *be;
  466. sbe = (struct soc_bytes_ext *)k->private_value;
  467. be = container_of(hdr, struct snd_soc_tplg_bytes_control, hdr);
  468. /* TLV bytes controls need standard kcontrol info handler,
  469. * TLV callback and extended put/get handlers.
  470. */
  471. k->info = snd_soc_bytes_info_ext;
  472. k->tlv.c = snd_soc_bytes_tlv_callback;
  473. ext_ops = tplg->bytes_ext_ops;
  474. num_ops = tplg->bytes_ext_ops_count;
  475. for (i = 0; i < num_ops; i++) {
  476. if (!sbe->put && ext_ops[i].id == be->ext_ops.put)
  477. sbe->put = ext_ops[i].put;
  478. if (!sbe->get && ext_ops[i].id == be->ext_ops.get)
  479. sbe->get = ext_ops[i].get;
  480. }
  481. if (sbe->put && sbe->get)
  482. return 0;
  483. else
  484. return -EINVAL;
  485. }
  486. /* try and map vendor specific kcontrol handlers first */
  487. ops = tplg->io_ops;
  488. num_ops = tplg->io_ops_count;
  489. for (i = 0; i < num_ops; i++) {
  490. if (k->put == NULL && ops[i].id == hdr->ops.put)
  491. k->put = ops[i].put;
  492. if (k->get == NULL && ops[i].id == hdr->ops.get)
  493. k->get = ops[i].get;
  494. if (k->info == NULL && ops[i].id == hdr->ops.info)
  495. k->info = ops[i].info;
  496. }
  497. /* vendor specific handlers found ? */
  498. if (k->put && k->get && k->info)
  499. return 0;
  500. /* none found so try standard kcontrol handlers */
  501. ops = io_ops;
  502. num_ops = ARRAY_SIZE(io_ops);
  503. for (i = 0; i < num_ops; i++) {
  504. if (k->put == NULL && ops[i].id == hdr->ops.put)
  505. k->put = ops[i].put;
  506. if (k->get == NULL && ops[i].id == hdr->ops.get)
  507. k->get = ops[i].get;
  508. if (k->info == NULL && ops[i].id == hdr->ops.info)
  509. k->info = ops[i].info;
  510. }
  511. /* standard handlers found ? */
  512. if (k->put && k->get && k->info)
  513. return 0;
  514. /* nothing to bind */
  515. return -EINVAL;
  516. }
  517. /* bind a widgets to it's evnt handlers */
  518. int snd_soc_tplg_widget_bind_event(struct snd_soc_dapm_widget *w,
  519. const struct snd_soc_tplg_widget_events *events,
  520. int num_events, u16 event_type)
  521. {
  522. int i;
  523. w->event = NULL;
  524. for (i = 0; i < num_events; i++) {
  525. if (event_type == events[i].type) {
  526. /* found - so assign event */
  527. w->event = events[i].event_handler;
  528. return 0;
  529. }
  530. }
  531. /* not found */
  532. return -EINVAL;
  533. }
  534. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_bind_event);
  535. /* optionally pass new dynamic kcontrol to component driver. */
  536. static int soc_tplg_init_kcontrol(struct soc_tplg *tplg,
  537. struct snd_kcontrol_new *k, struct snd_soc_tplg_ctl_hdr *hdr)
  538. {
  539. if (tplg->comp && tplg->ops && tplg->ops->control_load)
  540. return tplg->ops->control_load(tplg->comp, tplg->index, k,
  541. hdr);
  542. return 0;
  543. }
  544. static int soc_tplg_create_tlv_db_scale(struct soc_tplg *tplg,
  545. struct snd_kcontrol_new *kc, struct snd_soc_tplg_tlv_dbscale *scale)
  546. {
  547. unsigned int item_len = 2 * sizeof(unsigned int);
  548. unsigned int *p;
  549. p = kzalloc(item_len + 2 * sizeof(unsigned int), GFP_KERNEL);
  550. if (!p)
  551. return -ENOMEM;
  552. p[0] = SNDRV_CTL_TLVT_DB_SCALE;
  553. p[1] = item_len;
  554. p[2] = scale->min;
  555. p[3] = (scale->step & TLV_DB_SCALE_MASK)
  556. | (scale->mute ? TLV_DB_SCALE_MUTE : 0);
  557. kc->tlv.p = (void *)p;
  558. return 0;
  559. }
  560. static int soc_tplg_create_tlv(struct soc_tplg *tplg,
  561. struct snd_kcontrol_new *kc, struct snd_soc_tplg_ctl_hdr *tc)
  562. {
  563. struct snd_soc_tplg_ctl_tlv *tplg_tlv;
  564. if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE))
  565. return 0;
  566. if (!(tc->access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK)) {
  567. tplg_tlv = &tc->tlv;
  568. switch (tplg_tlv->type) {
  569. case SNDRV_CTL_TLVT_DB_SCALE:
  570. return soc_tplg_create_tlv_db_scale(tplg, kc,
  571. &tplg_tlv->scale);
  572. /* TODO: add support for other TLV types */
  573. default:
  574. dev_dbg(tplg->dev, "Unsupported TLV type %d\n",
  575. tplg_tlv->type);
  576. return -EINVAL;
  577. }
  578. }
  579. return 0;
  580. }
  581. static inline void soc_tplg_free_tlv(struct soc_tplg *tplg,
  582. struct snd_kcontrol_new *kc)
  583. {
  584. kfree(kc->tlv.p);
  585. }
  586. static int soc_tplg_dbytes_create(struct soc_tplg *tplg, unsigned int count,
  587. size_t size)
  588. {
  589. struct snd_soc_tplg_bytes_control *be;
  590. struct soc_bytes_ext *sbe;
  591. struct snd_kcontrol_new kc;
  592. int i, err;
  593. if (soc_tplg_check_elem_count(tplg,
  594. sizeof(struct snd_soc_tplg_bytes_control), count,
  595. size, "mixer bytes")) {
  596. dev_err(tplg->dev, "ASoC: Invalid count %d for byte control\n",
  597. count);
  598. return -EINVAL;
  599. }
  600. for (i = 0; i < count; i++) {
  601. be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
  602. /* validate kcontrol */
  603. if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  604. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  605. return -EINVAL;
  606. sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
  607. if (sbe == NULL)
  608. return -ENOMEM;
  609. tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
  610. be->priv.size);
  611. dev_dbg(tplg->dev,
  612. "ASoC: adding bytes kcontrol %s with access 0x%x\n",
  613. be->hdr.name, be->hdr.access);
  614. memset(&kc, 0, sizeof(kc));
  615. kc.name = be->hdr.name;
  616. kc.private_value = (long)sbe;
  617. kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  618. kc.access = be->hdr.access;
  619. sbe->max = be->max;
  620. sbe->dobj.type = SND_SOC_DOBJ_BYTES;
  621. sbe->dobj.ops = tplg->ops;
  622. INIT_LIST_HEAD(&sbe->dobj.list);
  623. /* map io handlers */
  624. err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc, tplg);
  625. if (err) {
  626. soc_control_err(tplg, &be->hdr, be->hdr.name);
  627. kfree(sbe);
  628. continue;
  629. }
  630. /* pass control to driver for optional further init */
  631. err = soc_tplg_init_kcontrol(tplg, &kc,
  632. (struct snd_soc_tplg_ctl_hdr *)be);
  633. if (err < 0) {
  634. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  635. be->hdr.name);
  636. kfree(sbe);
  637. continue;
  638. }
  639. /* register control here */
  640. err = soc_tplg_add_kcontrol(tplg, &kc,
  641. &sbe->dobj.control.kcontrol);
  642. if (err < 0) {
  643. dev_err(tplg->dev, "ASoC: failed to add %s\n",
  644. be->hdr.name);
  645. kfree(sbe);
  646. continue;
  647. }
  648. list_add(&sbe->dobj.list, &tplg->comp->dobj_list);
  649. }
  650. return 0;
  651. }
  652. static int soc_tplg_dmixer_create(struct soc_tplg *tplg, unsigned int count,
  653. size_t size)
  654. {
  655. struct snd_soc_tplg_mixer_control *mc;
  656. struct soc_mixer_control *sm;
  657. struct snd_kcontrol_new kc;
  658. int i, err;
  659. if (soc_tplg_check_elem_count(tplg,
  660. sizeof(struct snd_soc_tplg_mixer_control),
  661. count, size, "mixers")) {
  662. dev_err(tplg->dev, "ASoC: invalid count %d for controls\n",
  663. count);
  664. return -EINVAL;
  665. }
  666. for (i = 0; i < count; i++) {
  667. mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
  668. /* validate kcontrol */
  669. if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  670. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  671. return -EINVAL;
  672. sm = kzalloc(sizeof(*sm), GFP_KERNEL);
  673. if (sm == NULL)
  674. return -ENOMEM;
  675. tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
  676. mc->priv.size);
  677. dev_dbg(tplg->dev,
  678. "ASoC: adding mixer kcontrol %s with access 0x%x\n",
  679. mc->hdr.name, mc->hdr.access);
  680. memset(&kc, 0, sizeof(kc));
  681. kc.name = mc->hdr.name;
  682. kc.private_value = (long)sm;
  683. kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  684. kc.access = mc->hdr.access;
  685. /* we only support FL/FR channel mapping atm */
  686. sm->reg = tplc_chan_get_reg(tplg, mc->channel,
  687. SNDRV_CHMAP_FL);
  688. sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
  689. SNDRV_CHMAP_FR);
  690. sm->shift = tplc_chan_get_shift(tplg, mc->channel,
  691. SNDRV_CHMAP_FL);
  692. sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
  693. SNDRV_CHMAP_FR);
  694. sm->max = mc->max;
  695. sm->min = mc->min;
  696. sm->invert = mc->invert;
  697. sm->platform_max = mc->platform_max;
  698. sm->dobj.index = tplg->index;
  699. sm->dobj.ops = tplg->ops;
  700. sm->dobj.type = SND_SOC_DOBJ_MIXER;
  701. INIT_LIST_HEAD(&sm->dobj.list);
  702. /* map io handlers */
  703. err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc, tplg);
  704. if (err) {
  705. soc_control_err(tplg, &mc->hdr, mc->hdr.name);
  706. kfree(sm);
  707. continue;
  708. }
  709. /* pass control to driver for optional further init */
  710. err = soc_tplg_init_kcontrol(tplg, &kc,
  711. (struct snd_soc_tplg_ctl_hdr *) mc);
  712. if (err < 0) {
  713. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  714. mc->hdr.name);
  715. kfree(sm);
  716. continue;
  717. }
  718. /* create any TLV data */
  719. soc_tplg_create_tlv(tplg, &kc, &mc->hdr);
  720. /* register control here */
  721. err = soc_tplg_add_kcontrol(tplg, &kc,
  722. &sm->dobj.control.kcontrol);
  723. if (err < 0) {
  724. dev_err(tplg->dev, "ASoC: failed to add %s\n",
  725. mc->hdr.name);
  726. soc_tplg_free_tlv(tplg, &kc);
  727. kfree(sm);
  728. continue;
  729. }
  730. list_add(&sm->dobj.list, &tplg->comp->dobj_list);
  731. }
  732. return 0;
  733. }
  734. static int soc_tplg_denum_create_texts(struct soc_enum *se,
  735. struct snd_soc_tplg_enum_control *ec)
  736. {
  737. int i, ret;
  738. se->dobj.control.dtexts =
  739. kcalloc(ec->items, sizeof(char *), GFP_KERNEL);
  740. if (se->dobj.control.dtexts == NULL)
  741. return -ENOMEM;
  742. for (i = 0; i < ec->items; i++) {
  743. if (strnlen(ec->texts[i], SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  744. SNDRV_CTL_ELEM_ID_NAME_MAXLEN) {
  745. ret = -EINVAL;
  746. goto err;
  747. }
  748. se->dobj.control.dtexts[i] = kstrdup(ec->texts[i], GFP_KERNEL);
  749. if (!se->dobj.control.dtexts[i]) {
  750. ret = -ENOMEM;
  751. goto err;
  752. }
  753. }
  754. se->texts = (const char * const *)se->dobj.control.dtexts;
  755. return 0;
  756. err:
  757. for (--i; i >= 0; i--)
  758. kfree(se->dobj.control.dtexts[i]);
  759. kfree(se->dobj.control.dtexts);
  760. return ret;
  761. }
  762. static int soc_tplg_denum_create_values(struct soc_enum *se,
  763. struct snd_soc_tplg_enum_control *ec)
  764. {
  765. if (ec->items > sizeof(*ec->values))
  766. return -EINVAL;
  767. se->dobj.control.dvalues = kmemdup(ec->values,
  768. ec->items * sizeof(u32),
  769. GFP_KERNEL);
  770. if (!se->dobj.control.dvalues)
  771. return -ENOMEM;
  772. return 0;
  773. }
  774. static int soc_tplg_denum_create(struct soc_tplg *tplg, unsigned int count,
  775. size_t size)
  776. {
  777. struct snd_soc_tplg_enum_control *ec;
  778. struct soc_enum *se;
  779. struct snd_kcontrol_new kc;
  780. int i, ret, err;
  781. if (soc_tplg_check_elem_count(tplg,
  782. sizeof(struct snd_soc_tplg_enum_control),
  783. count, size, "enums")) {
  784. dev_err(tplg->dev, "ASoC: invalid count %d for enum controls\n",
  785. count);
  786. return -EINVAL;
  787. }
  788. for (i = 0; i < count; i++) {
  789. ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
  790. tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
  791. ec->priv.size);
  792. /* validate kcontrol */
  793. if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  794. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  795. return -EINVAL;
  796. se = kzalloc((sizeof(*se)), GFP_KERNEL);
  797. if (se == NULL)
  798. return -ENOMEM;
  799. dev_dbg(tplg->dev, "ASoC: adding enum kcontrol %s size %d\n",
  800. ec->hdr.name, ec->items);
  801. memset(&kc, 0, sizeof(kc));
  802. kc.name = ec->hdr.name;
  803. kc.private_value = (long)se;
  804. kc.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  805. kc.access = ec->hdr.access;
  806. se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
  807. se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
  808. SNDRV_CHMAP_FL);
  809. se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
  810. SNDRV_CHMAP_FL);
  811. se->items = ec->items;
  812. se->mask = ec->mask;
  813. se->dobj.index = tplg->index;
  814. se->dobj.type = SND_SOC_DOBJ_ENUM;
  815. se->dobj.ops = tplg->ops;
  816. INIT_LIST_HEAD(&se->dobj.list);
  817. switch (ec->hdr.ops.info) {
  818. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  819. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  820. err = soc_tplg_denum_create_values(se, ec);
  821. if (err < 0) {
  822. dev_err(tplg->dev,
  823. "ASoC: could not create values for %s\n",
  824. ec->hdr.name);
  825. kfree(se);
  826. continue;
  827. }
  828. /* fall through and create texts */
  829. case SND_SOC_TPLG_CTL_ENUM:
  830. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  831. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  832. err = soc_tplg_denum_create_texts(se, ec);
  833. if (err < 0) {
  834. dev_err(tplg->dev,
  835. "ASoC: could not create texts for %s\n",
  836. ec->hdr.name);
  837. kfree(se);
  838. continue;
  839. }
  840. break;
  841. default:
  842. dev_err(tplg->dev,
  843. "ASoC: invalid enum control type %d for %s\n",
  844. ec->hdr.ops.info, ec->hdr.name);
  845. kfree(se);
  846. continue;
  847. }
  848. /* map io handlers */
  849. err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc, tplg);
  850. if (err) {
  851. soc_control_err(tplg, &ec->hdr, ec->hdr.name);
  852. kfree(se);
  853. continue;
  854. }
  855. /* pass control to driver for optional further init */
  856. err = soc_tplg_init_kcontrol(tplg, &kc,
  857. (struct snd_soc_tplg_ctl_hdr *) ec);
  858. if (err < 0) {
  859. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  860. ec->hdr.name);
  861. kfree(se);
  862. continue;
  863. }
  864. /* register control here */
  865. ret = soc_tplg_add_kcontrol(tplg,
  866. &kc, &se->dobj.control.kcontrol);
  867. if (ret < 0) {
  868. dev_err(tplg->dev, "ASoC: could not add kcontrol %s\n",
  869. ec->hdr.name);
  870. kfree(se);
  871. continue;
  872. }
  873. list_add(&se->dobj.list, &tplg->comp->dobj_list);
  874. }
  875. return 0;
  876. }
  877. static int soc_tplg_kcontrol_elems_load(struct soc_tplg *tplg,
  878. struct snd_soc_tplg_hdr *hdr)
  879. {
  880. struct snd_soc_tplg_ctl_hdr *control_hdr;
  881. int i;
  882. if (tplg->pass != SOC_TPLG_PASS_MIXER) {
  883. tplg->pos += hdr->size + hdr->payload_size;
  884. return 0;
  885. }
  886. dev_dbg(tplg->dev, "ASoC: adding %d kcontrols at 0x%lx\n", hdr->count,
  887. soc_tplg_get_offset(tplg));
  888. for (i = 0; i < hdr->count; i++) {
  889. control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
  890. if (control_hdr->size != sizeof(*control_hdr)) {
  891. dev_err(tplg->dev, "ASoC: invalid control size\n");
  892. return -EINVAL;
  893. }
  894. switch (control_hdr->ops.info) {
  895. case SND_SOC_TPLG_CTL_VOLSW:
  896. case SND_SOC_TPLG_CTL_STROBE:
  897. case SND_SOC_TPLG_CTL_VOLSW_SX:
  898. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  899. case SND_SOC_TPLG_CTL_RANGE:
  900. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  901. case SND_SOC_TPLG_DAPM_CTL_PIN:
  902. soc_tplg_dmixer_create(tplg, 1, hdr->payload_size);
  903. break;
  904. case SND_SOC_TPLG_CTL_ENUM:
  905. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  906. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  907. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  908. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  909. soc_tplg_denum_create(tplg, 1, hdr->payload_size);
  910. break;
  911. case SND_SOC_TPLG_CTL_BYTES:
  912. soc_tplg_dbytes_create(tplg, 1, hdr->payload_size);
  913. break;
  914. default:
  915. soc_bind_err(tplg, control_hdr, i);
  916. return -EINVAL;
  917. }
  918. }
  919. return 0;
  920. }
  921. /* optionally pass new dynamic kcontrol to component driver. */
  922. static int soc_tplg_add_route(struct soc_tplg *tplg,
  923. struct snd_soc_dapm_route *route)
  924. {
  925. if (tplg->comp && tplg->ops && tplg->ops->dapm_route_load)
  926. return tplg->ops->dapm_route_load(tplg->comp, tplg->index,
  927. route);
  928. return 0;
  929. }
  930. static int soc_tplg_dapm_graph_elems_load(struct soc_tplg *tplg,
  931. struct snd_soc_tplg_hdr *hdr)
  932. {
  933. struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
  934. struct snd_soc_dapm_route route;
  935. struct snd_soc_tplg_dapm_graph_elem *elem;
  936. int count = hdr->count, i;
  937. if (tplg->pass != SOC_TPLG_PASS_GRAPH) {
  938. tplg->pos += hdr->size + hdr->payload_size;
  939. return 0;
  940. }
  941. if (soc_tplg_check_elem_count(tplg,
  942. sizeof(struct snd_soc_tplg_dapm_graph_elem),
  943. count, hdr->payload_size, "graph")) {
  944. dev_err(tplg->dev, "ASoC: invalid count %d for DAPM routes\n",
  945. count);
  946. return -EINVAL;
  947. }
  948. dev_dbg(tplg->dev, "ASoC: adding %d DAPM routes for index %d\n", count,
  949. hdr->index);
  950. for (i = 0; i < count; i++) {
  951. elem = (struct snd_soc_tplg_dapm_graph_elem *)tplg->pos;
  952. tplg->pos += sizeof(struct snd_soc_tplg_dapm_graph_elem);
  953. /* validate routes */
  954. if (strnlen(elem->source, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  955. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  956. return -EINVAL;
  957. if (strnlen(elem->sink, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  958. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  959. return -EINVAL;
  960. if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  961. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  962. return -EINVAL;
  963. route.source = elem->source;
  964. route.sink = elem->sink;
  965. route.connected = NULL; /* set to NULL atm for tplg users */
  966. if (strnlen(elem->control, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) == 0)
  967. route.control = NULL;
  968. else
  969. route.control = elem->control;
  970. soc_tplg_add_route(tplg, &route);
  971. /* add route, but keep going if some fail */
  972. snd_soc_dapm_add_routes(dapm, &route, 1);
  973. }
  974. return 0;
  975. }
  976. static struct snd_kcontrol_new *soc_tplg_dapm_widget_dmixer_create(
  977. struct soc_tplg *tplg, int num_kcontrols)
  978. {
  979. struct snd_kcontrol_new *kc;
  980. struct soc_mixer_control *sm;
  981. struct snd_soc_tplg_mixer_control *mc;
  982. int i, err;
  983. kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
  984. if (kc == NULL)
  985. return NULL;
  986. for (i = 0; i < num_kcontrols; i++) {
  987. mc = (struct snd_soc_tplg_mixer_control *)tplg->pos;
  988. sm = kzalloc(sizeof(*sm), GFP_KERNEL);
  989. if (sm == NULL)
  990. goto err;
  991. tplg->pos += (sizeof(struct snd_soc_tplg_mixer_control) +
  992. mc->priv.size);
  993. /* validate kcontrol */
  994. if (strnlen(mc->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  995. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  996. goto err_str;
  997. dev_dbg(tplg->dev, " adding DAPM widget mixer control %s at %d\n",
  998. mc->hdr.name, i);
  999. kc[i].name = kstrdup(mc->hdr.name, GFP_KERNEL);
  1000. if (kc[i].name == NULL)
  1001. goto err_str;
  1002. kc[i].private_value = (long)sm;
  1003. kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1004. kc[i].access = mc->hdr.access;
  1005. /* we only support FL/FR channel mapping atm */
  1006. sm->reg = tplc_chan_get_reg(tplg, mc->channel,
  1007. SNDRV_CHMAP_FL);
  1008. sm->rreg = tplc_chan_get_reg(tplg, mc->channel,
  1009. SNDRV_CHMAP_FR);
  1010. sm->shift = tplc_chan_get_shift(tplg, mc->channel,
  1011. SNDRV_CHMAP_FL);
  1012. sm->rshift = tplc_chan_get_shift(tplg, mc->channel,
  1013. SNDRV_CHMAP_FR);
  1014. sm->max = mc->max;
  1015. sm->min = mc->min;
  1016. sm->invert = mc->invert;
  1017. sm->platform_max = mc->platform_max;
  1018. sm->dobj.index = tplg->index;
  1019. INIT_LIST_HEAD(&sm->dobj.list);
  1020. /* map io handlers */
  1021. err = soc_tplg_kcontrol_bind_io(&mc->hdr, &kc[i], tplg);
  1022. if (err) {
  1023. soc_control_err(tplg, &mc->hdr, mc->hdr.name);
  1024. kfree(sm);
  1025. continue;
  1026. }
  1027. /* pass control to driver for optional further init */
  1028. err = soc_tplg_init_kcontrol(tplg, &kc[i],
  1029. (struct snd_soc_tplg_ctl_hdr *)mc);
  1030. if (err < 0) {
  1031. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  1032. mc->hdr.name);
  1033. kfree(sm);
  1034. continue;
  1035. }
  1036. /* create any TLV data */
  1037. soc_tplg_create_tlv(tplg, &kc[i], &mc->hdr);
  1038. }
  1039. return kc;
  1040. err_str:
  1041. kfree(sm);
  1042. err:
  1043. for (--i; i >= 0; i--) {
  1044. kfree((void *)kc[i].private_value);
  1045. kfree(kc[i].name);
  1046. }
  1047. kfree(kc);
  1048. return NULL;
  1049. }
  1050. static struct snd_kcontrol_new *soc_tplg_dapm_widget_denum_create(
  1051. struct soc_tplg *tplg, int num_kcontrols)
  1052. {
  1053. struct snd_kcontrol_new *kc;
  1054. struct snd_soc_tplg_enum_control *ec;
  1055. struct soc_enum *se;
  1056. int i, j, err;
  1057. kc = kcalloc(num_kcontrols, sizeof(*kc), GFP_KERNEL);
  1058. if (kc == NULL)
  1059. return NULL;
  1060. for (i = 0; i < num_kcontrols; i++) {
  1061. ec = (struct snd_soc_tplg_enum_control *)tplg->pos;
  1062. /* validate kcontrol */
  1063. if (strnlen(ec->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1064. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1065. goto err;
  1066. se = kzalloc(sizeof(*se), GFP_KERNEL);
  1067. if (se == NULL)
  1068. goto err;
  1069. dev_dbg(tplg->dev, " adding DAPM widget enum control %s\n",
  1070. ec->hdr.name);
  1071. kc[i].name = kstrdup(ec->hdr.name, GFP_KERNEL);
  1072. if (kc[i].name == NULL) {
  1073. kfree(se);
  1074. goto err_se;
  1075. }
  1076. kc[i].private_value = (long)se;
  1077. kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1078. kc[i].access = ec->hdr.access;
  1079. /* we only support FL/FR channel mapping atm */
  1080. se->reg = tplc_chan_get_reg(tplg, ec->channel, SNDRV_CHMAP_FL);
  1081. se->shift_l = tplc_chan_get_shift(tplg, ec->channel,
  1082. SNDRV_CHMAP_FL);
  1083. se->shift_r = tplc_chan_get_shift(tplg, ec->channel,
  1084. SNDRV_CHMAP_FR);
  1085. se->items = ec->items;
  1086. se->mask = ec->mask;
  1087. se->dobj.index = tplg->index;
  1088. switch (ec->hdr.ops.info) {
  1089. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  1090. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  1091. err = soc_tplg_denum_create_values(se, ec);
  1092. if (err < 0) {
  1093. dev_err(tplg->dev, "ASoC: could not create values for %s\n",
  1094. ec->hdr.name);
  1095. goto err_se;
  1096. }
  1097. /* fall through to create texts */
  1098. case SND_SOC_TPLG_CTL_ENUM:
  1099. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  1100. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  1101. err = soc_tplg_denum_create_texts(se, ec);
  1102. if (err < 0) {
  1103. dev_err(tplg->dev, "ASoC: could not create texts for %s\n",
  1104. ec->hdr.name);
  1105. goto err_se;
  1106. }
  1107. break;
  1108. default:
  1109. dev_err(tplg->dev, "ASoC: invalid enum control type %d for %s\n",
  1110. ec->hdr.ops.info, ec->hdr.name);
  1111. goto err_se;
  1112. }
  1113. /* map io handlers */
  1114. err = soc_tplg_kcontrol_bind_io(&ec->hdr, &kc[i], tplg);
  1115. if (err) {
  1116. soc_control_err(tplg, &ec->hdr, ec->hdr.name);
  1117. goto err_se;
  1118. }
  1119. /* pass control to driver for optional further init */
  1120. err = soc_tplg_init_kcontrol(tplg, &kc[i],
  1121. (struct snd_soc_tplg_ctl_hdr *)ec);
  1122. if (err < 0) {
  1123. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  1124. ec->hdr.name);
  1125. goto err_se;
  1126. }
  1127. tplg->pos += (sizeof(struct snd_soc_tplg_enum_control) +
  1128. ec->priv.size);
  1129. }
  1130. return kc;
  1131. err_se:
  1132. for (; i >= 0; i--) {
  1133. /* free values and texts */
  1134. se = (struct soc_enum *)kc[i].private_value;
  1135. if (!se)
  1136. continue;
  1137. kfree(se->dobj.control.dvalues);
  1138. for (j = 0; j < ec->items; j++)
  1139. kfree(se->dobj.control.dtexts[j]);
  1140. kfree(se);
  1141. kfree(kc[i].name);
  1142. }
  1143. err:
  1144. kfree(kc);
  1145. return NULL;
  1146. }
  1147. static struct snd_kcontrol_new *soc_tplg_dapm_widget_dbytes_create(
  1148. struct soc_tplg *tplg, int count)
  1149. {
  1150. struct snd_soc_tplg_bytes_control *be;
  1151. struct soc_bytes_ext *sbe;
  1152. struct snd_kcontrol_new *kc;
  1153. int i, err;
  1154. kc = kcalloc(count, sizeof(*kc), GFP_KERNEL);
  1155. if (!kc)
  1156. return NULL;
  1157. for (i = 0; i < count; i++) {
  1158. be = (struct snd_soc_tplg_bytes_control *)tplg->pos;
  1159. /* validate kcontrol */
  1160. if (strnlen(be->hdr.name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1161. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1162. goto err;
  1163. sbe = kzalloc(sizeof(*sbe), GFP_KERNEL);
  1164. if (sbe == NULL)
  1165. goto err;
  1166. tplg->pos += (sizeof(struct snd_soc_tplg_bytes_control) +
  1167. be->priv.size);
  1168. dev_dbg(tplg->dev,
  1169. "ASoC: adding bytes kcontrol %s with access 0x%x\n",
  1170. be->hdr.name, be->hdr.access);
  1171. kc[i].name = kstrdup(be->hdr.name, GFP_KERNEL);
  1172. if (kc[i].name == NULL) {
  1173. kfree(sbe);
  1174. goto err;
  1175. }
  1176. kc[i].private_value = (long)sbe;
  1177. kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1178. kc[i].access = be->hdr.access;
  1179. sbe->max = be->max;
  1180. INIT_LIST_HEAD(&sbe->dobj.list);
  1181. /* map standard io handlers and check for external handlers */
  1182. err = soc_tplg_kcontrol_bind_io(&be->hdr, &kc[i], tplg);
  1183. if (err) {
  1184. soc_control_err(tplg, &be->hdr, be->hdr.name);
  1185. kfree(sbe);
  1186. continue;
  1187. }
  1188. /* pass control to driver for optional further init */
  1189. err = soc_tplg_init_kcontrol(tplg, &kc[i],
  1190. (struct snd_soc_tplg_ctl_hdr *)be);
  1191. if (err < 0) {
  1192. dev_err(tplg->dev, "ASoC: failed to init %s\n",
  1193. be->hdr.name);
  1194. kfree(sbe);
  1195. continue;
  1196. }
  1197. }
  1198. return kc;
  1199. err:
  1200. for (--i; i >= 0; i--) {
  1201. kfree((void *)kc[i].private_value);
  1202. kfree(kc[i].name);
  1203. }
  1204. kfree(kc);
  1205. return NULL;
  1206. }
  1207. static int soc_tplg_dapm_widget_create(struct soc_tplg *tplg,
  1208. struct snd_soc_tplg_dapm_widget *w)
  1209. {
  1210. struct snd_soc_dapm_context *dapm = &tplg->comp->dapm;
  1211. struct snd_soc_dapm_widget template, *widget;
  1212. struct snd_soc_tplg_ctl_hdr *control_hdr;
  1213. struct snd_soc_card *card = tplg->comp->card;
  1214. unsigned int kcontrol_type;
  1215. int ret = 0;
  1216. if (strnlen(w->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1217. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1218. return -EINVAL;
  1219. if (strnlen(w->sname, SNDRV_CTL_ELEM_ID_NAME_MAXLEN) ==
  1220. SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1221. return -EINVAL;
  1222. dev_dbg(tplg->dev, "ASoC: creating DAPM widget %s id %d\n",
  1223. w->name, w->id);
  1224. memset(&template, 0, sizeof(template));
  1225. /* map user to kernel widget ID */
  1226. template.id = get_widget_id(w->id);
  1227. if (template.id < 0)
  1228. return template.id;
  1229. /* strings are allocated here, but used and freed by the widget */
  1230. template.name = kstrdup(w->name, GFP_KERNEL);
  1231. if (!template.name)
  1232. return -ENOMEM;
  1233. template.sname = kstrdup(w->sname, GFP_KERNEL);
  1234. if (!template.sname) {
  1235. ret = -ENOMEM;
  1236. goto err;
  1237. }
  1238. template.reg = w->reg;
  1239. template.shift = w->shift;
  1240. template.mask = w->mask;
  1241. template.subseq = w->subseq;
  1242. template.on_val = w->invert ? 0 : 1;
  1243. template.off_val = w->invert ? 1 : 0;
  1244. template.ignore_suspend = w->ignore_suspend;
  1245. template.event_flags = w->event_flags;
  1246. template.dobj.index = tplg->index;
  1247. tplg->pos +=
  1248. (sizeof(struct snd_soc_tplg_dapm_widget) + w->priv.size);
  1249. if (w->num_kcontrols == 0) {
  1250. kcontrol_type = 0;
  1251. template.num_kcontrols = 0;
  1252. goto widget;
  1253. }
  1254. control_hdr = (struct snd_soc_tplg_ctl_hdr *)tplg->pos;
  1255. dev_dbg(tplg->dev, "ASoC: template %s has %d controls of type %x\n",
  1256. w->name, w->num_kcontrols, control_hdr->type);
  1257. switch (control_hdr->ops.info) {
  1258. case SND_SOC_TPLG_CTL_VOLSW:
  1259. case SND_SOC_TPLG_CTL_STROBE:
  1260. case SND_SOC_TPLG_CTL_VOLSW_SX:
  1261. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  1262. case SND_SOC_TPLG_CTL_RANGE:
  1263. case SND_SOC_TPLG_DAPM_CTL_VOLSW:
  1264. kcontrol_type = SND_SOC_TPLG_TYPE_MIXER; /* volume mixer */
  1265. template.num_kcontrols = w->num_kcontrols;
  1266. template.kcontrol_news =
  1267. soc_tplg_dapm_widget_dmixer_create(tplg,
  1268. template.num_kcontrols);
  1269. if (!template.kcontrol_news) {
  1270. ret = -ENOMEM;
  1271. goto hdr_err;
  1272. }
  1273. break;
  1274. case SND_SOC_TPLG_CTL_ENUM:
  1275. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  1276. case SND_SOC_TPLG_DAPM_CTL_ENUM_DOUBLE:
  1277. case SND_SOC_TPLG_DAPM_CTL_ENUM_VIRT:
  1278. case SND_SOC_TPLG_DAPM_CTL_ENUM_VALUE:
  1279. kcontrol_type = SND_SOC_TPLG_TYPE_ENUM; /* enumerated mixer */
  1280. template.num_kcontrols = w->num_kcontrols;
  1281. template.kcontrol_news =
  1282. soc_tplg_dapm_widget_denum_create(tplg,
  1283. template.num_kcontrols);
  1284. if (!template.kcontrol_news) {
  1285. ret = -ENOMEM;
  1286. goto hdr_err;
  1287. }
  1288. break;
  1289. case SND_SOC_TPLG_CTL_BYTES:
  1290. kcontrol_type = SND_SOC_TPLG_TYPE_BYTES; /* bytes control */
  1291. template.num_kcontrols = w->num_kcontrols;
  1292. template.kcontrol_news =
  1293. soc_tplg_dapm_widget_dbytes_create(tplg,
  1294. template.num_kcontrols);
  1295. if (!template.kcontrol_news) {
  1296. ret = -ENOMEM;
  1297. goto hdr_err;
  1298. }
  1299. break;
  1300. default:
  1301. dev_err(tplg->dev, "ASoC: invalid widget control type %d:%d:%d\n",
  1302. control_hdr->ops.get, control_hdr->ops.put,
  1303. control_hdr->ops.info);
  1304. ret = -EINVAL;
  1305. goto hdr_err;
  1306. }
  1307. widget:
  1308. ret = soc_tplg_widget_load(tplg, &template, w);
  1309. if (ret < 0)
  1310. goto hdr_err;
  1311. /* card dapm mutex is held by the core if we are loading topology
  1312. * data during sound card init. */
  1313. if (card->instantiated)
  1314. widget = snd_soc_dapm_new_control(dapm, &template);
  1315. else
  1316. widget = snd_soc_dapm_new_control_unlocked(dapm, &template);
  1317. if (IS_ERR(widget)) {
  1318. ret = PTR_ERR(widget);
  1319. /* Do not nag about probe deferrals */
  1320. if (ret != -EPROBE_DEFER)
  1321. dev_err(tplg->dev,
  1322. "ASoC: failed to create widget %s controls (%d)\n",
  1323. w->name, ret);
  1324. goto hdr_err;
  1325. }
  1326. if (widget == NULL) {
  1327. dev_err(tplg->dev, "ASoC: failed to create widget %s controls\n",
  1328. w->name);
  1329. ret = -ENOMEM;
  1330. goto hdr_err;
  1331. }
  1332. widget->dobj.type = SND_SOC_DOBJ_WIDGET;
  1333. widget->dobj.widget.kcontrol_type = kcontrol_type;
  1334. widget->dobj.ops = tplg->ops;
  1335. widget->dobj.index = tplg->index;
  1336. list_add(&widget->dobj.list, &tplg->comp->dobj_list);
  1337. ret = soc_tplg_widget_ready(tplg, widget, w);
  1338. if (ret < 0)
  1339. goto ready_err;
  1340. return 0;
  1341. ready_err:
  1342. snd_soc_tplg_widget_remove(widget);
  1343. snd_soc_dapm_free_widget(widget);
  1344. hdr_err:
  1345. kfree(template.sname);
  1346. err:
  1347. kfree(template.name);
  1348. return ret;
  1349. }
  1350. static int soc_tplg_dapm_widget_elems_load(struct soc_tplg *tplg,
  1351. struct snd_soc_tplg_hdr *hdr)
  1352. {
  1353. struct snd_soc_tplg_dapm_widget *widget;
  1354. int ret, count = hdr->count, i;
  1355. if (tplg->pass != SOC_TPLG_PASS_WIDGET)
  1356. return 0;
  1357. dev_dbg(tplg->dev, "ASoC: adding %d DAPM widgets\n", count);
  1358. for (i = 0; i < count; i++) {
  1359. widget = (struct snd_soc_tplg_dapm_widget *) tplg->pos;
  1360. if (widget->size != sizeof(*widget)) {
  1361. dev_err(tplg->dev, "ASoC: invalid widget size\n");
  1362. return -EINVAL;
  1363. }
  1364. ret = soc_tplg_dapm_widget_create(tplg, widget);
  1365. if (ret < 0) {
  1366. dev_err(tplg->dev, "ASoC: failed to load widget %s\n",
  1367. widget->name);
  1368. return ret;
  1369. }
  1370. }
  1371. return 0;
  1372. }
  1373. static int soc_tplg_dapm_complete(struct soc_tplg *tplg)
  1374. {
  1375. struct snd_soc_card *card = tplg->comp->card;
  1376. int ret;
  1377. /* Card might not have been registered at this point.
  1378. * If so, just return success.
  1379. */
  1380. if (!card || !card->instantiated) {
  1381. dev_warn(tplg->dev, "ASoC: Parent card not yet available,"
  1382. " widget card binding deferred\n");
  1383. return 0;
  1384. }
  1385. ret = snd_soc_dapm_new_widgets(card);
  1386. if (ret < 0)
  1387. dev_err(tplg->dev, "ASoC: failed to create new widgets %d\n",
  1388. ret);
  1389. return 0;
  1390. }
  1391. static void set_stream_info(struct snd_soc_pcm_stream *stream,
  1392. struct snd_soc_tplg_stream_caps *caps)
  1393. {
  1394. stream->stream_name = kstrdup(caps->name, GFP_KERNEL);
  1395. stream->channels_min = caps->channels_min;
  1396. stream->channels_max = caps->channels_max;
  1397. stream->rates = caps->rates;
  1398. stream->rate_min = caps->rate_min;
  1399. stream->rate_max = caps->rate_max;
  1400. stream->formats = caps->formats;
  1401. stream->sig_bits = caps->sig_bits;
  1402. }
  1403. static void set_dai_flags(struct snd_soc_dai_driver *dai_drv,
  1404. unsigned int flag_mask, unsigned int flags)
  1405. {
  1406. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES)
  1407. dai_drv->symmetric_rates =
  1408. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
  1409. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS)
  1410. dai_drv->symmetric_channels =
  1411. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_CHANNELS ?
  1412. 1 : 0;
  1413. if (flag_mask & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS)
  1414. dai_drv->symmetric_samplebits =
  1415. flags & SND_SOC_TPLG_DAI_FLGBIT_SYMMETRIC_SAMPLEBITS ?
  1416. 1 : 0;
  1417. }
  1418. static int soc_tplg_dai_create(struct soc_tplg *tplg,
  1419. struct snd_soc_tplg_pcm *pcm)
  1420. {
  1421. struct snd_soc_dai_driver *dai_drv;
  1422. struct snd_soc_pcm_stream *stream;
  1423. struct snd_soc_tplg_stream_caps *caps;
  1424. int ret;
  1425. dai_drv = kzalloc(sizeof(struct snd_soc_dai_driver), GFP_KERNEL);
  1426. if (dai_drv == NULL)
  1427. return -ENOMEM;
  1428. if (strlen(pcm->dai_name))
  1429. dai_drv->name = kstrdup(pcm->dai_name, GFP_KERNEL);
  1430. dai_drv->id = pcm->dai_id;
  1431. if (pcm->playback) {
  1432. stream = &dai_drv->playback;
  1433. caps = &pcm->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1434. set_stream_info(stream, caps);
  1435. }
  1436. if (pcm->capture) {
  1437. stream = &dai_drv->capture;
  1438. caps = &pcm->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1439. set_stream_info(stream, caps);
  1440. }
  1441. if (pcm->compress)
  1442. dai_drv->compress_new = snd_soc_new_compress;
  1443. /* pass control to component driver for optional further init */
  1444. ret = soc_tplg_dai_load(tplg, dai_drv, pcm, NULL);
  1445. if (ret < 0) {
  1446. dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
  1447. kfree(dai_drv);
  1448. return ret;
  1449. }
  1450. dai_drv->dobj.index = tplg->index;
  1451. dai_drv->dobj.ops = tplg->ops;
  1452. dai_drv->dobj.type = SND_SOC_DOBJ_PCM;
  1453. list_add(&dai_drv->dobj.list, &tplg->comp->dobj_list);
  1454. /* register the DAI to the component */
  1455. return snd_soc_register_dai(tplg->comp, dai_drv);
  1456. }
  1457. static void set_link_flags(struct snd_soc_dai_link *link,
  1458. unsigned int flag_mask, unsigned int flags)
  1459. {
  1460. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES)
  1461. link->symmetric_rates =
  1462. flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_RATES ? 1 : 0;
  1463. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS)
  1464. link->symmetric_channels =
  1465. flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_CHANNELS ?
  1466. 1 : 0;
  1467. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS)
  1468. link->symmetric_samplebits =
  1469. flags & SND_SOC_TPLG_LNK_FLGBIT_SYMMETRIC_SAMPLEBITS ?
  1470. 1 : 0;
  1471. if (flag_mask & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP)
  1472. link->ignore_suspend =
  1473. flags & SND_SOC_TPLG_LNK_FLGBIT_VOICE_WAKEUP ?
  1474. 1 : 0;
  1475. }
  1476. /* create the FE DAI link */
  1477. static int soc_tplg_fe_link_create(struct soc_tplg *tplg,
  1478. struct snd_soc_tplg_pcm *pcm)
  1479. {
  1480. struct snd_soc_dai_link *link;
  1481. int ret;
  1482. link = kzalloc(sizeof(struct snd_soc_dai_link), GFP_KERNEL);
  1483. if (link == NULL)
  1484. return -ENOMEM;
  1485. if (strlen(pcm->pcm_name)) {
  1486. link->name = kstrdup(pcm->pcm_name, GFP_KERNEL);
  1487. link->stream_name = kstrdup(pcm->pcm_name, GFP_KERNEL);
  1488. }
  1489. link->id = pcm->pcm_id;
  1490. if (strlen(pcm->dai_name))
  1491. link->cpu_dai_name = kstrdup(pcm->dai_name, GFP_KERNEL);
  1492. link->codec_name = "snd-soc-dummy";
  1493. link->codec_dai_name = "snd-soc-dummy-dai";
  1494. /* enable DPCM */
  1495. link->dynamic = 1;
  1496. link->dpcm_playback = pcm->playback;
  1497. link->dpcm_capture = pcm->capture;
  1498. if (pcm->flag_mask)
  1499. set_link_flags(link, pcm->flag_mask, pcm->flags);
  1500. /* pass control to component driver for optional further init */
  1501. ret = soc_tplg_dai_link_load(tplg, link, NULL);
  1502. if (ret < 0) {
  1503. dev_err(tplg->comp->dev, "ASoC: FE link loading failed\n");
  1504. kfree(link);
  1505. return ret;
  1506. }
  1507. link->dobj.index = tplg->index;
  1508. link->dobj.ops = tplg->ops;
  1509. link->dobj.type = SND_SOC_DOBJ_DAI_LINK;
  1510. list_add(&link->dobj.list, &tplg->comp->dobj_list);
  1511. snd_soc_add_dai_link(tplg->comp->card, link);
  1512. return 0;
  1513. }
  1514. /* create a FE DAI and DAI link from the PCM object */
  1515. static int soc_tplg_pcm_create(struct soc_tplg *tplg,
  1516. struct snd_soc_tplg_pcm *pcm)
  1517. {
  1518. int ret;
  1519. ret = soc_tplg_dai_create(tplg, pcm);
  1520. if (ret < 0)
  1521. return ret;
  1522. return soc_tplg_fe_link_create(tplg, pcm);
  1523. }
  1524. /* copy stream caps from the old version 4 of source */
  1525. static void stream_caps_new_ver(struct snd_soc_tplg_stream_caps *dest,
  1526. struct snd_soc_tplg_stream_caps_v4 *src)
  1527. {
  1528. dest->size = sizeof(*dest);
  1529. memcpy(dest->name, src->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1530. dest->formats = src->formats;
  1531. dest->rates = src->rates;
  1532. dest->rate_min = src->rate_min;
  1533. dest->rate_max = src->rate_max;
  1534. dest->channels_min = src->channels_min;
  1535. dest->channels_max = src->channels_max;
  1536. dest->periods_min = src->periods_min;
  1537. dest->periods_max = src->periods_max;
  1538. dest->period_size_min = src->period_size_min;
  1539. dest->period_size_max = src->period_size_max;
  1540. dest->buffer_size_min = src->buffer_size_min;
  1541. dest->buffer_size_max = src->buffer_size_max;
  1542. }
  1543. /**
  1544. * pcm_new_ver - Create the new version of PCM from the old version.
  1545. * @tplg: topology context
  1546. * @src: older version of pcm as a source
  1547. * @pcm: latest version of pcm created from the source
  1548. *
  1549. * Support from vesion 4. User should free the returned pcm manually.
  1550. */
  1551. static int pcm_new_ver(struct soc_tplg *tplg,
  1552. struct snd_soc_tplg_pcm *src,
  1553. struct snd_soc_tplg_pcm **pcm)
  1554. {
  1555. struct snd_soc_tplg_pcm *dest;
  1556. struct snd_soc_tplg_pcm_v4 *src_v4;
  1557. int i;
  1558. *pcm = NULL;
  1559. if (src->size != sizeof(*src_v4)) {
  1560. dev_err(tplg->dev, "ASoC: invalid PCM size\n");
  1561. return -EINVAL;
  1562. }
  1563. dev_warn(tplg->dev, "ASoC: old version of PCM\n");
  1564. src_v4 = (struct snd_soc_tplg_pcm_v4 *)src;
  1565. dest = kzalloc(sizeof(*dest), GFP_KERNEL);
  1566. if (!dest)
  1567. return -ENOMEM;
  1568. dest->size = sizeof(*dest); /* size of latest abi version */
  1569. memcpy(dest->pcm_name, src_v4->pcm_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1570. memcpy(dest->dai_name, src_v4->dai_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1571. dest->pcm_id = src_v4->pcm_id;
  1572. dest->dai_id = src_v4->dai_id;
  1573. dest->playback = src_v4->playback;
  1574. dest->capture = src_v4->capture;
  1575. dest->compress = src_v4->compress;
  1576. dest->num_streams = src_v4->num_streams;
  1577. for (i = 0; i < dest->num_streams; i++)
  1578. memcpy(&dest->stream[i], &src_v4->stream[i],
  1579. sizeof(struct snd_soc_tplg_stream));
  1580. for (i = 0; i < 2; i++)
  1581. stream_caps_new_ver(&dest->caps[i], &src_v4->caps[i]);
  1582. *pcm = dest;
  1583. return 0;
  1584. }
  1585. static int soc_tplg_pcm_elems_load(struct soc_tplg *tplg,
  1586. struct snd_soc_tplg_hdr *hdr)
  1587. {
  1588. struct snd_soc_tplg_pcm *pcm, *_pcm;
  1589. int count = hdr->count;
  1590. int i;
  1591. bool abi_match;
  1592. if (tplg->pass != SOC_TPLG_PASS_PCM_DAI)
  1593. return 0;
  1594. /* check the element size and count */
  1595. pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
  1596. if (pcm->size > sizeof(struct snd_soc_tplg_pcm)
  1597. || pcm->size < sizeof(struct snd_soc_tplg_pcm_v4)) {
  1598. dev_err(tplg->dev, "ASoC: invalid size %d for PCM elems\n",
  1599. pcm->size);
  1600. return -EINVAL;
  1601. }
  1602. if (soc_tplg_check_elem_count(tplg,
  1603. pcm->size, count,
  1604. hdr->payload_size, "PCM DAI")) {
  1605. dev_err(tplg->dev, "ASoC: invalid count %d for PCM DAI elems\n",
  1606. count);
  1607. return -EINVAL;
  1608. }
  1609. for (i = 0; i < count; i++) {
  1610. pcm = (struct snd_soc_tplg_pcm *)tplg->pos;
  1611. /* check ABI version by size, create a new version of pcm
  1612. * if abi not match.
  1613. */
  1614. if (pcm->size == sizeof(*pcm)) {
  1615. abi_match = true;
  1616. _pcm = pcm;
  1617. } else {
  1618. abi_match = false;
  1619. pcm_new_ver(tplg, pcm, &_pcm);
  1620. }
  1621. /* create the FE DAIs and DAI links */
  1622. soc_tplg_pcm_create(tplg, _pcm);
  1623. /* offset by version-specific struct size and
  1624. * real priv data size
  1625. */
  1626. tplg->pos += pcm->size + _pcm->priv.size;
  1627. if (!abi_match)
  1628. kfree(_pcm); /* free the duplicated one */
  1629. }
  1630. dev_dbg(tplg->dev, "ASoC: adding %d PCM DAIs\n", count);
  1631. return 0;
  1632. }
  1633. /**
  1634. * set_link_hw_format - Set the HW audio format of the physical DAI link.
  1635. * @link: &snd_soc_dai_link which should be updated
  1636. * @cfg: physical link configs.
  1637. *
  1638. * Topology context contains a list of supported HW formats (configs) and
  1639. * a default format ID for the physical link. This function will use this
  1640. * default ID to choose the HW format to set the link's DAI format for init.
  1641. */
  1642. static void set_link_hw_format(struct snd_soc_dai_link *link,
  1643. struct snd_soc_tplg_link_config *cfg)
  1644. {
  1645. struct snd_soc_tplg_hw_config *hw_config;
  1646. unsigned char bclk_master, fsync_master;
  1647. unsigned char invert_bclk, invert_fsync;
  1648. int i;
  1649. for (i = 0; i < cfg->num_hw_configs; i++) {
  1650. hw_config = &cfg->hw_config[i];
  1651. if (hw_config->id != cfg->default_hw_config_id)
  1652. continue;
  1653. link->dai_fmt = hw_config->fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  1654. /* clock gating */
  1655. switch (hw_config->clock_gated) {
  1656. case SND_SOC_TPLG_DAI_CLK_GATE_GATED:
  1657. link->dai_fmt |= SND_SOC_DAIFMT_GATED;
  1658. break;
  1659. case SND_SOC_TPLG_DAI_CLK_GATE_CONT:
  1660. link->dai_fmt |= SND_SOC_DAIFMT_CONT;
  1661. break;
  1662. default:
  1663. /* ignore the value */
  1664. break;
  1665. }
  1666. /* clock signal polarity */
  1667. invert_bclk = hw_config->invert_bclk;
  1668. invert_fsync = hw_config->invert_fsync;
  1669. if (!invert_bclk && !invert_fsync)
  1670. link->dai_fmt |= SND_SOC_DAIFMT_NB_NF;
  1671. else if (!invert_bclk && invert_fsync)
  1672. link->dai_fmt |= SND_SOC_DAIFMT_NB_IF;
  1673. else if (invert_bclk && !invert_fsync)
  1674. link->dai_fmt |= SND_SOC_DAIFMT_IB_NF;
  1675. else
  1676. link->dai_fmt |= SND_SOC_DAIFMT_IB_IF;
  1677. /* clock masters */
  1678. bclk_master = (hw_config->bclk_master ==
  1679. SND_SOC_TPLG_BCLK_CM);
  1680. fsync_master = (hw_config->fsync_master ==
  1681. SND_SOC_TPLG_FSYNC_CM);
  1682. if (bclk_master && fsync_master)
  1683. link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1684. else if (!bclk_master && fsync_master)
  1685. link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1686. else if (bclk_master && !fsync_master)
  1687. link->dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1688. else
  1689. link->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1690. }
  1691. }
  1692. /**
  1693. * link_new_ver - Create a new physical link config from the old
  1694. * version of source.
  1695. * @tplg: topology context
  1696. * @src: old version of phyical link config as a source
  1697. * @link: latest version of physical link config created from the source
  1698. *
  1699. * Support from vesion 4. User need free the returned link config manually.
  1700. */
  1701. static int link_new_ver(struct soc_tplg *tplg,
  1702. struct snd_soc_tplg_link_config *src,
  1703. struct snd_soc_tplg_link_config **link)
  1704. {
  1705. struct snd_soc_tplg_link_config *dest;
  1706. struct snd_soc_tplg_link_config_v4 *src_v4;
  1707. int i;
  1708. *link = NULL;
  1709. if (src->size != sizeof(struct snd_soc_tplg_link_config_v4)) {
  1710. dev_err(tplg->dev, "ASoC: invalid physical link config size\n");
  1711. return -EINVAL;
  1712. }
  1713. dev_warn(tplg->dev, "ASoC: old version of physical link config\n");
  1714. src_v4 = (struct snd_soc_tplg_link_config_v4 *)src;
  1715. dest = kzalloc(sizeof(*dest), GFP_KERNEL);
  1716. if (!dest)
  1717. return -ENOMEM;
  1718. dest->size = sizeof(*dest);
  1719. dest->id = src_v4->id;
  1720. dest->num_streams = src_v4->num_streams;
  1721. for (i = 0; i < dest->num_streams; i++)
  1722. memcpy(&dest->stream[i], &src_v4->stream[i],
  1723. sizeof(struct snd_soc_tplg_stream));
  1724. *link = dest;
  1725. return 0;
  1726. }
  1727. /* Find and configure an existing physical DAI link */
  1728. static int soc_tplg_link_config(struct soc_tplg *tplg,
  1729. struct snd_soc_tplg_link_config *cfg)
  1730. {
  1731. struct snd_soc_dai_link *link;
  1732. const char *name, *stream_name;
  1733. size_t len;
  1734. int ret;
  1735. len = strnlen(cfg->name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1736. if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1737. return -EINVAL;
  1738. else if (len)
  1739. name = cfg->name;
  1740. else
  1741. name = NULL;
  1742. len = strnlen(cfg->stream_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
  1743. if (len == SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
  1744. return -EINVAL;
  1745. else if (len)
  1746. stream_name = cfg->stream_name;
  1747. else
  1748. stream_name = NULL;
  1749. link = snd_soc_find_dai_link(tplg->comp->card, cfg->id,
  1750. name, stream_name);
  1751. if (!link) {
  1752. dev_err(tplg->dev, "ASoC: physical link %s (id %d) not exist\n",
  1753. name, cfg->id);
  1754. return -EINVAL;
  1755. }
  1756. /* hw format */
  1757. if (cfg->num_hw_configs)
  1758. set_link_hw_format(link, cfg);
  1759. /* flags */
  1760. if (cfg->flag_mask)
  1761. set_link_flags(link, cfg->flag_mask, cfg->flags);
  1762. /* pass control to component driver for optional further init */
  1763. ret = soc_tplg_dai_link_load(tplg, link, cfg);
  1764. if (ret < 0) {
  1765. dev_err(tplg->dev, "ASoC: physical link loading failed\n");
  1766. return ret;
  1767. }
  1768. return 0;
  1769. }
  1770. /* Load physical link config elements from the topology context */
  1771. static int soc_tplg_link_elems_load(struct soc_tplg *tplg,
  1772. struct snd_soc_tplg_hdr *hdr)
  1773. {
  1774. struct snd_soc_tplg_link_config *link, *_link;
  1775. int count = hdr->count;
  1776. int i, ret;
  1777. bool abi_match;
  1778. if (tplg->pass != SOC_TPLG_PASS_LINK) {
  1779. tplg->pos += hdr->size + hdr->payload_size;
  1780. return 0;
  1781. };
  1782. /* check the element size and count */
  1783. link = (struct snd_soc_tplg_link_config *)tplg->pos;
  1784. if (link->size > sizeof(struct snd_soc_tplg_link_config)
  1785. || link->size < sizeof(struct snd_soc_tplg_link_config_v4)) {
  1786. dev_err(tplg->dev, "ASoC: invalid size %d for physical link elems\n",
  1787. link->size);
  1788. return -EINVAL;
  1789. }
  1790. if (soc_tplg_check_elem_count(tplg,
  1791. link->size, count,
  1792. hdr->payload_size, "physical link config")) {
  1793. dev_err(tplg->dev, "ASoC: invalid count %d for physical link elems\n",
  1794. count);
  1795. return -EINVAL;
  1796. }
  1797. /* config physical DAI links */
  1798. for (i = 0; i < count; i++) {
  1799. link = (struct snd_soc_tplg_link_config *)tplg->pos;
  1800. if (link->size == sizeof(*link)) {
  1801. abi_match = true;
  1802. _link = link;
  1803. } else {
  1804. abi_match = false;
  1805. ret = link_new_ver(tplg, link, &_link);
  1806. if (ret < 0)
  1807. return ret;
  1808. }
  1809. ret = soc_tplg_link_config(tplg, _link);
  1810. if (ret < 0)
  1811. return ret;
  1812. /* offset by version-specific struct size and
  1813. * real priv data size
  1814. */
  1815. tplg->pos += link->size + _link->priv.size;
  1816. if (!abi_match)
  1817. kfree(_link); /* free the duplicated one */
  1818. }
  1819. return 0;
  1820. }
  1821. /**
  1822. * soc_tplg_dai_config - Find and configure an existing physical DAI.
  1823. * @tplg: topology context
  1824. * @d: physical DAI configs.
  1825. *
  1826. * The physical dai should already be registered by the platform driver.
  1827. * The platform driver should specify the DAI name and ID for matching.
  1828. */
  1829. static int soc_tplg_dai_config(struct soc_tplg *tplg,
  1830. struct snd_soc_tplg_dai *d)
  1831. {
  1832. struct snd_soc_dai_link_component dai_component = {0};
  1833. struct snd_soc_dai *dai;
  1834. struct snd_soc_dai_driver *dai_drv;
  1835. struct snd_soc_pcm_stream *stream;
  1836. struct snd_soc_tplg_stream_caps *caps;
  1837. int ret;
  1838. dai_component.dai_name = d->dai_name;
  1839. dai = snd_soc_find_dai(&dai_component);
  1840. if (!dai) {
  1841. dev_err(tplg->dev, "ASoC: physical DAI %s not registered\n",
  1842. d->dai_name);
  1843. return -EINVAL;
  1844. }
  1845. if (d->dai_id != dai->id) {
  1846. dev_err(tplg->dev, "ASoC: physical DAI %s id mismatch\n",
  1847. d->dai_name);
  1848. return -EINVAL;
  1849. }
  1850. dai_drv = dai->driver;
  1851. if (!dai_drv)
  1852. return -EINVAL;
  1853. if (d->playback) {
  1854. stream = &dai_drv->playback;
  1855. caps = &d->caps[SND_SOC_TPLG_STREAM_PLAYBACK];
  1856. set_stream_info(stream, caps);
  1857. }
  1858. if (d->capture) {
  1859. stream = &dai_drv->capture;
  1860. caps = &d->caps[SND_SOC_TPLG_STREAM_CAPTURE];
  1861. set_stream_info(stream, caps);
  1862. }
  1863. if (d->flag_mask)
  1864. set_dai_flags(dai_drv, d->flag_mask, d->flags);
  1865. /* pass control to component driver for optional further init */
  1866. ret = soc_tplg_dai_load(tplg, dai_drv, NULL, dai);
  1867. if (ret < 0) {
  1868. dev_err(tplg->comp->dev, "ASoC: DAI loading failed\n");
  1869. return ret;
  1870. }
  1871. return 0;
  1872. }
  1873. /* load physical DAI elements */
  1874. static int soc_tplg_dai_elems_load(struct soc_tplg *tplg,
  1875. struct snd_soc_tplg_hdr *hdr)
  1876. {
  1877. struct snd_soc_tplg_dai *dai;
  1878. int count = hdr->count;
  1879. int i;
  1880. if (tplg->pass != SOC_TPLG_PASS_BE_DAI)
  1881. return 0;
  1882. /* config the existing BE DAIs */
  1883. for (i = 0; i < count; i++) {
  1884. dai = (struct snd_soc_tplg_dai *)tplg->pos;
  1885. if (dai->size != sizeof(*dai)) {
  1886. dev_err(tplg->dev, "ASoC: invalid physical DAI size\n");
  1887. return -EINVAL;
  1888. }
  1889. soc_tplg_dai_config(tplg, dai);
  1890. tplg->pos += (sizeof(*dai) + dai->priv.size);
  1891. }
  1892. dev_dbg(tplg->dev, "ASoC: Configure %d BE DAIs\n", count);
  1893. return 0;
  1894. }
  1895. /**
  1896. * manifest_new_ver - Create a new version of manifest from the old version
  1897. * of source.
  1898. * @tplg: topology context
  1899. * @src: old version of manifest as a source
  1900. * @manifest: latest version of manifest created from the source
  1901. *
  1902. * Support from vesion 4. Users need free the returned manifest manually.
  1903. */
  1904. static int manifest_new_ver(struct soc_tplg *tplg,
  1905. struct snd_soc_tplg_manifest *src,
  1906. struct snd_soc_tplg_manifest **manifest)
  1907. {
  1908. struct snd_soc_tplg_manifest *dest;
  1909. struct snd_soc_tplg_manifest_v4 *src_v4;
  1910. *manifest = NULL;
  1911. if (src->size != sizeof(*src_v4)) {
  1912. dev_warn(tplg->dev, "ASoC: invalid manifest size %d\n",
  1913. src->size);
  1914. if (src->size)
  1915. return -EINVAL;
  1916. src->size = sizeof(*src_v4);
  1917. }
  1918. dev_warn(tplg->dev, "ASoC: old version of manifest\n");
  1919. src_v4 = (struct snd_soc_tplg_manifest_v4 *)src;
  1920. dest = kzalloc(sizeof(*dest) + src_v4->priv.size, GFP_KERNEL);
  1921. if (!dest)
  1922. return -ENOMEM;
  1923. dest->size = sizeof(*dest); /* size of latest abi version */
  1924. dest->control_elems = src_v4->control_elems;
  1925. dest->widget_elems = src_v4->widget_elems;
  1926. dest->graph_elems = src_v4->graph_elems;
  1927. dest->pcm_elems = src_v4->pcm_elems;
  1928. dest->dai_link_elems = src_v4->dai_link_elems;
  1929. dest->priv.size = src_v4->priv.size;
  1930. if (dest->priv.size)
  1931. memcpy(dest->priv.data, src_v4->priv.data,
  1932. src_v4->priv.size);
  1933. *manifest = dest;
  1934. return 0;
  1935. }
  1936. static int soc_tplg_manifest_load(struct soc_tplg *tplg,
  1937. struct snd_soc_tplg_hdr *hdr)
  1938. {
  1939. struct snd_soc_tplg_manifest *manifest, *_manifest;
  1940. bool abi_match;
  1941. int err;
  1942. if (tplg->pass != SOC_TPLG_PASS_MANIFEST)
  1943. return 0;
  1944. manifest = (struct snd_soc_tplg_manifest *)tplg->pos;
  1945. /* check ABI version by size, create a new manifest if abi not match */
  1946. if (manifest->size == sizeof(*manifest)) {
  1947. abi_match = true;
  1948. _manifest = manifest;
  1949. } else {
  1950. abi_match = false;
  1951. err = manifest_new_ver(tplg, manifest, &_manifest);
  1952. if (err < 0)
  1953. return err;
  1954. }
  1955. /* pass control to component driver for optional further init */
  1956. if (tplg->comp && tplg->ops && tplg->ops->manifest)
  1957. return tplg->ops->manifest(tplg->comp, tplg->index, _manifest);
  1958. if (!abi_match) /* free the duplicated one */
  1959. kfree(_manifest);
  1960. return 0;
  1961. }
  1962. /* validate header magic, size and type */
  1963. static int soc_valid_header(struct soc_tplg *tplg,
  1964. struct snd_soc_tplg_hdr *hdr)
  1965. {
  1966. if (soc_tplg_get_hdr_offset(tplg) >= tplg->fw->size)
  1967. return 0;
  1968. if (hdr->size != sizeof(*hdr)) {
  1969. dev_err(tplg->dev,
  1970. "ASoC: invalid header size for type %d at offset 0x%lx size 0x%zx.\n",
  1971. hdr->type, soc_tplg_get_hdr_offset(tplg),
  1972. tplg->fw->size);
  1973. return -EINVAL;
  1974. }
  1975. /* big endian firmware objects not supported atm */
  1976. if (hdr->magic == cpu_to_be32(SND_SOC_TPLG_MAGIC)) {
  1977. dev_err(tplg->dev,
  1978. "ASoC: pass %d big endian not supported header got %x at offset 0x%lx size 0x%zx.\n",
  1979. tplg->pass, hdr->magic,
  1980. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1981. return -EINVAL;
  1982. }
  1983. if (hdr->magic != SND_SOC_TPLG_MAGIC) {
  1984. dev_err(tplg->dev,
  1985. "ASoC: pass %d does not have a valid header got %x at offset 0x%lx size 0x%zx.\n",
  1986. tplg->pass, hdr->magic,
  1987. soc_tplg_get_hdr_offset(tplg), tplg->fw->size);
  1988. return -EINVAL;
  1989. }
  1990. /* Support ABI from version 4 */
  1991. if (hdr->abi > SND_SOC_TPLG_ABI_VERSION
  1992. || hdr->abi < SND_SOC_TPLG_ABI_VERSION_MIN) {
  1993. dev_err(tplg->dev,
  1994. "ASoC: pass %d invalid ABI version got 0x%x need 0x%x at offset 0x%lx size 0x%zx.\n",
  1995. tplg->pass, hdr->abi,
  1996. SND_SOC_TPLG_ABI_VERSION, soc_tplg_get_hdr_offset(tplg),
  1997. tplg->fw->size);
  1998. return -EINVAL;
  1999. }
  2000. if (hdr->payload_size == 0) {
  2001. dev_err(tplg->dev, "ASoC: header has 0 size at offset 0x%lx.\n",
  2002. soc_tplg_get_hdr_offset(tplg));
  2003. return -EINVAL;
  2004. }
  2005. if (tplg->pass == hdr->type)
  2006. dev_dbg(tplg->dev,
  2007. "ASoC: Got 0x%x bytes of type %d version %d vendor %d at pass %d\n",
  2008. hdr->payload_size, hdr->type, hdr->version,
  2009. hdr->vendor_type, tplg->pass);
  2010. return 1;
  2011. }
  2012. /* check header type and call appropriate handler */
  2013. static int soc_tplg_load_header(struct soc_tplg *tplg,
  2014. struct snd_soc_tplg_hdr *hdr)
  2015. {
  2016. tplg->pos = tplg->hdr_pos + sizeof(struct snd_soc_tplg_hdr);
  2017. /* check for matching ID */
  2018. if (hdr->index != tplg->req_index &&
  2019. tplg->req_index != SND_SOC_TPLG_INDEX_ALL)
  2020. return 0;
  2021. tplg->index = hdr->index;
  2022. switch (hdr->type) {
  2023. case SND_SOC_TPLG_TYPE_MIXER:
  2024. case SND_SOC_TPLG_TYPE_ENUM:
  2025. case SND_SOC_TPLG_TYPE_BYTES:
  2026. return soc_tplg_kcontrol_elems_load(tplg, hdr);
  2027. case SND_SOC_TPLG_TYPE_DAPM_GRAPH:
  2028. return soc_tplg_dapm_graph_elems_load(tplg, hdr);
  2029. case SND_SOC_TPLG_TYPE_DAPM_WIDGET:
  2030. return soc_tplg_dapm_widget_elems_load(tplg, hdr);
  2031. case SND_SOC_TPLG_TYPE_PCM:
  2032. return soc_tplg_pcm_elems_load(tplg, hdr);
  2033. case SND_SOC_TPLG_TYPE_DAI:
  2034. return soc_tplg_dai_elems_load(tplg, hdr);
  2035. case SND_SOC_TPLG_TYPE_DAI_LINK:
  2036. case SND_SOC_TPLG_TYPE_BACKEND_LINK:
  2037. /* physical link configurations */
  2038. return soc_tplg_link_elems_load(tplg, hdr);
  2039. case SND_SOC_TPLG_TYPE_MANIFEST:
  2040. return soc_tplg_manifest_load(tplg, hdr);
  2041. default:
  2042. /* bespoke vendor data object */
  2043. return soc_tplg_vendor_load(tplg, hdr);
  2044. }
  2045. return 0;
  2046. }
  2047. /* process the topology file headers */
  2048. static int soc_tplg_process_headers(struct soc_tplg *tplg)
  2049. {
  2050. struct snd_soc_tplg_hdr *hdr;
  2051. int ret;
  2052. tplg->pass = SOC_TPLG_PASS_START;
  2053. /* process the header types from start to end */
  2054. while (tplg->pass <= SOC_TPLG_PASS_END) {
  2055. tplg->hdr_pos = tplg->fw->data;
  2056. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  2057. while (!soc_tplg_is_eof(tplg)) {
  2058. /* make sure header is valid before loading */
  2059. ret = soc_valid_header(tplg, hdr);
  2060. if (ret < 0)
  2061. return ret;
  2062. else if (ret == 0)
  2063. break;
  2064. /* load the header object */
  2065. ret = soc_tplg_load_header(tplg, hdr);
  2066. if (ret < 0)
  2067. return ret;
  2068. /* goto next header */
  2069. tplg->hdr_pos += hdr->payload_size +
  2070. sizeof(struct snd_soc_tplg_hdr);
  2071. hdr = (struct snd_soc_tplg_hdr *)tplg->hdr_pos;
  2072. }
  2073. /* next data type pass */
  2074. tplg->pass++;
  2075. }
  2076. /* signal DAPM we are complete */
  2077. ret = soc_tplg_dapm_complete(tplg);
  2078. if (ret < 0)
  2079. dev_err(tplg->dev,
  2080. "ASoC: failed to initialise DAPM from Firmware\n");
  2081. return ret;
  2082. }
  2083. static int soc_tplg_load(struct soc_tplg *tplg)
  2084. {
  2085. int ret;
  2086. ret = soc_tplg_process_headers(tplg);
  2087. if (ret == 0)
  2088. soc_tplg_complete(tplg);
  2089. return ret;
  2090. }
  2091. /* load audio component topology from "firmware" file */
  2092. int snd_soc_tplg_component_load(struct snd_soc_component *comp,
  2093. struct snd_soc_tplg_ops *ops, const struct firmware *fw, u32 id)
  2094. {
  2095. struct soc_tplg tplg;
  2096. /* setup parsing context */
  2097. memset(&tplg, 0, sizeof(tplg));
  2098. tplg.fw = fw;
  2099. tplg.dev = comp->dev;
  2100. tplg.comp = comp;
  2101. tplg.ops = ops;
  2102. tplg.req_index = id;
  2103. tplg.io_ops = ops->io_ops;
  2104. tplg.io_ops_count = ops->io_ops_count;
  2105. tplg.bytes_ext_ops = ops->bytes_ext_ops;
  2106. tplg.bytes_ext_ops_count = ops->bytes_ext_ops_count;
  2107. return soc_tplg_load(&tplg);
  2108. }
  2109. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_load);
  2110. /* remove this dynamic widget */
  2111. void snd_soc_tplg_widget_remove(struct snd_soc_dapm_widget *w)
  2112. {
  2113. /* make sure we are a widget */
  2114. if (w->dobj.type != SND_SOC_DOBJ_WIDGET)
  2115. return;
  2116. remove_widget(w->dapm->component, &w->dobj, SOC_TPLG_PASS_WIDGET);
  2117. }
  2118. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove);
  2119. /* remove all dynamic widgets from this DAPM context */
  2120. void snd_soc_tplg_widget_remove_all(struct snd_soc_dapm_context *dapm,
  2121. u32 index)
  2122. {
  2123. struct snd_soc_dapm_widget *w, *next_w;
  2124. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  2125. /* make sure we are a widget with correct context */
  2126. if (w->dobj.type != SND_SOC_DOBJ_WIDGET || w->dapm != dapm)
  2127. continue;
  2128. /* match ID */
  2129. if (w->dobj.index != index &&
  2130. w->dobj.index != SND_SOC_TPLG_INDEX_ALL)
  2131. continue;
  2132. /* check and free and dynamic widget kcontrols */
  2133. snd_soc_tplg_widget_remove(w);
  2134. snd_soc_dapm_free_widget(w);
  2135. }
  2136. snd_soc_dapm_reset_cache(dapm);
  2137. }
  2138. EXPORT_SYMBOL_GPL(snd_soc_tplg_widget_remove_all);
  2139. /* remove dynamic controls from the component driver */
  2140. int snd_soc_tplg_component_remove(struct snd_soc_component *comp, u32 index)
  2141. {
  2142. struct snd_soc_dobj *dobj, *next_dobj;
  2143. int pass = SOC_TPLG_PASS_END;
  2144. /* process the header types from end to start */
  2145. while (pass >= SOC_TPLG_PASS_START) {
  2146. /* remove mixer controls */
  2147. list_for_each_entry_safe(dobj, next_dobj, &comp->dobj_list,
  2148. list) {
  2149. /* match index */
  2150. if (dobj->index != index &&
  2151. index != SND_SOC_TPLG_INDEX_ALL)
  2152. continue;
  2153. switch (dobj->type) {
  2154. case SND_SOC_DOBJ_MIXER:
  2155. remove_mixer(comp, dobj, pass);
  2156. break;
  2157. case SND_SOC_DOBJ_ENUM:
  2158. remove_enum(comp, dobj, pass);
  2159. break;
  2160. case SND_SOC_DOBJ_BYTES:
  2161. remove_bytes(comp, dobj, pass);
  2162. break;
  2163. case SND_SOC_DOBJ_WIDGET:
  2164. remove_widget(comp, dobj, pass);
  2165. break;
  2166. case SND_SOC_DOBJ_PCM:
  2167. remove_dai(comp, dobj, pass);
  2168. break;
  2169. case SND_SOC_DOBJ_DAI_LINK:
  2170. remove_link(comp, dobj, pass);
  2171. break;
  2172. default:
  2173. dev_err(comp->dev, "ASoC: invalid component type %d for removal\n",
  2174. dobj->type);
  2175. break;
  2176. }
  2177. }
  2178. pass--;
  2179. }
  2180. /* let caller know if FW can be freed when no objects are left */
  2181. return !list_empty(&comp->dobj_list);
  2182. }
  2183. EXPORT_SYMBOL_GPL(snd_soc_tplg_component_remove);