/arch/arm/mach-fsm/qdsp5v2/audio_amrwb.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase · C · 1709 lines · 1425 code · 203 blank · 81 comment · 218 complexity · 6fae4983730556b2ee2a2f4bf382f33d MD5 · raw file

  1. /* amrwb audio decoder device
  2. *
  3. * Copyright (c) 2008-2010, Code Aurora Forum. All rights reserved.
  4. *
  5. * Based on the mp3 native driver in arch/arm/mach-msm/qdsp5v2/audio_mp3.c
  6. *
  7. * Copyright (C) 2008 Google, Inc.
  8. * Copyright (C) 2008 HTC Corporation
  9. *
  10. * All source code in this file is licensed under the following license except
  11. * where indicated.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2 as published
  15. * by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * See the GNU General Public License for more details.
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, you can find it at http://www.fsf.org
  24. */
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/kthread.h>
  30. #include <linux/wait.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/delay.h>
  34. #include <linux/list.h>
  35. #include <linux/earlysuspend.h>
  36. #include <linux/android_pmem.h>
  37. #include <asm/atomic.h>
  38. #include <asm/ioctls.h>
  39. #include <mach/msm_adsp.h>
  40. #include <linux/msm_audio.h>
  41. #include <mach/qdsp5v2/qdsp5audppmsg.h>
  42. #include <mach/qdsp5v2/qdsp5audplaycmdi.h>
  43. #include <mach/qdsp5v2/qdsp5audplaymsg.h>
  44. #include <mach/qdsp5v2/audio_dev_ctl.h>
  45. #include <mach/qdsp5v2/audpp.h>
  46. #include <mach/debug_mm.h>
  47. #define BUFSZ 4110 /* Hold minimum 700ms voice data and 14 bytes of meta in*/
  48. #define DMASZ (BUFSZ * 2)
  49. #define AUDPLAY_INVALID_READ_PTR_OFFSET 0xFFFF
  50. #define AUDDEC_DEC_AMRWB 11
  51. #define PCM_BUFSZ_MIN 8216 /* 100ms worth of data and 24 bytes of meta out*/
  52. #define PCM_BUF_MAX_COUNT 5 /* DSP only accepts 5 buffers at most
  53. but support 2 buffers currently */
  54. #define ROUTING_MODE_FTRT 1
  55. #define ROUTING_MODE_RT 2
  56. /* Decoder status received from AUDPPTASK */
  57. #define AUDPP_DEC_STATUS_SLEEP 0
  58. #define AUDPP_DEC_STATUS_INIT 1
  59. #define AUDPP_DEC_STATUS_CFG 2
  60. #define AUDPP_DEC_STATUS_PLAY 3
  61. #define AUDAMRWB_METAFIELD_MASK 0xFFFF0000
  62. #define AUDAMRWB_EOS_FLG_OFFSET 0x0A /* Offset from beginning of buffer */
  63. #define AUDAMRWB_EOS_FLG_MASK 0x01
  64. #define AUDAMRWB_EOS_NONE 0x0 /* No EOS detected */
  65. #define AUDAMRWB_EOS_SET 0x1 /* EOS set in meta field */
  66. #define AUDAMRWB_EVENT_NUM 10 /* Default number of pre-allocated event pkts */
  67. struct buffer {
  68. void *data;
  69. unsigned size;
  70. unsigned used; /* Input usage actual DSP produced PCM size */
  71. unsigned addr;
  72. unsigned short mfield_sz; /*only useful for data has meta field */
  73. };
  74. #ifdef CONFIG_HAS_EARLYSUSPEND
  75. struct audamrwb_suspend_ctl {
  76. struct early_suspend node;
  77. struct audio *audio;
  78. };
  79. #endif
  80. struct audamrwb_event{
  81. struct list_head list;
  82. int event_type;
  83. union msm_audio_event_payload payload;
  84. };
  85. struct audio {
  86. struct buffer out[2];
  87. spinlock_t dsp_lock;
  88. uint8_t out_head;
  89. uint8_t out_tail;
  90. uint8_t out_needed; /* number of buffers the dsp is waiting for */
  91. atomic_t out_bytes;
  92. struct mutex lock;
  93. struct mutex write_lock;
  94. wait_queue_head_t write_wait;
  95. /* Host PCM section */
  96. struct buffer in[PCM_BUF_MAX_COUNT];
  97. struct mutex read_lock;
  98. wait_queue_head_t read_wait; /* Wait queue for read */
  99. char *read_data; /* pointer to reader buffer */
  100. int32_t read_phys; /* physical address of reader buffer */
  101. uint8_t read_next; /* index to input buffers to be read next */
  102. uint8_t fill_next; /* index to buffer that DSP should be filling */
  103. uint8_t pcm_buf_count; /* number of pcm buffer allocated */
  104. /* ---- End of Host PCM section */
  105. struct msm_adsp_module *audplay;
  106. /* configuration to use on next enable */
  107. uint32_t out_sample_rate;
  108. uint32_t out_channel_mode;
  109. /* data allocated for various buffers */
  110. char *data;
  111. int32_t phys; /* physical address of write buffer */
  112. int mfield; /* meta field embedded in data */
  113. int rflush; /* Read flush */
  114. int wflush; /* Write flush */
  115. int opened;
  116. int enabled;
  117. int running;
  118. int stopped; /* set when stopped, cleared on flush */
  119. int pcm_feedback;
  120. int buf_refresh;
  121. int teos; /* valid only if tunnel mode & no data left for decoder */
  122. enum msm_aud_decoder_state dec_state; /* Represents decoder state */
  123. int reserved; /* A byte is being reserved */
  124. char rsv_byte; /* Handle odd length user data */
  125. const char *module_name;
  126. unsigned queue_id;
  127. uint16_t dec_id;
  128. uint32_t read_ptr_offset;
  129. int16_t source;
  130. #ifdef CONFIG_HAS_EARLYSUSPEND
  131. struct audamrwb_suspend_ctl suspend_ctl;
  132. #endif
  133. #ifdef CONFIG_DEBUG_FS
  134. struct dentry *dentry;
  135. #endif
  136. wait_queue_head_t wait;
  137. struct list_head free_event_queue;
  138. struct list_head event_queue;
  139. wait_queue_head_t event_wait;
  140. spinlock_t event_queue_lock;
  141. struct mutex get_event_lock;
  142. int event_abort;
  143. /* AV sync Info */
  144. int avsync_flag; /* Flag to indicate feedback from DSP */
  145. wait_queue_head_t avsync_wait;/* Wait queue for AV Sync Message */
  146. /* flags, 48 bits sample/bytes counter per channel */
  147. uint16_t avsync[AUDPP_AVSYNC_CH_COUNT * AUDPP_AVSYNC_NUM_WORDS + 1];
  148. uint32_t device_events;
  149. int eq_enable;
  150. int eq_needs_commit;
  151. struct audpp_cmd_cfg_object_params_eqalizer eq;
  152. struct audpp_cmd_cfg_object_params_volume vol_pan;
  153. };
  154. static int auddec_dsp_config(struct audio *audio, int enable);
  155. static void audpp_cmd_cfg_adec_params(struct audio *audio);
  156. static void audpp_cmd_cfg_routing_mode(struct audio *audio);
  157. static void audamrwb_send_data(struct audio *audio, unsigned needed);
  158. static void audamrwb_config_hostpcm(struct audio *audio);
  159. static void audamrwb_buffer_refresh(struct audio *audio);
  160. static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg);
  161. static void audamrwb_post_event(struct audio *audio, int type,
  162. union msm_audio_event_payload payload);
  163. /* must be called with audio->lock held */
  164. static int audamrwb_enable(struct audio *audio)
  165. {
  166. MM_DBG("\n"); /* Macro prints the file name and function */
  167. if (audio->enabled)
  168. return 0;
  169. audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
  170. audio->out_tail = 0;
  171. audio->out_needed = 0;
  172. if (msm_adsp_enable(audio->audplay)) {
  173. MM_ERR("msm_adsp_enable(audplay) failed\n");
  174. return -ENODEV;
  175. }
  176. if (audpp_enable(audio->dec_id, audamrwb_dsp_event, audio)) {
  177. MM_ERR("audpp_enable() failed\n");
  178. msm_adsp_disable(audio->audplay);
  179. return -ENODEV;
  180. }
  181. audio->enabled = 1;
  182. return 0;
  183. }
  184. static void amrwb_listner(u32 evt_id, union auddev_evt_data *evt_payload,
  185. void *private_data)
  186. {
  187. struct audio *audio = (struct audio *) private_data;
  188. switch (evt_id) {
  189. case AUDDEV_EVT_DEV_RDY:
  190. MM_DBG("AUDDEV_EVT_DEV_RDY\n");
  191. audio->source |= (0x1 << evt_payload->routing_id);
  192. if (audio->running == 1 && audio->enabled == 1)
  193. audpp_route_stream(audio->dec_id, audio->source);
  194. break;
  195. case AUDDEV_EVT_DEV_RLS:
  196. MM_DBG("AUDDEV_EVT_DEV_RLS\n");
  197. audio->source &= ~(0x1 << evt_payload->routing_id);
  198. if (audio->running == 1 && audio->enabled == 1)
  199. audpp_route_stream(audio->dec_id, audio->source);
  200. break;
  201. case AUDDEV_EVT_STREAM_VOL_CHG:
  202. audio->vol_pan.volume = evt_payload->session_vol;
  203. MM_DBG("AUDDEV_EVT_STREAM_VOL_CHG, stream vol %d\n",
  204. audio->vol_pan.volume);
  205. if (audio->running)
  206. audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
  207. POPP);
  208. break;
  209. default:
  210. MM_ERR("ERROR:wrong event\n");
  211. break;
  212. }
  213. }
  214. /* must be called with audio->lock held */
  215. static int audamrwb_disable(struct audio *audio)
  216. {
  217. int rc = 0;
  218. MM_DBG("\n"); /* Macro prints the file name and function */
  219. if (audio->enabled) {
  220. audio->enabled = 0;
  221. audio->dec_state = MSM_AUD_DECODER_STATE_NONE;
  222. auddec_dsp_config(audio, 0);
  223. rc = wait_event_interruptible_timeout(audio->wait,
  224. audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
  225. msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
  226. if (rc == 0)
  227. rc = -ETIMEDOUT;
  228. else if (audio->dec_state != MSM_AUD_DECODER_STATE_CLOSE)
  229. rc = -EFAULT;
  230. else
  231. rc = 0;
  232. wake_up(&audio->write_wait);
  233. wake_up(&audio->read_wait);
  234. msm_adsp_disable(audio->audplay);
  235. audpp_disable(audio->dec_id, audio);
  236. audio->out_needed = 0;
  237. }
  238. return rc;
  239. }
  240. /* ------------------- dsp --------------------- */
  241. static void audamrwb_update_pcm_buf_entry(struct audio *audio,
  242. uint32_t *payload)
  243. {
  244. uint8_t index;
  245. unsigned long flags;
  246. if (audio->rflush)
  247. return;
  248. spin_lock_irqsave(&audio->dsp_lock, flags);
  249. for (index = 0; index < payload[1]; index++) {
  250. if (audio->in[audio->fill_next].addr ==
  251. payload[2 + index * 2]) {
  252. MM_DBG("audamrwb_update_pcm_buf_entry: \
  253. in[%d] ready\n", audio->fill_next);
  254. audio->in[audio->fill_next].used =
  255. payload[3 + index * 2];
  256. if ((++audio->fill_next) == audio->pcm_buf_count)
  257. audio->fill_next = 0;
  258. } else {
  259. MM_ERR("expected=%x ret=%x\n",
  260. audio->in[audio->fill_next].addr,
  261. payload[1 + index * 2]);
  262. break;
  263. }
  264. }
  265. if (audio->in[audio->fill_next].used == 0) {
  266. audamrwb_buffer_refresh(audio);
  267. } else {
  268. MM_DBG("audamrwb_update_pcm_buf_entry: \
  269. read cannot keep up\n");
  270. audio->buf_refresh = 1;
  271. }
  272. wake_up(&audio->read_wait);
  273. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  274. }
  275. static void audplay_dsp_event(void *data, unsigned id, size_t len,
  276. void (*getevent) (void *ptr, size_t len))
  277. {
  278. struct audio *audio = data;
  279. uint32_t msg[28];
  280. getevent(msg, sizeof(msg));
  281. MM_DBG("audplay_dsp_event: msg_id=%x\n", id);
  282. switch (id) {
  283. case AUDPLAY_MSG_DEC_NEEDS_DATA:
  284. audamrwb_send_data(audio, 1);
  285. break;
  286. case AUDPLAY_MSG_BUFFER_UPDATE:
  287. audamrwb_update_pcm_buf_entry(audio, msg);
  288. break;
  289. default:
  290. MM_DBG("unexpected message from decoder\n");
  291. }
  292. }
  293. static void audamrwb_dsp_event(void *private, unsigned id, uint16_t *msg)
  294. {
  295. struct audio *audio = private;
  296. switch (id) {
  297. case AUDPP_MSG_STATUS_MSG:{
  298. unsigned status = msg[1];
  299. switch (status) {
  300. case AUDPP_DEC_STATUS_SLEEP: {
  301. uint16_t reason = msg[2];
  302. MM_DBG("decoder status:sleep reason=0x%04x\n",
  303. reason);
  304. if ((reason == AUDPP_MSG_REASON_MEM)
  305. || (reason ==
  306. AUDPP_MSG_REASON_NODECODER)) {
  307. audio->dec_state =
  308. MSM_AUD_DECODER_STATE_FAILURE;
  309. wake_up(&audio->wait);
  310. } else if (reason == AUDPP_MSG_REASON_NONE) {
  311. /* decoder is in disable state */
  312. audio->dec_state =
  313. MSM_AUD_DECODER_STATE_CLOSE;
  314. wake_up(&audio->wait);
  315. }
  316. break;
  317. }
  318. case AUDPP_DEC_STATUS_INIT:
  319. MM_DBG("decoder status: init\n");
  320. if (audio->pcm_feedback)
  321. audpp_cmd_cfg_routing_mode(audio);
  322. else
  323. audpp_cmd_cfg_adec_params(audio);
  324. break;
  325. case AUDPP_DEC_STATUS_CFG:
  326. MM_DBG("decoder status: cfg\n");
  327. break;
  328. case AUDPP_DEC_STATUS_PLAY:
  329. MM_DBG("decoder status: play\n");
  330. /* send mixer command */
  331. audpp_route_stream(audio->dec_id,
  332. audio->source);
  333. if (audio->pcm_feedback) {
  334. audamrwb_config_hostpcm(audio);
  335. audamrwb_buffer_refresh(audio);
  336. }
  337. audio->dec_state =
  338. MSM_AUD_DECODER_STATE_SUCCESS;
  339. wake_up(&audio->wait);
  340. break;
  341. default:
  342. MM_DBG("unknown decoder status\n");
  343. break;
  344. }
  345. break;
  346. }
  347. case AUDPP_MSG_CFG_MSG:
  348. if (msg[0] == AUDPP_MSG_ENA_ENA) {
  349. MM_DBG("CFG_MSG ENABLE\n");
  350. auddec_dsp_config(audio, 1);
  351. audio->out_needed = 0;
  352. audio->running = 1;
  353. audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
  354. POPP);
  355. audpp_dsp_set_eq(audio->dec_id, audio->eq_enable,
  356. &audio->eq, POPP);
  357. } else if (msg[0] == AUDPP_MSG_ENA_DIS) {
  358. MM_DBG("CFG_MSG DISABLE\n");
  359. audio->running = 0;
  360. } else {
  361. MM_DBG("CFG_MSG %d?\n", msg[0]);
  362. }
  363. break;
  364. case AUDPP_MSG_ROUTING_ACK:
  365. MM_DBG("ROUTING_ACK mode=%d\n", msg[1]);
  366. audpp_cmd_cfg_adec_params(audio);
  367. break;
  368. case AUDPP_MSG_FLUSH_ACK:
  369. MM_DBG("FLUSH_ACK\n");
  370. audio->wflush = 0;
  371. audio->rflush = 0;
  372. wake_up(&audio->write_wait);
  373. if (audio->pcm_feedback)
  374. audamrwb_buffer_refresh(audio);
  375. break;
  376. case AUDPP_MSG_PCMDMAMISSED:
  377. MM_DBG("PCMDMAMISSED\n");
  378. audio->teos = 1;
  379. wake_up(&audio->write_wait);
  380. break;
  381. case AUDPP_MSG_AVSYNC_MSG:
  382. MM_DBG("AUDPP_MSG_AVSYNC_MSG\n");
  383. memcpy(&audio->avsync[0], msg, sizeof(audio->avsync));
  384. audio->avsync_flag = 1;
  385. wake_up(&audio->avsync_wait);
  386. break;
  387. default:
  388. MM_DBG("UNKNOWN (%d)\n", id);
  389. }
  390. }
  391. struct msm_adsp_ops audplay_adsp_ops_amrwb = {
  392. .event = audplay_dsp_event,
  393. };
  394. #define audplay_send_queue0(audio, cmd, len) \
  395. msm_adsp_write(audio->audplay, audio->queue_id, \
  396. cmd, len)
  397. static int auddec_dsp_config(struct audio *audio, int enable)
  398. {
  399. struct audpp_cmd_cfg_dec_type cfg_dec_cmd;
  400. memset(&cfg_dec_cmd, 0, sizeof(cfg_dec_cmd));
  401. cfg_dec_cmd.cmd_id = AUDPP_CMD_CFG_DEC_TYPE;
  402. if (enable)
  403. cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
  404. AUDPP_CMD_ENA_DEC_V | AUDDEC_DEC_AMRWB;
  405. else
  406. cfg_dec_cmd.dec_cfg = AUDPP_CMD_UPDATDE_CFG_DEC |
  407. AUDPP_CMD_DIS_DEC_V;
  408. cfg_dec_cmd.dm_mode = 0x0;
  409. cfg_dec_cmd.stream_id = audio->dec_id;
  410. return audpp_send_queue1(&cfg_dec_cmd, sizeof(cfg_dec_cmd));
  411. }
  412. static void audpp_cmd_cfg_adec_params(struct audio *audio)
  413. {
  414. struct audpp_cmd_cfg_adec_params_amrwb cmd;
  415. memset(&cmd, 0, sizeof(cmd));
  416. cmd.common.cmd_id = AUDPP_CMD_CFG_ADEC_PARAMS;
  417. cmd.common.length = AUDPP_CMD_CFG_ADEC_PARAMS_AMRWB_LEN;
  418. cmd.common.dec_id = audio->dec_id;
  419. cmd.common.input_sampling_frequency = audio->out_sample_rate;
  420. cmd.stereo_cfg = audio->out_channel_mode;
  421. audpp_send_queue2(&cmd, sizeof(cmd));
  422. }
  423. static void audpp_cmd_cfg_routing_mode(struct audio *audio)
  424. {
  425. struct audpp_cmd_routing_mode cmd;
  426. MM_DBG("\n"); /* Macro prints the file name and function */
  427. memset(&cmd, 0, sizeof(cmd));
  428. cmd.cmd_id = AUDPP_CMD_ROUTING_MODE;
  429. cmd.object_number = audio->dec_id;
  430. if (audio->pcm_feedback)
  431. cmd.routing_mode = ROUTING_MODE_FTRT;
  432. else
  433. cmd.routing_mode = ROUTING_MODE_RT;
  434. audpp_send_queue1(&cmd, sizeof(cmd));
  435. }
  436. static int audplay_dsp_send_data_avail(struct audio *audio,
  437. unsigned idx, unsigned len)
  438. {
  439. struct audplay_cmd_bitstream_data_avail_nt2 cmd;
  440. cmd.cmd_id = AUDPLAY_CMD_BITSTREAM_DATA_AVAIL_NT2;
  441. if (audio->mfield)
  442. cmd.decoder_id = AUDAMRWB_METAFIELD_MASK |
  443. (audio->out[idx].mfield_sz >> 1);
  444. else
  445. cmd.decoder_id = audio->dec_id;
  446. cmd.buf_ptr = audio->out[idx].addr;
  447. cmd.buf_size = len / 2;
  448. cmd.partition_number = 0;
  449. return audplay_send_queue0(audio, &cmd, sizeof(cmd));
  450. }
  451. static void audamrwb_buffer_refresh(struct audio *audio)
  452. {
  453. struct audplay_cmd_buffer_refresh refresh_cmd;
  454. refresh_cmd.cmd_id = AUDPLAY_CMD_BUFFER_REFRESH;
  455. refresh_cmd.num_buffers = 1;
  456. refresh_cmd.buf0_address = audio->in[audio->fill_next].addr;
  457. refresh_cmd.buf0_length = audio->in[audio->fill_next].size;
  458. refresh_cmd.buf_read_count = 0;
  459. MM_DBG("buf0_addr=%x buf0_len=%d\n", refresh_cmd.buf0_address,
  460. refresh_cmd.buf0_length);
  461. (void)audplay_send_queue0(audio, &refresh_cmd, sizeof(refresh_cmd));
  462. }
  463. static void audamrwb_config_hostpcm(struct audio *audio)
  464. {
  465. struct audplay_cmd_hpcm_buf_cfg cfg_cmd;
  466. MM_DBG("\n"); /* Macro prints the file name and function */
  467. cfg_cmd.cmd_id = AUDPLAY_CMD_HPCM_BUF_CFG;
  468. cfg_cmd.max_buffers = audio->pcm_buf_count;
  469. cfg_cmd.byte_swap = 0;
  470. cfg_cmd.hostpcm_config = (0x8000) | (0x4000);
  471. cfg_cmd.feedback_frequency = 1;
  472. cfg_cmd.partition_number = 0;
  473. (void)audplay_send_queue0(audio, &cfg_cmd, sizeof(cfg_cmd));
  474. }
  475. static void audamrwb_send_data(struct audio *audio, unsigned needed)
  476. {
  477. struct buffer *frame;
  478. unsigned long flags;
  479. spin_lock_irqsave(&audio->dsp_lock, flags);
  480. if (!audio->running)
  481. goto done;
  482. if (needed && !audio->wflush) {
  483. /* We were called from the callback because the DSP
  484. * requested more data. Note that the DSP does want
  485. * more data, and if a buffer was in-flight, mark it
  486. * as available (since the DSP must now be done with
  487. * it).
  488. */
  489. audio->out_needed = 1;
  490. frame = audio->out + audio->out_tail;
  491. if (frame->used == 0xffffffff) {
  492. frame->used = 0;
  493. audio->out_tail ^= 1;
  494. wake_up(&audio->write_wait);
  495. }
  496. }
  497. if (audio->out_needed) {
  498. /* If the DSP currently wants data and we have a
  499. * buffer available, we will send it and reset
  500. * the needed flag. We'll mark the buffer as in-flight
  501. * so that it won't be recycled until the next buffer
  502. * is requested
  503. */
  504. frame = audio->out + audio->out_tail;
  505. if (frame->used) {
  506. BUG_ON(frame->used == 0xffffffff);
  507. MM_DBG("frame %d busy\n", audio->out_tail);
  508. audplay_dsp_send_data_avail(audio, audio->out_tail,
  509. frame->used);
  510. frame->used = 0xffffffff;
  511. audio->out_needed = 0;
  512. }
  513. }
  514. done:
  515. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  516. }
  517. /* ------------------- device --------------------- */
  518. static void audamrwb_flush(struct audio *audio)
  519. {
  520. audio->out[0].used = 0;
  521. audio->out[1].used = 0;
  522. audio->out_head = 0;
  523. audio->out_tail = 0;
  524. audio->reserved = 0;
  525. audio->out_needed = 0;
  526. atomic_set(&audio->out_bytes, 0);
  527. }
  528. static void audamrwb_flush_pcm_buf(struct audio *audio)
  529. {
  530. uint8_t index;
  531. for (index = 0; index < PCM_BUF_MAX_COUNT; index++)
  532. audio->in[index].used = 0;
  533. audio->buf_refresh = 0;
  534. audio->read_next = 0;
  535. audio->fill_next = 0;
  536. }
  537. static void audamrwb_ioport_reset(struct audio *audio)
  538. {
  539. /* Make sure read/write thread are free from
  540. * sleep and knowing that system is not able
  541. * to process io request at the moment
  542. */
  543. wake_up(&audio->write_wait);
  544. mutex_lock(&audio->write_lock);
  545. audamrwb_flush(audio);
  546. mutex_unlock(&audio->write_lock);
  547. wake_up(&audio->read_wait);
  548. mutex_lock(&audio->read_lock);
  549. audamrwb_flush_pcm_buf(audio);
  550. mutex_unlock(&audio->read_lock);
  551. audio->avsync_flag = 1;
  552. wake_up(&audio->avsync_wait);
  553. }
  554. static int audamrwb_events_pending(struct audio *audio)
  555. {
  556. unsigned long flags;
  557. int empty;
  558. spin_lock_irqsave(&audio->event_queue_lock, flags);
  559. empty = !list_empty(&audio->event_queue);
  560. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  561. return empty || audio->event_abort;
  562. }
  563. static void audamrwb_reset_event_queue(struct audio *audio)
  564. {
  565. unsigned long flags;
  566. struct audamrwb_event *drv_evt;
  567. struct list_head *ptr, *next;
  568. spin_lock_irqsave(&audio->event_queue_lock, flags);
  569. list_for_each_safe(ptr, next, &audio->event_queue) {
  570. drv_evt = list_first_entry(&audio->event_queue,
  571. struct audamrwb_event, list);
  572. list_del(&drv_evt->list);
  573. kfree(drv_evt);
  574. }
  575. list_for_each_safe(ptr, next, &audio->free_event_queue) {
  576. drv_evt = list_first_entry(&audio->free_event_queue,
  577. struct audamrwb_event, list);
  578. list_del(&drv_evt->list);
  579. kfree(drv_evt);
  580. }
  581. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  582. return;
  583. }
  584. static long audamrwb_process_event_req(struct audio *audio, void __user *arg)
  585. {
  586. long rc;
  587. struct msm_audio_event usr_evt;
  588. struct audamrwb_event *drv_evt = NULL;
  589. int timeout;
  590. unsigned long flags;
  591. if (copy_from_user(&usr_evt, arg, sizeof(struct msm_audio_event)))
  592. return -EFAULT;
  593. timeout = (int) usr_evt.timeout_ms;
  594. if (timeout > 0) {
  595. rc = wait_event_interruptible_timeout(
  596. audio->event_wait, audamrwb_events_pending(audio),
  597. msecs_to_jiffies(timeout));
  598. if (rc == 0)
  599. return -ETIMEDOUT;
  600. } else {
  601. rc = wait_event_interruptible(
  602. audio->event_wait, audamrwb_events_pending(audio));
  603. }
  604. if (rc < 0)
  605. return rc;
  606. if (audio->event_abort) {
  607. audio->event_abort = 0;
  608. return -ENODEV;
  609. }
  610. rc = 0;
  611. spin_lock_irqsave(&audio->event_queue_lock, flags);
  612. if (!list_empty(&audio->event_queue)) {
  613. drv_evt = list_first_entry(&audio->event_queue,
  614. struct audamrwb_event, list);
  615. list_del(&drv_evt->list);
  616. }
  617. if (drv_evt) {
  618. usr_evt.event_type = drv_evt->event_type;
  619. usr_evt.event_payload = drv_evt->payload;
  620. list_add_tail(&drv_evt->list, &audio->free_event_queue);
  621. } else
  622. rc = -1;
  623. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  624. if (!rc && copy_to_user(arg, &usr_evt, sizeof(usr_evt)))
  625. rc = -EFAULT;
  626. return rc;
  627. }
  628. static int audio_enable_eq(struct audio *audio, int enable)
  629. {
  630. if (audio->eq_enable == enable && !audio->eq_needs_commit)
  631. return 0;
  632. audio->eq_enable = enable;
  633. if (audio->running) {
  634. audpp_dsp_set_eq(audio->dec_id, enable, &audio->eq, POPP);
  635. audio->eq_needs_commit = 0;
  636. }
  637. return 0;
  638. }
  639. static int audio_get_avsync_data(struct audio *audio,
  640. struct msm_audio_stats *stats)
  641. {
  642. int rc = -EINVAL;
  643. unsigned long flags;
  644. local_irq_save(flags);
  645. if (audio->dec_id == audio->avsync[0] && audio->avsync_flag) {
  646. /* av_sync sample count */
  647. stats->sample_count = (audio->avsync[2] << 16) |
  648. (audio->avsync[3]);
  649. /* av_sync byte_count */
  650. stats->byte_count = (audio->avsync[5] << 16) |
  651. (audio->avsync[6]);
  652. audio->avsync_flag = 0;
  653. rc = 0;
  654. }
  655. local_irq_restore(flags);
  656. return rc;
  657. }
  658. static long audamrwb_ioctl(struct file *file, unsigned int cmd,
  659. unsigned long arg)
  660. {
  661. struct audio *audio = file->private_data;
  662. int rc = -EINVAL;
  663. unsigned long flags = 0;
  664. uint16_t enable_mask;
  665. int enable;
  666. int prev_state;
  667. MM_DBG("cmd = %d\n", cmd);
  668. if (cmd == AUDIO_GET_STATS) {
  669. struct msm_audio_stats stats;
  670. audio->avsync_flag = 0;
  671. memset(&stats, 0, sizeof(stats));
  672. if (audpp_query_avsync(audio->dec_id) < 0)
  673. return rc;
  674. rc = wait_event_interruptible_timeout(audio->avsync_wait,
  675. (audio->avsync_flag == 1),
  676. msecs_to_jiffies(AUDPP_AVSYNC_EVENT_TIMEOUT));
  677. if (rc < 0)
  678. return rc;
  679. else if ((rc > 0) || ((rc == 0) && (audio->avsync_flag == 1))) {
  680. if (audio_get_avsync_data(audio, &stats) < 0)
  681. return rc;
  682. if (copy_to_user((void *)arg, &stats, sizeof(stats)))
  683. return -EFAULT;
  684. return 0;
  685. } else
  686. return -EAGAIN;
  687. }
  688. switch (cmd) {
  689. case AUDIO_ENABLE_AUDPP:
  690. if (copy_from_user(&enable_mask, (void *) arg,
  691. sizeof(enable_mask))) {
  692. rc = -EFAULT;
  693. break;
  694. }
  695. spin_lock_irqsave(&audio->dsp_lock, flags);
  696. enable = (enable_mask & EQ_ENABLE) ? 1 : 0;
  697. audio_enable_eq(audio, enable);
  698. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  699. rc = 0;
  700. break;
  701. case AUDIO_SET_VOLUME:
  702. spin_lock_irqsave(&audio->dsp_lock, flags);
  703. audio->vol_pan.volume = arg;
  704. if (audio->running)
  705. audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
  706. POPP);
  707. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  708. rc = 0;
  709. break;
  710. case AUDIO_SET_PAN:
  711. spin_lock_irqsave(&audio->dsp_lock, flags);
  712. audio->vol_pan.pan = arg;
  713. if (audio->running)
  714. audpp_dsp_set_vol_pan(audio->dec_id, &audio->vol_pan,
  715. POPP);
  716. spin_unlock_irqrestore(&audio->dsp_lock, flags);
  717. rc = 0;
  718. break;
  719. case AUDIO_SET_EQ:
  720. prev_state = audio->eq_enable;
  721. audio->eq_enable = 0;
  722. if (copy_from_user(&audio->eq.num_bands, (void *) arg,
  723. sizeof(audio->eq) -
  724. (AUDPP_CMD_CFG_OBJECT_PARAMS_COMMON_LEN + 2))) {
  725. rc = -EFAULT;
  726. break;
  727. }
  728. audio->eq_enable = prev_state;
  729. audio->eq_needs_commit = 1;
  730. rc = 0;
  731. break;
  732. }
  733. if (-EINVAL != rc)
  734. return rc;
  735. if (cmd == AUDIO_GET_EVENT) {
  736. MM_DBG("AUDIO_GET_EVENT\n");
  737. if (mutex_trylock(&audio->get_event_lock)) {
  738. rc = audamrwb_process_event_req(audio,
  739. (void __user *) arg);
  740. mutex_unlock(&audio->get_event_lock);
  741. } else
  742. rc = -EBUSY;
  743. return rc;
  744. }
  745. if (cmd == AUDIO_ABORT_GET_EVENT) {
  746. audio->event_abort = 1;
  747. wake_up(&audio->event_wait);
  748. return 0;
  749. }
  750. mutex_lock(&audio->lock);
  751. switch (cmd) {
  752. case AUDIO_START:
  753. MM_DBG("AUDIO_START\n");
  754. rc = audamrwb_enable(audio);
  755. if (!rc) {
  756. rc = wait_event_interruptible_timeout(audio->wait,
  757. audio->dec_state != MSM_AUD_DECODER_STATE_NONE,
  758. msecs_to_jiffies(MSM_AUD_DECODER_WAIT_MS));
  759. MM_INFO("dec_state %d rc = %d\n", audio->dec_state, rc);
  760. if (audio->dec_state != MSM_AUD_DECODER_STATE_SUCCESS)
  761. rc = -ENODEV;
  762. else
  763. rc = 0;
  764. }
  765. break;
  766. case AUDIO_STOP:
  767. MM_DBG("AUDIO_STOP\n");
  768. rc = audamrwb_disable(audio);
  769. audio->stopped = 1;
  770. audamrwb_ioport_reset(audio);
  771. audio->stopped = 0;
  772. break;
  773. case AUDIO_FLUSH:
  774. MM_DBG("AUDIO_FLUSH\n");
  775. audio->rflush = 1;
  776. audio->wflush = 1;
  777. audamrwb_ioport_reset(audio);
  778. if (audio->running) {
  779. audpp_flush(audio->dec_id);
  780. rc = wait_event_interruptible(audio->write_wait,
  781. !audio->wflush);
  782. if (rc < 0) {
  783. MM_ERR("AUDIO_FLUSH interrupted\n");
  784. rc = -EINTR;
  785. }
  786. } else {
  787. audio->rflush = 0;
  788. audio->wflush = 0;
  789. }
  790. break;
  791. case AUDIO_SET_CONFIG:{
  792. struct msm_audio_config config;
  793. if (copy_from_user
  794. (&config, (void *)arg, sizeof(config))) {
  795. rc = -EFAULT;
  796. break;
  797. }
  798. if (config.channel_count == 1)
  799. config.channel_count =
  800. AUDPP_CMD_PCM_INTF_MONO_V;
  801. else if (config.channel_count == 2)
  802. config.channel_count =
  803. AUDPP_CMD_PCM_INTF_STEREO_V;
  804. else
  805. rc = -EINVAL;
  806. audio->out_channel_mode = config.channel_count;
  807. audio->out_sample_rate = config.sample_rate;
  808. audio->mfield = config.meta_field;
  809. rc = 0;
  810. break;
  811. }
  812. case AUDIO_GET_CONFIG:{
  813. struct msm_audio_config config;
  814. config.buffer_size = BUFSZ;
  815. config.buffer_count = 2;
  816. config.sample_rate = audio->out_sample_rate;
  817. if (audio->out_channel_mode ==
  818. AUDPP_CMD_PCM_INTF_MONO_V)
  819. config.channel_count = 1;
  820. else
  821. config.channel_count = 2;
  822. config.meta_field = 0;
  823. config.unused[0] = 0;
  824. config.unused[1] = 0;
  825. config.unused[2] = 0;
  826. if (copy_to_user((void *)arg, &config,
  827. sizeof(config)))
  828. rc = -EFAULT;
  829. else
  830. rc = 0;
  831. break;
  832. }
  833. case AUDIO_GET_PCM_CONFIG:{
  834. struct msm_audio_pcm_config config;
  835. config.pcm_feedback = 0;
  836. config.buffer_count = PCM_BUF_MAX_COUNT;
  837. config.buffer_size = PCM_BUFSZ_MIN;
  838. if (copy_to_user((void *)arg, &config,
  839. sizeof(config)))
  840. rc = -EFAULT;
  841. else
  842. rc = 0;
  843. break;
  844. }
  845. case AUDIO_SET_PCM_CONFIG:{
  846. struct msm_audio_pcm_config config;
  847. if (copy_from_user
  848. (&config, (void *)arg, sizeof(config))) {
  849. rc = -EFAULT;
  850. break;
  851. }
  852. if ((config.buffer_count > PCM_BUF_MAX_COUNT) ||
  853. (config.buffer_count == 1))
  854. config.buffer_count = PCM_BUF_MAX_COUNT;
  855. if (config.buffer_size < PCM_BUFSZ_MIN)
  856. config.buffer_size = PCM_BUFSZ_MIN;
  857. /* Check if pcm feedback is required */
  858. if ((config.pcm_feedback) && (!audio->read_data)) {
  859. MM_DBG("allocate PCM buf %d\n", config.buffer_count *
  860. config.buffer_size);
  861. audio->read_phys = pmem_kalloc(
  862. config.buffer_size *
  863. config.buffer_count,
  864. PMEM_MEMTYPE_EBI1|
  865. PMEM_ALIGNMENT_4K);
  866. if (IS_ERR((void *)audio->read_phys)) {
  867. rc = -ENOMEM;
  868. break;
  869. }
  870. audio->read_data = ioremap(audio->read_phys,
  871. config.buffer_size *
  872. config.buffer_count);
  873. if (!audio->read_data) {
  874. MM_ERR("no mem for read buf\n");
  875. rc = -ENOMEM;
  876. pmem_kfree(audio->read_phys);
  877. } else {
  878. uint8_t index;
  879. uint32_t offset = 0;
  880. audio->pcm_feedback = 1;
  881. audio->buf_refresh = 0;
  882. audio->pcm_buf_count =
  883. config.buffer_count;
  884. audio->read_next = 0;
  885. audio->fill_next = 0;
  886. for (index = 0;
  887. index < config.buffer_count; index++) {
  888. audio->in[index].data =
  889. audio->read_data + offset;
  890. audio->in[index].addr =
  891. audio->read_phys + offset;
  892. audio->in[index].size =
  893. config.buffer_size;
  894. audio->in[index].used = 0;
  895. offset += config.buffer_size;
  896. }
  897. MM_DBG("read buf: phy addr 0x%08x \
  898. kernel addr 0x%08x\n",
  899. audio->read_phys,
  900. (int)audio->read_data);
  901. rc = 0;
  902. }
  903. } else {
  904. rc = 0;
  905. }
  906. break;
  907. }
  908. case AUDIO_GET_SESSION_ID:
  909. if (copy_to_user((void *) arg, &audio->dec_id,
  910. sizeof(unsigned short)))
  911. rc = -EFAULT;
  912. else
  913. rc = 0;
  914. break;
  915. default:
  916. rc = -EINVAL;
  917. }
  918. mutex_unlock(&audio->lock);
  919. return rc;
  920. }
  921. /* Only useful in tunnel-mode */
  922. static int audamrwb_fsync(struct file *file, struct dentry *dentry,
  923. int datasync)
  924. {
  925. struct audio *audio = file->private_data;
  926. struct buffer *frame;
  927. int rc = 0;
  928. MM_DBG("\n"); /* Macro prints the file name and function */
  929. if (!audio->running || audio->pcm_feedback) {
  930. rc = -EINVAL;
  931. goto done_nolock;
  932. }
  933. mutex_lock(&audio->write_lock);
  934. rc = wait_event_interruptible(audio->write_wait,
  935. (!audio->out[0].used &&
  936. !audio->out[1].used &&
  937. audio->out_needed) || audio->wflush);
  938. if (rc < 0)
  939. goto done;
  940. else if (audio->wflush) {
  941. rc = -EBUSY;
  942. goto done;
  943. }
  944. if (audio->reserved) {
  945. MM_DBG("send reserved byte\n");
  946. frame = audio->out + audio->out_tail;
  947. ((char *) frame->data)[0] = audio->rsv_byte;
  948. ((char *) frame->data)[1] = 0;
  949. frame->used = 2;
  950. audamrwb_send_data(audio, 0);
  951. rc = wait_event_interruptible(audio->write_wait,
  952. (!audio->out[0].used &&
  953. !audio->out[1].used &&
  954. audio->out_needed) || audio->wflush);
  955. if (rc < 0)
  956. goto done;
  957. else if (audio->wflush) {
  958. rc = -EBUSY;
  959. goto done;
  960. }
  961. }
  962. /* pcm dmamiss message is sent continously
  963. * when decoder is starved so no race
  964. * condition concern
  965. */
  966. audio->teos = 0;
  967. rc = wait_event_interruptible(audio->write_wait,
  968. audio->teos || audio->wflush);
  969. if (audio->wflush)
  970. rc = -EBUSY;
  971. done:
  972. mutex_unlock(&audio->write_lock);
  973. done_nolock:
  974. return rc;
  975. }
  976. static ssize_t audamrwb_read(struct file *file, char __user *buf, size_t count,
  977. loff_t *pos)
  978. {
  979. struct audio *audio = file->private_data;
  980. const char __user *start = buf;
  981. int rc = 0;
  982. if (!audio->pcm_feedback)
  983. return 0; /* PCM feedback is not enabled. Nothing to read */
  984. mutex_lock(&audio->read_lock);
  985. MM_DBG("count %d\n", count);
  986. while (count > 0) {
  987. rc = wait_event_interruptible(audio->read_wait,
  988. (audio->in[audio->read_next].used > 0) ||
  989. (audio->stopped) || (audio->rflush));
  990. if (rc < 0)
  991. break;
  992. if (audio->stopped || audio->rflush) {
  993. rc = -EBUSY;
  994. break;
  995. }
  996. if (count < audio->in[audio->read_next].used) {
  997. /* Read must happen in frame boundary. Since driver does
  998. * not know frame size, read count must be greater or
  999. * equal to size of PCM samples
  1000. */
  1001. MM_DBG("read stop - partial frame\n");
  1002. break;
  1003. } else {
  1004. MM_DBG("read from in[%d]\n", audio->read_next);
  1005. if (copy_to_user
  1006. (buf, audio->in[audio->read_next].data,
  1007. audio->in[audio->read_next].used)) {
  1008. MM_ERR("invalid addr %x\n", (unsigned int)buf);
  1009. rc = -EFAULT;
  1010. break;
  1011. }
  1012. count -= audio->in[audio->read_next].used;
  1013. buf += audio->in[audio->read_next].used;
  1014. audio->in[audio->read_next].used = 0;
  1015. if ((++audio->read_next) == audio->pcm_buf_count)
  1016. audio->read_next = 0;
  1017. break;
  1018. }
  1019. }
  1020. /* don't feed output buffer to HW decoder during flushing
  1021. * buffer refresh command will be sent once flush completes
  1022. * send buf refresh command here can confuse HW decoder
  1023. */
  1024. if (audio->buf_refresh && !audio->rflush) {
  1025. audio->buf_refresh = 0;
  1026. MM_DBG("kick start pcm feedback again\n");
  1027. audamrwb_buffer_refresh(audio);
  1028. }
  1029. mutex_unlock(&audio->read_lock);
  1030. if (buf > start)
  1031. rc = buf - start;
  1032. MM_DBG("read %d bytes\n", rc);
  1033. return rc;
  1034. }
  1035. static int audamrwb_process_eos(struct audio *audio,
  1036. const char __user *buf_start, unsigned short mfield_size)
  1037. {
  1038. struct buffer *frame;
  1039. char *buf_ptr;
  1040. int rc = 0;
  1041. MM_DBG("signal input EOS reserved=%d\n", audio->reserved);
  1042. if (audio->reserved) {
  1043. MM_DBG("Pass reserve byte\n");
  1044. frame = audio->out + audio->out_head;
  1045. buf_ptr = frame->data;
  1046. rc = wait_event_interruptible(audio->write_wait,
  1047. (frame->used == 0)
  1048. || (audio->stopped)
  1049. || (audio->wflush));
  1050. if (rc < 0)
  1051. goto done;
  1052. if (audio->stopped || audio->wflush) {
  1053. rc = -EBUSY;
  1054. goto done;
  1055. }
  1056. buf_ptr[0] = audio->rsv_byte;
  1057. buf_ptr[1] = 0;
  1058. audio->out_head ^= 1;
  1059. frame->mfield_sz = 0;
  1060. audio->reserved = 0;
  1061. frame->used = 2;
  1062. audamrwb_send_data(audio, 0);
  1063. }
  1064. MM_DBG("Now signal input EOS after reserved bytes %d %d %d\n",
  1065. audio->out[0].used, audio->out[1].used, audio->out_needed);
  1066. frame = audio->out + audio->out_head;
  1067. rc = wait_event_interruptible(audio->write_wait,
  1068. (audio->out_needed &&
  1069. audio->out[0].used == 0 &&
  1070. audio->out[1].used == 0)
  1071. || (audio->stopped)
  1072. || (audio->wflush));
  1073. if (rc < 0)
  1074. goto done;
  1075. if (audio->stopped || audio->wflush) {
  1076. rc = -EBUSY;
  1077. goto done;
  1078. }
  1079. if (copy_from_user(frame->data, buf_start, mfield_size)) {
  1080. rc = -EFAULT;
  1081. goto done;
  1082. }
  1083. frame->mfield_sz = mfield_size;
  1084. audio->out_head ^= 1;
  1085. frame->used = mfield_size;
  1086. audamrwb_send_data(audio, 0);
  1087. done:
  1088. return rc;
  1089. }
  1090. static ssize_t audamrwb_write(struct file *file, const char __user *buf,
  1091. size_t count, loff_t *pos)
  1092. {
  1093. struct audio *audio = file->private_data;
  1094. const char __user *start = buf;
  1095. struct buffer *frame;
  1096. size_t xfer;
  1097. char *cpy_ptr;
  1098. int rc = 0, eos_condition = AUDAMRWB_EOS_NONE;
  1099. unsigned short mfield_size = 0;
  1100. unsigned dsize;
  1101. MM_DBG("cnt=%d\n", count);
  1102. mutex_lock(&audio->write_lock);
  1103. while (count > 0) {
  1104. frame = audio->out + audio->out_head;
  1105. cpy_ptr = frame->data;
  1106. dsize = 0;
  1107. rc = wait_event_interruptible(audio->write_wait,
  1108. (frame->used == 0)
  1109. || (audio->stopped)
  1110. || (audio->wflush));
  1111. MM_DBG("buffer available\n");
  1112. if (rc < 0)
  1113. break;
  1114. if (audio->stopped || audio->wflush) {
  1115. rc = -EBUSY;
  1116. break;
  1117. }
  1118. if (audio->mfield) {
  1119. if (buf == start) {
  1120. /* Processing beginning of user buffer */
  1121. if (__get_user(mfield_size,
  1122. (unsigned short __user *) buf)) {
  1123. rc = -EFAULT;
  1124. break;
  1125. } else if (mfield_size > count) {
  1126. rc = -EINVAL;
  1127. break;
  1128. }
  1129. MM_DBG("mf offset_val %x\n", mfield_size);
  1130. if (copy_from_user(cpy_ptr, buf, mfield_size)) {
  1131. rc = -EFAULT;
  1132. break;
  1133. }
  1134. /* Check if EOS flag is set and buffer
  1135. * contains just meta field
  1136. */
  1137. if (cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &
  1138. AUDAMRWB_EOS_FLG_MASK) {
  1139. MM_DBG("eos set\n");
  1140. eos_condition = AUDAMRWB_EOS_SET;
  1141. if (mfield_size == count) {
  1142. buf += mfield_size;
  1143. break;
  1144. } else
  1145. cpy_ptr[AUDAMRWB_EOS_FLG_OFFSET] &=
  1146. ~AUDAMRWB_EOS_FLG_MASK;
  1147. }
  1148. cpy_ptr += mfield_size;
  1149. count -= mfield_size;
  1150. dsize += mfield_size;
  1151. buf += mfield_size;
  1152. } else {
  1153. mfield_size = 0;
  1154. MM_DBG("continuous buffer\n");
  1155. }
  1156. frame->mfield_sz = mfield_size;
  1157. }
  1158. if (audio->reserved) {
  1159. MM_DBG("append reserved byte %x\n", audio->rsv_byte);
  1160. *cpy_ptr = audio->rsv_byte;
  1161. xfer = (count > ((frame->size - mfield_size) - 1)) ?
  1162. ((frame->size - mfield_size) - 1) : count;
  1163. cpy_ptr++;
  1164. dsize += 1;
  1165. audio->reserved = 0;
  1166. } else
  1167. xfer = (count > (frame->size - mfield_size)) ?
  1168. (frame->size - mfield_size) : count;
  1169. if (copy_from_user(cpy_ptr, buf, xfer)) {
  1170. rc = -EFAULT;
  1171. break;
  1172. }
  1173. dsize += xfer;
  1174. if (dsize & 1) {
  1175. audio->rsv_byte = ((char *) frame->data)[dsize - 1];
  1176. MM_DBG("odd length buf reserve last byte %x\n",
  1177. audio->rsv_byte);
  1178. audio->reserved = 1;
  1179. dsize--;
  1180. }
  1181. count -= xfer;
  1182. buf += xfer;
  1183. if (dsize > 0) {
  1184. audio->out_head ^= 1;
  1185. frame->used = dsize;
  1186. audamrwb_send_data(audio, 0);
  1187. }
  1188. }
  1189. MM_DBG("eos_condition %x buf[0x%x] start[0x%x]\n", eos_condition,
  1190. (int) buf, (int) start);
  1191. if (eos_condition == AUDAMRWB_EOS_SET)
  1192. rc = audamrwb_process_eos(audio, start, mfield_size);
  1193. mutex_unlock(&audio->write_lock);
  1194. if (!rc) {
  1195. if (buf > start)
  1196. return buf - start;
  1197. }
  1198. return rc;
  1199. }
  1200. static int audamrwb_release(struct inode *inode, struct file *file)
  1201. {
  1202. struct audio *audio = file->private_data;
  1203. MM_INFO("audio instance 0x%08x freeing\n", (int)audio);
  1204. mutex_lock(&audio->lock);
  1205. auddev_unregister_evt_listner(AUDDEV_CLNT_DEC, audio->dec_id);
  1206. audamrwb_disable(audio);
  1207. audamrwb_flush(audio);
  1208. audamrwb_flush_pcm_buf(audio);
  1209. msm_adsp_put(audio->audplay);
  1210. audpp_adec_free(audio->dec_id);
  1211. #ifdef CONFIG_HAS_EARLYSUSPEND
  1212. unregister_early_suspend(&audio->suspend_ctl.node);
  1213. #endif
  1214. audio->event_abort = 1;
  1215. wake_up(&audio->event_wait);
  1216. audamrwb_reset_event_queue(audio);
  1217. iounmap(audio->data);
  1218. pmem_kfree(audio->phys);
  1219. if (audio->read_data) {
  1220. iounmap(audio->read_data);
  1221. pmem_kfree(audio->read_phys);
  1222. }
  1223. mutex_unlock(&audio->lock);
  1224. #ifdef CONFIG_DEBUG_FS
  1225. if (audio->dentry)
  1226. debugfs_remove(audio->dentry);
  1227. #endif
  1228. kfree(audio);
  1229. return 0;
  1230. }
  1231. static void audamrwb_post_event(struct audio *audio, int type,
  1232. union msm_audio_event_payload payload)
  1233. {
  1234. struct audamrwb_event *e_node = NULL;
  1235. unsigned long flags;
  1236. spin_lock_irqsave(&audio->event_queue_lock, flags);
  1237. if (!list_empty(&audio->free_event_queue)) {
  1238. e_node = list_first_entry(&audio->free_event_queue,
  1239. struct audamrwb_event, list);
  1240. list_del(&e_node->list);
  1241. } else {
  1242. e_node = kmalloc(sizeof(struct audamrwb_event), GFP_ATOMIC);
  1243. if (!e_node) {
  1244. MM_ERR("No mem to post event %d\n", type);
  1245. return;
  1246. }
  1247. }
  1248. e_node->event_type = type;
  1249. e_node->payload = payload;
  1250. list_add_tail(&e_node->list, &audio->event_queue);
  1251. spin_unlock_irqrestore(&audio->event_queue_lock, flags);
  1252. wake_up(&audio->event_wait);
  1253. }
  1254. #ifdef CONFIG_HAS_EARLYSUSPEND
  1255. static void audamrwb_suspend(struct early_suspend *h)
  1256. {
  1257. struct audamrwb_suspend_ctl *ctl =
  1258. container_of(h, struct audamrwb_suspend_ctl, node);
  1259. union msm_audio_event_payload payload;
  1260. MM_DBG("\n"); /* Macro prints the file name and function */
  1261. audamrwb_post_event(ctl->audio, AUDIO_EVENT_SUSPEND, payload);
  1262. }
  1263. static void audamrwb_resume(struct early_suspend *h)
  1264. {
  1265. struct audamrwb_suspend_ctl *ctl =
  1266. container_of(h, struct audamrwb_suspend_ctl, node);
  1267. union msm_audio_event_payload payload;
  1268. MM_DBG("\n"); /* Macro prints the file name and function */
  1269. audamrwb_post_event(ctl->audio, AUDIO_EVENT_RESUME, payload);
  1270. }
  1271. #endif
  1272. #ifdef CONFIG_DEBUG_FS
  1273. static ssize_t audamrwb_debug_open(struct inode *inode, struct file *file)
  1274. {
  1275. file->private_data = inode->i_private;
  1276. return 0;
  1277. }
  1278. static ssize_t audamrwb_debug_read(struct file *file, char __user *buf,
  1279. size_t count, loff_t *ppos)
  1280. {
  1281. const int debug_bufmax = 1024;
  1282. static char buffer[1024];
  1283. int n = 0, i;
  1284. struct audio *audio = file->private_data;
  1285. mutex_lock(&audio->lock);
  1286. n = scnprintf(buffer, debug_bufmax, "opened %d\n", audio->opened);
  1287. n += scnprintf(buffer + n, debug_bufmax - n,
  1288. "enabled %d\n", audio->enabled);
  1289. n += scnprintf(buffer + n, debug_bufmax - n,
  1290. "stopped %d\n", audio->stopped);
  1291. n += scnprintf(buffer + n, debug_bufmax - n,
  1292. "pcm_feedback %d\n", audio->pcm_feedback);
  1293. n += scnprintf(buffer + n, debug_bufmax - n,
  1294. "out_buf_sz %d\n", audio->out[0].size);
  1295. n += scnprintf(buffer + n, debug_bufmax - n,
  1296. "pcm_buf_count %d \n", audio->pcm_buf_count);
  1297. n += scnprintf(buffer + n, debug_bufmax - n,
  1298. "pcm_buf_sz %d \n", audio->in[0].size);
  1299. n += scnprintf(buffer + n, debug_bufmax - n,
  1300. "volume %x \n", audio->vol_pan.volume);
  1301. n += scnprintf(buffer + n, debug_bufmax - n,
  1302. "sample rate %d \n", audio->out_sample_rate);
  1303. n += scnprintf(buffer + n, debug_bufmax - n,
  1304. "channel mode %d \n", audio->out_channel_mode);
  1305. mutex_unlock(&audio->lock);
  1306. /* Following variables are only useful for debugging when
  1307. * when playback halts unexpectedly. Thus, no mutual exclusion
  1308. * enforced
  1309. */
  1310. n += scnprintf(buffer + n, debug_bufmax - n,
  1311. "wflush %d\n", audio->wflush);
  1312. n += scnprintf(buffer + n, debug_bufmax - n,
  1313. "rflush %d\n", audio->rflush);
  1314. n += scnprintf(buffer + n, debug_bufmax - n,
  1315. "running %d \n", audio->running);
  1316. n += scnprintf(buffer + n, debug_bufmax - n,
  1317. "dec state %d \n", audio->dec_state);
  1318. n += scnprintf(buffer + n, debug_bufmax - n,
  1319. "out_needed %d \n", audio->out_needed);
  1320. n += scnprintf(buffer + n, debug_bufmax - n,
  1321. "out_head %d \n", audio->out_head);
  1322. n += scnprintf(buffer + n, debug_bufmax - n,
  1323. "out_tail %d \n", audio->out_tail);
  1324. n += scnprintf(buffer + n, debug_bufmax - n,
  1325. "out[0].used %d \n", audio->out[0].used);
  1326. n += scnprintf(buffer + n, debug_bufmax - n,
  1327. "out[1].used %d \n", audio->out[1].used);
  1328. n += scnprintf(buffer + n, debug_bufmax - n,
  1329. "buffer_refresh %d \n", audio->buf_refresh);
  1330. n += scnprintf(buffer + n, debug_bufmax - n,
  1331. "read_next %d \n", audio->read_next);
  1332. n += scnprintf(buffer + n, debug_bufmax - n,
  1333. "fill_next %d \n", audio->fill_next);
  1334. for (i = 0; i < audio->pcm_buf_count; i++)
  1335. n += scnprintf(buffer + n, debug_bufmax - n,
  1336. "in[%d].used %d \n", i, audio->in[i].used);
  1337. buffer[n] = 0;
  1338. return simple_read_from_buffer(buf, count, ppos, buffer, n);
  1339. }
  1340. static const struct file_operations audamrwb_debug_fops = {
  1341. .read = audamrwb_debug_read,
  1342. .open = audamrwb_debug_open,
  1343. };
  1344. #endif
  1345. static int audamrwb_open(struct inode *inode, struct file *file)
  1346. {
  1347. struct audio *audio = NULL;
  1348. int rc, dec_attrb, decid, i;
  1349. struct audamrwb_event *e_node = NULL;
  1350. #ifdef CONFIG_DEBUG_FS
  1351. /* 4 bytes represents decoder number, 1 byte for terminate string */
  1352. char name[sizeof "msm_amrwb_" + 5];
  1353. #endif
  1354. /* Allocate Mem for audio instance */
  1355. audio = kzalloc(sizeof(struct audio), GFP_KERNEL);
  1356. if (!audio) {
  1357. MM_ERR("no memory to allocate audio instance\n");
  1358. rc = -ENOMEM;
  1359. goto done;
  1360. }
  1361. MM_INFO("audio instance 0x%08x created\n", (int)audio);
  1362. /* Allocate the decoder */
  1363. dec_attrb = AUDDEC_DEC_AMRWB;
  1364. if (file->f_mode & FMODE_READ)
  1365. dec_attrb |= MSM_AUD_MODE_NONTUNNEL;
  1366. else
  1367. dec_attrb |= MSM_AUD_MODE_TUNNEL;
  1368. decid = audpp_adec_alloc(dec_attrb, &audio->module_name,
  1369. &audio->queue_id);
  1370. if (decid < 0) {
  1371. MM_ERR("No free decoder available, freeing instance 0x%08x\n",
  1372. (int)audio);
  1373. rc = -ENODEV;
  1374. kfree(audio);
  1375. goto done;
  1376. }
  1377. audio->dec_id = decid & MSM_AUD_DECODER_MASK;
  1378. audio->phys = pmem_kalloc(DMASZ, PMEM_MEMTYPE_EBI1|PMEM_ALIGNMENT_4K);
  1379. if (IS_ERR((void *)audio->phys)) {
  1380. MM_ERR("could not allocate write buffers, freeing instance \
  1381. 0x%08x\n", (int)audio);
  1382. rc = -ENOMEM;
  1383. audpp_adec_free(audio->dec_id);
  1384. kfree(audio);
  1385. goto done;
  1386. } else {
  1387. audio->data = ioremap(audio->phys, DMASZ);
  1388. if (!audio->data) {
  1389. MM_ERR("could not allocate write buffers, freeing \
  1390. instance 0x%08x\n", (int)audio);
  1391. rc = -ENOMEM;
  1392. pmem_kfree(audio->phys);
  1393. audpp_adec_free(audio->dec_id);
  1394. kfree(audio);
  1395. goto done;
  1396. }
  1397. MM_DBG("write buf: phy addr 0x%08x kernel addr 0x%08x\n",
  1398. audio->phys, (int)audio->data);
  1399. }
  1400. rc = msm_adsp_get(audio->module_name, &audio->audplay,
  1401. &audplay_adsp_ops_amrwb, audio);
  1402. if (rc) {
  1403. MM_ERR("failed to get %s module freeing instance 0x%08x\n",
  1404. audio->module_name, (int)audio);
  1405. goto err;
  1406. }
  1407. mutex_init(&audio->lock);
  1408. mutex_init(&audio->write_lock);
  1409. mutex_init(&audio->read_lock);
  1410. mutex_init(&audio->get_event_lock);
  1411. spin_lock_init(&audio->dsp_lock);
  1412. spin_lock_init(&audio->event_queue_lock);
  1413. INIT_LIST_HEAD(&audio->free_event_queue);
  1414. INIT_LIST_HEAD(&audio->event_queue);
  1415. init_waitqueue_head(&audio->write_wait);
  1416. init_waitqueue_head(&audio->read_wait);
  1417. init_waitqueue_head(&audio->wait);
  1418. init_waitqueue_head(&audio->event_wait);
  1419. init_waitqueue_head(&audio->avsync_wait);
  1420. audio->out[0].data = audio->data + 0;
  1421. audio->out[0].addr = audio->phys + 0;
  1422. audio->out[0].size = BUFSZ;
  1423. audio->out[1].data = audio->data + BUFSZ;
  1424. audio->out[1].addr = audio->phys + BUFSZ;
  1425. audio->out[1].size = BUFSZ;
  1426. audio->vol_pan.volume = 0x2000;
  1427. audio->vol_pan.pan = 0x0;
  1428. audio->eq_enable = 0;
  1429. audio->out_sample_rate = 44100;
  1430. audio->out_channel_mode = AUDPP_CMD_PCM_INTF_STEREO_V;
  1431. audamrwb_flush(audio);
  1432. file->private_data = audio;
  1433. audio->opened = 1;
  1434. audio->event_abort = 0;
  1435. audio->device_events = AUDDEV_EVT_DEV_RDY
  1436. |AUDDEV_EVT_DEV_RLS|
  1437. AUDDEV_EVT_STREAM_VOL_CHG;
  1438. rc = auddev_register_evt_listner(audio->device_events,
  1439. AUDDEV_CLNT_DEC,
  1440. audio->dec_id,
  1441. amrwb_listner,
  1442. (void *)audio);
  1443. if (rc) {
  1444. MM_ERR("failed to register listner\n");
  1445. goto event_err;
  1446. }
  1447. #ifdef CONFIG_DEBUG_FS
  1448. snprintf(name, sizeof name, "msm_amrwb_%04x", audio->dec_id);
  1449. audio->dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
  1450. NULL, (void *) audio, &audamrwb_debug_fops);
  1451. if (IS_ERR(audio->dentry))
  1452. MM_DBG("debugfs_create_file failed\n");
  1453. #endif
  1454. #ifdef CONFIG_HAS_EARLYSUSPEND
  1455. audio->suspend_ctl.node.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
  1456. audio->suspend_ctl.node.resume = audamrwb_resume;
  1457. audio->suspend_ctl.node.suspend = audamrwb_suspend;
  1458. audio->suspend_ctl.audio = audio;
  1459. register_early_suspend(&audio->suspend_ctl.node);
  1460. #endif
  1461. for (i = 0; i < AUDAMRWB_EVENT_NUM; i++) {
  1462. e_node = kmalloc(sizeof(struct audamrwb_event), GFP_KERNEL);
  1463. if (e_node)
  1464. list_add_tail(&e_node->list, &audio->free_event_queue);
  1465. else {
  1466. MM_ERR("event pkt alloc failed\n");
  1467. break;
  1468. }
  1469. }
  1470. done:
  1471. return rc;
  1472. event_err:
  1473. msm_adsp_put(audio->audplay);
  1474. err:
  1475. iounmap(audio->data);
  1476. pmem_kfree(audio->phys);
  1477. audpp_adec_free(audio->dec_id);
  1478. kfree(audio);
  1479. return rc;
  1480. }
  1481. static const struct file_operations audio_amrwb_fops = {
  1482. .owner = THIS_MODULE,
  1483. .open = audamrwb_open,
  1484. .release = audamrwb_release,
  1485. .read = audamrwb_read,
  1486. .write = audamrwb_write,
  1487. .unlocked_ioctl = audamrwb_ioctl,
  1488. .fsync = audamrwb_fsync,
  1489. };
  1490. struct miscdevice audio_amrwb_misc = {
  1491. .minor = MISC_DYNAMIC_MINOR,
  1492. .name = "msm_amrwb",
  1493. .fops = &audio_amrwb_fops,
  1494. };
  1495. static int __init audamrwb_init(void)
  1496. {
  1497. return misc_register(&audio_amrwb_misc);
  1498. }
  1499. static void __exit audamrwb_exit(void)
  1500. {
  1501. misc_deregister(&audio_amrwb_misc);
  1502. }
  1503. module_init(audamrwb_init);
  1504. module_exit(audamrwb_exit);
  1505. MODULE_DESCRIPTION("MSM AMR-WB driver");
  1506. MODULE_LICENSE("GPL v2");