msm-transcode-loopback-q6-v2.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/time.h>
  17. #include <linux/math64.h>
  18. #include <linux/wait.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <sound/pcm.h>
  25. #include <sound/initval.h>
  26. #include <sound/control.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/timer.h>
  29. #include <sound/tlv.h>
  30. #include <sound/compress_params.h>
  31. #include <sound/compress_offload.h>
  32. #include <sound/compress_driver.h>
  33. #include <dsp/msm_audio_ion.h>
  34. #include <dsp/apr_audio-v2.h>
  35. #include <dsp/q6asm-v2.h>
  36. #include "msm-pcm-routing-v2.h"
  37. #include "msm-qti-pp-config.h"
  38. #define LOOPBACK_SESSION_MAX_NUM_STREAMS 2
  39. static DEFINE_MUTEX(transcode_loopback_session_lock);
  40. struct trans_loopback_pdata {
  41. struct snd_compr_stream *cstream[MSM_FRONTEND_DAI_MAX];
  42. };
  43. struct loopback_stream {
  44. struct snd_compr_stream *cstream;
  45. uint32_t codec_format;
  46. bool start;
  47. };
  48. enum loopback_session_state {
  49. /* One or both streams not opened */
  50. LOOPBACK_SESSION_CLOSE = 0,
  51. /* Loopback streams opened */
  52. LOOPBACK_SESSION_READY,
  53. /* Loopback streams opened and formats configured */
  54. LOOPBACK_SESSION_START,
  55. /* Trigger issued on either of streams when in START state */
  56. LOOPBACK_SESSION_RUN
  57. };
  58. struct msm_transcode_loopback {
  59. struct loopback_stream source;
  60. struct loopback_stream sink;
  61. struct snd_compr_caps source_compr_cap;
  62. struct snd_compr_caps sink_compr_cap;
  63. uint32_t instance;
  64. uint32_t num_streams;
  65. int session_state;
  66. struct mutex lock;
  67. int session_id;
  68. struct audio_client *audio_client;
  69. };
  70. /* Transcode loopback global info struct */
  71. static struct msm_transcode_loopback transcode_info;
  72. static void loopback_event_handler(uint32_t opcode,
  73. uint32_t token, uint32_t *payload, void *priv)
  74. {
  75. struct msm_transcode_loopback *trans =
  76. (struct msm_transcode_loopback *)priv;
  77. struct snd_soc_pcm_runtime *rtd;
  78. struct snd_compr_stream *cstream;
  79. struct audio_client *ac;
  80. int stream_id;
  81. int ret;
  82. if (!trans || !payload) {
  83. pr_err("%s: rtd or payload is NULL\n", __func__);
  84. return;
  85. }
  86. cstream = trans->source.cstream;
  87. ac = trans->audio_client;
  88. /*
  89. * Token for rest of the compressed commands use to set
  90. * session id, stream id, dir etc.
  91. */
  92. stream_id = q6asm_get_stream_id_from_token(token);
  93. switch (opcode) {
  94. case ASM_STREAM_CMD_ENCDEC_EVENTS:
  95. case ASM_IEC_61937_MEDIA_FMT_EVENT:
  96. pr_debug("%s: ASM_IEC_61937_MEDIA_FMT_EVENT\n", __func__);
  97. rtd = cstream->private_data;
  98. if (!rtd) {
  99. pr_err("%s: rtd is NULL\n", __func__);
  100. return;
  101. }
  102. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  103. if (ret) {
  104. pr_err("%s: failed to inform mixer ctrl. err = %d\n",
  105. __func__, ret);
  106. return;
  107. }
  108. break;
  109. case APR_BASIC_RSP_RESULT: {
  110. switch (payload[0]) {
  111. case ASM_SESSION_CMD_RUN_V2:
  112. pr_debug("%s: ASM_SESSION_CMD_RUN_V2:", __func__);
  113. pr_debug("token 0x%x, stream id %d\n", token,
  114. stream_id);
  115. break;
  116. case ASM_STREAM_CMD_CLOSE:
  117. pr_debug("%s: ASM_DATA_CMD_CLOSE:", __func__);
  118. pr_debug("token 0x%x, stream id %d\n", token,
  119. stream_id);
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. }
  126. default:
  127. pr_debug("%s: Not Supported Event opcode[0x%x]\n",
  128. __func__, opcode);
  129. break;
  130. }
  131. }
  132. static void populate_codec_list(struct msm_transcode_loopback *trans,
  133. struct snd_compr_stream *cstream)
  134. {
  135. struct snd_compr_caps compr_cap;
  136. pr_debug("%s\n", __func__);
  137. memset(&compr_cap, 0, sizeof(struct snd_compr_caps));
  138. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  139. compr_cap.direction = SND_COMPRESS_CAPTURE;
  140. compr_cap.num_codecs = 3;
  141. compr_cap.codecs[0] = SND_AUDIOCODEC_PCM;
  142. compr_cap.codecs[1] = SND_AUDIOCODEC_AC3;
  143. compr_cap.codecs[2] = SND_AUDIOCODEC_EAC3;
  144. memcpy(&trans->source_compr_cap, &compr_cap,
  145. sizeof(struct snd_compr_caps));
  146. }
  147. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  148. compr_cap.direction = SND_COMPRESS_PLAYBACK;
  149. compr_cap.num_codecs = 1;
  150. compr_cap.codecs[0] = SND_AUDIOCODEC_PCM;
  151. memcpy(&trans->sink_compr_cap, &compr_cap,
  152. sizeof(struct snd_compr_caps));
  153. }
  154. }
  155. static int msm_transcode_loopback_open(struct snd_compr_stream *cstream)
  156. {
  157. int ret = 0;
  158. struct snd_compr_runtime *runtime;
  159. struct snd_soc_pcm_runtime *rtd;
  160. struct msm_transcode_loopback *trans = &transcode_info;
  161. struct trans_loopback_pdata *pdata;
  162. if (cstream == NULL) {
  163. pr_err("%s: Invalid substream\n", __func__);
  164. return -EINVAL;
  165. }
  166. runtime = cstream->runtime;
  167. rtd = snd_pcm_substream_chip(cstream);
  168. pdata = snd_soc_platform_get_drvdata(rtd->platform);
  169. pdata->cstream[rtd->dai_link->id] = cstream;
  170. mutex_lock(&trans->lock);
  171. if (trans->num_streams > LOOPBACK_SESSION_MAX_NUM_STREAMS) {
  172. pr_err("msm_transcode_open failed..invalid stream\n");
  173. ret = -EINVAL;
  174. goto exit;
  175. }
  176. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  177. if (trans->source.cstream == NULL) {
  178. trans->source.cstream = cstream;
  179. trans->num_streams++;
  180. } else {
  181. pr_err("%s: capture stream already opened\n",
  182. __func__);
  183. ret = -EINVAL;
  184. goto exit;
  185. }
  186. } else if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  187. if (trans->sink.cstream == NULL) {
  188. trans->sink.cstream = cstream;
  189. trans->num_streams++;
  190. } else {
  191. pr_debug("%s: playback stream already opened\n",
  192. __func__);
  193. ret = -EINVAL;
  194. goto exit;
  195. }
  196. }
  197. pr_debug("%s: num stream%d, stream name %s\n", __func__,
  198. trans->num_streams, cstream->name);
  199. populate_codec_list(trans, cstream);
  200. if (trans->num_streams == LOOPBACK_SESSION_MAX_NUM_STREAMS) {
  201. pr_debug("%s: Moving loopback session to READY state %d\n",
  202. __func__, trans->session_state);
  203. trans->session_state = LOOPBACK_SESSION_READY;
  204. }
  205. runtime->private_data = trans;
  206. if (trans->num_streams == 1)
  207. msm_adsp_init_mixer_ctl_pp_event_queue(rtd);
  208. exit:
  209. mutex_unlock(&trans->lock);
  210. return ret;
  211. }
  212. static void stop_transcoding(struct msm_transcode_loopback *trans)
  213. {
  214. struct snd_soc_pcm_runtime *soc_pcm_rx;
  215. struct snd_soc_pcm_runtime *soc_pcm_tx;
  216. if (trans->audio_client != NULL) {
  217. q6asm_cmd(trans->audio_client, CMD_CLOSE);
  218. if (trans->sink.cstream != NULL) {
  219. soc_pcm_rx = trans->sink.cstream->private_data;
  220. msm_pcm_routing_dereg_phy_stream(
  221. soc_pcm_rx->dai_link->id,
  222. SND_COMPRESS_PLAYBACK);
  223. }
  224. if (trans->source.cstream != NULL) {
  225. soc_pcm_tx = trans->source.cstream->private_data;
  226. msm_pcm_routing_dereg_phy_stream(
  227. soc_pcm_tx->dai_link->id,
  228. SND_COMPRESS_CAPTURE);
  229. }
  230. q6asm_audio_client_free(trans->audio_client);
  231. trans->audio_client = NULL;
  232. }
  233. }
  234. static int msm_transcode_loopback_free(struct snd_compr_stream *cstream)
  235. {
  236. struct snd_compr_runtime *runtime = cstream->runtime;
  237. struct msm_transcode_loopback *trans = runtime->private_data;
  238. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(cstream);
  239. int ret = 0;
  240. mutex_lock(&trans->lock);
  241. pr_debug("%s: Transcode loopback end:%d, streams %d\n", __func__,
  242. cstream->direction, trans->num_streams);
  243. trans->num_streams--;
  244. stop_transcoding(trans);
  245. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  246. memset(&trans->sink, 0, sizeof(struct loopback_stream));
  247. else if (cstream->direction == SND_COMPRESS_CAPTURE)
  248. memset(&trans->source, 0, sizeof(struct loopback_stream));
  249. trans->session_state = LOOPBACK_SESSION_CLOSE;
  250. if (trans->num_streams == 1)
  251. msm_adsp_clean_mixer_ctl_pp_event_queue(rtd);
  252. mutex_unlock(&trans->lock);
  253. return ret;
  254. }
  255. static int msm_transcode_loopback_trigger(struct snd_compr_stream *cstream,
  256. int cmd)
  257. {
  258. struct snd_compr_runtime *runtime = cstream->runtime;
  259. struct msm_transcode_loopback *trans = runtime->private_data;
  260. switch (cmd) {
  261. case SNDRV_PCM_TRIGGER_START:
  262. case SNDRV_PCM_TRIGGER_RESUME:
  263. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  264. if (trans->session_state == LOOPBACK_SESSION_START) {
  265. pr_debug("%s: Issue Loopback session %d RUN\n",
  266. __func__, trans->instance);
  267. q6asm_run_nowait(trans->audio_client, 0, 0, 0);
  268. trans->session_state = LOOPBACK_SESSION_RUN;
  269. }
  270. break;
  271. case SNDRV_PCM_TRIGGER_SUSPEND:
  272. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  273. case SNDRV_PCM_TRIGGER_STOP:
  274. pr_debug("%s: Issue Loopback session %d STOP\n", __func__,
  275. trans->instance);
  276. if (trans->session_state == LOOPBACK_SESSION_RUN)
  277. q6asm_cmd_nowait(trans->audio_client, CMD_PAUSE);
  278. trans->session_state = LOOPBACK_SESSION_START;
  279. break;
  280. default:
  281. break;
  282. }
  283. return 0;
  284. }
  285. static int msm_transcode_loopback_set_params(struct snd_compr_stream *cstream,
  286. struct snd_compr_params *codec_param)
  287. {
  288. struct snd_compr_runtime *runtime = cstream->runtime;
  289. struct msm_transcode_loopback *trans = runtime->private_data;
  290. struct snd_soc_pcm_runtime *soc_pcm_rx;
  291. struct snd_soc_pcm_runtime *soc_pcm_tx;
  292. uint32_t bit_width = 16;
  293. int ret = 0;
  294. if (trans == NULL) {
  295. pr_err("%s: Invalid param\n", __func__);
  296. return -EINVAL;
  297. }
  298. mutex_lock(&trans->lock);
  299. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  300. if (codec_param->codec.id == SND_AUDIOCODEC_PCM) {
  301. trans->sink.codec_format =
  302. FORMAT_LINEAR_PCM;
  303. switch (codec_param->codec.format) {
  304. case SNDRV_PCM_FORMAT_S32_LE:
  305. bit_width = 32;
  306. break;
  307. case SNDRV_PCM_FORMAT_S24_LE:
  308. bit_width = 24;
  309. break;
  310. case SNDRV_PCM_FORMAT_S24_3LE:
  311. bit_width = 24;
  312. break;
  313. case SNDRV_PCM_FORMAT_S16_LE:
  314. default:
  315. bit_width = 16;
  316. break;
  317. }
  318. } else {
  319. pr_debug("%s: unknown sink codec\n", __func__);
  320. ret = -EINVAL;
  321. goto exit;
  322. }
  323. trans->sink.start = true;
  324. }
  325. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  326. switch (codec_param->codec.id) {
  327. case SND_AUDIOCODEC_PCM:
  328. pr_debug("Source SND_AUDIOCODEC_PCM\n");
  329. trans->source.codec_format =
  330. FORMAT_LINEAR_PCM;
  331. break;
  332. case SND_AUDIOCODEC_AC3:
  333. pr_debug("Source SND_AUDIOCODEC_AC3\n");
  334. trans->source.codec_format =
  335. FORMAT_AC3;
  336. break;
  337. case SND_AUDIOCODEC_EAC3:
  338. pr_debug("Source SND_AUDIOCODEC_EAC3\n");
  339. trans->source.codec_format =
  340. FORMAT_EAC3;
  341. break;
  342. default:
  343. pr_debug("%s: unknown source codec\n", __func__);
  344. ret = -EINVAL;
  345. goto exit;
  346. }
  347. trans->source.start = true;
  348. }
  349. pr_debug("%s: trans->source.start %d trans->sink.start %d trans->source.cstream %pK trans->sink.cstream %pK trans->session_state %d\n",
  350. __func__, trans->source.start, trans->sink.start,
  351. trans->source.cstream, trans->sink.cstream,
  352. trans->session_state);
  353. if ((trans->session_state == LOOPBACK_SESSION_READY) &&
  354. trans->source.start && trans->sink.start) {
  355. pr_debug("%s: Moving loopback session to start state\n",
  356. __func__);
  357. trans->session_state = LOOPBACK_SESSION_START;
  358. }
  359. if (trans->session_state == LOOPBACK_SESSION_START) {
  360. if (trans->audio_client != NULL) {
  361. pr_debug("%s: ASM client already opened, closing\n",
  362. __func__);
  363. stop_transcoding(trans);
  364. }
  365. trans->audio_client = q6asm_audio_client_alloc(
  366. (app_cb)loopback_event_handler, trans);
  367. if (!trans->audio_client) {
  368. pr_err("%s: Could not allocate memory\n", __func__);
  369. ret = -EINVAL;
  370. goto exit;
  371. }
  372. pr_debug("%s: ASM client allocated, callback %pK\n", __func__,
  373. loopback_event_handler);
  374. trans->session_id = trans->audio_client->session;
  375. trans->audio_client->perf_mode = false;
  376. ret = q6asm_open_transcode_loopback(trans->audio_client,
  377. bit_width,
  378. trans->source.codec_format,
  379. trans->sink.codec_format);
  380. if (ret < 0) {
  381. pr_err("%s: Session transcode loopback open failed\n",
  382. __func__);
  383. q6asm_audio_client_free(trans->audio_client);
  384. trans->audio_client = NULL;
  385. goto exit;
  386. }
  387. pr_debug("%s: Starting ADM open for loopback\n", __func__);
  388. soc_pcm_rx = trans->sink.cstream->private_data;
  389. soc_pcm_tx = trans->source.cstream->private_data;
  390. if (trans->source.codec_format != FORMAT_LINEAR_PCM)
  391. msm_pcm_routing_reg_phy_compr_stream(
  392. soc_pcm_tx->dai_link->id,
  393. trans->audio_client->perf_mode,
  394. trans->session_id,
  395. SNDRV_PCM_STREAM_CAPTURE,
  396. true);
  397. else
  398. msm_pcm_routing_reg_phy_stream(
  399. soc_pcm_tx->dai_link->id,
  400. trans->audio_client->perf_mode,
  401. trans->session_id,
  402. SNDRV_PCM_STREAM_CAPTURE);
  403. msm_pcm_routing_reg_phy_stream(
  404. soc_pcm_rx->dai_link->id,
  405. trans->audio_client->perf_mode,
  406. trans->session_id,
  407. SNDRV_PCM_STREAM_PLAYBACK);
  408. pr_debug("%s: Successfully opened ADM sessions\n", __func__);
  409. }
  410. exit:
  411. mutex_unlock(&trans->lock);
  412. return ret;
  413. }
  414. static int msm_transcode_loopback_get_caps(struct snd_compr_stream *cstream,
  415. struct snd_compr_caps *arg)
  416. {
  417. struct snd_compr_runtime *runtime;
  418. struct msm_transcode_loopback *trans;
  419. if (!arg || !cstream) {
  420. pr_err("%s: Invalid arguments\n", __func__);
  421. return -EINVAL;
  422. }
  423. runtime = cstream->runtime;
  424. trans = runtime->private_data;
  425. pr_debug("%s\n", __func__);
  426. if (cstream->direction == SND_COMPRESS_CAPTURE)
  427. memcpy(arg, &trans->source_compr_cap,
  428. sizeof(struct snd_compr_caps));
  429. else
  430. memcpy(arg, &trans->sink_compr_cap,
  431. sizeof(struct snd_compr_caps));
  432. return 0;
  433. }
  434. static int msm_transcode_stream_cmd_put(struct snd_kcontrol *kcontrol,
  435. struct snd_ctl_elem_value *ucontrol)
  436. {
  437. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  438. unsigned long fe_id = kcontrol->private_value;
  439. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  440. snd_soc_component_get_drvdata(comp);
  441. struct snd_compr_stream *cstream = NULL;
  442. struct msm_transcode_loopback *prtd;
  443. int ret = 0;
  444. struct msm_adsp_event_data *event_data = NULL;
  445. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  446. pr_err("%s Received invalid fe_id %lu\n",
  447. __func__, fe_id);
  448. ret = -EINVAL;
  449. goto done;
  450. }
  451. cstream = pdata->cstream[fe_id];
  452. if (cstream == NULL) {
  453. pr_err("%s cstream is null.\n", __func__);
  454. ret = -EINVAL;
  455. goto done;
  456. }
  457. prtd = cstream->runtime->private_data;
  458. if (!prtd) {
  459. pr_err("%s: prtd is null.\n", __func__);
  460. ret = -EINVAL;
  461. goto done;
  462. }
  463. if (prtd->audio_client == NULL) {
  464. pr_err("%s: audio_client is null.\n", __func__);
  465. ret = -EINVAL;
  466. goto done;
  467. }
  468. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  469. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  470. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  471. pr_err("%s: invalid event_type=%d",
  472. __func__, event_data->event_type);
  473. ret = -EINVAL;
  474. goto done;
  475. }
  476. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  477. - sizeof(struct msm_adsp_event_data)) {
  478. pr_err("%s param length=%d exceeds limit",
  479. __func__, event_data->payload_len);
  480. ret = -EINVAL;
  481. goto done;
  482. }
  483. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  484. if (ret < 0)
  485. pr_err("%s: failed to send stream event cmd, err = %d\n",
  486. __func__, ret);
  487. done:
  488. return ret;
  489. }
  490. static int msm_transcode_ion_fd_map_put(struct snd_kcontrol *kcontrol,
  491. struct snd_ctl_elem_value *ucontrol)
  492. {
  493. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  494. unsigned long fe_id = kcontrol->private_value;
  495. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  496. snd_soc_component_get_drvdata(comp);
  497. struct snd_compr_stream *cstream = NULL;
  498. struct msm_transcode_loopback *prtd;
  499. int fd;
  500. int ret = 0;
  501. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  502. pr_err("%s Received out of bounds invalid fe_id %lu\n",
  503. __func__, fe_id);
  504. ret = -EINVAL;
  505. goto done;
  506. }
  507. cstream = pdata->cstream[fe_id];
  508. if (cstream == NULL) {
  509. pr_err("%s cstream is null\n", __func__);
  510. ret = -EINVAL;
  511. goto done;
  512. }
  513. prtd = cstream->runtime->private_data;
  514. if (!prtd) {
  515. pr_err("%s: prtd is null\n", __func__);
  516. ret = -EINVAL;
  517. goto done;
  518. }
  519. if (prtd->audio_client == NULL) {
  520. pr_err("%s: audio_client is null\n", __func__);
  521. ret = -EINVAL;
  522. goto done;
  523. }
  524. memcpy(&fd, ucontrol->value.bytes.data, sizeof(fd));
  525. ret = q6asm_send_ion_fd(prtd->audio_client, fd);
  526. if (ret < 0)
  527. pr_err("%s: failed to register ion fd\n", __func__);
  528. done:
  529. return ret;
  530. }
  531. static int msm_transcode_rtic_event_ack_put(struct snd_kcontrol *kcontrol,
  532. struct snd_ctl_elem_value *ucontrol)
  533. {
  534. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  535. unsigned long fe_id = kcontrol->private_value;
  536. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  537. snd_soc_component_get_drvdata(comp);
  538. struct snd_compr_stream *cstream = NULL;
  539. struct msm_transcode_loopback *prtd;
  540. int ret = 0;
  541. int param_length = 0;
  542. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  543. pr_err("%s Received invalid fe_id %lu\n",
  544. __func__, fe_id);
  545. ret = -EINVAL;
  546. goto done;
  547. }
  548. cstream = pdata->cstream[fe_id];
  549. if (cstream == NULL) {
  550. pr_err("%s cstream is null\n", __func__);
  551. ret = -EINVAL;
  552. goto done;
  553. }
  554. prtd = cstream->runtime->private_data;
  555. if (!prtd) {
  556. pr_err("%s: prtd is null\n", __func__);
  557. ret = -EINVAL;
  558. goto done;
  559. }
  560. if (prtd->audio_client == NULL) {
  561. pr_err("%s: audio_client is null\n", __func__);
  562. ret = -EINVAL;
  563. goto done;
  564. }
  565. memcpy(&param_length, ucontrol->value.bytes.data,
  566. sizeof(param_length));
  567. if ((param_length + sizeof(param_length))
  568. >= sizeof(ucontrol->value.bytes.data)) {
  569. pr_err("%s param length=%d exceeds limit",
  570. __func__, param_length);
  571. ret = -EINVAL;
  572. goto done;
  573. }
  574. ret = q6asm_send_rtic_event_ack(prtd->audio_client,
  575. ucontrol->value.bytes.data + sizeof(param_length),
  576. param_length);
  577. if (ret < 0)
  578. pr_err("%s: failed to send rtic event ack, err = %d\n",
  579. __func__, ret);
  580. done:
  581. return ret;
  582. }
  583. static int msm_transcode_stream_cmd_control(
  584. struct snd_soc_pcm_runtime *rtd)
  585. {
  586. const char *mixer_ctl_name = DSP_STREAM_CMD;
  587. const char *deviceNo = "NN";
  588. char *mixer_str = NULL;
  589. int ctl_len = 0, ret = 0;
  590. struct snd_kcontrol_new fe_loopback_stream_cmd_config_control[1] = {
  591. {
  592. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  593. .name = "?",
  594. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  595. .info = msm_adsp_stream_cmd_info,
  596. .put = msm_transcode_stream_cmd_put,
  597. .private_value = 0,
  598. }
  599. };
  600. if (!rtd) {
  601. pr_err("%s NULL rtd\n", __func__);
  602. ret = -EINVAL;
  603. goto done;
  604. }
  605. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  606. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  607. if (!mixer_str) {
  608. ret = -ENOMEM;
  609. goto done;
  610. }
  611. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  612. fe_loopback_stream_cmd_config_control[0].name = mixer_str;
  613. fe_loopback_stream_cmd_config_control[0].private_value =
  614. rtd->dai_link->id;
  615. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  616. ret = snd_soc_add_platform_controls(rtd->platform,
  617. fe_loopback_stream_cmd_config_control,
  618. ARRAY_SIZE(fe_loopback_stream_cmd_config_control));
  619. if (ret < 0)
  620. pr_err("%s: failed to add ctl %s. err = %d\n",
  621. __func__, mixer_str, ret);
  622. kfree(mixer_str);
  623. done:
  624. return ret;
  625. }
  626. static int msm_transcode_stream_callback_control(
  627. struct snd_soc_pcm_runtime *rtd)
  628. {
  629. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  630. const char *deviceNo = "NN";
  631. char *mixer_str = NULL;
  632. int ctl_len = 0, ret = 0;
  633. struct snd_kcontrol *kctl;
  634. struct snd_kcontrol_new fe_loopback_callback_config_control[1] = {
  635. {
  636. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  637. .name = "?",
  638. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  639. .info = msm_adsp_stream_callback_info,
  640. .get = msm_adsp_stream_callback_get,
  641. .private_value = 0,
  642. }
  643. };
  644. if (!rtd) {
  645. pr_err("%s: rtd is NULL\n", __func__);
  646. ret = -EINVAL;
  647. goto done;
  648. }
  649. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  650. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  651. if (!mixer_str) {
  652. ret = -ENOMEM;
  653. goto done;
  654. }
  655. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  656. fe_loopback_callback_config_control[0].name = mixer_str;
  657. fe_loopback_callback_config_control[0].private_value =
  658. rtd->dai_link->id;
  659. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  660. ret = snd_soc_add_platform_controls(rtd->platform,
  661. fe_loopback_callback_config_control,
  662. ARRAY_SIZE(fe_loopback_callback_config_control));
  663. if (ret < 0) {
  664. pr_err("%s: failed to add ctl %s. err = %d\n",
  665. __func__, mixer_str, ret);
  666. ret = -EINVAL;
  667. goto free_mixer_str;
  668. }
  669. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  670. if (!kctl) {
  671. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  672. ret = -EINVAL;
  673. goto free_mixer_str;
  674. }
  675. kctl->private_data = NULL;
  676. free_mixer_str:
  677. kfree(mixer_str);
  678. done:
  679. return ret;
  680. }
  681. static int msm_transcode_add_ion_fd_cmd_control(struct snd_soc_pcm_runtime *rtd)
  682. {
  683. const char *mixer_ctl_name = "Playback ION FD";
  684. const char *deviceNo = "NN";
  685. char *mixer_str = NULL;
  686. int ctl_len = 0, ret = 0;
  687. struct snd_kcontrol_new fe_ion_fd_config_control[1] = {
  688. {
  689. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  690. .name = "?",
  691. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  692. .info = msm_adsp_stream_cmd_info,
  693. .put = msm_transcode_ion_fd_map_put,
  694. .private_value = 0,
  695. }
  696. };
  697. if (!rtd) {
  698. pr_err("%s NULL rtd\n", __func__);
  699. ret = -EINVAL;
  700. goto done;
  701. }
  702. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  703. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  704. if (!mixer_str) {
  705. ret = -ENOMEM;
  706. goto done;
  707. }
  708. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  709. fe_ion_fd_config_control[0].name = mixer_str;
  710. fe_ion_fd_config_control[0].private_value = rtd->dai_link->id;
  711. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  712. ret = snd_soc_add_platform_controls(rtd->platform,
  713. fe_ion_fd_config_control,
  714. ARRAY_SIZE(fe_ion_fd_config_control));
  715. if (ret < 0)
  716. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  717. kfree(mixer_str);
  718. done:
  719. return ret;
  720. }
  721. static int msm_transcode_add_event_ack_cmd_control(
  722. struct snd_soc_pcm_runtime *rtd)
  723. {
  724. const char *mixer_ctl_name = "Playback Event Ack";
  725. const char *deviceNo = "NN";
  726. char *mixer_str = NULL;
  727. int ctl_len = 0, ret = 0;
  728. struct snd_kcontrol_new fe_event_ack_config_control[1] = {
  729. {
  730. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  731. .name = "?",
  732. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  733. .info = msm_adsp_stream_cmd_info,
  734. .put = msm_transcode_rtic_event_ack_put,
  735. .private_value = 0,
  736. }
  737. };
  738. if (!rtd) {
  739. pr_err("%s NULL rtd\n", __func__);
  740. ret = -EINVAL;
  741. goto done;
  742. }
  743. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  744. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  745. if (!mixer_str) {
  746. ret = -ENOMEM;
  747. goto done;
  748. }
  749. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  750. fe_event_ack_config_control[0].name = mixer_str;
  751. fe_event_ack_config_control[0].private_value = rtd->dai_link->id;
  752. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  753. ret = snd_soc_add_platform_controls(rtd->platform,
  754. fe_event_ack_config_control,
  755. ARRAY_SIZE(fe_event_ack_config_control));
  756. if (ret < 0)
  757. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  758. kfree(mixer_str);
  759. done:
  760. return ret;
  761. }
  762. static int msm_transcode_loopback_new(struct snd_soc_pcm_runtime *rtd)
  763. {
  764. int rc;
  765. rc = msm_transcode_stream_cmd_control(rtd);
  766. if (rc)
  767. pr_err("%s: ADSP Stream Cmd Control open failed\n", __func__);
  768. rc = msm_transcode_stream_callback_control(rtd);
  769. if (rc)
  770. pr_err("%s: ADSP Stream callback Control open failed\n",
  771. __func__);
  772. rc = msm_transcode_add_ion_fd_cmd_control(rtd);
  773. if (rc)
  774. pr_err("%s: Could not add transcode ion fd Control\n",
  775. __func__);
  776. rc = msm_transcode_add_event_ack_cmd_control(rtd);
  777. if (rc)
  778. pr_err("%s: Could not add transcode event ack Control\n",
  779. __func__);
  780. return 0;
  781. }
  782. static struct snd_compr_ops msm_transcode_loopback_ops = {
  783. .open = msm_transcode_loopback_open,
  784. .free = msm_transcode_loopback_free,
  785. .trigger = msm_transcode_loopback_trigger,
  786. .set_params = msm_transcode_loopback_set_params,
  787. .get_caps = msm_transcode_loopback_get_caps,
  788. };
  789. static int msm_transcode_loopback_probe(struct snd_soc_platform *platform)
  790. {
  791. struct trans_loopback_pdata *pdata = NULL;
  792. pr_debug("%s\n", __func__);
  793. pdata = (struct trans_loopback_pdata *)
  794. kzalloc(sizeof(struct trans_loopback_pdata),
  795. GFP_KERNEL);
  796. if (!pdata)
  797. return -ENOMEM;
  798. snd_soc_platform_set_drvdata(platform, pdata);
  799. return 0;
  800. }
  801. static struct snd_soc_platform_driver msm_soc_platform = {
  802. .probe = msm_transcode_loopback_probe,
  803. .compr_ops = &msm_transcode_loopback_ops,
  804. .pcm_new = msm_transcode_loopback_new,
  805. };
  806. static int msm_transcode_dev_probe(struct platform_device *pdev)
  807. {
  808. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  809. if (pdev->dev.of_node)
  810. dev_set_name(&pdev->dev, "%s", "msm-transcode-loopback");
  811. return snd_soc_register_platform(&pdev->dev,
  812. &msm_soc_platform);
  813. }
  814. static int msm_transcode_remove(struct platform_device *pdev)
  815. {
  816. snd_soc_unregister_platform(&pdev->dev);
  817. return 0;
  818. }
  819. static const struct of_device_id msm_transcode_loopback_dt_match[] = {
  820. {.compatible = "qcom,msm-transcode-loopback"},
  821. {}
  822. };
  823. MODULE_DEVICE_TABLE(of, msm_transcode_loopback_dt_match);
  824. static struct platform_driver msm_transcode_loopback_driver = {
  825. .driver = {
  826. .name = "msm-transcode-loopback",
  827. .owner = THIS_MODULE,
  828. .of_match_table = msm_transcode_loopback_dt_match,
  829. },
  830. .probe = msm_transcode_dev_probe,
  831. .remove = msm_transcode_remove,
  832. };
  833. int __init msm_transcode_loopback_init(void)
  834. {
  835. memset(&transcode_info, 0, sizeof(struct msm_transcode_loopback));
  836. mutex_init(&transcode_info.lock);
  837. return platform_driver_register(&msm_transcode_loopback_driver);
  838. }
  839. void msm_transcode_loopback_exit(void)
  840. {
  841. mutex_destroy(&transcode_info.lock);
  842. platform_driver_unregister(&msm_transcode_loopback_driver);
  843. }
  844. MODULE_DESCRIPTION("Transcode loopback platform driver");
  845. MODULE_LICENSE("GPL v2");