soc-topology.c 67 KB

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