soc-topology.c 68 KB

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