ipc4-control.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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->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 int
  243. sof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock)
  244. {
  245. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  246. struct snd_soc_component *scomp = scontrol->scomp;
  247. struct sof_ipc4_control_msg_payload *msg_data;
  248. struct sof_abi_hdr *data = cdata->data;
  249. struct sof_ipc4_msg *msg = &cdata->msg;
  250. size_t data_size;
  251. int ret;
  252. data_size = struct_size(msg_data, data, data->size);
  253. msg_data = kzalloc(data_size, GFP_KERNEL);
  254. if (!msg_data)
  255. return -ENOMEM;
  256. msg_data->id = cdata->index;
  257. msg_data->num_elems = data->size;
  258. memcpy(msg_data->data, data->data, data->size);
  259. msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
  260. msg->data_ptr = msg_data;
  261. msg->data_size = data_size;
  262. ret = sof_ipc4_set_get_kcontrol_data(scontrol, true, lock);
  263. msg->data_ptr = NULL;
  264. msg->data_size = 0;
  265. if (ret < 0)
  266. dev_err(scomp->dev, "%s: Failed to set control update for %s\n",
  267. __func__, scontrol->name);
  268. kfree(msg_data);
  269. return ret;
  270. }
  271. static int
  272. sof_ipc4_refresh_bytes_control(struct snd_sof_control *scontrol, bool lock)
  273. {
  274. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  275. struct snd_soc_component *scomp = scontrol->scomp;
  276. struct sof_ipc4_control_msg_payload *msg_data;
  277. struct sof_abi_hdr *data = cdata->data;
  278. struct sof_ipc4_msg *msg = &cdata->msg;
  279. size_t data_size;
  280. int ret = 0;
  281. if (!scontrol->comp_data_dirty)
  282. return 0;
  283. if (!pm_runtime_active(scomp->dev))
  284. return 0;
  285. data_size = scontrol->max_size - sizeof(*data);
  286. if (data_size < sizeof(*msg_data))
  287. data_size = sizeof(*msg_data);
  288. msg_data = kzalloc(data_size, GFP_KERNEL);
  289. if (!msg_data)
  290. return -ENOMEM;
  291. msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
  292. msg_data->id = cdata->index;
  293. msg_data->num_elems = 0; /* ignored for bytes */
  294. msg->data_ptr = msg_data;
  295. msg->data_size = data_size;
  296. scontrol->comp_data_dirty = false;
  297. ret = sof_ipc4_set_get_kcontrol_data(scontrol, false, lock);
  298. if (!ret) {
  299. if (msg->data_size > scontrol->max_size - sizeof(*data)) {
  300. dev_err(scomp->dev,
  301. "%s: no space for data in %s (%zu, %zu)\n",
  302. __func__, scontrol->name, msg->data_size,
  303. scontrol->max_size - sizeof(*data));
  304. ret = -EINVAL;
  305. goto out;
  306. }
  307. data->size = msg->data_size;
  308. scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
  309. memcpy(data->data, msg->data_ptr, data->size);
  310. } else {
  311. dev_err(scomp->dev, "Failed to read control data for %s\n",
  312. scontrol->name);
  313. scontrol->comp_data_dirty = true;
  314. }
  315. out:
  316. msg->data_ptr = NULL;
  317. msg->data_size = 0;
  318. kfree(msg_data);
  319. return ret;
  320. }
  321. static bool sof_ipc4_switch_put(struct snd_sof_control *scontrol,
  322. struct snd_ctl_elem_value *ucontrol)
  323. {
  324. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  325. struct snd_soc_component *scomp = scontrol->scomp;
  326. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  327. struct snd_sof_widget *swidget;
  328. bool widget_found = false;
  329. bool change = false;
  330. unsigned int i;
  331. u32 value;
  332. int ret;
  333. /* update each channel */
  334. for (i = 0; i < scontrol->num_channels; i++) {
  335. value = ucontrol->value.integer.value[i];
  336. change = change || (value != cdata->chanv[i].value);
  337. cdata->chanv[i].channel = i;
  338. cdata->chanv[i].value = value;
  339. }
  340. if (!pm_runtime_active(scomp->dev))
  341. return change;
  342. /* find widget associated with the control */
  343. list_for_each_entry(swidget, &sdev->widget_list, list) {
  344. if (swidget->comp_id == scontrol->comp_id) {
  345. widget_found = true;
  346. break;
  347. }
  348. }
  349. if (!widget_found) {
  350. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  351. return false;
  352. }
  353. ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);
  354. if (ret < 0)
  355. return false;
  356. return change;
  357. }
  358. static int sof_ipc4_switch_get(struct snd_sof_control *scontrol,
  359. struct snd_ctl_elem_value *ucontrol)
  360. {
  361. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  362. unsigned int i;
  363. sof_ipc4_refresh_generic_control(scontrol);
  364. /* read back each channel */
  365. for (i = 0; i < scontrol->num_channels; i++)
  366. ucontrol->value.integer.value[i] = cdata->chanv[i].value;
  367. return 0;
  368. }
  369. static bool sof_ipc4_enum_put(struct snd_sof_control *scontrol,
  370. struct snd_ctl_elem_value *ucontrol)
  371. {
  372. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  373. struct snd_soc_component *scomp = scontrol->scomp;
  374. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  375. struct snd_sof_widget *swidget;
  376. bool widget_found = false;
  377. bool change = false;
  378. unsigned int i;
  379. u32 value;
  380. int ret;
  381. /* update each channel */
  382. for (i = 0; i < scontrol->num_channels; i++) {
  383. value = ucontrol->value.enumerated.item[i];
  384. change = change || (value != cdata->chanv[i].value);
  385. cdata->chanv[i].channel = i;
  386. cdata->chanv[i].value = value;
  387. }
  388. if (!pm_runtime_active(scomp->dev))
  389. return change;
  390. /* find widget associated with the control */
  391. list_for_each_entry(swidget, &sdev->widget_list, list) {
  392. if (swidget->comp_id == scontrol->comp_id) {
  393. widget_found = true;
  394. break;
  395. }
  396. }
  397. if (!widget_found) {
  398. dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);
  399. return false;
  400. }
  401. ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);
  402. if (ret < 0)
  403. return false;
  404. return change;
  405. }
  406. static int sof_ipc4_enum_get(struct snd_sof_control *scontrol,
  407. struct snd_ctl_elem_value *ucontrol)
  408. {
  409. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  410. unsigned int i;
  411. sof_ipc4_refresh_generic_control(scontrol);
  412. /* read back each channel */
  413. for (i = 0; i < scontrol->num_channels; i++)
  414. ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
  415. return 0;
  416. }
  417. static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,
  418. struct snd_sof_control *scontrol,
  419. bool set, bool lock)
  420. {
  421. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  422. struct sof_abi_hdr *data = cdata->data;
  423. struct sof_ipc4_msg *msg = &cdata->msg;
  424. int ret = 0;
  425. /* Send the new data to the firmware only if it is powered up */
  426. if (set) {
  427. if (!pm_runtime_active(sdev->dev))
  428. return 0;
  429. if (!data->size) {
  430. dev_dbg(sdev->dev, "%s: No data to be sent.\n",
  431. scontrol->name);
  432. return 0;
  433. }
  434. }
  435. if (data->type == SOF_IPC4_BYTES_CONTROL_PARAM_ID) {
  436. if (set)
  437. return sof_ipc4_set_bytes_control_data(scontrol, lock);
  438. else
  439. return sof_ipc4_refresh_bytes_control(scontrol, lock);
  440. }
  441. msg->extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type);
  442. msg->data_ptr = data->data;
  443. if (set)
  444. msg->data_size = data->size;
  445. else
  446. msg->data_size = scontrol->max_size - sizeof(*data);
  447. ret = sof_ipc4_set_get_kcontrol_data(scontrol, set, lock);
  448. if (ret < 0) {
  449. dev_err(sdev->dev, "Failed to %s for %s\n",
  450. set ? "set bytes update" : "get bytes",
  451. scontrol->name);
  452. } else if (!set) {
  453. /* Update the sizes according to the received payload data */
  454. data->size = msg->data_size;
  455. scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
  456. }
  457. msg->data_ptr = NULL;
  458. msg->data_size = 0;
  459. return ret;
  460. }
  461. static int sof_ipc4_bytes_put(struct snd_sof_control *scontrol,
  462. struct snd_ctl_elem_value *ucontrol)
  463. {
  464. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  465. struct snd_soc_component *scomp = scontrol->scomp;
  466. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  467. struct sof_abi_hdr *data = cdata->data;
  468. size_t size;
  469. int ret;
  470. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  471. dev_err_ratelimited(scomp->dev,
  472. "data max %zu exceeds ucontrol data array size\n",
  473. scontrol->max_size);
  474. return -EINVAL;
  475. }
  476. /* scontrol->max_size has been verified to be >= sizeof(struct sof_abi_hdr) */
  477. if (data->size > scontrol->max_size - sizeof(*data)) {
  478. dev_err_ratelimited(scomp->dev,
  479. "data size too big %u bytes max is %zu\n",
  480. data->size, scontrol->max_size - sizeof(*data));
  481. return -EINVAL;
  482. }
  483. size = data->size + sizeof(*data);
  484. /* copy from kcontrol */
  485. memcpy(data, ucontrol->value.bytes.data, size);
  486. ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
  487. if (!ret)
  488. /* Update the cdata size */
  489. scontrol->size = sizeof(*cdata) + size;
  490. return ret;
  491. }
  492. static int sof_ipc4_bytes_get(struct snd_sof_control *scontrol,
  493. struct snd_ctl_elem_value *ucontrol)
  494. {
  495. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  496. struct snd_soc_component *scomp = scontrol->scomp;
  497. struct sof_abi_hdr *data = cdata->data;
  498. size_t size;
  499. if (scontrol->max_size > sizeof(ucontrol->value.bytes.data)) {
  500. dev_err_ratelimited(scomp->dev, "data max %zu exceeds ucontrol data array size\n",
  501. scontrol->max_size);
  502. return -EINVAL;
  503. }
  504. if (data->size > scontrol->max_size - sizeof(*data)) {
  505. dev_err_ratelimited(scomp->dev,
  506. "%u bytes of control data is invalid, max is %zu\n",
  507. data->size, scontrol->max_size - sizeof(*data));
  508. return -EINVAL;
  509. }
  510. sof_ipc4_refresh_bytes_control(scontrol, true);
  511. size = data->size + sizeof(*data);
  512. /* copy back to kcontrol */
  513. memcpy(ucontrol->value.bytes.data, data, size);
  514. return 0;
  515. }
  516. static int sof_ipc4_bytes_ext_put(struct snd_sof_control *scontrol,
  517. const unsigned int __user *binary_data,
  518. unsigned int size)
  519. {
  520. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  521. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  522. struct snd_soc_component *scomp = scontrol->scomp;
  523. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  524. struct sof_abi_hdr *data = cdata->data;
  525. struct sof_abi_hdr abi_hdr;
  526. struct snd_ctl_tlv header;
  527. /*
  528. * The beginning of bytes data contains a header from where
  529. * the length (as bytes) is needed to know the correct copy
  530. * length of data from tlvd->tlv.
  531. */
  532. if (copy_from_user(&header, tlvd, sizeof(struct snd_ctl_tlv)))
  533. return -EFAULT;
  534. /* make sure TLV info is consistent */
  535. if (header.length + sizeof(struct snd_ctl_tlv) > size) {
  536. dev_err_ratelimited(scomp->dev,
  537. "Inconsistent TLV, data %d + header %zu > %d\n",
  538. header.length, sizeof(struct snd_ctl_tlv), size);
  539. return -EINVAL;
  540. }
  541. /* be->max is coming from topology */
  542. if (header.length > scontrol->max_size) {
  543. dev_err_ratelimited(scomp->dev,
  544. "Bytes data size %d exceeds max %zu\n",
  545. header.length, scontrol->max_size);
  546. return -EINVAL;
  547. }
  548. /* Check header id */
  549. if (header.numid != SOF_CTRL_CMD_BINARY) {
  550. dev_err_ratelimited(scomp->dev,
  551. "Incorrect numid for bytes put %d\n",
  552. header.numid);
  553. return -EINVAL;
  554. }
  555. /* Verify the ABI header first */
  556. if (copy_from_user(&abi_hdr, tlvd->tlv, sizeof(abi_hdr)))
  557. return -EFAULT;
  558. if (abi_hdr.magic != SOF_IPC4_ABI_MAGIC) {
  559. dev_err_ratelimited(scomp->dev, "Wrong ABI magic 0x%08x\n",
  560. abi_hdr.magic);
  561. return -EINVAL;
  562. }
  563. if (abi_hdr.size > scontrol->max_size - sizeof(abi_hdr)) {
  564. dev_err_ratelimited(scomp->dev,
  565. "%u bytes of control data is invalid, max is %zu\n",
  566. abi_hdr.size, scontrol->max_size - sizeof(abi_hdr));
  567. return -EINVAL;
  568. }
  569. if (!scontrol->old_ipc_control_data) {
  570. /* Create a backup of the current, valid bytes control */
  571. scontrol->old_ipc_control_data = kmemdup(scontrol->ipc_control_data,
  572. scontrol->size, GFP_KERNEL);
  573. if (!scontrol->old_ipc_control_data)
  574. return -ENOMEM;
  575. }
  576. /* Copy the whole binary data which includes the ABI header and the payload */
  577. if (copy_from_user(data, tlvd->tlv, header.length)) {
  578. memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,
  579. scontrol->size);
  580. kfree(scontrol->old_ipc_control_data);
  581. scontrol->old_ipc_control_data = NULL;
  582. return -EFAULT;
  583. }
  584. /* Update the cdata size */
  585. scontrol->size = sizeof(*cdata) + header.length;
  586. return sof_ipc4_set_get_bytes_data(sdev, scontrol, true, true);
  587. }
  588. static int _sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
  589. const unsigned int __user *binary_data,
  590. unsigned int size, bool from_dsp)
  591. {
  592. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  593. struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;
  594. struct snd_soc_component *scomp = scontrol->scomp;
  595. struct sof_abi_hdr *data = cdata->data;
  596. struct snd_ctl_tlv header;
  597. size_t data_size;
  598. /*
  599. * Decrement the limit by ext bytes header size to ensure the user space
  600. * buffer is not exceeded.
  601. */
  602. if (size < sizeof(struct snd_ctl_tlv))
  603. return -ENOSPC;
  604. size -= sizeof(struct snd_ctl_tlv);
  605. /* get all the component data from DSP */
  606. if (from_dsp) {
  607. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
  608. int ret = sof_ipc4_set_get_bytes_data(sdev, scontrol, false, true);
  609. if (ret < 0)
  610. return ret;
  611. /* Set the ABI magic (if the control is not initialized) */
  612. data->magic = SOF_IPC4_ABI_MAGIC;
  613. }
  614. if (data->size > scontrol->max_size - sizeof(*data)) {
  615. dev_err_ratelimited(scomp->dev,
  616. "%u bytes of control data is invalid, max is %zu\n",
  617. data->size, scontrol->max_size - sizeof(*data));
  618. return -EINVAL;
  619. }
  620. data_size = data->size + sizeof(struct sof_abi_hdr);
  621. /* make sure we don't exceed size provided by user space for data */
  622. if (data_size > size)
  623. return -ENOSPC;
  624. /* Set header id and length */
  625. header.numid = SOF_CTRL_CMD_BINARY;
  626. header.length = data_size;
  627. if (copy_to_user(tlvd, &header, sizeof(struct snd_ctl_tlv)))
  628. return -EFAULT;
  629. if (copy_to_user(tlvd->tlv, data, data_size))
  630. return -EFAULT;
  631. return 0;
  632. }
  633. static int sof_ipc4_bytes_ext_get(struct snd_sof_control *scontrol,
  634. const unsigned int __user *binary_data,
  635. unsigned int size)
  636. {
  637. sof_ipc4_refresh_bytes_control(scontrol, true);
  638. return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, false);
  639. }
  640. static int sof_ipc4_bytes_ext_volatile_get(struct snd_sof_control *scontrol,
  641. const unsigned int __user *binary_data,
  642. unsigned int size)
  643. {
  644. return _sof_ipc4_bytes_ext_get(scontrol, binary_data, size, true);
  645. }
  646. static int
  647. sof_ipc4_volsw_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
  648. struct snd_sof_control *scontrol)
  649. {
  650. if (scontrol->max == 1)
  651. return sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, false);
  652. return sof_ipc4_set_volume_data(sdev, swidget, scontrol, false);
  653. }
  654. #define PARAM_ID_FROM_EXTENSION(_ext) (((_ext) & SOF_IPC4_MOD_EXT_MSG_PARAM_ID_MASK) \
  655. >> SOF_IPC4_MOD_EXT_MSG_PARAM_ID_SHIFT)
  656. static void sof_ipc4_control_update(struct snd_sof_dev *sdev, void *ipc_message)
  657. {
  658. struct sof_ipc4_msg *ipc4_msg = ipc_message;
  659. struct sof_ipc4_notify_module_data *ndata = ipc4_msg->data_ptr;
  660. struct sof_ipc4_control_msg_payload *msg_data;
  661. struct sof_ipc4_control_data *cdata;
  662. struct snd_soc_dapm_widget *widget;
  663. struct snd_sof_control *scontrol;
  664. struct snd_sof_widget *swidget;
  665. struct snd_kcontrol *kc = NULL;
  666. bool scontrol_found = false;
  667. u32 event_param_id;
  668. int i, type;
  669. if (ndata->event_data_size < sizeof(*msg_data)) {
  670. dev_err(sdev->dev,
  671. "%s: Invalid event data size for module %u.%u: %u\n",
  672. __func__, ndata->module_id, ndata->instance_id,
  673. ndata->event_data_size);
  674. return;
  675. }
  676. event_param_id = ndata->event_id & SOF_IPC4_NOTIFY_MODULE_EVENTID_ALSA_PARAMID_MASK;
  677. switch (event_param_id) {
  678. case SOF_IPC4_SWITCH_CONTROL_PARAM_ID:
  679. type = SND_SOC_TPLG_TYPE_MIXER;
  680. break;
  681. case SOF_IPC4_ENUM_CONTROL_PARAM_ID:
  682. type = SND_SOC_TPLG_TYPE_ENUM;
  683. break;
  684. case SOF_IPC4_BYTES_CONTROL_PARAM_ID:
  685. type = SND_SOC_TPLG_TYPE_BYTES;
  686. break;
  687. default:
  688. dev_err(sdev->dev,
  689. "%s: Invalid control type for module %u.%u: %u\n",
  690. __func__, ndata->module_id, ndata->instance_id,
  691. event_param_id);
  692. return;
  693. }
  694. /* Find the swidget based on ndata->module_id and ndata->instance_id */
  695. swidget = sof_ipc4_find_swidget_by_ids(sdev, ndata->module_id,
  696. ndata->instance_id);
  697. if (!swidget) {
  698. dev_err(sdev->dev, "%s: Failed to find widget for module %u.%u\n",
  699. __func__, ndata->module_id, ndata->instance_id);
  700. return;
  701. }
  702. /* Find the scontrol which is the source of the notification */
  703. msg_data = (struct sof_ipc4_control_msg_payload *)ndata->event_data;
  704. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  705. if (scontrol->comp_id == swidget->comp_id) {
  706. u32 local_param_id;
  707. cdata = scontrol->ipc_control_data;
  708. /*
  709. * The scontrol's param_id is stored in the IPC message
  710. * template's extension
  711. */
  712. local_param_id = PARAM_ID_FROM_EXTENSION(cdata->msg.extension);
  713. if (local_param_id == event_param_id &&
  714. msg_data->id == cdata->index) {
  715. scontrol_found = true;
  716. break;
  717. }
  718. }
  719. }
  720. if (!scontrol_found) {
  721. dev_err(sdev->dev,
  722. "%s: Failed to find control on widget %s: %u:%u\n",
  723. __func__, swidget->widget->name, ndata->event_id & 0xffff,
  724. msg_data->id);
  725. return;
  726. }
  727. if (msg_data->num_elems) {
  728. /*
  729. * The message includes the updated value/data, update the
  730. * control's local cache using the received notification
  731. */
  732. if (type == SND_SOC_TPLG_TYPE_BYTES) {
  733. struct sof_abi_hdr *data = cdata->data;
  734. if (msg_data->num_elems > scontrol->max_size - sizeof(*data)) {
  735. dev_warn(sdev->dev,
  736. "%s: no space for data in %s (%u, %zu)\n",
  737. __func__, scontrol->name, msg_data->num_elems,
  738. scontrol->max_size - sizeof(*data));
  739. } else {
  740. memcpy(data->data, msg_data->data, msg_data->num_elems);
  741. data->size = msg_data->num_elems;
  742. scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;
  743. }
  744. } else {
  745. for (i = 0; i < msg_data->num_elems; i++) {
  746. u32 channel = msg_data->chanv[i].channel;
  747. if (channel >= scontrol->num_channels) {
  748. dev_warn(sdev->dev,
  749. "Invalid channel index for %s: %u\n",
  750. scontrol->name, i);
  751. /*
  752. * Mark the scontrol as dirty to force a refresh
  753. * on next read
  754. */
  755. scontrol->comp_data_dirty = true;
  756. break;
  757. }
  758. cdata->chanv[channel].value = msg_data->chanv[i].value;
  759. }
  760. }
  761. } else {
  762. /*
  763. * Mark the scontrol as dirty because the value/data is changed
  764. * in firmware, forcing a refresh on next read access
  765. */
  766. scontrol->comp_data_dirty = true;
  767. }
  768. /*
  769. * Look up the ALSA kcontrol of the scontrol to be able to send a
  770. * notification to user space
  771. */
  772. widget = swidget->widget;
  773. for (i = 0; i < widget->num_kcontrols; i++) {
  774. /* skip non matching types or non matching indexes within type */
  775. if (widget->dobj.widget.kcontrol_type[i] == type &&
  776. widget->kcontrol_news[i].index == cdata->index) {
  777. kc = widget->kcontrols[i];
  778. break;
  779. }
  780. }
  781. if (!kc)
  782. return;
  783. snd_ctl_notify_one(swidget->scomp->card->snd_card,
  784. SNDRV_CTL_EVENT_MASK_VALUE, kc, 0);
  785. }
  786. /* set up all controls for the widget */
  787. static int sof_ipc4_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
  788. {
  789. struct snd_sof_control *scontrol;
  790. int ret = 0;
  791. list_for_each_entry(scontrol, &sdev->kcontrol_list, list) {
  792. if (scontrol->comp_id == swidget->comp_id) {
  793. switch (scontrol->info_type) {
  794. case SND_SOC_TPLG_CTL_VOLSW:
  795. case SND_SOC_TPLG_CTL_VOLSW_SX:
  796. case SND_SOC_TPLG_CTL_VOLSW_XR_SX:
  797. ret = sof_ipc4_volsw_setup(sdev, swidget, scontrol);
  798. break;
  799. case SND_SOC_TPLG_CTL_BYTES:
  800. ret = sof_ipc4_set_get_bytes_data(sdev, scontrol,
  801. true, false);
  802. break;
  803. case SND_SOC_TPLG_CTL_ENUM:
  804. case SND_SOC_TPLG_CTL_ENUM_VALUE:
  805. ret = sof_ipc4_set_generic_control_data(sdev, swidget,
  806. scontrol, false);
  807. break;
  808. default:
  809. break;
  810. }
  811. if (ret < 0) {
  812. dev_err(sdev->dev,
  813. "kcontrol %d set up failed for widget %s\n",
  814. scontrol->comp_id, swidget->widget->name);
  815. return ret;
  816. }
  817. }
  818. }
  819. return 0;
  820. }
  821. static int
  822. sof_ipc4_set_up_volume_table(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS], int size)
  823. {
  824. int i;
  825. /* init the volume table */
  826. scontrol->volume_table = kcalloc(size, sizeof(u32), GFP_KERNEL);
  827. if (!scontrol->volume_table)
  828. return -ENOMEM;
  829. /* populate the volume table */
  830. for (i = 0; i < size ; i++) {
  831. u32 val = vol_compute_gain(i, tlv);
  832. u64 q31val = ((u64)val) << 15; /* Can be over Q1.31, need to saturate */
  833. scontrol->volume_table[i] = q31val > SOF_IPC4_VOL_ZERO_DB ?
  834. SOF_IPC4_VOL_ZERO_DB : q31val;
  835. }
  836. return 0;
  837. }
  838. const struct sof_ipc_tplg_control_ops tplg_ipc4_control_ops = {
  839. .volume_put = sof_ipc4_volume_put,
  840. .volume_get = sof_ipc4_volume_get,
  841. .switch_put = sof_ipc4_switch_put,
  842. .switch_get = sof_ipc4_switch_get,
  843. .enum_put = sof_ipc4_enum_put,
  844. .enum_get = sof_ipc4_enum_get,
  845. .bytes_put = sof_ipc4_bytes_put,
  846. .bytes_get = sof_ipc4_bytes_get,
  847. .bytes_ext_put = sof_ipc4_bytes_ext_put,
  848. .bytes_ext_get = sof_ipc4_bytes_ext_get,
  849. .bytes_ext_volatile_get = sof_ipc4_bytes_ext_volatile_get,
  850. .update = sof_ipc4_control_update,
  851. .widget_kcontrol_setup = sof_ipc4_widget_kcontrol_setup,
  852. .set_up_volume_table = sof_ipc4_set_up_volume_table,
  853. };