ipc4-control.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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) 2022 Intel Corporation
  7. //
  8. //
  9. #include "sof-priv.h"
  10. #include "sof-audio.h"
  11. #include "ipc4-priv.h"
  12. #include "ipc4-topology.h"
  13. static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,
  14. bool set, bool lock)
  15. {
  16. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  17. struct snd_soc_component *scomp = scontrol->scomp;
  18. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  19. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  20. struct sof_ipc4_msg *msg = &cdata->msg;
  21. struct snd_sof_widget *swidget;
  22. bool widget_found = false;
  23. int ret = 0;
  24. /* find widget associated with the control */
  25. list_for_each_entry(swidget, &sdev->widget_list, list) {
  26. if (swidget->comp_id == scontrol->comp_id) {
  27. widget_found = true;
  28. break;
  29. }
  30. }
  31. if (!widget_found) {
  32. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  33. return -ENOENT;
  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. msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;
  47. msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id);
  48. ret = iops->set_get_data(sdev, msg, msg->data_size, set);
  49. if (!set)
  50. goto unlock;
  51. /* It is a set-data operation, and we have a valid backup that we can restore */
  52. if (ret < 0) {
  53. if (!scontrol->old_ipc_control_data)
  54. goto unlock;
  55. /*
  56. * Current ipc_control_data is not valid, we use the last known good
  57. * configuration
  58. */
  59. memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
  60. scontrol->max_size);
  61. kfree(scontrol->old_ipc_control_data);
  62. scontrol->old_ipc_control_data = NULL;
  63. /* Send the last known good configuration to firmware */
  64. ret = iops->set_get_data(sdev, msg, msg->data_size, set);
  65. if (ret < 0)
  66. goto unlock;
  67. }
  68. unlock:
  69. if (lock)
  70. mutex_unlock(&swidget->setup_mutex);
  71. return ret;
  72. }
  73. static int
  74. sof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
  75. struct snd_sof_control *scontrol, bool lock)
  76. {
  77. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  78. struct sof_ipc4_gain *gain = swidget->private;
  79. struct sof_ipc4_msg *msg = &cdata->msg;
  80. struct sof_ipc4_gain_params params;
  81. bool all_channels_equal = true;
  82. u32 value;
  83. int ret, i;
  84. /* check if all channel values are equal */
  85. value = cdata->chanv[0].value;
  86. for (i = 1; i < scontrol->num_channels; i++) {
  87. if (cdata->chanv[i].value != value) {
  88. all_channels_equal = false;
  89. break;
  90. }
  91. }
  92. /*
  93. * notify DSP with a single IPC message if all channel values are equal. Otherwise send
  94. * a separate IPC for each channel.
  95. */
  96. for (i = 0; i < scontrol->num_channels; i++) {
  97. if (all_channels_equal) {
  98. params.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;
  99. params.init_val = cdata->chanv[0].value;
  100. } else {
  101. params.channels = cdata->chanv[i].channel;
  102. params.init_val = cdata->chanv[i].value;
  103. }
  104. /* set curve type and duration from topology */
  105. params.curve_duration_l = gain->data.params.curve_duration_l;
  106. params.curve_duration_h = gain->data.params.curve_duration_h;
  107. params.curve_type = gain->data.params.curve_type;
  108. msg->data_ptr = &params;
  109. msg->data_size = sizeof(params);
  110. ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
  111. msg->data_ptr = NULL;
  112. msg->data_size = 0;
  113. if (ret < 0) {
  114. dev_err(sdev->dev, "Failed to set volume update for %s\n",
  115. scontrol->name);
  116. return ret;
  117. }
  118. if (all_channels_equal)
  119. break;
  120. }
  121. return 0;
  122. }
  123. static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,
  124. struct snd_ctl_elem_value *ucontrol)
  125. {
  126. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  127. struct snd_soc_component *scomp = scontrol->scomp;
  128. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  129. unsigned int channels = scontrol->num_channels;
  130. struct snd_sof_widget *swidget;
  131. bool widget_found = false;
  132. bool change = false;
  133. unsigned int i;
  134. int ret;
  135. /* update each channel */
  136. for (i = 0; i < channels; i++) {
  137. u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
  138. scontrol->volume_table, scontrol->max + 1);
  139. change = change || (value != cdata->chanv[i].value);
  140. cdata->chanv[i].channel = i;
  141. cdata->chanv[i].value = value;
  142. }
  143. if (!pm_runtime_active(scomp->dev))
  144. return change;
  145. /* find widget associated with the control */
  146. list_for_each_entry(swidget, &sdev->widget_list, list) {
  147. if (swidget->comp_id == scontrol->comp_id) {
  148. widget_found = true;
  149. break;
  150. }
  151. }
  152. if (!widget_found) {
  153. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  154. return false;
  155. }
  156. ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, true);
  157. if (ret < 0)
  158. return false;
  159. return change;
  160. }
  161. static int sof_ipc4_volume_get(struct snd_sof_control *scontrol,
  162. struct snd_ctl_elem_value *ucontrol)
  163. {
  164. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  165. unsigned int channels = scontrol->num_channels;
  166. unsigned int i;
  167. for (i = 0; i < channels; i++)
  168. ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,
  169. scontrol->volume_table,
  170. scontrol->max + 1);
  171. return 0;
  172. }
  173. static int
  174. sof_ipc4_set_generic_control_data(struct snd_sof_dev *sdev,
  175. struct snd_sof_widget *swidget,
  176. struct snd_sof_control *scontrol, bool lock)
  177. {
  178. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  179. struct sof_ipc4_control_msg_payload *data;
  180. struct sof_ipc4_msg *msg = &cdata->msg;
  181. size_t data_size;
  182. unsigned int i;
  183. int ret;
  184. data_size = struct_size(data, chanv, scontrol->num_channels);
  185. data = kzalloc(data_size, GFP_KERNEL);
  186. if (!data)
  187. return -ENOMEM;
  188. data->id = cdata->index;
  189. data->num_elems = scontrol->num_channels;
  190. for (i = 0; i < scontrol->num_channels; i++) {
  191. data->chanv[i].channel = cdata->chanv[i].channel;
  192. data->chanv[i].value = cdata->chanv[i].value;
  193. }
  194. msg->data_ptr = data;
  195. msg->data_size = data_size;
  196. ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
  197. msg->data_ptr = NULL;
  198. msg->data_size = 0;
  199. if (ret < 0)
  200. dev_err(sdev->dev, "Failed to set control update for %s\n",
  201. scontrol->name);
  202. kfree(data);
  203. return ret;
  204. }
  205. static void sof_ipc4_refresh_generic_control(struct snd_sof_control *scontrol)
  206. {
  207. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  208. struct snd_soc_component *scomp = scontrol->scomp;
  209. struct sof_ipc4_control_msg_payload *data;
  210. struct sof_ipc4_msg *msg = &cdata->msg;
  211. size_t data_size;
  212. unsigned int i;
  213. int ret;
  214. if (!scontrol->comp_data_dirty)
  215. return;
  216. if (!pm_runtime_active(scomp->dev))
  217. return;
  218. data_size = struct_size(data, chanv, scontrol->num_channels);
  219. data = kmalloc(data_size, GFP_KERNEL);
  220. if (!data)
  221. return;
  222. data->id = cdata->index;
  223. data->num_elems = scontrol->num_channels;
  224. msg->data_ptr = data;
  225. msg->data_size = data_size;
  226. scontrol->comp_data_dirty = false;
  227. ret = sof_ipc4_set_get_kcontrol_data(scontrol, false, true);
  228. msg->data_ptr = NULL;
  229. msg->data_size = 0;
  230. if (!ret) {
  231. for (i = 0; i < scontrol->num_channels; i++) {
  232. cdata->chanv[i].channel = data->chanv[i].channel;
  233. cdata->chanv[i].value = data->chanv[i].value;
  234. }
  235. } else {
  236. dev_err(scomp->dev, "Failed to read control data for %s\n",
  237. scontrol->name);
  238. scontrol->comp_data_dirty = true;
  239. }
  240. kfree(data);
  241. }
  242. static bool sof_ipc4_switch_put(struct snd_sof_control *scontrol,
  243. struct snd_ctl_elem_value *ucontrol)
  244. {
  245. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  246. struct snd_soc_component *scomp = scontrol->scomp;
  247. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  248. struct snd_sof_widget *swidget;
  249. bool widget_found = false;
  250. bool change = false;
  251. unsigned int i;
  252. u32 value;
  253. int ret;
  254. /* update each channel */
  255. for (i = 0; i < scontrol->num_channels; i++) {
  256. value = ucontrol->value.integer.value[i];
  257. change = change || (value != cdata->chanv[i].value);
  258. cdata->chanv[i].channel = i;
  259. cdata->chanv[i].value = value;
  260. }
  261. if (!pm_runtime_active(scomp->dev))
  262. return change;
  263. /* find widget associated with the control */
  264. list_for_each_entry(swidget, &sdev->widget_list, list) {
  265. if (swidget->comp_id == scontrol->comp_id) {
  266. widget_found = true;
  267. break;
  268. }
  269. }
  270. if (!widget_found) {
  271. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  272. return false;
  273. }
  274. ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);
  275. if (ret < 0)
  276. return false;
  277. return change;
  278. }
  279. static int sof_ipc4_switch_get(struct snd_sof_control *scontrol,
  280. struct snd_ctl_elem_value *ucontrol)
  281. {
  282. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  283. unsigned int i;
  284. sof_ipc4_refresh_generic_control(scontrol);
  285. /* read back each channel */
  286. for (i = 0; i < scontrol->num_channels; i++)
  287. ucontrol->value.integer.value[i] = cdata->chanv[i].value;
  288. return 0;
  289. }
  290. static bool sof_ipc4_enum_put(struct snd_sof_control *scontrol,
  291. struct snd_ctl_elem_value *ucontrol)
  292. {
  293. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  294. struct snd_soc_component *scomp = scontrol->scomp;
  295. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  296. struct snd_sof_widget *swidget;
  297. bool widget_found = false;
  298. bool change = false;
  299. unsigned int i;
  300. u32 value;
  301. int ret;
  302. /* update each channel */
  303. for (i = 0; i < scontrol->num_channels; i++) {
  304. value = ucontrol->value.enumerated.item[i];
  305. change = change || (value != cdata->chanv[i].value);
  306. cdata->chanv[i].channel = i;
  307. cdata->chanv[i].value = value;
  308. }
  309. if (!pm_runtime_active(scomp->dev))
  310. return change;
  311. /* find widget associated with the control */
  312. list_for_each_entry(swidget, &sdev->widget_list, list) {
  313. if (swidget->comp_id == scontrol->comp_id) {
  314. widget_found = true;
  315. break;
  316. }
  317. }
  318. if (!widget_found) {
  319. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  320. return false;
  321. }
  322. ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);
  323. if (ret < 0)
  324. return false;
  325. return change;
  326. }
  327. static int sof_ipc4_enum_get(struct snd_sof_control *scontrol,
  328. struct snd_ctl_elem_value *ucontrol)
  329. {
  330. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  331. unsigned int i;
  332. sof_ipc4_refresh_generic_control(scontrol);
  333. /* read back each channel */
  334. for (i = 0; i < scontrol->num_channels; i++)
  335. ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
  336. return 0;
  337. }
  338. static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
  339. struct snd_sof_control *scontrol,
  340. bool set, bool lock)
  341. {
  342. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  343. struct sof_abi_hdr *data = cdata->data;
  344. struct sof_ipc4_msg *msg = &cdata->msg;
  345. int ret = 0;
  346. /* Send the new data to the firmware only if it is powered up */
  347. if (set && !pm_runtime_active(sdev->dev))
  348. return 0;
  349. msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
  350. msg->data_ptr = data->data;
  351. msg->data_size = data->size;
  352. ret = sof_ipc4_set_get_kcontrol_data(scontrol, set, lock);
  353. if (ret < 0)
  354. dev_err(sdev->dev, "Failed to %s for %s\n",
  355. set ? "set bytes update" : "get bytes",
  356. scontrol->name);
  357. msg->data_ptr = NULL;
  358. msg->data_size = 0;
  359. return ret;
  360. }
  361. static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol,
  362. struct snd_ctl_elem_value *ucontrol)
  363. {
  364. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  365. struct snd_soc_component *scomp = scontrol->scomp;
  366. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  367. struct sof_abi_hdr *data = cdata->data;
  368. size_t size;
  369. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  370. dev_err_ratelimited(scomp->dev,
  371. "data max %zu exceeds ucontrol data array size\n",
  372. scontrol->max_size);
  373. return -EINVAL;
  374. }
  375. /* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
  376. if (data->size > scontrol->max_size - sizeof(*data)) {
  377. dev_err_ratelimited(scomp->dev,
  378. "data size too big %u bytes max is %zu\n",
  379. data->size, scontrol->max_size - sizeof(*data));
  380. return -EINVAL;
  381. }
  382. size = data->size + sizeof(*data);
  383. /* copy from kcontrol */
  384. memcpy(data, ucontrol->value.bytes.data, size);
  385. sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
  386. return 0;
  387. }
  388. static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol,
  389. struct snd_ctl_elem_value *ucontrol)
  390. {
  391. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  392. struct snd_soc_component *scomp = scontrol->scomp;
  393. struct sof_abi_hdr *data = cdata->data;
  394. size_t size;
  395. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  396. dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
  397. scontrol->max_size);
  398. return -EINVAL;
  399. }
  400. if (data->size > scontrol->max_size - sizeof(*data)) {
  401. dev_err_ratelimited(scomp->dev,
  402. "%u bytes of control data is invalid, max is %zu\n",
  403. data->size, scontrol->max_size - sizeof(*data));
  404. return -EINVAL;
  405. }
  406. size = data->size + sizeof(*data);
  407. /* copy back to kcontrol */
  408. memcpy(ucontrol->value.bytes.data, data, size);
  409. return 0;
  410. }
  411. static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
  412. const unsigned int __user *binary_data,
  413. unsigned int size)
  414. {
  415. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  416. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  417. struct snd_soc_component *scomp = scontrol->scomp;
  418. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  419. struct sof_abi_hdr *data = cdata->data;
  420. struct sof_abi_hdr abi_hdr;
  421. struct snd_ctl_tlv header;
  422. /*
  423. * The beginning of bytes data contains a header from where
  424. * the length (as bytes) is needed to know the correct copy
  425. * length of data from tlvd->tlv.
  426. */
  427. if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
  428. return -EFAULT;
  429. /* make sure TLV info is consistent */
  430. if (header.length + sizeof(struct snd_ctl_tlv) > size) {
  431. dev_err_ratelimited(scomp->dev,
  432. "Inconsistent TLV, data %d + header %zu > %d\n",
  433. header.length, sizeof(struct snd_ctl_tlv), size);
  434. return -EINVAL;
  435. }
  436. /* be->max is coming from topology */
  437. if (header.length > scontrol->max_size) {
  438. dev_err_ratelimited(scomp->dev,
  439. "Bytes data size %d exceeds max %zu\n",
  440. header.length, scontrol->max_size);
  441. return -EINVAL;
  442. }
  443. /* Check header id */
  444. if (header.numid != SOF_CTRL_CMD_BINARY) {
  445. dev_err_ratelimited(scomp->dev,
  446. "Incorrect numid for bytes put %d\n",
  447. header.numid);
  448. return -EINVAL;
  449. }
  450. /* Verify the ABI header first */
  451. if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr)))
  452. return -EFAULT;
  453. if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) {
  454. dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n",
  455. abi_hdr.magic);
  456. return -EINVAL;
  457. }
  458. if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) {
  459. dev_err_ratelimited(scomp->dev,
  460. "%u bytes of control data is invalid, max is %zu\n",
  461. abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
  462. return -EINVAL;
  463. }
  464. if (!scontrol->old_ipc_control_data) {
  465. /* Create a backup of the current, valid bytes control */
  466. scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
  467. scontrol->max_size, GFP_KERNEL);
  468. if (!scontrol->old_ipc_control_data)
  469. return -ENOMEM;
  470. }
  471. /* Copy the whole binary data which includes the ABI header and the payload */
  472. if (copy_from_user(data, tlvd->tlv, header.length)) {
  473. memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
  474. scontrol->max_size);
  475. kfree(scontrol->old_ipc_control_data);
  476. scontrol->old_ipc_control_data = NULL;
  477. return -EFAULT;
  478. }
  479. return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
  480. }
  481. static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
  482. const unsigned int __user *binary_data,
  483. unsigned int size, bool from_dsp)
  484. {
  485. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  486. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  487. struct snd_soc_component *scomp = scontrol->scomp;
  488. struct sof_abi_hdr *data = cdata->data;
  489. struct snd_ctl_tlv header;
  490. size_t data_size;
  491. /*
  492. * Decrement the limit by ext bytes header size to ensure the user space
  493. * buffer is not exceeded.
  494. */
  495. if (size < sizeof(struct snd_ctl_tlv))
  496. return -ENOSPC;
  497. size -= sizeof(struct snd_ctl_tlv);
  498. /* get all the component data from DSP */
  499. if (from_dsp) {
  500. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  501. int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true);
  502. if (ret < 0)
  503. return ret;
  504. /* Set the ABI magic (if the control is not initialized) */
  505. data->magic = SOF_IPC4_ABI_MAGIC;
  506. }
  507. if (data->size > scontrol->max_size - sizeof(*data)) {
  508. dev_err_ratelimited(scomp->dev,
  509. "%u bytes of control data is invalid, max is %zu\n",
  510. data->size, scontrol->max_size - sizeof(*data));
  511. return -EINVAL;
  512. }
  513. data_size = data->size + sizeof(struct sof_abi_hdr);
  514. /* make sure we don't exceed size provided by user space for data */
  515. if (data_size > size)
  516. return -ENOSPC;
  517. /* Set header id and length */
  518. header.numid = SOF_CTRL_CMD_BINARY;
  519. header.length = data_size;
  520. if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
  521. return -EFAULT;
  522. if (copy_to_user(tlvd->tlv, data, data_size))
  523. return -EFAULT;
  524. return 0;
  525. }
  526. static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
  527. const unsigned int __user *binary_data,
  528. unsigned int size)
  529. {
  530. return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false);
  531. }
  532. static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
  533. const unsigned int __user *binary_data,
  534. unsigned int size)
  535. {
  536. return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true);
  537. }
  538. static int
  539. sof_ipc4_volsw_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
  540. struct snd_sof_control *scontrol)
  541. {
  542. if (scontrol->max == 1)
  543. return sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, false);
  544. return sof_ipc4_set_volume_data(sdev, swidget, scontrol, false);
  545. }
  546. #define PARAM_ID_FROM_EXTENSION(_ext) (((_ext) & SOF_IPC4_MOD_EXT_MSG_PARAM_ID_MASK) \
  547. >> SOF_IPC4_MOD_EXT_MSG_PARAM_ID_SHIFT)
  548. static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message)
  549. {
  550. struct sof_ipc4_msg *ipc4_msg = ipc_message;
  551. struct sof_ipc4_notify_module_data *ndata = ipc4_msg->data_ptr;
  552. struct sof_ipc4_control_msg_payload *msg_data;
  553. struct sof_ipc4_control_data *cdata;
  554. struct snd_soc_dapm_widget *widget;
  555. struct snd_sof_control *scontrol;
  556. struct snd_sof_widget *swidget;
  557. struct snd_kcontrol *kc = NULL;
  558. bool scontrol_found = false;
  559. u32 event_param_id;
  560. int i, type;
  561. if (ndata->event_data_size < sizeof(*msg_data)) {
  562. dev_err(sdev->dev,
  563. "%s: Invalid event data size for module %u.%u: %u\n",
  564. __func__, ndata->module_id, ndata->instance_id,
  565. ndata->event_data_size);
  566. return;
  567. }
  568. event_param_id = ndata->event_id & SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_PARAMID_MASK;
  569. switch (event_param_id) {
  570. case SOF_IPC4_SWITCH_CONTROL_PARAM_ID:
  571. type = SND_SOC_TPLG_TYPE_MIXER;
  572. break;
  573. case SOF_IPC4_ENUM_CONTROL_PARAM_ID:
  574. type = SND_SOC_TPLG_TYPE_ENUM;
  575. break;
  576. default:
  577. dev_err(sdev->dev,
  578. "%s: Invalid control type for module %u.%u: %u\n",
  579. __func__, ndata->module_id, ndata->instance_id,
  580. event_param_id);
  581. return;
  582. }
  583. /* Find the swidget based on ndata->module_id and ndata->instance_id */
  584. swidget = sof_ipc4_find_swidget_by_ids(sdev, ndata->module_id,
  585. ndata->instance_id);
  586. if (!swidget) {
  587. dev_err(sdev->dev, "%s: Failed to find widget for module %u.%u\n",
  588. __func__, ndata->module_id, ndata->instance_id);
  589. return;
  590. }
  591. /* Find the scontrol which is the source of the notification */
  592. msg_data = (struct sof_ipc4_control_msg_payload *)ndata->event_data;
  593. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  594. if (scontrol->comp_id == swidget->comp_id) {
  595. u32 local_param_id;
  596. cdata = scontrol->ipc_control_data;
  597. /*
  598. * The scontrol's param_id is stored in the IPC message
  599. * template's extension
  600. */
  601. local_param_id = PARAM_ID_FROM_EXTENSION(cdata->msg.extension);
  602. if (local_param_id == event_param_id &&
  603. msg_data->id == cdata->index) {
  604. scontrol_found = true;
  605. break;
  606. }
  607. }
  608. }
  609. if (!scontrol_found) {
  610. dev_err(sdev->dev,
  611. "%s: Failed to find control on widget %s: %u:%u\n",
  612. __func__, swidget->widget->name, ndata->event_id & 0xffff,
  613. msg_data->id);
  614. return;
  615. }
  616. if (msg_data->num_elems) {
  617. /*
  618. * The message includes the updated value/data, update the
  619. * control's local cache using the received notification
  620. */
  621. for (i = 0; i < msg_data->num_elems; i++) {
  622. u32 channel = msg_data->chanv[i].channel;
  623. if (channel >= scontrol->num_channels) {
  624. dev_warn(sdev->dev,
  625. "Invalid channel index for %s: %u\n",
  626. scontrol->name, i);
  627. /*
  628. * Mark the scontrol as dirty to force a refresh
  629. * on next read
  630. */
  631. scontrol->comp_data_dirty = true;
  632. break;
  633. }
  634. cdata->chanv[channel].value = msg_data->chanv[i].value;
  635. }
  636. } else {
  637. /*
  638. * Mark the scontrol as dirty because the value/data is changed
  639. * in firmware, forcing a refresh on next read access
  640. */
  641. scontrol->comp_data_dirty = true;
  642. }
  643. /*
  644. * Look up the ALSA kcontrol of the scontrol to be able to send a
  645. * notification to user space
  646. */
  647. widget = swidget->widget;
  648. for (i = 0; i < widget->num_kcontrols; i++) {
  649. /* skip non matching types or non matching indexes within type */
  650. if (widget->dobj.widget.kcontrol_type[i] == type &&
  651. widget->kcontrol_news[i].index == cdata->index) {
  652. kc = widget->kcontrols[i];
  653. break;
  654. }
  655. }
  656. if (!kc)
  657. return;
  658. snd_ctl_notify_one(swidget->scomp->card->snd_card,
  659. SNDRV_CTL_EVENT_MASK_VALUE, kc, 0);
  660. }
  661. /* set up all controls for the widget */
  662. static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  663. {
  664. struct snd_sof_control *scontrol;
  665. int ret = 0;
  666. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  667. if (scontrol->comp_id == swidget->comp_id) {
  668. switch (scontrol->info_type) {
  669. case SND_SOC_TPLG_CTL_VOLSW:
  670. case SND_SOC_TPLG_CTL_VOLSW_SX:
  671. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  672. ret = sof_ipc4_volsw_setup(sdev, swidget, scontrol);
  673. break;
  674. case SND_SOC_TPLG_CTL_BYTES:
  675. ret = sof_ipc4_set_get_bytes_data(sdev, scontrol,
  676. true, false);
  677. break;
  678. case SND_SOC_TPLG_CTL_ENUM:
  679. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  680. ret = sof_ipc4_set_generic_control_data(sdev, swidget,
  681. scontrol, false);
  682. break;
  683. default:
  684. break;
  685. }
  686. if (ret < 0) {
  687. dev_err(sdev->dev,
  688. "kcontrol %d set up failed for widget %s\n",
  689. scontrol->comp_id, swidget->widget->name);
  690. return ret;
  691. }
  692. }
  693. }
  694. return 0;
  695. }
  696. static int
  697. sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
  698. {
  699. int i;
  700. /* init the volume table */
  701. scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
  702. if (!scontrol->volume_table)
  703. return -ENOMEM;
  704. /* populate the volume table */
  705. for (i = 0; i < size ; i++) {
  706. u32 val = vol_compute_gain(i, tlv);
  707. u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
  708. scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
  709. SOF_IPC4_VOL_ZERO_DB : q31val;
  710. }
  711. return 0;
  712. }
  713. const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
  714. .volume_put = sof_ipc4_volume_put,
  715. .volume_get = sof_ipc4_volume_get,
  716. .switch_put = sof_ipc4_switch_put,
  717. .switch_get = sof_ipc4_switch_get,
  718. .enum_put = sof_ipc4_enum_put,
  719. .enum_get = sof_ipc4_enum_get,
  720. .bytes_put = sof_ipc4_bytes_put,
  721. .bytes_get = sof_ipc4_bytes_get,
  722. .bytes_ext_put = sof_ipc4_bytes_ext_put,
  723. .bytes_ext_get = sof_ipc4_bytes_ext_get,
  724. .bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get,
  725. .update = sof_ipc4_control_update,
  726. .widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
  727. .set_up_volume_table = sof_ipc4_set_up_volume_table,
  728. };