control.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. //
  10. /* Mixer Controls */
  11. #include <linux/pm_runtime.h>
  12. #include <linux/leds.h>
  13. #include "sof-priv.h"
  14. #include "sof-audio.h"
  15. static void update_mute_led(struct snd_sof_control *scontrol,
  16. struct snd_kcontrol *kcontrol,
  17. struct snd_ctl_elem_value *ucontrol)
  18. {
  19. int temp = 0;
  20. int mask;
  21. int i;
  22. mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  23. for (i = 0; i < scontrol->num_channels; i++) {
  24. if (ucontrol->value.integer.value[i]) {
  25. temp |= mask;
  26. break;
  27. }
  28. }
  29. if (temp == scontrol->led_ctl.led_value)
  30. return;
  31. scontrol->led_ctl.led_value = temp;
  32. #if IS_REACHABLE(CONFIG_LEDS_TRIGGER_AUDIO)
  33. if (!scontrol->led_ctl.direction)
  34. ledtrig_audio_set(LED_AUDIO_MUTE, temp ? LED_OFF : LED_ON);
  35. else
  36. ledtrig_audio_set(LED_AUDIO_MICMUTE, temp ? LED_OFF : LED_ON);
  37. #endif
  38. }
  39. static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
  40. {
  41. if (value >= size)
  42. return volume_map[size - 1];
  43. return volume_map[value];
  44. }
  45. static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
  46. {
  47. int i;
  48. for (i = 0; i < size; i++) {
  49. if (volume_map[i] >= value)
  50. return i;
  51. }
  52. return i - 1;
  53. }
  54. int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
  55. struct snd_ctl_elem_value *ucontrol)
  56. {
  57. struct soc_mixer_control *sm =
  58. (struct soc_mixer_control *)kcontrol->private_value;
  59. struct snd_sof_control *scontrol = sm->dobj.private;
  60. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  61. unsigned int i, channels = scontrol->num_channels;
  62. /* read back each channel */
  63. for (i = 0; i < channels; i++)
  64. ucontrol->value.integer.value[i] =
  65. ipc_to_mixer(cdata->chanv[i].value,
  66. scontrol->volume_table, sm->max + 1);
  67. return 0;
  68. }
  69. int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
  70. struct snd_ctl_elem_value *ucontrol)
  71. {
  72. struct soc_mixer_control *sm =
  73. (struct soc_mixer_control *)kcontrol->private_value;
  74. struct snd_sof_control *scontrol = sm->dobj.private;
  75. struct snd_soc_component *scomp = scontrol->scomp;
  76. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  77. unsigned int i, channels = scontrol->num_channels;
  78. bool change = false;
  79. u32 value;
  80. /* update each channel */
  81. for (i = 0; i < channels; i++) {
  82. value = mixer_to_ipc(ucontrol->value.integer.value[i],
  83. scontrol->volume_table, sm->max + 1);
  84. change = change || (value != cdata->chanv[i].value);
  85. cdata->chanv[i].channel = i;
  86. cdata->chanv[i].value = value;
  87. }
  88. /* notify DSP of mixer updates */
  89. if (pm_runtime_active(scomp->dev))
  90. snd_sof_ipc_set_get_comp_data(scontrol,
  91. SOF_IPC_COMP_SET_VALUE,
  92. SOF_CTRL_TYPE_VALUE_CHAN_GET,
  93. SOF_CTRL_CMD_VOLUME,
  94. true);
  95. return change;
  96. }
  97. int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
  98. struct snd_ctl_elem_value *ucontrol)
  99. {
  100. struct soc_mixer_control *sm =
  101. (struct soc_mixer_control *)kcontrol->private_value;
  102. struct snd_sof_control *scontrol = sm->dobj.private;
  103. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  104. unsigned int i, channels = scontrol->num_channels;
  105. /* read back each channel */
  106. for (i = 0; i < channels; i++)
  107. ucontrol->value.integer.value[i] = cdata->chanv[i].value;
  108. return 0;
  109. }
  110. int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
  111. struct snd_ctl_elem_value *ucontrol)
  112. {
  113. struct soc_mixer_control *sm =
  114. (struct soc_mixer_control *)kcontrol->private_value;
  115. struct snd_sof_control *scontrol = sm->dobj.private;
  116. struct snd_soc_component *scomp = scontrol->scomp;
  117. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  118. unsigned int i, channels = scontrol->num_channels;
  119. bool change = false;
  120. u32 value;
  121. /* update each channel */
  122. for (i = 0; i < channels; i++) {
  123. value = ucontrol->value.integer.value[i];
  124. change = change || (value != cdata->chanv[i].value);
  125. cdata->chanv[i].channel = i;
  126. cdata->chanv[i].value = value;
  127. }
  128. if (scontrol->led_ctl.use_led)
  129. update_mute_led(scontrol, kcontrol, ucontrol);
  130. /* notify DSP of mixer updates */
  131. if (pm_runtime_active(scomp->dev))
  132. snd_sof_ipc_set_get_comp_data(scontrol,
  133. SOF_IPC_COMP_SET_VALUE,
  134. SOF_CTRL_TYPE_VALUE_CHAN_GET,
  135. SOF_CTRL_CMD_SWITCH,
  136. true);
  137. return change;
  138. }
  139. int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
  140. struct snd_ctl_elem_value *ucontrol)
  141. {
  142. struct soc_enum *se =
  143. (struct soc_enum *)kcontrol->private_value;
  144. struct snd_sof_control *scontrol = se->dobj.private;
  145. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  146. unsigned int i, channels = scontrol->num_channels;
  147. /* read back each channel */
  148. for (i = 0; i < channels; i++)
  149. ucontrol->value.enumerated.item[i] = cdata->chanv[i].value;
  150. return 0;
  151. }
  152. int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol)
  154. {
  155. struct soc_enum *se =
  156. (struct soc_enum *)kcontrol->private_value;
  157. struct snd_sof_control *scontrol = se->dobj.private;
  158. struct snd_soc_component *scomp = scontrol->scomp;
  159. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  160. unsigned int i, channels = scontrol->num_channels;
  161. bool change = false;
  162. u32 value;
  163. /* update each channel */
  164. for (i = 0; i < channels; i++) {
  165. value = ucontrol->value.enumerated.item[i];
  166. change = change || (value != cdata->chanv[i].value);
  167. cdata->chanv[i].channel = i;
  168. cdata->chanv[i].value = value;
  169. }
  170. /* notify DSP of enum updates */
  171. if (pm_runtime_active(scomp->dev))
  172. snd_sof_ipc_set_get_comp_data(scontrol,
  173. SOF_IPC_COMP_SET_VALUE,
  174. SOF_CTRL_TYPE_VALUE_CHAN_GET,
  175. SOF_CTRL_CMD_ENUM,
  176. true);
  177. return change;
  178. }
  179. int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
  180. struct snd_ctl_elem_value *ucontrol)
  181. {
  182. struct soc_bytes_ext *be =
  183. (struct soc_bytes_ext *)kcontrol->private_value;
  184. struct snd_sof_control *scontrol = be->dobj.private;
  185. struct snd_soc_component *scomp = scontrol->scomp;
  186. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  187. struct sof_abi_hdr *data = cdata->data;
  188. size_t size;
  189. if (be->max > sizeof(ucontrol->value.bytes.data)) {
  190. dev_err_ratelimited(scomp->dev,
  191. "error: data max %d exceeds ucontrol data array size\n",
  192. be->max);
  193. return -EINVAL;
  194. }
  195. /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
  196. if (data->size > be->max - sizeof(*data)) {
  197. dev_err_ratelimited(scomp->dev,
  198. "error: %u bytes of control data is invalid, max is %zu\n",
  199. data->size, be->max - sizeof(*data));
  200. return -EINVAL;
  201. }
  202. size = data->size + sizeof(*data);
  203. /* copy back to kcontrol */
  204. memcpy(ucontrol->value.bytes.data, data, size);
  205. return 0;
  206. }
  207. int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
  208. struct snd_ctl_elem_value *ucontrol)
  209. {
  210. struct soc_bytes_ext *be =
  211. (struct soc_bytes_ext *)kcontrol->private_value;
  212. struct snd_sof_control *scontrol = be->dobj.private;
  213. struct snd_soc_component *scomp = scontrol->scomp;
  214. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  215. struct sof_abi_hdr *data = cdata->data;
  216. size_t size;
  217. if (be->max > sizeof(ucontrol->value.bytes.data)) {
  218. dev_err_ratelimited(scomp->dev,
  219. "error: data max %d exceeds ucontrol data array size\n",
  220. be->max);
  221. return -EINVAL;
  222. }
  223. /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
  224. if (data->size > be->max - sizeof(*data)) {
  225. dev_err_ratelimited(scomp->dev,
  226. "error: data size too big %u bytes max is %zu\n",
  227. data->size, be->max - sizeof(*data));
  228. return -EINVAL;
  229. }
  230. size = data->size + sizeof(*data);
  231. /* copy from kcontrol */
  232. memcpy(data, ucontrol->value.bytes.data, size);
  233. /* notify DSP of byte control updates */
  234. if (pm_runtime_active(scomp->dev))
  235. snd_sof_ipc_set_get_comp_data(scontrol,
  236. SOF_IPC_COMP_SET_DATA,
  237. SOF_CTRL_TYPE_DATA_SET,
  238. scontrol->cmd,
  239. true);
  240. return 0;
  241. }
  242. int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
  243. const unsigned int __user *binary_data,
  244. unsigned int size)
  245. {
  246. struct soc_bytes_ext *be =
  247. (struct soc_bytes_ext *)kcontrol->private_value;
  248. struct snd_sof_control *scontrol = be->dobj.private;
  249. struct snd_soc_component *scomp = scontrol->scomp;
  250. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  251. struct snd_ctl_tlv header;
  252. const struct snd_ctl_tlv __user *tlvd =
  253. (const struct snd_ctl_tlv __user *)binary_data;
  254. /* make sure we have at least a header */
  255. if (size < sizeof(struct snd_ctl_tlv))
  256. return -EINVAL;
  257. /*
  258. * The beginning of bytes data contains a header from where
  259. * the length (as bytes) is needed to know the correct copy
  260. * length of data from tlvd->tlv.
  261. */
  262. if (copy_from_user(&header, tlvd, sizeof(const struct snd_ctl_tlv)))
  263. return -EFAULT;
  264. /* make sure TLV info is consistent */
  265. if (header.length + sizeof(struct snd_ctl_tlv) > size) {
  266. dev_err_ratelimited(scomp->dev, "error: inconsistent TLV, data %d + header %zu > %d\n",
  267. header.length, sizeof(struct snd_ctl_tlv), size);
  268. return -EINVAL;
  269. }
  270. /* be->max is coming from topology */
  271. if (header.length > be->max) {
  272. dev_err_ratelimited(scomp->dev, "error: Bytes data size %d exceeds max %d.\n",
  273. header.length, be->max);
  274. return -EINVAL;
  275. }
  276. /* Check that header id matches the command */
  277. if (header.numid != scontrol->cmd) {
  278. dev_err_ratelimited(scomp->dev,
  279. "error: incorrect numid %d\n",
  280. header.numid);
  281. return -EINVAL;
  282. }
  283. if (copy_from_user(cdata->data, tlvd->tlv, header.length))
  284. return -EFAULT;
  285. if (cdata->data->magic != SOF_ABI_MAGIC) {
  286. dev_err_ratelimited(scomp->dev,
  287. "error: Wrong ABI magic 0x%08x.\n",
  288. cdata->data->magic);
  289. return -EINVAL;
  290. }
  291. if (SOF_ABI_VERSION_INCOMPATIBLE(SOF_ABI_VERSION, cdata->data->abi)) {
  292. dev_err_ratelimited(scomp->dev, "error: Incompatible ABI version 0x%08x.\n",
  293. cdata->data->abi);
  294. return -EINVAL;
  295. }
  296. /* be->max has been verified to be >= sizeof(struct sof_abi_hdr) */
  297. if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
  298. dev_err_ratelimited(scomp->dev, "error: Mismatch in ABI data size (truncated?).\n");
  299. return -EINVAL;
  300. }
  301. /* notify DSP of byte control updates */
  302. if (pm_runtime_active(scomp->dev))
  303. snd_sof_ipc_set_get_comp_data(scontrol,
  304. SOF_IPC_COMP_SET_DATA,
  305. SOF_CTRL_TYPE_DATA_SET,
  306. scontrol->cmd,
  307. true);
  308. return 0;
  309. }
  310. int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
  311. unsigned int size)
  312. {
  313. struct soc_bytes_ext *be = (struct soc_bytes_ext *)kcontrol->private_value;
  314. struct snd_sof_control *scontrol = be->dobj.private;
  315. struct snd_soc_component *scomp = scontrol->scomp;
  316. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  317. struct snd_ctl_tlv header;
  318. struct snd_ctl_tlv __user *tlvd = (struct snd_ctl_tlv __user *)binary_data;
  319. size_t data_size;
  320. int ret;
  321. int err;
  322. /*
  323. * Decrement the limit by ext bytes header size to
  324. * ensure the user space buffer is not exceeded.
  325. */
  326. if (size < sizeof(struct snd_ctl_tlv))
  327. return -ENOSPC;
  328. size -= sizeof(struct snd_ctl_tlv);
  329. ret = pm_runtime_get_sync(scomp->dev);
  330. if (ret < 0 && ret != -EACCES) {
  331. dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to resume %d\n", ret);
  332. pm_runtime_put_noidle(scomp->dev);
  333. return ret;
  334. }
  335. /* set the ABI header values */
  336. cdata->data->magic = SOF_ABI_MAGIC;
  337. cdata->data->abi = SOF_ABI_VERSION;
  338. /* get all the component data from DSP */
  339. ret = snd_sof_ipc_set_get_comp_data(scontrol, SOF_IPC_COMP_GET_DATA, SOF_CTRL_TYPE_DATA_GET,
  340. scontrol->cmd, false);
  341. if (ret < 0)
  342. goto out;
  343. /* check data size doesn't exceed max coming from topology */
  344. if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
  345. dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
  346. cdata->data->size,
  347. be->max - sizeof(const struct sof_abi_hdr));
  348. ret = -EINVAL;
  349. goto out;
  350. }
  351. data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
  352. /* make sure we don't exceed size provided by user space for data */
  353. if (data_size > size) {
  354. ret = -ENOSPC;
  355. goto out;
  356. }
  357. header.numid = scontrol->cmd;
  358. header.length = data_size;
  359. if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv))) {
  360. ret = -EFAULT;
  361. goto out;
  362. }
  363. if (copy_to_user(tlvd->tlv, cdata->data, data_size))
  364. ret = -EFAULT;
  365. out:
  366. pm_runtime_mark_last_busy(scomp->dev);
  367. err = pm_runtime_put_autosuspend(scomp->dev);
  368. if (err < 0)
  369. dev_err_ratelimited(scomp->dev, "error: bytes_ext get failed to idle %d\n", err);
  370. return ret;
  371. }
  372. int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
  373. unsigned int __user *binary_data,
  374. unsigned int size)
  375. {
  376. struct soc_bytes_ext *be =
  377. (struct soc_bytes_ext *)kcontrol->private_value;
  378. struct snd_sof_control *scontrol = be->dobj.private;
  379. struct snd_soc_component *scomp = scontrol->scomp;
  380. struct sof_ipc_ctrl_data *cdata = scontrol->control_data;
  381. struct snd_ctl_tlv header;
  382. struct snd_ctl_tlv __user *tlvd =
  383. (struct snd_ctl_tlv __user *)binary_data;
  384. size_t data_size;
  385. /*
  386. * Decrement the limit by ext bytes header size to
  387. * ensure the user space buffer is not exceeded.
  388. */
  389. if (size < sizeof(struct snd_ctl_tlv))
  390. return -ENOSPC;
  391. size -= sizeof(struct snd_ctl_tlv);
  392. /* set the ABI header values */
  393. cdata->data->magic = SOF_ABI_MAGIC;
  394. cdata->data->abi = SOF_ABI_VERSION;
  395. /* check data size doesn't exceed max coming from topology */
  396. if (cdata->data->size > be->max - sizeof(const struct sof_abi_hdr)) {
  397. dev_err_ratelimited(scomp->dev, "error: user data size %d exceeds max size %zu.\n",
  398. cdata->data->size,
  399. be->max - sizeof(const struct sof_abi_hdr));
  400. return -EINVAL;
  401. }
  402. data_size = cdata->data->size + sizeof(const struct sof_abi_hdr);
  403. /* make sure we don't exceed size provided by user space for data */
  404. if (data_size > size)
  405. return -ENOSPC;
  406. header.numid = scontrol->cmd;
  407. header.length = data_size;
  408. if (copy_to_user(tlvd, &header, sizeof(const struct snd_ctl_tlv)))
  409. return -EFAULT;
  410. if (copy_to_user(tlvd->tlv, cdata->data, data_size))
  411. return -EFAULT;
  412. return 0;
  413. }