audio_utils.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2010-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/module.h>
  5. #include <linux/fs.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/sched.h>
  9. #include <linux/wait.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/slab.h>
  12. #include <linux/atomic.h>
  13. #include <linux/compat.h>
  14. #include <asm/ioctls.h>
  15. #include "audio_utils.h"
  16. /*
  17. * Define maximum buffer size. Below values are chosen considering the higher
  18. * values used among all native drivers.
  19. */
  20. #define MAX_FRAME_SIZE 1536
  21. #define MAX_FRAMES 5
  22. #define META_SIZE (sizeof(struct meta_out_dsp))
  23. #define MAX_BUFFER_SIZE (1 + ((MAX_FRAME_SIZE + META_SIZE) * MAX_FRAMES))
  24. static int audio_in_pause(struct q6audio_in *audio)
  25. {
  26. int rc;
  27. rc = q6asm_cmd(audio->ac, CMD_PAUSE);
  28. if (rc < 0)
  29. pr_err("%s:session id %d: pause cmd failed rc=%d\n", __func__,
  30. audio->ac->session, rc);
  31. return rc;
  32. }
  33. static int audio_in_flush(struct q6audio_in *audio)
  34. {
  35. int rc;
  36. pr_debug("%s:session id %d: flush\n", __func__, audio->ac->session);
  37. /* Flush if session running */
  38. if (audio->enabled) {
  39. /* Implicitly issue a pause to the encoder before flushing */
  40. rc = audio_in_pause(audio);
  41. if (rc < 0) {
  42. pr_err("%s:session id %d: pause cmd failed rc=%d\n",
  43. __func__, audio->ac->session, rc);
  44. return rc;
  45. }
  46. rc = q6asm_cmd(audio->ac, CMD_FLUSH);
  47. if (rc < 0) {
  48. pr_err("%s:session id %d: flush cmd failed rc=%d\n",
  49. __func__, audio->ac->session, rc);
  50. return rc;
  51. }
  52. /* 2nd arg: 0 -> run immediately
  53. * 3rd arg: 0 -> msw_ts,
  54. * 4th arg: 0 ->lsw_ts
  55. */
  56. q6asm_run(audio->ac, 0x00, 0x00, 0x00);
  57. pr_debug("Rerun the session\n");
  58. }
  59. audio->rflush = 1;
  60. audio->wflush = 1;
  61. memset(audio->out_frame_info, 0, sizeof(audio->out_frame_info));
  62. wake_up(&audio->read_wait);
  63. /* get read_lock to ensure no more waiting read thread */
  64. mutex_lock(&audio->read_lock);
  65. audio->rflush = 0;
  66. mutex_unlock(&audio->read_lock);
  67. wake_up(&audio->write_wait);
  68. /* get write_lock to ensure no more waiting write thread */
  69. mutex_lock(&audio->write_lock);
  70. audio->wflush = 0;
  71. mutex_unlock(&audio->write_lock);
  72. pr_debug("%s:session id %d: in_bytes %d\n", __func__,
  73. audio->ac->session, atomic_read(&audio->in_bytes));
  74. pr_debug("%s:session id %d: in_samples %d\n", __func__,
  75. audio->ac->session, atomic_read(&audio->in_samples));
  76. atomic_set(&audio->in_bytes, 0);
  77. atomic_set(&audio->in_samples, 0);
  78. atomic_set(&audio->out_count, 0);
  79. return 0;
  80. }
  81. /* must be called with audio->lock held */
  82. int audio_in_enable(struct q6audio_in *audio)
  83. {
  84. if (audio->enabled)
  85. return 0;
  86. /* 2nd arg: 0 -> run immediately
  87. * 3rd arg: 0 -> msw_ts,
  88. * 4th arg: 0 ->lsw_ts
  89. */
  90. return q6asm_run(audio->ac, 0x00, 0x00, 0x00);
  91. }
  92. /* must be called with audio->lock held */
  93. int audio_in_disable(struct q6audio_in *audio)
  94. {
  95. int rc = 0;
  96. if (!audio->stopped) {
  97. audio->enabled = 0;
  98. audio->opened = 0;
  99. pr_debug("%s:session id %d: inbytes[%d] insamples[%d]\n",
  100. __func__, audio->ac->session,
  101. atomic_read(&audio->in_bytes),
  102. atomic_read(&audio->in_samples));
  103. rc = q6asm_cmd(audio->ac, CMD_CLOSE);
  104. if (rc < 0)
  105. pr_err("%s:session id %d: Failed to close the session rc=%d\n",
  106. __func__, audio->ac->session,
  107. rc);
  108. audio->stopped = 1;
  109. memset(audio->out_frame_info, 0,
  110. sizeof(audio->out_frame_info));
  111. wake_up(&audio->read_wait);
  112. wake_up(&audio->write_wait);
  113. }
  114. pr_debug("%s:session id %d: enabled[%d]\n", __func__,
  115. audio->ac->session, audio->enabled);
  116. return rc;
  117. }
  118. int audio_in_buf_alloc(struct q6audio_in *audio)
  119. {
  120. int rc = 0;
  121. switch (audio->buf_alloc) {
  122. case NO_BUF_ALLOC:
  123. if (audio->feedback == NON_TUNNEL_MODE) {
  124. rc = q6asm_audio_client_buf_alloc(IN,
  125. audio->ac,
  126. ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size),
  127. audio->pcm_cfg.buffer_count);
  128. if (rc < 0) {
  129. pr_err("%s:session id %d: Buffer Alloc failed\n",
  130. __func__,
  131. audio->ac->session);
  132. rc = -ENOMEM;
  133. break;
  134. }
  135. audio->buf_alloc |= BUF_ALLOC_IN;
  136. }
  137. rc = q6asm_audio_client_buf_alloc(OUT, audio->ac,
  138. ALIGN_BUF_SIZE(audio->str_cfg.buffer_size),
  139. audio->str_cfg.buffer_count);
  140. if (rc < 0) {
  141. pr_err("%s:session id %d: Buffer Alloc failed rc=%d\n",
  142. __func__, audio->ac->session, rc);
  143. rc = -ENOMEM;
  144. break;
  145. }
  146. audio->buf_alloc |= BUF_ALLOC_OUT;
  147. break;
  148. case BUF_ALLOC_IN:
  149. rc = q6asm_audio_client_buf_alloc(OUT, audio->ac,
  150. ALIGN_BUF_SIZE(audio->str_cfg.buffer_size),
  151. audio->str_cfg.buffer_count);
  152. if (rc < 0) {
  153. pr_err("%s:session id %d: Buffer Alloc failed rc=%d\n",
  154. __func__, audio->ac->session, rc);
  155. rc = -ENOMEM;
  156. break;
  157. }
  158. audio->buf_alloc |= BUF_ALLOC_OUT;
  159. break;
  160. case BUF_ALLOC_OUT:
  161. if (audio->feedback == NON_TUNNEL_MODE) {
  162. rc = q6asm_audio_client_buf_alloc(IN, audio->ac,
  163. ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size),
  164. audio->pcm_cfg.buffer_count);
  165. if (rc < 0) {
  166. pr_err("%s:session id %d: Buffer Alloc failed\n",
  167. __func__,
  168. audio->ac->session);
  169. rc = -ENOMEM;
  170. break;
  171. }
  172. audio->buf_alloc |= BUF_ALLOC_IN;
  173. }
  174. break;
  175. default:
  176. pr_debug("%s:session id %d: buf[%d]\n", __func__,
  177. audio->ac->session, audio->buf_alloc);
  178. }
  179. return rc;
  180. }
  181. int audio_in_set_config(struct file *file,
  182. struct msm_audio_config *cfg)
  183. {
  184. int rc = 0;
  185. struct q6audio_in *audio = file->private_data;
  186. if (audio->feedback != NON_TUNNEL_MODE) {
  187. pr_err("%s:session id %d: Not sufficient permission to change the record mode\n",
  188. __func__, audio->ac->session);
  189. rc = -EACCES;
  190. goto ret;
  191. }
  192. if ((cfg->buffer_count > PCM_BUF_COUNT) ||
  193. (cfg->buffer_count == 1))
  194. cfg->buffer_count = PCM_BUF_COUNT;
  195. audio->pcm_cfg.buffer_count = cfg->buffer_count;
  196. audio->pcm_cfg.buffer_size = cfg->buffer_size;
  197. audio->pcm_cfg.channel_count = cfg->channel_count;
  198. audio->pcm_cfg.sample_rate = cfg->sample_rate;
  199. if (audio->opened && audio->feedback == NON_TUNNEL_MODE) {
  200. rc = q6asm_audio_client_buf_alloc(IN, audio->ac,
  201. ALIGN_BUF_SIZE(audio->pcm_cfg.buffer_size),
  202. audio->pcm_cfg.buffer_count);
  203. if (rc < 0) {
  204. pr_err("%s:session id %d: Buffer Alloc failed\n",
  205. __func__, audio->ac->session);
  206. rc = -ENOMEM;
  207. goto ret;
  208. }
  209. }
  210. audio->buf_alloc |= BUF_ALLOC_IN;
  211. rc = 0;
  212. pr_debug("%s:session id %d: AUDIO_SET_CONFIG %d %d\n", __func__,
  213. audio->ac->session, audio->pcm_cfg.buffer_count,
  214. audio->pcm_cfg.buffer_size);
  215. ret:
  216. return rc;
  217. }
  218. /* ------------------- device --------------------- */
  219. static long audio_in_ioctl_shared(struct file *file,
  220. unsigned int cmd, unsigned long arg)
  221. {
  222. struct q6audio_in *audio = file->private_data;
  223. int rc = 0;
  224. switch (cmd) {
  225. case AUDIO_FLUSH: {
  226. /* Make sure we're stopped and we wake any threads
  227. * that might be blocked holding the read_lock.
  228. * While audio->stopped read threads will always
  229. * exit immediately.
  230. */
  231. rc = audio_in_flush(audio);
  232. if (rc < 0)
  233. pr_err("%s:session id %d: Flush Fail rc=%d\n",
  234. __func__, audio->ac->session, rc);
  235. else { /* Register back the flushed read buffer with DSP */
  236. int cnt = 0;
  237. while (cnt++ < audio->str_cfg.buffer_count)
  238. q6asm_read(audio->ac); /* Push buffer to DSP */
  239. pr_debug("register the read buffer\n");
  240. }
  241. break;
  242. }
  243. case AUDIO_PAUSE: {
  244. pr_debug("%s:session id %d: AUDIO_PAUSE\n", __func__,
  245. audio->ac->session);
  246. if (audio->enabled)
  247. audio_in_pause(audio);
  248. break;
  249. }
  250. case AUDIO_GET_SESSION_ID: {
  251. if (copy_to_user((void *) arg, &audio->ac->session,
  252. sizeof(u16))) {
  253. pr_err("%s: copy_to_user for AUDIO_GET_SESSION_ID failed\n",
  254. __func__);
  255. rc = -EFAULT;
  256. }
  257. break;
  258. }
  259. default:
  260. pr_err("%s: Unknown ioctl cmd = %d", __func__, cmd);
  261. rc = -EINVAL;
  262. }
  263. return rc;
  264. }
  265. long audio_in_ioctl(struct file *file,
  266. unsigned int cmd, unsigned long arg)
  267. {
  268. struct q6audio_in *audio = file->private_data;
  269. int rc = 0;
  270. if (cmd == AUDIO_GET_STATS) {
  271. struct msm_audio_stats stats;
  272. memset(&stats, 0, sizeof(stats));
  273. stats.byte_count = atomic_read(&audio->in_bytes);
  274. stats.sample_count = atomic_read(&audio->in_samples);
  275. if (copy_to_user((void *) arg, &stats, sizeof(stats)))
  276. return -EFAULT;
  277. return rc;
  278. }
  279. mutex_lock(&audio->lock);
  280. switch (cmd) {
  281. case AUDIO_FLUSH:
  282. case AUDIO_PAUSE:
  283. case AUDIO_GET_SESSION_ID:
  284. rc = audio_in_ioctl_shared(file, cmd, arg);
  285. break;
  286. case AUDIO_GET_STREAM_CONFIG: {
  287. struct msm_audio_stream_config cfg;
  288. memset(&cfg, 0, sizeof(cfg));
  289. cfg.buffer_size = audio->str_cfg.buffer_size;
  290. cfg.buffer_count = audio->str_cfg.buffer_count;
  291. if (copy_to_user((void *)arg, &cfg, sizeof(cfg)))
  292. rc = -EFAULT;
  293. pr_debug("%s:session id %d: AUDIO_GET_STREAM_CONFIG %d %d\n",
  294. __func__, audio->ac->session, cfg.buffer_size,
  295. cfg.buffer_count);
  296. break;
  297. }
  298. case AUDIO_SET_STREAM_CONFIG: {
  299. struct msm_audio_stream_config cfg;
  300. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  301. pr_err("%s: copy_from_user for AUDIO_SET_STREAM_CONFIG failed\n"
  302. , __func__);
  303. rc = -EFAULT;
  304. break;
  305. }
  306. /* Minimum single frame size,
  307. * but with in maximum frames number
  308. */
  309. if ((cfg.buffer_size < (audio->min_frame_size +
  310. sizeof(struct meta_out_dsp))) ||
  311. (cfg.buffer_count < FRAME_NUM)) {
  312. rc = -EINVAL;
  313. break;
  314. }
  315. if (cfg.buffer_size > MAX_BUFFER_SIZE) {
  316. rc = -EINVAL;
  317. break;
  318. }
  319. audio->str_cfg.buffer_size = cfg.buffer_size;
  320. audio->str_cfg.buffer_count = cfg.buffer_count;
  321. if (audio->opened) {
  322. rc = q6asm_audio_client_buf_alloc(OUT, audio->ac,
  323. ALIGN_BUF_SIZE(audio->str_cfg.buffer_size),
  324. audio->str_cfg.buffer_count);
  325. if (rc < 0) {
  326. pr_err("%s: session id %d: Buffer Alloc failed rc=%d\n",
  327. __func__, audio->ac->session, rc);
  328. rc = -ENOMEM;
  329. break;
  330. }
  331. }
  332. audio->buf_alloc |= BUF_ALLOC_OUT;
  333. rc = 0;
  334. pr_debug("%s:session id %d: AUDIO_SET_STREAM_CONFIG %d %d\n",
  335. __func__, audio->ac->session,
  336. audio->str_cfg.buffer_size,
  337. audio->str_cfg.buffer_count);
  338. break;
  339. }
  340. case AUDIO_SET_BUF_CFG: {
  341. struct msm_audio_buf_cfg cfg;
  342. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  343. rc = -EFAULT;
  344. break;
  345. }
  346. if ((audio->feedback == NON_TUNNEL_MODE) &&
  347. !cfg.meta_info_enable) {
  348. rc = -EFAULT;
  349. break;
  350. }
  351. /* Restrict the num of frames per buf to coincide with
  352. * default buf size
  353. */
  354. if (cfg.frames_per_buf > audio->max_frames_per_buf) {
  355. rc = -EFAULT;
  356. break;
  357. }
  358. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  359. audio->buf_cfg.frames_per_buf = cfg.frames_per_buf;
  360. pr_debug("%s:session id %d: Set-buf-cfg: meta[%d] framesperbuf[%d]\n",
  361. __func__,
  362. audio->ac->session, cfg.meta_info_enable,
  363. cfg.frames_per_buf);
  364. break;
  365. }
  366. case AUDIO_GET_BUF_CFG: {
  367. pr_debug("%s:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  368. __func__,
  369. audio->ac->session, audio->buf_cfg.meta_info_enable,
  370. audio->buf_cfg.frames_per_buf);
  371. if (copy_to_user((void *)arg, &audio->buf_cfg,
  372. sizeof(struct msm_audio_buf_cfg)))
  373. rc = -EFAULT;
  374. break;
  375. }
  376. case AUDIO_GET_CONFIG: {
  377. if (copy_to_user((void *)arg, &audio->pcm_cfg,
  378. sizeof(struct msm_audio_config)))
  379. rc = -EFAULT;
  380. break;
  381. }
  382. case AUDIO_SET_CONFIG: {
  383. struct msm_audio_config cfg;
  384. if (copy_from_user(&cfg, (void *)arg, sizeof(cfg))) {
  385. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG failed\n",
  386. __func__);
  387. rc = -EFAULT;
  388. break;
  389. }
  390. rc = audio_in_set_config(file, &cfg);
  391. break;
  392. }
  393. default:
  394. /* call codec specific ioctl */
  395. rc = audio->enc_ioctl(file, cmd, arg);
  396. }
  397. mutex_unlock(&audio->lock);
  398. return rc;
  399. }
  400. #ifdef CONFIG_COMPAT
  401. struct msm_audio_stats32 {
  402. u32 byte_count;
  403. u32 sample_count;
  404. u32 unused[2];
  405. };
  406. struct msm_audio_stream_config32 {
  407. u32 buffer_size;
  408. u32 buffer_count;
  409. };
  410. struct msm_audio_config32 {
  411. u32 buffer_size;
  412. u32 buffer_count;
  413. u32 channel_count;
  414. u32 sample_rate;
  415. u32 type;
  416. u32 meta_field;
  417. u32 bits;
  418. u32 unused[3];
  419. };
  420. struct msm_audio_buf_cfg32 {
  421. u32 meta_info_enable;
  422. u32 frames_per_buf;
  423. };
  424. enum {
  425. AUDIO_GET_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 3,
  426. struct msm_audio_config32),
  427. AUDIO_SET_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 4,
  428. struct msm_audio_config32),
  429. AUDIO_GET_STATS_32 = _IOR(AUDIO_IOCTL_MAGIC, 5,
  430. struct msm_audio_stats32),
  431. AUDIO_SET_STREAM_CONFIG_32 = _IOW(AUDIO_IOCTL_MAGIC, 80,
  432. struct msm_audio_stream_config32),
  433. AUDIO_GET_STREAM_CONFIG_32 = _IOR(AUDIO_IOCTL_MAGIC, 81,
  434. struct msm_audio_stream_config32),
  435. AUDIO_SET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 94,
  436. struct msm_audio_buf_cfg32),
  437. AUDIO_GET_BUF_CFG_32 = _IOW(AUDIO_IOCTL_MAGIC, 93,
  438. struct msm_audio_buf_cfg32),
  439. };
  440. long audio_in_compat_ioctl(struct file *file,
  441. unsigned int cmd, unsigned long arg)
  442. {
  443. struct q6audio_in *audio = file->private_data;
  444. int rc = 0;
  445. if (cmd == AUDIO_GET_STATS_32) {
  446. struct msm_audio_stats32 stats_32;
  447. memset(&stats_32, 0, sizeof(stats_32));
  448. stats_32.byte_count = atomic_read(&audio->in_bytes);
  449. stats_32.sample_count = atomic_read(&audio->in_samples);
  450. if (copy_to_user((void *) arg, &stats_32, sizeof(stats_32))) {
  451. pr_err("%s: copy_to_user failed for AUDIO_GET_STATS_32\n",
  452. __func__);
  453. return -EFAULT;
  454. }
  455. return rc;
  456. }
  457. mutex_lock(&audio->lock);
  458. switch (cmd) {
  459. case AUDIO_FLUSH:
  460. case AUDIO_PAUSE:
  461. case AUDIO_GET_SESSION_ID:
  462. rc = audio_in_ioctl_shared(file, cmd, arg);
  463. break;
  464. case AUDIO_GET_STREAM_CONFIG_32: {
  465. struct msm_audio_stream_config32 cfg_32;
  466. memset(&cfg_32, 0, sizeof(cfg_32));
  467. cfg_32.buffer_size = audio->str_cfg.buffer_size;
  468. cfg_32.buffer_count = audio->str_cfg.buffer_count;
  469. if (copy_to_user((void *)arg, &cfg_32, sizeof(cfg_32))) {
  470. pr_err("%s: Copy to user failed\n", __func__);
  471. rc = -EFAULT;
  472. }
  473. pr_debug("%s:session id %d: AUDIO_GET_STREAM_CONFIG %d %d\n",
  474. __func__, audio->ac->session,
  475. cfg_32.buffer_size,
  476. cfg_32.buffer_count);
  477. break;
  478. }
  479. case AUDIO_SET_STREAM_CONFIG_32: {
  480. struct msm_audio_stream_config32 cfg_32;
  481. struct msm_audio_stream_config cfg;
  482. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  483. pr_err("%s: copy_from_user for AUDIO_SET_STREAM_CONFIG_32 failed\n",
  484. __func__);
  485. rc = -EFAULT;
  486. break;
  487. }
  488. cfg.buffer_size = cfg_32.buffer_size;
  489. cfg.buffer_count = cfg_32.buffer_count;
  490. /* Minimum single frame size,
  491. * but with in maximum frames number
  492. */
  493. if ((cfg.buffer_size < (audio->min_frame_size +
  494. sizeof(struct meta_out_dsp))) ||
  495. (cfg.buffer_count < FRAME_NUM)) {
  496. rc = -EINVAL;
  497. break;
  498. }
  499. audio->str_cfg.buffer_size = cfg.buffer_size;
  500. audio->str_cfg.buffer_count = cfg.buffer_count;
  501. if (audio->opened) {
  502. rc = q6asm_audio_client_buf_alloc(OUT, audio->ac,
  503. ALIGN_BUF_SIZE(audio->str_cfg.buffer_size),
  504. audio->str_cfg.buffer_count);
  505. if (rc < 0) {
  506. pr_err("%s: session id %d:\n",
  507. __func__, audio->ac->session);
  508. pr_err("Buffer Alloc failed rc=%d\n", rc);
  509. rc = -ENOMEM;
  510. break;
  511. }
  512. }
  513. audio->buf_alloc |= BUF_ALLOC_OUT;
  514. pr_debug("%s:session id %d: AUDIO_SET_STREAM_CONFIG %d %d\n",
  515. __func__, audio->ac->session,
  516. audio->str_cfg.buffer_size,
  517. audio->str_cfg.buffer_count);
  518. break;
  519. }
  520. case AUDIO_SET_BUF_CFG_32: {
  521. struct msm_audio_buf_cfg32 cfg_32;
  522. struct msm_audio_buf_cfg cfg;
  523. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  524. pr_err("%s: copy_from_user for AUDIO_SET_BUG_CFG_32 failed",
  525. __func__);
  526. rc = -EFAULT;
  527. break;
  528. }
  529. cfg.meta_info_enable = cfg_32.meta_info_enable;
  530. cfg.frames_per_buf = cfg_32.frames_per_buf;
  531. if ((audio->feedback == NON_TUNNEL_MODE) &&
  532. !cfg.meta_info_enable) {
  533. rc = -EFAULT;
  534. break;
  535. }
  536. /* Restrict the num of frames per buf to coincide with
  537. * default buf size
  538. */
  539. if (cfg.frames_per_buf > audio->max_frames_per_buf) {
  540. rc = -EFAULT;
  541. break;
  542. }
  543. audio->buf_cfg.meta_info_enable = cfg.meta_info_enable;
  544. audio->buf_cfg.frames_per_buf = cfg.frames_per_buf;
  545. pr_debug("%s:session id %d: Set-buf-cfg: meta[%d] framesperbuf[%d]\n",
  546. __func__, audio->ac->session, cfg.meta_info_enable,
  547. cfg.frames_per_buf);
  548. break;
  549. }
  550. case AUDIO_GET_BUF_CFG_32: {
  551. struct msm_audio_buf_cfg32 cfg_32;
  552. pr_debug("%s:session id %d: Get-buf-cfg: meta[%d] framesperbuf[%d]\n",
  553. __func__,
  554. audio->ac->session, audio->buf_cfg.meta_info_enable,
  555. audio->buf_cfg.frames_per_buf);
  556. cfg_32.meta_info_enable = audio->buf_cfg.meta_info_enable;
  557. cfg_32.frames_per_buf = audio->buf_cfg.frames_per_buf;
  558. if (copy_to_user((void *)arg, &cfg_32,
  559. sizeof(struct msm_audio_buf_cfg32))) {
  560. pr_err("%s: Copy to user failed\n", __func__);
  561. rc = -EFAULT;
  562. }
  563. break;
  564. }
  565. case AUDIO_GET_CONFIG_32: {
  566. struct msm_audio_config32 cfg_32;
  567. memset(&cfg_32, 0, sizeof(cfg_32));
  568. cfg_32.buffer_size = audio->pcm_cfg.buffer_size;
  569. cfg_32.buffer_count = audio->pcm_cfg.buffer_count;
  570. cfg_32.channel_count = audio->pcm_cfg.channel_count;
  571. cfg_32.sample_rate = audio->pcm_cfg.sample_rate;
  572. cfg_32.type = audio->pcm_cfg.type;
  573. cfg_32.meta_field = audio->pcm_cfg.meta_field;
  574. cfg_32.bits = audio->pcm_cfg.bits;
  575. if (copy_to_user((void *)arg, &cfg_32,
  576. sizeof(struct msm_audio_config32))) {
  577. pr_err("%s: Copy to user failed\n", __func__);
  578. rc = -EFAULT;
  579. }
  580. break;
  581. }
  582. case AUDIO_SET_CONFIG_32: {
  583. struct msm_audio_config32 cfg_32;
  584. struct msm_audio_config cfg;
  585. if (copy_from_user(&cfg_32, (void *)arg, sizeof(cfg_32))) {
  586. pr_err("%s: copy_from_user for AUDIO_SET_CONFIG_32 failed\n",
  587. __func__);
  588. rc = -EFAULT;
  589. break;
  590. }
  591. cfg.buffer_size = cfg_32.buffer_size;
  592. cfg.buffer_count = cfg_32.buffer_count;
  593. cfg.channel_count = cfg_32.channel_count;
  594. cfg.sample_rate = cfg_32.sample_rate;
  595. cfg.type = cfg_32.type;
  596. cfg.meta_field = cfg_32.meta_field;
  597. cfg.bits = cfg_32.bits;
  598. rc = audio_in_set_config(file, &cfg);
  599. break;
  600. }
  601. default:
  602. /* call codec specific ioctl */
  603. rc = audio->enc_compat_ioctl(file, cmd, arg);
  604. }
  605. mutex_unlock(&audio->lock);
  606. return rc;
  607. }
  608. #endif
  609. ssize_t audio_in_read(struct file *file,
  610. char __user *buf,
  611. size_t count, loff_t *pos)
  612. {
  613. struct q6audio_in *audio = file->private_data;
  614. const char __user *start = buf;
  615. unsigned char *data;
  616. uint32_t offset = 0;
  617. uint32_t size = 0;
  618. int rc = 0;
  619. uint32_t idx;
  620. struct meta_out_dsp meta;
  621. uint32_t bytes_to_copy = 0;
  622. uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 :
  623. (sizeof(unsigned char) +
  624. (sizeof(struct meta_out_dsp)*(audio->buf_cfg.frames_per_buf)));
  625. memset(&meta, 0, sizeof(meta));
  626. pr_debug("%s:session id %d: read - %zd\n", __func__, audio->ac->session,
  627. count);
  628. if (audio->reset_event)
  629. return -ENETRESET;
  630. if (!audio->enabled)
  631. return -EFAULT;
  632. mutex_lock(&audio->read_lock);
  633. while (count > 0) {
  634. rc = wait_event_interruptible(
  635. audio->read_wait,
  636. ((atomic_read(&audio->out_count) > 0) ||
  637. (audio->stopped) ||
  638. audio->rflush || audio->eos_rsp ||
  639. audio->event_abort));
  640. if (audio->event_abort) {
  641. rc = -EIO;
  642. break;
  643. }
  644. if (rc < 0)
  645. break;
  646. if ((audio->stopped && !(atomic_read(&audio->out_count))) ||
  647. audio->rflush) {
  648. pr_debug("%s:session id %d: driver in stop state or flush,No more buf to read",
  649. __func__,
  650. audio->ac->session);
  651. rc = 0;/* End of File */
  652. break;
  653. }
  654. if (!(atomic_read(&audio->out_count)) &&
  655. (audio->eos_rsp == 1) &&
  656. (count >= (sizeof(unsigned char) +
  657. sizeof(struct meta_out_dsp)))) {
  658. unsigned char num_of_frames;
  659. pr_info("%s:session id %d: eos %d at output\n",
  660. __func__, audio->ac->session, audio->eos_rsp);
  661. if (buf != start)
  662. break;
  663. num_of_frames = 0xFF;
  664. if (copy_to_user(buf, &num_of_frames,
  665. sizeof(unsigned char))) {
  666. rc = -EFAULT;
  667. break;
  668. }
  669. buf += sizeof(unsigned char);
  670. meta.frame_size = 0xFFFF;
  671. meta.encoded_pcm_samples = 0xFFFF;
  672. meta.msw_ts = 0x00;
  673. meta.lsw_ts = 0x00;
  674. meta.nflags = AUD_EOS_SET;
  675. audio->eos_rsp = 0;
  676. if (copy_to_user(buf, &meta, sizeof(meta))) {
  677. rc = -EFAULT;
  678. break;
  679. }
  680. buf += sizeof(meta);
  681. break;
  682. }
  683. data = (unsigned char *)q6asm_is_cpu_buf_avail(OUT, audio->ac,
  684. &size, &idx);
  685. if ((count >= (size + mfield_size)) && data) {
  686. if (audio->buf_cfg.meta_info_enable) {
  687. if (copy_to_user(buf,
  688. &audio->out_frame_info[idx][0],
  689. sizeof(unsigned char))) {
  690. rc = -EFAULT;
  691. break;
  692. }
  693. bytes_to_copy =
  694. (size + audio->out_frame_info[idx][1]);
  695. if (bytes_to_copy == 0) {
  696. rc = 0;
  697. break;
  698. }
  699. /* Number of frames information copied */
  700. buf += sizeof(unsigned char);
  701. count -= sizeof(unsigned char);
  702. } else {
  703. offset = audio->out_frame_info[idx][1];
  704. bytes_to_copy = size;
  705. }
  706. pr_debug("%s:session id %d: offset=%d nr of frames= %d\n",
  707. __func__, audio->ac->session,
  708. audio->out_frame_info[idx][1],
  709. audio->out_frame_info[idx][0]);
  710. if (copy_to_user(buf, &data[offset], bytes_to_copy)) {
  711. rc = -EFAULT;
  712. break;
  713. }
  714. count -= bytes_to_copy;
  715. buf += bytes_to_copy;
  716. } else {
  717. pr_err("%s:session id %d: short read data[%pK] bytesavail[%d]bytesrequest[%zd]\n",
  718. __func__,
  719. audio->ac->session,
  720. data, size, count);
  721. }
  722. atomic_dec(&audio->out_count);
  723. q6asm_read(audio->ac);
  724. break;
  725. }
  726. mutex_unlock(&audio->read_lock);
  727. pr_debug("%s:session id %d: read: %zd bytes\n", __func__,
  728. audio->ac->session, (buf-start));
  729. if (!rc) {
  730. if (buf > start)
  731. return buf - start;
  732. }
  733. return rc;
  734. }
  735. static int extract_meta_info(char *buf, unsigned long *msw_ts,
  736. unsigned long *lsw_ts, unsigned int *flags)
  737. {
  738. struct meta_in *meta = (struct meta_in *)buf;
  739. *msw_ts = meta->ntimestamp.highpart;
  740. *lsw_ts = meta->ntimestamp.lowpart;
  741. *flags = meta->nflags;
  742. return 0;
  743. }
  744. ssize_t audio_in_write(struct file *file,
  745. const char __user *buf,
  746. size_t count, loff_t *pos)
  747. {
  748. struct q6audio_in *audio = file->private_data;
  749. const char __user *start = buf;
  750. size_t xfer = 0;
  751. char *cpy_ptr;
  752. int rc = 0;
  753. unsigned char *data;
  754. uint32_t size = 0;
  755. uint32_t idx = 0;
  756. uint32_t nflags = 0;
  757. unsigned long msw_ts = 0;
  758. unsigned long lsw_ts = 0;
  759. uint32_t mfield_size = (audio->buf_cfg.meta_info_enable == 0) ? 0 :
  760. sizeof(struct meta_in);
  761. pr_debug("%s:session id %d: to write[%zd]\n", __func__,
  762. audio->ac->session, count);
  763. if (audio->reset_event)
  764. return -ENETRESET;
  765. if (!audio->enabled)
  766. return -EFAULT;
  767. mutex_lock(&audio->write_lock);
  768. while (count > 0) {
  769. rc = wait_event_interruptible(audio->write_wait,
  770. ((atomic_read(&audio->in_count) > 0) ||
  771. (audio->stopped) ||
  772. (audio->wflush) || (audio->event_abort)));
  773. if (audio->event_abort) {
  774. rc = -EIO;
  775. break;
  776. }
  777. if (rc < 0)
  778. break;
  779. if (audio->stopped || audio->wflush) {
  780. pr_debug("%s: session id %d: stop or flush\n", __func__,
  781. audio->ac->session);
  782. rc = -EBUSY;
  783. break;
  784. }
  785. /* if no PCM data, might have only eos buffer
  786. * such case do not hold cpu buffer
  787. */
  788. if ((buf == start) && (count == mfield_size)) {
  789. char eos_buf[sizeof(struct meta_in)];
  790. /* Processing beginning of user buffer */
  791. if (copy_from_user(eos_buf, buf, mfield_size)) {
  792. rc = -EFAULT;
  793. break;
  794. }
  795. /* Check if EOS flag is set and buffer has
  796. * contains just meta field
  797. */
  798. extract_meta_info(eos_buf, &msw_ts, &lsw_ts,
  799. &nflags);
  800. buf += mfield_size;
  801. /* send the EOS and return */
  802. pr_debug("%s:session id %d: send EOS 0x%8x\n",
  803. __func__,
  804. audio->ac->session, nflags);
  805. break;
  806. }
  807. data = (unsigned char *)q6asm_is_cpu_buf_avail(IN, audio->ac,
  808. &size, &idx);
  809. if (!data) {
  810. pr_debug("%s:session id %d: No buf available\n",
  811. __func__, audio->ac->session);
  812. continue;
  813. }
  814. cpy_ptr = data;
  815. if (audio->buf_cfg.meta_info_enable) {
  816. if (buf == start) {
  817. /* Processing beginning of user buffer */
  818. if (copy_from_user(cpy_ptr, buf, mfield_size)) {
  819. rc = -EFAULT;
  820. break;
  821. }
  822. /* Check if EOS flag is set and buffer has
  823. * contains just meta field
  824. */
  825. extract_meta_info(cpy_ptr, &msw_ts, &lsw_ts,
  826. &nflags);
  827. buf += mfield_size;
  828. count -= mfield_size;
  829. } else {
  830. pr_debug("%s:session id %d: continuous buffer\n",
  831. __func__, audio->ac->session);
  832. }
  833. }
  834. xfer = (count > size) ? size : count;
  835. if (copy_from_user(cpy_ptr, buf, xfer)) {
  836. rc = -EFAULT;
  837. break;
  838. }
  839. rc = q6asm_write(audio->ac, xfer, msw_ts, lsw_ts, 0x00);
  840. if (rc < 0) {
  841. rc = -EFAULT;
  842. break;
  843. }
  844. atomic_dec(&audio->in_count);
  845. count -= xfer;
  846. buf += xfer;
  847. }
  848. mutex_unlock(&audio->write_lock);
  849. pr_debug("%s:session id %d: eos_condition 0x%x buf[0x%pK] start[0x%pK]\n",
  850. __func__, audio->ac->session,
  851. nflags, buf, start);
  852. if (nflags & AUD_EOS_SET) {
  853. rc = q6asm_cmd(audio->ac, CMD_EOS);
  854. pr_info("%s:session id %d: eos %d at input\n", __func__,
  855. audio->ac->session, audio->eos_rsp);
  856. }
  857. pr_debug("%s:session id %d: Written %zd Avail Buf[%d]", __func__,
  858. audio->ac->session, (buf - start - mfield_size),
  859. atomic_read(&audio->in_count));
  860. if (!rc) {
  861. if (buf > start)
  862. return buf - start;
  863. }
  864. return rc;
  865. }
  866. int audio_in_release(struct inode *inode, struct file *file)
  867. {
  868. struct q6audio_in *audio = file->private_data;
  869. pr_info("%s: session id %d\n", __func__, audio->ac->session);
  870. mutex_lock(&audio->lock);
  871. audio_in_disable(audio);
  872. q6asm_audio_client_free(audio->ac);
  873. mutex_unlock(&audio->lock);
  874. kfree(audio->enc_cfg);
  875. kfree(audio->codec_cfg);
  876. kfree(audio);
  877. return 0;
  878. }