ipc3-control.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2021 Intel Corporation
  7. //
  8. //
  9. #include "sof-priv.h"
  10. #include "sof-audio.h"
  11. #include "ipc3-priv.h"
  12. /* IPC set()/get() for kcontrols. */
  13. static int sof_ipc3_set_get_kcontrol_data(struct snd_sof_control *scontrol,
  14. bool set, bool lock)
  15. {
  16. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scontrol->scomp);
  17. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  18. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  19. enum sof_ipc_ctrl_type ctrl_type;
  20. struct snd_sof_widget *swidget;
  21. bool widget_found = false;
  22. u32 ipc_cmd, msg_bytes;
  23. int ret = 0;
  24. list_for_each_entry(swidget, &sdev->widget_list, list) {
  25. if (swidget->comp_id == scontrol->comp_id) {
  26. widget_found = true;
  27. break;
  28. }
  29. }
  30. if (!widget_found) {
  31. dev_err(sdev->dev, "%s: can't find widget with id %d\n", __func__,
  32. scontrol->comp_id);
  33. return -EINVAL;
  34. }
  35. if (lock)
  36. mutex_lock(&swidget->setup_mutex);
  37. else
  38. lockdep_assert_held(&swidget->setup_mutex);
  39. /*
  40. * Volatile controls should always be part of static pipelines and the
  41. * widget use_count would always be > 0 in this case. For the others,
  42. * just return the cached value if the widget is not set up.
  43. */
  44. if (!swidget->use_count)
  45. goto unlock;
  46. /*
  47. * Select the IPC cmd and the ctrl_type based on the ctrl_cmd and the
  48. * direction
  49. * Note: SOF_CTRL_TYPE_VALUE_COMP_* is not used and supported currently
  50. * for ctrl_type
  51. */
  52. if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
  53. ipc_cmd = set ? SOF_IPC_COMP_SET_DATA : SOF_IPC_COMP_GET_DATA;
  54. ctrl_type = set ? SOF_CTRL_TYPE_DATA_SET : SOF_CTRL_TYPE_DATA_GET;
  55. } else {
  56. ipc_cmd = set ? SOF_IPC_COMP_SET_VALUE : SOF_IPC_COMP_GET_VALUE;
  57. ctrl_type = set ? SOF_CTRL_TYPE_VALUE_CHAN_SET : SOF_CTRL_TYPE_VALUE_CHAN_GET;
  58. }
  59. cdata->rhdr.hdr.cmd = SOF_IPC_GLB_COMP_MSG | ipc_cmd;
  60. cdata->type = ctrl_type;
  61. cdata->comp_id = scontrol->comp_id;
  62. cdata->msg_index = 0;
  63. /* calculate header and data size */
  64. switch (cdata->type) {
  65. case SOF_CTRL_TYPE_VALUE_CHAN_GET:
  66. case SOF_CTRL_TYPE_VALUE_CHAN_SET:
  67. cdata->num_elems = scontrol->num_channels;
  68. msg_bytes = scontrol->num_channels *
  69. sizeof(struct sof_ipc_ctrl_value_chan);
  70. msg_bytes += sizeof(struct sof_ipc_ctrl_data);
  71. break;
  72. case SOF_CTRL_TYPE_DATA_GET:
  73. case SOF_CTRL_TYPE_DATA_SET:
  74. cdata->num_elems = cdata->data->size;
  75. msg_bytes = cdata->data->size;
  76. msg_bytes += sizeof(struct sof_ipc_ctrl_data) +
  77. sizeof(struct sof_abi_hdr);
  78. break;
  79. default:
  80. ret = -EINVAL;
  81. goto unlock;
  82. }
  83. cdata->rhdr.hdr.size = msg_bytes;
  84. cdata->elems_remaining = 0;
  85. ret = iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
  86. if (!set)
  87. goto unlock;
  88. /* It is a set-data operation, and we have a backup that we can restore */
  89. if (ret < 0) {
  90. if (!scontrol->old_ipc_control_data)
  91. goto unlock;
  92. /*
  93. * Current ipc_control_data is not valid, we use the last known good
  94. * configuration
  95. */
  96. memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
  97. scontrol->max_size);
  98. kfree(scontrol->old_ipc_control_data);
  99. scontrol->old_ipc_control_data = NULL;
  100. /* Send the last known good configuration to firmware */
  101. ret = iops->set_get_data(sdev, cdata, cdata->rhdr.hdr.size, set);
  102. if (ret < 0)
  103. goto unlock;
  104. }
  105. unlock:
  106. if (lock)
  107. mutex_unlock(&swidget->setup_mutex);
  108. return ret;
  109. }
  110. static void sof_ipc3_refresh_control(struct snd_sof_control *scontrol)
  111. {
  112. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  113. struct snd_soc_component *scomp = scontrol->scomp;
  114. int ret;
  115. if (!scontrol->comp_data_dirty)
  116. return;
  117. if (!pm_runtime_active(scomp->dev))
  118. return;
  119. /* set the ABI header values */
  120. cdata->data->magic = SOF_ABI_MAGIC;
  121. cdata->data->abi = SOF_ABI_VERSION;
  122. /* refresh the component data from DSP */
  123. scontrol->comp_data_dirty = false;
  124. ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, true);
  125. if (ret < 0) {
  126. dev_err(scomp->dev, "Failed to get control data: %d\n", ret);
  127. /* Set the flag to re-try next time to get the data */
  128. scontrol->comp_data_dirty = true;
  129. }
  130. }
  131. static int sof_ipc3_volume_get(struct snd_sof_control *scontrol,
  132. struct snd_ctl_elem_value *ucontrol)
  133. {
  134. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  135. unsigned int channels = scontrol->num_channels;
  136. unsigned int i;
  137. sof_ipc3_refresh_control(scontrol);
  138. /* read back each channel */
  139. for (i = 0; i < channels; i++)
  140. ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
  141. scontrol->volume_table,
  142. scontrol->max + 1);
  143. return 0;
  144. }
  145. static bool sof_ipc3_volume_put(struct snd_sof_control *scontrol,
  146. struct snd_ctl_elem_value *ucontrol)
  147. {
  148. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  149. struct snd_soc_component *scomp = scontrol->scomp;
  150. unsigned int channels = scontrol->num_channels;
  151. unsigned int i;
  152. bool change = false;
  153. /* update each channel */
  154. for (i = 0; i < channels; i++) {
  155. u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
  156. scontrol->volume_table, scontrol->max + 1);
  157. change = change || (value != cdata->chanv[i].value);
  158. cdata->chanv[i].channel = i;
  159. cdata->chanv[i].value = value;
  160. }
  161. /* notify DSP of mixer updates */
  162. if (pm_runtime_active(scomp->dev)) {
  163. int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
  164. if (ret < 0) {
  165. dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
  166. scontrol->name);
  167. return false;
  168. }
  169. }
  170. return change;
  171. }
  172. static int sof_ipc3_switch_get(struct snd_sof_control *scontrol,
  173. struct snd_ctl_elem_value *ucontrol)
  174. {
  175. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  176. unsigned int channels = scontrol->num_channels;
  177. unsigned int i;
  178. sof_ipc3_refresh_control(scontrol);
  179. /* read back each channel */
  180. for (i = 0; i < channels; i++)
  181. ucontrol->value.integer.value[i] = cdata->chanv[i].value;
  182. return 0;
  183. }
  184. static bool sof_ipc3_switch_put(struct snd_sof_control *scontrol,
  185. struct snd_ctl_elem_value *ucontrol)
  186. {
  187. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  188. struct snd_soc_component *scomp = scontrol->scomp;
  189. unsigned int channels = scontrol->num_channels;
  190. unsigned int i;
  191. bool change = false;
  192. u32 value;
  193. /* update each channel */
  194. for (i = 0; i < channels; i++) {
  195. value = ucontrol->value.integer.value[i];
  196. change = change || (value != cdata->chanv[i].value);
  197. cdata->chanv[i].channel = i;
  198. cdata->chanv[i].value = value;
  199. }
  200. /* notify DSP of mixer updates */
  201. if (pm_runtime_active(scomp->dev)) {
  202. int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
  203. if (ret < 0) {
  204. dev_err(scomp->dev, "Failed to set mixer updates for %s\n",
  205. scontrol->name);
  206. return false;
  207. }
  208. }
  209. return change;
  210. }
  211. static int sof_ipc3_enum_get(struct snd_sof_control *scontrol,
  212. struct snd_ctl_elem_value *ucontrol)
  213. {
  214. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  215. unsigned int channels = scontrol->num_channels;
  216. unsigned int i;
  217. sof_ipc3_refresh_control(scontrol);
  218. /* read back each channel */
  219. for (i = 0; i < channels; i++)
  220. ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
  221. return 0;
  222. }
  223. static bool sof_ipc3_enum_put(struct snd_sof_control *scontrol,
  224. struct snd_ctl_elem_value *ucontrol)
  225. {
  226. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  227. struct snd_soc_component *scomp = scontrol->scomp;
  228. unsigned int channels = scontrol->num_channels;
  229. unsigned int i;
  230. bool change = false;
  231. u32 value;
  232. /* update each channel */
  233. for (i = 0; i < channels; i++) {
  234. value = ucontrol->value.enumerated.item[i];
  235. change = change || (value != cdata->chanv[i].value);
  236. cdata->chanv[i].channel = i;
  237. cdata->chanv[i].value = value;
  238. }
  239. /* notify DSP of enum updates */
  240. if (pm_runtime_active(scomp->dev)) {
  241. int ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
  242. if (ret < 0) {
  243. dev_err(scomp->dev, "Failed to set enum updates for %s\n",
  244. scontrol->name);
  245. return false;
  246. }
  247. }
  248. return change;
  249. }
  250. static int sof_ipc3_bytes_get(struct snd_sof_control *scontrol,
  251. struct snd_ctl_elem_value *ucontrol)
  252. {
  253. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  254. struct snd_soc_component *scomp = scontrol->scomp;
  255. struct sof_abi_hdr *data = cdata->data;
  256. size_t size;
  257. sof_ipc3_refresh_control(scontrol);
  258. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  259. dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
  260. scontrol->max_size);
  261. return -EINVAL;
  262. }
  263. /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
  264. if (data->size > scontrol->max_size - sizeof(*data)) {
  265. dev_err_ratelimited(scomp->dev,
  266. "%u bytes of control data is invalid, max is %zu\n",
  267. data->size, scontrol->max_size - sizeof(*data));
  268. return -EINVAL;
  269. }
  270. size = data->size + sizeof(*data);
  271. /* copy back to kcontrol */
  272. memcpy(ucontrol->value.bytes.data, data, size);
  273. return 0;
  274. }
  275. static int sof_ipc3_bytes_put(struct snd_sof_control *scontrol,
  276. struct snd_ctl_elem_value *ucontrol)
  277. {
  278. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  279. struct snd_soc_component *scomp = scontrol->scomp;
  280. struct sof_abi_hdr *data = cdata->data;
  281. size_t size;
  282. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  283. dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
  284. scontrol->max_size);
  285. return -EINVAL;
  286. }
  287. /* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
  288. if (data->size > scontrol->max_size - sizeof(*data)) {
  289. dev_err_ratelimited(scomp->dev, "data size too big %u bytes max is %zu\n",
  290. data->size, scontrol->max_size - sizeof(*data));
  291. return -EINVAL;
  292. }
  293. size = data->size + sizeof(*data);
  294. /* copy from kcontrol */
  295. memcpy(data, ucontrol->value.bytes.data, size);
  296. /* notify DSP of byte control updates */
  297. if (pm_runtime_active(scomp->dev))
  298. return sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
  299. return 0;
  300. }
  301. static int sof_ipc3_bytes_ext_put(struct snd_sof_control *scontrol,
  302. const unsigned int __user *binary_data,
  303. unsigned int size)
  304. {
  305. const struct snd_ctl_tlv __user *tlvd = (const struct snd_ctl_tlv __user *)binary_data;
  306. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  307. struct snd_soc_component *scomp = scontrol->scomp;
  308. struct snd_ctl_tlv header;
  309. int ret = -EINVAL;
  310. /*
  311. * The beginning of bytes data contains a header from where
  312. * the length (as bytes) is needed to know the correct copy
  313. * length of data from tlvd->tlv.
  314. */
  315. if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
  316. return -EFAULT;
  317. /* make sure TLV info is consistent */
  318. if (header.length + sizeof(struct snd_ctl_tlv) > size) {
  319. dev_err_ratelimited(scomp->dev, "Inconsistent TLV, data %d + header %zu > %d\n",
  320. header.length, sizeof(struct snd_ctl_tlv), size);
  321. return -EINVAL;
  322. }
  323. /* be->max is coming from topology */
  324. if (header.length > scontrol->max_size) {
  325. dev_err_ratelimited(scomp->dev, "Bytes data size %d exceeds max %zu\n",
  326. header.length, scontrol->max_size);
  327. return -EINVAL;
  328. }
  329. /* Check that header id matches the command */
  330. if (header.numid != cdata->cmd) {
  331. dev_err_ratelimited(scomp->dev, "Incorrect command for bytes put %d\n",
  332. header.numid);
  333. return -EINVAL;
  334. }
  335. if (!scontrol->old_ipc_control_data) {
  336. /* Create a backup of the current, valid bytes control */
  337. scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
  338. scontrol->max_size, GFP_KERNEL);
  339. if (!scontrol->old_ipc_control_data)
  340. return -ENOMEM;
  341. }
  342. if (copy_from_user(cdata->data, tlvd->tlv, header.length)) {
  343. ret = -EFAULT;
  344. goto err_restore;
  345. }
  346. if (cdata->data->magic != SOF_ABI_MAGIC) {
  347. dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n", cdata->data->magic);
  348. goto err_restore;
  349. }
  350. if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
  351. dev_err_ratelimited(scomp->dev, "Incompatible ABI version 0x%08x\n",
  352. cdata->data->abi);
  353. goto err_restore;
  354. }
  355. /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
  356. if (cdata->data->size > scontrol->max_size - sizeof(struct sof_abi_hdr)) {
  357. dev_err_ratelimited(scomp->dev, "Mismatch in ABI data size (truncated?)\n");
  358. goto err_restore;
  359. }
  360. /* notify DSP of byte control updates */
  361. if (pm_runtime_active(scomp->dev)) {
  362. /* Actually send the data to the DSP; this is an opportunity to validate the data */
  363. return sof_ipc3_set_get_kcontrol_data(scontrol, true, true);
  364. }
  365. return 0;
  366. err_restore:
  367. /* If we have an issue, we restore the old, valid bytes control data */
  368. if (scontrol->old_ipc_control_data) {
  369. memcpy(cdata->data, scontrol->old_ipc_control_data, scontrol->max_size);
  370. kfree(scontrol->old_ipc_control_data);
  371. scontrol->old_ipc_control_data = NULL;
  372. }
  373. return ret;
  374. }
  375. static int _sof_ipc3_bytes_ext_get(struct snd_sof_control *scontrol,
  376. const unsigned int __user *binary_data,
  377. unsigned int size, bool from_dsp)
  378. {
  379. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  380. struct sof_ipc_ctrl_data *cdata = scontrol->ipc_control_data;
  381. struct snd_soc_component *scomp = scontrol->scomp;
  382. struct snd_ctl_tlv header;
  383. size_t data_size;
  384. /*
  385. * Decrement the limit by ext bytes header size to
  386. * ensure the user space buffer is not exceeded.
  387. */
  388. if (size < sizeof(struct snd_ctl_tlv))
  389. return -ENOSPC;
  390. size -= sizeof(struct snd_ctl_tlv);
  391. /* set the ABI header values */
  392. cdata->data->magic = SOF_ABI_MAGIC;
  393. cdata->data->abi = SOF_ABI_VERSION;
  394. /* get all the component data from DSP */
  395. if (from_dsp) {
  396. int ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, true);
  397. if (ret < 0)
  398. return ret;
  399. }
  400. /* check data size doesn't exceed max coming from topology */
  401. if (cdata->data->size > scontrol->max_size - sizeof(struct sof_abi_hdr)) {
  402. dev_err_ratelimited(scomp->dev, "User data size %d exceeds max size %zu\n",
  403. cdata->data->size,
  404. scontrol->max_size - sizeof(struct sof_abi_hdr));
  405. return -EINVAL;
  406. }
  407. data_size = cdata->data->size + sizeof(struct sof_abi_hdr);
  408. /* make sure we don't exceed size provided by user space for data */
  409. if (data_size > size)
  410. return -ENOSPC;
  411. header.numid = cdata->cmd;
  412. header.length = data_size;
  413. if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
  414. return -EFAULT;
  415. if (copy_to_user(tlvd->tlv, cdata->data, data_size))
  416. return -EFAULT;
  417. return 0;
  418. }
  419. static int sof_ipc3_bytes_ext_get(struct snd_sof_control *scontrol,
  420. const unsigned int __user *binary_data, unsigned int size)
  421. {
  422. return _sof_ipc3_bytes_ext_get(scontrol, binary_data, size, false);
  423. }
  424. static int sof_ipc3_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
  425. const unsigned int __user *binary_data,
  426. unsigned int size)
  427. {
  428. return _sof_ipc3_bytes_ext_get(scontrol, binary_data, size, true);
  429. }
  430. static void snd_sof_update_control(struct snd_sof_control *scontrol,
  431. struct sof_ipc_ctrl_data *cdata)
  432. {
  433. struct snd_soc_component *scomp = scontrol->scomp;
  434. struct sof_ipc_ctrl_data *local_cdata;
  435. int i;
  436. local_cdata = scontrol->ipc_control_data;
  437. if (cdata->cmd == SOF_CTRL_CMD_BINARY) {
  438. if (cdata->num_elems != local_cdata->data->size) {
  439. dev_err(scomp->dev, "cdata binary size mismatch %u - %u\n",
  440. cdata->num_elems, local_cdata->data->size);
  441. return;
  442. }
  443. /* copy the new binary data */
  444. memcpy(local_cdata->data, cdata->data, cdata->num_elems);
  445. } else if (cdata->num_elems != scontrol->num_channels) {
  446. dev_err(scomp->dev, "cdata channel count mismatch %u - %d\n",
  447. cdata->num_elems, scontrol->num_channels);
  448. } else {
  449. /* copy the new values */
  450. for (i = 0; i < cdata->num_elems; i++)
  451. local_cdata->chanv[i].value = cdata->chanv[i].value;
  452. }
  453. }
  454. static void sof_ipc3_control_update(struct snd_sof_dev *sdev, void *ipc_control_message)
  455. {
  456. struct sof_ipc_ctrl_data *cdata = ipc_control_message;
  457. struct snd_soc_dapm_widget *widget;
  458. struct snd_sof_control *scontrol;
  459. struct snd_sof_widget *swidget;
  460. struct snd_kcontrol *kc = NULL;
  461. struct soc_mixer_control *sm;
  462. struct soc_bytes_ext *be;
  463. size_t expected_size;
  464. struct soc_enum *se;
  465. bool found = false;
  466. int i, type;
  467. if (cdata->type == SOF_CTRL_TYPE_VALUE_COMP_GET ||
  468. cdata->type == SOF_CTRL_TYPE_VALUE_COMP_SET) {
  469. dev_err(sdev->dev, "Component data is not supported in control notification\n");
  470. return;
  471. }
  472. /* Find the swidget first */
  473. list_for_each_entry(swidget, &sdev->widget_list, list) {
  474. if (swidget->comp_id == cdata->comp_id) {
  475. found = true;
  476. break;
  477. }
  478. }
  479. if (!found)
  480. return;
  481. /* Translate SOF cmd to TPLG type */
  482. switch (cdata->cmd) {
  483. case SOF_CTRL_CMD_VOLUME:
  484. case SOF_CTRL_CMD_SWITCH:
  485. type = SND_SOC_TPLG_TYPE_MIXER;
  486. break;
  487. case SOF_CTRL_CMD_BINARY:
  488. type = SND_SOC_TPLG_TYPE_BYTES;
  489. break;
  490. case SOF_CTRL_CMD_ENUM:
  491. type = SND_SOC_TPLG_TYPE_ENUM;
  492. break;
  493. default:
  494. dev_err(sdev->dev, "Unknown cmd %u in %s\n", cdata->cmd, __func__);
  495. return;
  496. }
  497. widget = swidget->widget;
  498. for (i = 0; i < widget->num_kcontrols; i++) {
  499. /* skip non matching types or non matching indexes within type */
  500. if (widget->dobj.widget.kcontrol_type[i] == type &&
  501. widget->kcontrol_news[i].index == cdata->index) {
  502. kc = widget->kcontrols[i];
  503. break;
  504. }
  505. }
  506. if (!kc)
  507. return;
  508. switch (cdata->cmd) {
  509. case SOF_CTRL_CMD_VOLUME:
  510. case SOF_CTRL_CMD_SWITCH:
  511. sm = (struct soc_mixer_control *)kc->private_value;
  512. scontrol = sm->dobj.private;
  513. break;
  514. case SOF_CTRL_CMD_BINARY:
  515. be = (struct soc_bytes_ext *)kc->private_value;
  516. scontrol = be->dobj.private;
  517. break;
  518. case SOF_CTRL_CMD_ENUM:
  519. se = (struct soc_enum *)kc->private_value;
  520. scontrol = se->dobj.private;
  521. break;
  522. default:
  523. return;
  524. }
  525. expected_size = sizeof(struct sof_ipc_ctrl_data);
  526. switch (cdata->type) {
  527. case SOF_CTRL_TYPE_VALUE_CHAN_GET:
  528. case SOF_CTRL_TYPE_VALUE_CHAN_SET:
  529. expected_size += cdata->num_elems *
  530. sizeof(struct sof_ipc_ctrl_value_chan);
  531. break;
  532. case SOF_CTRL_TYPE_DATA_GET:
  533. case SOF_CTRL_TYPE_DATA_SET:
  534. expected_size += cdata->num_elems + sizeof(struct sof_abi_hdr);
  535. break;
  536. default:
  537. return;
  538. }
  539. if (cdata->rhdr.hdr.size != expected_size) {
  540. dev_err(sdev->dev, "Component notification size mismatch\n");
  541. return;
  542. }
  543. if (cdata->num_elems)
  544. /*
  545. * The message includes the updated value/data, update the
  546. * control's local cache using the received notification
  547. */
  548. snd_sof_update_control(scontrol, cdata);
  549. else
  550. /* Mark the scontrol that the value/data is changed in SOF */
  551. scontrol->comp_data_dirty = true;
  552. snd_ctl_notify_one(swidget->scomp->card->snd_card, SNDRV_CTL_EVENT_MASK_VALUE, kc, 0);
  553. }
  554. static int sof_ipc3_widget_kcontrol_setup(struct snd_sof_dev *sdev,
  555. struct snd_sof_widget *swidget)
  556. {
  557. struct snd_sof_control *scontrol;
  558. int ret;
  559. /* set up all controls for the widget */
  560. list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
  561. if (scontrol->comp_id == swidget->comp_id) {
  562. /* set kcontrol data in DSP */
  563. ret = sof_ipc3_set_get_kcontrol_data(scontrol, true, false);
  564. if (ret < 0) {
  565. dev_err(sdev->dev,
  566. "kcontrol %d set up failed for widget %s\n",
  567. scontrol->comp_id, swidget->widget->name);
  568. return ret;
  569. }
  570. /*
  571. * Read back the data from the DSP for static widgets.
  572. * This is particularly useful for binary kcontrols
  573. * associated with static pipeline widgets to initialize
  574. * the data size to match that in the DSP.
  575. */
  576. if (swidget->dynamic_pipeline_widget)
  577. continue;
  578. ret = sof_ipc3_set_get_kcontrol_data(scontrol, false, false);
  579. if (ret < 0)
  580. dev_warn(sdev->dev,
  581. "kcontrol %d read failed for widget %s\n",
  582. scontrol->comp_id, swidget->widget->name);
  583. }
  584. return 0;
  585. }
  586. static int
  587. sof_ipc3_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
  588. {
  589. int i;
  590. /* init the volume table */
  591. scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
  592. if (!scontrol->volume_table)
  593. return -ENOMEM;
  594. /* populate the volume table */
  595. for (i = 0; i < size ; i++)
  596. scontrol->volume_table[i] = vol_compute_gain(i, tlv);
  597. return 0;
  598. }
  599. const struct sof_ipc_tplg_control_ops tplg_ipc3_control_ops = {
  600. .volume_put = sof_ipc3_volume_put,
  601. .volume_get = sof_ipc3_volume_get,
  602. .switch_put = sof_ipc3_switch_put,
  603. .switch_get = sof_ipc3_switch_get,
  604. .enum_put = sof_ipc3_enum_put,
  605. .enum_get = sof_ipc3_enum_get,
  606. .bytes_put = sof_ipc3_bytes_put,
  607. .bytes_get = sof_ipc3_bytes_get,
  608. .bytes_ext_put = sof_ipc3_bytes_ext_put,
  609. .bytes_ext_get = sof_ipc3_bytes_ext_get,
  610. .bytes_ext_volatile_get = sof_ipc3_bytes_ext_volatile_get,
  611. .update = sof_ipc3_control_update,
  612. .widget_kcontrol_setup = sof_ipc3_widget_kcontrol_setup,
  613. .set_up_volume_table = sof_ipc3_set_up_volume_table,
  614. };