soc-topology.c 72 KB

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